1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *  @test
  26  *  @bug 4334008
  27  *  @author Robert Field
  28  *
  29  *  @modules jdk.jdi
  30  *  @run compile -g RepStepTarg.java
  31  *  @run build VMConnection RepStep
  32  *
  33  *  @run driver RepStep
  34  *
  35  * @summary RepStep detects missed step events due to lack of
  36  * frame pop events (in back-end).
  37  */
  38 import com.sun.jdi.*;
  39 import com.sun.jdi.event.*;
  40 import com.sun.jdi.request.*;
  41 import com.sun.jdi.connect.*;
  42 
  43 import java.util.*;
  44 
  45 public class RepStep {
  46     static final String TARGET = "RepStepTarg";
  47     static final int GRANULARITY = StepRequest.STEP_LINE;
  48     static final int DEPTH = StepRequest.STEP_INTO;
  49     static final int SUCCESS_COUNT = 30;
  50 
  51     VirtualMachine vm;
  52     final EventRequestManager requestManager;
  53     int stepNum = 0;
  54     boolean passed = false;
  55 
  56     public static void main(String args[]) throws Exception {
  57         new RepStep(args);
  58     }
  59 
  60     RepStep(String args[]) throws Exception {
  61         if (args.length > 0) {
  62             attachTarget(args[0]);
  63         } else {
  64             launchTarget();
  65         }
  66         //        vm.setDebugTraceMode(VirtualMachine.TRACE_ALL);
  67         requestManager = vm.eventRequestManager();
  68         runTests();
  69         dieNice();
  70     }
  71 
  72     private void createStep(ThreadReference thread) {
  73         final StepRequest sr =
  74                   requestManager.createStepRequest(thread,
  75                                                    GRANULARITY,
  76                                                    DEPTH);
  77 
  78         sr.addClassExclusionFilter("java.*");
  79         sr.addClassExclusionFilter("javax.*");
  80         sr.addClassExclusionFilter("sun.*");
  81         sr.addClassExclusionFilter("com.sun.*");
  82         sr.addClassExclusionFilter("com.oracle.*");
  83         sr.addClassExclusionFilter("oracle.*");
  84         sr.addClassExclusionFilter("jdk.internal.*");
  85         sr.enable();
  86     }
  87 
  88     private void runTests() throws Exception {
  89         ThreadReference thread = null;
  90         EventQueue queue = vm.eventQueue();
  91         while (true) {
  92             EventSet set = queue.remove();
  93             for (EventIterator it = set.eventIterator(); it.hasNext(); ) {
  94                 Event event = it.nextEvent();
  95                 if (event instanceof VMStartEvent) {
  96                     // get thread for setting step later
  97                     thread = ((VMStartEvent)event).thread();
  98                     ClassPrepareRequest cpReq
  99                         = requestManager.createClassPrepareRequest();
 100                     cpReq.addClassFilter(TARGET);
 101                     cpReq.enable();
 102                 } else if (event instanceof ClassPrepareEvent) {
 103                     createStep(thread);
 104                     event.request().disable();
 105                 } else if (event instanceof StepEvent) {
 106                     // StepEvent stepEvent = (StepEvent)event;
 107                     // System.out.println(stepEvent);
 108                     System.out.println(++stepNum);
 109                     if (stepNum >= SUCCESS_COUNT) {
 110                         // would have failed by now (> 4)
 111                         System.out.println("RepStep passed");
 112                         event.request().disable();
 113                         set.resume();
 114                         return; // Success exit
 115                     }
 116                 } else if (event instanceof VMDeathEvent) {
 117                     throw new Exception("RepStep failed: steps missed");
 118                 } else {
 119                     throw new Exception("Unexpected event: " + event);
 120                 }
 121             }
 122             set.resume();
 123        }
 124     }
 125 
 126     private void dieNice() throws Exception {
 127         EventQueue queue = vm.eventQueue();
 128         while (true) {
 129             EventSet set = queue.remove();
 130             for (EventIterator it = set.eventIterator(); it.hasNext(); ) {
 131                 Event event = it.nextEvent();
 132                 if (event instanceof VMDeathEvent) {
 133                     // ignore
 134                 } else if (event instanceof VMDisconnectEvent) {
 135                     set.resume();
 136                     return; // Success exit
 137                 } else {
 138                     throw new Exception("Unexpected event: " + event);
 139                 }
 140             }
 141             set.resume();
 142        }
 143     }
 144 
 145     private Connector findConnector(String name) throws Exception {
 146         List connectors = Bootstrap.virtualMachineManager().allConnectors();
 147         Iterator iter = connectors.iterator();
 148         while (iter.hasNext()) {
 149             Connector connector = (Connector)iter.next();
 150             if (connector.name().equals(name)) {
 151                 return connector;
 152             }
 153         }
 154         throw new Exception("No connector: " + name);
 155     }
 156 
 157     /* launch child target vm */
 158     private void launchTarget() throws Exception {
 159         LaunchingConnector launcher =
 160           (LaunchingConnector)findConnector("com.sun.jdi.CommandLineLaunch");
 161         Map connectorArgs = launcher.defaultArguments();
 162         Connector.Argument mainArg =
 163             (Connector.Argument)connectorArgs.get("main");
 164         mainArg.setValue(TARGET);
 165         Connector.Argument optionsArg =
 166             (Connector.Argument)connectorArgs.get("options");
 167         optionsArg.setValue(VMConnection.getDebuggeeVMOptions());
 168 
 169         vm = launcher.launch(connectorArgs);
 170         System.out.println("launched: " + TARGET);
 171     }
 172 
 173     private void attachTarget(String portNum) throws Exception {
 174         AttachingConnector conn =
 175             (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
 176         Map connectorArgs = conn.defaultArguments();
 177         Connector.Argument portArg =
 178             (Connector.Argument)connectorArgs.get("port");
 179         portArg.setValue(portNum);
 180         vm = conn.attach(connectorArgs);
 181         System.out.println("attached to: " + portNum);
 182     }
 183 
 184 }