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