diff --git a/substratevm/CHANGELOG.md b/substratevm/CHANGELOG.md index 9e94b2597f85..4966ef7a6dba 100644 --- a/substratevm/CHANGELOG.md +++ b/substratevm/CHANGELOG.md @@ -21,6 +21,7 @@ This changelog summarizes major changes to GraalVM Native Image. * (GR-52578) Print information about embedded resources into `embedded-resources.json` using the `-H:+GenerateEmbeddedResourcesFile` option. * (GR-51172) Add support to catch OutOfMemoryError exceptions on native image if there is no memory left. * (GR-43837) `--report-unsupported-elements-at-runtime` is now enabled by default and the option is deprecated. +* (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. ## GraalVM for JDK 22 (Internal Version 24.0.0) * (GR-48304) Red Hat added support for the JFR event ThreadAllocationStatistics. diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageBFDNameProvider.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageBFDNameProvider.java index 39bcc2b5438a..8b6b69841020 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageBFDNameProvider.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageBFDNameProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -90,9 +90,11 @@ public String uniqueShortLoaderName(ClassLoader loader) { String name = SubstrateUtil.classLoaderNameAndId(loader); // name will look like "org.foo.bar.FooBarClassLoader @1234" // trim it down to something more manageable + // escaping quotes in the classlaoder name does not work in GDB + // but the name is still unique without quotes name = SubstrateUtil.stripPackage(name); name = stripOuterClass(name); - name = name.replace(" @", "_"); + name = name.replace(" @", "_").replace("'", "").replace("\"", ""); return name; } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoFeature.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoFeature.java index 40651fa48129..1d71e5cf98f8 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoFeature.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoFeature.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,9 +24,19 @@ */ package com.oracle.svm.hosted.image; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.function.Function; +import java.util.function.Supplier; +import com.oracle.svm.core.c.CGlobalData; +import com.oracle.svm.core.c.CGlobalDataFactory; +import com.oracle.svm.core.config.ConfigurationValues; +import com.oracle.svm.core.heap.Heap; +import com.oracle.svm.hosted.c.CGlobalDataFeature; +import com.oracle.svm.util.ReflectionUtil; +import jdk.graal.compiler.core.common.CompressEncoding; import jdk.graal.compiler.debug.DebugContext; import jdk.graal.compiler.printer.GraalDebugHandlersFactory; import com.oracle.svm.core.graal.meta.RuntimeConfiguration; @@ -49,6 +59,8 @@ import com.oracle.svm.hosted.ProgressReporter; import com.oracle.svm.hosted.image.sources.SourceManager; import com.oracle.svm.hosted.util.DiagnosticUtils; +import org.graalvm.word.PointerBase; +import org.graalvm.word.WordFactory; @AutomaticallyRegisteredFeature @SuppressWarnings("unused") @@ -97,6 +109,21 @@ public void beforeAnalysis(BeforeAnalysisAccess access) { var accessImpl = (FeatureImpl.BeforeAnalysisAccessImpl) access; bfdNameProvider.setNativeLibs(accessImpl.getNativeLibraries()); } + + /* + * Ensure ClassLoader.nameAndId is available at runtime for type lookup from gdb + */ + access.registerAsAccessed(ReflectionUtil.lookupField(ClassLoader.class, "nameAndId")); + + CompressEncoding compressEncoding = ImageSingletons.lookup(CompressEncoding.class); + CGlobalData compressedShift = CGlobalDataFactory.createWord(WordFactory.signed(compressEncoding.getShift()), "__svm_compressed_shift"); + CGlobalData useHeapBase = CGlobalDataFactory.createWord(WordFactory.unsigned(compressEncoding.hasBase() ? 1 : 0), "__svm_use_heap_base"); + CGlobalData oopTagsMask = CGlobalDataFactory.createWord(WordFactory.unsigned(Heap.getHeap().getObjectHeader().getReservedBitsMask()), "__svm_oop_tags_mask"); + CGlobalData objectAlignment = CGlobalDataFactory.createWord(WordFactory.unsigned(ConfigurationValues.getObjectLayout().getAlignment()), "__svm_object_alignment"); + CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(compressedShift); + CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(useHeapBase); + CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(oopTagsMask); + CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(objectAlignment); } @Override @@ -134,11 +161,29 @@ public boolean isLoadable() { }; }; + Supplier makeGDBSectionImpl = () -> { + var content = AssemblyBuffer.createOutputAssembler(objectFile.getByteOrder()); + // 1 -> python file + content.writeByte((byte) 1); + content.writeString("./svmhelpers.py"); + return new BasicProgbitsSectionImpl(content.getBlob()) { + @Override + public boolean isLoadable() { + return false; + } + }; + }; + var imageClassLoader = accessImpl.getImageClassLoader(); objectFile.newUserDefinedSection(".debug.svm.imagebuild.classpath", makeSectionImpl.apply(DiagnosticUtils.getClassPath(imageClassLoader))); objectFile.newUserDefinedSection(".debug.svm.imagebuild.modulepath", makeSectionImpl.apply(DiagnosticUtils.getModulePath(imageClassLoader))); objectFile.newUserDefinedSection(".debug.svm.imagebuild.arguments", makeSectionImpl.apply(DiagnosticUtils.getBuilderArguments(imageClassLoader))); objectFile.newUserDefinedSection(".debug.svm.imagebuild.java.properties", makeSectionImpl.apply(DiagnosticUtils.getBuilderProperties())); + + Path svmDebugHelper = Path.of(System.getProperty("java.home"), "lib/svm/debug/svmhelpers.py"); + if (Files.exists(svmDebugHelper)) { + objectFile.newUserDefinedSection(".debug_gdb_scripts", makeGDBSectionImpl.get()); + } } } ProgressReporter.singleton().setDebugInfoTimer(timer);