debug.hpp 10.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28
#ifndef SHARE_VM_UTILITIES_DEBUG_HPP
#define SHARE_VM_UTILITIES_DEBUG_HPP

#include "utilities/globalDefinitions.hpp"
29
#include "prims/jvm.h"
30

31 32 33
#include <stdarg.h>

// Simple class to format the ctor arguments into a fixed-sized buffer.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
class FormatBufferBase {
 protected:
  char* _buf;
  inline FormatBufferBase(char* buf) : _buf(buf) {}
 public:
  operator const char *() const { return _buf; }
};

// Use resource area for buffer
#define RES_BUFSZ 256
class FormatBufferResource : public FormatBufferBase {
 public:
  FormatBufferResource(const char * format, ...);
};

// Use stack for buffer
50
template <size_t bufsz = 256>
51
class FormatBuffer : public FormatBufferBase {
52
 public:
53
  inline FormatBuffer(const char * format, ...);
54
  inline void append(const char* format, ...);
55 56
  inline void print(const char* format, ...);
  inline void printv(const char* format, va_list ap);
57

58 59 60 61
  char* buffer() { return _buf; }
  int size() { return bufsz; }

 private:
62
  FormatBuffer(const FormatBuffer &); // prevent copies
63
  char _buffer[bufsz];
64

65 66
 protected:
  inline FormatBuffer();
67 68 69
};

template <size_t bufsz>
70
FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) {
71 72
  va_list argp;
  va_start(argp, format);
T
tonyp 已提交
73
  jio_vsnprintf(_buf, bufsz, format, argp);
74 75 76
  va_end(argp);
}

77
template <size_t bufsz>
78
FormatBuffer<bufsz>::FormatBuffer() : FormatBufferBase(_buffer) {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  _buf[0] = '\0';
}

template <size_t bufsz>
void FormatBuffer<bufsz>::print(const char * format, ...) {
  va_list argp;
  va_start(argp, format);
  jio_vsnprintf(_buf, bufsz, format, argp);
  va_end(argp);
}

template <size_t bufsz>
void FormatBuffer<bufsz>::printv(const char * format, va_list argp) {
  jio_vsnprintf(_buf, bufsz, format, argp);
}

95 96 97 98 99 100 101 102 103
template <size_t bufsz>
void FormatBuffer<bufsz>::append(const char* format, ...) {
  // Given that the constructor does a vsnprintf we can assume that
  // _buf is already initialized.
  size_t len = strlen(_buf);
  char* buf_end = _buf + len;

  va_list argp;
  va_start(argp, format);
T
tonyp 已提交
104
  jio_vsnprintf(buf_end, bufsz - len, format, argp);
105 106 107
  va_end(argp);
}

108 109
// Used to format messages for assert(), guarantee(), fatal(), etc.
typedef FormatBuffer<> err_msg;
110
typedef FormatBufferResource err_msg_res;
111

D
duke 已提交
112 113
// assertions
#ifdef ASSERT
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#ifndef USE_REPEATED_ASSERTS
#define assert(p, msg)                                                       \
do {                                                                         \
  if (!(p)) {                                                                \
    report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg);       \
    BREAKPOINT;                                                              \
  }                                                                          \
} while (0)
#else // #ifndef USE_REPEATED_ASSERTS
#define assert(p, msg)
do {                                                                         \
  for (int __i = 0; __i < AssertRepeat; __i++) {                             \
    if (!(p)) {                                                              \
      report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg);     \
      BREAKPOINT;                                                            \
    }                                                                        \
  }                                                                          \
} while (0)
#endif // #ifndef USE_REPEATED_ASSERTS
D
duke 已提交
133 134 135 136 137 138 139 140

// This version of assert is for use with checking return status from
// library calls that return actual error values eg. EINVAL,
// ENOMEM etc, rather than returning -1 and setting errno.
// When the status is not what is expected it is very useful to know
// what status was actually returned, so we pass the status variable as
// an extra arg and use strerror to convert it to a meaningful string
// like "Invalid argument", "out of memory" etc
141 142 143 144 145 146 147 148 149
#define assert_status(p, status, msg)                                        \
do {                                                                         \
  if (!(p)) {                                                                \
    report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed",             \
                    err_msg("error %s(%d) %s", strerror(status),             \
                            status, msg));                                   \
    BREAKPOINT;                                                              \
  }                                                                          \
} while (0)
D
duke 已提交
150 151 152

// Do not assert this condition if there's already another error reported.
#define assert_if_no_error(cond,msg) assert((cond) || is_error_reported(), msg)
153
#else // #ifdef ASSERT
D
duke 已提交
154 155 156
  #define assert(p,msg)
  #define assert_status(p,status,msg)
  #define assert_if_no_error(cond,msg)
157
#endif // #ifdef ASSERT
D
duke 已提交
158

159 160 161 162 163 164 165 166 167 168
// guarantee is like assert except it's always executed -- use it for
// cheap tests that catch errors that would otherwise be hard to find.
// guarantee is also used for Verify options.
#define guarantee(p, msg)                                                    \
do {                                                                         \
  if (!(p)) {                                                                \
    report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", msg);    \
    BREAKPOINT;                                                              \
  }                                                                          \
} while (0)
D
duke 已提交
169

170 171 172 173 174
#define fatal(msg)                                                           \
do {                                                                         \
  report_fatal(__FILE__, __LINE__, msg);                                     \
  BREAKPOINT;                                                                \
} while (0)
D
duke 已提交
175 176

// out of memory
177
#define vm_exit_out_of_memory(size, vm_err_type, msg)                        \
178
do {                                                                         \
179
  report_vm_out_of_memory(__FILE__, __LINE__, size, vm_err_type, msg);       \
180 181
  BREAKPOINT;                                                                \
} while (0)
D
duke 已提交
182

183 184 185 186 187 188 189 190 191 192 193
#define ShouldNotCallThis()                                                  \
do {                                                                         \
  report_should_not_call(__FILE__, __LINE__);                                \
  BREAKPOINT;                                                                \
} while (0)

#define ShouldNotReachHere()                                                 \
do {                                                                         \
  report_should_not_reach_here(__FILE__, __LINE__);                          \
  BREAKPOINT;                                                                \
} while (0)
D
duke 已提交
194

195 196 197 198 199 200 201 202 203 204 205
#define Unimplemented()                                                      \
do {                                                                         \
  report_unimplemented(__FILE__, __LINE__);                                  \
  BREAKPOINT;                                                                \
} while (0)

#define Untested(msg)                                                        \
do {                                                                         \
  report_untested(__FILE__, __LINE__, msg);                                  \
  BREAKPOINT;                                                                \
} while (0);
D
duke 已提交
206

207 208 209 210 211 212 213 214

// types of VM error - originally in vmError.hpp
enum VMErrorType {
  INTERNAL_ERROR   = 0xe0000000,
  OOM_MALLOC_ERROR = 0xe0000001,
  OOM_MMAP_ERROR   = 0xe0000002
};

D
duke 已提交
215
// error reporting helper functions
216 217 218 219
void report_vm_error(const char* file, int line, const char* error_msg,
                     const char* detail_msg = NULL);
void report_fatal(const char* file, int line, const char* message);
void report_vm_out_of_memory(const char* file, int line, size_t size,
220
                             VMErrorType vm_err_type, const char* message);
221 222 223 224 225
void report_should_not_call(const char* file, int line);
void report_should_not_reach_here(const char* file, int line);
void report_unimplemented(const char* file, int line);
void report_untested(const char* file, int line, const char* message);

D
duke 已提交
226 227
void warning(const char* format, ...);

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
#ifdef ASSERT
// Compile-time asserts.
template <bool> struct StaticAssert;
template <> struct StaticAssert<true> {};

// Only StaticAssert<true> is defined, so if cond evaluates to false we get
// a compile time exception when trying to use StaticAssert<false>.
#define STATIC_ASSERT(cond)                   \
  do {                                        \
    StaticAssert<(cond)> DUMMY_STATIC_ASSERT; \
    (void)DUMMY_STATIC_ASSERT; /* ignore */   \
  } while (false)
#else
#define STATIC_ASSERT(cond)
#endif

244 245 246 247 248 249 250 251 252 253
// out of shared space reporting
enum SharedSpaceType {
  SharedPermGen,
  SharedReadOnly,
  SharedReadWrite,
  SharedMiscData
};

void report_out_of_shared_space(SharedSpaceType space_type);

D
duke 已提交
254 255 256 257 258 259 260
// out of memory reporting
void report_java_out_of_memory(const char* message);

// Support for self-destruct
bool is_error_reported();
void set_error_reported();

261
/* Test assert(), fatal(), guarantee(), etc. */
262
NOT_PRODUCT(void test_error_handler();)
263

D
duke 已提交
264 265
void pd_ps(frame f);
void pd_obfuscate_location(char *buf, size_t buflen);
266 267

#endif // SHARE_VM_UTILITIES_DEBUG_HPP