debug.hpp revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 1997, 2007, 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#include <stdarg.h> 26 27// Simple class to format the ctor arguments into a fixed-sized buffer. 28template <size_t bufsz = 256> 29class FormatBuffer { 30public: 31 inline FormatBuffer(const char * format, ...); 32 operator const char *() const { return _buf; } 33 34private: 35 FormatBuffer(const FormatBuffer &); // prevent copies 36 37private: 38 char _buf[bufsz]; 39}; 40 41template <size_t bufsz> 42FormatBuffer<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. 50typedef FormatBuffer<> err_msg; 51 52// assertions 53#ifdef ASSERT 54#ifndef USE_REPEATED_ASSERTS 55#define assert(p, msg) \ 56do { \ 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) 64do { \ 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 73 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) \ 82do { \ 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) 90 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 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) \ 103do { \ 104 if (!(p)) { \ 105 report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", msg); \ 106 BREAKPOINT; \ 107 } \ 108} while (0) 109 110#define fatal(msg) \ 111do { \ 112 report_fatal(__FILE__, __LINE__, msg); \ 113 BREAKPOINT; \ 114} while (0) 115 116// out of memory 117#define vm_exit_out_of_memory(size, msg) \ 118do { \ 119 report_vm_out_of_memory(__FILE__, __LINE__, size, msg); \ 120 BREAKPOINT; \ 121} while (0) 122 123#define ShouldNotCallThis() \ 124do { \ 125 report_should_not_call(__FILE__, __LINE__); \ 126 BREAKPOINT; \ 127} while (0) 128 129#define ShouldNotReachHere() \ 130do { \ 131 report_should_not_reach_here(__FILE__, __LINE__); \ 132 BREAKPOINT; \ 133} while (0) 134 135#define Unimplemented() \ 136do { \ 137 report_unimplemented(__FILE__, __LINE__); \ 138 BREAKPOINT; \ 139} while (0) 140 141#define Untested(msg) \ 142do { \ 143 report_untested(__FILE__, __LINE__, msg); \ 144 BREAKPOINT; \ 145} while (0); 146 147// error reporting helper functions 148void report_vm_error(const char* file, int line, const char* error_msg, 149 const char* detail_msg = NULL); 150void report_fatal(const char* file, int line, const char* message); 151void report_vm_out_of_memory(const char* file, int line, size_t size, 152 const char* message); 153void report_should_not_call(const char* file, int line); 154void report_should_not_reach_here(const char* file, int line); 155void report_unimplemented(const char* file, int line); 156void report_untested(const char* file, int line, const char* message); 157 158void warning(const char* format, ...); 159 160// out of memory reporting 161void report_java_out_of_memory(const char* message); 162 163// Support for self-destruct 164bool is_error_reported(); 165void set_error_reported(); 166 167/* Test assert(), fatal(), guarantee(), etc. */ 168NOT_PRODUCT(void test_error_handler(size_t test_num);) 169 170void pd_ps(frame f); 171void pd_obfuscate_location(char *buf, size_t buflen); 172