Skip to content

Showcase how to dump locals info via debuginfo #4227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@
import java.util.List;
import java.util.Map;

import com.oracle.objectfile.debuginfo.DebugInfoProvider.DebugFileInfo;
import jdk.vm.ci.meta.ResolvedJavaType;
import org.graalvm.compiler.debug.DebugContext;

import com.oracle.objectfile.debuginfo.DebugInfoProvider;
import com.oracle.objectfile.debuginfo.DebugInfoProvider.DebugFileInfo;
import com.oracle.objectfile.debuginfo.DebugInfoProvider.DebugTypeInfo.DebugTypeKind;
import com.oracle.objectfile.elf.dwarf.DwarfDebugInfo;

import jdk.vm.ci.meta.ResolvedJavaType;

/**
* An abstract class which indexes the information presented by the DebugInfoProvider in an
* organization suitable for use by subclasses targeting a specific binary format.
Expand Down Expand Up @@ -234,6 +235,9 @@ public void installDebugInfo(DebugInfoProvider debugInfoProvider) {
}));

debugInfoProvider.codeInfoProvider().forEach(debugCodeInfo -> debugCodeInfo.debugContext((debugContext) -> {

debugCodeInfo.buildFrameTree();

/*
* Primary file name and full method name need to be written to the debug_str section.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
import java.util.function.Consumer;
import java.util.stream.Stream;

import jdk.vm.ci.meta.ResolvedJavaType;
import org.graalvm.compiler.debug.DebugContext;

import jdk.vm.ci.meta.ResolvedJavaType;

/**
* Interfaces used to allow a native image to communicate details of types, code and data to the
* underlying object file so that the latter can insert appropriate debug info.
Expand Down Expand Up @@ -268,6 +269,8 @@ interface DebugCodeInfo extends DebugRangeInfo {
* to an empty frame
*/
List<DebugFrameSizeChange> getFrameSizeChanges();

void buildFrameTree();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@
package com.oracle.svm.core.code;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

import jdk.vm.ci.meta.ResolvedJavaMethod;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.code.SourceMapping;
import org.graalvm.compiler.debug.DebugContext;

import com.oracle.svm.core.util.VMError;

import jdk.vm.ci.code.BytecodeFrame;
import jdk.vm.ci.code.BytecodePosition;
import jdk.vm.ci.code.site.Call;
import jdk.vm.ci.code.site.Infopoint;
import jdk.vm.ci.code.site.InfopointReason;
import jdk.vm.ci.common.JVMCIError;
import jdk.vm.ci.meta.JavaValue;
import jdk.vm.ci.meta.Local;
import jdk.vm.ci.meta.LocalVariableTable;

public final class CompilationResultFrameTree {

Expand Down Expand Up @@ -278,6 +284,48 @@ public String toString() {
return String.format("-%s, span %d, bci %d, method %s", getPosStr(), getSpan(), frame.getBCI(), frame.getMethod().format("%h.%n(%p)"));
}

public String getLocalsStr() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adinn this is a hopefully more correct example code how to get locals info for the CompilationResultFrameTree

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olpaw Thanks. I'll work that into something that communicates the frame tree content to the debug info generation code.

String localsInfo = "";
if (frame instanceof BytecodeFrame) {
BytecodeFrame bcf = (BytecodeFrame) frame;
Local[] locals = getLocalsBySlot(frame.getMethod());
StringBuilder sb = new StringBuilder();
if (locals == null) {
return localsInfo;
}
int combinedLength = Integer.min(locals.length, bcf.numLocals);
for (int i = 0; i < combinedLength; i++) {
JavaValue value = bcf.getLocalValue(i);
if (i > 0) {
sb.append(", ");
}
sb.append("li(");
Local local = locals != null ? locals[i] : null;
if (local != null) {
sb.append(local.getName());
sb.append("=");
}
sb.append(value);
sb.append(")");
}
localsInfo = sb.toString();
}
return localsInfo;
}

private Local[] getLocalsBySlot(ResolvedJavaMethod method) {
LocalVariableTable lvt = method.getLocalVariableTable();
Local[] nonEmptySortedLocals = null;
if (lvt != null) {
Local[] locals = lvt.getLocals();
if (locals != null && locals.length > 0) {
nonEmptySortedLocals = Arrays.copyOf(locals, locals.length);
Arrays.sort(nonEmptySortedLocals, (Local l1, Local l2) -> l1.getSlot() - l2.getSlot());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrays.sort(nonEmptySortedLocals, Comparator.comparingInt(Local::getSlot)); is clearly more elegant.

}
}
return nonEmptySortedLocals;
}

public void visit(Visitor visitor, Object... varargs) {
visitor.apply(this, varargs);
}
Expand Down Expand Up @@ -678,12 +726,14 @@ private static final class FrameTreeDumper implements Visitor {
private final Consumer<String> printer;
private final boolean onlyCallTree;
private final boolean showSourcePos;
private final boolean showLocals;
private final int maxDepth;

FrameTreeDumper(Consumer<String> printer, boolean onlyCallTree, boolean showSourcePos, int maxDepth) {
this.printer = printer;
this.onlyCallTree = onlyCallTree;
this.showSourcePos = showSourcePos;
this.showLocals = true;
this.maxDepth = maxDepth;
}

Expand All @@ -710,6 +760,10 @@ public void apply(FrameNode node, Object... args) {
StackTraceElement stackTraceElement = node.getStackTraceElement();
printer.accept(" at " + stackTraceElement.getFileName() + ":" + stackTraceElement.getLineNumber());
}
if (showLocals) {
printer.accept(" locals: ");
printer.accept(node.getLocalsStr());
}
printer.accept("\n");
node.visitChildren(this, level + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public boolean isInitialized(AnalysisType type) {

@Override
public GraphBuilderConfiguration updateGraphBuilderConfiguration(GraphBuilderConfiguration config, AnalysisMethod method) {
return config.withRetainLocalVariables(retainLocalVariables());
return config.withRetainLocalVariables(retainLocalVariables()).withFullInfopoints(SubstrateOptions.GenerateDebugInfo.getValue() > 0);
}

private boolean retainLocalVariables() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.function.Consumer;
import java.util.stream.Stream;

import jdk.vm.ci.meta.JavaType;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.code.SourceMapping;
import org.graalvm.compiler.core.common.CompressEncoding;
Expand All @@ -50,6 +49,7 @@
import com.oracle.objectfile.debuginfo.DebugInfoProvider;
import com.oracle.svm.core.StaticFieldsSupport;
import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.code.CompilationResultFrameTree;
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.config.ObjectLayout;
import com.oracle.svm.core.graal.code.SubstrateBackend;
Expand All @@ -75,6 +75,7 @@
import com.oracle.svm.hosted.substitute.SubstitutionType;

import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.LineNumberTable;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaMethod;
Expand Down Expand Up @@ -853,6 +854,12 @@ public void debugContext(Consumer<DebugContext> action) {
}
}

@Override
public void buildFrameTree() {
CompilationResultFrameTree.CallNode root = new CompilationResultFrameTree.Builder(debugContext, compilation.getTargetCodeSize(), true).build(compilation);
CompilationResultFrameTree.dump(root, System.out::print, false, false, 0);
}

@Override
public ResolvedJavaType ownerType() {
return getDeclaringClass(hostedMethod, true);
Expand Down