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, |
4c38e0a4 | 4 | 2002, 2004, 2007, 2008, 2009, 2010 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" |
32178cab MS |
32 | |
33 | /* | |
34 | * DATA STRUCTURE | |
35 | * | |
36 | * Here is the actual register cache. | |
37 | */ | |
38 | ||
3fadccb3 AC |
39 | /* Per-architecture object describing the layout of a register cache. |
40 | Computed once when the architecture is created */ | |
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; | |
56 | long sizeof_raw_register_valid_p; | |
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 AC |
65 | long sizeof_cooked_registers; |
66 | long sizeof_cooked_register_valid_p; | |
d138e37a AC |
67 | |
68 | /* Offset and size (in 8 bit bytes), of reach register in the | |
69 | register cache. All registers (including those in the range | |
70 | [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset. | |
71 | Assigning all registers an offset makes it possible to keep | |
72 | legacy code, such as that found in read_register_bytes() and | |
73 | write_register_bytes() working. */ | |
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); | |
97 | descr->sizeof_cooked_register_valid_p = gdbarch_num_regs (gdbarch) | |
f57d151a | 98 | + gdbarch_num_pseudo_regs |
214e098a | 99 | (gdbarch); |
3fadccb3 | 100 | |
bb425013 | 101 | /* Fill in a table of register types. */ |
116f06ea AC |
102 | descr->register_type |
103 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, 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); |
bb1db049 AC |
110 | |
111 | /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p | |
112 | array. This pretects GDB from erant code that accesses elements | |
f57d151a UW |
113 | of the global register_valid_p[] array in the range |
114 | [gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs). */ | |
bb1db049 AC |
115 | descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p; |
116 | ||
067df2e5 | 117 | /* Lay out the register cache. |
3fadccb3 | 118 | |
bb425013 AC |
119 | NOTE: cagney/2002-05-22: Only register_type() is used when |
120 | constructing the register cache. It is assumed that the | |
121 | register's raw size, virtual size and type length are all the | |
122 | same. */ | |
3fadccb3 AC |
123 | |
124 | { | |
125 | long offset = 0; | |
123f5f96 | 126 | |
116f06ea AC |
127 | descr->sizeof_register |
128 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
129 | descr->register_offset | |
130 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
d138e37a | 131 | for (i = 0; i < descr->nr_cooked_registers; i++) |
3fadccb3 | 132 | { |
bb425013 | 133 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); |
3fadccb3 AC |
134 | descr->register_offset[i] = offset; |
135 | offset += descr->sizeof_register[i]; | |
123a958e | 136 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); |
3fadccb3 AC |
137 | } |
138 | /* Set the real size of the register cache buffer. */ | |
067df2e5 | 139 | descr->sizeof_cooked_registers = offset; |
3fadccb3 AC |
140 | } |
141 | ||
067df2e5 | 142 | /* FIXME: cagney/2002-05-22: Should only need to allocate space for |
ce2826aa | 143 | the raw registers. Unfortunately some code still accesses the |
067df2e5 AC |
144 | register array directly using the global registers[]. Until that |
145 | code has been purged, play safe and over allocating the register | |
146 | buffer. Ulgh! */ | |
147 | descr->sizeof_raw_registers = descr->sizeof_cooked_registers; | |
148 | ||
3fadccb3 AC |
149 | return descr; |
150 | } | |
151 | ||
152 | static struct regcache_descr * | |
153 | regcache_descr (struct gdbarch *gdbarch) | |
154 | { | |
155 | return gdbarch_data (gdbarch, regcache_descr_handle); | |
156 | } | |
157 | ||
bb425013 AC |
158 | /* Utility functions returning useful register attributes stored in |
159 | the regcache descr. */ | |
160 | ||
161 | struct type * | |
162 | register_type (struct gdbarch *gdbarch, int regnum) | |
163 | { | |
164 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
123f5f96 | 165 | |
bb425013 AC |
166 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
167 | return descr->register_type[regnum]; | |
168 | } | |
169 | ||
0ed04cce AC |
170 | /* Utility functions returning useful register attributes stored in |
171 | the regcache descr. */ | |
172 | ||
08a617da AC |
173 | int |
174 | register_size (struct gdbarch *gdbarch, int regnum) | |
175 | { | |
176 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
177 | int size; | |
123f5f96 | 178 | |
f57d151a | 179 | gdb_assert (regnum >= 0 |
214e098a UW |
180 | && regnum < (gdbarch_num_regs (gdbarch) |
181 | + gdbarch_num_pseudo_regs (gdbarch))); | |
08a617da | 182 | size = descr->sizeof_register[regnum]; |
08a617da AC |
183 | return size; |
184 | } | |
185 | ||
3fadccb3 AC |
186 | /* The register cache for storing raw register values. */ |
187 | ||
188 | struct regcache | |
189 | { | |
190 | struct regcache_descr *descr; | |
6c95b8df PA |
191 | |
192 | /* The address space of this register cache (for registers where it | |
193 | makes sense, like PC or SP). */ | |
194 | struct address_space *aspace; | |
195 | ||
51b1fe4e | 196 | /* The register buffers. A read-only register cache can hold the |
f57d151a UW |
197 | full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write |
198 | register cache can only hold [0 .. gdbarch_num_regs). */ | |
2d522557 | 199 | gdb_byte *registers; |
b05e64e5 FR |
200 | /* Register cache status: |
201 | register_valid_p[REG] == 0 if REG value is not in the cache | |
202 | > 0 if REG value is in the cache | |
203 | < 0 if REG value is permanently unavailable */ | |
204 | signed char *register_valid_p; | |
2d28509a AC |
205 | /* Is this a read-only cache? A read-only cache is used for saving |
206 | the target's register state (e.g, across an inferior function | |
207 | call or just before forcing a function return). A read-only | |
208 | cache can only be updated via the methods regcache_dup() and | |
209 | regcache_cpy(). The actual contents are determined by the | |
210 | reggroup_save and reggroup_restore methods. */ | |
211 | int readonly_p; | |
594f7785 UW |
212 | /* If this is a read-write cache, which thread's registers is |
213 | it connected to? */ | |
214 | ptid_t ptid; | |
3fadccb3 AC |
215 | }; |
216 | ||
217 | struct regcache * | |
d37346f0 | 218 | regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace) |
3fadccb3 AC |
219 | { |
220 | struct regcache_descr *descr; | |
221 | struct regcache *regcache; | |
123f5f96 | 222 | |
3fadccb3 AC |
223 | gdb_assert (gdbarch != NULL); |
224 | descr = regcache_descr (gdbarch); | |
225 | regcache = XMALLOC (struct regcache); | |
226 | regcache->descr = descr; | |
51b1fe4e | 227 | regcache->registers |
2d522557 | 228 | = XCALLOC (descr->sizeof_raw_registers, gdb_byte); |
51b1fe4e | 229 | regcache->register_valid_p |
2d522557 | 230 | = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte); |
d37346f0 | 231 | regcache->aspace = aspace; |
2d28509a | 232 | regcache->readonly_p = 1; |
594f7785 | 233 | regcache->ptid = minus_one_ptid; |
3fadccb3 AC |
234 | return regcache; |
235 | } | |
236 | ||
237 | void | |
238 | regcache_xfree (struct regcache *regcache) | |
239 | { | |
240 | if (regcache == NULL) | |
241 | return; | |
51b1fe4e AC |
242 | xfree (regcache->registers); |
243 | xfree (regcache->register_valid_p); | |
3fadccb3 AC |
244 | xfree (regcache); |
245 | } | |
246 | ||
b9362cc7 | 247 | static void |
36160dc4 AC |
248 | do_regcache_xfree (void *data) |
249 | { | |
250 | regcache_xfree (data); | |
251 | } | |
252 | ||
253 | struct cleanup * | |
254 | make_cleanup_regcache_xfree (struct regcache *regcache) | |
255 | { | |
256 | return make_cleanup (do_regcache_xfree, regcache); | |
257 | } | |
258 | ||
41d35cb0 MK |
259 | /* Return REGCACHE's architecture. */ |
260 | ||
261 | struct gdbarch * | |
262 | get_regcache_arch (const struct regcache *regcache) | |
263 | { | |
264 | return regcache->descr->gdbarch; | |
265 | } | |
266 | ||
6c95b8df PA |
267 | struct address_space * |
268 | get_regcache_aspace (const struct regcache *regcache) | |
269 | { | |
270 | return regcache->aspace; | |
271 | } | |
272 | ||
51b1fe4e AC |
273 | /* Return a pointer to register REGNUM's buffer cache. */ |
274 | ||
2d522557 | 275 | static gdb_byte * |
9a661b68 | 276 | register_buffer (const struct regcache *regcache, int regnum) |
51b1fe4e AC |
277 | { |
278 | return regcache->registers + regcache->descr->register_offset[regnum]; | |
279 | } | |
280 | ||
2d28509a | 281 | void |
5602984a AC |
282 | regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read, |
283 | void *src) | |
2d28509a AC |
284 | { |
285 | struct gdbarch *gdbarch = dst->descr->gdbarch; | |
2d522557 | 286 | gdb_byte buf[MAX_REGISTER_SIZE]; |
2d28509a | 287 | int regnum; |
123f5f96 | 288 | |
2d28509a | 289 | /* The DST should be `read-only', if it wasn't then the save would |
5602984a | 290 | end up trying to write the register values back out to the |
2d28509a | 291 | target. */ |
2d28509a AC |
292 | gdb_assert (dst->readonly_p); |
293 | /* Clear the dest. */ | |
294 | memset (dst->registers, 0, dst->descr->sizeof_cooked_registers); | |
295 | memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p); | |
296 | /* Copy over any registers (identified by their membership in the | |
f57d151a UW |
297 | save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs + |
298 | gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a | 299 | to save/restore `cooked' registers that live in memory. */ |
2d28509a AC |
300 | for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++) |
301 | { | |
302 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
303 | { | |
5602984a | 304 | int valid = cooked_read (src, regnum, buf); |
123f5f96 | 305 | |
5602984a AC |
306 | if (valid) |
307 | { | |
308 | memcpy (register_buffer (dst, regnum), buf, | |
309 | register_size (gdbarch, regnum)); | |
310 | dst->register_valid_p[regnum] = 1; | |
311 | } | |
2d28509a AC |
312 | } |
313 | } | |
314 | } | |
315 | ||
316 | void | |
5602984a AC |
317 | regcache_restore (struct regcache *dst, |
318 | regcache_cooked_read_ftype *cooked_read, | |
2d522557 | 319 | void *cooked_read_context) |
2d28509a AC |
320 | { |
321 | struct gdbarch *gdbarch = dst->descr->gdbarch; | |
2d522557 | 322 | gdb_byte buf[MAX_REGISTER_SIZE]; |
2d28509a | 323 | int regnum; |
123f5f96 | 324 | |
5602984a AC |
325 | /* The dst had better not be read-only. If it is, the `restore' |
326 | doesn't make much sense. */ | |
2d28509a | 327 | gdb_assert (!dst->readonly_p); |
2d28509a | 328 | /* Copy over any registers, being careful to only restore those that |
f57d151a UW |
329 | were both saved and need to be restored. The full [0 .. gdbarch_num_regs |
330 | + gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a AC |
331 | to save/restore `cooked' registers that live in memory. */ |
332 | for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++) | |
2d28509a | 333 | { |
5602984a | 334 | if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)) |
2d28509a | 335 | { |
2d522557 | 336 | int valid = cooked_read (cooked_read_context, regnum, buf); |
123f5f96 | 337 | |
5602984a AC |
338 | if (valid) |
339 | regcache_cooked_write (dst, regnum, buf); | |
2d28509a AC |
340 | } |
341 | } | |
342 | } | |
343 | ||
5602984a | 344 | static int |
2d522557 | 345 | do_cooked_read (void *src, int regnum, gdb_byte *buf) |
5602984a AC |
346 | { |
347 | struct regcache *regcache = src; | |
123f5f96 | 348 | |
6f4e5a41 | 349 | if (!regcache->register_valid_p[regnum] && regcache->readonly_p) |
5602984a AC |
350 | /* Don't even think about fetching a register from a read-only |
351 | cache when the register isn't yet valid. There isn't a target | |
352 | from which the register value can be fetched. */ | |
353 | return 0; | |
354 | regcache_cooked_read (regcache, regnum, buf); | |
355 | return 1; | |
356 | } | |
357 | ||
358 | ||
3fadccb3 AC |
359 | void |
360 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
361 | { | |
3fadccb3 AC |
362 | gdb_assert (src != NULL && dst != NULL); |
363 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
364 | gdb_assert (src != dst); | |
2d28509a | 365 | gdb_assert (src->readonly_p || dst->readonly_p); |
6c95b8df | 366 | |
2d28509a | 367 | if (!src->readonly_p) |
5602984a | 368 | regcache_save (dst, do_cooked_read, src); |
2d28509a | 369 | else if (!dst->readonly_p) |
5602984a | 370 | regcache_restore (dst, do_cooked_read, src); |
2d28509a AC |
371 | else |
372 | regcache_cpy_no_passthrough (dst, src); | |
3fadccb3 AC |
373 | } |
374 | ||
375 | void | |
376 | regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src) | |
377 | { | |
3fadccb3 AC |
378 | gdb_assert (src != NULL && dst != NULL); |
379 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
380 | /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough | |
594f7785 | 381 | move of data into the current regcache. Doing this would be |
9564ee9f | 382 | silly - it would mean that valid_p would be completely invalid. */ |
594f7785 | 383 | gdb_assert (dst->readonly_p); |
6c95b8df | 384 | |
51b1fe4e AC |
385 | memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers); |
386 | memcpy (dst->register_valid_p, src->register_valid_p, | |
3fadccb3 AC |
387 | dst->descr->sizeof_raw_register_valid_p); |
388 | } | |
389 | ||
390 | struct regcache * | |
391 | regcache_dup (struct regcache *src) | |
392 | { | |
393 | struct regcache *newbuf; | |
123f5f96 | 394 | |
d37346f0 | 395 | newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src)); |
3fadccb3 AC |
396 | regcache_cpy (newbuf, src); |
397 | return newbuf; | |
398 | } | |
399 | ||
400 | struct regcache * | |
401 | regcache_dup_no_passthrough (struct regcache *src) | |
402 | { | |
403 | struct regcache *newbuf; | |
123f5f96 | 404 | |
d37346f0 | 405 | newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src)); |
3fadccb3 AC |
406 | regcache_cpy_no_passthrough (newbuf, src); |
407 | return newbuf; | |
408 | } | |
409 | ||
410 | int | |
6ed7ea50 | 411 | regcache_valid_p (const struct regcache *regcache, int regnum) |
3fadccb3 AC |
412 | { |
413 | gdb_assert (regcache != NULL); | |
6ed7ea50 UW |
414 | gdb_assert (regnum >= 0); |
415 | if (regcache->readonly_p) | |
416 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); | |
417 | else | |
418 | gdb_assert (regnum < regcache->descr->nr_raw_registers); | |
419 | ||
51b1fe4e | 420 | return regcache->register_valid_p[regnum]; |
3fadccb3 AC |
421 | } |
422 | ||
9c5ea4d9 UW |
423 | void |
424 | regcache_invalidate (struct regcache *regcache, int regnum) | |
425 | { | |
426 | gdb_assert (regcache != NULL); | |
427 | gdb_assert (regnum >= 0); | |
428 | gdb_assert (!regcache->readonly_p); | |
429 | gdb_assert (regnum < regcache->descr->nr_raw_registers); | |
430 | regcache->register_valid_p[regnum] = 0; | |
431 | } | |
432 | ||
433 | ||
3fadccb3 | 434 | /* Global structure containing the current regcache. */ |
3fadccb3 | 435 | |
5ebd2499 | 436 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
32178cab MS |
437 | recording if the register values have been changed (eg. by the |
438 | user). Therefore all registers must be written back to the | |
439 | target when appropriate. */ | |
440 | ||
c2250ad1 | 441 | struct regcache_list |
594f7785 | 442 | { |
c2250ad1 UW |
443 | struct regcache *regcache; |
444 | struct regcache_list *next; | |
445 | }; | |
446 | ||
447 | static struct regcache_list *current_regcache; | |
448 | ||
449 | struct regcache * | |
450 | get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch) | |
451 | { | |
452 | struct regcache_list *list; | |
453 | struct regcache *new_regcache; | |
594f7785 | 454 | |
c2250ad1 UW |
455 | for (list = current_regcache; list; list = list->next) |
456 | if (ptid_equal (list->regcache->ptid, ptid) | |
457 | && get_regcache_arch (list->regcache) == gdbarch) | |
458 | return list->regcache; | |
594f7785 | 459 | |
d37346f0 DJ |
460 | new_regcache = regcache_xmalloc (gdbarch, |
461 | target_thread_address_space (ptid)); | |
c2250ad1 UW |
462 | new_regcache->readonly_p = 0; |
463 | new_regcache->ptid = ptid; | |
6c95b8df | 464 | gdb_assert (new_regcache->aspace != NULL); |
594f7785 | 465 | |
c2250ad1 UW |
466 | list = xmalloc (sizeof (struct regcache_list)); |
467 | list->regcache = new_regcache; | |
468 | list->next = current_regcache; | |
469 | current_regcache = list; | |
594f7785 | 470 | |
c2250ad1 | 471 | return new_regcache; |
594f7785 UW |
472 | } |
473 | ||
c2250ad1 UW |
474 | static ptid_t current_thread_ptid; |
475 | static struct gdbarch *current_thread_arch; | |
476 | ||
477 | struct regcache * | |
478 | get_thread_regcache (ptid_t ptid) | |
479 | { | |
480 | if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid)) | |
481 | { | |
482 | current_thread_ptid = ptid; | |
483 | current_thread_arch = target_thread_architecture (ptid); | |
484 | } | |
485 | ||
486 | return get_thread_arch_regcache (ptid, current_thread_arch); | |
487 | } | |
488 | ||
489 | struct regcache * | |
490 | get_current_regcache (void) | |
594f7785 UW |
491 | { |
492 | return get_thread_regcache (inferior_ptid); | |
493 | } | |
32178cab | 494 | |
32178cab | 495 | |
f4c5303c OF |
496 | /* Observer for the target_changed event. */ |
497 | ||
2c0b251b | 498 | static void |
f4c5303c OF |
499 | regcache_observer_target_changed (struct target_ops *target) |
500 | { | |
501 | registers_changed (); | |
502 | } | |
503 | ||
5231c1fd PA |
504 | /* Update global variables old ptids to hold NEW_PTID if they were |
505 | holding OLD_PTID. */ | |
506 | static void | |
507 | regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) | |
508 | { | |
c2250ad1 UW |
509 | struct regcache_list *list; |
510 | ||
511 | for (list = current_regcache; list; list = list->next) | |
512 | if (ptid_equal (list->regcache->ptid, old_ptid)) | |
513 | list->regcache->ptid = new_ptid; | |
5231c1fd PA |
514 | } |
515 | ||
32178cab MS |
516 | /* Low level examining and depositing of registers. |
517 | ||
518 | The caller is responsible for making sure that the inferior is | |
519 | stopped before calling the fetching routines, or it will get | |
520 | garbage. (a change from GDB version 3, in which the caller got the | |
521 | value from the last stop). */ | |
522 | ||
523 | /* REGISTERS_CHANGED () | |
524 | ||
525 | Indicate that registers may have changed, so invalidate the cache. */ | |
526 | ||
527 | void | |
e66408ed | 528 | registers_changed_ptid (ptid_t ptid) |
32178cab | 529 | { |
e66408ed | 530 | struct regcache_list *list, **list_link; |
c2250ad1 | 531 | |
e66408ed PA |
532 | list = current_regcache; |
533 | list_link = ¤t_regcache; | |
534 | while (list) | |
c2250ad1 | 535 | { |
e66408ed PA |
536 | if (ptid_match (list->regcache->ptid, ptid)) |
537 | { | |
538 | struct regcache_list *dead = list; | |
539 | ||
540 | *list_link = list->next; | |
541 | regcache_xfree (list->regcache); | |
542 | list = *list_link; | |
543 | xfree (dead); | |
544 | continue; | |
545 | } | |
546 | ||
547 | list_link = &list->next; | |
548 | list = *list_link; | |
c2250ad1 | 549 | } |
32178cab | 550 | |
594f7785 | 551 | current_regcache = NULL; |
32178cab | 552 | |
c2250ad1 UW |
553 | current_thread_ptid = null_ptid; |
554 | current_thread_arch = NULL; | |
555 | ||
a5d9d57d DJ |
556 | /* Need to forget about any frames we have cached, too. */ |
557 | reinit_frame_cache (); | |
558 | ||
32178cab MS |
559 | /* Force cleanup of any alloca areas if using C alloca instead of |
560 | a builtin alloca. This particular call is used to clean up | |
561 | areas allocated by low level target code which may build up | |
562 | during lengthy interactions between gdb and the target before | |
563 | gdb gives control to the user (ie watchpoints). */ | |
564 | alloca (0); | |
32178cab MS |
565 | } |
566 | ||
e66408ed PA |
567 | void |
568 | registers_changed (void) | |
569 | { | |
570 | registers_changed_ptid (minus_one_ptid); | |
571 | } | |
32178cab | 572 | |
61a0eb5b | 573 | void |
2d522557 | 574 | regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
61a0eb5b | 575 | { |
3fadccb3 AC |
576 | gdb_assert (regcache != NULL && buf != NULL); |
577 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
3fadccb3 AC |
578 | /* Make certain that the register cache is up-to-date with respect |
579 | to the current thread. This switching shouldn't be necessary | |
580 | only there is still only one target side register cache. Sigh! | |
581 | On the bright side, at least there is a regcache object. */ | |
2d28509a | 582 | if (!regcache->readonly_p) |
3fadccb3 | 583 | { |
594f7785 | 584 | if (!regcache_valid_p (regcache, regnum)) |
3fadccb3 | 585 | { |
594f7785 | 586 | struct cleanup *old_chain = save_inferior_ptid (); |
123f5f96 | 587 | |
594f7785 UW |
588 | inferior_ptid = regcache->ptid; |
589 | target_fetch_registers (regcache, regnum); | |
590 | do_cleanups (old_chain); | |
3fadccb3 | 591 | } |
0a8146bf AC |
592 | #if 0 |
593 | /* FIXME: cagney/2004-08-07: At present a number of targets | |
04c663e3 DA |
594 | forget (or didn't know that they needed) to set this leading to |
595 | panics. Also is the problem that targets need to indicate | |
0a8146bf AC |
596 | that a register is in one of the possible states: valid, |
597 | undefined, unknown. The last of which isn't yet | |
598 | possible. */ | |
9c5ea4d9 | 599 | gdb_assert (regcache_valid_p (regcache, regnum)); |
0a8146bf | 600 | #endif |
3fadccb3 AC |
601 | } |
602 | /* Copy the value directly into the register cache. */ | |
51b1fe4e | 603 | memcpy (buf, register_buffer (regcache, regnum), |
3fadccb3 | 604 | regcache->descr->sizeof_register[regnum]); |
61a0eb5b AC |
605 | } |
606 | ||
28fc6740 AC |
607 | void |
608 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) | |
609 | { | |
2d522557 | 610 | gdb_byte *buf; |
123f5f96 | 611 | |
28fc6740 AC |
612 | gdb_assert (regcache != NULL); |
613 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
614 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
615 | regcache_raw_read (regcache, regnum, buf); | |
e17a4113 UW |
616 | (*val) = extract_signed_integer |
617 | (buf, regcache->descr->sizeof_register[regnum], | |
618 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
28fc6740 AC |
619 | } |
620 | ||
621 | void | |
622 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, | |
623 | ULONGEST *val) | |
624 | { | |
2d522557 | 625 | gdb_byte *buf; |
123f5f96 | 626 | |
28fc6740 AC |
627 | gdb_assert (regcache != NULL); |
628 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
629 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
630 | regcache_raw_read (regcache, regnum, buf); | |
e17a4113 UW |
631 | (*val) = extract_unsigned_integer |
632 | (buf, regcache->descr->sizeof_register[regnum], | |
633 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
28fc6740 AC |
634 | } |
635 | ||
c00dcbe9 MK |
636 | void |
637 | regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) | |
638 | { | |
639 | void *buf; | |
123f5f96 | 640 | |
c00dcbe9 MK |
641 | gdb_assert (regcache != NULL); |
642 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
643 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
644 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], |
645 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
c00dcbe9 MK |
646 | regcache_raw_write (regcache, regnum, buf); |
647 | } | |
648 | ||
649 | void | |
650 | regcache_raw_write_unsigned (struct regcache *regcache, int regnum, | |
651 | ULONGEST val) | |
652 | { | |
653 | void *buf; | |
123f5f96 | 654 | |
c00dcbe9 MK |
655 | gdb_assert (regcache != NULL); |
656 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
657 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
658 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], |
659 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
c00dcbe9 MK |
660 | regcache_raw_write (regcache, regnum, buf); |
661 | } | |
662 | ||
68365089 | 663 | void |
2d522557 | 664 | regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
68365089 | 665 | { |
d138e37a | 666 | gdb_assert (regnum >= 0); |
68365089 AC |
667 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
668 | if (regnum < regcache->descr->nr_raw_registers) | |
669 | regcache_raw_read (regcache, regnum, buf); | |
2d28509a AC |
670 | else if (regcache->readonly_p |
671 | && regnum < regcache->descr->nr_cooked_registers | |
672 | && regcache->register_valid_p[regnum]) | |
b2fa5097 | 673 | /* Read-only register cache, perhaps the cooked value was cached? */ |
2d28509a AC |
674 | memcpy (buf, register_buffer (regcache, regnum), |
675 | regcache->descr->sizeof_register[regnum]); | |
d138e37a | 676 | else |
68365089 AC |
677 | gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache, |
678 | regnum, buf); | |
61a0eb5b AC |
679 | } |
680 | ||
a378f419 AC |
681 | void |
682 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, | |
683 | LONGEST *val) | |
684 | { | |
2d522557 | 685 | gdb_byte *buf; |
123f5f96 | 686 | |
a378f419 | 687 | gdb_assert (regcache != NULL); |
a66a9c23 | 688 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 AC |
689 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
690 | regcache_cooked_read (regcache, regnum, buf); | |
e17a4113 UW |
691 | (*val) = extract_signed_integer |
692 | (buf, regcache->descr->sizeof_register[regnum], | |
693 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
a378f419 AC |
694 | } |
695 | ||
696 | void | |
697 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, | |
698 | ULONGEST *val) | |
699 | { | |
2d522557 | 700 | gdb_byte *buf; |
123f5f96 | 701 | |
a378f419 | 702 | gdb_assert (regcache != NULL); |
a66a9c23 | 703 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 AC |
704 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
705 | regcache_cooked_read (regcache, regnum, buf); | |
e17a4113 UW |
706 | (*val) = extract_unsigned_integer |
707 | (buf, regcache->descr->sizeof_register[regnum], | |
708 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
a378f419 AC |
709 | } |
710 | ||
a66a9c23 AC |
711 | void |
712 | regcache_cooked_write_signed (struct regcache *regcache, int regnum, | |
713 | LONGEST val) | |
714 | { | |
715 | void *buf; | |
123f5f96 | 716 | |
a66a9c23 AC |
717 | gdb_assert (regcache != NULL); |
718 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
719 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
720 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], |
721 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
a66a9c23 AC |
722 | regcache_cooked_write (regcache, regnum, buf); |
723 | } | |
724 | ||
725 | void | |
726 | regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, | |
727 | ULONGEST val) | |
728 | { | |
729 | void *buf; | |
123f5f96 | 730 | |
a66a9c23 AC |
731 | gdb_assert (regcache != NULL); |
732 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
733 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
734 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], |
735 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
a66a9c23 AC |
736 | regcache_cooked_write (regcache, regnum, buf); |
737 | } | |
738 | ||
61a0eb5b | 739 | void |
2d522557 AC |
740 | regcache_raw_write (struct regcache *regcache, int regnum, |
741 | const gdb_byte *buf) | |
61a0eb5b | 742 | { |
594f7785 UW |
743 | struct cleanup *old_chain; |
744 | ||
3fadccb3 AC |
745 | gdb_assert (regcache != NULL && buf != NULL); |
746 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
2d28509a | 747 | gdb_assert (!regcache->readonly_p); |
3fadccb3 | 748 | |
3fadccb3 AC |
749 | /* On the sparc, writing %g0 is a no-op, so we don't even want to |
750 | change the registers array if something writes to this register. */ | |
214e098a | 751 | if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum)) |
3fadccb3 AC |
752 | return; |
753 | ||
3fadccb3 AC |
754 | /* If we have a valid copy of the register, and new value == old |
755 | value, then don't bother doing the actual store. */ | |
756 | if (regcache_valid_p (regcache, regnum) | |
757 | && (memcmp (register_buffer (regcache, regnum), buf, | |
758 | regcache->descr->sizeof_register[regnum]) == 0)) | |
759 | return; | |
760 | ||
594f7785 UW |
761 | old_chain = save_inferior_ptid (); |
762 | inferior_ptid = regcache->ptid; | |
763 | ||
316f2060 | 764 | target_prepare_to_store (regcache); |
3fadccb3 AC |
765 | memcpy (register_buffer (regcache, regnum), buf, |
766 | regcache->descr->sizeof_register[regnum]); | |
51b1fe4e | 767 | regcache->register_valid_p[regnum] = 1; |
56be3814 | 768 | target_store_registers (regcache, regnum); |
594f7785 UW |
769 | |
770 | do_cleanups (old_chain); | |
61a0eb5b AC |
771 | } |
772 | ||
68365089 | 773 | void |
2d522557 AC |
774 | regcache_cooked_write (struct regcache *regcache, int regnum, |
775 | const gdb_byte *buf) | |
68365089 | 776 | { |
d138e37a | 777 | gdb_assert (regnum >= 0); |
68365089 AC |
778 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
779 | if (regnum < regcache->descr->nr_raw_registers) | |
780 | regcache_raw_write (regcache, regnum, buf); | |
d138e37a | 781 | else |
68365089 | 782 | gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache, |
d8124050 | 783 | regnum, buf); |
61a0eb5b AC |
784 | } |
785 | ||
06c0b04e AC |
786 | /* Perform a partial register transfer using a read, modify, write |
787 | operation. */ | |
788 | ||
789 | typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum, | |
790 | void *buf); | |
791 | typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum, | |
792 | const void *buf); | |
793 | ||
b9362cc7 | 794 | static void |
06c0b04e AC |
795 | regcache_xfer_part (struct regcache *regcache, int regnum, |
796 | int offset, int len, void *in, const void *out, | |
2d522557 AC |
797 | void (*read) (struct regcache *regcache, int regnum, |
798 | gdb_byte *buf), | |
799 | void (*write) (struct regcache *regcache, int regnum, | |
800 | const gdb_byte *buf)) | |
06c0b04e AC |
801 | { |
802 | struct regcache_descr *descr = regcache->descr; | |
fc1a4b47 | 803 | gdb_byte reg[MAX_REGISTER_SIZE]; |
123f5f96 | 804 | |
06c0b04e AC |
805 | gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]); |
806 | gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]); | |
807 | /* Something to do? */ | |
808 | if (offset + len == 0) | |
809 | return; | |
810 | /* Read (when needed) ... */ | |
811 | if (in != NULL | |
812 | || offset > 0 | |
813 | || offset + len < descr->sizeof_register[regnum]) | |
814 | { | |
815 | gdb_assert (read != NULL); | |
816 | read (regcache, regnum, reg); | |
817 | } | |
818 | /* ... modify ... */ | |
819 | if (in != NULL) | |
820 | memcpy (in, reg + offset, len); | |
821 | if (out != NULL) | |
822 | memcpy (reg + offset, out, len); | |
823 | /* ... write (when needed). */ | |
824 | if (out != NULL) | |
825 | { | |
826 | gdb_assert (write != NULL); | |
827 | write (regcache, regnum, reg); | |
828 | } | |
829 | } | |
830 | ||
831 | void | |
832 | regcache_raw_read_part (struct regcache *regcache, int regnum, | |
2d522557 | 833 | int offset, int len, gdb_byte *buf) |
06c0b04e AC |
834 | { |
835 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 836 | |
06c0b04e AC |
837 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); |
838 | regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, | |
839 | regcache_raw_read, regcache_raw_write); | |
840 | } | |
841 | ||
842 | void | |
843 | regcache_raw_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 844 | int offset, int len, const gdb_byte *buf) |
06c0b04e AC |
845 | { |
846 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 847 | |
06c0b04e AC |
848 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); |
849 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
850 | regcache_raw_read, regcache_raw_write); | |
851 | } | |
852 | ||
853 | void | |
854 | regcache_cooked_read_part (struct regcache *regcache, int regnum, | |
2d522557 | 855 | int offset, int len, gdb_byte *buf) |
06c0b04e AC |
856 | { |
857 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 858 | |
06c0b04e AC |
859 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
860 | regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, | |
861 | regcache_cooked_read, regcache_cooked_write); | |
862 | } | |
863 | ||
864 | void | |
865 | regcache_cooked_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 866 | int offset, int len, const gdb_byte *buf) |
06c0b04e AC |
867 | { |
868 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 869 | |
06c0b04e AC |
870 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
871 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
872 | regcache_cooked_read, regcache_cooked_write); | |
873 | } | |
32178cab | 874 | |
a16d75cc | 875 | /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */ |
9a661b68 MK |
876 | |
877 | void | |
6618125d | 878 | regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf) |
9a661b68 MK |
879 | { |
880 | void *regbuf; | |
881 | size_t size; | |
882 | ||
a16d75cc | 883 | gdb_assert (regcache != NULL); |
9a661b68 MK |
884 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); |
885 | gdb_assert (!regcache->readonly_p); | |
886 | ||
9a661b68 MK |
887 | regbuf = register_buffer (regcache, regnum); |
888 | size = regcache->descr->sizeof_register[regnum]; | |
889 | ||
890 | if (buf) | |
891 | memcpy (regbuf, buf, size); | |
892 | else | |
893 | memset (regbuf, 0, size); | |
894 | ||
895 | /* Mark the register as cached. */ | |
896 | regcache->register_valid_p[regnum] = 1; | |
897 | } | |
898 | ||
899 | /* Collect register REGNUM from REGCACHE and store its contents in BUF. */ | |
900 | ||
901 | void | |
6618125d | 902 | regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf) |
9a661b68 MK |
903 | { |
904 | const void *regbuf; | |
905 | size_t size; | |
906 | ||
907 | gdb_assert (regcache != NULL && buf != NULL); | |
908 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
909 | ||
910 | regbuf = register_buffer (regcache, regnum); | |
911 | size = regcache->descr->sizeof_register[regnum]; | |
912 | memcpy (buf, regbuf, size); | |
913 | } | |
914 | ||
193cb69f | 915 | |
515630c5 | 916 | /* Special handling for register PC. */ |
32178cab MS |
917 | |
918 | CORE_ADDR | |
515630c5 | 919 | regcache_read_pc (struct regcache *regcache) |
32178cab | 920 | { |
61a1198a UW |
921 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
922 | ||
32178cab MS |
923 | CORE_ADDR pc_val; |
924 | ||
61a1198a UW |
925 | if (gdbarch_read_pc_p (gdbarch)) |
926 | pc_val = gdbarch_read_pc (gdbarch, regcache); | |
cde9ea48 | 927 | /* Else use per-frame method on get_current_frame. */ |
214e098a | 928 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
cde9ea48 | 929 | { |
61a1198a | 930 | ULONGEST raw_val; |
123f5f96 | 931 | |
3e8c568d | 932 | regcache_cooked_read_unsigned (regcache, |
214e098a | 933 | gdbarch_pc_regnum (gdbarch), |
3e8c568d | 934 | &raw_val); |
214e098a | 935 | pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val); |
cde9ea48 AC |
936 | } |
937 | else | |
515630c5 UW |
938 | internal_error (__FILE__, __LINE__, |
939 | _("regcache_read_pc: Unable to find PC")); | |
32178cab MS |
940 | return pc_val; |
941 | } | |
942 | ||
32178cab | 943 | void |
515630c5 | 944 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) |
32178cab | 945 | { |
61a1198a UW |
946 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
947 | ||
61a1198a UW |
948 | if (gdbarch_write_pc_p (gdbarch)) |
949 | gdbarch_write_pc (gdbarch, regcache, pc); | |
214e098a | 950 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
3e8c568d | 951 | regcache_cooked_write_unsigned (regcache, |
214e098a | 952 | gdbarch_pc_regnum (gdbarch), pc); |
61a1198a UW |
953 | else |
954 | internal_error (__FILE__, __LINE__, | |
515630c5 | 955 | _("regcache_write_pc: Unable to update PC")); |
edb3359d DJ |
956 | |
957 | /* Writing the PC (for instance, from "load") invalidates the | |
958 | current frame. */ | |
959 | reinit_frame_cache (); | |
32178cab MS |
960 | } |
961 | ||
32178cab | 962 | |
705152c5 MS |
963 | static void |
964 | reg_flush_command (char *command, int from_tty) | |
965 | { | |
966 | /* Force-flush the register cache. */ | |
967 | registers_changed (); | |
968 | if (from_tty) | |
a3f17187 | 969 | printf_filtered (_("Register cache flushed.\n")); |
705152c5 MS |
970 | } |
971 | ||
af030b9a AC |
972 | static void |
973 | dump_endian_bytes (struct ui_file *file, enum bfd_endian endian, | |
974 | const unsigned char *buf, long len) | |
975 | { | |
976 | int i; | |
123f5f96 | 977 | |
af030b9a AC |
978 | switch (endian) |
979 | { | |
980 | case BFD_ENDIAN_BIG: | |
981 | for (i = 0; i < len; i++) | |
982 | fprintf_unfiltered (file, "%02x", buf[i]); | |
983 | break; | |
984 | case BFD_ENDIAN_LITTLE: | |
985 | for (i = len - 1; i >= 0; i--) | |
986 | fprintf_unfiltered (file, "%02x", buf[i]); | |
987 | break; | |
988 | default: | |
e2e0b3e5 | 989 | internal_error (__FILE__, __LINE__, _("Bad switch")); |
af030b9a AC |
990 | } |
991 | } | |
992 | ||
993 | enum regcache_dump_what | |
994 | { | |
b59ff9d5 | 995 | regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups |
af030b9a AC |
996 | }; |
997 | ||
998 | static void | |
999 | regcache_dump (struct regcache *regcache, struct ui_file *file, | |
1000 | enum regcache_dump_what what_to_dump) | |
1001 | { | |
1002 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); | |
b59ff9d5 | 1003 | struct gdbarch *gdbarch = regcache->descr->gdbarch; |
af030b9a AC |
1004 | int regnum; |
1005 | int footnote_nr = 0; | |
1006 | int footnote_register_size = 0; | |
1007 | int footnote_register_offset = 0; | |
1008 | int footnote_register_type_name_null = 0; | |
1009 | long register_offset = 0; | |
123a958e | 1010 | unsigned char buf[MAX_REGISTER_SIZE]; |
af030b9a AC |
1011 | |
1012 | #if 0 | |
af030b9a AC |
1013 | fprintf_unfiltered (file, "nr_raw_registers %d\n", |
1014 | regcache->descr->nr_raw_registers); | |
1015 | fprintf_unfiltered (file, "nr_cooked_registers %d\n", | |
1016 | regcache->descr->nr_cooked_registers); | |
1017 | fprintf_unfiltered (file, "sizeof_raw_registers %ld\n", | |
1018 | regcache->descr->sizeof_raw_registers); | |
1019 | fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n", | |
1020 | regcache->descr->sizeof_raw_register_valid_p); | |
f57d151a | 1021 | fprintf_unfiltered (file, "gdbarch_num_regs %d\n", |
214e098a | 1022 | gdbarch_num_regs (gdbarch)); |
f57d151a | 1023 | fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n", |
214e098a | 1024 | gdbarch_num_pseudo_regs (gdbarch)); |
af030b9a AC |
1025 | #endif |
1026 | ||
1027 | gdb_assert (regcache->descr->nr_cooked_registers | |
214e098a UW |
1028 | == (gdbarch_num_regs (gdbarch) |
1029 | + gdbarch_num_pseudo_regs (gdbarch))); | |
af030b9a AC |
1030 | |
1031 | for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++) | |
1032 | { | |
1033 | /* Name. */ | |
1034 | if (regnum < 0) | |
1035 | fprintf_unfiltered (file, " %-10s", "Name"); | |
1036 | else | |
1037 | { | |
214e098a | 1038 | const char *p = gdbarch_register_name (gdbarch, regnum); |
123f5f96 | 1039 | |
af030b9a AC |
1040 | if (p == NULL) |
1041 | p = ""; | |
1042 | else if (p[0] == '\0') | |
1043 | p = "''"; | |
1044 | fprintf_unfiltered (file, " %-10s", p); | |
1045 | } | |
1046 | ||
1047 | /* Number. */ | |
1048 | if (regnum < 0) | |
1049 | fprintf_unfiltered (file, " %4s", "Nr"); | |
1050 | else | |
1051 | fprintf_unfiltered (file, " %4d", regnum); | |
1052 | ||
1053 | /* Relative number. */ | |
1054 | if (regnum < 0) | |
1055 | fprintf_unfiltered (file, " %4s", "Rel"); | |
214e098a | 1056 | else if (regnum < gdbarch_num_regs (gdbarch)) |
af030b9a AC |
1057 | fprintf_unfiltered (file, " %4d", regnum); |
1058 | else | |
f57d151a | 1059 | fprintf_unfiltered (file, " %4d", |
214e098a | 1060 | (regnum - gdbarch_num_regs (gdbarch))); |
af030b9a AC |
1061 | |
1062 | /* Offset. */ | |
1063 | if (regnum < 0) | |
1064 | fprintf_unfiltered (file, " %6s ", "Offset"); | |
1065 | else | |
1066 | { | |
1067 | fprintf_unfiltered (file, " %6ld", | |
1068 | regcache->descr->register_offset[regnum]); | |
a7e3c2ad | 1069 | if (register_offset != regcache->descr->register_offset[regnum] |
d3b22ed5 AC |
1070 | || (regnum > 0 |
1071 | && (regcache->descr->register_offset[regnum] | |
1072 | != (regcache->descr->register_offset[regnum - 1] | |
1073 | + regcache->descr->sizeof_register[regnum - 1]))) | |
1074 | ) | |
af030b9a AC |
1075 | { |
1076 | if (!footnote_register_offset) | |
1077 | footnote_register_offset = ++footnote_nr; | |
1078 | fprintf_unfiltered (file, "*%d", footnote_register_offset); | |
1079 | } | |
1080 | else | |
1081 | fprintf_unfiltered (file, " "); | |
1082 | register_offset = (regcache->descr->register_offset[regnum] | |
1083 | + regcache->descr->sizeof_register[regnum]); | |
1084 | } | |
1085 | ||
1086 | /* Size. */ | |
1087 | if (regnum < 0) | |
1088 | fprintf_unfiltered (file, " %5s ", "Size"); | |
1089 | else | |
01e1877c AC |
1090 | fprintf_unfiltered (file, " %5ld", |
1091 | regcache->descr->sizeof_register[regnum]); | |
af030b9a AC |
1092 | |
1093 | /* Type. */ | |
b59ff9d5 AC |
1094 | { |
1095 | const char *t; | |
123f5f96 | 1096 | |
b59ff9d5 AC |
1097 | if (regnum < 0) |
1098 | t = "Type"; | |
1099 | else | |
1100 | { | |
1101 | static const char blt[] = "builtin_type"; | |
123f5f96 | 1102 | |
b59ff9d5 AC |
1103 | t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum)); |
1104 | if (t == NULL) | |
1105 | { | |
1106 | char *n; | |
123f5f96 | 1107 | |
b59ff9d5 AC |
1108 | if (!footnote_register_type_name_null) |
1109 | footnote_register_type_name_null = ++footnote_nr; | |
b435e160 | 1110 | n = xstrprintf ("*%d", footnote_register_type_name_null); |
b59ff9d5 AC |
1111 | make_cleanup (xfree, n); |
1112 | t = n; | |
1113 | } | |
1114 | /* Chop a leading builtin_type. */ | |
1115 | if (strncmp (t, blt, strlen (blt)) == 0) | |
1116 | t += strlen (blt); | |
1117 | } | |
1118 | fprintf_unfiltered (file, " %-15s", t); | |
1119 | } | |
1120 | ||
1121 | /* Leading space always present. */ | |
1122 | fprintf_unfiltered (file, " "); | |
af030b9a AC |
1123 | |
1124 | /* Value, raw. */ | |
1125 | if (what_to_dump == regcache_dump_raw) | |
1126 | { | |
1127 | if (regnum < 0) | |
1128 | fprintf_unfiltered (file, "Raw value"); | |
1129 | else if (regnum >= regcache->descr->nr_raw_registers) | |
1130 | fprintf_unfiltered (file, "<cooked>"); | |
1131 | else if (!regcache_valid_p (regcache, regnum)) | |
1132 | fprintf_unfiltered (file, "<invalid>"); | |
1133 | else | |
1134 | { | |
1135 | regcache_raw_read (regcache, regnum, buf); | |
1136 | fprintf_unfiltered (file, "0x"); | |
0d20ae72 | 1137 | dump_endian_bytes (file, |
214e098a | 1138 | gdbarch_byte_order (gdbarch), buf, |
01e1877c | 1139 | regcache->descr->sizeof_register[regnum]); |
af030b9a AC |
1140 | } |
1141 | } | |
1142 | ||
1143 | /* Value, cooked. */ | |
1144 | if (what_to_dump == regcache_dump_cooked) | |
1145 | { | |
1146 | if (regnum < 0) | |
1147 | fprintf_unfiltered (file, "Cooked value"); | |
1148 | else | |
1149 | { | |
1150 | regcache_cooked_read (regcache, regnum, buf); | |
1151 | fprintf_unfiltered (file, "0x"); | |
0d20ae72 | 1152 | dump_endian_bytes (file, |
214e098a | 1153 | gdbarch_byte_order (gdbarch), buf, |
01e1877c | 1154 | regcache->descr->sizeof_register[regnum]); |
af030b9a AC |
1155 | } |
1156 | } | |
1157 | ||
b59ff9d5 AC |
1158 | /* Group members. */ |
1159 | if (what_to_dump == regcache_dump_groups) | |
1160 | { | |
1161 | if (regnum < 0) | |
1162 | fprintf_unfiltered (file, "Groups"); | |
1163 | else | |
1164 | { | |
b59ff9d5 | 1165 | const char *sep = ""; |
6c7d17ba | 1166 | struct reggroup *group; |
123f5f96 | 1167 | |
6c7d17ba AC |
1168 | for (group = reggroup_next (gdbarch, NULL); |
1169 | group != NULL; | |
1170 | group = reggroup_next (gdbarch, group)) | |
b59ff9d5 | 1171 | { |
6c7d17ba | 1172 | if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) |
b59ff9d5 | 1173 | { |
6c7d17ba | 1174 | fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group)); |
b59ff9d5 AC |
1175 | sep = ","; |
1176 | } | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | ||
af030b9a AC |
1181 | fprintf_unfiltered (file, "\n"); |
1182 | } | |
1183 | ||
1184 | if (footnote_register_size) | |
1185 | fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n", | |
1186 | footnote_register_size); | |
1187 | if (footnote_register_offset) | |
1188 | fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n", | |
1189 | footnote_register_offset); | |
1190 | if (footnote_register_type_name_null) | |
1191 | fprintf_unfiltered (file, | |
1192 | "*%d: Register type's name NULL.\n", | |
1193 | footnote_register_type_name_null); | |
1194 | do_cleanups (cleanups); | |
1195 | } | |
1196 | ||
1197 | static void | |
1198 | regcache_print (char *args, enum regcache_dump_what what_to_dump) | |
1199 | { | |
1200 | if (args == NULL) | |
28c38f10 | 1201 | regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump); |
af030b9a AC |
1202 | else |
1203 | { | |
724b958c | 1204 | struct cleanup *cleanups; |
af030b9a | 1205 | struct ui_file *file = gdb_fopen (args, "w"); |
123f5f96 | 1206 | |
af030b9a | 1207 | if (file == NULL) |
e2e0b3e5 | 1208 | perror_with_name (_("maintenance print architecture")); |
724b958c | 1209 | cleanups = make_cleanup_ui_file_delete (file); |
28c38f10 | 1210 | regcache_dump (get_current_regcache (), file, what_to_dump); |
724b958c | 1211 | do_cleanups (cleanups); |
af030b9a AC |
1212 | } |
1213 | } | |
1214 | ||
1215 | static void | |
1216 | maintenance_print_registers (char *args, int from_tty) | |
1217 | { | |
1218 | regcache_print (args, regcache_dump_none); | |
1219 | } | |
1220 | ||
1221 | static void | |
1222 | maintenance_print_raw_registers (char *args, int from_tty) | |
1223 | { | |
1224 | regcache_print (args, regcache_dump_raw); | |
1225 | } | |
1226 | ||
1227 | static void | |
1228 | maintenance_print_cooked_registers (char *args, int from_tty) | |
1229 | { | |
1230 | regcache_print (args, regcache_dump_cooked); | |
1231 | } | |
1232 | ||
b59ff9d5 AC |
1233 | static void |
1234 | maintenance_print_register_groups (char *args, int from_tty) | |
1235 | { | |
1236 | regcache_print (args, regcache_dump_groups); | |
1237 | } | |
1238 | ||
b9362cc7 AC |
1239 | extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */ |
1240 | ||
32178cab MS |
1241 | void |
1242 | _initialize_regcache (void) | |
1243 | { | |
030f20e1 | 1244 | regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr); |
705152c5 | 1245 | |
f4c5303c | 1246 | observer_attach_target_changed (regcache_observer_target_changed); |
5231c1fd | 1247 | observer_attach_thread_ptid_changed (regcache_thread_ptid_changed); |
f4c5303c | 1248 | |
705152c5 | 1249 | add_com ("flushregs", class_maintenance, reg_flush_command, |
1bedd215 | 1250 | _("Force gdb to flush its register cache (maintainer command)")); |
39f77062 | 1251 | |
1a966eab AC |
1252 | add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\ |
1253 | Print the internal register configuration.\n\ | |
1254 | Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1255 | add_cmd ("raw-registers", class_maintenance, |
1a966eab AC |
1256 | maintenance_print_raw_registers, _("\ |
1257 | Print the internal register configuration including raw values.\n\ | |
1258 | Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1259 | add_cmd ("cooked-registers", class_maintenance, |
1a966eab AC |
1260 | maintenance_print_cooked_registers, _("\ |
1261 | Print the internal register configuration including cooked values.\n\ | |
1262 | Takes an optional file parameter."), &maintenanceprintlist); | |
b59ff9d5 | 1263 | add_cmd ("register-groups", class_maintenance, |
1a966eab AC |
1264 | maintenance_print_register_groups, _("\ |
1265 | Print the internal register configuration including each register's group.\n\ | |
1266 | Takes an optional file parameter."), | |
af030b9a AC |
1267 | &maintenanceprintlist); |
1268 | ||
32178cab | 1269 | } |