1 /* Helper routines for libthread_db.
2 Copyright (C) 2003-2013 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 <http://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 && idx
- (psaddr_t
) 0 > DB_DESC_NELEM (desc
))
73 /* This is an internal indicator to callers with nonzero IDX
74 that the IDX value is too big. */
77 elemsize
= DB_DESC_SIZE (desc
);
78 if (elemsize
& 0xff000000U
)
79 elemsize
= bswap_32 (elemsize
);
81 *address
+= (int32_t) DB_DESC_OFFSET (desc
);
82 *address
+= (elemsize
/ 8 * (idx
- (psaddr_t
) 0));
87 _td_fetch_value (td_thragent_t
*ta
,
88 db_desc_t desc
, int descriptor_name
,
89 psaddr_t idx
, psaddr_t address
,
93 td_err_e terr
= _td_locate_field (ta
, desc
, descriptor_name
, idx
, &address
);
97 if (DB_DESC_SIZE (desc
) == 8 || DB_DESC_SIZE (desc
) == bswap_32 (8))
100 err
= ps_pdread (ta
->ph
, address
, &value
, sizeof value
);
101 *result
= (psaddr_t
) 0 + value
;
103 else if (DB_DESC_SIZE (desc
) == 32)
106 err
= ps_pdread (ta
->ph
, address
, &value
, sizeof value
);
107 *result
= (psaddr_t
) 0 + value
;
109 else if (DB_DESC_SIZE (desc
) == 64)
112 if (sizeof (psaddr_t
) < 8)
114 err
= ps_pdread (ta
->ph
, address
, &value
, sizeof value
);
115 *result
= (psaddr_t
) 0 + value
;
117 else if (DB_DESC_SIZE (desc
) == bswap_32 (32))
120 err
= ps_pdread (ta
->ph
, address
, &value
, sizeof value
);
121 value
= bswap_32 (value
);
122 *result
= (psaddr_t
) 0 + value
;
124 else if (DB_DESC_SIZE (desc
) == bswap_32 (64))
127 if (sizeof (psaddr_t
) < 8)
129 err
= ps_pdread (ta
->ph
, address
, &value
, sizeof value
);
130 value
= bswap_64 (value
);
131 *result
= (psaddr_t
) 0 + value
;
136 return err
== PS_OK
? TD_OK
: TD_ERR
;
141 _td_store_value (td_thragent_t
*ta
,
142 uint32_t desc
[2], int descriptor_name
, psaddr_t idx
,
143 psaddr_t address
, psaddr_t widened_value
)
146 td_err_e terr
= _td_locate_field (ta
, desc
, descriptor_name
, idx
, &address
);
150 if (DB_DESC_SIZE (desc
) == 8 || DB_DESC_SIZE (desc
) == bswap_32 (8))
152 uint8_t value
= widened_value
- (psaddr_t
) 0;
153 err
= ps_pdwrite (ta
->ph
, address
, &value
, sizeof value
);
155 else if (DB_DESC_SIZE (desc
) == 32)
157 uint32_t value
= widened_value
- (psaddr_t
) 0;
158 err
= ps_pdwrite (ta
->ph
, address
, &value
, sizeof value
);
160 else if (DB_DESC_SIZE (desc
) == 64)
162 uint64_t value
= widened_value
- (psaddr_t
) 0;
163 if (sizeof (psaddr_t
) < 8)
165 err
= ps_pdwrite (ta
->ph
, address
, &value
, sizeof value
);
167 else if (DB_DESC_SIZE (desc
) == bswap_32 (32))
169 uint32_t value
= widened_value
- (psaddr_t
) 0;
170 value
= bswap_32 (value
);
171 err
= ps_pdwrite (ta
->ph
, address
, &value
, sizeof value
);
173 else if (DB_DESC_SIZE (desc
) == bswap_32 (64))
175 uint64_t value
= widened_value
- (psaddr_t
) 0;
176 if (sizeof (psaddr_t
) < 8)
178 value
= bswap_64 (value
);
179 err
= ps_pdwrite (ta
->ph
, address
, &value
, sizeof value
);
184 return err
== PS_OK
? TD_OK
: TD_ERR
;
188 _td_fetch_value_local (td_thragent_t
*ta
,
189 db_desc_t desc
, int descriptor_name
, psaddr_t idx
,
193 td_err_e terr
= _td_locate_field (ta
, desc
, descriptor_name
, idx
, &address
);
197 if (DB_DESC_SIZE (desc
) == 8 || DB_DESC_SIZE (desc
) == bswap_32 (8))
200 memcpy (&value
, address
, sizeof value
);
201 *result
= (psaddr_t
) 0 + value
;
203 else if (DB_DESC_SIZE (desc
) == 32)
206 memcpy (&value
, address
, sizeof value
);
207 *result
= (psaddr_t
) 0 + value
;
209 else if (DB_DESC_SIZE (desc
) == 64)
212 if (sizeof (psaddr_t
) < 8)
214 memcpy (&value
, address
, sizeof value
);
215 *result
= (psaddr_t
) 0 + value
;
217 else if (DB_DESC_SIZE (desc
) == bswap_32 (32))
220 memcpy (&value
, address
, sizeof value
);
221 value
= bswap_32 (value
);
222 *result
= (psaddr_t
) 0 + value
;
224 else if (DB_DESC_SIZE (desc
) == bswap_32 (64))
227 if (sizeof (psaddr_t
) < 8)
229 memcpy (&value
, address
, sizeof value
);
230 value
= bswap_64 (value
);
231 *result
= (psaddr_t
) 0 + value
;
241 _td_store_value_local (td_thragent_t
*ta
,
242 uint32_t desc
[2], int descriptor_name
, psaddr_t idx
,
243 void *address
, psaddr_t widened_value
)
245 td_err_e terr
= _td_locate_field (ta
, desc
, descriptor_name
, idx
, &address
);
249 if (DB_DESC_SIZE (desc
) == 8 || DB_DESC_SIZE (desc
) == bswap_32 (8))
251 uint8_t value
= widened_value
- (psaddr_t
) 0;
252 memcpy (address
, &value
, sizeof value
);
254 else if (DB_DESC_SIZE (desc
) == 32)
256 uint32_t value
= widened_value
- (psaddr_t
) 0;
257 memcpy (address
, &value
, sizeof value
);
259 else if (DB_DESC_SIZE (desc
) == 64)
261 uint64_t value
= widened_value
- (psaddr_t
) 0;
262 if (sizeof (psaddr_t
) < 8)
264 memcpy (address
, &value
, sizeof value
);
266 else if (DB_DESC_SIZE (desc
) == bswap_32 (32))
268 uint32_t value
= widened_value
- (psaddr_t
) 0;
269 value
= bswap_32 (value
);
270 memcpy (address
, &value
, sizeof value
);
272 else if (DB_DESC_SIZE (desc
) == bswap_32 (64))
274 uint64_t value
= widened_value
- (psaddr_t
) 0;
275 if (sizeof (psaddr_t
) < 8)
277 value
= bswap_64 (value
);
278 memcpy (address
, &value
, sizeof value
);