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