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 4513488
  27  * @summary Test for JDI: Internal JDI helper threads should setDaemon(true)
  28  * @author Tim Bell
  29  *
  30  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  31  * @run compile -g DebuggerThreadTest.java
  32  * @run driver DebuggerThreadTest
  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 DebuggerThreadTarg {
  43     public void ready() {
  44     }
  45     public static void main(String[] args){
  46         System.out.println("Howdy!");
  47         DebuggerThreadTarg targ = new DebuggerThreadTarg();
  48         targ.ready();
  49         System.out.println("Goodbye from DebuggerThreadTarg!");
  50     }
  51 }
  52 
  53     /********** test program **********/
  54 
  55 public class DebuggerThreadTest extends TestScaffold {
  56 
  57     DebuggerThreadTest (String args[]) {
  58         super(args);
  59     }
  60 
  61     public static void main(String[] args)      throws Exception {
  62         new DebuggerThreadTest(args).startTests();
  63     }
  64 
  65     /*
  66      * Move to top ThreadGroup and dump all threads.
  67      */
  68     public void dumpThreads() {
  69         ThreadGroup tg = Thread.currentThread().getThreadGroup();
  70         ThreadGroup parent = tg.getParent();
  71         while (parent != null) {
  72             tg = parent;
  73             parent = tg.getParent();
  74         }
  75         int listThreads = tg.activeCount();
  76         Thread list[] = new Thread[listThreads * 2];
  77         int gotThreads = tg.enumerate(list, true);
  78         for (int i = 0; i < Math.min(gotThreads, list.length); i++){
  79             Thread t = list[i];
  80             String groupName = t.getThreadGroup().getName();
  81 
  82             System.out.println("Thread [" + i + "] group = '" +
  83                                groupName +
  84                                "' name = '" + t.getName() +
  85                                "' daemon = " + t.isDaemon());
  86 
  87             if (groupName.startsWith("JDI ") &&
  88                 (! t.isDaemon())) {
  89                 failure("FAIL: non-daemon thread '" + t.getName() +
  90                         "' found in ThreadGroup '" + groupName + "'");
  91             }
  92 
  93         }
  94     }
  95 
  96     protected void runTests() throws Exception {
  97         try {
  98             /*
  99              * Get to the top of ready()
 100              */
 101             startTo("DebuggerThreadTarg", "ready", "()V");
 102 
 103             dumpThreads();
 104 
 105             listenUntilVMDisconnect();
 106 
 107         } finally {
 108             /*
 109              * deal with results of test
 110              * if anything has called failure("foo") testFailed will be true
 111              */
 112             if (!testFailed) {
 113                 println("DebuggerThreadTest: passed");
 114             } else {
 115                 throw new Exception("DebuggerThreadTest: failed");
 116             }
 117         }
 118         return;
 119     }
 120 }