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 | |
| 35 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getStackTrace( |
| 36 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) { |
| 37 | std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]); |
| 38 | |
| 39 | jint count; |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 40 | { |
| 41 | jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count); |
| 42 | if (result != JVMTI_ERROR_NONE) { |
| 43 | char* err; |
| 44 | jvmti_env->GetErrorName(result, &err); |
| 45 | printf("Failure running GetStackTrace: %s\n", err); |
| 46 | return nullptr; |
| 47 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 50 | auto callback = [&](jint method_index) -> jobjectArray { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 51 | char* name; |
| 52 | char* sig; |
| 53 | char* gen; |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 54 | { |
| 55 | jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen); |
| 56 | if (result2 != JVMTI_ERROR_NONE) { |
| 57 | char* err; |
| 58 | jvmti_env->GetErrorName(result2, &err); |
| 59 | printf("Failure running GetMethodName: %s\n", err); |
| 60 | return nullptr; |
| 61 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 62 | } |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 63 | |
| 64 | auto inner_callback = [&](jint component_index) -> jstring { |
| 65 | switch (component_index) { |
| 66 | case 0: |
| 67 | return (name == nullptr) ? nullptr : env->NewStringUTF(name); |
| 68 | case 1: |
| 69 | return (sig == nullptr) ? nullptr : env->NewStringUTF(sig); |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 70 | case 2: |
| 71 | return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str()); |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 72 | } |
| 73 | LOG(FATAL) << "Unreachable"; |
| 74 | UNREACHABLE(); |
| 75 | }; |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 76 | jobjectArray inner_array = CreateObjectArray(env, 3, "java/lang/String", inner_callback); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 77 | |
| 78 | if (name != nullptr) { |
| 79 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name)); |
| 80 | } |
| 81 | if (sig != nullptr) { |
| 82 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig)); |
| 83 | } |
| 84 | if (gen != nullptr) { |
| 85 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen)); |
| 86 | } |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 87 | |
| 88 | return inner_array; |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 89 | }; |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 90 | return CreateObjectArray(env, count, "[Ljava/lang/String;", callback); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Don't do anything |
| 94 | jint OnLoad(JavaVM* vm, |
| 95 | char* options ATTRIBUTE_UNUSED, |
| 96 | void* reserved ATTRIBUTE_UNUSED) { |
| 97 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 98 | printf("Unable to get jvmti env!\n"); |
| 99 | return 1; |
| 100 | } |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 101 | SetAllCapabilities(jvmti_env); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | } // namespace Test911GetStackTrace |
| 106 | } // namespace art |