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
19 #include "android-base/stringprintf.h"
20
21 #include "art_method-inl.h"
22 #include "base/enums.h"
23 #include "base/utils.h"
24 #include "class-inl.h"
25 #include "class_root-inl.h"
26 #include "dex/dex_file-inl.h"
27 #include "gc/accounting/card_table-inl.h"
28 #include "obj_ptr-inl.h"
29 #include "object-inl.h"
30 #include "object_array-inl.h"
31 #include "object_array.h"
32 #include "stack_trace_element-inl.h"
33 #include "string.h"
34 #include "well_known_classes.h"
35
36 namespace art {
37 namespace mirror {
38
39 using android::base::StringPrintf;
40
SetDetailMessage(ObjPtr<String> new_detail_message)41 void Throwable::SetDetailMessage(ObjPtr<String> new_detail_message) {
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
SetCause(ObjPtr<Throwable> cause)50 void Throwable::SetCause(ObjPtr<Throwable> cause) {
51 CHECK(cause != nullptr);
52 CHECK(cause != this);
53 ObjPtr<Throwable> current_cause =
54 GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
55 CHECK(current_cause == nullptr || current_cause == this);
56 if (Runtime::Current()->IsActiveTransaction()) {
57 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
58 } else {
59 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
60 }
61 }
62
SetStackState(ObjPtr<Object> state)63 void Throwable::SetStackState(ObjPtr<Object> state) REQUIRES_SHARED(Locks::mutator_lock_) {
64 CHECK(state != nullptr);
65 if (Runtime::Current()->IsActiveTransaction()) {
66 SetFieldObjectVolatile<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
67 } else {
68 SetFieldObjectVolatile<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
69 }
70 }
71
IsCheckedException()72 bool Throwable::IsCheckedException() {
73 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
74 return false;
75 }
76 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
77 }
78
IsError()79 bool Throwable::IsError() {
80 return InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error));
81 }
82
GetStackDepth()83 int32_t Throwable::GetStackDepth() {
84 const ObjPtr<Object> stack_state = GetStackState();
85 if (stack_state == nullptr || !stack_state->IsObjectArray()) {
86 return -1;
87 }
88 const ObjPtr<mirror::ObjectArray<Object>> trace = stack_state->AsObjectArray<Object>();
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;
93 }
94
Dump()95 std::string Throwable::Dump() {
96 std::string result(PrettyTypeOf());
97 result += ": ";
98 ObjPtr<String> msg = GetDetailMessage();
99 if (msg != nullptr) {
100 result += msg->ToModifiedUtf8();
101 }
102 result += "\n";
103 ObjPtr<Object> stack_state = GetStackState();
104 // check stack state isn't missing or corrupt
105 if (stack_state != nullptr && stack_state->IsObjectArray()) {
106 ObjPtr<ObjectArray<Object>> object_array = stack_state->AsObjectArray<Object>();
107 // Decode the internal stack trace into the depth and method trace
108 // See method BuildInternalStackTraceVisitor::Init for the format.
109 DCHECK_GT(object_array->GetLength(), 0);
110 ObjPtr<Object> methods_and_dex_pcs = object_array->Get(0);
111 DCHECK(methods_and_dex_pcs->IsIntArray() || methods_and_dex_pcs->IsLongArray());
112 ObjPtr<PointerArray> method_trace = ObjPtr<PointerArray>::DownCast(methods_and_dex_pcs);
113 const int32_t array_len = method_trace->GetLength();
114 CHECK_EQ(array_len % 2, 0);
115 const auto depth = array_len / 2;
116 if (depth == 0) {
117 result += "(Throwable with empty stack trace)\n";
118 } else {
119 const PointerSize ptr_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
120 for (int32_t i = 0; i < depth; ++i) {
121 ArtMethod* method = method_trace->GetElementPtrSize<ArtMethod*>(i, ptr_size);
122 uintptr_t dex_pc = method_trace->GetElementPtrSize<uintptr_t>(i + depth, ptr_size);
123 int32_t line_number = method->GetLineNumFromDexPC(dex_pc);
124 const char* source_file = method->GetDeclaringClassSourceFile();
125 result += StringPrintf(" at %s (%s:%d)\n", method->PrettyMethod(true).c_str(),
126 source_file, line_number);
127 }
128 }
129 } else {
130 ObjPtr<Object> stack_trace = GetStackTrace();
131 if (stack_trace != nullptr && stack_trace->IsObjectArray()) {
132 CHECK_EQ(stack_trace->GetClass()->GetComponentType(), GetClassRoot<StackTraceElement>());
133 ObjPtr<ObjectArray<StackTraceElement>> ste_array =
134 ObjPtr<ObjectArray<StackTraceElement>>::DownCast(stack_trace);
135 if (ste_array->GetLength() == 0) {
136 result += "(Throwable with empty stack trace)\n";
137 } else {
138 for (int32_t i = 0; i < ste_array->GetLength(); ++i) {
139 ObjPtr<StackTraceElement> ste = ste_array->Get(i);
140 DCHECK(ste != nullptr);
141 ObjPtr<String> method_name = ste->GetMethodName();
142 ObjPtr<String> file_name = ste->GetFileName();
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());
148 }
149 }
150 } else {
151 result += "(Throwable with no stack trace)\n";
152 }
153 }
154 ObjPtr<Throwable> cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
155 if (cause != nullptr && cause != this) { // Constructor makes cause == this by default.
156 result += "Caused by: ";
157 result += cause->Dump();
158 }
159 return result;
160 }
161
GetStackState()162 ObjPtr<Object> Throwable::GetStackState() {
163 return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_));
164 }
165
GetStackTrace()166 ObjPtr<Object> Throwable::GetStackTrace() {
167 return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_));
168 }
169
GetDetailMessage()170 ObjPtr<String> Throwable::GetDetailMessage() {
171 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_));
172 }
173
174 } // namespace mirror
175 } // namespace art
176