1 /*
   2  * Copyright (c) 1997, 2014, 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   operator const char *() const { return _buf; }
  40 };
  41 
  42 // Use resource area for buffer
  43 #define RES_BUFSZ 256
  44 class FormatBufferResource : public FormatBufferBase {
  45  public:
  46   FormatBufferResource(const char * format, ...) ATTRIBUTE_PRINTF(2, 3);
  47 };
  48 
  49 // Use stack for buffer
  50 template <size_t bufsz = 256>
  51 class FormatBuffer : public FormatBufferBase {
  52  public:
  53   inline FormatBuffer(const char * format, ...) ATTRIBUTE_PRINTF(2, 3);
  54   inline void append(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);
  55   inline void print(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);
  56   inline void printv(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0);
  57 
  58   char* buffer() { return _buf; }
  59   int size() { return bufsz; }
  60 
  61  private:
  62   FormatBuffer(const FormatBuffer &); // prevent copies
  63   char _buffer[bufsz];
  64 
  65  protected:
  66   inline FormatBuffer();
  67 };
  68 
  69 template <size_t bufsz>
  70 FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) {
  71   va_list argp;
  72   va_start(argp, format);
  73   jio_vsnprintf(_buf, bufsz, format, argp);
  74   va_end(argp);
  75 }
  76 
  77 template <size_t bufsz>
  78 FormatBuffer<bufsz>::FormatBuffer() : FormatBufferBase(_buffer) {
  79   _buf[0] = '\0';
  80 }
  81 
  82 template <size_t bufsz>
  83 void FormatBuffer<bufsz>::print(const char * format, ...) {
  84   va_list argp;
  85   va_start(argp, format);
  86   jio_vsnprintf(_buf, bufsz, format, argp);
  87   va_end(argp);
  88 }
  89 
  90 template <size_t bufsz>
  91 void FormatBuffer<bufsz>::printv(const char * format, va_list argp) {
  92   jio_vsnprintf(_buf, bufsz, format, argp);
  93 }
  94 
  95 template <size_t bufsz>
  96 void FormatBuffer<bufsz>::append(const char* format, ...) {
  97   // Given that the constructor does a vsnprintf we can assume that
  98   // _buf is already initialized.
  99   size_t len = strlen(_buf);
 100   char* buf_end = _buf + len;
 101 
 102   va_list argp;
 103   va_start(argp, format);
 104   jio_vsnprintf(buf_end, bufsz - len, format, argp);
 105   va_end(argp);
 106 }
 107 
 108 // Used to format messages for vmassert(), guarantee(), fatal(), etc.
 109 typedef FormatBuffer<> err_msg;
 110 typedef FormatBufferResource err_msg_res;
 111 
 112 // assertions
 113 #ifndef ASSERT
 114 #define vmassert(p, msg)
 115 #else
 116 #define vmassert(p, msg)                                                     \
 117 do {                                                                         \
 118   if (!(p)) {                                                                \
 119     report_vm_error(__FILE__, __LINE__, "vmassert(" #p ") failed", msg);     \
 120     BREAKPOINT;                                                              \
 121   }                                                                          \
 122 } while (0)
 123 #endif
 124 
 125 // For backward compatibility.
 126 #define assert(p, msg) vmassert(p, msg)
 127 
 128 // This version of vmassert is for use with checking return status from
 129 // library calls that return actual error values eg. EINVAL,
 130 // ENOMEM etc, rather than returning -1 and setting errno.
 131 // When the status is not what is expected it is very useful to know
 132 // what status was actually returned, so we pass the status variable as
 133 // an extra arg and use strerror to convert it to a meaningful string
 134 // like "Invalid argument", "out of memory" etc
 135 #define vmassert_status(p, status, msg) \
 136   vmassert(p, err_msg("error %s(%d), %s", strerror(status), status, msg))
 137 
 138 // For backward compatibility.
 139 #define assert_status(p, status, msg) vmassert_status(p, status, msg)
 140 
 141 // guarantee is like vmassert except it's always executed -- use it for
 142 // cheap tests that catch errors that would otherwise be hard to find.
 143 // guarantee is also used for Verify options.
 144 #define guarantee(p, msg)                                                    \
 145 do {                                                                         \
 146   if (!(p)) {                                                                \
 147     report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", msg);    \
 148     BREAKPOINT;                                                              \
 149   }                                                                          \
 150 } while (0)
 151 
 152 #define fatal(msg)                                                           \
 153 do {                                                                         \
 154   report_fatal(__FILE__, __LINE__, msg);                                     \
 155   BREAKPOINT;                                                                \
 156 } while (0)
 157 
 158 // out of memory
 159 #define vm_exit_out_of_memory(size, vm_err_type, msg)                        \
 160 do {                                                                         \
 161   report_vm_out_of_memory(__FILE__, __LINE__, size, vm_err_type, msg);       \
 162   BREAKPOINT;                                                                \
 163 } while (0)
 164 
 165 #define ShouldNotCallThis()                                                  \
 166 do {                                                                         \
 167   report_should_not_call(__FILE__, __LINE__);                                \
 168   BREAKPOINT;                                                                \
 169 } while (0)
 170 
 171 #define ShouldNotReachHere()                                                 \
 172 do {                                                                         \
 173   report_should_not_reach_here(__FILE__, __LINE__);                          \
 174   BREAKPOINT;                                                                \
 175 } while (0)
 176 
 177 #define Unimplemented()                                                      \
 178 do {                                                                         \
 179   report_unimplemented(__FILE__, __LINE__);                                  \
 180   BREAKPOINT;                                                                \
 181 } while (0)
 182 
 183 #define Untested(msg)                                                        \
 184 do {                                                                         \
 185   report_untested(__FILE__, __LINE__, msg);                                  \
 186   BREAKPOINT;                                                                \
 187 } while (0);
 188 
 189 
 190 // types of VM error - originally in vmError.hpp
 191 enum VMErrorType {
 192   INTERNAL_ERROR   = 0xe0000000,
 193   OOM_MALLOC_ERROR = 0xe0000001,
 194   OOM_MMAP_ERROR   = 0xe0000002
 195 };
 196 
 197 // error reporting helper functions
 198 void report_vm_error(const char* file, int line, const char* error_msg,
 199                      const char* detail_msg = NULL);
 200 void report_fatal(const char* file, int line, const char* message);
 201 void report_vm_out_of_memory(const char* file, int line, size_t size,
 202                              VMErrorType vm_err_type, const char* message);
 203 void report_should_not_call(const char* file, int line);
 204 void report_should_not_reach_here(const char* file, int line);
 205 void report_unimplemented(const char* file, int line);
 206 void report_untested(const char* file, int line, const char* message);
 207 
 208 void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2);
 209 
 210 #ifdef ASSERT
 211 // Compile-time asserts.
 212 template <bool> struct StaticAssert;
 213 template <> struct StaticAssert<true> {};
 214 
 215 // Only StaticAssert<true> is defined, so if cond evaluates to false we get
 216 // a compile time exception when trying to use StaticAssert<false>.
 217 #define STATIC_ASSERT(cond)                   \
 218   do {                                        \
 219     StaticAssert<(cond)> DUMMY_STATIC_ASSERT; \
 220     (void)DUMMY_STATIC_ASSERT; /* ignore */   \
 221   } while (false)
 222 #else
 223 #define STATIC_ASSERT(cond)
 224 #endif
 225 
 226 // out of shared space reporting
 227 enum SharedSpaceType {
 228   SharedReadOnly,
 229   SharedReadWrite,
 230   SharedMiscData,
 231   SharedMiscCode
 232 };
 233 
 234 void report_out_of_shared_space(SharedSpaceType space_type);
 235 
 236 // out of memory reporting
 237 void report_java_out_of_memory(const char* message);
 238 
 239 // Support for self-destruct
 240 bool is_error_reported();
 241 void set_error_reported();
 242 
 243 /* Test vmassert(), fatal(), guarantee(), etc. */
 244 NOT_PRODUCT(void test_error_handler();)
 245 
 246 void pd_ps(frame f);
 247 void pd_obfuscate_location(char *buf, size_t buflen);
 248 
 249 class outputStream;
 250 void print_native_stack(outputStream* st, frame fr, Thread* t, char* buf, int buf_size);
 251 
 252 #endif // SHARE_VM_UTILITIES_DEBUG_HPP