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