1 /*
   2  * Copyright (c) 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 import java.util.ArrayList;
  25 import java.util.List;
  26 import java.util.stream.Collectors;
  27 
  28 
  29 import jdk.test.lib.apps.LingeredApp;
  30 import jdk.test.lib.apps.LingeredAppWithDeadlock;
  31 
  32 import jdk.test.lib.Utils;
  33 import jdk.test.lib.Platform;
  34 import jdk.test.lib.JDKToolLauncher;
  35 import jdk.test.lib.OutputAnalyzer;
  36 import jdk.test.lib.ProcessTools;
  37 
  38 /*
  39  * @test
  40  * @summary Test deadlock detection
  41  * @library /test/lib/share/classes
  42  * @library /testlibrary
  43  * @modules java.base/jdk.internal.misc
  44  * @modules java.management
  45  * @build jdk.test.lib.*
  46  * @build jdk.test.lib.apps.*
  47  * @build DeadlockDetectionTest
  48  * @run main DeadlockDetectionTest
  49  */
  50 
  51 public class DeadlockDetectionTest {
  52 
  53     private static LingeredAppWithDeadlock theApp = null;
  54     private static ProcessBuilder processBuilder = new ProcessBuilder();
  55 
  56     private static OutputAnalyzer jstack(String... toolArgs) throws Exception {
  57         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
  58         launcher.addToolArg("jstack");
  59         if (toolArgs != null) {
  60             for (String toolArg : toolArgs) {
  61                 launcher.addToolArg(toolArg);
  62             }
  63         }
  64 
  65         processBuilder.command(launcher.getCommand());
  66         System.out.println(processBuilder.command().stream().collect(Collectors.joining(" ")));
  67         OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
  68         System.out.println(output.getOutput());
  69 
  70         return output;
  71     }
  72 
  73     public static void main(String[] args) throws Exception {
  74         System.out.println("Starting DeadlockDetectionTest");
  75 
  76         if (!Platform.shouldSAAttach()) {
  77             // Silently skip the test if we don't have enough permissions to attach
  78             // Not all conditions checked by function is relevant to SA but it's worth
  79             // to check
  80             System.err.println("Error! Insufficient permissions to attach.");
  81             return;
  82         }
  83 
  84         if (Platform.isOSX()) {
  85             // Coredump stackwalking is not implemented for Darwin
  86             System.out.println("This test is not expected to work on OS X. Skipping");
  87             return;
  88         }
  89 
  90 
  91         if (!LingeredApp.isLastModifiedWorking()) {
  92             // Exact behaviour of the test depends on operating system and the test nature,
  93             // so just print the warning and continue
  94             System.err.println("Warning! Last modified time doesn't work.");
  95         }
  96 
  97         try {
  98             List<String> vmArgs = new ArrayList<String>();
  99             vmArgs.add("-XX:+UsePerfData");
 100             vmArgs.addAll(Utils.getVmOptions());
 101 
 102             theApp = new LingeredAppWithDeadlock();
 103             LingeredApp.startApp(vmArgs, theApp);
 104             OutputAnalyzer output = jstack("--pid", Long.toString(theApp.getPid()));
 105             System.out.println(output.getOutput());
 106 
 107             if (output.getExitValue() == 3) {
 108                 System.out.println("Test can't run for some reason. Skipping");
 109             }
 110             else {
 111                 output.shouldHaveExitValue(0);
 112                 output.shouldContain("Found a total of 1 deadlock.");
 113             }
 114 
 115         } finally {
 116             LingeredApp.stopApp(theApp);
 117         }
 118     }
 119 }