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 /*
  25  * @test
  26  * @summary Test for repeated attaches and detaches
  27  * @library /test/lib
  28  * @run main TestAttachDetach
  29  */
  30 
  31 import java.io.BufferedReader;
  32 import java.io.IOException;
  33 import java.io.OutputStream;
  34 import java.io.InputStreamReader;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Arrays;
  38 import java.util.Optional;
  39 import jdk.test.lib.JDKToolLauncher;
  40 import jdk.test.lib.Utils;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 import jdk.test.lib.process.ProcessTools;
  43 import jdk.test.lib.apps.LingeredApp;
  44 import jdk.test.lib.Platform;
  45 import jdk.test.lib.JDKToolLauncher;
  46 
  47 public class TestAttachDetach {
  48 
  49     private static LingeredApp theApp = null;
  50 
  51     private static JDKToolLauncher createSALauncher() {
  52         JDKToolLauncher launcher = null;
  53         launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
  54 
  55         return launcher;
  56     }
  57 
  58     public static void launchCLHSDB()
  59         throws IOException {
  60 
  61         System.out.println("Starting LingeredApp");
  62         try {
  63             theApp = LingeredApp.startApp();
  64 
  65             System.out.println("Starting clhsdb against " + theApp.getPid());
  66             JDKToolLauncher launcher = createSALauncher();
  67             launcher.addToolArg("clhsdb");
  68             launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
  69 
  70             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
  71             Process toolProcess = processBuilder.start();
  72 
  73             try (OutputStream out = toolProcess.getOutputStream()) {
  74                 out.write("detach\n".getBytes());
  75                 out.write("reattach\n".getBytes());
  76                 out.write("threads\n".getBytes());
  77                 out.write("detach\n".getBytes());
  78                 out.write("quit\n".getBytes());
  79             }
  80 
  81             Exception unexpected = null;
  82             try (BufferedReader reader =
  83                 new BufferedReader(new InputStreamReader(toolProcess.getErrorStream()))) {
  84                 String line;
  85                 int numberOfMatches = 0;
  86 
  87                 while ((line = reader.readLine()) != null) {
  88                     line = line.trim();
  89                     System.out.println(line);
  90 
  91                     if (line.contains("Attaching to process")) {
  92                         numberOfMatches++;
  93                     }
  94                 }
  95                 if (numberOfMatches != 2) {
  96                     unexpected =
  97                         new RuntimeException(
  98                             "The String 'Attaching to process' appears " +
  99                              numberOfMatches + " number of times");
 100                 }
 101             }
 102 
 103             if (unexpected == null) {
 104                 try (BufferedReader reader =
 105                     new BufferedReader(new InputStreamReader(toolProcess.getInputStream()))) {
 106                     String line;
 107                     boolean found = false;
 108 
 109                     while ((line = reader.readLine()) != null) {
 110                         line = line.trim();
 111                         System.out.println(line);
 112                         if (line.contains("Last_Java_SP")) {
 113                             found = true;
 114                             break;
 115                         }
 116                     }
 117                     if (found == false) {
 118                         unexpected =
 119                             new RuntimeException("Output from the 'threads' command not found");
 120                     }
 121                 }
 122             }
 123 
 124             toolProcess.waitFor();
 125 
 126             if (toolProcess.exitValue() != 0) {
 127                 throw new RuntimeException("FAILED CLHSDB terminated with non-zero exit code "
 128                                             + toolProcess.exitValue());
 129             }
 130 
 131             if (unexpected != null) {
 132                 throw unexpected;
 133             }
 134 
 135         } catch (Exception ex) {
 136             throw new RuntimeException("Test ERROR " + ex, ex);
 137         } finally {
 138             LingeredApp.stopApp(theApp);
 139         }
 140     }
 141 
 142     public static void main(String[] args) throws Exception {
 143 
 144         if (!Platform.shouldSAAttach()) {
 145             // Silently skip the test if we don't have enough permissions to attach
 146             System.err.println("Error! Insufficient permissions to attach.");
 147             return;
 148         }
 149 
 150         launchCLHSDB();
 151 
 152         // The test throws RuntimeException on error.
 153         // IOException is thrown if LingeredApp can't start because of some bad
 154         // environment condition
 155         System.out.println("Test PASSED");
 156     }
 157 }