test/runtime/7158988/FieldMonitor.java

Thu, 01 May 2014 14:57:02 -0700

author
amurillo
date
Thu, 01 May 2014 14:57:02 -0700
changeset 6651
4bc28e6b9aba
parent 6311
2996010c4820
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added tag hs25.20-b13 for changeset 798f5b02be89

coleenp@3698 1 /*
coleenp@3698 2 * Copyright 2012 SAP AG. All Rights Reserved.
coleenp@3698 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
coleenp@3698 4 *
coleenp@3698 5 * This code is free software; you can redistribute it and/or modify it
coleenp@3698 6 * under the terms of the GNU General Public License version 2 only, as
coleenp@3698 7 * published by the Free Software Foundation.
coleenp@3698 8 *
coleenp@3698 9 * This code is distributed in the hope that it will be useful, but WITHOUT
coleenp@3698 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
coleenp@3698 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
coleenp@3698 12 * version 2 for more details (a copy is included in the LICENSE file that
coleenp@3698 13 * accompanied this code).
coleenp@3698 14 *
coleenp@3698 15 * You should have received a copy of the GNU General Public License version
coleenp@3698 16 * 2 along with this work; if not, write to the Free Software Foundation,
coleenp@3698 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
coleenp@3698 18 *
coleenp@3698 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
coleenp@3698 20 * or visit www.oracle.com if you need additional information or have any
coleenp@3698 21 * questions.
coleenp@3698 22 */
coleenp@3698 23
coleenp@3698 24 /*
coleenp@3698 25 * @test FieldMonitor.java
coleenp@3698 26 * @bug 7158988
ctornqvi@4519 27 * @key regression
coleenp@3698 28 * @summary verify jvm does not crash while debugging
ctornqvi@4519 29 * @run compile TestPostFieldModification.java
ctornqvi@4519 30 * @run main/othervm FieldMonitor
coleenp@3698 31 * @author axel.siebenborn@sap.com
coleenp@3698 32 */
coleenp@3698 33 import java.io.BufferedReader;
coleenp@3698 34 import java.io.IOException;
coleenp@3698 35 import java.io.InputStream;
coleenp@3698 36 import java.io.InputStreamReader;
coleenp@3698 37 import java.util.Iterator;
coleenp@3698 38 import java.util.List;
coleenp@3698 39 import java.util.Map;
coleenp@3698 40
coleenp@3698 41 import com.sun.jdi.Bootstrap;
coleenp@3698 42 import com.sun.jdi.Field;
coleenp@3698 43 import com.sun.jdi.ReferenceType;
coleenp@3698 44 import com.sun.jdi.VirtualMachine;
coleenp@3698 45 import com.sun.jdi.connect.Connector;
coleenp@3698 46 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
coleenp@3698 47 import com.sun.jdi.connect.LaunchingConnector;
coleenp@3698 48 import com.sun.jdi.connect.VMStartException;
coleenp@3698 49 import com.sun.jdi.event.ClassPrepareEvent;
coleenp@3698 50 import com.sun.jdi.event.Event;
coleenp@3698 51 import com.sun.jdi.event.EventQueue;
coleenp@3698 52 import com.sun.jdi.event.EventSet;
coleenp@3698 53 import com.sun.jdi.event.ModificationWatchpointEvent;
coleenp@3698 54 import com.sun.jdi.event.VMDeathEvent;
sjiang@6311 55 import com.sun.jdi.event.VMStartEvent;
coleenp@3698 56 import com.sun.jdi.event.VMDisconnectEvent;
coleenp@3698 57 import com.sun.jdi.request.ClassPrepareRequest;
coleenp@3698 58 import com.sun.jdi.request.EventRequest;
coleenp@3698 59 import com.sun.jdi.request.EventRequestManager;
coleenp@3698 60 import com.sun.jdi.request.ModificationWatchpointRequest;
coleenp@3698 61
coleenp@3698 62 public class FieldMonitor {
coleenp@3698 63
coleenp@3698 64 public static final String CLASS_NAME = "TestPostFieldModification";
coleenp@3698 65 public static final String FIELD_NAME = "value";
coleenp@3698 66 public static final String ARGUMENTS = "-Xshare:off -XX:+PrintGC";
coleenp@3698 67
coleenp@3698 68 public static void main(String[] args)
coleenp@3698 69 throws IOException, InterruptedException {
coleenp@3698 70
coleenp@3698 71 //VirtualMachine vm = launchTarget(sb.toString());
coleenp@3698 72 VirtualMachine vm = launchTarget(CLASS_NAME);
coleenp@3698 73
coleenp@3698 74 System.out.println("Vm launched");
coleenp@3698 75
coleenp@3698 76 // process events
coleenp@3698 77 EventQueue eventQueue = vm.eventQueue();
coleenp@3698 78 // resume the vm
coleenp@3698 79
coleenp@3698 80 Process process = vm.process();
coleenp@3698 81
coleenp@3698 82
coleenp@3698 83 // Copy target's output and error to our output and error.
coleenp@3698 84 Thread outThread = new StreamRedirectThread("out reader", process.getInputStream());
coleenp@3698 85 Thread errThread = new StreamRedirectThread("error reader", process.getErrorStream());
coleenp@3698 86
coleenp@3698 87 errThread.start();
coleenp@3698 88 outThread.start();
coleenp@3698 89
coleenp@3698 90 boolean connected = true;
sjiang@6311 91 int watched = 0;
coleenp@3698 92 while (connected) {
coleenp@3698 93 EventSet eventSet = eventQueue.remove();
coleenp@3698 94 for (Event event : eventSet) {
sjiang@6311 95 System.out.println("FieldMonitor-main receives: "+event);
sjiang@6311 96 if (event instanceof VMStartEvent) {
sjiang@6311 97 addClassWatch(vm);
sjiang@6311 98 } else if (event instanceof VMDeathEvent
coleenp@3698 99 || event instanceof VMDisconnectEvent) {
coleenp@3698 100 // exit
coleenp@3698 101 connected = false;
coleenp@3698 102 } else if (event instanceof ClassPrepareEvent) {
coleenp@3698 103 // watch field on loaded class
coleenp@3698 104 System.out.println("ClassPrepareEvent");
coleenp@3698 105 ClassPrepareEvent classPrepEvent = (ClassPrepareEvent) event;
coleenp@3698 106 ReferenceType refType = classPrepEvent
coleenp@3698 107 .referenceType();
coleenp@3698 108 addFieldWatch(vm, refType);
coleenp@3698 109 } else if (event instanceof ModificationWatchpointEvent) {
sjiang@6311 110 watched++;
coleenp@3698 111 System.out.println("sleep for 500 ms");
coleenp@3698 112 Thread.sleep(500);
coleenp@3698 113
coleenp@3698 114 ModificationWatchpointEvent modEvent = (ModificationWatchpointEvent) event;
coleenp@3698 115 System.out.println("old="
coleenp@3698 116 + modEvent.valueCurrent());
coleenp@3698 117 System.out.println("new=" + modEvent.valueToBe());
coleenp@3698 118 }
coleenp@3698 119 }
sjiang@6311 120 System.out.println("resume...");
coleenp@3698 121 eventSet.resume();
coleenp@3698 122 }
coleenp@3698 123 // Shutdown begins when event thread terminates
coleenp@3698 124 try {
coleenp@3698 125 errThread.join(); // Make sure output is forwarded
coleenp@3698 126 outThread.join();
coleenp@3698 127 } catch (InterruptedException exc) {
coleenp@3698 128 // we don't interrupt
coleenp@3698 129 }
sjiang@6311 130
sjiang@6311 131 if (watched != 11) { // init + 10 modifications in TestPostFieldModification class
sjiang@6311 132 throw new Error("Expected to receive 11 times ModificationWatchpointEvent, but got "+watched);
sjiang@6311 133 }
coleenp@3698 134 }
coleenp@3698 135
coleenp@3698 136 /**
coleenp@3698 137 * Find a com.sun.jdi.CommandLineLaunch connector
coleenp@3698 138 */
coleenp@3698 139 static LaunchingConnector findLaunchingConnector() {
coleenp@3698 140 List <Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();
coleenp@3698 141 Iterator <Connector> iter = connectors.iterator();
coleenp@3698 142 while (iter.hasNext()) {
coleenp@3698 143 Connector connector = iter.next();
coleenp@3698 144 if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) {
coleenp@3698 145 return (LaunchingConnector)connector;
coleenp@3698 146 }
coleenp@3698 147 }
coleenp@3698 148 throw new Error("No launching connector");
coleenp@3698 149 }
coleenp@3698 150 /**
coleenp@3698 151 * Return the launching connector's arguments.
coleenp@3698 152 */
coleenp@3698 153 static Map <String,Connector.Argument> connectorArguments(LaunchingConnector connector, String mainArgs) {
coleenp@3698 154 Map<String,Connector.Argument> arguments = connector.defaultArguments();
coleenp@3698 155 for (String key : arguments.keySet()) {
coleenp@3698 156 System.out.println(key);
coleenp@3698 157 }
coleenp@3698 158
coleenp@3698 159 Connector.Argument mainArg = (Connector.Argument)arguments.get("main");
coleenp@3698 160 if (mainArg == null) {
coleenp@3698 161 throw new Error("Bad launching connector");
coleenp@3698 162 }
coleenp@3698 163 mainArg.setValue(mainArgs);
coleenp@3698 164
coleenp@3698 165 Connector.Argument optionsArg = (Connector.Argument)arguments.get("options");
coleenp@3698 166 if (optionsArg == null) {
coleenp@3698 167 throw new Error("Bad launching connector");
coleenp@3698 168 }
coleenp@3698 169 optionsArg.setValue(ARGUMENTS);
coleenp@3698 170 return arguments;
coleenp@3698 171 }
coleenp@3698 172
coleenp@3698 173 static VirtualMachine launchTarget(String mainArgs) {
coleenp@3698 174 LaunchingConnector connector = findLaunchingConnector();
coleenp@3698 175 Map arguments = connectorArguments(connector, mainArgs);
coleenp@3698 176 try {
coleenp@3698 177 return (VirtualMachine) connector.launch(arguments);
coleenp@3698 178 } catch (IOException exc) {
coleenp@3698 179 throw new Error("Unable to launch target VM: " + exc);
coleenp@3698 180 } catch (IllegalConnectorArgumentsException exc) {
coleenp@3698 181 throw new Error("Internal error: " + exc);
coleenp@3698 182 } catch (VMStartException exc) {
coleenp@3698 183 throw new Error("Target VM failed to initialize: " +
coleenp@3698 184 exc.getMessage());
coleenp@3698 185 }
coleenp@3698 186 }
coleenp@3698 187
coleenp@3698 188
coleenp@3698 189 private static void addClassWatch(VirtualMachine vm) {
coleenp@3698 190 EventRequestManager erm = vm.eventRequestManager();
coleenp@3698 191 ClassPrepareRequest classPrepareRequest = erm
coleenp@3698 192 .createClassPrepareRequest();
coleenp@3698 193 classPrepareRequest.addClassFilter(CLASS_NAME);
coleenp@3698 194 classPrepareRequest.setEnabled(true);
coleenp@3698 195 }
coleenp@3698 196
coleenp@3698 197
coleenp@3698 198 private static void addFieldWatch(VirtualMachine vm,
coleenp@3698 199 ReferenceType refType) {
coleenp@3698 200 EventRequestManager erm = vm.eventRequestManager();
coleenp@3698 201 Field field = refType.fieldByName(FIELD_NAME);
coleenp@3698 202 ModificationWatchpointRequest modificationWatchpointRequest = erm
coleenp@3698 203 .createModificationWatchpointRequest(field);
coleenp@3698 204 modificationWatchpointRequest.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
coleenp@3698 205 modificationWatchpointRequest.setEnabled(true);
coleenp@3698 206 }
coleenp@3698 207 }
coleenp@3698 208
coleenp@3698 209 class StreamRedirectThread extends Thread {
coleenp@3698 210
coleenp@3698 211 private final BufferedReader in;
coleenp@3698 212
coleenp@3698 213 private static final int BUFFER_SIZE = 2048;
coleenp@3698 214
coleenp@3698 215 /**
coleenp@3698 216 * Set up for copy.
coleenp@3698 217 * @param name Name of the thread
coleenp@3698 218 * @param in Stream to copy from
coleenp@3698 219 * @param out Stream to copy to
coleenp@3698 220 */
coleenp@3698 221 StreamRedirectThread(String name, InputStream in) {
coleenp@3698 222 super(name);
coleenp@3698 223 this.in = new BufferedReader(new InputStreamReader(in));
coleenp@3698 224 }
coleenp@3698 225
coleenp@3698 226 /**
coleenp@3698 227 * Copy.
coleenp@3698 228 */
coleenp@3698 229 public void run() {
coleenp@3698 230 try {
coleenp@3698 231 String line;
coleenp@3698 232 while ((line = in.readLine ()) != null) {
coleenp@3698 233 System.out.println ("testvm: " + line);
coleenp@3698 234 }
coleenp@3698 235 System.out.flush();
coleenp@3698 236 } catch(IOException exc) {
coleenp@3698 237 System.err.println("Child I/O Transfer - " + exc);
coleenp@3698 238 }
coleenp@3698 239 }
coleenp@3698 240 }

mercurial