blob: c3d263b0c971dba73aa97de50fdbc48696d9c428 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#ifndef _THREAD_DBP_H
21#define _THREAD_DBP_H 1
22
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. */
32
33#ifdef __UCLIBC__
34#define __alloca alloca
35#endif
36
37/* Indeces for the symbol names. */
38enum
39 {
40# define DB_STRUCT(type) SYM_SIZEOF_##type,
41# define DB_STRUCT_FIELD(type, field) SYM_##type##_FIELD_##field,
42# define DB_SYMBOL(name) SYM_##name,
43# define DB_FUNCTION(name) SYM_##name,
44# define DB_VARIABLE(name) SYM_##name, SYM_DESC_##name,
45# include "structs.def"
46# undef DB_STRUCT
47# undef DB_STRUCT_FIELD
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"))
65extern 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) }
77typedef uint32_t db_desc_t[3];
78
79
80/* Handle for a process. This type is opaque. */
81struct 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_SYMBOL(name) \
96 psaddr_t ta_addr_##name;
97# define DB_FUNCTION(name) \
98 psaddr_t ta_addr_##name;
99# define DB_VARIABLE(name) \
100 psaddr_t ta_addr_##name; \
101 db_desc_t ta_var_##name;
102# include "structs.def"
103# undef DB_STRUCT
104# undef DB_STRUCT_FIELD
105# undef DB_FUNCTION
106# undef DB_SYMBOL
107# undef DB_VARIABLE
108
109 /* The method of locating a thread's th_unique value. */
110 enum
111 {
112 ta_howto_unknown,
113 ta_howto_reg,
114 ta_howto_reg_thread_area,
115 ta_howto_const_thread_area
116 } ta_howto;
117 union
118 {
119 uint32_t const_thread_area; /* Constant argument to ps_get_thread_area. */
120 /* These are as if the descriptor of the field in prregset_t,
121 but DB_DESC_NELEM is overloaded as follows: */
122 db_desc_t reg; /* Signed bias applied to register value. */
123 db_desc_t reg_thread_area; /* Bits to scale down register value. */
124 } ta_howto_data;
125};
126
127
128/* List of all known descriptors. */
129extern list_t __td_agent_list attribute_hidden;
130
131
132/* Function used to test for correct thread agent pointer. */
133static inline bool
134ta_ok (const td_thragent_t *ta)
135{
136 list_t *runp;
137
138 list_for_each (runp, &__td_agent_list)
139 if (list_entry (runp, td_thragent_t, list) == ta)
140 return true;
141
142 return false;
143}
144
145
146/* Internal wrapper around ps_pglobal_lookup. */
147extern ps_err_e td_lookup (struct ps_prochandle *ps,
148 int idx, psaddr_t *sym_addr) attribute_hidden;
149
150
151
152
153/* Store in psaddr_t VAR the address of inferior's symbol NAME. */
154#define DB_GET_SYMBOL(var, ta, name) \
155 (((ta)->ta_addr_##name == 0 \
156 && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
157 ? TD_ERR : ((var) = (ta)->ta_addr_##name, TD_OK))
158
159/* Store in psaddr_t VAR the value of ((TYPE) PTR)->FIELD[IDX] in the inferior.
160 A target field smaller than psaddr_t is zero-extended. */
161#define DB_GET_FIELD(var, ta, ptr, type, field, idx) \
162 _td_fetch_value ((ta), (ta)->ta_field_##type##_##field, \
163 SYM_##type##_FIELD_##field, \
164 (psaddr_t) 0 + (idx), (ptr), &(var))
165
166#define DB_GET_FIELD_ADDRESS(var, ta, ptr, type, field, idx) \
167 ((var) = (ptr), _td_locate_field ((ta), (ta)->ta_field_##type##_##field, \
168 SYM_##type##_FIELD_##field, \
169 (psaddr_t) 0 + (idx), &(var)))
170
171extern td_err_e _td_locate_field (td_thragent_t *ta,
172 db_desc_t desc, int descriptor_name,
173 psaddr_t idx,
174 psaddr_t *address) attribute_hidden;
175
176
177/* Like DB_GET_FIELD, but PTR is a local pointer to a structure that
178 has already been copied in from the inferior. */
179#define DB_GET_FIELD_LOCAL(var, ta, ptr, type, field, idx) \
180 _td_fetch_value_local ((ta), (ta)->ta_field_##type##_##field, \
181 SYM_##type##_FIELD_##field, \
182 (psaddr_t) 0 + (idx), (ptr), &(var))
183
184/* Store in psaddr_t VAR the value of variable NAME[IDX] in the inferior.
185 A target value smaller than psaddr_t is zero-extended. */
186#define DB_GET_VALUE(var, ta, name, idx) \
187 (((ta)->ta_addr_##name == 0 \
188 && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
189 ? TD_ERR \
190 : _td_fetch_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \
191 (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, &(var)))
192
193/* Helper functions for those. */
194extern td_err_e _td_fetch_value (td_thragent_t *ta,
195 db_desc_t field, int descriptor_name,
196 psaddr_t idx, psaddr_t address,
197 psaddr_t *result) attribute_hidden;
198extern td_err_e _td_fetch_value_local (td_thragent_t *ta,
199 db_desc_t field,
200 int descriptor_name,
201 psaddr_t idx, void *address,
202 psaddr_t *result) attribute_hidden;
203
204/* Store psaddr_t VALUE in ((TYPE) PTR)->FIELD[IDX] in the inferior.
205 A target field smaller than psaddr_t is zero-extended. */
206#define DB_PUT_FIELD(ta, ptr, type, field, idx, value) \
207 _td_store_value ((ta), (ta)->ta_field_##type##_##field, \
208 SYM_##type##_FIELD_##field, \
209 (psaddr_t) 0 + (idx), (ptr), (value))
210
211#define DB_PUT_FIELD_LOCAL(ta, ptr, type, field, idx, value) \
212 _td_store_value_local ((ta), (ta)->ta_field_##type##_##field, \
213 SYM_##type##_FIELD_##field, \
214 (psaddr_t) 0 + (idx), (ptr), (value))
215
216/* Store psaddr_t VALUE in variable NAME[IDX] in the inferior.
217 A target field smaller than psaddr_t is zero-extended. */
218#define DB_PUT_VALUE(ta, name, idx, value) \
219 (((ta)->ta_addr_##name == 0 \
220 && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK) \
221 ? TD_ERR \
222 : _td_store_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, \
223 (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, (value)))
224
225/* Helper functions for those. */
226extern td_err_e _td_store_value (td_thragent_t *ta,
227 db_desc_t field, int descriptor_name,
228 psaddr_t idx, psaddr_t address,
229 psaddr_t value) attribute_hidden;
230extern td_err_e _td_store_value_local (td_thragent_t *ta,
231 db_desc_t field, int descriptor_name,
232 psaddr_t idx, void *address,
233 psaddr_t value) attribute_hidden;
234
235#define DB_GET_STRUCT(var, ta, ptr, type) \
236 ({ td_err_e _err = TD_OK; \
237 if ((ta)->ta_sizeof_##type == 0) \
238 _err = _td_check_sizeof ((ta), &(ta)->ta_sizeof_##type, \
239 SYM_SIZEOF_##type); \
240 if (_err == TD_OK) \
241 _err = ps_pdread ((ta)->ph, (ptr), \
242 (var) = __alloca ((ta)->ta_sizeof_##type), \
243 (ta)->ta_sizeof_##type) \
244 == PS_OK ? TD_OK : TD_ERR; \
245 else \
246 (var) = NULL; \
247 _err; \
248 })
249#define DB_PUT_STRUCT(ta, ptr, type, copy) \
250 ({ assert ((ta)->ta_sizeof_##type != 0); \
251 ps_pdwrite ((ta)->ph, (ptr), (copy), (ta)->ta_sizeof_##type) \
252 == PS_OK ? TD_OK : TD_ERR; \
253 })
254
255extern td_err_e _td_check_sizeof (td_thragent_t *ta, uint32_t *sizep,
256 int sizep_name) attribute_hidden;
257
258#endif /* thread_dbP.h */