1 /* 2 * Copyright (c) 1997, 2016, 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 #ifndef SHARE_VM_UTILITIES_DEBUG_HPP 26 #define SHARE_VM_UTILITIES_DEBUG_HPP 27 28 #include "utilities/globalDefinitions.hpp" 29 #include "prims/jvm.h" 30 31 #include <stdarg.h> 32 33 // Simple class to format the ctor arguments into a fixed-sized buffer. 34 class FormatBufferBase { 35 protected: 36 char* _buf; 37 inline FormatBufferBase(char* buf) : _buf(buf) {} 38 public: 39 static const int BufferSize = 256; 40 operator const char *() const { return _buf; } 41 }; 42 43 // Use resource area for buffer 44 class FormatBufferResource : public FormatBufferBase { 45 public: 46 FormatBufferResource(const char * format, ...) ATTRIBUTE_PRINTF(2, 3); 47 }; 48 49 class FormatBufferDummy {}; 50 51 // Use stack for buffer 52 template <size_t bufsz = FormatBufferBase::BufferSize> 53 class FormatBuffer : public FormatBufferBase { 54 public: 55 inline FormatBuffer(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 56 // since va_list is unspecified type (can be char*), we use FormatBufferDummy to disambiguate these constructors 57 inline FormatBuffer(FormatBufferDummy dummy, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0); 58 inline void append(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 59 inline void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 60 inline void printv(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); 61 62 char* buffer() { return _buf; } 63 int size() { return bufsz; } 64 65 private: 66 FormatBuffer(const FormatBuffer &); // prevent copies 67 char _buffer[bufsz]; 68 69 protected: 70 inline FormatBuffer(); 71 }; 72 73 template <size_t bufsz> 74 FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) { 75 va_list argp; 76 va_start(argp, format); 77 jio_vsnprintf(_buf, bufsz, format, argp); 78 va_end(argp); 79 } 80 81 template <size_t bufsz> 82 FormatBuffer<bufsz>::FormatBuffer(FormatBufferDummy dummy, const char * format, va_list ap) : FormatBufferBase(_buffer) { 83 jio_vsnprintf(_buf, bufsz, format, ap); 84 } 85 86 template <size_t bufsz> 87 FormatBuffer<bufsz>::FormatBuffer() : FormatBufferBase(_buffer) { 88 _buf[0] = '\0'; 89 } 90 91 template <size_t bufsz> 92 void FormatBuffer<bufsz>::print(const char * format, ...) { 93 va_list argp; 94 va_start(argp, format); 95 jio_vsnprintf(_buf, bufsz, format, argp); 96 va_end(argp); 97 } 98 99 template <size_t bufsz> 100 void FormatBuffer<bufsz>::printv(const char * format, va_list argp) { 101 jio_vsnprintf(_buf, bufsz, format, argp); 102 } 103 104 template <size_t bufsz> 105 void FormatBuffer<bufsz>::append(const char* format, ...) { 106 // Given that the constructor does a vsnprintf we can assume that 107 // _buf is already initialized. 108 size_t len = strlen(_buf); 109 char* buf_end = _buf + len; 110 111 va_list argp; 112 va_start(argp, format); 113 jio_vsnprintf(buf_end, bufsz - len, format, argp); 114 va_end(argp); 115 } 116 117 // Used to format messages. 118 typedef FormatBuffer<> err_msg; 119 120 // assertions 121 #ifndef ASSERT 122 #define vmassert(p, ...) 123 #else 124 // Note: message says "assert" rather than "vmassert" for backward 125 // compatibility with tools that parse/match the message text. 126 // Note: The signature is vmassert(p, format, ...), but the solaris 127 // compiler can't handle an empty ellipsis in a macro without a warning. 128 #define vmassert(p, ...) \ 129 do { \ 130 if (!(p)) { \ 131 if (is_executing_unit_tests()) { \ 132 report_assert_msg(__VA_ARGS__); \ 133 } \ 134 report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", __VA_ARGS__); \ 135 BREAKPOINT; \ 136 } \ 137 } while (0) 138 #endif 139 140 // For backward compatibility. 141 #define assert(p, ...) vmassert(p, __VA_ARGS__) 142 143 // This version of vmassert is for use with checking return status from 144 // library calls that return actual error values eg. EINVAL, 145 // ENOMEM etc, rather than returning -1 and setting errno. 146 // When the status is not what is expected it is very useful to know 147 // what status was actually returned, so we pass the status variable as 148 // an extra arg and use strerror to convert it to a meaningful string 149 // like "Invalid argument", "out of memory" etc 150 #define vmassert_status(p, status, msg) \ 151 do { \ 152 if (!(p)) { \ 153 report_vm_status_error(__FILE__, __LINE__, "assert(" #p ") failed", \ 154 status, msg); \ 155 BREAKPOINT; \ 156 } \ 157 } while (0) 158 159 // For backward compatibility. 160 #define assert_status(p, status, msg) vmassert_status(p, status, msg) 161 162 // guarantee is like vmassert except it's always executed -- use it for 163 // cheap tests that catch errors that would otherwise be hard to find. 164 // guarantee is also used for Verify options. 165 #define guarantee(p, ...) \ 166 do { \ 167 if (!(p)) { \ 168 report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", __VA_ARGS__); \ 169 BREAKPOINT; \ 170 } \ 171 } while (0) 172 173 #define fatal(...) \ 174 do { \ 175 report_fatal(__FILE__, __LINE__, __VA_ARGS__); \ 176 BREAKPOINT; \ 177 } while (0) 178 179 // out of memory 180 #define vm_exit_out_of_memory(size, vm_err_type, ...) \ 181 do { \ 182 report_vm_out_of_memory(__FILE__, __LINE__, size, vm_err_type, __VA_ARGS__); \ 183 BREAKPOINT; \ 184 } while (0) 185 186 #define ShouldNotCallThis() \ 187 do { \ 188 report_should_not_call(__FILE__, __LINE__); \ 189 BREAKPOINT; \ 190 } while (0) 191 192 #define ShouldNotReachHere() \ 193 do { \ 194 report_should_not_reach_here(__FILE__, __LINE__); \ 195 BREAKPOINT; \ 196 } while (0) 197 198 #define Unimplemented() \ 199 do { \ 200 report_unimplemented(__FILE__, __LINE__); \ 201 BREAKPOINT; \ 202 } while (0) 203 204 #define Untested(msg) \ 205 do { \ 206 report_untested(__FILE__, __LINE__, msg); \ 207 BREAKPOINT; \ 208 } while (0); 209 210 211 // types of VM error - originally in vmError.hpp 212 enum VMErrorType { 213 INTERNAL_ERROR = 0xe0000000, 214 OOM_MALLOC_ERROR = 0xe0000001, 215 OOM_MMAP_ERROR = 0xe0000002 216 }; 217 218 // error reporting helper functions 219 void report_vm_error(const char* file, int line, const char* error_msg); 220 #if !defined(__GNUC__) || defined (__clang_major__) || (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || __GNUC__ > 4) 221 // ATTRIBUTE_PRINTF works with gcc >= 4.8 and any other compiler. 222 void report_vm_error(const char* file, int line, const char* error_msg, 223 const char* detail_fmt, ...) ATTRIBUTE_PRINTF(4, 5); 224 #ifdef ASSERT 225 void report_assert_msg(const char* msg, ...) ATTRIBUTE_PRINTF(1, 2); 226 #endif // ASSERT 227 #else 228 // GCC < 4.8 warns because of empty format string. Warning can not be switched off selectively. 229 void report_vm_error(const char* file, int line, const char* error_msg, 230 const char* detail_fmt, ...); 231 #ifdef ASSERT 232 void report_assert_msg(const char* msg, ...); 233 #endif // ASSERT 234 #endif 235 void report_vm_status_error(const char* file, int line, const char* error_msg, 236 int status, const char* detail); 237 void report_fatal(const char* file, int line, const char* detail_fmt, ...) ATTRIBUTE_PRINTF(3, 4); 238 void report_vm_out_of_memory(const char* file, int line, size_t size, VMErrorType vm_err_type, 239 const char* detail_fmt, ...) ATTRIBUTE_PRINTF(5, 6); 240 void report_should_not_call(const char* file, int line); 241 void report_should_not_reach_here(const char* file, int line); 242 void report_unimplemented(const char* file, int line); 243 void report_untested(const char* file, int line, const char* message); 244 245 #ifdef ASSERT 246 // unit test support 247 bool is_executing_unit_tests(); 248 #endif // ASSERT 249 250 void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2); 251 252 // Compile-time asserts. Cond must be a compile-time constant expression that 253 // is convertible to bool. STATIC_ASSERT() can be used anywhere a declaration 254 // may appear. 255 // 256 // Implementation Note: STATIC_ASSERT_FAILURE<true> provides a value member 257 // rather than type member that could be used directly in the typedef, because 258 // a type member would require conditional use of "typename", depending on 259 // whether Cond is dependent or not. The use of a value member leads to the 260 // use of an array type. 261 262 template<bool x> struct STATIC_ASSERT_FAILURE; 263 template<> struct STATIC_ASSERT_FAILURE<true> { enum { value = 1 }; }; 264 265 #define STATIC_ASSERT(Cond) \ 266 typedef char PASTE_TOKENS(STATIC_ASSERT_DUMMY_TYPE_, __LINE__)[ \ 267 STATIC_ASSERT_FAILURE< (Cond) >::value ] 268 269 // out of shared space reporting 270 enum SharedSpaceType { 271 SharedReadOnly, 272 SharedReadWrite, 273 SharedMiscData, 274 SharedMiscCode, 275 SharedOptional 276 }; 277 278 void report_out_of_shared_space(SharedSpaceType space_type); 279 280 void report_insufficient_metaspace(size_t required_size); 281 282 // out of memory reporting 283 void report_java_out_of_memory(const char* message); 284 285 // Support for self-destruct 286 bool is_error_reported(); 287 void set_error_reported(); 288 289 /* Test vmassert(), fatal(), guarantee(), etc. */ 290 NOT_PRODUCT(void test_error_handler();) 291 292 // crash in a controlled way: 293 // how can be one of: 294 // 1,2 - asserts 295 // 3,4 - guarantee 296 // 5-7 - fatal 297 // 8 - vm_exit_out_of_memory 298 // 9 - ShouldNotCallThis 299 // 10 - ShouldNotReachHere 300 // 11 - Unimplemented 301 // 12,13 - (not guaranteed) crashes 302 // 14 - SIGSEGV 303 // 15 - SIGFPE 304 NOT_PRODUCT(void controlled_crash(int how);) 305 306 // returns an address which is guaranteed to generate a SIGSEGV on read, 307 // for test purposes, which is not NULL and contains bits in every word 308 NOT_PRODUCT(void* get_segfault_address();) 309 310 void pd_ps(frame f); 311 void pd_obfuscate_location(char *buf, size_t buflen); 312 313 class outputStream; 314 void print_native_stack(outputStream* st, frame fr, Thread* t, char* buf, int buf_size); 315 316 #endif // SHARE_VM_UTILITIES_DEBUG_HPP