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