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 4364671
  27  *  @summary Creating a StepRequest on a nonexistant thread fails
  28  *
  29  *  @author jjh
  30  *
  31  *  @modules jdk.jdi
  32  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  *  @run compile -g AfterThreadDeathTest.java
  34  *  @run driver AfterThreadDeathTest
  35  */
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.event.*;
  38 import com.sun.jdi.request.*;
  39 
  40 import java.util.*;
  41 
  42     /********** target program **********/
  43 
  44 class AfterDeathTarg {
  45     public static void main(String[] args){
  46         System.out.println("Howdy!");
  47         System.out.println("Goodbye from AfterDeathTarg!");
  48     }
  49 }
  50 
  51     /********** test program **********/
  52 
  53 public class AfterThreadDeathTest extends TestScaffold {
  54     ReferenceType targetClass;
  55     ThreadReference mainThread;
  56     StepRequest stepRequest = null;
  57     EventRequestManager erm;
  58     boolean mainIsDead;
  59 
  60     AfterThreadDeathTest (String args[]) {
  61         super(args);
  62     }
  63 
  64     public static void main(String[] args)      throws Exception {
  65         new AfterThreadDeathTest(args).startTests();
  66     }
  67 
  68     /********** event handlers **********/
  69 
  70     public void threadStarted(ThreadStartEvent event) {
  71         println("Got ThreadStartEvent: " + event);
  72 
  73         if (stepRequest != null) {
  74             erm.deleteEventRequest(stepRequest);
  75             stepRequest = null;
  76             println("Deleted stepRequest");
  77         }
  78 
  79         if (mainIsDead) {
  80             // Here is the odd thing about this test; whatever thread this event
  81             // is for, we do a step on the mainThread. If the mainThread is
  82             // already dead, we should get the exception.  Note that we don't
  83             // come here for the start of the main thread.
  84             stepRequest = erm.createStepRequest(mainThread,
  85                                                 StepRequest.STEP_LINE,
  86                                                 StepRequest.STEP_OVER);
  87             stepRequest.addCountFilter(1);
  88             stepRequest.setSuspendPolicy (EventRequest.SUSPEND_ALL);
  89             try {
  90                 stepRequest.enable();
  91             } catch (IllegalThreadStateException ee) {
  92                 println("Ok; got expected IllegalThreadStateException");
  93                 return;
  94             } catch (Exception ee) {
  95                 failure("FAILED: Did not get expected"
  96                       + " IllegalThreadStateException"
  97                       + " on a StepRequest.enable().  \n"
  98                       + "        Got this exception instead: " + ee);
  99                 return;
 100             }
 101             failure("FAILED: Did not get expected IllegalThreadStateException"
 102                     + " on a StepRequest.enable()");
 103         }
 104     }
 105 
 106     public void threadDied(ThreadDeathEvent event) {
 107         println("Got ThreadDeathEvent: " + event);
 108         if (! mainIsDead) {
 109             if (mainThread.equals(event.thread())) {
 110                 mainIsDead = true;
 111             }
 112         }
 113     }
 114 
 115     public void vmDied(VMDeathEvent event) {
 116         println("Got VMDeathEvent");
 117     }
 118 
 119     public void vmDisconnected(VMDisconnectEvent event) {
 120         println("Got VMDisconnectEvent");
 121     }
 122 
 123     /********** test core **********/
 124 
 125     protected void runTests() throws Exception {
 126         /*
 127          * Get to the top of main()
 128          * to determine targetClass and mainThread
 129          */
 130         BreakpointEvent bpe = startToMain("AfterDeathTarg");
 131         targetClass = bpe.location().declaringType();
 132         mainThread = bpe.thread();
 133         erm = vm().eventRequestManager();
 134 
 135         /*
 136          * Set event requests
 137          */
 138         ThreadStartRequest request = erm.createThreadStartRequest();
 139         request.setSuspendPolicy(EventRequest.SUSPEND_ALL);
 140         request.enable();
 141 
 142         ThreadDeathRequest request1 = erm.createThreadDeathRequest();
 143         request1.setSuspendPolicy(EventRequest.SUSPEND_NONE);
 144         request1.enable();
 145 
 146         /*
 147          * resume the target listening for events
 148          */
 149         listenUntilVMDisconnect();
 150 
 151         /*
 152          * deal with results of test
 153          * if anything has called failure("foo") testFailed will be true
 154          */
 155         if (!testFailed) {
 156             println("AfterThreadDeathTest: passed");
 157         } else {
 158             throw new Exception("AfterThreadDeathTest: failed");
 159         }
 160     }
 161 }