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