Commit | Line | Data |
---|---|---|
32178cab | 1 | /* Cache and manage the values of registers for GDB, the GNU debugger. |
3fadccb3 AC |
2 | |
3 | Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, | |
4 | 2001, 2002 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 | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
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" |
61a0eb5b | 29 | #include "gdb_assert.h" |
b66d6d2e | 30 | #include "gdb_string.h" |
af030b9a | 31 | #include "gdbcmd.h" /* For maintenanceprintlist. */ |
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 | ||
49 | /* Is this a ``legacy'' register cache? Such caches reserve space | |
50 | for raw and pseudo registers and allow access to both. */ | |
51 | int legacy_p; | |
52 | ||
53 | /* The raw register cache. This should contain just [0 | |
54 | .. NUM_RAW_REGISTERS). However, for older targets, it contains | |
55 | space for the full [0 .. NUM_RAW_REGISTERS + | |
56 | NUM_PSEUDO_REGISTERS). */ | |
57 | int nr_raw_registers; | |
58 | long sizeof_raw_registers; | |
59 | long sizeof_raw_register_valid_p; | |
60 | ||
d138e37a AC |
61 | /* The cooked register space. Each cooked register in the range |
62 | [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw | |
63 | register. The remaining [NR_RAW_REGISTERS | |
64 | .. NR_COOKED_REGISTERS) (a.k.a. pseudo regiters) are mapped onto | |
65 | both raw registers and memory by the architecture methods | |
66 | gdbarch_register_read and gdbarch_register_write. */ | |
67 | int nr_cooked_registers; | |
68 | ||
69 | /* Offset and size (in 8 bit bytes), of reach register in the | |
70 | register cache. All registers (including those in the range | |
71 | [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset. | |
72 | Assigning all registers an offset makes it possible to keep | |
73 | legacy code, such as that found in read_register_bytes() and | |
74 | write_register_bytes() working. */ | |
3fadccb3 | 75 | long *register_offset; |
3fadccb3 | 76 | long *sizeof_register; |
3fadccb3 | 77 | |
d138e37a AC |
78 | /* Useful constant. Largest of all the registers. */ |
79 | long max_register_size; | |
3fadccb3 AC |
80 | }; |
81 | ||
82 | static void * | |
83 | init_legacy_regcache_descr (struct gdbarch *gdbarch) | |
84 | { | |
85 | int i; | |
86 | struct regcache_descr *descr; | |
87 | /* FIXME: cagney/2002-05-11: gdbarch_data() should take that | |
88 | ``gdbarch'' as a parameter. */ | |
89 | gdb_assert (gdbarch != NULL); | |
90 | ||
91 | descr = XMALLOC (struct regcache_descr); | |
92 | descr->gdbarch = gdbarch; | |
93 | descr->legacy_p = 1; | |
94 | ||
95 | /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers | |
96 | in the register buffer. Unfortunatly some architectures do. */ | |
d138e37a AC |
97 | descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS; |
98 | descr->nr_raw_registers = descr->nr_cooked_registers; | |
99 | descr->sizeof_raw_register_valid_p = descr->nr_cooked_registers; | |
3fadccb3 AC |
100 | |
101 | /* FIXME: cagney/2002-05-11: Instead of using REGISTER_BYTE() this | |
102 | code should compute the offets et.al. at runtime. This currently | |
103 | isn't possible because some targets overlap register locations - | |
104 | see the mess in read_register_bytes() and write_register_bytes() | |
105 | registers. */ | |
d138e37a AC |
106 | descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long); |
107 | descr->register_offset = XCALLOC (descr->nr_cooked_registers, long); | |
3fadccb3 | 108 | descr->max_register_size = 0; |
d138e37a | 109 | for (i = 0; i < descr->nr_cooked_registers; i++) |
3fadccb3 AC |
110 | { |
111 | descr->register_offset[i] = REGISTER_BYTE (i); | |
112 | descr->sizeof_register[i] = REGISTER_RAW_SIZE (i); | |
113 | if (descr->max_register_size < REGISTER_RAW_SIZE (i)) | |
114 | descr->max_register_size = REGISTER_RAW_SIZE (i); | |
115 | } | |
116 | ||
117 | /* Come up with the real size of the registers buffer. */ | |
118 | descr->sizeof_raw_registers = REGISTER_BYTES; /* OK use. */ | |
d138e37a | 119 | for (i = 0; i < descr->nr_cooked_registers; i++) |
3fadccb3 AC |
120 | { |
121 | long regend; | |
122 | /* Keep extending the buffer so that there is always enough | |
123 | space for all registers. The comparison is necessary since | |
124 | legacy code is free to put registers in random places in the | |
125 | buffer separated by holes. Once REGISTER_BYTE() is killed | |
126 | this can be greatly simplified. */ | |
127 | /* FIXME: cagney/2001-12-04: This code shouldn't need to use | |
128 | REGISTER_BYTE(). Unfortunatly, legacy code likes to lay the | |
129 | buffer out so that certain registers just happen to overlap. | |
130 | Ulgh! New targets use gdbarch's register read/write and | |
131 | entirely avoid this uglyness. */ | |
132 | regend = descr->register_offset[i] + descr->sizeof_register[i]; | |
133 | if (descr->sizeof_raw_registers < regend) | |
134 | descr->sizeof_raw_registers = regend; | |
135 | } | |
136 | return descr; | |
137 | } | |
138 | ||
139 | static void * | |
140 | init_regcache_descr (struct gdbarch *gdbarch) | |
141 | { | |
142 | int i; | |
143 | struct regcache_descr *descr; | |
144 | gdb_assert (gdbarch != NULL); | |
145 | ||
146 | /* If an old style architecture, construct the register cache | |
147 | description using all the register macros. */ | |
d8124050 AC |
148 | if (!gdbarch_pseudo_register_read_p (gdbarch) |
149 | && !gdbarch_pseudo_register_write_p (gdbarch)) | |
3fadccb3 AC |
150 | return init_legacy_regcache_descr (gdbarch); |
151 | ||
152 | descr = XMALLOC (struct regcache_descr); | |
153 | descr->gdbarch = gdbarch; | |
154 | descr->legacy_p = 0; | |
155 | ||
d138e37a AC |
156 | /* Total size of the register space. The raw registers are mapped |
157 | directly onto the raw register cache while the pseudo's are | |
3fadccb3 | 158 | either mapped onto raw-registers or memory. */ |
d138e37a | 159 | descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS; |
3fadccb3 AC |
160 | |
161 | /* Construct a strictly RAW register cache. Don't allow pseudo's | |
162 | into the register cache. */ | |
163 | descr->nr_raw_registers = NUM_REGS; | |
164 | descr->sizeof_raw_register_valid_p = NUM_REGS; | |
165 | ||
166 | /* Lay out the register cache. The pseud-registers are included in | |
167 | the layout even though their value isn't stored in the register | |
168 | cache. Some code, via read_register_bytes() access a register | |
169 | using an offset/length rather than a register number. | |
170 | ||
171 | NOTE: cagney/2002-05-22: Only REGISTER_VIRTUAL_TYPE() needs to be | |
172 | used when constructing the register cache. It is assumed that | |
173 | register raw size, virtual size and type length of the type are | |
174 | all the same. */ | |
175 | ||
176 | { | |
177 | long offset = 0; | |
d138e37a AC |
178 | descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long); |
179 | descr->register_offset = XCALLOC (descr->nr_cooked_registers, long); | |
3fadccb3 | 180 | descr->max_register_size = 0; |
d138e37a | 181 | for (i = 0; i < descr->nr_cooked_registers; i++) |
3fadccb3 AC |
182 | { |
183 | descr->sizeof_register[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i)); | |
184 | descr->register_offset[i] = offset; | |
185 | offset += descr->sizeof_register[i]; | |
186 | if (descr->max_register_size < descr->sizeof_register[i]) | |
187 | descr->max_register_size = descr->sizeof_register[i]; | |
188 | } | |
189 | /* Set the real size of the register cache buffer. */ | |
190 | /* FIXME: cagney/2002-05-22: Should only need to allocate space | |
191 | for the raw registers. Unfortunatly some code still accesses | |
192 | the register array directly using the global registers[]. | |
193 | Until that code has been purged, play safe and over allocating | |
194 | the register buffer. Ulgh! */ | |
195 | descr->sizeof_raw_registers = offset; | |
196 | /* = descr->register_offset[descr->nr_raw_registers]; */ | |
197 | } | |
198 | ||
199 | #if 0 | |
200 | /* Sanity check. Confirm that the assumptions about gdbarch are | |
201 | true. The REGCACHE_DESCR_HANDLE is set before doing the checks | |
202 | so that targets using the generic methods supplied by regcache | |
203 | don't go into infinite recursion trying to, again, create the | |
204 | regcache. */ | |
205 | set_gdbarch_data (gdbarch, regcache_descr_handle, descr); | |
d138e37a | 206 | for (i = 0; i < descr->nr_cooked_registers; i++) |
3fadccb3 AC |
207 | { |
208 | gdb_assert (descr->sizeof_register[i] == REGISTER_RAW_SIZE (i)); | |
209 | gdb_assert (descr->sizeof_register[i] == REGISTER_VIRTUAL_SIZE (i)); | |
210 | gdb_assert (descr->register_offset[i] == REGISTER_BYTE (i)); | |
211 | } | |
212 | /* gdb_assert (descr->sizeof_raw_registers == REGISTER_BYTES (i)); */ | |
213 | #endif | |
214 | ||
215 | return descr; | |
216 | } | |
217 | ||
218 | static struct regcache_descr * | |
219 | regcache_descr (struct gdbarch *gdbarch) | |
220 | { | |
221 | return gdbarch_data (gdbarch, regcache_descr_handle); | |
222 | } | |
223 | ||
224 | static void | |
225 | xfree_regcache_descr (struct gdbarch *gdbarch, void *ptr) | |
226 | { | |
227 | struct regcache_descr *descr = ptr; | |
228 | if (descr == NULL) | |
229 | return; | |
230 | xfree (descr->register_offset); | |
231 | xfree (descr->sizeof_register); | |
232 | descr->register_offset = NULL; | |
233 | descr->sizeof_register = NULL; | |
234 | xfree (descr); | |
235 | } | |
236 | ||
237 | /* The register cache for storing raw register values. */ | |
238 | ||
239 | struct regcache | |
240 | { | |
241 | struct regcache_descr *descr; | |
242 | char *raw_registers; | |
243 | char *raw_register_valid_p; | |
244 | /* If a value isn't in the cache should the corresponding target be | |
245 | queried for a value. */ | |
246 | int passthrough_p; | |
247 | }; | |
248 | ||
249 | struct regcache * | |
250 | regcache_xmalloc (struct gdbarch *gdbarch) | |
251 | { | |
252 | struct regcache_descr *descr; | |
253 | struct regcache *regcache; | |
254 | gdb_assert (gdbarch != NULL); | |
255 | descr = regcache_descr (gdbarch); | |
256 | regcache = XMALLOC (struct regcache); | |
257 | regcache->descr = descr; | |
258 | regcache->raw_registers | |
259 | = XCALLOC (descr->sizeof_raw_registers, char); | |
260 | regcache->raw_register_valid_p | |
261 | = XCALLOC (descr->sizeof_raw_register_valid_p, char); | |
262 | regcache->passthrough_p = 0; | |
263 | return regcache; | |
264 | } | |
265 | ||
266 | void | |
267 | regcache_xfree (struct regcache *regcache) | |
268 | { | |
269 | if (regcache == NULL) | |
270 | return; | |
271 | xfree (regcache->raw_registers); | |
272 | xfree (regcache->raw_register_valid_p); | |
273 | xfree (regcache); | |
274 | } | |
275 | ||
36160dc4 AC |
276 | void |
277 | do_regcache_xfree (void *data) | |
278 | { | |
279 | regcache_xfree (data); | |
280 | } | |
281 | ||
282 | struct cleanup * | |
283 | make_cleanup_regcache_xfree (struct regcache *regcache) | |
284 | { | |
285 | return make_cleanup (do_regcache_xfree, regcache); | |
286 | } | |
287 | ||
3fadccb3 AC |
288 | void |
289 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
290 | { | |
291 | int i; | |
292 | char *buf; | |
293 | gdb_assert (src != NULL && dst != NULL); | |
294 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
295 | gdb_assert (src != dst); | |
296 | /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite. | |
297 | It keeps the existing code working where things rely on going | |
298 | through to the register cache. */ | |
299 | if (src == current_regcache && src->descr->legacy_p) | |
300 | { | |
301 | /* ULGH!!!! Old way. Use REGISTER bytes and let code below | |
302 | untangle fetch. */ | |
303 | read_register_bytes (0, dst->raw_registers, REGISTER_BYTES); | |
304 | return; | |
305 | } | |
306 | /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite. | |
307 | It keeps the existing code working where things rely on going | |
308 | through to the register cache. */ | |
309 | if (dst == current_regcache && dst->descr->legacy_p) | |
310 | { | |
311 | /* ULGH!!!! Old way. Use REGISTER bytes and let code below | |
312 | untangle fetch. */ | |
313 | write_register_bytes (0, src->raw_registers, REGISTER_BYTES); | |
314 | return; | |
315 | } | |
316 | buf = alloca (src->descr->max_register_size); | |
317 | for (i = 0; i < src->descr->nr_raw_registers; i++) | |
318 | { | |
319 | /* Should we worry about the valid bit here? */ | |
0818c12a AC |
320 | regcache_raw_read (src, i, buf); |
321 | regcache_raw_write (dst, i, buf); | |
3fadccb3 AC |
322 | } |
323 | } | |
324 | ||
325 | void | |
326 | regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src) | |
327 | { | |
328 | int i; | |
329 | gdb_assert (src != NULL && dst != NULL); | |
330 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
331 | /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough | |
332 | move of data into the current_regcache(). Doing this would be | |
333 | silly - it would mean that valid_p would be completly invalid. */ | |
334 | gdb_assert (dst != current_regcache); | |
335 | memcpy (dst->raw_registers, src->raw_registers, | |
336 | dst->descr->sizeof_raw_registers); | |
337 | memcpy (dst->raw_register_valid_p, src->raw_register_valid_p, | |
338 | dst->descr->sizeof_raw_register_valid_p); | |
339 | } | |
340 | ||
341 | struct regcache * | |
342 | regcache_dup (struct regcache *src) | |
343 | { | |
344 | struct regcache *newbuf; | |
345 | gdb_assert (current_regcache != NULL); | |
346 | newbuf = regcache_xmalloc (src->descr->gdbarch); | |
347 | regcache_cpy (newbuf, src); | |
348 | return newbuf; | |
349 | } | |
350 | ||
351 | struct regcache * | |
352 | regcache_dup_no_passthrough (struct regcache *src) | |
353 | { | |
354 | struct regcache *newbuf; | |
355 | gdb_assert (current_regcache != NULL); | |
356 | newbuf = regcache_xmalloc (src->descr->gdbarch); | |
357 | regcache_cpy_no_passthrough (newbuf, src); | |
358 | return newbuf; | |
359 | } | |
360 | ||
361 | int | |
362 | regcache_valid_p (struct regcache *regcache, int regnum) | |
363 | { | |
364 | gdb_assert (regcache != NULL); | |
365 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
366 | return regcache->raw_register_valid_p[regnum]; | |
367 | } | |
368 | ||
3fadccb3 AC |
369 | char * |
370 | deprecated_grub_regcache_for_registers (struct regcache *regcache) | |
371 | { | |
372 | return regcache->raw_registers; | |
373 | } | |
374 | ||
375 | char * | |
376 | deprecated_grub_regcache_for_register_valid (struct regcache *regcache) | |
377 | { | |
378 | return regcache->raw_register_valid_p; | |
379 | } | |
380 | ||
381 | /* Global structure containing the current regcache. */ | |
382 | /* FIXME: cagney/2002-05-11: The two global arrays registers[] and | |
383 | register_valid[] currently point into this structure. */ | |
384 | struct regcache *current_regcache; | |
385 | ||
5ebd2499 | 386 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
32178cab MS |
387 | recording if the register values have been changed (eg. by the |
388 | user). Therefore all registers must be written back to the | |
389 | target when appropriate. */ | |
390 | ||
391 | /* REGISTERS contains the cached register values (in target byte order). */ | |
392 | ||
393 | char *registers; | |
394 | ||
395 | /* REGISTER_VALID is 0 if the register needs to be fetched, | |
396 | 1 if it has been fetched, and | |
397 | -1 if the register value was not available. | |
c97dcfc7 AC |
398 | |
399 | "Not available" indicates that the target is not not able to supply | |
400 | the register at this state. The register may become available at a | |
401 | later time (after the next resume). This often occures when GDB is | |
402 | manipulating a target that contains only a snapshot of the entire | |
403 | system being debugged - some of the registers in such a system may | |
404 | not have been saved. */ | |
32178cab MS |
405 | |
406 | signed char *register_valid; | |
407 | ||
39f77062 | 408 | /* The thread/process associated with the current set of registers. */ |
32178cab | 409 | |
39f77062 | 410 | static ptid_t registers_ptid; |
32178cab MS |
411 | |
412 | /* | |
413 | * FUNCTIONS: | |
414 | */ | |
415 | ||
416 | /* REGISTER_CACHED() | |
417 | ||
418 | Returns 0 if the value is not in the cache (needs fetch). | |
419 | >0 if the value is in the cache. | |
420 | <0 if the value is permanently unavailable (don't ask again). */ | |
421 | ||
422 | int | |
423 | register_cached (int regnum) | |
424 | { | |
425 | return register_valid[regnum]; | |
426 | } | |
427 | ||
7302a204 ND |
428 | /* Record that REGNUM's value is cached if STATE is >0, uncached but |
429 | fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */ | |
430 | ||
431 | void | |
432 | set_register_cached (int regnum, int state) | |
433 | { | |
434 | register_valid[regnum] = state; | |
435 | } | |
436 | ||
2dc4e391 DT |
437 | /* REGISTER_CHANGED |
438 | ||
439 | invalidate a single register REGNUM in the cache */ | |
440 | void | |
441 | register_changed (int regnum) | |
442 | { | |
7302a204 ND |
443 | set_register_cached (regnum, 0); |
444 | } | |
445 | ||
446 | /* If REGNUM >= 0, return a pointer to register REGNUM's cache buffer area, | |
447 | else return a pointer to the start of the cache buffer. */ | |
448 | ||
193cb69f | 449 | static char * |
3fadccb3 | 450 | register_buffer (struct regcache *regcache, int regnum) |
7302a204 | 451 | { |
3fadccb3 | 452 | return regcache->raw_registers + regcache->descr->register_offset[regnum]; |
7302a204 ND |
453 | } |
454 | ||
455 | /* Return whether register REGNUM is a real register. */ | |
456 | ||
457 | static int | |
458 | real_register (int regnum) | |
459 | { | |
460 | return regnum >= 0 && regnum < NUM_REGS; | |
461 | } | |
462 | ||
32178cab MS |
463 | /* Low level examining and depositing of registers. |
464 | ||
465 | The caller is responsible for making sure that the inferior is | |
466 | stopped before calling the fetching routines, or it will get | |
467 | garbage. (a change from GDB version 3, in which the caller got the | |
468 | value from the last stop). */ | |
469 | ||
470 | /* REGISTERS_CHANGED () | |
471 | ||
472 | Indicate that registers may have changed, so invalidate the cache. */ | |
473 | ||
474 | void | |
475 | registers_changed (void) | |
476 | { | |
477 | int i; | |
32178cab | 478 | |
39f77062 | 479 | registers_ptid = pid_to_ptid (-1); |
32178cab MS |
480 | |
481 | /* Force cleanup of any alloca areas if using C alloca instead of | |
482 | a builtin alloca. This particular call is used to clean up | |
483 | areas allocated by low level target code which may build up | |
484 | during lengthy interactions between gdb and the target before | |
485 | gdb gives control to the user (ie watchpoints). */ | |
486 | alloca (0); | |
487 | ||
31e9866e | 488 | for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++) |
7302a204 | 489 | set_register_cached (i, 0); |
32178cab MS |
490 | |
491 | if (registers_changed_hook) | |
492 | registers_changed_hook (); | |
493 | } | |
494 | ||
495 | /* REGISTERS_FETCHED () | |
496 | ||
497 | Indicate that all registers have been fetched, so mark them all valid. */ | |
498 | ||
31e9866e AC |
499 | /* NOTE: cagney/2001-12-04: This function does not set valid on the |
500 | pseudo-register range since pseudo registers are always supplied | |
501 | using supply_register(). */ | |
502 | /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target | |
503 | code was blatting the registers[] array and then calling this. | |
504 | Since targets should only be using supply_register() the need for | |
505 | this function/hack is eliminated. */ | |
32178cab MS |
506 | |
507 | void | |
508 | registers_fetched (void) | |
509 | { | |
510 | int i; | |
32178cab | 511 | |
a728f042 | 512 | for (i = 0; i < NUM_REGS; i++) |
7302a204 | 513 | set_register_cached (i, 1); |
fcdc5976 | 514 | /* Do not assume that the pseudo-regs have also been fetched. |
31e9866e | 515 | Fetching all real regs NEVER accounts for pseudo-regs. */ |
32178cab MS |
516 | } |
517 | ||
518 | /* read_register_bytes and write_register_bytes are generally a *BAD* | |
519 | idea. They are inefficient because they need to check for partial | |
520 | updates, which can only be done by scanning through all of the | |
521 | registers and seeing if the bytes that are being read/written fall | |
522 | inside of an invalid register. [The main reason this is necessary | |
523 | is that register sizes can vary, so a simple index won't suffice.] | |
524 | It is far better to call read_register_gen and write_register_gen | |
525 | if you want to get at the raw register contents, as it only takes a | |
5ebd2499 | 526 | regnum as an argument, and therefore can't do a partial register |
32178cab MS |
527 | update. |
528 | ||
529 | Prior to the recent fixes to check for partial updates, both read | |
530 | and write_register_bytes always checked to see if any registers | |
531 | were stale, and then called target_fetch_registers (-1) to update | |
532 | the whole set. This caused really slowed things down for remote | |
533 | targets. */ | |
534 | ||
535 | /* Copy INLEN bytes of consecutive data from registers | |
536 | starting with the INREGBYTE'th byte of register data | |
537 | into memory at MYADDR. */ | |
538 | ||
539 | void | |
61a0eb5b | 540 | read_register_bytes (int in_start, char *in_buf, int in_len) |
32178cab | 541 | { |
61a0eb5b | 542 | int in_end = in_start + in_len; |
5ebd2499 | 543 | int regnum; |
61a0eb5b | 544 | char *reg_buf = alloca (MAX_REGISTER_RAW_SIZE); |
32178cab MS |
545 | |
546 | /* See if we are trying to read bytes from out-of-date registers. If so, | |
547 | update just those registers. */ | |
548 | ||
5ebd2499 | 549 | for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++) |
32178cab | 550 | { |
61a0eb5b AC |
551 | int reg_start; |
552 | int reg_end; | |
553 | int reg_len; | |
554 | int start; | |
555 | int end; | |
556 | int byte; | |
32178cab | 557 | |
61a0eb5b AC |
558 | reg_start = REGISTER_BYTE (regnum); |
559 | reg_len = REGISTER_RAW_SIZE (regnum); | |
560 | reg_end = reg_start + reg_len; | |
32178cab | 561 | |
61a0eb5b | 562 | if (reg_end <= in_start || in_end <= reg_start) |
5ebd2499 | 563 | /* The range the user wants to read doesn't overlap with regnum. */ |
32178cab MS |
564 | continue; |
565 | ||
275f450c AC |
566 | if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0') |
567 | /* Force the cache to fetch the entire register. */ | |
568 | read_register_gen (regnum, reg_buf); | |
569 | else | |
570 | /* Legacy note: even though this register is ``invalid'' we | |
571 | still need to return something. It would appear that some | |
572 | code relies on apparent gaps in the register array also | |
573 | being returned. */ | |
574 | /* FIXME: cagney/2001-08-18: This is just silly. It defeats | |
575 | the entire register read/write flow of control. Must | |
576 | resist temptation to return 0xdeadbeef. */ | |
577 | memcpy (reg_buf, registers + reg_start, reg_len); | |
32178cab | 578 | |
61a0eb5b AC |
579 | /* Legacy note: This function, for some reason, allows a NULL |
580 | input buffer. If the buffer is NULL, the registers are still | |
581 | fetched, just the final transfer is skipped. */ | |
582 | if (in_buf == NULL) | |
583 | continue; | |
584 | ||
585 | /* start = max (reg_start, in_start) */ | |
586 | if (reg_start > in_start) | |
587 | start = reg_start; | |
588 | else | |
589 | start = in_start; | |
590 | ||
591 | /* end = min (reg_end, in_end) */ | |
592 | if (reg_end < in_end) | |
593 | end = reg_end; | |
594 | else | |
595 | end = in_end; | |
596 | ||
597 | /* Transfer just the bytes common to both IN_BUF and REG_BUF */ | |
598 | for (byte = start; byte < end; byte++) | |
165cd47f | 599 | { |
61a0eb5b | 600 | in_buf[byte - in_start] = reg_buf[byte - reg_start]; |
165cd47f | 601 | } |
32178cab | 602 | } |
32178cab MS |
603 | } |
604 | ||
5ebd2499 ND |
605 | /* Read register REGNUM into memory at MYADDR, which must be large |
606 | enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the | |
32178cab MS |
607 | register is known to be the size of a CORE_ADDR or smaller, |
608 | read_register can be used instead. */ | |
609 | ||
61a0eb5b AC |
610 | static void |
611 | legacy_read_register_gen (int regnum, char *myaddr) | |
32178cab | 612 | { |
61a0eb5b | 613 | gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS)); |
39f77062 | 614 | if (! ptid_equal (registers_ptid, inferior_ptid)) |
32178cab MS |
615 | { |
616 | registers_changed (); | |
39f77062 | 617 | registers_ptid = inferior_ptid; |
32178cab MS |
618 | } |
619 | ||
7302a204 | 620 | if (!register_cached (regnum)) |
5c27f28a | 621 | target_fetch_registers (regnum); |
7302a204 | 622 | |
3fadccb3 | 623 | memcpy (myaddr, register_buffer (current_regcache, regnum), |
5ebd2499 | 624 | REGISTER_RAW_SIZE (regnum)); |
32178cab MS |
625 | } |
626 | ||
61a0eb5b | 627 | void |
1aaa5f99 | 628 | regcache_raw_read (struct regcache *regcache, int regnum, void *buf) |
61a0eb5b | 629 | { |
3fadccb3 AC |
630 | gdb_assert (regcache != NULL && buf != NULL); |
631 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
632 | if (regcache->descr->legacy_p | |
633 | && regcache->passthrough_p) | |
634 | { | |
635 | gdb_assert (regcache == current_regcache); | |
636 | /* For moment, just use underlying legacy code. Ulgh!!! This | |
637 | silently and very indirectly updates the regcache's regcache | |
638 | via the global register_valid[]. */ | |
639 | legacy_read_register_gen (regnum, buf); | |
640 | return; | |
641 | } | |
642 | /* Make certain that the register cache is up-to-date with respect | |
643 | to the current thread. This switching shouldn't be necessary | |
644 | only there is still only one target side register cache. Sigh! | |
645 | On the bright side, at least there is a regcache object. */ | |
646 | if (regcache->passthrough_p) | |
647 | { | |
648 | gdb_assert (regcache == current_regcache); | |
649 | if (! ptid_equal (registers_ptid, inferior_ptid)) | |
650 | { | |
651 | registers_changed (); | |
652 | registers_ptid = inferior_ptid; | |
653 | } | |
654 | if (!register_cached (regnum)) | |
5c27f28a | 655 | target_fetch_registers (regnum); |
3fadccb3 AC |
656 | } |
657 | /* Copy the value directly into the register cache. */ | |
658 | memcpy (buf, (regcache->raw_registers | |
659 | + regcache->descr->register_offset[regnum]), | |
660 | regcache->descr->sizeof_register[regnum]); | |
61a0eb5b AC |
661 | } |
662 | ||
28fc6740 AC |
663 | void |
664 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) | |
665 | { | |
666 | char *buf; | |
667 | gdb_assert (regcache != NULL); | |
668 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
669 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
670 | regcache_raw_read (regcache, regnum, buf); | |
671 | (*val) = extract_signed_integer (buf, | |
672 | regcache->descr->sizeof_register[regnum]); | |
673 | } | |
674 | ||
675 | void | |
676 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, | |
677 | ULONGEST *val) | |
678 | { | |
679 | char *buf; | |
680 | gdb_assert (regcache != NULL); | |
681 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
682 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
683 | regcache_raw_read (regcache, regnum, buf); | |
684 | (*val) = extract_unsigned_integer (buf, | |
685 | regcache->descr->sizeof_register[regnum]); | |
686 | } | |
687 | ||
61a0eb5b AC |
688 | void |
689 | read_register_gen (int regnum, char *buf) | |
690 | { | |
3fadccb3 AC |
691 | gdb_assert (current_regcache != NULL); |
692 | gdb_assert (current_regcache->descr->gdbarch == current_gdbarch); | |
693 | if (current_regcache->descr->legacy_p) | |
61a0eb5b AC |
694 | { |
695 | legacy_read_register_gen (regnum, buf); | |
696 | return; | |
697 | } | |
68365089 AC |
698 | regcache_cooked_read (current_regcache, regnum, buf); |
699 | } | |
700 | ||
701 | void | |
29e1842b | 702 | regcache_cooked_read (struct regcache *regcache, int regnum, void *buf) |
68365089 | 703 | { |
d138e37a | 704 | gdb_assert (regnum >= 0); |
68365089 AC |
705 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
706 | if (regnum < regcache->descr->nr_raw_registers) | |
707 | regcache_raw_read (regcache, regnum, buf); | |
d138e37a | 708 | else |
68365089 AC |
709 | gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache, |
710 | regnum, buf); | |
61a0eb5b AC |
711 | } |
712 | ||
a378f419 AC |
713 | void |
714 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, | |
715 | LONGEST *val) | |
716 | { | |
717 | char *buf; | |
718 | gdb_assert (regcache != NULL); | |
719 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
720 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
721 | regcache_cooked_read (regcache, regnum, buf); | |
722 | (*val) = extract_signed_integer (buf, | |
723 | regcache->descr->sizeof_register[regnum]); | |
724 | } | |
725 | ||
726 | void | |
727 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, | |
728 | ULONGEST *val) | |
729 | { | |
730 | char *buf; | |
731 | gdb_assert (regcache != NULL); | |
732 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
733 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
734 | regcache_cooked_read (regcache, regnum, buf); | |
735 | (*val) = extract_unsigned_integer (buf, | |
736 | regcache->descr->sizeof_register[regnum]); | |
737 | } | |
738 | ||
5ebd2499 ND |
739 | /* Write register REGNUM at MYADDR to the target. MYADDR points at |
740 | REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */ | |
32178cab | 741 | |
61a0eb5b | 742 | static void |
1aaa5f99 | 743 | legacy_write_register_gen (int regnum, const void *myaddr) |
32178cab MS |
744 | { |
745 | int size; | |
61a0eb5b | 746 | gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS)); |
32178cab MS |
747 | |
748 | /* On the sparc, writing %g0 is a no-op, so we don't even want to | |
749 | change the registers array if something writes to this register. */ | |
5ebd2499 | 750 | if (CANNOT_STORE_REGISTER (regnum)) |
32178cab MS |
751 | return; |
752 | ||
39f77062 | 753 | if (! ptid_equal (registers_ptid, inferior_ptid)) |
32178cab MS |
754 | { |
755 | registers_changed (); | |
39f77062 | 756 | registers_ptid = inferior_ptid; |
32178cab MS |
757 | } |
758 | ||
5ebd2499 | 759 | size = REGISTER_RAW_SIZE (regnum); |
32178cab | 760 | |
7302a204 | 761 | if (real_register (regnum)) |
1297a2f0 MS |
762 | { |
763 | /* If we have a valid copy of the register, and new value == old | |
764 | value, then don't bother doing the actual store. */ | |
765 | if (register_cached (regnum) | |
3fadccb3 AC |
766 | && (memcmp (register_buffer (current_regcache, regnum), myaddr, size) |
767 | == 0)) | |
1297a2f0 MS |
768 | return; |
769 | else | |
770 | target_prepare_to_store (); | |
771 | } | |
32178cab | 772 | |
3fadccb3 | 773 | memcpy (register_buffer (current_regcache, regnum), myaddr, size); |
32178cab | 774 | |
7302a204 | 775 | set_register_cached (regnum, 1); |
5c27f28a | 776 | target_store_registers (regnum); |
32178cab MS |
777 | } |
778 | ||
61a0eb5b | 779 | void |
1aaa5f99 | 780 | regcache_raw_write (struct regcache *regcache, int regnum, const void *buf) |
61a0eb5b | 781 | { |
3fadccb3 AC |
782 | gdb_assert (regcache != NULL && buf != NULL); |
783 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
784 | ||
785 | if (regcache->passthrough_p | |
786 | && regcache->descr->legacy_p) | |
787 | { | |
788 | /* For moment, just use underlying legacy code. Ulgh!!! This | |
789 | silently and very indirectly updates the regcache's buffers | |
790 | via the globals register_valid[] and registers[]. */ | |
791 | gdb_assert (regcache == current_regcache); | |
792 | legacy_write_register_gen (regnum, buf); | |
793 | return; | |
794 | } | |
795 | ||
796 | /* On the sparc, writing %g0 is a no-op, so we don't even want to | |
797 | change the registers array if something writes to this register. */ | |
798 | if (CANNOT_STORE_REGISTER (regnum)) | |
799 | return; | |
800 | ||
801 | /* Handle the simple case first -> not write through so just store | |
802 | value in cache. */ | |
803 | if (!regcache->passthrough_p) | |
804 | { | |
805 | memcpy ((regcache->raw_registers | |
806 | + regcache->descr->register_offset[regnum]), buf, | |
807 | regcache->descr->sizeof_register[regnum]); | |
808 | regcache->raw_register_valid_p[regnum] = 1; | |
809 | return; | |
810 | } | |
811 | ||
812 | /* Make certain that the correct cache is selected. */ | |
813 | gdb_assert (regcache == current_regcache); | |
814 | if (! ptid_equal (registers_ptid, inferior_ptid)) | |
815 | { | |
816 | registers_changed (); | |
817 | registers_ptid = inferior_ptid; | |
818 | } | |
819 | ||
820 | /* If we have a valid copy of the register, and new value == old | |
821 | value, then don't bother doing the actual store. */ | |
822 | if (regcache_valid_p (regcache, regnum) | |
823 | && (memcmp (register_buffer (regcache, regnum), buf, | |
824 | regcache->descr->sizeof_register[regnum]) == 0)) | |
825 | return; | |
826 | ||
827 | target_prepare_to_store (); | |
828 | memcpy (register_buffer (regcache, regnum), buf, | |
829 | regcache->descr->sizeof_register[regnum]); | |
830 | regcache->raw_register_valid_p[regnum] = 1; | |
5c27f28a | 831 | target_store_registers (regnum); |
61a0eb5b AC |
832 | } |
833 | ||
834 | void | |
835 | write_register_gen (int regnum, char *buf) | |
836 | { | |
3fadccb3 AC |
837 | gdb_assert (current_regcache != NULL); |
838 | gdb_assert (current_regcache->descr->gdbarch == current_gdbarch); | |
839 | if (current_regcache->descr->legacy_p) | |
61a0eb5b AC |
840 | { |
841 | legacy_write_register_gen (regnum, buf); | |
842 | return; | |
843 | } | |
68365089 AC |
844 | regcache_cooked_write (current_regcache, regnum, buf); |
845 | } | |
846 | ||
847 | void | |
29e1842b | 848 | regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf) |
68365089 | 849 | { |
d138e37a | 850 | gdb_assert (regnum >= 0); |
68365089 AC |
851 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
852 | if (regnum < regcache->descr->nr_raw_registers) | |
853 | regcache_raw_write (regcache, regnum, buf); | |
d138e37a | 854 | else |
68365089 | 855 | gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache, |
d8124050 | 856 | regnum, buf); |
61a0eb5b AC |
857 | } |
858 | ||
32178cab MS |
859 | /* Copy INLEN bytes of consecutive data from memory at MYADDR |
860 | into registers starting with the MYREGSTART'th byte of register data. */ | |
861 | ||
862 | void | |
863 | write_register_bytes (int myregstart, char *myaddr, int inlen) | |
864 | { | |
865 | int myregend = myregstart + inlen; | |
5ebd2499 | 866 | int regnum; |
32178cab MS |
867 | |
868 | target_prepare_to_store (); | |
869 | ||
870 | /* Scan through the registers updating any that are covered by the | |
871 | range myregstart<=>myregend using write_register_gen, which does | |
872 | nice things like handling threads, and avoiding updates when the | |
873 | new and old contents are the same. */ | |
874 | ||
5ebd2499 | 875 | for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++) |
32178cab MS |
876 | { |
877 | int regstart, regend; | |
878 | ||
5ebd2499 ND |
879 | regstart = REGISTER_BYTE (regnum); |
880 | regend = regstart + REGISTER_RAW_SIZE (regnum); | |
32178cab MS |
881 | |
882 | /* Is this register completely outside the range the user is writing? */ | |
883 | if (myregend <= regstart || regend <= myregstart) | |
884 | /* do nothing */ ; | |
885 | ||
886 | /* Is this register completely within the range the user is writing? */ | |
887 | else if (myregstart <= regstart && regend <= myregend) | |
5ebd2499 | 888 | write_register_gen (regnum, myaddr + (regstart - myregstart)); |
32178cab MS |
889 | |
890 | /* The register partially overlaps the range being written. */ | |
891 | else | |
892 | { | |
e6cbd02a | 893 | char *regbuf = (char*) alloca (MAX_REGISTER_RAW_SIZE); |
32178cab MS |
894 | /* What's the overlap between this register's bytes and |
895 | those the caller wants to write? */ | |
896 | int overlapstart = max (regstart, myregstart); | |
897 | int overlapend = min (regend, myregend); | |
898 | ||
899 | /* We may be doing a partial update of an invalid register. | |
900 | Update it from the target before scribbling on it. */ | |
5ebd2499 | 901 | read_register_gen (regnum, regbuf); |
32178cab MS |
902 | |
903 | memcpy (registers + overlapstart, | |
904 | myaddr + (overlapstart - myregstart), | |
905 | overlapend - overlapstart); | |
906 | ||
5c27f28a | 907 | target_store_registers (regnum); |
32178cab MS |
908 | } |
909 | } | |
910 | } | |
911 | ||
912 | ||
5ebd2499 | 913 | /* Return the contents of register REGNUM as an unsigned integer. */ |
32178cab | 914 | |
173155e8 | 915 | ULONGEST |
5ebd2499 | 916 | read_register (int regnum) |
32178cab | 917 | { |
61a0eb5b AC |
918 | char *buf = alloca (REGISTER_RAW_SIZE (regnum)); |
919 | read_register_gen (regnum, buf); | |
920 | return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum))); | |
32178cab MS |
921 | } |
922 | ||
173155e8 | 923 | ULONGEST |
39f77062 | 924 | read_register_pid (int regnum, ptid_t ptid) |
32178cab | 925 | { |
39f77062 | 926 | ptid_t save_ptid; |
32178cab MS |
927 | int save_pid; |
928 | CORE_ADDR retval; | |
929 | ||
39f77062 | 930 | if (ptid_equal (ptid, inferior_ptid)) |
5ebd2499 | 931 | return read_register (regnum); |
32178cab | 932 | |
39f77062 | 933 | save_ptid = inferior_ptid; |
32178cab | 934 | |
39f77062 | 935 | inferior_ptid = ptid; |
32178cab | 936 | |
5ebd2499 | 937 | retval = read_register (regnum); |
32178cab | 938 | |
39f77062 | 939 | inferior_ptid = save_ptid; |
32178cab MS |
940 | |
941 | return retval; | |
942 | } | |
943 | ||
5ebd2499 | 944 | /* Return the contents of register REGNUM as a signed integer. */ |
173155e8 AC |
945 | |
946 | LONGEST | |
5ebd2499 | 947 | read_signed_register (int regnum) |
173155e8 | 948 | { |
61a0eb5b AC |
949 | void *buf = alloca (REGISTER_RAW_SIZE (regnum)); |
950 | read_register_gen (regnum, buf); | |
951 | return (extract_signed_integer (buf, REGISTER_RAW_SIZE (regnum))); | |
173155e8 AC |
952 | } |
953 | ||
954 | LONGEST | |
39f77062 | 955 | read_signed_register_pid (int regnum, ptid_t ptid) |
173155e8 | 956 | { |
39f77062 | 957 | ptid_t save_ptid; |
173155e8 AC |
958 | LONGEST retval; |
959 | ||
39f77062 | 960 | if (ptid_equal (ptid, inferior_ptid)) |
5ebd2499 | 961 | return read_signed_register (regnum); |
173155e8 | 962 | |
39f77062 | 963 | save_ptid = inferior_ptid; |
173155e8 | 964 | |
39f77062 | 965 | inferior_ptid = ptid; |
173155e8 | 966 | |
5ebd2499 | 967 | retval = read_signed_register (regnum); |
173155e8 | 968 | |
39f77062 | 969 | inferior_ptid = save_ptid; |
173155e8 AC |
970 | |
971 | return retval; | |
972 | } | |
973 | ||
5ebd2499 | 974 | /* Store VALUE into the raw contents of register number REGNUM. */ |
32178cab MS |
975 | |
976 | void | |
5ebd2499 | 977 | write_register (int regnum, LONGEST val) |
32178cab | 978 | { |
61a0eb5b | 979 | void *buf; |
32178cab | 980 | int size; |
5ebd2499 | 981 | size = REGISTER_RAW_SIZE (regnum); |
32178cab MS |
982 | buf = alloca (size); |
983 | store_signed_integer (buf, size, (LONGEST) val); | |
61a0eb5b | 984 | write_register_gen (regnum, buf); |
32178cab MS |
985 | } |
986 | ||
987 | void | |
39f77062 | 988 | write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid) |
32178cab | 989 | { |
39f77062 | 990 | ptid_t save_ptid; |
32178cab | 991 | |
39f77062 | 992 | if (ptid_equal (ptid, inferior_ptid)) |
32178cab | 993 | { |
5ebd2499 | 994 | write_register (regnum, val); |
32178cab MS |
995 | return; |
996 | } | |
997 | ||
39f77062 | 998 | save_ptid = inferior_ptid; |
32178cab | 999 | |
39f77062 | 1000 | inferior_ptid = ptid; |
32178cab | 1001 | |
5ebd2499 | 1002 | write_register (regnum, val); |
32178cab | 1003 | |
39f77062 | 1004 | inferior_ptid = save_ptid; |
32178cab MS |
1005 | } |
1006 | ||
1007 | /* SUPPLY_REGISTER() | |
1008 | ||
5ebd2499 | 1009 | Record that register REGNUM contains VAL. This is used when the |
32178cab MS |
1010 | value is obtained from the inferior or core dump, so there is no |
1011 | need to store the value there. | |
1012 | ||
1013 | If VAL is a NULL pointer, then it's probably an unsupported register. | |
5ebd2499 | 1014 | We just set its value to all zeros. We might want to record this |
32178cab MS |
1015 | fact, and report it to the users of read_register and friends. */ |
1016 | ||
1017 | void | |
1aaa5f99 | 1018 | supply_register (int regnum, const void *val) |
32178cab MS |
1019 | { |
1020 | #if 1 | |
39f77062 | 1021 | if (! ptid_equal (registers_ptid, inferior_ptid)) |
32178cab MS |
1022 | { |
1023 | registers_changed (); | |
39f77062 | 1024 | registers_ptid = inferior_ptid; |
32178cab MS |
1025 | } |
1026 | #endif | |
1027 | ||
7302a204 | 1028 | set_register_cached (regnum, 1); |
32178cab | 1029 | if (val) |
3fadccb3 | 1030 | memcpy (register_buffer (current_regcache, regnum), val, |
5ebd2499 | 1031 | REGISTER_RAW_SIZE (regnum)); |
32178cab | 1032 | else |
3fadccb3 | 1033 | memset (register_buffer (current_regcache, regnum), '\000', |
5ebd2499 | 1034 | REGISTER_RAW_SIZE (regnum)); |
32178cab MS |
1035 | |
1036 | /* On some architectures, e.g. HPPA, there are a few stray bits in | |
1037 | some registers, that the rest of the code would like to ignore. */ | |
1038 | ||
61a0eb5b AC |
1039 | /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is |
1040 | going to be deprecated. Instead architectures will leave the raw | |
1041 | register value as is and instead clean things up as they pass | |
d8124050 | 1042 | through the method gdbarch_pseudo_register_read() clean up the |
61a0eb5b AC |
1043 | values. */ |
1044 | ||
4ee3352d | 1045 | #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE |
0b434a00 AC |
1046 | DEPRECATED_CLEAN_UP_REGISTER_VALUE \ |
1047 | (regnum, register_buffer (current_regcache, regnum)); | |
32178cab MS |
1048 | #endif |
1049 | } | |
1050 | ||
193cb69f AC |
1051 | void |
1052 | regcache_collect (int regnum, void *buf) | |
1053 | { | |
3fadccb3 AC |
1054 | memcpy (buf, register_buffer (current_regcache, regnum), |
1055 | REGISTER_RAW_SIZE (regnum)); | |
193cb69f AC |
1056 | } |
1057 | ||
1058 | ||
8227c0ff AC |
1059 | /* read_pc, write_pc, read_sp, write_sp, read_fp, etc. Special |
1060 | handling for registers PC, SP, and FP. */ | |
32178cab | 1061 | |
4e052eda AC |
1062 | /* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(), |
1063 | read_pc_pid(), read_pc(), generic_target_write_pc(), | |
1064 | write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(), | |
8227c0ff AC |
1065 | generic_target_write_sp(), write_sp(), generic_target_read_fp() and |
1066 | read_fp(), will eventually be moved out of the reg-cache into | |
1067 | either frame.[hc] or to the multi-arch framework. The are not part | |
1068 | of the raw register cache. */ | |
4e052eda | 1069 | |
32178cab MS |
1070 | /* This routine is getting awfully cluttered with #if's. It's probably |
1071 | time to turn this into READ_PC and define it in the tm.h file. | |
1072 | Ditto for write_pc. | |
1073 | ||
1074 | 1999-06-08: The following were re-written so that it assumes the | |
8e1a459b | 1075 | existence of a TARGET_READ_PC et.al. macro. A default generic |
32178cab MS |
1076 | version of that macro is made available where needed. |
1077 | ||
1078 | Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled | |
1079 | by the multi-arch framework, it will eventually be possible to | |
1080 | eliminate the intermediate read_pc_pid(). The client would call | |
1081 | TARGET_READ_PC directly. (cagney). */ | |
1082 | ||
32178cab | 1083 | CORE_ADDR |
39f77062 | 1084 | generic_target_read_pc (ptid_t ptid) |
32178cab MS |
1085 | { |
1086 | #ifdef PC_REGNUM | |
1087 | if (PC_REGNUM >= 0) | |
1088 | { | |
39f77062 | 1089 | CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, ptid)); |
32178cab MS |
1090 | return pc_val; |
1091 | } | |
1092 | #endif | |
8e65ff28 AC |
1093 | internal_error (__FILE__, __LINE__, |
1094 | "generic_target_read_pc"); | |
32178cab MS |
1095 | return 0; |
1096 | } | |
1097 | ||
1098 | CORE_ADDR | |
39f77062 | 1099 | read_pc_pid (ptid_t ptid) |
32178cab | 1100 | { |
39f77062 | 1101 | ptid_t saved_inferior_ptid; |
32178cab MS |
1102 | CORE_ADDR pc_val; |
1103 | ||
39f77062 KB |
1104 | /* In case ptid != inferior_ptid. */ |
1105 | saved_inferior_ptid = inferior_ptid; | |
1106 | inferior_ptid = ptid; | |
32178cab | 1107 | |
39f77062 | 1108 | pc_val = TARGET_READ_PC (ptid); |
32178cab | 1109 | |
39f77062 | 1110 | inferior_ptid = saved_inferior_ptid; |
32178cab MS |
1111 | return pc_val; |
1112 | } | |
1113 | ||
1114 | CORE_ADDR | |
1115 | read_pc (void) | |
1116 | { | |
39f77062 | 1117 | return read_pc_pid (inferior_ptid); |
32178cab MS |
1118 | } |
1119 | ||
32178cab | 1120 | void |
39f77062 | 1121 | generic_target_write_pc (CORE_ADDR pc, ptid_t ptid) |
32178cab MS |
1122 | { |
1123 | #ifdef PC_REGNUM | |
1124 | if (PC_REGNUM >= 0) | |
39f77062 | 1125 | write_register_pid (PC_REGNUM, pc, ptid); |
32178cab | 1126 | if (NPC_REGNUM >= 0) |
39f77062 | 1127 | write_register_pid (NPC_REGNUM, pc + 4, ptid); |
32178cab | 1128 | #else |
8e65ff28 AC |
1129 | internal_error (__FILE__, __LINE__, |
1130 | "generic_target_write_pc"); | |
32178cab MS |
1131 | #endif |
1132 | } | |
1133 | ||
1134 | void | |
39f77062 | 1135 | write_pc_pid (CORE_ADDR pc, ptid_t ptid) |
32178cab | 1136 | { |
39f77062 | 1137 | ptid_t saved_inferior_ptid; |
32178cab | 1138 | |
39f77062 KB |
1139 | /* In case ptid != inferior_ptid. */ |
1140 | saved_inferior_ptid = inferior_ptid; | |
1141 | inferior_ptid = ptid; | |
32178cab | 1142 | |
39f77062 | 1143 | TARGET_WRITE_PC (pc, ptid); |
32178cab | 1144 | |
39f77062 | 1145 | inferior_ptid = saved_inferior_ptid; |
32178cab MS |
1146 | } |
1147 | ||
1148 | void | |
1149 | write_pc (CORE_ADDR pc) | |
1150 | { | |
39f77062 | 1151 | write_pc_pid (pc, inferior_ptid); |
32178cab MS |
1152 | } |
1153 | ||
1154 | /* Cope with strage ways of getting to the stack and frame pointers */ | |
1155 | ||
32178cab MS |
1156 | CORE_ADDR |
1157 | generic_target_read_sp (void) | |
1158 | { | |
1159 | #ifdef SP_REGNUM | |
1160 | if (SP_REGNUM >= 0) | |
1161 | return read_register (SP_REGNUM); | |
1162 | #endif | |
8e65ff28 AC |
1163 | internal_error (__FILE__, __LINE__, |
1164 | "generic_target_read_sp"); | |
32178cab MS |
1165 | } |
1166 | ||
1167 | CORE_ADDR | |
1168 | read_sp (void) | |
1169 | { | |
1170 | return TARGET_READ_SP (); | |
1171 | } | |
1172 | ||
32178cab MS |
1173 | void |
1174 | generic_target_write_sp (CORE_ADDR val) | |
1175 | { | |
1176 | #ifdef SP_REGNUM | |
1177 | if (SP_REGNUM >= 0) | |
1178 | { | |
1179 | write_register (SP_REGNUM, val); | |
1180 | return; | |
1181 | } | |
1182 | #endif | |
8e65ff28 AC |
1183 | internal_error (__FILE__, __LINE__, |
1184 | "generic_target_write_sp"); | |
32178cab MS |
1185 | } |
1186 | ||
1187 | void | |
1188 | write_sp (CORE_ADDR val) | |
1189 | { | |
1190 | TARGET_WRITE_SP (val); | |
1191 | } | |
1192 | ||
32178cab MS |
1193 | CORE_ADDR |
1194 | generic_target_read_fp (void) | |
1195 | { | |
1196 | #ifdef FP_REGNUM | |
1197 | if (FP_REGNUM >= 0) | |
1198 | return read_register (FP_REGNUM); | |
1199 | #endif | |
8e65ff28 AC |
1200 | internal_error (__FILE__, __LINE__, |
1201 | "generic_target_read_fp"); | |
32178cab MS |
1202 | } |
1203 | ||
1204 | CORE_ADDR | |
1205 | read_fp (void) | |
1206 | { | |
1207 | return TARGET_READ_FP (); | |
1208 | } | |
1209 | ||
705152c5 MS |
1210 | /* ARGSUSED */ |
1211 | static void | |
1212 | reg_flush_command (char *command, int from_tty) | |
1213 | { | |
1214 | /* Force-flush the register cache. */ | |
1215 | registers_changed (); | |
1216 | if (from_tty) | |
1217 | printf_filtered ("Register cache flushed.\n"); | |
1218 | } | |
1219 | ||
32178cab MS |
1220 | static void |
1221 | build_regcache (void) | |
3fadccb3 AC |
1222 | { |
1223 | current_regcache = regcache_xmalloc (current_gdbarch); | |
1224 | current_regcache->passthrough_p = 1; | |
1225 | registers = deprecated_grub_regcache_for_registers (current_regcache); | |
1226 | register_valid = deprecated_grub_regcache_for_register_valid (current_regcache); | |
1227 | } | |
1228 | ||
af030b9a AC |
1229 | static void |
1230 | dump_endian_bytes (struct ui_file *file, enum bfd_endian endian, | |
1231 | const unsigned char *buf, long len) | |
1232 | { | |
1233 | int i; | |
1234 | switch (endian) | |
1235 | { | |
1236 | case BFD_ENDIAN_BIG: | |
1237 | for (i = 0; i < len; i++) | |
1238 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1239 | break; | |
1240 | case BFD_ENDIAN_LITTLE: | |
1241 | for (i = len - 1; i >= 0; i--) | |
1242 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1243 | break; | |
1244 | default: | |
1245 | internal_error (__FILE__, __LINE__, "Bad switch"); | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | enum regcache_dump_what | |
1250 | { | |
1251 | regcache_dump_none, regcache_dump_raw, regcache_dump_cooked | |
1252 | }; | |
1253 | ||
1254 | static void | |
1255 | regcache_dump (struct regcache *regcache, struct ui_file *file, | |
1256 | enum regcache_dump_what what_to_dump) | |
1257 | { | |
1258 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); | |
1259 | int regnum; | |
1260 | int footnote_nr = 0; | |
1261 | int footnote_register_size = 0; | |
1262 | int footnote_register_offset = 0; | |
1263 | int footnote_register_type_name_null = 0; | |
1264 | long register_offset = 0; | |
1265 | unsigned char *buf = alloca (regcache->descr->max_register_size); | |
1266 | ||
1267 | #if 0 | |
1268 | fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p); | |
1269 | fprintf_unfiltered (file, "nr_raw_registers %d\n", | |
1270 | regcache->descr->nr_raw_registers); | |
1271 | fprintf_unfiltered (file, "nr_cooked_registers %d\n", | |
1272 | regcache->descr->nr_cooked_registers); | |
1273 | fprintf_unfiltered (file, "sizeof_raw_registers %ld\n", | |
1274 | regcache->descr->sizeof_raw_registers); | |
1275 | fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n", | |
1276 | regcache->descr->sizeof_raw_register_valid_p); | |
1277 | fprintf_unfiltered (file, "max_register_size %ld\n", | |
1278 | regcache->descr->max_register_size); | |
1279 | fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS); | |
1280 | fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS); | |
1281 | #endif | |
1282 | ||
1283 | gdb_assert (regcache->descr->nr_cooked_registers | |
1284 | == (NUM_REGS + NUM_PSEUDO_REGS)); | |
1285 | ||
1286 | for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++) | |
1287 | { | |
1288 | /* Name. */ | |
1289 | if (regnum < 0) | |
1290 | fprintf_unfiltered (file, " %-10s", "Name"); | |
1291 | else | |
1292 | { | |
1293 | const char *p = REGISTER_NAME (regnum); | |
1294 | if (p == NULL) | |
1295 | p = ""; | |
1296 | else if (p[0] == '\0') | |
1297 | p = "''"; | |
1298 | fprintf_unfiltered (file, " %-10s", p); | |
1299 | } | |
1300 | ||
1301 | /* Number. */ | |
1302 | if (regnum < 0) | |
1303 | fprintf_unfiltered (file, " %4s", "Nr"); | |
1304 | else | |
1305 | fprintf_unfiltered (file, " %4d", regnum); | |
1306 | ||
1307 | /* Relative number. */ | |
1308 | if (regnum < 0) | |
1309 | fprintf_unfiltered (file, " %4s", "Rel"); | |
1310 | else if (regnum < NUM_REGS) | |
1311 | fprintf_unfiltered (file, " %4d", regnum); | |
1312 | else | |
1313 | fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS)); | |
1314 | ||
1315 | /* Offset. */ | |
1316 | if (regnum < 0) | |
1317 | fprintf_unfiltered (file, " %6s ", "Offset"); | |
1318 | else | |
1319 | { | |
1320 | fprintf_unfiltered (file, " %6ld", | |
1321 | regcache->descr->register_offset[regnum]); | |
a7e3c2ad AC |
1322 | if (register_offset != regcache->descr->register_offset[regnum] |
1323 | || register_offset != REGISTER_BYTE (regnum)) | |
af030b9a AC |
1324 | { |
1325 | if (!footnote_register_offset) | |
1326 | footnote_register_offset = ++footnote_nr; | |
1327 | fprintf_unfiltered (file, "*%d", footnote_register_offset); | |
1328 | } | |
1329 | else | |
1330 | fprintf_unfiltered (file, " "); | |
1331 | register_offset = (regcache->descr->register_offset[regnum] | |
1332 | + regcache->descr->sizeof_register[regnum]); | |
1333 | } | |
1334 | ||
1335 | /* Size. */ | |
1336 | if (regnum < 0) | |
1337 | fprintf_unfiltered (file, " %5s ", "Size"); | |
1338 | else | |
1339 | { | |
1340 | fprintf_unfiltered (file, " %5ld", | |
1341 | regcache->descr->sizeof_register[regnum]); | |
1342 | if ((regcache->descr->sizeof_register[regnum] | |
1343 | != REGISTER_RAW_SIZE (regnum)) | |
1344 | || (regcache->descr->sizeof_register[regnum] | |
1345 | != REGISTER_VIRTUAL_SIZE (regnum)) | |
1346 | || (regcache->descr->sizeof_register[regnum] | |
1347 | != TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum))) | |
1348 | ) | |
1349 | { | |
1350 | if (!footnote_register_size) | |
1351 | footnote_register_size = ++footnote_nr; | |
1352 | fprintf_unfiltered (file, "*%d", footnote_register_size); | |
1353 | } | |
1354 | else | |
1355 | fprintf_unfiltered (file, " "); | |
1356 | } | |
1357 | ||
1358 | /* Type. */ | |
1359 | if (regnum < 0) | |
1360 | fprintf_unfiltered (file, " %-20s", "Type"); | |
1361 | else | |
1362 | { | |
1363 | static const char blt[] = "builtin_type"; | |
1364 | const char *t = TYPE_NAME (REGISTER_VIRTUAL_TYPE (regnum)); | |
1365 | if (t == NULL) | |
1366 | { | |
1367 | char *n; | |
1368 | if (!footnote_register_type_name_null) | |
1369 | footnote_register_type_name_null = ++footnote_nr; | |
1370 | xasprintf (&n, "*%d", footnote_register_type_name_null); | |
1371 | make_cleanup (xfree, n); | |
1372 | t = n; | |
1373 | } | |
1374 | /* Chop a leading builtin_type. */ | |
1375 | if (strncmp (t, blt, strlen (blt)) == 0) | |
1376 | t += strlen (blt); | |
1377 | fprintf_unfiltered (file, " %-20s", t); | |
1378 | } | |
1379 | ||
1380 | /* Value, raw. */ | |
1381 | if (what_to_dump == regcache_dump_raw) | |
1382 | { | |
1383 | if (regnum < 0) | |
1384 | fprintf_unfiltered (file, "Raw value"); | |
1385 | else if (regnum >= regcache->descr->nr_raw_registers) | |
1386 | fprintf_unfiltered (file, "<cooked>"); | |
1387 | else if (!regcache_valid_p (regcache, regnum)) | |
1388 | fprintf_unfiltered (file, "<invalid>"); | |
1389 | else | |
1390 | { | |
1391 | regcache_raw_read (regcache, regnum, buf); | |
1392 | fprintf_unfiltered (file, "0x"); | |
1393 | dump_endian_bytes (file, TARGET_BYTE_ORDER, buf, | |
1394 | REGISTER_RAW_SIZE (regnum)); | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | /* Value, cooked. */ | |
1399 | if (what_to_dump == regcache_dump_cooked) | |
1400 | { | |
1401 | if (regnum < 0) | |
1402 | fprintf_unfiltered (file, "Cooked value"); | |
1403 | else | |
1404 | { | |
1405 | regcache_cooked_read (regcache, regnum, buf); | |
1406 | fprintf_unfiltered (file, "0x"); | |
1407 | dump_endian_bytes (file, TARGET_BYTE_ORDER, buf, | |
1408 | REGISTER_VIRTUAL_SIZE (regnum)); | |
1409 | } | |
1410 | } | |
1411 | ||
1412 | fprintf_unfiltered (file, "\n"); | |
1413 | } | |
1414 | ||
1415 | if (footnote_register_size) | |
1416 | fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n", | |
1417 | footnote_register_size); | |
1418 | if (footnote_register_offset) | |
1419 | fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n", | |
1420 | footnote_register_offset); | |
1421 | if (footnote_register_type_name_null) | |
1422 | fprintf_unfiltered (file, | |
1423 | "*%d: Register type's name NULL.\n", | |
1424 | footnote_register_type_name_null); | |
1425 | do_cleanups (cleanups); | |
1426 | } | |
1427 | ||
1428 | static void | |
1429 | regcache_print (char *args, enum regcache_dump_what what_to_dump) | |
1430 | { | |
1431 | if (args == NULL) | |
1432 | regcache_dump (current_regcache, gdb_stdout, what_to_dump); | |
1433 | else | |
1434 | { | |
1435 | struct ui_file *file = gdb_fopen (args, "w"); | |
1436 | if (file == NULL) | |
1437 | perror_with_name ("maintenance print architecture"); | |
1438 | regcache_dump (current_regcache, file, what_to_dump); | |
1439 | ui_file_delete (file); | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | static void | |
1444 | maintenance_print_registers (char *args, int from_tty) | |
1445 | { | |
1446 | regcache_print (args, regcache_dump_none); | |
1447 | } | |
1448 | ||
1449 | static void | |
1450 | maintenance_print_raw_registers (char *args, int from_tty) | |
1451 | { | |
1452 | regcache_print (args, regcache_dump_raw); | |
1453 | } | |
1454 | ||
1455 | static void | |
1456 | maintenance_print_cooked_registers (char *args, int from_tty) | |
1457 | { | |
1458 | regcache_print (args, regcache_dump_cooked); | |
1459 | } | |
1460 | ||
32178cab MS |
1461 | void |
1462 | _initialize_regcache (void) | |
1463 | { | |
3fadccb3 AC |
1464 | regcache_descr_handle = register_gdbarch_data (init_regcache_descr, |
1465 | xfree_regcache_descr); | |
1466 | REGISTER_GDBARCH_SWAP (current_regcache); | |
32178cab MS |
1467 | register_gdbarch_swap (®isters, sizeof (registers), NULL); |
1468 | register_gdbarch_swap (®ister_valid, sizeof (register_valid), NULL); | |
1469 | register_gdbarch_swap (NULL, 0, build_regcache); | |
705152c5 MS |
1470 | |
1471 | add_com ("flushregs", class_maintenance, reg_flush_command, | |
1472 | "Force gdb to flush its register cache (maintainer command)"); | |
39f77062 KB |
1473 | |
1474 | /* Initialize the thread/process associated with the current set of | |
1475 | registers. For now, -1 is special, and means `no current process'. */ | |
1476 | registers_ptid = pid_to_ptid (-1); | |
af030b9a AC |
1477 | |
1478 | add_cmd ("registers", class_maintenance, | |
1479 | maintenance_print_registers, | |
1480 | "Print the internal register configuration.\ | |
1481 | Takes an optional file parameter.", | |
1482 | &maintenanceprintlist); | |
1483 | add_cmd ("raw-registers", class_maintenance, | |
1484 | maintenance_print_raw_registers, | |
1485 | "Print the internal register configuration including raw values.\ | |
1486 | Takes an optional file parameter.", | |
1487 | &maintenanceprintlist); | |
1488 | add_cmd ("cooked-registers", class_maintenance, | |
1489 | maintenance_print_cooked_registers, | |
1490 | "Print the internal register configuration including cooked values.\ | |
1491 | Takes an optional file parameter.", | |
1492 | &maintenanceprintlist); | |
1493 | ||
32178cab | 1494 | } |