test/runtime/7158988/FieldMonitor.java

Sat, 02 Feb 2013 20:13:27 +0100

author
ctornqvi
date
Sat, 02 Feb 2013 20:13:27 +0100
changeset 4519
a7f9a1195d86
parent 3698
19e197e2a1af
child 6311
2996010c4820
permissions
-rw-r--r--

8000363: runtime/7158988/FieldMonitor.java fails with exception
Summary: Removed unnecessary shell script in the test.
Reviewed-by: coleenp, sla

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

mercurial