]>
Commit | Line | Data |
---|---|---|
32178cab | 1 | /* Cache and manage the values of registers for GDB, the GNU debugger. |
3fadccb3 | 2 | |
d01e8234 | 3 | Copyright (C) 1986-2025 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 | 19 | |
ec452525 | 20 | #include "extract-store-integer.h" |
32178cab | 21 | #include "inferior.h" |
00431a78 | 22 | #include "gdbthread.h" |
32178cab | 23 | #include "target.h" |
c180496d | 24 | #include "test-target.h" |
236ef034 | 25 | #include "scoped-mock-context.h" |
32178cab | 26 | #include "gdbarch.h" |
4e052eda | 27 | #include "regcache.h" |
b59ff9d5 | 28 | #include "reggroups.h" |
76727919 | 29 | #include "observable.h" |
0b309272 | 30 | #include "regset.h" |
1038e933 | 31 | #include "gdbsupport/unordered_map.h" |
888bdb2b | 32 | #include <unordered_map> |
50a5f187 | 33 | #include "cli/cli-cmds.h" |
32178cab MS |
34 | |
35 | /* | |
36 | * DATA STRUCTURE | |
37 | * | |
38 | * Here is the actual register cache. | |
39 | */ | |
40 | ||
3fadccb3 | 41 | /* Per-architecture object describing the layout of a register cache. |
0df8b418 | 42 | Computed once when the architecture is created. */ |
3fadccb3 | 43 | |
3fadccb3 AC |
44 | struct regcache_descr |
45 | { | |
46 | /* The architecture this descriptor belongs to. */ | |
cb275538 | 47 | struct gdbarch *gdbarch = nullptr; |
3fadccb3 | 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. */ |
cb275538 | 54 | long sizeof_raw_registers = 0; |
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. */ |
cb275538 TT |
62 | int nr_cooked_registers = 0; |
63 | long sizeof_cooked_registers = 0; | |
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. */ | |
cb275538 TT |
69 | long *register_offset = nullptr; |
70 | long *sizeof_register = nullptr; | |
3fadccb3 | 71 | |
bb425013 | 72 | /* Cached table containing the type of each register. */ |
cb275538 | 73 | struct type **register_type = nullptr; |
3fadccb3 AC |
74 | }; |
75 | ||
cb275538 TT |
76 | static const registry<gdbarch>::key<struct regcache_descr> |
77 | regcache_descr_handle; | |
78 | ||
79 | static struct regcache_descr * | |
3fadccb3 AC |
80 | init_regcache_descr (struct gdbarch *gdbarch) |
81 | { | |
82 | int i; | |
83 | struct regcache_descr *descr; | |
84 | gdb_assert (gdbarch != NULL); | |
85 | ||
bb425013 | 86 | /* Create an initial, zero filled, table. */ |
cb275538 | 87 | descr = new struct regcache_descr; |
3fadccb3 | 88 | descr->gdbarch = gdbarch; |
3fadccb3 | 89 | |
d138e37a AC |
90 | /* Total size of the register space. The raw registers are mapped |
91 | directly onto the raw register cache while the pseudo's are | |
3fadccb3 | 92 | either mapped onto raw-registers or memory. */ |
f6efe3f8 | 93 | descr->nr_cooked_registers = gdbarch_num_cooked_regs (gdbarch); |
3fadccb3 | 94 | |
bb425013 | 95 | /* Fill in a table of register types. */ |
116f06ea | 96 | descr->register_type |
3e43a32a MS |
97 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, |
98 | struct type *); | |
bb425013 | 99 | for (i = 0; i < descr->nr_cooked_registers; i++) |
336a3131 | 100 | descr->register_type[i] = gdbarch_register_type (gdbarch, i); |
bb425013 | 101 | |
bb1db049 AC |
102 | /* Construct a strictly RAW register cache. Don't allow pseudo's |
103 | into the register cache. */ | |
bb1db049 | 104 | |
067df2e5 | 105 | /* Lay out the register cache. |
3fadccb3 | 106 | |
78134374 | 107 | NOTE: cagney/2002-05-22: Only register_type () is used when |
bb425013 AC |
108 | constructing the register cache. It is assumed that the |
109 | register's raw size, virtual size and type length are all the | |
110 | same. */ | |
3fadccb3 AC |
111 | |
112 | { | |
113 | long offset = 0; | |
123f5f96 | 114 | |
116f06ea AC |
115 | descr->sizeof_register |
116 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
117 | descr->register_offset | |
118 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
d999647b | 119 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
99e42fd8 | 120 | { |
df86565b | 121 | descr->sizeof_register[i] = descr->register_type[i]->length (); |
99e42fd8 PA |
122 | descr->register_offset[i] = offset; |
123 | offset += descr->sizeof_register[i]; | |
99e42fd8 PA |
124 | } |
125 | /* Set the real size of the raw register cache buffer. */ | |
126 | descr->sizeof_raw_registers = offset; | |
127 | ||
128 | for (; i < descr->nr_cooked_registers; i++) | |
3fadccb3 | 129 | { |
df86565b | 130 | descr->sizeof_register[i] = descr->register_type[i]->length (); |
3fadccb3 AC |
131 | descr->register_offset[i] = offset; |
132 | offset += descr->sizeof_register[i]; | |
3fadccb3 | 133 | } |
99e42fd8 | 134 | /* Set the real size of the readonly register cache buffer. */ |
067df2e5 | 135 | descr->sizeof_cooked_registers = offset; |
3fadccb3 AC |
136 | } |
137 | ||
3fadccb3 AC |
138 | return descr; |
139 | } | |
140 | ||
141 | static struct regcache_descr * | |
142 | regcache_descr (struct gdbarch *gdbarch) | |
143 | { | |
cb275538 TT |
144 | struct regcache_descr *result = regcache_descr_handle.get (gdbarch); |
145 | if (result == nullptr) | |
146 | { | |
147 | result = init_regcache_descr (gdbarch); | |
148 | regcache_descr_handle.set (gdbarch, result); | |
149 | } | |
150 | ||
151 | return result; | |
3fadccb3 AC |
152 | } |
153 | ||
bb425013 AC |
154 | /* Utility functions returning useful register attributes stored in |
155 | the regcache descr. */ | |
156 | ||
157 | struct type * | |
158 | register_type (struct gdbarch *gdbarch, int regnum) | |
159 | { | |
160 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
123f5f96 | 161 | |
bb425013 AC |
162 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
163 | return descr->register_type[regnum]; | |
164 | } | |
165 | ||
0ed04cce AC |
166 | /* Utility functions returning useful register attributes stored in |
167 | the regcache descr. */ | |
168 | ||
08a617da AC |
169 | int |
170 | register_size (struct gdbarch *gdbarch, int regnum) | |
171 | { | |
172 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
173 | int size; | |
123f5f96 | 174 | |
f6efe3f8 | 175 | gdb_assert (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch)); |
08a617da | 176 | size = descr->sizeof_register[regnum]; |
08a617da AC |
177 | return size; |
178 | } | |
179 | ||
268a13a5 | 180 | /* See gdbsupport/common-regcache.h. */ |
8d689ee5 YQ |
181 | |
182 | int | |
d7070173 | 183 | reg_buffer::register_size (int regnum) const |
8d689ee5 | 184 | { |
d7070173 | 185 | return ::register_size (this->arch (), regnum); |
8d689ee5 YQ |
186 | } |
187 | ||
31716595 YQ |
188 | reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo) |
189 | : m_has_pseudo (has_pseudo) | |
3fadccb3 | 190 | { |
ef79d9a3 YQ |
191 | gdb_assert (gdbarch != NULL); |
192 | m_descr = regcache_descr (gdbarch); | |
4621115f | 193 | |
11bb5c41 SM |
194 | /* We don't zero-initialize the M_REGISTERS array, as the bytes it contains |
195 | aren't meaningful as long as the corresponding register status is not | |
196 | REG_VALID. */ | |
31716595 | 197 | if (has_pseudo) |
4621115f | 198 | { |
11bb5c41 | 199 | m_registers.reset (new gdb_byte[m_descr->sizeof_cooked_registers]); |
835dcf92 SM |
200 | m_register_status.reset |
201 | (new register_status[m_descr->nr_cooked_registers] ()); | |
4621115f YQ |
202 | } |
203 | else | |
204 | { | |
11bb5c41 | 205 | m_registers.reset (new gdb_byte[m_descr->sizeof_raw_registers]); |
835dcf92 SM |
206 | m_register_status.reset |
207 | (new register_status[gdbarch_num_regs (gdbarch)] ()); | |
4621115f | 208 | } |
31716595 YQ |
209 | } |
210 | ||
74387712 | 211 | regcache::regcache (inferior *inf_for_target_calls, gdbarch *gdbarch) |
796bb026 YQ |
212 | /* The register buffers. A read/write register cache can only hold |
213 | [0 .. gdbarch_num_regs). */ | |
74387712 | 214 | : detached_regcache (gdbarch, false), |
98994c7a | 215 | m_inf_for_target_calls (inf_for_target_calls) |
31716595 | 216 | { |
ef79d9a3 YQ |
217 | m_ptid = minus_one_ptid; |
218 | } | |
4621115f | 219 | |
302abd6e SM |
220 | readonly_detached_regcache::readonly_detached_regcache (regcache &src) |
221 | : readonly_detached_regcache (src.arch (), | |
51e6b8cf SM |
222 | [&src] (int regnum, |
223 | gdb::array_view<gdb_byte> buf) | |
224 | { return src.cooked_read (regnum, buf); }) | |
daf6667d YQ |
225 | { |
226 | } | |
227 | ||
ef79d9a3 | 228 | gdbarch * |
31716595 | 229 | reg_buffer::arch () const |
ef79d9a3 YQ |
230 | { |
231 | return m_descr->gdbarch; | |
232 | } | |
3fadccb3 | 233 | |
51e6b8cf | 234 | /* Helper for reg_buffer::register_buffer. */ |
51b1fe4e | 235 | |
51e6b8cf SM |
236 | template<typename ElemType> |
237 | gdb::array_view<ElemType> | |
31716595 | 238 | reg_buffer::register_buffer (int regnum) const |
51b1fe4e | 239 | { |
51e6b8cf SM |
240 | assert_regnum (regnum); |
241 | ElemType *start = &m_registers[m_descr->register_offset[regnum]]; | |
242 | int size = m_descr->sizeof_register[regnum]; | |
243 | return gdb::array_view<ElemType> (start, size); | |
244 | } | |
245 | ||
246 | /* See regcache.h. */ | |
247 | ||
248 | gdb::array_view<const gdb_byte> | |
249 | reg_buffer::register_buffer (int regnum) const | |
250 | { | |
251 | return register_buffer<const gdb_byte> (regnum); | |
252 | } | |
253 | ||
254 | /* See regcache.h. */ | |
255 | ||
256 | gdb::array_view<gdb_byte> | |
257 | reg_buffer::register_buffer (int regnum) | |
258 | { | |
259 | return register_buffer<gdb_byte> (regnum); | |
51b1fe4e AC |
260 | } |
261 | ||
ef79d9a3 | 262 | void |
302abd6e | 263 | reg_buffer::save (register_read_ftype cooked_read) |
ef79d9a3 YQ |
264 | { |
265 | struct gdbarch *gdbarch = m_descr->gdbarch; | |
123f5f96 | 266 | |
daf6667d YQ |
267 | /* It should have pseudo registers. */ |
268 | gdb_assert (m_has_pseudo); | |
2d28509a | 269 | /* Clear the dest. */ |
835dcf92 SM |
270 | memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers); |
271 | memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers); | |
2d28509a | 272 | /* Copy over any registers (identified by their membership in the |
f57d151a UW |
273 | save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs + |
274 | gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a | 275 | to save/restore `cooked' registers that live in memory. */ |
51e6b8cf | 276 | for (int regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) |
2d28509a AC |
277 | { |
278 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
279 | { | |
51e6b8cf SM |
280 | gdb::array_view<gdb_byte> dst_buf = register_buffer (regnum); |
281 | register_status status = cooked_read (regnum, dst_buf); | |
123f5f96 | 282 | |
50d6adef AH |
283 | gdb_assert (status != REG_UNKNOWN); |
284 | ||
285 | if (status != REG_VALID) | |
51e6b8cf | 286 | memset (dst_buf.data (), 0, dst_buf.size ()); |
05d1431c | 287 | |
ef79d9a3 | 288 | m_register_status[regnum] = status; |
2d28509a AC |
289 | } |
290 | } | |
291 | } | |
292 | ||
ef79d9a3 | 293 | void |
daf6667d | 294 | regcache::restore (readonly_detached_regcache *src) |
2d28509a | 295 | { |
ef79d9a3 | 296 | struct gdbarch *gdbarch = m_descr->gdbarch; |
2d28509a | 297 | int regnum; |
123f5f96 | 298 | |
fc5b8736 | 299 | gdb_assert (src != NULL); |
daf6667d | 300 | gdb_assert (src->m_has_pseudo); |
fc5b8736 YQ |
301 | |
302 | gdb_assert (gdbarch == src->arch ()); | |
303 | ||
2d28509a | 304 | /* Copy over any registers, being careful to only restore those that |
f57d151a UW |
305 | were both saved and need to be restored. The full [0 .. gdbarch_num_regs |
306 | + gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a | 307 | to save/restore `cooked' registers that live in memory. */ |
ef79d9a3 | 308 | for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) |
2d28509a | 309 | { |
5602984a | 310 | if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)) |
2d28509a | 311 | { |
ef79d9a3 YQ |
312 | if (src->m_register_status[regnum] == REG_VALID) |
313 | cooked_write (regnum, src->register_buffer (regnum)); | |
2d28509a AC |
314 | } |
315 | } | |
316 | } | |
317 | ||
268a13a5 | 318 | /* See gdbsupport/common-regcache.h. */ |
9c861883 | 319 | |
ef79d9a3 | 320 | enum register_status |
c8ec2f33 | 321 | reg_buffer::get_register_status (int regnum) const |
ef79d9a3 | 322 | { |
c8ec2f33 | 323 | assert_regnum (regnum); |
6ed7ea50 | 324 | |
aac0d564 | 325 | return m_register_status[regnum]; |
3fadccb3 AC |
326 | } |
327 | ||
ef79d9a3 | 328 | void |
9c861883 | 329 | reg_buffer::invalidate (int regnum) |
ef79d9a3 | 330 | { |
4e888c28 | 331 | assert_regnum (regnum); |
ef79d9a3 YQ |
332 | m_register_status[regnum] = REG_UNKNOWN; |
333 | } | |
9c5ea4d9 | 334 | |
4e888c28 | 335 | void |
31716595 | 336 | reg_buffer::assert_regnum (int regnum) const |
4e888c28 | 337 | { |
31716595 YQ |
338 | gdb_assert (regnum >= 0); |
339 | if (m_has_pseudo) | |
340 | gdb_assert (regnum < m_descr->nr_cooked_registers); | |
341 | else | |
342 | gdb_assert (regnum < gdbarch_num_regs (arch ())); | |
4e888c28 YQ |
343 | } |
344 | ||
888bdb2b SM |
345 | /* Type to map a ptid to a list of regcaches (one thread may have multiple |
346 | regcaches, associated to different gdbarches). */ | |
347 | ||
348 | using ptid_regcache_map | |
9a343d2b | 349 | = std::unordered_multimap<ptid_t, regcache_up>; |
888bdb2b | 350 | |
b70e516e | 351 | /* Type holding regcaches for a given pid. */ |
888bdb2b | 352 | |
1038e933 | 353 | using pid_ptid_regcache_map = gdb::unordered_map<int, ptid_regcache_map>; |
b70e516e SM |
354 | |
355 | /* Type holding regcaches for a given target. */ | |
356 | ||
357 | using target_pid_ptid_regcache_map | |
1038e933 | 358 | = gdb::unordered_map<process_stratum_target *, pid_ptid_regcache_map>; |
888bdb2b SM |
359 | |
360 | /* Global structure containing the existing regcaches. */ | |
3fadccb3 | 361 | |
5ebd2499 | 362 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
32178cab MS |
363 | recording if the register values have been changed (eg. by the |
364 | user). Therefore all registers must be written back to the | |
365 | target when appropriate. */ | |
b70e516e | 366 | static target_pid_ptid_regcache_map regcaches; |
c2250ad1 | 367 | |
74387712 SM |
368 | regcache * |
369 | get_thread_arch_regcache (inferior *inf_for_target_calls, ptid_t ptid, | |
370 | gdbarch *arch) | |
c2250ad1 | 371 | { |
98994c7a SM |
372 | gdb_assert (inf_for_target_calls != nullptr); |
373 | ||
374 | process_stratum_target *proc_target = inf_for_target_calls->process_target (); | |
375 | gdb_assert (proc_target != nullptr); | |
5b6d1e4f | 376 | |
b70e516e | 377 | /* Find the map for this target. */ |
98994c7a | 378 | pid_ptid_regcache_map &pid_ptid_regc_map = regcaches[proc_target]; |
b70e516e SM |
379 | |
380 | /* Find the map for this pid. */ | |
381 | ptid_regcache_map &ptid_regc_map = pid_ptid_regc_map[ptid.pid ()]; | |
594f7785 | 382 | |
888bdb2b SM |
383 | /* Check first if a regcache for this arch already exists. */ |
384 | auto range = ptid_regc_map.equal_range (ptid); | |
385 | for (auto it = range.first; it != range.second; ++it) | |
386 | { | |
387 | if (it->second->arch () == arch) | |
388 | return it->second.get (); | |
389 | } | |
594f7785 | 390 | |
888bdb2b | 391 | /* It does not exist, create it. */ |
74387712 | 392 | regcache *new_regcache = new regcache (inf_for_target_calls, arch); |
ef79d9a3 | 393 | new_regcache->set_ptid (ptid); |
38f8aa06 | 394 | /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up |
3bfdcabb | 395 | constructor explicitly instead of implicitly. */ |
38f8aa06 | 396 | ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache))); |
e2d96639 | 397 | |
e2d96639 YQ |
398 | return new_regcache; |
399 | } | |
400 | ||
5b6d1e4f | 401 | static process_stratum_target *current_thread_target; |
c2250ad1 UW |
402 | static ptid_t current_thread_ptid; |
403 | static struct gdbarch *current_thread_arch; | |
404 | ||
405 | struct regcache * | |
5b6d1e4f | 406 | get_thread_regcache (process_stratum_target *target, ptid_t ptid) |
c2250ad1 | 407 | { |
74387712 SM |
408 | inferior *inf = find_inferior_ptid (target, ptid); |
409 | ||
5b6d1e4f PA |
410 | if (!current_thread_arch |
411 | || target != current_thread_target | |
412 | || current_thread_ptid != ptid) | |
c2250ad1 | 413 | { |
5b6d1e4f PA |
414 | gdb_assert (ptid != null_ptid); |
415 | ||
c2250ad1 | 416 | current_thread_ptid = ptid; |
5b6d1e4f PA |
417 | current_thread_target = target; |
418 | ||
419 | scoped_restore_current_inferior restore_current_inferior; | |
74387712 | 420 | set_current_inferior (inf); |
c2250ad1 UW |
421 | current_thread_arch = target_thread_architecture (ptid); |
422 | } | |
423 | ||
74387712 | 424 | return get_thread_arch_regcache (inf, ptid, current_thread_arch); |
c2250ad1 UW |
425 | } |
426 | ||
00431a78 PA |
427 | /* See regcache.h. */ |
428 | ||
429 | struct regcache * | |
430 | get_thread_regcache (thread_info *thread) | |
431 | { | |
249d0812 PA |
432 | gdb_assert (thread->state != THREAD_EXITED); |
433 | ||
5b6d1e4f PA |
434 | return get_thread_regcache (thread->inf->process_target (), |
435 | thread->ptid); | |
00431a78 PA |
436 | } |
437 | ||
268a13a5 | 438 | /* See gdbsupport/common-regcache.h. */ |
361c8ade | 439 | |
e4e20d45 | 440 | reg_buffer_common * |
361c8ade GB |
441 | get_thread_regcache_for_ptid (ptid_t ptid) |
442 | { | |
5b6d1e4f PA |
443 | /* This function doesn't take a process_stratum_target parameter |
444 | because it's a gdbsupport/ routine implemented by both gdb and | |
445 | gdbserver. It always refers to a ptid of the current target. */ | |
446 | process_stratum_target *proc_target = current_inferior ()->process_target (); | |
447 | return get_thread_regcache (proc_target, ptid); | |
361c8ade | 448 | } |
32178cab | 449 | |
f4c5303c OF |
450 | /* Observer for the target_changed event. */ |
451 | ||
2c0b251b | 452 | static void |
f4c5303c OF |
453 | regcache_observer_target_changed (struct target_ops *target) |
454 | { | |
455 | registers_changed (); | |
456 | } | |
457 | ||
159ed7d9 SM |
458 | /* Update regcaches related to OLD_PTID to now use NEW_PTID. */ |
459 | static void | |
b161a60d SM |
460 | regcache_thread_ptid_changed (process_stratum_target *target, |
461 | ptid_t old_ptid, ptid_t new_ptid) | |
5231c1fd | 462 | { |
b70e516e SM |
463 | /* Look up map for target. */ |
464 | auto pid_ptid_regc_map_it = regcaches.find (target); | |
465 | if (pid_ptid_regc_map_it == regcaches.end ()) | |
466 | return; | |
888bdb2b | 467 | |
b70e516e SM |
468 | /* Look up map for pid. */ |
469 | pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second; | |
470 | auto ptid_regc_map_it = pid_ptid_regc_map.find (old_ptid.pid ()); | |
471 | if (ptid_regc_map_it == pid_ptid_regc_map.end ()) | |
888bdb2b SM |
472 | return; |
473 | ||
b70e516e SM |
474 | /* Update all regcaches belonging to old_ptid. */ |
475 | ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second; | |
888bdb2b SM |
476 | auto range = ptid_regc_map.equal_range (old_ptid); |
477 | for (auto it = range.first; it != range.second;) | |
94bb8dfe | 478 | { |
888bdb2b SM |
479 | regcache_up rc = std::move (it->second); |
480 | rc->set_ptid (new_ptid); | |
481 | ||
482 | /* Remove old before inserting new, to avoid rehashing, | |
483 | which would invalidate iterators. */ | |
484 | it = ptid_regc_map.erase (it); | |
485 | ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc))); | |
94bb8dfe | 486 | } |
5231c1fd PA |
487 | } |
488 | ||
32178cab MS |
489 | /* Low level examining and depositing of registers. |
490 | ||
491 | The caller is responsible for making sure that the inferior is | |
492 | stopped before calling the fetching routines, or it will get | |
493 | garbage. (a change from GDB version 3, in which the caller got the | |
494 | value from the last stop). */ | |
495 | ||
496 | /* REGISTERS_CHANGED () | |
497 | ||
498 | Indicate that registers may have changed, so invalidate the cache. */ | |
499 | ||
500 | void | |
5b6d1e4f | 501 | registers_changed_ptid (process_stratum_target *target, ptid_t ptid) |
32178cab | 502 | { |
888bdb2b SM |
503 | if (target == nullptr) |
504 | { | |
505 | /* Since there can be ptid clashes between targets, it's not valid to | |
506 | pass a ptid without saying to which target it belongs. */ | |
507 | gdb_assert (ptid == minus_one_ptid); | |
508 | ||
509 | /* Delete all the regcaches of all targets. */ | |
510 | regcaches.clear (); | |
511 | } | |
b70e516e SM |
512 | else if (ptid.is_pid ()) |
513 | { | |
514 | /* Non-NULL target and pid ptid, delete all regcaches belonging | |
515 | to this (TARGET, PID). */ | |
516 | ||
517 | /* Look up map for target. */ | |
518 | auto pid_ptid_regc_map_it = regcaches.find (target); | |
519 | if (pid_ptid_regc_map_it != regcaches.end ()) | |
520 | { | |
521 | pid_ptid_regcache_map &pid_ptid_regc_map | |
522 | = pid_ptid_regc_map_it->second; | |
523 | ||
524 | pid_ptid_regc_map.erase (ptid.pid ()); | |
525 | } | |
526 | } | |
888bdb2b | 527 | else if (ptid != minus_one_ptid) |
c2250ad1 | 528 | { |
888bdb2b | 529 | /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging |
b70e516e SM |
530 | to this (TARGET, PTID). */ |
531 | ||
532 | /* Look up map for target. */ | |
533 | auto pid_ptid_regc_map_it = regcaches.find (target); | |
534 | if (pid_ptid_regc_map_it != regcaches.end ()) | |
e66408ed | 535 | { |
b70e516e SM |
536 | pid_ptid_regcache_map &pid_ptid_regc_map |
537 | = pid_ptid_regc_map_it->second; | |
538 | ||
539 | /* Look up map for pid. */ | |
540 | auto ptid_regc_map_it | |
541 | = pid_ptid_regc_map.find (ptid.pid ()); | |
542 | if (ptid_regc_map_it != pid_ptid_regc_map.end ()) | |
543 | { | |
544 | ptid_regcache_map &ptid_regc_map | |
545 | = ptid_regc_map_it->second; | |
546 | ||
547 | ptid_regc_map.erase (ptid); | |
548 | } | |
e66408ed | 549 | } |
888bdb2b SM |
550 | } |
551 | else | |
552 | { | |
553 | /* Non-NULL target and minus_one_ptid, delete all regcaches | |
554 | associated to this target. */ | |
555 | regcaches.erase (target); | |
c2250ad1 | 556 | } |
32178cab | 557 | |
5b6d1e4f PA |
558 | if ((target == nullptr || current_thread_target == target) |
559 | && current_thread_ptid.matches (ptid)) | |
041274d8 | 560 | { |
5b6d1e4f | 561 | current_thread_target = NULL; |
041274d8 PA |
562 | current_thread_ptid = null_ptid; |
563 | current_thread_arch = NULL; | |
564 | } | |
32178cab | 565 | |
5b6d1e4f PA |
566 | if ((target == nullptr || current_inferior ()->process_target () == target) |
567 | && inferior_ptid.matches (ptid)) | |
041274d8 PA |
568 | { |
569 | /* We just deleted the regcache of the current thread. Need to | |
570 | forget about any frames we have cached, too. */ | |
571 | reinit_frame_cache (); | |
572 | } | |
573 | } | |
c2250ad1 | 574 | |
00431a78 PA |
575 | /* See regcache.h. */ |
576 | ||
577 | void | |
578 | registers_changed_thread (thread_info *thread) | |
579 | { | |
5b6d1e4f | 580 | registers_changed_ptid (thread->inf->process_target (), thread->ptid); |
00431a78 PA |
581 | } |
582 | ||
041274d8 PA |
583 | void |
584 | registers_changed (void) | |
585 | { | |
5b6d1e4f | 586 | registers_changed_ptid (nullptr, minus_one_ptid); |
32178cab MS |
587 | } |
588 | ||
ef79d9a3 YQ |
589 | void |
590 | regcache::raw_update (int regnum) | |
591 | { | |
4e888c28 | 592 | assert_regnum (regnum); |
8e368124 | 593 | |
3fadccb3 AC |
594 | /* Make certain that the register cache is up-to-date with respect |
595 | to the current thread. This switching shouldn't be necessary | |
596 | only there is still only one target side register cache. Sigh! | |
597 | On the bright side, at least there is a regcache object. */ | |
8e368124 | 598 | |
796bb026 | 599 | if (get_register_status (regnum) == REG_UNKNOWN) |
3fadccb3 | 600 | { |
6b09f134 | 601 | std::optional<scoped_restore_current_thread> maybe_restore_thread |
98994c7a SM |
602 | = maybe_switch_inferior (m_inf_for_target_calls); |
603 | ||
ef79d9a3 | 604 | target_fetch_registers (this, regnum); |
788c8b10 PA |
605 | |
606 | /* A number of targets can't access the whole set of raw | |
607 | registers (because the debug API provides no means to get at | |
608 | them). */ | |
ef79d9a3 YQ |
609 | if (m_register_status[regnum] == REG_UNKNOWN) |
610 | m_register_status[regnum] = REG_UNAVAILABLE; | |
3fadccb3 | 611 | } |
8e368124 AH |
612 | } |
613 | ||
51e6b8cf SM |
614 | register_status |
615 | readable_regcache::raw_read (int regnum, gdb::array_view<gdb_byte> dst) | |
8e368124 | 616 | { |
51e6b8cf SM |
617 | assert_regnum (regnum); |
618 | gdb_assert (dst.size () == m_descr->sizeof_register[regnum]); | |
619 | ||
ef79d9a3 | 620 | raw_update (regnum); |
05d1431c | 621 | |
ef79d9a3 | 622 | if (m_register_status[regnum] != REG_VALID) |
51e6b8cf | 623 | memset (dst.data (), 0, dst.size ()); |
05d1431c | 624 | else |
51e6b8cf | 625 | copy (register_buffer (regnum), dst); |
05d1431c | 626 | |
aac0d564 | 627 | return m_register_status[regnum]; |
61a0eb5b AC |
628 | } |
629 | ||
51e6b8cf SM |
630 | register_status |
631 | readable_regcache::raw_read (int regnum, gdb_byte *dst) | |
632 | { | |
633 | assert_regnum (regnum); | |
634 | int size = m_descr->sizeof_register[regnum]; | |
635 | return raw_read (regnum, gdb::make_array_view (dst, size)); | |
636 | } | |
637 | ||
05d1431c | 638 | enum register_status |
28fc6740 | 639 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) |
ef79d9a3 YQ |
640 | { |
641 | gdb_assert (regcache != NULL); | |
6f98355c | 642 | return regcache->raw_read (regnum, val); |
ef79d9a3 YQ |
643 | } |
644 | ||
6f98355c | 645 | template<typename T, typename> |
ef79d9a3 | 646 | enum register_status |
849d0ba8 | 647 | readable_regcache::raw_read (int regnum, T *val) |
28fc6740 | 648 | { |
4e888c28 | 649 | assert_regnum (regnum); |
51e6b8cf SM |
650 | size_t size = m_descr->sizeof_register[regnum]; |
651 | gdb_byte *buf = (gdb_byte *) alloca (size); | |
652 | auto view = gdb::make_array_view (buf, size); | |
653 | register_status status = raw_read (regnum, view); | |
654 | ||
05d1431c | 655 | if (status == REG_VALID) |
51e6b8cf | 656 | *val = extract_integer<T> (view, gdbarch_byte_order (m_descr->gdbarch)); |
05d1431c PA |
657 | else |
658 | *val = 0; | |
51e6b8cf | 659 | |
05d1431c | 660 | return status; |
28fc6740 AC |
661 | } |
662 | ||
05d1431c | 663 | enum register_status |
e4e20d45 | 664 | regcache_raw_read_unsigned (reg_buffer_common *regcache, int regnum, |
28fc6740 | 665 | ULONGEST *val) |
ef79d9a3 YQ |
666 | { |
667 | gdb_assert (regcache != NULL); | |
e4e20d45 SM |
668 | return gdb::checked_static_cast<struct regcache *> (regcache)->raw_read |
669 | (regnum, val); | |
28fc6740 AC |
670 | } |
671 | ||
c00dcbe9 MK |
672 | void |
673 | regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) | |
ef79d9a3 YQ |
674 | { |
675 | gdb_assert (regcache != NULL); | |
6f98355c | 676 | regcache->raw_write (regnum, val); |
ef79d9a3 YQ |
677 | } |
678 | ||
6f98355c | 679 | template<typename T, typename> |
ef79d9a3 | 680 | void |
6f98355c | 681 | regcache::raw_write (int regnum, T val) |
c00dcbe9 | 682 | { |
4e888c28 | 683 | assert_regnum (regnum); |
51e6b8cf SM |
684 | |
685 | int size = m_descr->sizeof_register[regnum]; | |
686 | gdb_byte *buf = (gdb_byte *) alloca (size); | |
687 | auto view = gdb::make_array_view (buf, size); | |
688 | store_integer (view, gdbarch_byte_order (m_descr->gdbarch), val); | |
689 | raw_write (regnum, view); | |
c00dcbe9 MK |
690 | } |
691 | ||
692 | void | |
693 | regcache_raw_write_unsigned (struct regcache *regcache, int regnum, | |
694 | ULONGEST val) | |
ef79d9a3 YQ |
695 | { |
696 | gdb_assert (regcache != NULL); | |
6f98355c | 697 | regcache->raw_write (regnum, val); |
c00dcbe9 MK |
698 | } |
699 | ||
9fd15b2e YQ |
700 | LONGEST |
701 | regcache_raw_get_signed (struct regcache *regcache, int regnum) | |
702 | { | |
703 | LONGEST value; | |
704 | enum register_status status; | |
705 | ||
706 | status = regcache_raw_read_signed (regcache, regnum, &value); | |
707 | if (status == REG_UNAVAILABLE) | |
708 | throw_error (NOT_AVAILABLE_ERROR, | |
709 | _("Register %d is not available"), regnum); | |
710 | return value; | |
711 | } | |
712 | ||
51e6b8cf SM |
713 | /* See regcache.h. */ |
714 | ||
715 | register_status | |
716 | readable_regcache::cooked_read (int regnum, gdb::array_view<gdb_byte> dst) | |
68365089 | 717 | { |
d138e37a | 718 | gdb_assert (regnum >= 0); |
ef79d9a3 | 719 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
51e6b8cf | 720 | |
d999647b | 721 | if (regnum < num_raw_registers ()) |
51e6b8cf SM |
722 | return raw_read (regnum, dst); |
723 | ||
724 | gdb_assert (dst.size () == m_descr->sizeof_register[regnum]); | |
725 | ||
726 | if (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN) | |
05d1431c | 727 | { |
ef79d9a3 | 728 | if (m_register_status[regnum] == REG_VALID) |
51e6b8cf | 729 | copy (register_buffer (regnum), dst); |
05d1431c | 730 | else |
51e6b8cf | 731 | memset (dst.data (), 0, dst.size ()); |
05d1431c | 732 | |
aac0d564 | 733 | return m_register_status[regnum]; |
05d1431c | 734 | } |
ef79d9a3 | 735 | else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) |
3543a589 | 736 | { |
51e6b8cf | 737 | register_status result = REG_VALID; |
65558ca5 | 738 | scoped_value_mark mark; |
b3245cef SM |
739 | value *computed = gdbarch_pseudo_register_read_value |
740 | (m_descr->gdbarch, get_next_frame_sentinel_okay (get_current_frame ()), | |
741 | regnum); | |
3543a589 | 742 | |
d00664db | 743 | if (computed->entirely_available ()) |
51e6b8cf | 744 | copy (computed->contents_raw (), dst); |
3543a589 TT |
745 | else |
746 | { | |
51e6b8cf | 747 | memset (dst.data (), 0, dst.size ()); |
3543a589 TT |
748 | result = REG_UNAVAILABLE; |
749 | } | |
750 | ||
3543a589 TT |
751 | return result; |
752 | } | |
d138e37a | 753 | else |
51e6b8cf SM |
754 | return gdbarch_pseudo_register_read (m_descr->gdbarch, this, regnum, |
755 | dst.data ()); | |
756 | } | |
757 | ||
758 | /* See regcache.h. */ | |
759 | ||
760 | register_status | |
761 | readable_regcache::cooked_read (int regnum, gdb_byte *dst) | |
762 | { | |
763 | gdb_assert (regnum >= 0); | |
764 | gdb_assert (regnum < m_descr->nr_cooked_registers); | |
765 | ||
766 | int size = m_descr->sizeof_register[regnum]; | |
767 | return cooked_read (regnum, gdb::make_array_view (dst, size)); | |
61a0eb5b AC |
768 | } |
769 | ||
ef79d9a3 | 770 | struct value * |
849d0ba8 | 771 | readable_regcache::cooked_read_value (int regnum) |
3543a589 TT |
772 | { |
773 | gdb_assert (regnum >= 0); | |
ef79d9a3 | 774 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
3543a589 | 775 | |
d999647b | 776 | if (regnum < num_raw_registers () |
849d0ba8 | 777 | || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN) |
ef79d9a3 | 778 | || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) |
3543a589 | 779 | { |
6831f2cd SM |
780 | value *result = value::allocate_register |
781 | (get_next_frame_sentinel_okay (get_current_frame ()), regnum); | |
3543a589 TT |
782 | |
783 | /* It is more efficient in general to do this delegation in this | |
784 | direction than in the other one, even though the value-based | |
785 | API is preferred. */ | |
51e6b8cf | 786 | if (cooked_read (regnum, result->contents_raw ()) == REG_UNAVAILABLE) |
d00664db TT |
787 | result->mark_bytes_unavailable (0, |
788 | result->type ()->length ()); | |
3543a589 TT |
789 | |
790 | return result; | |
791 | } | |
792 | else | |
b3245cef SM |
793 | return gdbarch_pseudo_register_read_value |
794 | (m_descr->gdbarch, get_next_frame_sentinel_okay (get_current_frame ()), | |
795 | regnum); | |
3543a589 TT |
796 | } |
797 | ||
05d1431c | 798 | enum register_status |
a378f419 AC |
799 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, |
800 | LONGEST *val) | |
ef79d9a3 YQ |
801 | { |
802 | gdb_assert (regcache != NULL); | |
6f98355c | 803 | return regcache->cooked_read (regnum, val); |
ef79d9a3 YQ |
804 | } |
805 | ||
6f98355c | 806 | template<typename T, typename> |
ef79d9a3 | 807 | enum register_status |
849d0ba8 | 808 | readable_regcache::cooked_read (int regnum, T *val) |
a378f419 | 809 | { |
ef79d9a3 | 810 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); |
51e6b8cf SM |
811 | size_t size = m_descr->sizeof_register[regnum]; |
812 | gdb_byte *buf = (gdb_byte *) alloca (size); | |
813 | auto view = gdb::make_array_view (buf, size); | |
814 | register_status status = cooked_read (regnum, view); | |
05d1431c | 815 | if (status == REG_VALID) |
51e6b8cf | 816 | *val = extract_integer<T> (view, gdbarch_byte_order (m_descr->gdbarch)); |
05d1431c PA |
817 | else |
818 | *val = 0; | |
819 | return status; | |
a378f419 AC |
820 | } |
821 | ||
05d1431c | 822 | enum register_status |
a378f419 AC |
823 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, |
824 | ULONGEST *val) | |
ef79d9a3 YQ |
825 | { |
826 | gdb_assert (regcache != NULL); | |
6f98355c | 827 | return regcache->cooked_read (regnum, val); |
a378f419 AC |
828 | } |
829 | ||
a66a9c23 AC |
830 | void |
831 | regcache_cooked_write_signed (struct regcache *regcache, int regnum, | |
832 | LONGEST val) | |
ef79d9a3 YQ |
833 | { |
834 | gdb_assert (regcache != NULL); | |
6f98355c | 835 | regcache->cooked_write (regnum, val); |
ef79d9a3 YQ |
836 | } |
837 | ||
6f98355c | 838 | template<typename T, typename> |
ef79d9a3 | 839 | void |
6f98355c | 840 | regcache::cooked_write (int regnum, T val) |
a66a9c23 | 841 | { |
51e6b8cf SM |
842 | gdb_assert (regnum >= 0); |
843 | gdb_assert (regnum < m_descr->nr_cooked_registers); | |
123f5f96 | 844 | |
51e6b8cf SM |
845 | int size = m_descr->sizeof_register[regnum]; |
846 | gdb_byte *buf = (gdb_byte *) alloca (size); | |
847 | auto view = gdb::make_array_view (buf, size); | |
848 | store_integer (view, gdbarch_byte_order (m_descr->gdbarch), val); | |
849 | cooked_write (regnum, view); | |
a66a9c23 AC |
850 | } |
851 | ||
852 | void | |
853 | regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, | |
854 | ULONGEST val) | |
ef79d9a3 YQ |
855 | { |
856 | gdb_assert (regcache != NULL); | |
6f98355c | 857 | regcache->cooked_write (regnum, val); |
a66a9c23 AC |
858 | } |
859 | ||
ef79d9a3 | 860 | void |
51e6b8cf | 861 | regcache::raw_write (int regnum, gdb::array_view<const gdb_byte> src) |
61a0eb5b | 862 | { |
4e888c28 | 863 | assert_regnum (regnum); |
51e6b8cf | 864 | gdb_assert (src.size () == m_descr->sizeof_register[regnum]); |
3fadccb3 | 865 | |
3fadccb3 AC |
866 | /* On the sparc, writing %g0 is a no-op, so we don't even want to |
867 | change the registers array if something writes to this register. */ | |
ef79d9a3 | 868 | if (gdbarch_cannot_store_register (arch (), regnum)) |
3fadccb3 AC |
869 | return; |
870 | ||
3fadccb3 | 871 | /* If we have a valid copy of the register, and new value == old |
0df8b418 | 872 | value, then don't bother doing the actual store. */ |
ef79d9a3 | 873 | if (get_register_status (regnum) == REG_VALID |
51e6b8cf SM |
874 | && (memcmp (register_buffer (regnum).data (), src.data (), src.size ()) |
875 | == 0)) | |
3fadccb3 AC |
876 | return; |
877 | ||
6b09f134 | 878 | std::optional<scoped_restore_current_thread> maybe_restore_thread |
98994c7a SM |
879 | = maybe_switch_inferior (m_inf_for_target_calls); |
880 | ||
ef79d9a3 | 881 | target_prepare_to_store (this); |
51e6b8cf | 882 | raw_supply (regnum, src); |
b94ade42 | 883 | |
b292235f TT |
884 | /* Invalidate the register after it is written, in case of a |
885 | failure. */ | |
311dc83a TT |
886 | auto invalidator |
887 | = make_scope_exit ([&] { this->invalidate (regnum); }); | |
b94ade42 | 888 | |
ef79d9a3 | 889 | target_store_registers (this, regnum); |
594f7785 | 890 | |
b292235f TT |
891 | /* The target did not throw an error so we can discard invalidating |
892 | the register. */ | |
893 | invalidator.release (); | |
61a0eb5b AC |
894 | } |
895 | ||
ef79d9a3 | 896 | void |
51e6b8cf SM |
897 | regcache::raw_write (int regnum, const gdb_byte *src) |
898 | { | |
899 | assert_regnum (regnum); | |
900 | ||
901 | int size = m_descr->sizeof_register[regnum]; | |
902 | raw_write (regnum, gdb::make_array_view (src, size)); | |
903 | } | |
904 | ||
905 | /* See regcache.h. */ | |
906 | ||
907 | void | |
908 | regcache::cooked_write (int regnum, gdb::array_view<const gdb_byte> src) | |
68365089 | 909 | { |
d138e37a | 910 | gdb_assert (regnum >= 0); |
ef79d9a3 | 911 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
51e6b8cf | 912 | |
d999647b | 913 | if (regnum < num_raw_registers ()) |
51e6b8cf | 914 | raw_write (regnum, src); |
1f624181 SM |
915 | else if (gdbarch_pseudo_register_write_p (m_descr->gdbarch)) |
916 | gdbarch_pseudo_register_write | |
917 | (m_descr->gdbarch, get_next_frame_sentinel_okay (get_current_frame ()), | |
918 | regnum, src); | |
d138e37a | 919 | else |
7f0f3b0f SM |
920 | gdbarch_deprecated_pseudo_register_write (m_descr->gdbarch, this, regnum, |
921 | src.data ()); | |
61a0eb5b AC |
922 | } |
923 | ||
33bab475 | 924 | /* See regcache.h. */ |
06c0b04e | 925 | |
51e6b8cf SM |
926 | void |
927 | regcache::cooked_write (int regnum, const gdb_byte *src) | |
928 | { | |
929 | gdb_assert (regnum >= 0); | |
930 | gdb_assert (regnum < m_descr->nr_cooked_registers); | |
931 | ||
932 | int size = m_descr->sizeof_register[regnum]; | |
933 | return cooked_write (regnum, gdb::make_array_view (src, size)); | |
934 | } | |
935 | ||
936 | /* See regcache.h. */ | |
937 | ||
938 | register_status | |
939 | readable_regcache::read_part (int regnum, int offset, | |
940 | gdb::array_view<gdb_byte> dst, bool is_raw) | |
849d0ba8 | 941 | { |
d7070173 | 942 | int reg_size = register_size (regnum); |
33bab475 | 943 | |
08d8e7ff | 944 | gdb_assert (offset >= 0); |
51e6b8cf | 945 | gdb_assert (offset + dst.size () <= reg_size); |
33bab475 | 946 | |
51e6b8cf | 947 | if (dst.size () == 0) |
33bab475 AH |
948 | { |
949 | /* Nothing to do. */ | |
950 | return REG_VALID; | |
951 | } | |
952 | ||
51e6b8cf | 953 | if (dst.size () == reg_size) |
33bab475 AH |
954 | { |
955 | /* Read the full register. */ | |
51e6b8cf SM |
956 | if (is_raw) |
957 | return raw_read (regnum, dst); | |
958 | else | |
959 | return cooked_read (regnum, dst); | |
33bab475 | 960 | } |
849d0ba8 | 961 | |
33bab475 | 962 | /* Read full register to buffer. */ |
51e6b8cf SM |
963 | register_status status; |
964 | gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size); | |
965 | auto reg = gdb::make_array_view (reg_buf, reg_size); | |
966 | ||
967 | if (is_raw) | |
968 | status = raw_read (regnum, reg); | |
969 | else | |
970 | status = cooked_read (regnum, reg); | |
971 | ||
849d0ba8 YQ |
972 | if (status != REG_VALID) |
973 | return status; | |
974 | ||
33bab475 | 975 | /* Copy out. */ |
51e6b8cf | 976 | copy (reg.slice (offset, dst.size ()), dst); |
849d0ba8 YQ |
977 | return REG_VALID; |
978 | } | |
979 | ||
33bab475 AH |
980 | /* See regcache.h. */ |
981 | ||
8e7767e3 | 982 | void |
51e6b8cf SM |
983 | reg_buffer::raw_collect_part (int regnum, int offset, |
984 | gdb::array_view<gdb_byte> dst) const | |
8e7767e3 | 985 | { |
d7070173 | 986 | int reg_size = register_size (regnum); |
8e7767e3 | 987 | |
08d8e7ff | 988 | gdb_assert (offset >= 0); |
51e6b8cf | 989 | gdb_assert (offset + dst.size () <= reg_size); |
8e7767e3 | 990 | |
51e6b8cf | 991 | if (dst.size () == 0) |
8e7767e3 AH |
992 | { |
993 | /* Nothing to do. */ | |
994 | return; | |
995 | } | |
996 | ||
51e6b8cf | 997 | if (dst.size () == reg_size) |
8e7767e3 AH |
998 | { |
999 | /* Collect the full register. */ | |
51e6b8cf | 1000 | return raw_collect (regnum, dst); |
8e7767e3 AH |
1001 | } |
1002 | ||
1003 | /* Read to buffer, then write out. */ | |
51e6b8cf SM |
1004 | gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size); |
1005 | auto reg = gdb::make_array_view (reg_buf, reg_size); | |
8e7767e3 | 1006 | raw_collect (regnum, reg); |
51e6b8cf | 1007 | copy (reg.slice (offset, dst.size ()), dst); |
8e7767e3 AH |
1008 | } |
1009 | ||
1010 | /* See regcache.h. */ | |
1011 | ||
51e6b8cf SM |
1012 | register_status |
1013 | regcache::write_part (int regnum, int offset, | |
1014 | gdb::array_view<const gdb_byte> src, bool is_raw) | |
ef79d9a3 | 1015 | { |
d7070173 | 1016 | int reg_size = register_size (regnum); |
123f5f96 | 1017 | |
08d8e7ff | 1018 | gdb_assert (offset >= 0); |
51e6b8cf | 1019 | gdb_assert (offset + src.size () <= reg_size); |
33bab475 | 1020 | |
51e6b8cf | 1021 | if (src.size () == 0) |
06c0b04e | 1022 | { |
33bab475 AH |
1023 | /* Nothing to do. */ |
1024 | return REG_VALID; | |
1025 | } | |
05d1431c | 1026 | |
51e6b8cf | 1027 | if (src.size () == reg_size) |
33bab475 AH |
1028 | { |
1029 | /* Write the full register. */ | |
51e6b8cf SM |
1030 | if (is_raw) |
1031 | raw_write (regnum, src); | |
1032 | else | |
1033 | cooked_write (regnum, src); | |
1034 | ||
33bab475 | 1035 | return REG_VALID; |
06c0b04e | 1036 | } |
849d0ba8 | 1037 | |
33bab475 | 1038 | /* Read existing register to buffer. */ |
51e6b8cf SM |
1039 | register_status status; |
1040 | gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size); | |
1041 | auto reg = gdb::make_array_view (reg_buf, reg_size); | |
1042 | ||
1043 | if (is_raw) | |
1044 | status = raw_read (regnum, reg); | |
1045 | else | |
1046 | status = cooked_read (regnum, reg); | |
1047 | ||
33bab475 AH |
1048 | if (status != REG_VALID) |
1049 | return status; | |
1050 | ||
1051 | /* Update buffer, then write back to regcache. */ | |
51e6b8cf SM |
1052 | copy (src, reg.slice (offset, src.size ())); |
1053 | ||
1054 | if (is_raw) | |
1055 | raw_write (regnum, reg); | |
1056 | else | |
1057 | cooked_write (regnum, reg); | |
1058 | ||
05d1431c | 1059 | return REG_VALID; |
06c0b04e AC |
1060 | } |
1061 | ||
33bab475 AH |
1062 | /* See regcache.h. */ |
1063 | ||
8e7767e3 | 1064 | void |
51e6b8cf SM |
1065 | reg_buffer::raw_supply_part (int regnum, int offset, |
1066 | gdb::array_view<const gdb_byte> src) | |
8e7767e3 | 1067 | { |
d7070173 | 1068 | int reg_size = register_size (regnum); |
8e7767e3 | 1069 | |
08d8e7ff | 1070 | gdb_assert (offset >= 0); |
51e6b8cf | 1071 | gdb_assert (offset + src.size () <= reg_size); |
8e7767e3 | 1072 | |
51e6b8cf | 1073 | if (src.size () == 0) |
8e7767e3 AH |
1074 | { |
1075 | /* Nothing to do. */ | |
1076 | return; | |
1077 | } | |
1078 | ||
51e6b8cf | 1079 | if (src.size () == reg_size) |
8e7767e3 AH |
1080 | { |
1081 | /* Supply the full register. */ | |
51e6b8cf | 1082 | return raw_supply (regnum, src); |
8e7767e3 AH |
1083 | } |
1084 | ||
8e7767e3 | 1085 | /* Read existing value to buffer. */ |
51e6b8cf SM |
1086 | gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size); |
1087 | auto reg = gdb::make_array_view (reg_buf, reg_size); | |
8e7767e3 AH |
1088 | raw_collect (regnum, reg); |
1089 | ||
1090 | /* Write to buffer, then write out. */ | |
51e6b8cf | 1091 | copy (src, reg.slice (offset, src.size ())); |
8e7767e3 AH |
1092 | raw_supply (regnum, reg); |
1093 | } | |
1094 | ||
51e6b8cf SM |
1095 | register_status |
1096 | readable_regcache::raw_read_part (int regnum, int offset, | |
1097 | gdb::array_view<gdb_byte> dst) | |
ef79d9a3 | 1098 | { |
4e888c28 | 1099 | assert_regnum (regnum); |
51e6b8cf | 1100 | return read_part (regnum, offset, dst, true); |
06c0b04e AC |
1101 | } |
1102 | ||
4f0420fd | 1103 | /* See regcache.h. */ |
123f5f96 | 1104 | |
ef79d9a3 | 1105 | void |
51e6b8cf SM |
1106 | regcache::raw_write_part (int regnum, int offset, |
1107 | gdb::array_view<const gdb_byte> src) | |
ef79d9a3 | 1108 | { |
4e888c28 | 1109 | assert_regnum (regnum); |
51e6b8cf | 1110 | write_part (regnum, offset, src, true); |
06c0b04e AC |
1111 | } |
1112 | ||
33bab475 AH |
1113 | /* See regcache.h. */ |
1114 | ||
51e6b8cf SM |
1115 | register_status |
1116 | readable_regcache::cooked_read_part (int regnum, int offset, | |
1117 | gdb::array_view<gdb_byte> dst) | |
ef79d9a3 YQ |
1118 | { |
1119 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); | |
51e6b8cf | 1120 | return read_part (regnum, offset, dst, false); |
06c0b04e AC |
1121 | } |
1122 | ||
33bab475 AH |
1123 | /* See regcache.h. */ |
1124 | ||
ef79d9a3 | 1125 | void |
51e6b8cf SM |
1126 | regcache::cooked_write_part (int regnum, int offset, |
1127 | gdb::array_view<const gdb_byte> src) | |
ef79d9a3 YQ |
1128 | { |
1129 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); | |
51e6b8cf | 1130 | write_part (regnum, offset, src, false); |
06c0b04e | 1131 | } |
32178cab | 1132 | |
268a13a5 | 1133 | /* See gdbsupport/common-regcache.h. */ |
9c861883 | 1134 | |
ef79d9a3 | 1135 | void |
51e6b8cf | 1136 | reg_buffer::raw_supply (int regnum, gdb::array_view<const gdb_byte> src) |
9a661b68 | 1137 | { |
51e6b8cf | 1138 | gdb::array_view<gdb_byte> dst = register_buffer (regnum); |
9a661b68 | 1139 | |
51e6b8cf | 1140 | if (src.data () != nullptr) |
ee99023e | 1141 | { |
51e6b8cf | 1142 | copy (src, dst); |
ef79d9a3 | 1143 | m_register_status[regnum] = REG_VALID; |
ee99023e | 1144 | } |
9a661b68 | 1145 | else |
ee99023e PA |
1146 | { |
1147 | /* This memset not strictly necessary, but better than garbage | |
1148 | in case the register value manages to escape somewhere (due | |
1149 | to a bug, no less). */ | |
51e6b8cf | 1150 | memset (dst.data (), 0, dst.size ()); |
ef79d9a3 | 1151 | m_register_status[regnum] = REG_UNAVAILABLE; |
ee99023e | 1152 | } |
9a661b68 MK |
1153 | } |
1154 | ||
9c861883 | 1155 | /* See regcache.h. */ |
b057297a AH |
1156 | |
1157 | void | |
51e6b8cf | 1158 | reg_buffer::raw_supply (int regnum, const void *src) |
b057297a | 1159 | { |
4e888c28 | 1160 | assert_regnum (regnum); |
b057297a | 1161 | |
51e6b8cf SM |
1162 | int size = m_descr->sizeof_register[regnum]; |
1163 | raw_supply (regnum, gdb::make_array_view ((const gdb_byte *) src, size)); | |
1164 | } | |
1165 | ||
1166 | /* See regcache.h. */ | |
b057297a | 1167 | |
51e6b8cf SM |
1168 | void |
1169 | reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len, | |
1170 | bool is_signed) | |
1171 | { | |
1172 | gdb::array_view<gdb_byte> dst = register_buffer (regnum); | |
1173 | bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); | |
1174 | ||
1175 | copy_integer_to_size (dst.data (), dst.size (), addr, addr_len, is_signed, | |
b057297a AH |
1176 | byte_order); |
1177 | m_register_status[regnum] = REG_VALID; | |
1178 | } | |
1179 | ||
9c861883 | 1180 | /* See regcache.h. */ |
f81fdd35 AH |
1181 | |
1182 | void | |
9c861883 | 1183 | reg_buffer::raw_supply_zeroed (int regnum) |
f81fdd35 | 1184 | { |
51e6b8cf SM |
1185 | gdb::array_view<gdb_byte> dst = register_buffer (regnum); |
1186 | memset (dst.data (), 0, dst.size ()); | |
f81fdd35 AH |
1187 | m_register_status[regnum] = REG_VALID; |
1188 | } | |
1189 | ||
268a13a5 | 1190 | /* See gdbsupport/common-regcache.h. */ |
9c861883 | 1191 | |
a4a5f052 TV |
1192 | void |
1193 | reg_buffer::raw_supply_part_zeroed (int regnum, int offset, size_t size) | |
1194 | { | |
1195 | gdb::array_view<gdb_byte> dst = register_buffer (regnum).slice (offset, size); | |
1196 | memset (dst.data (), 0, dst.size ()); | |
1197 | m_register_status[regnum] = REG_VALID; | |
1198 | } | |
1199 | ||
1200 | /* See gdbsupport/common-regcache.h. */ | |
1201 | ||
ef79d9a3 | 1202 | void |
51e6b8cf | 1203 | reg_buffer::raw_collect (int regnum, gdb::array_view<gdb_byte> dst) const |
9a661b68 | 1204 | { |
51e6b8cf SM |
1205 | gdb::array_view<const gdb_byte> src = register_buffer (regnum); |
1206 | copy (src, dst); | |
1207 | } | |
9a661b68 | 1208 | |
51e6b8cf SM |
1209 | /* See regcache.h. */ |
1210 | ||
1211 | void | |
1212 | reg_buffer::raw_collect (int regnum, void *dst) const | |
1213 | { | |
4e888c28 | 1214 | assert_regnum (regnum); |
9a661b68 | 1215 | |
51e6b8cf SM |
1216 | int size = m_descr->sizeof_register[regnum]; |
1217 | return raw_collect (regnum, gdb::make_array_view ((gdb_byte *) dst, size)); | |
9a661b68 MK |
1218 | } |
1219 | ||
9c861883 | 1220 | /* See regcache.h. */ |
b057297a AH |
1221 | |
1222 | void | |
9c861883 AH |
1223 | reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len, |
1224 | bool is_signed) const | |
b057297a | 1225 | { |
51e6b8cf SM |
1226 | gdb::array_view<const gdb_byte> dst = register_buffer (regnum); |
1227 | bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); | |
1228 | copy_integer_to_size (addr, addr_len, dst.data (), dst.size (), is_signed, | |
b057297a AH |
1229 | byte_order); |
1230 | } | |
1231 | ||
8e7767e3 AH |
1232 | /* See regcache.h. */ |
1233 | ||
1234 | void | |
1235 | regcache::transfer_regset_register (struct regcache *out_regcache, int regnum, | |
1236 | const gdb_byte *in_buf, gdb_byte *out_buf, | |
1237 | int slot_size, int offs) const | |
1238 | { | |
d7070173 | 1239 | int reg_size = std::min (register_size (regnum), slot_size); |
8e7767e3 AH |
1240 | |
1241 | /* Use part versions and reg_size to prevent possible buffer overflows when | |
1242 | accessing the regcache. */ | |
1243 | ||
1244 | if (out_buf != nullptr) | |
1245 | { | |
51e6b8cf SM |
1246 | raw_collect_part (regnum, 0, |
1247 | gdb::make_array_view (out_buf + offs, reg_size)); | |
8e7767e3 AH |
1248 | |
1249 | /* Ensure any additional space is cleared. */ | |
1250 | if (slot_size > reg_size) | |
1251 | memset (out_buf + offs + reg_size, 0, slot_size - reg_size); | |
1252 | } | |
1253 | else if (in_buf != nullptr) | |
b623bbc9 JB |
1254 | { |
1255 | /* Zero-extend the register value if the slot is smaller than the register. */ | |
d7070173 | 1256 | if (slot_size < register_size (regnum)) |
b623bbc9 | 1257 | out_regcache->raw_supply_zeroed (regnum); |
51e6b8cf SM |
1258 | out_regcache->raw_supply_part (regnum, 0, |
1259 | gdb::make_array_view (in_buf + offs, | |
1260 | reg_size)); | |
b623bbc9 | 1261 | } |
8e7767e3 AH |
1262 | else |
1263 | { | |
1264 | /* Invalidate the register. */ | |
51e6b8cf | 1265 | out_regcache->raw_supply (regnum, {}); |
8e7767e3 AH |
1266 | } |
1267 | } | |
1268 | ||
1269 | /* See regcache.h. */ | |
9c861883 | 1270 | |
ef79d9a3 | 1271 | void |
0ee9f16c | 1272 | regcache::transfer_regset (const struct regset *regset, int regbase, |
ef79d9a3 | 1273 | struct regcache *out_regcache, |
8e7767e3 AH |
1274 | int regnum, const gdb_byte *in_buf, |
1275 | gdb_byte *out_buf, size_t size) const | |
0b309272 AA |
1276 | { |
1277 | const struct regcache_map_entry *map; | |
1278 | int offs = 0, count; | |
1279 | ||
19ba03f4 SM |
1280 | for (map = (const struct regcache_map_entry *) regset->regmap; |
1281 | (count = map->count) != 0; | |
1282 | map++) | |
0b309272 AA |
1283 | { |
1284 | int regno = map->regno; | |
1285 | int slot_size = map->size; | |
1286 | ||
0ee9f16c JB |
1287 | if (regno != REGCACHE_MAP_SKIP) |
1288 | regno += regbase; | |
1289 | ||
0b309272 | 1290 | if (slot_size == 0 && regno != REGCACHE_MAP_SKIP) |
ef79d9a3 | 1291 | slot_size = m_descr->sizeof_register[regno]; |
0b309272 AA |
1292 | |
1293 | if (regno == REGCACHE_MAP_SKIP | |
1294 | || (regnum != -1 | |
1295 | && (regnum < regno || regnum >= regno + count))) | |
1296 | offs += count * slot_size; | |
1297 | ||
1298 | else if (regnum == -1) | |
1299 | for (; count--; regno++, offs += slot_size) | |
1300 | { | |
1301 | if (offs + slot_size > size) | |
c47886c7 | 1302 | return; |
0b309272 | 1303 | |
8e7767e3 AH |
1304 | transfer_regset_register (out_regcache, regno, in_buf, out_buf, |
1305 | slot_size, offs); | |
0b309272 AA |
1306 | } |
1307 | else | |
1308 | { | |
1309 | /* Transfer a single register and return. */ | |
1310 | offs += (regnum - regno) * slot_size; | |
1311 | if (offs + slot_size > size) | |
1312 | return; | |
1313 | ||
8e7767e3 AH |
1314 | transfer_regset_register (out_regcache, regnum, in_buf, out_buf, |
1315 | slot_size, offs); | |
0b309272 AA |
1316 | return; |
1317 | } | |
1318 | } | |
1319 | } | |
1320 | ||
1321 | /* Supply register REGNUM from BUF to REGCACHE, using the register map | |
1322 | in REGSET. If REGNUM is -1, do this for all registers in REGSET. | |
1323 | If BUF is NULL, set the register(s) to "unavailable" status. */ | |
1324 | ||
1325 | void | |
1326 | regcache_supply_regset (const struct regset *regset, | |
1327 | struct regcache *regcache, | |
1328 | int regnum, const void *buf, size_t size) | |
1329 | { | |
8e7767e3 | 1330 | regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size); |
ef79d9a3 YQ |
1331 | } |
1332 | ||
0ee9f16c JB |
1333 | /* See regcache.h. */ |
1334 | ||
ef79d9a3 | 1335 | void |
0ee9f16c | 1336 | regcache::supply_regset (const struct regset *regset, int regbase, |
ef79d9a3 YQ |
1337 | int regnum, const void *buf, size_t size) |
1338 | { | |
0ee9f16c JB |
1339 | transfer_regset (regset, regbase, this, regnum, (const gdb_byte *) buf, |
1340 | nullptr, size); | |
0b309272 AA |
1341 | } |
1342 | ||
1343 | /* Collect register REGNUM from REGCACHE to BUF, using the register | |
1344 | map in REGSET. If REGNUM is -1, do this for all registers in | |
1345 | REGSET. */ | |
1346 | ||
1347 | void | |
1348 | regcache_collect_regset (const struct regset *regset, | |
1349 | const struct regcache *regcache, | |
1350 | int regnum, void *buf, size_t size) | |
1351 | { | |
8e7767e3 | 1352 | regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size); |
ef79d9a3 YQ |
1353 | } |
1354 | ||
0ee9f16c JB |
1355 | /* See regcache.h */ |
1356 | ||
ef79d9a3 | 1357 | void |
0ee9f16c | 1358 | regcache::collect_regset (const struct regset *regset, int regbase, |
ef79d9a3 YQ |
1359 | int regnum, void *buf, size_t size) const |
1360 | { | |
0ee9f16c JB |
1361 | transfer_regset (regset, regbase, nullptr, regnum, nullptr, (gdb_byte *) buf, |
1362 | size); | |
0b309272 AA |
1363 | } |
1364 | ||
30a696c5 JB |
1365 | bool |
1366 | regcache_map_supplies (const struct regcache_map_entry *map, int regnum, | |
1367 | struct gdbarch *gdbarch, size_t size) | |
1368 | { | |
1369 | int offs = 0, count; | |
1370 | ||
1371 | for (; (count = map->count) != 0; map++) | |
1372 | { | |
1373 | int regno = map->regno; | |
1374 | int slot_size = map->size; | |
1375 | ||
1376 | if (slot_size == 0 && regno != REGCACHE_MAP_SKIP) | |
1377 | slot_size = register_size (gdbarch, regno); | |
1378 | ||
1379 | if (regno != REGCACHE_MAP_SKIP && regnum >= regno | |
1380 | && regnum < regno + count) | |
1381 | return offs + (regnum - regno + 1) * slot_size <= size; | |
1382 | ||
1383 | offs += count * slot_size; | |
1384 | if (offs >= size) | |
1385 | return false; | |
1386 | } | |
1387 | return false; | |
1388 | } | |
1389 | ||
268a13a5 | 1390 | /* See gdbsupport/common-regcache.h. */ |
f868386e AH |
1391 | |
1392 | bool | |
1393 | reg_buffer::raw_compare (int regnum, const void *buf, int offset) const | |
1394 | { | |
1395 | gdb_assert (buf != NULL); | |
f868386e | 1396 | |
51e6b8cf SM |
1397 | gdb::array_view<const gdb_byte> regbuf = register_buffer (regnum); |
1398 | gdb_assert (offset <= regbuf.size ()); | |
1399 | regbuf = regbuf.slice (offset); | |
f868386e | 1400 | |
51e6b8cf | 1401 | return memcmp (buf, regbuf.data (), regbuf.size ()) == 0; |
f868386e | 1402 | } |
193cb69f | 1403 | |
515630c5 | 1404 | /* Special handling for register PC. */ |
32178cab MS |
1405 | |
1406 | CORE_ADDR | |
e4e20d45 | 1407 | regcache_read_pc (reg_buffer_common *reg_buf) |
32178cab | 1408 | { |
e4e20d45 | 1409 | regcache *regcache = gdb::checked_static_cast<struct regcache *> (reg_buf); |
ac7936df | 1410 | struct gdbarch *gdbarch = regcache->arch (); |
61a1198a | 1411 | |
32178cab MS |
1412 | CORE_ADDR pc_val; |
1413 | ||
61a1198a UW |
1414 | if (gdbarch_read_pc_p (gdbarch)) |
1415 | pc_val = gdbarch_read_pc (gdbarch, regcache); | |
cde9ea48 | 1416 | /* Else use per-frame method on get_current_frame. */ |
214e098a | 1417 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
cde9ea48 | 1418 | { |
61a1198a | 1419 | ULONGEST raw_val; |
123f5f96 | 1420 | |
05d1431c PA |
1421 | if (regcache_cooked_read_unsigned (regcache, |
1422 | gdbarch_pc_regnum (gdbarch), | |
1423 | &raw_val) == REG_UNAVAILABLE) | |
1424 | throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available")); | |
1425 | ||
214e098a | 1426 | pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val); |
cde9ea48 AC |
1427 | } |
1428 | else | |
f34652de | 1429 | internal_error (_("regcache_read_pc: Unable to find PC")); |
32178cab MS |
1430 | return pc_val; |
1431 | } | |
1432 | ||
fc75c28b TBA |
1433 | /* See gdbsupport/common-regcache.h. */ |
1434 | ||
1435 | CORE_ADDR | |
e4e20d45 | 1436 | regcache_read_pc_protected (reg_buffer_common *regcache) |
fc75c28b TBA |
1437 | { |
1438 | CORE_ADDR pc; | |
1439 | try | |
1440 | { | |
1441 | pc = regcache_read_pc (regcache); | |
1442 | } | |
1443 | catch (const gdb_exception_error &ex) | |
1444 | { | |
1445 | pc = 0; | |
1446 | } | |
1447 | ||
1448 | return pc; | |
1449 | } | |
1450 | ||
32178cab | 1451 | void |
515630c5 | 1452 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) |
32178cab | 1453 | { |
ac7936df | 1454 | struct gdbarch *gdbarch = regcache->arch (); |
61a1198a | 1455 | |
61a1198a UW |
1456 | if (gdbarch_write_pc_p (gdbarch)) |
1457 | gdbarch_write_pc (gdbarch, regcache, pc); | |
214e098a | 1458 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
3e8c568d | 1459 | regcache_cooked_write_unsigned (regcache, |
214e098a | 1460 | gdbarch_pc_regnum (gdbarch), pc); |
61a1198a | 1461 | else |
f34652de | 1462 | internal_error (_("regcache_write_pc: Unable to update PC")); |
edb3359d DJ |
1463 | |
1464 | /* Writing the PC (for instance, from "load") invalidates the | |
1465 | current frame. */ | |
1466 | reinit_frame_cache (); | |
32178cab MS |
1467 | } |
1468 | ||
d999647b | 1469 | int |
31716595 | 1470 | reg_buffer::num_raw_registers () const |
d999647b YQ |
1471 | { |
1472 | return gdbarch_num_regs (arch ()); | |
1473 | } | |
1474 | ||
8a8b6c53 SM |
1475 | std::string |
1476 | regcache::register_debug_string (int regno) | |
ed771251 | 1477 | { |
ef79d9a3 | 1478 | struct gdbarch *gdbarch = arch (); |
8a8b6c53 | 1479 | std::string s; |
ed771251 | 1480 | |
ed771251 | 1481 | if (regno >= 0 && regno < gdbarch_num_regs (gdbarch) |
ed771251 | 1482 | && gdbarch_register_name (gdbarch, regno)[0] != '\0') |
8a8b6c53 | 1483 | string_appendf (s, "register %s:", gdbarch_register_name (gdbarch, regno)); |
ed771251 | 1484 | else |
8a8b6c53 SM |
1485 | string_appendf (s, "register %d:", regno); |
1486 | ||
ed771251 AH |
1487 | if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)) |
1488 | { | |
51e6b8cf | 1489 | gdb::array_view<gdb_byte> buf = register_buffer (regno); |
ed771251 | 1490 | |
8a8b6c53 SM |
1491 | string_appendf (s, " = "); |
1492 | ||
51e6b8cf | 1493 | for (gdb_byte byte : buf) |
8a8b6c53 | 1494 | string_appendf (s, "%02x", byte); |
51e6b8cf SM |
1495 | |
1496 | if (buf.size () <= sizeof (LONGEST)) | |
ed771251 | 1497 | { |
8a8b6c53 SM |
1498 | ULONGEST val |
1499 | = extract_unsigned_integer (buf, gdbarch_byte_order (gdbarch)); | |
ed771251 | 1500 | |
8a8b6c53 SM |
1501 | string_appendf (s, " %s %s", |
1502 | core_addr_to_string_nz (val), plongest (val)); | |
ed771251 AH |
1503 | } |
1504 | } | |
8a8b6c53 SM |
1505 | |
1506 | return s; | |
ed771251 | 1507 | } |
32178cab | 1508 | |
50a5f187 AB |
1509 | /* Implement 'maint flush register-cache' command. */ |
1510 | ||
705152c5 | 1511 | static void |
0b39b52e | 1512 | reg_flush_command (const char *command, int from_tty) |
705152c5 MS |
1513 | { |
1514 | /* Force-flush the register cache. */ | |
1515 | registers_changed (); | |
1516 | if (from_tty) | |
6cb06a8c | 1517 | gdb_printf (_("Register cache flushed.\n")); |
705152c5 MS |
1518 | } |
1519 | ||
4c74fe6b | 1520 | void |
17ecffd7 | 1521 | register_dump::dump (ui_out *out, const char *name) |
af030b9a | 1522 | { |
4c74fe6b YQ |
1523 | auto descr = regcache_descr (m_gdbarch); |
1524 | int regnum; | |
1525 | int footnote_nr = 0; | |
1526 | int footnote_register_offset = 0; | |
1527 | int footnote_register_type_name_null = 0; | |
1528 | long register_offset = 0; | |
af030b9a | 1529 | |
4c74fe6b | 1530 | gdb_assert (descr->nr_cooked_registers |
f6efe3f8 | 1531 | == gdbarch_num_cooked_regs (m_gdbarch)); |
af030b9a | 1532 | |
17ecffd7 TT |
1533 | ui_out_emit_table table (out, 6 + num_additional_headers (), -1, name); |
1534 | out->table_header (10, ui_left, "name", "Name"); | |
1535 | out->table_header (4, ui_left, "num", "Nr"); | |
1536 | out->table_header (4, ui_left, "relnum", "Rel"); | |
1537 | out->table_header (8, ui_left, "offset", "Offset"); | |
1538 | out->table_header (5, ui_left, "size", "Size"); | |
1539 | out->table_header (15, ui_left, "type", "Type"); | |
1540 | additional_headers (out); | |
1541 | out->table_body (); | |
1542 | ||
1543 | for (regnum = 0; regnum < descr->nr_cooked_registers; regnum++) | |
4c74fe6b | 1544 | { |
17ecffd7 | 1545 | ui_out_emit_tuple tuple_emitter (out, nullptr); |
123f5f96 | 1546 | |
17ecffd7 TT |
1547 | /* Name. */ |
1548 | const char *p = gdbarch_register_name (m_gdbarch, regnum); | |
1549 | if (p[0] == '\0') | |
1550 | p = "''"; | |
1551 | out->field_string ("name", p); | |
af030b9a | 1552 | |
4c74fe6b | 1553 | /* Number. */ |
17ecffd7 | 1554 | out->field_signed ("num", regnum); |
af030b9a | 1555 | |
4c74fe6b | 1556 | /* Relative number. */ |
17ecffd7 TT |
1557 | if (regnum < gdbarch_num_regs (m_gdbarch)) |
1558 | out->field_signed ("relnum", regnum); | |
4c74fe6b | 1559 | else |
17ecffd7 | 1560 | out->field_signed ("relnum", (regnum - gdbarch_num_regs (m_gdbarch))); |
af030b9a | 1561 | |
4c74fe6b | 1562 | /* Offset. */ |
17ecffd7 TT |
1563 | if (register_offset != descr->register_offset[regnum] |
1564 | || (regnum > 0 | |
1565 | && (descr->register_offset[regnum] | |
1566 | != (descr->register_offset[regnum - 1] | |
1567 | + descr->sizeof_register[regnum - 1])))) | |
af030b9a | 1568 | { |
17ecffd7 TT |
1569 | if (!footnote_register_offset) |
1570 | footnote_register_offset = ++footnote_nr; | |
1571 | std::string val = string_printf ("%ld*%d", | |
1572 | descr->register_offset[regnum], | |
1573 | footnote_register_offset); | |
1574 | out->field_string ("offset", val); | |
af030b9a | 1575 | } |
17ecffd7 TT |
1576 | else |
1577 | out->field_signed ("offset", descr->register_offset[regnum]); | |
1578 | register_offset = (descr->register_offset[regnum] | |
1579 | + descr->sizeof_register[regnum]); | |
af030b9a | 1580 | |
4c74fe6b | 1581 | /* Size. */ |
17ecffd7 | 1582 | out->field_signed ("size", descr->sizeof_register[regnum]); |
f3384e66 | 1583 | |
4c74fe6b | 1584 | /* Type. */ |
f3384e66 | 1585 | { |
4c74fe6b YQ |
1586 | const char *t; |
1587 | std::string name_holder; | |
b59ff9d5 | 1588 | |
17ecffd7 TT |
1589 | static const char blt[] = "builtin_type"; |
1590 | ||
1591 | t = register_type (m_gdbarch, regnum)->name (); | |
1592 | if (t == NULL) | |
215c69dc | 1593 | { |
17ecffd7 TT |
1594 | if (!footnote_register_type_name_null) |
1595 | footnote_register_type_name_null = ++footnote_nr; | |
1596 | name_holder = string_printf ("*%d", | |
1597 | footnote_register_type_name_null); | |
1598 | t = name_holder.c_str (); | |
f3384e66 | 1599 | } |
17ecffd7 TT |
1600 | /* Chop a leading builtin_type. */ |
1601 | if (startswith (t, blt)) | |
1602 | t += strlen (blt); | |
f3384e66 | 1603 | |
17ecffd7 TT |
1604 | out->field_string ("type", t); |
1605 | } | |
af030b9a | 1606 | |
17ecffd7 | 1607 | dump_reg (out, regnum); |
ed4227b7 | 1608 | |
17ecffd7 | 1609 | out->text ("\n"); |
ed4227b7 PA |
1610 | } |
1611 | ||
4c74fe6b | 1612 | if (footnote_register_offset) |
17ecffd7 TT |
1613 | out->message ("*%d: Inconsistent register offsets.\n", |
1614 | footnote_register_offset); | |
4c74fe6b | 1615 | if (footnote_register_type_name_null) |
17ecffd7 TT |
1616 | out->message ("*%d: Register type's name NULL.\n", |
1617 | footnote_register_type_name_null); | |
c21236dc PA |
1618 | } |
1619 | ||
8248946c | 1620 | #if GDB_SELF_TEST |
268a13a5 | 1621 | #include "gdbsupport/selftest.h" |
1b30aaa5 | 1622 | #include "selftest-arch.h" |
ec7a5fcb | 1623 | #include "target-float.h" |
8248946c YQ |
1624 | |
1625 | namespace selftests { | |
1626 | ||
159ed7d9 SM |
1627 | static size_t |
1628 | regcaches_size () | |
8248946c | 1629 | { |
888bdb2b | 1630 | size_t size = 0; |
b70e516e SM |
1631 | |
1632 | for (auto pid_ptid_regc_map_it = regcaches.cbegin (); | |
1633 | pid_ptid_regc_map_it != regcaches.cend (); | |
1634 | ++pid_ptid_regc_map_it) | |
888bdb2b | 1635 | { |
b70e516e SM |
1636 | const pid_ptid_regcache_map &pid_ptid_regc_map |
1637 | = pid_ptid_regc_map_it->second; | |
1638 | ||
1639 | for (auto ptid_regc_map_it = pid_ptid_regc_map.cbegin (); | |
1640 | ptid_regc_map_it != pid_ptid_regc_map.cend (); | |
1641 | ++ptid_regc_map_it) | |
1642 | { | |
1643 | const ptid_regcache_map &ptid_regc_map | |
1644 | = ptid_regc_map_it->second; | |
1645 | ||
1646 | size += ptid_regc_map.size (); | |
1647 | } | |
888bdb2b SM |
1648 | } |
1649 | ||
1650 | return size; | |
159ed7d9 | 1651 | } |
8248946c | 1652 | |
cdd9148a SM |
1653 | /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */ |
1654 | ||
1655 | static int | |
1656 | regcache_count (process_stratum_target *target, ptid_t ptid) | |
1657 | { | |
b70e516e SM |
1658 | /* Look up map for target. */ |
1659 | auto pid_ptid_regc_map_it = regcaches.find (target); | |
1660 | if (pid_ptid_regc_map_it != regcaches.end ()) | |
cdd9148a | 1661 | { |
b70e516e | 1662 | pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second; |
cdd9148a | 1663 | |
b70e516e SM |
1664 | /* Look map for pid. */ |
1665 | auto ptid_regc_map_it = pid_ptid_regc_map.find (ptid.pid ()); | |
1666 | if (ptid_regc_map_it != pid_ptid_regc_map.end ()) | |
1667 | { | |
1668 | ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second; | |
1669 | auto range = ptid_regc_map.equal_range (ptid); | |
1670 | ||
1671 | return std::distance (range.first, range.second); | |
1672 | } | |
cdd9148a SM |
1673 | } |
1674 | ||
1675 | return 0; | |
1676 | }; | |
1677 | ||
74387712 | 1678 | /* Wrapper around get_thread_arch_regcache that does some self checks. */ |
5b6d1e4f PA |
1679 | |
1680 | static void | |
74387712 SM |
1681 | get_thread_arch_regcache_and_check (inferior *inf_for_target_calls, |
1682 | ptid_t ptid) | |
5b6d1e4f | 1683 | { |
dd125343 SM |
1684 | /* We currently only test with a single gdbarch. Any gdbarch will do, so use |
1685 | the current inferior's gdbarch. Also use the current inferior's address | |
1686 | space. */ | |
27b1f19f | 1687 | gdbarch *arch = inf_for_target_calls->arch (); |
74387712 SM |
1688 | regcache *regcache |
1689 | = get_thread_arch_regcache (inf_for_target_calls, ptid, arch); | |
dd125343 | 1690 | |
5b6d1e4f | 1691 | SELF_CHECK (regcache != NULL); |
5b6d1e4f | 1692 | SELF_CHECK (regcache->ptid () == ptid); |
dd125343 | 1693 | SELF_CHECK (regcache->arch () == arch); |
5b6d1e4f PA |
1694 | } |
1695 | ||
cdd9148a SM |
1696 | /* The data that the regcaches selftests must hold onto for the duration of the |
1697 | test. */ | |
1698 | ||
1699 | struct regcache_test_data | |
8248946c | 1700 | { |
cdd9148a | 1701 | regcache_test_data () |
98994c7a | 1702 | /* The specific arch doesn't matter. */ |
27b1f19f SM |
1703 | : test_ctx_1 (current_inferior ()->arch ()), |
1704 | test_ctx_2 (current_inferior ()->arch ()) | |
cdd9148a SM |
1705 | { |
1706 | /* Ensure the regcaches container is empty at the start. */ | |
1707 | registers_changed (); | |
1708 | } | |
8248946c | 1709 | |
cdd9148a SM |
1710 | ~regcache_test_data () |
1711 | { | |
1712 | /* Make sure to leave the global regcaches container empty. */ | |
1713 | registers_changed (); | |
1714 | } | |
8248946c | 1715 | |
98994c7a SM |
1716 | scoped_mock_context<test_target_ops> test_ctx_1; |
1717 | scoped_mock_context<test_target_ops> test_ctx_2; | |
cdd9148a SM |
1718 | }; |
1719 | ||
1720 | using regcache_test_data_up = std::unique_ptr<regcache_test_data>; | |
1721 | ||
1722 | /* Set up a few regcaches from two different targets, for use in | |
1723 | regcache-management tests. | |
1724 | ||
1725 | Return a pointer, because the `regcache_test_data` type is not moveable. */ | |
1726 | ||
1727 | static regcache_test_data_up | |
1728 | populate_regcaches_for_test () | |
1729 | { | |
1730 | regcache_test_data_up data (new regcache_test_data); | |
1731 | size_t expected_regcache_size = 0; | |
1732 | ||
1733 | SELF_CHECK (regcaches_size () == 0); | |
1734 | ||
1735 | /* Populate the regcache container with a few regcaches for the two test | |
1736 | targets. */ | |
1737 | for (int pid : { 1, 2 }) | |
1738 | { | |
1739 | for (long lwp : { 1, 2, 3 }) | |
1740 | { | |
74387712 | 1741 | get_thread_arch_regcache_and_check |
98994c7a | 1742 | (&data->test_ctx_1.mock_inferior, ptid_t (pid, lwp)); |
cdd9148a SM |
1743 | expected_regcache_size++; |
1744 | SELF_CHECK (regcaches_size () == expected_regcache_size); | |
1745 | ||
74387712 | 1746 | get_thread_arch_regcache_and_check |
98994c7a | 1747 | (&data->test_ctx_2.mock_inferior, ptid_t (pid, lwp)); |
cdd9148a SM |
1748 | expected_regcache_size++; |
1749 | SELF_CHECK (regcaches_size () == expected_regcache_size); | |
1750 | } | |
1751 | } | |
1752 | ||
1753 | return data; | |
1754 | } | |
1755 | ||
1756 | static void | |
74387712 | 1757 | get_thread_arch_regcache_test () |
cdd9148a SM |
1758 | { |
1759 | /* populate_regcaches_for_test already tests most of the | |
74387712 | 1760 | get_thread_arch_regcache functionality. */ |
cdd9148a SM |
1761 | regcache_test_data_up data = populate_regcaches_for_test (); |
1762 | size_t regcaches_size_before = regcaches_size (); | |
1763 | ||
1764 | /* Test that getting an existing regcache doesn't create a new one. */ | |
74387712 SM |
1765 | get_thread_arch_regcache_and_check (&data->test_ctx_1.mock_inferior, |
1766 | ptid_t (2, 2)); | |
cdd9148a SM |
1767 | SELF_CHECK (regcaches_size () == regcaches_size_before); |
1768 | } | |
1769 | ||
1770 | /* Test marking all regcaches of all targets as changed. */ | |
1771 | ||
1772 | static void | |
1773 | registers_changed_ptid_all_test () | |
1774 | { | |
1775 | regcache_test_data_up data = populate_regcaches_for_test (); | |
8248946c | 1776 | |
5b6d1e4f | 1777 | registers_changed_ptid (nullptr, minus_one_ptid); |
159ed7d9 | 1778 | SELF_CHECK (regcaches_size () == 0); |
cdd9148a | 1779 | } |
3ee93972 | 1780 | |
cdd9148a SM |
1781 | /* Test marking regcaches of a specific target as changed. */ |
1782 | ||
1783 | static void | |
1784 | registers_changed_ptid_target_test () | |
1785 | { | |
1786 | regcache_test_data_up data = populate_regcaches_for_test (); | |
1787 | ||
98994c7a | 1788 | registers_changed_ptid (&data->test_ctx_1.mock_target, minus_one_ptid); |
cdd9148a SM |
1789 | SELF_CHECK (regcaches_size () == 6); |
1790 | ||
1791 | /* Check that we deleted the regcache for the right target. */ | |
98994c7a SM |
1792 | SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target, |
1793 | ptid_t (2, 2)) == 0); | |
1794 | SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target, | |
1795 | ptid_t (2, 2)) == 1); | |
cdd9148a SM |
1796 | } |
1797 | ||
b70e516e SM |
1798 | /* Test marking regcaches of a specific (target, pid) as changed. */ |
1799 | ||
1800 | static void | |
1801 | registers_changed_ptid_target_pid_test () | |
1802 | { | |
1803 | regcache_test_data_up data = populate_regcaches_for_test (); | |
1804 | ||
98994c7a | 1805 | registers_changed_ptid (&data->test_ctx_1.mock_target, ptid_t (2)); |
b70e516e SM |
1806 | SELF_CHECK (regcaches_size () == 9); |
1807 | ||
1808 | /* Regcaches from target1 should not exist, while regcaches from target2 | |
1809 | should exist. */ | |
98994c7a SM |
1810 | SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target, |
1811 | ptid_t (2, 2)) == 0); | |
1812 | SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target, | |
1813 | ptid_t (2, 2)) == 1); | |
b70e516e SM |
1814 | } |
1815 | ||
cdd9148a SM |
1816 | /* Test marking regcaches of a specific (target, ptid) as changed. */ |
1817 | ||
1818 | static void | |
1819 | registers_changed_ptid_target_ptid_test () | |
1820 | { | |
1821 | regcache_test_data_up data = populate_regcaches_for_test (); | |
1822 | ||
98994c7a | 1823 | registers_changed_ptid (&data->test_ctx_1.mock_target, ptid_t (2, 2)); |
cdd9148a SM |
1824 | SELF_CHECK (regcaches_size () == 11); |
1825 | ||
1826 | /* Check that we deleted the regcache for the right target. */ | |
98994c7a SM |
1827 | SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target, |
1828 | ptid_t (2, 2)) == 0); | |
1829 | SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target, | |
1830 | ptid_t (2, 2)) == 1); | |
8248946c YQ |
1831 | } |
1832 | ||
51e6b8cf SM |
1833 | /* Test using reg_buffer::raw_compare with offset equal to the register size |
1834 | (thus comparing 0 bytes). */ | |
1835 | ||
1836 | static void | |
1837 | reg_buffer_raw_compare_zero_len_test () | |
1838 | { | |
1839 | regcache_test_data_up data = populate_regcaches_for_test (); | |
1840 | inferior &inf = data->test_ctx_1.mock_inferior; | |
1841 | const regcache *regcache | |
1842 | = get_thread_arch_regcache (&inf, ptid_t (1, 1), inf.arch ()); | |
1843 | ||
1844 | /* The buffer address is irrelevant since we end up comparing 0 bytes, we just | |
1845 | need to pass something. */ | |
1846 | gdb_byte buf; | |
1847 | SELF_CHECK (regcache->raw_compare (0, &buf, register_size (inf.arch (), 0))); | |
1848 | } | |
1849 | ||
1b30aaa5 YQ |
1850 | class target_ops_no_register : public test_target_ops |
1851 | { | |
1852 | public: | |
1853 | target_ops_no_register () | |
1854 | : test_target_ops {} | |
f6ac5f3d | 1855 | {} |
1b30aaa5 YQ |
1856 | |
1857 | void reset () | |
1858 | { | |
1859 | fetch_registers_called = 0; | |
1860 | store_registers_called = 0; | |
1861 | xfer_partial_called = 0; | |
1862 | } | |
1863 | ||
f6ac5f3d PA |
1864 | void fetch_registers (regcache *regs, int regno) override; |
1865 | void store_registers (regcache *regs, int regno) override; | |
1866 | ||
1867 | enum target_xfer_status xfer_partial (enum target_object object, | |
1868 | const char *annex, gdb_byte *readbuf, | |
1869 | const gdb_byte *writebuf, | |
1870 | ULONGEST offset, ULONGEST len, | |
1871 | ULONGEST *xfered_len) override; | |
1872 | ||
1b30aaa5 YQ |
1873 | unsigned int fetch_registers_called = 0; |
1874 | unsigned int store_registers_called = 0; | |
1875 | unsigned int xfer_partial_called = 0; | |
1876 | }; | |
1877 | ||
f6ac5f3d PA |
1878 | void |
1879 | target_ops_no_register::fetch_registers (regcache *regs, int regno) | |
1b30aaa5 | 1880 | { |
1b30aaa5 YQ |
1881 | /* Mark register available. */ |
1882 | regs->raw_supply_zeroed (regno); | |
f6ac5f3d | 1883 | this->fetch_registers_called++; |
1b30aaa5 YQ |
1884 | } |
1885 | ||
f6ac5f3d PA |
1886 | void |
1887 | target_ops_no_register::store_registers (regcache *regs, int regno) | |
1b30aaa5 | 1888 | { |
f6ac5f3d | 1889 | this->store_registers_called++; |
1b30aaa5 YQ |
1890 | } |
1891 | ||
f6ac5f3d PA |
1892 | enum target_xfer_status |
1893 | target_ops_no_register::xfer_partial (enum target_object object, | |
1894 | const char *annex, gdb_byte *readbuf, | |
1895 | const gdb_byte *writebuf, | |
1896 | ULONGEST offset, ULONGEST len, | |
1897 | ULONGEST *xfered_len) | |
1b30aaa5 | 1898 | { |
f6ac5f3d | 1899 | this->xfer_partial_called++; |
1b30aaa5 YQ |
1900 | |
1901 | *xfered_len = len; | |
1902 | return TARGET_XFER_OK; | |
1903 | } | |
1904 | ||
1905 | class readwrite_regcache : public regcache | |
1906 | { | |
1907 | public: | |
98994c7a | 1908 | readwrite_regcache (inferior *inf_for_target_calls, |
5b6d1e4f | 1909 | struct gdbarch *gdbarch) |
74387712 | 1910 | : regcache (inf_for_target_calls, gdbarch) |
1b30aaa5 YQ |
1911 | {} |
1912 | }; | |
1913 | ||
1914 | /* Test regcache::cooked_read gets registers from raw registers and | |
1915 | memory instead of target to_{fetch,store}_registers. */ | |
1916 | ||
1917 | static void | |
1918 | cooked_read_test (struct gdbarch *gdbarch) | |
1919 | { | |
79580fac | 1920 | if (selftest_skip_warning_arch (gdbarch)) |
c7220939 TV |
1921 | return; |
1922 | ||
236ef034 | 1923 | scoped_mock_context<target_ops_no_register> mockctx (gdbarch); |
1b30aaa5 YQ |
1924 | |
1925 | /* Test that read one raw register from regcache_no_target will go | |
1926 | to the target layer. */ | |
1b30aaa5 YQ |
1927 | |
1928 | /* Find a raw register which size isn't zero. */ | |
b926417a TT |
1929 | int nonzero_regnum; |
1930 | for (nonzero_regnum = 0; | |
1931 | nonzero_regnum < gdbarch_num_regs (gdbarch); | |
1932 | nonzero_regnum++) | |
1b30aaa5 | 1933 | { |
b926417a | 1934 | if (register_size (gdbarch, nonzero_regnum) != 0) |
1b30aaa5 YQ |
1935 | break; |
1936 | } | |
1937 | ||
b3245cef SM |
1938 | /* Install this regcache in the regcaches global structure, so that. */ |
1939 | pid_ptid_regcache_map &x = regcaches[&mockctx.mock_target]; | |
1940 | ptid_regcache_map &y = x[mockctx.mock_ptid.pid ()]; | |
1941 | regcache &readwrite | |
1942 | = *y.emplace (std::make_pair (mockctx.mock_ptid, | |
1943 | std::make_unique<readwrite_regcache> ( | |
1944 | &mockctx.mock_inferior, gdbarch))) | |
1945 | ->second; | |
1946 | ||
98994c7a | 1947 | readwrite.set_ptid (mockctx.mock_ptid); |
1b30aaa5 | 1948 | |
b3245cef | 1949 | gdb::byte_vector buf (register_size (gdbarch, nonzero_regnum)); |
51e6b8cf | 1950 | readwrite.raw_read (nonzero_regnum, buf); |
1b30aaa5 YQ |
1951 | |
1952 | /* raw_read calls target_fetch_registers. */ | |
236ef034 PA |
1953 | SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0); |
1954 | mockctx.mock_target.reset (); | |
1b30aaa5 YQ |
1955 | |
1956 | /* Mark all raw registers valid, so the following raw registers | |
1957 | accesses won't go to target. */ | |
1958 | for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++) | |
1959 | readwrite.raw_update (i); | |
1960 | ||
236ef034 | 1961 | mockctx.mock_target.reset (); |
1b30aaa5 YQ |
1962 | /* Then, read all raw and pseudo registers, and don't expect calling |
1963 | to_{fetch,store}_registers. */ | |
f6efe3f8 | 1964 | for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) |
1b30aaa5 YQ |
1965 | { |
1966 | if (register_size (gdbarch, regnum) == 0) | |
1967 | continue; | |
1968 | ||
aab2d3a6 | 1969 | gdb::byte_vector inner_buf (register_size (gdbarch, regnum)); |
1b30aaa5 | 1970 | |
51e6b8cf | 1971 | SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, inner_buf)); |
236ef034 PA |
1972 | SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0); |
1973 | SELF_CHECK (mockctx.mock_target.store_registers_called == 0); | |
1974 | SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0); | |
1b30aaa5 | 1975 | |
236ef034 | 1976 | mockctx.mock_target.reset (); |
1b30aaa5 | 1977 | } |
a63f2d2f | 1978 | |
215c69dc | 1979 | readonly_detached_regcache readonly (readwrite); |
a63f2d2f YQ |
1980 | |
1981 | /* GDB may go to target layer to fetch all registers and memory for | |
1982 | readonly regcache. */ | |
236ef034 | 1983 | mockctx.mock_target.reset (); |
a63f2d2f | 1984 | |
f6efe3f8 | 1985 | for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) |
a63f2d2f | 1986 | { |
a63f2d2f YQ |
1987 | if (register_size (gdbarch, regnum) == 0) |
1988 | continue; | |
1989 | ||
aab2d3a6 | 1990 | gdb::byte_vector inner_buf (register_size (gdbarch, regnum)); |
51e6b8cf | 1991 | register_status status = readonly.cooked_read (regnum, inner_buf); |
a63f2d2f YQ |
1992 | |
1993 | if (regnum < gdbarch_num_regs (gdbarch)) | |
1994 | { | |
1995 | auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; | |
1996 | ||
18b4d073 SM |
1997 | if (bfd_arch == bfd_arch_amdgcn |
1998 | || bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300 | |
a63f2d2f YQ |
1999 | || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh |
2000 | || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850 | |
2001 | || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep | |
2002 | || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850 | |
2003 | || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300 | |
ea005f31 | 2004 | || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score |
bea556ab | 2005 | || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky) |
a63f2d2f YQ |
2006 | { |
2007 | /* Raw registers. If raw registers are not in save_reggroup, | |
2008 | their status are unknown. */ | |
2009 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
2010 | SELF_CHECK (status == REG_VALID); | |
2011 | else | |
2012 | SELF_CHECK (status == REG_UNKNOWN); | |
2013 | } | |
2014 | else | |
2015 | SELF_CHECK (status == REG_VALID); | |
2016 | } | |
2017 | else | |
2018 | { | |
2019 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
2020 | SELF_CHECK (status == REG_VALID); | |
2021 | else | |
2022 | { | |
2023 | /* If pseudo registers are not in save_reggroup, some of | |
2024 | them can be computed from saved raw registers, but some | |
2025 | of them are unknown. */ | |
2026 | auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; | |
2027 | ||
2028 | if (bfd_arch == bfd_arch_frv | |
2029 | || bfd_arch == bfd_arch_m32c | |
2030 | || bfd_arch == bfd_arch_mep | |
2031 | || bfd_arch == bfd_arch_sh) | |
2032 | SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN); | |
2033 | else if (bfd_arch == bfd_arch_mips | |
2034 | || bfd_arch == bfd_arch_h8300) | |
2035 | SELF_CHECK (status == REG_UNKNOWN); | |
2036 | else | |
2037 | SELF_CHECK (status == REG_VALID); | |
2038 | } | |
2039 | } | |
2040 | ||
236ef034 PA |
2041 | SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0); |
2042 | SELF_CHECK (mockctx.mock_target.store_registers_called == 0); | |
2043 | SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0); | |
a63f2d2f | 2044 | |
236ef034 | 2045 | mockctx.mock_target.reset (); |
a63f2d2f | 2046 | } |
b3245cef SM |
2047 | |
2048 | regcaches.erase (&mockctx.mock_target); | |
1b30aaa5 YQ |
2049 | } |
2050 | ||
ec7a5fcb YQ |
2051 | /* Test regcache::cooked_write by writing some expected contents to |
2052 | registers, and checking that contents read from registers and the | |
2053 | expected contents are the same. */ | |
2054 | ||
2055 | static void | |
2056 | cooked_write_test (struct gdbarch *gdbarch) | |
2057 | { | |
79580fac | 2058 | if (selftest_skip_warning_arch (gdbarch)) |
c7220939 TV |
2059 | return; |
2060 | ||
ec7a5fcb | 2061 | /* Create a mock environment. A process_stratum target pushed. */ |
d890c720 | 2062 | scoped_mock_context<target_ops_no_register> ctx (gdbarch); |
b3245cef SM |
2063 | |
2064 | ||
2065 | /* Install this regcache in the regcaches global structure, so that. */ | |
2066 | pid_ptid_regcache_map &x = regcaches[&ctx.mock_target]; | |
2067 | ptid_regcache_map &y = x[ctx.mock_ptid.pid ()]; | |
2068 | regcache &readwrite | |
2069 | = *y.emplace (std::make_pair (ctx.mock_ptid, | |
2070 | std::make_unique<readwrite_regcache> ( | |
2071 | &ctx.mock_inferior, gdbarch))) | |
2072 | ->second; | |
2073 | ||
98994c7a | 2074 | readwrite.set_ptid (ctx.mock_ptid); |
f6efe3f8 | 2075 | const int num_regs = gdbarch_num_cooked_regs (gdbarch); |
ec7a5fcb YQ |
2076 | |
2077 | for (auto regnum = 0; regnum < num_regs; regnum++) | |
2078 | { | |
2079 | if (register_size (gdbarch, regnum) == 0 | |
2080 | || gdbarch_cannot_store_register (gdbarch, regnum)) | |
2081 | continue; | |
2082 | ||
2083 | auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; | |
2084 | ||
abf516c6 UW |
2085 | if (bfd_arch == bfd_arch_sparc |
2086 | /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM, | |
2087 | SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */ | |
2088 | && gdbarch_ptr_bit (gdbarch) == 64 | |
2089 | && (regnum >= gdbarch_num_regs (gdbarch) | |
2090 | && regnum <= gdbarch_num_regs (gdbarch) + 4)) | |
ec7a5fcb YQ |
2091 | continue; |
2092 | ||
51e6b8cf SM |
2093 | gdb::byte_vector expected (register_size (gdbarch, regnum), 0); |
2094 | gdb::byte_vector buf (register_size (gdbarch, regnum), 0); | |
ec7a5fcb YQ |
2095 | const auto type = register_type (gdbarch, regnum); |
2096 | ||
78134374 SM |
2097 | if (type->code () == TYPE_CODE_FLT |
2098 | || type->code () == TYPE_CODE_DECFLOAT) | |
ec7a5fcb YQ |
2099 | { |
2100 | /* Generate valid float format. */ | |
2101 | target_float_from_string (expected.data (), type, "1.25"); | |
2102 | } | |
78134374 SM |
2103 | else if (type->code () == TYPE_CODE_INT |
2104 | || type->code () == TYPE_CODE_ARRAY | |
2105 | || type->code () == TYPE_CODE_PTR | |
2106 | || type->code () == TYPE_CODE_UNION | |
2107 | || type->code () == TYPE_CODE_STRUCT) | |
ec7a5fcb YQ |
2108 | { |
2109 | if (bfd_arch == bfd_arch_ia64 | |
2110 | || (regnum >= gdbarch_num_regs (gdbarch) | |
2111 | && (bfd_arch == bfd_arch_xtensa | |
2112 | || bfd_arch == bfd_arch_bfin | |
2113 | || bfd_arch == bfd_arch_m32c | |
2114 | /* m68hc11 pseudo registers are in memory. */ | |
2115 | || bfd_arch == bfd_arch_m68hc11 | |
2116 | || bfd_arch == bfd_arch_m68hc12 | |
2117 | || bfd_arch == bfd_arch_s390)) | |
2118 | || (bfd_arch == bfd_arch_frv | |
2119 | /* FRV pseudo registers except iacc0. */ | |
2120 | && regnum > gdbarch_num_regs (gdbarch))) | |
2121 | { | |
2122 | /* Skip setting the expected values for some architecture | |
2123 | registers. */ | |
2124 | } | |
2125 | else if (bfd_arch == bfd_arch_rl78 && regnum == 40) | |
2126 | { | |
2127 | /* RL78_PC_REGNUM */ | |
2128 | for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++) | |
2129 | expected[j] = j; | |
2130 | } | |
2131 | else | |
2132 | { | |
2133 | for (auto j = 0; j < register_size (gdbarch, regnum); j++) | |
2134 | expected[j] = j; | |
2135 | } | |
2136 | } | |
78134374 | 2137 | else if (type->code () == TYPE_CODE_FLAGS) |
ec7a5fcb YQ |
2138 | { |
2139 | /* No idea how to test flags. */ | |
2140 | continue; | |
2141 | } | |
2142 | else | |
2143 | { | |
2144 | /* If we don't know how to create the expected value for the | |
2145 | this type, make it fail. */ | |
2146 | SELF_CHECK (0); | |
2147 | } | |
2148 | ||
51e6b8cf | 2149 | readwrite.cooked_write (regnum, expected); |
ec7a5fcb | 2150 | |
51e6b8cf | 2151 | SELF_CHECK (readwrite.cooked_read (regnum, buf) == REG_VALID); |
ec7a5fcb YQ |
2152 | SELF_CHECK (expected == buf); |
2153 | } | |
b3245cef SM |
2154 | |
2155 | regcaches.erase (&ctx.mock_target); | |
ec7a5fcb YQ |
2156 | } |
2157 | ||
b161a60d SM |
2158 | /* Verify that when two threads with the same ptid exist (from two different |
2159 | targets) and one of them changes ptid, we only update the appropriate | |
2160 | regcaches. */ | |
2161 | ||
2162 | static void | |
2163 | regcache_thread_ptid_changed () | |
2164 | { | |
2165 | /* This test relies on the global regcache list to initially be empty. */ | |
2166 | registers_changed (); | |
2167 | ||
2168 | /* Any arch will do. */ | |
27b1f19f | 2169 | gdbarch *arch = current_inferior ()->arch (); |
b161a60d SM |
2170 | |
2171 | /* Prepare two targets with one thread each, with the same ptid. */ | |
2172 | scoped_mock_context<test_target_ops> target1 (arch); | |
2173 | scoped_mock_context<test_target_ops> target2 (arch); | |
b161a60d SM |
2174 | |
2175 | ptid_t old_ptid (111, 222); | |
2176 | ptid_t new_ptid (111, 333); | |
2177 | ||
2178 | target1.mock_inferior.pid = old_ptid.pid (); | |
2179 | target1.mock_thread.ptid = old_ptid; | |
922cc93d SM |
2180 | target1.mock_inferior.ptid_thread_map.clear (); |
2181 | target1.mock_inferior.ptid_thread_map[old_ptid] = &target1.mock_thread; | |
2182 | ||
b161a60d SM |
2183 | target2.mock_inferior.pid = old_ptid.pid (); |
2184 | target2.mock_thread.ptid = old_ptid; | |
922cc93d SM |
2185 | target2.mock_inferior.ptid_thread_map.clear (); |
2186 | target2.mock_inferior.ptid_thread_map[old_ptid] = &target2.mock_thread; | |
b161a60d SM |
2187 | |
2188 | gdb_assert (regcaches.empty ()); | |
2189 | ||
2190 | /* Populate the regcaches container. */ | |
74387712 SM |
2191 | get_thread_arch_regcache (&target1.mock_inferior, old_ptid, arch); |
2192 | get_thread_arch_regcache (&target2.mock_inferior, old_ptid, arch); | |
b161a60d | 2193 | |
888bdb2b SM |
2194 | gdb_assert (regcaches.size () == 2); |
2195 | gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1); | |
2196 | gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0); | |
2197 | gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1); | |
2198 | gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0); | |
b161a60d SM |
2199 | |
2200 | thread_change_ptid (&target1.mock_target, old_ptid, new_ptid); | |
2201 | ||
888bdb2b SM |
2202 | gdb_assert (regcaches.size () == 2); |
2203 | gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0); | |
2204 | gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1); | |
2205 | gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1); | |
2206 | gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0); | |
b161a60d SM |
2207 | |
2208 | /* Leave the regcache list empty. */ | |
2209 | registers_changed (); | |
2210 | gdb_assert (regcaches.empty ()); | |
2211 | } | |
2212 | ||
b6fb76ec | 2213 | } /* namespace selftests */ |
8248946c YQ |
2214 | #endif /* GDB_SELF_TEST */ |
2215 | ||
5fe70629 | 2216 | INIT_GDB_FILE (regcache) |
32178cab | 2217 | { |
50a5f187 AB |
2218 | struct cmd_list_element *c; |
2219 | ||
c90e7d63 SM |
2220 | gdb::observers::target_changed.attach (regcache_observer_target_changed, |
2221 | "regcache"); | |
2222 | gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed, | |
2223 | "regcache"); | |
f4c5303c | 2224 | |
3947f654 SM |
2225 | cmd_list_element *maintenance_flush_register_cache_cmd |
2226 | = add_cmd ("register-cache", class_maintenance, reg_flush_command, | |
2227 | _("Force gdb to flush its register and frame cache."), | |
2228 | &maintenanceflushlist); | |
2229 | c = add_com_alias ("flushregs", maintenance_flush_register_cache_cmd, | |
50a5f187 AB |
2230 | class_maintenance, 0); |
2231 | deprecate_cmd (c, "maintenance flush register-cache"); | |
39f77062 | 2232 | |
8248946c | 2233 | #if GDB_SELF_TEST |
74387712 SM |
2234 | selftests::register_test ("get_thread_arch_regcache", |
2235 | selftests::get_thread_arch_regcache_test); | |
cdd9148a SM |
2236 | selftests::register_test ("registers_changed_ptid_all", |
2237 | selftests::registers_changed_ptid_all_test); | |
b70e516e | 2238 | selftests::register_test ("registers_changed_ptid_target", |
24b21115 | 2239 | selftests::registers_changed_ptid_target_test); |
b70e516e | 2240 | selftests::register_test ("registers_changed_ptid_target_pid", |
24b21115 | 2241 | selftests::registers_changed_ptid_target_pid_test); |
cdd9148a SM |
2242 | selftests::register_test ("registers_changed_ptid_target_ptid", |
2243 | selftests::registers_changed_ptid_target_ptid_test); | |
51e6b8cf SM |
2244 | selftests::register_test ("reg_buffer_raw_compare_zero_len", |
2245 | selftests::reg_buffer_raw_compare_zero_len_test); | |
1b30aaa5 YQ |
2246 | |
2247 | selftests::register_test_foreach_arch ("regcache::cooked_read_test", | |
2248 | selftests::cooked_read_test); | |
ec7a5fcb YQ |
2249 | selftests::register_test_foreach_arch ("regcache::cooked_write_test", |
2250 | selftests::cooked_write_test); | |
b161a60d SM |
2251 | selftests::register_test ("regcache_thread_ptid_changed", |
2252 | selftests::regcache_thread_ptid_changed); | |
8248946c | 2253 | #endif |
32178cab | 2254 | } |