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