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.HashMap;
  25 import java.util.List;
  26 import java.util.Map;
  27 import java.util.ArrayList;
  28 import jdk.test.lib.apps.LingeredApp;
  29 
  30 /*
  31  * @test
  32  * @bug 8192985
  33  * @summary Test the clhsdb 'inspect' command
  34  * @library /test/lib
  35  * @run main/othervm ClhsdbInspect
  36  */
  37 
  38 public class ClhsdbInspect {
  39 
  40     public static void main(String[] args) throws Exception {
  41         System.out.println("Starting the ClhsdbInspect test");
  42 
  43         LingeredAppWithLock theApp = null;
  44         try {
  45             ClhsdbLauncher test = new ClhsdbLauncher();
  46 
  47             theApp = new LingeredAppWithLock();
  48             LingeredApp.startApp(null, theApp);
  49             System.out.println("Started LingeredApp with pid " + theApp.getPid());
  50 
  51             // Run the 'jstack -v' command to get the address of a Method*
  52             // and the oop address of a java.lang.ref.ReferenceQueue$Lock
  53             // object
  54             List<String> cmds = List.of("jstack -v");
  55 
  56             String jstackOutput =
  57                 test.run(theApp.getPid(), cmds, null, null);
  58 
  59             if (jstackOutput != null) {
  60                 // Output could be null due to attach permission issues
  61                 // and if we are skipping this.
  62                 String addressString = null;
  63                 Map<String, String> tokensMap = new HashMap<>();
  64                 tokensMap.put("waiting to lock",
  65                               "instance of Oop for java/lang/Class");
  66                 tokensMap.put("Method\\*=", "Type is Method");
  67                 tokensMap.put("waiting to re-lock in wait",
  68                               "instance of Oop for java/lang/ref/ReferenceQueue$Lock");
  69 
  70                 for(String key: tokensMap.keySet()) {
  71                     cmds = new ArrayList<String>();
  72                     Map<String, List<String>> expStrMap = new HashMap<>();
  73 
  74                     String[] snippets = jstackOutput.split(key);
  75                     String[] tokens = snippets[1].split(" ");
  76                     for (String token: tokens) {
  77                         if (token.contains("0x")) {
  78                             addressString =
  79                                 (token.replace("<", "")).replace(">", "");
  80                             break;
  81                         }
  82                     }
  83 
  84                     String cmd = "inspect " + addressString;
  85                     cmds.add(cmd);
  86                     expStrMap.put(cmd, List.of(tokensMap.get(key)));
  87                     test.run(theApp.getPid(), cmds, expStrMap, null);
  88                 }
  89             }
  90         } catch (Exception ex) {
  91             throw new RuntimeException("Test ERROR " + ex, ex);
  92         } finally {
  93             LingeredApp.stopApp(theApp);
  94         }
  95         System.out.println("Test PASSED");
  96     }
  97 }