Old src/share/vm/utilities/debug.hpp (original) (raw)
1 /*
2 * Copyright (c) 1997, 2015, 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 // Use stack for buffer
50 template
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
70 FormatBuffer::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
78 FormatBuffer::FormatBuffer() : FormatBufferBase(_buffer) {
79 _buf[0] = '\0';
80 }
81
82 template
83 void FormatBuffer::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
91 void FormatBuffer::printv(const char * format, va_list argp) {
92 jio_vsnprintf(_buf, bufsz, format, argp);
93 }
94
95 template
96 void FormatBuffer::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.
109 typedef FormatBuffer<> err_msg;
110
111 // assertions
112 #ifndef ASSERT
113 #define vmassert(p, ...)
114 #else
115 // Note: message says "assert" rather than "vmassert" for backward
116 // compatibility with tools that parse/match the message text.
117 // Note: The signature is vmassert(p, format, ...), but the solaris
118 // compiler can't handle an empty ellipsis in a macro without a warning.
119 #define vmassert(p, ...)
120 do {
121 if (!(p)) {
122 report_vm_error(FILE, LINE, "assert(" #p ") failed", VA_ARGS);
123 BREAKPOINT;
124 }
125 } while (0)
126
127 #endif
128
129 // For backward compatibility.
130 #define assert(p, ...) vmassert(p, VA_ARGS)
131
132 // This version of vmassert is for use with checking return status from
133 // library calls that return actual error values eg. EINVAL,
134 // ENOMEM etc, rather than returning -1 and setting errno.
135 // When the status is not what is expected it is very useful to know
136 // what status was actually returned, so we pass the status variable as
137 // an extra arg and use strerror to convert it to a meaningful string
138 // like "Invalid argument", "out of memory" etc
139 #define vmassert_status(p, status, msg)
140 vmassert(p, "error %s(%d), %s", strerror(status), status, msg)
141
142 // For backward compatibility.
143 #define assert_status(p, status, msg) vmassert_status(p, status, msg)
144
145 // guarantee is like vmassert except it's always executed -- use it for
146 // cheap tests that catch errors that would otherwise be hard to find.
147 // guarantee is also used for Verify options.
148 #define guarantee(p, ...)
149 do {
150 if (!(p)) {
151 report_vm_error(FILE, LINE, "guarantee(" #p ") failed", VA_ARGS);
152 BREAKPOINT;
153 }
154 } while (0)
155
156 #define fatal(...)
157 do {
158 report_fatal(FILE, LINE, VA_ARGS);
159 BREAKPOINT;
160 } while (0)
161
162 // out of memory
163 #define vm_exit_out_of_memory(size, vm_err_type, ...)
164 do {
165 report_vm_out_of_memory(FILE, LINE, size, vm_err_type, VA_ARGS);
166 BREAKPOINT;
167 } while (0)
168
169 #define ShouldNotCallThis()
170 do {
171 report_should_not_call(FILE, LINE);
172 BREAKPOINT;
173 } while (0)
174
175 #define ShouldNotReachHere()
176 do {
177 report_should_not_reach_here(FILE, LINE);
178 BREAKPOINT;
179 } while (0)
180
181 #define Unimplemented()
182 do {
183 report_unimplemented(FILE, LINE);
184 BREAKPOINT;
185 } while (0)
186
187 #define Untested(msg)
188 do {
189 report_untested(FILE, LINE, msg);
190 BREAKPOINT;
191 } while (0);
192
193
194 // types of VM error - originally in vmError.hpp
195 enum VMErrorType {
196 INTERNAL_ERROR = 0xe0000000,
197 OOM_MALLOC_ERROR = 0xe0000001,
198 OOM_MMAP_ERROR = 0xe0000002
199 };
200
201 // error reporting helper functions
202 void report_vm_error(const char* file, int line, const char* error_msg);
203 #if !defined(GNUC) || defined (clang_major) || (((GNUC == 4) && (GNUC_MINOR >= 8)) || GNUC > 4)
204 // ATTRIBUTE_PRINTF works with gcc >= 4.8 and any other compiler.
205 void report_vm_error(const char* file, int line, const char* error_msg,
206 const char* detail_fmt, ...) ATTRIBUTE_PRINTF(4, 5);
207 #else
208 // GCC < 4.8 warns because of empty format string. Warning can not be switched off selectively.
209 void report_vm_error(const char* file, int line, const char* error_msg,
210 const char* detail_fmt, ...);
211 #endif
212 void report_fatal(const char* file, int line, const char* detail_fmt, ...) ATTRIBUTE_PRINTF(3, 4);
213 void report_vm_out_of_memory(const char* file, int line, size_t size, VMErrorType vm_err_type,
214 const char* detail_fmt, ...) ATTRIBUTE_PRINTF(5, 6);
215 void report_should_not_call(const char* file, int line);
216 void report_should_not_reach_here(const char* file, int line);
217 void report_unimplemented(const char* file, int line);
218 void report_untested(const char* file, int line, const char* message);
219
220 void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2);
221
222 // Compile-time asserts. Cond must be a compile-time constant expression that
223 // is convertible to bool. STATIC_ASSERT() can be used anywhere a declaration
224 // may appear.
225 //
226 // Implementation Note: STATIC_ASSERT_FAILURE provides a value member
227 // rather than type member that could be used directly in the typedef, because
228 // a type member would require conditional use of "typename", depending on
229 // whether Cond is dependent or not. The use of a value member leads to the
230 // use of an array type.
231
232 template struct STATIC_ASSERT_FAILURE;
233 template<> struct STATIC_ASSERT_FAILURE { enum { value = 1 }; };
234
235 #define STATIC_ASSERT(Cond)
236 typedef char PASTE_TOKENS(STATIC_ASSERT_DUMMY_TYPE, LINE)[
237 STATIC_ASSERT_FAILURE< (Cond) >::value ]
238
239 // out of shared space reporting
240 enum SharedSpaceType {
241 SharedReadOnly,
242 SharedReadWrite,
243 SharedMiscData,
244 SharedMiscCode
245 };
246
247 void report_out_of_shared_space(SharedSpaceType space_type);
248
249 void report_insufficient_metaspace(size_t required_size);
250
251 // out of memory reporting
252 void report_java_out_of_memory(const char* message);
253
254 // Support for self-destruct
255 bool is_error_reported();
256 void set_error_reported();
257
258 /* Test vmassert(), fatal(), guarantee(), etc. /
259 NOT_PRODUCT(void test_error_handler();)
260
261 // crash in a controlled way:
262 // how can be one of:
263 // 1,2 - asserts
264 // 3,4 - guarantee
265 // 5-7 - fatal
266 // 8 - vm_exit_out_of_memory
267 // 9 - ShouldNotCallThis
268 // 10 - ShouldNotReachHere
269 // 11 - Unimplemented
270 // 12,13 - (not guaranteed) crashes
271 // 14 - SIGSEGV
272 // 15 - SIGFPE
273 NOT_PRODUCT(void controlled_crash(int how);)
274
275 // returns an address which is guaranteed to generate a SIGSEGV on read,
276 // for test purposes, which is not NULL and contains bits in every word
277 NOT_PRODUCT(void get_segfault_address();)
278
279 void pd_ps(frame f);
280 void pd_obfuscate_location(char buf, size_t buflen);
281
282 class outputStream;
283 void print_native_stack(outputStream st, frame fr, Thread* t, char* buf, int buf_size);
284
285 #endif // SHARE_VM_UTILITIES_DEBUG_HPP