Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "throwable.h" |
| 18 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include "class-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 21 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 22 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "object-inl.h" |
| 24 | #include "object_array.h" |
| 25 | #include "object_array-inl.h" |
| 26 | #include "object_utils.h" |
| 27 | #include "utils.h" |
| 28 | #include "well_known_classes.h" |
| 29 | |
| 30 | namespace art { |
| 31 | namespace mirror { |
| 32 | |
| 33 | Class* Throwable::java_lang_Throwable_ = NULL; |
| 34 | |
| 35 | void Throwable::SetCause(Throwable* cause) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 36 | CHECK(cause != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | CHECK(cause != this); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 38 | Throwable* current_cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), |
| 39 | false); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 40 | CHECK(current_cause == NULL || current_cause == this); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 41 | if (Runtime::Current()->IsActiveTransaction()) { |
| 42 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false); |
| 43 | } else { |
| 44 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false); |
| 45 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 48 | bool Throwable::IsCheckedException() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) { |
| 50 | return false; |
| 51 | } |
| 52 | return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException)); |
| 53 | } |
| 54 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 55 | std::string Throwable::Dump() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | std::string result(PrettyTypeOf(this)); |
| 57 | result += ": "; |
| 58 | String* msg = GetDetailMessage(); |
| 59 | if (msg != NULL) { |
| 60 | result += msg->ToModifiedUtf8(); |
| 61 | } |
| 62 | result += "\n"; |
| 63 | Object* stack_state = GetStackState(); |
| 64 | // check stack state isn't missing or corrupt |
| 65 | if (stack_state != NULL && stack_state->IsObjectArray()) { |
| 66 | // Decode the internal stack trace into the depth and method trace |
| 67 | ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state); |
| 68 | int32_t depth = method_trace->GetLength() - 1; |
| 69 | IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 70 | MethodHelper mh; |
| 71 | for (int32_t i = 0; i < depth; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 72 | ArtMethod* method = down_cast<ArtMethod*>(method_trace->Get(i)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | mh.ChangeMethod(method); |
| 74 | uint32_t dex_pc = pc_trace->Get(i); |
| 75 | int32_t line_number = mh.GetLineNumFromDexPC(dex_pc); |
| 76 | const char* source_file = mh.GetDeclaringClassSourceFile(); |
| 77 | result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(), |
| 78 | source_file, line_number); |
| 79 | } |
| 80 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 81 | Throwable* cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 82 | if (cause != NULL && cause != this) { // Constructor makes cause == this by default. |
| 83 | result += "Caused by: "; |
| 84 | result += cause->Dump(); |
| 85 | } |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | void Throwable::SetClass(Class* java_lang_Throwable) { |
| 90 | CHECK(java_lang_Throwable_ == NULL); |
| 91 | CHECK(java_lang_Throwable != NULL); |
| 92 | java_lang_Throwable_ = java_lang_Throwable; |
| 93 | } |
| 94 | |
| 95 | void Throwable::ResetClass() { |
| 96 | CHECK(java_lang_Throwable_ != NULL); |
| 97 | java_lang_Throwable_ = NULL; |
| 98 | } |
| 99 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 100 | void Throwable::VisitRoots(RootCallback* callback, void* arg) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 101 | if (java_lang_Throwable_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 102 | callback(reinterpret_cast<mirror::Object**>(&java_lang_Throwable_), arg, 0, kRootStickyClass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | } // namespace mirror |
| 107 | } // namespace art |