1.1 --- a/src/share/vm/utilities/debug.hpp Sun Oct 11 16:19:25 2009 -0700 1.2 +++ b/src/share/vm/utilities/debug.hpp Thu Apr 22 13:23:15 2010 -0700 1.3 @@ -22,28 +22,54 @@ 1.4 * 1.5 */ 1.6 1.7 +#include <stdarg.h> 1.8 + 1.9 +// Simple class to format the ctor arguments into a fixed-sized buffer. 1.10 +template <size_t bufsz = 256> 1.11 +class FormatBuffer { 1.12 +public: 1.13 + inline FormatBuffer(const char * format, ...); 1.14 + operator const char *() const { return _buf; } 1.15 + 1.16 +private: 1.17 + FormatBuffer(const FormatBuffer &); // prevent copies 1.18 + 1.19 +private: 1.20 + char _buf[bufsz]; 1.21 +}; 1.22 + 1.23 +template <size_t bufsz> 1.24 +FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) { 1.25 + va_list argp; 1.26 + va_start(argp, format); 1.27 + vsnprintf(_buf, bufsz, format, argp); 1.28 + va_end(argp); 1.29 +} 1.30 + 1.31 +// Used to format messages for assert(), guarantee(), fatal(), etc. 1.32 +typedef FormatBuffer<> err_msg; 1.33 + 1.34 // assertions 1.35 #ifdef ASSERT 1.36 -// Turn this off by default: 1.37 -//#define USE_REPEATED_ASSERTS 1.38 -#ifdef USE_REPEATED_ASSERTS 1.39 - #define assert(p,msg) \ 1.40 - { for (int __i = 0; __i < AssertRepeat; __i++) { \ 1.41 - if (!(p)) { \ 1.42 - report_assertion_failure(__FILE__, __LINE__, \ 1.43 - "assert(" XSTR(p) ",\"" msg "\")");\ 1.44 - BREAKPOINT; \ 1.45 - } \ 1.46 - } \ 1.47 - } 1.48 -#else 1.49 - #define assert(p,msg) \ 1.50 - if (!(p)) { \ 1.51 - report_assertion_failure(__FILE__, __LINE__, \ 1.52 - "assert(" XSTR(p) ",\"" msg "\")");\ 1.53 - BREAKPOINT; \ 1.54 - } 1.55 -#endif 1.56 +#ifndef USE_REPEATED_ASSERTS 1.57 +#define assert(p, msg) \ 1.58 +do { \ 1.59 + if (!(p)) { \ 1.60 + report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \ 1.61 + BREAKPOINT; \ 1.62 + } \ 1.63 +} while (0) 1.64 +#else // #ifndef USE_REPEATED_ASSERTS 1.65 +#define assert(p, msg) 1.66 +do { \ 1.67 + for (int __i = 0; __i < AssertRepeat; __i++) { \ 1.68 + if (!(p)) { \ 1.69 + report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \ 1.70 + BREAKPOINT; \ 1.71 + } \ 1.72 + } \ 1.73 +} while (0) 1.74 +#endif // #ifndef USE_REPEATED_ASSERTS 1.75 1.76 // This version of assert is for use with checking return status from 1.77 // library calls that return actual error values eg. EINVAL, 1.78 @@ -52,70 +78,83 @@ 1.79 // what status was actually returned, so we pass the status variable as 1.80 // an extra arg and use strerror to convert it to a meaningful string 1.81 // like "Invalid argument", "out of memory" etc 1.82 -#define assert_status(p, status, msg) \ 1.83 - do { \ 1.84 - if (!(p)) { \ 1.85 - char buf[128]; \ 1.86 - snprintf(buf, 127, \ 1.87 - "assert_status(" XSTR(p) ", error: %s(%d), \"" msg "\")" , \ 1.88 - strerror((status)), (status)); \ 1.89 - report_assertion_failure(__FILE__, __LINE__, buf); \ 1.90 - BREAKPOINT; \ 1.91 - } \ 1.92 - } while (0) 1.93 - 1.94 -// Another version of assert where the message is not a string literal 1.95 -// The boolean condition is not printed out because cpp doesn't like it. 1.96 -#define assert_msg(p, msg) \ 1.97 - if (!(p)) { \ 1.98 - report_assertion_failure(__FILE__, __LINE__, msg); \ 1.99 - BREAKPOINT; \ 1.100 - } 1.101 +#define assert_status(p, status, msg) \ 1.102 +do { \ 1.103 + if (!(p)) { \ 1.104 + report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", \ 1.105 + err_msg("error %s(%d) %s", strerror(status), \ 1.106 + status, msg)); \ 1.107 + BREAKPOINT; \ 1.108 + } \ 1.109 +} while (0) 1.110 1.111 // Do not assert this condition if there's already another error reported. 1.112 #define assert_if_no_error(cond,msg) assert((cond) || is_error_reported(), msg) 1.113 -#else 1.114 +#else // #ifdef ASSERT 1.115 #define assert(p,msg) 1.116 #define assert_status(p,status,msg) 1.117 #define assert_if_no_error(cond,msg) 1.118 - #define assert_msg(cond,msg) 1.119 -#endif 1.120 +#endif // #ifdef ASSERT 1.121 1.122 +// guarantee is like assert except it's always executed -- use it for 1.123 +// cheap tests that catch errors that would otherwise be hard to find. 1.124 +// guarantee is also used for Verify options. 1.125 +#define guarantee(p, msg) \ 1.126 +do { \ 1.127 + if (!(p)) { \ 1.128 + report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", msg); \ 1.129 + BREAKPOINT; \ 1.130 + } \ 1.131 +} while (0) 1.132 1.133 -// fatals 1.134 -#define fatal(m) { report_fatal(__FILE__, __LINE__, m ); BREAKPOINT; } 1.135 -#define fatal1(m,x1) { report_fatal_vararg(__FILE__, __LINE__, m, x1 ); BREAKPOINT; } 1.136 -#define fatal2(m,x1,x2) { report_fatal_vararg(__FILE__, __LINE__, m, x1, x2 ); BREAKPOINT; } 1.137 -#define fatal3(m,x1,x2,x3) { report_fatal_vararg(__FILE__, __LINE__, m, x1, x2, x3 ); BREAKPOINT; } 1.138 -#define fatal4(m,x1,x2,x3,x4) { report_fatal_vararg(__FILE__, __LINE__, m, x1, x2, x3, x4 ); BREAKPOINT; } 1.139 +#define fatal(msg) \ 1.140 +do { \ 1.141 + report_fatal(__FILE__, __LINE__, msg); \ 1.142 + BREAKPOINT; \ 1.143 +} while (0) 1.144 1.145 // out of memory 1.146 -#define vm_exit_out_of_memory(s,m) { report_vm_out_of_memory(__FILE__, __LINE__, s, m ); BREAKPOINT; } 1.147 -#define vm_exit_out_of_memory1(s,m,x1) { report_vm_out_of_memory_vararg(__FILE__, __LINE__, s, m, x1 ); BREAKPOINT; } 1.148 -#define vm_exit_out_of_memory2(s,m,x1,x2) { report_vm_out_of_memory_vararg(__FILE__, __LINE__, s, m, x1, x2 ); BREAKPOINT; } 1.149 -#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; } 1.150 -#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; } 1.151 +#define vm_exit_out_of_memory(size, msg) \ 1.152 +do { \ 1.153 + report_vm_out_of_memory(__FILE__, __LINE__, size, msg); \ 1.154 + BREAKPOINT; \ 1.155 +} while (0) 1.156 1.157 -// guarantee is like assert except it's always executed -- use it for 1.158 -// cheap tests that catch errors that would otherwise be hard to find 1.159 -// guarantee is also used for Verify options. 1.160 -#define guarantee(b,msg) { if (!(b)) fatal("guarantee(" XSTR(b) ",\"" msg "\")"); } 1.161 +#define ShouldNotCallThis() \ 1.162 +do { \ 1.163 + report_should_not_call(__FILE__, __LINE__); \ 1.164 + BREAKPOINT; \ 1.165 +} while (0) 1.166 1.167 -#define ShouldNotCallThis() { report_should_not_call (__FILE__, __LINE__); BREAKPOINT; } 1.168 -#define ShouldNotReachHere() { report_should_not_reach_here (__FILE__, __LINE__); BREAKPOINT; } 1.169 -#define Unimplemented() { report_unimplemented (__FILE__, __LINE__); BREAKPOINT; } 1.170 -#define Untested(msg) { report_untested (__FILE__, __LINE__, msg); BREAKPOINT; } 1.171 +#define ShouldNotReachHere() \ 1.172 +do { \ 1.173 + report_should_not_reach_here(__FILE__, __LINE__); \ 1.174 + BREAKPOINT; \ 1.175 +} while (0) 1.176 + 1.177 +#define Unimplemented() \ 1.178 +do { \ 1.179 + report_unimplemented(__FILE__, __LINE__); \ 1.180 + BREAKPOINT; \ 1.181 +} while (0) 1.182 + 1.183 +#define Untested(msg) \ 1.184 +do { \ 1.185 + report_untested(__FILE__, __LINE__, msg); \ 1.186 + BREAKPOINT; \ 1.187 +} while (0); 1.188 1.189 // error reporting helper functions 1.190 -void report_assertion_failure(const char* file_name, int line_no, const char* message); 1.191 -void report_fatal_vararg(const char* file_name, int line_no, const char* format, ...); 1.192 -void report_fatal(const char* file_name, int line_no, const char* message); 1.193 -void report_vm_out_of_memory_vararg(const char* file_name, int line_no, size_t size, const char* format, ...); 1.194 -void report_vm_out_of_memory(const char* file_name, int line_no, size_t size, const char* message); 1.195 -void report_should_not_call(const char* file_name, int line_no); 1.196 -void report_should_not_reach_here(const char* file_name, int line_no); 1.197 -void report_unimplemented(const char* file_name, int line_no); 1.198 -void report_untested(const char* file_name, int line_no, const char* msg); 1.199 +void report_vm_error(const char* file, int line, const char* error_msg, 1.200 + const char* detail_msg = NULL); 1.201 +void report_fatal(const char* file, int line, const char* message); 1.202 +void report_vm_out_of_memory(const char* file, int line, size_t size, 1.203 + const char* message); 1.204 +void report_should_not_call(const char* file, int line); 1.205 +void report_should_not_reach_here(const char* file, int line); 1.206 +void report_unimplemented(const char* file, int line); 1.207 +void report_untested(const char* file, int line, const char* message); 1.208 + 1.209 void warning(const char* format, ...); 1.210 1.211 // out of memory reporting 1.212 @@ -125,5 +164,8 @@ 1.213 bool is_error_reported(); 1.214 void set_error_reported(); 1.215 1.216 +/* Test assert(), fatal(), guarantee(), etc. */ 1.217 +NOT_PRODUCT(void test_error_handler(size_t test_num);) 1.218 + 1.219 void pd_ps(frame f); 1.220 void pd_obfuscate_location(char *buf, size_t buflen);