blob: 8fc0af47329070b1912321aa17e257fce496c37c [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
Andreas Gampe2340e3f2016-12-12 19:37:19 -080017#include <inttypes.h>
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070018#include <memory>
19#include <stdio.h>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070023#include "base/logging.h"
Andreas Gampeceafe352016-12-12 18:49:33 -080024#include "base/macros.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070025#include "jni.h"
26#include "openjdkjvmti/jvmti.h"
27#include "ScopedLocalRef.h"
Andreas Gampe336c3c32016-11-08 17:02:19 -080028#include "ti-agent/common_helper.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070029#include "ti-agent/common_load.h"
30
31namespace art {
32namespace Test911GetStackTrace {
33
Andreas Gampe46ee31b2016-12-14 10:11:49 -080034using android::base::StringPrintf;
35
Andreas Gampeda3e5612016-12-13 19:00:53 -080036static jint FindLineNumber(jint line_number_count,
37 jvmtiLineNumberEntry* line_number_table,
38 jlocation location) {
39 if (line_number_table == nullptr) {
40 return -2;
41 }
42
43 jint line_number = -1;
44 for (jint i = 0; i != line_number_count; ++i) {
45 if (line_number_table[i].start_location > location) {
46 return line_number;
47 }
48 line_number = line_number_table[i].line_number;
49 }
50 return line_number;
51}
52
Andreas Gampea1a27c62017-01-11 16:37:16 -080053static jobjectArray TranslateJvmtiFrameInfoArray(JNIEnv* env,
54 jvmtiFrameInfo* frames,
55 jint count) {
Andreas Gampeceafe352016-12-12 18:49:33 -080056 auto callback = [&](jint method_index) -> jobjectArray {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070057 char* name;
58 char* sig;
59 char* gen;
Andreas Gampe336c3c32016-11-08 17:02:19 -080060 {
61 jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen);
62 if (result2 != JVMTI_ERROR_NONE) {
63 char* err;
64 jvmti_env->GetErrorName(result2, &err);
65 printf("Failure running GetMethodName: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080066 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe336c3c32016-11-08 17:02:19 -080067 return nullptr;
68 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070069 }
Andreas Gampeceafe352016-12-12 18:49:33 -080070
Andreas Gampeda3e5612016-12-13 19:00:53 -080071 jint line_number_count;
72 jvmtiLineNumberEntry* line_number_table;
73 {
74 jvmtiError line_result = jvmti_env->GetLineNumberTable(frames[method_index].method,
75 &line_number_count,
76 &line_number_table);
77 if (line_result != JVMTI_ERROR_NONE) {
78 // Accept absent info and native method errors.
79 if (line_result != JVMTI_ERROR_ABSENT_INFORMATION &&
80 line_result != JVMTI_ERROR_NATIVE_METHOD) {
81 char* err;
82 jvmti_env->GetErrorName(line_result, &err);
83 printf("Failure running GetLineNumberTable: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080084 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeda3e5612016-12-13 19:00:53 -080085 return nullptr;
86 }
87 line_number_table = nullptr;
88 line_number_count = 0;
89 }
90 }
91
Andreas Gampeceafe352016-12-12 18:49:33 -080092 auto inner_callback = [&](jint component_index) -> jstring {
93 switch (component_index) {
94 case 0:
95 return (name == nullptr) ? nullptr : env->NewStringUTF(name);
96 case 1:
97 return (sig == nullptr) ? nullptr : env->NewStringUTF(sig);
Andreas Gampe2340e3f2016-12-12 19:37:19 -080098 case 2:
99 return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str());
Andreas Gampeda3e5612016-12-13 19:00:53 -0800100 case 3: {
101 jint line_number = FindLineNumber(line_number_count,
102 line_number_table,
103 frames[method_index].location);
104 return env->NewStringUTF(StringPrintf("%d", line_number).c_str());
105 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800106 }
107 LOG(FATAL) << "Unreachable";
108 UNREACHABLE();
109 };
Andreas Gampeda3e5612016-12-13 19:00:53 -0800110 jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700111
112 if (name != nullptr) {
113 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
114 }
115 if (sig != nullptr) {
116 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
117 }
118 if (gen != nullptr) {
119 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
120 }
Andreas Gampeda3e5612016-12-13 19:00:53 -0800121 if (line_number_table != nullptr) {
122 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table));
123 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800124
125 return inner_array;
Andreas Gampe336c3c32016-11-08 17:02:19 -0800126 };
Andreas Gampeceafe352016-12-12 18:49:33 -0800127 return CreateObjectArray(env, count, "[Ljava/lang/String;", callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700128}
129
Andreas Gampe966de9e2017-01-12 20:51:02 -0800130extern "C" JNIEXPORT jobjectArray JNICALL Java_PrintThread_getStackTrace(
Andreas Gampea1a27c62017-01-11 16:37:16 -0800131 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) {
132 std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]);
133
134 jint count;
135 {
136 jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count);
137 if (result != JVMTI_ERROR_NONE) {
138 char* err;
139 jvmti_env->GetErrorName(result, &err);
140 printf("Failure running GetStackTrace: %s\n", err);
141 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
142 return nullptr;
143 }
144 }
145
146 return TranslateJvmtiFrameInfoArray(env, frames.get(), count);
147}
148
Andreas Gampe966de9e2017-01-12 20:51:02 -0800149extern "C" JNIEXPORT jobjectArray JNICALL Java_AllTraces_getAllStackTraces(
Andreas Gampea1a27c62017-01-11 16:37:16 -0800150 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint max) {
151 std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]);
152
153 jint thread_count;
154 jvmtiStackInfo* stack_infos;
155 {
156 jvmtiError result = jvmti_env->GetAllStackTraces(max, &stack_infos, &thread_count);
157 if (result != JVMTI_ERROR_NONE) {
158 char* err;
159 jvmti_env->GetErrorName(result, &err);
160 printf("Failure running GetAllStackTraces: %s\n", err);
161 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
162 return nullptr;
163 }
164 }
165
166 auto callback = [&](jint thread_index) -> jobject {
167 auto inner_callback = [&](jint index) -> jobject {
168 if (index == 0) {
169 return stack_infos[thread_index].thread;
170 } else {
171 return TranslateJvmtiFrameInfoArray(env,
172 stack_infos[thread_index].frame_buffer,
173 stack_infos[thread_index].frame_count);
174 }
175 };
176 return CreateObjectArray(env, 2, "java/lang/Object", inner_callback);
177 };
178 jobjectArray ret = CreateObjectArray(env, thread_count, "[Ljava/lang/Object;", callback);
179 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(stack_infos));
180 return ret;
181}
182
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700183} // namespace Test911GetStackTrace
184} // namespace art