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