Commit | Line | Data |
---|---|---|
32178cab | 1 | /* Cache and manage the values of registers for GDB, the GNU debugger. |
3fadccb3 | 2 | |
61baf725 | 3 | Copyright (C) 1986-2017 Free Software Foundation, Inc. |
32178cab MS |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
32178cab MS |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
32178cab MS |
19 | |
20 | #include "defs.h" | |
32178cab MS |
21 | #include "inferior.h" |
22 | #include "target.h" | |
23 | #include "gdbarch.h" | |
705152c5 | 24 | #include "gdbcmd.h" |
4e052eda | 25 | #include "regcache.h" |
b59ff9d5 | 26 | #include "reggroups.h" |
f4c5303c | 27 | #include "observer.h" |
c21236dc | 28 | #include "remote.h" |
d3eaaf66 | 29 | #include "valprint.h" |
0b309272 | 30 | #include "regset.h" |
94bb8dfe | 31 | #include <forward_list> |
32178cab MS |
32 | |
33 | /* | |
34 | * DATA STRUCTURE | |
35 | * | |
36 | * Here is the actual register cache. | |
37 | */ | |
38 | ||
3fadccb3 | 39 | /* Per-architecture object describing the layout of a register cache. |
0df8b418 | 40 | Computed once when the architecture is created. */ |
3fadccb3 AC |
41 | |
42 | struct gdbarch_data *regcache_descr_handle; | |
43 | ||
44 | struct regcache_descr | |
45 | { | |
46 | /* The architecture this descriptor belongs to. */ | |
47 | struct gdbarch *gdbarch; | |
48 | ||
bb1db049 AC |
49 | /* The raw register cache. Each raw (or hard) register is supplied |
50 | by the target interface. The raw cache should not contain | |
51 | redundant information - if the PC is constructed from two | |
d2f0b918 | 52 | registers then those registers and not the PC lives in the raw |
bb1db049 | 53 | cache. */ |
3fadccb3 AC |
54 | int nr_raw_registers; |
55 | long sizeof_raw_registers; | |
ee99023e | 56 | long sizeof_raw_register_status; |
3fadccb3 | 57 | |
d138e37a AC |
58 | /* The cooked register space. Each cooked register in the range |
59 | [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw | |
60 | register. The remaining [NR_RAW_REGISTERS | |
02f60eae | 61 | .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto |
d138e37a | 62 | both raw registers and memory by the architecture methods |
02f60eae | 63 | gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */ |
d138e37a | 64 | int nr_cooked_registers; |
067df2e5 | 65 | long sizeof_cooked_registers; |
ee99023e | 66 | long sizeof_cooked_register_status; |
d138e37a | 67 | |
86d31898 | 68 | /* Offset and size (in 8 bit bytes), of each register in the |
d138e37a | 69 | register cache. All registers (including those in the range |
99e42fd8 PA |
70 | [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an |
71 | offset. */ | |
3fadccb3 | 72 | long *register_offset; |
3fadccb3 | 73 | long *sizeof_register; |
3fadccb3 | 74 | |
bb425013 AC |
75 | /* Cached table containing the type of each register. */ |
76 | struct type **register_type; | |
3fadccb3 AC |
77 | }; |
78 | ||
3fadccb3 AC |
79 | static void * |
80 | init_regcache_descr (struct gdbarch *gdbarch) | |
81 | { | |
82 | int i; | |
83 | struct regcache_descr *descr; | |
84 | gdb_assert (gdbarch != NULL); | |
85 | ||
bb425013 | 86 | /* Create an initial, zero filled, table. */ |
116f06ea | 87 | descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr); |
3fadccb3 | 88 | descr->gdbarch = gdbarch; |
3fadccb3 | 89 | |
d138e37a AC |
90 | /* Total size of the register space. The raw registers are mapped |
91 | directly onto the raw register cache while the pseudo's are | |
3fadccb3 | 92 | either mapped onto raw-registers or memory. */ |
214e098a UW |
93 | descr->nr_cooked_registers = gdbarch_num_regs (gdbarch) |
94 | + gdbarch_num_pseudo_regs (gdbarch); | |
ee99023e PA |
95 | descr->sizeof_cooked_register_status |
96 | = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); | |
3fadccb3 | 97 | |
bb425013 | 98 | /* Fill in a table of register types. */ |
116f06ea | 99 | descr->register_type |
3e43a32a MS |
100 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, |
101 | struct type *); | |
bb425013 | 102 | for (i = 0; i < descr->nr_cooked_registers; i++) |
336a3131 | 103 | descr->register_type[i] = gdbarch_register_type (gdbarch, i); |
bb425013 | 104 | |
bb1db049 AC |
105 | /* Construct a strictly RAW register cache. Don't allow pseudo's |
106 | into the register cache. */ | |
214e098a | 107 | descr->nr_raw_registers = gdbarch_num_regs (gdbarch); |
ee99023e | 108 | descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch); |
bb1db049 | 109 | |
067df2e5 | 110 | /* Lay out the register cache. |
3fadccb3 | 111 | |
bb425013 AC |
112 | NOTE: cagney/2002-05-22: Only register_type() is used when |
113 | constructing the register cache. It is assumed that the | |
114 | register's raw size, virtual size and type length are all the | |
115 | same. */ | |
3fadccb3 AC |
116 | |
117 | { | |
118 | long offset = 0; | |
123f5f96 | 119 | |
116f06ea AC |
120 | descr->sizeof_register |
121 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
122 | descr->register_offset | |
123 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
99e42fd8 PA |
124 | for (i = 0; i < descr->nr_raw_registers; i++) |
125 | { | |
126 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); | |
127 | descr->register_offset[i] = offset; | |
128 | offset += descr->sizeof_register[i]; | |
129 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); | |
130 | } | |
131 | /* Set the real size of the raw register cache buffer. */ | |
132 | descr->sizeof_raw_registers = offset; | |
133 | ||
134 | for (; i < descr->nr_cooked_registers; i++) | |
3fadccb3 | 135 | { |
bb425013 | 136 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); |
3fadccb3 AC |
137 | descr->register_offset[i] = offset; |
138 | offset += descr->sizeof_register[i]; | |
123a958e | 139 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); |
3fadccb3 | 140 | } |
99e42fd8 | 141 | /* Set the real size of the readonly register cache buffer. */ |
067df2e5 | 142 | descr->sizeof_cooked_registers = offset; |
3fadccb3 AC |
143 | } |
144 | ||
3fadccb3 AC |
145 | return descr; |
146 | } | |
147 | ||
148 | static struct regcache_descr * | |
149 | regcache_descr (struct gdbarch *gdbarch) | |
150 | { | |
19ba03f4 SM |
151 | return (struct regcache_descr *) gdbarch_data (gdbarch, |
152 | regcache_descr_handle); | |
3fadccb3 AC |
153 | } |
154 | ||
bb425013 AC |
155 | /* Utility functions returning useful register attributes stored in |
156 | the regcache descr. */ | |
157 | ||
158 | struct type * | |
159 | register_type (struct gdbarch *gdbarch, int regnum) | |
160 | { | |
161 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
123f5f96 | 162 | |
bb425013 AC |
163 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
164 | return descr->register_type[regnum]; | |
165 | } | |
166 | ||
0ed04cce AC |
167 | /* Utility functions returning useful register attributes stored in |
168 | the regcache descr. */ | |
169 | ||
08a617da AC |
170 | int |
171 | register_size (struct gdbarch *gdbarch, int regnum) | |
172 | { | |
173 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
174 | int size; | |
123f5f96 | 175 | |
f57d151a | 176 | gdb_assert (regnum >= 0 |
214e098a UW |
177 | && regnum < (gdbarch_num_regs (gdbarch) |
178 | + gdbarch_num_pseudo_regs (gdbarch))); | |
08a617da | 179 | size = descr->sizeof_register[regnum]; |
08a617da AC |
180 | return size; |
181 | } | |
182 | ||
8d689ee5 YQ |
183 | /* See common/common-regcache.h. */ |
184 | ||
185 | int | |
186 | regcache_register_size (const struct regcache *regcache, int n) | |
187 | { | |
188 | return register_size (get_regcache_arch (regcache), n); | |
189 | } | |
190 | ||
ef79d9a3 YQ |
191 | regcache::regcache (gdbarch *gdbarch, address_space *aspace_, |
192 | bool readonly_p_) | |
193 | : m_aspace (aspace_), m_readonly_p (readonly_p_) | |
3fadccb3 | 194 | { |
ef79d9a3 YQ |
195 | gdb_assert (gdbarch != NULL); |
196 | m_descr = regcache_descr (gdbarch); | |
4621115f | 197 | |
ef79d9a3 | 198 | if (m_readonly_p) |
4621115f | 199 | { |
ef79d9a3 YQ |
200 | m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers); |
201 | m_register_status = XCNEWVEC (signed char, | |
202 | m_descr->sizeof_cooked_register_status); | |
4621115f YQ |
203 | } |
204 | else | |
205 | { | |
ef79d9a3 YQ |
206 | m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers); |
207 | m_register_status = XCNEWVEC (signed char, | |
208 | m_descr->sizeof_raw_register_status); | |
4621115f | 209 | } |
ef79d9a3 YQ |
210 | m_ptid = minus_one_ptid; |
211 | } | |
4621115f | 212 | |
deb1fa3e YQ |
213 | static enum register_status |
214 | do_cooked_read (void *src, int regnum, gdb_byte *buf) | |
215 | { | |
216 | struct regcache *regcache = (struct regcache *) src; | |
217 | ||
218 | return regcache_cooked_read (regcache, regnum, buf); | |
219 | } | |
220 | ||
221 | regcache::regcache (readonly_t, const regcache &src) | |
222 | : regcache (src.arch (), src.aspace (), true) | |
223 | { | |
224 | gdb_assert (!src.m_readonly_p); | |
225 | save (do_cooked_read, (void *) &src); | |
226 | } | |
227 | ||
ef79d9a3 YQ |
228 | gdbarch * |
229 | regcache::arch () const | |
230 | { | |
231 | return m_descr->gdbarch; | |
232 | } | |
3fadccb3 | 233 | |
ddaaf0fb SM |
234 | /* See regcache.h. */ |
235 | ||
236 | ptid_t | |
237 | regcache_get_ptid (const struct regcache *regcache) | |
238 | { | |
ef79d9a3 | 239 | gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid)); |
ddaaf0fb | 240 | |
ef79d9a3 | 241 | return regcache->ptid (); |
ddaaf0fb SM |
242 | } |
243 | ||
99e42fd8 PA |
244 | struct regcache * |
245 | regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace) | |
246 | { | |
4621115f | 247 | return new regcache (gdbarch, aspace); |
99e42fd8 PA |
248 | } |
249 | ||
3fadccb3 AC |
250 | void |
251 | regcache_xfree (struct regcache *regcache) | |
252 | { | |
253 | if (regcache == NULL) | |
254 | return; | |
4621115f YQ |
255 | |
256 | delete regcache; | |
3fadccb3 AC |
257 | } |
258 | ||
b9362cc7 | 259 | static void |
36160dc4 AC |
260 | do_regcache_xfree (void *data) |
261 | { | |
19ba03f4 | 262 | regcache_xfree ((struct regcache *) data); |
36160dc4 AC |
263 | } |
264 | ||
265 | struct cleanup * | |
266 | make_cleanup_regcache_xfree (struct regcache *regcache) | |
267 | { | |
268 | return make_cleanup (do_regcache_xfree, regcache); | |
269 | } | |
270 | ||
b94ade42 PL |
271 | /* Cleanup routines for invalidating a register. */ |
272 | ||
273 | struct register_to_invalidate | |
274 | { | |
275 | struct regcache *regcache; | |
276 | int regnum; | |
277 | }; | |
278 | ||
279 | static void | |
280 | do_regcache_invalidate (void *data) | |
281 | { | |
19ba03f4 | 282 | struct register_to_invalidate *reg = (struct register_to_invalidate *) data; |
b94ade42 PL |
283 | |
284 | regcache_invalidate (reg->regcache, reg->regnum); | |
285 | } | |
286 | ||
287 | static struct cleanup * | |
288 | make_cleanup_regcache_invalidate (struct regcache *regcache, int regnum) | |
289 | { | |
290 | struct register_to_invalidate* reg = XNEW (struct register_to_invalidate); | |
291 | ||
292 | reg->regcache = regcache; | |
293 | reg->regnum = regnum; | |
294 | return make_cleanup_dtor (do_regcache_invalidate, (void *) reg, xfree); | |
295 | } | |
296 | ||
41d35cb0 MK |
297 | /* Return REGCACHE's architecture. */ |
298 | ||
299 | struct gdbarch * | |
300 | get_regcache_arch (const struct regcache *regcache) | |
301 | { | |
ef79d9a3 | 302 | return regcache->arch (); |
41d35cb0 MK |
303 | } |
304 | ||
6c95b8df PA |
305 | struct address_space * |
306 | get_regcache_aspace (const struct regcache *regcache) | |
307 | { | |
ef79d9a3 | 308 | return regcache->aspace (); |
6c95b8df PA |
309 | } |
310 | ||
51b1fe4e AC |
311 | /* Return a pointer to register REGNUM's buffer cache. */ |
312 | ||
ef79d9a3 YQ |
313 | gdb_byte * |
314 | regcache::register_buffer (int regnum) const | |
51b1fe4e | 315 | { |
ef79d9a3 | 316 | return m_registers + m_descr->register_offset[regnum]; |
51b1fe4e AC |
317 | } |
318 | ||
2d28509a | 319 | void |
ef79d9a3 YQ |
320 | regcache_save (struct regcache *regcache, |
321 | regcache_cooked_read_ftype *cooked_read, void *src) | |
2d28509a | 322 | { |
ef79d9a3 YQ |
323 | regcache->save (cooked_read, src); |
324 | } | |
325 | ||
326 | void | |
327 | regcache::save (regcache_cooked_read_ftype *cooked_read, | |
328 | void *src) | |
329 | { | |
330 | struct gdbarch *gdbarch = m_descr->gdbarch; | |
2d28509a | 331 | int regnum; |
123f5f96 | 332 | |
2d28509a | 333 | /* The DST should be `read-only', if it wasn't then the save would |
5602984a | 334 | end up trying to write the register values back out to the |
2d28509a | 335 | target. */ |
ef79d9a3 | 336 | gdb_assert (m_readonly_p); |
2d28509a | 337 | /* Clear the dest. */ |
ef79d9a3 YQ |
338 | memset (m_registers, 0, m_descr->sizeof_cooked_registers); |
339 | memset (m_register_status, 0, m_descr->sizeof_cooked_register_status); | |
2d28509a | 340 | /* Copy over any registers (identified by their membership in the |
f57d151a UW |
341 | save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs + |
342 | gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a | 343 | to save/restore `cooked' registers that live in memory. */ |
ef79d9a3 | 344 | for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) |
2d28509a AC |
345 | { |
346 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
347 | { | |
50d6adef AH |
348 | gdb_byte *dst_buf = register_buffer (regnum); |
349 | enum register_status status = cooked_read (src, regnum, dst_buf); | |
123f5f96 | 350 | |
50d6adef AH |
351 | gdb_assert (status != REG_UNKNOWN); |
352 | ||
353 | if (status != REG_VALID) | |
354 | memset (dst_buf, 0, register_size (gdbarch, regnum)); | |
05d1431c | 355 | |
ef79d9a3 | 356 | m_register_status[regnum] = status; |
2d28509a AC |
357 | } |
358 | } | |
359 | } | |
360 | ||
ef79d9a3 YQ |
361 | void |
362 | regcache::restore (struct regcache *src) | |
2d28509a | 363 | { |
ef79d9a3 | 364 | struct gdbarch *gdbarch = m_descr->gdbarch; |
2d28509a | 365 | int regnum; |
123f5f96 | 366 | |
5602984a AC |
367 | /* The dst had better not be read-only. If it is, the `restore' |
368 | doesn't make much sense. */ | |
ef79d9a3 YQ |
369 | gdb_assert (!m_readonly_p); |
370 | gdb_assert (src->m_readonly_p); | |
2d28509a | 371 | /* Copy over any registers, being careful to only restore those that |
f57d151a UW |
372 | were both saved and need to be restored. The full [0 .. gdbarch_num_regs |
373 | + gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a | 374 | to save/restore `cooked' registers that live in memory. */ |
ef79d9a3 | 375 | for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) |
2d28509a | 376 | { |
5602984a | 377 | if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)) |
2d28509a | 378 | { |
ef79d9a3 YQ |
379 | if (src->m_register_status[regnum] == REG_VALID) |
380 | cooked_write (regnum, src->register_buffer (regnum)); | |
2d28509a AC |
381 | } |
382 | } | |
383 | } | |
384 | ||
3fadccb3 AC |
385 | void |
386 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
387 | { | |
3fadccb3 | 388 | gdb_assert (src != NULL && dst != NULL); |
ef79d9a3 | 389 | gdb_assert (src->m_descr->gdbarch == dst->m_descr->gdbarch); |
3fadccb3 | 390 | gdb_assert (src != dst); |
ef79d9a3 | 391 | gdb_assert (src->m_readonly_p || dst->m_readonly_p); |
6c95b8df | 392 | |
ef79d9a3 | 393 | if (!src->m_readonly_p) |
5602984a | 394 | regcache_save (dst, do_cooked_read, src); |
ef79d9a3 YQ |
395 | else if (!dst->m_readonly_p) |
396 | dst->restore (src); | |
2d28509a | 397 | else |
ef79d9a3 | 398 | dst->cpy_no_passthrough (src); |
3fadccb3 AC |
399 | } |
400 | ||
bd49952b JK |
401 | /* Copy/duplicate the contents of a register cache. Unlike regcache_cpy, |
402 | which is pass-through, this does not go through to the target. | |
403 | Only values values already in the cache are transferred. The SRC and DST | |
404 | buffers must not overlap. */ | |
405 | ||
ef79d9a3 YQ |
406 | void |
407 | regcache::cpy_no_passthrough (struct regcache *src) | |
3fadccb3 | 408 | { |
ef79d9a3 YQ |
409 | gdb_assert (src != NULL); |
410 | gdb_assert (src->m_descr->gdbarch == m_descr->gdbarch); | |
3fadccb3 | 411 | /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough |
ee99023e PA |
412 | move of data into a thread's regcache. Doing this would be silly |
413 | - it would mean that regcache->register_status would be | |
414 | completely invalid. */ | |
ef79d9a3 | 415 | gdb_assert (m_readonly_p && src->m_readonly_p); |
6c95b8df | 416 | |
ef79d9a3 YQ |
417 | memcpy (m_registers, src->m_registers, |
418 | m_descr->sizeof_cooked_registers); | |
419 | memcpy (m_register_status, src->m_register_status, | |
420 | m_descr->sizeof_cooked_register_status); | |
3fadccb3 AC |
421 | } |
422 | ||
423 | struct regcache * | |
424 | regcache_dup (struct regcache *src) | |
425 | { | |
deb1fa3e | 426 | return new regcache (regcache::readonly, *src); |
3fadccb3 AC |
427 | } |
428 | ||
39181896 | 429 | enum register_status |
ee99023e | 430 | regcache_register_status (const struct regcache *regcache, int regnum) |
3fadccb3 AC |
431 | { |
432 | gdb_assert (regcache != NULL); | |
ef79d9a3 YQ |
433 | return regcache->get_register_status (regnum); |
434 | } | |
435 | ||
436 | enum register_status | |
437 | regcache::get_register_status (int regnum) const | |
438 | { | |
6ed7ea50 | 439 | gdb_assert (regnum >= 0); |
ef79d9a3 YQ |
440 | if (m_readonly_p) |
441 | gdb_assert (regnum < m_descr->nr_cooked_registers); | |
6ed7ea50 | 442 | else |
ef79d9a3 | 443 | gdb_assert (regnum < m_descr->nr_raw_registers); |
6ed7ea50 | 444 | |
ef79d9a3 | 445 | return (enum register_status) m_register_status[regnum]; |
3fadccb3 AC |
446 | } |
447 | ||
9c5ea4d9 UW |
448 | void |
449 | regcache_invalidate (struct regcache *regcache, int regnum) | |
450 | { | |
451 | gdb_assert (regcache != NULL); | |
ef79d9a3 | 452 | regcache->invalidate (regnum); |
9c5ea4d9 UW |
453 | } |
454 | ||
ef79d9a3 YQ |
455 | void |
456 | regcache::invalidate (int regnum) | |
457 | { | |
458 | gdb_assert (regnum >= 0); | |
459 | gdb_assert (!m_readonly_p); | |
460 | gdb_assert (regnum < m_descr->nr_raw_registers); | |
461 | m_register_status[regnum] = REG_UNKNOWN; | |
462 | } | |
9c5ea4d9 | 463 | |
3fadccb3 | 464 | /* Global structure containing the current regcache. */ |
3fadccb3 | 465 | |
5ebd2499 | 466 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
32178cab MS |
467 | recording if the register values have been changed (eg. by the |
468 | user). Therefore all registers must be written back to the | |
469 | target when appropriate. */ | |
e521e87e | 470 | std::forward_list<regcache *> regcache::current_regcache; |
c2250ad1 UW |
471 | |
472 | struct regcache * | |
e2d96639 YQ |
473 | get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch, |
474 | struct address_space *aspace) | |
c2250ad1 | 475 | { |
e521e87e | 476 | for (const auto ®cache : regcache::current_regcache) |
94bb8dfe YQ |
477 | if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch) |
478 | return regcache; | |
594f7785 | 479 | |
94bb8dfe | 480 | regcache *new_regcache = new regcache (gdbarch, aspace, false); |
594f7785 | 481 | |
e521e87e | 482 | regcache::current_regcache.push_front (new_regcache); |
ef79d9a3 | 483 | new_regcache->set_ptid (ptid); |
e2d96639 | 484 | |
e2d96639 YQ |
485 | return new_regcache; |
486 | } | |
487 | ||
488 | struct regcache * | |
489 | get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch) | |
490 | { | |
491 | struct address_space *aspace; | |
492 | ||
b78974c3 PA |
493 | /* For the benefit of "maint print registers" & co when debugging an |
494 | executable, allow dumping the regcache even when there is no | |
495 | thread selected (target_thread_address_space internal-errors if | |
496 | no address space is found). Note that normal user commands will | |
497 | fail higher up on the call stack due to no | |
498 | target_has_registers. */ | |
499 | aspace = (ptid_equal (null_ptid, ptid) | |
500 | ? NULL | |
501 | : target_thread_address_space (ptid)); | |
502 | ||
e2d96639 | 503 | return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace); |
594f7785 UW |
504 | } |
505 | ||
c2250ad1 UW |
506 | static ptid_t current_thread_ptid; |
507 | static struct gdbarch *current_thread_arch; | |
508 | ||
509 | struct regcache * | |
510 | get_thread_regcache (ptid_t ptid) | |
511 | { | |
512 | if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid)) | |
513 | { | |
514 | current_thread_ptid = ptid; | |
515 | current_thread_arch = target_thread_architecture (ptid); | |
516 | } | |
517 | ||
518 | return get_thread_arch_regcache (ptid, current_thread_arch); | |
519 | } | |
520 | ||
521 | struct regcache * | |
522 | get_current_regcache (void) | |
594f7785 UW |
523 | { |
524 | return get_thread_regcache (inferior_ptid); | |
525 | } | |
32178cab | 526 | |
361c8ade GB |
527 | /* See common/common-regcache.h. */ |
528 | ||
529 | struct regcache * | |
530 | get_thread_regcache_for_ptid (ptid_t ptid) | |
531 | { | |
532 | return get_thread_regcache (ptid); | |
533 | } | |
32178cab | 534 | |
f4c5303c OF |
535 | /* Observer for the target_changed event. */ |
536 | ||
2c0b251b | 537 | static void |
f4c5303c OF |
538 | regcache_observer_target_changed (struct target_ops *target) |
539 | { | |
540 | registers_changed (); | |
541 | } | |
542 | ||
5231c1fd PA |
543 | /* Update global variables old ptids to hold NEW_PTID if they were |
544 | holding OLD_PTID. */ | |
e521e87e YQ |
545 | void |
546 | regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) | |
5231c1fd | 547 | { |
e521e87e | 548 | for (auto ®cache : regcache::current_regcache) |
94bb8dfe YQ |
549 | { |
550 | if (ptid_equal (regcache->ptid (), old_ptid)) | |
551 | regcache->set_ptid (new_ptid); | |
552 | } | |
5231c1fd PA |
553 | } |
554 | ||
32178cab MS |
555 | /* Low level examining and depositing of registers. |
556 | ||
557 | The caller is responsible for making sure that the inferior is | |
558 | stopped before calling the fetching routines, or it will get | |
559 | garbage. (a change from GDB version 3, in which the caller got the | |
560 | value from the last stop). */ | |
561 | ||
562 | /* REGISTERS_CHANGED () | |
563 | ||
564 | Indicate that registers may have changed, so invalidate the cache. */ | |
565 | ||
566 | void | |
e66408ed | 567 | registers_changed_ptid (ptid_t ptid) |
32178cab | 568 | { |
e521e87e | 569 | for (auto oit = regcache::current_regcache.before_begin (), |
94bb8dfe | 570 | it = std::next (oit); |
e521e87e | 571 | it != regcache::current_regcache.end (); |
94bb8dfe | 572 | ) |
c2250ad1 | 573 | { |
94bb8dfe | 574 | if (ptid_match ((*it)->ptid (), ptid)) |
e66408ed | 575 | { |
94bb8dfe | 576 | delete *it; |
e521e87e | 577 | it = regcache::current_regcache.erase_after (oit); |
e66408ed | 578 | } |
94bb8dfe YQ |
579 | else |
580 | oit = it++; | |
c2250ad1 | 581 | } |
32178cab | 582 | |
c34fd852 | 583 | if (ptid_match (current_thread_ptid, ptid)) |
041274d8 PA |
584 | { |
585 | current_thread_ptid = null_ptid; | |
586 | current_thread_arch = NULL; | |
587 | } | |
32178cab | 588 | |
c34fd852 | 589 | if (ptid_match (inferior_ptid, ptid)) |
041274d8 PA |
590 | { |
591 | /* We just deleted the regcache of the current thread. Need to | |
592 | forget about any frames we have cached, too. */ | |
593 | reinit_frame_cache (); | |
594 | } | |
595 | } | |
c2250ad1 | 596 | |
041274d8 PA |
597 | void |
598 | registers_changed (void) | |
599 | { | |
600 | registers_changed_ptid (minus_one_ptid); | |
a5d9d57d | 601 | |
32178cab MS |
602 | /* Force cleanup of any alloca areas if using C alloca instead of |
603 | a builtin alloca. This particular call is used to clean up | |
604 | areas allocated by low level target code which may build up | |
605 | during lengthy interactions between gdb and the target before | |
606 | gdb gives control to the user (ie watchpoints). */ | |
607 | alloca (0); | |
32178cab MS |
608 | } |
609 | ||
8e368124 AH |
610 | void |
611 | regcache_raw_update (struct regcache *regcache, int regnum) | |
61a0eb5b | 612 | { |
8e368124 | 613 | gdb_assert (regcache != NULL); |
ef79d9a3 YQ |
614 | |
615 | regcache->raw_update (regnum); | |
616 | } | |
617 | ||
618 | void | |
619 | regcache::raw_update (int regnum) | |
620 | { | |
621 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); | |
8e368124 | 622 | |
3fadccb3 AC |
623 | /* Make certain that the register cache is up-to-date with respect |
624 | to the current thread. This switching shouldn't be necessary | |
625 | only there is still only one target side register cache. Sigh! | |
626 | On the bright side, at least there is a regcache object. */ | |
8e368124 | 627 | |
ef79d9a3 | 628 | if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN) |
3fadccb3 | 629 | { |
ef79d9a3 | 630 | target_fetch_registers (this, regnum); |
788c8b10 PA |
631 | |
632 | /* A number of targets can't access the whole set of raw | |
633 | registers (because the debug API provides no means to get at | |
634 | them). */ | |
ef79d9a3 YQ |
635 | if (m_register_status[regnum] == REG_UNKNOWN) |
636 | m_register_status[regnum] = REG_UNAVAILABLE; | |
3fadccb3 | 637 | } |
8e368124 AH |
638 | } |
639 | ||
640 | enum register_status | |
641 | regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf) | |
ef79d9a3 YQ |
642 | { |
643 | return regcache->raw_read (regnum, buf); | |
644 | } | |
645 | ||
646 | enum register_status | |
647 | regcache::raw_read (int regnum, gdb_byte *buf) | |
8e368124 AH |
648 | { |
649 | gdb_assert (buf != NULL); | |
ef79d9a3 | 650 | raw_update (regnum); |
05d1431c | 651 | |
ef79d9a3 YQ |
652 | if (m_register_status[regnum] != REG_VALID) |
653 | memset (buf, 0, m_descr->sizeof_register[regnum]); | |
05d1431c | 654 | else |
ef79d9a3 YQ |
655 | memcpy (buf, register_buffer (regnum), |
656 | m_descr->sizeof_register[regnum]); | |
05d1431c | 657 | |
ef79d9a3 | 658 | return (enum register_status) m_register_status[regnum]; |
61a0eb5b AC |
659 | } |
660 | ||
05d1431c | 661 | enum register_status |
28fc6740 | 662 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) |
ef79d9a3 YQ |
663 | { |
664 | gdb_assert (regcache != NULL); | |
6f98355c | 665 | return regcache->raw_read (regnum, val); |
ef79d9a3 YQ |
666 | } |
667 | ||
6f98355c | 668 | template<typename T, typename> |
ef79d9a3 | 669 | enum register_status |
6f98355c | 670 | regcache::raw_read (int regnum, T *val) |
28fc6740 | 671 | { |
2d522557 | 672 | gdb_byte *buf; |
05d1431c | 673 | enum register_status status; |
123f5f96 | 674 | |
ef79d9a3 YQ |
675 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); |
676 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); | |
677 | status = raw_read (regnum, buf); | |
05d1431c | 678 | if (status == REG_VALID) |
6f98355c YQ |
679 | *val = extract_integer<T> (buf, |
680 | m_descr->sizeof_register[regnum], | |
681 | gdbarch_byte_order (m_descr->gdbarch)); | |
05d1431c PA |
682 | else |
683 | *val = 0; | |
684 | return status; | |
28fc6740 AC |
685 | } |
686 | ||
05d1431c | 687 | enum register_status |
28fc6740 AC |
688 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, |
689 | ULONGEST *val) | |
ef79d9a3 YQ |
690 | { |
691 | gdb_assert (regcache != NULL); | |
6f98355c | 692 | return regcache->raw_read (regnum, val); |
28fc6740 AC |
693 | } |
694 | ||
c00dcbe9 MK |
695 | void |
696 | regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) | |
ef79d9a3 YQ |
697 | { |
698 | gdb_assert (regcache != NULL); | |
6f98355c | 699 | regcache->raw_write (regnum, val); |
ef79d9a3 YQ |
700 | } |
701 | ||
6f98355c | 702 | template<typename T, typename> |
ef79d9a3 | 703 | void |
6f98355c | 704 | regcache::raw_write (int regnum, T val) |
c00dcbe9 | 705 | { |
7c543f7b | 706 | gdb_byte *buf; |
123f5f96 | 707 | |
ef79d9a3 YQ |
708 | gdb_assert (regnum >=0 && regnum < m_descr->nr_raw_registers); |
709 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); | |
6f98355c YQ |
710 | store_integer (buf, m_descr->sizeof_register[regnum], |
711 | gdbarch_byte_order (m_descr->gdbarch), val); | |
ef79d9a3 | 712 | raw_write (regnum, buf); |
c00dcbe9 MK |
713 | } |
714 | ||
715 | void | |
716 | regcache_raw_write_unsigned (struct regcache *regcache, int regnum, | |
717 | ULONGEST val) | |
ef79d9a3 YQ |
718 | { |
719 | gdb_assert (regcache != NULL); | |
6f98355c | 720 | regcache->raw_write (regnum, val); |
c00dcbe9 MK |
721 | } |
722 | ||
9fd15b2e YQ |
723 | LONGEST |
724 | regcache_raw_get_signed (struct regcache *regcache, int regnum) | |
725 | { | |
726 | LONGEST value; | |
727 | enum register_status status; | |
728 | ||
729 | status = regcache_raw_read_signed (regcache, regnum, &value); | |
730 | if (status == REG_UNAVAILABLE) | |
731 | throw_error (NOT_AVAILABLE_ERROR, | |
732 | _("Register %d is not available"), regnum); | |
733 | return value; | |
734 | } | |
735 | ||
05d1431c | 736 | enum register_status |
2d522557 | 737 | regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
ef79d9a3 YQ |
738 | { |
739 | return regcache->cooked_read (regnum, buf); | |
740 | } | |
741 | ||
742 | enum register_status | |
743 | regcache::cooked_read (int regnum, gdb_byte *buf) | |
68365089 | 744 | { |
d138e37a | 745 | gdb_assert (regnum >= 0); |
ef79d9a3 YQ |
746 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
747 | if (regnum < m_descr->nr_raw_registers) | |
748 | return raw_read (regnum, buf); | |
749 | else if (m_readonly_p | |
750 | && m_register_status[regnum] != REG_UNKNOWN) | |
05d1431c PA |
751 | { |
752 | /* Read-only register cache, perhaps the cooked value was | |
753 | cached? */ | |
ef79d9a3 YQ |
754 | if (m_register_status[regnum] == REG_VALID) |
755 | memcpy (buf, register_buffer (regnum), | |
756 | m_descr->sizeof_register[regnum]); | |
05d1431c | 757 | else |
ef79d9a3 | 758 | memset (buf, 0, m_descr->sizeof_register[regnum]); |
05d1431c | 759 | |
ef79d9a3 | 760 | return (enum register_status) m_register_status[regnum]; |
05d1431c | 761 | } |
ef79d9a3 | 762 | else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) |
3543a589 TT |
763 | { |
764 | struct value *mark, *computed; | |
765 | enum register_status result = REG_VALID; | |
766 | ||
767 | mark = value_mark (); | |
768 | ||
ef79d9a3 YQ |
769 | computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch, |
770 | this, regnum); | |
3543a589 TT |
771 | if (value_entirely_available (computed)) |
772 | memcpy (buf, value_contents_raw (computed), | |
ef79d9a3 | 773 | m_descr->sizeof_register[regnum]); |
3543a589 TT |
774 | else |
775 | { | |
ef79d9a3 | 776 | memset (buf, 0, m_descr->sizeof_register[regnum]); |
3543a589 TT |
777 | result = REG_UNAVAILABLE; |
778 | } | |
779 | ||
780 | value_free_to_mark (mark); | |
781 | ||
782 | return result; | |
783 | } | |
d138e37a | 784 | else |
ef79d9a3 | 785 | return gdbarch_pseudo_register_read (m_descr->gdbarch, this, |
05d1431c | 786 | regnum, buf); |
61a0eb5b AC |
787 | } |
788 | ||
3543a589 TT |
789 | struct value * |
790 | regcache_cooked_read_value (struct regcache *regcache, int regnum) | |
ef79d9a3 YQ |
791 | { |
792 | return regcache->cooked_read_value (regnum); | |
793 | } | |
794 | ||
795 | struct value * | |
796 | regcache::cooked_read_value (int regnum) | |
3543a589 TT |
797 | { |
798 | gdb_assert (regnum >= 0); | |
ef79d9a3 | 799 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
3543a589 | 800 | |
ef79d9a3 YQ |
801 | if (regnum < m_descr->nr_raw_registers |
802 | || (m_readonly_p && m_register_status[regnum] != REG_UNKNOWN) | |
803 | || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) | |
3543a589 TT |
804 | { |
805 | struct value *result; | |
806 | ||
ef79d9a3 | 807 | result = allocate_value (register_type (m_descr->gdbarch, regnum)); |
3543a589 TT |
808 | VALUE_LVAL (result) = lval_register; |
809 | VALUE_REGNUM (result) = regnum; | |
810 | ||
811 | /* It is more efficient in general to do this delegation in this | |
812 | direction than in the other one, even though the value-based | |
813 | API is preferred. */ | |
ef79d9a3 YQ |
814 | if (cooked_read (regnum, |
815 | value_contents_raw (result)) == REG_UNAVAILABLE) | |
3543a589 TT |
816 | mark_value_bytes_unavailable (result, 0, |
817 | TYPE_LENGTH (value_type (result))); | |
818 | ||
819 | return result; | |
820 | } | |
821 | else | |
ef79d9a3 YQ |
822 | return gdbarch_pseudo_register_read_value (m_descr->gdbarch, |
823 | this, regnum); | |
3543a589 TT |
824 | } |
825 | ||
05d1431c | 826 | enum register_status |
a378f419 AC |
827 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, |
828 | LONGEST *val) | |
ef79d9a3 YQ |
829 | { |
830 | gdb_assert (regcache != NULL); | |
6f98355c | 831 | return regcache->cooked_read (regnum, val); |
ef79d9a3 YQ |
832 | } |
833 | ||
6f98355c | 834 | template<typename T, typename> |
ef79d9a3 | 835 | enum register_status |
6f98355c | 836 | regcache::cooked_read (int regnum, T *val) |
a378f419 | 837 | { |
05d1431c | 838 | enum register_status status; |
2d522557 | 839 | gdb_byte *buf; |
123f5f96 | 840 | |
ef79d9a3 YQ |
841 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); |
842 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); | |
843 | status = cooked_read (regnum, buf); | |
05d1431c | 844 | if (status == REG_VALID) |
6f98355c YQ |
845 | *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum], |
846 | gdbarch_byte_order (m_descr->gdbarch)); | |
05d1431c PA |
847 | else |
848 | *val = 0; | |
849 | return status; | |
a378f419 AC |
850 | } |
851 | ||
05d1431c | 852 | enum register_status |
a378f419 AC |
853 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, |
854 | ULONGEST *val) | |
ef79d9a3 YQ |
855 | { |
856 | gdb_assert (regcache != NULL); | |
6f98355c | 857 | return regcache->cooked_read (regnum, val); |
a378f419 AC |
858 | } |
859 | ||
a66a9c23 AC |
860 | void |
861 | regcache_cooked_write_signed (struct regcache *regcache, int regnum, | |
862 | LONGEST val) | |
ef79d9a3 YQ |
863 | { |
864 | gdb_assert (regcache != NULL); | |
6f98355c | 865 | regcache->cooked_write (regnum, val); |
ef79d9a3 YQ |
866 | } |
867 | ||
6f98355c | 868 | template<typename T, typename> |
ef79d9a3 | 869 | void |
6f98355c | 870 | regcache::cooked_write (int regnum, T val) |
a66a9c23 | 871 | { |
7c543f7b | 872 | gdb_byte *buf; |
123f5f96 | 873 | |
ef79d9a3 YQ |
874 | gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers); |
875 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); | |
6f98355c YQ |
876 | store_integer (buf, m_descr->sizeof_register[regnum], |
877 | gdbarch_byte_order (m_descr->gdbarch), val); | |
ef79d9a3 | 878 | cooked_write (regnum, buf); |
a66a9c23 AC |
879 | } |
880 | ||
881 | void | |
882 | regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, | |
883 | ULONGEST val) | |
ef79d9a3 YQ |
884 | { |
885 | gdb_assert (regcache != NULL); | |
6f98355c | 886 | regcache->cooked_write (regnum, val); |
a66a9c23 AC |
887 | } |
888 | ||
20aa2c60 PA |
889 | /* See regcache.h. */ |
890 | ||
891 | void | |
892 | regcache_raw_set_cached_value (struct regcache *regcache, int regnum, | |
893 | const gdb_byte *buf) | |
894 | { | |
ef79d9a3 YQ |
895 | regcache->raw_set_cached_value (regnum, buf); |
896 | } | |
897 | ||
898 | void | |
899 | regcache::raw_set_cached_value (int regnum, const gdb_byte *buf) | |
900 | { | |
901 | memcpy (register_buffer (regnum), buf, | |
902 | m_descr->sizeof_register[regnum]); | |
903 | m_register_status[regnum] = REG_VALID; | |
20aa2c60 PA |
904 | } |
905 | ||
61a0eb5b | 906 | void |
2d522557 AC |
907 | regcache_raw_write (struct regcache *regcache, int regnum, |
908 | const gdb_byte *buf) | |
ef79d9a3 YQ |
909 | { |
910 | gdb_assert (regcache != NULL && buf != NULL); | |
911 | regcache->raw_write (regnum, buf); | |
912 | } | |
913 | ||
914 | void | |
915 | regcache::raw_write (int regnum, const gdb_byte *buf) | |
61a0eb5b | 916 | { |
3e00d44f | 917 | struct cleanup *old_chain; |
594f7785 | 918 | |
ef79d9a3 YQ |
919 | gdb_assert (buf != NULL); |
920 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); | |
921 | gdb_assert (!m_readonly_p); | |
3fadccb3 | 922 | |
3fadccb3 AC |
923 | /* On the sparc, writing %g0 is a no-op, so we don't even want to |
924 | change the registers array if something writes to this register. */ | |
ef79d9a3 | 925 | if (gdbarch_cannot_store_register (arch (), regnum)) |
3fadccb3 AC |
926 | return; |
927 | ||
3fadccb3 | 928 | /* If we have a valid copy of the register, and new value == old |
0df8b418 | 929 | value, then don't bother doing the actual store. */ |
ef79d9a3 YQ |
930 | if (get_register_status (regnum) == REG_VALID |
931 | && (memcmp (register_buffer (regnum), buf, | |
932 | m_descr->sizeof_register[regnum]) == 0)) | |
3fadccb3 AC |
933 | return; |
934 | ||
ef79d9a3 YQ |
935 | target_prepare_to_store (this); |
936 | raw_set_cached_value (regnum, buf); | |
b94ade42 PL |
937 | |
938 | /* Register a cleanup function for invalidating the register after it is | |
939 | written, in case of a failure. */ | |
ef79d9a3 | 940 | old_chain = make_cleanup_regcache_invalidate (this, regnum); |
b94ade42 | 941 | |
ef79d9a3 | 942 | target_store_registers (this, regnum); |
594f7785 | 943 | |
b94ade42 PL |
944 | /* The target did not throw an error so we can discard invalidating the |
945 | register and restore the cleanup chain to what it was. */ | |
3e00d44f | 946 | discard_cleanups (old_chain); |
61a0eb5b AC |
947 | } |
948 | ||
68365089 | 949 | void |
2d522557 AC |
950 | regcache_cooked_write (struct regcache *regcache, int regnum, |
951 | const gdb_byte *buf) | |
ef79d9a3 YQ |
952 | { |
953 | regcache->cooked_write (regnum, buf); | |
954 | } | |
955 | ||
956 | void | |
957 | regcache::cooked_write (int regnum, const gdb_byte *buf) | |
68365089 | 958 | { |
d138e37a | 959 | gdb_assert (regnum >= 0); |
ef79d9a3 YQ |
960 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
961 | if (regnum < m_descr->nr_raw_registers) | |
962 | raw_write (regnum, buf); | |
d138e37a | 963 | else |
ef79d9a3 | 964 | gdbarch_pseudo_register_write (m_descr->gdbarch, this, |
d8124050 | 965 | regnum, buf); |
61a0eb5b AC |
966 | } |
967 | ||
06c0b04e AC |
968 | /* Perform a partial register transfer using a read, modify, write |
969 | operation. */ | |
970 | ||
971 | typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum, | |
972 | void *buf); | |
973 | typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum, | |
974 | const void *buf); | |
975 | ||
ef79d9a3 YQ |
976 | enum register_status |
977 | regcache::xfer_part (int regnum, int offset, int len, void *in, | |
978 | const void *out, | |
979 | enum register_status (*read) (struct regcache *regcache, | |
980 | int regnum, | |
981 | gdb_byte *buf), | |
982 | void (*write) (struct regcache *regcache, int regnum, | |
983 | const gdb_byte *buf)) | |
984 | { | |
985 | struct gdbarch *gdbarch = arch (); | |
9890e433 | 986 | gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum)); |
123f5f96 | 987 | |
ef79d9a3 YQ |
988 | gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]); |
989 | gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]); | |
06c0b04e AC |
990 | /* Something to do? */ |
991 | if (offset + len == 0) | |
05d1431c | 992 | return REG_VALID; |
0df8b418 | 993 | /* Read (when needed) ... */ |
06c0b04e AC |
994 | if (in != NULL |
995 | || offset > 0 | |
ef79d9a3 | 996 | || offset + len < m_descr->sizeof_register[regnum]) |
06c0b04e | 997 | { |
05d1431c PA |
998 | enum register_status status; |
999 | ||
06c0b04e | 1000 | gdb_assert (read != NULL); |
ef79d9a3 | 1001 | status = read (this, regnum, reg); |
05d1431c PA |
1002 | if (status != REG_VALID) |
1003 | return status; | |
06c0b04e | 1004 | } |
0df8b418 | 1005 | /* ... modify ... */ |
06c0b04e AC |
1006 | if (in != NULL) |
1007 | memcpy (in, reg + offset, len); | |
1008 | if (out != NULL) | |
1009 | memcpy (reg + offset, out, len); | |
1010 | /* ... write (when needed). */ | |
1011 | if (out != NULL) | |
1012 | { | |
1013 | gdb_assert (write != NULL); | |
ef79d9a3 | 1014 | write (this, regnum, reg); |
06c0b04e | 1015 | } |
05d1431c PA |
1016 | |
1017 | return REG_VALID; | |
06c0b04e AC |
1018 | } |
1019 | ||
05d1431c | 1020 | enum register_status |
06c0b04e | 1021 | regcache_raw_read_part (struct regcache *regcache, int regnum, |
2d522557 | 1022 | int offset, int len, gdb_byte *buf) |
06c0b04e | 1023 | { |
ef79d9a3 YQ |
1024 | return regcache->raw_read_part (regnum, offset, len, buf); |
1025 | } | |
123f5f96 | 1026 | |
ef79d9a3 YQ |
1027 | enum register_status |
1028 | regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf) | |
1029 | { | |
1030 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); | |
1031 | return xfer_part (regnum, offset, len, buf, NULL, | |
1032 | regcache_raw_read, regcache_raw_write); | |
06c0b04e AC |
1033 | } |
1034 | ||
1035 | void | |
1036 | regcache_raw_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 1037 | int offset, int len, const gdb_byte *buf) |
06c0b04e | 1038 | { |
ef79d9a3 YQ |
1039 | regcache->raw_write_part (regnum, offset, len, buf); |
1040 | } | |
123f5f96 | 1041 | |
ef79d9a3 YQ |
1042 | void |
1043 | regcache::raw_write_part (int regnum, int offset, int len, | |
1044 | const gdb_byte *buf) | |
1045 | { | |
1046 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); | |
1047 | xfer_part (regnum, offset, len, NULL, buf, regcache_raw_read, | |
1048 | regcache_raw_write); | |
06c0b04e AC |
1049 | } |
1050 | ||
05d1431c | 1051 | enum register_status |
06c0b04e | 1052 | regcache_cooked_read_part (struct regcache *regcache, int regnum, |
2d522557 | 1053 | int offset, int len, gdb_byte *buf) |
06c0b04e | 1054 | { |
ef79d9a3 YQ |
1055 | return regcache->cooked_read_part (regnum, offset, len, buf); |
1056 | } | |
123f5f96 | 1057 | |
ef79d9a3 YQ |
1058 | |
1059 | enum register_status | |
1060 | regcache::cooked_read_part (int regnum, int offset, int len, gdb_byte *buf) | |
1061 | { | |
1062 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); | |
1063 | return xfer_part (regnum, offset, len, buf, NULL, | |
1064 | regcache_cooked_read, regcache_cooked_write); | |
06c0b04e AC |
1065 | } |
1066 | ||
1067 | void | |
1068 | regcache_cooked_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 1069 | int offset, int len, const gdb_byte *buf) |
06c0b04e | 1070 | { |
ef79d9a3 YQ |
1071 | regcache->cooked_write_part (regnum, offset, len, buf); |
1072 | } | |
123f5f96 | 1073 | |
ef79d9a3 YQ |
1074 | void |
1075 | regcache::cooked_write_part (int regnum, int offset, int len, | |
1076 | const gdb_byte *buf) | |
1077 | { | |
1078 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); | |
1079 | xfer_part (regnum, offset, len, NULL, buf, | |
1080 | regcache_cooked_read, regcache_cooked_write); | |
06c0b04e | 1081 | } |
32178cab | 1082 | |
a16d75cc | 1083 | /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */ |
9a661b68 MK |
1084 | |
1085 | void | |
6618125d | 1086 | regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf) |
ef79d9a3 YQ |
1087 | { |
1088 | gdb_assert (regcache != NULL); | |
1089 | regcache->raw_supply (regnum, buf); | |
1090 | } | |
1091 | ||
1092 | void | |
1093 | regcache::raw_supply (int regnum, const void *buf) | |
9a661b68 MK |
1094 | { |
1095 | void *regbuf; | |
1096 | size_t size; | |
1097 | ||
ef79d9a3 YQ |
1098 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); |
1099 | gdb_assert (!m_readonly_p); | |
9a661b68 | 1100 | |
ef79d9a3 YQ |
1101 | regbuf = register_buffer (regnum); |
1102 | size = m_descr->sizeof_register[regnum]; | |
9a661b68 MK |
1103 | |
1104 | if (buf) | |
ee99023e PA |
1105 | { |
1106 | memcpy (regbuf, buf, size); | |
ef79d9a3 | 1107 | m_register_status[regnum] = REG_VALID; |
ee99023e | 1108 | } |
9a661b68 | 1109 | else |
ee99023e PA |
1110 | { |
1111 | /* This memset not strictly necessary, but better than garbage | |
1112 | in case the register value manages to escape somewhere (due | |
1113 | to a bug, no less). */ | |
1114 | memset (regbuf, 0, size); | |
ef79d9a3 | 1115 | m_register_status[regnum] = REG_UNAVAILABLE; |
ee99023e | 1116 | } |
9a661b68 MK |
1117 | } |
1118 | ||
b057297a AH |
1119 | /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at |
1120 | address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If | |
1121 | the register size is greater than ADDR_LEN, then the integer will be sign or | |
1122 | zero extended. If the register size is smaller than the integer, then the | |
1123 | most significant bytes of the integer will be truncated. */ | |
1124 | ||
1125 | void | |
1126 | regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len, | |
1127 | bool is_signed) | |
1128 | { | |
1129 | enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); | |
1130 | gdb_byte *regbuf; | |
1131 | size_t regsize; | |
1132 | ||
1133 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); | |
1134 | gdb_assert (!m_readonly_p); | |
1135 | ||
1136 | regbuf = register_buffer (regnum); | |
1137 | regsize = m_descr->sizeof_register[regnum]; | |
1138 | ||
1139 | copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed, | |
1140 | byte_order); | |
1141 | m_register_status[regnum] = REG_VALID; | |
1142 | } | |
1143 | ||
f81fdd35 AH |
1144 | /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same |
1145 | as calling raw_supply with NULL (which will set the state to | |
1146 | unavailable). */ | |
1147 | ||
1148 | void | |
1149 | regcache::raw_supply_zeroed (int regnum) | |
1150 | { | |
1151 | void *regbuf; | |
1152 | size_t size; | |
1153 | ||
1154 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); | |
1155 | gdb_assert (!m_readonly_p); | |
1156 | ||
1157 | regbuf = register_buffer (regnum); | |
1158 | size = m_descr->sizeof_register[regnum]; | |
1159 | ||
1160 | memset (regbuf, 0, size); | |
1161 | m_register_status[regnum] = REG_VALID; | |
1162 | } | |
1163 | ||
9a661b68 MK |
1164 | /* Collect register REGNUM from REGCACHE and store its contents in BUF. */ |
1165 | ||
1166 | void | |
6618125d | 1167 | regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf) |
ef79d9a3 YQ |
1168 | { |
1169 | gdb_assert (regcache != NULL && buf != NULL); | |
1170 | regcache->raw_collect (regnum, buf); | |
1171 | } | |
1172 | ||
1173 | void | |
1174 | regcache::raw_collect (int regnum, void *buf) const | |
9a661b68 MK |
1175 | { |
1176 | const void *regbuf; | |
1177 | size_t size; | |
1178 | ||
ef79d9a3 YQ |
1179 | gdb_assert (buf != NULL); |
1180 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); | |
9a661b68 | 1181 | |
ef79d9a3 YQ |
1182 | regbuf = register_buffer (regnum); |
1183 | size = m_descr->sizeof_register[regnum]; | |
9a661b68 MK |
1184 | memcpy (buf, regbuf, size); |
1185 | } | |
1186 | ||
0b309272 AA |
1187 | /* Transfer a single or all registers belonging to a certain register |
1188 | set to or from a buffer. This is the main worker function for | |
1189 | regcache_supply_regset and regcache_collect_regset. */ | |
1190 | ||
b057297a AH |
1191 | /* Collect register REGNUM from REGCACHE. Store collected value as an integer |
1192 | at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. | |
1193 | If ADDR_LEN is greater than the register size, then the integer will be sign | |
1194 | or zero extended. If ADDR_LEN is smaller than the register size, then the | |
1195 | most significant bytes of the integer will be truncated. */ | |
1196 | ||
1197 | void | |
1198 | regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len, | |
1199 | bool is_signed) const | |
1200 | { | |
1201 | enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); | |
1202 | const gdb_byte *regbuf; | |
1203 | size_t regsize; | |
1204 | ||
1205 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers); | |
1206 | ||
1207 | regbuf = register_buffer (regnum); | |
1208 | regsize = m_descr->sizeof_register[regnum]; | |
1209 | ||
1210 | copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed, | |
1211 | byte_order); | |
1212 | } | |
1213 | ||
ef79d9a3 YQ |
1214 | void |
1215 | regcache::transfer_regset (const struct regset *regset, | |
1216 | struct regcache *out_regcache, | |
1217 | int regnum, const void *in_buf, | |
1218 | void *out_buf, size_t size) const | |
0b309272 AA |
1219 | { |
1220 | const struct regcache_map_entry *map; | |
1221 | int offs = 0, count; | |
1222 | ||
19ba03f4 SM |
1223 | for (map = (const struct regcache_map_entry *) regset->regmap; |
1224 | (count = map->count) != 0; | |
1225 | map++) | |
0b309272 AA |
1226 | { |
1227 | int regno = map->regno; | |
1228 | int slot_size = map->size; | |
1229 | ||
1230 | if (slot_size == 0 && regno != REGCACHE_MAP_SKIP) | |
ef79d9a3 | 1231 | slot_size = m_descr->sizeof_register[regno]; |
0b309272 AA |
1232 | |
1233 | if (regno == REGCACHE_MAP_SKIP | |
1234 | || (regnum != -1 | |
1235 | && (regnum < regno || regnum >= regno + count))) | |
1236 | offs += count * slot_size; | |
1237 | ||
1238 | else if (regnum == -1) | |
1239 | for (; count--; regno++, offs += slot_size) | |
1240 | { | |
1241 | if (offs + slot_size > size) | |
1242 | break; | |
1243 | ||
1244 | if (out_buf) | |
ef79d9a3 | 1245 | raw_collect (regno, (gdb_byte *) out_buf + offs); |
0b309272 | 1246 | else |
ef79d9a3 YQ |
1247 | out_regcache->raw_supply (regno, in_buf |
1248 | ? (const gdb_byte *) in_buf + offs | |
1249 | : NULL); | |
0b309272 AA |
1250 | } |
1251 | else | |
1252 | { | |
1253 | /* Transfer a single register and return. */ | |
1254 | offs += (regnum - regno) * slot_size; | |
1255 | if (offs + slot_size > size) | |
1256 | return; | |
1257 | ||
1258 | if (out_buf) | |
ef79d9a3 | 1259 | raw_collect (regnum, (gdb_byte *) out_buf + offs); |
0b309272 | 1260 | else |
ef79d9a3 YQ |
1261 | out_regcache->raw_supply (regnum, in_buf |
1262 | ? (const gdb_byte *) in_buf + offs | |
1263 | : NULL); | |
0b309272 AA |
1264 | return; |
1265 | } | |
1266 | } | |
1267 | } | |
1268 | ||
1269 | /* Supply register REGNUM from BUF to REGCACHE, using the register map | |
1270 | in REGSET. If REGNUM is -1, do this for all registers in REGSET. | |
1271 | If BUF is NULL, set the register(s) to "unavailable" status. */ | |
1272 | ||
1273 | void | |
1274 | regcache_supply_regset (const struct regset *regset, | |
1275 | struct regcache *regcache, | |
1276 | int regnum, const void *buf, size_t size) | |
1277 | { | |
ef79d9a3 YQ |
1278 | regcache->supply_regset (regset, regnum, buf, size); |
1279 | } | |
1280 | ||
1281 | void | |
1282 | regcache::supply_regset (const struct regset *regset, | |
1283 | int regnum, const void *buf, size_t size) | |
1284 | { | |
1285 | transfer_regset (regset, this, regnum, buf, NULL, size); | |
0b309272 AA |
1286 | } |
1287 | ||
1288 | /* Collect register REGNUM from REGCACHE to BUF, using the register | |
1289 | map in REGSET. If REGNUM is -1, do this for all registers in | |
1290 | REGSET. */ | |
1291 | ||
1292 | void | |
1293 | regcache_collect_regset (const struct regset *regset, | |
1294 | const struct regcache *regcache, | |
1295 | int regnum, void *buf, size_t size) | |
1296 | { | |
ef79d9a3 YQ |
1297 | regcache->collect_regset (regset, regnum, buf, size); |
1298 | } | |
1299 | ||
1300 | void | |
1301 | regcache::collect_regset (const struct regset *regset, | |
1302 | int regnum, void *buf, size_t size) const | |
1303 | { | |
1304 | transfer_regset (regset, NULL, regnum, NULL, buf, size); | |
0b309272 AA |
1305 | } |
1306 | ||
193cb69f | 1307 | |
515630c5 | 1308 | /* Special handling for register PC. */ |
32178cab MS |
1309 | |
1310 | CORE_ADDR | |
515630c5 | 1311 | regcache_read_pc (struct regcache *regcache) |
32178cab | 1312 | { |
61a1198a UW |
1313 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
1314 | ||
32178cab MS |
1315 | CORE_ADDR pc_val; |
1316 | ||
61a1198a UW |
1317 | if (gdbarch_read_pc_p (gdbarch)) |
1318 | pc_val = gdbarch_read_pc (gdbarch, regcache); | |
cde9ea48 | 1319 | /* Else use per-frame method on get_current_frame. */ |
214e098a | 1320 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
cde9ea48 | 1321 | { |
61a1198a | 1322 | ULONGEST raw_val; |
123f5f96 | 1323 | |
05d1431c PA |
1324 | if (regcache_cooked_read_unsigned (regcache, |
1325 | gdbarch_pc_regnum (gdbarch), | |
1326 | &raw_val) == REG_UNAVAILABLE) | |
1327 | throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available")); | |
1328 | ||
214e098a | 1329 | pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val); |
cde9ea48 AC |
1330 | } |
1331 | else | |
515630c5 UW |
1332 | internal_error (__FILE__, __LINE__, |
1333 | _("regcache_read_pc: Unable to find PC")); | |
32178cab MS |
1334 | return pc_val; |
1335 | } | |
1336 | ||
32178cab | 1337 | void |
515630c5 | 1338 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) |
32178cab | 1339 | { |
61a1198a UW |
1340 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
1341 | ||
61a1198a UW |
1342 | if (gdbarch_write_pc_p (gdbarch)) |
1343 | gdbarch_write_pc (gdbarch, regcache, pc); | |
214e098a | 1344 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
3e8c568d | 1345 | regcache_cooked_write_unsigned (regcache, |
214e098a | 1346 | gdbarch_pc_regnum (gdbarch), pc); |
61a1198a UW |
1347 | else |
1348 | internal_error (__FILE__, __LINE__, | |
515630c5 | 1349 | _("regcache_write_pc: Unable to update PC")); |
edb3359d DJ |
1350 | |
1351 | /* Writing the PC (for instance, from "load") invalidates the | |
1352 | current frame. */ | |
1353 | reinit_frame_cache (); | |
32178cab MS |
1354 | } |
1355 | ||
ed771251 | 1356 | void |
ef79d9a3 | 1357 | regcache::debug_print_register (const char *func, int regno) |
ed771251 | 1358 | { |
ef79d9a3 | 1359 | struct gdbarch *gdbarch = arch (); |
ed771251 AH |
1360 | |
1361 | fprintf_unfiltered (gdb_stdlog, "%s ", func); | |
1362 | if (regno >= 0 && regno < gdbarch_num_regs (gdbarch) | |
1363 | && gdbarch_register_name (gdbarch, regno) != NULL | |
1364 | && gdbarch_register_name (gdbarch, regno)[0] != '\0') | |
1365 | fprintf_unfiltered (gdb_stdlog, "(%s)", | |
1366 | gdbarch_register_name (gdbarch, regno)); | |
1367 | else | |
1368 | fprintf_unfiltered (gdb_stdlog, "(%d)", regno); | |
1369 | if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)) | |
1370 | { | |
1371 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
1372 | int size = register_size (gdbarch, regno); | |
ef79d9a3 | 1373 | gdb_byte *buf = register_buffer (regno); |
ed771251 AH |
1374 | |
1375 | fprintf_unfiltered (gdb_stdlog, " = "); | |
1376 | for (int i = 0; i < size; i++) | |
1377 | { | |
1378 | fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]); | |
1379 | } | |
1380 | if (size <= sizeof (LONGEST)) | |
1381 | { | |
1382 | ULONGEST val = extract_unsigned_integer (buf, size, byte_order); | |
1383 | ||
1384 | fprintf_unfiltered (gdb_stdlog, " %s %s", | |
1385 | core_addr_to_string_nz (val), plongest (val)); | |
1386 | } | |
1387 | } | |
1388 | fprintf_unfiltered (gdb_stdlog, "\n"); | |
1389 | } | |
32178cab | 1390 | |
705152c5 MS |
1391 | static void |
1392 | reg_flush_command (char *command, int from_tty) | |
1393 | { | |
1394 | /* Force-flush the register cache. */ | |
1395 | registers_changed (); | |
1396 | if (from_tty) | |
a3f17187 | 1397 | printf_filtered (_("Register cache flushed.\n")); |
705152c5 MS |
1398 | } |
1399 | ||
ef79d9a3 YQ |
1400 | void |
1401 | regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump) | |
af030b9a AC |
1402 | { |
1403 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); | |
ef79d9a3 | 1404 | struct gdbarch *gdbarch = m_descr->gdbarch; |
af030b9a AC |
1405 | int regnum; |
1406 | int footnote_nr = 0; | |
1407 | int footnote_register_size = 0; | |
1408 | int footnote_register_offset = 0; | |
1409 | int footnote_register_type_name_null = 0; | |
1410 | long register_offset = 0; | |
af030b9a AC |
1411 | |
1412 | #if 0 | |
af030b9a | 1413 | fprintf_unfiltered (file, "nr_raw_registers %d\n", |
ef79d9a3 | 1414 | m_descr->nr_raw_registers); |
af030b9a | 1415 | fprintf_unfiltered (file, "nr_cooked_registers %d\n", |
ef79d9a3 | 1416 | m_descr->nr_cooked_registers); |
af030b9a | 1417 | fprintf_unfiltered (file, "sizeof_raw_registers %ld\n", |
ef79d9a3 | 1418 | m_descr->sizeof_raw_registers); |
ee99023e | 1419 | fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n", |
ef79d9a3 | 1420 | m_descr->sizeof_raw_register_status); |
f57d151a | 1421 | fprintf_unfiltered (file, "gdbarch_num_regs %d\n", |
214e098a | 1422 | gdbarch_num_regs (gdbarch)); |
f57d151a | 1423 | fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n", |
214e098a | 1424 | gdbarch_num_pseudo_regs (gdbarch)); |
af030b9a AC |
1425 | #endif |
1426 | ||
ef79d9a3 | 1427 | gdb_assert (m_descr->nr_cooked_registers |
214e098a UW |
1428 | == (gdbarch_num_regs (gdbarch) |
1429 | + gdbarch_num_pseudo_regs (gdbarch))); | |
af030b9a | 1430 | |
ef79d9a3 | 1431 | for (regnum = -1; regnum < m_descr->nr_cooked_registers; regnum++) |
af030b9a AC |
1432 | { |
1433 | /* Name. */ | |
1434 | if (regnum < 0) | |
1435 | fprintf_unfiltered (file, " %-10s", "Name"); | |
1436 | else | |
1437 | { | |
214e098a | 1438 | const char *p = gdbarch_register_name (gdbarch, regnum); |
123f5f96 | 1439 | |
af030b9a AC |
1440 | if (p == NULL) |
1441 | p = ""; | |
1442 | else if (p[0] == '\0') | |
1443 | p = "''"; | |
1444 | fprintf_unfiltered (file, " %-10s", p); | |
1445 | } | |
1446 | ||
1447 | /* Number. */ | |
1448 | if (regnum < 0) | |
1449 | fprintf_unfiltered (file, " %4s", "Nr"); | |
1450 | else | |
1451 | fprintf_unfiltered (file, " %4d", regnum); | |
1452 | ||
1453 | /* Relative number. */ | |
1454 | if (regnum < 0) | |
1455 | fprintf_unfiltered (file, " %4s", "Rel"); | |
214e098a | 1456 | else if (regnum < gdbarch_num_regs (gdbarch)) |
af030b9a AC |
1457 | fprintf_unfiltered (file, " %4d", regnum); |
1458 | else | |
f57d151a | 1459 | fprintf_unfiltered (file, " %4d", |
214e098a | 1460 | (regnum - gdbarch_num_regs (gdbarch))); |
af030b9a AC |
1461 | |
1462 | /* Offset. */ | |
1463 | if (regnum < 0) | |
1464 | fprintf_unfiltered (file, " %6s ", "Offset"); | |
1465 | else | |
1466 | { | |
1467 | fprintf_unfiltered (file, " %6ld", | |
ef79d9a3 YQ |
1468 | m_descr->register_offset[regnum]); |
1469 | if (register_offset != m_descr->register_offset[regnum] | |
d3b22ed5 | 1470 | || (regnum > 0 |
ef79d9a3 YQ |
1471 | && (m_descr->register_offset[regnum] |
1472 | != (m_descr->register_offset[regnum - 1] | |
1473 | + m_descr->sizeof_register[regnum - 1]))) | |
d3b22ed5 | 1474 | ) |
af030b9a AC |
1475 | { |
1476 | if (!footnote_register_offset) | |
1477 | footnote_register_offset = ++footnote_nr; | |
1478 | fprintf_unfiltered (file, "*%d", footnote_register_offset); | |
1479 | } | |
1480 | else | |
1481 | fprintf_unfiltered (file, " "); | |
ef79d9a3 YQ |
1482 | register_offset = (m_descr->register_offset[regnum] |
1483 | + m_descr->sizeof_register[regnum]); | |
af030b9a AC |
1484 | } |
1485 | ||
1486 | /* Size. */ | |
1487 | if (regnum < 0) | |
1488 | fprintf_unfiltered (file, " %5s ", "Size"); | |
1489 | else | |
ef79d9a3 | 1490 | fprintf_unfiltered (file, " %5ld", m_descr->sizeof_register[regnum]); |
af030b9a AC |
1491 | |
1492 | /* Type. */ | |
b59ff9d5 AC |
1493 | { |
1494 | const char *t; | |
123f5f96 | 1495 | |
b59ff9d5 AC |
1496 | if (regnum < 0) |
1497 | t = "Type"; | |
1498 | else | |
1499 | { | |
1500 | static const char blt[] = "builtin_type"; | |
123f5f96 | 1501 | |
ef79d9a3 | 1502 | t = TYPE_NAME (register_type (arch (), regnum)); |
b59ff9d5 AC |
1503 | if (t == NULL) |
1504 | { | |
1505 | char *n; | |
123f5f96 | 1506 | |
b59ff9d5 AC |
1507 | if (!footnote_register_type_name_null) |
1508 | footnote_register_type_name_null = ++footnote_nr; | |
b435e160 | 1509 | n = xstrprintf ("*%d", footnote_register_type_name_null); |
b59ff9d5 AC |
1510 | make_cleanup (xfree, n); |
1511 | t = n; | |
1512 | } | |
1513 | /* Chop a leading builtin_type. */ | |
61012eef | 1514 | if (startswith (t, blt)) |
b59ff9d5 AC |
1515 | t += strlen (blt); |
1516 | } | |
1517 | fprintf_unfiltered (file, " %-15s", t); | |
1518 | } | |
1519 | ||
1520 | /* Leading space always present. */ | |
1521 | fprintf_unfiltered (file, " "); | |
af030b9a AC |
1522 | |
1523 | /* Value, raw. */ | |
1524 | if (what_to_dump == regcache_dump_raw) | |
1525 | { | |
1526 | if (regnum < 0) | |
1527 | fprintf_unfiltered (file, "Raw value"); | |
ef79d9a3 | 1528 | else if (regnum >= m_descr->nr_raw_registers) |
af030b9a | 1529 | fprintf_unfiltered (file, "<cooked>"); |
ef79d9a3 | 1530 | else if (get_register_status (regnum) == REG_UNKNOWN) |
af030b9a | 1531 | fprintf_unfiltered (file, "<invalid>"); |
ef79d9a3 | 1532 | else if (get_register_status (regnum) == REG_UNAVAILABLE) |
ee99023e | 1533 | fprintf_unfiltered (file, "<unavailable>"); |
af030b9a AC |
1534 | else |
1535 | { | |
50d6adef AH |
1536 | raw_update (regnum); |
1537 | print_hex_chars (file, register_buffer (regnum), | |
ef79d9a3 | 1538 | m_descr->sizeof_register[regnum], |
30a25466 | 1539 | gdbarch_byte_order (gdbarch), true); |
af030b9a AC |
1540 | } |
1541 | } | |
1542 | ||
1543 | /* Value, cooked. */ | |
1544 | if (what_to_dump == regcache_dump_cooked) | |
1545 | { | |
1546 | if (regnum < 0) | |
1547 | fprintf_unfiltered (file, "Cooked value"); | |
1548 | else | |
1549 | { | |
50d6adef | 1550 | const gdb_byte *buf = NULL; |
05d1431c | 1551 | enum register_status status; |
50d6adef AH |
1552 | struct value *value = NULL; |
1553 | ||
1554 | if (regnum < m_descr->nr_raw_registers) | |
1555 | { | |
1556 | raw_update (regnum); | |
1557 | status = get_register_status (regnum); | |
1558 | buf = register_buffer (regnum); | |
1559 | } | |
1560 | else | |
1561 | { | |
1562 | value = cooked_read_value (regnum); | |
1563 | ||
1564 | if (!value_optimized_out (value) | |
1565 | && value_entirely_available (value)) | |
1566 | { | |
1567 | status = REG_VALID; | |
1568 | buf = value_contents_all (value); | |
1569 | } | |
1570 | else | |
1571 | status = REG_UNAVAILABLE; | |
1572 | } | |
05d1431c | 1573 | |
05d1431c PA |
1574 | if (status == REG_UNKNOWN) |
1575 | fprintf_unfiltered (file, "<invalid>"); | |
1576 | else if (status == REG_UNAVAILABLE) | |
1577 | fprintf_unfiltered (file, "<unavailable>"); | |
1578 | else | |
d3eaaf66 | 1579 | print_hex_chars (file, buf, |
ef79d9a3 | 1580 | m_descr->sizeof_register[regnum], |
30a25466 | 1581 | gdbarch_byte_order (gdbarch), true); |
50d6adef AH |
1582 | |
1583 | if (value != NULL) | |
1584 | { | |
1585 | release_value (value); | |
1586 | value_free (value); | |
1587 | } | |
af030b9a AC |
1588 | } |
1589 | } | |
1590 | ||
b59ff9d5 AC |
1591 | /* Group members. */ |
1592 | if (what_to_dump == regcache_dump_groups) | |
1593 | { | |
1594 | if (regnum < 0) | |
1595 | fprintf_unfiltered (file, "Groups"); | |
1596 | else | |
1597 | { | |
b59ff9d5 | 1598 | const char *sep = ""; |
6c7d17ba | 1599 | struct reggroup *group; |
123f5f96 | 1600 | |
6c7d17ba AC |
1601 | for (group = reggroup_next (gdbarch, NULL); |
1602 | group != NULL; | |
1603 | group = reggroup_next (gdbarch, group)) | |
b59ff9d5 | 1604 | { |
6c7d17ba | 1605 | if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) |
b59ff9d5 | 1606 | { |
3e43a32a MS |
1607 | fprintf_unfiltered (file, |
1608 | "%s%s", sep, reggroup_name (group)); | |
b59ff9d5 AC |
1609 | sep = ","; |
1610 | } | |
1611 | } | |
1612 | } | |
1613 | } | |
1614 | ||
c21236dc PA |
1615 | /* Remote packet configuration. */ |
1616 | if (what_to_dump == regcache_dump_remote) | |
1617 | { | |
1618 | if (regnum < 0) | |
1619 | { | |
1620 | fprintf_unfiltered (file, "Rmt Nr g/G Offset"); | |
1621 | } | |
ef79d9a3 | 1622 | else if (regnum < m_descr->nr_raw_registers) |
c21236dc PA |
1623 | { |
1624 | int pnum, poffset; | |
1625 | ||
ef79d9a3 | 1626 | if (remote_register_number_and_offset (arch (), regnum, |
c21236dc PA |
1627 | &pnum, &poffset)) |
1628 | fprintf_unfiltered (file, "%7d %11d", pnum, poffset); | |
1629 | } | |
1630 | } | |
1631 | ||
af030b9a AC |
1632 | fprintf_unfiltered (file, "\n"); |
1633 | } | |
1634 | ||
1635 | if (footnote_register_size) | |
1636 | fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n", | |
1637 | footnote_register_size); | |
1638 | if (footnote_register_offset) | |
1639 | fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n", | |
1640 | footnote_register_offset); | |
1641 | if (footnote_register_type_name_null) | |
1642 | fprintf_unfiltered (file, | |
1643 | "*%d: Register type's name NULL.\n", | |
1644 | footnote_register_type_name_null); | |
1645 | do_cleanups (cleanups); | |
1646 | } | |
1647 | ||
1648 | static void | |
1649 | regcache_print (char *args, enum regcache_dump_what what_to_dump) | |
1650 | { | |
1651 | if (args == NULL) | |
ef79d9a3 | 1652 | get_current_regcache ()->dump (gdb_stdout, what_to_dump); |
af030b9a AC |
1653 | else |
1654 | { | |
d7e74731 | 1655 | stdio_file file; |
123f5f96 | 1656 | |
d7e74731 | 1657 | if (!file.open (args, "w")) |
e2e0b3e5 | 1658 | perror_with_name (_("maintenance print architecture")); |
ef79d9a3 | 1659 | get_current_regcache ()->dump (&file, what_to_dump); |
af030b9a AC |
1660 | } |
1661 | } | |
1662 | ||
1663 | static void | |
1664 | maintenance_print_registers (char *args, int from_tty) | |
1665 | { | |
1666 | regcache_print (args, regcache_dump_none); | |
1667 | } | |
1668 | ||
1669 | static void | |
1670 | maintenance_print_raw_registers (char *args, int from_tty) | |
1671 | { | |
1672 | regcache_print (args, regcache_dump_raw); | |
1673 | } | |
1674 | ||
1675 | static void | |
1676 | maintenance_print_cooked_registers (char *args, int from_tty) | |
1677 | { | |
1678 | regcache_print (args, regcache_dump_cooked); | |
1679 | } | |
1680 | ||
b59ff9d5 AC |
1681 | static void |
1682 | maintenance_print_register_groups (char *args, int from_tty) | |
1683 | { | |
1684 | regcache_print (args, regcache_dump_groups); | |
1685 | } | |
1686 | ||
c21236dc PA |
1687 | static void |
1688 | maintenance_print_remote_registers (char *args, int from_tty) | |
1689 | { | |
1690 | regcache_print (args, regcache_dump_remote); | |
1691 | } | |
1692 | ||
8248946c YQ |
1693 | #if GDB_SELF_TEST |
1694 | #include "selftest.h" | |
1695 | ||
1696 | namespace selftests { | |
1697 | ||
e521e87e | 1698 | class regcache_access : public regcache |
8248946c | 1699 | { |
e521e87e YQ |
1700 | public: |
1701 | ||
1702 | /* Return the number of elements in current_regcache. */ | |
1703 | ||
1704 | static size_t | |
1705 | current_regcache_size () | |
1706 | { | |
1707 | return std::distance (regcache::current_regcache.begin (), | |
1708 | regcache::current_regcache.end ()); | |
1709 | } | |
1710 | }; | |
8248946c YQ |
1711 | |
1712 | static void | |
1713 | current_regcache_test (void) | |
1714 | { | |
1715 | /* It is empty at the start. */ | |
e521e87e | 1716 | SELF_CHECK (regcache_access::current_regcache_size () == 0); |
8248946c YQ |
1717 | |
1718 | ptid_t ptid1 (1), ptid2 (2), ptid3 (3); | |
1719 | ||
1720 | /* Get regcache from ptid1, a new regcache is added to | |
1721 | current_regcache. */ | |
1722 | regcache *regcache = get_thread_arch_aspace_regcache (ptid1, | |
1723 | target_gdbarch (), | |
1724 | NULL); | |
1725 | ||
1726 | SELF_CHECK (regcache != NULL); | |
1727 | SELF_CHECK (regcache->ptid () == ptid1); | |
e521e87e | 1728 | SELF_CHECK (regcache_access::current_regcache_size () == 1); |
8248946c YQ |
1729 | |
1730 | /* Get regcache from ptid2, a new regcache is added to | |
1731 | current_regcache. */ | |
1732 | regcache = get_thread_arch_aspace_regcache (ptid2, | |
1733 | target_gdbarch (), | |
1734 | NULL); | |
1735 | SELF_CHECK (regcache != NULL); | |
1736 | SELF_CHECK (regcache->ptid () == ptid2); | |
e521e87e | 1737 | SELF_CHECK (regcache_access::current_regcache_size () == 2); |
8248946c YQ |
1738 | |
1739 | /* Get regcache from ptid3, a new regcache is added to | |
1740 | current_regcache. */ | |
1741 | regcache = get_thread_arch_aspace_regcache (ptid3, | |
1742 | target_gdbarch (), | |
1743 | NULL); | |
1744 | SELF_CHECK (regcache != NULL); | |
1745 | SELF_CHECK (regcache->ptid () == ptid3); | |
e521e87e | 1746 | SELF_CHECK (regcache_access::current_regcache_size () == 3); |
8248946c YQ |
1747 | |
1748 | /* Get regcache from ptid2 again, nothing is added to | |
1749 | current_regcache. */ | |
1750 | regcache = get_thread_arch_aspace_regcache (ptid2, | |
1751 | target_gdbarch (), | |
1752 | NULL); | |
1753 | SELF_CHECK (regcache != NULL); | |
1754 | SELF_CHECK (regcache->ptid () == ptid2); | |
e521e87e | 1755 | SELF_CHECK (regcache_access::current_regcache_size () == 3); |
8248946c YQ |
1756 | |
1757 | /* Mark ptid2 is changed, so regcache of ptid2 should be removed from | |
1758 | current_regcache. */ | |
1759 | registers_changed_ptid (ptid2); | |
e521e87e | 1760 | SELF_CHECK (regcache_access::current_regcache_size () == 2); |
8248946c YQ |
1761 | } |
1762 | ||
1763 | } // namespace selftests | |
1764 | #endif /* GDB_SELF_TEST */ | |
1765 | ||
b9362cc7 AC |
1766 | extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */ |
1767 | ||
32178cab MS |
1768 | void |
1769 | _initialize_regcache (void) | |
1770 | { | |
3e43a32a MS |
1771 | regcache_descr_handle |
1772 | = gdbarch_data_register_post_init (init_regcache_descr); | |
705152c5 | 1773 | |
f4c5303c | 1774 | observer_attach_target_changed (regcache_observer_target_changed); |
e521e87e | 1775 | observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed); |
f4c5303c | 1776 | |
705152c5 | 1777 | add_com ("flushregs", class_maintenance, reg_flush_command, |
1bedd215 | 1778 | _("Force gdb to flush its register cache (maintainer command)")); |
39f77062 | 1779 | |
3e43a32a MS |
1780 | add_cmd ("registers", class_maintenance, maintenance_print_registers, |
1781 | _("Print the internal register configuration.\n" | |
1782 | "Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1783 | add_cmd ("raw-registers", class_maintenance, |
3e43a32a MS |
1784 | maintenance_print_raw_registers, |
1785 | _("Print the internal register configuration " | |
1786 | "including raw values.\n" | |
1787 | "Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1788 | add_cmd ("cooked-registers", class_maintenance, |
3e43a32a MS |
1789 | maintenance_print_cooked_registers, |
1790 | _("Print the internal register configuration " | |
1791 | "including cooked values.\n" | |
1792 | "Takes an optional file parameter."), &maintenanceprintlist); | |
b59ff9d5 | 1793 | add_cmd ("register-groups", class_maintenance, |
3e43a32a MS |
1794 | maintenance_print_register_groups, |
1795 | _("Print the internal register configuration " | |
1796 | "including each register's group.\n" | |
1797 | "Takes an optional file parameter."), | |
af030b9a | 1798 | &maintenanceprintlist); |
c21236dc PA |
1799 | add_cmd ("remote-registers", class_maintenance, |
1800 | maintenance_print_remote_registers, _("\ | |
1801 | Print the internal register configuration including each register's\n\ | |
1802 | remote register number and buffer offset in the g/G packets.\n\ | |
1803 | Takes an optional file parameter."), | |
1804 | &maintenanceprintlist); | |
8248946c YQ |
1805 | #if GDB_SELF_TEST |
1806 | register_self_test (selftests::current_regcache_test); | |
1807 | #endif | |
32178cab | 1808 | } |