blob: b538fa91251104bc12ec22f2dfb1a3ef95c680d7 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
David Sehrc431b9d2018-03-02 12:01:51 -080023#include "base/utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "class-inl.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010025#include "class_root-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/accounting/card_table-inl.h"
Vladimir Marko3b458902019-03-26 17:08:41 +000028#include "obj_ptr-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "object_array-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "object_array.h"
Vladimir Markobdc93b42019-03-29 16:12:04 +000032#include "stack_trace_element-inl.h"
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070033#include "string.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "well_known_classes.h"
35
36namespace art {
37namespace mirror {
38
Andreas Gampe46ee31b2016-12-14 10:11:49 -080039using android::base::StringPrintf;
40
Mathieu Chartier31e88222016-10-14 18:43:19 -070041void Throwable::SetDetailMessage(ObjPtr<String> new_detail_message) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070042 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 Chartier31e88222016-10-14 18:43:19 -070050void Throwable::SetCause(ObjPtr<Throwable> cause) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080051 CHECK(cause != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 CHECK(cause != this);
Vladimir Markobdc93b42019-03-29 16:12:04 +000053 ObjPtr<Throwable> current_cause =
54 GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070055 CHECK(current_cause == nullptr || current_cause == this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010056 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070057 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010058 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070059 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010060 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061}
62
Mathieu Chartier31e88222016-10-14 18:43:19 -070063void Throwable::SetStackState(ObjPtr<Object> state) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc45b8b52014-05-03 01:39:59 -070064 CHECK(state != nullptr);
65 if (Runtime::Current()->IsActiveTransaction()) {
Piotr Jastrzebskic16a50f2015-05-07 09:41:00 +010066 SetFieldObjectVolatile<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
Ian Rogersc45b8b52014-05-03 01:39:59 -070067 } else {
Piotr Jastrzebskic16a50f2015-05-07 09:41:00 +010068 SetFieldObjectVolatile<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
Ian Rogersc45b8b52014-05-03 01:39:59 -070069 }
70}
71
Ian Rogersef7d42f2014-01-06 12:55:46 -080072bool Throwable::IsCheckedException() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
74 return false;
75 }
76 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
77}
78
Orion Hodsona5dca522018-02-27 12:42:11 +000079bool Throwable::IsError() {
80 return InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error));
81}
82
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +000083int32_t Throwable::GetStackDepth() {
Vladimir Marko0984e482019-03-27 16:41:41 +000084 const ObjPtr<Object> stack_state = GetStackState();
Mathieu Chartier910e8272015-09-30 09:24:22 -070085 if (stack_state == nullptr || !stack_state->IsObjectArray()) {
Sebastien Hertz415fd082015-06-01 11:42:27 +020086 return -1;
87 }
Vladimir Marko0984e482019-03-27 16:41:41 +000088 const ObjPtr<mirror::ObjectArray<Object>> trace = stack_state->AsObjectArray<Object>();
Mathieu Chartier910e8272015-09-30 09:24:22 -070089 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 Geoffray7642cfc2015-02-26 10:56:09 +000093}
94
Ian Rogersef7d42f2014-01-06 12:55:46 -080095std::string Throwable::Dump() {
David Sehr709b0702016-10-13 09:12:37 -070096 std::string result(PrettyTypeOf());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 result += ": ";
Mathieu Chartier31e88222016-10-14 18:43:19 -070098 ObjPtr<String> msg = GetDetailMessage();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070099 if (msg != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 result += msg->ToModifiedUtf8();
101 }
102 result += "\n";
Mathieu Chartier31e88222016-10-14 18:43:19 -0700103 ObjPtr<Object> stack_state = GetStackState();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 // check stack state isn't missing or corrupt
Mathieu Chartier910e8272015-09-30 09:24:22 -0700105 if (stack_state != nullptr && stack_state->IsObjectArray()) {
Mathieu Chartier31e88222016-10-14 18:43:19 -0700106 ObjPtr<ObjectArray<Object>> object_array = stack_state->AsObjectArray<Object>();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 // Decode the internal stack trace into the depth and method trace
Mathieu Chartier910e8272015-09-30 09:24:22 -0700108 // See method BuildInternalStackTraceVisitor::Init for the format.
109 DCHECK_GT(object_array->GetLength(), 0);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700110 ObjPtr<Object> methods_and_dex_pcs = object_array->Get(0);
Mathieu Chartier910e8272015-09-30 09:24:22 -0700111 DCHECK(methods_and_dex_pcs->IsIntArray() || methods_and_dex_pcs->IsLongArray());
Mathieu Chartier31e88222016-10-14 18:43:19 -0700112 ObjPtr<PointerArray> method_trace = ObjPtr<PointerArray>::DownCast(methods_and_dex_pcs);
Mathieu Chartier910e8272015-09-30 09:24:22 -0700113 const int32_t array_len = method_trace->GetLength();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700114 CHECK_EQ(array_len % 2, 0);
115 const auto depth = array_len / 2;
Ian Rogersc45b8b52014-05-03 01:39:59 -0700116 if (depth == 0) {
Andreas Gampecca340b2016-11-18 17:43:05 -0800117 result += "(Throwable with empty stack trace)\n";
Ian Rogersc45b8b52014-05-03 01:39:59 -0700118 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700119 const PointerSize ptr_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersc45b8b52014-05-03 01:39:59 -0700120 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 ArtMethod* method = method_trace->GetElementPtrSize<ArtMethod*>(i, ptr_size);
122 uintptr_t dex_pc = method_trace->GetElementPtrSize<uintptr_t>(i + depth, ptr_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700123 int32_t line_number = method->GetLineNumFromDexPC(dex_pc);
124 const char* source_file = method->GetDeclaringClassSourceFile();
David Sehr709b0702016-10-13 09:12:37 -0700125 result += StringPrintf(" at %s (%s:%d)\n", method->PrettyMethod(true).c_str(),
Ian Rogersc45b8b52014-05-03 01:39:59 -0700126 source_file, line_number);
127 }
128 }
129 } else {
Mathieu Chartier31e88222016-10-14 18:43:19 -0700130 ObjPtr<Object> stack_trace = GetStackTrace();
Ian Rogersc45b8b52014-05-03 01:39:59 -0700131 if (stack_trace != nullptr && stack_trace->IsObjectArray()) {
Vladimir Markoadbceb72018-05-29 14:34:14 +0100132 CHECK_EQ(stack_trace->GetClass()->GetComponentType(), GetClassRoot<StackTraceElement>());
Mathieu Chartier31e88222016-10-14 18:43:19 -0700133 ObjPtr<ObjectArray<StackTraceElement>> ste_array =
134 ObjPtr<ObjectArray<StackTraceElement>>::DownCast(stack_trace);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700135 if (ste_array->GetLength() == 0) {
Andreas Gampecca340b2016-11-18 17:43:05 -0800136 result += "(Throwable with empty stack trace)\n";
Ian Rogersc45b8b52014-05-03 01:39:59 -0700137 } else {
138 for (int32_t i = 0; i < ste_array->GetLength(); ++i) {
Vladimir Marko423bebb2019-03-26 15:17:21 +0000139 ObjPtr<StackTraceElement> ste = ste_array->Get(i);
Mathieu Chartier4a248582015-04-28 13:53:02 -0700140 DCHECK(ste != nullptr);
Vladimir Markobdc93b42019-03-29 16:12:04 +0000141 ObjPtr<String> method_name = ste->GetMethodName();
142 ObjPtr<String> file_name = ste->GetFileName();
Mathieu Chartier4a248582015-04-28 13:53:02 -0700143 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 Rogersc45b8b52014-05-03 01:39:59 -0700148 }
149 }
150 } else {
Andreas Gampecca340b2016-11-18 17:43:05 -0800151 result += "(Throwable with no stack trace)\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 }
153 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700154 ObjPtr<Throwable> cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
Ian Rogersc45b8b52014-05-03 01:39:59 -0700155 if (cause != nullptr && cause != this) { // Constructor makes cause == this by default.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 result += "Caused by: ";
157 result += cause->Dump();
158 }
159 return result;
160}
161
Vladimir Marko3b458902019-03-26 17:08:41 +0000162ObjPtr<Object> Throwable::GetStackState() {
Andreas Gampe5d08fcc2017-06-05 17:56:46 -0700163 return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_));
164}
165
Vladimir Marko3b458902019-03-26 17:08:41 +0000166ObjPtr<Object> Throwable::GetStackTrace() {
Andreas Gampe5d08fcc2017-06-05 17:56:46 -0700167 return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_));
168}
169
Vladimir Marko3b458902019-03-26 17:08:41 +0000170ObjPtr<String> Throwable::GetDetailMessage() {
Andreas Gampe5d08fcc2017-06-05 17:56:46 -0700171 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_));
172}
173
Nicolas Geoffrayf740be5e2021-09-16 14:07:37 +0100174ObjPtr<Throwable> Throwable::GetCause() {
175 return GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
176}
177
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178} // namespace mirror
179} // namespace art