blob: 9092f2f7d3eb01e584763de09f2865be22b1a711 [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
Andreas Gampeda3e5612016-12-13 19:00:53 -080035static jint FindLineNumber(jint line_number_count,
36 jvmtiLineNumberEntry* line_number_table,
37 jlocation location) {
38 if (line_number_table == nullptr) {
39 return -2;
40 }
41
42 jint line_number = -1;
43 for (jint i = 0; i != line_number_count; ++i) {
44 if (line_number_table[i].start_location > location) {
45 return line_number;
46 }
47 line_number = line_number_table[i].line_number;
48 }
49 return line_number;
50}
51
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070052extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getStackTrace(
53 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) {
54 std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]);
55
56 jint count;
Andreas Gampe336c3c32016-11-08 17:02:19 -080057 {
58 jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count);
59 if (result != JVMTI_ERROR_NONE) {
60 char* err;
61 jvmti_env->GetErrorName(result, &err);
62 printf("Failure running GetStackTrace: %s\n", err);
63 return nullptr;
64 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070065 }
66
Andreas Gampeceafe352016-12-12 18:49:33 -080067 auto callback = [&](jint method_index) -> jobjectArray {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070068 char* name;
69 char* sig;
70 char* gen;
Andreas Gampe336c3c32016-11-08 17:02:19 -080071 {
72 jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen);
73 if (result2 != JVMTI_ERROR_NONE) {
74 char* err;
75 jvmti_env->GetErrorName(result2, &err);
76 printf("Failure running GetMethodName: %s\n", err);
77 return nullptr;
78 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070079 }
Andreas Gampeceafe352016-12-12 18:49:33 -080080
Andreas Gampeda3e5612016-12-13 19:00:53 -080081 jint line_number_count;
82 jvmtiLineNumberEntry* line_number_table;
83 {
84 jvmtiError line_result = jvmti_env->GetLineNumberTable(frames[method_index].method,
85 &line_number_count,
86 &line_number_table);
87 if (line_result != JVMTI_ERROR_NONE) {
88 // Accept absent info and native method errors.
89 if (line_result != JVMTI_ERROR_ABSENT_INFORMATION &&
90 line_result != JVMTI_ERROR_NATIVE_METHOD) {
91 char* err;
92 jvmti_env->GetErrorName(line_result, &err);
93 printf("Failure running GetLineNumberTable: %s\n", err);
94 return nullptr;
95 }
96 line_number_table = nullptr;
97 line_number_count = 0;
98 }
99 }
100
Andreas Gampeceafe352016-12-12 18:49:33 -0800101 auto inner_callback = [&](jint component_index) -> jstring {
102 switch (component_index) {
103 case 0:
104 return (name == nullptr) ? nullptr : env->NewStringUTF(name);
105 case 1:
106 return (sig == nullptr) ? nullptr : env->NewStringUTF(sig);
Andreas Gampe2340e3f2016-12-12 19:37:19 -0800107 case 2:
108 return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str());
Andreas Gampeda3e5612016-12-13 19:00:53 -0800109 case 3: {
110 jint line_number = FindLineNumber(line_number_count,
111 line_number_table,
112 frames[method_index].location);
113 return env->NewStringUTF(StringPrintf("%d", line_number).c_str());
114 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800115 }
116 LOG(FATAL) << "Unreachable";
117 UNREACHABLE();
118 };
Andreas Gampeda3e5612016-12-13 19:00:53 -0800119 jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700120
121 if (name != nullptr) {
122 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
123 }
124 if (sig != nullptr) {
125 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
126 }
127 if (gen != nullptr) {
128 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
129 }
Andreas Gampeda3e5612016-12-13 19:00:53 -0800130 if (line_number_table != nullptr) {
131 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table));
132 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800133
134 return inner_array;
Andreas Gampe336c3c32016-11-08 17:02:19 -0800135 };
Andreas Gampeceafe352016-12-12 18:49:33 -0800136 return CreateObjectArray(env, count, "[Ljava/lang/String;", callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700137}
138
139// Don't do anything
140jint OnLoad(JavaVM* vm,
141 char* options ATTRIBUTE_UNUSED,
142 void* reserved ATTRIBUTE_UNUSED) {
143 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
144 printf("Unable to get jvmti env!\n");
145 return 1;
146 }
Alex Lighte6574242016-08-17 09:56:24 -0700147 SetAllCapabilities(jvmti_env);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700148 return 0;
149}
150
151} // namespace Test911GetStackTrace
152} // namespace art