blob: b866a63aefc278c36abe07cf3d9828a92ba34883 [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"
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "class-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "object-inl.h"
25#include "object_array.h"
26#include "object_array-inl.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
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070034GcRoot<Class> Throwable::java_lang_Throwable_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035
Mathieu Chartier31e88222016-10-14 18:43:19 -070036void Throwable::SetDetailMessage(ObjPtr<String> new_detail_message) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070037 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
Mathieu Chartier31e88222016-10-14 18:43:19 -070045void Throwable::SetCause(ObjPtr<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_));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070049 CHECK(current_cause == nullptr || 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
Mathieu Chartier31e88222016-10-14 18:43:19 -070057void Throwable::SetStackState(ObjPtr<Object> state) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc45b8b52014-05-03 01:39:59 -070058 CHECK(state != nullptr);
59 if (Runtime::Current()->IsActiveTransaction()) {
Piotr Jastrzebskic16a50f2015-05-07 09:41:00 +010060 SetFieldObjectVolatile<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
Ian Rogersc45b8b52014-05-03 01:39:59 -070061 } else {
Piotr Jastrzebskic16a50f2015-05-07 09:41:00 +010062 SetFieldObjectVolatile<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
Ian Rogersc45b8b52014-05-03 01:39:59 -070063 }
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
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +000073int32_t Throwable::GetStackDepth() {
Mathieu Chartier31e88222016-10-14 18:43:19 -070074 ObjPtr<Object> stack_state = GetStackState();
Mathieu Chartier910e8272015-09-30 09:24:22 -070075 if (stack_state == nullptr || !stack_state->IsObjectArray()) {
Sebastien Hertz415fd082015-06-01 11:42:27 +020076 return -1;
77 }
Mathieu Chartier31e88222016-10-14 18:43:19 -070078 ObjPtr<mirror::ObjectArray<Object>> const trace = stack_state->AsObjectArray<Object>();
Mathieu Chartier910e8272015-09-30 09:24:22 -070079 const int32_t array_len = trace->GetLength();
80 DCHECK_GT(array_len, 0);
81 // See method BuildInternalStackTraceVisitor::Init for the format.
82 return array_len - 1;
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +000083}
84
Ian Rogersef7d42f2014-01-06 12:55:46 -080085std::string Throwable::Dump() {
David Sehr709b0702016-10-13 09:12:37 -070086 std::string result(PrettyTypeOf());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 result += ": ";
Mathieu Chartier31e88222016-10-14 18:43:19 -070088 ObjPtr<String> msg = GetDetailMessage();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 if (msg != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 result += msg->ToModifiedUtf8();
91 }
92 result += "\n";
Mathieu Chartier31e88222016-10-14 18:43:19 -070093 ObjPtr<Object> stack_state = GetStackState();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 // check stack state isn't missing or corrupt
Mathieu Chartier910e8272015-09-30 09:24:22 -070095 if (stack_state != nullptr && stack_state->IsObjectArray()) {
Mathieu Chartier31e88222016-10-14 18:43:19 -070096 ObjPtr<ObjectArray<Object>> object_array = stack_state->AsObjectArray<Object>();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 // Decode the internal stack trace into the depth and method trace
Mathieu Chartier910e8272015-09-30 09:24:22 -070098 // See method BuildInternalStackTraceVisitor::Init for the format.
99 DCHECK_GT(object_array->GetLength(), 0);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700100 ObjPtr<Object> methods_and_dex_pcs = object_array->Get(0);
Mathieu Chartier910e8272015-09-30 09:24:22 -0700101 DCHECK(methods_and_dex_pcs->IsIntArray() || methods_and_dex_pcs->IsLongArray());
Mathieu Chartier31e88222016-10-14 18:43:19 -0700102 ObjPtr<PointerArray> method_trace = ObjPtr<PointerArray>::DownCast(methods_and_dex_pcs);
Mathieu Chartier910e8272015-09-30 09:24:22 -0700103 const int32_t array_len = method_trace->GetLength();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104 CHECK_EQ(array_len % 2, 0);
105 const auto depth = array_len / 2;
Ian Rogersc45b8b52014-05-03 01:39:59 -0700106 if (depth == 0) {
107 result += "(Throwable with empty stack trace)";
108 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700109 const PointerSize ptr_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersc45b8b52014-05-03 01:39:59 -0700110 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700111 ArtMethod* method = method_trace->GetElementPtrSize<ArtMethod*>(i, ptr_size);
112 uintptr_t dex_pc = method_trace->GetElementPtrSize<uintptr_t>(i + depth, ptr_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700113 int32_t line_number = method->GetLineNumFromDexPC(dex_pc);
114 const char* source_file = method->GetDeclaringClassSourceFile();
David Sehr709b0702016-10-13 09:12:37 -0700115 result += StringPrintf(" at %s (%s:%d)\n", method->PrettyMethod(true).c_str(),
Ian Rogersc45b8b52014-05-03 01:39:59 -0700116 source_file, line_number);
117 }
118 }
119 } else {
Mathieu Chartier31e88222016-10-14 18:43:19 -0700120 ObjPtr<Object> stack_trace = GetStackTrace();
Ian Rogersc45b8b52014-05-03 01:39:59 -0700121 if (stack_trace != nullptr && stack_trace->IsObjectArray()) {
122 CHECK_EQ(stack_trace->GetClass()->GetComponentType(),
123 StackTraceElement::GetStackTraceElement());
Mathieu Chartier31e88222016-10-14 18:43:19 -0700124 ObjPtr<ObjectArray<StackTraceElement>> ste_array =
125 ObjPtr<ObjectArray<StackTraceElement>>::DownCast(stack_trace);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700126 if (ste_array->GetLength() == 0) {
127 result += "(Throwable with empty stack trace)";
128 } else {
129 for (int32_t i = 0; i < ste_array->GetLength(); ++i) {
130 StackTraceElement* ste = ste_array->Get(i);
Mathieu Chartier4a248582015-04-28 13:53:02 -0700131 DCHECK(ste != nullptr);
132 auto* method_name = ste->GetMethodName();
133 auto* file_name = ste->GetFileName();
134 result += StringPrintf(
135 " at %s (%s:%d)\n",
136 method_name != nullptr ? method_name->ToModifiedUtf8().c_str() : "<unknown method>",
137 file_name != nullptr ? file_name->ToModifiedUtf8().c_str() : "(Unknown Source)",
138 ste->GetLineNumber());
Ian Rogersc45b8b52014-05-03 01:39:59 -0700139 }
140 }
141 } else {
142 result += "(Throwable with no stack trace)";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 }
144 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700145 ObjPtr<Throwable> cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
Ian Rogersc45b8b52014-05-03 01:39:59 -0700146 if (cause != nullptr && cause != this) { // Constructor makes cause == this by default.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147 result += "Caused by: ";
148 result += cause->Dump();
149 }
150 return result;
151}
152
Mathieu Chartier31e88222016-10-14 18:43:19 -0700153void Throwable::SetClass(ObjPtr<Class> java_lang_Throwable) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700154 CHECK(java_lang_Throwable_.IsNull());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 CHECK(java_lang_Throwable != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700156 java_lang_Throwable_ = GcRoot<Class>(java_lang_Throwable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157}
158
159void Throwable::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700160 CHECK(!java_lang_Throwable_.IsNull());
161 java_lang_Throwable_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162}
163
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700164void Throwable::VisitRoots(RootVisitor* visitor) {
165 java_lang_Throwable_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800166}
167
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168} // namespace mirror
169} // namespace art