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