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