1 | /* Private header for thread debug library |
2 | Copyright (C) 2003-2021 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 "../nptl/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 | /* The method of locating a thread's th_unique value. */ |
112 | enum |
113 | { |
114 | ta_howto_unknown, |
115 | ta_howto_reg, |
116 | ta_howto_reg_thread_area, |
117 | ta_howto_const_thread_area |
118 | } ta_howto; |
119 | union |
120 | { |
121 | uint32_t const_thread_area; /* Constant argument to ps_get_thread_area. */ |
122 | /* These are as if the descriptor of the field in prregset_t, |
123 | but DB_DESC_NELEM is overloaded as follows: */ |
124 | db_desc_t reg; /* Signed bias applied to register value. */ |
125 | db_desc_t reg_thread_area; /* Bits to scale down register value. */ |
126 | } ta_howto_data; |
127 | }; |
128 | |
129 | |
130 | /* List of all known descriptors. */ |
131 | extern list_t __td_agent_list attribute_hidden; |
132 | |
133 | |
134 | /* Function used to test for correct thread agent pointer. */ |
135 | static inline bool |
136 | ta_ok (const td_thragent_t *ta) |
137 | { |
138 | list_t *runp; |
139 | |
140 | list_for_each (runp, &__td_agent_list) |
141 | if (list_entry (runp, td_thragent_t, list) == ta) |
142 | return true; |
143 | |
144 | return false; |
145 | } |
146 | |
147 | |
148 | /* Internal wrappers around ps_pglobal_lookup. */ |
149 | extern ps_err_e td_mod_lookup (struct ps_prochandle *ps, const char *modname, |
150 | int idx, psaddr_t *sym_addr) attribute_hidden; |
151 | #define td_lookup(ps, idx, sym_addr) \ |
152 | td_mod_lookup ((ps), LIBPTHREAD_SO, (idx), (sym_addr)) |
153 | |
154 | |
155 | /* Store in psaddr_t VAR the address of inferior's symbol NAME. */ |
156 | #define DB_GET_SYMBOL(var, ta, name) \ |
157 | (((ta)->ta_addr_##name == 0 \ |
158 | && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \ |
159 | ? TD_ERR : ((var) = (ta)->ta_addr_##name, TD_OK)) |
160 | |
161 | /* Store in psaddr_t VAR the value of ((TYPE) PTR)->FIELD[IDX] in the inferior. |
162 | A target field smaller than psaddr_t is zero-extended. */ |
163 | #define DB_GET_FIELD(var, ta, ptr, type, field, idx) \ |
164 | _td_fetch_value ((ta), (ta)->ta_field_##type##_##field, \ |
165 | SYM_##type##_FIELD_##field, \ |
166 | (psaddr_t) 0 + (idx), (ptr), &(var)) |
167 | |
168 | /* With GCC 5.3 when compiling with -Os the compiler emits a warning |
169 | that slot may be used uninitialized. This is never the case since |
170 | the dynamic loader initializes the slotinfo list and |
171 | dtv_slotinfo_list will point slot at the first entry. Therefore |
172 | when DB_GET_FIELD_ADDRESS is called with a slot for ptr, the slot is |
173 | always initialized. */ |
174 | DIAG_PUSH_NEEDS_COMMENT; |
175 | DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized" ); |
176 | #define DB_GET_FIELD_ADDRESS(var, ta, ptr, type, field, idx) \ |
177 | ((var) = (ptr), _td_locate_field ((ta), (ta)->ta_field_##type##_##field, \ |
178 | SYM_##type##_FIELD_##field, \ |
179 | (psaddr_t) 0 + (idx), &(var))) |
180 | DIAG_POP_NEEDS_COMMENT; |
181 | |
182 | extern td_err_e _td_locate_field (td_thragent_t *ta, |
183 | db_desc_t desc, int descriptor_name, |
184 | psaddr_t idx, |
185 | psaddr_t *address) attribute_hidden; |
186 | |
187 | |
188 | /* Like DB_GET_FIELD, but PTR is a local pointer to a structure that |
189 | has already been copied in from the inferior. */ |
190 | #define DB_GET_FIELD_LOCAL(var, ta, ptr, type, field, idx) \ |
191 | _td_fetch_value_local ((ta), (ta)->ta_field_##type##_##field, \ |
192 | SYM_##type##_FIELD_##field, \ |
193 | (psaddr_t) 0 + (idx), (ptr), &(var)) |
194 | |
195 | /* Store in psaddr_t VAR the value of variable NAME[IDX] in the inferior. |
196 | A target value smaller than psaddr_t is zero-extended. */ |
197 | #define DB_GET_VALUE(var, ta, name, idx) \ |
198 | (((ta)->ta_addr_##name == 0 \ |
199 | && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \ |
200 | ? TD_ERR \ |
201 | : _td_fetch_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \ |
202 | (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, &(var))) |
203 | |
204 | /* Helper functions for those. */ |
205 | extern td_err_e _td_fetch_value (td_thragent_t *ta, |
206 | db_desc_t field, int descriptor_name, |
207 | psaddr_t idx, psaddr_t address, |
208 | psaddr_t *result) attribute_hidden; |
209 | extern td_err_e _td_fetch_value_local (td_thragent_t *ta, |
210 | db_desc_t field, |
211 | int descriptor_name, |
212 | psaddr_t idx, void *address, |
213 | psaddr_t *result) attribute_hidden; |
214 | |
215 | /* Store psaddr_t VALUE in ((TYPE) PTR)->FIELD[IDX] in the inferior. |
216 | A target field smaller than psaddr_t is zero-extended. */ |
217 | #define DB_PUT_FIELD(ta, ptr, type, field, idx, value) \ |
218 | _td_store_value ((ta), (ta)->ta_field_##type##_##field, \ |
219 | SYM_##type##_FIELD_##field, \ |
220 | (psaddr_t) 0 + (idx), (ptr), (value)) |
221 | |
222 | #define DB_PUT_FIELD_LOCAL(ta, ptr, type, field, idx, value) \ |
223 | _td_store_value_local ((ta), (ta)->ta_field_##type##_##field, \ |
224 | SYM_##type##_FIELD_##field, \ |
225 | (psaddr_t) 0 + (idx), (ptr), (value)) |
226 | |
227 | /* Store psaddr_t VALUE in variable NAME[IDX] in the inferior. |
228 | A target field smaller than psaddr_t is zero-extended. */ |
229 | #define DB_PUT_VALUE(ta, name, idx, value) \ |
230 | (((ta)->ta_addr_##name == 0 \ |
231 | && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \ |
232 | ? TD_ERR \ |
233 | : _td_store_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \ |
234 | (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, (value))) |
235 | |
236 | /* Helper functions for those. */ |
237 | extern td_err_e _td_store_value (td_thragent_t *ta, |
238 | db_desc_t field, int descriptor_name, |
239 | psaddr_t idx, psaddr_t address, |
240 | psaddr_t value) attribute_hidden; |
241 | extern td_err_e _td_store_value_local (td_thragent_t *ta, |
242 | db_desc_t field, int descriptor_name, |
243 | psaddr_t idx, void *address, |
244 | psaddr_t value) attribute_hidden; |
245 | |
246 | #define DB_GET_STRUCT(var, ta, ptr, type) \ |
247 | ({ td_err_e _err = TD_OK; \ |
248 | if ((ta)->ta_sizeof_##type == 0) \ |
249 | _err = _td_check_sizeof ((ta), &(ta)->ta_sizeof_##type, \ |
250 | SYM_SIZEOF_##type); \ |
251 | if (_err == TD_OK) \ |
252 | _err = ps_pdread ((ta)->ph, (ptr), \ |
253 | (var) = __alloca ((ta)->ta_sizeof_##type), \ |
254 | (ta)->ta_sizeof_##type) \ |
255 | == PS_OK ? TD_OK : TD_ERR; \ |
256 | else \ |
257 | (var) = NULL; \ |
258 | _err; \ |
259 | }) |
260 | #define DB_PUT_STRUCT(ta, ptr, type, copy) \ |
261 | ({ assert ((ta)->ta_sizeof_##type != 0); \ |
262 | ps_pdwrite ((ta)->ph, (ptr), (copy), (ta)->ta_sizeof_##type) \ |
263 | == PS_OK ? TD_OK : TD_ERR; \ |
264 | }) |
265 | |
266 | extern td_err_e _td_check_sizeof (td_thragent_t *ta, uint32_t *sizep, |
267 | int sizep_name) attribute_hidden; |
268 | |
269 | extern td_err_e __td_ta_lookup_th_unique (const td_thragent_t *ta, |
270 | lwpid_t lwpid, td_thrhandle_t *th); |
271 | |
272 | /* Try to initialize TA->ta_addr__rtld_global. Return true on |
273 | success, false on failure (which may be cached). */ |
274 | bool __td_ta_rtld_global (td_thragent_t *ta) attribute_hidden; |
275 | |
276 | /* Obtain the address of the list_t fields _dl_stack_user and |
277 | _dl_stack_used in _rtld_global, or fall back to the global |
278 | variables of the same name (to support statically linked |
279 | programs). */ |
280 | td_err_e __td_ta_stack_user (td_thragent_t *ta, psaddr_t *plist) |
281 | attribute_hidden; |
282 | td_err_e __td_ta_stack_used (td_thragent_t *ta, psaddr_t *plist) |
283 | attribute_hidden; |
284 | |
285 | #endif /* thread_dbP.h */ |
286 | |