3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #ifndef COMMON_COMMON_DEFS_H
21 #define COMMON_COMMON_DEFS_H
23 #if defined (__SANITIZE_THREAD__) && defined (__GNUC__) \
24 && !defined (__clang__) && __GNUC__ <= 13
26 /* Work around PR gcc/110799. */
27 #pragma GCC optimize("-fno-hoist-adjacent-loads")
30 #include <gdbsupport/config.h>
34 #undef PACKAGE_VERSION
36 #undef PACKAGE_TARNAME
38 #include "gnulib/config.h"
41 https://www.gnu.org/software/gnulib/manual/html_node/stdint_002eh.html
43 "On some hosts that predate C++11, when using C++ one must define
44 __STDC_CONSTANT_MACROS to make visible the definitions of constant
45 macros such as INTMAX_C, and one must define __STDC_LIMIT_MACROS to
46 make visible the definitions of limit macros such as INTMAX_MAX.".
49 https://www.gnu.org/software/gnulib/manual/html_node/inttypes_002eh.html
51 "On some hosts that predate C++11, when using C++ one must define
52 __STDC_FORMAT_MACROS to make visible the declarations of format
53 macros such as PRIdMAX."
55 Must do this before including any system header, since other system
56 headers may include stdint.h/inttypes.h. */
57 #define __STDC_CONSTANT_MACROS 1
58 #define __STDC_LIMIT_MACROS 1
59 #define __STDC_FORMAT_MACROS 1
61 /* Some distros enable _FORTIFY_SOURCE by default, which on occasion
62 has caused build failures with -Wunused-result when a patch is
63 developed on a distro that does not enable _FORTIFY_SOURCE. We
64 enable it here in order to try to catch these problems earlier;
65 plus this seems like a reasonable safety measure. The check for
66 optimization is required because _FORTIFY_SOURCE only works when
67 optimization is enabled. If _FORTIFY_SOURCE is already defined,
68 then we don't do anything. Also, on MinGW, fortify requires
69 linking to -lssp, and to avoid the hassle of checking for
70 that and linking to it statically, we just don't define
71 _FORTIFY_SOURCE there. */
73 #if (!defined _FORTIFY_SOURCE && defined __OPTIMIZE__ && __OPTIMIZE__ > 0 \
74 && !defined(__MINGW32__))
75 #define _FORTIFY_SOURCE 2
78 /* We don't support Windows versions before XP, so we define
79 _WIN32_WINNT correspondingly to ensure the Windows API headers
80 expose the required symbols.
82 NOTE: this must be kept in sync with common.m4. */
83 #if defined (__MINGW32__) || defined (__CYGWIN__)
85 # if _WIN32_WINNT < 0x0501
87 # define _WIN32_WINNT 0x0501
90 # define _WIN32_WINNT 0x0501
92 #endif /* __MINGW32__ || __CYGWIN__ */
97 /* Include both cstdlib and stdlib.h to ensure we have standard functions
98 defined both in the std:: namespace and in the global namespace. */
105 #ifdef HAVE_STRINGS_H
113 #include "ansidecl.h"
114 /* This is defined by ansidecl.h, but we prefer gnulib's version. On
115 MinGW, gnulib might enable __USE_MINGW_ANSI_STDIO, which may or not
116 require use of attribute gnu_printf instead of printf. gnulib
117 checks that at configure time. Since _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD
118 is compatible with ATTRIBUTE_PRINTF, simply use it. */
119 #undef ATTRIBUTE_PRINTF
120 #define ATTRIBUTE_PRINTF _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD
122 /* This is defined by ansidecl.h, but we disable the attribute.
124 Say a developer starts out with:
126 extern void foo (void *ptr) __attribute__((nonnull (1)));
127 void foo (void *ptr) {}
129 with the idea in mind to catch:
133 at compile time with -Werror=nonnull, and then adds:
135 void foo (void *ptr) {
136 + gdb_assert (ptr != nullptr);
141 foo (variable_with_nullptr_value);
145 Said developer then verifies that the assert works (using -O0), and commits
148 Some other developer then checks out the code and accidentally writes some
151 foo (variable_with_nullptr_value);
153 and builds with -O2, and ... the assert doesn't trigger, because it's
154 optimized away by gcc.
156 There's no suppported recipe to prevent the assertion from being optimized
157 away (other than: build with -O0, or remove the nonnull attribute). Note
158 that -fno-delete-null-pointer-checks does not help. A patch was submitted
159 to improve gcc documentation to point this out more clearly (
160 https://gcc.gnu.org/pipermail/gcc-patches/2021-July/576218.html ). The
161 patch also mentions a possible workaround that obfuscates the pointer
164 void foo (void *ptr) {
165 + asm ("" : "+r"(ptr));
166 gdb_assert (ptr != nullptr);
169 but that still requires the developer to manually add this in all cases
170 where that's necessary.
172 A warning was added to detect the situation: -Wnonnull-compare, which does
173 help in detecting those cases, but each new gcc release may indicate a new
174 batch of locations that needs fixing, which means we've added a maintenance
177 We could try to deal with the problem more proactively by introducing a
178 gdb_assert variant like:
180 void gdb_assert_non_null (void *ptr) {
181 asm ("" : "+r"(ptr));
182 gdb_assert (ptr != nullptr);
184 void foo (void *ptr) {
185 gdb_assert_nonnull (ptr);
188 and make it a coding style to use it everywhere, but again, maintenance
191 With all these things considered, for now we go with the solution with the
192 least maintenance burden: disable the attribute, such that we reliably deal
193 with it everywhere. */
194 #undef ATTRIBUTE_NONNULL
195 #define ATTRIBUTE_NONNULL(m)
197 #define ATTRIBUTE_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
198 #define ATTRIBUTE_USED __attribute__ ((__used__))
200 #include "libiberty.h"
202 #include "gdb/signals.h"
203 #include "gdb_locale.h"
205 #include "common-types.h"
206 #include "common-utils.h"
207 #include "gdb_assert.h"
209 #include "print-utils.h"
210 #include "common-debug.h"
211 #include "cleanups.h"
212 #include "common-exceptions.h"
213 #include "gdbsupport/poison.h"
215 /* Pull in gdb::unique_xmalloc_ptr. */
216 #include "gdbsupport/gdb_unique_ptr.h"
218 /* sbrk on macOS is not useful for our purposes, since sbrk(0) always
219 returns the same value. brk/sbrk on macOS is just an emulation
220 that always returns a pointer to a 4MB section reserved for
223 #if defined (HAVE_SBRK) && !__APPLE__
224 #define HAVE_USEFUL_SBRK 1
227 #endif /* COMMON_COMMON_DEFS_H */