throwable.cc revision 31e88225b2ef68e7f32f11186acf922c74ddabab
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 "art_method-inl.h"
20#include "base/enums.h"
21#include "class-inl.h"
22#include "dex_file-inl.h"
23#include "gc/accounting/card_table-inl.h"
24#include "object-inl.h"
25#include "object_array.h"
26#include "object_array-inl.h"
27#include "stack_trace_element.h"
28#include "utils.h"
29#include "well_known_classes.h"
30
31namespace art {
32namespace mirror {
33
34GcRoot<Class> Throwable::java_lang_Throwable_;
35
36void Throwable::SetDetailMessage(ObjPtr<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
45void Throwable::SetCause(ObjPtr<Throwable> cause) {
46  CHECK(cause != nullptr);
47  CHECK(cause != this);
48  Throwable* current_cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
49  CHECK(current_cause == nullptr || current_cause == this);
50  if (Runtime::Current()->IsActiveTransaction()) {
51    SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
52  } else {
53    SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
54  }
55}
56
57void Throwable::SetStackState(ObjPtr<Object> state) REQUIRES_SHARED(Locks::mutator_lock_) {
58  CHECK(state != nullptr);
59  if (Runtime::Current()->IsActiveTransaction()) {
60    SetFieldObjectVolatile<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
61  } else {
62    SetFieldObjectVolatile<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_), state);
63  }
64}
65
66bool Throwable::IsCheckedException() {
67  if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
68    return false;
69  }
70  return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
71}
72
73int32_t Throwable::GetStackDepth() {
74  ObjPtr<Object> stack_state = GetStackState();
75  if (stack_state == nullptr || !stack_state->IsObjectArray()) {
76    return -1;
77  }
78  ObjPtr<mirror::ObjectArray<Object>> const trace = stack_state->AsObjectArray<Object>();
79  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;
83}
84
85std::string Throwable::Dump() {
86  std::string result(PrettyTypeOf(this));
87  result += ": ";
88  ObjPtr<String> msg = GetDetailMessage();
89  if (msg != nullptr) {
90    result += msg->ToModifiedUtf8();
91  }
92  result += "\n";
93  ObjPtr<Object> stack_state = GetStackState();
94  // check stack state isn't missing or corrupt
95  if (stack_state != nullptr && stack_state->IsObjectArray()) {
96    ObjPtr<ObjectArray<Object>> object_array = stack_state->AsObjectArray<Object>();
97    // Decode the internal stack trace into the depth and method trace
98    // See method BuildInternalStackTraceVisitor::Init for the format.
99    DCHECK_GT(object_array->GetLength(), 0);
100    ObjPtr<Object> methods_and_dex_pcs = object_array->Get(0);
101    DCHECK(methods_and_dex_pcs->IsIntArray() || methods_and_dex_pcs->IsLongArray());
102    ObjPtr<PointerArray> method_trace = ObjPtr<PointerArray>::DownCast(methods_and_dex_pcs);
103    const int32_t array_len = method_trace->GetLength();
104    CHECK_EQ(array_len % 2, 0);
105    const auto depth = array_len / 2;
106    if (depth == 0) {
107      result += "(Throwable with empty stack trace)";
108    } else {
109      const PointerSize ptr_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
110      for (int32_t i = 0; i < depth; ++i) {
111        ArtMethod* method = method_trace->GetElementPtrSize<ArtMethod*>(i, ptr_size);
112        uintptr_t dex_pc = method_trace->GetElementPtrSize<uintptr_t>(i + depth, ptr_size);
113        int32_t line_number = method->GetLineNumFromDexPC(dex_pc);
114        const char* source_file = method->GetDeclaringClassSourceFile();
115        result += StringPrintf("  at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
116                               source_file, line_number);
117      }
118    }
119  } else {
120    ObjPtr<Object> stack_trace = GetStackTrace();
121    if (stack_trace != nullptr && stack_trace->IsObjectArray()) {
122      CHECK_EQ(stack_trace->GetClass()->GetComponentType(),
123               StackTraceElement::GetStackTraceElement());
124      ObjPtr<ObjectArray<StackTraceElement>> ste_array =
125          ObjPtr<ObjectArray<StackTraceElement>>::DownCast(stack_trace);
126      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);
131          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());
139        }
140      }
141    } else {
142      result += "(Throwable with no stack trace)";
143    }
144  }
145  ObjPtr<Throwable> cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
146  if (cause != nullptr && cause != this) {  // Constructor makes cause == this by default.
147    result += "Caused by: ";
148    result += cause->Dump();
149  }
150  return result;
151}
152
153void Throwable::SetClass(ObjPtr<Class> java_lang_Throwable) {
154  CHECK(java_lang_Throwable_.IsNull());
155  CHECK(java_lang_Throwable != nullptr);
156  java_lang_Throwable_ = GcRoot<Class>(java_lang_Throwable);
157}
158
159void Throwable::ResetClass() {
160  CHECK(!java_lang_Throwable_.IsNull());
161  java_lang_Throwable_ = GcRoot<Class>(nullptr);
162}
163
164void Throwable::VisitRoots(RootVisitor* visitor) {
165  java_lang_Throwable_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
166}
167
168}  // namespace mirror
169}  // namespace art
170