1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "throwable.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/utils.h"
24*795d594fSAndroid Build Coastguard Worker #include "class-inl.h"
25*795d594fSAndroid Build Coastguard Worker #include "class_root-inl.h"
26*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/card_table-inl.h"
28*795d594fSAndroid Build Coastguard Worker #include "obj_ptr-inl.h"
29*795d594fSAndroid Build Coastguard Worker #include "object-inl.h"
30*795d594fSAndroid Build Coastguard Worker #include "object_array-inl.h"
31*795d594fSAndroid Build Coastguard Worker #include "object_array.h"
32*795d594fSAndroid Build Coastguard Worker #include "stack_trace_element-inl.h"
33*795d594fSAndroid Build Coastguard Worker #include "string.h"
34*795d594fSAndroid Build Coastguard Worker #include "well_known_classes-inl.h"
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
37*795d594fSAndroid Build Coastguard Worker namespace mirror {
38*795d594fSAndroid Build Coastguard Worker
39*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
40*795d594fSAndroid Build Coastguard Worker
SetDetailMessage(ObjPtr<String> new_detail_message)41*795d594fSAndroid Build Coastguard Worker void Throwable::SetDetailMessage(ObjPtr<String> new_detail_message) {
42*795d594fSAndroid Build Coastguard Worker if (Runtime::Current()->IsActiveTransaction()) {
43*795d594fSAndroid Build Coastguard Worker SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), new_detail_message);
44*795d594fSAndroid Build Coastguard Worker } else {
45*795d594fSAndroid Build Coastguard Worker SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
46*795d594fSAndroid Build Coastguard Worker new_detail_message);
47*795d594fSAndroid Build Coastguard Worker }
48*795d594fSAndroid Build Coastguard Worker }
49*795d594fSAndroid Build Coastguard Worker
SetCause(ObjPtr<Throwable> cause)50*795d594fSAndroid Build Coastguard Worker void Throwable::SetCause(ObjPtr<Throwable> cause) {
51*795d594fSAndroid Build Coastguard Worker CHECK(cause != nullptr);
52*795d594fSAndroid Build Coastguard Worker CHECK(cause != this);
53*795d594fSAndroid Build Coastguard Worker ObjPtr<Throwable> current_cause =
54*795d594fSAndroid Build Coastguard Worker GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
55*795d594fSAndroid Build Coastguard Worker CHECK(current_cause == nullptr || current_cause == this);
56*795d594fSAndroid Build Coastguard Worker if (Runtime::Current()->IsActiveTransaction()) {
57*795d594fSAndroid Build Coastguard Worker SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
58*795d594fSAndroid Build Coastguard Worker } else {
59*795d594fSAndroid Build Coastguard Worker SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker
SetStackState(ObjPtr<Object> state)63*795d594fSAndroid Build Coastguard Worker void Throwable::SetStackState(ObjPtr<Object> state) REQUIRES_SHARED(Locks::mutator_lock_) {
64*795d594fSAndroid Build Coastguard Worker CHECK(state != nullptr);
65*795d594fSAndroid Build Coastguard Worker if (Runtime::Current()->IsActiveTransaction()) {
66*795d594fSAndroid Build Coastguard Worker SetFieldObjectVolatile<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
67*795d594fSAndroid Build Coastguard Worker } else {
68*795d594fSAndroid Build Coastguard Worker SetFieldObjectVolatile<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
69*795d594fSAndroid Build Coastguard Worker }
70*795d594fSAndroid Build Coastguard Worker }
71*795d594fSAndroid Build Coastguard Worker
IsCheckedException()72*795d594fSAndroid Build Coastguard Worker bool Throwable::IsCheckedException() {
73*795d594fSAndroid Build Coastguard Worker if (IsError()) {
74*795d594fSAndroid Build Coastguard Worker return false;
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker return !InstanceOf(WellKnownClasses::java_lang_RuntimeException.Get());
77*795d594fSAndroid Build Coastguard Worker }
78*795d594fSAndroid Build Coastguard Worker
IsError()79*795d594fSAndroid Build Coastguard Worker bool Throwable::IsError() {
80*795d594fSAndroid Build Coastguard Worker return InstanceOf(WellKnownClasses::java_lang_Error.Get());
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker
GetStackDepth()83*795d594fSAndroid Build Coastguard Worker int32_t Throwable::GetStackDepth() {
84*795d594fSAndroid Build Coastguard Worker const ObjPtr<Object> stack_state = GetStackState();
85*795d594fSAndroid Build Coastguard Worker if (stack_state == nullptr || !stack_state->IsObjectArray()) {
86*795d594fSAndroid Build Coastguard Worker return -1;
87*795d594fSAndroid Build Coastguard Worker }
88*795d594fSAndroid Build Coastguard Worker const ObjPtr<mirror::ObjectArray<Object>> trace = stack_state->AsObjectArray<Object>();
89*795d594fSAndroid Build Coastguard Worker const int32_t array_len = trace->GetLength();
90*795d594fSAndroid Build Coastguard Worker DCHECK_GT(array_len, 0);
91*795d594fSAndroid Build Coastguard Worker // See method BuildInternalStackTraceVisitor::Init for the format.
92*795d594fSAndroid Build Coastguard Worker return array_len - 1;
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker
Dump()95*795d594fSAndroid Build Coastguard Worker std::string Throwable::Dump() {
96*795d594fSAndroid Build Coastguard Worker std::string result(PrettyTypeOf());
97*795d594fSAndroid Build Coastguard Worker result += ": ";
98*795d594fSAndroid Build Coastguard Worker ObjPtr<String> msg = GetDetailMessage();
99*795d594fSAndroid Build Coastguard Worker if (msg != nullptr) {
100*795d594fSAndroid Build Coastguard Worker result += msg->ToModifiedUtf8();
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker result += "\n";
103*795d594fSAndroid Build Coastguard Worker ObjPtr<Object> stack_state = GetStackState();
104*795d594fSAndroid Build Coastguard Worker // check stack state isn't missing or corrupt
105*795d594fSAndroid Build Coastguard Worker if (stack_state != nullptr && stack_state->IsObjectArray()) {
106*795d594fSAndroid Build Coastguard Worker ObjPtr<ObjectArray<Object>> object_array = stack_state->AsObjectArray<Object>();
107*795d594fSAndroid Build Coastguard Worker // Decode the internal stack trace into the depth and method trace
108*795d594fSAndroid Build Coastguard Worker // See method BuildInternalStackTraceVisitor::Init for the format.
109*795d594fSAndroid Build Coastguard Worker DCHECK_GT(object_array->GetLength(), 0);
110*795d594fSAndroid Build Coastguard Worker ObjPtr<Object> methods_and_dex_pcs = object_array->Get(0);
111*795d594fSAndroid Build Coastguard Worker DCHECK(methods_and_dex_pcs->IsIntArray() || methods_and_dex_pcs->IsLongArray());
112*795d594fSAndroid Build Coastguard Worker ObjPtr<PointerArray> method_trace = ObjPtr<PointerArray>::DownCast(methods_and_dex_pcs);
113*795d594fSAndroid Build Coastguard Worker const int32_t array_len = method_trace->GetLength();
114*795d594fSAndroid Build Coastguard Worker CHECK_EQ(array_len % 2, 0);
115*795d594fSAndroid Build Coastguard Worker const auto depth = array_len / 2;
116*795d594fSAndroid Build Coastguard Worker if (depth == 0) {
117*795d594fSAndroid Build Coastguard Worker result += "(Throwable with empty stack trace)\n";
118*795d594fSAndroid Build Coastguard Worker } else {
119*795d594fSAndroid Build Coastguard Worker const PointerSize ptr_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
120*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0; i < depth; ++i) {
121*795d594fSAndroid Build Coastguard Worker ArtMethod* method = method_trace->GetElementPtrSize<ArtMethod*>(i, ptr_size);
122*795d594fSAndroid Build Coastguard Worker uintptr_t dex_pc = method_trace->GetElementPtrSize<uintptr_t>(i + depth, ptr_size);
123*795d594fSAndroid Build Coastguard Worker int32_t line_number = method->GetLineNumFromDexPC(dex_pc);
124*795d594fSAndroid Build Coastguard Worker const char* source_file = method->GetDeclaringClassSourceFile();
125*795d594fSAndroid Build Coastguard Worker result += StringPrintf(" at %s (%s:%d)\n", method->PrettyMethod(true).c_str(),
126*795d594fSAndroid Build Coastguard Worker source_file, line_number);
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker } else {
130*795d594fSAndroid Build Coastguard Worker ObjPtr<Object> stack_trace = GetStackTrace();
131*795d594fSAndroid Build Coastguard Worker if (stack_trace != nullptr && stack_trace->IsObjectArray()) {
132*795d594fSAndroid Build Coastguard Worker CHECK_EQ(stack_trace->GetClass()->GetComponentType(), GetClassRoot<StackTraceElement>());
133*795d594fSAndroid Build Coastguard Worker ObjPtr<ObjectArray<StackTraceElement>> ste_array =
134*795d594fSAndroid Build Coastguard Worker ObjPtr<ObjectArray<StackTraceElement>>::DownCast(stack_trace);
135*795d594fSAndroid Build Coastguard Worker if (ste_array->GetLength() == 0) {
136*795d594fSAndroid Build Coastguard Worker result += "(Throwable with empty stack trace)\n";
137*795d594fSAndroid Build Coastguard Worker } else {
138*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0; i < ste_array->GetLength(); ++i) {
139*795d594fSAndroid Build Coastguard Worker ObjPtr<StackTraceElement> ste = ste_array->Get(i);
140*795d594fSAndroid Build Coastguard Worker DCHECK(ste != nullptr);
141*795d594fSAndroid Build Coastguard Worker ObjPtr<String> method_name = ste->GetMethodName();
142*795d594fSAndroid Build Coastguard Worker ObjPtr<String> file_name = ste->GetFileName();
143*795d594fSAndroid Build Coastguard Worker result += StringPrintf(
144*795d594fSAndroid Build Coastguard Worker " at %s (%s:%d)\n",
145*795d594fSAndroid Build Coastguard Worker method_name != nullptr ? method_name->ToModifiedUtf8().c_str() : "<unknown method>",
146*795d594fSAndroid Build Coastguard Worker file_name != nullptr ? file_name->ToModifiedUtf8().c_str() : "(Unknown Source)",
147*795d594fSAndroid Build Coastguard Worker ste->GetLineNumber());
148*795d594fSAndroid Build Coastguard Worker }
149*795d594fSAndroid Build Coastguard Worker }
150*795d594fSAndroid Build Coastguard Worker } else {
151*795d594fSAndroid Build Coastguard Worker result += "(Throwable with no stack trace)\n";
152*795d594fSAndroid Build Coastguard Worker }
153*795d594fSAndroid Build Coastguard Worker }
154*795d594fSAndroid Build Coastguard Worker ObjPtr<Throwable> cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
155*795d594fSAndroid Build Coastguard Worker if (cause != nullptr && cause != this) { // Constructor makes cause == this by default.
156*795d594fSAndroid Build Coastguard Worker result += "Caused by: ";
157*795d594fSAndroid Build Coastguard Worker result += cause->Dump();
158*795d594fSAndroid Build Coastguard Worker }
159*795d594fSAndroid Build Coastguard Worker return result;
160*795d594fSAndroid Build Coastguard Worker }
161*795d594fSAndroid Build Coastguard Worker
GetStackState()162*795d594fSAndroid Build Coastguard Worker ObjPtr<Object> Throwable::GetStackState() {
163*795d594fSAndroid Build Coastguard Worker return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_));
164*795d594fSAndroid Build Coastguard Worker }
165*795d594fSAndroid Build Coastguard Worker
GetStackTrace()166*795d594fSAndroid Build Coastguard Worker ObjPtr<Object> Throwable::GetStackTrace() {
167*795d594fSAndroid Build Coastguard Worker return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_));
168*795d594fSAndroid Build Coastguard Worker }
169*795d594fSAndroid Build Coastguard Worker
GetDetailMessage()170*795d594fSAndroid Build Coastguard Worker ObjPtr<String> Throwable::GetDetailMessage() {
171*795d594fSAndroid Build Coastguard Worker return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_));
172*795d594fSAndroid Build Coastguard Worker }
173*795d594fSAndroid Build Coastguard Worker
GetCause()174*795d594fSAndroid Build Coastguard Worker ObjPtr<Throwable> Throwable::GetCause() {
175*795d594fSAndroid Build Coastguard Worker return GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
176*795d594fSAndroid Build Coastguard Worker }
177*795d594fSAndroid Build Coastguard Worker
178*795d594fSAndroid Build Coastguard Worker } // namespace mirror
179*795d594fSAndroid Build Coastguard Worker } // namespace art
180