Commit | Line | Data |
---|---|---|
32178cab | 1 | /* Cache and manage the values of registers for GDB, the GNU debugger. |
3fadccb3 | 2 | |
6aba47ca | 3 | Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001, |
7b6bb8da | 4 | 2002, 2004, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
32178cab MS |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
32178cab MS |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
32178cab MS |
20 | |
21 | #include "defs.h" | |
32178cab MS |
22 | #include "inferior.h" |
23 | #include "target.h" | |
24 | #include "gdbarch.h" | |
705152c5 | 25 | #include "gdbcmd.h" |
4e052eda | 26 | #include "regcache.h" |
b59ff9d5 | 27 | #include "reggroups.h" |
61a0eb5b | 28 | #include "gdb_assert.h" |
b66d6d2e | 29 | #include "gdb_string.h" |
af030b9a | 30 | #include "gdbcmd.h" /* For maintenanceprintlist. */ |
f4c5303c | 31 | #include "observer.h" |
05d1431c | 32 | #include "exceptions.h" |
c21236dc | 33 | #include "remote.h" |
32178cab MS |
34 | |
35 | /* | |
36 | * DATA STRUCTURE | |
37 | * | |
38 | * Here is the actual register cache. | |
39 | */ | |
40 | ||
3fadccb3 | 41 | /* Per-architecture object describing the layout of a register cache. |
0df8b418 | 42 | Computed once when the architecture is created. */ |
3fadccb3 AC |
43 | |
44 | struct gdbarch_data *regcache_descr_handle; | |
45 | ||
46 | struct regcache_descr | |
47 | { | |
48 | /* The architecture this descriptor belongs to. */ | |
49 | struct gdbarch *gdbarch; | |
50 | ||
bb1db049 AC |
51 | /* The raw register cache. Each raw (or hard) register is supplied |
52 | by the target interface. The raw cache should not contain | |
53 | redundant information - if the PC is constructed from two | |
d2f0b918 | 54 | registers then those registers and not the PC lives in the raw |
bb1db049 | 55 | cache. */ |
3fadccb3 AC |
56 | int nr_raw_registers; |
57 | long sizeof_raw_registers; | |
ee99023e | 58 | long sizeof_raw_register_status; |
3fadccb3 | 59 | |
d138e37a AC |
60 | /* The cooked register space. Each cooked register in the range |
61 | [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw | |
62 | register. The remaining [NR_RAW_REGISTERS | |
02f60eae | 63 | .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto |
d138e37a | 64 | both raw registers and memory by the architecture methods |
02f60eae | 65 | gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */ |
d138e37a | 66 | int nr_cooked_registers; |
067df2e5 | 67 | long sizeof_cooked_registers; |
ee99023e | 68 | long sizeof_cooked_register_status; |
d138e37a | 69 | |
86d31898 | 70 | /* Offset and size (in 8 bit bytes), of each register in the |
d138e37a | 71 | register cache. All registers (including those in the range |
99e42fd8 PA |
72 | [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an |
73 | offset. */ | |
3fadccb3 | 74 | long *register_offset; |
3fadccb3 | 75 | long *sizeof_register; |
3fadccb3 | 76 | |
bb425013 AC |
77 | /* Cached table containing the type of each register. */ |
78 | struct type **register_type; | |
3fadccb3 AC |
79 | }; |
80 | ||
3fadccb3 AC |
81 | static void * |
82 | init_regcache_descr (struct gdbarch *gdbarch) | |
83 | { | |
84 | int i; | |
85 | struct regcache_descr *descr; | |
86 | gdb_assert (gdbarch != NULL); | |
87 | ||
bb425013 | 88 | /* Create an initial, zero filled, table. */ |
116f06ea | 89 | descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr); |
3fadccb3 | 90 | descr->gdbarch = gdbarch; |
3fadccb3 | 91 | |
d138e37a AC |
92 | /* Total size of the register space. The raw registers are mapped |
93 | directly onto the raw register cache while the pseudo's are | |
3fadccb3 | 94 | either mapped onto raw-registers or memory. */ |
214e098a UW |
95 | descr->nr_cooked_registers = gdbarch_num_regs (gdbarch) |
96 | + gdbarch_num_pseudo_regs (gdbarch); | |
ee99023e PA |
97 | descr->sizeof_cooked_register_status |
98 | = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); | |
3fadccb3 | 99 | |
bb425013 | 100 | /* Fill in a table of register types. */ |
116f06ea | 101 | descr->register_type |
3e43a32a MS |
102 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, |
103 | struct type *); | |
bb425013 | 104 | for (i = 0; i < descr->nr_cooked_registers; i++) |
336a3131 | 105 | descr->register_type[i] = gdbarch_register_type (gdbarch, i); |
bb425013 | 106 | |
bb1db049 AC |
107 | /* Construct a strictly RAW register cache. Don't allow pseudo's |
108 | into the register cache. */ | |
214e098a | 109 | descr->nr_raw_registers = gdbarch_num_regs (gdbarch); |
ee99023e | 110 | descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch); |
bb1db049 | 111 | |
067df2e5 | 112 | /* Lay out the register cache. |
3fadccb3 | 113 | |
bb425013 AC |
114 | NOTE: cagney/2002-05-22: Only register_type() is used when |
115 | constructing the register cache. It is assumed that the | |
116 | register's raw size, virtual size and type length are all the | |
117 | same. */ | |
3fadccb3 AC |
118 | |
119 | { | |
120 | long offset = 0; | |
123f5f96 | 121 | |
116f06ea AC |
122 | descr->sizeof_register |
123 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
124 | descr->register_offset | |
125 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
99e42fd8 PA |
126 | for (i = 0; i < descr->nr_raw_registers; i++) |
127 | { | |
128 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); | |
129 | descr->register_offset[i] = offset; | |
130 | offset += descr->sizeof_register[i]; | |
131 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); | |
132 | } | |
133 | /* Set the real size of the raw register cache buffer. */ | |
134 | descr->sizeof_raw_registers = offset; | |
135 | ||
136 | for (; i < descr->nr_cooked_registers; i++) | |
3fadccb3 | 137 | { |
bb425013 | 138 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); |
3fadccb3 AC |
139 | descr->register_offset[i] = offset; |
140 | offset += descr->sizeof_register[i]; | |
123a958e | 141 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); |
3fadccb3 | 142 | } |
99e42fd8 | 143 | /* Set the real size of the readonly register cache buffer. */ |
067df2e5 | 144 | descr->sizeof_cooked_registers = offset; |
3fadccb3 AC |
145 | } |
146 | ||
3fadccb3 AC |
147 | return descr; |
148 | } | |
149 | ||
150 | static struct regcache_descr * | |
151 | regcache_descr (struct gdbarch *gdbarch) | |
152 | { | |
153 | return gdbarch_data (gdbarch, regcache_descr_handle); | |
154 | } | |
155 | ||
bb425013 AC |
156 | /* Utility functions returning useful register attributes stored in |
157 | the regcache descr. */ | |
158 | ||
159 | struct type * | |
160 | register_type (struct gdbarch *gdbarch, int regnum) | |
161 | { | |
162 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
123f5f96 | 163 | |
bb425013 AC |
164 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
165 | return descr->register_type[regnum]; | |
166 | } | |
167 | ||
0ed04cce AC |
168 | /* Utility functions returning useful register attributes stored in |
169 | the regcache descr. */ | |
170 | ||
08a617da AC |
171 | int |
172 | register_size (struct gdbarch *gdbarch, int regnum) | |
173 | { | |
174 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
175 | int size; | |
123f5f96 | 176 | |
f57d151a | 177 | gdb_assert (regnum >= 0 |
214e098a UW |
178 | && regnum < (gdbarch_num_regs (gdbarch) |
179 | + gdbarch_num_pseudo_regs (gdbarch))); | |
08a617da | 180 | size = descr->sizeof_register[regnum]; |
08a617da AC |
181 | return size; |
182 | } | |
183 | ||
3fadccb3 AC |
184 | /* The register cache for storing raw register values. */ |
185 | ||
186 | struct regcache | |
187 | { | |
188 | struct regcache_descr *descr; | |
6c95b8df PA |
189 | |
190 | /* The address space of this register cache (for registers where it | |
191 | makes sense, like PC or SP). */ | |
192 | struct address_space *aspace; | |
193 | ||
51b1fe4e | 194 | /* The register buffers. A read-only register cache can hold the |
f57d151a UW |
195 | full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write |
196 | register cache can only hold [0 .. gdbarch_num_regs). */ | |
2d522557 | 197 | gdb_byte *registers; |
ee99023e PA |
198 | /* Register cache status. */ |
199 | signed char *register_status; | |
2d28509a AC |
200 | /* Is this a read-only cache? A read-only cache is used for saving |
201 | the target's register state (e.g, across an inferior function | |
202 | call or just before forcing a function return). A read-only | |
203 | cache can only be updated via the methods regcache_dup() and | |
204 | regcache_cpy(). The actual contents are determined by the | |
205 | reggroup_save and reggroup_restore methods. */ | |
206 | int readonly_p; | |
594f7785 UW |
207 | /* If this is a read-write cache, which thread's registers is |
208 | it connected to? */ | |
209 | ptid_t ptid; | |
3fadccb3 AC |
210 | }; |
211 | ||
99e42fd8 PA |
212 | static struct regcache * |
213 | regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace, | |
214 | int readonly_p) | |
3fadccb3 AC |
215 | { |
216 | struct regcache_descr *descr; | |
217 | struct regcache *regcache; | |
123f5f96 | 218 | |
3fadccb3 AC |
219 | gdb_assert (gdbarch != NULL); |
220 | descr = regcache_descr (gdbarch); | |
221 | regcache = XMALLOC (struct regcache); | |
222 | regcache->descr = descr; | |
99e42fd8 PA |
223 | regcache->readonly_p = readonly_p; |
224 | if (readonly_p) | |
225 | { | |
226 | regcache->registers | |
227 | = XCALLOC (descr->sizeof_cooked_registers, gdb_byte); | |
ee99023e PA |
228 | regcache->register_status |
229 | = XCALLOC (descr->sizeof_cooked_register_status, gdb_byte); | |
99e42fd8 PA |
230 | } |
231 | else | |
232 | { | |
233 | regcache->registers | |
234 | = XCALLOC (descr->sizeof_raw_registers, gdb_byte); | |
ee99023e PA |
235 | regcache->register_status |
236 | = XCALLOC (descr->sizeof_raw_register_status, gdb_byte); | |
99e42fd8 | 237 | } |
d37346f0 | 238 | regcache->aspace = aspace; |
594f7785 | 239 | regcache->ptid = minus_one_ptid; |
3fadccb3 AC |
240 | return regcache; |
241 | } | |
242 | ||
99e42fd8 PA |
243 | struct regcache * |
244 | regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace) | |
245 | { | |
246 | return regcache_xmalloc_1 (gdbarch, aspace, 1); | |
247 | } | |
248 | ||
3fadccb3 AC |
249 | void |
250 | regcache_xfree (struct regcache *regcache) | |
251 | { | |
252 | if (regcache == NULL) | |
253 | return; | |
51b1fe4e | 254 | xfree (regcache->registers); |
ee99023e | 255 | xfree (regcache->register_status); |
3fadccb3 AC |
256 | xfree (regcache); |
257 | } | |
258 | ||
b9362cc7 | 259 | static void |
36160dc4 AC |
260 | do_regcache_xfree (void *data) |
261 | { | |
262 | regcache_xfree (data); | |
263 | } | |
264 | ||
265 | struct cleanup * | |
266 | make_cleanup_regcache_xfree (struct regcache *regcache) | |
267 | { | |
268 | return make_cleanup (do_regcache_xfree, regcache); | |
269 | } | |
270 | ||
41d35cb0 MK |
271 | /* Return REGCACHE's architecture. */ |
272 | ||
273 | struct gdbarch * | |
274 | get_regcache_arch (const struct regcache *regcache) | |
275 | { | |
276 | return regcache->descr->gdbarch; | |
277 | } | |
278 | ||
6c95b8df PA |
279 | struct address_space * |
280 | get_regcache_aspace (const struct regcache *regcache) | |
281 | { | |
282 | return regcache->aspace; | |
283 | } | |
284 | ||
51b1fe4e AC |
285 | /* Return a pointer to register REGNUM's buffer cache. */ |
286 | ||
2d522557 | 287 | static gdb_byte * |
9a661b68 | 288 | register_buffer (const struct regcache *regcache, int regnum) |
51b1fe4e AC |
289 | { |
290 | return regcache->registers + regcache->descr->register_offset[regnum]; | |
291 | } | |
292 | ||
2d28509a | 293 | void |
5602984a AC |
294 | regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read, |
295 | void *src) | |
2d28509a AC |
296 | { |
297 | struct gdbarch *gdbarch = dst->descr->gdbarch; | |
2d522557 | 298 | gdb_byte buf[MAX_REGISTER_SIZE]; |
2d28509a | 299 | int regnum; |
123f5f96 | 300 | |
2d28509a | 301 | /* The DST should be `read-only', if it wasn't then the save would |
5602984a | 302 | end up trying to write the register values back out to the |
2d28509a | 303 | target. */ |
2d28509a AC |
304 | gdb_assert (dst->readonly_p); |
305 | /* Clear the dest. */ | |
306 | memset (dst->registers, 0, dst->descr->sizeof_cooked_registers); | |
ee99023e PA |
307 | memset (dst->register_status, 0, |
308 | dst->descr->sizeof_cooked_register_status); | |
2d28509a | 309 | /* Copy over any registers (identified by their membership in the |
f57d151a UW |
310 | save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs + |
311 | gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a | 312 | to save/restore `cooked' registers that live in memory. */ |
2d28509a AC |
313 | for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++) |
314 | { | |
315 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
316 | { | |
05d1431c | 317 | enum register_status status = cooked_read (src, regnum, buf); |
123f5f96 | 318 | |
05d1431c PA |
319 | if (status == REG_VALID) |
320 | memcpy (register_buffer (dst, regnum), buf, | |
321 | register_size (gdbarch, regnum)); | |
322 | else | |
5602984a | 323 | { |
05d1431c PA |
324 | gdb_assert (status != REG_UNKNOWN); |
325 | ||
326 | memset (register_buffer (dst, regnum), 0, | |
5602984a | 327 | register_size (gdbarch, regnum)); |
5602984a | 328 | } |
05d1431c | 329 | dst->register_status[regnum] = status; |
2d28509a AC |
330 | } |
331 | } | |
332 | } | |
333 | ||
334 | void | |
5602984a AC |
335 | regcache_restore (struct regcache *dst, |
336 | regcache_cooked_read_ftype *cooked_read, | |
2d522557 | 337 | void *cooked_read_context) |
2d28509a AC |
338 | { |
339 | struct gdbarch *gdbarch = dst->descr->gdbarch; | |
2d522557 | 340 | gdb_byte buf[MAX_REGISTER_SIZE]; |
2d28509a | 341 | int regnum; |
123f5f96 | 342 | |
5602984a AC |
343 | /* The dst had better not be read-only. If it is, the `restore' |
344 | doesn't make much sense. */ | |
2d28509a | 345 | gdb_assert (!dst->readonly_p); |
2d28509a | 346 | /* Copy over any registers, being careful to only restore those that |
f57d151a UW |
347 | were both saved and need to be restored. The full [0 .. gdbarch_num_regs |
348 | + gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a AC |
349 | to save/restore `cooked' registers that live in memory. */ |
350 | for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++) | |
2d28509a | 351 | { |
5602984a | 352 | if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)) |
2d28509a | 353 | { |
2d522557 | 354 | int valid = cooked_read (cooked_read_context, regnum, buf); |
123f5f96 | 355 | |
5602984a AC |
356 | if (valid) |
357 | regcache_cooked_write (dst, regnum, buf); | |
2d28509a AC |
358 | } |
359 | } | |
360 | } | |
361 | ||
05d1431c | 362 | static enum register_status |
2d522557 | 363 | do_cooked_read (void *src, int regnum, gdb_byte *buf) |
5602984a AC |
364 | { |
365 | struct regcache *regcache = src; | |
123f5f96 | 366 | |
05d1431c | 367 | return regcache_cooked_read (regcache, regnum, buf); |
5602984a AC |
368 | } |
369 | ||
3fadccb3 AC |
370 | void |
371 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
372 | { | |
3fadccb3 AC |
373 | gdb_assert (src != NULL && dst != NULL); |
374 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
375 | gdb_assert (src != dst); | |
2d28509a | 376 | gdb_assert (src->readonly_p || dst->readonly_p); |
6c95b8df | 377 | |
2d28509a | 378 | if (!src->readonly_p) |
5602984a | 379 | regcache_save (dst, do_cooked_read, src); |
2d28509a | 380 | else if (!dst->readonly_p) |
5602984a | 381 | regcache_restore (dst, do_cooked_read, src); |
2d28509a AC |
382 | else |
383 | regcache_cpy_no_passthrough (dst, src); | |
3fadccb3 AC |
384 | } |
385 | ||
386 | void | |
387 | regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src) | |
388 | { | |
3fadccb3 AC |
389 | gdb_assert (src != NULL && dst != NULL); |
390 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
391 | /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough | |
ee99023e PA |
392 | move of data into a thread's regcache. Doing this would be silly |
393 | - it would mean that regcache->register_status would be | |
394 | completely invalid. */ | |
99e42fd8 | 395 | gdb_assert (dst->readonly_p && src->readonly_p); |
6c95b8df | 396 | |
99e42fd8 PA |
397 | memcpy (dst->registers, src->registers, |
398 | dst->descr->sizeof_cooked_registers); | |
ee99023e PA |
399 | memcpy (dst->register_status, src->register_status, |
400 | dst->descr->sizeof_cooked_register_status); | |
3fadccb3 AC |
401 | } |
402 | ||
403 | struct regcache * | |
404 | regcache_dup (struct regcache *src) | |
405 | { | |
406 | struct regcache *newbuf; | |
123f5f96 | 407 | |
d37346f0 | 408 | newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src)); |
3fadccb3 AC |
409 | regcache_cpy (newbuf, src); |
410 | return newbuf; | |
411 | } | |
412 | ||
3fadccb3 | 413 | int |
ee99023e | 414 | regcache_register_status (const struct regcache *regcache, int regnum) |
3fadccb3 AC |
415 | { |
416 | gdb_assert (regcache != NULL); | |
6ed7ea50 UW |
417 | gdb_assert (regnum >= 0); |
418 | if (regcache->readonly_p) | |
419 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); | |
420 | else | |
421 | gdb_assert (regnum < regcache->descr->nr_raw_registers); | |
422 | ||
ee99023e | 423 | return regcache->register_status[regnum]; |
3fadccb3 AC |
424 | } |
425 | ||
9c5ea4d9 UW |
426 | void |
427 | regcache_invalidate (struct regcache *regcache, int regnum) | |
428 | { | |
429 | gdb_assert (regcache != NULL); | |
430 | gdb_assert (regnum >= 0); | |
431 | gdb_assert (!regcache->readonly_p); | |
432 | gdb_assert (regnum < regcache->descr->nr_raw_registers); | |
ee99023e | 433 | regcache->register_status[regnum] = REG_UNKNOWN; |
9c5ea4d9 UW |
434 | } |
435 | ||
436 | ||
3fadccb3 | 437 | /* Global structure containing the current regcache. */ |
3fadccb3 | 438 | |
5ebd2499 | 439 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
32178cab MS |
440 | recording if the register values have been changed (eg. by the |
441 | user). Therefore all registers must be written back to the | |
442 | target when appropriate. */ | |
443 | ||
c2250ad1 | 444 | struct regcache_list |
594f7785 | 445 | { |
c2250ad1 UW |
446 | struct regcache *regcache; |
447 | struct regcache_list *next; | |
448 | }; | |
449 | ||
450 | static struct regcache_list *current_regcache; | |
451 | ||
452 | struct regcache * | |
453 | get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch) | |
454 | { | |
455 | struct regcache_list *list; | |
456 | struct regcache *new_regcache; | |
b78974c3 | 457 | struct address_space *aspace; |
594f7785 | 458 | |
c2250ad1 UW |
459 | for (list = current_regcache; list; list = list->next) |
460 | if (ptid_equal (list->regcache->ptid, ptid) | |
461 | && get_regcache_arch (list->regcache) == gdbarch) | |
462 | return list->regcache; | |
594f7785 | 463 | |
b78974c3 PA |
464 | /* For the benefit of "maint print registers" & co when debugging an |
465 | executable, allow dumping the regcache even when there is no | |
466 | thread selected (target_thread_address_space internal-errors if | |
467 | no address space is found). Note that normal user commands will | |
468 | fail higher up on the call stack due to no | |
469 | target_has_registers. */ | |
470 | aspace = (ptid_equal (null_ptid, ptid) | |
471 | ? NULL | |
472 | : target_thread_address_space (ptid)); | |
473 | ||
474 | new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0); | |
c2250ad1 | 475 | new_regcache->ptid = ptid; |
594f7785 | 476 | |
c2250ad1 UW |
477 | list = xmalloc (sizeof (struct regcache_list)); |
478 | list->regcache = new_regcache; | |
479 | list->next = current_regcache; | |
480 | current_regcache = list; | |
594f7785 | 481 | |
c2250ad1 | 482 | return new_regcache; |
594f7785 UW |
483 | } |
484 | ||
c2250ad1 UW |
485 | static ptid_t current_thread_ptid; |
486 | static struct gdbarch *current_thread_arch; | |
487 | ||
488 | struct regcache * | |
489 | get_thread_regcache (ptid_t ptid) | |
490 | { | |
491 | if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid)) | |
492 | { | |
493 | current_thread_ptid = ptid; | |
494 | current_thread_arch = target_thread_architecture (ptid); | |
495 | } | |
496 | ||
497 | return get_thread_arch_regcache (ptid, current_thread_arch); | |
498 | } | |
499 | ||
500 | struct regcache * | |
501 | get_current_regcache (void) | |
594f7785 UW |
502 | { |
503 | return get_thread_regcache (inferior_ptid); | |
504 | } | |
32178cab | 505 | |
32178cab | 506 | |
f4c5303c OF |
507 | /* Observer for the target_changed event. */ |
508 | ||
2c0b251b | 509 | static void |
f4c5303c OF |
510 | regcache_observer_target_changed (struct target_ops *target) |
511 | { | |
512 | registers_changed (); | |
513 | } | |
514 | ||
5231c1fd PA |
515 | /* Update global variables old ptids to hold NEW_PTID if they were |
516 | holding OLD_PTID. */ | |
517 | static void | |
518 | regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) | |
519 | { | |
c2250ad1 UW |
520 | struct regcache_list *list; |
521 | ||
522 | for (list = current_regcache; list; list = list->next) | |
523 | if (ptid_equal (list->regcache->ptid, old_ptid)) | |
524 | list->regcache->ptid = new_ptid; | |
5231c1fd PA |
525 | } |
526 | ||
32178cab MS |
527 | /* Low level examining and depositing of registers. |
528 | ||
529 | The caller is responsible for making sure that the inferior is | |
530 | stopped before calling the fetching routines, or it will get | |
531 | garbage. (a change from GDB version 3, in which the caller got the | |
532 | value from the last stop). */ | |
533 | ||
534 | /* REGISTERS_CHANGED () | |
535 | ||
536 | Indicate that registers may have changed, so invalidate the cache. */ | |
537 | ||
538 | void | |
e66408ed | 539 | registers_changed_ptid (ptid_t ptid) |
32178cab | 540 | { |
e66408ed | 541 | struct regcache_list *list, **list_link; |
041274d8 | 542 | int wildcard = ptid_equal (ptid, minus_one_ptid); |
c2250ad1 | 543 | |
e66408ed PA |
544 | list = current_regcache; |
545 | list_link = ¤t_regcache; | |
546 | while (list) | |
c2250ad1 | 547 | { |
e66408ed PA |
548 | if (ptid_match (list->regcache->ptid, ptid)) |
549 | { | |
550 | struct regcache_list *dead = list; | |
551 | ||
552 | *list_link = list->next; | |
553 | regcache_xfree (list->regcache); | |
554 | list = *list_link; | |
555 | xfree (dead); | |
556 | continue; | |
557 | } | |
558 | ||
559 | list_link = &list->next; | |
560 | list = *list_link; | |
c2250ad1 | 561 | } |
32178cab | 562 | |
041274d8 PA |
563 | if (wildcard || ptid_equal (ptid, current_thread_ptid)) |
564 | { | |
565 | current_thread_ptid = null_ptid; | |
566 | current_thread_arch = NULL; | |
567 | } | |
32178cab | 568 | |
041274d8 PA |
569 | if (wildcard || ptid_equal (ptid, inferior_ptid)) |
570 | { | |
571 | /* We just deleted the regcache of the current thread. Need to | |
572 | forget about any frames we have cached, too. */ | |
573 | reinit_frame_cache (); | |
574 | } | |
575 | } | |
c2250ad1 | 576 | |
041274d8 PA |
577 | void |
578 | registers_changed (void) | |
579 | { | |
580 | registers_changed_ptid (minus_one_ptid); | |
a5d9d57d | 581 | |
32178cab MS |
582 | /* Force cleanup of any alloca areas if using C alloca instead of |
583 | a builtin alloca. This particular call is used to clean up | |
584 | areas allocated by low level target code which may build up | |
585 | during lengthy interactions between gdb and the target before | |
586 | gdb gives control to the user (ie watchpoints). */ | |
587 | alloca (0); | |
32178cab MS |
588 | } |
589 | ||
05d1431c | 590 | enum register_status |
2d522557 | 591 | regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
61a0eb5b | 592 | { |
3fadccb3 AC |
593 | gdb_assert (regcache != NULL && buf != NULL); |
594 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
3fadccb3 AC |
595 | /* Make certain that the register cache is up-to-date with respect |
596 | to the current thread. This switching shouldn't be necessary | |
597 | only there is still only one target side register cache. Sigh! | |
598 | On the bright side, at least there is a regcache object. */ | |
788c8b10 PA |
599 | if (!regcache->readonly_p |
600 | && regcache_register_status (regcache, regnum) == REG_UNKNOWN) | |
3fadccb3 | 601 | { |
788c8b10 | 602 | struct cleanup *old_chain = save_inferior_ptid (); |
123f5f96 | 603 | |
788c8b10 PA |
604 | inferior_ptid = regcache->ptid; |
605 | target_fetch_registers (regcache, regnum); | |
606 | do_cleanups (old_chain); | |
607 | ||
608 | /* A number of targets can't access the whole set of raw | |
609 | registers (because the debug API provides no means to get at | |
610 | them). */ | |
611 | if (regcache->register_status[regnum] == REG_UNKNOWN) | |
612 | regcache->register_status[regnum] = REG_UNAVAILABLE; | |
3fadccb3 | 613 | } |
05d1431c PA |
614 | |
615 | if (regcache->register_status[regnum] != REG_VALID) | |
616 | memset (buf, 0, regcache->descr->sizeof_register[regnum]); | |
617 | else | |
618 | memcpy (buf, register_buffer (regcache, regnum), | |
619 | regcache->descr->sizeof_register[regnum]); | |
620 | ||
621 | return regcache->register_status[regnum]; | |
61a0eb5b AC |
622 | } |
623 | ||
05d1431c | 624 | enum register_status |
28fc6740 AC |
625 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) |
626 | { | |
2d522557 | 627 | gdb_byte *buf; |
05d1431c | 628 | enum register_status status; |
123f5f96 | 629 | |
28fc6740 AC |
630 | gdb_assert (regcache != NULL); |
631 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
632 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
05d1431c PA |
633 | status = regcache_raw_read (regcache, regnum, buf); |
634 | if (status == REG_VALID) | |
635 | *val = extract_signed_integer | |
636 | (buf, regcache->descr->sizeof_register[regnum], | |
637 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
638 | else | |
639 | *val = 0; | |
640 | return status; | |
28fc6740 AC |
641 | } |
642 | ||
05d1431c | 643 | enum register_status |
28fc6740 AC |
644 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, |
645 | ULONGEST *val) | |
646 | { | |
2d522557 | 647 | gdb_byte *buf; |
05d1431c | 648 | enum register_status status; |
123f5f96 | 649 | |
28fc6740 AC |
650 | gdb_assert (regcache != NULL); |
651 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
652 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
05d1431c PA |
653 | status = regcache_raw_read (regcache, regnum, buf); |
654 | if (status == REG_VALID) | |
655 | *val = extract_unsigned_integer | |
656 | (buf, regcache->descr->sizeof_register[regnum], | |
657 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
658 | else | |
659 | *val = 0; | |
660 | return status; | |
28fc6740 AC |
661 | } |
662 | ||
c00dcbe9 MK |
663 | void |
664 | regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) | |
665 | { | |
666 | void *buf; | |
123f5f96 | 667 | |
c00dcbe9 MK |
668 | gdb_assert (regcache != NULL); |
669 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
670 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
671 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], |
672 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
c00dcbe9 MK |
673 | regcache_raw_write (regcache, regnum, buf); |
674 | } | |
675 | ||
676 | void | |
677 | regcache_raw_write_unsigned (struct regcache *regcache, int regnum, | |
678 | ULONGEST val) | |
679 | { | |
680 | void *buf; | |
123f5f96 | 681 | |
c00dcbe9 MK |
682 | gdb_assert (regcache != NULL); |
683 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
684 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
685 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], |
686 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
c00dcbe9 MK |
687 | regcache_raw_write (regcache, regnum, buf); |
688 | } | |
689 | ||
05d1431c | 690 | enum register_status |
2d522557 | 691 | regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
68365089 | 692 | { |
d138e37a | 693 | gdb_assert (regnum >= 0); |
68365089 AC |
694 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
695 | if (regnum < regcache->descr->nr_raw_registers) | |
05d1431c | 696 | return regcache_raw_read (regcache, regnum, buf); |
2d28509a | 697 | else if (regcache->readonly_p |
05d1431c PA |
698 | && regcache->register_status[regnum] != REG_UNKNOWN) |
699 | { | |
700 | /* Read-only register cache, perhaps the cooked value was | |
701 | cached? */ | |
702 | struct gdbarch *gdbarch = regcache->descr->gdbarch; | |
703 | ||
704 | if (regcache->register_status[regnum] == REG_VALID) | |
705 | memcpy (buf, register_buffer (regcache, regnum), | |
706 | regcache->descr->sizeof_register[regnum]); | |
707 | else | |
708 | memset (buf, 0, regcache->descr->sizeof_register[regnum]); | |
709 | ||
710 | return regcache->register_status[regnum]; | |
711 | } | |
3543a589 TT |
712 | else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch)) |
713 | { | |
714 | struct value *mark, *computed; | |
715 | enum register_status result = REG_VALID; | |
716 | ||
717 | mark = value_mark (); | |
718 | ||
719 | computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch, | |
720 | regcache, regnum); | |
721 | if (value_entirely_available (computed)) | |
722 | memcpy (buf, value_contents_raw (computed), | |
723 | regcache->descr->sizeof_register[regnum]); | |
724 | else | |
725 | { | |
726 | memset (buf, 0, regcache->descr->sizeof_register[regnum]); | |
727 | result = REG_UNAVAILABLE; | |
728 | } | |
729 | ||
730 | value_free_to_mark (mark); | |
731 | ||
732 | return result; | |
733 | } | |
d138e37a | 734 | else |
05d1431c PA |
735 | return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache, |
736 | regnum, buf); | |
61a0eb5b AC |
737 | } |
738 | ||
3543a589 TT |
739 | struct value * |
740 | regcache_cooked_read_value (struct regcache *regcache, int regnum) | |
741 | { | |
742 | gdb_assert (regnum >= 0); | |
743 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); | |
744 | ||
745 | if (regnum < regcache->descr->nr_raw_registers | |
746 | || (regcache->readonly_p | |
747 | && regcache->register_status[regnum] != REG_UNKNOWN) | |
748 | || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch)) | |
749 | { | |
750 | struct value *result; | |
751 | ||
752 | result = allocate_value (register_type (regcache->descr->gdbarch, | |
753 | regnum)); | |
754 | VALUE_LVAL (result) = lval_register; | |
755 | VALUE_REGNUM (result) = regnum; | |
756 | ||
757 | /* It is more efficient in general to do this delegation in this | |
758 | direction than in the other one, even though the value-based | |
759 | API is preferred. */ | |
760 | if (regcache_cooked_read (regcache, regnum, | |
761 | value_contents_raw (result)) == REG_UNAVAILABLE) | |
762 | mark_value_bytes_unavailable (result, 0, | |
763 | TYPE_LENGTH (value_type (result))); | |
764 | ||
765 | return result; | |
766 | } | |
767 | else | |
768 | return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch, | |
769 | regcache, regnum); | |
770 | } | |
771 | ||
05d1431c | 772 | enum register_status |
a378f419 AC |
773 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, |
774 | LONGEST *val) | |
775 | { | |
05d1431c | 776 | enum register_status status; |
2d522557 | 777 | gdb_byte *buf; |
123f5f96 | 778 | |
a378f419 | 779 | gdb_assert (regcache != NULL); |
a66a9c23 | 780 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 | 781 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
05d1431c PA |
782 | status = regcache_cooked_read (regcache, regnum, buf); |
783 | if (status == REG_VALID) | |
784 | *val = extract_signed_integer | |
785 | (buf, regcache->descr->sizeof_register[regnum], | |
786 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
787 | else | |
788 | *val = 0; | |
789 | return status; | |
a378f419 AC |
790 | } |
791 | ||
05d1431c | 792 | enum register_status |
a378f419 AC |
793 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, |
794 | ULONGEST *val) | |
795 | { | |
05d1431c | 796 | enum register_status status; |
2d522557 | 797 | gdb_byte *buf; |
123f5f96 | 798 | |
a378f419 | 799 | gdb_assert (regcache != NULL); |
a66a9c23 | 800 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 | 801 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
05d1431c PA |
802 | status = regcache_cooked_read (regcache, regnum, buf); |
803 | if (status == REG_VALID) | |
804 | *val = extract_unsigned_integer | |
805 | (buf, regcache->descr->sizeof_register[regnum], | |
806 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
807 | else | |
808 | *val = 0; | |
809 | return status; | |
a378f419 AC |
810 | } |
811 | ||
a66a9c23 AC |
812 | void |
813 | regcache_cooked_write_signed (struct regcache *regcache, int regnum, | |
814 | LONGEST val) | |
815 | { | |
816 | void *buf; | |
123f5f96 | 817 | |
a66a9c23 AC |
818 | gdb_assert (regcache != NULL); |
819 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
820 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
821 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], |
822 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
a66a9c23 AC |
823 | regcache_cooked_write (regcache, regnum, buf); |
824 | } | |
825 | ||
826 | void | |
827 | regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, | |
828 | ULONGEST val) | |
829 | { | |
830 | void *buf; | |
123f5f96 | 831 | |
a66a9c23 AC |
832 | gdb_assert (regcache != NULL); |
833 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
834 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
835 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], |
836 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
a66a9c23 AC |
837 | regcache_cooked_write (regcache, regnum, buf); |
838 | } | |
839 | ||
61a0eb5b | 840 | void |
2d522557 AC |
841 | regcache_raw_write (struct regcache *regcache, int regnum, |
842 | const gdb_byte *buf) | |
61a0eb5b | 843 | { |
594f7785 UW |
844 | struct cleanup *old_chain; |
845 | ||
3fadccb3 AC |
846 | gdb_assert (regcache != NULL && buf != NULL); |
847 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
2d28509a | 848 | gdb_assert (!regcache->readonly_p); |
3fadccb3 | 849 | |
3fadccb3 AC |
850 | /* On the sparc, writing %g0 is a no-op, so we don't even want to |
851 | change the registers array if something writes to this register. */ | |
214e098a | 852 | if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum)) |
3fadccb3 AC |
853 | return; |
854 | ||
3fadccb3 | 855 | /* If we have a valid copy of the register, and new value == old |
0df8b418 | 856 | value, then don't bother doing the actual store. */ |
ee99023e | 857 | if (regcache_register_status (regcache, regnum) == REG_VALID |
3fadccb3 AC |
858 | && (memcmp (register_buffer (regcache, regnum), buf, |
859 | regcache->descr->sizeof_register[regnum]) == 0)) | |
860 | return; | |
861 | ||
594f7785 UW |
862 | old_chain = save_inferior_ptid (); |
863 | inferior_ptid = regcache->ptid; | |
864 | ||
316f2060 | 865 | target_prepare_to_store (regcache); |
3fadccb3 AC |
866 | memcpy (register_buffer (regcache, regnum), buf, |
867 | regcache->descr->sizeof_register[regnum]); | |
ee99023e | 868 | regcache->register_status[regnum] = REG_VALID; |
56be3814 | 869 | target_store_registers (regcache, regnum); |
594f7785 UW |
870 | |
871 | do_cleanups (old_chain); | |
61a0eb5b AC |
872 | } |
873 | ||
68365089 | 874 | void |
2d522557 AC |
875 | regcache_cooked_write (struct regcache *regcache, int regnum, |
876 | const gdb_byte *buf) | |
68365089 | 877 | { |
d138e37a | 878 | gdb_assert (regnum >= 0); |
68365089 AC |
879 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
880 | if (regnum < regcache->descr->nr_raw_registers) | |
881 | regcache_raw_write (regcache, regnum, buf); | |
d138e37a | 882 | else |
68365089 | 883 | gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache, |
d8124050 | 884 | regnum, buf); |
61a0eb5b AC |
885 | } |
886 | ||
06c0b04e AC |
887 | /* Perform a partial register transfer using a read, modify, write |
888 | operation. */ | |
889 | ||
890 | typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum, | |
891 | void *buf); | |
892 | typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum, | |
893 | const void *buf); | |
894 | ||
05d1431c | 895 | static enum register_status |
06c0b04e AC |
896 | regcache_xfer_part (struct regcache *regcache, int regnum, |
897 | int offset, int len, void *in, const void *out, | |
05d1431c PA |
898 | enum register_status (*read) (struct regcache *regcache, |
899 | int regnum, | |
900 | gdb_byte *buf), | |
2d522557 AC |
901 | void (*write) (struct regcache *regcache, int regnum, |
902 | const gdb_byte *buf)) | |
06c0b04e AC |
903 | { |
904 | struct regcache_descr *descr = regcache->descr; | |
fc1a4b47 | 905 | gdb_byte reg[MAX_REGISTER_SIZE]; |
123f5f96 | 906 | |
06c0b04e AC |
907 | gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]); |
908 | gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]); | |
909 | /* Something to do? */ | |
910 | if (offset + len == 0) | |
05d1431c | 911 | return REG_VALID; |
0df8b418 | 912 | /* Read (when needed) ... */ |
06c0b04e AC |
913 | if (in != NULL |
914 | || offset > 0 | |
915 | || offset + len < descr->sizeof_register[regnum]) | |
916 | { | |
05d1431c PA |
917 | enum register_status status; |
918 | ||
06c0b04e | 919 | gdb_assert (read != NULL); |
05d1431c PA |
920 | status = read (regcache, regnum, reg); |
921 | if (status != REG_VALID) | |
922 | return status; | |
06c0b04e | 923 | } |
0df8b418 | 924 | /* ... modify ... */ |
06c0b04e AC |
925 | if (in != NULL) |
926 | memcpy (in, reg + offset, len); | |
927 | if (out != NULL) | |
928 | memcpy (reg + offset, out, len); | |
929 | /* ... write (when needed). */ | |
930 | if (out != NULL) | |
931 | { | |
932 | gdb_assert (write != NULL); | |
933 | write (regcache, regnum, reg); | |
934 | } | |
05d1431c PA |
935 | |
936 | return REG_VALID; | |
06c0b04e AC |
937 | } |
938 | ||
05d1431c | 939 | enum register_status |
06c0b04e | 940 | regcache_raw_read_part (struct regcache *regcache, int regnum, |
2d522557 | 941 | int offset, int len, gdb_byte *buf) |
06c0b04e AC |
942 | { |
943 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 944 | |
06c0b04e | 945 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); |
05d1431c PA |
946 | return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, |
947 | regcache_raw_read, regcache_raw_write); | |
06c0b04e AC |
948 | } |
949 | ||
950 | void | |
951 | regcache_raw_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 952 | int offset, int len, const gdb_byte *buf) |
06c0b04e AC |
953 | { |
954 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 955 | |
06c0b04e AC |
956 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); |
957 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
958 | regcache_raw_read, regcache_raw_write); | |
959 | } | |
960 | ||
05d1431c | 961 | enum register_status |
06c0b04e | 962 | regcache_cooked_read_part (struct regcache *regcache, int regnum, |
2d522557 | 963 | int offset, int len, gdb_byte *buf) |
06c0b04e AC |
964 | { |
965 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 966 | |
06c0b04e | 967 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
05d1431c PA |
968 | return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, |
969 | regcache_cooked_read, regcache_cooked_write); | |
06c0b04e AC |
970 | } |
971 | ||
972 | void | |
973 | regcache_cooked_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 974 | int offset, int len, const gdb_byte *buf) |
06c0b04e AC |
975 | { |
976 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 977 | |
06c0b04e AC |
978 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
979 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
980 | regcache_cooked_read, regcache_cooked_write); | |
981 | } | |
32178cab | 982 | |
a16d75cc | 983 | /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */ |
9a661b68 MK |
984 | |
985 | void | |
6618125d | 986 | regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf) |
9a661b68 MK |
987 | { |
988 | void *regbuf; | |
989 | size_t size; | |
990 | ||
a16d75cc | 991 | gdb_assert (regcache != NULL); |
9a661b68 MK |
992 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); |
993 | gdb_assert (!regcache->readonly_p); | |
994 | ||
9a661b68 MK |
995 | regbuf = register_buffer (regcache, regnum); |
996 | size = regcache->descr->sizeof_register[regnum]; | |
997 | ||
998 | if (buf) | |
ee99023e PA |
999 | { |
1000 | memcpy (regbuf, buf, size); | |
1001 | regcache->register_status[regnum] = REG_VALID; | |
1002 | } | |
9a661b68 | 1003 | else |
ee99023e PA |
1004 | { |
1005 | /* This memset not strictly necessary, but better than garbage | |
1006 | in case the register value manages to escape somewhere (due | |
1007 | to a bug, no less). */ | |
1008 | memset (regbuf, 0, size); | |
1009 | regcache->register_status[regnum] = REG_UNAVAILABLE; | |
1010 | } | |
9a661b68 MK |
1011 | } |
1012 | ||
1013 | /* Collect register REGNUM from REGCACHE and store its contents in BUF. */ | |
1014 | ||
1015 | void | |
6618125d | 1016 | regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf) |
9a661b68 MK |
1017 | { |
1018 | const void *regbuf; | |
1019 | size_t size; | |
1020 | ||
1021 | gdb_assert (regcache != NULL && buf != NULL); | |
1022 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
1023 | ||
1024 | regbuf = register_buffer (regcache, regnum); | |
1025 | size = regcache->descr->sizeof_register[regnum]; | |
1026 | memcpy (buf, regbuf, size); | |
1027 | } | |
1028 | ||
193cb69f | 1029 | |
515630c5 | 1030 | /* Special handling for register PC. */ |
32178cab MS |
1031 | |
1032 | CORE_ADDR | |
515630c5 | 1033 | regcache_read_pc (struct regcache *regcache) |
32178cab | 1034 | { |
61a1198a UW |
1035 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
1036 | ||
32178cab MS |
1037 | CORE_ADDR pc_val; |
1038 | ||
61a1198a UW |
1039 | if (gdbarch_read_pc_p (gdbarch)) |
1040 | pc_val = gdbarch_read_pc (gdbarch, regcache); | |
cde9ea48 | 1041 | /* Else use per-frame method on get_current_frame. */ |
214e098a | 1042 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
cde9ea48 | 1043 | { |
61a1198a | 1044 | ULONGEST raw_val; |
123f5f96 | 1045 | |
05d1431c PA |
1046 | if (regcache_cooked_read_unsigned (regcache, |
1047 | gdbarch_pc_regnum (gdbarch), | |
1048 | &raw_val) == REG_UNAVAILABLE) | |
1049 | throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available")); | |
1050 | ||
214e098a | 1051 | pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val); |
cde9ea48 AC |
1052 | } |
1053 | else | |
515630c5 UW |
1054 | internal_error (__FILE__, __LINE__, |
1055 | _("regcache_read_pc: Unable to find PC")); | |
32178cab MS |
1056 | return pc_val; |
1057 | } | |
1058 | ||
32178cab | 1059 | void |
515630c5 | 1060 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) |
32178cab | 1061 | { |
61a1198a UW |
1062 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
1063 | ||
61a1198a UW |
1064 | if (gdbarch_write_pc_p (gdbarch)) |
1065 | gdbarch_write_pc (gdbarch, regcache, pc); | |
214e098a | 1066 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
3e8c568d | 1067 | regcache_cooked_write_unsigned (regcache, |
214e098a | 1068 | gdbarch_pc_regnum (gdbarch), pc); |
61a1198a UW |
1069 | else |
1070 | internal_error (__FILE__, __LINE__, | |
515630c5 | 1071 | _("regcache_write_pc: Unable to update PC")); |
edb3359d DJ |
1072 | |
1073 | /* Writing the PC (for instance, from "load") invalidates the | |
1074 | current frame. */ | |
1075 | reinit_frame_cache (); | |
32178cab MS |
1076 | } |
1077 | ||
32178cab | 1078 | |
705152c5 MS |
1079 | static void |
1080 | reg_flush_command (char *command, int from_tty) | |
1081 | { | |
1082 | /* Force-flush the register cache. */ | |
1083 | registers_changed (); | |
1084 | if (from_tty) | |
a3f17187 | 1085 | printf_filtered (_("Register cache flushed.\n")); |
705152c5 MS |
1086 | } |
1087 | ||
af030b9a AC |
1088 | static void |
1089 | dump_endian_bytes (struct ui_file *file, enum bfd_endian endian, | |
1090 | const unsigned char *buf, long len) | |
1091 | { | |
1092 | int i; | |
123f5f96 | 1093 | |
af030b9a AC |
1094 | switch (endian) |
1095 | { | |
1096 | case BFD_ENDIAN_BIG: | |
1097 | for (i = 0; i < len; i++) | |
1098 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1099 | break; | |
1100 | case BFD_ENDIAN_LITTLE: | |
1101 | for (i = len - 1; i >= 0; i--) | |
1102 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1103 | break; | |
1104 | default: | |
e2e0b3e5 | 1105 | internal_error (__FILE__, __LINE__, _("Bad switch")); |
af030b9a AC |
1106 | } |
1107 | } | |
1108 | ||
1109 | enum regcache_dump_what | |
1110 | { | |
3e43a32a | 1111 | regcache_dump_none, regcache_dump_raw, |
c21236dc PA |
1112 | regcache_dump_cooked, regcache_dump_groups, |
1113 | regcache_dump_remote | |
af030b9a AC |
1114 | }; |
1115 | ||
1116 | static void | |
1117 | regcache_dump (struct regcache *regcache, struct ui_file *file, | |
1118 | enum regcache_dump_what what_to_dump) | |
1119 | { | |
1120 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); | |
b59ff9d5 | 1121 | struct gdbarch *gdbarch = regcache->descr->gdbarch; |
af030b9a AC |
1122 | int regnum; |
1123 | int footnote_nr = 0; | |
1124 | int footnote_register_size = 0; | |
1125 | int footnote_register_offset = 0; | |
1126 | int footnote_register_type_name_null = 0; | |
1127 | long register_offset = 0; | |
123a958e | 1128 | unsigned char buf[MAX_REGISTER_SIZE]; |
af030b9a AC |
1129 | |
1130 | #if 0 | |
af030b9a AC |
1131 | fprintf_unfiltered (file, "nr_raw_registers %d\n", |
1132 | regcache->descr->nr_raw_registers); | |
1133 | fprintf_unfiltered (file, "nr_cooked_registers %d\n", | |
1134 | regcache->descr->nr_cooked_registers); | |
1135 | fprintf_unfiltered (file, "sizeof_raw_registers %ld\n", | |
1136 | regcache->descr->sizeof_raw_registers); | |
ee99023e PA |
1137 | fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n", |
1138 | regcache->descr->sizeof_raw_register_status); | |
f57d151a | 1139 | fprintf_unfiltered (file, "gdbarch_num_regs %d\n", |
214e098a | 1140 | gdbarch_num_regs (gdbarch)); |
f57d151a | 1141 | fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n", |
214e098a | 1142 | gdbarch_num_pseudo_regs (gdbarch)); |
af030b9a AC |
1143 | #endif |
1144 | ||
1145 | gdb_assert (regcache->descr->nr_cooked_registers | |
214e098a UW |
1146 | == (gdbarch_num_regs (gdbarch) |
1147 | + gdbarch_num_pseudo_regs (gdbarch))); | |
af030b9a AC |
1148 | |
1149 | for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++) | |
1150 | { | |
1151 | /* Name. */ | |
1152 | if (regnum < 0) | |
1153 | fprintf_unfiltered (file, " %-10s", "Name"); | |
1154 | else | |
1155 | { | |
214e098a | 1156 | const char *p = gdbarch_register_name (gdbarch, regnum); |
123f5f96 | 1157 | |
af030b9a AC |
1158 | if (p == NULL) |
1159 | p = ""; | |
1160 | else if (p[0] == '\0') | |
1161 | p = "''"; | |
1162 | fprintf_unfiltered (file, " %-10s", p); | |
1163 | } | |
1164 | ||
1165 | /* Number. */ | |
1166 | if (regnum < 0) | |
1167 | fprintf_unfiltered (file, " %4s", "Nr"); | |
1168 | else | |
1169 | fprintf_unfiltered (file, " %4d", regnum); | |
1170 | ||
1171 | /* Relative number. */ | |
1172 | if (regnum < 0) | |
1173 | fprintf_unfiltered (file, " %4s", "Rel"); | |
214e098a | 1174 | else if (regnum < gdbarch_num_regs (gdbarch)) |
af030b9a AC |
1175 | fprintf_unfiltered (file, " %4d", regnum); |
1176 | else | |
f57d151a | 1177 | fprintf_unfiltered (file, " %4d", |
214e098a | 1178 | (regnum - gdbarch_num_regs (gdbarch))); |
af030b9a AC |
1179 | |
1180 | /* Offset. */ | |
1181 | if (regnum < 0) | |
1182 | fprintf_unfiltered (file, " %6s ", "Offset"); | |
1183 | else | |
1184 | { | |
1185 | fprintf_unfiltered (file, " %6ld", | |
1186 | regcache->descr->register_offset[regnum]); | |
a7e3c2ad | 1187 | if (register_offset != regcache->descr->register_offset[regnum] |
d3b22ed5 AC |
1188 | || (regnum > 0 |
1189 | && (regcache->descr->register_offset[regnum] | |
1190 | != (regcache->descr->register_offset[regnum - 1] | |
1191 | + regcache->descr->sizeof_register[regnum - 1]))) | |
1192 | ) | |
af030b9a AC |
1193 | { |
1194 | if (!footnote_register_offset) | |
1195 | footnote_register_offset = ++footnote_nr; | |
1196 | fprintf_unfiltered (file, "*%d", footnote_register_offset); | |
1197 | } | |
1198 | else | |
1199 | fprintf_unfiltered (file, " "); | |
1200 | register_offset = (regcache->descr->register_offset[regnum] | |
1201 | + regcache->descr->sizeof_register[regnum]); | |
1202 | } | |
1203 | ||
1204 | /* Size. */ | |
1205 | if (regnum < 0) | |
1206 | fprintf_unfiltered (file, " %5s ", "Size"); | |
1207 | else | |
01e1877c AC |
1208 | fprintf_unfiltered (file, " %5ld", |
1209 | regcache->descr->sizeof_register[regnum]); | |
af030b9a AC |
1210 | |
1211 | /* Type. */ | |
b59ff9d5 AC |
1212 | { |
1213 | const char *t; | |
123f5f96 | 1214 | |
b59ff9d5 AC |
1215 | if (regnum < 0) |
1216 | t = "Type"; | |
1217 | else | |
1218 | { | |
1219 | static const char blt[] = "builtin_type"; | |
123f5f96 | 1220 | |
b59ff9d5 AC |
1221 | t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum)); |
1222 | if (t == NULL) | |
1223 | { | |
1224 | char *n; | |
123f5f96 | 1225 | |
b59ff9d5 AC |
1226 | if (!footnote_register_type_name_null) |
1227 | footnote_register_type_name_null = ++footnote_nr; | |
b435e160 | 1228 | n = xstrprintf ("*%d", footnote_register_type_name_null); |
b59ff9d5 AC |
1229 | make_cleanup (xfree, n); |
1230 | t = n; | |
1231 | } | |
1232 | /* Chop a leading builtin_type. */ | |
1233 | if (strncmp (t, blt, strlen (blt)) == 0) | |
1234 | t += strlen (blt); | |
1235 | } | |
1236 | fprintf_unfiltered (file, " %-15s", t); | |
1237 | } | |
1238 | ||
1239 | /* Leading space always present. */ | |
1240 | fprintf_unfiltered (file, " "); | |
af030b9a AC |
1241 | |
1242 | /* Value, raw. */ | |
1243 | if (what_to_dump == regcache_dump_raw) | |
1244 | { | |
1245 | if (regnum < 0) | |
1246 | fprintf_unfiltered (file, "Raw value"); | |
1247 | else if (regnum >= regcache->descr->nr_raw_registers) | |
1248 | fprintf_unfiltered (file, "<cooked>"); | |
ee99023e | 1249 | else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN) |
af030b9a | 1250 | fprintf_unfiltered (file, "<invalid>"); |
ee99023e PA |
1251 | else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE) |
1252 | fprintf_unfiltered (file, "<unavailable>"); | |
af030b9a AC |
1253 | else |
1254 | { | |
1255 | regcache_raw_read (regcache, regnum, buf); | |
1256 | fprintf_unfiltered (file, "0x"); | |
0d20ae72 | 1257 | dump_endian_bytes (file, |
214e098a | 1258 | gdbarch_byte_order (gdbarch), buf, |
01e1877c | 1259 | regcache->descr->sizeof_register[regnum]); |
af030b9a AC |
1260 | } |
1261 | } | |
1262 | ||
1263 | /* Value, cooked. */ | |
1264 | if (what_to_dump == regcache_dump_cooked) | |
1265 | { | |
1266 | if (regnum < 0) | |
1267 | fprintf_unfiltered (file, "Cooked value"); | |
1268 | else | |
1269 | { | |
05d1431c PA |
1270 | enum register_status status; |
1271 | ||
1272 | status = regcache_cooked_read (regcache, regnum, buf); | |
1273 | if (status == REG_UNKNOWN) | |
1274 | fprintf_unfiltered (file, "<invalid>"); | |
1275 | else if (status == REG_UNAVAILABLE) | |
1276 | fprintf_unfiltered (file, "<unavailable>"); | |
1277 | else | |
1278 | { | |
1279 | fprintf_unfiltered (file, "0x"); | |
1280 | dump_endian_bytes (file, | |
1281 | gdbarch_byte_order (gdbarch), buf, | |
1282 | regcache->descr->sizeof_register[regnum]); | |
1283 | } | |
af030b9a AC |
1284 | } |
1285 | } | |
1286 | ||
b59ff9d5 AC |
1287 | /* Group members. */ |
1288 | if (what_to_dump == regcache_dump_groups) | |
1289 | { | |
1290 | if (regnum < 0) | |
1291 | fprintf_unfiltered (file, "Groups"); | |
1292 | else | |
1293 | { | |
b59ff9d5 | 1294 | const char *sep = ""; |
6c7d17ba | 1295 | struct reggroup *group; |
123f5f96 | 1296 | |
6c7d17ba AC |
1297 | for (group = reggroup_next (gdbarch, NULL); |
1298 | group != NULL; | |
1299 | group = reggroup_next (gdbarch, group)) | |
b59ff9d5 | 1300 | { |
6c7d17ba | 1301 | if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) |
b59ff9d5 | 1302 | { |
3e43a32a MS |
1303 | fprintf_unfiltered (file, |
1304 | "%s%s", sep, reggroup_name (group)); | |
b59ff9d5 AC |
1305 | sep = ","; |
1306 | } | |
1307 | } | |
1308 | } | |
1309 | } | |
1310 | ||
c21236dc PA |
1311 | /* Remote packet configuration. */ |
1312 | if (what_to_dump == regcache_dump_remote) | |
1313 | { | |
1314 | if (regnum < 0) | |
1315 | { | |
1316 | fprintf_unfiltered (file, "Rmt Nr g/G Offset"); | |
1317 | } | |
1318 | else if (regnum < regcache->descr->nr_raw_registers) | |
1319 | { | |
1320 | int pnum, poffset; | |
1321 | ||
1322 | if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum, | |
1323 | &pnum, &poffset)) | |
1324 | fprintf_unfiltered (file, "%7d %11d", pnum, poffset); | |
1325 | } | |
1326 | } | |
1327 | ||
af030b9a AC |
1328 | fprintf_unfiltered (file, "\n"); |
1329 | } | |
1330 | ||
1331 | if (footnote_register_size) | |
1332 | fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n", | |
1333 | footnote_register_size); | |
1334 | if (footnote_register_offset) | |
1335 | fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n", | |
1336 | footnote_register_offset); | |
1337 | if (footnote_register_type_name_null) | |
1338 | fprintf_unfiltered (file, | |
1339 | "*%d: Register type's name NULL.\n", | |
1340 | footnote_register_type_name_null); | |
1341 | do_cleanups (cleanups); | |
1342 | } | |
1343 | ||
1344 | static void | |
1345 | regcache_print (char *args, enum regcache_dump_what what_to_dump) | |
1346 | { | |
1347 | if (args == NULL) | |
28c38f10 | 1348 | regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump); |
af030b9a AC |
1349 | else |
1350 | { | |
724b958c | 1351 | struct cleanup *cleanups; |
af030b9a | 1352 | struct ui_file *file = gdb_fopen (args, "w"); |
123f5f96 | 1353 | |
af030b9a | 1354 | if (file == NULL) |
e2e0b3e5 | 1355 | perror_with_name (_("maintenance print architecture")); |
724b958c | 1356 | cleanups = make_cleanup_ui_file_delete (file); |
28c38f10 | 1357 | regcache_dump (get_current_regcache (), file, what_to_dump); |
724b958c | 1358 | do_cleanups (cleanups); |
af030b9a AC |
1359 | } |
1360 | } | |
1361 | ||
1362 | static void | |
1363 | maintenance_print_registers (char *args, int from_tty) | |
1364 | { | |
1365 | regcache_print (args, regcache_dump_none); | |
1366 | } | |
1367 | ||
1368 | static void | |
1369 | maintenance_print_raw_registers (char *args, int from_tty) | |
1370 | { | |
1371 | regcache_print (args, regcache_dump_raw); | |
1372 | } | |
1373 | ||
1374 | static void | |
1375 | maintenance_print_cooked_registers (char *args, int from_tty) | |
1376 | { | |
1377 | regcache_print (args, regcache_dump_cooked); | |
1378 | } | |
1379 | ||
b59ff9d5 AC |
1380 | static void |
1381 | maintenance_print_register_groups (char *args, int from_tty) | |
1382 | { | |
1383 | regcache_print (args, regcache_dump_groups); | |
1384 | } | |
1385 | ||
c21236dc PA |
1386 | static void |
1387 | maintenance_print_remote_registers (char *args, int from_tty) | |
1388 | { | |
1389 | regcache_print (args, regcache_dump_remote); | |
1390 | } | |
1391 | ||
b9362cc7 AC |
1392 | extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */ |
1393 | ||
32178cab MS |
1394 | void |
1395 | _initialize_regcache (void) | |
1396 | { | |
3e43a32a MS |
1397 | regcache_descr_handle |
1398 | = gdbarch_data_register_post_init (init_regcache_descr); | |
705152c5 | 1399 | |
f4c5303c | 1400 | observer_attach_target_changed (regcache_observer_target_changed); |
5231c1fd | 1401 | observer_attach_thread_ptid_changed (regcache_thread_ptid_changed); |
f4c5303c | 1402 | |
705152c5 | 1403 | add_com ("flushregs", class_maintenance, reg_flush_command, |
1bedd215 | 1404 | _("Force gdb to flush its register cache (maintainer command)")); |
39f77062 | 1405 | |
3e43a32a MS |
1406 | add_cmd ("registers", class_maintenance, maintenance_print_registers, |
1407 | _("Print the internal register configuration.\n" | |
1408 | "Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1409 | add_cmd ("raw-registers", class_maintenance, |
3e43a32a MS |
1410 | maintenance_print_raw_registers, |
1411 | _("Print the internal register configuration " | |
1412 | "including raw values.\n" | |
1413 | "Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1414 | add_cmd ("cooked-registers", class_maintenance, |
3e43a32a MS |
1415 | maintenance_print_cooked_registers, |
1416 | _("Print the internal register configuration " | |
1417 | "including cooked values.\n" | |
1418 | "Takes an optional file parameter."), &maintenanceprintlist); | |
b59ff9d5 | 1419 | add_cmd ("register-groups", class_maintenance, |
3e43a32a MS |
1420 | maintenance_print_register_groups, |
1421 | _("Print the internal register configuration " | |
1422 | "including each register's group.\n" | |
1423 | "Takes an optional file parameter."), | |
af030b9a | 1424 | &maintenanceprintlist); |
c21236dc PA |
1425 | add_cmd ("remote-registers", class_maintenance, |
1426 | maintenance_print_remote_registers, _("\ | |
1427 | Print the internal register configuration including each register's\n\ | |
1428 | remote register number and buffer offset in the g/G packets.\n\ | |
1429 | Takes an optional file parameter."), | |
1430 | &maintenanceprintlist); | |
af030b9a | 1431 | |
32178cab | 1432 | } |