1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *  @test
  26  *  @bug 4420844 4449394
  27  *  @summary Checks that no events are sent after VMDeath, and test vm.canBeModified
  28  *
  29  *  @author Robert Field
  30  *
  31  *  @modules jdk.jdi
  32  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  *  @run compile -g HelloWorld.java
  34  *  @run build VMDeathLastTest
  35  *  @run driver VMDeathLastTest
  36  */
  37 import com.sun.jdi.*;
  38 import com.sun.jdi.event.*;
  39 import com.sun.jdi.request.*;
  40 
  41 import java.util.*;
  42 
  43     /********** test program **********/
  44 
  45 public class VMDeathLastTest extends TestScaffold {
  46     Object syncer = new Object();
  47     boolean vmDead = false;
  48     boolean disconnected = false;
  49 
  50     VMDeathLastTest (String args[]) {
  51         super(args);
  52     }
  53 
  54     public static void main(String[] args)      throws Exception {
  55         new VMDeathLastTest(args).startTests();
  56     }
  57 
  58     /********** event handlers **********/
  59 
  60     public void methodEntered(MethodEntryEvent event) {
  61         if (vmDead) {
  62             failure("Failure: Got MethodEntryEvent after VM Dead");
  63         }
  64     }
  65 
  66     public void classPrepared(ClassPrepareEvent event) {
  67         if (vmDead) {
  68             failure("Failure: Got ClassPrepareEvent after VM Dead");
  69         }
  70     }
  71 
  72     public void threadDied(ThreadDeathEvent event) {
  73         if (vmDead) {
  74             failure("Failure: Got ThreadDeathEvent after VM Dead");
  75         }
  76     }
  77 
  78     public void vmDied(VMDeathEvent event) {
  79         println("Got VMDeathEvent");
  80         vmDead = true;
  81     }
  82 
  83     public void vmDisconnected(VMDisconnectEvent event) {
  84         println("Got VMDisconnectEvent");
  85         if (!vmDead) {
  86             failure("Test failure: didn't get VMDeath");
  87         }
  88         disconnected = true;
  89         synchronized (syncer) {
  90             syncer.notifyAll();
  91         }
  92     }
  93 
  94     /**
  95      * Turn off default VMDeath handling.
  96      */
  97     protected void createDefaultVMDeathRequest() {
  98     }
  99 
 100     /********** test core **********/
 101 
 102     protected void runTests() throws Exception {
 103         /*
 104          * Get to the top of main()
 105          * to determine targetClass and mainThread
 106          */
 107         startToMain("HelloWorld");
 108         if (!vm().canBeModified()) {
 109             failure("VM says it is read-only");
 110         }
 111         EventRequestManager erm = vm().eventRequestManager();
 112 
 113         /*
 114          * Set event requests
 115          */
 116         erm.createMethodEntryRequest().enable();
 117         erm.createClassPrepareRequest().enable();
 118         erm.createThreadDeathRequest().enable();
 119 
 120         /*
 121          * resume the target listening for events
 122          */
 123         addListener(this);
 124         synchronized (syncer) {
 125             vm().resume();
 126             while (!disconnected) {
 127                 try {
 128                     syncer.wait();
 129                 } catch (InterruptedException e) {
 130                 }
 131             }
 132         }
 133 
 134         /*
 135          * deal with results of test
 136          */
 137         if (!testFailed) {
 138             println("VMDeathLastTest: passed");
 139         } else {
 140             throw new Exception("VMDeathLastTest: failed");
 141         }
 142     }
 143 }