Skip to content

Commit 500a3a2

Browse files
committed
8358799: Refactor os::jvm_path()
Reviewed-by: dholmes, jsjolen
1 parent a2f99fd commit 500a3a2

File tree

4 files changed

+89
-211
lines changed

4 files changed

+89
-211
lines changed

src/hotspot/os/aix/os_aix.cpp

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,69 +1261,6 @@ void os::pd_print_cpu_info(outputStream* st, char* buf, size_t buflen) {
12611261
// Nothing to do beyond of what os::print_cpu_info() does.
12621262
}
12631263

1264-
static char saved_jvm_path[MAXPATHLEN] = {0};
1265-
1266-
// Find the full path to the current module, libjvm.so.
1267-
void os::jvm_path(char *buf, jint buflen) {
1268-
// Error checking.
1269-
if (buflen < MAXPATHLEN) {
1270-
assert(false, "must use a large-enough buffer");
1271-
buf[0] = '\0';
1272-
return;
1273-
}
1274-
// Lazy resolve the path to current module.
1275-
if (saved_jvm_path[0] != 0) {
1276-
strcpy(buf, saved_jvm_path);
1277-
return;
1278-
}
1279-
1280-
Dl_info dlinfo;
1281-
int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
1282-
assert(ret != 0, "cannot locate libjvm");
1283-
char* rp = os::realpath((char *)dlinfo.dli_fname, buf, buflen);
1284-
assert(rp != nullptr, "error in realpath(): maybe the 'path' argument is too long?");
1285-
1286-
// If executing unit tests we require JAVA_HOME to point to the real JDK.
1287-
if (Arguments::executing_unit_tests()) {
1288-
// Look for JAVA_HOME in the environment.
1289-
char* java_home_var = ::getenv("JAVA_HOME");
1290-
if (java_home_var != nullptr && java_home_var[0] != 0) {
1291-
1292-
// Check the current module name "libjvm.so".
1293-
const char* p = strrchr(buf, '/');
1294-
if (p == nullptr) {
1295-
return;
1296-
}
1297-
assert(strstr(p, "/libjvm") == p, "invalid library name");
1298-
1299-
stringStream ss(buf, buflen);
1300-
rp = os::realpath(java_home_var, buf, buflen);
1301-
if (rp == nullptr) {
1302-
return;
1303-
}
1304-
1305-
assert((int)strlen(buf) < buflen, "Ran out of buffer room");
1306-
ss.print("%s/lib", buf);
1307-
1308-
if (0 == access(buf, F_OK)) {
1309-
// Use current module name "libjvm.so"
1310-
ss.print("/%s/libjvm%s", Abstract_VM_Version::vm_variant(), JNI_LIB_SUFFIX);
1311-
assert(strcmp(buf + strlen(buf) - strlen(JNI_LIB_SUFFIX), JNI_LIB_SUFFIX) == 0,
1312-
"buf has been truncated");
1313-
} else {
1314-
// Go back to path of .so
1315-
rp = os::realpath((char *)dlinfo.dli_fname, buf, buflen);
1316-
if (rp == nullptr) {
1317-
return;
1318-
}
1319-
}
1320-
}
1321-
}
1322-
1323-
strncpy(saved_jvm_path, buf, sizeof(saved_jvm_path));
1324-
saved_jvm_path[sizeof(saved_jvm_path) - 1] = '\0';
1325-
}
1326-
13271264
////////////////////////////////////////////////////////////////////////////////
13281265
// Virtual Memory
13291266

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,83 +1482,6 @@ void os::print_memory_info(outputStream* st) {
14821482
st->cr();
14831483
}
14841484

1485-
static char saved_jvm_path[MAXPATHLEN] = {0};
1486-
1487-
// Find the full path to the current module, libjvm
1488-
void os::jvm_path(char *buf, jint buflen) {
1489-
// Error checking.
1490-
if (buflen < MAXPATHLEN) {
1491-
assert(false, "must use a large-enough buffer");
1492-
buf[0] = '\0';
1493-
return;
1494-
}
1495-
// Lazy resolve the path to current module.
1496-
if (saved_jvm_path[0] != 0) {
1497-
strcpy(buf, saved_jvm_path);
1498-
return;
1499-
}
1500-
1501-
char dli_fname[MAXPATHLEN];
1502-
dli_fname[0] = '\0';
1503-
bool ret = dll_address_to_library_name(
1504-
CAST_FROM_FN_PTR(address, os::jvm_path),
1505-
dli_fname, sizeof(dli_fname), nullptr);
1506-
assert(ret, "cannot locate libjvm");
1507-
char *rp = nullptr;
1508-
if (ret && dli_fname[0] != '\0') {
1509-
rp = os::realpath(dli_fname, buf, buflen);
1510-
}
1511-
if (rp == nullptr) {
1512-
return;
1513-
}
1514-
1515-
// If executing unit tests we require JAVA_HOME to point to the real JDK.
1516-
if (Arguments::executing_unit_tests()) {
1517-
// Look for JAVA_HOME in the environment.
1518-
char* java_home_var = ::getenv("JAVA_HOME");
1519-
if (java_home_var != nullptr && java_home_var[0] != 0) {
1520-
1521-
// Check the current module name "libjvm"
1522-
const char* p = strrchr(buf, '/');
1523-
assert(strstr(p, "/libjvm") == p, "invalid library name");
1524-
1525-
stringStream ss(buf, buflen);
1526-
rp = os::realpath(java_home_var, buf, buflen);
1527-
if (rp == nullptr) {
1528-
return;
1529-
}
1530-
1531-
assert((int)strlen(buf) < buflen, "Ran out of buffer space");
1532-
// Add the appropriate library and JVM variant subdirs
1533-
ss.print("%s/lib/%s", buf, Abstract_VM_Version::vm_variant());
1534-
1535-
if (0 != access(buf, F_OK)) {
1536-
ss.reset();
1537-
ss.print("%s/lib", buf);
1538-
}
1539-
1540-
// If the path exists within JAVA_HOME, add the JVM library name
1541-
// to complete the path to JVM being overridden. Otherwise fallback
1542-
// to the path to the current library.
1543-
if (0 == access(buf, F_OK)) {
1544-
// Use current module name "libjvm"
1545-
ss.print("/libjvm%s", JNI_LIB_SUFFIX);
1546-
assert(strcmp(buf + strlen(buf) - strlen(JNI_LIB_SUFFIX), JNI_LIB_SUFFIX) == 0,
1547-
"buf has been truncated");
1548-
} else {
1549-
// Fall back to path of current library
1550-
rp = os::realpath(dli_fname, buf, buflen);
1551-
if (rp == nullptr) {
1552-
return;
1553-
}
1554-
}
1555-
}
1556-
}
1557-
1558-
strncpy(saved_jvm_path, buf, MAXPATHLEN);
1559-
saved_jvm_path[MAXPATHLEN - 1] = '\0';
1560-
}
1561-
15621485
////////////////////////////////////////////////////////////////////////////////
15631486
// Virtual Memory
15641487

src/hotspot/os/linux/os_linux.cpp

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,77 +2746,6 @@ void os::get_summary_cpu_info(char* cpuinfo, size_t length) {
27462746
#endif
27472747
}
27482748

2749-
static char saved_jvm_path[MAXPATHLEN] = {0};
2750-
2751-
// Find the full path to the current module, libjvm.so
2752-
void os::jvm_path(char *buf, jint buflen) {
2753-
// Error checking.
2754-
if (buflen < MAXPATHLEN) {
2755-
assert(false, "must use a large-enough buffer");
2756-
buf[0] = '\0';
2757-
return;
2758-
}
2759-
// Lazy resolve the path to current module.
2760-
if (saved_jvm_path[0] != 0) {
2761-
strcpy(buf, saved_jvm_path);
2762-
return;
2763-
}
2764-
2765-
char dli_fname[MAXPATHLEN];
2766-
dli_fname[0] = '\0';
2767-
bool ret = dll_address_to_library_name(
2768-
CAST_FROM_FN_PTR(address, os::jvm_path),
2769-
dli_fname, sizeof(dli_fname), nullptr);
2770-
assert(ret, "cannot locate libjvm");
2771-
char *rp = nullptr;
2772-
if (ret && dli_fname[0] != '\0') {
2773-
rp = os::realpath(dli_fname, buf, buflen);
2774-
}
2775-
if (rp == nullptr) {
2776-
return;
2777-
}
2778-
2779-
// If executing unit tests we require JAVA_HOME to point to the real JDK.
2780-
if (Arguments::executing_unit_tests()) {
2781-
// Look for JAVA_HOME in the environment.
2782-
char* java_home_var = ::getenv("JAVA_HOME");
2783-
if (java_home_var != nullptr && java_home_var[0] != 0) {
2784-
2785-
// Check the current module name "libjvm.so".
2786-
const char* p = strrchr(buf, '/');
2787-
if (p == nullptr) {
2788-
return;
2789-
}
2790-
assert(strstr(p, "/libjvm") == p, "invalid library name");
2791-
2792-
stringStream ss(buf, buflen);
2793-
rp = os::realpath(java_home_var, buf, buflen);
2794-
if (rp == nullptr) {
2795-
return;
2796-
}
2797-
2798-
assert((int)strlen(buf) < buflen, "Ran out of buffer room");
2799-
ss.print("%s/lib", buf);
2800-
2801-
if (0 == access(buf, F_OK)) {
2802-
// Use current module name "libjvm.so"
2803-
ss.print("/%s/libjvm%s", Abstract_VM_Version::vm_variant(), JNI_LIB_SUFFIX);
2804-
assert(strcmp(buf + strlen(buf) - strlen(JNI_LIB_SUFFIX), JNI_LIB_SUFFIX) == 0,
2805-
"buf has been truncated");
2806-
} else {
2807-
// Go back to path of .so
2808-
rp = os::realpath(dli_fname, buf, buflen);
2809-
if (rp == nullptr) {
2810-
return;
2811-
}
2812-
}
2813-
}
2814-
}
2815-
2816-
strncpy(saved_jvm_path, buf, MAXPATHLEN);
2817-
saved_jvm_path[MAXPATHLEN - 1] = '\0';
2818-
}
2819-
28202749
////////////////////////////////////////////////////////////////////////////////
28212750
// Virtual Memory
28222751

src/hotspot/os/posix/os_posix.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,95 @@ bool os::same_files(const char* file1, const char* file2) {
10601060
return is_same;
10611061
}
10621062

1063+
static char saved_jvm_path[MAXPATHLEN] = {0};
1064+
1065+
// Find the full path to the current module, libjvm.so
1066+
void os::jvm_path(char *buf, jint buflen) {
1067+
// Error checking.
1068+
if (buflen < MAXPATHLEN) {
1069+
assert(false, "must use a large-enough buffer");
1070+
buf[0] = '\0';
1071+
return;
1072+
}
1073+
// Lazy resolve the path to current module.
1074+
if (saved_jvm_path[0] != 0) {
1075+
strcpy(buf, saved_jvm_path);
1076+
return;
1077+
}
1078+
1079+
char* fname;
1080+
#ifdef AIX
1081+
Dl_info dlinfo;
1082+
int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
1083+
assert(ret != 0, "cannot locate libjvm");
1084+
if (ret == 0) {
1085+
return;
1086+
}
1087+
fname = dlinfo.dli_fname;
1088+
#else
1089+
char dli_fname[MAXPATHLEN];
1090+
dli_fname[0] = '\0';
1091+
bool ret = dll_address_to_library_name(
1092+
CAST_FROM_FN_PTR(address, os::jvm_path),
1093+
dli_fname, sizeof(dli_fname), nullptr);
1094+
assert(ret, "cannot locate libjvm");
1095+
if (!ret) {
1096+
return;
1097+
}
1098+
fname = dli_fname;
1099+
#endif // AIX
1100+
char* rp = nullptr;
1101+
if (fname[0] != '\0') {
1102+
rp = os::realpath(dli_fname, buf, buflen);
1103+
}
1104+
if (rp == nullptr) {
1105+
return;
1106+
}
1107+
1108+
// If executing unit tests we require JAVA_HOME to point to the real JDK.
1109+
if (Arguments::executing_unit_tests()) {
1110+
// Look for JAVA_HOME in the environment.
1111+
char* java_home_var = ::getenv("JAVA_HOME");
1112+
if (java_home_var != nullptr && java_home_var[0] != 0) {
1113+
1114+
// Check the current module name "libjvm.so".
1115+
const char* p = strrchr(buf, '/');
1116+
if (p == nullptr) {
1117+
return;
1118+
}
1119+
assert(strstr(p, "/libjvm") == p, "invalid library name");
1120+
1121+
stringStream ss(buf, buflen);
1122+
rp = os::realpath(java_home_var, buf, buflen);
1123+
if (rp == nullptr) {
1124+
return;
1125+
}
1126+
1127+
assert((int)strlen(buf) < buflen, "Ran out of buffer room");
1128+
ss.print("%s/lib", buf);
1129+
1130+
// If the path exists within JAVA_HOME, add the VM variant directory and JVM
1131+
// library name to complete the path to JVM being overridden. Otherwise fallback
1132+
// to the path to the current library.
1133+
if (0 == access(buf, F_OK)) {
1134+
// Use current module name "libjvm.so"
1135+
ss.print("/%s/libjvm%s", Abstract_VM_Version::vm_variant(), JNI_LIB_SUFFIX);
1136+
assert(strcmp(buf + strlen(buf) - strlen(JNI_LIB_SUFFIX), JNI_LIB_SUFFIX) == 0,
1137+
"buf has been truncated");
1138+
} else {
1139+
// Go back to path of .so
1140+
rp = os::realpath(dli_fname, buf, buflen);
1141+
if (rp == nullptr) {
1142+
return;
1143+
}
1144+
}
1145+
}
1146+
}
1147+
1148+
strncpy(saved_jvm_path, buf, MAXPATHLEN);
1149+
saved_jvm_path[MAXPATHLEN - 1] = '\0';
1150+
}
1151+
10631152
// Called when creating the thread. The minimum stack sizes have already been calculated
10641153
size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) {
10651154
size_t stack_size;

0 commit comments

Comments
 (0)