1 /*
2  * Copyright (c) 1997, 2018, 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/breakpoint.hpp"
29 #include "utilities/compilerWarnings.hpp"
30 #include "utilities/macros.hpp"
31 
32 #include <stddef.h>
33 
34 // ShowRegistersOnAssert support (for now Linux only)
35 #if defined(LINUX) && !defined(ZERO)
36 #define CAN_SHOW_REGISTERS_ON_ASSERT
37 extern char* g_assert_poison;
38 #define TOUCH_ASSERT_POISON (*g_assert_poison) = 'X';
39 void initialize_assert_poison();
40 bool handle_assert_poison_fault(const void* ucVoid, const void* faulting_address);
41 #else
42 #define TOUCH_ASSERT_POISON
43 #endif // CAN_SHOW_REGISTERS_ON_ASSERT
44 
45 // assertions
46 #ifndef ASSERT
47 #define vmassert(p, ...)
48 #else
49 // Note: message says "assert" rather than "vmassert" for backward
50 // compatibility with tools that parse/match the message text.
51 // Note: The signature is vmassert(p, format, ...), but the solaris
52 // compiler can't handle an empty ellipsis in a macro without a warning.
53 #define vmassert(p, ...)                                                       \
54 do {                                                                           \
55   if (!(p)) {                                                                  \
56     TOUCH_ASSERT_POISON;                                                       \
57     if (is_executing_unit_tests()) {                                           \
58       report_assert_msg(__VA_ARGS__);                                          \
59     }                                                                          \
60     report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", __VA_ARGS__); \
61     BREAKPOINT;                                                                \
62   }                                                                            \
63 } while (0)
64 #endif
65 
66 // For backward compatibility.
67 #define assert(p, ...) vmassert(p, __VA_ARGS__)
68 
69 #ifndef ASSERT
70 #define vmassert_status(p, status, msg)
71 #else
72 // This version of vmassert is for use with checking return status from
73 // library calls that return actual error values eg. EINVAL,
74 // ENOMEM etc, rather than returning -1 and setting errno.
75 // When the status is not what is expected it is very useful to know
76 // what status was actually returned, so we pass the status variable as
77 // an extra arg and use strerror to convert it to a meaningful string
78 // like "Invalid argument", "out of memory" etc
79 #define vmassert_status(p, status, msg) \
80 do {                                                                           \
81   if (!(p)) {                                                                  \
82     TOUCH_ASSERT_POISON;                                                       \
83     report_vm_status_error(__FILE__, __LINE__, "assert(" #p ") failed",        \
84                            status, msg);                                       \
85     BREAKPOINT;                                                                \
86   }                                                                            \
87 } while (0)
88 #endif
89 
90 // For backward compatibility.
91 #define assert_status(p, status, msg) vmassert_status(p, status, msg)
92 
93 // guarantee is like vmassert except it's always executed -- use it for
94 // cheap tests that catch errors that would otherwise be hard to find.
95 // guarantee is also used for Verify options.
96 #define guarantee(p, ...)                                                         \
97 do {                                                                              \
98   if (!(p)) {                                                                     \
99     TOUCH_ASSERT_POISON;                                                          \
100     report_vm_error(__FILE__, __LINE__, "guarantee(" #p ") failed", __VA_ARGS__); \
101     BREAKPOINT;                                                                   \
102   }                                                                               \
103 } while (0)
104 
105 #define fatal(...)                                                                \
106 do {                                                                              \
107   TOUCH_ASSERT_POISON;                                                            \
108   report_fatal(__FILE__, __LINE__, __VA_ARGS__);                                  \
109   BREAKPOINT;                                                                     \
110 } while (0)
111 
112 // out of memory
113 #define vm_exit_out_of_memory(size, vm_err_type, ...)                             \
114 do {                                                                              \
115   report_vm_out_of_memory(__FILE__, __LINE__, size, vm_err_type, __VA_ARGS__);    \
116   BREAKPOINT;                                                                     \
117 } while (0)
118 
119 #define ShouldNotCallThis()                                                       \
120 do {                                                                              \
121   TOUCH_ASSERT_POISON;                                                            \
122   report_should_not_call(__FILE__, __LINE__);                                     \
123   BREAKPOINT;                                                                     \
124 } while (0)
125 
126 #define ShouldNotReachHere()                                                      \
127 do {                                                                              \
128   TOUCH_ASSERT_POISON;                                                            \
129   report_should_not_reach_here(__FILE__, __LINE__);                               \
130   BREAKPOINT;                                                                     \
131 } while (0)
132 
133 #define Unimplemented()                                                           \
134 do {                                                                              \
135   TOUCH_ASSERT_POISON;                                                            \
136   report_unimplemented(__FILE__, __LINE__);                                       \
137   BREAKPOINT;                                                                     \
138 } while (0)
139 
140 #define Untested(msg)                                                             \
141 do {                                                                              \
142   report_untested(__FILE__, __LINE__, msg);                                       \
143   BREAKPOINT;                                                                     \
144 } while (0);
145 
146 
147 // types of VM error - originally in vmError.hpp
148 enum VMErrorType {
149   INTERNAL_ERROR   = 0xe0000000,
150   OOM_MALLOC_ERROR = 0xe0000001,
151   OOM_MMAP_ERROR   = 0xe0000002
152 };
153 
154 // error reporting helper functions
155 void report_vm_error(const char* file, int line, const char* error_msg);
156 #if !defined(__GNUC__) || defined (__clang_major__) || (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || __GNUC__ > 4)
157 // ATTRIBUTE_PRINTF works with gcc >= 4.8 and any other compiler.
158 void report_vm_error(const char* file, int line, const char* error_msg,
159                      const char* detail_fmt, ...) ATTRIBUTE_PRINTF(4, 5);
160 #ifdef ASSERT
161 void report_assert_msg(const char* msg, ...) ATTRIBUTE_PRINTF(1, 2);
162 #endif // ASSERT
163 #else
164 // GCC < 4.8 warns because of empty format string.  Warning can not be switched off selectively.
165 void report_vm_error(const char* file, int line, const char* error_msg,
166                      const char* detail_fmt, ...);
167 #ifdef ASSERT
168 void report_assert_msg(const char* msg, ...);
169 #endif // ASSERT
170 #endif
171 void report_vm_status_error(const char* file, int line, const char* error_msg,
172                             int status, const char* detail);
173 void report_fatal(const char* file, int line, const char* detail_fmt, ...) ATTRIBUTE_PRINTF(3, 4);
174 void report_vm_out_of_memory(const char* file, int line, size_t size, VMErrorType vm_err_type,
175                              const char* detail_fmt, ...) ATTRIBUTE_PRINTF(5, 6);
176 void report_should_not_call(const char* file, int line);
177 void report_should_not_reach_here(const char* file, int line);
178 void report_unimplemented(const char* file, int line);
179 void report_untested(const char* file, int line, const char* message);
180 
181 #ifdef ASSERT
182 // unit test support
183 bool is_executing_unit_tests();
184 #endif // ASSERT
185 
186 void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2);
187 
188 // Compile-time asserts.  Cond must be a compile-time constant expression that
189 // is convertible to bool.  STATIC_ASSERT() can be used anywhere a declaration
190 // may appear.
191 //
192 // Implementation Note: STATIC_ASSERT_FAILURE<true> provides a value member
193 // rather than type member that could be used directly in the typedef, because
194 // a type member would require conditional use of "typename", depending on
195 // whether Cond is dependent or not.  The use of a value member leads to the
196 // use of an array type.
197 
198 template<bool x> struct STATIC_ASSERT_FAILURE;
199 template<> struct STATIC_ASSERT_FAILURE<true> { enum { value = 1 }; };
200 
201 #define STATIC_ASSERT(Cond) \
202   typedef char PASTE_TOKENS(STATIC_ASSERT_DUMMY_TYPE_, __LINE__)[ \
203     STATIC_ASSERT_FAILURE< (Cond) >::value ]
204 
205 // out of memory reporting
206 void report_java_out_of_memory(const char* message);
207 
208 #endif // SHARE_VM_UTILITIES_DEBUG_HPP
209