blob: d393a13da61c4af93714130f8210f4155dbae5be [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#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
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070035void Throwable::SetDetailMessage(String* new_detail_message) {
36 if (Runtime::Current()->IsActiveTransaction()) {
37 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), new_detail_message);
38 } else {
39 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
40 new_detail_message);
41 }
42}
43
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044void Throwable::SetCause(Throwable* cause) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080045 CHECK(cause != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 CHECK(cause != this);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070047 Throwable* current_cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
Ian Rogers62d6c772013-02-27 08:32:07 -080048 CHECK(current_cause == NULL || current_cause == this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010049 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070050 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010051 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070052 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010053 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054}
55
Ian Rogersef7d42f2014-01-06 12:55:46 -080056bool Throwable::IsCheckedException() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
58 return false;
59 }
60 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
61}
62
Ian Rogersef7d42f2014-01-06 12:55:46 -080063std::string Throwable::Dump() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 std::string result(PrettyTypeOf(this));
65 result += ": ";
66 String* msg = GetDetailMessage();
67 if (msg != NULL) {
68 result += msg->ToModifiedUtf8();
69 }
70 result += "\n";
71 Object* stack_state = GetStackState();
72 // check stack state isn't missing or corrupt
73 if (stack_state != NULL && stack_state->IsObjectArray()) {
74 // Decode the internal stack trace into the depth and method trace
75 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
76 int32_t depth = method_trace->GetLength() - 1;
77 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
78 MethodHelper mh;
79 for (int32_t i = 0; i < depth; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -070080 ArtMethod* method = down_cast<ArtMethod*>(method_trace->Get(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 mh.ChangeMethod(method);
82 uint32_t dex_pc = pc_trace->Get(i);
83 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
84 const char* source_file = mh.GetDeclaringClassSourceFile();
85 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
86 source_file, line_number);
87 }
88 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070089 Throwable* cause = GetFieldObject<Throwable>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
91 result += "Caused by: ";
92 result += cause->Dump();
93 }
94 return result;
95}
96
97void Throwable::SetClass(Class* java_lang_Throwable) {
98 CHECK(java_lang_Throwable_ == NULL);
99 CHECK(java_lang_Throwable != NULL);
100 java_lang_Throwable_ = java_lang_Throwable;
101}
102
103void Throwable::ResetClass() {
104 CHECK(java_lang_Throwable_ != NULL);
105 java_lang_Throwable_ = NULL;
106}
107
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800108void Throwable::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800109 if (java_lang_Throwable_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800110 callback(reinterpret_cast<mirror::Object**>(&java_lang_Throwable_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800111 }
112}
113
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114} // namespace mirror
115} // namespace art