blob: 6874fe567bf57133cbc77192c5038c2f2189b283 [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"
26#include "object_utils.h"
Ian Rogersc45b8b52014-05-03 01:39:59 -070027#include "stack_trace_element.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "utils.h"
29#include "well_known_classes.h"
30
31namespace art {
32namespace mirror {
33
34Class* Throwable::java_lang_Throwable_ = NULL;
35
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070036void Throwable::SetDetailMessage(String* new_detail_message) {
37 if (Runtime::Current()->IsActiveTransaction()) {
38 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), new_detail_message);
39 } else {
40 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
41 new_detail_message);
42 }
43}
44
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045void Throwable::SetCause(Throwable* cause) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080046 CHECK(cause != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 CHECK(cause != this);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070048 Throwable* current_cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
Ian Rogers62d6c772013-02-27 08:32:07 -080049 CHECK(current_cause == NULL || current_cause == this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010050 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070051 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010052 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070053 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010054 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055}
56
Ian Rogersc45b8b52014-05-03 01:39:59 -070057void Throwable::SetStackState(Object* state) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
58 CHECK(state != nullptr);
59 if (Runtime::Current()->IsActiveTransaction()) {
60 SetFieldObjectVolatile<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), state);
61 } else {
62 SetFieldObjectVolatile<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), state);
63 }
64}
65
Ian Rogersef7d42f2014-01-06 12:55:46 -080066bool Throwable::IsCheckedException() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
68 return false;
69 }
70 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
71}
72
Ian Rogersef7d42f2014-01-06 12:55:46 -080073std::string Throwable::Dump() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 std::string result(PrettyTypeOf(this));
75 result += ": ";
76 String* msg = GetDetailMessage();
77 if (msg != NULL) {
78 result += msg->ToModifiedUtf8();
79 }
80 result += "\n";
81 Object* stack_state = GetStackState();
82 // check stack state isn't missing or corrupt
Ian Rogersc45b8b52014-05-03 01:39:59 -070083 if (stack_state != nullptr && stack_state->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 // Decode the internal stack trace into the depth and method trace
85 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
86 int32_t depth = method_trace->GetLength() - 1;
87 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
88 MethodHelper mh;
Ian Rogersc45b8b52014-05-03 01:39:59 -070089 if (depth == 0) {
90 result += "(Throwable with empty stack trace)";
91 } else {
92 for (int32_t i = 0; i < depth; ++i) {
93 ArtMethod* method = down_cast<ArtMethod*>(method_trace->Get(i));
94 mh.ChangeMethod(method);
95 uint32_t dex_pc = pc_trace->Get(i);
96 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
97 const char* source_file = mh.GetDeclaringClassSourceFile();
98 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
99 source_file, line_number);
100 }
101 }
102 } else {
103 Object* stack_trace = GetStackTrace();
104 if (stack_trace != nullptr && stack_trace->IsObjectArray()) {
105 CHECK_EQ(stack_trace->GetClass()->GetComponentType(),
106 StackTraceElement::GetStackTraceElement());
107 ObjectArray<StackTraceElement>* ste_array =
108 down_cast<ObjectArray<StackTraceElement>*>(stack_trace);
109 if (ste_array->GetLength() == 0) {
110 result += "(Throwable with empty stack trace)";
111 } else {
112 for (int32_t i = 0; i < ste_array->GetLength(); ++i) {
113 StackTraceElement* ste = ste_array->Get(i);
114 result += StringPrintf(" at %s (%s:%d)\n",
115 ste->GetMethodName()->ToModifiedUtf8().c_str(),
116 ste->GetFileName()->ToModifiedUtf8().c_str(),
117 ste->GetLineNumber());
118 }
119 }
120 } else {
121 result += "(Throwable with no stack trace)";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 }
123 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700124 Throwable* cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
Ian Rogersc45b8b52014-05-03 01:39:59 -0700125 if (cause != nullptr && cause != this) { // Constructor makes cause == this by default.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 result += "Caused by: ";
127 result += cause->Dump();
128 }
129 return result;
130}
131
132void Throwable::SetClass(Class* java_lang_Throwable) {
133 CHECK(java_lang_Throwable_ == NULL);
134 CHECK(java_lang_Throwable != NULL);
135 java_lang_Throwable_ = java_lang_Throwable;
136}
137
138void Throwable::ResetClass() {
139 CHECK(java_lang_Throwable_ != NULL);
140 java_lang_Throwable_ = NULL;
141}
142
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800143void Throwable::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800144 if (java_lang_Throwable_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800145 callback(reinterpret_cast<mirror::Object**>(&java_lang_Throwable_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800146 }
147}
148
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149} // namespace mirror
150} // namespace art