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