Update.
[glibc.git] / nptl_db / thread_dbP.h
blob4546d24c23a65752b5fdfb1da287973f4b76aae4
1 /* Private header for thread debug library
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _THREAD_DBP_H
21 #define _THREAD_DBP_H 1
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <assert.h>
29 #include "proc_service.h"
30 #include "thread_db.h"
31 #include "../nptl/pthreadP.h" /* This is for *_BITMASK only. */
33 /* Indeces for the symbol names. */
34 enum
36 # define DB_STRUCT(type) SYM_SIZEOF_##type,
37 # define DB_STRUCT_FIELD(type, field) SYM_##type##_FIELD_##field,
38 # define DB_SYMBOL(name) SYM_##name,
39 # define DB_VARIABLE(name) SYM_##name, SYM_DESC_##name,
40 # include "structs.def"
41 # undef DB_STRUCT
42 # undef DB_STRUCT_FIELD
43 # undef DB_SYMBOL
44 # undef DB_VARIABLE
46 SYM_TH_UNIQUE_CONST_THREAD_AREA,
47 SYM_TH_UNIQUE_REGISTER64,
48 SYM_TH_UNIQUE_REGISTER32,
49 SYM_TH_UNIQUE_REGISTER64_THREAD_AREA,
50 SYM_TH_UNIQUE_REGISTER32_THREAD_AREA,
52 SYM_NUM_MESSAGES
56 /* Comment out the following for less verbose output. */
57 #ifndef NDEBUG
58 # define LOG(c) if (__td_debug) __libc_write (2, c "\n", strlen (c "\n"))
59 extern int __td_debug attribute_hidden;
60 #else
61 # define LOG(c)
62 #endif
65 #define DB_DESC_SIZE(desc) ((desc)[0])
66 #define DB_DESC_NELEM(desc) ((desc)[1])
67 #define DB_DESC_OFFSET(desc) ((int32_t) (desc)[2])
68 #define DB_SIZEOF_DESC (3 * sizeof (uint32_t))
69 #define DB_DEFINE_DESC(name, size, nelem, offset) \
70 const uint32_t name[3] = { (size), (nelem), (offset) }
71 typedef uint32_t db_desc_t[3];
74 /* Handle for a process. This type is opaque. */
75 struct td_thragent
77 /* Chain on the list of all agent structures. */
78 list_t list;
80 /* Delivered by the debugger and we have to pass it back in the
81 proc callbacks. */
82 struct ps_prochandle *ph;
84 /* Cached values read from the inferior. */
85 # define DB_STRUCT(type) \
86 uint32_t ta_sizeof_##type;
87 # define DB_STRUCT_FIELD(type, field) \
88 db_desc_t ta_field_##type##_##field;
89 # define DB_SYMBOL(name) \
90 psaddr_t ta_addr_##name;
91 # define DB_VARIABLE(name) \
92 psaddr_t ta_addr_##name; \
93 db_desc_t ta_var_##name;
94 # include "structs.def"
95 # undef DB_STRUCT
96 # undef DB_STRUCT_FIELD
97 # undef DB_SYMBOL
98 # undef DB_VARIABLE
100 /* The method of locating a thread's th_unique value. */
101 enum
103 ta_howto_unknown,
104 ta_howto_reg,
105 ta_howto_reg_thread_area,
106 ta_howto_const_thread_area
107 } ta_howto;
108 union
110 uint32_t const_thread_area; /* Constant argument to ps_get_thread_area. */
111 /* These are as if the descriptor of the field in prregset_t,
112 but DB_DESC_NELEM is overloaded as follows: */
113 db_desc_t reg; /* Signed bias applied to register value. */
114 db_desc_t reg_thread_area; /* Bits to scale down register value. */
115 } ta_howto_data;
119 /* List of all known descriptors. */
120 extern list_t __td_agent_list attribute_hidden;
123 /* Function used to test for correct thread agent pointer. */
124 static inline bool
125 ta_ok (const td_thragent_t *ta)
127 list_t *runp;
129 list_for_each (runp, &__td_agent_list)
130 if (list_entry (runp, td_thragent_t, list) == ta)
131 return true;
133 return false;
137 /* Internal wrapper around ps_pglobal_lookup. */
138 extern ps_err_e td_lookup (struct ps_prochandle *ps,
139 int idx, psaddr_t *sym_addr) attribute_hidden;
144 /* Store in psaddr_t VAR the address of inferior's symbol NAME. */
145 #define DB_GET_SYMBOL(var, ta, name) \
146 (((ta)->ta_addr_##name == 0 \
147 && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
148 ? TD_ERR : ((var) = (ta)->ta_addr_##name, TD_OK))
150 /* Store in psaddr_t VAR the value of ((TYPE) PTR)->FIELD[IDX] in the inferior.
151 A target field smaller than psaddr_t is zero-extended. */
152 #define DB_GET_FIELD(var, ta, ptr, type, field, idx) \
153 _td_fetch_value ((ta), (ta)->ta_field_##type##_##field, \
154 SYM_##type##_FIELD_##field, \
155 (psaddr_t) 0 + (idx), (ptr), &(var))
157 #define DB_GET_FIELD_ADDRESS(var, ta, ptr, type, field, idx) \
158 ((var) = (ptr), _td_locate_field ((ta), (ta)->ta_field_##type##_##field, \
159 SYM_##type##_FIELD_##field, \
160 (psaddr_t) 0 + (idx), &(var)))
162 extern td_err_e _td_locate_field (td_thragent_t *ta,
163 db_desc_t desc, int descriptor_name,
164 psaddr_t idx,
165 psaddr_t *address) attribute_hidden;
168 /* Like DB_GET_FIELD, but PTR is a local pointer to a structure that
169 has already been copied in from the inferior. */
170 #define DB_GET_FIELD_LOCAL(var, ta, ptr, type, field, idx) \
171 _td_fetch_value_local ((ta), (ta)->ta_field_##type##_##field, \
172 SYM_##type##_FIELD_##field, \
173 (psaddr_t) 0 + (idx), (ptr), &(var))
175 /* Store in psaddr_t VAR the value of variable NAME[IDX] in the inferior.
176 A target value smaller than psaddr_t is zero-extended. */
177 #define DB_GET_VALUE(var, ta, name, idx) \
178 (((ta)->ta_addr_##name == 0 \
179 && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
180 ? TD_ERR \
181 : _td_fetch_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \
182 (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, &(var)))
184 /* Helper functions for those. */
185 extern td_err_e _td_fetch_value (td_thragent_t *ta,
186 db_desc_t field, int descriptor_name,
187 psaddr_t idx, psaddr_t address,
188 psaddr_t *result) attribute_hidden;
189 extern td_err_e _td_fetch_value_local (td_thragent_t *ta,
190 db_desc_t field,
191 int descriptor_name,
192 psaddr_t idx, void *address,
193 psaddr_t *result) attribute_hidden;
195 /* Store psaddr_t VALUE in ((TYPE) PTR)->FIELD[IDX] in the inferior.
196 A target field smaller than psaddr_t is zero-extended. */
197 #define DB_PUT_FIELD(ta, ptr, type, field, idx, value) \
198 _td_store_value ((ta), (ta)->ta_field_##type##_##field, \
199 SYM_##type##_FIELD_##field, \
200 (psaddr_t) 0 + (idx), (ptr), (value))
202 #define DB_PUT_FIELD_LOCAL(ta, ptr, type, field, idx, value) \
203 _td_store_value_local ((ta), (ta)->ta_field_##type##_##field, \
204 SYM_##type##_FIELD_##field, \
205 (psaddr_t) 0 + (idx), (ptr), (value))
207 /* Store psaddr_t VALUE in variable NAME[IDX] in the inferior.
208 A target field smaller than psaddr_t is zero-extended. */
209 #define DB_PUT_VALUE(ta, name, idx, value) \
210 (((ta)->ta_addr_##name == 0 \
211 && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
212 ? TD_ERR \
213 : _td_store_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \
214 (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, (value)))
216 /* Helper functions for those. */
217 extern td_err_e _td_store_value (td_thragent_t *ta,
218 db_desc_t field, int descriptor_name,
219 psaddr_t idx, psaddr_t address,
220 psaddr_t value) attribute_hidden;
221 extern td_err_e _td_store_value_local (td_thragent_t *ta,
222 db_desc_t field, int descriptor_name,
223 psaddr_t idx, void *address,
224 psaddr_t value) attribute_hidden;
226 #define DB_GET_STRUCT(var, ta, ptr, type) \
227 ({ td_err_e _err = TD_OK; \
228 if ((ta)->ta_sizeof_##type == 0) \
229 _err = _td_check_sizeof ((ta), &(ta)->ta_sizeof_##type, \
230 SYM_SIZEOF_##type); \
231 if (_err == TD_OK) \
232 _err = ps_pdread ((ta)->ph, (ptr), \
233 (var) = __alloca ((ta)->ta_sizeof_##type), \
234 (ta)->ta_sizeof_##type) \
235 == PS_OK ? TD_OK : TD_ERR; \
236 else \
237 (var) = NULL; \
238 _err; \
240 #define DB_PUT_STRUCT(ta, ptr, type, copy) \
241 ({ assert ((ta)->ta_sizeof_##type != 0); \
242 ps_pdwrite ((ta)->ph, (ptr), (copy), (ta)->ta_sizeof_##type) \
243 == PS_OK ? TD_OK : TD_ERR; \
246 extern td_err_e _td_check_sizeof (td_thragent_t *ta, uint32_t *sizep,
247 int sizep_name) attribute_hidden;
249 #endif /* thread_dbP.h */