1 /*
   2  * Copyright (c) 2013, 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 7105883
  27  * @summary Ensure that JDWP doesn't crash with a null thread group name
  28  *
  29  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  30  * @run driver NullThreadGroupNameTest
  31  */
  32 import com.sun.jdi.*;
  33 import com.sun.jdi.connect.*;
  34 import com.sun.jdi.event.*;
  35 import com.sun.jdi.request.*;
  36 import com.sun.jdi.VMDisconnectedException;
  37 import java.util.concurrent.CountDownLatch;
  38 import java.util.*;
  39 
  40 class DebugTarget {
  41     public final static String DEBUG_THREAD_NAME = "DebugThread";
  42 
  43     public static void main(String[] args) throws Exception {
  44         DebugThread thread = new DebugThread();
  45         thread.start();
  46         thread.runningLatch.await();
  47         breakpointHere();
  48         thread.breakpointLatch.countDown();
  49     }
  50 
  51     public static void breakpointHere() {
  52         System.out.println("Breakpoint finished!");
  53     }
  54 
  55     static class DebugThread extends Thread {
  56         final CountDownLatch runningLatch = new CountDownLatch(1);
  57         final CountDownLatch breakpointLatch = new CountDownLatch(1);
  58 
  59         public DebugThread() {
  60             super(new ThreadGroup(null), DEBUG_THREAD_NAME);
  61         }
  62 
  63         public void run() {
  64             runningLatch.countDown();
  65             try {
  66                 breakpointLatch.await();
  67             } catch (InterruptedException ie) {
  68                 ie.printStackTrace();
  69             }
  70         }
  71     }
  72 }
  73 
  74 public class NullThreadGroupNameTest extends TestScaffold {
  75 
  76     NullThreadGroupNameTest(String args[]) {
  77         super(args);
  78     }
  79 
  80     public static void main(String[] args) throws Exception {
  81         new NullThreadGroupNameTest(args).startTests();
  82     }
  83 
  84     protected void runTests() throws Exception {
  85         startTo("DebugTarget", "breakpointHere", "()V");
  86 
  87         ThreadReference thread = findThread(DebugTarget.DEBUG_THREAD_NAME);
  88         assertThreadGroupName(thread.threadGroup(), "");
  89 
  90         listenUntilVMDisconnect();
  91     }
  92 
  93     private ThreadReference findThread(String name) {
  94         for (ThreadReference thread : vm().allThreads()) {
  95             if (name.equals(thread.name())) {
  96                 return thread;
  97             }
  98         }
  99         throw new NoSuchElementException("Couldn't find " + name);
 100     }
 101 
 102     private void assertThreadGroupName(ThreadGroupReference threadGroup, String expectedName) {
 103         try {
 104             String name = threadGroup.name();
 105             if (!expectedName.equals(name)) {
 106                 throw new AssertionError("Unexpected thread group name '" + name + "'");
 107             }
 108         } catch (VMDisconnectedException vmde) {
 109             throw new AssertionError("Likely JVM crash with null thread group name", vmde);
 110         }
 111     }
 112 }