duke@435: /* stefank@2314: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_UTILITIES_DEBUG_HPP stefank@2314: #define SHARE_VM_UTILITIES_DEBUG_HPP stefank@2314: stefank@2314: #include "utilities/globalDefinitions.hpp" stefank@2314: jcoomes@1845: #include jcoomes@1845: jcoomes@1845: // Simple class to format the ctor arguments into a fixed-sized buffer. jcoomes@1845: template jcoomes@1845: class FormatBuffer { jcoomes@1845: public: jcoomes@1845: inline FormatBuffer(const char * format, ...); tonyp@2472: inline void append(const char* format, ...); jcoomes@1845: operator const char *() const { return _buf; } jcoomes@1845: jcoomes@1845: private: jcoomes@1845: FormatBuffer(const FormatBuffer &); // prevent copies jcoomes@1845: jcoomes@1845: private: jcoomes@1845: char _buf[bufsz]; jcoomes@1845: }; jcoomes@1845: jcoomes@1845: template jcoomes@1845: FormatBuffer::FormatBuffer(const char * format, ...) { jcoomes@1845: va_list argp; jcoomes@1845: va_start(argp, format); jcoomes@1845: vsnprintf(_buf, bufsz, format, argp); jcoomes@1845: va_end(argp); jcoomes@1845: } jcoomes@1845: tonyp@2472: template tonyp@2472: void FormatBuffer::append(const char* format, ...) { tonyp@2472: // Given that the constructor does a vsnprintf we can assume that tonyp@2472: // _buf is already initialized. tonyp@2472: size_t len = strlen(_buf); tonyp@2472: char* buf_end = _buf + len; tonyp@2472: tonyp@2472: va_list argp; tonyp@2472: va_start(argp, format); tonyp@2472: vsnprintf(buf_end, bufsz - len, format, argp); tonyp@2472: va_end(argp); tonyp@2472: } tonyp@2472: jcoomes@1845: // Used to format messages for assert(), guarantee(), fatal(), etc. jcoomes@1845: typedef FormatBuffer<> err_msg; jcoomes@1845: duke@435: // assertions duke@435: #ifdef ASSERT jcoomes@1845: #ifndef USE_REPEATED_ASSERTS jcoomes@1845: #define assert(p, msg) \ jcoomes@1845: do { \ jcoomes@1845: if (!(p)) { \ jcoomes@1845: report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } \ jcoomes@1845: } while (0) jcoomes@1845: #else // #ifndef USE_REPEATED_ASSERTS jcoomes@1845: #define assert(p, msg) jcoomes@1845: do { \ jcoomes@1845: for (int __i = 0; __i < AssertRepeat; __i++) { \ jcoomes@1845: if (!(p)) { \ jcoomes@1845: report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } \ jcoomes@1845: } \ jcoomes@1845: } while (0) jcoomes@1845: #endif // #ifndef USE_REPEATED_ASSERTS 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 jcoomes@1845: #define assert_status(p, status, msg) \ jcoomes@1845: do { \ jcoomes@1845: if (!(p)) { \ jcoomes@1845: report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", \ jcoomes@1845: err_msg("error %s(%d) %s", strerror(status), \ jcoomes@1845: status, msg)); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } \ jcoomes@1845: } while (0) 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) jcoomes@1845: #else // #ifdef ASSERT duke@435: #define assert(p,msg) duke@435: #define assert_status(p,status,msg) duke@435: #define assert_if_no_error(cond,msg) jcoomes@1845: #endif // #ifdef ASSERT duke@435: jcoomes@1845: // guarantee is like assert except it's always executed -- use it for jcoomes@1845: // cheap tests that catch errors that would otherwise be hard to find. jcoomes@1845: // guarantee is also used for Verify options. jcoomes@1845: #define guarantee(p, msg) \ jcoomes@1845: do { \ jcoomes@1845: if (!(p)) { \ jcoomes@1845: report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", msg); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } \ jcoomes@1845: } while (0) duke@435: jcoomes@1845: #define fatal(msg) \ jcoomes@1845: do { \ jcoomes@1845: report_fatal(__FILE__, __LINE__, msg); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } while (0) duke@435: duke@435: // out of memory jcoomes@1845: #define vm_exit_out_of_memory(size, msg) \ jcoomes@1845: do { \ jcoomes@1845: report_vm_out_of_memory(__FILE__, __LINE__, size, msg); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } while (0) duke@435: jcoomes@1845: #define ShouldNotCallThis() \ jcoomes@1845: do { \ jcoomes@1845: report_should_not_call(__FILE__, __LINE__); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } while (0) duke@435: jcoomes@1845: #define ShouldNotReachHere() \ jcoomes@1845: do { \ jcoomes@1845: report_should_not_reach_here(__FILE__, __LINE__); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } while (0) jcoomes@1845: jcoomes@1845: #define Unimplemented() \ jcoomes@1845: do { \ jcoomes@1845: report_unimplemented(__FILE__, __LINE__); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } while (0) jcoomes@1845: jcoomes@1845: #define Untested(msg) \ jcoomes@1845: do { \ jcoomes@1845: report_untested(__FILE__, __LINE__, msg); \ jcoomes@1845: BREAKPOINT; \ jcoomes@1845: } while (0); duke@435: duke@435: // error reporting helper functions jcoomes@1845: void report_vm_error(const char* file, int line, const char* error_msg, jcoomes@1845: const char* detail_msg = NULL); jcoomes@1845: void report_fatal(const char* file, int line, const char* message); jcoomes@1845: void report_vm_out_of_memory(const char* file, int line, size_t size, jcoomes@1845: const char* message); jcoomes@1845: void report_should_not_call(const char* file, int line); jcoomes@1845: void report_should_not_reach_here(const char* file, int line); jcoomes@1845: void report_unimplemented(const char* file, int line); jcoomes@1845: void report_untested(const char* file, int line, const char* message); jcoomes@1845: duke@435: void warning(const char* format, ...); duke@435: coleenp@2497: // out of shared space reporting coleenp@2497: enum SharedSpaceType { coleenp@2497: SharedPermGen, coleenp@2497: SharedReadOnly, coleenp@2497: SharedReadWrite, coleenp@2497: SharedMiscData coleenp@2497: }; coleenp@2497: coleenp@2497: void report_out_of_shared_space(SharedSpaceType space_type); coleenp@2497: 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: jcoomes@1845: /* Test assert(), fatal(), guarantee(), etc. */ jcoomes@1845: NOT_PRODUCT(void test_error_handler(size_t test_num);) jcoomes@1845: duke@435: void pd_ps(frame f); duke@435: void pd_obfuscate_location(char *buf, size_t buflen); stefank@2314: stefank@2314: #endif // SHARE_VM_UTILITIES_DEBUG_HPP