thread_dbP.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Private header for thread debug library
  2. Copyright (C) 2003, 2004 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. #ifdef __UCLIBC__
  27. #define __alloca alloca
  28. #endif
  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 wrapper around ps_pglobal_lookup. */
  120. extern ps_err_e td_lookup (struct ps_prochandle *ps,
  121. int idx, psaddr_t *sym_addr) attribute_hidden;
  122. /* Store in psaddr_t VAR the address of inferior's symbol NAME. */
  123. #define DB_GET_SYMBOL(var, ta, name) \
  124. (((ta)->ta_addr_##name == 0 \
  125. && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
  126. ? TD_ERR : ((var) = (ta)->ta_addr_##name, TD_OK))
  127. /* Store in psaddr_t VAR the value of ((TYPE) PTR)->FIELD[IDX] in the inferior.
  128. A target field smaller than psaddr_t is zero-extended. */
  129. #define DB_GET_FIELD(var, ta, ptr, type, field, idx) \
  130. _td_fetch_value ((ta), (ta)->ta_field_##type##_##field, \
  131. SYM_##type##_FIELD_##field, \
  132. (psaddr_t) 0 + (idx), (ptr), &(var))
  133. #define DB_GET_FIELD_ADDRESS(var, ta, ptr, type, field, idx) \
  134. ((var) = (ptr), _td_locate_field ((ta), (ta)->ta_field_##type##_##field, \
  135. SYM_##type##_FIELD_##field, \
  136. (psaddr_t) 0 + (idx), &(var)))
  137. extern td_err_e _td_locate_field (td_thragent_t *ta,
  138. db_desc_t desc, int descriptor_name,
  139. psaddr_t idx,
  140. psaddr_t *address) attribute_hidden;
  141. /* Like DB_GET_FIELD, but PTR is a local pointer to a structure that
  142. has already been copied in from the inferior. */
  143. #define DB_GET_FIELD_LOCAL(var, ta, ptr, type, field, idx) \
  144. _td_fetch_value_local ((ta), (ta)->ta_field_##type##_##field, \
  145. SYM_##type##_FIELD_##field, \
  146. (psaddr_t) 0 + (idx), (ptr), &(var))
  147. /* Store in psaddr_t VAR the value of variable NAME[IDX] in the inferior.
  148. A target value smaller than psaddr_t is zero-extended. */
  149. #define DB_GET_VALUE(var, ta, name, idx) \
  150. (((ta)->ta_addr_##name == 0 \
  151. && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
  152. ? TD_ERR \
  153. : _td_fetch_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \
  154. (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, &(var)))
  155. /* Helper functions for those. */
  156. extern td_err_e _td_fetch_value (td_thragent_t *ta,
  157. db_desc_t field, int descriptor_name,
  158. psaddr_t idx, psaddr_t address,
  159. psaddr_t *result) attribute_hidden;
  160. extern td_err_e _td_fetch_value_local (td_thragent_t *ta,
  161. db_desc_t field,
  162. int descriptor_name,
  163. psaddr_t idx, void *address,
  164. psaddr_t *result) attribute_hidden;
  165. /* Store psaddr_t VALUE in ((TYPE) PTR)->FIELD[IDX] in the inferior.
  166. A target field smaller than psaddr_t is zero-extended. */
  167. #define DB_PUT_FIELD(ta, ptr, type, field, idx, value) \
  168. _td_store_value ((ta), (ta)->ta_field_##type##_##field, \
  169. SYM_##type##_FIELD_##field, \
  170. (psaddr_t) 0 + (idx), (ptr), (value))
  171. #define DB_PUT_FIELD_LOCAL(ta, ptr, type, field, idx, value) \
  172. _td_store_value_local ((ta), (ta)->ta_field_##type##_##field, \
  173. SYM_##type##_FIELD_##field, \
  174. (psaddr_t) 0 + (idx), (ptr), (value))
  175. /* Store psaddr_t VALUE in variable NAME[IDX] in the inferior.
  176. A target field smaller than psaddr_t is zero-extended. */
  177. #define DB_PUT_VALUE(ta, name, idx, value) \
  178. (((ta)->ta_addr_##name == 0 \
  179. && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
  180. ? TD_ERR \
  181. : _td_store_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \
  182. (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, (value)))
  183. /* Helper functions for those. */
  184. extern td_err_e _td_store_value (td_thragent_t *ta,
  185. db_desc_t field, int descriptor_name,
  186. psaddr_t idx, psaddr_t address,
  187. psaddr_t value) attribute_hidden;
  188. extern td_err_e _td_store_value_local (td_thragent_t *ta,
  189. db_desc_t field, int descriptor_name,
  190. psaddr_t idx, void *address,
  191. psaddr_t value) attribute_hidden;
  192. #define DB_GET_STRUCT(var, ta, ptr, type) \
  193. ({ td_err_e _err = TD_OK; \
  194. if ((ta)->ta_sizeof_##type == 0) \
  195. _err = _td_check_sizeof ((ta), &(ta)->ta_sizeof_##type, \
  196. SYM_SIZEOF_##type); \
  197. if (_err == TD_OK) \
  198. _err = ps_pdread ((ta)->ph, (ptr), \
  199. (var) = __alloca ((ta)->ta_sizeof_##type), \
  200. (ta)->ta_sizeof_##type) \
  201. == PS_OK ? TD_OK : TD_ERR; \
  202. else \
  203. (var) = NULL; \
  204. _err; \
  205. })
  206. #define DB_PUT_STRUCT(ta, ptr, type, copy) \
  207. ({ assert ((ta)->ta_sizeof_##type != 0); \
  208. ps_pdwrite ((ta)->ph, (ptr), (copy), (ta)->ta_sizeof_##type) \
  209. == PS_OK ? TD_OK : TD_ERR; \
  210. })
  211. extern td_err_e _td_check_sizeof (td_thragent_t *ta, uint32_t *sizep,
  212. int sizep_name) attribute_hidden;
  213. #endif /* thread_dbP.h */