jdk/test/com/sun/jdi/RepStep.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 20184 a2c4b7079eb6
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
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: 4335
diff changeset
     2
 * Copyright (c) 2000, 2006, 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: 4335
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4335
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4335
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 4334008
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @author Robert Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @run compile -g RepStepTarg.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  @run build VMConnection RepStep
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
    32
 *  @run main/othervm RepStep
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @summary RepStep detects missed step events due to lack of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * frame pop events (in back-end).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jdi.connect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class RepStep {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final String TARGET = "RepStepTarg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final int GRANULARITY = StepRequest.STEP_LINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final int DEPTH = StepRequest.STEP_INTO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final int SUCCESS_COUNT = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    VirtualMachine vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    final EventRequestManager requestManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int stepNum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    boolean passed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        new RepStep(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    RepStep(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            attachTarget(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            launchTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        //        vm.setDebugTraceMode(VirtualMachine.TRACE_ALL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        requestManager = vm.eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        runTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        dieNice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private void createStep(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        final StepRequest sr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                  requestManager.createStepRequest(thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                                   GRANULARITY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                                   DEPTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        sr.addClassExclusionFilter("java.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        sr.addClassExclusionFilter("sun.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        sr.addClassExclusionFilter("com.sun.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        sr.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ThreadReference thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        EventQueue queue = vm.eventQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            EventSet set = queue.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            for (EventIterator it = set.eventIterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                Event event = it.nextEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (event instanceof VMStartEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    // get thread for setting step later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    thread = ((VMStartEvent)event).thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    ClassPrepareRequest cpReq
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        = requestManager.createClassPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    cpReq.addClassFilter(TARGET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    cpReq.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                } else if (event instanceof ClassPrepareEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    createStep(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    event.request().disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                } else if (event instanceof StepEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    // StepEvent stepEvent = (StepEvent)event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    // System.out.println(stepEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    System.out.println(++stepNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    if (stepNum >= SUCCESS_COUNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        // would have failed by now (> 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        System.out.println("RepStep passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        event.request().disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        set.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        return; // Success exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                } else if (event instanceof VMDeathEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    throw new Exception("RepStep failed: steps missed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    throw new Exception("Unexpected event: " + event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            set.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private void dieNice() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        EventQueue queue = vm.eventQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            EventSet set = queue.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            for (EventIterator it = set.eventIterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                Event event = it.nextEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (event instanceof VMDeathEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                } else if (event instanceof VMDisconnectEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    set.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    return; // Success exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    throw new Exception("Unexpected event: " + event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            set.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private Connector findConnector(String name) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Iterator iter = connectors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            Connector connector = (Connector)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        throw new Exception("No connector: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /* launch child target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private void launchTarget() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        LaunchingConnector launcher =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
          (LaunchingConnector)findConnector("com.sun.jdi.CommandLineLaunch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Map connectorArgs = launcher.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Connector.Argument mainArg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            (Connector.Argument)connectorArgs.get("main");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        mainArg.setValue(TARGET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Connector.Argument optionsArg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            (Connector.Argument)connectorArgs.get("options");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        optionsArg.setValue(VMConnection.getDebuggeeVMOptions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        vm = launcher.launch(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        System.out.println("launched: " + TARGET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private void attachTarget(String portNum) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        AttachingConnector conn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        Map connectorArgs = conn.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Connector.Argument portArg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            (Connector.Argument)connectorArgs.get("port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        portArg.setValue(portNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        vm = conn.attach(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        System.out.println("attached to: " + portNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}