Skip to content

Commit 5c8989e

Browse files
committed
GH-537 improve lookup of contextImpl, fallback to defult when GraalVMLocator is not present
1 parent 4a842b8 commit 5c8989e

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

visualvm/sampler.truffle/libsrc/org/graalvm/visualvm/sampler/truffle/stagent/Truffle.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ private Method getFindActiveEngines() {
151151
try {
152152
Method m = Engine.class.getDeclaredMethod("findActiveEngines");
153153
m.setAccessible(true);
154+
if (TruffleJMX.DEBUG) System.out.println("Engine "+m.getDeclaringClass()+" cl "+m.getDeclaringClass().getClassLoader());
154155
return m;
155156
} catch (SecurityException ex) {
156157
Logger.getLogger(Truffle.class.getName()).log(Level.SEVERE, null, ex);

visualvm/sampler.truffle/libsrc/org/graalvm/visualvm/sampler/truffle/stagent/TruffleClassLoader.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TruffleClassLoader extends ClassLoader {
4343

4444
private Collection<ClassLoader> loaders;
4545

46-
TruffleClassLoader(ClassLoader parent, Unsafe unsafe) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
46+
TruffleClassLoader(ClassLoader parent, Unsafe unsafe) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
4747
super(parent);
4848
loaders = getTruffleLocatorLoaders(parent);
4949
if (loaders == null) {
@@ -54,6 +54,9 @@ class TruffleClassLoader extends ClassLoader {
5454

5555
@Override
5656
public Class<?> findClass(String name) throws ClassNotFoundException {
57+
if (loaders == null) {
58+
return super.findClass(name);
59+
}
5760
for (ClassLoader loader : loaders) {
5861
if (loader == null) {
5962
continue;
@@ -75,9 +78,9 @@ private static Class getClass(ClassLoader cl, String className) throws ClassNotF
7578
}
7679
}
7780

78-
private static Collection<ClassLoader> getTruffleLocatorLoaders(ClassLoader cl) throws ClassNotFoundException {
79-
Class LocatorClass = getClass(cl, TRUFFLE_LOCATOR_CLASS_NAME);
81+
private static Collection<ClassLoader> getTruffleLocatorLoaders(ClassLoader cl) {
8082
try {
83+
Class LocatorClass = getClass(cl, TRUFFLE_LOCATOR_CLASS_NAME);
8184
return (Collection<ClassLoader>) LocatorClass.getMethod("loaders", (Class[])null).invoke(null, (Object[])null);
8285
} catch (Exception ex) {
8386
if (TruffleJMX.DEBUG) {
@@ -87,9 +90,9 @@ private static Collection<ClassLoader> getTruffleLocatorLoaders(ClassLoader cl)
8790
return null;
8891
}
8992

90-
private static Collection<ClassLoader> getGraalVMLocatorLoaders(ClassLoader cl, Unsafe unsafe) throws ClassNotFoundException {
91-
Class LocatorClass = getClass(cl, GRAALVM_LOCATOR_CLASS_NAME);
93+
private static Collection<ClassLoader> getGraalVMLocatorLoaders(ClassLoader cl, Unsafe unsafe) {
9294
try {
95+
Class LocatorClass = getClass(cl, GRAALVM_LOCATOR_CLASS_NAME);
9396
Field f = LocatorClass.getDeclaredField("loader");
9497
Object base = unsafe.staticFieldBase(f);
9598
ClassLoader loader = (ClassLoader) unsafe.getObject(base, unsafe.staticFieldOffset(f));

visualvm/sampler.truffle/libsrc/org/graalvm/visualvm/sampler/truffle/stagent/TruffleJMX.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.net.URL;
3737
import java.net.URLClassLoader;
3838
import java.util.Arrays;
39+
import java.util.HashMap;
40+
import java.util.Map;
3941
import java.util.logging.Level;
4042
import java.util.logging.Logger;
4143
import javax.management.InstanceAlreadyExistsException;
@@ -104,8 +106,8 @@ private static Object getContext() throws ClassNotFoundException, NoSuchMethodEx
104106
}
105107

106108
private static Object getContextImpl(Object context) throws IllegalArgumentException, SecurityException, NoSuchFieldException, IllegalAccessException {
107-
// return context.impl or context.dispatch
108-
Field implField = getDeclaredField(context, "impl", "dispatch");
109+
// return context.impl or context.receiver or context.dispatch
110+
Field implField = getDeclaredField(context, "impl", "receiver", "dispatch");
109111
try {
110112
implField.setAccessible(true);
111113
Object impl = implField.get(context);
@@ -121,12 +123,11 @@ private static Object getContextImpl(Object context) throws IllegalArgumentExcep
121123
}
122124

123125
static Field getDeclaredField(Object obj, String... names) throws NoSuchFieldException {
124-
for (Field f : obj.getClass().getDeclaredFields()) {
125-
for (String name : names) {
126-
if (name.equals(f.getName())) {
127-
return f;
128-
}
129-
}
126+
Map<String,Field> fields = new HashMap<>();
127+
for (Field f : obj.getClass().getDeclaredFields()) fields.put(f.getName(), f);
128+
for (String name : names) {
129+
Field f = fields.get(name);
130+
if (f!=null) return f;
130131
}
131132
throw new NoSuchFieldException(Arrays.toString(names));
132133
}

0 commit comments

Comments
 (0)