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