Skip to content
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

Debugging of native images built by GraalVM CE. #4124

Merged
merged 2 commits into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -158,7 +158,7 @@ static Map<String, NIVariable> retrieveVariables(CPPFrame frame, CPPVariable par
record = frame.thread.getDebugger().sendAndGet("-stack-list-variables --thread " + frame.thread.getId() + " --frame " + frame.level + " --no-frame-filters 2");
} else {
// from to
record = frame.thread.getDebugger().sendAndGet("-var-list-children --thread " + frame.thread.getId() + " --frame " + frame.level + " --all-values " + parentVar.getUniqueName());
record = frame.thread.getDebugger().sendAndGet("-var-list-children --thread " + frame.thread.getId() + " --frame " + frame.level + " --all-values " + "\"" + parentVar.getUniqueName() + "\"");
}
} catch (InterruptedException ex) {
return Collections.emptyMap();
Expand Down Expand Up @@ -257,7 +257,7 @@ public CompletableFuture<NIVariable> evaluateAsync(String expression, String res
result.complete(value);
return result;
}
thread.getDebugger().send(new Command("-var-create --thread " + thread.getId() + " --frame " + level + " - * " + expression) {
thread.getDebugger().send(new Command("-var-create --thread " + thread.getId() + " --frame " + level + " - * \"" + expression + "\"") {
@Override
protected void onDone(MIRecord record) {
MITList results = record.results();
Expand Down
Expand Up @@ -398,12 +398,12 @@ public String readMemory(String address, long offset, int length) {
MIRecord memory;
String offsetArg;
if (offset != 0) {
offsetArg = "-o " + offset + " ";
offsetArg = "-o " + offset + " \"";
} else {
offsetArg = "";
offsetArg = "\"";
}
try {
memory = sendAndGet("-data-read-memory-bytes " + offsetArg + address + " " + length);
memory = sendAndGet("-data-read-memory-bytes " + offsetArg + address + "\" " + length);
} catch (InterruptedException ex) {
return null;
}
Expand Down Expand Up @@ -434,7 +434,7 @@ public String getVersion() {
public List<Location> listLocations(String filePath) {
MIRecord lines;
try {
lines = sendAndGet("-symbol-list-lines " + filePath);
lines = sendAndGet("-symbol-list-lines \"" + filePath + "\"");
} catch (InterruptedException ex) {
return null;
}
Expand Down
Expand Up @@ -121,7 +121,7 @@ public NIVariable[] getChildren(int from, int to) {
public String getExpressionPath() {
MIRecord pathRecord;
try {
pathRecord = frame.getThread().getDebugger().sendAndGet("-var-info-path-expression " + uniqueName);
pathRecord = frame.getThread().getDebugger().sendAndGet("-var-info-path-expression \"" + uniqueName + "\"");
} catch (InterruptedException ex) {
return null;
}
Expand Down
@@ -0,0 +1,126 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.java.nativeimage.debugger.displayer;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.modules.nativeimage.api.debug.NIVariable;

import static org.netbeans.modules.java.nativeimage.debugger.displayer.JavaVariablesDisplayer.PRIVATE;
import static org.netbeans.modules.java.nativeimage.debugger.displayer.JavaVariablesDisplayer.PUBLIC;
import static org.netbeans.modules.java.nativeimage.debugger.displayer.Utils.findChild;
import static org.netbeans.modules.java.nativeimage.debugger.displayer.Utils.getVarsByName;

/**
* Hub of the native image object.
*/
final class DynamicHub {

private static final String HUB = "hub"; // NOI18N
private static final String HUB_TYPE = "hubType"; // NOI18N
private static final int HUB_TYPE_INSTANCE = 2; // Instances are less than or equal to 2
private static final int HUB_TYPE_ARRAY = 4; // Arrays are greater than or equal to 4
private static final String OBJ_HEADER = "_objhdr"; // NOI18N
private static final String NAME = "name"; // NOI18N

private final NIVariable hub;
private final Map<String, NIVariable> childrenByName;

enum HubType {

OBJECT,
ARRAY;

static HubType getFrom(int hubType) {
if (hubType <= HUB_TYPE_INSTANCE) {
return OBJECT;
}
if (hubType >= HUB_TYPE_ARRAY) {
return ARRAY;
}
return null;
}
}

private DynamicHub(NIVariable hub) {
this.hub = hub;
this.childrenByName = getVarsByName(hub.getChildren());
}

@CheckForNull
static DynamicHub find(NIVariable var) {
NIVariable object = findObjectType(var);
if (object == null) {
return null;
}
NIVariable[] children = object.getChildren();
NIVariable hub = findChild(children, OBJ_HEADER, PUBLIC, HUB, Class.class.getName(), PRIVATE);
if (hub != null) {
return new DynamicHub(hub);
} else {
return null;
}
}

@CheckForNull
private static NIVariable findObjectType(NIVariable var) {
NIVariable[] children = var.getChildren();
if (children.length < 1) {
return null;
}
if (children[0].getName().equals(Object.class.getName())) {
return children[0];
}
// Prevent from infinite recursion
Set<String> visitedNames = new HashSet<>();
var = children[0];
do {
children = var.getChildren();
if (children.length < 1) {
return null;
}
var = children[0];
if (var.getName().equals(Object.class.getName())) {
return var;
}
} while (visitedNames.add(var.getName()));
return null;
}

@CheckForNull
HubType getType() {
NIVariable hubTypeVar = childrenByName.get(HUB_TYPE);
if (hubTypeVar == null) {
return null;
}
try {
int hubType = Integer.parseInt(hubTypeVar.getValue());
return HubType.getFrom(hubType);
} catch (NumberFormatException ex) {
return null;
}
}

@CheckForNull
NIVariable findClassNameVar() {
return childrenByName.get(NAME);
}
}
Expand Up @@ -77,17 +77,30 @@ private static String getDisplayName(NIFrame frame) {
if (methodEnd < 0) {
methodEnd = functionName.length();
}
int methodStart = functionName.lastIndexOf('.', methodEnd);
if (methodStart < 0) {
clsMethod = functionName.substring(0, methodEnd);
} else {
int clsStart = functionName.lastIndexOf('.', methodStart - 1);
int methodStart = functionName.indexOf("::");
if (methodStart > 0) {
int clsStart = functionName.lastIndexOf('.', methodStart);
if (clsStart < 0) {
clsStart = 0;
} else {
clsStart++;
}
clsMethod = functionName.substring(clsStart, methodEnd);
String clazz = functionName.substring(clsStart, methodStart);
String method = functionName.substring(methodStart + 2, methodEnd);
clsMethod = clazz + '.' + method;
} else {
methodStart = functionName.lastIndexOf('.', methodEnd);
if (methodStart < 0) {
clsMethod = functionName.substring(0, methodEnd);
} else {
int clsStart = functionName.lastIndexOf('.', methodStart - 1);
if (clsStart < 0) {
clsStart = 0;
} else {
clsStart++;
}
clsMethod = functionName.substring(clsStart, methodEnd);
}
}
int line = frame.getLine();
if (line < 0) {
Expand All @@ -104,6 +117,7 @@ private static String getDescription(NIFrame frame) {
methodEnd = functionName.length();
}
String clsMethod = functionName.substring(0, methodEnd);
clsMethod = clsMethod.replace("::", ".");
int line = frame.getLine();
if (line < 0) {
return clsMethod;
Expand All @@ -119,7 +133,10 @@ private URI getSourceURI(NIFrame frame) {
methodEnd = functionName.length();
}
if (methodEnd > 0) {
int methodStart = functionName.lastIndexOf('.', methodEnd);
int methodStart = functionName.indexOf("::");
if (methodStart < 0) {
methodStart = functionName.lastIndexOf('.', methodEnd);
}
if (methodStart > 0) {
String className = functionName.substring(0, methodStart);
URI uri = findClassURI(sourcePath, className);
Expand Down