blob: b5b5678f542b2e137a7c3efd8ef871034e7b244c [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
Andreas Gampe2340e3f2016-12-12 19:37:19 -080019#include <inttypes.h>
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070020#include <memory>
21#include <stdio.h>
22
23#include "base/logging.h"
Andreas Gampeceafe352016-12-12 18:49:33 -080024#include "base/macros.h"
Andreas Gampe2340e3f2016-12-12 19:37:19 -080025#include "base/stringprintf.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070026#include "jni.h"
27#include "openjdkjvmti/jvmti.h"
28#include "ScopedLocalRef.h"
Andreas Gampe336c3c32016-11-08 17:02:19 -080029#include "ti-agent/common_helper.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070030#include "ti-agent/common_load.h"
31
32namespace art {
33namespace Test911GetStackTrace {
34
35extern "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 Gampe336c3c32016-11-08 17:02:19 -080040 {
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 Gampeb5eb94a2016-10-27 19:23:09 -070048 }
49
Andreas Gampeceafe352016-12-12 18:49:33 -080050 auto callback = [&](jint method_index) -> jobjectArray {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070051 char* name;
52 char* sig;
53 char* gen;
Andreas Gampe336c3c32016-11-08 17:02:19 -080054 {
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 Gampeb5eb94a2016-10-27 19:23:09 -070062 }
Andreas Gampeceafe352016-12-12 18:49:33 -080063
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 Gampe2340e3f2016-12-12 19:37:19 -080070 case 2:
71 return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str());
Andreas Gampeceafe352016-12-12 18:49:33 -080072 }
73 LOG(FATAL) << "Unreachable";
74 UNREACHABLE();
75 };
Andreas Gampe2340e3f2016-12-12 19:37:19 -080076 jobjectArray inner_array = CreateObjectArray(env, 3, "java/lang/String", inner_callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070077
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 Gampeceafe352016-12-12 18:49:33 -080087
88 return inner_array;
Andreas Gampe336c3c32016-11-08 17:02:19 -080089 };
Andreas Gampeceafe352016-12-12 18:49:33 -080090 return CreateObjectArray(env, count, "[Ljava/lang/String;", callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070091}
92
93// Don't do anything
94jint 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 Lighte6574242016-08-17 09:56:24 -0700101 SetAllCapabilities(jvmti_env);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700102 return 0;
103}
104
105} // namespace Test911GetStackTrace
106} // namespace art