1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/utilities/debug.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,267 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_UTILITIES_DEBUG_HPP 1.29 +#define SHARE_VM_UTILITIES_DEBUG_HPP 1.30 + 1.31 +#include "utilities/globalDefinitions.hpp" 1.32 +#include "prims/jvm.h" 1.33 + 1.34 +#include <stdarg.h> 1.35 + 1.36 +// Simple class to format the ctor arguments into a fixed-sized buffer. 1.37 +class FormatBufferBase { 1.38 + protected: 1.39 + char* _buf; 1.40 + inline FormatBufferBase(char* buf) : _buf(buf) {} 1.41 + public: 1.42 + operator const char *() const { return _buf; } 1.43 +}; 1.44 + 1.45 +// Use resource area for buffer 1.46 +#define RES_BUFSZ 256 1.47 +class FormatBufferResource : public FormatBufferBase { 1.48 + public: 1.49 + FormatBufferResource(const char * format, ...) ATTRIBUTE_PRINTF(2, 3); 1.50 +}; 1.51 + 1.52 +// Use stack for buffer 1.53 +template <size_t bufsz = 256> 1.54 +class FormatBuffer : public FormatBufferBase { 1.55 + public: 1.56 + inline FormatBuffer(const char * format, ...) ATTRIBUTE_PRINTF(2, 3); 1.57 + inline void append(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 1.58 + inline void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 1.59 + inline void printv(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); 1.60 + 1.61 + char* buffer() { return _buf; } 1.62 + int size() { return bufsz; } 1.63 + 1.64 + private: 1.65 + FormatBuffer(const FormatBuffer &); // prevent copies 1.66 + char _buffer[bufsz]; 1.67 + 1.68 + protected: 1.69 + inline FormatBuffer(); 1.70 +}; 1.71 + 1.72 +template <size_t bufsz> 1.73 +FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) { 1.74 + va_list argp; 1.75 + va_start(argp, format); 1.76 + jio_vsnprintf(_buf, bufsz, format, argp); 1.77 + va_end(argp); 1.78 +} 1.79 + 1.80 +template <size_t bufsz> 1.81 +FormatBuffer<bufsz>::FormatBuffer() : FormatBufferBase(_buffer) { 1.82 + _buf[0] = '\0'; 1.83 +} 1.84 + 1.85 +template <size_t bufsz> 1.86 +void FormatBuffer<bufsz>::print(const char * format, ...) { 1.87 + va_list argp; 1.88 + va_start(argp, format); 1.89 + jio_vsnprintf(_buf, bufsz, format, argp); 1.90 + va_end(argp); 1.91 +} 1.92 + 1.93 +template <size_t bufsz> 1.94 +void FormatBuffer<bufsz>::printv(const char * format, va_list argp) { 1.95 + jio_vsnprintf(_buf, bufsz, format, argp); 1.96 +} 1.97 + 1.98 +template <size_t bufsz> 1.99 +void FormatBuffer<bufsz>::append(const char* format, ...) { 1.100 + // Given that the constructor does a vsnprintf we can assume that 1.101 + // _buf is already initialized. 1.102 + size_t len = strlen(_buf); 1.103 + char* buf_end = _buf + len; 1.104 + 1.105 + va_list argp; 1.106 + va_start(argp, format); 1.107 + jio_vsnprintf(buf_end, bufsz - len, format, argp); 1.108 + va_end(argp); 1.109 +} 1.110 + 1.111 +// Used to format messages for assert(), guarantee(), fatal(), etc. 1.112 +typedef FormatBuffer<> err_msg; 1.113 +typedef FormatBufferResource err_msg_res; 1.114 + 1.115 +// assertions 1.116 +#ifdef ASSERT 1.117 +#ifndef USE_REPEATED_ASSERTS 1.118 +#define assert(p, msg) \ 1.119 +do { \ 1.120 + if (!(p)) { \ 1.121 + report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \ 1.122 + BREAKPOINT; \ 1.123 + } \ 1.124 +} while (0) 1.125 +#else // #ifndef USE_REPEATED_ASSERTS 1.126 +#define assert(p, msg) 1.127 +do { \ 1.128 + for (int __i = 0; __i < AssertRepeat; __i++) { \ 1.129 + if (!(p)) { \ 1.130 + report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", msg); \ 1.131 + BREAKPOINT; \ 1.132 + } \ 1.133 + } \ 1.134 +} while (0) 1.135 +#endif // #ifndef USE_REPEATED_ASSERTS 1.136 + 1.137 +// This version of assert is for use with checking return status from 1.138 +// library calls that return actual error values eg. EINVAL, 1.139 +// ENOMEM etc, rather than returning -1 and setting errno. 1.140 +// When the status is not what is expected it is very useful to know 1.141 +// what status was actually returned, so we pass the status variable as 1.142 +// an extra arg and use strerror to convert it to a meaningful string 1.143 +// like "Invalid argument", "out of memory" etc 1.144 +#define assert_status(p, status, msg) \ 1.145 +do { \ 1.146 + if (!(p)) { \ 1.147 + report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", \ 1.148 + err_msg("error %s(%d) %s", strerror(status), \ 1.149 + status, msg)); \ 1.150 + BREAKPOINT; \ 1.151 + } \ 1.152 +} while (0) 1.153 + 1.154 +// Do not assert this condition if there's already another error reported. 1.155 +#define assert_if_no_error(cond,msg) assert((cond) || is_error_reported(), msg) 1.156 +#else // #ifdef ASSERT 1.157 + #define assert(p,msg) 1.158 + #define assert_status(p,status,msg) 1.159 + #define assert_if_no_error(cond,msg) 1.160 +#endif // #ifdef ASSERT 1.161 + 1.162 +// guarantee is like assert except it's always executed -- use it for 1.163 +// cheap tests that catch errors that would otherwise be hard to find. 1.164 +// guarantee is also used for Verify options. 1.165 +#define guarantee(p, msg) \ 1.166 +do { \ 1.167 + if (!(p)) { \ 1.168 + report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", msg); \ 1.169 + BREAKPOINT; \ 1.170 + } \ 1.171 +} while (0) 1.172 + 1.173 +#define fatal(msg) \ 1.174 +do { \ 1.175 + report_fatal(__FILE__, __LINE__, msg); \ 1.176 + BREAKPOINT; \ 1.177 +} while (0) 1.178 + 1.179 +// out of memory 1.180 +#define vm_exit_out_of_memory(size, vm_err_type, msg) \ 1.181 +do { \ 1.182 + report_vm_out_of_memory(__FILE__, __LINE__, size, vm_err_type, msg); \ 1.183 + BREAKPOINT; \ 1.184 +} while (0) 1.185 + 1.186 +#define ShouldNotCallThis() \ 1.187 +do { \ 1.188 + report_should_not_call(__FILE__, __LINE__); \ 1.189 + BREAKPOINT; \ 1.190 +} while (0) 1.191 + 1.192 +#define ShouldNotReachHere() \ 1.193 +do { \ 1.194 + report_should_not_reach_here(__FILE__, __LINE__); \ 1.195 + BREAKPOINT; \ 1.196 +} while (0) 1.197 + 1.198 +#define Unimplemented() \ 1.199 +do { \ 1.200 + report_unimplemented(__FILE__, __LINE__); \ 1.201 + BREAKPOINT; \ 1.202 +} while (0) 1.203 + 1.204 +#define Untested(msg) \ 1.205 +do { \ 1.206 + report_untested(__FILE__, __LINE__, msg); \ 1.207 + BREAKPOINT; \ 1.208 +} while (0); 1.209 + 1.210 + 1.211 +// types of VM error - originally in vmError.hpp 1.212 +enum VMErrorType { 1.213 + INTERNAL_ERROR = 0xe0000000, 1.214 + OOM_MALLOC_ERROR = 0xe0000001, 1.215 + OOM_MMAP_ERROR = 0xe0000002 1.216 +}; 1.217 + 1.218 +// error reporting helper functions 1.219 +void report_vm_error(const char* file, int line, const char* error_msg, 1.220 + const char* detail_msg = NULL); 1.221 +void report_fatal(const char* file, int line, const char* message); 1.222 +void report_vm_out_of_memory(const char* file, int line, size_t size, 1.223 + VMErrorType vm_err_type, const char* message); 1.224 +void report_should_not_call(const char* file, int line); 1.225 +void report_should_not_reach_here(const char* file, int line); 1.226 +void report_unimplemented(const char* file, int line); 1.227 +void report_untested(const char* file, int line, const char* message); 1.228 + 1.229 +void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2); 1.230 + 1.231 +#ifdef ASSERT 1.232 +// Compile-time asserts. 1.233 +template <bool> struct StaticAssert; 1.234 +template <> struct StaticAssert<true> {}; 1.235 + 1.236 +// Only StaticAssert<true> is defined, so if cond evaluates to false we get 1.237 +// a compile time exception when trying to use StaticAssert<false>. 1.238 +#define STATIC_ASSERT(cond) \ 1.239 + do { \ 1.240 + StaticAssert<(cond)> DUMMY_STATIC_ASSERT; \ 1.241 + (void)DUMMY_STATIC_ASSERT; /* ignore */ \ 1.242 + } while (false) 1.243 +#else 1.244 +#define STATIC_ASSERT(cond) 1.245 +#endif 1.246 + 1.247 +// out of shared space reporting 1.248 +enum SharedSpaceType { 1.249 + SharedPermGen, 1.250 + SharedReadOnly, 1.251 + SharedReadWrite, 1.252 + SharedMiscData 1.253 +}; 1.254 + 1.255 +void report_out_of_shared_space(SharedSpaceType space_type); 1.256 + 1.257 +// out of memory reporting 1.258 +void report_java_out_of_memory(const char* message); 1.259 + 1.260 +// Support for self-destruct 1.261 +bool is_error_reported(); 1.262 +void set_error_reported(); 1.263 + 1.264 +/* Test assert(), fatal(), guarantee(), etc. */ 1.265 +NOT_PRODUCT(void test_error_handler();) 1.266 + 1.267 +void pd_ps(frame f); 1.268 +void pd_obfuscate_location(char *buf, size_t buflen); 1.269 + 1.270 +#endif // SHARE_VM_UTILITIES_DEBUG_HPP