Skip to content

Commit

Permalink
No not use Graal logging for native tool tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
olpaw committed Sep 9, 2020
1 parent e62a14a commit d739234
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ public Boolean getValue(OptionValues values) {
@Option(help = "Comma-separated list of fully qualified class names that object instantiation is traced for.")//
public static final HostedOptionKey<String> TraceObjectInstantiation = new HostedOptionKey<>("");

@Option(help = "Trace all native tool invocations as part of image building", type = User)//
static final HostedOptionKey<Boolean> TraceNativeToolUsage = new HostedOptionKey<>(false);

public static boolean traceNativeToolUsage() {
return TraceNativeToolUsage.getValue();
}

@Option(help = "Prefix that is added to the names of entry point methods.")//
public static final HostedOptionKey<String> EntryPointNamePrefix = new HostedOptionKey<>("");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@
import java.util.concurrent.ForkJoinPool;
import java.util.function.Consumer;

import org.graalvm.collections.EconomicMap;
import org.graalvm.collections.Pair;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.debug.DebugContext.Builder;
import org.graalvm.compiler.debug.DebugOptions;
import org.graalvm.compiler.options.OptionKey;
import org.graalvm.compiler.options.OptionValues;
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
Expand All @@ -62,7 +59,6 @@
import com.oracle.svm.core.JavaMainWrapper.JavaMainSupport;
import com.oracle.svm.core.OS;
import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.option.OptionUtils;
import com.oracle.svm.core.option.SubstrateOptionsParser;
import com.oracle.svm.core.util.InterruptImageBuilding;
import com.oracle.svm.core.util.UserError;
Expand Down Expand Up @@ -245,21 +241,11 @@ private int buildImage(String[] arguments, ImageClassLoader classLoader) {
throw UserError.abort("Unknown options: " + Arrays.toString(remainingArgs));
}

/* Add NativeImageOptions.SubstrateLog values to DebugOptions.Log */
EconomicMap<OptionKey<?>, Object> hostedValues = optionParser.getHostedValues();
String[] substrateLogValues = (String[]) hostedValues.get(NativeImageOptions.SubstrateLog);
if (substrateLogValues != null) {
String[] graalLogValues = {(String) hostedValues.get(DebugOptions.Log, null)};
List<String> logValues = new ArrayList<>(OptionUtils.flatten(",", graalLogValues));
logValues.addAll(Arrays.asList(substrateLogValues));
hostedValues.put(DebugOptions.Log, String.join(",", logValues));
}

/*
* We do not have the VMConfiguration and the HostedOptionValues set up yet, so we need
* to pass the OptionValues explicitly when accessing options.
*/
parsedHostedOptions = new OptionValues(hostedValues);
parsedHostedOptions = new OptionValues(optionParser.getHostedValues());
DebugContext debug = new Builder(parsedHostedOptions, new GraalDebugHandlersFactory(GraalAccess.getOriginalSnippetReflection())).build();

String imageName = SubstrateOptions.Name.getValue(parsedHostedOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.graalvm.collections.EconomicMap;
import org.graalvm.compiler.options.Option;
import org.graalvm.compiler.options.OptionKey;
import org.graalvm.compiler.options.OptionType;
import org.graalvm.compiler.options.OptionValues;
import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess;

Expand Down Expand Up @@ -109,10 +108,6 @@ protected void onValueUpdate(EconomicMap<OptionKey<?>, Object> values, Boolean o
}
};

@APIOption(name = "native-compiler-logging", fixedValue = "InvokeCC:3", customHelp = "Log C compiler invocations and the invocation outputs")//
@Option(help = "Accumulative version of org.graalvm.compiler.debug.DebugOptions.Log", type = OptionType.Debug)//
public static final HostedOptionKey<String[]> SubstrateLog = new HostedOptionKey<>(null);

@SuppressWarnings("all")
private static boolean areAssertionsEnabled() {
boolean assertsEnabled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,11 @@ public void compileAndParseError(boolean strict, List<String> compileOptions, Pa
.command(createCompilerCommand(options, target.normalize(), source.normalize()))
.directory(tempDirectory.toFile());
Process compilingProcess = null;
try (DebugContext.Scope s = debug.scope("InvokeCC")) {
debug.log("Using CompilerCommand: %s", SubstrateUtil.getShellCommandString(pb.command(), false));
try {
if (SubstrateOptions.traceNativeToolUsage()) {
System.out.printf(">> %s%n", SubstrateUtil.getShellCommandString(pb.command(), false));
}

compilingProcess = pb.start();

List<String> lines;
Expand All @@ -427,16 +430,15 @@ public void compileAndParseError(boolean strict, List<String> compileOptions, Pa
}
boolean errorReported = false;
for (String line : lines) {
if (debug.isLogEnabled(DebugContext.VERBOSE_LEVEL)) {
debug.log(DebugContext.VERBOSE_LEVEL, "%s", line);
errorReported = true;
} else {
if (detectError(line)) {
if (handler != null) {
handler.handle(pb, source, line);
}
errorReported = true;
if (SubstrateOptions.traceNativeToolUsage()) {
System.out.printf("># %s%n", line);
}

if (detectError(line)) {
if (handler != null) {
handler.handle(pb, source, line);
}
errorReported = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,32 +405,36 @@ public LinkerInvocation write(DebugContext debug, Path outputDirectory, Path tem
for (Function<LinkerInvocation, LinkerInvocation> fn : config.getLinkerInvocationTransformers()) {
inv = fn.apply(inv);
}
try (DebugContext.Scope s = debug.scope("InvokeCC")) {
List<String> cmd = inv.getCommand();
String commandLine = SubstrateUtil.getShellCommandString(cmd, false);
debug.log("Using CompilerCommand: %s", commandLine);

ProcessBuilder pb = new ProcessBuilder().command(cmd);
pb.directory(inv.getTempDirectory().toFile());
pb.redirectErrorStream(true);
int status;
ByteArrayOutputStream output;
try {
Process p = pb.start();
List<String> cmd = inv.getCommand();
String commandLine = SubstrateUtil.getShellCommandString(cmd, false);

output = new ByteArrayOutputStream();
FileUtils.drainInputStream(p.getInputStream(), output);
status = p.waitFor();
} catch (IOException | InterruptedException e) {
throw handleLinkerFailure(e.toString(), commandLine, null);
}
if (SubstrateOptions.traceNativeToolUsage()) {
System.out.printf(">> %s%n", commandLine);
}

debug.log(DebugContext.VERBOSE_LEVEL, "%s", output);
ProcessBuilder pb = new ProcessBuilder().command(cmd);
pb.directory(inv.getTempDirectory().toFile());
pb.redirectErrorStream(true);
int status;
ByteArrayOutputStream output;
try {
Process p = pb.start();
output = new ByteArrayOutputStream();
FileUtils.drainInputStream(p.getInputStream(), output);
status = p.waitFor();
} catch (IOException | InterruptedException e) {
throw handleLinkerFailure(e.toString(), commandLine, null);
}

if (status != 0) {
throw handleLinkerFailure("Linker command exited with " + status, commandLine, output.toString());
if (SubstrateOptions.traceNativeToolUsage()) {
for (String line : SubstrateUtil.split(output.toString(), "\n")) {
System.out.printf("># %s%n", line);
}
}

if (status != 0) {
throw handleLinkerFailure("Linker command exited with " + status, commandLine, output.toString());
}
return inv;
}
}
Expand Down

0 comments on commit d739234

Please sign in to comment.