blob: 2b620b1fc0d5eb10d0e56f79d949d0cca4ddbfb8 [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 Gampe8cf9cb32017-07-19 09:28:38 -070018
19#include <cstdio>
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070020#include <memory>
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070021
Andreas Gampe027444b2017-03-31 12:49:07 -070022#include "android-base/logging.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070025#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070026#include "jvmti.h"
Andreas Gampe027444b2017-03-31 12:49:07 -070027#include "scoped_local_ref.h"
Andreas Gampe3f46c962017-03-30 10:26:59 -070028
29// Test infrastructure
30#include "jni_binder.h"
31#include "jni_helper.h"
32#include "jvmti_helper.h"
33#include "test_env.h"
Andreas Gampe027444b2017-03-31 12:49:07 -070034#include "ti_macros.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070035
36namespace art {
37namespace Test911GetStackTrace {
38
Andreas Gampe46ee31b2016-12-14 10:11:49 -080039using android::base::StringPrintf;
40
Andreas Gampeda3e5612016-12-13 19:00:53 -080041static jint FindLineNumber(jint line_number_count,
42 jvmtiLineNumberEntry* line_number_table,
43 jlocation location) {
44 if (line_number_table == nullptr) {
45 return -2;
46 }
47
48 jint line_number = -1;
49 for (jint i = 0; i != line_number_count; ++i) {
50 if (line_number_table[i].start_location > location) {
51 return line_number;
52 }
53 line_number = line_number_table[i].line_number;
54 }
55 return line_number;
56}
57
Andreas Gampea1a27c62017-01-11 16:37:16 -080058static jobjectArray TranslateJvmtiFrameInfoArray(JNIEnv* env,
59 jvmtiFrameInfo* frames,
60 jint count) {
Andreas Gampeceafe352016-12-12 18:49:33 -080061 auto callback = [&](jint method_index) -> jobjectArray {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070062 char* name;
63 char* sig;
64 char* gen;
Andreas Gampe336c3c32016-11-08 17:02:19 -080065 {
66 jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen);
Andreas Gampe3f46c962017-03-30 10:26:59 -070067 if (JvmtiErrorToException(env, jvmti_env, result2)) {
Andreas Gampe336c3c32016-11-08 17:02:19 -080068 return nullptr;
69 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070070 }
Andreas Gampeceafe352016-12-12 18:49:33 -080071
Andreas Gampeda3e5612016-12-13 19:00:53 -080072 jint line_number_count;
73 jvmtiLineNumberEntry* line_number_table;
74 {
75 jvmtiError line_result = jvmti_env->GetLineNumberTable(frames[method_index].method,
76 &line_number_count,
77 &line_number_table);
78 if (line_result != JVMTI_ERROR_NONE) {
79 // Accept absent info and native method errors.
80 if (line_result != JVMTI_ERROR_ABSENT_INFORMATION &&
81 line_result != JVMTI_ERROR_NATIVE_METHOD) {
Andreas Gampe3f46c962017-03-30 10:26:59 -070082 JvmtiErrorToException(env, jvmti_env, line_result);
Andreas Gampeda3e5612016-12-13 19:00:53 -080083 return nullptr;
84 }
85 line_number_table = nullptr;
86 line_number_count = 0;
87 }
88 }
89
Andreas Gampeceafe352016-12-12 18:49:33 -080090 auto inner_callback = [&](jint component_index) -> jstring {
91 switch (component_index) {
92 case 0:
93 return (name == nullptr) ? nullptr : env->NewStringUTF(name);
94 case 1:
95 return (sig == nullptr) ? nullptr : env->NewStringUTF(sig);
Andreas Gampe2340e3f2016-12-12 19:37:19 -080096 case 2:
97 return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str());
Andreas Gampeda3e5612016-12-13 19:00:53 -080098 case 3: {
99 jint line_number = FindLineNumber(line_number_count,
100 line_number_table,
101 frames[method_index].location);
102 return env->NewStringUTF(StringPrintf("%d", line_number).c_str());
103 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800104 }
105 LOG(FATAL) << "Unreachable";
106 UNREACHABLE();
107 };
Andreas Gampeda3e5612016-12-13 19:00:53 -0800108 jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700109
110 if (name != nullptr) {
111 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
112 }
113 if (sig != nullptr) {
114 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
115 }
116 if (gen != nullptr) {
117 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
118 }
Andreas Gampeda3e5612016-12-13 19:00:53 -0800119 if (line_number_table != nullptr) {
120 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table));
121 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800122
123 return inner_array;
Andreas Gampe336c3c32016-11-08 17:02:19 -0800124 };
Andreas Gampeceafe352016-12-12 18:49:33 -0800125 return CreateObjectArray(env, count, "[Ljava/lang/String;", callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700126}
127
Andreas Gampe46651672017-04-07 09:00:04 -0700128extern "C" JNIEXPORT jobjectArray JNICALL Java_art_PrintThread_getStackTrace(
Andreas Gampea1a27c62017-01-11 16:37:16 -0800129 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) {
130 std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]);
131
132 jint count;
133 {
134 jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700135 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampea1a27c62017-01-11 16:37:16 -0800136 return nullptr;
137 }
138 }
139
140 return TranslateJvmtiFrameInfoArray(env, frames.get(), count);
141}
142
Andreas Gampe46651672017-04-07 09:00:04 -0700143extern "C" JNIEXPORT jobjectArray JNICALL Java_art_AllTraces_getAllStackTraces(
Andreas Gampea1a27c62017-01-11 16:37:16 -0800144 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint max) {
Andreas Gampea1a27c62017-01-11 16:37:16 -0800145 jint thread_count;
146 jvmtiStackInfo* stack_infos;
147 {
148 jvmtiError result = jvmti_env->GetAllStackTraces(max, &stack_infos, &thread_count);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700149 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800150 return nullptr;
151 }
152 }
153
154 auto callback = [&](jint thread_index) -> jobject {
155 auto inner_callback = [&](jint index) -> jobject {
156 if (index == 0) {
157 return stack_infos[thread_index].thread;
158 } else {
159 return TranslateJvmtiFrameInfoArray(env,
160 stack_infos[thread_index].frame_buffer,
161 stack_infos[thread_index].frame_count);
162 }
163 };
164 return CreateObjectArray(env, 2, "java/lang/Object", inner_callback);
165 };
166 jobjectArray ret = CreateObjectArray(env, thread_count, "[Ljava/lang/Object;", callback);
167 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(stack_infos));
168 return ret;
169}
170
Andreas Gampe46651672017-04-07 09:00:04 -0700171extern "C" JNIEXPORT jobjectArray JNICALL Java_art_ThreadListTraces_getThreadListStackTraces(
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800172 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobjectArray jthreads, jint max) {
173 jint thread_count = env->GetArrayLength(jthreads);
174 std::unique_ptr<jthread[]> threads(new jthread[thread_count]);
175 for (jint i = 0; i != thread_count; ++i) {
176 threads[i] = env->GetObjectArrayElement(jthreads, i);
177 }
178
179 jvmtiStackInfo* stack_infos;
180 {
181 jvmtiError result = jvmti_env->GetThreadListStackTraces(thread_count,
182 threads.get(),
183 max,
184 &stack_infos);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700185 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampea1a27c62017-01-11 16:37:16 -0800186 return nullptr;
187 }
188 }
189
190 auto callback = [&](jint thread_index) -> jobject {
191 auto inner_callback = [&](jint index) -> jobject {
192 if (index == 0) {
193 return stack_infos[thread_index].thread;
194 } else {
195 return TranslateJvmtiFrameInfoArray(env,
196 stack_infos[thread_index].frame_buffer,
197 stack_infos[thread_index].frame_count);
198 }
199 };
200 return CreateObjectArray(env, 2, "java/lang/Object", inner_callback);
201 };
202 jobjectArray ret = CreateObjectArray(env, thread_count, "[Ljava/lang/Object;", callback);
203 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(stack_infos));
204 return ret;
205}
206
Andreas Gampe46651672017-04-07 09:00:04 -0700207extern "C" JNIEXPORT jint JNICALL Java_art_Frames_getFrameCount(
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800208 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread) {
209 jint count;
210 jvmtiError result = jvmti_env->GetFrameCount(thread, &count);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700211 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800212 return -1;
213 }
214 return count;
215}
216
Andreas Gampe46651672017-04-07 09:00:04 -0700217extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Frames_getFrameLocation(
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800218 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint depth) {
219 jmethodID method;
220 jlocation location;
221
222 jvmtiError result = jvmti_env->GetFrameLocation(thread, depth, &method, &location);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700223 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800224 return nullptr;
225 }
226
227 auto callback = [&](jint index) -> jobject {
228 switch (index) {
229 case 0:
230 {
231 jclass decl_class;
232 jvmtiError class_result = jvmti_env->GetMethodDeclaringClass(method, &decl_class);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700233 if (JvmtiErrorToException(env, jvmti_env, class_result)) {
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800234 return nullptr;
235 }
236 jint modifiers;
237 jvmtiError mod_result = jvmti_env->GetMethodModifiers(method, &modifiers);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700238 if (JvmtiErrorToException(env, jvmti_env, mod_result)) {
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800239 return nullptr;
240 }
241 constexpr jint kStatic = 0x8;
242 return env->ToReflectedMethod(decl_class,
243 method,
244 (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE);
245 }
246 case 1:
247 return env->NewStringUTF(
248 android::base::StringPrintf("%x", static_cast<uint32_t>(location)).c_str());
249 }
250 LOG(FATAL) << "Unreachable";
251 UNREACHABLE();
252 };
253 jobjectArray ret = CreateObjectArray(env, 2, "java/lang/Object", callback);
254 return ret;
255}
256
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700257} // namespace Test911GetStackTrace
258} // namespace art