Skip to content

Commit c158413

Browse files
committed
8261098: Add clhsdb "findsym" command
Reviewed-by: amenkov, sspitsyn
1 parent 0c31d5b commit c158413

File tree

4 files changed

+83
-8
lines changed

4 files changed

+83
-8
lines changed

src/jdk.hotspot.agent/linux/native/libsaproc/LinuxDebuggerLocal.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class AutoJavaString {
6868
public:
6969
// check env->ExceptionOccurred() after ctor
7070
AutoJavaString(JNIEnv* env, jstring str)
71-
: m_env(env), m_str(str), m_buf(env->GetStringUTFChars(str, NULL)) {
71+
: m_env(env), m_str(str), m_buf(str == NULL ? NULL : env->GetStringUTFChars(str, NULL)) {
7272
}
7373

7474
~AutoJavaString() {
@@ -350,12 +350,14 @@ JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_l
350350
jlong addr;
351351
jboolean isCopy;
352352
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
353+
// Note, objectName is ignored, and may in fact be NULL.
354+
// lookup_symbol will always search all objects/libs
353355
AutoJavaString objectName_cstr(env, objectName);
354356
CHECK_EXCEPTION_(0);
355357
AutoJavaString symbolName_cstr(env, symbolName);
356358
CHECK_EXCEPTION_(0);
357359

358-
addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
360+
addr = (jlong) lookup_symbol(ph, NULL, symbolName_cstr);
359361
return addr;
360362
}
361363

src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/CommandProcessor.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,6 +47,8 @@
4747
import sun.jvm.hotspot.code.CodeBlob;
4848
import sun.jvm.hotspot.code.CodeCacheVisitor;
4949
import sun.jvm.hotspot.code.NMethod;
50+
import sun.jvm.hotspot.debugger.cdbg.CDebugger;
51+
import sun.jvm.hotspot.debugger.cdbg.LoadObject;
5052
import sun.jvm.hotspot.debugger.Address;
5153
import sun.jvm.hotspot.debugger.OopHandle;
5254
import sun.jvm.hotspot.classfile.ClassLoaderDataGraph;
@@ -607,6 +609,40 @@ public void doit(Tokens t) {
607609
}
608610
}
609611
},
612+
new Command("findsym", "findsym name", false) {
613+
public void doit(Tokens t) {
614+
if (t.countTokens() != 1) {
615+
usage();
616+
} else {
617+
String symbol = t.nextToken();
618+
Address addr = VM.getVM().getDebugger().lookup(null, symbol);
619+
if (addr == null && VM.getVM().getDebugger().getOS().equals("win32")) {
620+
// On win32 symbols are prefixed with the dll name. Do the user
621+
// a favor and see if this is a symbol in jvm.dll or java.dll.
622+
addr = VM.getVM().getDebugger().lookup(null, "jvm!" + symbol);
623+
if (addr == null) {
624+
addr = VM.getVM().getDebugger().lookup(null, "java!" + symbol);
625+
}
626+
}
627+
if (addr == null) {
628+
out.println("Symbol not found");
629+
return;
630+
}
631+
out.print(addr); // Print the address of the symbol.
632+
CDebugger cdbg = VM.getVM().getDebugger().getCDebugger();
633+
LoadObject loadObject = cdbg.loadObjectContainingPC(addr);
634+
// Print the shared library path and the offset of the symbol.
635+
if (loadObject != null) {
636+
out.print(": " + loadObject.getName());
637+
long diff = addr.minus(loadObject.getBase());
638+
if (diff != 0L) {
639+
out.print(" + 0x" + Long.toHexString(diff));
640+
}
641+
}
642+
out.println();
643+
}
644+
}
645+
},
610646
new Command("findpc", "findpc address", false) {
611647
public void doit(Tokens t) {
612648
if (t.countTokens() != 1) {
@@ -892,7 +928,7 @@ public void visit(Klass k) {
892928
}
893929
},
894930
new Command("dumpideal", "dumpideal { -a | id }", false) {
895-
// Do a full dump of the nodes reachabile from root in each compiler thread.
931+
// Do a full dump of the nodes reachable from root in each compiler thread.
896932
public void doit(Tokens t) {
897933
if (t.countTokens() != 1) {
898934
usage();

src/jdk.hotspot.agent/windows/native/libsaproc/sawindbg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class AutoJavaString {
101101
public:
102102
// check env->ExceptionOccurred() after ctor
103103
AutoJavaString(JNIEnv* env, jstring str)
104-
: m_env(env), m_str(str), m_buf(env->GetStringUTFChars(str, nullptr)) {
104+
: m_env(env), m_str(str), m_buf(str == nullptr ? nullptr : env->GetStringUTFChars(str, nullptr)) {
105105
}
106106

107107
~AutoJavaString() {

test/hotspot/jtreg/serviceability/sa/ClhsdbFindPC.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class ClhsdbFindPC {
8282

8383
private static void testFindPC(boolean withXcomp, boolean withCore) throws Exception {
8484
try {
85+
String linesep = System.getProperty("line.separator");
8586
String segvAddress = null;
8687
List<String> cmds = null;
8788
String cmdStr = null;
@@ -218,7 +219,7 @@ private static void testFindPC(boolean withXcomp, boolean withCore) throws Excep
218219
String examineOutput = runTest(withCore, cmds, null);
219220
// Extract <value>.
220221
parts = examineOutput.split(tid + ": ");
221-
String value = parts[1];
222+
String value = parts[1].split(linesep)[0];
222223
// Use findpc on <value>. The output should look something like:
223224
// Address 0x00007fed86f610b8: vtable for JavaThread + 0x10
224225
cmdStr = "findpc " + value;
@@ -229,13 +230,49 @@ private static void testFindPC(boolean withXcomp, boolean withCore) throws Excep
229230
} else if (Platform.isOSX()) {
230231
if (withCore) {
231232
expStrMap.put(cmdStr, List.of("__ZTV10JavaThread"));
232-
} else { // symbol lookups not supported with OSX live process
233+
} else { // address -> symbol lookups not supported with OSX live process
233234
expStrMap.put(cmdStr, List.of("In unknown location"));
234235
}
235236
} else {
236237
expStrMap.put(cmdStr, List.of("vtable for JavaThread"));
237238
}
238-
runTest(withCore, cmds, expStrMap);
239+
String findpcOutput = runTest(withCore, cmds, expStrMap);
240+
241+
// Determine if we have symbol support. Currently we assume yes except on windows.
242+
boolean hasSymbols = true;
243+
if (Platform.isWindows()) {
244+
if (findpcOutput.indexOf("jvm!JavaThread::`vftable'") == -1) {
245+
hasSymbols = false;
246+
}
247+
}
248+
249+
// Run "findsym MaxJNILocalCapacity". The output should look something like:
250+
// 0x00007eff8e1a0da0: <jdk-dir>/lib/server/libjvm.so + 0x1d81da0
251+
String symbol = "MaxJNILocalCapacity";
252+
cmds = List.of("findsym " + symbol);
253+
expStrMap = new HashMap<>();
254+
if (!hasSymbols) {
255+
expStrMap.put(cmdStr, List.of("Symbol not found"));
256+
}
257+
String findsymOutput = runTest(withCore, cmds, expStrMap);
258+
// Run findpc on the result of "findsym MaxJNILocalCapacity". The output
259+
// should look something like:
260+
// Address 0x00007eff8e1a0da0: MaxJNILocalCapacity
261+
if (hasSymbols) {
262+
parts = findsymOutput.split("findsym " + symbol + linesep);
263+
parts = parts[1].split(":");
264+
String findsymAddress = parts[0].split(linesep)[0];
265+
cmdStr = "findpc " + findsymAddress;
266+
cmds = List.of(cmdStr);
267+
expStrMap = new HashMap<>();
268+
if (Platform.isOSX() && !withCore) {
269+
// address -> symbol lookups not supported with OSX live process
270+
expStrMap.put(cmdStr, List.of("Address " + findsymAddress + ": In unknown location"));
271+
} else {
272+
expStrMap.put(cmdStr, List.of("Address " + findsymAddress + ": .*" + symbol));
273+
}
274+
runTest(withCore, cmds, expStrMap);
275+
}
239276
} catch (SkippedException se) {
240277
throw se;
241278
} catch (Exception ex) {

0 commit comments

Comments
 (0)