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 
  29 import jdk.test.lib.apps.LingeredApp;
  30 
  31 /*
  32  * @test
  33  * @bug 8193124
  34  * @summary Test the clhsdb 'findpc' command
  35  * @library /test/lib
  36  * @run main/othervm ClhsdbFindPC
  37  */
  38 
  39 public class ClhsdbFindPC {
  40 
  41     private static void testFindPC(boolean withXcomp) throws Exception {
  42         LingeredApp theApp = null;
  43         try {
  44             ClhsdbLauncher test = new ClhsdbLauncher();
  45             theApp = withXcomp ? LingeredApp.startApp(List.of("-Xcomp"))
  46                                : LingeredApp.startApp(List.of("-Xint"));
  47             System.out.print("Started LingeredApp ");
  48             if (withXcomp) {
  49                 System.out.print("(-Xcomp) ");
  50             } else {
  51                 System.out.print("(-Xint) ");
  52             }
  53             System.out.println("with pid " + theApp.getPid());
  54 
  55             // Run 'jstack -v' command to get the pc
  56             List<String> cmds = List.of("jstack -v");
  57             String output = test.run(theApp.getPid(), cmds, null, null);
  58 
  59             // Test the 'findpc' command passing in the pc obtained from
  60             // the 'jstack -v' command
  61             cmds = new ArrayList<String>();
  62 
  63             // Output could be null if the test was skipped due to
  64             // attach permission issues.
  65             if (output != null) {
  66                 String cmdStr = null;
  67                 String[] parts = output.split("LingeredApp.main");
  68                 String[] tokens = parts[1].split(" ");
  69                 for (String token : tokens) {
  70                     if (token.contains("pc")) {
  71                         String[] address = token.split("=");
  72                         // address[1] represents the address of the Method
  73                         cmdStr = "findpc " + address[1].replace(",","");
  74                         cmds.add(cmdStr);
  75                         break;
  76                     }
  77                 }
  78 
  79                 Map<String, List<String>> expStrMap = new HashMap<>();
  80                 if (withXcomp) {
  81                     expStrMap.put(cmdStr, List.of(
  82                             "In code in NMethod for jdk/test/lib/apps/LingeredApp.main",
  83                             "content:",
  84                             "oops:",
  85                             "frame size:"));
  86                 } else {
  87                     expStrMap.put(cmdStr, List.of(
  88                             "In interpreter codelet",
  89                             "invoke return entry points"));
  90                 }
  91 
  92                 test.run(theApp.getPid(), cmds, expStrMap, null);
  93             }
  94         } catch (Exception ex) {
  95             throw new RuntimeException("Test ERROR " + ex, ex);
  96         } finally {
  97             LingeredApp.stopApp(theApp);
  98         }
  99     }
 100 
 101     public static void main(String[] args) throws Exception {
 102         System.out.println("Starting the ClhsdbFindPC test");
 103         testFindPC(true);
 104         testFindPC(false);
 105         System.out.println("Test PASSED");
 106     }
 107 }
 108