comparison src/share/vm/utilities/debug.hpp @ 1490:f03d0a26bf83

6888954: argument formatting for assert() and friends Reviewed-by: kvn, twisti, apetrusenko, never, dcubed
author jcoomes
date Thu, 22 Apr 2010 13:23:15 -0700
parents a61af66fc99e
children c18cbe5936b8
comparison
equal deleted inserted replaced
1489:cff162798819 1490:f03d0a26bf83
20 * CA 95054 USA or visit www.sun.com if you need additional information or 20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions. 21 * have any questions.
22 * 22 *
23 */ 23 */
24 24
25 #include <stdarg.h>
26
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; }
33
34 private:
35 FormatBuffer(const FormatBuffer &); // prevent copies
36
37 private:
38 char _buf[bufsz];
39 };
40
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 }
48
49 // Used to format messages for assert(), guarantee(), fatal(), etc.
50 typedef FormatBuffer<> err_msg;
51
25 // assertions 52 // assertions
26 #ifdef ASSERT 53 #ifdef ASSERT
27 // Turn this off by default: 54 #ifndef USE_REPEATED_ASSERTS
28 //#define USE_REPEATED_ASSERTS 55 #define assert(p, msg) \
29 #ifdef USE_REPEATED_ASSERTS 56 do { \
30 #define assert(p,msg) \ 57 if (!(p)) { \
31 { for (int __i = 0; __i < AssertRepeat; __i++) { \ 58 report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \
32 if (!(p)) { \ 59 BREAKPOINT; \
33 report_assertion_failure(__FILE__, __LINE__, \ 60 } \
34 "assert(" XSTR(p) ",\"" msg "\")");\ 61 } while (0)
35 BREAKPOINT; \ 62 #else // #ifndef USE_REPEATED_ASSERTS
36 } \ 63 #define assert(p, msg)
37 } \ 64 do { \
38 } 65 for (int __i = 0; __i < AssertRepeat; __i++) { \
39 #else 66 if (!(p)) { \
40 #define assert(p,msg) \ 67 report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \
41 if (!(p)) { \ 68 BREAKPOINT; \
42 report_assertion_failure(__FILE__, __LINE__, \ 69 } \
43 "assert(" XSTR(p) ",\"" msg "\")");\ 70 } \
44 BREAKPOINT; \ 71 } while (0)
45 } 72 #endif // #ifndef USE_REPEATED_ASSERTS
46 #endif
47 73
48 // This version of assert is for use with checking return status from 74 // This version of assert is for use with checking return status from
49 // library calls that return actual error values eg. EINVAL, 75 // library calls that return actual error values eg. EINVAL,
50 // ENOMEM etc, rather than returning -1 and setting errno. 76 // ENOMEM etc, rather than returning -1 and setting errno.
51 // When the status is not what is expected it is very useful to know 77 // When the status is not what is expected it is very useful to know
52 // what status was actually returned, so we pass the status variable as 78 // what status was actually returned, so we pass the status variable as
53 // an extra arg and use strerror to convert it to a meaningful string 79 // an extra arg and use strerror to convert it to a meaningful string
54 // like "Invalid argument", "out of memory" etc 80 // like "Invalid argument", "out of memory" etc
55 #define assert_status(p, status, msg) \ 81 #define assert_status(p, status, msg) \
56 do { \ 82 do { \
57 if (!(p)) { \ 83 if (!(p)) { \
58 char buf[128]; \ 84 report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", \
59 snprintf(buf, 127, \ 85 err_msg("error %s(%d) %s", strerror(status), \
60 "assert_status(" XSTR(p) ", error: %s(%d), \"" msg "\")" , \ 86 status, msg)); \
61 strerror((status)), (status)); \ 87 BREAKPOINT; \
62 report_assertion_failure(__FILE__, __LINE__, buf); \ 88 } \
63 BREAKPOINT; \ 89 } while (0)
64 } \
65 } while (0)
66
67 // Another version of assert where the message is not a string literal
68 // The boolean condition is not printed out because cpp doesn't like it.
69 #define assert_msg(p, msg) \
70 if (!(p)) { \
71 report_assertion_failure(__FILE__, __LINE__, msg); \
72 BREAKPOINT; \
73 }
74 90
75 // Do not assert this condition if there's already another error reported. 91 // Do not assert this condition if there's already another error reported.
76 #define assert_if_no_error(cond,msg) assert((cond) || is_error_reported(), msg) 92 #define assert_if_no_error(cond,msg) assert((cond) || is_error_reported(), msg)
77 #else 93 #else // #ifdef ASSERT
78 #define assert(p,msg) 94 #define assert(p,msg)
79 #define assert_status(p,status,msg) 95 #define assert_status(p,status,msg)
80 #define assert_if_no_error(cond,msg) 96 #define assert_if_no_error(cond,msg)
81 #define assert_msg(cond,msg) 97 #endif // #ifdef ASSERT
82 #endif
83 98
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)
84 109
85 // fatals 110 #define fatal(msg) \
86 #define fatal(m) { report_fatal(__FILE__, __LINE__, m ); BREAKPOINT; } 111 do { \
87 #define fatal1(m,x1) { report_fatal_vararg(__FILE__, __LINE__, m, x1 ); BREAKPOINT; } 112 report_fatal(__FILE__, __LINE__, msg); \
88 #define fatal2(m,x1,x2) { report_fatal_vararg(__FILE__, __LINE__, m, x1, x2 ); BREAKPOINT; } 113 BREAKPOINT; \
89 #define fatal3(m,x1,x2,x3) { report_fatal_vararg(__FILE__, __LINE__, m, x1, x2, x3 ); BREAKPOINT; } 114 } while (0)
90 #define fatal4(m,x1,x2,x3,x4) { report_fatal_vararg(__FILE__, __LINE__, m, x1, x2, x3, x4 ); BREAKPOINT; }
91 115
92 // out of memory 116 // out of memory
93 #define vm_exit_out_of_memory(s,m) { report_vm_out_of_memory(__FILE__, __LINE__, s, m ); BREAKPOINT; } 117 #define vm_exit_out_of_memory(size, msg) \
94 #define vm_exit_out_of_memory1(s,m,x1) { report_vm_out_of_memory_vararg(__FILE__, __LINE__, s, m, x1 ); BREAKPOINT; } 118 do { \
95 #define vm_exit_out_of_memory2(s,m,x1,x2) { report_vm_out_of_memory_vararg(__FILE__, __LINE__, s, m, x1, x2 ); BREAKPOINT; } 119 report_vm_out_of_memory(__FILE__, __LINE__, size, msg); \
96 #define vm_exit_out_of_memory3(s,m,x1,x2,x3) { report_vm_out_of_memory_vararg(__FILE__, __LINE__, s, m, x1, x2, x3 ); BREAKPOINT; } 120 BREAKPOINT; \
97 #define vm_exit_out_of_memory4(s,m,x1,x2,x3,x4) { report_vm_out_of_memory_vararg(__FILE__, __LINE__, s, m, x1, x2, x3, x4); BREAKPOINT; } 121 } while (0)
98 122
99 // guarantee is like assert except it's always executed -- use it for 123 #define ShouldNotCallThis() \
100 // cheap tests that catch errors that would otherwise be hard to find 124 do { \
101 // guarantee is also used for Verify options. 125 report_should_not_call(__FILE__, __LINE__); \
102 #define guarantee(b,msg) { if (!(b)) fatal("guarantee(" XSTR(b) ",\"" msg "\")"); } 126 BREAKPOINT; \
127 } while (0)
103 128
104 #define ShouldNotCallThis() { report_should_not_call (__FILE__, __LINE__); BREAKPOINT; } 129 #define ShouldNotReachHere() \
105 #define ShouldNotReachHere() { report_should_not_reach_here (__FILE__, __LINE__); BREAKPOINT; } 130 do { \
106 #define Unimplemented() { report_unimplemented (__FILE__, __LINE__); BREAKPOINT; } 131 report_should_not_reach_here(__FILE__, __LINE__); \
107 #define Untested(msg) { report_untested (__FILE__, __LINE__, msg); BREAKPOINT; } 132 BREAKPOINT; \
133 } while (0)
134
135 #define Unimplemented() \
136 do { \
137 report_unimplemented(__FILE__, __LINE__); \
138 BREAKPOINT; \
139 } while (0)
140
141 #define Untested(msg) \
142 do { \
143 report_untested(__FILE__, __LINE__, msg); \
144 BREAKPOINT; \
145 } while (0);
108 146
109 // error reporting helper functions 147 // error reporting helper functions
110 void report_assertion_failure(const char* file_name, int line_no, const char* message); 148 void report_vm_error(const char* file, int line, const char* error_msg,
111 void report_fatal_vararg(const char* file_name, int line_no, const char* format, ...); 149 const char* detail_msg = NULL);
112 void report_fatal(const char* file_name, int line_no, const char* message); 150 void report_fatal(const char* file, int line, const char* message);
113 void report_vm_out_of_memory_vararg(const char* file_name, int line_no, size_t size, const char* format, ...); 151 void report_vm_out_of_memory(const char* file, int line, size_t size,
114 void report_vm_out_of_memory(const char* file_name, int line_no, size_t size, const char* message); 152 const char* message);
115 void report_should_not_call(const char* file_name, int line_no); 153 void report_should_not_call(const char* file, int line);
116 void report_should_not_reach_here(const char* file_name, int line_no); 154 void report_should_not_reach_here(const char* file, int line);
117 void report_unimplemented(const char* file_name, int line_no); 155 void report_unimplemented(const char* file, int line);
118 void report_untested(const char* file_name, int line_no, const char* msg); 156 void report_untested(const char* file, int line, const char* message);
157
119 void warning(const char* format, ...); 158 void warning(const char* format, ...);
120 159
121 // out of memory reporting 160 // out of memory reporting
122 void report_java_out_of_memory(const char* message); 161 void report_java_out_of_memory(const char* message);
123 162
124 // Support for self-destruct 163 // Support for self-destruct
125 bool is_error_reported(); 164 bool is_error_reported();
126 void set_error_reported(); 165 void set_error_reported();
127 166
167 /* Test assert(), fatal(), guarantee(), etc. */
168 NOT_PRODUCT(void test_error_handler(size_t test_num);)
169
128 void pd_ps(frame f); 170 void pd_ps(frame f);
129 void pd_obfuscate_location(char *buf, size_t buflen); 171 void pd_obfuscate_location(char *buf, size_t buflen);