1 /* 2 * Copyright (c) 1999, 2013, 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 #include <pthread.h> 34 #include <signal.h> 35 36 37 // Check core dump limit and report possible place where core can be found 38 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) { 39 int n; 40 struct rlimit rlim; 41 bool success; 42 43 n = get_core_path(buffer, bufferSize); 44 45 if (getrlimit(RLIMIT_CORE, &rlim) != 0) { 46 jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (may not exist)", current_process_id()); 47 success = true; 48 } else { 49 switch(rlim.rlim_cur) { 50 case RLIM_INFINITY: 51 jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d", current_process_id()); 52 success = true; 53 break; 54 case 0: 55 jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again"); 56 success = false; 57 break; 58 default: 59 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)); 60 success = true; 61 break; 62 } 63 } 64 VMError::report_coredump_status(buffer, success); 65 } 66 67 address os::get_caller_pc(int n) { 68 #ifdef _NMT_NOINLINE_ 69 n ++; 70 #endif 71 frame fr = os::current_frame(); 72 while (n > 0 && fr.pc() && 73 !os::is_first_C_frame(&fr) && fr.sender_pc()) { 74 fr = os::get_sender_for_C_frame(&fr); 75 n --; 76 } 77 if (n == 0) { 78 return fr.pc(); 79 } else { 80 return NULL; 81 } 82 } 83 84 int os::get_last_error() { 85 return errno; 86 } 87 88 bool os::is_debugger_attached() { 89 // not implemented 90 return false; 91 } 92 93 void os::wait_for_keypress_at_exit(void) { 94 // don't do anything on posix platforms 95 return; 96 } 97 98 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED, 99 // so on posix, unmap the section at the start and at the end of the chunk that we mapped 100 // rather than unmapping and remapping the whole chunk to get requested alignment. 101 char* os::reserve_memory_aligned(size_t size, size_t alignment) { 102 assert((alignment & (os::vm_allocation_granularity() - 1)) == 0, 103 "Alignment must be a multiple of allocation granularity (page size)"); 104 assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned"); 105 106 size_t extra_size = size + alignment; 107 assert(extra_size >= size, "overflow, size is too large to allow alignment"); 108 109 char* extra_base = os::reserve_memory(extra_size, NULL, alignment); 110 111 if (extra_base == NULL) { 112 return NULL; 113 } 114 115 // Do manual alignment 116 char* aligned_base = (char*) align_size_up((uintptr_t) extra_base, alignment); 117 118 // [ | | ] 119 // ^ extra_base 120 // ^ extra_base + begin_offset == aligned_base 121 // extra_base + begin_offset + size ^ 122 // extra_base + extra_size ^ 123 // |<>| == begin_offset 124 // end_offset == |<>| 125 size_t begin_offset = aligned_base - extra_base; 126 size_t end_offset = (extra_base + extra_size) - (aligned_base + size); 127 128 if (begin_offset > 0) { 129 os::release_memory(extra_base, begin_offset); 130 } 131 132 if (end_offset > 0) { 133 os::release_memory(extra_base + begin_offset + size, end_offset); 134 } 135 136 return aligned_base; 137 } 138 139 void os::Posix::print_load_average(outputStream* st) { 140 st->print("load average:"); 141 double loadavg[3]; 142 os::loadavg(loadavg, 3); 143 st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]); 144 st->cr(); 145 } 146 147 void os::Posix::print_rlimit_info(outputStream* st) { 148 st->print("rlimit:"); 149 struct rlimit rlim; 150 151 st->print(" STACK "); 152 getrlimit(RLIMIT_STACK, &rlim); 153 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 154 else st->print("%uk", rlim.rlim_cur >> 10); 155 156 st->print(", CORE "); 157 getrlimit(RLIMIT_CORE, &rlim); 158 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 159 else st->print("%uk", rlim.rlim_cur >> 10); 160 161 //Isn't there on solaris 162 #ifndef TARGET_OS_FAMILY_solaris 163 st->print(", NPROC "); 164 getrlimit(RLIMIT_NPROC, &rlim); 165 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 166 else st->print("%d", rlim.rlim_cur); 167 #endif 168 169 st->print(", NOFILE "); 170 getrlimit(RLIMIT_NOFILE, &rlim); 171 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 172 else st->print("%d", rlim.rlim_cur); 173 174 st->print(", AS "); 175 getrlimit(RLIMIT_AS, &rlim); 176 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 177 else st->print("%uk", rlim.rlim_cur >> 10); 178 st->cr(); 179 } 180 181 void os::Posix::print_uname_info(outputStream* st) { 182 // kernel 183 st->print("uname:"); 184 struct utsname name; 185 uname(&name); 186 st->print(name.sysname); st->print(" "); 187 st->print(name.release); st->print(" "); 188 st->print(name.version); st->print(" "); 189 st->print(name.machine); 190 st->cr(); 191 } 192 193 bool os::has_allocatable_memory_limit(julong* limit) { 194 struct rlimit rlim; 195 int getrlimit_res = getrlimit(RLIMIT_AS, &rlim); 196 // if there was an error when calling getrlimit, assume that there is no limitation 197 // on virtual memory. 198 bool result; 199 if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) { 200 result = false; 201 } else { 202 *limit = (julong)rlim.rlim_cur; 203 result = true; 204 } 205 #ifdef _LP64 206 return result; 207 #else 208 // arbitrary virtual space limit for 32 bit Unices found by testing. If 209 // getrlimit above returned a limit, bound it with this limit. Otherwise 210 // directly use it. 211 const julong max_virtual_limit = (julong)3800*M; 212 if (result) { 213 *limit = MIN2(*limit, max_virtual_limit); 214 } else { 215 *limit = max_virtual_limit; 216 } 217 218 // bound by actually allocatable memory. The algorithm uses two bounds, an 219 // upper and a lower limit. The upper limit is the current highest amount of 220 // memory that could not be allocated, the lower limit is the current highest 221 // amount of memory that could be allocated. 222 // The algorithm iteratively refines the result by halving the difference 223 // between these limits, updating either the upper limit (if that value could 224 // not be allocated) or the lower limit (if the that value could be allocated) 225 // until the difference between these limits is "small". 226 227 // the minimum amount of memory we care about allocating. 228 const julong min_allocation_size = M; 229 230 julong upper_limit = *limit; 231 232 // first check a few trivial cases 233 if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) { 234 *limit = upper_limit; 235 } else if (!is_allocatable(min_allocation_size)) { 236 // we found that not even min_allocation_size is allocatable. Return it 237 // anyway. There is no point to search for a better value any more. 238 *limit = min_allocation_size; 239 } else { 240 // perform the binary search. 241 julong lower_limit = min_allocation_size; 242 while ((upper_limit - lower_limit) > min_allocation_size) { 243 julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit; 244 temp_limit = align_size_down_(temp_limit, min_allocation_size); 245 if (is_allocatable(temp_limit)) { 246 lower_limit = temp_limit; 247 } else { 248 upper_limit = temp_limit; 249 } 250 } 251 *limit = lower_limit; 252 } 253 return true; 254 #endif 255 } 256 257 const char* os::get_current_directory(char *buf, size_t buflen) { 258 return getcwd(buf, buflen); 259 } 260 261 FILE* os::open(int fd, const char* mode) { 262 return ::fdopen(fd, mode); 263 } 264 265 // Builds a platform dependent Agent_OnLoad_<lib_name> function name 266 // which is used to find statically linked in agents. 267 // Parameters: 268 // sym_name: Symbol in library we are looking for 269 // lib_name: Name of library to look in, NULL for shared libs. 270 // is_absolute_path == true if lib_name is absolute path to agent 271 // such as "/a/b/libL.so" 272 // == false if only the base name of the library is passed in 273 // such as "L" 274 char* os::build_agent_function_name(const char *sym_name, const char *lib_name, 275 bool is_absolute_path) { 276 char *agent_entry_name; 277 size_t len; 278 size_t name_len; 279 size_t prefix_len = strlen(JNI_LIB_PREFIX); 280 size_t suffix_len = strlen(JNI_LIB_SUFFIX); 281 const char *start; 282 283 if (lib_name != NULL) { 284 len = name_len = strlen(lib_name); 285 if (is_absolute_path) { 286 // Need to strip path, prefix and suffix 287 if ((start = strrchr(lib_name, *os::file_separator())) != NULL) { 288 lib_name = ++start; 289 } 290 if (len <= (prefix_len + suffix_len)) { 291 return NULL; 292 } 293 lib_name += prefix_len; 294 name_len = strlen(lib_name) - suffix_len; 295 } 296 } 297 len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2; 298 agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread); 299 if (agent_entry_name == NULL) { 300 return NULL; 301 } 302 strcpy(agent_entry_name, sym_name); 303 if (lib_name != NULL) { 304 strcat(agent_entry_name, "_"); 305 strncat(agent_entry_name, lib_name, name_len); 306 } 307 return agent_entry_name; 308 } 309 310 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() { 311 assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread"); 312 } 313 314 /* 315 * See the caveats for this class in os_posix.hpp 316 * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this 317 * method and returns false. If none of the signals are raised, returns true. 318 * The callback is supposed to provide the method that should be protected. 319 */ 320 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) { 321 sigset_t saved_sig_mask; 322 323 assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread"); 324 assert(!WatcherThread::watcher_thread()->has_crash_protection(), 325 "crash_protection already set?"); 326 327 // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask 328 // since on at least some systems (OS X) siglongjmp will restore the mask 329 // for the process, not the thread 330 pthread_sigmask(0, NULL, &saved_sig_mask); 331 if (sigsetjmp(_jmpbuf, 0) == 0) { 332 // make sure we can see in the signal handler that we have crash protection 333 // installed 334 WatcherThread::watcher_thread()->set_crash_protection(this); 335 cb.call(); 336 // and clear the crash protection 337 WatcherThread::watcher_thread()->set_crash_protection(NULL); 338 return true; 339 } 340 // this happens when we siglongjmp() back 341 pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL); 342 WatcherThread::watcher_thread()->set_crash_protection(NULL); 343 return false; 344 } 345 346 void os::WatcherThreadCrashProtection::restore() { 347 assert(WatcherThread::watcher_thread()->has_crash_protection(), 348 "must have crash protection"); 349 350 siglongjmp(_jmpbuf, 1); 351 } 352 353 void os::WatcherThreadCrashProtection::check_crash_protection(int sig, 354 Thread* thread) { 355 356 if (thread != NULL && 357 thread->is_Watcher_thread() && 358 WatcherThread::watcher_thread()->has_crash_protection()) { 359 360 if (sig == SIGSEGV || sig == SIGBUS) { 361 WatcherThread::watcher_thread()->crash_protection()->restore(); 362 } 363 } 364 }