Mon, 03 Jan 2011 14:09:11 -0500
6302804: Hotspot VM dies ungraceful death when C heap is exhausted in various places.
Summary: enhance the error reporting mechanism to help user to fix the problem rather than making it look like a VM error.
Reviewed-by: kvn, kamg
duke@435 | 1 | /* |
stefank@2314 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #ifndef SHARE_VM_UTILITIES_DEBUG_HPP |
stefank@2314 | 26 | #define SHARE_VM_UTILITIES_DEBUG_HPP |
stefank@2314 | 27 | |
stefank@2314 | 28 | #include "utilities/globalDefinitions.hpp" |
stefank@2314 | 29 | |
jcoomes@1845 | 30 | #include <stdarg.h> |
jcoomes@1845 | 31 | |
jcoomes@1845 | 32 | // Simple class to format the ctor arguments into a fixed-sized buffer. |
jcoomes@1845 | 33 | template <size_t bufsz = 256> |
jcoomes@1845 | 34 | class FormatBuffer { |
jcoomes@1845 | 35 | public: |
jcoomes@1845 | 36 | inline FormatBuffer(const char * format, ...); |
jcoomes@1845 | 37 | operator const char *() const { return _buf; } |
jcoomes@1845 | 38 | |
jcoomes@1845 | 39 | private: |
jcoomes@1845 | 40 | FormatBuffer(const FormatBuffer &); // prevent copies |
jcoomes@1845 | 41 | |
jcoomes@1845 | 42 | private: |
jcoomes@1845 | 43 | char _buf[bufsz]; |
jcoomes@1845 | 44 | }; |
jcoomes@1845 | 45 | |
jcoomes@1845 | 46 | template <size_t bufsz> |
jcoomes@1845 | 47 | FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) { |
jcoomes@1845 | 48 | va_list argp; |
jcoomes@1845 | 49 | va_start(argp, format); |
jcoomes@1845 | 50 | vsnprintf(_buf, bufsz, format, argp); |
jcoomes@1845 | 51 | va_end(argp); |
jcoomes@1845 | 52 | } |
jcoomes@1845 | 53 | |
jcoomes@1845 | 54 | // Used to format messages for assert(), guarantee(), fatal(), etc. |
jcoomes@1845 | 55 | typedef FormatBuffer<> err_msg; |
jcoomes@1845 | 56 | |
duke@435 | 57 | // assertions |
duke@435 | 58 | #ifdef ASSERT |
jcoomes@1845 | 59 | #ifndef USE_REPEATED_ASSERTS |
jcoomes@1845 | 60 | #define assert(p, msg) \ |
jcoomes@1845 | 61 | do { \ |
jcoomes@1845 | 62 | if (!(p)) { \ |
jcoomes@1845 | 63 | report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \ |
jcoomes@1845 | 64 | BREAKPOINT; \ |
jcoomes@1845 | 65 | } \ |
jcoomes@1845 | 66 | } while (0) |
jcoomes@1845 | 67 | #else // #ifndef USE_REPEATED_ASSERTS |
jcoomes@1845 | 68 | #define assert(p, msg) |
jcoomes@1845 | 69 | do { \ |
jcoomes@1845 | 70 | for (int __i = 0; __i < AssertRepeat; __i++) { \ |
jcoomes@1845 | 71 | if (!(p)) { \ |
jcoomes@1845 | 72 | report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \ |
jcoomes@1845 | 73 | BREAKPOINT; \ |
jcoomes@1845 | 74 | } \ |
jcoomes@1845 | 75 | } \ |
jcoomes@1845 | 76 | } while (0) |
jcoomes@1845 | 77 | #endif // #ifndef USE_REPEATED_ASSERTS |
duke@435 | 78 | |
duke@435 | 79 | // This version of assert is for use with checking return status from |
duke@435 | 80 | // library calls that return actual error values eg. EINVAL, |
duke@435 | 81 | // ENOMEM etc, rather than returning -1 and setting errno. |
duke@435 | 82 | // When the status is not what is expected it is very useful to know |
duke@435 | 83 | // what status was actually returned, so we pass the status variable as |
duke@435 | 84 | // an extra arg and use strerror to convert it to a meaningful string |
duke@435 | 85 | // like "Invalid argument", "out of memory" etc |
jcoomes@1845 | 86 | #define assert_status(p, status, msg) \ |
jcoomes@1845 | 87 | do { \ |
jcoomes@1845 | 88 | if (!(p)) { \ |
jcoomes@1845 | 89 | report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", \ |
jcoomes@1845 | 90 | err_msg("error %s(%d) %s", strerror(status), \ |
jcoomes@1845 | 91 | status, msg)); \ |
jcoomes@1845 | 92 | BREAKPOINT; \ |
jcoomes@1845 | 93 | } \ |
jcoomes@1845 | 94 | } while (0) |
duke@435 | 95 | |
duke@435 | 96 | // Do not assert this condition if there's already another error reported. |
duke@435 | 97 | #define assert_if_no_error(cond,msg) assert((cond) || is_error_reported(), msg) |
jcoomes@1845 | 98 | #else // #ifdef ASSERT |
duke@435 | 99 | #define assert(p,msg) |
duke@435 | 100 | #define assert_status(p,status,msg) |
duke@435 | 101 | #define assert_if_no_error(cond,msg) |
jcoomes@1845 | 102 | #endif // #ifdef ASSERT |
duke@435 | 103 | |
jcoomes@1845 | 104 | // guarantee is like assert except it's always executed -- use it for |
jcoomes@1845 | 105 | // cheap tests that catch errors that would otherwise be hard to find. |
jcoomes@1845 | 106 | // guarantee is also used for Verify options. |
jcoomes@1845 | 107 | #define guarantee(p, msg) \ |
jcoomes@1845 | 108 | do { \ |
jcoomes@1845 | 109 | if (!(p)) { \ |
jcoomes@1845 | 110 | report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", msg); \ |
jcoomes@1845 | 111 | BREAKPOINT; \ |
jcoomes@1845 | 112 | } \ |
jcoomes@1845 | 113 | } while (0) |
duke@435 | 114 | |
jcoomes@1845 | 115 | #define fatal(msg) \ |
jcoomes@1845 | 116 | do { \ |
jcoomes@1845 | 117 | report_fatal(__FILE__, __LINE__, msg); \ |
jcoomes@1845 | 118 | BREAKPOINT; \ |
jcoomes@1845 | 119 | } while (0) |
duke@435 | 120 | |
duke@435 | 121 | // out of memory |
jcoomes@1845 | 122 | #define vm_exit_out_of_memory(size, msg) \ |
jcoomes@1845 | 123 | do { \ |
jcoomes@1845 | 124 | report_vm_out_of_memory(__FILE__, __LINE__, size, msg); \ |
jcoomes@1845 | 125 | BREAKPOINT; \ |
jcoomes@1845 | 126 | } while (0) |
duke@435 | 127 | |
jcoomes@1845 | 128 | #define ShouldNotCallThis() \ |
jcoomes@1845 | 129 | do { \ |
jcoomes@1845 | 130 | report_should_not_call(__FILE__, __LINE__); \ |
jcoomes@1845 | 131 | BREAKPOINT; \ |
jcoomes@1845 | 132 | } while (0) |
duke@435 | 133 | |
jcoomes@1845 | 134 | #define ShouldNotReachHere() \ |
jcoomes@1845 | 135 | do { \ |
jcoomes@1845 | 136 | report_should_not_reach_here(__FILE__, __LINE__); \ |
jcoomes@1845 | 137 | BREAKPOINT; \ |
jcoomes@1845 | 138 | } while (0) |
jcoomes@1845 | 139 | |
jcoomes@1845 | 140 | #define Unimplemented() \ |
jcoomes@1845 | 141 | do { \ |
jcoomes@1845 | 142 | report_unimplemented(__FILE__, __LINE__); \ |
jcoomes@1845 | 143 | BREAKPOINT; \ |
jcoomes@1845 | 144 | } while (0) |
jcoomes@1845 | 145 | |
jcoomes@1845 | 146 | #define Untested(msg) \ |
jcoomes@1845 | 147 | do { \ |
jcoomes@1845 | 148 | report_untested(__FILE__, __LINE__, msg); \ |
jcoomes@1845 | 149 | BREAKPOINT; \ |
jcoomes@1845 | 150 | } while (0); |
duke@435 | 151 | |
duke@435 | 152 | // error reporting helper functions |
jcoomes@1845 | 153 | void report_vm_error(const char* file, int line, const char* error_msg, |
jcoomes@1845 | 154 | const char* detail_msg = NULL); |
jcoomes@1845 | 155 | void report_fatal(const char* file, int line, const char* message); |
jcoomes@1845 | 156 | void report_vm_out_of_memory(const char* file, int line, size_t size, |
jcoomes@1845 | 157 | const char* message); |
jcoomes@1845 | 158 | void report_should_not_call(const char* file, int line); |
jcoomes@1845 | 159 | void report_should_not_reach_here(const char* file, int line); |
jcoomes@1845 | 160 | void report_unimplemented(const char* file, int line); |
jcoomes@1845 | 161 | void report_untested(const char* file, int line, const char* message); |
jcoomes@1845 | 162 | |
duke@435 | 163 | void warning(const char* format, ...); |
duke@435 | 164 | |
duke@435 | 165 | // out of memory reporting |
duke@435 | 166 | void report_java_out_of_memory(const char* message); |
duke@435 | 167 | |
duke@435 | 168 | // Support for self-destruct |
duke@435 | 169 | bool is_error_reported(); |
duke@435 | 170 | void set_error_reported(); |
duke@435 | 171 | |
jcoomes@1845 | 172 | /* Test assert(), fatal(), guarantee(), etc. */ |
jcoomes@1845 | 173 | NOT_PRODUCT(void test_error_handler(size_t test_num);) |
jcoomes@1845 | 174 | |
duke@435 | 175 | void pd_ps(frame f); |
duke@435 | 176 | void pd_obfuscate_location(char *buf, size_t buflen); |
stefank@2314 | 177 | |
stefank@2314 | 178 | #endif // SHARE_VM_UTILITIES_DEBUG_HPP |