1 /* 2 * Copyright (c) 1999, 2014, 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 "utilities/globalDefinitions.hpp" 26 #include "prims/jvm.h" 27 #include "runtime/frame.inline.hpp" 28 #include "runtime/interfaceSupport.hpp" 29 #include "runtime/os.hpp" 30 #include "utilities/vmError.hpp" 31 32 #include <signal.h> 33 #include <unistd.h> 34 #include <sys/resource.h> 35 #include <sys/utsname.h> 36 #include <pthread.h> 37 #include <signal.h> 38 39 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 40 41 // Todo: provide a os::get_max_process_id() or similar. Number of processes 42 // may have been configured, can be read more accurately from proc fs etc. 43 #ifndef MAX_PID 44 #define MAX_PID INT_MAX 45 #endif 46 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID) 47 48 // Check core dump limit and report possible place where core can be found 49 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) { 50 int n; 51 struct rlimit rlim; 52 bool success; 53 54 n = get_core_path(buffer, bufferSize); 55 56 if (getrlimit(RLIMIT_CORE, &rlim) != 0) { 57 jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (may not exist)", current_process_id()); 58 success = true; 59 } else { 60 switch(rlim.rlim_cur) { 61 case RLIM_INFINITY: 62 jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d", current_process_id()); 63 success = true; 64 break; 65 66 case 0: 67 { 68 jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again"); 69 70 #ifdef LINUX 71 /* 72 * Max length of /proc/sys/kernel/core_pattern is 128 characters. 73 * See https://www.kernel.org/doc/Documentation/sysctl/kernel.txt 74 */ 75 const int core_pattern_len = 129; 76 77 char core_pattern[core_pattern_len] = {0}; 78 bool is_redirect = false; 79 FILE *core_pattern_file = fopen("/proc/sys/kernel/core_pattern", "r"); 80 if(core_pattern_file != NULL){ 81 fgets(core_pattern, core_pattern_len, core_pattern_file); 82 fclose(core_pattern_file); 83 is_redirect = core_pattern[0] == '|'; 84 } 85 86 if(is_redirect){ 87 jio_snprintf(buffer, bufferSize, 88 "Core dumps may be treated with \"%s\"", &core_pattern[1]); 89 } 90 #endif 91 92 success = false; 93 break; 94 } 95 96 default: 97 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)); 98 success = true; 99 break; 100 } 101 } 102 VMError::report_coredump_status(buffer, success); 103 } 104 105 int os::get_native_stack(address* stack, int frames, int toSkip) { 106 #ifdef _NMT_NOINLINE_ 107 toSkip++; 108 #endif 109 110 int frame_idx = 0; 111 int num_of_frames; // number of frames captured 112 frame fr = os::current_frame(); 113 while (fr.pc() && frame_idx < frames) { 114 if (toSkip > 0) { 115 toSkip --; 116 } else { 117 stack[frame_idx ++] = fr.pc(); 118 } 119 if (fr.fp() == NULL || os::is_first_C_frame(&fr) 120 ||fr.sender_pc() == NULL || fr.cb() != NULL) break; 121 122 if (fr.sender_pc() && !os::is_first_C_frame(&fr)) { 123 fr = os::get_sender_for_C_frame(&fr); 124 } else { 125 break; 126 } 127 } 128 num_of_frames = frame_idx; 129 for (; frame_idx < frames; frame_idx ++) { 130 stack[frame_idx] = NULL; 131 } 132 133 return num_of_frames; 134 } 135 136 137 bool os::unsetenv(const char* name) { 138 assert(name != NULL, "Null pointer"); 139 return (::unsetenv(name) == 0); 140 } 141 142 int os::get_last_error() { 143 return errno; 144 } 145 146 bool os::is_debugger_attached() { 147 // not implemented 148 return false; 149 } 150 151 void os::wait_for_keypress_at_exit(void) { 152 // don't do anything on posix platforms 153 return; 154 } 155 156 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED, 157 // so on posix, unmap the section at the start and at the end of the chunk that we mapped 158 // rather than unmapping and remapping the whole chunk to get requested alignment. 159 char* os::reserve_memory_aligned(size_t size, size_t alignment) { 160 assert((alignment & (os::vm_allocation_granularity() - 1)) == 0, 161 "Alignment must be a multiple of allocation granularity (page size)"); 162 assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned"); 163 164 size_t extra_size = size + alignment; 165 assert(extra_size >= size, "overflow, size is too large to allow alignment"); 166 167 char* extra_base = os::reserve_memory(extra_size, NULL, alignment); 168 169 if (extra_base == NULL) { 170 return NULL; 171 } 172 173 // Do manual alignment 174 char* aligned_base = (char*) align_size_up((uintptr_t) extra_base, alignment); 175 176 // [ | | ] 177 // ^ extra_base 178 // ^ extra_base + begin_offset == aligned_base 179 // extra_base + begin_offset + size ^ 180 // extra_base + extra_size ^ 181 // |<>| == begin_offset 182 // end_offset == |<>| 183 size_t begin_offset = aligned_base - extra_base; 184 size_t end_offset = (extra_base + extra_size) - (aligned_base + size); 185 186 if (begin_offset > 0) { 187 os::release_memory(extra_base, begin_offset); 188 } 189 190 if (end_offset > 0) { 191 os::release_memory(extra_base + begin_offset + size, end_offset); 192 } 193 194 return aligned_base; 195 } 196 197 void os::Posix::print_load_average(outputStream* st) { 198 st->print("load average:"); 199 double loadavg[3]; 200 os::loadavg(loadavg, 3); 201 st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]); 202 st->cr(); 203 } 204 205 void os::Posix::print_rlimit_info(outputStream* st) { 206 st->print("rlimit:"); 207 struct rlimit rlim; 208 209 st->print(" STACK "); 210 getrlimit(RLIMIT_STACK, &rlim); 211 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 212 else st->print("%uk", rlim.rlim_cur >> 10); 213 214 st->print(", CORE "); 215 getrlimit(RLIMIT_CORE, &rlim); 216 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 217 else st->print("%uk", rlim.rlim_cur >> 10); 218 219 // Isn't there on solaris 220 #if !defined(TARGET_OS_FAMILY_solaris) && !defined(TARGET_OS_FAMILY_aix) 221 st->print(", NPROC "); 222 getrlimit(RLIMIT_NPROC, &rlim); 223 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 224 else st->print("%d", rlim.rlim_cur); 225 #endif 226 227 st->print(", NOFILE "); 228 getrlimit(RLIMIT_NOFILE, &rlim); 229 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 230 else st->print("%d", rlim.rlim_cur); 231 232 st->print(", AS "); 233 getrlimit(RLIMIT_AS, &rlim); 234 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 235 else st->print("%uk", rlim.rlim_cur >> 10); 236 st->cr(); 237 } 238 239 void os::Posix::print_uname_info(outputStream* st) { 240 // kernel 241 st->print("uname:"); 242 struct utsname name; 243 uname(&name); 244 st->print("%s ", name.sysname); 245 #ifdef ASSERT 246 st->print("%s ", name.nodename); 247 #endif 248 st->print("%s ", name.release); 249 st->print("%s ", name.version); 250 st->print("%s", name.machine); 251 st->cr(); 252 } 253 254 bool os::has_allocatable_memory_limit(julong* limit) { 255 struct rlimit rlim; 256 int getrlimit_res = getrlimit(RLIMIT_AS, &rlim); 257 // if there was an error when calling getrlimit, assume that there is no limitation 258 // on virtual memory. 259 bool result; 260 if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) { 261 result = false; 262 } else { 263 *limit = (julong)rlim.rlim_cur; 264 result = true; 265 } 266 #ifdef _LP64 267 return result; 268 #else 269 // arbitrary virtual space limit for 32 bit Unices found by testing. If 270 // getrlimit above returned a limit, bound it with this limit. Otherwise 271 // directly use it. 272 const julong max_virtual_limit = (julong)3800*M; 273 if (result) { 274 *limit = MIN2(*limit, max_virtual_limit); 275 } else { 276 *limit = max_virtual_limit; 277 } 278 279 // bound by actually allocatable memory. The algorithm uses two bounds, an 280 // upper and a lower limit. The upper limit is the current highest amount of 281 // memory that could not be allocated, the lower limit is the current highest 282 // amount of memory that could be allocated. 283 // The algorithm iteratively refines the result by halving the difference 284 // between these limits, updating either the upper limit (if that value could 285 // not be allocated) or the lower limit (if the that value could be allocated) 286 // until the difference between these limits is "small". 287 288 // the minimum amount of memory we care about allocating. 289 const julong min_allocation_size = M; 290 291 julong upper_limit = *limit; 292 293 // first check a few trivial cases 294 if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) { 295 *limit = upper_limit; 296 } else if (!is_allocatable(min_allocation_size)) { 297 // we found that not even min_allocation_size is allocatable. Return it 298 // anyway. There is no point to search for a better value any more. 299 *limit = min_allocation_size; 300 } else { 301 // perform the binary search. 302 julong lower_limit = min_allocation_size; 303 while ((upper_limit - lower_limit) > min_allocation_size) { 304 julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit; 305 temp_limit = align_size_down_(temp_limit, min_allocation_size); 306 if (is_allocatable(temp_limit)) { 307 lower_limit = temp_limit; 308 } else { 309 upper_limit = temp_limit; 310 } 311 } 312 *limit = lower_limit; 313 } 314 return true; 315 #endif 316 } 317 318 const char* os::get_current_directory(char *buf, size_t buflen) { 319 return getcwd(buf, buflen); 320 } 321 322 FILE* os::open(int fd, const char* mode) { 323 return ::fdopen(fd, mode); 324 } 325 326 // Builds a platform dependent Agent_OnLoad_<lib_name> function name 327 // which is used to find statically linked in agents. 328 // Parameters: 329 // sym_name: Symbol in library we are looking for 330 // lib_name: Name of library to look in, NULL for shared libs. 331 // is_absolute_path == true if lib_name is absolute path to agent 332 // such as "/a/b/libL.so" 333 // == false if only the base name of the library is passed in 334 // such as "L" 335 char* os::build_agent_function_name(const char *sym_name, const char *lib_name, 336 bool is_absolute_path) { 337 char *agent_entry_name; 338 size_t len; 339 size_t name_len; 340 size_t prefix_len = strlen(JNI_LIB_PREFIX); 341 size_t suffix_len = strlen(JNI_LIB_SUFFIX); 342 const char *start; 343 344 if (lib_name != NULL) { 345 len = name_len = strlen(lib_name); 346 if (is_absolute_path) { 347 // Need to strip path, prefix and suffix 348 if ((start = strrchr(lib_name, *os::file_separator())) != NULL) { 349 lib_name = ++start; 350 } 351 if (len <= (prefix_len + suffix_len)) { 352 return NULL; 353 } 354 lib_name += prefix_len; 355 name_len = strlen(lib_name) - suffix_len; 356 } 357 } 358 len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2; 359 agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread); 360 if (agent_entry_name == NULL) { 361 return NULL; 362 } 363 strcpy(agent_entry_name, sym_name); 364 if (lib_name != NULL) { 365 strcat(agent_entry_name, "_"); 366 strncat(agent_entry_name, lib_name, name_len); 367 } 368 return agent_entry_name; 369 } 370 371 int os::sleep(Thread* thread, jlong millis, bool interruptible) { 372 assert(thread == Thread::current(), "thread consistency check"); 373 374 ParkEvent * const slp = thread->_SleepEvent ; 375 slp->reset() ; 376 OrderAccess::fence() ; 377 378 if (interruptible) { 379 jlong prevtime = javaTimeNanos(); 380 381 for (;;) { 382 if (os::is_interrupted(thread, true)) { 383 return OS_INTRPT; 384 } 385 386 jlong newtime = javaTimeNanos(); 387 388 if (newtime - prevtime < 0) { 389 // time moving backwards, should only happen if no monotonic clock 390 // not a guarantee() because JVM should not abort on kernel/glibc bugs 391 assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected in os::sleep(interruptible)"); 392 } else { 393 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC; 394 } 395 396 if (millis <= 0) { 397 return OS_OK; 398 } 399 400 prevtime = newtime; 401 402 { 403 assert(thread->is_Java_thread(), "sanity check"); 404 JavaThread *jt = (JavaThread *) thread; 405 ThreadBlockInVM tbivm(jt); 406 OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */); 407 408 jt->set_suspend_equivalent(); 409 // cleared by handle_special_suspend_equivalent_condition() or 410 // java_suspend_self() via check_and_wait_while_suspended() 411 412 slp->park(millis); 413 414 // were we externally suspended while we were waiting? 415 jt->check_and_wait_while_suspended(); 416 } 417 } 418 } else { 419 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 420 jlong prevtime = javaTimeNanos(); 421 422 for (;;) { 423 // It'd be nice to avoid the back-to-back javaTimeNanos() calls on 424 // the 1st iteration ... 425 jlong newtime = javaTimeNanos(); 426 427 if (newtime - prevtime < 0) { 428 // time moving backwards, should only happen if no monotonic clock 429 // not a guarantee() because JVM should not abort on kernel/glibc bugs 430 assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected on os::sleep(!interruptible)"); 431 } else { 432 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC; 433 } 434 435 if (millis <= 0) break ; 436 437 prevtime = newtime; 438 slp->park(millis); 439 } 440 return OS_OK ; 441 } 442 } 443 444 //////////////////////////////////////////////////////////////////////////////// 445 // interrupt support 446 447 void os::interrupt(Thread* thread) { 448 assert(Thread::current() == thread || Threads_lock->owned_by_self(), 449 "possibility of dangling Thread pointer"); 450 451 OSThread* osthread = thread->osthread(); 452 453 if (!osthread->interrupted()) { 454 osthread->set_interrupted(true); 455 // More than one thread can get here with the same value of osthread, 456 // resulting in multiple notifications. We do, however, want the store 457 // to interrupted() to be visible to other threads before we execute unpark(). 458 OrderAccess::fence(); 459 ParkEvent * const slp = thread->_SleepEvent ; 460 if (slp != NULL) slp->unpark() ; 461 } 462 463 // For JSR166. Unpark even if interrupt status already was set 464 if (thread->is_Java_thread()) 465 ((JavaThread*)thread)->parker()->unpark(); 466 467 ParkEvent * ev = thread->_ParkEvent ; 468 if (ev != NULL) ev->unpark() ; 469 470 } 471 472 bool os::is_interrupted(Thread* thread, bool clear_interrupted) { 473 assert(Thread::current() == thread || Threads_lock->owned_by_self(), 474 "possibility of dangling Thread pointer"); 475 476 OSThread* osthread = thread->osthread(); 477 478 bool interrupted = osthread->interrupted(); 479 480 // NOTE that since there is no "lock" around the interrupt and 481 // is_interrupted operations, there is the possibility that the 482 // interrupted flag (in osThread) will be "false" but that the 483 // low-level events will be in the signaled state. This is 484 // intentional. The effect of this is that Object.wait() and 485 // LockSupport.park() will appear to have a spurious wakeup, which 486 // is allowed and not harmful, and the possibility is so rare that 487 // it is not worth the added complexity to add yet another lock. 488 // For the sleep event an explicit reset is performed on entry 489 // to os::sleep, so there is no early return. It has also been 490 // recommended not to put the interrupted flag into the "event" 491 // structure because it hides the issue. 492 if (interrupted && clear_interrupted) { 493 osthread->set_interrupted(false); 494 // consider thread->_SleepEvent->reset() ... optional optimization 495 } 496 497 return interrupted; 498 } 499 500 // Returned string is a constant. For unknown signals "UNKNOWN" is returned. 501 const char* os::Posix::get_signal_name(int sig, char* out, size_t outlen) { 502 503 static const struct { 504 int sig; const char* name; 505 } 506 info[] = 507 { 508 { SIGABRT, "SIGABRT" }, 509 #ifdef SIGAIO 510 { SIGAIO, "SIGAIO" }, 511 #endif 512 { SIGALRM, "SIGALRM" }, 513 #ifdef SIGALRM1 514 { SIGALRM1, "SIGALRM1" }, 515 #endif 516 { SIGBUS, "SIGBUS" }, 517 #ifdef SIGCANCEL 518 { SIGCANCEL, "SIGCANCEL" }, 519 #endif 520 { SIGCHLD, "SIGCHLD" }, 521 #ifdef SIGCLD 522 { SIGCLD, "SIGCLD" }, 523 #endif 524 { SIGCONT, "SIGCONT" }, 525 #ifdef SIGCPUFAIL 526 { SIGCPUFAIL, "SIGCPUFAIL" }, 527 #endif 528 #ifdef SIGDANGER 529 { SIGDANGER, "SIGDANGER" }, 530 #endif 531 #ifdef SIGDIL 532 { SIGDIL, "SIGDIL" }, 533 #endif 534 #ifdef SIGEMT 535 { SIGEMT, "SIGEMT" }, 536 #endif 537 { SIGFPE, "SIGFPE" }, 538 #ifdef SIGFREEZE 539 { SIGFREEZE, "SIGFREEZE" }, 540 #endif 541 #ifdef SIGGFAULT 542 { SIGGFAULT, "SIGGFAULT" }, 543 #endif 544 #ifdef SIGGRANT 545 { SIGGRANT, "SIGGRANT" }, 546 #endif 547 { SIGHUP, "SIGHUP" }, 548 { SIGILL, "SIGILL" }, 549 { SIGINT, "SIGINT" }, 550 #ifdef SIGIO 551 { SIGIO, "SIGIO" }, 552 #endif 553 #ifdef SIGIOINT 554 { SIGIOINT, "SIGIOINT" }, 555 #endif 556 #ifdef SIGIOT 557 // SIGIOT is there for BSD compatibility, but on most Unices just a 558 // synonym for SIGABRT. The result should be "SIGABRT", not 559 // "SIGIOT". 560 #if (SIGIOT != SIGABRT ) 561 { SIGIOT, "SIGIOT" }, 562 #endif 563 #endif 564 #ifdef SIGKAP 565 { SIGKAP, "SIGKAP" }, 566 #endif 567 { SIGKILL, "SIGKILL" }, 568 #ifdef SIGLOST 569 { SIGLOST, "SIGLOST" }, 570 #endif 571 #ifdef SIGLWP 572 { SIGLWP, "SIGLWP" }, 573 #endif 574 #ifdef SIGLWPTIMER 575 { SIGLWPTIMER, "SIGLWPTIMER" }, 576 #endif 577 #ifdef SIGMIGRATE 578 { SIGMIGRATE, "SIGMIGRATE" }, 579 #endif 580 #ifdef SIGMSG 581 { SIGMSG, "SIGMSG" }, 582 #endif 583 { SIGPIPE, "SIGPIPE" }, 584 #ifdef SIGPOLL 585 { SIGPOLL, "SIGPOLL" }, 586 #endif 587 #ifdef SIGPRE 588 { SIGPRE, "SIGPRE" }, 589 #endif 590 { SIGPROF, "SIGPROF" }, 591 #ifdef SIGPTY 592 { SIGPTY, "SIGPTY" }, 593 #endif 594 #ifdef SIGPWR 595 { SIGPWR, "SIGPWR" }, 596 #endif 597 { SIGQUIT, "SIGQUIT" }, 598 #ifdef SIGRECONFIG 599 { SIGRECONFIG, "SIGRECONFIG" }, 600 #endif 601 #ifdef SIGRECOVERY 602 { SIGRECOVERY, "SIGRECOVERY" }, 603 #endif 604 #ifdef SIGRESERVE 605 { SIGRESERVE, "SIGRESERVE" }, 606 #endif 607 #ifdef SIGRETRACT 608 { SIGRETRACT, "SIGRETRACT" }, 609 #endif 610 #ifdef SIGSAK 611 { SIGSAK, "SIGSAK" }, 612 #endif 613 { SIGSEGV, "SIGSEGV" }, 614 #ifdef SIGSOUND 615 { SIGSOUND, "SIGSOUND" }, 616 #endif 617 { SIGSTOP, "SIGSTOP" }, 618 { SIGSYS, "SIGSYS" }, 619 #ifdef SIGSYSERROR 620 { SIGSYSERROR, "SIGSYSERROR" }, 621 #endif 622 #ifdef SIGTALRM 623 { SIGTALRM, "SIGTALRM" }, 624 #endif 625 { SIGTERM, "SIGTERM" }, 626 #ifdef SIGTHAW 627 { SIGTHAW, "SIGTHAW" }, 628 #endif 629 { SIGTRAP, "SIGTRAP" }, 630 #ifdef SIGTSTP 631 { SIGTSTP, "SIGTSTP" }, 632 #endif 633 { SIGTTIN, "SIGTTIN" }, 634 { SIGTTOU, "SIGTTOU" }, 635 #ifdef SIGURG 636 { SIGURG, "SIGURG" }, 637 #endif 638 { SIGUSR1, "SIGUSR1" }, 639 { SIGUSR2, "SIGUSR2" }, 640 #ifdef SIGVIRT 641 { SIGVIRT, "SIGVIRT" }, 642 #endif 643 { SIGVTALRM, "SIGVTALRM" }, 644 #ifdef SIGWAITING 645 { SIGWAITING, "SIGWAITING" }, 646 #endif 647 #ifdef SIGWINCH 648 { SIGWINCH, "SIGWINCH" }, 649 #endif 650 #ifdef SIGWINDOW 651 { SIGWINDOW, "SIGWINDOW" }, 652 #endif 653 { SIGXCPU, "SIGXCPU" }, 654 { SIGXFSZ, "SIGXFSZ" }, 655 #ifdef SIGXRES 656 { SIGXRES, "SIGXRES" }, 657 #endif 658 { -1, NULL } 659 }; 660 661 const char* ret = NULL; 662 663 #ifdef SIGRTMIN 664 if (sig >= SIGRTMIN && sig <= SIGRTMAX) { 665 if (sig == SIGRTMIN) { 666 ret = "SIGRTMIN"; 667 } else if (sig == SIGRTMAX) { 668 ret = "SIGRTMAX"; 669 } else { 670 jio_snprintf(out, outlen, "SIGRTMIN+%d", sig - SIGRTMIN); 671 return out; 672 } 673 } 674 #endif 675 676 if (sig > 0) { 677 for (int idx = 0; info[idx].sig != -1; idx ++) { 678 if (info[idx].sig == sig) { 679 ret = info[idx].name; 680 break; 681 } 682 } 683 } 684 685 if (!ret) { 686 if (!is_valid_signal(sig)) { 687 ret = "INVALID"; 688 } else { 689 ret = "UNKNOWN"; 690 } 691 } 692 693 jio_snprintf(out, outlen, ret); 694 return out; 695 } 696 697 // Returns true if signal number is valid. 698 bool os::Posix::is_valid_signal(int sig) { 699 // MacOS not really POSIX compliant: sigaddset does not return 700 // an error for invalid signal numbers. However, MacOS does not 701 // support real time signals and simply seems to have just 33 702 // signals with no holes in the signal range. 703 #ifdef __APPLE__ 704 return sig >= 1 && sig < NSIG; 705 #else 706 // Use sigaddset to check for signal validity. 707 sigset_t set; 708 if (sigaddset(&set, sig) == -1 && errno == EINVAL) { 709 return false; 710 } 711 return true; 712 #endif 713 } 714 715 #define NUM_IMPORTANT_SIGS 32 716 // Returns one-line short description of a signal set in a user provided buffer. 717 const char* os::Posix::describe_signal_set_short(const sigset_t* set, char* buffer, size_t buf_size) { 718 assert(buf_size == (NUM_IMPORTANT_SIGS + 1), "wrong buffer size"); 719 // Note: for shortness, just print out the first 32. That should 720 // cover most of the useful ones, apart from realtime signals. 721 for (int sig = 1; sig <= NUM_IMPORTANT_SIGS; sig++) { 722 const int rc = sigismember(set, sig); 723 if (rc == -1 && errno == EINVAL) { 724 buffer[sig-1] = '?'; 725 } else { 726 buffer[sig-1] = rc == 0 ? '0' : '1'; 727 } 728 } 729 buffer[NUM_IMPORTANT_SIGS] = 0; 730 return buffer; 731 } 732 733 // Prints one-line description of a signal set. 734 void os::Posix::print_signal_set_short(outputStream* st, const sigset_t* set) { 735 char buf[NUM_IMPORTANT_SIGS + 1]; 736 os::Posix::describe_signal_set_short(set, buf, sizeof(buf)); 737 st->print("%s", buf); 738 } 739 740 // Writes one-line description of a combination of sigaction.sa_flags into a user 741 // provided buffer. Returns that buffer. 742 const char* os::Posix::describe_sa_flags(int flags, char* buffer, size_t size) { 743 char* p = buffer; 744 size_t remaining = size; 745 bool first = true; 746 int idx = 0; 747 748 assert(buffer, "invalid argument"); 749 750 if (size == 0) { 751 return buffer; 752 } 753 754 strncpy(buffer, "none", size); 755 756 const struct { 757 int i; 758 const char* s; 759 } flaginfo [] = { 760 { SA_NOCLDSTOP, "SA_NOCLDSTOP" }, 761 { SA_ONSTACK, "SA_ONSTACK" }, 762 { SA_RESETHAND, "SA_RESETHAND" }, 763 { SA_RESTART, "SA_RESTART" }, 764 { SA_SIGINFO, "SA_SIGINFO" }, 765 { SA_NOCLDWAIT, "SA_NOCLDWAIT" }, 766 { SA_NODEFER, "SA_NODEFER" }, 767 #ifdef AIX 768 { SA_ONSTACK, "SA_ONSTACK" }, 769 { SA_OLDSTYLE, "SA_OLDSTYLE" }, 770 #endif 771 { 0, NULL } 772 }; 773 774 for (idx = 0; flaginfo[idx].s && remaining > 1; idx++) { 775 if (flags & flaginfo[idx].i) { 776 if (first) { 777 jio_snprintf(p, remaining, "%s", flaginfo[idx].s); 778 first = false; 779 } else { 780 jio_snprintf(p, remaining, "|%s", flaginfo[idx].s); 781 } 782 const size_t len = strlen(p); 783 p += len; 784 remaining -= len; 785 } 786 } 787 788 buffer[size - 1] = '\0'; 789 790 return buffer; 791 } 792 793 // Prints one-line description of a combination of sigaction.sa_flags. 794 void os::Posix::print_sa_flags(outputStream* st, int flags) { 795 char buffer[0x100]; 796 os::Posix::describe_sa_flags(flags, buffer, sizeof(buffer)); 797 st->print("%s", buffer); 798 } 799 800 // Helper function for os::Posix::print_siginfo_...(): 801 // return a textual description for signal code. 802 struct enum_sigcode_desc_t { 803 const char* s_name; 804 const char* s_desc; 805 }; 806 807 static bool get_signal_code_description(const siginfo_t* si, enum_sigcode_desc_t* out) { 808 809 const struct { 810 int sig; int code; const char* s_code; const char* s_desc; 811 } t1 [] = { 812 { SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode." }, 813 { SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand." }, 814 { SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode." }, 815 { SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap." }, 816 { SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode." }, 817 { SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register." }, 818 { SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error." }, 819 { SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error." }, 820 #if defined(IA64) && defined(LINUX) 821 { SIGILL, ILL_BADIADDR, "ILL_BADIADDR", "Unimplemented instruction address" }, 822 { SIGILL, ILL_BREAK, "ILL_BREAK", "Application Break instruction" }, 823 #endif 824 { SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero." }, 825 { SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow." }, 826 { SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating-point divide by zero." }, 827 { SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating-point overflow." }, 828 { SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating-point underflow." }, 829 { SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating-point inexact result." }, 830 { SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating-point operation." }, 831 { SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range." }, 832 { SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object." }, 833 { SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for mapped object." }, 834 #ifdef AIX 835 // no explanation found what keyerr would be 836 { SIGSEGV, SEGV_KEYERR, "SEGV_KEYERR", "key error" }, 837 #endif 838 #if defined(IA64) && !defined(AIX) 839 { SIGSEGV, SEGV_PSTKOVF, "SEGV_PSTKOVF", "Paragraph stack overflow" }, 840 #endif 841 { SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment." }, 842 { SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Nonexistent physical address." }, 843 { SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object-specific hardware error." }, 844 { SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint." }, 845 { SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap." }, 846 { SIGCHLD, CLD_EXITED, "CLD_EXITED", "Child has exited." }, 847 { SIGCHLD, CLD_KILLED, "CLD_KILLED", "Child has terminated abnormally and did not create a core file." }, 848 { SIGCHLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally and created a core file." }, 849 { SIGCHLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped." }, 850 { SIGCHLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped." }, 851 { SIGCHLD, CLD_CONTINUED,"CLD_CONTINUED","Stopped child has continued." }, 852 #ifdef SIGPOLL 853 { SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available." }, 854 { SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available." }, 855 { SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error." }, 856 { SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available." }, 857 { SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected. [Option End]" }, 858 #endif 859 { -1, -1, NULL, NULL } 860 }; 861 862 // Codes valid in any signal context. 863 const struct { 864 int code; const char* s_code; const char* s_desc; 865 } t2 [] = { 866 { SI_USER, "SI_USER", "Signal sent by kill()." }, 867 { SI_QUEUE, "SI_QUEUE", "Signal sent by the sigqueue()." }, 868 { SI_TIMER, "SI_TIMER", "Signal generated by expiration of a timer set by timer_settime()." }, 869 { SI_ASYNCIO, "SI_ASYNCIO", "Signal generated by completion of an asynchronous I/O request." }, 870 { SI_MESGQ, "SI_MESGQ", "Signal generated by arrival of a message on an empty message queue." }, 871 // Linux specific 872 #ifdef SI_TKILL 873 { SI_TKILL, "SI_TKILL", "Signal sent by tkill (pthread_kill)" }, 874 #endif 875 #ifdef SI_DETHREAD 876 { SI_DETHREAD, "SI_DETHREAD", "Signal sent by execve() killing subsidiary threads" }, 877 #endif 878 #ifdef SI_KERNEL 879 { SI_KERNEL, "SI_KERNEL", "Signal sent by kernel." }, 880 #endif 881 #ifdef SI_SIGIO 882 { SI_SIGIO, "SI_SIGIO", "Signal sent by queued SIGIO" }, 883 #endif 884 885 #ifdef AIX 886 { SI_UNDEFINED, "SI_UNDEFINED","siginfo contains partial information" }, 887 { SI_EMPTY, "SI_EMPTY", "siginfo contains no useful information" }, 888 #endif 889 890 #ifdef __sun 891 { SI_NOINFO, "SI_NOINFO", "No signal information" }, 892 { SI_RCTL, "SI_RCTL", "kernel generated signal via rctl action" }, 893 { SI_LWP, "SI_LWP", "Signal sent via lwp_kill" }, 894 #endif 895 896 { -1, NULL, NULL } 897 }; 898 899 const char* s_code = NULL; 900 const char* s_desc = NULL; 901 902 for (int i = 0; t1[i].sig != -1; i ++) { 903 if (t1[i].sig == si->si_signo && t1[i].code == si->si_code) { 904 s_code = t1[i].s_code; 905 s_desc = t1[i].s_desc; 906 break; 907 } 908 } 909 910 if (s_code == NULL) { 911 for (int i = 0; t2[i].s_code != NULL; i ++) { 912 if (t2[i].code == si->si_code) { 913 s_code = t2[i].s_code; 914 s_desc = t2[i].s_desc; 915 } 916 } 917 } 918 919 if (s_code == NULL) { 920 out->s_name = "unknown"; 921 out->s_desc = "unknown"; 922 return false; 923 } 924 925 out->s_name = s_code; 926 out->s_desc = s_desc; 927 928 return true; 929 } 930 931 // A POSIX conform, platform-independend siginfo print routine. 932 // Short print out on one line. 933 void os::Posix::print_siginfo_brief(outputStream* os, const siginfo_t* si) { 934 char buf[20]; 935 os->print("siginfo: "); 936 937 if (!si) { 938 os->print("<null>"); 939 return; 940 } 941 942 // See print_siginfo_full() for details. 943 const int sig = si->si_signo; 944 945 os->print("si_signo: %d (%s)", sig, os::Posix::get_signal_name(sig, buf, sizeof(buf))); 946 947 enum_sigcode_desc_t ed; 948 if (get_signal_code_description(si, &ed)) { 949 os->print(", si_code: %d (%s)", si->si_code, ed.s_name); 950 } else { 951 os->print(", si_code: %d (unknown)", si->si_code); 952 } 953 954 if (si->si_errno) { 955 os->print(", si_errno: %d", si->si_errno); 956 } 957 958 const int me = (int) ::getpid(); 959 const int pid = (int) si->si_pid; 960 961 if (si->si_code == SI_USER || si->si_code == SI_QUEUE) { 962 if (IS_VALID_PID(pid) && pid != me) { 963 os->print(", sent from pid: %d (uid: %d)", pid, (int) si->si_uid); 964 } 965 } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 966 sig == SIGTRAP || sig == SIGFPE) { 967 os->print(", si_addr: " PTR_FORMAT, si->si_addr); 968 #ifdef SIGPOLL 969 } else if (sig == SIGPOLL) { 970 os->print(", si_band: " PTR64_FORMAT, (uint64_t)si->si_band); 971 #endif 972 } else if (sig == SIGCHLD) { 973 os->print_cr(", si_pid: %d, si_uid: %d, si_status: %d", (int) si->si_pid, si->si_uid, si->si_status); 974 } 975 } 976 977 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() { 978 assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread"); 979 } 980 981 /* 982 * See the caveats for this class in os_posix.hpp 983 * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this 984 * method and returns false. If none of the signals are raised, returns true. 985 * The callback is supposed to provide the method that should be protected. 986 */ 987 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) { 988 sigset_t saved_sig_mask; 989 990 assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread"); 991 assert(!WatcherThread::watcher_thread()->has_crash_protection(), 992 "crash_protection already set?"); 993 994 // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask 995 // since on at least some systems (OS X) siglongjmp will restore the mask 996 // for the process, not the thread 997 pthread_sigmask(0, NULL, &saved_sig_mask); 998 if (sigsetjmp(_jmpbuf, 0) == 0) { 999 // make sure we can see in the signal handler that we have crash protection 1000 // installed 1001 WatcherThread::watcher_thread()->set_crash_protection(this); 1002 cb.call(); 1003 // and clear the crash protection 1004 WatcherThread::watcher_thread()->set_crash_protection(NULL); 1005 return true; 1006 } 1007 // this happens when we siglongjmp() back 1008 pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL); 1009 WatcherThread::watcher_thread()->set_crash_protection(NULL); 1010 return false; 1011 } 1012 1013 void os::WatcherThreadCrashProtection::restore() { 1014 assert(WatcherThread::watcher_thread()->has_crash_protection(), 1015 "must have crash protection"); 1016 1017 siglongjmp(_jmpbuf, 1); 1018 } 1019 1020 void os::WatcherThreadCrashProtection::check_crash_protection(int sig, 1021 Thread* thread) { 1022 1023 if (thread != NULL && 1024 thread->is_Watcher_thread() && 1025 WatcherThread::watcher_thread()->has_crash_protection()) { 1026 1027 if (sig == SIGSEGV || sig == SIGBUS) { 1028 WatcherThread::watcher_thread()->crash_protection()->restore(); 1029 } 1030 } 1031 }