1 /*
   2 * Copyright (c) 1999, 2012, 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/frame.inline.hpp"
  27 #include "runtime/os.hpp"
  28 #include "utilities/vmError.hpp"
  29 
  30 #include <unistd.h>
  31 #include <sys/resource.h>
  32 #include <sys/utsname.h>
  33 
  34 
  35 // Check core dump limit and report possible place where core can be found
  36 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) {
  37   int n;
  38   struct rlimit rlim;
  39   bool success;
  40 
  41   n = get_core_path(buffer, bufferSize);
  42 
  43   if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
  44     jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (may not exist)", current_process_id());
  45     success = true;
  46   } else {
  47     switch(rlim.rlim_cur) {
  48       case RLIM_INFINITY:
  49         jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d", current_process_id());
  50         success = true;
  51         break;
  52       case 0:
  53         jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");
  54         success = false;
  55         break;
  56       default:
  57         jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (max size %lu kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", current_process_id(), (unsigned long)(rlim.rlim_cur >> 10));
  58         success = true;
  59         break;
  60     }
  61   }
  62   VMError::report_coredump_status(buffer, success);
  63 }
  64 
  65 address os::get_caller_pc(int n) {
  66 #ifdef _NMT_NOINLINE_
  67   n ++;
  68 #endif
  69   frame fr = os::current_frame();
  70   while (n > 0 && fr.pc() &&
  71     !os::is_first_C_frame(&fr) && fr.sender_pc()) {
  72     fr = os::get_sender_for_C_frame(&fr);
  73     n --;
  74   }
  75   if (n == 0) {
  76     return fr.pc();
  77   } else {
  78     return NULL;
  79   }
  80 }
  81 
  82 int os::get_last_error() {
  83   return errno;
  84 }
  85 
  86 bool os::is_debugger_attached() {
  87   // not implemented
  88   return false;
  89 }
  90 
  91 void os::wait_for_keypress_at_exit(void) {
  92   // don't do anything on posix platforms
  93   return;
  94 }
  95 
  96 char* os::reserve_memory_aligned(size_t size, size_t alignment) {
  97   assert(alignment & (os::vm_allocation_granularity() - 1) == 0,
  98       "Alignment must be a multiple of allocation granularity (page size)");
  99   assert(size & (alignment -1) == 0, "size must be 'alignment' aligned");
 100   size_t extra_size = size + alignment;
 101   char* extra_base = os::reserve_memory(extra_size, NULL, alignment);
 102 
 103   if (extra_base == NULL) {
 104     return NULL;
 105   }
 106 
 107   // Do manual alignment
 108   char* aligned_base = (char*) align_size_up((uintptr_t) extra_base, alignment);
 109 
 110   // [  |                                       |  ]
 111   // ^ extra_base
 112   //    ^ extra_base + begin_offset == aligned_base
 113   //     extra_base + begin_offset + size       ^
 114   //                       extra_base + extra_size ^
 115   // |<>| == begin_offset
 116   //                              end_offset == |<>|
 117   size_t begin_offset = aligned_base - extra_base;
 118   size_t end_offset = (extra_base + extra_size) - (aligned_base + size);
 119 
 120   if (begin_offset > 0) {
 121       os::release_memory(extra_base, begin_offset);
 122   }
 123 
 124   if (end_offset > 0) {
 125       os::release_memory(extra_base + begin_offset + size, end_offset);
 126   }
 127 
 128   return aligned_base;
 129 }
 130 
 131 void os::Posix::print_load_average(outputStream* st) {
 132   st->print("load average:");
 133   double loadavg[3];
 134   os::loadavg(loadavg, 3);
 135   st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
 136   st->cr();
 137 }
 138 
 139 void os::Posix::print_rlimit_info(outputStream* st) {
 140   st->print("rlimit:");
 141   struct rlimit rlim;
 142 
 143   st->print(" STACK ");
 144   getrlimit(RLIMIT_STACK, &rlim);
 145   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 146   else st->print("%uk", rlim.rlim_cur >> 10);
 147 
 148   st->print(", CORE ");
 149   getrlimit(RLIMIT_CORE, &rlim);
 150   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 151   else st->print("%uk", rlim.rlim_cur >> 10);
 152 
 153   //Isn't there on solaris
 154 #ifndef TARGET_OS_FAMILY_solaris
 155   st->print(", NPROC ");
 156   getrlimit(RLIMIT_NPROC, &rlim);
 157   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 158   else st->print("%d", rlim.rlim_cur);
 159 #endif
 160 
 161   st->print(", NOFILE ");
 162   getrlimit(RLIMIT_NOFILE, &rlim);
 163   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 164   else st->print("%d", rlim.rlim_cur);
 165 
 166   st->print(", AS ");
 167   getrlimit(RLIMIT_AS, &rlim);
 168   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 169   else st->print("%uk", rlim.rlim_cur >> 10);
 170   st->cr();
 171 }
 172 
 173 void os::Posix::print_uname_info(outputStream* st) {
 174   // kernel
 175   st->print("uname:");
 176   struct utsname name;
 177   uname(&name);
 178   st->print(name.sysname); st->print(" ");
 179   st->print(name.release); st->print(" ");
 180   st->print(name.version); st->print(" ");
 181   st->print(name.machine);
 182   st->cr();
 183 }
 184 
 185