blob: b3e8bc3b1fafd33ee924f17da793f19a59edf2e6 [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
17#include "stack_trace.h"
18
Andreas Gampe2340e3f2016-12-12 19:37:19 -080019#include <inttypes.h>
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070020#include <memory>
21#include <stdio.h>
22
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070025#include "base/logging.h"
Andreas Gampeceafe352016-12-12 18:49:33 -080026#include "base/macros.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070027#include "jni.h"
28#include "openjdkjvmti/jvmti.h"
29#include "ScopedLocalRef.h"
Andreas Gampe336c3c32016-11-08 17:02:19 -080030#include "ti-agent/common_helper.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070031#include "ti-agent/common_load.h"
32
33namespace art {
34namespace Test911GetStackTrace {
35
Andreas Gampe46ee31b2016-12-14 10:11:49 -080036using android::base::StringPrintf;
37
Andreas Gampeda3e5612016-12-13 19:00:53 -080038static jint FindLineNumber(jint line_number_count,
39 jvmtiLineNumberEntry* line_number_table,
40 jlocation location) {
41 if (line_number_table == nullptr) {
42 return -2;
43 }
44
45 jint line_number = -1;
46 for (jint i = 0; i != line_number_count; ++i) {
47 if (line_number_table[i].start_location > location) {
48 return line_number;
49 }
50 line_number = line_number_table[i].line_number;
51 }
52 return line_number;
53}
54
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070055extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getStackTrace(
56 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jint start, jint max) {
57 std::unique_ptr<jvmtiFrameInfo[]> frames(new jvmtiFrameInfo[max]);
58
59 jint count;
Andreas Gampe336c3c32016-11-08 17:02:19 -080060 {
61 jvmtiError result = jvmti_env->GetStackTrace(thread, start, max, frames.get(), &count);
62 if (result != JVMTI_ERROR_NONE) {
63 char* err;
64 jvmti_env->GetErrorName(result, &err);
65 printf("Failure running GetStackTrace: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080066 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe336c3c32016-11-08 17:02:19 -080067 return nullptr;
68 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070069 }
70
Andreas Gampeceafe352016-12-12 18:49:33 -080071 auto callback = [&](jint method_index) -> jobjectArray {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070072 char* name;
73 char* sig;
74 char* gen;
Andreas Gampe336c3c32016-11-08 17:02:19 -080075 {
76 jvmtiError result2 = jvmti_env->GetMethodName(frames[method_index].method, &name, &sig, &gen);
77 if (result2 != JVMTI_ERROR_NONE) {
78 char* err;
79 jvmti_env->GetErrorName(result2, &err);
80 printf("Failure running GetMethodName: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080081 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe336c3c32016-11-08 17:02:19 -080082 return nullptr;
83 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070084 }
Andreas Gampeceafe352016-12-12 18:49:33 -080085
Andreas Gampeda3e5612016-12-13 19:00:53 -080086 jint line_number_count;
87 jvmtiLineNumberEntry* line_number_table;
88 {
89 jvmtiError line_result = jvmti_env->GetLineNumberTable(frames[method_index].method,
90 &line_number_count,
91 &line_number_table);
92 if (line_result != JVMTI_ERROR_NONE) {
93 // Accept absent info and native method errors.
94 if (line_result != JVMTI_ERROR_ABSENT_INFORMATION &&
95 line_result != JVMTI_ERROR_NATIVE_METHOD) {
96 char* err;
97 jvmti_env->GetErrorName(line_result, &err);
98 printf("Failure running GetLineNumberTable: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080099 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeda3e5612016-12-13 19:00:53 -0800100 return nullptr;
101 }
102 line_number_table = nullptr;
103 line_number_count = 0;
104 }
105 }
106
Andreas Gampeceafe352016-12-12 18:49:33 -0800107 auto inner_callback = [&](jint component_index) -> jstring {
108 switch (component_index) {
109 case 0:
110 return (name == nullptr) ? nullptr : env->NewStringUTF(name);
111 case 1:
112 return (sig == nullptr) ? nullptr : env->NewStringUTF(sig);
Andreas Gampe2340e3f2016-12-12 19:37:19 -0800113 case 2:
114 return env->NewStringUTF(StringPrintf("%" PRId64, frames[method_index].location).c_str());
Andreas Gampeda3e5612016-12-13 19:00:53 -0800115 case 3: {
116 jint line_number = FindLineNumber(line_number_count,
117 line_number_table,
118 frames[method_index].location);
119 return env->NewStringUTF(StringPrintf("%d", line_number).c_str());
120 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800121 }
122 LOG(FATAL) << "Unreachable";
123 UNREACHABLE();
124 };
Andreas Gampeda3e5612016-12-13 19:00:53 -0800125 jobjectArray inner_array = CreateObjectArray(env, 4, "java/lang/String", inner_callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700126
127 if (name != nullptr) {
128 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
129 }
130 if (sig != nullptr) {
131 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
132 }
133 if (gen != nullptr) {
134 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
135 }
Andreas Gampeda3e5612016-12-13 19:00:53 -0800136 if (line_number_table != nullptr) {
137 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(line_number_table));
138 }
Andreas Gampeceafe352016-12-12 18:49:33 -0800139
140 return inner_array;
Andreas Gampe336c3c32016-11-08 17:02:19 -0800141 };
Andreas Gampeceafe352016-12-12 18:49:33 -0800142 return CreateObjectArray(env, count, "[Ljava/lang/String;", callback);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700143}
144
145// Don't do anything
146jint OnLoad(JavaVM* vm,
147 char* options ATTRIBUTE_UNUSED,
148 void* reserved ATTRIBUTE_UNUSED) {
149 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
150 printf("Unable to get jvmti env!\n");
151 return 1;
152 }
Alex Lighte6574242016-08-17 09:56:24 -0700153 SetAllCapabilities(jvmti_env);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700154 return 0;
155}
156
157} // namespace Test911GetStackTrace
158} // namespace art