Search in sources :

Example 1 with JvmtiFrameInfo

use of com.oracle.svm.jvmtiagentbase.jvmti.JvmtiFrameInfo in project graal by oracle.

the class BreakpointInterceptor method loadClass.

private static boolean loadClass(JNIEnvironment jni, Breakpoint bp, InterceptedState state) {
    assert experimentalClassLoaderSupport;
    /*
         * There is no easy way to tell if it was the virtual machine that called the class loader
         * because if so, the caller is simply the Java method that triggered loading the class. We
         * have to check the current bytecode in the caller method whether it is in fact a call to
         * loadClass().
         */
    JNIObjectHandle callerClass = nullHandle();
    JvmtiFrameInfo frameInfo = StackValue.get(JvmtiFrameInfo.class);
    CIntPointer frameCountPtr = StackValue.get(CIntPointer.class);
    if (jvmtiFunctions().GetStackTrace().invoke(jvmtiEnv(), nullHandle(), 1, 1, (WordPointer) frameInfo, frameCountPtr) == JvmtiError.JVMTI_ERROR_NONE && frameCountPtr.read() == 1) {
        callerClass = getMethodDeclaringClass(frameInfo.getMethod());
        if (callerClass.notEqual(nullHandle()) && jniFunctions().getIsAssignableFrom().invoke(jni, callerClass, agent.handles().javaLangClassLoader)) {
            // ignore recursive class loader calls, we must have seen the root invocation
            return true;
        }
        MethodLocation location = new MethodLocation(frameInfo.getMethod(), NumUtil.safeToInt(frameInfo.getLocation()));
        if (!observedExplicitLoadClassCallSites.containsKey(location)) {
            if (!isLoadClassInvocation(callerClass, location.method, location.bci, bp.specification.methodName, bp.specification.signature)) {
                return true;
            }
            observedExplicitLoadClassCallSites.put(location, Boolean.TRUE);
        }
    }
    JNIObjectHandle self = getObjectArgument(0);
    JNIObjectHandle name = getObjectArgument(1);
    String className = fromJniString(jni, name);
    JNIObjectHandle clazz = Support.callObjectMethodL(jni, self, bp.method, name);
    if (clearException(jni)) {
        clazz = nullHandle();
    }
    traceBreakpoint(jni, bp.clazz, nullHandle(), callerClass, bp.specification.methodName, clazz.notEqual(nullHandle()), state.getFullStackTraceOrNull(), className);
    return true;
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) JvmtiFrameInfo(com.oracle.svm.jvmtiagentbase.jvmti.JvmtiFrameInfo) Support.fromJniString(com.oracle.svm.jvmtiagentbase.Support.fromJniString) Support.toCString(com.oracle.svm.jvmtiagentbase.Support.toCString) Support.fromCString(com.oracle.svm.jvmtiagentbase.Support.fromCString) JNIObjectHandle(com.oracle.svm.jni.nativeapi.JNIObjectHandle)

Example 2 with JvmtiFrameInfo

use of com.oracle.svm.jvmtiagentbase.jvmti.JvmtiFrameInfo in project graal by oracle.

the class EagerlyLoadedJavaStackAccess method getCurrentThreadStackTrace.

/**
 * Returns the stack trace of the current thread.
 *
 * The returned array contains the current thread's stack, with the first element representing
 * the top of the stack. Elements in the array correspond to the raw value of JNIMethodId of the
 * particular method on the stack.
 */
private static JNIMethodId[] getCurrentThreadStackTrace() {
    JvmtiEnv jvmti = Support.jvmtiEnv();
    int threadStackFrameCount = getCurrentThreadStackFrameCount(jvmti);
    if (threadStackFrameCount < 0) {
        return new JNIMethodId[0];
    }
    /*
         * If we are unlucky, we may have a phase change while the stack trace entries are being
         * initialized.
         */
    boolean wrongPhase = false;
    int iterationCount = threadStackFrameCount / STACK_FRAMES_PER_ITERATION;
    if (threadStackFrameCount % STACK_FRAMES_PER_ITERATION > 0) {
        iterationCount = iterationCount + 1;
    }
    JNIMethodId[] stackTraceJMethodIDs = new JNIMethodId[threadStackFrameCount];
    int frameInfoSize = SizeOf.get(JvmtiFrameInfo.class);
    Pointer stackFramesBuffer = UnmanagedMemory.malloc(frameInfoSize * STACK_FRAMES_PER_ITERATION);
    CIntPointer readFrameCount = StackValue.get(CIntPointer.class);
    /* Build the stack trace incrementally in chunks of STACK_FRAMES_PER_ITERATION frames. */
    for (int i = 0; i < iterationCount; ++i) {
        JvmtiError error = jvmti.getFunctions().GetStackTrace().invoke(jvmti, nullHandle(), i * STACK_FRAMES_PER_ITERATION, STACK_FRAMES_PER_ITERATION, (WordPointer) stackFramesBuffer, readFrameCount);
        if (error.equals(JvmtiError.JVMTI_ERROR_WRONG_PHASE)) {
            wrongPhase = true;
            break;
        }
        check(error);
        for (int j = 0; j < readFrameCount.read(); ++j) {
            JvmtiFrameInfo frameInfo = (JvmtiFrameInfo) stackFramesBuffer.add(j * frameInfoSize);
            stackTraceJMethodIDs[i * STACK_FRAMES_PER_ITERATION + j] = frameInfo.getMethod();
        }
    }
    UnmanagedMemory.free(stackFramesBuffer);
    return wrongPhase ? null : stackTraceJMethodIDs;
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) JvmtiError(com.oracle.svm.jvmtiagentbase.jvmti.JvmtiError) JvmtiFrameInfo(com.oracle.svm.jvmtiagentbase.jvmti.JvmtiFrameInfo) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) Pointer(org.graalvm.word.Pointer) JvmtiEnv(com.oracle.svm.jvmtiagentbase.jvmti.JvmtiEnv) JNIMethodId(com.oracle.svm.jni.nativeapi.JNIMethodId)

Example 3 with JvmtiFrameInfo

use of com.oracle.svm.jvmtiagentbase.jvmti.JvmtiFrameInfo in project graal by oracle.

the class Support method getCallerMethod.

public static JNIMethodId getCallerMethod(int depth) {
    JvmtiFrameInfo frameInfo = StackValue.get(JvmtiFrameInfo.class);
    CIntPointer countPtr = StackValue.get(CIntPointer.class);
    JvmtiError result = jvmtiFunctions().GetStackTrace().invoke(jvmtiEnv(), nullHandle(), depth, 1, (WordPointer) frameInfo, countPtr);
    if (result == JvmtiError.JVMTI_ERROR_NONE && countPtr.read() == 1) {
        return frameInfo.getMethod();
    }
    return nullPointer();
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) JvmtiFrameInfo(com.oracle.svm.jvmtiagentbase.jvmti.JvmtiFrameInfo) JvmtiError(com.oracle.svm.jvmtiagentbase.jvmti.JvmtiError)

Example 4 with JvmtiFrameInfo

use of com.oracle.svm.jvmtiagentbase.jvmti.JvmtiFrameInfo in project graal by oracle.

the class JavaStackTraceCreator method getStackTraceArray.

public JNIObjectHandle getStackTraceArray() {
    int threadStackFrameCount = getCurrentThreadStackFrameCount();
    int frameInfoSize = SizeOf.get(JvmtiFrameInfo.class);
    Pointer stackFramesPtr = UnmanagedMemory.malloc(frameInfoSize * threadStackFrameCount);
    CIntPointer readStackFramesPtr = StackValue.get(CIntPointer.class);
    check(jvmti.getFunctions().GetStackTrace().invoke(jvmti, nullHandle(), 0, threadStackFrameCount, (WordPointer) stackFramesPtr, readStackFramesPtr));
    VMError.guarantee(readStackFramesPtr.read() == threadStackFrameCount);
    NativeImageDiagnosticsAgent agent = JvmtiAgentBase.singleton();
    JNIObjectHandle stackTraceArray = jni.getFunctions().getNewObjectArray().invoke(jni, threadStackFrameCount, agent.handles().javaLangStackTraceElement, nullHandle());
    for (int i = 0; i < threadStackFrameCount; ++i) {
        JvmtiFrameInfo frameInfo = (JvmtiFrameInfo) stackFramesPtr.add(i * frameInfoSize);
        StackTraceElement stackTraceElement = constructStackTraceElement(frameInfo);
        JNIObjectHandle classNameHandle = Support.toJniString(jni, stackTraceElement.getClassName());
        JNIObjectHandle methodNameHandle = Support.toJniString(jni, stackTraceElement.getMethodName());
        JNIObjectHandle sourceFileNameHandle = Support.toJniString(jni, stackTraceElement.getFileName());
        int lineNumber = stackTraceElement.getLineNumber();
        JNIObjectHandle stackTraceElementHandle = Support.newObjectLLLJ(jni, agent.handles().javaLangStackTraceElement, agent.handles().javaLangStackTraceElementCtor4, classNameHandle, methodNameHandle, sourceFileNameHandle, lineNumber);
        jni.getFunctions().getSetObjectArrayElement().invoke(jni, stackTraceArray, i, stackTraceElementHandle);
        inspectStackTraceElementMethod(frameInfo.getMethod());
    }
    UnmanagedMemory.free(stackFramesPtr);
    return stackTraceArray;
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) JvmtiFrameInfo(com.oracle.svm.jvmtiagentbase.jvmti.JvmtiFrameInfo) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) Pointer(org.graalvm.word.Pointer) CCharPointerPointer(org.graalvm.nativeimage.c.type.CCharPointerPointer) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) JNIObjectHandle(com.oracle.svm.jni.nativeapi.JNIObjectHandle)

Aggregations

JvmtiFrameInfo (com.oracle.svm.jvmtiagentbase.jvmti.JvmtiFrameInfo)4 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)4 JNIObjectHandle (com.oracle.svm.jni.nativeapi.JNIObjectHandle)2 JvmtiError (com.oracle.svm.jvmtiagentbase.jvmti.JvmtiError)2 WordPointer (org.graalvm.nativeimage.c.type.WordPointer)2 Pointer (org.graalvm.word.Pointer)2 JNIMethodId (com.oracle.svm.jni.nativeapi.JNIMethodId)1 Support.fromCString (com.oracle.svm.jvmtiagentbase.Support.fromCString)1 Support.fromJniString (com.oracle.svm.jvmtiagentbase.Support.fromJniString)1 Support.toCString (com.oracle.svm.jvmtiagentbase.Support.toCString)1 JvmtiEnv (com.oracle.svm.jvmtiagentbase.jvmti.JvmtiEnv)1 CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)1 CCharPointerPointer (org.graalvm.nativeimage.c.type.CCharPointerPointer)1