jdk/test/com/sun/jdi/PopAndInvokeTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *  @bug 6517249
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary JDWP: Cannot do an invokeMethod after a popFrames operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @author jjh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run compile -g PopAndInvokeTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main PopAndInvokeTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class PopAndInvokeTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static boolean waiting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static void A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        System.out.println("    debuggee: in A");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void invokeee() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.out.println("    debuggee: invokee");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void waiter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (waiting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        waiting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        System.out.println("    debuggee: in waiter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.out.println("    debuggee: Howdy!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * Debugger will bkpt in A, popFrames back to here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * and then do an invokeMethod on invokeee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * This should work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        A();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         * Debugger will resume and we will enter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         * waiter().  Debugger will then do a suspend,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * a popFrames back to here, and an invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * which should fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        waiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.out.println("    debuggee: Goodbye from PopAndInvokeTarg!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public class PopAndInvokeTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    ClassType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    PopAndInvokeTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        new PopAndInvokeTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    StackFrame frameFor(String methodName) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Iterator it = mainThread.frames().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            StackFrame frame = (StackFrame)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (frame.location().method().name().equals(methodName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                return frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        failure("FAIL: " + methodName + " not on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        runOnce();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    void runOnce() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        BreakpointEvent bpe = startTo("PopAndInvokeTarg", "A", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        targetClass = (ClassType)bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         * Verify that an invokeMethod works ok after a popFrames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         * in a thread suspended by an event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        mainThread.popFrames(frameFor("A"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.out.println("Debugger: Popped back to the call to A()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("Debugger: Doing invoke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Method invokeeeMethod = (Method)targetClass.methodsByName("invokeee").get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            targetClass.invokeMethod(mainThread, invokeeeMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                     new ArrayList(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            failure("failure: invoke got unexpected exception: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        System.out.println("Debugger: invoke done");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * Verify that an invokeMethod gets an IncompatibleThreadStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * after a popFrames in a thread that is _not_ suspended by an event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        System.out.println("Debugger: Resuming debuggee");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        vm().resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Field waiting = targetClass.fieldByName("waiting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // Wait until debuggee enters the 'waiting' method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            BooleanValue bv= (BooleanValue)targetClass.getValue(waiting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (!bv.value()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                } catch (InterruptedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            // debuggee has entered the waiting method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            System.out.println("Debugger: Suspending debuggee");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            vm().suspend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            System.out.println("Debugger: Popping frame for waiter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            mainThread.popFrames(frameFor("waiter"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            System.out.println("Debugger: Invoking method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                targetClass.invokeMethod(mainThread, invokeeeMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                         new ArrayList(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } catch (IncompatibleThreadStateException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                System.out.println("Debugger: Success: Got expected IncompatibleThreadStateException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } catch (Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                failure("FAIL: Got unexpected exception: " + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            failure("FAIL: Did not get IncompatibleThreadStateException " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    "when debuggee is not suspended by an event");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new Exception("PopAndInvokeTest failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        System.out.println("Passed:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}