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