Skip to content

Commit 8b45497

Browse files
committed
8259037: livenmethods cannot find hsdis library
Reviewed-by: cjplummer, sspitsyn
1 parent 7d76966 commit 8b45497

File tree

2 files changed

+58
-54
lines changed

2 files changed

+58
-54
lines changed

src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/asm/Disassembler.java

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 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
@@ -25,9 +25,14 @@
2525
package sun.jvm.hotspot.asm;
2626

2727
import java.io.PrintStream;
28+
import java.nio.file.Path;
29+
import java.util.List;
30+
import java.util.Iterator;
31+
import java.util.Properties;
2832
import sun.jvm.hotspot.code.CodeBlob;
2933
import sun.jvm.hotspot.code.NMethod;
3034
import sun.jvm.hotspot.debugger.Address;
35+
import sun.jvm.hotspot.debugger.DebuggerException;
3136
import sun.jvm.hotspot.runtime.VM;
3237

3338
public class Disassembler {
@@ -59,44 +64,60 @@ private Disassembler(long startPc, byte[] code) {
5964

6065
// Lazily load hsdis
6166
if (decode_function == 0) {
62-
StringBuilder path = new StringBuilder(System.getProperty("java.home"));
63-
String sep = System.getProperty("file.separator");
64-
String os = System.getProperty("os.name");
65-
String libname = "hsdis";
66-
String arch = System.getProperty("os.arch");
67-
if (os.lastIndexOf("Windows", 0) != -1) {
68-
if (arch.equals("x86")) {
69-
libname += "-i386";
70-
} else if (arch.equals("amd64")) {
71-
libname += "-amd64";
72-
} else {
73-
libname += "-" + arch;
74-
}
75-
path.append(sep + "bin" + sep);
76-
libname += ".dll";
77-
} else if (os.lastIndexOf("Linux", 0) != -1) {
78-
if (arch.equals("x86") || arch.equals("i386")) {
79-
path.append(sep + "lib" + sep + "i386" + sep);
80-
libname += "-i386.so";
81-
} else if (arch.equals("amd64") || arch.equals("x86_64")) {
82-
path.append(sep + "lib" + sep + "amd64" + sep);
83-
libname += "-amd64.so";
84-
} else {
85-
path.append(sep + "lib" + sep + arch + sep);
86-
libname += "-" + arch + ".so";
67+
// Search for hsdis library in the following 4 locations:
68+
// 1. <home>/lib/<vm>/libhsdis-<arch>.so
69+
// 2. <home>/lib/<vm>/hsdis-<arch>.so
70+
// 3. <home>/lib/hsdis-<arch>.so
71+
// 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)
72+
Properties targetSysProps = VM.getVM().getSystemProperties();
73+
String os = targetSysProps.getProperty("os.name");
74+
String ext = ".so";
75+
if (os.contains("Windows")) {
76+
ext = ".dll";
77+
} else if (os.contains("Mac OS")) {
78+
ext = ".dylib";
79+
}
80+
81+
// Find the full path to libjvm.so (jvm.dll and libjvm.dylib on Windows and OSX).
82+
String jvmPattern = "^(lib)?jvm\\" + ext + "$";
83+
Path jvmPath = VM.getVM()
84+
.getDebugger()
85+
.getCDebugger()
86+
.getLoadObjectList()
87+
.stream()
88+
.map(o -> Path.of(o.getName()))
89+
.filter(p -> p.getFileName().toString().matches(jvmPattern))
90+
.findAny()
91+
.get();
92+
93+
String arch = targetSysProps.getProperty("os.arch");
94+
String libname = "hsdis-" + arch + ext;
95+
96+
List<String> libs = List.of(
97+
// 1. <home>/lib/<vm>/libhsdis-<arch>.so
98+
jvmPath.resolveSibling("lib" + libname).toString(),
99+
// 2. <home>/lib/<vm>/hsdis-<arch>.so
100+
jvmPath.resolveSibling(libname).toString(),
101+
// 3. <home>/lib/hsdis-<arch>.so
102+
jvmPath.getParent().resolveSibling(libname).toString(),
103+
// 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)
104+
libname
105+
);
106+
107+
var itr = libs.iterator();
108+
while (itr.hasNext() && (decode_function == 0L)) {
109+
try {
110+
decode_function = load_library(itr.next());
111+
} catch (DebuggerException e) {
112+
if (!itr.hasNext()) {
113+
throw e;
114+
}
87115
}
88-
} else if (os.lastIndexOf("Mac OS X", 0) != -1) {
89-
path.append(sep + "lib" + sep);
90-
libname += "-amd64" + ".dylib"; // x86_64 => amd64
91-
} else {
92-
path.append(sep + "lib" + sep + "arch" + sep);
93-
libname += "-" + arch + ".so";
94116
}
95-
decode_function = load_library(path.toString(), libname);
96117
}
97118
}
98119

99-
private static native long load_library(String installed_jrepath, String hsdis_library_name);
120+
private static native long load_library(String hsdis_library_name);
100121

101122
private native void decode(InstructionVisitor visitor, long pc, byte[] code,
102123
String options, long decode_function);

src/jdk.hotspot.agent/share/native/libsaproc/sadis.c

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 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
@@ -110,42 +110,30 @@ static int getLastErrorString(char *buf, size_t len)
110110
/*
111111
* Class: sun_jvm_hotspot_asm_Disassembler
112112
* Method: load_library
113-
* Signature: (Ljava/lang/String;)L
113+
* Signature: (Ljava/lang/String;)J
114114
*/
115115
JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIEnv * env,
116116
jclass disclass,
117-
jstring jrepath_s,
118117
jstring libname_s) {
119118
uintptr_t func = 0;
120119
const char *error_message = NULL;
121-
const char *jrepath = NULL;
122120
const char *libname = NULL;
123-
char buffer[JVM_MAXPATHLEN];
124121

125122
#ifdef _WINDOWS
123+
char buffer[JVM_MAXPATHLEN];
126124
HINSTANCE hsdis_handle = (HINSTANCE) NULL;
127125
#else
128126
void* hsdis_handle = NULL;
129127
#endif
130128

131-
jrepath = (*env)->GetStringUTFChars(env, jrepath_s, NULL); // like $JAVA_HOME/jre/lib/sparc/
132-
if (jrepath == NULL || (*env)->ExceptionOccurred(env)) {
133-
return 0;
134-
}
135-
136129
libname = (*env)->GetStringUTFChars(env, libname_s, NULL);
137130
if (libname == NULL || (*env)->ExceptionOccurred(env)) {
138-
(*env)->ReleaseStringUTFChars(env, jrepath_s, jrepath);
139131
return 0;
140132
}
141133

142134
/* Load the hsdis library */
143135
#ifdef _WINDOWS
144136
hsdis_handle = LoadLibrary(libname);
145-
if (hsdis_handle == NULL) {
146-
snprintf(buffer, sizeof(buffer), "%s%s", jrepath, libname);
147-
hsdis_handle = LoadLibrary(buffer);
148-
}
149137
if (hsdis_handle != NULL) {
150138
func = (uintptr_t)GetProcAddress(hsdis_handle, "decode_instructions_virtual");
151139
}
@@ -155,10 +143,6 @@ JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIE
155143
}
156144
#else
157145
hsdis_handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
158-
if (hsdis_handle == NULL) {
159-
snprintf(buffer, sizeof(buffer), "%s%s", jrepath, libname);
160-
hsdis_handle = dlopen(buffer, RTLD_LAZY | RTLD_GLOBAL);
161-
}
162146
if (hsdis_handle != NULL) {
163147
func = (uintptr_t)dlsym(hsdis_handle, "decode_instructions_virtual");
164148
}
@@ -168,7 +152,6 @@ JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIE
168152
#endif
169153

170154
(*env)->ReleaseStringUTFChars(env, libname_s, libname);
171-
(*env)->ReleaseStringUTFChars(env, jrepath_s, jrepath);
172155

173156
if (func == 0) {
174157
/* Couldn't find entry point. error_message should contain some

0 commit comments

Comments
 (0)