2828import java .nio .file .Files ;
2929import java .nio .file .Path ;
3030
31- import jdk .graal .compiler .debug .DebugContext ;
32- import jdk .graal .compiler .debug .DebugContext .Builder ;
33- import jdk .graal .compiler .debug .Indent ;
34- import jdk .graal .compiler .printer .GraalDebugHandlersFactory ;
31+ import org .graalvm .nativeimage .Platform ;
32+ import org .graalvm .nativeimage .impl .InternalPlatform ;
3533
3634import com .oracle .graal .pointsto .util .GraalAccess ;
3735import com .oracle .objectfile .ObjectFile ;
4745import com .oracle .svm .hosted .c .util .FileUtils ;
4846import com .oracle .svm .util .LogUtils ;
4947
48+ import jdk .graal .compiler .core .common .SuppressFBWarnings ;
49+ import jdk .graal .compiler .debug .DebugContext ;
50+ import jdk .graal .compiler .debug .DebugContext .Builder ;
51+ import jdk .graal .compiler .debug .Indent ;
52+ import jdk .graal .compiler .printer .GraalDebugHandlersFactory ;
53+
5054@ AutomaticallyRegisteredFeature
5155public class NativeImageDebugInfoStripFeature implements InternalFeature {
5256
5357 @ Override
5458 public boolean isInConfiguration (IsInConfigurationAccess access ) {
55- return SubstrateOptions .useDebugInfoGeneration () && SubstrateOptions . StripDebugInfo .getValue ();
59+ return SubstrateOptions .StripDebugInfo .getValue ();
5660 }
5761
5862 @ SuppressWarnings ("try" )
@@ -77,13 +81,17 @@ public void afterImageWrite(AfterImageWriteAccess access) {
7781 }
7882 }
7983
84+ @ SuppressFBWarnings (value = "" , justification = "FB reports null pointer dereferencing although it is not possible in this case." )
8085 private static void stripLinux (AfterImageWriteAccessImpl accessImpl ) {
8186 String objcopyExe = "objcopy" ;
8287 String debugExtension = ".debug" ;
8388 Path imagePath = accessImpl .getImagePath ();
89+ if (imagePath == null ) {
90+ assert !Platform .includedIn (InternalPlatform .NATIVE_ONLY .class );
91+ return ;
92+ }
93+
8494 Path imageName = imagePath .getFileName ();
85- Path outputDirectory = imagePath .getParent ();
86- String debugInfoName = imageName + debugExtension ;
8795 boolean objcopyAvailable = false ;
8896 try {
8997 objcopyAvailable = FileUtils .executeCommand (objcopyExe , "--version" ) == 0 ;
@@ -94,16 +102,21 @@ private static void stripLinux(AfterImageWriteAccessImpl accessImpl) {
94102 }
95103
96104 if (!objcopyAvailable ) {
97- LogUtils .warning ("%s not available. Skipping generation of separate debuginfo file %s, debuginfo will remain embedded in the executable." , objcopyExe , debugInfoName );
105+ LogUtils .warning ("%s not available. The debuginfo will remain embedded in the executable." , objcopyExe );
98106 } else {
99107 try {
108+ Path outputDirectory = imagePath .getParent ();
100109 String imageFilePath = outputDirectory .resolve (imageName ).toString ();
101- Path debugInfoFilePath = outputDirectory .resolve (debugInfoName );
102- FileUtils .executeCommand (objcopyExe , "--only-keep-debug" , imageFilePath , debugInfoFilePath .toString ());
103- BuildArtifacts .singleton ().add (ArtifactType .DEBUG_INFO , debugInfoFilePath );
110+ if (SubstrateOptions .useDebugInfoGeneration ()) {
111+ /* Generate a separate debug file before stripping the executable. */
112+ String debugInfoName = imageName + debugExtension ;
113+ Path debugInfoFilePath = outputDirectory .resolve (debugInfoName );
114+ FileUtils .executeCommand (objcopyExe , "--only-keep-debug" , imageFilePath , debugInfoFilePath .toString ());
115+ BuildArtifacts .singleton ().add (ArtifactType .DEBUG_INFO , debugInfoFilePath );
116+ FileUtils .executeCommand (objcopyExe , "--add-gnu-debuglink=" + debugInfoFilePath , imageFilePath );
117+ }
104118 Path exportedSymbolsPath = createKeepSymbolsListFile (accessImpl );
105119 FileUtils .executeCommand (objcopyExe , "--strip-all" , "--keep-symbols=" + exportedSymbolsPath , imageFilePath );
106- FileUtils .executeCommand (objcopyExe , "--add-gnu-debuglink=" + debugInfoFilePath , imageFilePath );
107120 } catch (IOException e ) {
108121 throw UserError .abort ("Generation of separate debuginfo file failed" , e );
109122 } catch (InterruptedException e ) {
0 commit comments