duke@435: /* duke@435: * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // assertions duke@435: #ifdef ASSERT duke@435: // Turn this off by default: duke@435: //#define USE_REPEATED_ASSERTS duke@435: #ifdef USE_REPEATED_ASSERTS duke@435: #define assert(p,msg) \ duke@435: { for (int __i = 0; __i < AssertRepeat; __i++) { \ duke@435: if (!(p)) { \ duke@435: report_assertion_failure(__FILE__, __LINE__, \ duke@435: "assert(" XSTR(p) ",\"" msg "\")");\ duke@435: BREAKPOINT; \ duke@435: } \ duke@435: } \ duke@435: } duke@435: #else duke@435: #define assert(p,msg) \ duke@435: if (!(p)) { \ duke@435: report_assertion_failure(__FILE__, __LINE__, \ duke@435: "assert(" XSTR(p) ",\"" msg "\")");\ duke@435: BREAKPOINT; \ duke@435: } duke@435: #endif duke@435: duke@435: // This version of assert is for use with checking return status from duke@435: // library calls that return actual error values eg. EINVAL, duke@435: // ENOMEM etc, rather than returning -1 and setting errno. duke@435: // When the status is not what is expected it is very useful to know duke@435: // what status was actually returned, so we pass the status variable as duke@435: // an extra arg and use strerror to convert it to a meaningful string duke@435: // like "Invalid argument", "out of memory" etc duke@435: #define assert_status(p, status, msg) \ duke@435: do { \ duke@435: if (!(p)) { \ duke@435: char buf[128]; \ duke@435: snprintf(buf, 127, \ duke@435: "assert_status(" XSTR(p) ", error: %s(%d), \"" msg "\")" , \ duke@435: strerror((status)), (status)); \ duke@435: report_assertion_failure(__FILE__, __LINE__, buf); \ duke@435: BREAKPOINT; \ duke@435: } \ duke@435: } while (0) duke@435: duke@435: // Another version of assert where the message is not a string literal duke@435: // The boolean condition is not printed out because cpp doesn't like it. duke@435: #define assert_msg(p, msg) \ duke@435: if (!(p)) { \ duke@435: report_assertion_failure(__FILE__, __LINE__, msg); \ duke@435: BREAKPOINT; \ duke@435: } duke@435: duke@435: // Do not assert this condition if there's already another error reported. duke@435: #define assert_if_no_error(cond,msg) assert((cond) || is_error_reported(), msg) duke@435: #else duke@435: #define assert(p,msg) duke@435: #define assert_status(p,status,msg) duke@435: #define assert_if_no_error(cond,msg) duke@435: #define assert_msg(cond,msg) duke@435: #endif duke@435: duke@435: duke@435: // fatals duke@435: #define fatal(m) { report_fatal(__FILE__, __LINE__, m ); BREAKPOINT; } duke@435: #define fatal1(m,x1) { report_fatal_vararg(__FILE__, __LINE__, m, x1 ); BREAKPOINT; } duke@435: #define fatal2(m,x1,x2) { report_fatal_vararg(__FILE__, __LINE__, m, x1, x2 ); BREAKPOINT; } duke@435: #define fatal3(m,x1,x2,x3) { report_fatal_vararg(__FILE__, __LINE__, m, x1, x2, x3 ); BREAKPOINT; } duke@435: #define fatal4(m,x1,x2,x3,x4) { report_fatal_vararg(__FILE__, __LINE__, m, x1, x2, x3, x4 ); BREAKPOINT; } duke@435: duke@435: // out of memory duke@435: #define vm_exit_out_of_memory(s,m) { report_vm_out_of_memory(__FILE__, __LINE__, s, m ); BREAKPOINT; } duke@435: #define vm_exit_out_of_memory1(s,m,x1) { report_vm_out_of_memory_vararg(__FILE__, __LINE__, s, m, x1 ); BREAKPOINT; } duke@435: #define vm_exit_out_of_memory2(s,m,x1,x2) { report_vm_out_of_memory_vararg(__FILE__, __LINE__, s, m, x1, x2 ); BREAKPOINT; } duke@435: #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; } duke@435: #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; } duke@435: duke@435: // guarantee is like assert except it's always executed -- use it for duke@435: // cheap tests that catch errors that would otherwise be hard to find duke@435: // guarantee is also used for Verify options. duke@435: #define guarantee(b,msg) { if (!(b)) fatal("guarantee(" XSTR(b) ",\"" msg "\")"); } duke@435: duke@435: #define ShouldNotCallThis() { report_should_not_call (__FILE__, __LINE__); BREAKPOINT; } duke@435: #define ShouldNotReachHere() { report_should_not_reach_here (__FILE__, __LINE__); BREAKPOINT; } duke@435: #define Unimplemented() { report_unimplemented (__FILE__, __LINE__); BREAKPOINT; } duke@435: #define Untested(msg) { report_untested (__FILE__, __LINE__, msg); BREAKPOINT; } duke@435: duke@435: // error reporting helper functions duke@435: void report_assertion_failure(const char* file_name, int line_no, const char* message); duke@435: void report_fatal_vararg(const char* file_name, int line_no, const char* format, ...); duke@435: void report_fatal(const char* file_name, int line_no, const char* message); duke@435: void report_vm_out_of_memory_vararg(const char* file_name, int line_no, size_t size, const char* format, ...); duke@435: void report_vm_out_of_memory(const char* file_name, int line_no, size_t size, const char* message); duke@435: void report_should_not_call(const char* file_name, int line_no); duke@435: void report_should_not_reach_here(const char* file_name, int line_no); duke@435: void report_unimplemented(const char* file_name, int line_no); duke@435: void report_untested(const char* file_name, int line_no, const char* msg); duke@435: void warning(const char* format, ...); duke@435: duke@435: // out of memory reporting duke@435: void report_java_out_of_memory(const char* message); duke@435: duke@435: // Support for self-destruct duke@435: bool is_error_reported(); duke@435: void set_error_reported(); duke@435: duke@435: void pd_ps(frame f); duke@435: void pd_obfuscate_location(char *buf, size_t buflen);