1 /*
   2  * Copyright (c) 1997, 2016, 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 #include "prims/jvm.h"
  30 
  31 #include <stdarg.h>
  32 
  33 // Simple class to format the ctor arguments into a fixed-sized buffer.
  34 class FormatBufferBase {
  35  protected:
  36   char* _buf;
  37   inline FormatBufferBase(char* buf) : _buf(buf) {}
  38  public:
  39   static const int BufferSize = 256;
  40   operator const char *() const { return _buf; }
  41 };
  42 
  43 // Use resource area for buffer
  44 class FormatBufferResource : public FormatBufferBase {
  45  public:
  46   FormatBufferResource(const char * format, ...) ATTRIBUTE_PRINTF(2, 3);
  47 };
  48 
  49 class FormatBufferDummy {};
  50 
  51 // Use stack for buffer
  52 template <size_t bufsz = FormatBufferBase::BufferSize>
  53 class FormatBuffer : public FormatBufferBase {
  54  public:
  55   inline FormatBuffer(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
  56   inline FormatBuffer(FormatBufferDummy dummy, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0);
  57   inline void append(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);
  58   inline void print(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);
  59   inline void printv(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0);
  60 
  61   char* buffer() { return _buf; }
  62   int size() { return bufsz; }
  63 
  64  private:
  65   FormatBuffer(const FormatBuffer &); // prevent copies
  66   char _buffer[bufsz];
  67 
  68  protected:
  69   inline FormatBuffer();
  70 };
  71 
  72 template <size_t bufsz>
  73 FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) {
  74   va_list argp;
  75   va_start(argp, format);
  76   jio_vsnprintf(_buf, bufsz, format, argp);
  77   va_end(argp);
  78 }
  79 
  80 template <size_t bufsz>
  81 FormatBuffer<bufsz>::FormatBuffer(FormatBufferDummy dummy, const char * format, va_list ap) : FormatBufferBase(_buffer) {
  82   jio_vsnprintf(_buf, bufsz, format, ap);
  83 }
  84 
  85 template <size_t bufsz>
  86 FormatBuffer<bufsz>::FormatBuffer() : FormatBufferBase(_buffer) {
  87   _buf[0] = '\0';
  88 }
  89 
  90 template <size_t bufsz>
  91 void FormatBuffer<bufsz>::print(const char * format, ...) {
  92   va_list argp;
  93   va_start(argp, format);
  94   jio_vsnprintf(_buf, bufsz, format, argp);
  95   va_end(argp);
  96 }
  97 
  98 template <size_t bufsz>
  99 void FormatBuffer<bufsz>::printv(const char * format, va_list argp) {
 100   jio_vsnprintf(_buf, bufsz, format, argp);
 101 }
 102 
 103 template <size_t bufsz>
 104 void FormatBuffer<bufsz>::append(const char* format, ...) {
 105   // Given that the constructor does a vsnprintf we can assume that
 106   // _buf is already initialized.
 107   size_t len = strlen(_buf);
 108   char* buf_end = _buf + len;
 109 
 110   va_list argp;
 111   va_start(argp, format);
 112   jio_vsnprintf(buf_end, bufsz - len, format, argp);
 113   va_end(argp);
 114 }
 115 
 116 // Used to format messages.
 117 typedef FormatBuffer<> err_msg;
 118 
 119 // assertions
 120 #ifndef ASSERT
 121 #define vmassert(p, ...)
 122 #else
 123 // Note: message says "assert" rather than "vmassert" for backward
 124 // compatibility with tools that parse/match the message text.
 125 // Note: The signature is vmassert(p, format, ...), but the solaris
 126 // compiler can't handle an empty ellipsis in a macro without a warning.
 127 #define vmassert(p, ...)                                                       \
 128 do {                                                                           \
 129   if (!(p)) {                                                                  \
 130     if (is_executing_unit_tests()) {                                           \
 131       report_assert_msg(__VA_ARGS__);                                          \
 132     }                                                                          \
 133     report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", __VA_ARGS__); \
 134     BREAKPOINT;                                                                \
 135   }                                                                            \
 136 } while (0)
 137 #endif
 138 
 139 // For backward compatibility.
 140 #define assert(p, ...) vmassert(p, __VA_ARGS__)
 141 
 142 // This version of vmassert is for use with checking return status from
 143 // library calls that return actual error values eg. EINVAL,
 144 // ENOMEM etc, rather than returning -1 and setting errno.
 145 // When the status is not what is expected it is very useful to know
 146 // what status was actually returned, so we pass the status variable as
 147 // an extra arg and use strerror to convert it to a meaningful string
 148 // like "Invalid argument", "out of memory" etc
 149 #define vmassert_status(p, status, msg) \
 150 do {                                                                           \
 151   if (!(p)) {                                                                  \
 152     report_vm_status_error(__FILE__, __LINE__, "assert(" #p ") failed",        \
 153                            status, msg);                                       \
 154     BREAKPOINT;                                                                \
 155   }                                                                            \
 156 } while (0)
 157 
 158 // For backward compatibility.
 159 #define assert_status(p, status, msg) vmassert_status(p, status, msg)
 160 
 161 // guarantee is like vmassert except it's always executed -- use it for
 162 // cheap tests that catch errors that would otherwise be hard to find.
 163 // guarantee is also used for Verify options.
 164 #define guarantee(p, ...)                                                         \
 165 do {                                                                              \
 166   if (!(p)) {                                                                     \
 167     report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", __VA_ARGS__); \
 168     BREAKPOINT;                                                                   \
 169   }                                                                               \
 170 } while (0)
 171 
 172 #define fatal(...)                                                                \
 173 do {                                                                              \
 174   report_fatal(__FILE__, __LINE__, __VA_ARGS__);                                  \
 175   BREAKPOINT;                                                                     \
 176 } while (0)
 177 
 178 // out of memory
 179 #define vm_exit_out_of_memory(size, vm_err_type, ...)                             \
 180 do {                                                                              \
 181   report_vm_out_of_memory(__FILE__, __LINE__, size, vm_err_type, __VA_ARGS__);    \
 182   BREAKPOINT;                                                                     \
 183 } while (0)
 184 
 185 #define ShouldNotCallThis()                                                       \
 186 do {                                                                              \
 187   report_should_not_call(__FILE__, __LINE__);                                     \
 188   BREAKPOINT;                                                                     \
 189 } while (0)
 190 
 191 #define ShouldNotReachHere()                                                      \
 192 do {                                                                              \
 193   report_should_not_reach_here(__FILE__, __LINE__);                               \
 194   BREAKPOINT;                                                                     \
 195 } while (0)
 196 
 197 #define Unimplemented()                                                           \
 198 do {                                                                              \
 199   report_unimplemented(__FILE__, __LINE__);                                       \
 200   BREAKPOINT;                                                                     \
 201 } while (0)
 202 
 203 #define Untested(msg)                                                             \
 204 do {                                                                              \
 205   report_untested(__FILE__, __LINE__, msg);                                       \
 206   BREAKPOINT;                                                                     \
 207 } while (0);
 208 
 209 
 210 // types of VM error - originally in vmError.hpp
 211 enum VMErrorType {
 212   INTERNAL_ERROR   = 0xe0000000,
 213   OOM_MALLOC_ERROR = 0xe0000001,
 214   OOM_MMAP_ERROR   = 0xe0000002
 215 };
 216 
 217 // error reporting helper functions
 218 void report_vm_error(const char* file, int line, const char* error_msg);
 219 #if !defined(__GNUC__) || defined (__clang_major__) || (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || __GNUC__ > 4)
 220 // ATTRIBUTE_PRINTF works with gcc >= 4.8 and any other compiler.
 221 void report_vm_error(const char* file, int line, const char* error_msg,
 222                      const char* detail_fmt, ...) ATTRIBUTE_PRINTF(4, 5);
 223 #else
 224 // GCC < 4.8 warns because of empty format string.  Warning can not be switched off selectively.
 225 void report_vm_error(const char* file, int line, const char* error_msg,
 226                      const char* detail_fmt, ...);
 227 #endif
 228 void report_vm_status_error(const char* file, int line, const char* error_msg,
 229                             int status, const char* detail);
 230 void report_fatal(const char* file, int line, const char* detail_fmt, ...) ATTRIBUTE_PRINTF(3, 4);
 231 void report_vm_out_of_memory(const char* file, int line, size_t size, VMErrorType vm_err_type,
 232                              const char* detail_fmt, ...) ATTRIBUTE_PRINTF(5, 6);
 233 void report_should_not_call(const char* file, int line);
 234 void report_should_not_reach_here(const char* file, int line);
 235 void report_unimplemented(const char* file, int line);
 236 void report_untested(const char* file, int line, const char* message);
 237 
 238 #ifdef ASSERT
 239 // unit test support
 240 bool is_executing_unit_tests();
 241 void report_assert_msg(const char* msg, ...) ATTRIBUTE_PRINTF(1, 2);
 242 #endif // ASSERT
 243 
 244 void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2);
 245 
 246 // Compile-time asserts.  Cond must be a compile-time constant expression that
 247 // is convertible to bool.  STATIC_ASSERT() can be used anywhere a declaration
 248 // may appear.
 249 //
 250 // Implementation Note: STATIC_ASSERT_FAILURE<true> provides a value member
 251 // rather than type member that could be used directly in the typedef, because
 252 // a type member would require conditional use of "typename", depending on
 253 // whether Cond is dependent or not.  The use of a value member leads to the
 254 // use of an array type.
 255 
 256 template<bool x> struct STATIC_ASSERT_FAILURE;
 257 template<> struct STATIC_ASSERT_FAILURE<true> { enum { value = 1 }; };
 258 
 259 #define STATIC_ASSERT(Cond) \
 260   typedef char PASTE_TOKENS(STATIC_ASSERT_DUMMY_TYPE_, __LINE__)[ \
 261     STATIC_ASSERT_FAILURE< (Cond) >::value ]
 262 
 263 // out of shared space reporting
 264 enum SharedSpaceType {
 265   SharedReadOnly,
 266   SharedReadWrite,
 267   SharedMiscData,
 268   SharedMiscCode
 269 };
 270 
 271 void report_out_of_shared_space(SharedSpaceType space_type);
 272 
 273 void report_insufficient_metaspace(size_t required_size);
 274 
 275 // out of memory reporting
 276 void report_java_out_of_memory(const char* message);
 277 
 278 // Support for self-destruct
 279 bool is_error_reported();
 280 void set_error_reported();
 281 
 282 /* Test vmassert(), fatal(), guarantee(), etc. */
 283 NOT_PRODUCT(void test_error_handler();)
 284 
 285 // crash in a controlled way:
 286 // how can be one of:
 287 // 1,2 - asserts
 288 // 3,4 - guarantee
 289 // 5-7 - fatal
 290 // 8 - vm_exit_out_of_memory
 291 // 9 - ShouldNotCallThis
 292 // 10 - ShouldNotReachHere
 293 // 11 - Unimplemented
 294 // 12,13 - (not guaranteed) crashes
 295 // 14 - SIGSEGV
 296 // 15 - SIGFPE
 297 NOT_PRODUCT(void controlled_crash(int how);)
 298 
 299 // returns an address which is guaranteed to generate a SIGSEGV on read,
 300 // for test purposes, which is not NULL and contains bits in every word
 301 NOT_PRODUCT(void* get_segfault_address();)
 302 
 303 void pd_ps(frame f);
 304 void pd_obfuscate_location(char *buf, size_t buflen);
 305 
 306 class outputStream;
 307 void print_native_stack(outputStream* st, frame fr, Thread* t, char* buf, int buf_size);
 308 
 309 #endif // SHARE_VM_UTILITIES_DEBUG_HPP