src/os/posix/vm/os_posix.cpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 9478
f3108e56b502
parent 8604
04d83ba48607
child 9637
eef07cd490d4
permissions
-rw-r--r--

Merge

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

mercurial