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" |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 26 | #include "stack_trace_element.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "utils.h" |
| 28 | #include "well_known_classes.h" |
| 29 | |
| 30 | namespace art { |
| 31 | namespace mirror { |
| 32 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 33 | GcRoot<Class> Throwable::java_lang_Throwable_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 35 | void Throwable::SetDetailMessage(String* new_detail_message) { |
| 36 | if (Runtime::Current()->IsActiveTransaction()) { |
| 37 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), new_detail_message); |
| 38 | } else { |
| 39 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), |
| 40 | new_detail_message); |
| 41 | } |
| 42 | } |
| 43 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | void Throwable::SetCause(Throwable* cause) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 45 | CHECK(cause != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | CHECK(cause != this); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 47 | Throwable* current_cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 48 | CHECK(current_cause == nullptr || current_cause == this); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 49 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 50 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 51 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 52 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 53 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 56 | void Throwable::SetStackState(Object* state) SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 57 | CHECK(state != nullptr); |
| 58 | if (Runtime::Current()->IsActiveTransaction()) { |
| 59 | SetFieldObjectVolatile<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), state); |
| 60 | } else { |
| 61 | SetFieldObjectVolatile<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), state); |
| 62 | } |
| 63 | } |
| 64 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 65 | bool Throwable::IsCheckedException() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) { |
| 67 | return false; |
| 68 | } |
| 69 | return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException)); |
| 70 | } |
| 71 | |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 72 | int32_t Throwable::GetStackDepth() { |
| 73 | Object* stack_state = GetStackState(); |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame^] | 74 | if (stack_state == nullptr || !stack_state->IsObjectArray()) { |
Sebastien Hertz | 415fd08 | 2015-06-01 11:42:27 +0200 | [diff] [blame] | 75 | return -1; |
| 76 | } |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame^] | 77 | mirror::ObjectArray<mirror::Object>* const trace = stack_state->AsObjectArray<mirror::Object>(); |
| 78 | const int32_t array_len = trace->GetLength(); |
| 79 | DCHECK_GT(array_len, 0); |
| 80 | // See method BuildInternalStackTraceVisitor::Init for the format. |
| 81 | return array_len - 1; |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 84 | std::string Throwable::Dump() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | std::string result(PrettyTypeOf(this)); |
| 86 | result += ": "; |
| 87 | String* msg = GetDetailMessage(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 88 | if (msg != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 89 | result += msg->ToModifiedUtf8(); |
| 90 | } |
| 91 | result += "\n"; |
| 92 | Object* stack_state = GetStackState(); |
| 93 | // check stack state isn't missing or corrupt |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame^] | 94 | if (stack_state != nullptr && stack_state->IsObjectArray()) { |
| 95 | mirror::ObjectArray<mirror::Object>* object_array = |
| 96 | stack_state->AsObjectArray<mirror::Object>(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 97 | // Decode the internal stack trace into the depth and method trace |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame^] | 98 | // See method BuildInternalStackTraceVisitor::Init for the format. |
| 99 | DCHECK_GT(object_array->GetLength(), 0); |
| 100 | mirror::Object* methods_and_dex_pcs = object_array->Get(0); |
| 101 | DCHECK(methods_and_dex_pcs->IsIntArray() || methods_and_dex_pcs->IsLongArray()); |
| 102 | mirror::PointerArray* method_trace = down_cast<mirror::PointerArray*>(methods_and_dex_pcs); |
| 103 | const int32_t array_len = method_trace->GetLength(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 104 | CHECK_EQ(array_len % 2, 0); |
| 105 | const auto depth = array_len / 2; |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 106 | if (depth == 0) { |
| 107 | result += "(Throwable with empty stack trace)"; |
| 108 | } else { |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame^] | 109 | const size_t ptr_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 110 | for (int32_t i = 0; i < depth; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 111 | ArtMethod* method = method_trace->GetElementPtrSize<ArtMethod*>(i, ptr_size); |
| 112 | uintptr_t dex_pc = method_trace->GetElementPtrSize<uintptr_t>(i + depth, ptr_size); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 113 | int32_t line_number = method->GetLineNumFromDexPC(dex_pc); |
| 114 | const char* source_file = method->GetDeclaringClassSourceFile(); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 115 | result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(), |
| 116 | source_file, line_number); |
| 117 | } |
| 118 | } |
| 119 | } else { |
| 120 | Object* stack_trace = GetStackTrace(); |
| 121 | if (stack_trace != nullptr && stack_trace->IsObjectArray()) { |
| 122 | CHECK_EQ(stack_trace->GetClass()->GetComponentType(), |
| 123 | StackTraceElement::GetStackTraceElement()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 124 | auto* ste_array = down_cast<ObjectArray<StackTraceElement>*>(stack_trace); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 125 | if (ste_array->GetLength() == 0) { |
| 126 | result += "(Throwable with empty stack trace)"; |
| 127 | } else { |
| 128 | for (int32_t i = 0; i < ste_array->GetLength(); ++i) { |
| 129 | StackTraceElement* ste = ste_array->Get(i); |
Mathieu Chartier | 4a24858 | 2015-04-28 13:53:02 -0700 | [diff] [blame] | 130 | DCHECK(ste != nullptr); |
| 131 | auto* method_name = ste->GetMethodName(); |
| 132 | auto* file_name = ste->GetFileName(); |
| 133 | result += StringPrintf( |
| 134 | " at %s (%s:%d)\n", |
| 135 | method_name != nullptr ? method_name->ToModifiedUtf8().c_str() : "<unknown method>", |
| 136 | file_name != nullptr ? file_name->ToModifiedUtf8().c_str() : "(Unknown Source)", |
| 137 | ste->GetLineNumber()); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } else { |
| 141 | result += "(Throwable with no stack trace)"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 144 | Throwable* cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_)); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 145 | if (cause != nullptr && cause != this) { // Constructor makes cause == this by default. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 146 | result += "Caused by: "; |
| 147 | result += cause->Dump(); |
| 148 | } |
| 149 | return result; |
| 150 | } |
| 151 | |
| 152 | void Throwable::SetClass(Class* java_lang_Throwable) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 153 | CHECK(java_lang_Throwable_.IsNull()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 154 | CHECK(java_lang_Throwable != nullptr); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 155 | java_lang_Throwable_ = GcRoot<Class>(java_lang_Throwable); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void Throwable::ResetClass() { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 159 | CHECK(!java_lang_Throwable_.IsNull()); |
| 160 | java_lang_Throwable_ = GcRoot<Class>(nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 163 | void Throwable::VisitRoots(RootVisitor* visitor) { |
| 164 | java_lang_Throwable_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 167 | } // namespace mirror |
| 168 | } // namespace art |