| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 17 | #include <inttypes.h> |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 18 | #include <memory> |
| 19 | #include <stdio.h> |
| 20 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 24 | #include "base/logging.h" |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 25 | #include "base/macros.h" |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 26 | #include "jni.h" |
| Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 27 | #include "jvmti.h" |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 28 | #include "ScopedLocalRef.h" |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 29 | |
| 30 | // Test infrastructure |
| 31 | #include "jni_binder.h" |
| 32 | #include "jni_helper.h" |
| 33 | #include "jvmti_helper.h" |
| 34 | #include "test_env.h" |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | namespace Test911GetStackTrace { |
| 38 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 39 | using android::base::StringPrintf; |
| 40 | |
| Andreas Gampe | 53ae780 | 2017-01-19 21:13:46 -0800 | [diff] [blame] | 41 | extern "C" JNIEXPORT void JNICALL Java_Main_bindTest911Classes( |
| 42 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { |
| 43 | BindFunctions(jvmti_env, env, "AllTraces"); |
| 44 | BindFunctions(jvmti_env, env, "Frames"); |
| 45 | BindFunctions(jvmti_env, env, "PrintThread"); |
| 46 | BindFunctions(jvmti_env, env, "ThreadListTraces"); |
| 47 | } |
| 48 | |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 49 | static jint FindLineNumber(jint line_number_count, |
| 50 | jvmtiLineNumberEntry* line_number_table, |
| 51 | jlocation location) { |
| 52 | if (line_number_table == nullptr) { |
| 53 | return -2; |
| 54 | } |
| 55 | |
| 56 | jint line_number = -1; |
| 57 | for (jint i = 0; i != line_number_count; ++i) { |
| 58 | if (line_number_table[i].start_location > location) { |
| 59 | return line_number; |
| 60 | } |
| 61 | line_number = line_number_table[i].line_number; |
| 62 | } |
| 63 | return line_number; |
| 64 | } |
| 65 | |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 66 | static jobjectArray TranslateJvmtiFrameInfoArray(JNIEnv* env, |
| 67 | jvmtiFrameInfo* frames, |
| 68 | jint count) { |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 69 | auto callback = [&](jint method_index) -> jobjectArray { |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 70 | char* name; |
| 71 | char* sig; |
| 72 | char* gen; |
| Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 73 | { |
| 74 | jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 75 | if (JvmtiErrorToException(env, jvmti_env, result2)) { |
| Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 76 | return nullptr; |
| 77 | } |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 78 | } |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 79 | |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 80 | jint line_number_count; |
| 81 | jvmtiLineNumberEntry* line_number_table; |
| 82 | { |
| 83 | jvmtiError line_result = jvmti_env->GetLineNumberTable(frames[method_index].method, |
| 84 | &line_number_count, |
| 85 | &line_number_table); |
| 86 | if (line_result != JVMTI_ERROR_NONE) { |
| 87 | // Accept absent info and native method errors. |
| 88 | if (line_result != JVMTI_ERROR_ABSENT_INFORMATION && |
| 89 | line_result != JVMTI_ERROR_NATIVE_METHOD) { |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 90 | JvmtiErrorToException(env, jvmti_env, line_result); |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 91 | return nullptr; |
| 92 | } |
| 93 | line_number_table = nullptr; |
| 94 | line_number_count = 0; |
| 95 | } |
| 96 | } |
| 97 | |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 98 | auto inner_callback = [&](jint component_index) -> jstring { |
| 99 | switch (component_index) { |
| 100 | case 0: |
| 101 | return (name == nullptr) ? nullptr : env->NewStringUTF(name); |
| 102 | case 1: |
| 103 | return (sig == nullptr) ? nullptr : env->NewStringUTF(sig); |
| Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 104 | case 2: |
| 105 | return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str()); |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 106 | case 3: { |
| 107 | jint line_number = FindLineNumber(line_number_count, |
| 108 | line_number_table, |
| 109 | frames[method_index].location); |
| 110 | return env->NewStringUTF(StringPrintf("%d", line_number).c_str()); |
| 111 | } |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 112 | } |
| 113 | LOG(FATAL) << "Unreachable"; |
| 114 | UNREACHABLE(); |
| 115 | }; |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 116 | jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback); |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 117 | |
| 118 | if (name != nullptr) { |
| 119 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name)); |
| 120 | } |
| 121 | if (sig != nullptr) { |
| 122 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig)); |
| 123 | } |
| 124 | if (gen != nullptr) { |
| 125 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen)); |
| 126 | } |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 127 | if (line_number_table != nullptr) { |
| 128 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table)); |
| 129 | } |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 130 | |
| 131 | return inner_array; |
| Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 132 | }; |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 133 | return CreateObjectArray(env, count, "[Ljava/lang/String;", callback); |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| Andreas Gampe | 966de9e | 2017-01-12 20:51:02 -0800 | [diff] [blame] | 136 | extern "C" JNIEXPORT jobjectArray JNICALL Java_PrintThread_getStackTrace( |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 137 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) { |
| 138 | std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]); |
| 139 | |
| 140 | jint count; |
| 141 | { |
| 142 | jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 143 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 144 | return nullptr; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return TranslateJvmtiFrameInfoArray(env, frames.get(), count); |
| 149 | } |
| 150 | |
| Andreas Gampe | 966de9e | 2017-01-12 20:51:02 -0800 | [diff] [blame] | 151 | extern "C" JNIEXPORT jobjectArray JNICALL Java_AllTraces_getAllStackTraces( |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 152 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint max) { |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 153 | jint thread_count; |
| 154 | jvmtiStackInfo* stack_infos; |
| 155 | { |
| 156 | jvmtiError result = jvmti_env->GetAllStackTraces(max, &stack_infos, &thread_count); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 157 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 158 | return nullptr; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | auto callback = [&](jint thread_index) -> jobject { |
| 163 | auto inner_callback = [&](jint index) -> jobject { |
| 164 | if (index == 0) { |
| 165 | return stack_infos[thread_index].thread; |
| 166 | } else { |
| 167 | return TranslateJvmtiFrameInfoArray(env, |
| 168 | stack_infos[thread_index].frame_buffer, |
| 169 | stack_infos[thread_index].frame_count); |
| 170 | } |
| 171 | }; |
| 172 | return CreateObjectArray(env, 2, "java/lang/Object", inner_callback); |
| 173 | }; |
| 174 | jobjectArray ret = CreateObjectArray(env, thread_count, "[Ljava/lang/Object;", callback); |
| 175 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(stack_infos)); |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | extern "C" JNIEXPORT jobjectArray JNICALL Java_ThreadListTraces_getThreadListStackTraces( |
| 180 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobjectArray jthreads, jint max) { |
| 181 | jint thread_count = env->GetArrayLength(jthreads); |
| 182 | std::unique_ptr<jthread[]> threads(new jthread[thread_count]); |
| 183 | for (jint i = 0; i != thread_count; ++i) { |
| 184 | threads[i] = env->GetObjectArrayElement(jthreads, i); |
| 185 | } |
| 186 | |
| 187 | jvmtiStackInfo* stack_infos; |
| 188 | { |
| 189 | jvmtiError result = jvmti_env->GetThreadListStackTraces(thread_count, |
| 190 | threads.get(), |
| 191 | max, |
| 192 | &stack_infos); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 193 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 194 | return nullptr; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | auto callback = [&](jint thread_index) -> jobject { |
| 199 | auto inner_callback = [&](jint index) -> jobject { |
| 200 | if (index == 0) { |
| 201 | return stack_infos[thread_index].thread; |
| 202 | } else { |
| 203 | return TranslateJvmtiFrameInfoArray(env, |
| 204 | stack_infos[thread_index].frame_buffer, |
| 205 | stack_infos[thread_index].frame_count); |
| 206 | } |
| 207 | }; |
| 208 | return CreateObjectArray(env, 2, "java/lang/Object", inner_callback); |
| 209 | }; |
| 210 | jobjectArray ret = CreateObjectArray(env, thread_count, "[Ljava/lang/Object;", callback); |
| 211 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(stack_infos)); |
| 212 | return ret; |
| 213 | } |
| 214 | |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 215 | extern "C" JNIEXPORT jint JNICALL Java_Frames_getFrameCount( |
| 216 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread) { |
| 217 | jint count; |
| 218 | jvmtiError result = jvmti_env->GetFrameCount(thread, &count); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 219 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 220 | return -1; |
| 221 | } |
| 222 | return count; |
| 223 | } |
| 224 | |
| 225 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Frames_getFrameLocation( |
| 226 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint depth) { |
| 227 | jmethodID method; |
| 228 | jlocation location; |
| 229 | |
| 230 | jvmtiError result = jvmti_env->GetFrameLocation(thread, depth, &method, &location); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 231 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 232 | return nullptr; |
| 233 | } |
| 234 | |
| 235 | auto callback = [&](jint index) -> jobject { |
| 236 | switch (index) { |
| 237 | case 0: |
| 238 | { |
| 239 | jclass decl_class; |
| 240 | jvmtiError class_result = jvmti_env->GetMethodDeclaringClass(method, &decl_class); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 241 | if (JvmtiErrorToException(env, jvmti_env, class_result)) { |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 242 | return nullptr; |
| 243 | } |
| 244 | jint modifiers; |
| 245 | jvmtiError mod_result = jvmti_env->GetMethodModifiers(method, &modifiers); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 246 | if (JvmtiErrorToException(env, jvmti_env, mod_result)) { |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 247 | return nullptr; |
| 248 | } |
| 249 | constexpr jint kStatic = 0x8; |
| 250 | return env->ToReflectedMethod(decl_class, |
| 251 | method, |
| 252 | (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE); |
| 253 | } |
| 254 | case 1: |
| 255 | return env->NewStringUTF( |
| 256 | android::base::StringPrintf("%x", static_cast<uint32_t>(location)).c_str()); |
| 257 | } |
| 258 | LOG(FATAL) << "Unreachable"; |
| 259 | UNREACHABLE(); |
| 260 | }; |
| 261 | jobjectArray ret = CreateObjectArray(env, 2, "java/lang/Object", callback); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 265 | } // namespace Test911GetStackTrace |
| 266 | } // namespace art |