src/os/posix/vm/os_posix.cpp

Thu, 02 Aug 2018 03:54:51 -0700

author
kevinw
date
Thu, 02 Aug 2018 03:54:51 -0700
changeset 9478
f3108e56b502
parent 8423
2988e5adeb8c
child 9572
624a0741915c
child 9610
f43f77de876a
permissions
-rw-r--r--

8196882: VS2017 Hotspot Defined vsnprintf Function Causes C2084 Already Defined Compilation Error
Summary: Add os::vsnprintf and os::snprintf.
Reviewed-by: kbarrett, lfoltan, stuefe, mlarsson

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

mercurial