1 /*
   2  * Copyright (c) 2017, 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.ArrayList;
  25 import java.util.Scanner;
  26 import java.util.List;
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.util.stream.Collectors;
  30 import java.io.OutputStream;
  31 import jdk.test.lib.apps.LingeredApp;
  32 import jdk.test.lib.JDKToolLauncher;
  33 import jdk.test.lib.Platform;
  34 import jdk.test.lib.process.ProcessTools;
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import jdk.test.lib.Utils;
  37 import jdk.test.lib.Asserts;
  38 
  39 /*
  40  * @test
  41  * @library /test/lib
  42  * @run main/othervm TestJhsdbJstackLock
  43  */
  44 
  45 public class TestJhsdbJstackLock {
  46 
  47     public static void main (String... args) throws Exception {
  48 
  49         LingeredApp app = null;
  50 
  51         if (!Platform.shouldSAAttach()) {
  52             System.out.println(
  53                "SA attach not expected to work - test skipped.");
  54             return;
  55         }
  56 
  57         try {
  58             List<String> vmArgs = new ArrayList<String>(Utils.getVmOptions());
  59 
  60             app = new LingeredAppWithLock();
  61             LingeredApp.startApp(vmArgs, app);
  62             System.out.println ("Started LingeredApp with pid " + app.getPid());
  63 
  64             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
  65             launcher.addToolArg("jstack");
  66             launcher.addToolArg("--pid");
  67             launcher.addToolArg(Long.toString(app.getPid()));
  68 
  69             ProcessBuilder pb = new ProcessBuilder();
  70             pb.command(launcher.getCommand());
  71             Process jhsdb = pb.start();
  72 
  73             jhsdb.waitFor();
  74 
  75             OutputAnalyzer out = new OutputAnalyzer(jhsdb);
  76             System.out.println(out.getStdout());
  77             System.err.println(out.getStderr());
  78 
  79             out.shouldMatch("^\\s+- locked <0x[0-9a-f]+> \\(a java\\.lang\\.Class for LingeredAppWithLock\\)$");
  80             out.shouldMatch("^\\s+- waiting to lock <0x[0-9a-f]+> \\(a java\\.lang\\.Class for LingeredAppWithLock\\)$");
  81             out.shouldMatch("^\\s+- locked <0x[0-9a-f]+> \\(a java\\.lang\\.Thread\\)$");
  82             out.shouldMatch("^\\s+- locked <0x[0-9a-f]+> \\(a java\\.lang\\.Class for int\\)$");
  83             out.stderrShouldBeEmpty();
  84 
  85             System.out.println("Test Completed");
  86         } finally {
  87             LingeredApp.stopApp(app);
  88         }
  89     }
  90 }