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