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" |
| 27 | #include "openjdkjvmti/jvmti.h" |
| 28 | #include "ScopedLocalRef.h" |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 29 | #include "ti-agent/common_helper.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 30 | #include "ti-agent/common_load.h" |
| 31 | |
| 32 | namespace art { |
| 33 | namespace Test911GetStackTrace { |
| 34 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 35 | using android::base::StringPrintf; |
| 36 | |
Andreas Gampe | 53ae780 | 2017-01-19 21:13:46 -0800 | [diff] [blame] | 37 | extern "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 Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 45 | static 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 Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 62 | static jobjectArray TranslateJvmtiFrameInfoArray(JNIEnv* env, |
| 63 | jvmtiFrameInfo* frames, |
| 64 | jint count) { |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 65 | auto callback = [&](jint method_index) -> jobjectArray { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 66 | char* name; |
| 67 | char* sig; |
| 68 | char* gen; |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 69 | { |
| 70 | jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 71 | if (JvmtiErrorToException(env, result2)) { |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 72 | return nullptr; |
| 73 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 74 | } |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 75 | |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 76 | 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 Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 89 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 90 | return nullptr; |
| 91 | } |
| 92 | line_number_table = nullptr; |
| 93 | line_number_count = 0; |
| 94 | } |
| 95 | } |
| 96 | |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 97 | 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 Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 103 | case 2: |
| 104 | return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str()); |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 105 | 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 Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 111 | } |
| 112 | LOG(FATAL) << "Unreachable"; |
| 113 | UNREACHABLE(); |
| 114 | }; |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 115 | jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 116 | |
| 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 Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 126 | if (line_number_table != nullptr) { |
| 127 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table)); |
| 128 | } |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 129 | |
| 130 | return inner_array; |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 131 | }; |
Andreas Gampe | ceafe35 | 2016-12-12 18:49:33 -0800 | [diff] [blame] | 132 | return CreateObjectArray(env, count, "[Ljava/lang/String;", callback); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Andreas Gampe | 966de9e | 2017-01-12 20:51:02 -0800 | [diff] [blame] | 135 | extern "C" JNIEXPORT jobjectArray JNICALL Java_PrintThread_getStackTrace( |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 136 | 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 Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 142 | if (JvmtiErrorToException(env, result)) { |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 143 | return nullptr; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return TranslateJvmtiFrameInfoArray(env, frames.get(), count); |
| 148 | } |
| 149 | |
Andreas Gampe | 966de9e | 2017-01-12 20:51:02 -0800 | [diff] [blame] | 150 | extern "C" JNIEXPORT jobjectArray JNICALL Java_AllTraces_getAllStackTraces( |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 151 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint max) { |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 152 | jint thread_count; |
| 153 | jvmtiStackInfo* stack_infos; |
| 154 | { |
| 155 | jvmtiError result = jvmti_env->GetAllStackTraces(max, &stack_infos, &thread_count); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 156 | 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 | |
| 178 | extern "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 Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 193 | 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 Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 214 | extern "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 | |
| 224 | extern "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 Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 264 | } // namespace Test911GetStackTrace |
| 265 | } // namespace art |