jdk/test/com/sun/jdi/TwoThreadsTest.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 2005 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 6296125
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary  JDI: Disabling an EventRequest can cause a multi-threaded debuggee to hang
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 TwoThreadsTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main TwoThreadsTest
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 com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This debuggee basically runs two threads each of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * which loop, hitting a bkpt in each iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class TwoThreadsTarg extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static boolean one = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static String name1 = "Thread 1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static String name2 = "Thread 2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static int count = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        System.out.println("Howdy!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        TwoThreadsTarg t1 = new TwoThreadsTarg(name1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        TwoThreadsTarg t2 = new TwoThreadsTarg(name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        t1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        t2.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public TwoThreadsTarg(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (getName().equals(name1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            run1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            run2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void bkpt1(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void run1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        while (i < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            bkpt1(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public void bkpt2(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void run2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        while (i < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            bkpt2(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
/********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
public class TwoThreadsTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    ReferenceType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    BreakpointRequest request1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    BreakpointRequest request2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static volatile int bkpts = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    Thread timerThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static int waitTime = 20000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    TwoThreadsTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        new TwoThreadsTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /* BreakpointEvent handler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void breakpointReached(BreakpointEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (bkpts == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
             * This thread will watch for n secs to go by with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             * calls to this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            timerThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        synchronized("abc") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             * Note that this will most likely never get to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             * the number of times the two bkpt lines in the debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             * are hit because bkpts are lost while they are disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            bkpts++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         * The bug occurs when the requests are disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         * and then re-enabled during the event handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        request1.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        request2.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * This code between the disables and enables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * is just filler that leaves the requests disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * for awhile.  I suppose a sleep could be used instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        Method mmm = event.location().method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        List lvlist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            lvlist = mmm.variablesByName("i");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (AbsentInformationException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            failure("FAILED: can't get local var i");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        LocalVariable ivar = (LocalVariable)lvlist.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        ThreadReference thr = event.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        StackFrame sf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            sf = thr.frame(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        } catch (IncompatibleThreadStateException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            failure("FAILED: bad thread state");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        Value ival = sf.getValue(ivar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        println("Got bkpt at: " + event.location() + ", i = " + ival);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        request1.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        request2.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        BreakpointEvent bpe = startToMain("TwoThreadsTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        targetClass = bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        EventRequestManager erm = vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        final Thread mainThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * Set event requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Location loc1 = findMethod(targetClass, "bkpt1", "(I)V").location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Location loc2 = findMethod(targetClass, "bkpt2", "(I)V").location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        request1 = erm.createBreakpointRequest(loc1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        request2 = erm.createBreakpointRequest(loc2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        request1.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        request2.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         * This thread will be started when we get the first bkpt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         * (Which we always expect to get).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * It awakens every n seconds and checks to see if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * got any breakpoint events while it was asleep.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        timerThread = new Thread("test timer") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    int myBkpts = bkpts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                            Thread.sleep(waitTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                            System.out.println("bkpts = " + bkpts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                            if (myBkpts == bkpts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                // no bkpt for 'waitTime' secs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                failure("failure: Debuggee appears to be hung");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                vmDisconnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                // This awakens the main thread which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                // waiting for a VMDisconnect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                mainThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            myBkpts = bkpts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        } catch (InterruptedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            // If the test completes, this occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            println("timer Interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         * resume the target, listening for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        timerThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            println("TwoThreadsTest: passed; bkpts = " + bkpts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new Exception("TwoThreadsTest: failed; bkpts = " + bkpts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
}