1 /* Helper routines for libthread_db.
2 Copyright (C) 2003-2023 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, see
17 <https://www.gnu.org/licenses/>. */
19 #include "thread_dbP.h"
25 _td_check_sizeof (td_thragent_t *ta, uint32_t *sizep, int sizep_name)
30 ps_err_e err = td_lookup (ta->ph, sizep_name, &descptr);
34 err = ps_pdread (ta->ph, descptr, sizep, sizeof *sizep);
37 if (*sizep & 0xff000000U)
38 *sizep = bswap_32 (*sizep);
44 _td_locate_field (td_thragent_t *ta,
45 db_desc_t desc, int descriptor_name,
46 psaddr_t idx, psaddr_t *address)
50 if (DB_DESC_SIZE (desc) == 0)
52 /* Read the information about this field from the inferior. */
54 ps_err_e err = td_lookup (ta->ph, descriptor_name, &descptr);
58 err = ps_pdread (ta->ph, descptr, desc, DB_SIZEOF_DESC);
61 if (DB_DESC_SIZE (desc) == 0)
63 if (DB_DESC_SIZE (desc) & 0xff000000U)
65 /* Byte-swap these words, though we leave the size word
66 in native order as the handy way to distinguish. */
67 DB_DESC_OFFSET (desc) = bswap_32 (DB_DESC_OFFSET (desc));
68 DB_DESC_NELEM (desc) = bswap_32 (DB_DESC_NELEM (desc));
72 if (idx != 0 && DB_DESC_NELEM (desc) != 0
73 && idx - (psaddr_t) 0 > DB_DESC_NELEM (desc))
74 /* This is an internal indicator to callers with nonzero IDX
75 that the IDX value is too big. */
78 elemsize = DB_DESC_SIZE (desc);
79 if (elemsize & 0xff000000U)
80 elemsize = bswap_32 (elemsize);
82 *address += (int32_t) DB_DESC_OFFSET (desc);
83 *address += (elemsize / 8 * (idx - (psaddr_t) 0));
88 _td_fetch_value (td_thragent_t *ta,
89 db_desc_t desc, int descriptor_name,
90 psaddr_t idx, psaddr_t address,
94 td_err_e terr = _td_locate_field (ta, desc, descriptor_name, idx, &address);
98 if (DB_DESC_SIZE (desc) == 8 || DB_DESC_SIZE (desc) == bswap_32 (8))
101 err = ps_pdread (ta->ph, address, &value, sizeof value);
102 *result = (psaddr_t) 0 + value;
104 else if (DB_DESC_SIZE (desc) == 32)
107 err = ps_pdread (ta->ph, address, &value, sizeof value);
108 *result = (psaddr_t) 0 + value;
110 else if (DB_DESC_SIZE (desc) == 64)
113 if (sizeof (psaddr_t) < 8)
115 err = ps_pdread (ta->ph, address, &value, sizeof value);
116 *result = (psaddr_t) 0 + value;
118 else if (DB_DESC_SIZE (desc) == bswap_32 (32))
121 err = ps_pdread (ta->ph, address, &value, sizeof value);
122 value = bswap_32 (value);
123 *result = (psaddr_t) 0 + value;
125 else if (DB_DESC_SIZE (desc) == bswap_32 (64))
128 if (sizeof (psaddr_t) < 8)
130 err = ps_pdread (ta->ph, address, &value, sizeof value);
131 value = bswap_64 (value);
132 *result = (psaddr_t) 0 + value;
137 return err == PS_OK ? TD_OK : TD_ERR;
142 _td_store_value (td_thragent_t *ta,
143 db_desc_t desc, int descriptor_name, psaddr_t idx,
144 psaddr_t address, psaddr_t widened_value)
147 td_err_e terr = _td_locate_field (ta, desc, descriptor_name, idx, &address);
151 if (DB_DESC_SIZE (desc) == 8 || DB_DESC_SIZE (desc) == bswap_32 (8))
153 uint8_t value = widened_value - (psaddr_t) 0;
154 err = ps_pdwrite (ta->ph, address, &value, sizeof value);
156 else if (DB_DESC_SIZE (desc) == 32)
158 uint32_t value = widened_value - (psaddr_t) 0;
159 err = ps_pdwrite (ta->ph, address, &value, sizeof value);
161 else if (DB_DESC_SIZE (desc) == 64)
163 uint64_t value = widened_value - (psaddr_t) 0;
164 if (sizeof (psaddr_t) < 8)
166 err = ps_pdwrite (ta->ph, address, &value, sizeof value);
168 else if (DB_DESC_SIZE (desc) == bswap_32 (32))
170 uint32_t value = widened_value - (psaddr_t) 0;
171 value = bswap_32 (value);
172 err = ps_pdwrite (ta->ph, address, &value, sizeof value);
174 else if (DB_DESC_SIZE (desc) == bswap_32 (64))
176 uint64_t value = widened_value - (psaddr_t) 0;
177 if (sizeof (psaddr_t) < 8)
179 value = bswap_64 (value);
180 err = ps_pdwrite (ta->ph, address, &value, sizeof value);
185 return err == PS_OK ? TD_OK : TD_ERR;
189 _td_fetch_value_local (td_thragent_t *ta,
190 db_desc_t desc, int descriptor_name, psaddr_t idx,
194 td_err_e terr = _td_locate_field (ta, desc, descriptor_name, idx, &address);
198 if (DB_DESC_SIZE (desc) == 8 || DB_DESC_SIZE (desc) == bswap_32 (8))
201 memcpy (&value, address, sizeof value);
202 *result = (psaddr_t) 0 + value;
204 else if (DB_DESC_SIZE (desc) == 32)
207 memcpy (&value, address, sizeof value);
208 *result = (psaddr_t) 0 + value;
210 else if (DB_DESC_SIZE (desc) == 64)
213 if (sizeof (psaddr_t) < 8)
215 memcpy (&value, address, sizeof value);
216 *result = (psaddr_t) 0 + value;
218 else if (DB_DESC_SIZE (desc) == bswap_32 (32))
221 memcpy (&value, address, sizeof value);
222 value = bswap_32 (value);
223 *result = (psaddr_t) 0 + value;
225 else if (DB_DESC_SIZE (desc) == bswap_32 (64))
228 if (sizeof (psaddr_t) < 8)
230 memcpy (&value, address, sizeof value);
231 value = bswap_64 (value);
232 *result = (psaddr_t) 0 + value;
242 _td_store_value_local (td_thragent_t *ta,
243 db_desc_t desc, int descriptor_name, psaddr_t idx,
244 void *address, psaddr_t widened_value)
246 td_err_e terr = _td_locate_field (ta, desc, descriptor_name, idx, &address);
250 if (DB_DESC_SIZE (desc) == 8 || DB_DESC_SIZE (desc) == bswap_32 (8))
252 uint8_t value = widened_value - (psaddr_t) 0;
253 memcpy (address, &value, sizeof value);
255 else if (DB_DESC_SIZE (desc) == 32)
257 uint32_t value = widened_value - (psaddr_t) 0;
258 memcpy (address, &value, sizeof value);
260 else if (DB_DESC_SIZE (desc) == 64)
262 uint64_t value = widened_value - (psaddr_t) 0;
263 if (sizeof (psaddr_t) < 8)
265 memcpy (address, &value, sizeof value);
267 else if (DB_DESC_SIZE (desc) == bswap_32 (32))
269 uint32_t value = widened_value - (psaddr_t) 0;
270 value = bswap_32 (value);
271 memcpy (address, &value, sizeof value);
273 else if (DB_DESC_SIZE (desc) == bswap_32 (64))
275 uint64_t value = widened_value - (psaddr_t) 0;
276 if (sizeof (psaddr_t) < 8)
278 value = bswap_64 (value);
279 memcpy (address, &value, sizeof value);