1 /*
   2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4 *
   5 * This code is free software; you can redistribute it and/or modify it
   6 * under the terms of the GNU General Public License version 2 only, as
   7 * published by the Free Software Foundation.
   8 *
   9 * This code is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 * version 2 for more details (a copy is included in the LICENSE file that
  13 * accompanied this code).
  14 *
  15 * You should have received a copy of the GNU General Public License version
  16 * 2 along with this work; if not, write to the Free Software Foundation,
  17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18 *
  19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20 * or visit www.oracle.com if you need additional information or have any
  21 * questions.
  22 *
  23 */
  24 
  25 #include "prims/jvm.h"
  26 #include "runtime/os.hpp"
  27 #include "utilities/vmError.hpp"
  28 
  29 #include <unistd.h>
  30 #include <sys/resource.h>
  31 #include <sys/utsname.h>
  32 
  33 // Generate the candidate core paths into the supplied buffer
  34 // Return the number of characters actually generated
  35 int os::Posix::generate_core_paths(char* buffer, size_t bufferSize) {
  36   int n;
  37 
  38 #ifndef __APPLE__
  39   static char cwd[O_BUFLEN];
  40 
  41   get_current_directory(cwd, sizeof(cwd));
  42 
  43   n = jio_snprintf(buffer, bufferSize, "%s/core or core.%d", cwd, current_process_id());
  44 #else
  45   n = jio_snprintf(buffer, bufferSize, "/cores/core.%d", current_process_id());
  46 #endif
  47 
  48   // Truncate if theoretical string was longer than bufferSize
  49   n = MIN2(n, (int)bufferSize);
  50 
  51   return n;
  52 }
  53 
  54 
  55 // Check core dump limit and report possible place where core can be found
  56 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) {
  57   struct rlimit rlim;
  58   bool success;
  59   int n;
  60 
  61   if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
  62     n = os::Posix::generate_core_paths(buffer, bufferSize);
  63     jio_snprintf(buffer + n, bufferSize - n, " (may not exist)");
  64     success = true;
  65   } else {
  66     switch(rlim.rlim_cur) {
  67       case RLIM_INFINITY:
  68         os::Posix::generate_core_paths(buffer, bufferSize);
  69         success = true;
  70         break;
  71       case 0:
  72         jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");
  73         success = false;
  74         break;
  75       default:
  76         n = os::Posix::generate_core_paths(buffer, bufferSize);
  77         jio_snprintf(buffer + n, bufferSize - n, " (max size %lu kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", (unsigned long)(rlim.rlim_cur >> 10));
  78         success = true;
  79         break;
  80     }
  81   }
  82   VMError::report_coredump_status(buffer, success);
  83 }
  84 
  85 int os::get_last_error() {
  86   return errno;
  87 }
  88 
  89 bool os::is_debugger_attached() {
  90   // not implemented
  91   return false;
  92 }
  93 
  94 void os::wait_for_keypress_at_exit(void) {
  95   // don't do anything on posix platforms
  96   return;
  97 }
  98 
  99 void os::Posix::print_load_average(outputStream* st) {
 100   st->print("load average:");
 101   double loadavg[3];
 102   os::loadavg(loadavg, 3);
 103   st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
 104   st->cr();
 105 }
 106 
 107 void os::Posix::print_rlimit_info(outputStream* st) {
 108   st->print("rlimit:");
 109   struct rlimit rlim;
 110 
 111   st->print(" STACK ");
 112   getrlimit(RLIMIT_STACK, &rlim);
 113   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 114   else st->print("%uk", rlim.rlim_cur >> 10);
 115 
 116   st->print(", CORE ");
 117   getrlimit(RLIMIT_CORE, &rlim);
 118   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 119   else st->print("%uk", rlim.rlim_cur >> 10);
 120 
 121   //Isn't there on solaris
 122 #ifndef TARGET_OS_FAMILY_solaris
 123   st->print(", NPROC ");
 124   getrlimit(RLIMIT_NPROC, &rlim);
 125   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 126   else st->print("%d", rlim.rlim_cur);
 127 #endif
 128 
 129   st->print(", NOFILE ");
 130   getrlimit(RLIMIT_NOFILE, &rlim);
 131   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 132   else st->print("%d", rlim.rlim_cur);
 133 
 134   st->print(", AS ");
 135   getrlimit(RLIMIT_AS, &rlim);
 136   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 137   else st->print("%uk", rlim.rlim_cur >> 10);
 138   st->cr();
 139 }
 140 
 141 void os::Posix::print_uname_info(outputStream* st) {
 142   // kernel
 143   st->print("uname:");
 144   struct utsname name;
 145   uname(&name);
 146   st->print(name.sysname); st->print(" ");
 147   st->print(name.release); st->print(" ");
 148   st->print(name.version); st->print(" ");
 149   st->print(name.machine);
 150   st->cr();
 151 }
 152 
 153