Skip to content

Commit

Permalink
Merge pull request #10093 from galderz/t_debug_symbols_boolean_v2_9792
Browse files Browse the repository at this point in the history
Add debug symbols support #9792
  • Loading branch information
gsmet authored Jul 1, 2020
2 parents 359fb8f + ee7b838 commit 63fe936
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ public class NativeConfig {
@ConfigItem
public Optional<String> nativeImageXmx;

/**
* If debug symbols should be included
*/
@ConfigItem
public boolean debugSymbols;

/**
* If the native image build should wait for a debugger to be attached before running. This is an advanced option
* and is generally only intended for those familiar with GraalVM internals
Expand Down Expand Up @@ -276,4 +270,20 @@ public static class ResourcesConfig {
public Optional<List<String>> includes;

}

/**
* Debugging options.
*/
@ConfigItem
public Debug debug;

@ConfigGroup
public static class Debug {
/**
* If debug is enabled and debug symbols are generated.
* The symbols will be generated in a separate .debug file.
*/
@ConfigItem
public boolean enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
if (nativeConfig.reportExceptionStackTraces) {
command.add("-H:+ReportExceptionStackTraces");
}
if (nativeConfig.debugSymbols) {
command.add("-g");
if (nativeConfig.debug.enabled) {
command.add("-H:GenerateDebugInfo=1");
}
if (nativeConfig.debugBuildProcess) {
command.add("-J-Xrunjdwp:transport=dt_socket,address=" + DEBUG_BUILD_PROCESS_PORT + ",server=y,suspend=y");
Expand Down Expand Up @@ -353,6 +353,14 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
Files.delete(generatedImage);
System.setProperty("native.image.path", finalPath.toAbsolutePath().toString());

if (objcopyExists(env)) {
if (nativeConfig.debug.enabled) {
splitDebugSymbols(finalPath);
}
// Strip debug symbols regardless, because the underlying JDK might contain them
objcopy("--strip-debug", finalPath.toString());
}

return new NativeImageBuildItem(finalPath);
} catch (Exception e) {
throw new RuntimeException("Failed to build native image", e);
Expand Down Expand Up @@ -522,4 +530,47 @@ private static String testGCCArgument(String argument) {
return "";
}

private boolean objcopyExists(Map<String, String> env) {
// System path
String systemPath = env.get(PATH);
if (systemPath != null) {
String[] pathDirs = systemPath.split(File.pathSeparator);
for (String pathDir : pathDirs) {
File dir = new File(pathDir);
if (dir.isDirectory()) {
File file = new File(dir, "objcopy");
if (file.exists()) {
return true;
}
}
}
}

log.info("Cannot find executable (objcopy) to separate symbols from executable.");
return false;
}

private void splitDebugSymbols(Path executable) {
Path symbols = Paths.get(String.format("%s.debug", executable.toString()));
objcopy("--only-keep-debug", executable.toString(), symbols.toString());
objcopy(String.format("--add-gnu-debuglink=%s", symbols.toString()), executable.toString());
}

private static void objcopy(String... args) {
final List<String> command = new ArrayList<>(args.length + 1);
command.add("objcopy");
command.addAll(Arrays.asList(args));
log.infof("Execute %s", command);
Process process = null;
try {
process = new ProcessBuilder(command).start();
process.waitFor();
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Unable to invoke objcopy", e);
} finally {
if (process != null) {
process.destroy();
}
}
}
}

0 comments on commit 63fe936

Please sign in to comment.