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