< prev index next >

hotspot/src/os/linux/vm/os_linux.cpp

Print this page




 201 // i386: 224, ia64: 1105, amd64: 186, sparc 143
 202   #ifdef __ia64__
 203     #define SYS_gettid 1105
 204   #else
 205     #ifdef __i386__
 206       #define SYS_gettid 224
 207     #else
 208       #ifdef __amd64__
 209         #define SYS_gettid 186
 210       #else
 211         #ifdef __sparc__
 212           #define SYS_gettid 143
 213         #else
 214           #error define gettid for the arch
 215         #endif
 216       #endif
 217     #endif
 218   #endif
 219 #endif
 220 
 221 // Cpu architecture string
 222 static char cpu_arch[] = HOTSPOT_LIB_ARCH;
 223 
 224 
 225 // pid_t gettid()
 226 //
 227 // Returns the kernel thread id of the currently running thread. Kernel
 228 // thread id is used to access /proc.
 229 pid_t os::Linux::gettid() {
 230   int rslt = syscall(SYS_gettid);
 231   assert(rslt != -1, "must be."); // old linuxthreads implementation?
 232   return (pid_t)rslt;
 233 }
 234 
 235 // Most versions of linux have a bug where the number of processors are
 236 // determined by looking at the /proc file system.  In a chroot environment,
 237 // the system call returns 1.  This causes the VM to act as if it is
 238 // a single processor and elide locking (see is_MP() call).
 239 static bool unsafe_chroot_detected = false;
 240 static const char *unstable_chroot_error = "/proc file system not found.\n"
 241                      "Java may be unstable running multithreaded in a chroot "
 242                      "environment on Linux when /proc filesystem is not mounted.";
 243 


 246   if (processor_count() == 1) {
 247     pid_t pid = os::Linux::gettid();
 248     char fname[32];
 249     jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);
 250     FILE *fp = fopen(fname, "r");
 251     if (fp == NULL) {
 252       unsafe_chroot_detected = true;
 253     } else {
 254       fclose(fp);
 255     }
 256   }
 257   _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
 258   assert(processor_count() > 0, "linux error");
 259 }
 260 
 261 void os::init_system_properties_values() {
 262   // The next steps are taken in the product version:
 263   //
 264   // Obtain the JAVA_HOME value from the location of libjvm.so.
 265   // This library should be located at:
 266   // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so.
 267   //
 268   // If "/jre/lib/" appears at the right place in the path, then we
 269   // assume libjvm.so is installed in a JDK and we use this path.
 270   //
 271   // Otherwise exit with message: "Could not create the Java virtual machine."
 272   //
 273   // The following extra steps are taken in the debugging version:
 274   //
 275   // If "/jre/lib/" does NOT appear at the right place in the path
 276   // instead of exit check for $JAVA_HOME environment variable.
 277   //
 278   // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
 279   // then we append a fake suffix "hotspot/libjvm.so" to this path so
 280   // it looks like libjvm.so is installed there
 281   // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so.
 282   //
 283   // Otherwise exit.
 284   //
 285   // Important note: if the location of libjvm.so changes this
 286   // code needs to be changed accordingly.


 312   // sysclasspath, java_home, dll_dir
 313   {
 314     char *pslash;
 315     os::jvm_path(buf, bufsize);
 316 
 317     // Found the full path to libjvm.so.
 318     // Now cut the path to <java_home>/jre if we can.
 319     pslash = strrchr(buf, '/');
 320     if (pslash != NULL) {
 321       *pslash = '\0';            // Get rid of /libjvm.so.
 322     }
 323     pslash = strrchr(buf, '/');
 324     if (pslash != NULL) {
 325       *pslash = '\0';            // Get rid of /{client|server|hotspot}.
 326     }
 327     Arguments::set_dll_dir(buf);
 328 
 329     if (pslash != NULL) {
 330       pslash = strrchr(buf, '/');
 331       if (pslash != NULL) {
 332         *pslash = '\0';          // Get rid of /<arch>.
 333         pslash = strrchr(buf, '/');
 334         if (pslash != NULL) {
 335           *pslash = '\0';        // Get rid of /lib.
 336         }
 337       }
 338     }
 339     Arguments::set_java_home(buf);
 340     set_boot_path('/', ':');
 341   }
 342 
 343   // Where to look for native libraries.
 344   //
 345   // Note: Due to a legacy implementation, most of the library path
 346   // is set in the launcher. This was to accomodate linking restrictions
 347   // on legacy Linux implementations (which are no longer supported).
 348   // Eventually, all the library path setting will be done here.
 349   //
 350   // However, to prevent the proliferation of improperly built native
 351   // libraries, the new path component /usr/java/packages is added here.
 352   // Eventually, all the library path setting will be done here.
 353   {
 354     // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
 355     // should always exist (until the legacy problem cited above is
 356     // addressed).
 357     const char *v = ::getenv("LD_LIBRARY_PATH");
 358     const char *v_colon = ":";
 359     if (v == NULL) { v = ""; v_colon = ""; }
 360     // That's +1 for the colon and +1 for the trailing '\0'.
 361     char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char,
 362                                                      strlen(v) + 1 +
 363                                                      sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH) + 1,
 364                                                      mtInternal);
 365     sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, v, v_colon, cpu_arch);
 366     Arguments::set_library_path(ld_library_path);
 367     FREE_C_HEAP_ARRAY(char, ld_library_path);
 368   }
 369 
 370   // Extensions directories.
 371   sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
 372   Arguments::set_ext_dirs(buf);
 373 
 374   FREE_C_HEAP_ARRAY(char, buf);
 375 
 376 #undef DEFAULT_LIBPATH
 377 #undef SYS_EXT_DIR
 378 #undef EXTENSIONS_DIR
 379 }
 380 
 381 ////////////////////////////////////////////////////////////////////////////////
 382 // breakpoint support
 383 
 384 void os::breakpoint() {
 385   BREAKPOINT;


2293   if (saved_jvm_path[0] != 0) {
2294     strcpy(buf, saved_jvm_path);
2295     return;
2296   }
2297 
2298   char dli_fname[MAXPATHLEN];
2299   bool ret = dll_address_to_library_name(
2300                                          CAST_FROM_FN_PTR(address, os::jvm_path),
2301                                          dli_fname, sizeof(dli_fname), NULL);
2302   assert(ret, "cannot locate libjvm");
2303   char *rp = NULL;
2304   if (ret && dli_fname[0] != '\0') {
2305     rp = realpath(dli_fname, buf);
2306   }
2307   if (rp == NULL) {
2308     return;
2309   }
2310 
2311   if (Arguments::sun_java_launcher_is_altjvm()) {
2312     // Support for the java launcher's '-XXaltjvm=<path>' option. Typical
2313     // value for buf is "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so".
2314     // If "/jre/lib/" appears at the right place in the string, then
2315     // assume we are installed in a JDK and we're done. Otherwise, check
2316     // for a JAVA_HOME environment variable and fix up the path so it
2317     // looks like libjvm.so is installed there (append a fake suffix
2318     // hotspot/libjvm.so).
2319     const char *p = buf + strlen(buf) - 1;
2320     for (int count = 0; p > buf && count < 5; ++count) {
2321       for (--p; p > buf && *p != '/'; --p)
2322         /* empty */ ;
2323     }
2324 
2325     if (strncmp(p, "/jre/lib/", 9) != 0) {
2326       // Look for JAVA_HOME in the environment.
2327       char* java_home_var = ::getenv("JAVA_HOME");
2328       if (java_home_var != NULL && java_home_var[0] != 0) {
2329         char* jrelib_p;
2330         int len;
2331 
2332         // Check the current module name "libjvm.so".
2333         p = strrchr(buf, '/');
2334         if (p == NULL) {
2335           return;
2336         }
2337         assert(strstr(p, "/libjvm") == p, "invalid library name");
2338 
2339         rp = realpath(java_home_var, buf);
2340         if (rp == NULL) {
2341           return;
2342         }
2343 
2344         // determine if this is a legacy image or modules image
2345         // modules image doesn't have "jre" subdirectory
2346         len = strlen(buf);
2347         assert(len < buflen, "Ran out of buffer room");
2348         jrelib_p = buf + len;
2349         snprintf(jrelib_p, buflen-len, "/jre/lib/%s", cpu_arch);
2350         if (0 != access(buf, F_OK)) {
2351           snprintf(jrelib_p, buflen-len, "/lib/%s", cpu_arch);
2352         }
2353 
2354         if (0 == access(buf, F_OK)) {
2355           // Use current module name "libjvm.so"
2356           len = strlen(buf);
2357           snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
2358         } else {
2359           // Go back to path of .so
2360           rp = realpath(dli_fname, buf);
2361           if (rp == NULL) {
2362             return;
2363           }
2364         }
2365       }
2366     }
2367   }
2368 
2369   strncpy(saved_jvm_path, buf, MAXPATHLEN);
2370   saved_jvm_path[MAXPATHLEN - 1] = '\0';
2371 }




 201 // i386: 224, ia64: 1105, amd64: 186, sparc 143
 202   #ifdef __ia64__
 203     #define SYS_gettid 1105
 204   #else
 205     #ifdef __i386__
 206       #define SYS_gettid 224
 207     #else
 208       #ifdef __amd64__
 209         #define SYS_gettid 186
 210       #else
 211         #ifdef __sparc__
 212           #define SYS_gettid 143
 213         #else
 214           #error define gettid for the arch
 215         #endif
 216       #endif
 217     #endif
 218   #endif
 219 #endif
 220 



 221 
 222 // pid_t gettid()
 223 //
 224 // Returns the kernel thread id of the currently running thread. Kernel
 225 // thread id is used to access /proc.
 226 pid_t os::Linux::gettid() {
 227   int rslt = syscall(SYS_gettid);
 228   assert(rslt != -1, "must be."); // old linuxthreads implementation?
 229   return (pid_t)rslt;
 230 }
 231 
 232 // Most versions of linux have a bug where the number of processors are
 233 // determined by looking at the /proc file system.  In a chroot environment,
 234 // the system call returns 1.  This causes the VM to act as if it is
 235 // a single processor and elide locking (see is_MP() call).
 236 static bool unsafe_chroot_detected = false;
 237 static const char *unstable_chroot_error = "/proc file system not found.\n"
 238                      "Java may be unstable running multithreaded in a chroot "
 239                      "environment on Linux when /proc filesystem is not mounted.";
 240 


 243   if (processor_count() == 1) {
 244     pid_t pid = os::Linux::gettid();
 245     char fname[32];
 246     jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);
 247     FILE *fp = fopen(fname, "r");
 248     if (fp == NULL) {
 249       unsafe_chroot_detected = true;
 250     } else {
 251       fclose(fp);
 252     }
 253   }
 254   _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
 255   assert(processor_count() > 0, "linux error");
 256 }
 257 
 258 void os::init_system_properties_values() {
 259   // The next steps are taken in the product version:
 260   //
 261   // Obtain the JAVA_HOME value from the location of libjvm.so.
 262   // This library should be located at:
 263   // <JAVA_HOME>/lib/{client|server}/libjvm.so.
 264   //
 265   // If "/jre/lib/" appears at the right place in the path, then we
 266   // assume libjvm.so is installed in a JDK and we use this path.
 267   //
 268   // Otherwise exit with message: "Could not create the Java virtual machine."
 269   //
 270   // The following extra steps are taken in the debugging version:
 271   //
 272   // If "/jre/lib/" does NOT appear at the right place in the path
 273   // instead of exit check for $JAVA_HOME environment variable.
 274   //
 275   // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
 276   // then we append a fake suffix "hotspot/libjvm.so" to this path so
 277   // it looks like libjvm.so is installed there
 278   // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so.
 279   //
 280   // Otherwise exit.
 281   //
 282   // Important note: if the location of libjvm.so changes this
 283   // code needs to be changed accordingly.


 309   // sysclasspath, java_home, dll_dir
 310   {
 311     char *pslash;
 312     os::jvm_path(buf, bufsize);
 313 
 314     // Found the full path to libjvm.so.
 315     // Now cut the path to <java_home>/jre if we can.
 316     pslash = strrchr(buf, '/');
 317     if (pslash != NULL) {
 318       *pslash = '\0';            // Get rid of /libjvm.so.
 319     }
 320     pslash = strrchr(buf, '/');
 321     if (pslash != NULL) {
 322       *pslash = '\0';            // Get rid of /{client|server|hotspot}.
 323     }
 324     Arguments::set_dll_dir(buf);
 325 
 326     if (pslash != NULL) {
 327       pslash = strrchr(buf, '/');
 328       if (pslash != NULL) {



 329         *pslash = '\0';        // Get rid of /lib.
 330       }
 331     }

 332     Arguments::set_java_home(buf);
 333     set_boot_path('/', ':');
 334   }
 335 
 336   // Where to look for native libraries.
 337   //
 338   // Note: Due to a legacy implementation, most of the library path
 339   // is set in the launcher. This was to accomodate linking restrictions
 340   // on legacy Linux implementations (which are no longer supported).
 341   // Eventually, all the library path setting will be done here.
 342   //
 343   // However, to prevent the proliferation of improperly built native
 344   // libraries, the new path component /usr/java/packages is added here.
 345   // Eventually, all the library path setting will be done here.
 346   {
 347     // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
 348     // should always exist (until the legacy problem cited above is
 349     // addressed).
 350     const char *v = ::getenv("LD_LIBRARY_PATH");
 351     const char *v_colon = ":";
 352     if (v == NULL) { v = ""; v_colon = ""; }
 353     // That's +1 for the colon and +1 for the trailing '\0'.
 354     char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char,
 355                                                      strlen(v) + 1 +
 356                                                      sizeof(SYS_EXT_DIR) + sizeof("/lib/") + sizeof(DEFAULT_LIBPATH) + 1,
 357                                                      mtInternal);
 358     sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib:" DEFAULT_LIBPATH, v, v_colon);
 359     Arguments::set_library_path(ld_library_path);
 360     FREE_C_HEAP_ARRAY(char, ld_library_path);
 361   }
 362 
 363   // Extensions directories.
 364   sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
 365   Arguments::set_ext_dirs(buf);
 366 
 367   FREE_C_HEAP_ARRAY(char, buf);
 368 
 369 #undef DEFAULT_LIBPATH
 370 #undef SYS_EXT_DIR
 371 #undef EXTENSIONS_DIR
 372 }
 373 
 374 ////////////////////////////////////////////////////////////////////////////////
 375 // breakpoint support
 376 
 377 void os::breakpoint() {
 378   BREAKPOINT;


2286   if (saved_jvm_path[0] != 0) {
2287     strcpy(buf, saved_jvm_path);
2288     return;
2289   }
2290 
2291   char dli_fname[MAXPATHLEN];
2292   bool ret = dll_address_to_library_name(
2293                                          CAST_FROM_FN_PTR(address, os::jvm_path),
2294                                          dli_fname, sizeof(dli_fname), NULL);
2295   assert(ret, "cannot locate libjvm");
2296   char *rp = NULL;
2297   if (ret && dli_fname[0] != '\0') {
2298     rp = realpath(dli_fname, buf);
2299   }
2300   if (rp == NULL) {
2301     return;
2302   }
2303 
2304   if (Arguments::sun_java_launcher_is_altjvm()) {
2305     // Support for the java launcher's '-XXaltjvm=<path>' option. Typical
2306     // value for buf is "<JAVA_HOME>/jre/lib/<vmtype>/libjvm.so".
2307     // If "/jre/lib/" appears at the right place in the string, then
2308     // assume we are installed in a JDK and we're done. Otherwise, check
2309     // for a JAVA_HOME environment variable and fix up the path so it
2310     // looks like libjvm.so is installed there (append a fake suffix
2311     // hotspot/libjvm.so).
2312     const char *p = buf + strlen(buf) - 1;
2313     for (int count = 0; p > buf && count < 5; ++count) {
2314       for (--p; p > buf && *p != '/'; --p)
2315         /* empty */ ;
2316     }
2317 
2318     if (strncmp(p, "/jre/lib/", 9) != 0) {
2319       // Look for JAVA_HOME in the environment.
2320       char* java_home_var = ::getenv("JAVA_HOME");
2321       if (java_home_var != NULL && java_home_var[0] != 0) {
2322         char* jrelib_p;
2323         int len;
2324 
2325         // Check the current module name "libjvm.so".
2326         p = strrchr(buf, '/');
2327         if (p == NULL) {
2328           return;
2329         }
2330         assert(strstr(p, "/libjvm") == p, "invalid library name");
2331 
2332         rp = realpath(java_home_var, buf);
2333         if (rp == NULL) {
2334           return;
2335         }
2336 
2337         // determine if this is a legacy image or modules image
2338         // modules image doesn't have "jre" subdirectory
2339         len = strlen(buf);
2340         assert(len < buflen, "Ran out of buffer room");
2341         jrelib_p = buf + len;
2342         snprintf(jrelib_p, buflen-len, "/jre/lib");
2343         if (0 != access(buf, F_OK)) {
2344           snprintf(jrelib_p, buflen-len, "/lib");
2345         }
2346 
2347         if (0 == access(buf, F_OK)) {
2348           // Use current module name "libjvm.so"
2349           len = strlen(buf);
2350           snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
2351         } else {
2352           // Go back to path of .so
2353           rp = realpath(dli_fname, buf);
2354           if (rp == NULL) {
2355             return;
2356           }
2357         }
2358       }
2359     }
2360   }
2361 
2362   strncpy(saved_jvm_path, buf, MAXPATHLEN);
2363   saved_jvm_path[MAXPATHLEN - 1] = '\0';
2364 }


< prev index next >