From c82a6061c987f3430e354edbdbadf74fc86f0efd Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Wed, 22 Sep 2021 11:35:43 +0300 Subject: [PATCH] Use relative path in DW_AT_comp_dir if possible dee66ca6e28fd519cef061130d20e28ec35d1a1f started enforcing the use of absolute paths in DW_AT_comp_dir which results in the following issue. After compilation is done and one wants to debug their binary on their deployment machine they will need to transfer both the binary and the sources directory. As a result any absolute paths in the dwarf info will now be invalid. However, if the sources directory are kept in the expected relative-to-the-binary path, a relative DW_AT_comp_dir will remain valid and gdb (as well as other tools) will be able to pick the sources up without any additional configuration (e.g. `directory path/to/sources/` in gdb. --- .../objectfile/debuginfo/DebugInfoProvider.java | 6 ++++-- .../com/oracle/svm/core/SubstrateOptions.java | 16 +++++++++++++++- .../image/NativeImageDebugInfoProvider.java | 4 ++-- .../svm/hosted/image/NativeImageViaCC.java | 2 +- .../svm/hosted/image/sources/SourceCache.java | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/debuginfo/DebugInfoProvider.java b/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/debuginfo/DebugInfoProvider.java index b7af9cc7f695..36ad53fb7126 100644 --- a/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/debuginfo/DebugInfoProvider.java +++ b/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/debuginfo/DebugInfoProvider.java @@ -82,8 +82,10 @@ interface DebugFileInfo { Path filePath(); /** - * @return a relative path to the source cache containing the cached source file of a file - * element or {@code null} if sources are not available. + * @return the path to the source cache containing the cached source file of a file element + * or {@code null} if sources are not available. Relative paths to the target + * directory are preferred over absolute paths, to make it easier to move the binary + * and the cache directory around. */ Path cachePath(); } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java index cd99fbebf10f..a6e7da9c6356 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java @@ -476,7 +476,13 @@ public static void defaultDebugInfoValueUpdateHandler(EconomicMap, @Option(help = "Directory under which to create source file cache for Application or GraalVM classes")// public static final HostedOptionKey DebugInfoSourceCacheRoot = new HostedOptionKey<>("sources"); - public static Path getDebugInfoSourceCacheRoot() { + /** + * Returns {@link SubstrateOptions#DebugInfoSourceCacheRoot} as an absolute path, by resolving + * it on {@link SubstrateOptions#Path} if it's not already an absolute path. + * + * @return the source cache root as an absolute path + */ + public static Path getDebugInfoSourceCacheRootAsAbsolutePath() { try { return Paths.get(Path.getValue()).resolve(DebugInfoSourceCacheRoot.getValue()); } catch (InvalidPathException ipe) { @@ -484,6 +490,14 @@ public static Path getDebugInfoSourceCacheRoot() { } } + public static Path getDebugInfoSourceCacheRoot() { + try { + return Paths.get(DebugInfoSourceCacheRoot.getValue()); + } catch (InvalidPathException ipe) { + throw UserError.abort("Invalid path provided for option DebugInfoSourceCacheRoot %s", DebugInfoSourceCacheRoot.getValue()); + } + } + @Option(help = "Omit generation of DebugLineInfo originating from inlined methods") // public static final HostedOptionKey OmitInlinedMethodDebugLineInfo = new HostedOptionKey<>(true); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoProvider.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoProvider.java index 4997385e88af..a661d9ac466e 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoProvider.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoProvider.java @@ -87,6 +87,7 @@ class NativeImageDebugInfoProvider implements DebugInfoProvider { private final DebugContext debugContext; private final NativeImageCodeCache codeCache; + private final Path cachePath; @SuppressWarnings("unused") private final NativeImageHeap heap; boolean useHeapBase; int compressShift; @@ -101,6 +102,7 @@ class NativeImageDebugInfoProvider implements DebugInfoProvider { super(); this.debugContext = debugContext; this.codeCache = codeCache; + this.cachePath = SubstrateOptions.getDebugInfoSourceCacheRoot(); this.heap = heap; ObjectHeader objectHeader = Heap.getHeap().getObjectHeader(); ObjectInfo primitiveFields = heap.getObjectInfo(StaticFieldsSupport.getStaticPrimitiveFields()); @@ -236,8 +238,6 @@ private static String toJavaName(JavaType javaType) { return javaType.toJavaName(); } - private final Path cachePath = SubstrateOptions.getDebugInfoSourceCacheRoot(); - private abstract class NativeImageDebugFileInfo implements DebugFileInfo { private Path fullFilePath; diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageViaCC.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageViaCC.java index daebe68b7de2..3d8d496f3d90 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageViaCC.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageViaCC.java @@ -461,7 +461,7 @@ public LinkerInvocation write(DebugContext debug, Path outputDirectory, Path tem } if (SubstrateOptions.GenerateDebugInfo.getValue() > 0) { - BuildArtifacts.singleton().add(ArtifactType.DEBUG_INFO, SubstrateOptions.getDebugInfoSourceCacheRoot()); + BuildArtifacts.singleton().add(ArtifactType.DEBUG_INFO, SubstrateOptions.getDebugInfoSourceCacheRootAsAbsolutePath()); if (OS.getCurrent() == OS.WINDOWS) { BuildArtifacts.singleton().add(ArtifactType.DEBUG_INFO, imagePath.resolveSibling(imageName + ".pdb")); } else { diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/sources/SourceCache.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/sources/SourceCache.java index 8fcbce1b5ca8..190529e11c37 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/sources/SourceCache.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/sources/SourceCache.java @@ -94,7 +94,7 @@ public class SourceCache { * Create the source cache. */ protected SourceCache() { - basePath = SubstrateOptions.getDebugInfoSourceCacheRoot(); + basePath = SubstrateOptions.getDebugInfoSourceCacheRootAsAbsolutePath(); srcRoots = new ArrayList<>(); specialSrcRoots = new HashMap<>(); addJDKSources();