debug.hpp revision 1879:f95d63e2154a
1/*
2 * Copyright (c) 1997, 2010, 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#include <stdarg.h>
31
32// Simple class to format the ctor arguments into a fixed-sized buffer.
33template <size_t bufsz = 256>
34class FormatBuffer {
35public:
36  inline FormatBuffer(const char * format, ...);
37  operator const char *() const { return _buf; }
38
39private:
40  FormatBuffer(const FormatBuffer &); // prevent copies
41
42private:
43  char _buf[bufsz];
44};
45
46template <size_t bufsz>
47FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) {
48  va_list argp;
49  va_start(argp, format);
50  vsnprintf(_buf, bufsz, format, argp);
51  va_end(argp);
52}
53
54// Used to format messages for assert(), guarantee(), fatal(), etc.
55typedef FormatBuffer<> err_msg;
56
57// assertions
58#ifdef ASSERT
59#ifndef USE_REPEATED_ASSERTS
60#define assert(p, msg)                                                       \
61do {                                                                         \
62  if (!(p)) {                                                                \
63    report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg);       \
64    BREAKPOINT;                                                              \
65  }                                                                          \
66} while (0)
67#else // #ifndef USE_REPEATED_ASSERTS
68#define assert(p, msg)
69do {                                                                         \
70  for (int __i = 0; __i < AssertRepeat; __i++) {                             \
71    if (!(p)) {                                                              \
72      report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg);     \
73      BREAKPOINT;                                                            \
74    }                                                                        \
75  }                                                                          \
76} while (0)
77#endif // #ifndef USE_REPEATED_ASSERTS
78
79// This version of assert is for use with checking return status from
80// library calls that return actual error values eg. EINVAL,
81// ENOMEM etc, rather than returning -1 and setting errno.
82// When the status is not what is expected it is very useful to know
83// what status was actually returned, so we pass the status variable as
84// an extra arg and use strerror to convert it to a meaningful string
85// like "Invalid argument", "out of memory" etc
86#define assert_status(p, status, msg)                                        \
87do {                                                                         \
88  if (!(p)) {                                                                \
89    report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed",             \
90                    err_msg("error %s(%d) %s", strerror(status),             \
91                            status, msg));                                   \
92    BREAKPOINT;                                                              \
93  }                                                                          \
94} while (0)
95
96// Do not assert this condition if there's already another error reported.
97#define assert_if_no_error(cond,msg) assert((cond) || is_error_reported(), msg)
98#else // #ifdef ASSERT
99  #define assert(p,msg)
100  #define assert_status(p,status,msg)
101  #define assert_if_no_error(cond,msg)
102#endif // #ifdef ASSERT
103
104// guarantee is like assert except it's always executed -- use it for
105// cheap tests that catch errors that would otherwise be hard to find.
106// guarantee is also used for Verify options.
107#define guarantee(p, msg)                                                    \
108do {                                                                         \
109  if (!(p)) {                                                                \
110    report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", msg);    \
111    BREAKPOINT;                                                              \
112  }                                                                          \
113} while (0)
114
115#define fatal(msg)                                                           \
116do {                                                                         \
117  report_fatal(__FILE__, __LINE__, msg);                                     \
118  BREAKPOINT;                                                                \
119} while (0)
120
121// out of memory
122#define vm_exit_out_of_memory(size, msg)                                     \
123do {                                                                         \
124  report_vm_out_of_memory(__FILE__, __LINE__, size, msg);                    \
125  BREAKPOINT;                                                                \
126} while (0)
127
128#define ShouldNotCallThis()                                                  \
129do {                                                                         \
130  report_should_not_call(__FILE__, __LINE__);                                \
131  BREAKPOINT;                                                                \
132} while (0)
133
134#define ShouldNotReachHere()                                                 \
135do {                                                                         \
136  report_should_not_reach_here(__FILE__, __LINE__);                          \
137  BREAKPOINT;                                                                \
138} while (0)
139
140#define Unimplemented()                                                      \
141do {                                                                         \
142  report_unimplemented(__FILE__, __LINE__);                                  \
143  BREAKPOINT;                                                                \
144} while (0)
145
146#define Untested(msg)                                                        \
147do {                                                                         \
148  report_untested(__FILE__, __LINE__, msg);                                  \
149  BREAKPOINT;                                                                \
150} while (0);
151
152// error reporting helper functions
153void report_vm_error(const char* file, int line, const char* error_msg,
154                     const char* detail_msg = NULL);
155void report_fatal(const char* file, int line, const char* message);
156void report_vm_out_of_memory(const char* file, int line, size_t size,
157                             const char* message);
158void report_should_not_call(const char* file, int line);
159void report_should_not_reach_here(const char* file, int line);
160void report_unimplemented(const char* file, int line);
161void report_untested(const char* file, int line, const char* message);
162
163void warning(const char* format, ...);
164
165// out of memory reporting
166void report_java_out_of_memory(const char* message);
167
168// Support for self-destruct
169bool is_error_reported();
170void set_error_reported();
171
172/* Test assert(), fatal(), guarantee(), etc. */
173NOT_PRODUCT(void test_error_handler(size_t test_num);)
174
175void pd_ps(frame f);
176void pd_obfuscate_location(char *buf, size_t buflen);
177
178#endif // SHARE_VM_UTILITIES_DEBUG_HPP
179