Merge

Fri, 13 Apr 2012 08:11:36 -0700

author
coleenp
date
Fri, 13 Apr 2012 08:11:36 -0700
changeset 3707
0f701f572aed
parent 3705
df4cd4aac5c1
parent 3706
27dab8a7c762
child 3708
c263e0e9f14b

Merge

     1.1 --- a/test/runtime/7158988/TestPostFieldModification.java	Thu Apr 12 13:24:08 2012 +0200
     1.2 +++ b/test/runtime/7158988/TestPostFieldModification.java	Fri Apr 13 08:11:36 2012 -0700
     1.3 @@ -21,229 +21,38 @@
     1.4   * questions.
     1.5   */
     1.6  
     1.7 -/*
     1.8 - * @test FieldMonitor.java
     1.9 - * @bug 7158988
    1.10 - * @summary verify jvm does not crash while debugging
    1.11 - * @run shell TestFieldMonitor.sh
    1.12 - * @author axel.siebenborn@sap.com
    1.13 - */
    1.14 -import java.io.BufferedReader;
    1.15 -import java.io.IOException;
    1.16 -import java.io.InputStream;
    1.17 -import java.io.InputStreamReader;
    1.18 -import java.io.OutputStream;
    1.19 -import java.io.OutputStreamWriter;
    1.20 -import java.io.Reader;
    1.21 -import java.io.Writer;
    1.22 -import java.util.Iterator;
    1.23 -import java.util.List;
    1.24 -import java.util.Map;
    1.25 +public class TestPostFieldModification {
    1.26  
    1.27 -import com.sun.jdi.Bootstrap;
    1.28 -import com.sun.jdi.Field;
    1.29 -import com.sun.jdi.ReferenceType;
    1.30 -import com.sun.jdi.VirtualMachine;
    1.31 -import com.sun.jdi.connect.Connector;
    1.32 -import com.sun.jdi.connect.IllegalConnectorArgumentsException;
    1.33 -import com.sun.jdi.connect.LaunchingConnector;
    1.34 -import com.sun.jdi.connect.VMStartException;
    1.35 -import com.sun.jdi.event.ClassPrepareEvent;
    1.36 -import com.sun.jdi.event.Event;
    1.37 -import com.sun.jdi.event.EventQueue;
    1.38 -import com.sun.jdi.event.EventSet;
    1.39 -import com.sun.jdi.event.ModificationWatchpointEvent;
    1.40 -import com.sun.jdi.event.VMDeathEvent;
    1.41 -import com.sun.jdi.event.VMDisconnectEvent;
    1.42 -import com.sun.jdi.request.ClassPrepareRequest;
    1.43 -import com.sun.jdi.request.EventRequest;
    1.44 -import com.sun.jdi.request.EventRequestManager;
    1.45 -import com.sun.jdi.request.ModificationWatchpointRequest;
    1.46 +  public String value;  // watch modification of value
    1.47  
    1.48 -public class FieldMonitor {
    1.49 +  public static void main(String[] args){
    1.50  
    1.51 -  public static final String CLASS_NAME = "TestPostFieldModification";
    1.52 -  public static final String FIELD_NAME = "value";
    1.53 -  public static final String ARGUMENTS = "-Xshare:off -XX:+PrintGC";
    1.54 -
    1.55 -  public static void main(String[] args)
    1.56 -      throws IOException, InterruptedException {
    1.57 -
    1.58 -    StringBuffer sb = new StringBuffer();
    1.59 -
    1.60 -    for (int i=0; i < args.length; i++) {
    1.61 -        sb.append(' ');
    1.62 -        sb.append(args[i]);
    1.63 -    }
    1.64 -    //VirtualMachine vm = launchTarget(sb.toString());
    1.65 -    VirtualMachine vm = launchTarget(CLASS_NAME);
    1.66 -
    1.67 -    System.out.println("Vm launched");
    1.68 -    // set watch field on already loaded classes
    1.69 -    List<ReferenceType> referenceTypes = vm
    1.70 -        .classesByName(CLASS_NAME);
    1.71 -    for (ReferenceType refType : referenceTypes) {
    1.72 -      addFieldWatch(vm, refType);
    1.73 -    }
    1.74 -    // watch for loaded classes
    1.75 -    addClassWatch(vm);
    1.76 -
    1.77 -    // process events
    1.78 -    EventQueue eventQueue = vm.eventQueue();
    1.79 -    // resume the vm
    1.80 -
    1.81 -    Process process = vm.process();
    1.82 -
    1.83 -
    1.84 -    // Copy target's output and error to our output and error.
    1.85 -    Thread outThread = new StreamRedirectThread("out reader", process.getInputStream());
    1.86 -    Thread errThread = new StreamRedirectThread("error reader", process.getErrorStream());
    1.87 -
    1.88 -    errThread.start();
    1.89 -    outThread.start();
    1.90 -
    1.91 -
    1.92 -    vm.resume();
    1.93 -    boolean connected = true;
    1.94 -    while (connected) {
    1.95 -      EventSet eventSet = eventQueue.remove();
    1.96 -      for (Event event : eventSet) {
    1.97 -        if (event instanceof VMDeathEvent
    1.98 -            || event instanceof VMDisconnectEvent) {
    1.99 -          // exit
   1.100 -          connected = false;
   1.101 -        } else if (event instanceof ClassPrepareEvent) {
   1.102 -          // watch field on loaded class
   1.103 -          System.out.println("ClassPrepareEvent");
   1.104 -          ClassPrepareEvent classPrepEvent = (ClassPrepareEvent) event;
   1.105 -          ReferenceType refType = classPrepEvent
   1.106 -              .referenceType();
   1.107 -          addFieldWatch(vm, refType);
   1.108 -        } else if (event instanceof ModificationWatchpointEvent) {
   1.109 -          System.out.println("sleep for 500 ms");
   1.110 -          Thread.sleep(500);
   1.111 -          System.out.println("resume...");
   1.112 -
   1.113 -          ModificationWatchpointEvent modEvent = (ModificationWatchpointEvent) event;
   1.114 -          System.out.println("old="
   1.115 -              + modEvent.valueCurrent());
   1.116 -          System.out.println("new=" + modEvent.valueToBe());
   1.117 -          System.out.println();
   1.118 +    System.out.println("Start threads");
   1.119 +    // this thread modifies the field 'value'
   1.120 +    new Thread() {
   1.121 +      TestPostFieldModification test = new TestPostFieldModification();
   1.122 +      public void run() {
   1.123 +        test.value="test";
   1.124 +        for(int i = 0; i < 10; i++) {
   1.125 +          test.value += new String("_test");
   1.126          }
   1.127        }
   1.128 -      eventSet.resume();
   1.129 -    }
   1.130 -    // Shutdown begins when event thread terminates
   1.131 -    try {
   1.132 -        errThread.join(); // Make sure output is forwarded
   1.133 -        outThread.join();
   1.134 -    } catch (InterruptedException exc) {
   1.135 -        // we don't interrupt
   1.136 -    }
   1.137 -  }
   1.138 +    }.start();
   1.139  
   1.140 -  /**
   1.141 -   * Find a com.sun.jdi.CommandLineLaunch connector
   1.142 -   */
   1.143 -  static LaunchingConnector findLaunchingConnector() {
   1.144 -    List <Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();
   1.145 -    Iterator <Connector> iter = connectors.iterator();
   1.146 -    while (iter.hasNext()) {
   1.147 -      Connector connector = iter.next();
   1.148 -      if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) {
   1.149 -        return (LaunchingConnector)connector;
   1.150 +    // this thread is used to trigger a gc
   1.151 +    Thread d = new Thread() {
   1.152 +      public void run() {
   1.153 +        while(true) {
   1.154 +          try {
   1.155 +            Thread.sleep(100);
   1.156 +          } catch (InterruptedException e) {
   1.157 +
   1.158 +          }
   1.159 +          System.gc();
   1.160 +        }
   1.161        }
   1.162 -    }
   1.163 -    throw new Error("No launching connector");
   1.164 -  }
   1.165 -  /**
   1.166 -   * Return the launching connector's arguments.
   1.167 -   */
   1.168 - static Map <String,Connector.Argument> connectorArguments(LaunchingConnector connector, String mainArgs) {
   1.169 -      Map<String,Connector.Argument> arguments = connector.defaultArguments();
   1.170 -      for (String key : arguments.keySet()) {
   1.171 -        System.out.println(key);
   1.172 -      }
   1.173 -
   1.174 -      Connector.Argument mainArg = (Connector.Argument)arguments.get("main");
   1.175 -      if (mainArg == null) {
   1.176 -          throw new Error("Bad launching connector");
   1.177 -      }
   1.178 -      mainArg.setValue(mainArgs);
   1.179 -
   1.180 -      Connector.Argument optionsArg = (Connector.Argument)arguments.get("options");
   1.181 -      if (optionsArg == null) {
   1.182 -        throw new Error("Bad launching connector");
   1.183 -      }
   1.184 -      optionsArg.setValue(ARGUMENTS);
   1.185 -      return arguments;
   1.186 -  }
   1.187 -
   1.188 - static VirtualMachine launchTarget(String mainArgs) {
   1.189 -    LaunchingConnector connector = findLaunchingConnector();
   1.190 -    Map  arguments = connectorArguments(connector, mainArgs);
   1.191 -    try {
   1.192 -        return (VirtualMachine) connector.launch(arguments);
   1.193 -    } catch (IOException exc) {
   1.194 -        throw new Error("Unable to launch target VM: " + exc);
   1.195 -    } catch (IllegalConnectorArgumentsException exc) {
   1.196 -        throw new Error("Internal error: " + exc);
   1.197 -    } catch (VMStartException exc) {
   1.198 -        throw new Error("Target VM failed to initialize: " +
   1.199 -                        exc.getMessage());
   1.200 -    }
   1.201 -}
   1.202 -
   1.203 -
   1.204 -  private static void addClassWatch(VirtualMachine vm) {
   1.205 -    EventRequestManager erm = vm.eventRequestManager();
   1.206 -    ClassPrepareRequest classPrepareRequest = erm
   1.207 -        .createClassPrepareRequest();
   1.208 -    classPrepareRequest.addClassFilter(CLASS_NAME);
   1.209 -    classPrepareRequest.setEnabled(true);
   1.210 -  }
   1.211 -
   1.212 -
   1.213 -  private static void addFieldWatch(VirtualMachine vm,
   1.214 -      ReferenceType refType) {
   1.215 -    EventRequestManager erm = vm.eventRequestManager();
   1.216 -    Field field = refType.fieldByName(FIELD_NAME);
   1.217 -    ModificationWatchpointRequest modificationWatchpointRequest = erm
   1.218 -        .createModificationWatchpointRequest(field);
   1.219 -    modificationWatchpointRequest.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
   1.220 -    modificationWatchpointRequest.setEnabled(true);
   1.221 +    };
   1.222 +    d.setDaemon(true);
   1.223 +    d.start();
   1.224    }
   1.225  }
   1.226 -
   1.227 -class StreamRedirectThread extends Thread {
   1.228 -
   1.229 -  private final BufferedReader in;
   1.230 -
   1.231 -  private static final int BUFFER_SIZE = 2048;
   1.232 -
   1.233 -  /**
   1.234 -   * Set up for copy.
   1.235 -   * @param name  Name of the thread
   1.236 -   * @param in    Stream to copy from
   1.237 -   * @param out   Stream to copy to
   1.238 -   */
   1.239 -  StreamRedirectThread(String name, InputStream in) {
   1.240 -    super(name);
   1.241 -    this.in = new BufferedReader(new InputStreamReader(in));
   1.242 -  }
   1.243 -
   1.244 -  /**
   1.245 -   * Copy.
   1.246 -   */
   1.247 -  public void run() {
   1.248 -    try {
   1.249 -      String line;
   1.250 -        while ((line = in.readLine ()) != null) {
   1.251 -          System.out.println ("testvm: " + line);
   1.252 -      }
   1.253 -     System.out.flush();
   1.254 -    } catch(IOException exc) {
   1.255 -      System.err.println("Child I/O Transfer - " + exc);
   1.256 -    }
   1.257 -  }
   1.258 -}

mercurial