Commit | Line | Data |
---|---|---|
32178cab | 1 | /* Cache and manage the values of registers for GDB, the GNU debugger. |
3fadccb3 | 2 | |
e2882c85 | 3 | Copyright (C) 1986-2018 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" |
76727919 | 27 | #include "observable.h" |
0b309272 | 28 | #include "regset.h" |
94bb8dfe | 29 | #include <forward_list> |
32178cab MS |
30 | |
31 | /* | |
32 | * DATA STRUCTURE | |
33 | * | |
34 | * Here is the actual register cache. | |
35 | */ | |
36 | ||
3fadccb3 | 37 | /* Per-architecture object describing the layout of a register cache. |
0df8b418 | 38 | Computed once when the architecture is created. */ |
3fadccb3 AC |
39 | |
40 | struct gdbarch_data *regcache_descr_handle; | |
41 | ||
42 | struct regcache_descr | |
43 | { | |
44 | /* The architecture this descriptor belongs to. */ | |
45 | struct gdbarch *gdbarch; | |
46 | ||
bb1db049 AC |
47 | /* The raw register cache. Each raw (or hard) register is supplied |
48 | by the target interface. The raw cache should not contain | |
49 | redundant information - if the PC is constructed from two | |
d2f0b918 | 50 | registers then those registers and not the PC lives in the raw |
bb1db049 | 51 | cache. */ |
3fadccb3 | 52 | long sizeof_raw_registers; |
3fadccb3 | 53 | |
d138e37a AC |
54 | /* The cooked register space. Each cooked register in the range |
55 | [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw | |
56 | register. The remaining [NR_RAW_REGISTERS | |
02f60eae | 57 | .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto |
d138e37a | 58 | both raw registers and memory by the architecture methods |
02f60eae | 59 | gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */ |
d138e37a | 60 | int nr_cooked_registers; |
067df2e5 | 61 | long sizeof_cooked_registers; |
d138e37a | 62 | |
86d31898 | 63 | /* Offset and size (in 8 bit bytes), of each register in the |
d138e37a | 64 | register cache. All registers (including those in the range |
99e42fd8 PA |
65 | [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an |
66 | offset. */ | |
3fadccb3 | 67 | long *register_offset; |
3fadccb3 | 68 | long *sizeof_register; |
3fadccb3 | 69 | |
bb425013 AC |
70 | /* Cached table containing the type of each register. */ |
71 | struct type **register_type; | |
3fadccb3 AC |
72 | }; |
73 | ||
3fadccb3 AC |
74 | static void * |
75 | init_regcache_descr (struct gdbarch *gdbarch) | |
76 | { | |
77 | int i; | |
78 | struct regcache_descr *descr; | |
79 | gdb_assert (gdbarch != NULL); | |
80 | ||
bb425013 | 81 | /* Create an initial, zero filled, table. */ |
116f06ea | 82 | descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr); |
3fadccb3 | 83 | descr->gdbarch = gdbarch; |
3fadccb3 | 84 | |
d138e37a AC |
85 | /* Total size of the register space. The raw registers are mapped |
86 | directly onto the raw register cache while the pseudo's are | |
3fadccb3 | 87 | either mapped onto raw-registers or memory. */ |
214e098a UW |
88 | descr->nr_cooked_registers = gdbarch_num_regs (gdbarch) |
89 | + gdbarch_num_pseudo_regs (gdbarch); | |
3fadccb3 | 90 | |
bb425013 | 91 | /* Fill in a table of register types. */ |
116f06ea | 92 | descr->register_type |
3e43a32a MS |
93 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, |
94 | struct type *); | |
bb425013 | 95 | for (i = 0; i < descr->nr_cooked_registers; i++) |
336a3131 | 96 | descr->register_type[i] = gdbarch_register_type (gdbarch, i); |
bb425013 | 97 | |
bb1db049 AC |
98 | /* Construct a strictly RAW register cache. Don't allow pseudo's |
99 | into the register cache. */ | |
bb1db049 | 100 | |
067df2e5 | 101 | /* Lay out the register cache. |
3fadccb3 | 102 | |
bb425013 AC |
103 | NOTE: cagney/2002-05-22: Only register_type() is used when |
104 | constructing the register cache. It is assumed that the | |
105 | register's raw size, virtual size and type length are all the | |
106 | same. */ | |
3fadccb3 AC |
107 | |
108 | { | |
109 | long offset = 0; | |
123f5f96 | 110 | |
116f06ea AC |
111 | descr->sizeof_register |
112 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
113 | descr->register_offset | |
114 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
d999647b | 115 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
99e42fd8 PA |
116 | { |
117 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); | |
118 | descr->register_offset[i] = offset; | |
119 | offset += descr->sizeof_register[i]; | |
99e42fd8 PA |
120 | } |
121 | /* Set the real size of the raw register cache buffer. */ | |
122 | descr->sizeof_raw_registers = offset; | |
123 | ||
124 | for (; i < descr->nr_cooked_registers; i++) | |
3fadccb3 | 125 | { |
bb425013 | 126 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); |
3fadccb3 AC |
127 | descr->register_offset[i] = offset; |
128 | offset += descr->sizeof_register[i]; | |
3fadccb3 | 129 | } |
99e42fd8 | 130 | /* Set the real size of the readonly register cache buffer. */ |
067df2e5 | 131 | descr->sizeof_cooked_registers = offset; |
3fadccb3 AC |
132 | } |
133 | ||
3fadccb3 AC |
134 | return descr; |
135 | } | |
136 | ||
137 | static struct regcache_descr * | |
138 | regcache_descr (struct gdbarch *gdbarch) | |
139 | { | |
19ba03f4 SM |
140 | return (struct regcache_descr *) gdbarch_data (gdbarch, |
141 | regcache_descr_handle); | |
3fadccb3 AC |
142 | } |
143 | ||
bb425013 AC |
144 | /* Utility functions returning useful register attributes stored in |
145 | the regcache descr. */ | |
146 | ||
147 | struct type * | |
148 | register_type (struct gdbarch *gdbarch, int regnum) | |
149 | { | |
150 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
123f5f96 | 151 | |
bb425013 AC |
152 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
153 | return descr->register_type[regnum]; | |
154 | } | |
155 | ||
0ed04cce AC |
156 | /* Utility functions returning useful register attributes stored in |
157 | the regcache descr. */ | |
158 | ||
08a617da AC |
159 | int |
160 | register_size (struct gdbarch *gdbarch, int regnum) | |
161 | { | |
162 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
163 | int size; | |
123f5f96 | 164 | |
f57d151a | 165 | gdb_assert (regnum >= 0 |
214e098a UW |
166 | && regnum < (gdbarch_num_regs (gdbarch) |
167 | + gdbarch_num_pseudo_regs (gdbarch))); | |
08a617da | 168 | size = descr->sizeof_register[regnum]; |
08a617da AC |
169 | return size; |
170 | } | |
171 | ||
8d689ee5 YQ |
172 | /* See common/common-regcache.h. */ |
173 | ||
174 | int | |
175 | regcache_register_size (const struct regcache *regcache, int n) | |
176 | { | |
ac7936df | 177 | return register_size (regcache->arch (), n); |
8d689ee5 YQ |
178 | } |
179 | ||
31716595 YQ |
180 | reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo) |
181 | : m_has_pseudo (has_pseudo) | |
3fadccb3 | 182 | { |
ef79d9a3 YQ |
183 | gdb_assert (gdbarch != NULL); |
184 | m_descr = regcache_descr (gdbarch); | |
4621115f | 185 | |
31716595 | 186 | if (has_pseudo) |
4621115f | 187 | { |
ef79d9a3 YQ |
188 | m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers); |
189 | m_register_status = XCNEWVEC (signed char, | |
6c5218df | 190 | m_descr->nr_cooked_registers); |
4621115f YQ |
191 | } |
192 | else | |
193 | { | |
ef79d9a3 | 194 | m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers); |
d999647b | 195 | m_register_status = XCNEWVEC (signed char, gdbarch_num_regs (gdbarch)); |
4621115f | 196 | } |
31716595 YQ |
197 | } |
198 | ||
796bb026 YQ |
199 | regcache::regcache (gdbarch *gdbarch, const address_space *aspace_) |
200 | /* The register buffers. A read/write register cache can only hold | |
201 | [0 .. gdbarch_num_regs). */ | |
202 | : detached_regcache (gdbarch, false), m_aspace (aspace_) | |
31716595 | 203 | { |
ef79d9a3 YQ |
204 | m_ptid = minus_one_ptid; |
205 | } | |
4621115f | 206 | |
deb1fa3e YQ |
207 | static enum register_status |
208 | do_cooked_read (void *src, int regnum, gdb_byte *buf) | |
209 | { | |
210 | struct regcache *regcache = (struct regcache *) src; | |
211 | ||
212 | return regcache_cooked_read (regcache, regnum, buf); | |
213 | } | |
214 | ||
daf6667d YQ |
215 | readonly_detached_regcache::readonly_detached_regcache (const regcache &src) |
216 | : readonly_detached_regcache (src.arch (), do_cooked_read, (void *) &src) | |
217 | { | |
218 | } | |
219 | ||
ef79d9a3 | 220 | gdbarch * |
31716595 | 221 | reg_buffer::arch () const |
ef79d9a3 YQ |
222 | { |
223 | return m_descr->gdbarch; | |
224 | } | |
3fadccb3 | 225 | |
ddaaf0fb SM |
226 | /* See regcache.h. */ |
227 | ||
228 | ptid_t | |
229 | regcache_get_ptid (const struct regcache *regcache) | |
230 | { | |
ef79d9a3 | 231 | gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid)); |
ddaaf0fb | 232 | |
ef79d9a3 | 233 | return regcache->ptid (); |
ddaaf0fb SM |
234 | } |
235 | ||
b292235f | 236 | /* Cleanup class for invalidating a register. */ |
b94ade42 | 237 | |
b292235f | 238 | class regcache_invalidator |
b94ade42 | 239 | { |
b292235f | 240 | public: |
b94ade42 | 241 | |
b292235f TT |
242 | regcache_invalidator (struct regcache *regcache, int regnum) |
243 | : m_regcache (regcache), | |
244 | m_regnum (regnum) | |
245 | { | |
246 | } | |
b94ade42 | 247 | |
b292235f TT |
248 | ~regcache_invalidator () |
249 | { | |
250 | if (m_regcache != nullptr) | |
251 | regcache_invalidate (m_regcache, m_regnum); | |
252 | } | |
b94ade42 | 253 | |
b292235f | 254 | DISABLE_COPY_AND_ASSIGN (regcache_invalidator); |
b94ade42 | 255 | |
b292235f TT |
256 | void release () |
257 | { | |
258 | m_regcache = nullptr; | |
259 | } | |
260 | ||
261 | private: | |
262 | ||
263 | struct regcache *m_regcache; | |
264 | int m_regnum; | |
265 | }; | |
b94ade42 | 266 | |
51b1fe4e AC |
267 | /* Return a pointer to register REGNUM's buffer cache. */ |
268 | ||
ef79d9a3 | 269 | gdb_byte * |
31716595 | 270 | reg_buffer::register_buffer (int regnum) const |
51b1fe4e | 271 | { |
ef79d9a3 | 272 | return m_registers + m_descr->register_offset[regnum]; |
51b1fe4e AC |
273 | } |
274 | ||
ef79d9a3 | 275 | void |
daf6667d YQ |
276 | reg_buffer::save (regcache_cooked_read_ftype *cooked_read, |
277 | void *src) | |
ef79d9a3 YQ |
278 | { |
279 | struct gdbarch *gdbarch = m_descr->gdbarch; | |
2d28509a | 280 | int regnum; |
123f5f96 | 281 | |
daf6667d YQ |
282 | /* It should have pseudo registers. */ |
283 | gdb_assert (m_has_pseudo); | |
2d28509a | 284 | /* Clear the dest. */ |
ef79d9a3 | 285 | memset (m_registers, 0, m_descr->sizeof_cooked_registers); |
6c5218df | 286 | memset (m_register_status, 0, m_descr->nr_cooked_registers); |
2d28509a | 287 | /* Copy over any registers (identified by their membership in the |
f57d151a UW |
288 | save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs + |
289 | gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a | 290 | to save/restore `cooked' registers that live in memory. */ |
ef79d9a3 | 291 | for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) |
2d28509a AC |
292 | { |
293 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
294 | { | |
50d6adef AH |
295 | gdb_byte *dst_buf = register_buffer (regnum); |
296 | enum register_status status = cooked_read (src, regnum, dst_buf); | |
123f5f96 | 297 | |
50d6adef AH |
298 | gdb_assert (status != REG_UNKNOWN); |
299 | ||
300 | if (status != REG_VALID) | |
301 | memset (dst_buf, 0, register_size (gdbarch, regnum)); | |
05d1431c | 302 | |
ef79d9a3 | 303 | m_register_status[regnum] = status; |
2d28509a AC |
304 | } |
305 | } | |
306 | } | |
307 | ||
ef79d9a3 | 308 | void |
daf6667d | 309 | regcache::restore (readonly_detached_regcache *src) |
2d28509a | 310 | { |
ef79d9a3 | 311 | struct gdbarch *gdbarch = m_descr->gdbarch; |
2d28509a | 312 | int regnum; |
123f5f96 | 313 | |
fc5b8736 | 314 | gdb_assert (src != NULL); |
daf6667d | 315 | gdb_assert (src->m_has_pseudo); |
fc5b8736 YQ |
316 | |
317 | gdb_assert (gdbarch == src->arch ()); | |
318 | ||
2d28509a | 319 | /* Copy over any registers, being careful to only restore those that |
f57d151a UW |
320 | were both saved and need to be restored. The full [0 .. gdbarch_num_regs |
321 | + gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a | 322 | to save/restore `cooked' registers that live in memory. */ |
ef79d9a3 | 323 | for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) |
2d28509a | 324 | { |
5602984a | 325 | if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)) |
2d28509a | 326 | { |
ef79d9a3 YQ |
327 | if (src->m_register_status[regnum] == REG_VALID) |
328 | cooked_write (regnum, src->register_buffer (regnum)); | |
2d28509a AC |
329 | } |
330 | } | |
331 | } | |
332 | ||
39181896 | 333 | enum register_status |
ee99023e | 334 | regcache_register_status (const struct regcache *regcache, int regnum) |
3fadccb3 AC |
335 | { |
336 | gdb_assert (regcache != NULL); | |
ef79d9a3 YQ |
337 | return regcache->get_register_status (regnum); |
338 | } | |
339 | ||
340 | enum register_status | |
c8ec2f33 | 341 | reg_buffer::get_register_status (int regnum) const |
ef79d9a3 | 342 | { |
c8ec2f33 | 343 | assert_regnum (regnum); |
6ed7ea50 | 344 | |
ef79d9a3 | 345 | return (enum register_status) m_register_status[regnum]; |
3fadccb3 AC |
346 | } |
347 | ||
9c5ea4d9 UW |
348 | void |
349 | regcache_invalidate (struct regcache *regcache, int regnum) | |
350 | { | |
351 | gdb_assert (regcache != NULL); | |
ef79d9a3 | 352 | regcache->invalidate (regnum); |
9c5ea4d9 UW |
353 | } |
354 | ||
ef79d9a3 | 355 | void |
796bb026 | 356 | detached_regcache::invalidate (int regnum) |
ef79d9a3 | 357 | { |
4e888c28 | 358 | assert_regnum (regnum); |
ef79d9a3 YQ |
359 | m_register_status[regnum] = REG_UNKNOWN; |
360 | } | |
9c5ea4d9 | 361 | |
4e888c28 | 362 | void |
31716595 | 363 | reg_buffer::assert_regnum (int regnum) const |
4e888c28 | 364 | { |
31716595 YQ |
365 | gdb_assert (regnum >= 0); |
366 | if (m_has_pseudo) | |
367 | gdb_assert (regnum < m_descr->nr_cooked_registers); | |
368 | else | |
369 | gdb_assert (regnum < gdbarch_num_regs (arch ())); | |
4e888c28 YQ |
370 | } |
371 | ||
3fadccb3 | 372 | /* Global structure containing the current regcache. */ |
3fadccb3 | 373 | |
5ebd2499 | 374 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
32178cab MS |
375 | recording if the register values have been changed (eg. by the |
376 | user). Therefore all registers must be written back to the | |
377 | target when appropriate. */ | |
e521e87e | 378 | std::forward_list<regcache *> regcache::current_regcache; |
c2250ad1 UW |
379 | |
380 | struct regcache * | |
e2d96639 YQ |
381 | get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch, |
382 | struct address_space *aspace) | |
c2250ad1 | 383 | { |
e521e87e | 384 | for (const auto ®cache : regcache::current_regcache) |
94bb8dfe YQ |
385 | if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch) |
386 | return regcache; | |
594f7785 | 387 | |
796bb026 | 388 | regcache *new_regcache = new regcache (gdbarch, aspace); |
594f7785 | 389 | |
e521e87e | 390 | regcache::current_regcache.push_front (new_regcache); |
ef79d9a3 | 391 | new_regcache->set_ptid (ptid); |
e2d96639 | 392 | |
e2d96639 YQ |
393 | return new_regcache; |
394 | } | |
395 | ||
396 | struct regcache * | |
397 | get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch) | |
398 | { | |
ed4227b7 | 399 | address_space *aspace = target_thread_address_space (ptid); |
b78974c3 | 400 | |
e2d96639 | 401 | return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace); |
594f7785 UW |
402 | } |
403 | ||
c2250ad1 UW |
404 | static ptid_t current_thread_ptid; |
405 | static struct gdbarch *current_thread_arch; | |
406 | ||
407 | struct regcache * | |
408 | get_thread_regcache (ptid_t ptid) | |
409 | { | |
410 | if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid)) | |
411 | { | |
412 | current_thread_ptid = ptid; | |
413 | current_thread_arch = target_thread_architecture (ptid); | |
414 | } | |
415 | ||
416 | return get_thread_arch_regcache (ptid, current_thread_arch); | |
417 | } | |
418 | ||
419 | struct regcache * | |
420 | get_current_regcache (void) | |
594f7785 UW |
421 | { |
422 | return get_thread_regcache (inferior_ptid); | |
423 | } | |
32178cab | 424 | |
361c8ade GB |
425 | /* See common/common-regcache.h. */ |
426 | ||
427 | struct regcache * | |
428 | get_thread_regcache_for_ptid (ptid_t ptid) | |
429 | { | |
430 | return get_thread_regcache (ptid); | |
431 | } | |
32178cab | 432 | |
f4c5303c OF |
433 | /* Observer for the target_changed event. */ |
434 | ||
2c0b251b | 435 | static void |
f4c5303c OF |
436 | regcache_observer_target_changed (struct target_ops *target) |
437 | { | |
438 | registers_changed (); | |
439 | } | |
440 | ||
5231c1fd PA |
441 | /* Update global variables old ptids to hold NEW_PTID if they were |
442 | holding OLD_PTID. */ | |
e521e87e YQ |
443 | void |
444 | regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) | |
5231c1fd | 445 | { |
e521e87e | 446 | for (auto ®cache : regcache::current_regcache) |
94bb8dfe YQ |
447 | { |
448 | if (ptid_equal (regcache->ptid (), old_ptid)) | |
449 | regcache->set_ptid (new_ptid); | |
450 | } | |
5231c1fd PA |
451 | } |
452 | ||
32178cab MS |
453 | /* Low level examining and depositing of registers. |
454 | ||
455 | The caller is responsible for making sure that the inferior is | |
456 | stopped before calling the fetching routines, or it will get | |
457 | garbage. (a change from GDB version 3, in which the caller got the | |
458 | value from the last stop). */ | |
459 | ||
460 | /* REGISTERS_CHANGED () | |
461 | ||
462 | Indicate that registers may have changed, so invalidate the cache. */ | |
463 | ||
464 | void | |
e66408ed | 465 | registers_changed_ptid (ptid_t ptid) |
32178cab | 466 | { |
e521e87e | 467 | for (auto oit = regcache::current_regcache.before_begin (), |
94bb8dfe | 468 | it = std::next (oit); |
e521e87e | 469 | it != regcache::current_regcache.end (); |
94bb8dfe | 470 | ) |
c2250ad1 | 471 | { |
94bb8dfe | 472 | if (ptid_match ((*it)->ptid (), ptid)) |
e66408ed | 473 | { |
94bb8dfe | 474 | delete *it; |
e521e87e | 475 | it = regcache::current_regcache.erase_after (oit); |
e66408ed | 476 | } |
94bb8dfe YQ |
477 | else |
478 | oit = it++; | |
c2250ad1 | 479 | } |
32178cab | 480 | |
c34fd852 | 481 | if (ptid_match (current_thread_ptid, ptid)) |
041274d8 PA |
482 | { |
483 | current_thread_ptid = null_ptid; | |
484 | current_thread_arch = NULL; | |
485 | } | |
32178cab | 486 | |
c34fd852 | 487 | if (ptid_match (inferior_ptid, ptid)) |
041274d8 PA |
488 | { |
489 | /* We just deleted the regcache of the current thread. Need to | |
490 | forget about any frames we have cached, too. */ | |
491 | reinit_frame_cache (); | |
492 | } | |
493 | } | |
c2250ad1 | 494 | |
041274d8 PA |
495 | void |
496 | registers_changed (void) | |
497 | { | |
498 | registers_changed_ptid (minus_one_ptid); | |
a5d9d57d | 499 | |
32178cab MS |
500 | /* Force cleanup of any alloca areas if using C alloca instead of |
501 | a builtin alloca. This particular call is used to clean up | |
502 | areas allocated by low level target code which may build up | |
503 | during lengthy interactions between gdb and the target before | |
504 | gdb gives control to the user (ie watchpoints). */ | |
505 | alloca (0); | |
32178cab MS |
506 | } |
507 | ||
8e368124 AH |
508 | void |
509 | regcache_raw_update (struct regcache *regcache, int regnum) | |
61a0eb5b | 510 | { |
8e368124 | 511 | gdb_assert (regcache != NULL); |
ef79d9a3 YQ |
512 | |
513 | regcache->raw_update (regnum); | |
514 | } | |
515 | ||
516 | void | |
517 | regcache::raw_update (int regnum) | |
518 | { | |
4e888c28 | 519 | assert_regnum (regnum); |
8e368124 | 520 | |
3fadccb3 AC |
521 | /* Make certain that the register cache is up-to-date with respect |
522 | to the current thread. This switching shouldn't be necessary | |
523 | only there is still only one target side register cache. Sigh! | |
524 | On the bright side, at least there is a regcache object. */ | |
8e368124 | 525 | |
796bb026 | 526 | if (get_register_status (regnum) == REG_UNKNOWN) |
3fadccb3 | 527 | { |
ef79d9a3 | 528 | target_fetch_registers (this, regnum); |
788c8b10 PA |
529 | |
530 | /* A number of targets can't access the whole set of raw | |
531 | registers (because the debug API provides no means to get at | |
532 | them). */ | |
ef79d9a3 YQ |
533 | if (m_register_status[regnum] == REG_UNKNOWN) |
534 | m_register_status[regnum] = REG_UNAVAILABLE; | |
3fadccb3 | 535 | } |
8e368124 AH |
536 | } |
537 | ||
538 | enum register_status | |
539 | regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf) | |
ef79d9a3 YQ |
540 | { |
541 | return regcache->raw_read (regnum, buf); | |
542 | } | |
543 | ||
544 | enum register_status | |
849d0ba8 | 545 | readable_regcache::raw_read (int regnum, gdb_byte *buf) |
8e368124 AH |
546 | { |
547 | gdb_assert (buf != NULL); | |
ef79d9a3 | 548 | raw_update (regnum); |
05d1431c | 549 | |
ef79d9a3 YQ |
550 | if (m_register_status[regnum] != REG_VALID) |
551 | memset (buf, 0, m_descr->sizeof_register[regnum]); | |
05d1431c | 552 | else |
ef79d9a3 YQ |
553 | memcpy (buf, register_buffer (regnum), |
554 | m_descr->sizeof_register[regnum]); | |
05d1431c | 555 | |
ef79d9a3 | 556 | return (enum register_status) m_register_status[regnum]; |
61a0eb5b AC |
557 | } |
558 | ||
05d1431c | 559 | enum register_status |
28fc6740 | 560 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) |
ef79d9a3 YQ |
561 | { |
562 | gdb_assert (regcache != NULL); | |
6f98355c | 563 | return regcache->raw_read (regnum, val); |
ef79d9a3 YQ |
564 | } |
565 | ||
6f98355c | 566 | template<typename T, typename> |
ef79d9a3 | 567 | enum register_status |
849d0ba8 | 568 | readable_regcache::raw_read (int regnum, T *val) |
28fc6740 | 569 | { |
2d522557 | 570 | gdb_byte *buf; |
05d1431c | 571 | enum register_status status; |
123f5f96 | 572 | |
4e888c28 | 573 | assert_regnum (regnum); |
ef79d9a3 YQ |
574 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); |
575 | status = raw_read (regnum, buf); | |
05d1431c | 576 | if (status == REG_VALID) |
6f98355c YQ |
577 | *val = extract_integer<T> (buf, |
578 | m_descr->sizeof_register[regnum], | |
579 | gdbarch_byte_order (m_descr->gdbarch)); | |
05d1431c PA |
580 | else |
581 | *val = 0; | |
582 | return status; | |
28fc6740 AC |
583 | } |
584 | ||
05d1431c | 585 | enum register_status |
28fc6740 AC |
586 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, |
587 | ULONGEST *val) | |
ef79d9a3 YQ |
588 | { |
589 | gdb_assert (regcache != NULL); | |
6f98355c | 590 | return regcache->raw_read (regnum, val); |
28fc6740 AC |
591 | } |
592 | ||
c00dcbe9 MK |
593 | void |
594 | regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) | |
ef79d9a3 YQ |
595 | { |
596 | gdb_assert (regcache != NULL); | |
6f98355c | 597 | regcache->raw_write (regnum, val); |
ef79d9a3 YQ |
598 | } |
599 | ||
6f98355c | 600 | template<typename T, typename> |
ef79d9a3 | 601 | void |
6f98355c | 602 | regcache::raw_write (int regnum, T val) |
c00dcbe9 | 603 | { |
7c543f7b | 604 | gdb_byte *buf; |
123f5f96 | 605 | |
4e888c28 | 606 | assert_regnum (regnum); |
ef79d9a3 | 607 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); |
6f98355c YQ |
608 | store_integer (buf, m_descr->sizeof_register[regnum], |
609 | gdbarch_byte_order (m_descr->gdbarch), val); | |
ef79d9a3 | 610 | raw_write (regnum, buf); |
c00dcbe9 MK |
611 | } |
612 | ||
613 | void | |
614 | regcache_raw_write_unsigned (struct regcache *regcache, int regnum, | |
615 | ULONGEST val) | |
ef79d9a3 YQ |
616 | { |
617 | gdb_assert (regcache != NULL); | |
6f98355c | 618 | regcache->raw_write (regnum, val); |
c00dcbe9 MK |
619 | } |
620 | ||
9fd15b2e YQ |
621 | LONGEST |
622 | regcache_raw_get_signed (struct regcache *regcache, int regnum) | |
623 | { | |
624 | LONGEST value; | |
625 | enum register_status status; | |
626 | ||
627 | status = regcache_raw_read_signed (regcache, regnum, &value); | |
628 | if (status == REG_UNAVAILABLE) | |
629 | throw_error (NOT_AVAILABLE_ERROR, | |
630 | _("Register %d is not available"), regnum); | |
631 | return value; | |
632 | } | |
633 | ||
05d1431c | 634 | enum register_status |
2d522557 | 635 | regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
ef79d9a3 YQ |
636 | { |
637 | return regcache->cooked_read (regnum, buf); | |
638 | } | |
639 | ||
640 | enum register_status | |
849d0ba8 | 641 | readable_regcache::cooked_read (int regnum, gdb_byte *buf) |
68365089 | 642 | { |
d138e37a | 643 | gdb_assert (regnum >= 0); |
ef79d9a3 | 644 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
d999647b | 645 | if (regnum < num_raw_registers ()) |
ef79d9a3 | 646 | return raw_read (regnum, buf); |
849d0ba8 | 647 | else if (m_has_pseudo |
ef79d9a3 | 648 | && m_register_status[regnum] != REG_UNKNOWN) |
05d1431c | 649 | { |
ef79d9a3 YQ |
650 | if (m_register_status[regnum] == REG_VALID) |
651 | memcpy (buf, register_buffer (regnum), | |
652 | m_descr->sizeof_register[regnum]); | |
05d1431c | 653 | else |
ef79d9a3 | 654 | memset (buf, 0, m_descr->sizeof_register[regnum]); |
05d1431c | 655 | |
ef79d9a3 | 656 | return (enum register_status) m_register_status[regnum]; |
05d1431c | 657 | } |
ef79d9a3 | 658 | else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) |
3543a589 TT |
659 | { |
660 | struct value *mark, *computed; | |
661 | enum register_status result = REG_VALID; | |
662 | ||
663 | mark = value_mark (); | |
664 | ||
ef79d9a3 YQ |
665 | computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch, |
666 | this, regnum); | |
3543a589 TT |
667 | if (value_entirely_available (computed)) |
668 | memcpy (buf, value_contents_raw (computed), | |
ef79d9a3 | 669 | m_descr->sizeof_register[regnum]); |
3543a589 TT |
670 | else |
671 | { | |
ef79d9a3 | 672 | memset (buf, 0, m_descr->sizeof_register[regnum]); |
3543a589 TT |
673 | result = REG_UNAVAILABLE; |
674 | } | |
675 | ||
676 | value_free_to_mark (mark); | |
677 | ||
678 | return result; | |
679 | } | |
d138e37a | 680 | else |
ef79d9a3 | 681 | return gdbarch_pseudo_register_read (m_descr->gdbarch, this, |
05d1431c | 682 | regnum, buf); |
61a0eb5b AC |
683 | } |
684 | ||
3543a589 TT |
685 | struct value * |
686 | regcache_cooked_read_value (struct regcache *regcache, int regnum) | |
ef79d9a3 YQ |
687 | { |
688 | return regcache->cooked_read_value (regnum); | |
689 | } | |
690 | ||
691 | struct value * | |
849d0ba8 | 692 | readable_regcache::cooked_read_value (int regnum) |
3543a589 TT |
693 | { |
694 | gdb_assert (regnum >= 0); | |
ef79d9a3 | 695 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
3543a589 | 696 | |
d999647b | 697 | if (regnum < num_raw_registers () |
849d0ba8 | 698 | || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN) |
ef79d9a3 | 699 | || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) |
3543a589 TT |
700 | { |
701 | struct value *result; | |
702 | ||
ef79d9a3 | 703 | result = allocate_value (register_type (m_descr->gdbarch, regnum)); |
3543a589 TT |
704 | VALUE_LVAL (result) = lval_register; |
705 | VALUE_REGNUM (result) = regnum; | |
706 | ||
707 | /* It is more efficient in general to do this delegation in this | |
708 | direction than in the other one, even though the value-based | |
709 | API is preferred. */ | |
ef79d9a3 YQ |
710 | if (cooked_read (regnum, |
711 | value_contents_raw (result)) == REG_UNAVAILABLE) | |
3543a589 TT |
712 | mark_value_bytes_unavailable (result, 0, |
713 | TYPE_LENGTH (value_type (result))); | |
714 | ||
715 | return result; | |
716 | } | |
717 | else | |
ef79d9a3 YQ |
718 | return gdbarch_pseudo_register_read_value (m_descr->gdbarch, |
719 | this, regnum); | |
3543a589 TT |
720 | } |
721 | ||
05d1431c | 722 | enum register_status |
a378f419 AC |
723 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, |
724 | LONGEST *val) | |
ef79d9a3 YQ |
725 | { |
726 | gdb_assert (regcache != NULL); | |
6f98355c | 727 | return regcache->cooked_read (regnum, val); |
ef79d9a3 YQ |
728 | } |
729 | ||
6f98355c | 730 | template<typename T, typename> |
ef79d9a3 | 731 | enum register_status |
849d0ba8 | 732 | readable_regcache::cooked_read (int regnum, T *val) |
a378f419 | 733 | { |
05d1431c | 734 | enum register_status status; |
2d522557 | 735 | gdb_byte *buf; |
123f5f96 | 736 | |
ef79d9a3 YQ |
737 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); |
738 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); | |
739 | status = cooked_read (regnum, buf); | |
05d1431c | 740 | if (status == REG_VALID) |
6f98355c YQ |
741 | *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum], |
742 | gdbarch_byte_order (m_descr->gdbarch)); | |
05d1431c PA |
743 | else |
744 | *val = 0; | |
745 | return status; | |
a378f419 AC |
746 | } |
747 | ||
05d1431c | 748 | enum register_status |
a378f419 AC |
749 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, |
750 | ULONGEST *val) | |
ef79d9a3 YQ |
751 | { |
752 | gdb_assert (regcache != NULL); | |
6f98355c | 753 | return regcache->cooked_read (regnum, val); |
a378f419 AC |
754 | } |
755 | ||
a66a9c23 AC |
756 | void |
757 | regcache_cooked_write_signed (struct regcache *regcache, int regnum, | |
758 | LONGEST val) | |
ef79d9a3 YQ |
759 | { |
760 | gdb_assert (regcache != NULL); | |
6f98355c | 761 | regcache->cooked_write (regnum, val); |
ef79d9a3 YQ |
762 | } |
763 | ||
6f98355c | 764 | template<typename T, typename> |
ef79d9a3 | 765 | void |
6f98355c | 766 | regcache::cooked_write (int regnum, T val) |
a66a9c23 | 767 | { |
7c543f7b | 768 | gdb_byte *buf; |
123f5f96 | 769 | |
ef79d9a3 YQ |
770 | gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers); |
771 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); | |
6f98355c YQ |
772 | store_integer (buf, m_descr->sizeof_register[regnum], |
773 | gdbarch_byte_order (m_descr->gdbarch), val); | |
ef79d9a3 | 774 | cooked_write (regnum, buf); |
a66a9c23 AC |
775 | } |
776 | ||
777 | void | |
778 | regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, | |
779 | ULONGEST val) | |
ef79d9a3 YQ |
780 | { |
781 | gdb_assert (regcache != NULL); | |
6f98355c | 782 | regcache->cooked_write (regnum, val); |
a66a9c23 AC |
783 | } |
784 | ||
61a0eb5b | 785 | void |
2d522557 AC |
786 | regcache_raw_write (struct regcache *regcache, int regnum, |
787 | const gdb_byte *buf) | |
ef79d9a3 YQ |
788 | { |
789 | gdb_assert (regcache != NULL && buf != NULL); | |
790 | regcache->raw_write (regnum, buf); | |
791 | } | |
792 | ||
793 | void | |
794 | regcache::raw_write (int regnum, const gdb_byte *buf) | |
61a0eb5b | 795 | { |
594f7785 | 796 | |
ef79d9a3 | 797 | gdb_assert (buf != NULL); |
4e888c28 | 798 | assert_regnum (regnum); |
3fadccb3 | 799 | |
3fadccb3 AC |
800 | /* On the sparc, writing %g0 is a no-op, so we don't even want to |
801 | change the registers array if something writes to this register. */ | |
ef79d9a3 | 802 | if (gdbarch_cannot_store_register (arch (), regnum)) |
3fadccb3 AC |
803 | return; |
804 | ||
3fadccb3 | 805 | /* If we have a valid copy of the register, and new value == old |
0df8b418 | 806 | value, then don't bother doing the actual store. */ |
ef79d9a3 YQ |
807 | if (get_register_status (regnum) == REG_VALID |
808 | && (memcmp (register_buffer (regnum), buf, | |
809 | m_descr->sizeof_register[regnum]) == 0)) | |
3fadccb3 AC |
810 | return; |
811 | ||
ef79d9a3 | 812 | target_prepare_to_store (this); |
c8ec2f33 | 813 | raw_supply (regnum, buf); |
b94ade42 | 814 | |
b292235f TT |
815 | /* Invalidate the register after it is written, in case of a |
816 | failure. */ | |
817 | regcache_invalidator invalidator (this, regnum); | |
b94ade42 | 818 | |
ef79d9a3 | 819 | target_store_registers (this, regnum); |
594f7785 | 820 | |
b292235f TT |
821 | /* The target did not throw an error so we can discard invalidating |
822 | the register. */ | |
823 | invalidator.release (); | |
61a0eb5b AC |
824 | } |
825 | ||
68365089 | 826 | void |
2d522557 AC |
827 | regcache_cooked_write (struct regcache *regcache, int regnum, |
828 | const gdb_byte *buf) | |
ef79d9a3 YQ |
829 | { |
830 | regcache->cooked_write (regnum, buf); | |
831 | } | |
832 | ||
833 | void | |
834 | regcache::cooked_write (int regnum, const gdb_byte *buf) | |
68365089 | 835 | { |
d138e37a | 836 | gdb_assert (regnum >= 0); |
ef79d9a3 | 837 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
d999647b | 838 | if (regnum < num_raw_registers ()) |
ef79d9a3 | 839 | raw_write (regnum, buf); |
d138e37a | 840 | else |
ef79d9a3 | 841 | gdbarch_pseudo_register_write (m_descr->gdbarch, this, |
d8124050 | 842 | regnum, buf); |
61a0eb5b AC |
843 | } |
844 | ||
06c0b04e AC |
845 | /* Perform a partial register transfer using a read, modify, write |
846 | operation. */ | |
847 | ||
848 | typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum, | |
849 | void *buf); | |
850 | typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum, | |
851 | const void *buf); | |
852 | ||
ef79d9a3 | 853 | enum register_status |
849d0ba8 YQ |
854 | readable_regcache::read_part (int regnum, int offset, int len, void *in, |
855 | bool is_raw) | |
856 | { | |
857 | struct gdbarch *gdbarch = arch (); | |
858 | gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum)); | |
859 | ||
860 | gdb_assert (in != NULL); | |
861 | gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]); | |
862 | gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]); | |
863 | /* Something to do? */ | |
864 | if (offset + len == 0) | |
865 | return REG_VALID; | |
866 | /* Read (when needed) ... */ | |
867 | enum register_status status; | |
868 | ||
869 | if (is_raw) | |
870 | status = raw_read (regnum, reg); | |
871 | else | |
872 | status = cooked_read (regnum, reg); | |
873 | if (status != REG_VALID) | |
874 | return status; | |
875 | ||
876 | /* ... modify ... */ | |
877 | memcpy (in, reg + offset, len); | |
878 | ||
879 | return REG_VALID; | |
880 | } | |
881 | ||
882 | enum register_status | |
883 | regcache::write_part (int regnum, int offset, int len, | |
d3037ba6 | 884 | const void *out, bool is_raw) |
ef79d9a3 YQ |
885 | { |
886 | struct gdbarch *gdbarch = arch (); | |
9890e433 | 887 | gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum)); |
123f5f96 | 888 | |
849d0ba8 | 889 | gdb_assert (out != NULL); |
ef79d9a3 YQ |
890 | gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]); |
891 | gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]); | |
06c0b04e AC |
892 | /* Something to do? */ |
893 | if (offset + len == 0) | |
05d1431c | 894 | return REG_VALID; |
0df8b418 | 895 | /* Read (when needed) ... */ |
849d0ba8 | 896 | if (offset > 0 |
ef79d9a3 | 897 | || offset + len < m_descr->sizeof_register[regnum]) |
06c0b04e | 898 | { |
05d1431c PA |
899 | enum register_status status; |
900 | ||
d3037ba6 YQ |
901 | if (is_raw) |
902 | status = raw_read (regnum, reg); | |
903 | else | |
904 | status = cooked_read (regnum, reg); | |
05d1431c PA |
905 | if (status != REG_VALID) |
906 | return status; | |
06c0b04e | 907 | } |
849d0ba8 YQ |
908 | |
909 | memcpy (reg + offset, out, len); | |
06c0b04e | 910 | /* ... write (when needed). */ |
849d0ba8 YQ |
911 | if (is_raw) |
912 | raw_write (regnum, reg); | |
913 | else | |
914 | cooked_write (regnum, reg); | |
05d1431c PA |
915 | |
916 | return REG_VALID; | |
06c0b04e AC |
917 | } |
918 | ||
05d1431c | 919 | enum register_status |
06c0b04e | 920 | regcache_raw_read_part (struct regcache *regcache, int regnum, |
2d522557 | 921 | int offset, int len, gdb_byte *buf) |
06c0b04e | 922 | { |
ef79d9a3 YQ |
923 | return regcache->raw_read_part (regnum, offset, len, buf); |
924 | } | |
123f5f96 | 925 | |
ef79d9a3 | 926 | enum register_status |
849d0ba8 | 927 | readable_regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf) |
ef79d9a3 | 928 | { |
4e888c28 | 929 | assert_regnum (regnum); |
849d0ba8 | 930 | return read_part (regnum, offset, len, buf, true); |
06c0b04e AC |
931 | } |
932 | ||
933 | void | |
934 | regcache_raw_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 935 | int offset, int len, const gdb_byte *buf) |
06c0b04e | 936 | { |
ef79d9a3 YQ |
937 | regcache->raw_write_part (regnum, offset, len, buf); |
938 | } | |
123f5f96 | 939 | |
ef79d9a3 YQ |
940 | void |
941 | regcache::raw_write_part (int regnum, int offset, int len, | |
942 | const gdb_byte *buf) | |
943 | { | |
4e888c28 | 944 | assert_regnum (regnum); |
849d0ba8 | 945 | write_part (regnum, offset, len, buf, true); |
06c0b04e AC |
946 | } |
947 | ||
05d1431c | 948 | enum register_status |
06c0b04e | 949 | regcache_cooked_read_part (struct regcache *regcache, int regnum, |
2d522557 | 950 | int offset, int len, gdb_byte *buf) |
06c0b04e | 951 | { |
ef79d9a3 YQ |
952 | return regcache->cooked_read_part (regnum, offset, len, buf); |
953 | } | |
123f5f96 | 954 | |
ef79d9a3 YQ |
955 | |
956 | enum register_status | |
849d0ba8 YQ |
957 | readable_regcache::cooked_read_part (int regnum, int offset, int len, |
958 | gdb_byte *buf) | |
ef79d9a3 YQ |
959 | { |
960 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); | |
849d0ba8 | 961 | return read_part (regnum, offset, len, buf, false); |
06c0b04e AC |
962 | } |
963 | ||
964 | void | |
965 | regcache_cooked_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 966 | int offset, int len, const gdb_byte *buf) |
06c0b04e | 967 | { |
ef79d9a3 YQ |
968 | regcache->cooked_write_part (regnum, offset, len, buf); |
969 | } | |
123f5f96 | 970 | |
ef79d9a3 YQ |
971 | void |
972 | regcache::cooked_write_part (int regnum, int offset, int len, | |
973 | const gdb_byte *buf) | |
974 | { | |
975 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); | |
849d0ba8 | 976 | write_part (regnum, offset, len, buf, false); |
06c0b04e | 977 | } |
32178cab | 978 | |
a16d75cc | 979 | /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */ |
9a661b68 MK |
980 | |
981 | void | |
6618125d | 982 | regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf) |
ef79d9a3 YQ |
983 | { |
984 | gdb_assert (regcache != NULL); | |
985 | regcache->raw_supply (regnum, buf); | |
986 | } | |
987 | ||
988 | void | |
c8ec2f33 | 989 | detached_regcache::raw_supply (int regnum, const void *buf) |
9a661b68 MK |
990 | { |
991 | void *regbuf; | |
992 | size_t size; | |
993 | ||
4e888c28 | 994 | assert_regnum (regnum); |
9a661b68 | 995 | |
ef79d9a3 YQ |
996 | regbuf = register_buffer (regnum); |
997 | size = m_descr->sizeof_register[regnum]; | |
9a661b68 MK |
998 | |
999 | if (buf) | |
ee99023e PA |
1000 | { |
1001 | memcpy (regbuf, buf, size); | |
ef79d9a3 | 1002 | m_register_status[regnum] = REG_VALID; |
ee99023e | 1003 | } |
9a661b68 | 1004 | else |
ee99023e PA |
1005 | { |
1006 | /* This memset not strictly necessary, but better than garbage | |
1007 | in case the register value manages to escape somewhere (due | |
1008 | to a bug, no less). */ | |
1009 | memset (regbuf, 0, size); | |
ef79d9a3 | 1010 | m_register_status[regnum] = REG_UNAVAILABLE; |
ee99023e | 1011 | } |
9a661b68 MK |
1012 | } |
1013 | ||
b057297a AH |
1014 | /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at |
1015 | address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If | |
1016 | the register size is greater than ADDR_LEN, then the integer will be sign or | |
1017 | zero extended. If the register size is smaller than the integer, then the | |
1018 | most significant bytes of the integer will be truncated. */ | |
1019 | ||
1020 | void | |
796bb026 YQ |
1021 | detached_regcache::raw_supply_integer (int regnum, const gdb_byte *addr, |
1022 | int addr_len, bool is_signed) | |
b057297a AH |
1023 | { |
1024 | enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); | |
1025 | gdb_byte *regbuf; | |
1026 | size_t regsize; | |
1027 | ||
4e888c28 | 1028 | assert_regnum (regnum); |
b057297a AH |
1029 | |
1030 | regbuf = register_buffer (regnum); | |
1031 | regsize = m_descr->sizeof_register[regnum]; | |
1032 | ||
1033 | copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed, | |
1034 | byte_order); | |
1035 | m_register_status[regnum] = REG_VALID; | |
1036 | } | |
1037 | ||
f81fdd35 AH |
1038 | /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same |
1039 | as calling raw_supply with NULL (which will set the state to | |
1040 | unavailable). */ | |
1041 | ||
1042 | void | |
796bb026 | 1043 | detached_regcache::raw_supply_zeroed (int regnum) |
f81fdd35 AH |
1044 | { |
1045 | void *regbuf; | |
1046 | size_t size; | |
1047 | ||
4e888c28 | 1048 | assert_regnum (regnum); |
f81fdd35 AH |
1049 | |
1050 | regbuf = register_buffer (regnum); | |
1051 | size = m_descr->sizeof_register[regnum]; | |
1052 | ||
1053 | memset (regbuf, 0, size); | |
1054 | m_register_status[regnum] = REG_VALID; | |
1055 | } | |
1056 | ||
9a661b68 MK |
1057 | /* Collect register REGNUM from REGCACHE and store its contents in BUF. */ |
1058 | ||
1059 | void | |
6618125d | 1060 | regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf) |
ef79d9a3 YQ |
1061 | { |
1062 | gdb_assert (regcache != NULL && buf != NULL); | |
1063 | regcache->raw_collect (regnum, buf); | |
1064 | } | |
1065 | ||
1066 | void | |
1067 | regcache::raw_collect (int regnum, void *buf) const | |
9a661b68 MK |
1068 | { |
1069 | const void *regbuf; | |
1070 | size_t size; | |
1071 | ||
ef79d9a3 | 1072 | gdb_assert (buf != NULL); |
4e888c28 | 1073 | assert_regnum (regnum); |
9a661b68 | 1074 | |
ef79d9a3 YQ |
1075 | regbuf = register_buffer (regnum); |
1076 | size = m_descr->sizeof_register[regnum]; | |
9a661b68 MK |
1077 | memcpy (buf, regbuf, size); |
1078 | } | |
1079 | ||
0b309272 AA |
1080 | /* Transfer a single or all registers belonging to a certain register |
1081 | set to or from a buffer. This is the main worker function for | |
1082 | regcache_supply_regset and regcache_collect_regset. */ | |
1083 | ||
b057297a AH |
1084 | /* Collect register REGNUM from REGCACHE. Store collected value as an integer |
1085 | at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. | |
1086 | If ADDR_LEN is greater than the register size, then the integer will be sign | |
1087 | or zero extended. If ADDR_LEN is smaller than the register size, then the | |
1088 | most significant bytes of the integer will be truncated. */ | |
1089 | ||
1090 | void | |
1091 | regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len, | |
1092 | bool is_signed) const | |
1093 | { | |
1094 | enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); | |
1095 | const gdb_byte *regbuf; | |
1096 | size_t regsize; | |
1097 | ||
4e888c28 | 1098 | assert_regnum (regnum); |
b057297a AH |
1099 | |
1100 | regbuf = register_buffer (regnum); | |
1101 | regsize = m_descr->sizeof_register[regnum]; | |
1102 | ||
1103 | copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed, | |
1104 | byte_order); | |
1105 | } | |
1106 | ||
ef79d9a3 YQ |
1107 | void |
1108 | regcache::transfer_regset (const struct regset *regset, | |
1109 | struct regcache *out_regcache, | |
1110 | int regnum, const void *in_buf, | |
1111 | void *out_buf, size_t size) const | |
0b309272 AA |
1112 | { |
1113 | const struct regcache_map_entry *map; | |
1114 | int offs = 0, count; | |
1115 | ||
19ba03f4 SM |
1116 | for (map = (const struct regcache_map_entry *) regset->regmap; |
1117 | (count = map->count) != 0; | |
1118 | map++) | |
0b309272 AA |
1119 | { |
1120 | int regno = map->regno; | |
1121 | int slot_size = map->size; | |
1122 | ||
1123 | if (slot_size == 0 && regno != REGCACHE_MAP_SKIP) | |
ef79d9a3 | 1124 | slot_size = m_descr->sizeof_register[regno]; |
0b309272 AA |
1125 | |
1126 | if (regno == REGCACHE_MAP_SKIP | |
1127 | || (regnum != -1 | |
1128 | && (regnum < regno || regnum >= regno + count))) | |
1129 | offs += count * slot_size; | |
1130 | ||
1131 | else if (regnum == -1) | |
1132 | for (; count--; regno++, offs += slot_size) | |
1133 | { | |
1134 | if (offs + slot_size > size) | |
1135 | break; | |
1136 | ||
1137 | if (out_buf) | |
ef79d9a3 | 1138 | raw_collect (regno, (gdb_byte *) out_buf + offs); |
0b309272 | 1139 | else |
ef79d9a3 YQ |
1140 | out_regcache->raw_supply (regno, in_buf |
1141 | ? (const gdb_byte *) in_buf + offs | |
1142 | : NULL); | |
0b309272 AA |
1143 | } |
1144 | else | |
1145 | { | |
1146 | /* Transfer a single register and return. */ | |
1147 | offs += (regnum - regno) * slot_size; | |
1148 | if (offs + slot_size > size) | |
1149 | return; | |
1150 | ||
1151 | if (out_buf) | |
ef79d9a3 | 1152 | raw_collect (regnum, (gdb_byte *) out_buf + offs); |
0b309272 | 1153 | else |
ef79d9a3 YQ |
1154 | out_regcache->raw_supply (regnum, in_buf |
1155 | ? (const gdb_byte *) in_buf + offs | |
1156 | : NULL); | |
0b309272 AA |
1157 | return; |
1158 | } | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | /* Supply register REGNUM from BUF to REGCACHE, using the register map | |
1163 | in REGSET. If REGNUM is -1, do this for all registers in REGSET. | |
1164 | If BUF is NULL, set the register(s) to "unavailable" status. */ | |
1165 | ||
1166 | void | |
1167 | regcache_supply_regset (const struct regset *regset, | |
1168 | struct regcache *regcache, | |
1169 | int regnum, const void *buf, size_t size) | |
1170 | { | |
ef79d9a3 YQ |
1171 | regcache->supply_regset (regset, regnum, buf, size); |
1172 | } | |
1173 | ||
1174 | void | |
1175 | regcache::supply_regset (const struct regset *regset, | |
1176 | int regnum, const void *buf, size_t size) | |
1177 | { | |
1178 | transfer_regset (regset, this, regnum, buf, NULL, size); | |
0b309272 AA |
1179 | } |
1180 | ||
1181 | /* Collect register REGNUM from REGCACHE to BUF, using the register | |
1182 | map in REGSET. If REGNUM is -1, do this for all registers in | |
1183 | REGSET. */ | |
1184 | ||
1185 | void | |
1186 | regcache_collect_regset (const struct regset *regset, | |
1187 | const struct regcache *regcache, | |
1188 | int regnum, void *buf, size_t size) | |
1189 | { | |
ef79d9a3 YQ |
1190 | regcache->collect_regset (regset, regnum, buf, size); |
1191 | } | |
1192 | ||
1193 | void | |
1194 | regcache::collect_regset (const struct regset *regset, | |
1195 | int regnum, void *buf, size_t size) const | |
1196 | { | |
1197 | transfer_regset (regset, NULL, regnum, NULL, buf, size); | |
0b309272 AA |
1198 | } |
1199 | ||
193cb69f | 1200 | |
515630c5 | 1201 | /* Special handling for register PC. */ |
32178cab MS |
1202 | |
1203 | CORE_ADDR | |
515630c5 | 1204 | regcache_read_pc (struct regcache *regcache) |
32178cab | 1205 | { |
ac7936df | 1206 | struct gdbarch *gdbarch = regcache->arch (); |
61a1198a | 1207 | |
32178cab MS |
1208 | CORE_ADDR pc_val; |
1209 | ||
61a1198a UW |
1210 | if (gdbarch_read_pc_p (gdbarch)) |
1211 | pc_val = gdbarch_read_pc (gdbarch, regcache); | |
cde9ea48 | 1212 | /* Else use per-frame method on get_current_frame. */ |
214e098a | 1213 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
cde9ea48 | 1214 | { |
61a1198a | 1215 | ULONGEST raw_val; |
123f5f96 | 1216 | |
05d1431c PA |
1217 | if (regcache_cooked_read_unsigned (regcache, |
1218 | gdbarch_pc_regnum (gdbarch), | |
1219 | &raw_val) == REG_UNAVAILABLE) | |
1220 | throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available")); | |
1221 | ||
214e098a | 1222 | pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val); |
cde9ea48 AC |
1223 | } |
1224 | else | |
515630c5 UW |
1225 | internal_error (__FILE__, __LINE__, |
1226 | _("regcache_read_pc: Unable to find PC")); | |
32178cab MS |
1227 | return pc_val; |
1228 | } | |
1229 | ||
32178cab | 1230 | void |
515630c5 | 1231 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) |
32178cab | 1232 | { |
ac7936df | 1233 | struct gdbarch *gdbarch = regcache->arch (); |
61a1198a | 1234 | |
61a1198a UW |
1235 | if (gdbarch_write_pc_p (gdbarch)) |
1236 | gdbarch_write_pc (gdbarch, regcache, pc); | |
214e098a | 1237 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
3e8c568d | 1238 | regcache_cooked_write_unsigned (regcache, |
214e098a | 1239 | gdbarch_pc_regnum (gdbarch), pc); |
61a1198a UW |
1240 | else |
1241 | internal_error (__FILE__, __LINE__, | |
515630c5 | 1242 | _("regcache_write_pc: Unable to update PC")); |
edb3359d DJ |
1243 | |
1244 | /* Writing the PC (for instance, from "load") invalidates the | |
1245 | current frame. */ | |
1246 | reinit_frame_cache (); | |
32178cab MS |
1247 | } |
1248 | ||
d999647b | 1249 | int |
31716595 | 1250 | reg_buffer::num_raw_registers () const |
d999647b YQ |
1251 | { |
1252 | return gdbarch_num_regs (arch ()); | |
1253 | } | |
1254 | ||
ed771251 | 1255 | void |
ef79d9a3 | 1256 | regcache::debug_print_register (const char *func, int regno) |
ed771251 | 1257 | { |
ef79d9a3 | 1258 | struct gdbarch *gdbarch = arch (); |
ed771251 AH |
1259 | |
1260 | fprintf_unfiltered (gdb_stdlog, "%s ", func); | |
1261 | if (regno >= 0 && regno < gdbarch_num_regs (gdbarch) | |
1262 | && gdbarch_register_name (gdbarch, regno) != NULL | |
1263 | && gdbarch_register_name (gdbarch, regno)[0] != '\0') | |
1264 | fprintf_unfiltered (gdb_stdlog, "(%s)", | |
1265 | gdbarch_register_name (gdbarch, regno)); | |
1266 | else | |
1267 | fprintf_unfiltered (gdb_stdlog, "(%d)", regno); | |
1268 | if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)) | |
1269 | { | |
1270 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
1271 | int size = register_size (gdbarch, regno); | |
ef79d9a3 | 1272 | gdb_byte *buf = register_buffer (regno); |
ed771251 AH |
1273 | |
1274 | fprintf_unfiltered (gdb_stdlog, " = "); | |
1275 | for (int i = 0; i < size; i++) | |
1276 | { | |
1277 | fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]); | |
1278 | } | |
1279 | if (size <= sizeof (LONGEST)) | |
1280 | { | |
1281 | ULONGEST val = extract_unsigned_integer (buf, size, byte_order); | |
1282 | ||
1283 | fprintf_unfiltered (gdb_stdlog, " %s %s", | |
1284 | core_addr_to_string_nz (val), plongest (val)); | |
1285 | } | |
1286 | } | |
1287 | fprintf_unfiltered (gdb_stdlog, "\n"); | |
1288 | } | |
32178cab | 1289 | |
705152c5 | 1290 | static void |
0b39b52e | 1291 | reg_flush_command (const char *command, int from_tty) |
705152c5 MS |
1292 | { |
1293 | /* Force-flush the register cache. */ | |
1294 | registers_changed (); | |
1295 | if (from_tty) | |
a3f17187 | 1296 | printf_filtered (_("Register cache flushed.\n")); |
705152c5 MS |
1297 | } |
1298 | ||
4c74fe6b YQ |
1299 | void |
1300 | register_dump::dump (ui_file *file) | |
af030b9a | 1301 | { |
4c74fe6b YQ |
1302 | auto descr = regcache_descr (m_gdbarch); |
1303 | int regnum; | |
1304 | int footnote_nr = 0; | |
1305 | int footnote_register_offset = 0; | |
1306 | int footnote_register_type_name_null = 0; | |
1307 | long register_offset = 0; | |
af030b9a | 1308 | |
4c74fe6b YQ |
1309 | gdb_assert (descr->nr_cooked_registers |
1310 | == (gdbarch_num_regs (m_gdbarch) | |
1311 | + gdbarch_num_pseudo_regs (m_gdbarch))); | |
af030b9a | 1312 | |
4c74fe6b YQ |
1313 | for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++) |
1314 | { | |
1315 | /* Name. */ | |
1316 | if (regnum < 0) | |
1317 | fprintf_unfiltered (file, " %-10s", "Name"); | |
1318 | else | |
1319 | { | |
1320 | const char *p = gdbarch_register_name (m_gdbarch, regnum); | |
123f5f96 | 1321 | |
4c74fe6b YQ |
1322 | if (p == NULL) |
1323 | p = ""; | |
1324 | else if (p[0] == '\0') | |
1325 | p = "''"; | |
1326 | fprintf_unfiltered (file, " %-10s", p); | |
1327 | } | |
af030b9a | 1328 | |
4c74fe6b YQ |
1329 | /* Number. */ |
1330 | if (regnum < 0) | |
1331 | fprintf_unfiltered (file, " %4s", "Nr"); | |
1332 | else | |
1333 | fprintf_unfiltered (file, " %4d", regnum); | |
af030b9a | 1334 | |
4c74fe6b YQ |
1335 | /* Relative number. */ |
1336 | if (regnum < 0) | |
1337 | fprintf_unfiltered (file, " %4s", "Rel"); | |
1338 | else if (regnum < gdbarch_num_regs (m_gdbarch)) | |
1339 | fprintf_unfiltered (file, " %4d", regnum); | |
1340 | else | |
1341 | fprintf_unfiltered (file, " %4d", | |
1342 | (regnum - gdbarch_num_regs (m_gdbarch))); | |
af030b9a | 1343 | |
4c74fe6b YQ |
1344 | /* Offset. */ |
1345 | if (regnum < 0) | |
1346 | fprintf_unfiltered (file, " %6s ", "Offset"); | |
1347 | else | |
af030b9a | 1348 | { |
4c74fe6b YQ |
1349 | fprintf_unfiltered (file, " %6ld", |
1350 | descr->register_offset[regnum]); | |
1351 | if (register_offset != descr->register_offset[regnum] | |
1352 | || (regnum > 0 | |
1353 | && (descr->register_offset[regnum] | |
1354 | != (descr->register_offset[regnum - 1] | |
1355 | + descr->sizeof_register[regnum - 1]))) | |
1356 | ) | |
af030b9a | 1357 | { |
4c74fe6b YQ |
1358 | if (!footnote_register_offset) |
1359 | footnote_register_offset = ++footnote_nr; | |
1360 | fprintf_unfiltered (file, "*%d", footnote_register_offset); | |
af030b9a | 1361 | } |
4c74fe6b YQ |
1362 | else |
1363 | fprintf_unfiltered (file, " "); | |
1364 | register_offset = (descr->register_offset[regnum] | |
1365 | + descr->sizeof_register[regnum]); | |
af030b9a AC |
1366 | } |
1367 | ||
4c74fe6b YQ |
1368 | /* Size. */ |
1369 | if (regnum < 0) | |
1370 | fprintf_unfiltered (file, " %5s ", "Size"); | |
1371 | else | |
1372 | fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]); | |
f3384e66 | 1373 | |
4c74fe6b | 1374 | /* Type. */ |
f3384e66 | 1375 | { |
4c74fe6b YQ |
1376 | const char *t; |
1377 | std::string name_holder; | |
b59ff9d5 | 1378 | |
4c74fe6b YQ |
1379 | if (regnum < 0) |
1380 | t = "Type"; | |
215c69dc YQ |
1381 | else |
1382 | { | |
4c74fe6b | 1383 | static const char blt[] = "builtin_type"; |
123f5f96 | 1384 | |
4c74fe6b YQ |
1385 | t = TYPE_NAME (register_type (m_gdbarch, regnum)); |
1386 | if (t == NULL) | |
f3384e66 | 1387 | { |
4c74fe6b YQ |
1388 | if (!footnote_register_type_name_null) |
1389 | footnote_register_type_name_null = ++footnote_nr; | |
1390 | name_holder = string_printf ("*%d", | |
1391 | footnote_register_type_name_null); | |
1392 | t = name_holder.c_str (); | |
f3384e66 | 1393 | } |
4c74fe6b YQ |
1394 | /* Chop a leading builtin_type. */ |
1395 | if (startswith (t, blt)) | |
1396 | t += strlen (blt); | |
f3384e66 | 1397 | } |
4c74fe6b | 1398 | fprintf_unfiltered (file, " %-15s", t); |
f3384e66 | 1399 | } |
f3384e66 | 1400 | |
4c74fe6b YQ |
1401 | /* Leading space always present. */ |
1402 | fprintf_unfiltered (file, " "); | |
af030b9a | 1403 | |
4c74fe6b | 1404 | dump_reg (file, regnum); |
ed4227b7 | 1405 | |
4c74fe6b | 1406 | fprintf_unfiltered (file, "\n"); |
ed4227b7 PA |
1407 | } |
1408 | ||
4c74fe6b YQ |
1409 | if (footnote_register_offset) |
1410 | fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n", | |
1411 | footnote_register_offset); | |
1412 | if (footnote_register_type_name_null) | |
1413 | fprintf_unfiltered (file, | |
1414 | "*%d: Register type's name NULL.\n", | |
1415 | footnote_register_type_name_null); | |
c21236dc PA |
1416 | } |
1417 | ||
8248946c YQ |
1418 | #if GDB_SELF_TEST |
1419 | #include "selftest.h" | |
1b30aaa5 YQ |
1420 | #include "selftest-arch.h" |
1421 | #include "gdbthread.h" | |
ec7a5fcb | 1422 | #include "target-float.h" |
8248946c YQ |
1423 | |
1424 | namespace selftests { | |
1425 | ||
e521e87e | 1426 | class regcache_access : public regcache |
8248946c | 1427 | { |
e521e87e YQ |
1428 | public: |
1429 | ||
1430 | /* Return the number of elements in current_regcache. */ | |
1431 | ||
1432 | static size_t | |
1433 | current_regcache_size () | |
1434 | { | |
1435 | return std::distance (regcache::current_regcache.begin (), | |
1436 | regcache::current_regcache.end ()); | |
1437 | } | |
1438 | }; | |
8248946c YQ |
1439 | |
1440 | static void | |
1441 | current_regcache_test (void) | |
1442 | { | |
1443 | /* It is empty at the start. */ | |
e521e87e | 1444 | SELF_CHECK (regcache_access::current_regcache_size () == 0); |
8248946c YQ |
1445 | |
1446 | ptid_t ptid1 (1), ptid2 (2), ptid3 (3); | |
1447 | ||
1448 | /* Get regcache from ptid1, a new regcache is added to | |
1449 | current_regcache. */ | |
1450 | regcache *regcache = get_thread_arch_aspace_regcache (ptid1, | |
1451 | target_gdbarch (), | |
1452 | NULL); | |
1453 | ||
1454 | SELF_CHECK (regcache != NULL); | |
1455 | SELF_CHECK (regcache->ptid () == ptid1); | |
e521e87e | 1456 | SELF_CHECK (regcache_access::current_regcache_size () == 1); |
8248946c YQ |
1457 | |
1458 | /* Get regcache from ptid2, a new regcache is added to | |
1459 | current_regcache. */ | |
1460 | regcache = get_thread_arch_aspace_regcache (ptid2, | |
1461 | target_gdbarch (), | |
1462 | NULL); | |
1463 | SELF_CHECK (regcache != NULL); | |
1464 | SELF_CHECK (regcache->ptid () == ptid2); | |
e521e87e | 1465 | SELF_CHECK (regcache_access::current_regcache_size () == 2); |
8248946c YQ |
1466 | |
1467 | /* Get regcache from ptid3, a new regcache is added to | |
1468 | current_regcache. */ | |
1469 | regcache = get_thread_arch_aspace_regcache (ptid3, | |
1470 | target_gdbarch (), | |
1471 | NULL); | |
1472 | SELF_CHECK (regcache != NULL); | |
1473 | SELF_CHECK (regcache->ptid () == ptid3); | |
e521e87e | 1474 | SELF_CHECK (regcache_access::current_regcache_size () == 3); |
8248946c YQ |
1475 | |
1476 | /* Get regcache from ptid2 again, nothing is added to | |
1477 | current_regcache. */ | |
1478 | regcache = get_thread_arch_aspace_regcache (ptid2, | |
1479 | target_gdbarch (), | |
1480 | NULL); | |
1481 | SELF_CHECK (regcache != NULL); | |
1482 | SELF_CHECK (regcache->ptid () == ptid2); | |
e521e87e | 1483 | SELF_CHECK (regcache_access::current_regcache_size () == 3); |
8248946c YQ |
1484 | |
1485 | /* Mark ptid2 is changed, so regcache of ptid2 should be removed from | |
1486 | current_regcache. */ | |
1487 | registers_changed_ptid (ptid2); | |
e521e87e | 1488 | SELF_CHECK (regcache_access::current_regcache_size () == 2); |
8248946c YQ |
1489 | } |
1490 | ||
1b30aaa5 YQ |
1491 | static void test_target_fetch_registers (target_ops *self, regcache *regs, |
1492 | int regno); | |
1493 | static void test_target_store_registers (target_ops *self, regcache *regs, | |
1494 | int regno); | |
1495 | static enum target_xfer_status | |
1496 | test_target_xfer_partial (struct target_ops *ops, | |
1497 | enum target_object object, | |
1498 | const char *annex, gdb_byte *readbuf, | |
1499 | const gdb_byte *writebuf, | |
1500 | ULONGEST offset, ULONGEST len, | |
1501 | ULONGEST *xfered_len); | |
1502 | ||
1503 | class target_ops_no_register : public test_target_ops | |
1504 | { | |
1505 | public: | |
1506 | target_ops_no_register () | |
1507 | : test_target_ops {} | |
1508 | { | |
1509 | to_fetch_registers = test_target_fetch_registers; | |
1510 | to_store_registers = test_target_store_registers; | |
1511 | to_xfer_partial = test_target_xfer_partial; | |
1512 | ||
1513 | to_data = this; | |
1514 | } | |
1515 | ||
1516 | void reset () | |
1517 | { | |
1518 | fetch_registers_called = 0; | |
1519 | store_registers_called = 0; | |
1520 | xfer_partial_called = 0; | |
1521 | } | |
1522 | ||
1523 | unsigned int fetch_registers_called = 0; | |
1524 | unsigned int store_registers_called = 0; | |
1525 | unsigned int xfer_partial_called = 0; | |
1526 | }; | |
1527 | ||
1528 | static void | |
1529 | test_target_fetch_registers (target_ops *self, regcache *regs, int regno) | |
1530 | { | |
1531 | auto ops = static_cast<target_ops_no_register *> (self->to_data); | |
1532 | ||
1533 | /* Mark register available. */ | |
1534 | regs->raw_supply_zeroed (regno); | |
1535 | ops->fetch_registers_called++; | |
1536 | } | |
1537 | ||
1538 | static void | |
1539 | test_target_store_registers (target_ops *self, regcache *regs, int regno) | |
1540 | { | |
1541 | auto ops = static_cast<target_ops_no_register *> (self->to_data); | |
1542 | ||
1543 | ops->store_registers_called++; | |
1544 | } | |
1545 | ||
1546 | static enum target_xfer_status | |
1547 | test_target_xfer_partial (struct target_ops *self, enum target_object object, | |
1548 | const char *annex, gdb_byte *readbuf, | |
1549 | const gdb_byte *writebuf, | |
1550 | ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) | |
1551 | { | |
1552 | auto ops = static_cast<target_ops_no_register *> (self->to_data); | |
1553 | ||
1554 | ops->xfer_partial_called++; | |
1555 | ||
1556 | *xfered_len = len; | |
1557 | return TARGET_XFER_OK; | |
1558 | } | |
1559 | ||
1560 | class readwrite_regcache : public regcache | |
1561 | { | |
1562 | public: | |
1563 | readwrite_regcache (struct gdbarch *gdbarch) | |
796bb026 | 1564 | : regcache (gdbarch, nullptr) |
1b30aaa5 YQ |
1565 | {} |
1566 | }; | |
1567 | ||
1568 | /* Test regcache::cooked_read gets registers from raw registers and | |
1569 | memory instead of target to_{fetch,store}_registers. */ | |
1570 | ||
1571 | static void | |
1572 | cooked_read_test (struct gdbarch *gdbarch) | |
1573 | { | |
1574 | /* Error out if debugging something, because we're going to push the | |
1575 | test target, which would pop any existing target. */ | |
1576 | if (current_target.to_stratum >= process_stratum) | |
1577 | error (_("target already pushed")); | |
1578 | ||
1579 | /* Create a mock environment. An inferior with a thread, with a | |
1580 | process_stratum target pushed. */ | |
1581 | ||
1582 | target_ops_no_register mock_target; | |
1583 | ptid_t mock_ptid (1, 1); | |
1584 | inferior mock_inferior (mock_ptid.pid ()); | |
1585 | address_space mock_aspace {}; | |
1586 | mock_inferior.gdbarch = gdbarch; | |
1587 | mock_inferior.aspace = &mock_aspace; | |
1588 | thread_info mock_thread (&mock_inferior, mock_ptid); | |
1589 | ||
1590 | scoped_restore restore_thread_list | |
1591 | = make_scoped_restore (&thread_list, &mock_thread); | |
1592 | ||
1593 | /* Add the mock inferior to the inferior list so that look ups by | |
1594 | target+ptid can find it. */ | |
1595 | scoped_restore restore_inferior_list | |
1596 | = make_scoped_restore (&inferior_list); | |
1597 | inferior_list = &mock_inferior; | |
1598 | ||
1599 | /* Switch to the mock inferior. */ | |
1600 | scoped_restore_current_inferior restore_current_inferior; | |
1601 | set_current_inferior (&mock_inferior); | |
1602 | ||
1603 | /* Push the process_stratum target so we can mock accessing | |
1604 | registers. */ | |
1605 | push_target (&mock_target); | |
1606 | ||
1607 | /* Pop it again on exit (return/exception). */ | |
1608 | struct on_exit | |
1609 | { | |
1610 | ~on_exit () | |
1611 | { | |
1612 | pop_all_targets_at_and_above (process_stratum); | |
1613 | } | |
1614 | } pop_targets; | |
1615 | ||
1616 | /* Switch to the mock thread. */ | |
1617 | scoped_restore restore_inferior_ptid | |
1618 | = make_scoped_restore (&inferior_ptid, mock_ptid); | |
1619 | ||
1620 | /* Test that read one raw register from regcache_no_target will go | |
1621 | to the target layer. */ | |
1622 | int regnum; | |
1623 | ||
1624 | /* Find a raw register which size isn't zero. */ | |
1625 | for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++) | |
1626 | { | |
1627 | if (register_size (gdbarch, regnum) != 0) | |
1628 | break; | |
1629 | } | |
1630 | ||
1631 | readwrite_regcache readwrite (gdbarch); | |
1632 | gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum)); | |
1633 | ||
1634 | readwrite.raw_read (regnum, buf.data ()); | |
1635 | ||
1636 | /* raw_read calls target_fetch_registers. */ | |
1637 | SELF_CHECK (mock_target.fetch_registers_called > 0); | |
1638 | mock_target.reset (); | |
1639 | ||
1640 | /* Mark all raw registers valid, so the following raw registers | |
1641 | accesses won't go to target. */ | |
1642 | for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++) | |
1643 | readwrite.raw_update (i); | |
1644 | ||
1645 | mock_target.reset (); | |
1646 | /* Then, read all raw and pseudo registers, and don't expect calling | |
1647 | to_{fetch,store}_registers. */ | |
1648 | for (int regnum = 0; | |
1649 | regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); | |
1650 | regnum++) | |
1651 | { | |
1652 | if (register_size (gdbarch, regnum) == 0) | |
1653 | continue; | |
1654 | ||
1655 | gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum)); | |
1656 | ||
1657 | SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, buf.data ())); | |
1658 | ||
dc711524 YQ |
1659 | SELF_CHECK (mock_target.fetch_registers_called == 0); |
1660 | SELF_CHECK (mock_target.store_registers_called == 0); | |
1b30aaa5 YQ |
1661 | |
1662 | /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */ | |
1663 | if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu) | |
1664 | SELF_CHECK (mock_target.xfer_partial_called == 0); | |
1665 | ||
1666 | mock_target.reset (); | |
1667 | } | |
a63f2d2f | 1668 | |
215c69dc | 1669 | readonly_detached_regcache readonly (readwrite); |
a63f2d2f YQ |
1670 | |
1671 | /* GDB may go to target layer to fetch all registers and memory for | |
1672 | readonly regcache. */ | |
1673 | mock_target.reset (); | |
1674 | ||
1675 | for (int regnum = 0; | |
1676 | regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); | |
1677 | regnum++) | |
1678 | { | |
a63f2d2f YQ |
1679 | if (register_size (gdbarch, regnum) == 0) |
1680 | continue; | |
1681 | ||
1682 | gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum)); | |
1683 | enum register_status status = readonly.cooked_read (regnum, | |
1684 | buf.data ()); | |
1685 | ||
1686 | if (regnum < gdbarch_num_regs (gdbarch)) | |
1687 | { | |
1688 | auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; | |
1689 | ||
1690 | if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300 | |
1691 | || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh | |
1692 | || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850 | |
1693 | || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep | |
1694 | || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850 | |
1695 | || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300 | |
ea005f31 AB |
1696 | || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score |
1697 | || bfd_arch == bfd_arch_riscv) | |
a63f2d2f YQ |
1698 | { |
1699 | /* Raw registers. If raw registers are not in save_reggroup, | |
1700 | their status are unknown. */ | |
1701 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
1702 | SELF_CHECK (status == REG_VALID); | |
1703 | else | |
1704 | SELF_CHECK (status == REG_UNKNOWN); | |
1705 | } | |
1706 | else | |
1707 | SELF_CHECK (status == REG_VALID); | |
1708 | } | |
1709 | else | |
1710 | { | |
1711 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
1712 | SELF_CHECK (status == REG_VALID); | |
1713 | else | |
1714 | { | |
1715 | /* If pseudo registers are not in save_reggroup, some of | |
1716 | them can be computed from saved raw registers, but some | |
1717 | of them are unknown. */ | |
1718 | auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; | |
1719 | ||
1720 | if (bfd_arch == bfd_arch_frv | |
1721 | || bfd_arch == bfd_arch_m32c | |
1722 | || bfd_arch == bfd_arch_mep | |
1723 | || bfd_arch == bfd_arch_sh) | |
1724 | SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN); | |
1725 | else if (bfd_arch == bfd_arch_mips | |
1726 | || bfd_arch == bfd_arch_h8300) | |
1727 | SELF_CHECK (status == REG_UNKNOWN); | |
1728 | else | |
1729 | SELF_CHECK (status == REG_VALID); | |
1730 | } | |
1731 | } | |
1732 | ||
1733 | SELF_CHECK (mock_target.fetch_registers_called == 0); | |
1734 | SELF_CHECK (mock_target.store_registers_called == 0); | |
1735 | SELF_CHECK (mock_target.xfer_partial_called == 0); | |
1736 | ||
1737 | mock_target.reset (); | |
1738 | } | |
1b30aaa5 YQ |
1739 | } |
1740 | ||
ec7a5fcb YQ |
1741 | /* Test regcache::cooked_write by writing some expected contents to |
1742 | registers, and checking that contents read from registers and the | |
1743 | expected contents are the same. */ | |
1744 | ||
1745 | static void | |
1746 | cooked_write_test (struct gdbarch *gdbarch) | |
1747 | { | |
1748 | /* Error out if debugging something, because we're going to push the | |
1749 | test target, which would pop any existing target. */ | |
1750 | if (current_target.to_stratum >= process_stratum) | |
1751 | error (_("target already pushed")); | |
1752 | ||
1753 | /* Create a mock environment. A process_stratum target pushed. */ | |
1754 | ||
1755 | target_ops_no_register mock_target; | |
1756 | ||
1757 | /* Push the process_stratum target so we can mock accessing | |
1758 | registers. */ | |
1759 | push_target (&mock_target); | |
1760 | ||
1761 | /* Pop it again on exit (return/exception). */ | |
1762 | struct on_exit | |
1763 | { | |
1764 | ~on_exit () | |
1765 | { | |
1766 | pop_all_targets_at_and_above (process_stratum); | |
1767 | } | |
1768 | } pop_targets; | |
1769 | ||
1770 | readwrite_regcache readwrite (gdbarch); | |
1771 | ||
1772 | const int num_regs = (gdbarch_num_regs (gdbarch) | |
1773 | + gdbarch_num_pseudo_regs (gdbarch)); | |
1774 | ||
1775 | for (auto regnum = 0; regnum < num_regs; regnum++) | |
1776 | { | |
1777 | if (register_size (gdbarch, regnum) == 0 | |
1778 | || gdbarch_cannot_store_register (gdbarch, regnum)) | |
1779 | continue; | |
1780 | ||
1781 | auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; | |
1782 | ||
1783 | if ((bfd_arch == bfd_arch_sparc | |
1784 | /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM, | |
1785 | SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */ | |
1786 | && gdbarch_ptr_bit (gdbarch) == 64 | |
1787 | && (regnum >= gdbarch_num_regs (gdbarch) | |
1788 | && regnum <= gdbarch_num_regs (gdbarch) + 4)) | |
1789 | || (bfd_arch == bfd_arch_sh | |
1790 | /* FPSCR_C_REGNUM in sh64 is hard to test. */ | |
1791 | && gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh5 | |
1792 | && regnum == 243) | |
1793 | || (bfd_arch == bfd_arch_spu | |
1794 | /* SPU pseudo registers except SPU_SP_REGNUM are got by | |
1795 | TARGET_OBJECT_SPU. */ | |
1796 | && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130)) | |
1797 | continue; | |
1798 | ||
1799 | std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0); | |
1800 | std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0); | |
1801 | const auto type = register_type (gdbarch, regnum); | |
1802 | ||
1803 | if (TYPE_CODE (type) == TYPE_CODE_FLT | |
1804 | || TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1805 | { | |
1806 | /* Generate valid float format. */ | |
1807 | target_float_from_string (expected.data (), type, "1.25"); | |
1808 | } | |
1809 | else if (TYPE_CODE (type) == TYPE_CODE_INT | |
1810 | || TYPE_CODE (type) == TYPE_CODE_ARRAY | |
1811 | || TYPE_CODE (type) == TYPE_CODE_PTR | |
1812 | || TYPE_CODE (type) == TYPE_CODE_UNION | |
1813 | || TYPE_CODE (type) == TYPE_CODE_STRUCT) | |
1814 | { | |
1815 | if (bfd_arch == bfd_arch_ia64 | |
1816 | || (regnum >= gdbarch_num_regs (gdbarch) | |
1817 | && (bfd_arch == bfd_arch_xtensa | |
1818 | || bfd_arch == bfd_arch_bfin | |
1819 | || bfd_arch == bfd_arch_m32c | |
1820 | /* m68hc11 pseudo registers are in memory. */ | |
1821 | || bfd_arch == bfd_arch_m68hc11 | |
1822 | || bfd_arch == bfd_arch_m68hc12 | |
1823 | || bfd_arch == bfd_arch_s390)) | |
1824 | || (bfd_arch == bfd_arch_frv | |
1825 | /* FRV pseudo registers except iacc0. */ | |
1826 | && regnum > gdbarch_num_regs (gdbarch))) | |
1827 | { | |
1828 | /* Skip setting the expected values for some architecture | |
1829 | registers. */ | |
1830 | } | |
1831 | else if (bfd_arch == bfd_arch_rl78 && regnum == 40) | |
1832 | { | |
1833 | /* RL78_PC_REGNUM */ | |
1834 | for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++) | |
1835 | expected[j] = j; | |
1836 | } | |
1837 | else | |
1838 | { | |
1839 | for (auto j = 0; j < register_size (gdbarch, regnum); j++) | |
1840 | expected[j] = j; | |
1841 | } | |
1842 | } | |
1843 | else if (TYPE_CODE (type) == TYPE_CODE_FLAGS) | |
1844 | { | |
1845 | /* No idea how to test flags. */ | |
1846 | continue; | |
1847 | } | |
1848 | else | |
1849 | { | |
1850 | /* If we don't know how to create the expected value for the | |
1851 | this type, make it fail. */ | |
1852 | SELF_CHECK (0); | |
1853 | } | |
1854 | ||
1855 | readwrite.cooked_write (regnum, expected.data ()); | |
1856 | ||
1857 | SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID); | |
1858 | SELF_CHECK (expected == buf); | |
1859 | } | |
1860 | } | |
1861 | ||
8248946c YQ |
1862 | } // namespace selftests |
1863 | #endif /* GDB_SELF_TEST */ | |
1864 | ||
32178cab MS |
1865 | void |
1866 | _initialize_regcache (void) | |
1867 | { | |
3e43a32a MS |
1868 | regcache_descr_handle |
1869 | = gdbarch_data_register_post_init (init_regcache_descr); | |
705152c5 | 1870 | |
76727919 TT |
1871 | gdb::observers::target_changed.attach (regcache_observer_target_changed); |
1872 | gdb::observers::thread_ptid_changed.attach | |
1873 | (regcache::regcache_thread_ptid_changed); | |
f4c5303c | 1874 | |
705152c5 | 1875 | add_com ("flushregs", class_maintenance, reg_flush_command, |
1bedd215 | 1876 | _("Force gdb to flush its register cache (maintainer command)")); |
39f77062 | 1877 | |
8248946c | 1878 | #if GDB_SELF_TEST |
1526853e | 1879 | selftests::register_test ("current_regcache", selftests::current_regcache_test); |
1b30aaa5 YQ |
1880 | |
1881 | selftests::register_test_foreach_arch ("regcache::cooked_read_test", | |
1882 | selftests::cooked_read_test); | |
ec7a5fcb YQ |
1883 | selftests::register_test_foreach_arch ("regcache::cooked_write_test", |
1884 | selftests::cooked_write_test); | |
8248946c | 1885 | #endif |
32178cab | 1886 | } |