1217 if (pelements[i] != NULL) {
1218 FREE_C_HEAP_ARRAY(char, pelements[i], mtInternal);
1219 }
1220 }
1221 if (pelements != NULL) {
1222 FREE_C_HEAP_ARRAY(char*, pelements, mtInternal);
1223 }
1224 } else {
1225 snprintf(buffer, buflen, "%s/" JNI_LIB_PREFIX "%s" JNI_LIB_SUFFIX, pname, fname);
1226 retval = true;
1227 }
1228 return retval;
1229 }
1230
1231 // check if addr is inside libjvm.so
1232 bool os::address_is_in_vm(address addr) {
1233 static address libjvm_base_addr;
1234 Dl_info dlinfo;
1235
1236 if (libjvm_base_addr == NULL) {
1237 dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo);
1238 libjvm_base_addr = (address)dlinfo.dli_fbase;
1239 assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");
1240 }
1241
1242 if (dladdr((void *)addr, &dlinfo)) {
1243 if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
1244 }
1245
1246 return false;
1247 }
1248
1249
1250 #define MACH_MAXSYMLEN 256
1251
1252 bool os::dll_address_to_function_name(address addr, char *buf,
1253 int buflen, int *offset) {
1254 Dl_info dlinfo;
1255 char localbuf[MACH_MAXSYMLEN];
1256
1257 // dladdr will find names of dynamic functions only, but does
1258 // it set dli_fbase with mach_header address when it "fails" ?
1259 if (dladdr((void*)addr, &dlinfo) && dlinfo.dli_sname != NULL) {
1260 if (buf != NULL) {
1261 if(!Decoder::demangle(dlinfo.dli_sname, buf, buflen)) {
1262 jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
1263 }
1264 }
1265 if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
1266 return true;
1267 } else if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != 0) {
1268 if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
1269 buf, buflen, offset, dlinfo.dli_fname)) {
1270 return true;
1271 }
1272 }
1273
1274 // Handle non-dymanic manually:
1275 if (dlinfo.dli_fbase != NULL &&
1276 Decoder::decode(addr, localbuf, MACH_MAXSYMLEN, offset, dlinfo.dli_fbase)) {
1277 if(!Decoder::demangle(localbuf, buf, buflen)) {
1278 jio_snprintf(buf, buflen, "%s", localbuf);
1279 }
1280 return true;
1281 }
1282 if (buf != NULL) buf[0] = '\0';
1283 if (offset != NULL) *offset = -1;
1284 return false;
1285 }
1286
1287 // ported from solaris version
1288 bool os::dll_address_to_library_name(address addr, char* buf,
1289 int buflen, int* offset) {
1290 Dl_info dlinfo;
1291
1292 if (dladdr((void*)addr, &dlinfo)){
1293 if (buf) jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
1294 if (offset) *offset = addr - (address)dlinfo.dli_fbase;
1295 return true;
1296 } else {
1297 if (buf) buf[0] = '\0';
1298 if (offset) *offset = -1;
1299 return false;
1300 }
1301 }
1302
1303 // Loads .dll/.so and
1304 // in case of error it checks if .dll/.so was built for the
1305 // same architecture as Hotspot is running on
1306
1307 #ifdef __APPLE__
1308 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
1309 void * result= ::dlopen(filename, RTLD_LAZY);
1310 if (result != NULL) {
1311 // Successful loading
1312 return result;
1313 }
1314
1315 // Read system error message into ebuf
1316 ::strncpy(ebuf, ::dlerror(), ebuflen-1);
1317 ebuf[ebuflen-1]='\0';
1510
1511 char buf[32];
1512 int bytes;
1513 while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) {
1514 st->print_raw(buf, bytes);
1515 }
1516
1517 ::close(fd);
1518
1519 return true;
1520 }
1521
1522 void os::print_dll_info(outputStream *st) {
1523 st->print_cr("Dynamic libraries:");
1524 #ifdef RTLD_DI_LINKMAP
1525 Dl_info dli;
1526 void *handle;
1527 Link_map *map;
1528 Link_map *p;
1529
1530 if (!dladdr(CAST_FROM_FN_PTR(void *, os::print_dll_info), &dli)) {
1531 st->print_cr("Error: Cannot print dynamic libraries.");
1532 return;
1533 }
1534 handle = dlopen(dli.dli_fname, RTLD_LAZY);
1535 if (handle == NULL) {
1536 st->print_cr("Error: Cannot print dynamic libraries.");
1537 return;
1538 }
1539 dlinfo(handle, RTLD_DI_LINKMAP, &map);
1540 if (map == NULL) {
1541 st->print_cr("Error: Cannot print dynamic libraries.");
1542 return;
1543 }
1544
1545 while (map->l_prev != NULL)
1546 map = map->l_prev;
1547
1548 while (map != NULL) {
1549 st->print_cr(PTR_FORMAT " \t%s", map->l_addr, map->l_name);
1550 map = map->l_next;
1690 static char saved_jvm_path[MAXPATHLEN] = {0};
1691
1692 // Find the full path to the current module, libjvm
1693 void os::jvm_path(char *buf, jint buflen) {
1694 // Error checking.
1695 if (buflen < MAXPATHLEN) {
1696 assert(false, "must use a large-enough buffer");
1697 buf[0] = '\0';
1698 return;
1699 }
1700 // Lazy resolve the path to current module.
1701 if (saved_jvm_path[0] != 0) {
1702 strcpy(buf, saved_jvm_path);
1703 return;
1704 }
1705
1706 char dli_fname[MAXPATHLEN];
1707 bool ret = dll_address_to_library_name(
1708 CAST_FROM_FN_PTR(address, os::jvm_path),
1709 dli_fname, sizeof(dli_fname), NULL);
1710 assert(ret != 0, "cannot locate libjvm");
1711 char *rp = realpath(dli_fname, buf);
1712 if (rp == NULL)
1713 return;
1714
1715 if (Arguments::created_by_gamma_launcher()) {
1716 // Support for the gamma launcher. Typical value for buf is
1717 // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm". If "/jre/lib/" appears at
1718 // the right place in the string, then assume we are installed in a JDK and
1719 // we're done. Otherwise, check for a JAVA_HOME environment variable and
1720 // construct a path to the JVM being overridden.
1721
1722 const char *p = buf + strlen(buf) - 1;
1723 for (int count = 0; p > buf && count < 5; ++count) {
1724 for (--p; p > buf && *p != '/'; --p)
1725 /* empty */ ;
1726 }
1727
1728 if (strncmp(p, "/jre/lib/", 9) != 0) {
1729 // Look for JAVA_HOME in the environment.
1730 char* java_home_var = ::getenv("JAVA_HOME");
1731 if (java_home_var != NULL && java_home_var[0] != 0) {
3730 // Make sure that it is called by the watcher for the VMThread
3731 assert(Thread::current()->is_Watcher_thread(), "Must be watcher");
3732 assert(thread->is_VM_thread(), "Can only be called for VMThread");
3733
3734 PcFetcher fetcher(thread);
3735 fetcher.run();
3736 return fetcher.result();
3737 }
3738
3739 int os::Bsd::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)
3740 {
3741 return pthread_cond_timedwait(_cond, _mutex, _abstime);
3742 }
3743
3744 ////////////////////////////////////////////////////////////////////////////////
3745 // debug support
3746
3747 bool os::find(address addr, outputStream* st) {
3748 Dl_info dlinfo;
3749 memset(&dlinfo, 0, sizeof(dlinfo));
3750 if (dladdr(addr, &dlinfo)) {
3751 st->print(PTR_FORMAT ": ", addr);
3752 if (dlinfo.dli_sname != NULL) {
3753 st->print("%s+%#x", dlinfo.dli_sname,
3754 addr - (intptr_t)dlinfo.dli_saddr);
3755 } else if (dlinfo.dli_fname) {
3756 st->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);
3757 } else {
3758 st->print("<absolute address>");
3759 }
3760 if (dlinfo.dli_fname) {
3761 st->print(" in %s", dlinfo.dli_fname);
3762 }
3763 if (dlinfo.dli_fbase) {
3764 st->print(" at " PTR_FORMAT, dlinfo.dli_fbase);
3765 }
3766 st->cr();
3767
3768 if (Verbose) {
3769 // decode some bytes around the PC
3770 address begin = clamp_address_in_page(addr-40, addr, os::vm_page_size());
3771 address end = clamp_address_in_page(addr+40, addr, os::vm_page_size());
3772 address lowest = (address) dlinfo.dli_sname;
3773 if (!lowest) lowest = (address) dlinfo.dli_fbase;
3774 if (begin < lowest) begin = lowest;
3775 Dl_info dlinfo2;
3776 if (dladdr(end, &dlinfo2) && dlinfo2.dli_saddr != dlinfo.dli_saddr
3777 && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin)
3778 end = (address) dlinfo2.dli_saddr;
3779 Disassembler::decode(begin, end, st);
3780 }
3781 return true;
3782 }
3783 return false;
3784 }
3785
3786 ////////////////////////////////////////////////////////////////////////////////
3787 // misc
3788
3789 // This does not do anything on Bsd. This is basically a hook for being
3790 // able to use structured exception handling (thread-local exception filters)
3791 // on, e.g., Win32.
3792 void
3793 os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method,
3794 JavaCallArguments* args, Thread* thread) {
3795 f(value, method, args, thread);
3796 }
|
1217 if (pelements[i] != NULL) {
1218 FREE_C_HEAP_ARRAY(char, pelements[i], mtInternal);
1219 }
1220 }
1221 if (pelements != NULL) {
1222 FREE_C_HEAP_ARRAY(char*, pelements, mtInternal);
1223 }
1224 } else {
1225 snprintf(buffer, buflen, "%s/" JNI_LIB_PREFIX "%s" JNI_LIB_SUFFIX, pname, fname);
1226 retval = true;
1227 }
1228 return retval;
1229 }
1230
1231 // check if addr is inside libjvm.so
1232 bool os::address_is_in_vm(address addr) {
1233 static address libjvm_base_addr;
1234 Dl_info dlinfo;
1235
1236 if (libjvm_base_addr == NULL) {
1237 if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo) != 0) {
1238 libjvm_base_addr = (address)dlinfo.dli_fbase;
1239 }
1240 assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");
1241 }
1242
1243 if (dladdr((void *)addr, &dlinfo) != 0) {
1244 if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
1245 }
1246
1247 return false;
1248 }
1249
1250
1251 #define MACH_MAXSYMLEN 256
1252
1253 bool os::dll_address_to_function_name(address addr, char *buf,
1254 int buflen, int *offset) {
1255 // buf is not optional, but offset is optional
1256 assert(buf != NULL, "sanity check");
1257
1258 Dl_info dlinfo;
1259 char localbuf[MACH_MAXSYMLEN];
1260
1261 if (dladdr((void*)addr, &dlinfo) != 0) {
1262 // see if we have a matching symbol
1263 if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {
1264 if (!Decoder::demangle(dlinfo.dli_sname, buf, buflen)) {
1265 jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
1266 }
1267 if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
1268 return true;
1269 }
1270 // no matching symbol so try for just file info
1271 if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {
1272 if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
1273 buf, buflen, offset, dlinfo.dli_fname)) {
1274 return true;
1275 }
1276 }
1277
1278 // Handle non-dynamic manually:
1279 if (dlinfo.dli_fbase != NULL &&
1280 Decoder::decode(addr, localbuf, MACH_MAXSYMLEN, offset,
1281 dlinfo.dli_fbase)) {
1282 if (!Decoder::demangle(localbuf, buf, buflen)) {
1283 jio_snprintf(buf, buflen, "%s", localbuf);
1284 }
1285 return true;
1286 }
1287 }
1288 buf[0] = '\0';
1289 if (offset != NULL) *offset = -1;
1290 return false;
1291 }
1292
1293 // ported from solaris version
1294 bool os::dll_address_to_library_name(address addr, char* buf,
1295 int buflen, int* offset) {
1296 // buf is not optional, but offset is optional
1297 assert(buf != NULL, "sanity check");
1298
1299 Dl_info dlinfo;
1300
1301 if (dladdr((void*)addr, &dlinfo) != 0) {
1302 if (dlinfo.dli_fname != NULL) {
1303 jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
1304 }
1305 if (dlinfo.dli_fbase != NULL && offset != NULL) {
1306 *offset = addr - (address)dlinfo.dli_fbase;
1307 }
1308 return true;
1309 } else {
1310 buf[0] = '\0';
1311 if (offset) *offset = -1;
1312 return false;
1313 }
1314 }
1315
1316 // Loads .dll/.so and
1317 // in case of error it checks if .dll/.so was built for the
1318 // same architecture as Hotspot is running on
1319
1320 #ifdef __APPLE__
1321 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
1322 void * result= ::dlopen(filename, RTLD_LAZY);
1323 if (result != NULL) {
1324 // Successful loading
1325 return result;
1326 }
1327
1328 // Read system error message into ebuf
1329 ::strncpy(ebuf, ::dlerror(), ebuflen-1);
1330 ebuf[ebuflen-1]='\0';
1523
1524 char buf[32];
1525 int bytes;
1526 while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) {
1527 st->print_raw(buf, bytes);
1528 }
1529
1530 ::close(fd);
1531
1532 return true;
1533 }
1534
1535 void os::print_dll_info(outputStream *st) {
1536 st->print_cr("Dynamic libraries:");
1537 #ifdef RTLD_DI_LINKMAP
1538 Dl_info dli;
1539 void *handle;
1540 Link_map *map;
1541 Link_map *p;
1542
1543 if (dladdr(CAST_FROM_FN_PTR(void *, os::print_dll_info), &dli) == 0 ||
1544 dli.dli_fname == NULL) {
1545 st->print_cr("Error: Cannot print dynamic libraries.");
1546 return;
1547 }
1548 handle = dlopen(dli.dli_fname, RTLD_LAZY);
1549 if (handle == NULL) {
1550 st->print_cr("Error: Cannot print dynamic libraries.");
1551 return;
1552 }
1553 dlinfo(handle, RTLD_DI_LINKMAP, &map);
1554 if (map == NULL) {
1555 st->print_cr("Error: Cannot print dynamic libraries.");
1556 return;
1557 }
1558
1559 while (map->l_prev != NULL)
1560 map = map->l_prev;
1561
1562 while (map != NULL) {
1563 st->print_cr(PTR_FORMAT " \t%s", map->l_addr, map->l_name);
1564 map = map->l_next;
1704 static char saved_jvm_path[MAXPATHLEN] = {0};
1705
1706 // Find the full path to the current module, libjvm
1707 void os::jvm_path(char *buf, jint buflen) {
1708 // Error checking.
1709 if (buflen < MAXPATHLEN) {
1710 assert(false, "must use a large-enough buffer");
1711 buf[0] = '\0';
1712 return;
1713 }
1714 // Lazy resolve the path to current module.
1715 if (saved_jvm_path[0] != 0) {
1716 strcpy(buf, saved_jvm_path);
1717 return;
1718 }
1719
1720 char dli_fname[MAXPATHLEN];
1721 bool ret = dll_address_to_library_name(
1722 CAST_FROM_FN_PTR(address, os::jvm_path),
1723 dli_fname, sizeof(dli_fname), NULL);
1724 assert(ret, "cannot locate libjvm");
1725 char *rp = NULL;
1726 if (ret && dli_fname[0] != '\0') {
1727 rp = realpath(dli_fname, buf);
1728 }
1729 if (rp == NULL)
1730 return;
1731
1732 if (Arguments::created_by_gamma_launcher()) {
1733 // Support for the gamma launcher. Typical value for buf is
1734 // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm". If "/jre/lib/" appears at
1735 // the right place in the string, then assume we are installed in a JDK and
1736 // we're done. Otherwise, check for a JAVA_HOME environment variable and
1737 // construct a path to the JVM being overridden.
1738
1739 const char *p = buf + strlen(buf) - 1;
1740 for (int count = 0; p > buf && count < 5; ++count) {
1741 for (--p; p > buf && *p != '/'; --p)
1742 /* empty */ ;
1743 }
1744
1745 if (strncmp(p, "/jre/lib/", 9) != 0) {
1746 // Look for JAVA_HOME in the environment.
1747 char* java_home_var = ::getenv("JAVA_HOME");
1748 if (java_home_var != NULL && java_home_var[0] != 0) {
3747 // Make sure that it is called by the watcher for the VMThread
3748 assert(Thread::current()->is_Watcher_thread(), "Must be watcher");
3749 assert(thread->is_VM_thread(), "Can only be called for VMThread");
3750
3751 PcFetcher fetcher(thread);
3752 fetcher.run();
3753 return fetcher.result();
3754 }
3755
3756 int os::Bsd::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)
3757 {
3758 return pthread_cond_timedwait(_cond, _mutex, _abstime);
3759 }
3760
3761 ////////////////////////////////////////////////////////////////////////////////
3762 // debug support
3763
3764 bool os::find(address addr, outputStream* st) {
3765 Dl_info dlinfo;
3766 memset(&dlinfo, 0, sizeof(dlinfo));
3767 if (dladdr(addr, &dlinfo) != 0) {
3768 st->print(PTR_FORMAT ": ", addr);
3769 if (dlinfo.dli_sname != NULL && dlinfo.dli_saddr != NULL) {
3770 st->print("%s+%#x", dlinfo.dli_sname,
3771 addr - (intptr_t)dlinfo.dli_saddr);
3772 } else if (dlinfo.dli_fbase != NULL) {
3773 st->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);
3774 } else {
3775 st->print("<absolute address>");
3776 }
3777 if (dlinfo.dli_fname != NULL) {
3778 st->print(" in %s", dlinfo.dli_fname);
3779 }
3780 if (dlinfo.dli_fbase != NULL) {
3781 st->print(" at " PTR_FORMAT, dlinfo.dli_fbase);
3782 }
3783 st->cr();
3784
3785 if (Verbose) {
3786 // decode some bytes around the PC
3787 address begin = clamp_address_in_page(addr-40, addr, os::vm_page_size());
3788 address end = clamp_address_in_page(addr+40, addr, os::vm_page_size());
3789 address lowest = (address) dlinfo.dli_sname;
3790 if (!lowest) lowest = (address) dlinfo.dli_fbase;
3791 if (begin < lowest) begin = lowest;
3792 Dl_info dlinfo2;
3793 if (dladdr(end, &dlinfo2) != 0 && dlinfo2.dli_saddr != dlinfo.dli_saddr
3794 && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin)
3795 end = (address) dlinfo2.dli_saddr;
3796 Disassembler::decode(begin, end, st);
3797 }
3798 return true;
3799 }
3800 return false;
3801 }
3802
3803 ////////////////////////////////////////////////////////////////////////////////
3804 // misc
3805
3806 // This does not do anything on Bsd. This is basically a hook for being
3807 // able to use structured exception handling (thread-local exception filters)
3808 // on, e.g., Win32.
3809 void
3810 os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method,
3811 JavaCallArguments* args, Thread* thread) {
3812 f(value, method, args, thread);
3813 }
|