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