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