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