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