blob: d5627531c93ba1854c9edf4b2f1547b3eda6cc64 [file] [log] [blame]
Andreas Gampeb5eb94a2016-10-27 19:23:09 -07001/*
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 Gampeceafe352016-12-12 18:49:33 -080023#include "base/macros.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070024#include "jni.h"
25#include "openjdkjvmti/jvmti.h"
26#include "ScopedLocalRef.h"
Andreas Gampe336c3c32016-11-08 17:02:19 -080027#include "ti-agent/common_helper.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070028#include "ti-agent/common_load.h"
29
30namespace art {
31namespace Test911GetStackTrace {
32
33extern "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 Gampe336c3c32016-11-08 17:02:19 -080038 {
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 Gampeb5eb94a2016-10-27 19:23:09 -070046 }
47
Andreas Gampeceafe352016-12-12 18:49:33 -080048 auto callback = [&](jint method_index) -> jobjectArray {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070049 char* name;
50 char* sig;
51 char* gen;
Andreas Gampe336c3c32016-11-08 17:02:19 -080052 {
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 Gampeb5eb94a2016-10-27 19:23:09 -070060 }
Andreas Gampeceafe352016-12-12 18:49:33 -080061
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 Gampeb5eb94a2016-10-27 19:23:09 -070073
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 Gampeceafe352016-12-12 18:49:33 -080083
84 return inner_array;
Andreas Gampe336c3c32016-11-08 17:02:19 -080085 };
Andreas Gampeceafe352016-12-12 18:49:33 -080086 return CreateObjectArray(env, count, "[Ljava/lang/String;", callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070087}
88
89// Don't do anything
90jint 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 Lighte6574242016-08-17 09:56:24 -070097 SetAllCapabilities(jvmti_env);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070098 return 0;
99}
100
101} // namespace Test911GetStackTrace
102} // namespace art