thread_dbP.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Private header for thread debug library
  2. Copyright (C) 2003-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _THREAD_DBP_H
  16. #define _THREAD_DBP_H 1
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <assert.h>
  23. #include "proc_service.h"
  24. #include "thread_db.h"
  25. #include <pthreadP.h> /* This is for *_BITMASK only. */
  26. #include <list.h>
  27. #include <gnu/lib-names.h>
  28. #include <libc-diag.h>
  29. /* Indices for the symbol names. */
  30. enum
  31. {
  32. # define DB_STRUCT(type) SYM_SIZEOF_##type,
  33. # define DB_STRUCT_FIELD(type, field) SYM_##type##_FIELD_##field,
  34. # define DB_STRUCT_FLEXIBLE_ARRAY(type, field) DB_STRUCT_FIELD (type, field)
  35. # define DB_SYMBOL(name) SYM_##name,
  36. # define DB_FUNCTION(name) SYM_##name,
  37. # define DB_VARIABLE(name) SYM_##name, SYM_DESC_##name,
  38. # include "structs.def"
  39. # undef DB_STRUCT
  40. # undef DB_STRUCT_FIELD
  41. # undef DB_STRUCT_FLEXIBLE_ARRAY
  42. # undef DB_SYMBOL
  43. # undef DB_FUNCTION
  44. # undef DB_VARIABLE
  45. SYM_TH_UNIQUE_CONST_THREAD_AREA,
  46. SYM_TH_UNIQUE_REGISTER64,
  47. SYM_TH_UNIQUE_REGISTER32,
  48. SYM_TH_UNIQUE_REGISTER64_THREAD_AREA,
  49. SYM_TH_UNIQUE_REGISTER32_THREAD_AREA,
  50. SYM_NUM_MESSAGES
  51. };
  52. /* Comment out the following for less verbose output. */
  53. #ifndef NDEBUG
  54. # define LOG(c) \
  55. if (__td_debug) \
  56. assert (write (2, c "\n", strlen (c "\n")) == strlen (c "\n"))
  57. extern int __td_debug attribute_hidden;
  58. #else
  59. # define LOG(c)
  60. #endif
  61. #define DB_DESC_SIZE(desc) ((desc)[0])
  62. #define DB_DESC_NELEM(desc) ((desc)[1])
  63. #define DB_DESC_OFFSET(desc) ((desc)[2])
  64. #define DB_SIZEOF_DESC (3 * sizeof (uint32_t))
  65. #define DB_DEFINE_DESC(name, size, nelem, offset) \
  66. const uint32_t name[3] = { (size), (nelem), (offset) }
  67. typedef uint32_t db_desc_t[3];
  68. /* Handle for a process. This type is opaque. */
  69. struct td_thragent
  70. {
  71. /* Chain on the list of all agent structures. */
  72. list_t list;
  73. /* Delivered by the debugger and we have to pass it back in the
  74. proc callbacks. */
  75. struct ps_prochandle *ph;
  76. /* Cached values read from the inferior. */
  77. # define DB_STRUCT(type) \
  78. uint32_t ta_sizeof_##type;
  79. # define DB_STRUCT_FIELD(type, field) \
  80. db_desc_t ta_field_##type##_##field;
  81. # define DB_STRUCT_FLEXIBLE_ARRAY(type, field) DB_STRUCT_FIELD (type, field)
  82. # define DB_SYMBOL(name) \
  83. psaddr_t ta_addr_##name;
  84. # define DB_FUNCTION(name) \
  85. psaddr_t ta_addr_##name;
  86. # define DB_VARIABLE(name) \
  87. psaddr_t ta_addr_##name; \
  88. db_desc_t ta_var_##name;
  89. # include "structs.def"
  90. # undef DB_STRUCT
  91. # undef DB_STRUCT_FIELD
  92. # undef DB_STRUCT_FLEXIBLE_ARRAY
  93. # undef DB_FUNCTION
  94. # undef DB_SYMBOL
  95. # undef DB_VARIABLE
  96. psaddr_t ta_addr__rtld_global;
  97. /* The method of locating a thread's th_unique value. */
  98. enum
  99. {
  100. ta_howto_unknown,
  101. ta_howto_reg,
  102. ta_howto_reg_thread_area,
  103. ta_howto_const_thread_area
  104. } ta_howto;
  105. union
  106. {
  107. uint32_t const_thread_area; /* Constant argument to ps_get_thread_area. */
  108. /* These are as if the descriptor of the field in prregset_t,
  109. but DB_DESC_NELEM is overloaded as follows: */
  110. db_desc_t reg; /* Signed bias applied to register value. */
  111. db_desc_t reg_thread_area; /* Bits to scale down register value. */
  112. } ta_howto_data;
  113. };
  114. /* List of all known descriptors. */
  115. extern list_t __td_agent_list attribute_hidden;
  116. /* Function used to test for correct thread agent pointer. */
  117. static inline bool
  118. ta_ok (const td_thragent_t *ta)
  119. {
  120. list_t *runp;
  121. list_for_each (runp, &__td_agent_list)
  122. if (list_entry (runp, td_thragent_t, list) == ta)
  123. return true;
  124. return false;
  125. }
  126. /* Internal wrappers around ps_pglobal_lookup. */
  127. extern ps_err_e td_mod_lookup (struct ps_prochandle *ps, const char *modname,
  128. int idx, psaddr_t *sym_addr) attribute_hidden;
  129. #define td_lookup(ps, idx, sym_addr) \
  130. td_mod_lookup ((ps), LIBPTHREAD_SO, (idx), (sym_addr))
  131. /* Store in psaddr_t VAR the address of inferior's symbol NAME. */
  132. #define DB_GET_SYMBOL(var, ta, name) \
  133. (((ta)->ta_addr_##name == NULL \
  134. && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
  135. ? TD_ERR : ((var) = (ta)->ta_addr_##name, TD_OK))
  136. /* Store in psaddr_t VAR the value of ((TYPE) PTR)->FIELD[IDX] in the inferior.
  137. A target field smaller than psaddr_t is zero-extended. */
  138. #define DB_GET_FIELD(var, ta, ptr, type, field, idx) \
  139. _td_fetch_value ((ta), (ta)->ta_field_##type##_##field, \
  140. SYM_##type##_FIELD_##field, \
  141. (psaddr_t) 0 + (idx), (ptr), &(var))
  142. /* With GCC 5.3 when compiling with -Os the compiler emits a warning
  143. that slot may be used uninitialized. This is never the case since
  144. the dynamic loader initializes the slotinfo list and
  145. dtv_slotinfo_list will point slot at the first entry. Therefore
  146. when DB_GET_FIELD_ADDRESS is called with a slot for ptr, the slot is
  147. always initialized. */
  148. DIAG_PUSH_NEEDS_COMMENT;
  149. DIAG_IGNORE_Os_NEEDS_COMMENT_GCC (5, "-Wmaybe-uninitialized");
  150. #define DB_GET_FIELD_ADDRESS(var, ta, ptr, type, field, idx) \
  151. ((var) = (ptr), _td_locate_field ((ta), (ta)->ta_field_##type##_##field, \
  152. SYM_##type##_FIELD_##field, \
  153. (psaddr_t) 0 + (idx), &(var)))
  154. DIAG_POP_NEEDS_COMMENT;
  155. extern td_err_e _td_locate_field (td_thragent_t *ta,
  156. db_desc_t desc, int descriptor_name,
  157. psaddr_t idx,
  158. psaddr_t *address) attribute_hidden;
  159. /* Like DB_GET_FIELD, but PTR is a local pointer to a structure that
  160. has already been copied in from the inferior. */
  161. #define DB_GET_FIELD_LOCAL(var, ta, ptr, type, field, idx) \
  162. _td_fetch_value_local ((ta), (ta)->ta_field_##type##_##field, \
  163. SYM_##type##_FIELD_##field, \
  164. (psaddr_t) 0 + (idx), (ptr), &(var))
  165. /* Store in psaddr_t VAR the value of variable NAME[IDX] in the inferior.
  166. A target value smaller than psaddr_t is zero-extended. */
  167. #define DB_GET_VALUE(var, ta, name, idx) \
  168. (((ta)->ta_addr_##name == NULL \
  169. && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
  170. ? TD_ERR \
  171. : _td_fetch_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \
  172. (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, &(var)))
  173. /* Helper functions for those. */
  174. extern td_err_e _td_fetch_value (td_thragent_t *ta,
  175. db_desc_t field, int descriptor_name,
  176. psaddr_t idx, psaddr_t address,
  177. psaddr_t *result) attribute_hidden;
  178. extern td_err_e _td_fetch_value_local (td_thragent_t *ta,
  179. db_desc_t field,
  180. int descriptor_name,
  181. psaddr_t idx, void *address,
  182. psaddr_t *result) attribute_hidden;
  183. /* Store psaddr_t VALUE in ((TYPE) PTR)->FIELD[IDX] in the inferior.
  184. A target field smaller than psaddr_t is zero-extended. */
  185. #define DB_PUT_FIELD(ta, ptr, type, field, idx, value) \
  186. _td_store_value ((ta), (ta)->ta_field_##type##_##field, \
  187. SYM_##type##_FIELD_##field, \
  188. (psaddr_t) 0 + (idx), (ptr), (value))
  189. #define DB_PUT_FIELD_LOCAL(ta, ptr, type, field, idx, value) \
  190. _td_store_value_local ((ta), (ta)->ta_field_##type##_##field, \
  191. SYM_##type##_FIELD_##field, \
  192. (psaddr_t) 0 + (idx), (ptr), (value))
  193. /* Store psaddr_t VALUE in variable NAME[IDX] in the inferior.
  194. A target field smaller than psaddr_t is zero-extended. */
  195. #define DB_PUT_VALUE(ta, name, idx, value) \
  196. (((ta)->ta_addr_##name == NULL \
  197. && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
  198. ? TD_ERR \
  199. : _td_store_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \
  200. (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, (value)))
  201. /* Helper functions for those. */
  202. extern td_err_e _td_store_value (td_thragent_t *ta,
  203. db_desc_t field, int descriptor_name,
  204. psaddr_t idx, psaddr_t address,
  205. psaddr_t value) attribute_hidden;
  206. extern td_err_e _td_store_value_local (td_thragent_t *ta,
  207. db_desc_t field, int descriptor_name,
  208. psaddr_t idx, void *address,
  209. psaddr_t value) attribute_hidden;
  210. #define DB_GET_STRUCT(var, ta, ptr, type) \
  211. ({ td_err_e _err = TD_OK; \
  212. if ((ta)->ta_sizeof_##type == 0) \
  213. _err = _td_check_sizeof ((ta), &(ta)->ta_sizeof_##type, \
  214. SYM_SIZEOF_##type); \
  215. if (_err == TD_OK) \
  216. _err = ps_pdread ((ta)->ph, (ptr), \
  217. (var) = __alloca ((ta)->ta_sizeof_##type), \
  218. (ta)->ta_sizeof_##type) \
  219. == PS_OK ? TD_OK : TD_ERR; \
  220. else \
  221. (var) = NULL; \
  222. _err; \
  223. })
  224. #define DB_PUT_STRUCT(ta, ptr, type, copy) \
  225. ({ assert ((ta)->ta_sizeof_##type != 0); \
  226. ps_pdwrite ((ta)->ph, (ptr), (copy), (ta)->ta_sizeof_##type) \
  227. == PS_OK ? TD_OK : TD_ERR; \
  228. })
  229. extern td_err_e _td_check_sizeof (td_thragent_t *ta, uint32_t *sizep,
  230. int sizep_name) attribute_hidden;
  231. extern td_err_e __td_ta_lookup_th_unique (const td_thragent_t *ta,
  232. lwpid_t lwpid, td_thrhandle_t *th);
  233. /* Try to initialize TA->ta_addr__rtld_global. Return true on
  234. success, false on failure (which may be cached). */
  235. bool __td_ta_rtld_global (td_thragent_t *ta) attribute_hidden;
  236. /* Obtain the address of the list_t fields _dl_stack_user and
  237. _dl_stack_used in _rtld_global, or fall back to the global
  238. variables of the same name (to support statically linked
  239. programs). */
  240. td_err_e __td_ta_stack_user (td_thragent_t *ta, psaddr_t *plist)
  241. attribute_hidden;
  242. td_err_e __td_ta_stack_used (td_thragent_t *ta, psaddr_t *plist)
  243. attribute_hidden;
  244. #endif /* thread_dbP.h */