| /* |
| * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
| * Copyright (c) 2019, Google and/or its affiliates. All rights reserved. |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| * |
| * This code is free software; you can redistribute it and/or modify it |
| * under the terms of the GNU General Public License version 2 only, as |
| * published by the Free Software Foundation. |
| * |
| * This code is distributed in the hope that it will be useful, but WITHOUT |
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| * version 2 for more details (a copy is included in the LICENSE file that |
| * accompanied this code). |
| * |
| * You should have received a copy of the GNU General Public License version |
| * 2 along with this work; if not, write to the Free Software Foundation, |
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| * |
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| * or visit www.oracle.com if you need additional information or have any |
| * questions. |
| */ |
| |
| #include <assert.h> |
| #include <dlfcn.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include "jvmti.h" |
| |
| static jvmtiEnv* jvmti; |
| |
| template <class T> |
| class JvmtiDeallocator { |
| public: |
| JvmtiDeallocator() { |
| elem_ = NULL; |
| } |
| |
| ~JvmtiDeallocator() { |
| jvmti->Deallocate(reinterpret_cast<unsigned char*>(elem_)); |
| } |
| |
| T* get_addr() { |
| return &elem_; |
| } |
| |
| T get() { |
| return elem_; |
| } |
| |
| private: |
| T elem_; |
| }; |
| |
| static void GetJMethodIDs(jclass klass) { |
| jint method_count = 0; |
| JvmtiDeallocator<jmethodID*> methods; |
| jvmtiError err = jvmti->GetClassMethods(klass, &method_count, methods.get_addr()); |
| |
| // If ever the GetClassMethods fails, just ignore it, it was worth a try. |
| if (err != JVMTI_ERROR_NONE) { |
| fprintf(stderr, "GetJMethodIDs: Error in GetClassMethods: %d\n", err); |
| } |
| } |
| |
| // AsyncGetCallTrace needs class loading events to be turned on! |
| static void JNICALL OnClassLoad(jvmtiEnv *jvmti, JNIEnv *jni_env, |
| jthread thread, jclass klass) { |
| } |
| |
| static void JNICALL OnClassPrepare(jvmtiEnv *jvmti, JNIEnv *jni_env, |
| jthread thread, jclass klass) { |
| // We need to do this to "prime the pump" and get jmethodIDs primed. |
| GetJMethodIDs(klass); |
| } |
| |
| static void JNICALL OnVMInit(jvmtiEnv *jvmti, JNIEnv *jni_env, jthread thread) { |
| jint class_count = 0; |
| |
| // Get any previously loaded classes that won't have gone through the |
| // OnClassPrepare callback to prime the jmethods for AsyncGetCallTrace. |
| JvmtiDeallocator<jclass*> classes; |
| jvmtiError err = jvmti->GetLoadedClasses(&class_count, classes.get_addr()); |
| if (err != JVMTI_ERROR_NONE) { |
| fprintf(stderr, "OnVMInit: Error in GetLoadedClasses: %d\n", err); |
| return; |
| } |
| |
| // Prime any class already loaded and try to get the jmethodIDs set up. |
| jclass *classList = classes.get(); |
| for (int i = 0; i < class_count; ++i) { |
| GetJMethodIDs(classList[i]); |
| } |
| } |
| |
| extern "C" { |
| |
| static |
| jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { |
| jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION); |
| if (res != JNI_OK || jvmti == NULL) { |
| fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n"); |
| return JNI_ERR; |
| } |
| |
| jvmtiError err; |
| jvmtiCapabilities caps; |
| memset(&caps, 0, sizeof(caps)); |
| caps.can_get_line_numbers = 1; |
| caps.can_get_source_file_name = 1; |
| |
| err = jvmti->AddCapabilities(&caps); |
| if (err != JVMTI_ERROR_NONE) { |
| fprintf(stderr, "AgentInitialize: Error in AddCapabilities: %d\n", err); |
| return JNI_ERR; |
| } |
| |
| jvmtiEventCallbacks callbacks; |
| memset(&callbacks, 0, sizeof(callbacks)); |
| callbacks.ClassLoad = &OnClassLoad; |
| callbacks.VMInit = &OnVMInit; |
| callbacks.ClassPrepare = &OnClassPrepare; |
| |
| err = jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks)); |
| if (err != JVMTI_ERROR_NONE) { |
| fprintf(stderr, "AgentInitialize: Error in SetEventCallbacks: %d\n", err); |
| return JNI_ERR; |
| } |
| |
| err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL); |
| if (err != JVMTI_ERROR_NONE) { |
| fprintf(stderr, "AgentInitialize: Error in SetEventNotificationMode for CLASS_LOAD: %d\n", err); |
| return JNI_ERR; |
| } |
| |
| err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL); |
| if (err != JVMTI_ERROR_NONE) { |
| fprintf(stderr, |
| "AgentInitialize: Error in SetEventNotificationMode for CLASS_PREPARE: %d\n", |
| err); |
| return JNI_ERR; |
| } |
| |
| err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL); |
| if (err != JVMTI_ERROR_NONE) { |
| fprintf( |
| stderr, "AgentInitialize: Error in SetEventNotificationMode for VM_INIT: %d\n", |
| err); |
| return JNI_ERR; |
| } |
| |
| return JNI_OK; |
| } |
| |
| JNIEXPORT |
| jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { |
| return Agent_Initialize(jvm, options, reserved); |
| } |
| |
| JNIEXPORT |
| jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) { |
| return Agent_Initialize(jvm, options, reserved); |
| } |
| |
| JNIEXPORT |
| jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) { |
| return JNI_VERSION_1_8; |
| } |
| |
| // A copy of the ASGCT data structures. |
| typedef struct { |
| jint lineno; // line number in the source file |
| jmethodID method_id; // method executed in this frame |
| } ASGCT_CallFrame; |
| |
| typedef struct { |
| JNIEnv *env_id; // Env where trace was recorded |
| jint num_frames; // number of frames in this trace |
| ASGCT_CallFrame *frames; // frames |
| } ASGCT_CallTrace; |
| |
| typedef void (*ASGCTType)(ASGCT_CallTrace *, jint, void *); |
| |
| JNIEXPORT jboolean JNICALL |
| Java_MyPackage_ASGCTBaseTest_checkAsyncGetCallTraceCall(JNIEnv* env, jclass cls) { |
| ASGCTType agct = reinterpret_cast<ASGCTType>(dlsym(RTLD_DEFAULT, "AsyncGetCallTrace")); |
| |
| const int MAX_DEPTH = 16; |
| ASGCT_CallTrace trace; |
| ASGCT_CallFrame frames[MAX_DEPTH]; |
| trace.frames = frames; |
| trace.env_id = env; |
| trace.num_frames = 0; |
| |
| if (agct == NULL) { |
| fprintf(stderr, "AsyncGetCallTrace not found.\n"); |
| return false; |
| } |
| |
| agct(&trace, MAX_DEPTH, NULL); |
| |
| // For now, just check that the first frame is (-3, checkAsyncGetCallTraceCall). |
| if (trace.num_frames <= 0) { |
| fprintf(stderr, "The num_frames must be positive: %d\n", trace.num_frames); |
| return false; |
| } |
| |
| // AsyncGetCallTrace returns -3 as line number for a native frame. |
| if (trace.frames[0].lineno != -3) { |
| fprintf(stderr, "lineno is not -3 as expected: %d\n", trace.frames[0].lineno); |
| return false; |
| } |
| |
| JvmtiDeallocator<char*> name; |
| if (trace.frames[0].method_id == NULL) { |
| fprintf(stderr, "First frame method_id is NULL\n"); |
| return false; |
| } |
| |
| jvmtiError err = jvmti->GetMethodName(trace.frames[0].method_id, name.get_addr(), NULL, NULL); |
| if (err != JVMTI_ERROR_NONE) { |
| fprintf(stderr, "checkAsyncGetCallTrace: Error in GetMethodName: %d\n", err); |
| return false; |
| } |
| |
| if (name.get() == NULL) { |
| fprintf(stderr, "Name is NULL\n"); |
| return false; |
| } |
| |
| if (strcmp(name.get(), "checkAsyncGetCallTraceCall") != 0) { |
| fprintf(stderr, "Name is not checkAsyncGetCallTraceCall: %s\n", name.get()); |
| return false; |
| } |
| |
| // AsyncGetCallTrace and GetStackTrace should return comparable frames |
| // so we obtain the frames using GetStackTrace and compare them. |
| |
| jthread thread; |
| jvmti->GetCurrentThread(&thread); |
| jvmtiFrameInfo gstFrames[MAX_DEPTH]; |
| jint gstCount = 0; |
| |
| jvmti->GetStackTrace(thread, 0, MAX_DEPTH, gstFrames, &gstCount); |
| |
| if (gstCount != trace.num_frames) { |
| fprintf(stderr, "GetStackTrace and AsyncGetCallTrace return different number of frames: %d vs %d)", gstCount, trace.num_frames); |
| return false; |
| } |
| |
| for (int i = 0; i < trace.num_frames; ++i) { |
| if (trace.frames[i].lineno == -3) { |
| if (gstFrames[i].location != -1) { |
| fprintf(stderr, "%d: ASGCT found native frame but GST did not\n", i); |
| return false; |
| } |
| } else { |
| if (gstFrames[i].method != trace.frames[i].method_id) { |
| fprintf(stderr, "%d: method_id mismatch: %p vs %p\n", i, gstFrames[i].method, trace.frames[i].method_id); |
| return false; |
| } |
| } |
| } |
| return true; |
| } |
| |
| } |