| 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 | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame^] | 18 | |
| 19 | #include <cstdio> |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 20 | #include <memory> |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 21 | |
| Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 22 | #include "android-base/logging.h" |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
| 24 | |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 25 | #include "jni.h" |
| Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 26 | #include "jvmti.h" |
| Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 27 | #include "scoped_local_ref.h" |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 28 | |
| 29 | // Test infrastructure |
| 30 | #include "jni_binder.h" |
| 31 | #include "jni_helper.h" |
| 32 | #include "jvmti_helper.h" |
| 33 | #include "test_env.h" |
| Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 34 | #include "ti_macros.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 | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 41 | static 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 Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 58 | static jobjectArray TranslateJvmtiFrameInfoArray(JNIEnv* env, |
| 59 | jvmtiFrameInfo* frames, |
| 60 | jint count) { |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 61 | auto callback = [&](jint method_index) -> jobjectArray { |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 62 | char* name; |
| 63 | char* sig; |
| 64 | char* gen; |
| Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 65 | { |
| 66 | jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 67 | if (JvmtiErrorToException(env, jvmti_env, result2)) { |
| Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 68 | return nullptr; |
| 69 | } |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 70 | } |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 71 | |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 72 | 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 Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 82 | JvmtiErrorToException(env, jvmti_env, line_result); |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 83 | return nullptr; |
| 84 | } |
| 85 | line_number_table = nullptr; |
| 86 | line_number_count = 0; |
| 87 | } |
| 88 | } |
| 89 | |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 90 | 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 Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 96 | case 2: |
| 97 | return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str()); |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 98 | 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 Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 104 | } |
| 105 | LOG(FATAL) << "Unreachable"; |
| 106 | UNREACHABLE(); |
| 107 | }; |
| Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 108 | jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback); |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 109 | |
| 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 Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 119 | if (line_number_table != nullptr) { |
| 120 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table)); |
| 121 | } |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 122 | |
| 123 | return inner_array; |
| Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 124 | }; |
| Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 125 | return CreateObjectArray(env, count, "[Ljava/lang/String;", callback); |
| Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 128 | extern "C" JNIEXPORT jobjectArray JNICALL Java_art_PrintThread_getStackTrace( |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 129 | 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 Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 135 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 136 | return nullptr; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return TranslateJvmtiFrameInfoArray(env, frames.get(), count); |
| 141 | } |
| 142 | |
| Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 143 | extern "C" JNIEXPORT jobjectArray JNICALL Java_art_AllTraces_getAllStackTraces( |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 144 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint max) { |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 145 | jint thread_count; |
| 146 | jvmtiStackInfo* stack_infos; |
| 147 | { |
| 148 | jvmtiError result = jvmti_env->GetAllStackTraces(max, &stack_infos, &thread_count); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 149 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 150 | 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 Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 171 | extern "C" JNIEXPORT jobjectArray JNICALL Java_art_ThreadListTraces_getThreadListStackTraces( |
| Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 172 | 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 Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 185 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 186 | 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 Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 207 | extern "C" JNIEXPORT jint JNICALL Java_art_Frames_getFrameCount( |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 208 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread) { |
| 209 | jint count; |
| 210 | jvmtiError result = jvmti_env->GetFrameCount(thread, &count); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 211 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 212 | return -1; |
| 213 | } |
| 214 | return count; |
| 215 | } |
| 216 | |
| Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 217 | extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Frames_getFrameLocation( |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 218 | 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 Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 223 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 224 | 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 Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 233 | if (JvmtiErrorToException(env, jvmti_env, class_result)) { |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 234 | return nullptr; |
| 235 | } |
| 236 | jint modifiers; |
| 237 | jvmtiError mod_result = jvmti_env->GetMethodModifiers(method, &modifiers); |
| Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 238 | if (JvmtiErrorToException(env, jvmti_env, mod_result)) { |
| Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 239 | 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 Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 257 | } // namespace Test911GetStackTrace |
| 258 | } // namespace art |