1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/runtime/7158988/FieldMonitor.java Thu Apr 05 12:17:52 2012 -0400 1.3 @@ -0,0 +1,249 @@ 1.4 +/* 1.5 + * Copyright 2012 SAP AG. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test FieldMonitor.java 1.29 + * @bug 7158988 1.30 + * @summary verify jvm does not crash while debugging 1.31 + * @run shell TestFieldMonitor.sh 1.32 + * @author axel.siebenborn@sap.com 1.33 + */ 1.34 +import java.io.BufferedReader; 1.35 +import java.io.IOException; 1.36 +import java.io.InputStream; 1.37 +import java.io.InputStreamReader; 1.38 +import java.io.OutputStream; 1.39 +import java.io.OutputStreamWriter; 1.40 +import java.io.Reader; 1.41 +import java.io.Writer; 1.42 +import java.util.Iterator; 1.43 +import java.util.List; 1.44 +import java.util.Map; 1.45 + 1.46 +import com.sun.jdi.Bootstrap; 1.47 +import com.sun.jdi.Field; 1.48 +import com.sun.jdi.ReferenceType; 1.49 +import com.sun.jdi.VirtualMachine; 1.50 +import com.sun.jdi.connect.Connector; 1.51 +import com.sun.jdi.connect.IllegalConnectorArgumentsException; 1.52 +import com.sun.jdi.connect.LaunchingConnector; 1.53 +import com.sun.jdi.connect.VMStartException; 1.54 +import com.sun.jdi.event.ClassPrepareEvent; 1.55 +import com.sun.jdi.event.Event; 1.56 +import com.sun.jdi.event.EventQueue; 1.57 +import com.sun.jdi.event.EventSet; 1.58 +import com.sun.jdi.event.ModificationWatchpointEvent; 1.59 +import com.sun.jdi.event.VMDeathEvent; 1.60 +import com.sun.jdi.event.VMDisconnectEvent; 1.61 +import com.sun.jdi.request.ClassPrepareRequest; 1.62 +import com.sun.jdi.request.EventRequest; 1.63 +import com.sun.jdi.request.EventRequestManager; 1.64 +import com.sun.jdi.request.ModificationWatchpointRequest; 1.65 + 1.66 +public class FieldMonitor { 1.67 + 1.68 + public static final String CLASS_NAME = "TestPostFieldModification"; 1.69 + public static final String FIELD_NAME = "value"; 1.70 + public static final String ARGUMENTS = "-Xshare:off -XX:+PrintGC"; 1.71 + 1.72 + public static void main(String[] args) 1.73 + throws IOException, InterruptedException { 1.74 + 1.75 + StringBuffer sb = new StringBuffer(); 1.76 + 1.77 + for (int i=0; i < args.length; i++) { 1.78 + sb.append(' '); 1.79 + sb.append(args[i]); 1.80 + } 1.81 + //VirtualMachine vm = launchTarget(sb.toString()); 1.82 + VirtualMachine vm = launchTarget(CLASS_NAME); 1.83 + 1.84 + System.out.println("Vm launched"); 1.85 + // set watch field on already loaded classes 1.86 + List<ReferenceType> referenceTypes = vm 1.87 + .classesByName(CLASS_NAME); 1.88 + for (ReferenceType refType : referenceTypes) { 1.89 + addFieldWatch(vm, refType); 1.90 + } 1.91 + // watch for loaded classes 1.92 + addClassWatch(vm); 1.93 + 1.94 + // process events 1.95 + EventQueue eventQueue = vm.eventQueue(); 1.96 + // resume the vm 1.97 + 1.98 + Process process = vm.process(); 1.99 + 1.100 + 1.101 + // Copy target's output and error to our output and error. 1.102 + Thread outThread = new StreamRedirectThread("out reader", process.getInputStream()); 1.103 + Thread errThread = new StreamRedirectThread("error reader", process.getErrorStream()); 1.104 + 1.105 + errThread.start(); 1.106 + outThread.start(); 1.107 + 1.108 + 1.109 + vm.resume(); 1.110 + boolean connected = true; 1.111 + while (connected) { 1.112 + EventSet eventSet = eventQueue.remove(); 1.113 + for (Event event : eventSet) { 1.114 + if (event instanceof VMDeathEvent 1.115 + || event instanceof VMDisconnectEvent) { 1.116 + // exit 1.117 + connected = false; 1.118 + } else if (event instanceof ClassPrepareEvent) { 1.119 + // watch field on loaded class 1.120 + System.out.println("ClassPrepareEvent"); 1.121 + ClassPrepareEvent classPrepEvent = (ClassPrepareEvent) event; 1.122 + ReferenceType refType = classPrepEvent 1.123 + .referenceType(); 1.124 + addFieldWatch(vm, refType); 1.125 + } else if (event instanceof ModificationWatchpointEvent) { 1.126 + System.out.println("sleep for 500 ms"); 1.127 + Thread.sleep(500); 1.128 + System.out.println("resume..."); 1.129 + 1.130 + ModificationWatchpointEvent modEvent = (ModificationWatchpointEvent) event; 1.131 + System.out.println("old=" 1.132 + + modEvent.valueCurrent()); 1.133 + System.out.println("new=" + modEvent.valueToBe()); 1.134 + System.out.println(); 1.135 + } 1.136 + } 1.137 + eventSet.resume(); 1.138 + } 1.139 + // Shutdown begins when event thread terminates 1.140 + try { 1.141 + errThread.join(); // Make sure output is forwarded 1.142 + outThread.join(); 1.143 + } catch (InterruptedException exc) { 1.144 + // we don't interrupt 1.145 + } 1.146 + } 1.147 + 1.148 + /** 1.149 + * Find a com.sun.jdi.CommandLineLaunch connector 1.150 + */ 1.151 + static LaunchingConnector findLaunchingConnector() { 1.152 + List <Connector> connectors = Bootstrap.virtualMachineManager().allConnectors(); 1.153 + Iterator <Connector> iter = connectors.iterator(); 1.154 + while (iter.hasNext()) { 1.155 + Connector connector = iter.next(); 1.156 + if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) { 1.157 + return (LaunchingConnector)connector; 1.158 + } 1.159 + } 1.160 + throw new Error("No launching connector"); 1.161 + } 1.162 + /** 1.163 + * Return the launching connector's arguments. 1.164 + */ 1.165 + static Map <String,Connector.Argument> connectorArguments(LaunchingConnector connector, String mainArgs) { 1.166 + Map<String,Connector.Argument> arguments = connector.defaultArguments(); 1.167 + for (String key : arguments.keySet()) { 1.168 + System.out.println(key); 1.169 + } 1.170 + 1.171 + Connector.Argument mainArg = (Connector.Argument)arguments.get("main"); 1.172 + if (mainArg == null) { 1.173 + throw new Error("Bad launching connector"); 1.174 + } 1.175 + mainArg.setValue(mainArgs); 1.176 + 1.177 + Connector.Argument optionsArg = (Connector.Argument)arguments.get("options"); 1.178 + if (optionsArg == null) { 1.179 + throw new Error("Bad launching connector"); 1.180 + } 1.181 + optionsArg.setValue(ARGUMENTS); 1.182 + return arguments; 1.183 + } 1.184 + 1.185 + static VirtualMachine launchTarget(String mainArgs) { 1.186 + LaunchingConnector connector = findLaunchingConnector(); 1.187 + Map arguments = connectorArguments(connector, mainArgs); 1.188 + try { 1.189 + return (VirtualMachine) connector.launch(arguments); 1.190 + } catch (IOException exc) { 1.191 + throw new Error("Unable to launch target VM: " + exc); 1.192 + } catch (IllegalConnectorArgumentsException exc) { 1.193 + throw new Error("Internal error: " + exc); 1.194 + } catch (VMStartException exc) { 1.195 + throw new Error("Target VM failed to initialize: " + 1.196 + exc.getMessage()); 1.197 + } 1.198 +} 1.199 + 1.200 + 1.201 + private static void addClassWatch(VirtualMachine vm) { 1.202 + EventRequestManager erm = vm.eventRequestManager(); 1.203 + ClassPrepareRequest classPrepareRequest = erm 1.204 + .createClassPrepareRequest(); 1.205 + classPrepareRequest.addClassFilter(CLASS_NAME); 1.206 + classPrepareRequest.setEnabled(true); 1.207 + } 1.208 + 1.209 + 1.210 + private static void addFieldWatch(VirtualMachine vm, 1.211 + ReferenceType refType) { 1.212 + EventRequestManager erm = vm.eventRequestManager(); 1.213 + Field field = refType.fieldByName(FIELD_NAME); 1.214 + ModificationWatchpointRequest modificationWatchpointRequest = erm 1.215 + .createModificationWatchpointRequest(field); 1.216 + modificationWatchpointRequest.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD); 1.217 + modificationWatchpointRequest.setEnabled(true); 1.218 + } 1.219 +} 1.220 + 1.221 +class StreamRedirectThread extends Thread { 1.222 + 1.223 + private final BufferedReader in; 1.224 + 1.225 + private static final int BUFFER_SIZE = 2048; 1.226 + 1.227 + /** 1.228 + * Set up for copy. 1.229 + * @param name Name of the thread 1.230 + * @param in Stream to copy from 1.231 + * @param out Stream to copy to 1.232 + */ 1.233 + StreamRedirectThread(String name, InputStream in) { 1.234 + super(name); 1.235 + this.in = new BufferedReader(new InputStreamReader(in)); 1.236 + } 1.237 + 1.238 + /** 1.239 + * Copy. 1.240 + */ 1.241 + public void run() { 1.242 + try { 1.243 + String line; 1.244 + while ((line = in.readLine ()) != null) { 1.245 + System.out.println ("testvm: " + line); 1.246 + } 1.247 + System.out.flush(); 1.248 + } catch(IOException exc) { 1.249 + System.err.println("Child I/O Transfer - " + exc); 1.250 + } 1.251 + } 1.252 +}