Commit | Line | Data |
---|---|---|
976411d6 GB |
1 | /* Common definitions. |
2 | ||
88b9d363 | 3 | Copyright (C) 1986-2022 Free Software Foundation, Inc. |
976411d6 GB |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
1a5c2598 TT |
20 | #ifndef COMMON_COMMON_DEFS_H |
21 | #define COMMON_COMMON_DEFS_H | |
976411d6 | 22 | |
3684d331 | 23 | #include <gdbsupport/config.h> |
976411d6 | 24 | |
d0ac1c44 | 25 | #undef PACKAGE_NAME |
01027315 | 26 | #undef PACKAGE |
d0ac1c44 SM |
27 | #undef PACKAGE_VERSION |
28 | #undef PACKAGE_STRING | |
29 | #undef PACKAGE_TARNAME | |
30 | ||
01027315 TT |
31 | #include "gnulib/config.h" |
32 | ||
28fe4f87 PA |
33 | /* From: |
34 | https://www.gnu.org/software/gnulib/manual/html_node/stdint_002eh.html | |
35 | ||
36 | "On some hosts that predate C++11, when using C++ one must define | |
37 | __STDC_CONSTANT_MACROS to make visible the definitions of constant | |
38 | macros such as INTMAX_C, and one must define __STDC_LIMIT_MACROS to | |
39 | make visible the definitions of limit macros such as INTMAX_MAX.". | |
40 | ||
b30f354a PA |
41 | And: |
42 | https://www.gnu.org/software/gnulib/manual/html_node/inttypes_002eh.html | |
43 | ||
44 | "On some hosts that predate C++11, when using C++ one must define | |
45 | __STDC_FORMAT_MACROS to make visible the declarations of format | |
46 | macros such as PRIdMAX." | |
47 | ||
48 | Must do this before including any system header, since other system | |
49 | headers may include stdint.h/inttypes.h. */ | |
28fe4f87 PA |
50 | #define __STDC_CONSTANT_MACROS 1 |
51 | #define __STDC_LIMIT_MACROS 1 | |
b30f354a | 52 | #define __STDC_FORMAT_MACROS 1 |
28fe4f87 | 53 | |
a97b53dd TT |
54 | /* Some distros enable _FORTIFY_SOURCE by default, which on occasion |
55 | has caused build failures with -Wunused-result when a patch is | |
56 | developed on a distro that does not enable _FORTIFY_SOURCE. We | |
57 | enable it here in order to try to catch these problems earlier; | |
58 | plus this seems like a reasonable safety measure. The check for | |
59 | optimization is required because _FORTIFY_SOURCE only works when | |
656efb5e | 60 | optimization is enabled. If _FORTIFY_SOURCE is already defined, |
5f23a082 CB |
61 | then we don't do anything. Also, on MinGW, fortify requires |
62 | linking to -lssp, and to avoid the hassle of checking for | |
63 | that and linking to it statically, we just don't define | |
64 | _FORTIFY_SOURCE there. */ | |
a97b53dd | 65 | |
5f23a082 CB |
66 | #if (!defined _FORTIFY_SOURCE && defined __OPTIMIZE__ && __OPTIMIZE__ > 0 \ |
67 | && !defined(__MINGW32__)) | |
a97b53dd TT |
68 | #define _FORTIFY_SOURCE 2 |
69 | #endif | |
70 | ||
353ea2d1 EZ |
71 | /* We don't support Windows versions before XP, so we define |
72 | _WIN32_WINNT correspondingly to ensure the Windows API headers | |
73 | expose the required symbols. */ | |
74 | #if defined (__MINGW32__) || defined (__CYGWIN__) | |
75 | # ifdef _WIN32_WINNT | |
76 | # if _WIN32_WINNT < 0x0501 | |
77 | # undef _WIN32_WINNT | |
78 | # define _WIN32_WINNT 0x0501 | |
79 | # endif | |
80 | # else | |
81 | # define _WIN32_WINNT 0x0501 | |
82 | # endif | |
83 | #endif /* __MINGW32__ || __CYGWIN__ */ | |
84 | ||
355e2102 | 85 | #include <stdarg.h> |
d02f550d | 86 | #include <stdio.h> |
ff8885c3 SM |
87 | |
88 | /* Include both cstdlib and stdlib.h to ensure we have standard functions | |
89 | defined both in the std:: namespace and in the global namespace. */ | |
90 | #include <cstdlib> | |
d7096f71 | 91 | #include <stdlib.h> |
ff8885c3 | 92 | |
8ebb3f56 | 93 | #include <stddef.h> |
8cc73a39 | 94 | #include <stdint.h> |
6d3d12eb | 95 | #include <string.h> |
8ffc1bb1 EZ |
96 | #ifdef HAVE_STRINGS_H |
97 | #include <strings.h> /* for strcasecmp and strncasecmp */ | |
98 | #endif | |
bb974a24 | 99 | #include <errno.h> |
d3e2a5e8 | 100 | #if HAVE_ALLOCA_H |
9c232dda | 101 | #include <alloca.h> |
d3e2a5e8 | 102 | #endif |
91ee7171 | 103 | |
0e443c87 | 104 | #include "ansidecl.h" |
91ee7171 PA |
105 | /* This is defined by ansidecl.h, but we prefer gnulib's version. On |
106 | MinGW, gnulib might enable __USE_MINGW_ANSI_STDIO, which may or not | |
107 | require use of attribute gnu_printf instead of printf. gnulib | |
9c9d63b1 | 108 | checks that at configure time. Since _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD |
91ee7171 PA |
109 | is compatible with ATTRIBUTE_PRINTF, simply use it. */ |
110 | #undef ATTRIBUTE_PRINTF | |
9c9d63b1 | 111 | #define ATTRIBUTE_PRINTF _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD |
91ee7171 | 112 | |
7fab8f85 TV |
113 | /* This is defined by ansidecl.h, but we disable the attribute. |
114 | ||
115 | Say a developer starts out with: | |
116 | ... | |
117 | extern void foo (void *ptr) __atttribute__((nonnull (1))); | |
118 | void foo (void *ptr) {} | |
119 | ... | |
120 | with the idea in mind to catch: | |
121 | ... | |
122 | foo (nullptr); | |
123 | ... | |
124 | at compile time with -Werror=nonnull, and then adds: | |
125 | ... | |
126 | void foo (void *ptr) { | |
127 | + gdb_assert (ptr != nullptr); | |
128 | } | |
129 | ... | |
130 | to catch: | |
131 | ... | |
132 | foo (variable_with_nullptr_value); | |
133 | ... | |
134 | at runtime as well. | |
135 | ||
136 | Said developer then verifies that the assert works (using -O0), and commits | |
137 | the code. | |
138 | ||
139 | Some other developer then checks out the code and accidentally writes some | |
140 | variant of: | |
141 | ... | |
142 | foo (variable_with_nullptr_value); | |
143 | ... | |
144 | and builds with -O2, and ... the assert doesn't trigger, because it's | |
145 | optimized away by gcc. | |
146 | ||
147 | There's no suppported recipe to prevent the assertion from being optimized | |
148 | away (other than: build with -O0, or remove the nonnull attribute). Note | |
149 | that -fno-delete-null-pointer-checks does not help. A patch was submitted | |
150 | to improve gcc documentation to point this out more clearly ( | |
151 | https://gcc.gnu.org/pipermail/gcc-patches/2021-July/576218.html ). The | |
152 | patch also mentions a possible workaround that obfuscates the pointer | |
153 | using: | |
154 | ... | |
155 | void foo (void *ptr) { | |
156 | + asm ("" : "+r"(ptr)); | |
157 | gdb_assert (ptr != nullptr); | |
158 | } | |
159 | ... | |
160 | but that still requires the developer to manually add this in all cases | |
161 | where that's necessary. | |
162 | ||
163 | A warning was added to detect the situation: -Wnonnull-compare, which does | |
164 | help in detecting those cases, but each new gcc release may indicate a new | |
165 | batch of locations that needs fixing, which means we've added a maintenance | |
166 | burden. | |
167 | ||
168 | We could try to deal with the problem more proactively by introducing a | |
169 | gdb_assert variant like: | |
170 | ... | |
171 | void gdb_assert_non_null (void *ptr) { | |
172 | asm ("" : "+r"(ptr)); | |
173 | gdb_assert (ptr != nullptr); | |
174 | } | |
175 | void foo (void *ptr) { | |
176 | gdb_assert_nonnull (ptr); | |
177 | } | |
178 | ... | |
179 | and make it a coding style to use it everywhere, but again, maintenance | |
180 | burden. | |
181 | ||
182 | With all these things considered, for now we go with the solution with the | |
183 | least maintenance burden: disable the attribute, such that we reliably deal | |
184 | with it everywhere. */ | |
185 | #undef ATTRIBUTE_NONNULL | |
186 | #define ATTRIBUTE_NONNULL(m) | |
187 | ||
18cb7c9f TT |
188 | #if GCC_VERSION >= 3004 |
189 | #define ATTRIBUTE_UNUSED_RESULT __attribute__ ((__warn_unused_result__)) | |
190 | #else | |
191 | #define ATTRIBUTE_UNUSED_RESULT | |
192 | #endif | |
193 | ||
b9391142 | 194 | #include "libiberty.h" |
a5fceff8 | 195 | #include "pathmax.h" |
cb9f1a9b | 196 | #include "gdb/signals.h" |
3995eeee | 197 | #include "gdb_locale.h" |
4cb9c816 | 198 | #include "ptid.h" |
03aef70f | 199 | #include "common-types.h" |
e76df0d0 | 200 | #include "common-utils.h" |
dccbb609 | 201 | #include "gdb_assert.h" |
ef87c8bb | 202 | #include "errors.h" |
f6e94d78 | 203 | #include "print-utils.h" |
34abf635 | 204 | #include "common-debug.h" |
6f1947e8 | 205 | #include "cleanups.h" |
a442d071 | 206 | #include "common-exceptions.h" |
268a13a5 | 207 | #include "gdbsupport/poison.h" |
d02f550d | 208 | |
ddb6d633 PA |
209 | #define EXTERN_C extern "C" |
210 | #define EXTERN_C_PUSH extern "C" { | |
211 | #define EXTERN_C_POP } | |
56000a98 | 212 | |
5cc8c731 | 213 | /* Pull in gdb::unique_xmalloc_ptr. */ |
268a13a5 | 214 | #include "gdbsupport/gdb_unique_ptr.h" |
da804164 | 215 | |
b4987c95 SDJ |
216 | /* String containing the current directory (what getwd would return). */ |
217 | extern char *current_directory; | |
218 | ||
6242c6a6 SM |
219 | /* sbrk on macOS is not useful for our purposes, since sbrk(0) always |
220 | returns the same value. brk/sbrk on macOS is just an emulation | |
221 | that always returns a pointer to a 4MB section reserved for | |
222 | that. */ | |
223 | ||
224 | #if defined (HAVE_SBRK) && !__APPLE__ | |
225 | #define HAVE_USEFUL_SBRK 1 | |
226 | #endif | |
227 | ||
1a5c2598 | 228 | #endif /* COMMON_COMMON_DEFS_H */ |