Skip to content

Commit f8659d0

Browse files
dominikmascherbauerolpaw
authored andcommitted
[GR-53359] Provide native debugging friendly classloadername based on ClassLoader.nameAndId
PullRequest: graal/17465
2 parents fb70f40 + 2c3e036 commit f8659d0

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

substratevm/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This changelog summarizes major changes to GraalVM Native Image.
2121
* (GR-52578) Print information about embedded resources into `embedded-resources.json` using the `-H:+GenerateEmbeddedResourcesFile` option.
2222
* (GR-51172) Add support to catch OutOfMemoryError exceptions on native image if there is no memory left.
2323
* (GR-43837) `--report-unsupported-elements-at-runtime` is now enabled by default and the option is deprecated.
24+
* (GR-53359) Provide the `.debug_gdb_scripts` section that triggers auto-loading of `svmhelpers.py` in GDB. Remove single and double quotes from `ClassLoader.nameAndId` in the debuginfo.
2425

2526
## GraalVM for JDK 22 (Internal Version 24.0.0)
2627
* (GR-48304) Red Hat added support for the JFR event ThreadAllocationStatistics.

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageBFDNameProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -90,9 +90,11 @@ public String uniqueShortLoaderName(ClassLoader loader) {
9090
String name = SubstrateUtil.classLoaderNameAndId(loader);
9191
// name will look like "org.foo.bar.FooBarClassLoader @1234"
9292
// trim it down to something more manageable
93+
// escaping quotes in the classlaoder name does not work in GDB
94+
// but the name is still unique without quotes
9395
name = SubstrateUtil.stripPackage(name);
9496
name = stripOuterClass(name);
95-
name = name.replace(" @", "_");
97+
name = name.replace(" @", "_").replace("'", "").replace("\"", "");
9698
return name;
9799
}
98100

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoFeature.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,9 +24,19 @@
2424
*/
2525
package com.oracle.svm.hosted.image;
2626

27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
2729
import java.util.List;
2830
import java.util.function.Function;
31+
import java.util.function.Supplier;
2932

33+
import com.oracle.svm.core.c.CGlobalData;
34+
import com.oracle.svm.core.c.CGlobalDataFactory;
35+
import com.oracle.svm.core.config.ConfigurationValues;
36+
import com.oracle.svm.core.heap.Heap;
37+
import com.oracle.svm.hosted.c.CGlobalDataFeature;
38+
import com.oracle.svm.util.ReflectionUtil;
39+
import jdk.graal.compiler.core.common.CompressEncoding;
3040
import jdk.graal.compiler.debug.DebugContext;
3141
import jdk.graal.compiler.printer.GraalDebugHandlersFactory;
3242
import com.oracle.svm.core.graal.meta.RuntimeConfiguration;
@@ -49,6 +59,8 @@
4959
import com.oracle.svm.hosted.ProgressReporter;
5060
import com.oracle.svm.hosted.image.sources.SourceManager;
5161
import com.oracle.svm.hosted.util.DiagnosticUtils;
62+
import org.graalvm.word.PointerBase;
63+
import org.graalvm.word.WordFactory;
5264

5365
@AutomaticallyRegisteredFeature
5466
@SuppressWarnings("unused")
@@ -97,6 +109,21 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
97109
var accessImpl = (FeatureImpl.BeforeAnalysisAccessImpl) access;
98110
bfdNameProvider.setNativeLibs(accessImpl.getNativeLibraries());
99111
}
112+
113+
/*
114+
* Ensure ClassLoader.nameAndId is available at runtime for type lookup from gdb
115+
*/
116+
access.registerAsAccessed(ReflectionUtil.lookupField(ClassLoader.class, "nameAndId"));
117+
118+
CompressEncoding compressEncoding = ImageSingletons.lookup(CompressEncoding.class);
119+
CGlobalData<PointerBase> compressedShift = CGlobalDataFactory.createWord(WordFactory.signed(compressEncoding.getShift()), "__svm_compressed_shift");
120+
CGlobalData<PointerBase> useHeapBase = CGlobalDataFactory.createWord(WordFactory.unsigned(compressEncoding.hasBase() ? 1 : 0), "__svm_use_heap_base");
121+
CGlobalData<PointerBase> oopTagsMask = CGlobalDataFactory.createWord(WordFactory.unsigned(Heap.getHeap().getObjectHeader().getReservedBitsMask()), "__svm_oop_tags_mask");
122+
CGlobalData<PointerBase> objectAlignment = CGlobalDataFactory.createWord(WordFactory.unsigned(ConfigurationValues.getObjectLayout().getAlignment()), "__svm_object_alignment");
123+
CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(compressedShift);
124+
CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(useHeapBase);
125+
CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(oopTagsMask);
126+
CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(objectAlignment);
100127
}
101128

102129
@Override
@@ -134,11 +161,29 @@ public boolean isLoadable() {
134161
};
135162
};
136163

164+
Supplier<BasicProgbitsSectionImpl> makeGDBSectionImpl = () -> {
165+
var content = AssemblyBuffer.createOutputAssembler(objectFile.getByteOrder());
166+
// 1 -> python file
167+
content.writeByte((byte) 1);
168+
content.writeString("./svmhelpers.py");
169+
return new BasicProgbitsSectionImpl(content.getBlob()) {
170+
@Override
171+
public boolean isLoadable() {
172+
return false;
173+
}
174+
};
175+
};
176+
137177
var imageClassLoader = accessImpl.getImageClassLoader();
138178
objectFile.newUserDefinedSection(".debug.svm.imagebuild.classpath", makeSectionImpl.apply(DiagnosticUtils.getClassPath(imageClassLoader)));
139179
objectFile.newUserDefinedSection(".debug.svm.imagebuild.modulepath", makeSectionImpl.apply(DiagnosticUtils.getModulePath(imageClassLoader)));
140180
objectFile.newUserDefinedSection(".debug.svm.imagebuild.arguments", makeSectionImpl.apply(DiagnosticUtils.getBuilderArguments(imageClassLoader)));
141181
objectFile.newUserDefinedSection(".debug.svm.imagebuild.java.properties", makeSectionImpl.apply(DiagnosticUtils.getBuilderProperties()));
182+
183+
Path svmDebugHelper = Path.of(System.getProperty("java.home"), "lib/svm/debug/svmhelpers.py");
184+
if (Files.exists(svmDebugHelper)) {
185+
objectFile.newUserDefinedSection(".debug_gdb_scripts", makeGDBSectionImpl.get());
186+
}
142187
}
143188
}
144189
ProgressReporter.singleton().setDebugInfoTimer(timer);

0 commit comments

Comments
 (0)