Skip to content

Commit

Permalink
Introduce -H:StripDebugInfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Jan 3, 2023
1 parent 3859a1a commit f8a5beb
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ public static Path getDebugInfoSourceCacheRoot() {
}
}

@Option(help = "Strip debug info from the binary.")//
public static final HostedOptionKey<Boolean> StripDebugInfo = new HostedOptionKey<>(true);

@Option(help = "Omit generation of DebugLineInfo originating from inlined methods") //
public static final HostedOptionKey<Boolean> OmitInlinedMethodDebugLineInfo = new HostedOptionKey<>(true);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (c) 2022, 2022, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.hosted.image;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.debug.DebugContext.Builder;
import org.graalvm.compiler.debug.Indent;
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;

import com.oracle.graal.pointsto.util.GraalAccess;
import com.oracle.objectfile.ObjectFile;
import com.oracle.svm.core.BuildArtifacts;
import com.oracle.svm.core.BuildArtifacts.ArtifactType;
import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
import com.oracle.svm.core.feature.InternalFeature;
import com.oracle.svm.core.option.HostedOptionValues;
import com.oracle.svm.core.option.SubstrateOptionsParser;
import com.oracle.svm.core.util.InterruptImageBuilding;
import com.oracle.svm.core.util.UserError;
import com.oracle.svm.hosted.FeatureImpl.AfterImageWriteAccessImpl;
import com.oracle.svm.hosted.c.util.FileUtils;

@AutomaticallyRegisteredFeature
public class NativeImageDebugInfoStripFeature implements InternalFeature {

@Override
public boolean isInConfiguration(IsInConfigurationAccess access) {
return SubstrateOptions.GenerateDebugInfo.getValue() > 0 && SubstrateOptions.StripDebugInfo.getValue();
}

@SuppressWarnings("try")
@Override
public void afterImageWrite(AfterImageWriteAccess access) {
AfterImageWriteAccessImpl accessImpl = (AfterImageWriteAccessImpl) access;
DebugContext debugContext = new Builder(HostedOptionValues.singleton(), new GraalDebugHandlersFactory(GraalAccess.getOriginalSnippetReflection())).build();
try (Indent indent = debugContext.logAndIndent("Stripping debuginfo")) {
switch (ObjectFile.getNativeFormat()) {
case ELF:
stripLinux(accessImpl);
break;
case MACH_O:
case PECOFF:
if (SubstrateOptions.StripDebugInfo.hasBeenSet()) {
System.out.println("Warning: Using " + SubstrateOptionsParser.commandArgument(SubstrateOptions.StripDebugInfo, "+") + " not supported on macOS and Windows");
}
break;
default:
throw UserError.abort("Unsupported object file format");
}
}
}

private static void stripLinux(AfterImageWriteAccessImpl accessImpl) {
String objcopyExe = "objcopy";
String debugExtension = ".debug";
Path imagePath = accessImpl.getImagePath();
Path imageName = imagePath.getFileName();
Path outputDirectory = imagePath.getParent();
String debugInfoName = imageName + debugExtension;
boolean objcopyAvailable = false;
try {
objcopyAvailable = FileUtils.executeCommand(objcopyExe, "--version") == 0;
} catch (IOException e) {
/* Fall through to `if (!objcopyAvailable)` */
} catch (InterruptedException e) {
throw new InterruptImageBuilding("Interrupted during checking for " + objcopyExe + " availability");
}

if (!objcopyAvailable) {
System.out.printf("Warning: %s not available. Skip generation of debuginfo file %s%n", objcopyExe, debugInfoName);
} else {
try {
String imageFilePath = outputDirectory.resolve(imageName).toString();
Path debugInfoFilePath = outputDirectory.resolve(debugInfoName);
FileUtils.executeCommand(objcopyExe, "--only-keep-debug", imageFilePath, debugInfoFilePath.toString());
BuildArtifacts.singleton().add(ArtifactType.DEBUG_INFO, debugInfoFilePath);
Path exportedSymbolsPath = createKeepSymbolsListFile(accessImpl);
FileUtils.executeCommand(objcopyExe, "--strip-all", "--keep-symbols=" + exportedSymbolsPath, imageFilePath);
FileUtils.executeCommand(objcopyExe, "--add-gnu-debuglink=" + debugInfoFilePath, imageFilePath);
} catch (IOException e) {
throw UserError.abort("Generation of separate debuginfo file failed", e);
} catch (InterruptedException e) {
throw new InterruptImageBuilding("Interrupted during debuginfo file splitting of image " + imageName);
}
}
}

private static Path createKeepSymbolsListFile(AfterImageWriteAccessImpl accessImpl) throws IOException {
Path exportedSymbolsPath = accessImpl.getTempDirectory().resolve("keep-symbols.list").toAbsolutePath();
Files.write(exportedSymbolsPath, accessImpl.getImageSymbols(false));
return exportedSymbolsPath;
}
}

0 comments on commit f8a5beb

Please sign in to comment.