|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <cstdlib> |
| 25 | +#include <cstring> |
| 26 | +#include <jvmti.h> |
| 27 | +#include "jvmti_common.h" |
| 28 | + |
| 29 | +extern "C" { |
| 30 | + |
| 31 | +const int MAX_COUNT = 50; |
| 32 | +static jvmtiEnv *jvmti; |
| 33 | + |
| 34 | +JNIEXPORT jobjectArray JNICALL |
| 35 | +Java_VirtualStackTraceTest_getStackTrace(JNIEnv* jni, jclass clazz) { |
| 36 | + jvmtiError err; |
| 37 | + jint count = 0; |
| 38 | + jint skipped = 0; |
| 39 | + |
| 40 | + jobject visibleFrames[MAX_COUNT]; |
| 41 | + jvmtiFrameInfo frameInfo[MAX_COUNT]; |
| 42 | + |
| 43 | + err = jvmti->GetStackTrace(NULL, 0, MAX_COUNT, frameInfo, &count); |
| 44 | + check_jvmti_status(jni, err, "event handler: error in JVMTI GetStackTrace call"); |
| 45 | + |
| 46 | + for (int idx = 0; idx < count; idx++) { |
| 47 | + jclass declaringClass = NULL; |
| 48 | + char *clasSignature = NULL; |
| 49 | + char *methodName = NULL; |
| 50 | + |
| 51 | + err = jvmti->GetMethodDeclaringClass(frameInfo[idx].method, &declaringClass); |
| 52 | + check_jvmti_status(jni, err, "event handler: error in JVMTI GetMethodDeclaringClass call"); |
| 53 | + |
| 54 | + err = jvmti->GetClassSignature(declaringClass, &clasSignature, NULL); |
| 55 | + check_jvmti_status(jni, err, "event handler: error in JVMTI GetClassSignature call"); |
| 56 | + |
| 57 | + err = jvmti->GetMethodName(frameInfo[idx].method, &methodName, NULL, NULL); |
| 58 | + check_jvmti_status(jni, err, "event handler: error in JVMTI GetMethodName call"); |
| 59 | + |
| 60 | + if (strchr(clasSignature, '.')) { |
| 61 | + skipped++; |
| 62 | + continue; |
| 63 | + } |
| 64 | + visibleFrames[idx - skipped] = jni->NewStringUTF(methodName); |
| 65 | + |
| 66 | + jvmti->Deallocate(reinterpret_cast<unsigned char*>(methodName)); |
| 67 | + jvmti->Deallocate(reinterpret_cast<unsigned char*>(clasSignature)); |
| 68 | + } |
| 69 | + jobjectArray methodNames = jni->NewObjectArray(count - skipped, jni->FindClass("java/lang/String"), NULL); |
| 70 | + for (int idx = 0; idx < count - skipped; idx++) { |
| 71 | + jni->SetObjectArrayElement(methodNames, idx, visibleFrames[idx]); |
| 72 | + } |
| 73 | + print_stack_trace(jvmti, jni, NULL); |
| 74 | + |
| 75 | + return methodNames; |
| 76 | +} |
| 77 | + |
| 78 | +JNIEXPORT jint JNICALL |
| 79 | +Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { |
| 80 | + LOG("Agent_OnLoad started\n"); |
| 81 | + if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) { |
| 82 | + return JNI_ERR; |
| 83 | + } |
| 84 | + return JNI_OK; |
| 85 | +} |
| 86 | + |
| 87 | +JNIEXPORT jint JNICALL |
| 88 | +Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) { |
| 89 | + LOG("Agent_OnAttach started\n"); |
| 90 | + if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) { |
| 91 | + return JNI_ERR; |
| 92 | + } |
| 93 | + return JNI_OK; |
| 94 | +} |
| 95 | + |
| 96 | +} // extern "C" |
0 commit comments