blob: 78b76dc6efeba556164931c0e171f60eab756327 [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
19#include "abstract_method-inl.h"
20#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"
27#include "utils.h"
28#include "well_known_classes.h"
29
30namespace art {
31namespace mirror {
32
33Class* Throwable::java_lang_Throwable_ = NULL;
34
35void Throwable::SetCause(Throwable* cause) {
36 CHECK(cause != NULL);
37 CHECK(cause != this);
Ian Rogers62d6c772013-02-27 08:32:07 -080038 Throwable* current_cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_),
39 false);
40 CHECK(current_cause == NULL || current_cause == this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
42}
43
44bool Throwable::IsCheckedException() const {
45 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
46 return false;
47 }
48 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
49}
50
51std::string Throwable::Dump() const {
52 std::string result(PrettyTypeOf(this));
53 result += ": ";
54 String* msg = GetDetailMessage();
55 if (msg != NULL) {
56 result += msg->ToModifiedUtf8();
57 }
58 result += "\n";
59 Object* stack_state = GetStackState();
60 // check stack state isn't missing or corrupt
61 if (stack_state != NULL && stack_state->IsObjectArray()) {
62 // Decode the internal stack trace into the depth and method trace
63 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
64 int32_t depth = method_trace->GetLength() - 1;
65 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
66 MethodHelper mh;
67 for (int32_t i = 0; i < depth; ++i) {
68 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
69 mh.ChangeMethod(method);
70 uint32_t dex_pc = pc_trace->Get(i);
71 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
72 const char* source_file = mh.GetDeclaringClassSourceFile();
73 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
74 source_file, line_number);
75 }
76 }
77 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
78 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
79 result += "Caused by: ";
80 result += cause->Dump();
81 }
82 return result;
83}
84
85void Throwable::SetClass(Class* java_lang_Throwable) {
86 CHECK(java_lang_Throwable_ == NULL);
87 CHECK(java_lang_Throwable != NULL);
88 java_lang_Throwable_ = java_lang_Throwable;
89}
90
91void Throwable::ResetClass() {
92 CHECK(java_lang_Throwable_ != NULL);
93 java_lang_Throwable_ = NULL;
94}
95
96} // namespace mirror
97} // namespace art