thread_dbP.h 9.2 KB

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