blob: da649cf8c9651a99276654e993dede7a50d213ea [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"
26#include "ti-agent/common_load.h"
27
28namespace art {
29namespace Test911GetStackTrace {
30
31extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getStackTrace(
32 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) {
33 std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]);
34
35 jint count;
36 jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count);
37 if (result != JVMTI_ERROR_NONE) {
38 char* err;
39 jvmti_env->GetErrorName(result, &err);
40 printf("Failure running GetStackTrace: %s\n", err);
41 return nullptr;
42 }
43
44 ScopedLocalRef<jclass> obj_class(env, env->FindClass("java/lang/String"));
45 if (obj_class.get() == nullptr) {
46 return nullptr;
47 }
48
49 jobjectArray ret = env->NewObjectArray(2 * count, obj_class.get(), nullptr);
50 if (ret == nullptr) {
51 return ret;
52 }
53
54 for (size_t i = 0; i < static_cast<size_t>(count); ++i) {
55 char* name;
56 char* sig;
57 char* gen;
58 jvmtiError result2 = jvmti_env->GetMethodName(frames[i].method, &name, &sig, &gen);
59 if (result2 != JVMTI_ERROR_NONE) {
60 char* err;
61 jvmti_env->GetErrorName(result, &err);
62 printf("Failure running GetMethodName: %s\n", err);
63 return nullptr;
64 }
65 ScopedLocalRef<jstring> trace_name(env, name == nullptr ? nullptr : env->NewStringUTF(name));
66 ScopedLocalRef<jstring> trace_sig(env, sig == nullptr ? nullptr : env->NewStringUTF(sig));
67 env->SetObjectArrayElement(ret, static_cast<jint>(2 * i), trace_name.get());
68 env->SetObjectArrayElement(ret, static_cast<jint>(2 * i + 1), trace_sig.get());
69
70 if (name != nullptr) {
71 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
72 }
73 if (sig != nullptr) {
74 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
75 }
76 if (gen != nullptr) {
77 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
78 }
79 }
80
81 return ret;
82}
83
84// Don't do anything
85jint OnLoad(JavaVM* vm,
86 char* options ATTRIBUTE_UNUSED,
87 void* reserved ATTRIBUTE_UNUSED) {
88 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
89 printf("Unable to get jvmti env!\n");
90 return 1;
91 }
92 return 0;
93}
94
95} // namespace Test911GetStackTrace
96} // namespace art