src/os/linux/vm/os_linux.cpp

Print this page




1665       if (pelements[i] != NULL) {
1666         FREE_C_HEAP_ARRAY(char, pelements[i], mtInternal);
1667       }
1668     }
1669     if (pelements != NULL) {
1670       FREE_C_HEAP_ARRAY(char*, pelements, mtInternal);
1671     }
1672   } else {
1673     snprintf(buffer, buflen, "%s/lib%s.so", pname, fname);
1674     retval = true;
1675   }
1676   return retval;
1677 }
1678 
1679 // check if addr is inside libjvm.so
1680 bool os::address_is_in_vm(address addr) {
1681   static address libjvm_base_addr;
1682   Dl_info dlinfo;
1683 
1684   if (libjvm_base_addr == NULL) {
1685     dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo);
1686     libjvm_base_addr = (address)dlinfo.dli_fbase;

1687     assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");
1688   }
1689 
1690   if (dladdr((void *)addr, &dlinfo)) {
1691     if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
1692   }
1693 
1694   return false;
1695 }
1696 
1697 bool os::dll_address_to_function_name(address addr, char *buf,
1698                                       int buflen, int *offset) {



1699   Dl_info dlinfo;
1700 
1701   if (dladdr((void*)addr, &dlinfo) && dlinfo.dli_sname != NULL) {
1702     if (buf != NULL) {
1703       if(!Decoder::demangle(dlinfo.dli_sname, buf, buflen)) {

1704         jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
1705       }
1706     }
1707     if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
1708     return true;
1709   } else if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != 0) {


1710     if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
1711         buf, buflen, offset, dlinfo.dli_fname)) {
1712        return true;
1713     }
1714   }

1715 
1716   if (buf != NULL) buf[0] = '\0';
1717   if (offset != NULL) *offset = -1;
1718   return false;
1719 }
1720 
1721 struct _address_to_library_name {
1722   address addr;          // input : memory address
1723   size_t  buflen;        //         size of fname
1724   char*   fname;         // output: library name
1725   address base;          //         library base addr
1726 };
1727 
1728 static int address_to_library_name_callback(struct dl_phdr_info *info,
1729                                             size_t size, void *data) {
1730   int i;
1731   bool found = false;
1732   address libbase = NULL;
1733   struct _address_to_library_name * d = (struct _address_to_library_name *)data;
1734 
1735   // iterate through all loadable segments
1736   for (i = 0; i < info->dlpi_phnum; i++) {


1747         found = true;
1748       }
1749     }
1750   }
1751 
1752   // dlpi_name is NULL or empty if the ELF file is executable, return 0
1753   // so dll_address_to_library_name() can fall through to use dladdr() which
1754   // can figure out executable name from argv[0].
1755   if (found && info->dlpi_name && info->dlpi_name[0]) {
1756     d->base = libbase;
1757     if (d->fname) {
1758       jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name);
1759     }
1760     return 1;
1761   }
1762   return 0;
1763 }
1764 
1765 bool os::dll_address_to_library_name(address addr, char* buf,
1766                                      int buflen, int* offset) {



1767   Dl_info dlinfo;
1768   struct _address_to_library_name data;
1769 
1770   // There is a bug in old glibc dladdr() implementation that it could resolve
1771   // to wrong library name if the .so file has a base address != NULL. Here
1772   // we iterate through the program headers of all loaded libraries to find
1773   // out which library 'addr' really belongs to. This workaround can be
1774   // removed once the minimum requirement for glibc is moved to 2.3.x.
1775   data.addr = addr;
1776   data.fname = buf;
1777   data.buflen = buflen;
1778   data.base = NULL;
1779   int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data);
1780 
1781   if (rslt) {
1782      // buf already contains library name
1783      if (offset) *offset = addr - data.base;
1784      return true;
1785   } else if (dladdr((void*)addr, &dlinfo)){
1786      if (buf) jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
1787      if (offset) *offset = addr - (address)dlinfo.dli_fbase;




1788      return true;
1789   } else {
1790      if (buf) buf[0] = '\0';
1791      if (offset) *offset = -1;
1792      return false;
1793   }
1794 }
1795 
1796   // Loads .dll/.so and
1797   // in case of error it checks if .dll/.so was built for the
1798   // same architecture as Hotspot is running on
1799 
1800 
1801 // Remember the stack's state. The Linux dynamic linker will change
1802 // the stack to 'executable' at most once, so we must safepoint only once.
1803 bool os::Linux::_stack_is_executable = false;
1804 
1805 // VM operation that loads a library.  This is necessary if stack protection
1806 // of the Java stacks can be lost during loading the library.  If we
1807 // do not stop the Java threads, they can stack overflow before the stacks
1808 // are protected again.
1809 class VM_LinuxDllLoad: public VM_Operation {
1810  private:


2300 static char saved_jvm_path[MAXPATHLEN] = {0};
2301 
2302 // Find the full path to the current module, libjvm.so
2303 void os::jvm_path(char *buf, jint buflen) {
2304   // Error checking.
2305   if (buflen < MAXPATHLEN) {
2306     assert(false, "must use a large-enough buffer");
2307     buf[0] = '\0';
2308     return;
2309   }
2310   // Lazy resolve the path to current module.
2311   if (saved_jvm_path[0] != 0) {
2312     strcpy(buf, saved_jvm_path);
2313     return;
2314   }
2315 
2316   char dli_fname[MAXPATHLEN];
2317   bool ret = dll_address_to_library_name(
2318                 CAST_FROM_FN_PTR(address, os::jvm_path),
2319                 dli_fname, sizeof(dli_fname), NULL);
2320   assert(ret != 0, "cannot locate libjvm");
2321   char *rp = realpath(dli_fname, buf);



2322   if (rp == NULL)
2323     return;
2324 
2325   if (Arguments::created_by_gamma_launcher()) {
2326     // Support for the gamma launcher.  Typical value for buf is
2327     // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so".  If "/jre/lib/" appears at
2328     // the right place in the string, then assume we are installed in a JDK and
2329     // we're done.  Otherwise, check for a JAVA_HOME environment variable and fix
2330     // up the path so it looks like libjvm.so is installed there (append a
2331     // fake suffix hotspot/libjvm.so).
2332     const char *p = buf + strlen(buf) - 1;
2333     for (int count = 0; p > buf && count < 5; ++count) {
2334       for (--p; p > buf && *p != '/'; --p)
2335         /* empty */ ;
2336     }
2337 
2338     if (strncmp(p, "/jre/lib/", 9) != 0) {
2339       // Look for JAVA_HOME in the environment.
2340       char* java_home_var = ::getenv("JAVA_HOME");
2341       if (java_home_var != NULL && java_home_var[0] != 0) {


4713 {
4714    if (is_NPTL()) {
4715       return pthread_cond_timedwait(_cond, _mutex, _abstime);
4716    } else {
4717       // 6292965: LinuxThreads pthread_cond_timedwait() resets FPU control
4718       // word back to default 64bit precision if condvar is signaled. Java
4719       // wants 53bit precision.  Save and restore current value.
4720       int fpu = get_fpu_control_word();
4721       int status = pthread_cond_timedwait(_cond, _mutex, _abstime);
4722       set_fpu_control_word(fpu);
4723       return status;
4724    }
4725 }
4726 
4727 ////////////////////////////////////////////////////////////////////////////////
4728 // debug support
4729 
4730 bool os::find(address addr, outputStream* st) {
4731   Dl_info dlinfo;
4732   memset(&dlinfo, 0, sizeof(dlinfo));
4733   if (dladdr(addr, &dlinfo)) {
4734     st->print(PTR_FORMAT ": ", addr);
4735     if (dlinfo.dli_sname != NULL) {
4736       st->print("%s+%#x", dlinfo.dli_sname,
4737                  addr - (intptr_t)dlinfo.dli_saddr);
4738     } else if (dlinfo.dli_fname) {
4739       st->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);
4740     } else {
4741       st->print("<absolute address>");
4742     }
4743     if (dlinfo.dli_fname) {
4744       st->print(" in %s", dlinfo.dli_fname);
4745     }
4746     if (dlinfo.dli_fbase) {
4747       st->print(" at " PTR_FORMAT, dlinfo.dli_fbase);
4748     }
4749     st->cr();
4750 
4751     if (Verbose) {
4752       // decode some bytes around the PC
4753       address begin = clamp_address_in_page(addr-40, addr, os::vm_page_size());
4754       address end   = clamp_address_in_page(addr+40, addr, os::vm_page_size());
4755       address       lowest = (address) dlinfo.dli_sname;
4756       if (!lowest)  lowest = (address) dlinfo.dli_fbase;
4757       if (begin < lowest)  begin = lowest;
4758       Dl_info dlinfo2;
4759       if (dladdr(end, &dlinfo2) && dlinfo2.dli_saddr != dlinfo.dli_saddr
4760           && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin)
4761         end = (address) dlinfo2.dli_saddr;
4762       Disassembler::decode(begin, end, st);
4763     }
4764     return true;
4765   }
4766   return false;
4767 }
4768 
4769 ////////////////////////////////////////////////////////////////////////////////
4770 // misc
4771 
4772 // This does not do anything on Linux. This is basically a hook for being
4773 // able to use structured exception handling (thread-local exception filters)
4774 // on, e.g., Win32.
4775 void
4776 os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method,
4777                          JavaCallArguments* args, Thread* thread) {
4778   f(value, method, args, thread);
4779 }




1665       if (pelements[i] != NULL) {
1666         FREE_C_HEAP_ARRAY(char, pelements[i], mtInternal);
1667       }
1668     }
1669     if (pelements != NULL) {
1670       FREE_C_HEAP_ARRAY(char*, pelements, mtInternal);
1671     }
1672   } else {
1673     snprintf(buffer, buflen, "%s/lib%s.so", pname, fname);
1674     retval = true;
1675   }
1676   return retval;
1677 }
1678 
1679 // check if addr is inside libjvm.so
1680 bool os::address_is_in_vm(address addr) {
1681   static address libjvm_base_addr;
1682   Dl_info dlinfo;
1683 
1684   if (libjvm_base_addr == NULL) {
1685     if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo) != 0) {
1686       libjvm_base_addr = (address)dlinfo.dli_fbase;
1687     }
1688     assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");
1689   }
1690 
1691   if (dladdr((void *)addr, &dlinfo) != 0) {
1692     if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
1693   }
1694 
1695   return false;
1696 }
1697 
1698 bool os::dll_address_to_function_name(address addr, char *buf,
1699                                       int buflen, int *offset) {
1700   // buf is not optional, but offset is optional
1701   assert(buf != NULL, "sanity check");
1702 
1703   Dl_info dlinfo;
1704 
1705   if (dladdr((void*)addr, &dlinfo) != 0) {
1706     // see if we have a matching symbol
1707     if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {
1708       if (!Decoder::demangle(dlinfo.dli_sname, buf, buflen)) {
1709         jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
1710       }

1711       if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
1712       return true;
1713     }
1714     // no matching symbol so try for just file info
1715     if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {
1716       if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
1717                           buf, buflen, offset, dlinfo.dli_fname)) {
1718         return true;
1719       }
1720     }
1721   }
1722 
1723   buf[0] = '\0';
1724   if (offset != NULL) *offset = -1;
1725   return false;
1726 }
1727 
1728 struct _address_to_library_name {
1729   address addr;          // input : memory address
1730   size_t  buflen;        //         size of fname
1731   char*   fname;         // output: library name
1732   address base;          //         library base addr
1733 };
1734 
1735 static int address_to_library_name_callback(struct dl_phdr_info *info,
1736                                             size_t size, void *data) {
1737   int i;
1738   bool found = false;
1739   address libbase = NULL;
1740   struct _address_to_library_name * d = (struct _address_to_library_name *)data;
1741 
1742   // iterate through all loadable segments
1743   for (i = 0; i < info->dlpi_phnum; i++) {


1754         found = true;
1755       }
1756     }
1757   }
1758 
1759   // dlpi_name is NULL or empty if the ELF file is executable, return 0
1760   // so dll_address_to_library_name() can fall through to use dladdr() which
1761   // can figure out executable name from argv[0].
1762   if (found && info->dlpi_name && info->dlpi_name[0]) {
1763     d->base = libbase;
1764     if (d->fname) {
1765       jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name);
1766     }
1767     return 1;
1768   }
1769   return 0;
1770 }
1771 
1772 bool os::dll_address_to_library_name(address addr, char* buf,
1773                                      int buflen, int* offset) {
1774   // buf is not optional, but offset is optional
1775   assert(buf != NULL, "sanity check");
1776 
1777   Dl_info dlinfo;
1778   struct _address_to_library_name data;
1779 
1780   // There is a bug in old glibc dladdr() implementation that it could resolve
1781   // to wrong library name if the .so file has a base address != NULL. Here
1782   // we iterate through the program headers of all loaded libraries to find
1783   // out which library 'addr' really belongs to. This workaround can be
1784   // removed once the minimum requirement for glibc is moved to 2.3.x.
1785   data.addr = addr;
1786   data.fname = buf;
1787   data.buflen = buflen;
1788   data.base = NULL;
1789   int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data);
1790 
1791   if (rslt) {
1792      // buf already contains library name
1793      if (offset) *offset = addr - data.base;
1794      return true;
1795   } else if (dladdr((void*)addr, &dlinfo) != 0) {
1796      if (dlinfo.dli_fname != NULL) {
1797        jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
1798      }
1799      if (dlinfo.dli_fbase != NULL && offset != NULL) {
1800        *offset = addr - (address)dlinfo.dli_fbase;
1801      }
1802      return true;
1803   } else {
1804      buf[0] = '\0';
1805      if (offset) *offset = -1;
1806      return false;
1807   }
1808 }
1809 
1810   // Loads .dll/.so and
1811   // in case of error it checks if .dll/.so was built for the
1812   // same architecture as Hotspot is running on
1813 
1814 
1815 // Remember the stack's state. The Linux dynamic linker will change
1816 // the stack to 'executable' at most once, so we must safepoint only once.
1817 bool os::Linux::_stack_is_executable = false;
1818 
1819 // VM operation that loads a library.  This is necessary if stack protection
1820 // of the Java stacks can be lost during loading the library.  If we
1821 // do not stop the Java threads, they can stack overflow before the stacks
1822 // are protected again.
1823 class VM_LinuxDllLoad: public VM_Operation {
1824  private:


2314 static char saved_jvm_path[MAXPATHLEN] = {0};
2315 
2316 // Find the full path to the current module, libjvm.so
2317 void os::jvm_path(char *buf, jint buflen) {
2318   // Error checking.
2319   if (buflen < MAXPATHLEN) {
2320     assert(false, "must use a large-enough buffer");
2321     buf[0] = '\0';
2322     return;
2323   }
2324   // Lazy resolve the path to current module.
2325   if (saved_jvm_path[0] != 0) {
2326     strcpy(buf, saved_jvm_path);
2327     return;
2328   }
2329 
2330   char dli_fname[MAXPATHLEN];
2331   bool ret = dll_address_to_library_name(
2332                 CAST_FROM_FN_PTR(address, os::jvm_path),
2333                 dli_fname, sizeof(dli_fname), NULL);
2334   assert(ret, "cannot locate libjvm");
2335   char *rp = NULL;
2336   if (ret && dli_fname[0] != '\0') {
2337     rp = realpath(dli_fname, buf);
2338   }
2339   if (rp == NULL)
2340     return;
2341 
2342   if (Arguments::created_by_gamma_launcher()) {
2343     // Support for the gamma launcher.  Typical value for buf is
2344     // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so".  If "/jre/lib/" appears at
2345     // the right place in the string, then assume we are installed in a JDK and
2346     // we're done.  Otherwise, check for a JAVA_HOME environment variable and fix
2347     // up the path so it looks like libjvm.so is installed there (append a
2348     // fake suffix hotspot/libjvm.so).
2349     const char *p = buf + strlen(buf) - 1;
2350     for (int count = 0; p > buf && count < 5; ++count) {
2351       for (--p; p > buf && *p != '/'; --p)
2352         /* empty */ ;
2353     }
2354 
2355     if (strncmp(p, "/jre/lib/", 9) != 0) {
2356       // Look for JAVA_HOME in the environment.
2357       char* java_home_var = ::getenv("JAVA_HOME");
2358       if (java_home_var != NULL && java_home_var[0] != 0) {


4730 {
4731    if (is_NPTL()) {
4732       return pthread_cond_timedwait(_cond, _mutex, _abstime);
4733    } else {
4734       // 6292965: LinuxThreads pthread_cond_timedwait() resets FPU control
4735       // word back to default 64bit precision if condvar is signaled. Java
4736       // wants 53bit precision.  Save and restore current value.
4737       int fpu = get_fpu_control_word();
4738       int status = pthread_cond_timedwait(_cond, _mutex, _abstime);
4739       set_fpu_control_word(fpu);
4740       return status;
4741    }
4742 }
4743 
4744 ////////////////////////////////////////////////////////////////////////////////
4745 // debug support
4746 
4747 bool os::find(address addr, outputStream* st) {
4748   Dl_info dlinfo;
4749   memset(&dlinfo, 0, sizeof(dlinfo));
4750   if (dladdr(addr, &dlinfo) != 0) {
4751     st->print(PTR_FORMAT ": ", addr);
4752     if (dlinfo.dli_sname != NULL && dlinfo.dli_saddr != NULL) {
4753       st->print("%s+%#x", dlinfo.dli_sname,
4754                  addr - (intptr_t)dlinfo.dli_saddr);
4755     } else if (dlinfo.dli_fbase != NULL) {
4756       st->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);
4757     } else {
4758       st->print("<absolute address>");
4759     }
4760     if (dlinfo.dli_fname != NULL) {
4761       st->print(" in %s", dlinfo.dli_fname);
4762     }
4763     if (dlinfo.dli_fbase != NULL) {
4764       st->print(" at " PTR_FORMAT, dlinfo.dli_fbase);
4765     }
4766     st->cr();
4767 
4768     if (Verbose) {
4769       // decode some bytes around the PC
4770       address begin = clamp_address_in_page(addr-40, addr, os::vm_page_size());
4771       address end   = clamp_address_in_page(addr+40, addr, os::vm_page_size());
4772       address       lowest = (address) dlinfo.dli_sname;
4773       if (!lowest)  lowest = (address) dlinfo.dli_fbase;
4774       if (begin < lowest)  begin = lowest;
4775       Dl_info dlinfo2;
4776       if (dladdr(end, &dlinfo2) != 0 && dlinfo2.dli_saddr != dlinfo.dli_saddr
4777           && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin)
4778         end = (address) dlinfo2.dli_saddr;
4779       Disassembler::decode(begin, end, st);
4780     }
4781     return true;
4782   }
4783   return false;
4784 }
4785 
4786 ////////////////////////////////////////////////////////////////////////////////
4787 // misc
4788 
4789 // This does not do anything on Linux. This is basically a hook for being
4790 // able to use structured exception handling (thread-local exception filters)
4791 // on, e.g., Win32.
4792 void
4793 os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method,
4794                          JavaCallArguments* args, Thread* thread) {
4795   f(value, method, args, thread);
4796 }