1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_DEBUG_HPP
  26 #define SHARE_VM_UTILITIES_DEBUG_HPP
  27 
  28 #include "utilities/globalDefinitions.hpp"
  29 
  30 // assertions
  31 #ifndef ASSERT
  32 #define vmassert(p, ...)
  33 #else
  34 // Note: message says "assert" rather than "vmassert" for backward
  35 // compatibility with tools that parse/match the message text.
  36 // Note: The signature is vmassert(p, format, ...), but the solaris
  37 // compiler can't handle an empty ellipsis in a macro without a warning.
  38 #define vmassert(p, ...)                                                       \
  39 do {                                                                           \
  40   if (!(p)) {                                                                  \
  41     if (is_executing_unit_tests()) {                                           \
  42       report_assert_msg(__VA_ARGS__);                                          \
  43     }                                                                          \
  44     report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", __VA_ARGS__); \
  45     BREAKPOINT;                                                                \
  46   }                                                                            \
  47 } while (0)
  48 #endif
  49 
  50 // For backward compatibility.
  51 #define assert(p, ...) vmassert(p, __VA_ARGS__)
  52 
  53 #ifndef ASSERT
  54 #define vmassert_status(p, status, msg)
  55 #else
  56 // This version of vmassert is for use with checking return status from
  57 // library calls that return actual error values eg. EINVAL,
  58 // ENOMEM etc, rather than returning -1 and setting errno.
  59 // When the status is not what is expected it is very useful to know
  60 // what status was actually returned, so we pass the status variable as
  61 // an extra arg and use strerror to convert it to a meaningful string
  62 // like "Invalid argument", "out of memory" etc
  63 #define vmassert_status(p, status, msg) \
  64 do {                                                                           \
  65   if (!(p)) {                                                                  \
  66     report_vm_status_error(__FILE__, __LINE__, "assert(" #p ") failed",        \
  67                            status, msg);                                       \
  68     BREAKPOINT;                                                                \
  69   }                                                                            \
  70 } while (0)
  71 #endif
  72 
  73 // For backward compatibility.
  74 #define assert_status(p, status, msg) vmassert_status(p, status, msg)
  75 
  76 // guarantee is like vmassert except it's always executed -- use it for
  77 // cheap tests that catch errors that would otherwise be hard to find.
  78 // guarantee is also used for Verify options.
  79 #define guarantee(p, ...)                                                         \
  80 do {                                                                              \
  81   if (!(p)) {                                                                     \
  82     report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", __VA_ARGS__); \
  83     BREAKPOINT;                                                                   \
  84   }                                                                               \
  85 } while (0)
  86 
  87 #define fatal(...)                                                                \
  88 do {                                                                              \
  89   report_fatal(__FILE__, __LINE__, __VA_ARGS__);                                  \
  90   BREAKPOINT;                                                                     \
  91 } while (0)
  92 
  93 // out of memory
  94 #define vm_exit_out_of_memory(size, vm_err_type, ...)                             \
  95 do {                                                                              \
  96   report_vm_out_of_memory(__FILE__, __LINE__, size, vm_err_type, __VA_ARGS__);    \
  97   BREAKPOINT;                                                                     \
  98 } while (0)
  99 
 100 #define ShouldNotCallThis()                                                       \
 101 do {                                                                              \
 102   report_should_not_call(__FILE__, __LINE__);                                     \
 103   BREAKPOINT;                                                                     \
 104 } while (0)
 105 
 106 #define ShouldNotReachHere()                                                      \
 107 do {                                                                              \
 108   report_should_not_reach_here(__FILE__, __LINE__);                               \
 109   BREAKPOINT;                                                                     \
 110 } while (0)
 111 
 112 #define Unimplemented()                                                           \
 113 do {                                                                              \
 114   report_unimplemented(__FILE__, __LINE__);                                       \
 115   BREAKPOINT;                                                                     \
 116 } while (0)
 117 
 118 #define Untested(msg)                                                             \
 119 do {                                                                              \
 120   report_untested(__FILE__, __LINE__, msg);                                       \
 121   BREAKPOINT;                                                                     \
 122 } while (0);
 123 
 124 
 125 // types of VM error - originally in vmError.hpp
 126 enum VMErrorType {
 127   INTERNAL_ERROR   = 0xe0000000,
 128   OOM_MALLOC_ERROR = 0xe0000001,
 129   OOM_MMAP_ERROR   = 0xe0000002
 130 };
 131 
 132 // error reporting helper functions
 133 void report_vm_error(const char* file, int line, const char* error_msg);
 134 #if !defined(__GNUC__) || defined (__clang_major__) || (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || __GNUC__ > 4)
 135 // ATTRIBUTE_PRINTF works with gcc >= 4.8 and any other compiler.
 136 void report_vm_error(const char* file, int line, const char* error_msg,
 137                      const char* detail_fmt, ...) ATTRIBUTE_PRINTF(4, 5);
 138 #ifdef ASSERT
 139 void report_assert_msg(const char* msg, ...) ATTRIBUTE_PRINTF(1, 2);
 140 #endif // ASSERT
 141 #else
 142 // GCC < 4.8 warns because of empty format string.  Warning can not be switched off selectively.
 143 void report_vm_error(const char* file, int line, const char* error_msg,
 144                      const char* detail_fmt, ...);
 145 #ifdef ASSERT
 146 void report_assert_msg(const char* msg, ...);
 147 #endif // ASSERT
 148 #endif
 149 void report_vm_status_error(const char* file, int line, const char* error_msg,
 150                             int status, const char* detail);
 151 void report_fatal(const char* file, int line, const char* detail_fmt, ...) ATTRIBUTE_PRINTF(3, 4);
 152 void report_vm_out_of_memory(const char* file, int line, size_t size, VMErrorType vm_err_type,
 153                              const char* detail_fmt, ...) ATTRIBUTE_PRINTF(5, 6);
 154 void report_should_not_call(const char* file, int line);
 155 void report_should_not_reach_here(const char* file, int line);
 156 void report_unimplemented(const char* file, int line);
 157 void report_untested(const char* file, int line, const char* message);
 158 
 159 #ifdef ASSERT
 160 // unit test support
 161 bool is_executing_unit_tests();
 162 #endif // ASSERT
 163 
 164 void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2);
 165 
 166 // Compile-time asserts.  Cond must be a compile-time constant expression that
 167 // is convertible to bool.  STATIC_ASSERT() can be used anywhere a declaration
 168 // may appear.
 169 //
 170 // Implementation Note: STATIC_ASSERT_FAILURE<true> provides a value member
 171 // rather than type member that could be used directly in the typedef, because
 172 // a type member would require conditional use of "typename", depending on
 173 // whether Cond is dependent or not.  The use of a value member leads to the
 174 // use of an array type.
 175 
 176 template<bool x> struct STATIC_ASSERT_FAILURE;
 177 template<> struct STATIC_ASSERT_FAILURE<true> { enum { value = 1 }; };
 178 
 179 #define STATIC_ASSERT(Cond) \
 180   typedef char PASTE_TOKENS(STATIC_ASSERT_DUMMY_TYPE_, __LINE__)[ \
 181     STATIC_ASSERT_FAILURE< (Cond) >::value ]
 182 
 183 // out of shared space reporting
 184 enum SharedSpaceType {
 185   SharedReadOnly,
 186   SharedReadWrite,
 187   SharedMiscData,
 188   SharedMiscCode,
 189   SharedOptional
 190 };
 191 
 192 void report_out_of_shared_space(SharedSpaceType space_type);
 193 
 194 void report_insufficient_metaspace(size_t required_size);
 195 
 196 // out of memory reporting
 197 void report_java_out_of_memory(const char* message);
 198 
 199 // Support for self-destruct
 200 bool is_error_reported();
 201 void set_error_reported();
 202 
 203 /* Test vmassert(), fatal(), guarantee(), etc. */
 204 NOT_PRODUCT(void test_error_handler();)
 205 
 206 // crash in a controlled way:
 207 // how can be one of:
 208 // 1,2 - asserts
 209 // 3,4 - guarantee
 210 // 5-7 - fatal
 211 // 8 - vm_exit_out_of_memory
 212 // 9 - ShouldNotCallThis
 213 // 10 - ShouldNotReachHere
 214 // 11 - Unimplemented
 215 // 12,13 - (not guaranteed) crashes
 216 // 14 - SIGSEGV
 217 // 15 - SIGFPE
 218 NOT_PRODUCT(void controlled_crash(int how);)
 219 
 220 // returns an address which is guaranteed to generate a SIGSEGV on read,
 221 // for test purposes, which is not NULL and contains bits in every word
 222 NOT_PRODUCT(void* get_segfault_address();)
 223 
 224 class frame;
 225 void pd_ps(frame f);
 226 
 227 #endif // SHARE_VM_UTILITIES_DEBUG_HPP