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