ctornqvi@2520: /* kevinw@9478: * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. goetz@6460: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. goetz@6460: * goetz@6460: * This code is free software; you can redistribute it and/or modify it goetz@6460: * under the terms of the GNU General Public License version 2 only, as goetz@6460: * published by the Free Software Foundation. goetz@6460: * goetz@6460: * This code is distributed in the hope that it will be useful, but WITHOUT goetz@6460: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or goetz@6460: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License goetz@6460: * version 2 for more details (a copy is included in the LICENSE file that goetz@6460: * accompanied this code). goetz@6460: * goetz@6460: * You should have received a copy of the GNU General Public License version goetz@6460: * 2 along with this work; if not, write to the Free Software Foundation, goetz@6460: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. goetz@6460: * goetz@6460: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA goetz@6460: * or visit www.oracle.com if you need additional information or have any goetz@6460: * questions. goetz@6460: * goetz@6460: */ ctornqvi@2520: goetz@6460: #include "utilities/globalDefinitions.hpp" ctornqvi@2520: #include "prims/jvm.h" zgu@3900: #include "runtime/frame.inline.hpp" ctornqvi@2520: #include "runtime/os.hpp" ctornqvi@2520: #include "utilities/vmError.hpp" ctornqvi@2520: goetz@6460: #include ctornqvi@2520: #include ctornqvi@2520: #include nloodin@3783: #include sla@5609: #include sla@5609: #include nloodin@3783: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: goetz@6460: // Todo: provide a os::get_max_process_id() or similar. Number of processes goetz@6460: // may have been configured, can be read more accurately from proc fs etc. goetz@6460: #ifndef MAX_PID goetz@6460: #define MAX_PID INT_MAX goetz@6460: #endif goetz@6460: #define IS_VALID_PID(p) (p > 0 && p < MAX_PID) ctornqvi@2520: ctornqvi@2520: // Check core dump limit and report possible place where core can be found ctornqvi@2520: void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) { mikael@3903: int n; ctornqvi@2520: struct rlimit rlim; ctornqvi@2520: bool success; ctornqvi@2520: mikael@3903: n = get_core_path(buffer, bufferSize); ctornqvi@2520: ctornqvi@2520: if (getrlimit(RLIMIT_CORE, &rlim) != 0) { mikael@3903: jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (may not exist)", current_process_id()); ctornqvi@2520: success = true; ctornqvi@2520: } else { ctornqvi@2520: switch(rlim.rlim_cur) { ctornqvi@2520: case RLIM_INFINITY: mikael@3903: jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d", current_process_id()); ctornqvi@2520: success = true; ctornqvi@2520: break; ctornqvi@2520: case 0: ctornqvi@2520: jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again"); ctornqvi@2520: success = false; ctornqvi@2520: break; ctornqvi@2520: default: mikael@3903: 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)); ctornqvi@2520: success = true; ctornqvi@2520: break; ctornqvi@2520: } ctornqvi@2520: } ctornqvi@2520: VMError::report_coredump_status(buffer, success); ctornqvi@2520: } ctornqvi@2520: zgu@7074: int os::get_native_stack(address* stack, int frames, int toSkip) { zgu@3900: #ifdef _NMT_NOINLINE_ zgu@7074: toSkip++; zgu@3900: #endif zgu@7074: zgu@7074: int frame_idx = 0; zgu@7074: int num_of_frames; // number of frames captured zgu@3900: frame fr = os::current_frame(); zgu@7074: while (fr.pc() && frame_idx < frames) { zgu@7074: if (toSkip > 0) { zgu@7074: toSkip --; zgu@7074: } else { zgu@7074: stack[frame_idx ++] = fr.pc(); zgu@7074: } zgu@7074: if (fr.fp() == NULL || os::is_first_C_frame(&fr) zgu@7074: ||fr.sender_pc() == NULL || fr.cb() != NULL) break; zgu@7074: zgu@7074: if (fr.sender_pc() && !os::is_first_C_frame(&fr)) { zgu@7074: fr = os::get_sender_for_C_frame(&fr); zgu@7074: } else { zgu@7074: break; zgu@7074: } zgu@3900: } zgu@7074: num_of_frames = frame_idx; zgu@7074: for (; frame_idx < frames; frame_idx ++) { zgu@7074: stack[frame_idx] = NULL; zgu@3900: } zgu@7074: zgu@7074: return num_of_frames; zgu@7074: } zgu@7074: zgu@7074: zgu@7074: bool os::unsetenv(const char* name) { zgu@7074: assert(name != NULL, "Null pointer"); zgu@7074: return (::unsetenv(name) == 0); zgu@3900: } zgu@3900: phh@3379: int os::get_last_error() { phh@3379: return errno; phh@3379: } phh@3379: sla@2584: bool os::is_debugger_attached() { sla@2584: // not implemented sla@2584: return false; sla@2584: } sla@2584: sla@2584: void os::wait_for_keypress_at_exit(void) { sla@2584: // don't do anything on posix platforms sla@2584: return; sla@2584: } nloodin@3783: brutisso@4369: // Multiple threads can race in this code, and can remap over each other with MAP_FIXED, brutisso@4369: // so on posix, unmap the section at the start and at the end of the chunk that we mapped brutisso@4369: // rather than unmapping and remapping the whole chunk to get requested alignment. brutisso@4369: char* os::reserve_memory_aligned(size_t size, size_t alignment) { brutisso@4369: assert((alignment & (os::vm_allocation_granularity() - 1)) == 0, brutisso@4369: "Alignment must be a multiple of allocation granularity (page size)"); brutisso@4369: assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned"); brutisso@4369: brutisso@4369: size_t extra_size = size + alignment; brutisso@4369: assert(extra_size >= size, "overflow, size is too large to allow alignment"); brutisso@4369: brutisso@4369: char* extra_base = os::reserve_memory(extra_size, NULL, alignment); brutisso@4369: brutisso@4369: if (extra_base == NULL) { brutisso@4369: return NULL; brutisso@4369: } brutisso@4369: brutisso@4369: // Do manual alignment brutisso@4369: char* aligned_base = (char*) align_size_up((uintptr_t) extra_base, alignment); brutisso@4369: brutisso@4369: // [ | | ] brutisso@4369: // ^ extra_base brutisso@4369: // ^ extra_base + begin_offset == aligned_base brutisso@4369: // extra_base + begin_offset + size ^ brutisso@4369: // extra_base + extra_size ^ brutisso@4369: // |<>| == begin_offset brutisso@4369: // end_offset == |<>| brutisso@4369: size_t begin_offset = aligned_base - extra_base; brutisso@4369: size_t end_offset = (extra_base + extra_size) - (aligned_base + size); brutisso@4369: brutisso@4369: if (begin_offset > 0) { brutisso@4369: os::release_memory(extra_base, begin_offset); brutisso@4369: } brutisso@4369: brutisso@4369: if (end_offset > 0) { brutisso@4369: os::release_memory(extra_base + begin_offset + size, end_offset); brutisso@4369: } brutisso@4369: brutisso@4369: return aligned_base; brutisso@4369: } brutisso@4369: kevinw@9478: int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) { kevinw@9478: int result = ::vsnprintf(buf, len, fmt, args); kevinw@9478: // If an encoding error occurred (result < 0) then it's not clear kevinw@9478: // whether the buffer is NUL terminated, so ensure it is. kevinw@9478: if ((result < 0) && (len > 0)) { kevinw@9478: buf[len - 1] = '\0'; kevinw@9478: } kevinw@9478: return result; kevinw@9478: } kevinw@9478: nloodin@3783: void os::Posix::print_load_average(outputStream* st) { nloodin@3783: st->print("load average:"); nloodin@3783: double loadavg[3]; nloodin@3783: os::loadavg(loadavg, 3); nloodin@3783: st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]); nloodin@3783: st->cr(); nloodin@3783: } nloodin@3783: nloodin@3783: void os::Posix::print_rlimit_info(outputStream* st) { nloodin@3783: st->print("rlimit:"); nloodin@3783: struct rlimit rlim; nloodin@3783: nloodin@3783: st->print(" STACK "); nloodin@3783: getrlimit(RLIMIT_STACK, &rlim); nloodin@3783: if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); nloodin@3783: else st->print("%uk", rlim.rlim_cur >> 10); nloodin@3783: nloodin@3783: st->print(", CORE "); nloodin@3783: getrlimit(RLIMIT_CORE, &rlim); nloodin@3783: if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); nloodin@3783: else st->print("%uk", rlim.rlim_cur >> 10); nloodin@3783: goetz@6460: // Isn't there on solaris kvn@6462: #if !defined(TARGET_OS_FAMILY_solaris) && !defined(TARGET_OS_FAMILY_aix) nloodin@3783: st->print(", NPROC "); nloodin@3783: getrlimit(RLIMIT_NPROC, &rlim); nloodin@3783: if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); nloodin@3783: else st->print("%d", rlim.rlim_cur); nloodin@3783: #endif nloodin@3783: nloodin@3783: st->print(", NOFILE "); nloodin@3783: getrlimit(RLIMIT_NOFILE, &rlim); nloodin@3783: if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); nloodin@3783: else st->print("%d", rlim.rlim_cur); nloodin@3783: nloodin@3783: st->print(", AS "); nloodin@3783: getrlimit(RLIMIT_AS, &rlim); nloodin@3783: if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); nloodin@3783: else st->print("%uk", rlim.rlim_cur >> 10); nloodin@3783: st->cr(); nloodin@3783: } nloodin@3783: nloodin@3783: void os::Posix::print_uname_info(outputStream* st) { nloodin@3783: // kernel nloodin@3783: st->print("uname:"); nloodin@3783: struct utsname name; nloodin@3783: uname(&name); drchase@6680: st->print("%s ", name.sysname); drchase@6680: st->print("%s ", name.release); drchase@6680: st->print("%s ", name.version); drchase@6680: st->print("%s", name.machine); nloodin@3783: st->cr(); nloodin@3783: } nloodin@3783: tschatzl@4854: bool os::has_allocatable_memory_limit(julong* limit) { tschatzl@4854: struct rlimit rlim; tschatzl@4854: int getrlimit_res = getrlimit(RLIMIT_AS, &rlim); tschatzl@4854: // if there was an error when calling getrlimit, assume that there is no limitation tschatzl@4854: // on virtual memory. tschatzl@4854: bool result; tschatzl@4854: if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) { tschatzl@4854: result = false; tschatzl@4854: } else { tschatzl@4854: *limit = (julong)rlim.rlim_cur; tschatzl@4854: result = true; tschatzl@4854: } tschatzl@4854: #ifdef _LP64 tschatzl@4854: return result; tschatzl@4854: #else tschatzl@4854: // arbitrary virtual space limit for 32 bit Unices found by testing. If tschatzl@4854: // getrlimit above returned a limit, bound it with this limit. Otherwise tschatzl@4854: // directly use it. tschatzl@4854: const julong max_virtual_limit = (julong)3800*M; tschatzl@4854: if (result) { tschatzl@4854: *limit = MIN2(*limit, max_virtual_limit); tschatzl@4854: } else { tschatzl@4854: *limit = max_virtual_limit; tschatzl@4854: } nloodin@3783: tschatzl@4854: // bound by actually allocatable memory. The algorithm uses two bounds, an tschatzl@4854: // upper and a lower limit. The upper limit is the current highest amount of tschatzl@4854: // memory that could not be allocated, the lower limit is the current highest tschatzl@4854: // amount of memory that could be allocated. tschatzl@4854: // The algorithm iteratively refines the result by halving the difference tschatzl@4854: // between these limits, updating either the upper limit (if that value could tschatzl@4854: // not be allocated) or the lower limit (if the that value could be allocated) tschatzl@4854: // until the difference between these limits is "small". tschatzl@4854: tschatzl@4854: // the minimum amount of memory we care about allocating. tschatzl@4854: const julong min_allocation_size = M; tschatzl@4854: tschatzl@4854: julong upper_limit = *limit; tschatzl@4854: tschatzl@4854: // first check a few trivial cases tschatzl@4854: if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) { tschatzl@4854: *limit = upper_limit; tschatzl@4854: } else if (!is_allocatable(min_allocation_size)) { tschatzl@4854: // we found that not even min_allocation_size is allocatable. Return it tschatzl@4854: // anyway. There is no point to search for a better value any more. tschatzl@4854: *limit = min_allocation_size; tschatzl@4854: } else { tschatzl@4854: // perform the binary search. tschatzl@4854: julong lower_limit = min_allocation_size; tschatzl@4854: while ((upper_limit - lower_limit) > min_allocation_size) { tschatzl@4854: julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit; tschatzl@4854: temp_limit = align_size_down_(temp_limit, min_allocation_size); tschatzl@4854: if (is_allocatable(temp_limit)) { tschatzl@4854: lower_limit = temp_limit; tschatzl@4854: } else { tschatzl@4854: upper_limit = temp_limit; tschatzl@4854: } tschatzl@4854: } tschatzl@4854: *limit = lower_limit; tschatzl@4854: } tschatzl@4854: return true; tschatzl@4854: #endif tschatzl@4854: } vlivanov@5027: vlivanov@5027: const char* os::get_current_directory(char *buf, size_t buflen) { vlivanov@5027: return getcwd(buf, buflen); vlivanov@5027: } vlivanov@5027: vlivanov@5027: FILE* os::open(int fd, const char* mode) { vlivanov@5027: return ::fdopen(fd, mode); vlivanov@5027: } rbackman@5424: andrew@9711: DIR* os::opendir(const char* dirname) { andrew@9711: assert(dirname != NULL, "just checking"); andrew@9711: return ::opendir(dirname); andrew@9711: } andrew@9711: andrew@9711: struct dirent* os::readdir(DIR* dirp) { andrew@9711: assert(dirp != NULL, "just checking"); andrew@9711: return ::readdir(dirp); andrew@9711: } andrew@9711: andrew@9711: int os::closedir(DIR *dirp) { andrew@9711: assert(dirp != NULL, "just checking"); andrew@9711: return ::closedir(dirp); andrew@9711: } andrew@9711: bpittore@5585: // Builds a platform dependent Agent_OnLoad_ function name bpittore@5585: // which is used to find statically linked in agents. bpittore@5585: // Parameters: bpittore@5585: // sym_name: Symbol in library we are looking for bpittore@5585: // lib_name: Name of library to look in, NULL for shared libs. bpittore@5585: // is_absolute_path == true if lib_name is absolute path to agent bpittore@5585: // such as "/a/b/libL.so" bpittore@5585: // == false if only the base name of the library is passed in bpittore@5585: // such as "L" bpittore@5585: char* os::build_agent_function_name(const char *sym_name, const char *lib_name, bpittore@5585: bool is_absolute_path) { bpittore@5585: char *agent_entry_name; bpittore@5585: size_t len; bpittore@5585: size_t name_len; bpittore@5585: size_t prefix_len = strlen(JNI_LIB_PREFIX); bpittore@5585: size_t suffix_len = strlen(JNI_LIB_SUFFIX); bpittore@5585: const char *start; bpittore@5585: bpittore@5585: if (lib_name != NULL) { bpittore@5585: len = name_len = strlen(lib_name); bpittore@5585: if (is_absolute_path) { bpittore@5585: // Need to strip path, prefix and suffix bpittore@5585: if ((start = strrchr(lib_name, *os::file_separator())) != NULL) { bpittore@5585: lib_name = ++start; bpittore@5585: } bpittore@5585: if (len <= (prefix_len + suffix_len)) { bpittore@5585: return NULL; bpittore@5585: } bpittore@5585: lib_name += prefix_len; bpittore@5585: name_len = strlen(lib_name) - suffix_len; bpittore@5585: } bpittore@5585: } bpittore@5585: len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2; bpittore@5585: agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread); bpittore@5585: if (agent_entry_name == NULL) { bpittore@5585: return NULL; bpittore@5585: } bpittore@5585: strcpy(agent_entry_name, sym_name); bpittore@5585: if (lib_name != NULL) { bpittore@5585: strcat(agent_entry_name, "_"); bpittore@5585: strncat(agent_entry_name, lib_name, name_len); bpittore@5585: } bpittore@5585: return agent_entry_name; bpittore@5585: } bpittore@5585: goetz@6460: // Returned string is a constant. For unknown signals "UNKNOWN" is returned. goetz@6460: const char* os::Posix::get_signal_name(int sig, char* out, size_t outlen) { goetz@6460: goetz@6460: static const struct { goetz@6460: int sig; const char* name; goetz@6460: } goetz@6460: info[] = goetz@6460: { goetz@6460: { SIGABRT, "SIGABRT" }, goetz@6460: #ifdef SIGAIO goetz@6460: { SIGAIO, "SIGAIO" }, goetz@6460: #endif goetz@6460: { SIGALRM, "SIGALRM" }, goetz@6460: #ifdef SIGALRM1 goetz@6460: { SIGALRM1, "SIGALRM1" }, goetz@6460: #endif goetz@6460: { SIGBUS, "SIGBUS" }, goetz@6460: #ifdef SIGCANCEL goetz@6460: { SIGCANCEL, "SIGCANCEL" }, goetz@6460: #endif goetz@6460: { SIGCHLD, "SIGCHLD" }, goetz@6460: #ifdef SIGCLD goetz@6460: { SIGCLD, "SIGCLD" }, goetz@6460: #endif goetz@6460: { SIGCONT, "SIGCONT" }, goetz@6460: #ifdef SIGCPUFAIL goetz@6460: { SIGCPUFAIL, "SIGCPUFAIL" }, goetz@6460: #endif goetz@6460: #ifdef SIGDANGER goetz@6460: { SIGDANGER, "SIGDANGER" }, goetz@6460: #endif goetz@6460: #ifdef SIGDIL goetz@6460: { SIGDIL, "SIGDIL" }, goetz@6460: #endif goetz@6460: #ifdef SIGEMT goetz@6460: { SIGEMT, "SIGEMT" }, goetz@6460: #endif goetz@6460: { SIGFPE, "SIGFPE" }, goetz@6460: #ifdef SIGFREEZE goetz@6460: { SIGFREEZE, "SIGFREEZE" }, goetz@6460: #endif goetz@6460: #ifdef SIGGFAULT goetz@6460: { SIGGFAULT, "SIGGFAULT" }, goetz@6460: #endif goetz@6460: #ifdef SIGGRANT goetz@6460: { SIGGRANT, "SIGGRANT" }, goetz@6460: #endif goetz@6460: { SIGHUP, "SIGHUP" }, goetz@6460: { SIGILL, "SIGILL" }, goetz@6460: { SIGINT, "SIGINT" }, goetz@6460: #ifdef SIGIO goetz@6460: { SIGIO, "SIGIO" }, goetz@6460: #endif goetz@6460: #ifdef SIGIOINT goetz@6460: { SIGIOINT, "SIGIOINT" }, goetz@6460: #endif goetz@6460: #ifdef SIGIOT goetz@6460: // SIGIOT is there for BSD compatibility, but on most Unices just a goetz@6460: // synonym for SIGABRT. The result should be "SIGABRT", not goetz@6460: // "SIGIOT". goetz@6460: #if (SIGIOT != SIGABRT ) goetz@6460: { SIGIOT, "SIGIOT" }, goetz@6460: #endif goetz@6460: #endif goetz@6460: #ifdef SIGKAP goetz@6460: { SIGKAP, "SIGKAP" }, goetz@6460: #endif goetz@6460: { SIGKILL, "SIGKILL" }, goetz@6460: #ifdef SIGLOST goetz@6460: { SIGLOST, "SIGLOST" }, goetz@6460: #endif goetz@6460: #ifdef SIGLWP goetz@6460: { SIGLWP, "SIGLWP" }, goetz@6460: #endif goetz@6460: #ifdef SIGLWPTIMER goetz@6460: { SIGLWPTIMER, "SIGLWPTIMER" }, goetz@6460: #endif goetz@6460: #ifdef SIGMIGRATE goetz@6460: { SIGMIGRATE, "SIGMIGRATE" }, goetz@6460: #endif goetz@6460: #ifdef SIGMSG goetz@6460: { SIGMSG, "SIGMSG" }, goetz@6460: #endif goetz@6460: { SIGPIPE, "SIGPIPE" }, goetz@6460: #ifdef SIGPOLL goetz@6460: { SIGPOLL, "SIGPOLL" }, goetz@6460: #endif goetz@6460: #ifdef SIGPRE goetz@6460: { SIGPRE, "SIGPRE" }, goetz@6460: #endif goetz@6460: { SIGPROF, "SIGPROF" }, goetz@6460: #ifdef SIGPTY goetz@6460: { SIGPTY, "SIGPTY" }, goetz@6460: #endif goetz@6460: #ifdef SIGPWR goetz@6460: { SIGPWR, "SIGPWR" }, goetz@6460: #endif goetz@6460: { SIGQUIT, "SIGQUIT" }, goetz@6460: #ifdef SIGRECONFIG goetz@6460: { SIGRECONFIG, "SIGRECONFIG" }, goetz@6460: #endif goetz@6460: #ifdef SIGRECOVERY goetz@6460: { SIGRECOVERY, "SIGRECOVERY" }, goetz@6460: #endif goetz@6460: #ifdef SIGRESERVE goetz@6460: { SIGRESERVE, "SIGRESERVE" }, goetz@6460: #endif goetz@6460: #ifdef SIGRETRACT goetz@6460: { SIGRETRACT, "SIGRETRACT" }, goetz@6460: #endif goetz@6460: #ifdef SIGSAK goetz@6460: { SIGSAK, "SIGSAK" }, goetz@6460: #endif goetz@6460: { SIGSEGV, "SIGSEGV" }, goetz@6460: #ifdef SIGSOUND goetz@6460: { SIGSOUND, "SIGSOUND" }, goetz@6460: #endif goetz@6460: { SIGSTOP, "SIGSTOP" }, goetz@6460: { SIGSYS, "SIGSYS" }, goetz@6460: #ifdef SIGSYSERROR goetz@6460: { SIGSYSERROR, "SIGSYSERROR" }, goetz@6460: #endif goetz@6460: #ifdef SIGTALRM goetz@6460: { SIGTALRM, "SIGTALRM" }, goetz@6460: #endif goetz@6460: { SIGTERM, "SIGTERM" }, goetz@6460: #ifdef SIGTHAW goetz@6460: { SIGTHAW, "SIGTHAW" }, goetz@6460: #endif goetz@6460: { SIGTRAP, "SIGTRAP" }, goetz@6460: #ifdef SIGTSTP goetz@6460: { SIGTSTP, "SIGTSTP" }, goetz@6460: #endif goetz@6460: { SIGTTIN, "SIGTTIN" }, goetz@6460: { SIGTTOU, "SIGTTOU" }, goetz@6460: #ifdef SIGURG goetz@6460: { SIGURG, "SIGURG" }, goetz@6460: #endif goetz@6460: { SIGUSR1, "SIGUSR1" }, goetz@6460: { SIGUSR2, "SIGUSR2" }, goetz@6460: #ifdef SIGVIRT goetz@6460: { SIGVIRT, "SIGVIRT" }, goetz@6460: #endif goetz@6460: { SIGVTALRM, "SIGVTALRM" }, goetz@6460: #ifdef SIGWAITING goetz@6460: { SIGWAITING, "SIGWAITING" }, goetz@6460: #endif goetz@6460: #ifdef SIGWINCH goetz@6460: { SIGWINCH, "SIGWINCH" }, goetz@6460: #endif goetz@6460: #ifdef SIGWINDOW goetz@6460: { SIGWINDOW, "SIGWINDOW" }, goetz@6460: #endif goetz@6460: { SIGXCPU, "SIGXCPU" }, goetz@6460: { SIGXFSZ, "SIGXFSZ" }, goetz@6460: #ifdef SIGXRES goetz@6460: { SIGXRES, "SIGXRES" }, goetz@6460: #endif goetz@6460: { -1, NULL } goetz@6460: }; goetz@6460: goetz@6460: const char* ret = NULL; goetz@6460: goetz@6460: #ifdef SIGRTMIN goetz@6460: if (sig >= SIGRTMIN && sig <= SIGRTMAX) { goetz@6460: if (sig == SIGRTMIN) { goetz@6460: ret = "SIGRTMIN"; goetz@6460: } else if (sig == SIGRTMAX) { goetz@6460: ret = "SIGRTMAX"; goetz@6460: } else { goetz@6460: jio_snprintf(out, outlen, "SIGRTMIN+%d", sig - SIGRTMIN); goetz@6460: return out; goetz@6460: } goetz@6460: } goetz@6460: #endif goetz@6460: goetz@6460: if (sig > 0) { goetz@6460: for (int idx = 0; info[idx].sig != -1; idx ++) { goetz@6460: if (info[idx].sig == sig) { goetz@6460: ret = info[idx].name; goetz@6460: break; goetz@6460: } goetz@6460: } goetz@6460: } goetz@6460: goetz@6460: if (!ret) { goetz@6460: if (!is_valid_signal(sig)) { goetz@6460: ret = "INVALID"; goetz@6460: } else { goetz@6460: ret = "UNKNOWN"; goetz@6460: } goetz@6460: } goetz@6460: goetz@6460: jio_snprintf(out, outlen, ret); goetz@6460: return out; goetz@6460: } goetz@6460: goetz@6460: // Returns true if signal number is valid. goetz@6460: bool os::Posix::is_valid_signal(int sig) { goetz@6460: // MacOS not really POSIX compliant: sigaddset does not return goetz@6460: // an error for invalid signal numbers. However, MacOS does not goetz@6460: // support real time signals and simply seems to have just 33 goetz@6460: // signals with no holes in the signal range. goetz@6460: #ifdef __APPLE__ goetz@6460: return sig >= 1 && sig < NSIG; goetz@6460: #else goetz@6460: // Use sigaddset to check for signal validity. goetz@6460: sigset_t set; goetz@6460: if (sigaddset(&set, sig) == -1 && errno == EINVAL) { goetz@6460: return false; goetz@6460: } goetz@6460: return true; goetz@6460: #endif goetz@6460: } goetz@6460: goetz@6460: #define NUM_IMPORTANT_SIGS 32 goetz@6460: // Returns one-line short description of a signal set in a user provided buffer. goetz@6460: const char* os::Posix::describe_signal_set_short(const sigset_t* set, char* buffer, size_t buf_size) { goetz@6461: assert(buf_size == (NUM_IMPORTANT_SIGS + 1), "wrong buffer size"); goetz@6460: // Note: for shortness, just print out the first 32. That should goetz@6460: // cover most of the useful ones, apart from realtime signals. goetz@6460: for (int sig = 1; sig <= NUM_IMPORTANT_SIGS; sig++) { goetz@6460: const int rc = sigismember(set, sig); goetz@6460: if (rc == -1 && errno == EINVAL) { goetz@6460: buffer[sig-1] = '?'; goetz@6460: } else { goetz@6460: buffer[sig-1] = rc == 0 ? '0' : '1'; goetz@6460: } goetz@6460: } goetz@6460: buffer[NUM_IMPORTANT_SIGS] = 0; goetz@6460: return buffer; goetz@6460: } goetz@6460: goetz@6460: // Prints one-line description of a signal set. goetz@6460: void os::Posix::print_signal_set_short(outputStream* st, const sigset_t* set) { goetz@6460: char buf[NUM_IMPORTANT_SIGS + 1]; goetz@6460: os::Posix::describe_signal_set_short(set, buf, sizeof(buf)); drchase@6680: st->print("%s", buf); goetz@6460: } goetz@6460: goetz@6460: // Writes one-line description of a combination of sigaction.sa_flags into a user goetz@6460: // provided buffer. Returns that buffer. goetz@6460: const char* os::Posix::describe_sa_flags(int flags, char* buffer, size_t size) { goetz@6460: char* p = buffer; goetz@6460: size_t remaining = size; goetz@6460: bool first = true; goetz@6460: int idx = 0; goetz@6460: goetz@6460: assert(buffer, "invalid argument"); goetz@6460: goetz@6460: if (size == 0) { goetz@6460: return buffer; goetz@6460: } goetz@6460: goetz@6460: strncpy(buffer, "none", size); goetz@6460: goetz@6460: const struct { aph@9610: // NB: i is an unsigned int here because SA_RESETHAND is on some aph@9610: // systems 0x80000000, which is implicitly unsigned. Assignining aph@9610: // it to an int field would be an overflow in unsigned-to-signed aph@9610: // conversion. aph@9610: unsigned int i; goetz@6460: const char* s; goetz@6460: } flaginfo [] = { goetz@6460: { SA_NOCLDSTOP, "SA_NOCLDSTOP" }, goetz@6460: { SA_ONSTACK, "SA_ONSTACK" }, goetz@6460: { SA_RESETHAND, "SA_RESETHAND" }, goetz@6460: { SA_RESTART, "SA_RESTART" }, goetz@6460: { SA_SIGINFO, "SA_SIGINFO" }, goetz@6460: { SA_NOCLDWAIT, "SA_NOCLDWAIT" }, goetz@6460: { SA_NODEFER, "SA_NODEFER" }, goetz@6460: #ifdef AIX goetz@6460: { SA_ONSTACK, "SA_ONSTACK" }, goetz@6460: { SA_OLDSTYLE, "SA_OLDSTYLE" }, goetz@6460: #endif goetz@6460: { 0, NULL } goetz@6460: }; goetz@6460: goetz@6460: for (idx = 0; flaginfo[idx].s && remaining > 1; idx++) { goetz@6460: if (flags & flaginfo[idx].i) { goetz@6460: if (first) { goetz@6460: jio_snprintf(p, remaining, "%s", flaginfo[idx].s); goetz@6460: first = false; goetz@6460: } else { goetz@6460: jio_snprintf(p, remaining, "|%s", flaginfo[idx].s); goetz@6460: } goetz@6460: const size_t len = strlen(p); goetz@6460: p += len; goetz@6460: remaining -= len; goetz@6460: } goetz@6460: } goetz@6460: goetz@6460: buffer[size - 1] = '\0'; goetz@6460: goetz@6460: return buffer; goetz@6460: } goetz@6460: goetz@6460: // Prints one-line description of a combination of sigaction.sa_flags. goetz@6460: void os::Posix::print_sa_flags(outputStream* st, int flags) { goetz@6460: char buffer[0x100]; goetz@6460: os::Posix::describe_sa_flags(flags, buffer, sizeof(buffer)); drchase@6680: st->print("%s", buffer); goetz@6460: } goetz@6460: goetz@6460: // Helper function for os::Posix::print_siginfo_...(): goetz@6460: // return a textual description for signal code. goetz@6460: struct enum_sigcode_desc_t { goetz@6460: const char* s_name; goetz@6460: const char* s_desc; goetz@6460: }; goetz@6460: goetz@6460: static bool get_signal_code_description(const siginfo_t* si, enum_sigcode_desc_t* out) { goetz@6460: goetz@6460: const struct { goetz@6460: int sig; int code; const char* s_code; const char* s_desc; goetz@6460: } t1 [] = { goetz@6460: { SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode." }, goetz@6460: { SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand." }, goetz@6460: { SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode." }, goetz@6460: { SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap." }, goetz@6460: { SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode." }, goetz@6460: { SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register." }, goetz@6460: { SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error." }, goetz@6460: { SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error." }, goetz@6460: #if defined(IA64) && defined(LINUX) goetz@6460: { SIGILL, ILL_BADIADDR, "ILL_BADIADDR", "Unimplemented instruction address" }, goetz@6460: { SIGILL, ILL_BREAK, "ILL_BREAK", "Application Break instruction" }, goetz@6460: #endif goetz@6460: { SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero." }, goetz@6460: { SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow." }, goetz@6460: { SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating-point divide by zero." }, goetz@6460: { SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating-point overflow." }, goetz@6460: { SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating-point underflow." }, goetz@6460: { SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating-point inexact result." }, goetz@6460: { SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating-point operation." }, goetz@6460: { SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range." }, goetz@6460: { SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object." }, goetz@6460: { SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for mapped object." }, goetz@6460: #ifdef AIX goetz@6460: // no explanation found what keyerr would be goetz@6460: { SIGSEGV, SEGV_KEYERR, "SEGV_KEYERR", "key error" }, goetz@6460: #endif goetz@6460: #if defined(IA64) && !defined(AIX) goetz@6460: { SIGSEGV, SEGV_PSTKOVF, "SEGV_PSTKOVF", "Paragraph stack overflow" }, goetz@6460: #endif gthornbr@8423: #if defined(__sparc) && defined(SOLARIS) gthornbr@8423: // define Solaris Sparc M7 ADI SEGV signals gthornbr@8423: #if !defined(SEGV_ACCADI) gthornbr@8423: #define SEGV_ACCADI 3 gthornbr@8423: #endif gthornbr@8423: { SIGSEGV, SEGV_ACCADI, "SEGV_ACCADI", "ADI not enabled for mapped object." }, gthornbr@8423: #if !defined(SEGV_ACCDERR) gthornbr@8423: #define SEGV_ACCDERR 4 gthornbr@8423: #endif gthornbr@8423: { SIGSEGV, SEGV_ACCDERR, "SEGV_ACCDERR", "ADI disrupting exception." }, gthornbr@8423: #if !defined(SEGV_ACCPERR) gthornbr@8423: #define SEGV_ACCPERR 5 gthornbr@8423: #endif gthornbr@8423: { SIGSEGV, SEGV_ACCPERR, "SEGV_ACCPERR", "ADI precise exception." }, gthornbr@8423: #endif // defined(__sparc) && defined(SOLARIS) goetz@6460: { SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment." }, goetz@6460: { SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Nonexistent physical address." }, goetz@6460: { SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object-specific hardware error." }, goetz@6460: { SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint." }, goetz@6460: { SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap." }, goetz@6460: { SIGCHLD, CLD_EXITED, "CLD_EXITED", "Child has exited." }, goetz@6460: { SIGCHLD, CLD_KILLED, "CLD_KILLED", "Child has terminated abnormally and did not create a core file." }, goetz@6460: { SIGCHLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally and created a core file." }, goetz@6460: { SIGCHLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped." }, goetz@6460: { SIGCHLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped." }, goetz@6460: { SIGCHLD, CLD_CONTINUED,"CLD_CONTINUED","Stopped child has continued." }, goetz@6460: #ifdef SIGPOLL goetz@6460: { SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available." }, goetz@6460: { SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available." }, goetz@6460: { SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error." }, goetz@6460: { SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available." }, goetz@6460: { SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected. [Option End]" }, goetz@6460: #endif goetz@6460: { -1, -1, NULL, NULL } goetz@6460: }; goetz@6460: goetz@6460: // Codes valid in any signal context. goetz@6460: const struct { goetz@6460: int code; const char* s_code; const char* s_desc; goetz@6460: } t2 [] = { goetz@6460: { SI_USER, "SI_USER", "Signal sent by kill()." }, goetz@6460: { SI_QUEUE, "SI_QUEUE", "Signal sent by the sigqueue()." }, goetz@6460: { SI_TIMER, "SI_TIMER", "Signal generated by expiration of a timer set by timer_settime()." }, goetz@6460: { SI_ASYNCIO, "SI_ASYNCIO", "Signal generated by completion of an asynchronous I/O request." }, goetz@6460: { SI_MESGQ, "SI_MESGQ", "Signal generated by arrival of a message on an empty message queue." }, goetz@6460: // Linux specific goetz@6460: #ifdef SI_TKILL goetz@6460: { SI_TKILL, "SI_TKILL", "Signal sent by tkill (pthread_kill)" }, goetz@6460: #endif goetz@6460: #ifdef SI_DETHREAD goetz@6460: { SI_DETHREAD, "SI_DETHREAD", "Signal sent by execve() killing subsidiary threads" }, goetz@6460: #endif goetz@6460: #ifdef SI_KERNEL goetz@6460: { SI_KERNEL, "SI_KERNEL", "Signal sent by kernel." }, goetz@6460: #endif goetz@6460: #ifdef SI_SIGIO goetz@6460: { SI_SIGIO, "SI_SIGIO", "Signal sent by queued SIGIO" }, goetz@6460: #endif goetz@6460: goetz@6460: #ifdef AIX goetz@6460: { SI_UNDEFINED, "SI_UNDEFINED","siginfo contains partial information" }, goetz@6460: { SI_EMPTY, "SI_EMPTY", "siginfo contains no useful information" }, goetz@6460: #endif goetz@6460: goetz@6460: #ifdef __sun goetz@6460: { SI_NOINFO, "SI_NOINFO", "No signal information" }, goetz@6460: { SI_RCTL, "SI_RCTL", "kernel generated signal via rctl action" }, goetz@6460: { SI_LWP, "SI_LWP", "Signal sent via lwp_kill" }, goetz@6460: #endif goetz@6460: goetz@6460: { -1, NULL, NULL } goetz@6460: }; goetz@6460: goetz@6460: const char* s_code = NULL; goetz@6460: const char* s_desc = NULL; goetz@6460: goetz@6460: for (int i = 0; t1[i].sig != -1; i ++) { goetz@6460: if (t1[i].sig == si->si_signo && t1[i].code == si->si_code) { goetz@6460: s_code = t1[i].s_code; goetz@6460: s_desc = t1[i].s_desc; goetz@6460: break; goetz@6460: } goetz@6460: } goetz@6460: goetz@6460: if (s_code == NULL) { goetz@6460: for (int i = 0; t2[i].s_code != NULL; i ++) { goetz@6460: if (t2[i].code == si->si_code) { goetz@6460: s_code = t2[i].s_code; goetz@6460: s_desc = t2[i].s_desc; goetz@6460: } goetz@6460: } goetz@6460: } goetz@6460: goetz@6460: if (s_code == NULL) { goetz@6460: out->s_name = "unknown"; goetz@6460: out->s_desc = "unknown"; goetz@6460: return false; goetz@6460: } goetz@6460: goetz@6460: out->s_name = s_code; goetz@6460: out->s_desc = s_desc; goetz@6460: goetz@6460: return true; goetz@6460: } goetz@6460: goetz@6460: // A POSIX conform, platform-independend siginfo print routine. goetz@6460: // Short print out on one line. goetz@6460: void os::Posix::print_siginfo_brief(outputStream* os, const siginfo_t* si) { goetz@6460: char buf[20]; goetz@6460: os->print("siginfo: "); goetz@6460: goetz@6460: if (!si) { goetz@6460: os->print(""); goetz@6460: return; goetz@6460: } goetz@6460: goetz@6460: // See print_siginfo_full() for details. goetz@6460: const int sig = si->si_signo; goetz@6460: goetz@6460: os->print("si_signo: %d (%s)", sig, os::Posix::get_signal_name(sig, buf, sizeof(buf))); goetz@6460: goetz@6460: enum_sigcode_desc_t ed; goetz@6460: if (get_signal_code_description(si, &ed)) { goetz@6460: os->print(", si_code: %d (%s)", si->si_code, ed.s_name); goetz@6460: } else { goetz@6460: os->print(", si_code: %d (unknown)", si->si_code); goetz@6460: } goetz@6460: goetz@6460: if (si->si_errno) { goetz@6460: os->print(", si_errno: %d", si->si_errno); goetz@6460: } goetz@6460: goetz@6460: const int me = (int) ::getpid(); goetz@6460: const int pid = (int) si->si_pid; goetz@6460: goetz@6460: if (si->si_code == SI_USER || si->si_code == SI_QUEUE) { goetz@6460: if (IS_VALID_PID(pid) && pid != me) { goetz@6460: os->print(", sent from pid: %d (uid: %d)", pid, (int) si->si_uid); goetz@6460: } goetz@6460: } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || goetz@6460: sig == SIGTRAP || sig == SIGFPE) { goetz@6460: os->print(", si_addr: " PTR_FORMAT, si->si_addr); goetz@6460: #ifdef SIGPOLL goetz@6460: } else if (sig == SIGPOLL) { goetz@6460: os->print(", si_band: " PTR64_FORMAT, (uint64_t)si->si_band); goetz@6460: #endif goetz@6460: } else if (sig == SIGCHLD) { goetz@6460: os->print_cr(", si_pid: %d, si_uid: %d, si_status: %d", (int) si->si_pid, si->si_uid, si->si_status); goetz@6460: } goetz@6460: } goetz@6460: rbackman@5424: os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() { rbackman@5424: assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread"); rbackman@5424: } rbackman@5424: rbackman@5424: /* rbackman@5424: * See the caveats for this class in os_posix.hpp rbackman@5424: * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this rbackman@5424: * method and returns false. If none of the signals are raised, returns true. rbackman@5424: * The callback is supposed to provide the method that should be protected. rbackman@5424: */ rbackman@5424: bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) { sla@5609: sigset_t saved_sig_mask; sla@5609: rbackman@5424: assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread"); rbackman@5424: assert(!WatcherThread::watcher_thread()->has_crash_protection(), rbackman@5424: "crash_protection already set?"); rbackman@5424: sla@5609: // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask sla@5609: // since on at least some systems (OS X) siglongjmp will restore the mask sla@5609: // for the process, not the thread sla@5609: pthread_sigmask(0, NULL, &saved_sig_mask); sla@5609: if (sigsetjmp(_jmpbuf, 0) == 0) { rbackman@5424: // make sure we can see in the signal handler that we have crash protection rbackman@5424: // installed rbackman@5424: WatcherThread::watcher_thread()->set_crash_protection(this); rbackman@5424: cb.call(); rbackman@5424: // and clear the crash protection rbackman@5424: WatcherThread::watcher_thread()->set_crash_protection(NULL); rbackman@5424: return true; rbackman@5424: } rbackman@5424: // this happens when we siglongjmp() back sla@5609: pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL); rbackman@5424: WatcherThread::watcher_thread()->set_crash_protection(NULL); rbackman@5424: return false; rbackman@5424: } rbackman@5424: rbackman@5424: void os::WatcherThreadCrashProtection::restore() { rbackman@5424: assert(WatcherThread::watcher_thread()->has_crash_protection(), rbackman@5424: "must have crash protection"); rbackman@5424: rbackman@5424: siglongjmp(_jmpbuf, 1); rbackman@5424: } rbackman@5424: rbackman@5424: void os::WatcherThreadCrashProtection::check_crash_protection(int sig, rbackman@5424: Thread* thread) { rbackman@5424: rbackman@5424: if (thread != NULL && rbackman@5424: thread->is_Watcher_thread() && rbackman@5424: WatcherThread::watcher_thread()->has_crash_protection()) { rbackman@5424: rbackman@5424: if (sig == SIGSEGV || sig == SIGBUS) { rbackman@5424: WatcherThread::watcher_thread()->crash_protection()->restore(); rbackman@5424: } rbackman@5424: } rbackman@5424: }