blob: 68f6d8dfb2b4c4c456238b7c52754b5ec2fc2d5c [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 Gampef6f3b5f2017-01-13 09:21:42 -080023#include "android-base/stringprintf.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070024#include "base/logging.h"
Andreas Gampeceafe352016-12-12 18:49:33 -080025#include "base/macros.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 Gampe46ee31b2016-12-14 10:11:49 -080035using android::base::StringPrintf;
36
Andreas Gampe53ae7802017-01-19 21:13:46 -080037extern "C" JNIEXPORT void JNICALL Java_Main_bindTest911Classes(
38 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
39 BindFunctions(jvmti_env, env, "AllTraces");
40 BindFunctions(jvmti_env, env, "Frames");
41 BindFunctions(jvmti_env, env, "PrintThread");
42 BindFunctions(jvmti_env, env, "ThreadListTraces");
43}
44
Andreas Gampeda3e5612016-12-13 19:00:53 -080045static jint FindLineNumber(jint line_number_count,
46 jvmtiLineNumberEntry* line_number_table,
47 jlocation location) {
48 if (line_number_table == nullptr) {
49 return -2;
50 }
51
52 jint line_number = -1;
53 for (jint i = 0; i != line_number_count; ++i) {
54 if (line_number_table[i].start_location > location) {
55 return line_number;
56 }
57 line_number = line_number_table[i].line_number;
58 }
59 return line_number;
60}
61
Andreas Gampea1a27c62017-01-11 16:37:16 -080062static jobjectArray TranslateJvmtiFrameInfoArray(JNIEnv* env,
63 jvmtiFrameInfo* frames,
64 jint count) {
Andreas Gampeceafe352016-12-12 18:49:33 -080065 auto callback = [&](jint method_index) -> jobjectArray {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070066 char* name;
67 char* sig;
68 char* gen;
Andreas Gampe336c3c32016-11-08 17:02:19 -080069 {
70 jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen);
Andreas Gampeeba32fb2017-01-12 17:40:05 -080071 if (JvmtiErrorToException(env, result2)) {
Andreas Gampe336c3c32016-11-08 17:02:19 -080072 return nullptr;
73 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070074 }
Andreas Gampeceafe352016-12-12 18:49:33 -080075
Andreas Gampeda3e5612016-12-13 19:00:53 -080076 jint line_number_count;
77 jvmtiLineNumberEntry* line_number_table;
78 {
79 jvmtiError line_result = jvmti_env->GetLineNumberTable(frames[method_index].method,
80 &line_number_count,
81 &line_number_table);
82 if (line_result != JVMTI_ERROR_NONE) {
83 // Accept absent info and native method errors.
84 if (line_result != JVMTI_ERROR_ABSENT_INFORMATION &&
85 line_result != JVMTI_ERROR_NATIVE_METHOD) {
86 char* err;
87 jvmti_env->GetErrorName(line_result, &err);
88 printf("Failure running GetLineNumberTable: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080089 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeda3e5612016-12-13 19:00:53 -080090 return nullptr;
91 }
92 line_number_table = nullptr;
93 line_number_count = 0;
94 }
95 }
96
Andreas Gampeceafe352016-12-12 18:49:33 -080097 auto inner_callback = [&](jint component_index) -> jstring {
98 switch (component_index) {
99 case 0:
100 return (name == nullptr) ? nullptr : env->NewStringUTF(name);
101 case 1:
102 return (sig == nullptr) ? nullptr : env->NewStringUTF(sig);
Andreas Gampe2340e3f2016-12-12 19:37:19 -0800103 case 2:
104 return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str());
Andreas Gampeda3e5612016-12-13 19:00:53 -0800105 case 3: {
106 jint line_number = FindLineNumber(line_number_count,
107 line_number_table,
108 frames[method_index].location);
109 return env->NewStringUTF(StringPrintf("%d", line_number).c_str());
110 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800111 }
112 LOG(FATAL) << "Unreachable";
113 UNREACHABLE();
114 };
Andreas Gampeda3e5612016-12-13 19:00:53 -0800115 jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700116
117 if (name != nullptr) {
118 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
119 }
120 if (sig != nullptr) {
121 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
122 }
123 if (gen != nullptr) {
124 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
125 }
Andreas Gampeda3e5612016-12-13 19:00:53 -0800126 if (line_number_table != nullptr) {
127 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table));
128 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800129
130 return inner_array;
Andreas Gampe336c3c32016-11-08 17:02:19 -0800131 };
Andreas Gampeceafe352016-12-12 18:49:33 -0800132 return CreateObjectArray(env, count, "[Ljava/lang/String;", callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700133}
134
Andreas Gampe966de9e2017-01-12 20:51:02 -0800135extern "C" JNIEXPORT jobjectArray JNICALL Java_PrintThread_getStackTrace(
Andreas Gampea1a27c62017-01-11 16:37:16 -0800136 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) {
137 std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]);
138
139 jint count;
140 {
141 jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count);
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800142 if (JvmtiErrorToException(env, result)) {
Andreas Gampea1a27c62017-01-11 16:37:16 -0800143 return nullptr;
144 }
145 }
146
147 return TranslateJvmtiFrameInfoArray(env, frames.get(), count);
148}
149
Andreas Gampe966de9e2017-01-12 20:51:02 -0800150extern "C" JNIEXPORT jobjectArray JNICALL Java_AllTraces_getAllStackTraces(
Andreas Gampea1a27c62017-01-11 16:37:16 -0800151 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint max) {
Andreas Gampea1a27c62017-01-11 16:37:16 -0800152 jint thread_count;
153 jvmtiStackInfo* stack_infos;
154 {
155 jvmtiError result = jvmti_env->GetAllStackTraces(max, &stack_infos, &thread_count);
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800156 if (JvmtiErrorToException(env, result)) {
157 return nullptr;
158 }
159 }
160
161 auto callback = [&](jint thread_index) -> jobject {
162 auto inner_callback = [&](jint index) -> jobject {
163 if (index == 0) {
164 return stack_infos[thread_index].thread;
165 } else {
166 return TranslateJvmtiFrameInfoArray(env,
167 stack_infos[thread_index].frame_buffer,
168 stack_infos[thread_index].frame_count);
169 }
170 };
171 return CreateObjectArray(env, 2, "java/lang/Object", inner_callback);
172 };
173 jobjectArray ret = CreateObjectArray(env, thread_count, "[Ljava/lang/Object;", callback);
174 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(stack_infos));
175 return ret;
176}
177
178extern "C" JNIEXPORT jobjectArray JNICALL Java_ThreadListTraces_getThreadListStackTraces(
179 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobjectArray jthreads, jint max) {
180 jint thread_count = env->GetArrayLength(jthreads);
181 std::unique_ptr<jthread[]> threads(new jthread[thread_count]);
182 for (jint i = 0; i != thread_count; ++i) {
183 threads[i] = env->GetObjectArrayElement(jthreads, i);
184 }
185
186 jvmtiStackInfo* stack_infos;
187 {
188 jvmtiError result = jvmti_env->GetThreadListStackTraces(thread_count,
189 threads.get(),
190 max,
191 &stack_infos);
192 if (JvmtiErrorToException(env, result)) {
Andreas Gampea1a27c62017-01-11 16:37:16 -0800193 return nullptr;
194 }
195 }
196
197 auto callback = [&](jint thread_index) -> jobject {
198 auto inner_callback = [&](jint index) -> jobject {
199 if (index == 0) {
200 return stack_infos[thread_index].thread;
201 } else {
202 return TranslateJvmtiFrameInfoArray(env,
203 stack_infos[thread_index].frame_buffer,
204 stack_infos[thread_index].frame_count);
205 }
206 };
207 return CreateObjectArray(env, 2, "java/lang/Object", inner_callback);
208 };
209 jobjectArray ret = CreateObjectArray(env, thread_count, "[Ljava/lang/Object;", callback);
210 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(stack_infos));
211 return ret;
212}
213
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800214extern "C" JNIEXPORT jint JNICALL Java_Frames_getFrameCount(
215 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread) {
216 jint count;
217 jvmtiError result = jvmti_env->GetFrameCount(thread, &count);
218 if (JvmtiErrorToException(env, result)) {
219 return -1;
220 }
221 return count;
222}
223
224extern "C" JNIEXPORT jobjectArray JNICALL Java_Frames_getFrameLocation(
225 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint depth) {
226 jmethodID method;
227 jlocation location;
228
229 jvmtiError result = jvmti_env->GetFrameLocation(thread, depth, &method, &location);
230 if (JvmtiErrorToException(env, result)) {
231 return nullptr;
232 }
233
234 auto callback = [&](jint index) -> jobject {
235 switch (index) {
236 case 0:
237 {
238 jclass decl_class;
239 jvmtiError class_result = jvmti_env->GetMethodDeclaringClass(method, &decl_class);
240 if (JvmtiErrorToException(env, class_result)) {
241 return nullptr;
242 }
243 jint modifiers;
244 jvmtiError mod_result = jvmti_env->GetMethodModifiers(method, &modifiers);
245 if (JvmtiErrorToException(env, mod_result)) {
246 return nullptr;
247 }
248 constexpr jint kStatic = 0x8;
249 return env->ToReflectedMethod(decl_class,
250 method,
251 (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE);
252 }
253 case 1:
254 return env->NewStringUTF(
255 android::base::StringPrintf("%x", static_cast<uint32_t>(location)).c_str());
256 }
257 LOG(FATAL) << "Unreachable";
258 UNREACHABLE();
259 };
260 jobjectArray ret = CreateObjectArray(env, 2, "java/lang/Object", callback);
261 return ret;
262}
263
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700264} // namespace Test911GetStackTrace
265} // namespace art