blob: 224d266a15cf0b81ff0efe34e25222280352d213 [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
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "class-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "object-inl.h"
24#include "object_array.h"
25#include "object_array-inl.h"
Ian Rogersc45b8b52014-05-03 01:39:59 -070026#include "stack_trace_element.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "utils.h"
28#include "well_known_classes.h"
29
30namespace art {
31namespace mirror {
32
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070033GcRoot<Class> Throwable::java_lang_Throwable_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070035void 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 Rogers2dd0e2c2013-01-24 12:42:14 -080044void Throwable::SetCause(Throwable* cause) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080045 CHECK(cause != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 CHECK(cause != this);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070047 Throwable* current_cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070048 CHECK(current_cause == nullptr || current_cause == this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010049 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070050 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010051 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070052 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010053 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054}
55
Ian Rogersc45b8b52014-05-03 01:39:59 -070056void Throwable::SetStackState(Object* state) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
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 Rogersef7d42f2014-01-06 12:55:46 -080065bool Throwable::IsCheckedException() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
67 return false;
68 }
69 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
70}
71
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +000072int32_t Throwable::GetStackDepth() {
73 Object* stack_state = GetStackState();
74 if (stack_state == nullptr || !stack_state->IsObjectArray()) return -1;
75 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
76 return method_trace->GetLength() - 1;
77}
78
Ian Rogersef7d42f2014-01-06 12:55:46 -080079std::string Throwable::Dump() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 std::string result(PrettyTypeOf(this));
81 result += ": ";
82 String* msg = GetDetailMessage();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 if (msg != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 result += msg->ToModifiedUtf8();
85 }
86 result += "\n";
87 Object* stack_state = GetStackState();
88 // check stack state isn't missing or corrupt
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 if (stack_state != nullptr &&
90 (stack_state->IsIntArray() || stack_state->IsLongArray())) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 // Decode the internal stack trace into the depth and method trace
Mathieu Chartiere401d142015-04-22 13:56:20 -070092 // Format is [method pointers][pcs]
93 auto* method_trace = down_cast<mirror::PointerArray*>(stack_state->AsArray());
94 auto array_len = method_trace->GetLength();
95 CHECK_EQ(array_len % 2, 0);
96 const auto depth = array_len / 2;
Ian Rogersc45b8b52014-05-03 01:39:59 -070097 if (depth == 0) {
98 result += "(Throwable with empty stack trace)";
99 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100 auto ptr_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersc45b8b52014-05-03 01:39:59 -0700101 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102 ArtMethod* method = method_trace->GetElementPtrSize<ArtMethod*>(i, ptr_size);
103 uintptr_t dex_pc = method_trace->GetElementPtrSize<uintptr_t>(i + depth, ptr_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700104 int32_t line_number = method->GetLineNumFromDexPC(dex_pc);
105 const char* source_file = method->GetDeclaringClassSourceFile();
Ian Rogersc45b8b52014-05-03 01:39:59 -0700106 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
107 source_file, line_number);
108 }
109 }
110 } else {
111 Object* stack_trace = GetStackTrace();
112 if (stack_trace != nullptr && stack_trace->IsObjectArray()) {
113 CHECK_EQ(stack_trace->GetClass()->GetComponentType(),
114 StackTraceElement::GetStackTraceElement());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115 auto* ste_array = down_cast<ObjectArray<StackTraceElement>*>(stack_trace);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700116 if (ste_array->GetLength() == 0) {
117 result += "(Throwable with empty stack trace)";
118 } else {
119 for (int32_t i = 0; i < ste_array->GetLength(); ++i) {
120 StackTraceElement* ste = ste_array->Get(i);
Mathieu Chartier4a248582015-04-28 13:53:02 -0700121 DCHECK(ste != nullptr);
122 auto* method_name = ste->GetMethodName();
123 auto* file_name = ste->GetFileName();
124 result += StringPrintf(
125 " at %s (%s:%d)\n",
126 method_name != nullptr ? method_name->ToModifiedUtf8().c_str() : "<unknown method>",
127 file_name != nullptr ? file_name->ToModifiedUtf8().c_str() : "(Unknown Source)",
128 ste->GetLineNumber());
Ian Rogersc45b8b52014-05-03 01:39:59 -0700129 }
130 }
131 } else {
132 result += "(Throwable with no stack trace)";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 }
134 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700135 Throwable* cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
Ian Rogersc45b8b52014-05-03 01:39:59 -0700136 if (cause != nullptr && cause != this) { // Constructor makes cause == this by default.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 result += "Caused by: ";
138 result += cause->Dump();
139 }
140 return result;
141}
142
143void Throwable::SetClass(Class* java_lang_Throwable) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700144 CHECK(java_lang_Throwable_.IsNull());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700145 CHECK(java_lang_Throwable != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700146 java_lang_Throwable_ = GcRoot<Class>(java_lang_Throwable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147}
148
149void Throwable::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700150 CHECK(!java_lang_Throwable_.IsNull());
151 java_lang_Throwable_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152}
153
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700154void Throwable::VisitRoots(RootVisitor* visitor) {
155 java_lang_Throwable_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800156}
157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158} // namespace mirror
159} // namespace art