1 /*
   2  * Copyright (c) 2016, 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 import java.util.Arrays;
  25 import jdk.test.lib.JDKToolLauncher;
  26 import jdk.test.lib.OutputAnalyzer;
  27 import jdk.test.lib.ProcessTools;
  28 import utils.Utils;
  29 import java.util.concurrent.CountDownLatch;
  30 
  31 /*
  32  * @test JstackThreadTest
  33  * @bug 8151442
  34  * @summary jstack doesn't close quotation marks properly with threads' name greater than 1996 characters
  35  * @modules java.base/jdk.internal.misc
  36  * @library /testlibrary
  37  * @build jdk.test.lib.*
  38  * @run main JstackThreadTest
  39  */
  40 public class JstackThreadTest {
  41   static class NamedThread extends Thread {
  42    CountDownLatch latch;
  43    NamedThread(String name, CountDownLatch latch) {
  44       this.latch = latch;
  45       setName(name);
  46 
  47     }
  48     @Override
  49     public void run() {
  50      latch.countDown();
  51      Utils.sleep();
  52     }
  53    }
  54 
  55   public static void main(String[] args) throws Exception {
  56     StringBuilder sb = new StringBuilder();
  57      /*create a string more than 1996 character */
  58     for(int i = 0; i < 1998; i++){
  59       sb.append("a");
  60     }
  61     testWithName(sb.toString());
  62   }
  63 
  64   private static void testWithName(String name) throws Exception {
  65     //parent thread countDown latch
  66     CountDownLatch latch = new CountDownLatch(1);
  67     // Start a thread with a long thread name
  68     NamedThread thread = new NamedThread(name, latch);
  69     thread.setDaemon(true);
  70     thread.start();
  71     ProcessBuilder processBuilder = new ProcessBuilder();
  72     JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jstack");
  73     launcher.addToolArg("-l");
  74     launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
  75     processBuilder.command(launcher.getCommand());
  76     System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
  77     // Ensuring that Jstack will always run after NamedThread
  78     latch.await();
  79     OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
  80     System.out.println(output.getOutput());
  81     output.shouldContain("\""+ name + "\"");
  82   }
  83 }