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