test/runtime/7158988/FieldMonitor.java

Thu, 20 Sep 2012 13:44:28 -0700

author
katleman
date
Thu, 20 Sep 2012 13:44:28 -0700
changeset 4035
da0d652d0c2f
parent 3698
19e197e2a1af
child 4519
a7f9a1195d86
permissions
-rw-r--r--

Added tag jdk8-b57 for changeset d70102c4cb73

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
coleenp@3698 27 * @summary verify jvm does not crash while debugging
coleenp@3698 28 * @run shell TestFieldMonitor.sh
coleenp@3698 29 * @author axel.siebenborn@sap.com
coleenp@3698 30 */
coleenp@3698 31 import java.io.BufferedReader;
coleenp@3698 32 import java.io.IOException;
coleenp@3698 33 import java.io.InputStream;
coleenp@3698 34 import java.io.InputStreamReader;
coleenp@3698 35 import java.io.OutputStream;
coleenp@3698 36 import java.io.OutputStreamWriter;
coleenp@3698 37 import java.io.Reader;
coleenp@3698 38 import java.io.Writer;
coleenp@3698 39 import java.util.Iterator;
coleenp@3698 40 import java.util.List;
coleenp@3698 41 import java.util.Map;
coleenp@3698 42
coleenp@3698 43 import com.sun.jdi.Bootstrap;
coleenp@3698 44 import com.sun.jdi.Field;
coleenp@3698 45 import com.sun.jdi.ReferenceType;
coleenp@3698 46 import com.sun.jdi.VirtualMachine;
coleenp@3698 47 import com.sun.jdi.connect.Connector;
coleenp@3698 48 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
coleenp@3698 49 import com.sun.jdi.connect.LaunchingConnector;
coleenp@3698 50 import com.sun.jdi.connect.VMStartException;
coleenp@3698 51 import com.sun.jdi.event.ClassPrepareEvent;
coleenp@3698 52 import com.sun.jdi.event.Event;
coleenp@3698 53 import com.sun.jdi.event.EventQueue;
coleenp@3698 54 import com.sun.jdi.event.EventSet;
coleenp@3698 55 import com.sun.jdi.event.ModificationWatchpointEvent;
coleenp@3698 56 import com.sun.jdi.event.VMDeathEvent;
coleenp@3698 57 import com.sun.jdi.event.VMDisconnectEvent;
coleenp@3698 58 import com.sun.jdi.request.ClassPrepareRequest;
coleenp@3698 59 import com.sun.jdi.request.EventRequest;
coleenp@3698 60 import com.sun.jdi.request.EventRequestManager;
coleenp@3698 61 import com.sun.jdi.request.ModificationWatchpointRequest;
coleenp@3698 62
coleenp@3698 63 public class FieldMonitor {
coleenp@3698 64
coleenp@3698 65 public static final String CLASS_NAME = "TestPostFieldModification";
coleenp@3698 66 public static final String FIELD_NAME = "value";
coleenp@3698 67 public static final String ARGUMENTS = "-Xshare:off -XX:+PrintGC";
coleenp@3698 68
coleenp@3698 69 public static void main(String[] args)
coleenp@3698 70 throws IOException, InterruptedException {
coleenp@3698 71
coleenp@3698 72 StringBuffer sb = new StringBuffer();
coleenp@3698 73
coleenp@3698 74 for (int i=0; i < args.length; i++) {
coleenp@3698 75 sb.append(' ');
coleenp@3698 76 sb.append(args[i]);
coleenp@3698 77 }
coleenp@3698 78 //VirtualMachine vm = launchTarget(sb.toString());
coleenp@3698 79 VirtualMachine vm = launchTarget(CLASS_NAME);
coleenp@3698 80
coleenp@3698 81 System.out.println("Vm launched");
coleenp@3698 82 // set watch field on already loaded classes
coleenp@3698 83 List<ReferenceType> referenceTypes = vm
coleenp@3698 84 .classesByName(CLASS_NAME);
coleenp@3698 85 for (ReferenceType refType : referenceTypes) {
coleenp@3698 86 addFieldWatch(vm, refType);
coleenp@3698 87 }
coleenp@3698 88 // watch for loaded classes
coleenp@3698 89 addClassWatch(vm);
coleenp@3698 90
coleenp@3698 91 // process events
coleenp@3698 92 EventQueue eventQueue = vm.eventQueue();
coleenp@3698 93 // resume the vm
coleenp@3698 94
coleenp@3698 95 Process process = vm.process();
coleenp@3698 96
coleenp@3698 97
coleenp@3698 98 // Copy target's output and error to our output and error.
coleenp@3698 99 Thread outThread = new StreamRedirectThread("out reader", process.getInputStream());
coleenp@3698 100 Thread errThread = new StreamRedirectThread("error reader", process.getErrorStream());
coleenp@3698 101
coleenp@3698 102 errThread.start();
coleenp@3698 103 outThread.start();
coleenp@3698 104
coleenp@3698 105
coleenp@3698 106 vm.resume();
coleenp@3698 107 boolean connected = true;
coleenp@3698 108 while (connected) {
coleenp@3698 109 EventSet eventSet = eventQueue.remove();
coleenp@3698 110 for (Event event : eventSet) {
coleenp@3698 111 if (event instanceof VMDeathEvent
coleenp@3698 112 || event instanceof VMDisconnectEvent) {
coleenp@3698 113 // exit
coleenp@3698 114 connected = false;
coleenp@3698 115 } else if (event instanceof ClassPrepareEvent) {
coleenp@3698 116 // watch field on loaded class
coleenp@3698 117 System.out.println("ClassPrepareEvent");
coleenp@3698 118 ClassPrepareEvent classPrepEvent = (ClassPrepareEvent) event;
coleenp@3698 119 ReferenceType refType = classPrepEvent
coleenp@3698 120 .referenceType();
coleenp@3698 121 addFieldWatch(vm, refType);
coleenp@3698 122 } else if (event instanceof ModificationWatchpointEvent) {
coleenp@3698 123 System.out.println("sleep for 500 ms");
coleenp@3698 124 Thread.sleep(500);
coleenp@3698 125 System.out.println("resume...");
coleenp@3698 126
coleenp@3698 127 ModificationWatchpointEvent modEvent = (ModificationWatchpointEvent) event;
coleenp@3698 128 System.out.println("old="
coleenp@3698 129 + modEvent.valueCurrent());
coleenp@3698 130 System.out.println("new=" + modEvent.valueToBe());
coleenp@3698 131 System.out.println();
coleenp@3698 132 }
coleenp@3698 133 }
coleenp@3698 134 eventSet.resume();
coleenp@3698 135 }
coleenp@3698 136 // Shutdown begins when event thread terminates
coleenp@3698 137 try {
coleenp@3698 138 errThread.join(); // Make sure output is forwarded
coleenp@3698 139 outThread.join();
coleenp@3698 140 } catch (InterruptedException exc) {
coleenp@3698 141 // we don't interrupt
coleenp@3698 142 }
coleenp@3698 143 }
coleenp@3698 144
coleenp@3698 145 /**
coleenp@3698 146 * Find a com.sun.jdi.CommandLineLaunch connector
coleenp@3698 147 */
coleenp@3698 148 static LaunchingConnector findLaunchingConnector() {
coleenp@3698 149 List <Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();
coleenp@3698 150 Iterator <Connector> iter = connectors.iterator();
coleenp@3698 151 while (iter.hasNext()) {
coleenp@3698 152 Connector connector = iter.next();
coleenp@3698 153 if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) {
coleenp@3698 154 return (LaunchingConnector)connector;
coleenp@3698 155 }
coleenp@3698 156 }
coleenp@3698 157 throw new Error("No launching connector");
coleenp@3698 158 }
coleenp@3698 159 /**
coleenp@3698 160 * Return the launching connector's arguments.
coleenp@3698 161 */
coleenp@3698 162 static Map <String,Connector.Argument> connectorArguments(LaunchingConnector connector, String mainArgs) {
coleenp@3698 163 Map<String,Connector.Argument> arguments = connector.defaultArguments();
coleenp@3698 164 for (String key : arguments.keySet()) {
coleenp@3698 165 System.out.println(key);
coleenp@3698 166 }
coleenp@3698 167
coleenp@3698 168 Connector.Argument mainArg = (Connector.Argument)arguments.get("main");
coleenp@3698 169 if (mainArg == null) {
coleenp@3698 170 throw new Error("Bad launching connector");
coleenp@3698 171 }
coleenp@3698 172 mainArg.setValue(mainArgs);
coleenp@3698 173
coleenp@3698 174 Connector.Argument optionsArg = (Connector.Argument)arguments.get("options");
coleenp@3698 175 if (optionsArg == null) {
coleenp@3698 176 throw new Error("Bad launching connector");
coleenp@3698 177 }
coleenp@3698 178 optionsArg.setValue(ARGUMENTS);
coleenp@3698 179 return arguments;
coleenp@3698 180 }
coleenp@3698 181
coleenp@3698 182 static VirtualMachine launchTarget(String mainArgs) {
coleenp@3698 183 LaunchingConnector connector = findLaunchingConnector();
coleenp@3698 184 Map arguments = connectorArguments(connector, mainArgs);
coleenp@3698 185 try {
coleenp@3698 186 return (VirtualMachine) connector.launch(arguments);
coleenp@3698 187 } catch (IOException exc) {
coleenp@3698 188 throw new Error("Unable to launch target VM: " + exc);
coleenp@3698 189 } catch (IllegalConnectorArgumentsException exc) {
coleenp@3698 190 throw new Error("Internal error: " + exc);
coleenp@3698 191 } catch (VMStartException exc) {
coleenp@3698 192 throw new Error("Target VM failed to initialize: " +
coleenp@3698 193 exc.getMessage());
coleenp@3698 194 }
coleenp@3698 195 }
coleenp@3698 196
coleenp@3698 197
coleenp@3698 198 private static void addClassWatch(VirtualMachine vm) {
coleenp@3698 199 EventRequestManager erm = vm.eventRequestManager();
coleenp@3698 200 ClassPrepareRequest classPrepareRequest = erm
coleenp@3698 201 .createClassPrepareRequest();
coleenp@3698 202 classPrepareRequest.addClassFilter(CLASS_NAME);
coleenp@3698 203 classPrepareRequest.setEnabled(true);
coleenp@3698 204 }
coleenp@3698 205
coleenp@3698 206
coleenp@3698 207 private static void addFieldWatch(VirtualMachine vm,
coleenp@3698 208 ReferenceType refType) {
coleenp@3698 209 EventRequestManager erm = vm.eventRequestManager();
coleenp@3698 210 Field field = refType.fieldByName(FIELD_NAME);
coleenp@3698 211 ModificationWatchpointRequest modificationWatchpointRequest = erm
coleenp@3698 212 .createModificationWatchpointRequest(field);
coleenp@3698 213 modificationWatchpointRequest.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
coleenp@3698 214 modificationWatchpointRequest.setEnabled(true);
coleenp@3698 215 }
coleenp@3698 216 }
coleenp@3698 217
coleenp@3698 218 class StreamRedirectThread extends Thread {
coleenp@3698 219
coleenp@3698 220 private final BufferedReader in;
coleenp@3698 221
coleenp@3698 222 private static final int BUFFER_SIZE = 2048;
coleenp@3698 223
coleenp@3698 224 /**
coleenp@3698 225 * Set up for copy.
coleenp@3698 226 * @param name Name of the thread
coleenp@3698 227 * @param in Stream to copy from
coleenp@3698 228 * @param out Stream to copy to
coleenp@3698 229 */
coleenp@3698 230 StreamRedirectThread(String name, InputStream in) {
coleenp@3698 231 super(name);
coleenp@3698 232 this.in = new BufferedReader(new InputStreamReader(in));
coleenp@3698 233 }
coleenp@3698 234
coleenp@3698 235 /**
coleenp@3698 236 * Copy.
coleenp@3698 237 */
coleenp@3698 238 public void run() {
coleenp@3698 239 try {
coleenp@3698 240 String line;
coleenp@3698 241 while ((line = in.readLine ()) != null) {
coleenp@3698 242 System.out.println ("testvm: " + line);
coleenp@3698 243 }
coleenp@3698 244 System.out.flush();
coleenp@3698 245 } catch(IOException exc) {
coleenp@3698 246 System.err.println("Child I/O Transfer - " + exc);
coleenp@3698 247 }
coleenp@3698 248 }
coleenp@3698 249 }

mercurial