com.sun.jdi.StackFrame Java Examples

The following examples show how to use com.sun.jdi.StackFrame. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaHotCodeReplaceProvider.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns a list of frames which should be popped in the given threads.
 */
private List<StackFrame> getAffectedFrames(List<ThreadReference> threads, List<String> replacedClassNames)
        throws DebugException {
    List<StackFrame> popFrames = new ArrayList<>();
    for (ThreadReference thread : threads) {
        if (thread.isSuspended()) {
            StackFrame affectedFrame = getAffectedFrame(thread, replacedClassNames);
            if (affectedFrame == null) {
                // No frame to drop to in this thread
                continue;
            }
            if (supportsDropToFrame(thread, affectedFrame)) {
                popFrames.add(affectedFrame);
            }
        }
    }
    return popFrames;
}
 
Example #2
Source File: GetUninitializedStringValue.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #3
Source File: GetUninitializedStringValue.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #4
Source File: JavaHotCodeReplaceProvider.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
private boolean containsObsoleteMethods() throws DebugException {
    List<ThreadReference> threads = currentDebugSession.getAllThreads();
    for (ThreadReference thread : threads) {
        if (!thread.isSuspended()) {
            continue;
        }
        List<StackFrame> frames = getStackFrames(thread, true);
        if (frames == null || frames.isEmpty()) {
            continue;
        }
        for (StackFrame frame : frames) {
            if (StackFrameUtility.isObsolete(frame)) {
                return true;
            }
        }
    }
    return false;
}
 
Example #5
Source File: JavaHotCodeReplaceProvider.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
private boolean containsChangedType(StackFrame frame, List<String> replacedClassNames) throws DebugException {
    String declaringTypeName = JdtUtils.getDeclaringTypeName(frame);
    // Check if the frame's declaring type was changed
    if (replacedClassNames.contains(declaringTypeName)) {
        return true;
    }
    // Check if one of the frame's declaring type's inner classes have
    // changed
    for (String className : replacedClassNames) {
        int index = className.indexOf('$');
        if (index > -1 && declaringTypeName.equals(className.substring(0, index))) {
            return true;
        }
    }
    return false;
}
 
Example #6
Source File: GetUninitializedStringValue.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #7
Source File: GetUninitializedStringValue.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #8
Source File: GetUninitializedStringValue.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #9
Source File: GetUninitializedStringValue.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #10
Source File: GetUninitializedStringValue.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #11
Source File: JPDADebuggerImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void popFrames (ThreadReference thread, StackFrame frame) {
    PropertyChangeEvent evt = null;
    accessLock.readLock().lock();
    try {
        JPDAThreadImpl threadImpl = getThread(thread);
        setState (STATE_RUNNING);
        try {
            threadImpl.popFrames(frame);
            evt = updateCurrentCallStackFrameNoFire(threadImpl);
        } catch (IncompatibleThreadStateException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            setState (STATE_STOPPED);
        }
    } finally {
        accessLock.readLock().unlock();
    }
    if (evt != null) {
        firePropertyChange(evt);
    }
}
 
Example #12
Source File: JdtUtils.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Given a stack frame, find the target java project that the associated source file belongs to.
 *
 * @param stackFrame
 *                  the stack frame.
 * @param containers
 *                  the source container list.
 * @return the java project.
 */
public static IJavaProject findProject(StackFrame stackFrame, ISourceContainer[] containers) {
    Location location = stackFrame.location();
    try {
        Object sourceElement = findSourceElement(location.sourcePath(), containers);
        if (sourceElement instanceof IResource) {
            return JavaCore.create(((IResource) sourceElement).getProject());
        } else if (sourceElement instanceof IClassFile) {
            IJavaProject javaProject = ((IClassFile) sourceElement).getJavaProject();
            if (javaProject != null) {
                return javaProject;
            }
        }
    } catch (AbsentInformationException e) {
        // When the compiled .class file doesn't contain debug source information, return null.
    }
    return null;
}
 
Example #13
Source File: GetUninitializedStringValue.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #14
Source File: EvaluationContext.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new context in which to evaluate expressions.
 *
 * @param frame the frame in which context evaluation occurs
 * @param imports list of imports
 * @param staticImports list of static imports
 */
public EvaluationContext(JPDAThreadImpl thread, StackFrame frame, int frameDepth,
                         ObjectReference contextVariable,
                         List<String> imports, List<String> staticImports,
                         boolean canInvokeMethods, Runnable methodInvokePreproc,
                         JPDADebuggerImpl debugger, VMCache vmCache) {
    if (thread == null) throw new IllegalArgumentException("Thread argument must not be null");
    if (frame == null) throw new IllegalArgumentException("Frame argument must not be null");
    if (imports == null) throw new IllegalArgumentException("Imports argument must not be null");
    if (staticImports == null) throw new IllegalArgumentException("Static imports argument must not be null");
    this.thread = thread;
    this.frame = frame;
    this.frameDepth = frameDepth;
    this.contextVariable = contextVariable;
    this.sourceImports = imports;
    this.staticImports = staticImports;
    this.canInvokeMethods = canInvokeMethods;
    this.methodInvokePreproc = methodInvokePreproc;
    this.debugger = debugger;
    this.vmCache = vmCache;

    stack.push(new HashMap<String, ScriptVariable>());
}
 
Example #15
Source File: JPDAStepTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private String getCurrentClassName() {
    JPDADebuggerImpl debugger = (JPDADebuggerImpl) support.getDebugger();
    
    String className = null;
    
    try {
        JPDAThreadImpl jpdaThread = (JPDAThreadImpl) debugger.getCurrentThread();
        if (jpdaThread == null) {
            System.err.println("NULL Current Thread!");
            Thread.dumpStack();
        } else {
            StackFrame sf = jpdaThread.getThreadReference().frame(0);
            if (sf == null) {
                System.err.println("No stack frame!");
                Thread.dumpStack();
            } else {
                className = sf.location().declaringType().name();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return className;
}
 
Example #16
Source File: GetUninitializedStringValue.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #17
Source File: GetObjectLockCount.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static int getLockRecursions(VirtualMachine vm) {
    List <ThreadReference> threads = vm.allThreads();
    for (ThreadReference thread : threads) {
        if (thread.name().equals("main")) {

            System.out.println("Found main thread.");
            try{
                StackFrame frame = thread.frame(3);
                return frame.thisObject().entryCount();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("Main thread not found!");
    }
    return -1;
}
 
Example #18
Source File: GetUninitializedStringValue.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #19
Source File: GetUninitializedStringValue.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/********** test core **********/

    protected void runTests()
        throws Exception
    {
        /*
         * Run to String.<init>
         */
        startUp("GetUninitializedStringValueTarg");
        BreakpointEvent bpe = resumeTo("java.lang.String", "<init>", "(Ljava/lang/String;)V");

        /*
         * We've arrived. Look at 'this' - it will be uninitialized (the value field will not be set yet).
         */
        StackFrame frame = bpe.thread().frame(0);
        StringReference sr = (StringReference)frame.thisObject();
        if (!sr.value().equals("")) {
            throw new Exception("Unexpected value for the uninitialized String");
        }

        /*
         * resume the target listening for events
         */
        listenUntilVMDisconnect();
    }
 
Example #20
Source File: StackTraceRequestHandler.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
private Types.StackFrame convertDebuggerStackFrameToClient(StackFrame stackFrame, int frameId, IDebugAdapterContext context)
        throws URISyntaxException, AbsentInformationException {
    Location location = stackFrame.location();
    Method method = location.method();
    Types.Source clientSource = this.convertDebuggerSourceToClient(location, context);
    String methodName = formatMethodName(method, true, true);
    int lineNumber = AdapterUtils.convertLineNumber(location.lineNumber(), context.isDebuggerLinesStartAt1(), context.isClientLinesStartAt1());
    // Line number returns -1 if the information is not available; specifically, always returns -1 for native methods.
    if (lineNumber < 0) {
        if (method.isNative()) {
            // For native method, display a tip text "native method" in the Call Stack View.
            methodName += "[native method]";
        } else {
            // For other unavailable method, such as lambda expression's built-in methods run/accept/apply,
            // display "Unknown Source" in the Call Stack View.
            clientSource = null;
        }
    }
    return new Types.StackFrame(frameId, methodName, clientSource, lineNumber, context.isClientColumnsStartAt1() ? 1 : 0);
}
 
Example #21
Source File: RestartFrameHandler.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
private boolean canRestartFrame(IDebugAdapterContext context, StackFrameReference frameReference) {
    if (!context.getDebugSession().getVM().canPopFrames()) {
        return false;
    }
    ThreadReference reference = frameReference.getThread();
    StackFrame[] frames = context.getStackFrameManager().reloadStackFrames(reference);

    // The frame cannot be the bottom one of the call stack:
    if (frames.length <= frameReference.getDepth() + 1) {
        return false;
    }

    // Cannot restart frame involved with native call stacks:
    for (int i = 0; i <= frameReference.getDepth() + 1; i++) {
        if (StackFrameUtility.isNative(frames[i])) {
            return false;
        }
    }
    return true;
}
 
Example #22
Source File: JavaHotCodeReplaceProvider.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Looks for the deepest affected stack frames in the stack and forces pop
 * affected frames. Does this for all of the active stack frames in the session.
 */
private void attemptPopFrames(List<IResource> resources, List<String> replacedClassNames,
        List<ThreadReference> poppedThreads) throws DebugException {
    List<StackFrame> popFrames = getAffectedFrames(currentDebugSession.getAllThreads(), replacedClassNames);

    for (StackFrame popFrame : popFrames) {
        try {
            popStackFrame(popFrame);
            poppedThreads.add(popFrame.thread());
        } catch (DebugException de) {
            poppedThreads.remove(popFrame.thread());
        }
    }
}
 
Example #23
Source File: JavaHotCodeReplaceProvider.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
private boolean supportsDropToFrame(ThreadReference thread, StackFrame frame) {
    List<StackFrame> frames = getStackFrames(thread, false);
    for (int i = 0; i < frames.size(); i++) {
        if (StackFrameUtility.isNative(frames.get(i))) {
            return false;
        }
        if (frames.get(i) == frame) {
            return true;
        }
    }
    return false;
}
 
Example #24
Source File: AbstractJdiTestCase.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
protected StackFrame getStackFrame() {
    if (this.staticBreakpointEvent == null || !this.staticBreakpointEvent.thread().isAtBreakpoint()) {
        return null;
    }
    try {
        return this.staticBreakpointEvent.thread().frame(0);

    } catch (IncompatibleThreadStateException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #25
Source File: StackFrameUtility.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the StackFrame associated source file path.
 *
 * @param frame
 *            StackFrame for the source path
 * @return the source file path
 */
public static String getSourcePath(StackFrame frame) {
    try {
        return frame.location().sourcePath();
    } catch (AbsentInformationException e) {
        // Ignore it
    }
    return null;
}
 
Example #26
Source File: VariableUtils.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the static variable of an stack frame.
 *
 * @param stackFrame
 *            the stack frame
 * @return the static variable of an stack frame.
 */
public static List<Variable> listStaticVariables(StackFrame stackFrame) {
    List<Variable> res = new ArrayList<>();
    ReferenceType type = stackFrame.location().declaringType();
    type.allFields().stream().filter(TypeComponent::isStatic).forEach(field -> {
        Variable staticVar = new Variable(field.name(), type.getValue(field));
        staticVar.field = field;
        res.add(staticVar);
    });
    return res;
}
 
Example #27
Source File: VariableUtils.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the this variable of an stack frame.
 *
 * @param stackFrame
 *            the stack frame
 * @return this variable
 */
public static Variable getThisVariable(StackFrame stackFrame) {
    ObjectReference thisObject = stackFrame.thisObject();
    if (thisObject == null) {
        return null;
    }
    return new Variable("this", thisObject);
}
 
Example #28
Source File: ThreadInfo.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the current stackframe.
 *
 * @return the current stackframe.
 */
StackFrame getCurrentFrame() throws IncompatibleThreadStateException {
    if (thread.frameCount() == 0) {
        return null;
    }
    return thread.frame(currentFrameIndex);
}
 
Example #29
Source File: StackTraceRequestHandler.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public CompletableFuture<Response> handle(Command command, Arguments arguments, Response response, IDebugAdapterContext context) {
    StackTraceArguments stacktraceArgs = (StackTraceArguments) arguments;
    List<Types.StackFrame> result = new ArrayList<>();
    if (stacktraceArgs.startFrame < 0 || stacktraceArgs.levels < 0) {
        response.body = new Responses.StackTraceResponseBody(result, 0);
        return CompletableFuture.completedFuture(response);
    }
    ThreadReference thread = DebugUtility.getThread(context.getDebugSession(), stacktraceArgs.threadId);
    int totalFrames = 0;
    if (thread != null) {
        try {
            totalFrames = thread.frameCount();
            if (totalFrames <= stacktraceArgs.startFrame) {
                response.body = new Responses.StackTraceResponseBody(result, totalFrames);
                return CompletableFuture.completedFuture(response);
            }
            StackFrame[] frames = context.getStackFrameManager().reloadStackFrames(thread);

            int count = stacktraceArgs.levels == 0 ? totalFrames - stacktraceArgs.startFrame
                    : Math.min(totalFrames - stacktraceArgs.startFrame, stacktraceArgs.levels);
            for (int i = stacktraceArgs.startFrame; i < frames.length && count-- > 0; i++) {
                StackFrameReference stackframe = new StackFrameReference(thread, i);
                int frameId = context.getRecyclableIdPool().addObject(thread.uniqueID(), stackframe);
                result.add(convertDebuggerStackFrameToClient(frames[i], frameId, context));
            }
        } catch (IncompatibleThreadStateException | IndexOutOfBoundsException | URISyntaxException
                | AbsentInformationException | ObjectCollectedException e) {
            // when error happens, the possible reason is:
            // 1. the vscode has wrong parameter/wrong uri
            // 2. the thread actually terminates
            // TODO: should record a error log here.
        }
    }
    response.body = new Responses.StackTraceResponseBody(result, totalFrames);
    return CompletableFuture.completedFuture(response);
}
 
Example #30
Source File: ThreadInfo.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the current stackframe.
 *
 * @return the current stackframe.
 */
StackFrame getCurrentFrame() throws IncompatibleThreadStateException {
    if (thread.frameCount() == 0) {
        return null;
    }
    return thread.frame(currentFrameIndex);
}