Skip to content

Commit

Permalink
Adapt Native Image debugging for CE images.
Browse files Browse the repository at this point in the history
  • Loading branch information
entlicher committed May 17, 2022
1 parent d299087 commit ac86e59
Show file tree
Hide file tree
Showing 4 changed files with 721 additions and 104 deletions.
@@ -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

0 comments on commit ac86e59

Please sign in to comment.