blob: e7d9380469025f21ae93ffbc5390653e9cb170d9 [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"
23#include "jni.h"
24#include "openjdkjvmti/jvmti.h"
25#include "ScopedLocalRef.h"
Andreas Gampe336c3c32016-11-08 17:02:19 -080026#include "ti-agent/common_helper.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070027#include "ti-agent/common_load.h"
28
29namespace art {
30namespace Test911GetStackTrace {
31
32extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getStackTrace(
33 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) {
34 std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]);
35
36 jint count;
Andreas Gampe336c3c32016-11-08 17:02:19 -080037 {
38 jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count);
39 if (result != JVMTI_ERROR_NONE) {
40 char* err;
41 jvmti_env->GetErrorName(result, &err);
42 printf("Failure running GetStackTrace: %s\n", err);
43 return nullptr;
44 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070045 }
46
Andreas Gampe336c3c32016-11-08 17:02:19 -080047 auto callback = [&](jint i) -> jstring {
48 size_t method_index = static_cast<size_t>(i) / 2;
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 Gampe336c3c32016-11-08 17:02:19 -080061 jstring callback_result;
62 if (i % 2 == 0) {
63 callback_result = name == nullptr ? nullptr : env->NewStringUTF(name);
64 } else {
65 callback_result = sig == nullptr ? nullptr : env->NewStringUTF(sig);
66 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070067
68 if (name != nullptr) {
69 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
70 }
71 if (sig != nullptr) {
72 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
73 }
74 if (gen != nullptr) {
75 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
76 }
Andreas Gampe336c3c32016-11-08 17:02:19 -080077 return callback_result;
78 };
79 return CreateObjectArray(env, 2 * count, "java/lang/String", callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070080}
81
82// Don't do anything
83jint OnLoad(JavaVM* vm,
84 char* options ATTRIBUTE_UNUSED,
85 void* reserved ATTRIBUTE_UNUSED) {
86 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
87 printf("Unable to get jvmti env!\n");
88 return 1;
89 }
Alex Lighte6574242016-08-17 09:56:24 -070090 SetAllCapabilities(jvmti_env);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070091 return 0;
92}
93
94} // namespace Test911GetStackTrace
95} // namespace art