jdk/test/com/sun/jdi/PopAsynchronousTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 24973 8c4bc3fa4c4e
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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 4467564
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary Test the popping of frames in an asynchronous context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *           (that is, when suspended by the debugger at random points)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  @author Robert Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run compile -g PopAsynchronousTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  @run main PopAsynchronousTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class PopAsynchronousTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final int N = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    int fibonacci(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (n <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            return fibonacci(n-1) + fibonacci(n-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    void report(int n, int result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        System.out.println("fibonacci(" + n + ") = " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        int n = N;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Howdy!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        PopAsynchronousTarg pat = new PopAsynchronousTarg();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        pat.report(n, pat.fibonacci(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.out.println("Goodbye from PopAsynchronousTarg!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public class PopAsynchronousTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    ReferenceType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    int result = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    boolean harassTarget = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    Object harassLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    PopAsynchronousTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        new PopAsynchronousTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /********** event handlers **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void breakpointReached(BreakpointEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        harassTarget = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        synchronized(harassLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                StackFrame frame = event.thread().frame(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                LocalVariable lv = frame.visibleVariableByName("result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                IntegerValue resultV = (IntegerValue)frame.getValue(lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                result = resultV.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                exc.printStackTrace(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                failure("TEST FAILURE: exception " + exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /********** test assist **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    class HarassThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int harassCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                int prev = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                int delayTime = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                synchronized(harassLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    while (harassTarget && (harassCount < 10)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        boolean backoff = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        mainThread.suspend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        StackFrame top = mainThread.frame(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        Method meth = top.location().method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        String methName = meth.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        if (methName.equals("fibonacci")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            LocalVariable lv = top.visibleVariableByName("n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            IntegerValue nV = (IntegerValue)top.getValue(lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            int n = nV.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            if (n != prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                backoff = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                StackFrame popThis = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                Iterator it = mainThread.frames().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                /* pop lowest fibonacci frame */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                    StackFrame frame = (StackFrame)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                    if (!frame.location().method().name().equals("fibonacci")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                    popThis = frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                println("popping fibonacci(" + n + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                mainThread.popFrames(popThis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                ++harassCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                prev = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                println("ignoring another fibonacci(" + n + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                            println("ignoring " + methName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        if (backoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                            delayTime *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                            delayTime /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                            if (delayTime < harassCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                delayTime = harassCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        mainThread.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        println("Delaying for " + delayTime + "ms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        Thread.sleep(delayTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                exc.printStackTrace(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                failure("TEST FAILURE: exception " + exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            println("Harassment complete, count = " + harassCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        BreakpointEvent bpe = startToMain("PopAsynchronousTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        targetClass = bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        EventRequestManager erm = vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * Set event requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        List meths = targetClass.methodsByName("report");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Location loc = ((Method)(meths.get(0))).location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        BreakpointRequest request = erm.createBreakpointRequest(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        request.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         * start popping wildly away
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        (new HarassThread()).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * resume the target listening for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * check result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int correct = (new PopAsynchronousTarg()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            fibonacci(PopAsynchronousTarg.N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (result == correct) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            println("Got expected result: " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            failure("FAIL: expected result: " + correct +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    ", got: " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            println("PopAsynchronousTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new Exception("PopAsynchronousTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}