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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
| 20 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 21 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 23 | #include "base/utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "class-inl.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 25 | #include "class_root-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 26 | #include "dex/dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | #include "gc/accounting/card_table-inl.h" |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 28 | #include "obj_ptr-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "object_array-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 31 | #include "object_array.h" |
Vladimir Marko | bdc93b4 | 2019-03-29 16:12:04 +0000 | [diff] [blame] | 32 | #include "stack_trace_element-inl.h" |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 33 | #include "string.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "well_known_classes.h" |
| 35 | |
| 36 | namespace art { |
| 37 | namespace mirror { |
| 38 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 39 | using android::base::StringPrintf; |
| 40 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 41 | void Throwable::SetDetailMessage(ObjPtr<String> new_detail_message) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 42 | if (Runtime::Current()->IsActiveTransaction()) { |
| 43 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), new_detail_message); |
| 44 | } else { |
| 45 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), |
| 46 | new_detail_message); |
| 47 | } |
| 48 | } |
| 49 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 50 | void Throwable::SetCause(ObjPtr<Throwable> cause) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 51 | CHECK(cause != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | CHECK(cause != this); |
Vladimir Marko | bdc93b4 | 2019-03-29 16:12:04 +0000 | [diff] [blame] | 53 | ObjPtr<Throwable> current_cause = |
| 54 | GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 55 | CHECK(current_cause == nullptr || current_cause == this); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 56 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 57 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 58 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 59 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 60 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 63 | void Throwable::SetStackState(ObjPtr<Object> state) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 64 | CHECK(state != nullptr); |
| 65 | if (Runtime::Current()->IsActiveTransaction()) { |
Piotr Jastrzebski | c16a50f | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 66 | SetFieldObjectVolatile<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 67 | } else { |
Piotr Jastrzebski | c16a50f | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 68 | SetFieldObjectVolatile<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 72 | bool Throwable::IsCheckedException() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) { |
| 74 | return false; |
| 75 | } |
| 76 | return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException)); |
| 77 | } |
| 78 | |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 79 | bool Throwable::IsError() { |
| 80 | return InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error)); |
| 81 | } |
| 82 | |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 83 | int32_t Throwable::GetStackDepth() { |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 84 | const ObjPtr<Object> stack_state = GetStackState(); |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 85 | if (stack_state == nullptr || !stack_state->IsObjectArray()) { |
Sebastien Hertz | 415fd08 | 2015-06-01 11:42:27 +0200 | [diff] [blame] | 86 | return -1; |
| 87 | } |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 88 | const ObjPtr<mirror::ObjectArray<Object>> trace = stack_state->AsObjectArray<Object>(); |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 89 | const int32_t array_len = trace->GetLength(); |
| 90 | DCHECK_GT(array_len, 0); |
| 91 | // See method BuildInternalStackTraceVisitor::Init for the format. |
| 92 | return array_len - 1; |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 95 | std::string Throwable::Dump() { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 96 | std::string result(PrettyTypeOf()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 97 | result += ": "; |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 98 | ObjPtr<String> msg = GetDetailMessage(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 99 | if (msg != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | result += msg->ToModifiedUtf8(); |
| 101 | } |
| 102 | result += "\n"; |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 103 | ObjPtr<Object> stack_state = GetStackState(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | // check stack state isn't missing or corrupt |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 105 | if (stack_state != nullptr && stack_state->IsObjectArray()) { |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 106 | ObjPtr<ObjectArray<Object>> object_array = stack_state->AsObjectArray<Object>(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | // Decode the internal stack trace into the depth and method trace |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 108 | // See method BuildInternalStackTraceVisitor::Init for the format. |
| 109 | DCHECK_GT(object_array->GetLength(), 0); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 110 | ObjPtr<Object> methods_and_dex_pcs = object_array->Get(0); |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 111 | DCHECK(methods_and_dex_pcs->IsIntArray() || methods_and_dex_pcs->IsLongArray()); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 112 | ObjPtr<PointerArray> method_trace = ObjPtr<PointerArray>::DownCast(methods_and_dex_pcs); |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 113 | const int32_t array_len = method_trace->GetLength(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 114 | CHECK_EQ(array_len % 2, 0); |
| 115 | const auto depth = array_len / 2; |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 116 | if (depth == 0) { |
Andreas Gampe | cca340b | 2016-11-18 17:43:05 -0800 | [diff] [blame] | 117 | result += "(Throwable with empty stack trace)\n"; |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 118 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 119 | const PointerSize ptr_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 120 | for (int32_t i = 0; i < depth; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 121 | ArtMethod* method = method_trace->GetElementPtrSize<ArtMethod*>(i, ptr_size); |
| 122 | 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] | 123 | int32_t line_number = method->GetLineNumFromDexPC(dex_pc); |
| 124 | const char* source_file = method->GetDeclaringClassSourceFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 125 | result += StringPrintf(" at %s (%s:%d)\n", method->PrettyMethod(true).c_str(), |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 126 | source_file, line_number); |
| 127 | } |
| 128 | } |
| 129 | } else { |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 130 | ObjPtr<Object> stack_trace = GetStackTrace(); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 131 | if (stack_trace != nullptr && stack_trace->IsObjectArray()) { |
Vladimir Marko | adbceb7 | 2018-05-29 14:34:14 +0100 | [diff] [blame] | 132 | CHECK_EQ(stack_trace->GetClass()->GetComponentType(), GetClassRoot<StackTraceElement>()); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 133 | ObjPtr<ObjectArray<StackTraceElement>> ste_array = |
| 134 | ObjPtr<ObjectArray<StackTraceElement>>::DownCast(stack_trace); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 135 | if (ste_array->GetLength() == 0) { |
Andreas Gampe | cca340b | 2016-11-18 17:43:05 -0800 | [diff] [blame] | 136 | result += "(Throwable with empty stack trace)\n"; |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 137 | } else { |
| 138 | for (int32_t i = 0; i < ste_array->GetLength(); ++i) { |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 139 | ObjPtr<StackTraceElement> ste = ste_array->Get(i); |
Mathieu Chartier | 4a24858 | 2015-04-28 13:53:02 -0700 | [diff] [blame] | 140 | DCHECK(ste != nullptr); |
Vladimir Marko | bdc93b4 | 2019-03-29 16:12:04 +0000 | [diff] [blame] | 141 | ObjPtr<String> method_name = ste->GetMethodName(); |
| 142 | ObjPtr<String> file_name = ste->GetFileName(); |
Mathieu Chartier | 4a24858 | 2015-04-28 13:53:02 -0700 | [diff] [blame] | 143 | result += StringPrintf( |
| 144 | " at %s (%s:%d)\n", |
| 145 | method_name != nullptr ? method_name->ToModifiedUtf8().c_str() : "<unknown method>", |
| 146 | file_name != nullptr ? file_name->ToModifiedUtf8().c_str() : "(Unknown Source)", |
| 147 | ste->GetLineNumber()); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } else { |
Andreas Gampe | cca340b | 2016-11-18 17:43:05 -0800 | [diff] [blame] | 151 | result += "(Throwable with no stack trace)\n"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 154 | ObjPtr<Throwable> cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_)); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 155 | if (cause != nullptr && cause != this) { // Constructor makes cause == this by default. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | result += "Caused by: "; |
| 157 | result += cause->Dump(); |
| 158 | } |
| 159 | return result; |
| 160 | } |
| 161 | |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 162 | ObjPtr<Object> Throwable::GetStackState() { |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 163 | return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_)); |
| 164 | } |
| 165 | |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 166 | ObjPtr<Object> Throwable::GetStackTrace() { |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 167 | return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_)); |
| 168 | } |
| 169 | |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 170 | ObjPtr<String> Throwable::GetDetailMessage() { |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 171 | return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_)); |
| 172 | } |
| 173 | |
Nicolas Geoffray | f740be5e | 2021-09-16 14:07:37 +0100 | [diff] [blame] | 174 | ObjPtr<Throwable> Throwable::GetCause() { |
| 175 | return GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_)); |
| 176 | } |
| 177 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 178 | } // namespace mirror |
| 179 | } // namespace art |