stack_trace.cc revision a8883a0000a08dc4cb2bfec01cbead0da6272a5c
1/* 2 * Copyright (C) 2013 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 <inttypes.h> 18#include <memory> 19#include <stdio.h> 20 21#include "android-base/stringprintf.h" 22 23#include "base/logging.h" 24#include "base/macros.h" 25#include "jni.h" 26#include "openjdkjvmti/jvmti.h" 27#include "ScopedLocalRef.h" 28#include "ti-agent/common_helper.h" 29#include "ti-agent/common_load.h" 30 31namespace art { 32namespace Test911GetStackTrace { 33 34using android::base::StringPrintf; 35 36static jint FindLineNumber(jint line_number_count, 37 jvmtiLineNumberEntry* line_number_table, 38 jlocation location) { 39 if (line_number_table == nullptr) { 40 return -2; 41 } 42 43 jint line_number = -1; 44 for (jint i = 0; i != line_number_count; ++i) { 45 if (line_number_table[i].start_location > location) { 46 return line_number; 47 } 48 line_number = line_number_table[i].line_number; 49 } 50 return line_number; 51} 52 53extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getStackTrace( 54 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) { 55 std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]); 56 57 jint count; 58 { 59 jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count); 60 if (result != JVMTI_ERROR_NONE) { 61 char* err; 62 jvmti_env->GetErrorName(result, &err); 63 printf("Failure running GetStackTrace: %s\n", err); 64 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); 65 return nullptr; 66 } 67 } 68 69 auto callback = [&](jint method_index) -> jobjectArray { 70 char* name; 71 char* sig; 72 char* gen; 73 { 74 jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen); 75 if (result2 != JVMTI_ERROR_NONE) { 76 char* err; 77 jvmti_env->GetErrorName(result2, &err); 78 printf("Failure running GetMethodName: %s\n", err); 79 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); 80 return nullptr; 81 } 82 } 83 84 jint line_number_count; 85 jvmtiLineNumberEntry* line_number_table; 86 { 87 jvmtiError line_result = jvmti_env->GetLineNumberTable(frames[method_index].method, 88 &line_number_count, 89 &line_number_table); 90 if (line_result != JVMTI_ERROR_NONE) { 91 // Accept absent info and native method errors. 92 if (line_result != JVMTI_ERROR_ABSENT_INFORMATION && 93 line_result != JVMTI_ERROR_NATIVE_METHOD) { 94 char* err; 95 jvmti_env->GetErrorName(line_result, &err); 96 printf("Failure running GetLineNumberTable: %s\n", err); 97 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); 98 return nullptr; 99 } 100 line_number_table = nullptr; 101 line_number_count = 0; 102 } 103 } 104 105 auto inner_callback = [&](jint component_index) -> jstring { 106 switch (component_index) { 107 case 0: 108 return (name == nullptr) ? nullptr : env->NewStringUTF(name); 109 case 1: 110 return (sig == nullptr) ? nullptr : env->NewStringUTF(sig); 111 case 2: 112 return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str()); 113 case 3: { 114 jint line_number = FindLineNumber(line_number_count, 115 line_number_table, 116 frames[method_index].location); 117 return env->NewStringUTF(StringPrintf("%d", line_number).c_str()); 118 } 119 } 120 LOG(FATAL) << "Unreachable"; 121 UNREACHABLE(); 122 }; 123 jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback); 124 125 if (name != nullptr) { 126 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name)); 127 } 128 if (sig != nullptr) { 129 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig)); 130 } 131 if (gen != nullptr) { 132 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen)); 133 } 134 if (line_number_table != nullptr) { 135 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table)); 136 } 137 138 return inner_array; 139 }; 140 return CreateObjectArray(env, count, "[Ljava/lang/String;", callback); 141} 142 143} // namespace Test911GetStackTrace 144} // namespace art 145