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.process.OutputAnalyzer;
  27 import jdk.test.lib.process.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 /test/lib
  37  * @run main JstackThreadTest
  38  */
  39 public class JstackThreadTest {
  40   static class NamedThread extends Thread {
  41    CountDownLatch latch;
  42    NamedThread(String name, CountDownLatch latch) {
  43       this.latch = latch;
  44       setName(name);
  45 
  46     }
  47     @Override
  48     public void run() {
  49      latch.countDown();
  50      Utils.sleep();
  51     }
  52    }
  53 
  54   public static void main(String[] args) throws Exception {
  55     StringBuilder sb = new StringBuilder();
  56      /*create a string more than 1996 character */
  57     for(int i = 0; i < 1998; i++){
  58       sb.append("a");
  59     }
  60     testWithName(sb.toString());
  61   }
  62 
  63   private static void testWithName(String name) throws Exception {
  64     //parent thread countDown latch
  65     CountDownLatch latch = new CountDownLatch(1);
  66     // Start a thread with a long thread name
  67     NamedThread thread = new NamedThread(name, latch);
  68     thread.setDaemon(true);
  69     thread.start();
  70     ProcessBuilder processBuilder = new ProcessBuilder();
  71     JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jstack");
  72     launcher.addToolArg("-l");
  73     launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
  74     processBuilder.command(launcher.getCommand());
  75     System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
  76     // Ensuring that Jstack will always run after NamedThread
  77     latch.await();
  78     OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
  79     System.out.println(output.getOutput());
  80     output.shouldContain("\""+ name + "\"");
  81   }
  82 }