Skip to content

Commit

Permalink
8248194: Need better support for running SA tests on core files
Browse files Browse the repository at this point in the history
Reviewed-by: lucy
Backport-of: db2d4e8
  • Loading branch information
Andrew Lu committed Mar 21, 2024
1 parent a46d444 commit 6bcb8ac
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 173 deletions.
125 changes: 14 additions & 111 deletions test/hotspot/jtreg/serviceability/sa/ClhsdbCDSCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
* @summary Test the clhsdb commands 'printmdo', 'printall', 'jstack' on a CDS enabled corefile.
* @requires vm.cds
* @requires vm.hasSA
* @requires os.family != "windows"
* @requires vm.flavor == "server"
* @library /test/lib
* @modules java.base/jdk.internal.misc
Expand All @@ -47,15 +46,12 @@
import java.io.IOException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import jdk.test.lib.Asserts;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import jdk.internal.misc.Unsafe;
import java.util.Scanner;
import jdk.test.lib.Utils;
import jtreg.SkippedException;
import jdk.test.lib.util.CoreUtils;

class CrashApp {
public static void main(String[] args) {
Expand All @@ -64,12 +60,8 @@ public static void main(String[] args) {
}

public class ClhsdbCDSCore {

private static final String TEST_CDS_CORE_FILE_NAME = "cds_core_file";
private static final String LOCATIONS_STRING = "location: ";
private static final String RUN_SHELL_NO_LIMIT = "ulimit -c unlimited && ";
private static final String SHARED_ARCHIVE_NAME = "ArchiveForClhsdbCDSCore.jsa";
private static final String CORE_PATTERN_FILE_NAME = "/proc/sys/kernel/core_pattern";
private static String coreFileName;

public static void main(String[] args) throws Exception {
System.out.println("Starting ClhsdbCDSCore test");
Expand All @@ -90,68 +82,31 @@ public static void main(String[] args) throws Exception {
CrashApp.class.getName()
};

OutputAnalyzer crashOut;
OutputAnalyzer crashOutput;
try {
List<String> options = new ArrayList<>();
options.addAll(Arrays.asList(jArgs));
crashOut =
ProcessTools.executeProcess(getTestJvmCommandlineWithPrefix(
RUN_SHELL_NO_LIMIT, options.toArray(new String[0])));
ProcessBuilder pb = ProcessTools.createTestJvm(options);
// Add "ulimit -c unlimited" if we can since we are generating a core file.
pb = CoreUtils.addCoreUlimitCommand(pb);
crashOutput = ProcessTools.executeProcess(pb);
} catch (Throwable t) {
throw new Error("Can't execute the java cds process.", t);
}

System.out.println(crashOut.getOutput());
String crashOutputString = crashOut.getOutput();
String coreFileLocation = getCoreFileLocation(crashOutputString);
if (coreFileLocation == null) {
if (Platform.isOSX()) {
File coresDir = new File("/cores");
if (!coresDir.isDirectory()) {
cleanup();
throw new Error(coresDir + " is not a directory");
}
// the /cores directory is usually not writable on macOS 10.15
if (!coresDir.canWrite()) {
cleanup();
throw new SkippedException("Directory \"" + coresDir +
"\" is not writable");
}
} else if (Platform.isLinux()) {
// Check if a crash report tool is installed.
File corePatternFile = new File(CORE_PATTERN_FILE_NAME);
try (Scanner scanner = new Scanner(corePatternFile)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
line = line.trim();
System.out.println(line);
if (line.startsWith("|")) {
System.out.println(
"\nThis system uses a crash report tool ($cat /proc/sys/kernel/core_pattern).\n" +
"Core files might not be generated. Please reset /proc/sys/kernel/core_pattern\n" +
"to enable core generation. Skipping this test.");
cleanup();
throw new SkippedException("This system uses a crash report tool");
}
}
}
}
throw new Error("Couldn't find core file location in: '" + crashOutputString + "'");
}
try {
Asserts.assertGT(new File(coreFileLocation).length(), 0L, "Unexpected core size");
Files.move(Paths.get(coreFileLocation), Paths.get(TEST_CDS_CORE_FILE_NAME));
} catch (IOException ioe) {
throw new Error("Can't move core file: " + ioe, ioe);
coreFileName = CoreUtils.getCoreFileLocation(crashOutput.getStdout());
} catch (Exception e) {
cleanup();
throw e;
}

ClhsdbLauncher test = new ClhsdbLauncher();

// Ensure that UseSharedSpaces is turned on.
List<String> cmds = List.of("flags UseSharedSpaces");

String useSharedSpacesOutput = test.runOnCore(TEST_CDS_CORE_FILE_NAME, cmds,
null, null);
String useSharedSpacesOutput = test.runOnCore(coreFileName, cmds, null, null);

if (useSharedSpacesOutput == null) {
// Output could be null due to attach permission issues.
Expand Down Expand Up @@ -205,7 +160,7 @@ public static void main(String[] args) throws Exception {
"Method*"));
unExpStrMap.put("jstack -v", List.of(
"sun.jvm.hotspot.debugger.UnmappedAddressException"));
test.runOnCore(TEST_CDS_CORE_FILE_NAME, cmds, expStrMap, unExpStrMap);
test.runOnCore(coreFileName, cmds, expStrMap, unExpStrMap);
} catch (SkippedException e) {
throw e;
} catch (Exception ex) {
Expand All @@ -215,60 +170,8 @@ public static void main(String[] args) throws Exception {
System.out.println("Test PASSED");
}

// lets search for a few possible locations using process output and return existing location
private static String getCoreFileLocation(String crashOutputString) {
Asserts.assertTrue(crashOutputString.contains(LOCATIONS_STRING),
"Output doesn't contain the location of core file.");
String stringWithLocation = Arrays.stream(crashOutputString.split("\\r?\\n"))
.filter(str -> str.contains(LOCATIONS_STRING))
.findFirst()
.get();
stringWithLocation = stringWithLocation.substring(stringWithLocation
.indexOf(LOCATIONS_STRING) + LOCATIONS_STRING.length());
System.out.println("getCoreFileLocation found stringWithLocation = " + stringWithLocation);
String coreWithPid;
if (stringWithLocation.contains("or ")) {
Matcher m = Pattern.compile("or.* ([^ ]+[^\\)])\\)?").matcher(stringWithLocation);
if (!m.find()) {
throw new Error("Couldn't find path to core inside location string");
}
coreWithPid = m.group(1);
} else {
coreWithPid = stringWithLocation.trim();
}
if (new File(coreWithPid).exists()) {
return coreWithPid;
}
String justCore = Paths.get("core").toString();
if (new File(justCore).exists()) {
return justCore;
}
Path coreWithPidPath = Paths.get(coreWithPid);
String justFile = coreWithPidPath.getFileName().toString();
if (new File(justFile).exists()) {
return justFile;
}
Path parent = coreWithPidPath.getParent();
if (parent != null) {
String coreWithoutPid = parent.resolve("core").toString();
if (new File(coreWithoutPid).exists()) {
return coreWithoutPid;
}
}
return null;
}

private static String[] getTestJvmCommandlineWithPrefix(String prefix, String... args) {
try {
String cmd = ProcessTools.getCommandLine(ProcessTools.createTestJvm(args));
return new String[]{"sh", "-c", prefix + cmd};
} catch (Throwable t) {
throw new Error("Can't create process builder: " + t, t);
}
}

private static void cleanup() {
remove(TEST_CDS_CORE_FILE_NAME);
if (coreFileName != null) remove(coreFileName);
remove(SHARED_ARCHIVE_NAME);
}

Expand Down
94 changes: 76 additions & 18 deletions test/hotspot/jtreg/serviceability/sa/ClhsdbFindPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,63 @@
import java.util.ArrayList;

import jdk.test.lib.apps.LingeredApp;
import jdk.test.lib.util.CoreUtils;
import jtreg.SkippedException;

/**
* @test
* @bug 8193124
* @summary Test the clhsdb 'findpc' command
* @summary Test the clhsdb 'findpc' command with Xcomp on live process
* @requires vm.hasSA
* @requires vm.compiler1.enabled
* @requires vm.opt.DeoptimizeALot != true
* @library /test/lib
* @run main/othervm/timeout=480 ClhsdbFindPC
* @run main/othervm/timeout=480 ClhsdbFindPC true false
*/

/**
* @test
* @bug 8193124
* @summary Test the clhsdb 'findpc' command with Xcomp on core file
* @requires vm.compMode != "Xcomp"
* @requires vm.hasSA
* @requires vm.compiler1.enabled
* @library /test/lib
* @run main/othervm/timeout=480 ClhsdbFindPC true true
*/

/**
* @test
* @bug 8193124
* @summary Test the clhsdb 'findpc' command w/o Xcomp on live process
* @requires vm.hasSA
* @requires vm.compiler1.enabled
* @requires vm.opt.DeoptimizeALot != true
* @library /test/lib
* @run main/othervm/timeout=480 ClhsdbFindPC false false
*/

/**
* @test
* @bug 8193124
* @summary Test the clhsdb 'findpc' command w/o Xcomp on core file
* @requires vm.compMode != "Xcomp"
* @requires vm.hasSA
* @requires vm.compiler1.enabled
* @library /test/lib
* @run main/othervm/timeout=480 ClhsdbFindPC false true
*/

public class ClhsdbFindPC {

private static void testFindPC(boolean withXcomp) throws Exception {
private static void testFindPC(boolean withXcomp, boolean withCore) throws Exception {
LingeredApp theApp = null;
String coreFileName = null;
try {
ClhsdbLauncher test = new ClhsdbLauncher();

theApp = new LingeredAppWithTrivialMain();
theApp.setForceCrash(withCore);
if (withXcomp) {
LingeredApp.startApp(List.of("-Xcomp"), theApp);
} else {
Expand All @@ -60,27 +97,41 @@ private static void testFindPC(boolean withXcomp) throws Exception {
}
System.out.println("with pid " + theApp.getPid());

// Run 'jstack -v' command to get the pc
List<String> cmds = List.of("jstack -v");
String output = test.run(theApp.getPid(), cmds, null, null);
// Get the core file name if we are debugging a core instead of live process
if (withCore) {
coreFileName = CoreUtils.getCoreFileLocation(theApp.getOutput().getStdout());
}

// Test the 'findpc' command passing in the pc obtained from
// the 'jstack -v' command
cmds = new ArrayList<String>();
// Run 'jstack -v' command to get the findpc address
List<String> cmds = List.of("jstack -v");
String output;
if (withCore) {
output = test.runOnCore(coreFileName, cmds, null, null);
} else {
output = test.run(theApp.getPid(), cmds, null, null);
}

String cmdStr = null;
// Extract pc address from the following line:
// - LingeredAppWithTrivialMain.main(java.lang.String[]) @bci=1, line=33, pc=0x00007ff18ff519f0, ...
String pcAddress = null;
String[] parts = output.split("LingeredAppWithTrivialMain.main");
String[] tokens = parts[1].split(" ");
for (String token : tokens) {
if (token.contains("pc")) {
String[] address = token.split("=");
// address[1] represents the address of the Method
cmdStr = "findpc " + address[1].replace(",","");
cmds.add(cmdStr);
String[] addresses = token.split("=");
// addresses[1] represents the address of the Method
pcAddress = addresses[1].replace(",","");
break;
}
}
if (pcAddress == null) {
throw new RuntimeException("Cannot find LingeredAppWithTrivialMain.main pc in output");
}

// Test the 'findpc' command passing in the pc obtained from above
cmds = new ArrayList<String>();
String cmdStr = "findpc " + pcAddress;
cmds.add(cmdStr);
Map<String, List<String>> expStrMap = new HashMap<>();
if (withXcomp) {
expStrMap.put(cmdStr, List.of(
Expand All @@ -93,20 +144,27 @@ private static void testFindPC(boolean withXcomp) throws Exception {
"In interpreter codelet"));
}

test.run(theApp.getPid(), cmds, expStrMap, null);
if (withCore) {
test.runOnCore(coreFileName, cmds, expStrMap, null);
} else {
test.run(theApp.getPid(), cmds, expStrMap, null);
}
} catch (SkippedException se) {
throw se;
} catch (Exception ex) {
throw new RuntimeException("Test ERROR " + ex, ex);
} finally {
LingeredApp.stopApp(theApp);
if (!withCore) {
LingeredApp.stopApp(theApp);
}
}
}

public static void main(String[] args) throws Exception {
boolean withXcomp = Boolean.parseBoolean(args[0]);
boolean withCore = Boolean.parseBoolean(args[1]);
System.out.println("Starting the ClhsdbFindPC test");
testFindPC(true);
testFindPC(false);
testFindPC(withXcomp, withCore);
System.out.println("Test PASSED");
}
}
Expand Down
Loading

1 comment on commit 6bcb8ac

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.