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