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