Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 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 "stack_trace.h" |
| 18 | |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <stdio.h> |
| 22 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame^] | 23 | #include "android-base/stringprintf.h" |
| 24 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 25 | #include "base/logging.h" |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 26 | #include "base/macros.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 27 | #include "jni.h" |
| 28 | #include "openjdkjvmti/jvmti.h" |
| 29 | #include "ScopedLocalRef.h" |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 30 | #include "ti-agent/common_helper.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 31 | #include "ti-agent/common_load.h" |
| 32 | |
| 33 | namespace art { |
| 34 | namespace Test911GetStackTrace { |
| 35 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame^] | 36 | using android::base::StringPrintf; |
| 37 | |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 38 | static jint FindLineNumber(jint line_number_count, |
| 39 | jvmtiLineNumberEntry* line_number_table, |
| 40 | jlocation location) { |
| 41 | if (line_number_table == nullptr) { |
| 42 | return -2; |
| 43 | } |
| 44 | |
| 45 | jint line_number = -1; |
| 46 | for (jint i = 0; i != line_number_count; ++i) { |
| 47 | if (line_number_table[i].start_location > location) { |
| 48 | return line_number; |
| 49 | } |
| 50 | line_number = line_number_table[i].line_number; |
| 51 | } |
| 52 | return line_number; |
| 53 | } |
| 54 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 55 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getStackTrace( |
| 56 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) { |
| 57 | std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]); |
| 58 | |
| 59 | jint count; |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 60 | { |
| 61 | jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count); |
| 62 | if (result != JVMTI_ERROR_NONE) { |
| 63 | char* err; |
| 64 | jvmti_env->GetErrorName(result, &err); |
| 65 | printf("Failure running GetStackTrace: %s\n", err); |
| 66 | return nullptr; |
| 67 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 70 | auto callback = [&](jint method_index) -> jobjectArray { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 71 | char* name; |
| 72 | char* sig; |
| 73 | char* gen; |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 74 | { |
| 75 | jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen); |
| 76 | if (result2 != JVMTI_ERROR_NONE) { |
| 77 | char* err; |
| 78 | jvmti_env->GetErrorName(result2, &err); |
| 79 | printf("Failure running GetMethodName: %s\n", err); |
| 80 | return nullptr; |
| 81 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 82 | } |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 83 | |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 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 | return nullptr; |
| 98 | } |
| 99 | line_number_table = nullptr; |
| 100 | line_number_count = 0; |
| 101 | } |
| 102 | } |
| 103 | |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 104 | auto inner_callback = [&](jint component_index) -> jstring { |
| 105 | switch (component_index) { |
| 106 | case 0: |
| 107 | return (name == nullptr) ? nullptr : env->NewStringUTF(name); |
| 108 | case 1: |
| 109 | return (sig == nullptr) ? nullptr : env->NewStringUTF(sig); |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 110 | case 2: |
| 111 | return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str()); |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 112 | case 3: { |
| 113 | jint line_number = FindLineNumber(line_number_count, |
| 114 | line_number_table, |
| 115 | frames[method_index].location); |
| 116 | return env->NewStringUTF(StringPrintf("%d", line_number).c_str()); |
| 117 | } |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 118 | } |
| 119 | LOG(FATAL) << "Unreachable"; |
| 120 | UNREACHABLE(); |
| 121 | }; |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 122 | jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 123 | |
| 124 | if (name != nullptr) { |
| 125 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name)); |
| 126 | } |
| 127 | if (sig != nullptr) { |
| 128 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig)); |
| 129 | } |
| 130 | if (gen != nullptr) { |
| 131 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen)); |
| 132 | } |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 133 | if (line_number_table != nullptr) { |
| 134 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table)); |
| 135 | } |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 136 | |
| 137 | return inner_array; |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 138 | }; |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 139 | return CreateObjectArray(env, count, "[Ljava/lang/String;", callback); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Don't do anything |
| 143 | jint OnLoad(JavaVM* vm, |
| 144 | char* options ATTRIBUTE_UNUSED, |
| 145 | void* reserved ATTRIBUTE_UNUSED) { |
| 146 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 147 | printf("Unable to get jvmti env!\n"); |
| 148 | return 1; |
| 149 | } |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 150 | SetAllCapabilities(jvmti_env); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | } // namespace Test911GetStackTrace |
| 155 | } // namespace art |