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