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