common-defs.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Common definitions.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_COMMON_DEFS_H
  15. #define COMMON_COMMON_DEFS_H
  16. #include <gdbsupport/config.h>
  17. #undef PACKAGE_NAME
  18. #undef PACKAGE
  19. #undef PACKAGE_VERSION
  20. #undef PACKAGE_STRING
  21. #undef PACKAGE_TARNAME
  22. #include "gnulib/config.h"
  23. /* From:
  24. https://www.gnu.org/software/gnulib/manual/html_node/stdint_002eh.html
  25. "On some hosts that predate C++11, when using C++ one must define
  26. __STDC_CONSTANT_MACROS to make visible the definitions of constant
  27. macros such as INTMAX_C, and one must define __STDC_LIMIT_MACROS to
  28. make visible the definitions of limit macros such as INTMAX_MAX.".
  29. And:
  30. https://www.gnu.org/software/gnulib/manual/html_node/inttypes_002eh.html
  31. "On some hosts that predate C++11, when using C++ one must define
  32. __STDC_FORMAT_MACROS to make visible the declarations of format
  33. macros such as PRIdMAX."
  34. Must do this before including any system header, since other system
  35. headers may include stdint.h/inttypes.h. */
  36. #define __STDC_CONSTANT_MACROS 1
  37. #define __STDC_LIMIT_MACROS 1
  38. #define __STDC_FORMAT_MACROS 1
  39. /* Some distros enable _FORTIFY_SOURCE by default, which on occasion
  40. has caused build failures with -Wunused-result when a patch is
  41. developed on a distro that does not enable _FORTIFY_SOURCE. We
  42. enable it here in order to try to catch these problems earlier;
  43. plus this seems like a reasonable safety measure. The check for
  44. optimization is required because _FORTIFY_SOURCE only works when
  45. optimization is enabled. If _FORTIFY_SOURCE is already defined,
  46. then we don't do anything. Also, on MinGW, fortify requires
  47. linking to -lssp, and to avoid the hassle of checking for
  48. that and linking to it statically, we just don't define
  49. _FORTIFY_SOURCE there. */
  50. #if (!defined _FORTIFY_SOURCE && defined __OPTIMIZE__ && __OPTIMIZE__ > 0 \
  51. && !defined(__MINGW32__))
  52. #define _FORTIFY_SOURCE 2
  53. #endif
  54. /* We don't support Windows versions before XP, so we define
  55. _WIN32_WINNT correspondingly to ensure the Windows API headers
  56. expose the required symbols. */
  57. #if defined (__MINGW32__) || defined (__CYGWIN__)
  58. # ifdef _WIN32_WINNT
  59. # if _WIN32_WINNT < 0x0501
  60. # undef _WIN32_WINNT
  61. # define _WIN32_WINNT 0x0501
  62. # endif
  63. # else
  64. # define _WIN32_WINNT 0x0501
  65. # endif
  66. #endif /* __MINGW32__ || __CYGWIN__ */
  67. #include <stdarg.h>
  68. #include <stdio.h>
  69. /* Include both cstdlib and stdlib.h to ensure we have standard functions
  70. defined both in the std:: namespace and in the global namespace. */
  71. #include <cstdlib>
  72. #include <stdlib.h>
  73. #include <stddef.h>
  74. #include <stdint.h>
  75. #include <string.h>
  76. #ifdef HAVE_STRINGS_H
  77. #include <strings.h> /* for strcasecmp and strncasecmp */
  78. #endif
  79. #include <errno.h>
  80. #if HAVE_ALLOCA_H
  81. #include <alloca.h>
  82. #endif
  83. #include "ansidecl.h"
  84. /* This is defined by ansidecl.h, but we prefer gnulib's version. On
  85. MinGW, gnulib might enable __USE_MINGW_ANSI_STDIO, which may or not
  86. require use of attribute gnu_printf instead of printf. gnulib
  87. checks that at configure time. Since _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD
  88. is compatible with ATTRIBUTE_PRINTF, simply use it. */
  89. #undef ATTRIBUTE_PRINTF
  90. #define ATTRIBUTE_PRINTF _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD
  91. /* This is defined by ansidecl.h, but we disable the attribute.
  92. Say a developer starts out with:
  93. ...
  94. extern void foo (void *ptr) __atttribute__((nonnull (1)));
  95. void foo (void *ptr) {}
  96. ...
  97. with the idea in mind to catch:
  98. ...
  99. foo (nullptr);
  100. ...
  101. at compile time with -Werror=nonnull, and then adds:
  102. ...
  103. void foo (void *ptr) {
  104. + gdb_assert (ptr != nullptr);
  105. }
  106. ...
  107. to catch:
  108. ...
  109. foo (variable_with_nullptr_value);
  110. ...
  111. at runtime as well.
  112. Said developer then verifies that the assert works (using -O0), and commits
  113. the code.
  114. Some other developer then checks out the code and accidentally writes some
  115. variant of:
  116. ...
  117. foo (variable_with_nullptr_value);
  118. ...
  119. and builds with -O2, and ... the assert doesn't trigger, because it's
  120. optimized away by gcc.
  121. There's no suppported recipe to prevent the assertion from being optimized
  122. away (other than: build with -O0, or remove the nonnull attribute). Note
  123. that -fno-delete-null-pointer-checks does not help. A patch was submitted
  124. to improve gcc documentation to point this out more clearly (
  125. https://gcc.gnu.org/pipermail/gcc-patches/2021-July/576218.html ). The
  126. patch also mentions a possible workaround that obfuscates the pointer
  127. using:
  128. ...
  129. void foo (void *ptr) {
  130. + asm ("" : "+r"(ptr));
  131. gdb_assert (ptr != nullptr);
  132. }
  133. ...
  134. but that still requires the developer to manually add this in all cases
  135. where that's necessary.
  136. A warning was added to detect the situation: -Wnonnull-compare, which does
  137. help in detecting those cases, but each new gcc release may indicate a new
  138. batch of locations that needs fixing, which means we've added a maintenance
  139. burden.
  140. We could try to deal with the problem more proactively by introducing a
  141. gdb_assert variant like:
  142. ...
  143. void gdb_assert_non_null (void *ptr) {
  144. asm ("" : "+r"(ptr));
  145. gdb_assert (ptr != nullptr);
  146. }
  147. void foo (void *ptr) {
  148. gdb_assert_nonnull (ptr);
  149. }
  150. ...
  151. and make it a coding style to use it everywhere, but again, maintenance
  152. burden.
  153. With all these things considered, for now we go with the solution with the
  154. least maintenance burden: disable the attribute, such that we reliably deal
  155. with it everywhere. */
  156. #undef ATTRIBUTE_NONNULL
  157. #define ATTRIBUTE_NONNULL(m)
  158. #if GCC_VERSION >= 3004
  159. #define ATTRIBUTE_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
  160. #else
  161. #define ATTRIBUTE_UNUSED_RESULT
  162. #endif
  163. #include "libiberty.h"
  164. #include "pathmax.h"
  165. #include "gdb/signals.h"
  166. #include "gdb_locale.h"
  167. #include "ptid.h"
  168. #include "common-types.h"
  169. #include "common-utils.h"
  170. #include "gdb_assert.h"
  171. #include "errors.h"
  172. #include "print-utils.h"
  173. #include "common-debug.h"
  174. #include "cleanups.h"
  175. #include "common-exceptions.h"
  176. #include "gdbsupport/poison.h"
  177. #define EXTERN_C extern "C"
  178. #define EXTERN_C_PUSH extern "C" {
  179. #define EXTERN_C_POP }
  180. /* Pull in gdb::unique_xmalloc_ptr. */
  181. #include "gdbsupport/gdb_unique_ptr.h"
  182. /* sbrk on macOS is not useful for our purposes, since sbrk(0) always
  183. returns the same value. brk/sbrk on macOS is just an emulation
  184. that always returns a pointer to a 4MB section reserved for
  185. that. */
  186. #if defined (HAVE_SBRK) && !__APPLE__
  187. #define HAVE_USEFUL_SBRK 1
  188. #endif
  189. #endif /* COMMON_COMMON_DEFS_H */