1 /* Traditional frame unwind support, for GDB the GNU Debugger.
3 Copyright (C) 2003-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "trad-frame.h"
24 #include "frame-unwind.h"
29 struct trad_frame_cache
31 struct frame_info
*this_frame
;
33 trad_frame_saved_reg
*prev_regs
;
34 struct frame_id this_id
;
37 struct trad_frame_cache
*
38 trad_frame_cache_zalloc (struct frame_info
*this_frame
)
40 struct trad_frame_cache
*this_trad_cache
;
42 this_trad_cache
= FRAME_OBSTACK_ZALLOC (struct trad_frame_cache
);
43 this_trad_cache
->prev_regs
= trad_frame_alloc_saved_regs (this_frame
);
44 this_trad_cache
->this_frame
= this_frame
;
45 return this_trad_cache
;
48 /* See trad-frame.h. */
51 trad_frame_reset_saved_regs (struct gdbarch
*gdbarch
,
52 trad_frame_saved_reg
*regs
)
54 int numregs
= gdbarch_num_cooked_regs (gdbarch
);
56 for (int regnum
= 0; regnum
< numregs
; regnum
++)
57 regs
[regnum
].set_realreg (regnum
);
60 trad_frame_saved_reg
*
61 trad_frame_alloc_saved_regs (struct gdbarch
*gdbarch
)
63 gdb_static_assert (std::is_trivially_default_constructible
<trad_frame_saved_reg
>::value
);
65 int numregs
= gdbarch_num_cooked_regs (gdbarch
);
66 trad_frame_saved_reg
*this_saved_regs
67 = FRAME_OBSTACK_CALLOC (numregs
, trad_frame_saved_reg
);
69 trad_frame_reset_saved_regs (gdbarch
, this_saved_regs
);
70 return this_saved_regs
;
73 /* A traditional frame is unwound by analysing the function prologue
74 and using the information gathered to track registers. For
75 non-optimized frames, the technique is reliable (just need to check
76 for all potential instruction sequences). */
78 trad_frame_saved_reg
*
79 trad_frame_alloc_saved_regs (struct frame_info
*this_frame
)
81 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
83 return trad_frame_alloc_saved_regs (gdbarch
);
87 trad_frame_value_p (trad_frame_saved_reg this_saved_regs
[], int regnum
)
89 return this_saved_regs
[regnum
].is_value ();
93 trad_frame_addr_p (trad_frame_saved_reg this_saved_regs
[], int regnum
)
95 return this_saved_regs
[regnum
].is_addr ();
99 trad_frame_realreg_p (trad_frame_saved_reg this_saved_regs
[],
102 return this_saved_regs
[regnum
].is_realreg ();
105 /* See trad-frame.h. */
108 trad_frame_value_bytes_p (trad_frame_saved_reg this_saved_regs
[],
111 return this_saved_regs
[regnum
].is_value_bytes ();
115 trad_frame_set_value (trad_frame_saved_reg this_saved_regs
[],
116 int regnum
, LONGEST val
)
118 this_saved_regs
[regnum
].set_value (val
);
121 /* See trad-frame.h. */
124 trad_frame_set_realreg (trad_frame_saved_reg this_saved_regs
[],
125 int regnum
, int realreg
)
127 this_saved_regs
[regnum
].set_realreg (realreg
);
130 /* See trad-frame.h. */
133 trad_frame_set_addr (trad_frame_saved_reg this_saved_regs
[],
134 int regnum
, CORE_ADDR addr
)
136 this_saved_regs
[regnum
].set_addr (addr
);
140 trad_frame_set_reg_value (struct trad_frame_cache
*this_trad_cache
,
141 int regnum
, LONGEST val
)
143 /* External interface for users of trad_frame_cache
144 (who cannot access the prev_regs object directly). */
145 trad_frame_set_value (this_trad_cache
->prev_regs
, regnum
, val
);
149 trad_frame_set_reg_realreg (struct trad_frame_cache
*this_trad_cache
,
150 int regnum
, int realreg
)
152 trad_frame_set_realreg (this_trad_cache
->prev_regs
, regnum
, realreg
);
156 trad_frame_set_reg_addr (struct trad_frame_cache
*this_trad_cache
,
157 int regnum
, CORE_ADDR addr
)
159 trad_frame_set_addr (this_trad_cache
->prev_regs
, regnum
, addr
);
163 trad_frame_set_reg_regmap (struct trad_frame_cache
*this_trad_cache
,
164 const struct regcache_map_entry
*regmap
,
165 CORE_ADDR addr
, size_t size
)
167 struct gdbarch
*gdbarch
= get_frame_arch (this_trad_cache
->this_frame
);
170 for (; (count
= regmap
->count
) != 0; regmap
++)
172 int regno
= regmap
->regno
;
173 int slot_size
= regmap
->size
;
175 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
176 slot_size
= register_size (gdbarch
, regno
);
178 if (offs
+ slot_size
> size
)
181 if (regno
== REGCACHE_MAP_SKIP
)
182 offs
+= count
* slot_size
;
184 for (; count
--; regno
++, offs
+= slot_size
)
186 /* Mimic the semantics of regcache::transfer_regset if a
187 register slot's size does not match the size of a
190 If a register slot is larger than a register, assume
191 the register's value is stored in the first N bytes of
192 the slot and ignore the remaining bytes.
194 If the register slot is smaller than the register,
195 assume that the slot contains the low N bytes of the
196 register's value. Since trad_frame assumes that
197 registers stored by address are sized according to the
198 register, read the low N bytes and zero-extend them to
199 generate a register value. */
200 if (slot_size
>= register_size (gdbarch
, regno
))
201 trad_frame_set_reg_addr (this_trad_cache
, regno
, addr
+ offs
);
204 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
205 gdb_byte buf
[slot_size
];
207 if (target_read_memory (addr
+ offs
, buf
, sizeof buf
) == 0)
210 = extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
211 trad_frame_set_reg_value (this_trad_cache
, regno
, val
);
219 trad_frame_set_unknown (trad_frame_saved_reg this_saved_regs
[],
222 this_saved_regs
[regnum
].set_unknown ();
225 /* See trad-frame.h. */
228 trad_frame_set_value_bytes (trad_frame_saved_reg this_saved_regs
[],
229 int regnum
, const gdb_byte
*bytes
,
232 /* Allocate the space and copy the data bytes. */
233 gdb_byte
*data
= FRAME_OBSTACK_CALLOC (size
, gdb_byte
);
234 memcpy (data
, bytes
, size
);
235 this_saved_regs
[regnum
].set_value_bytes (data
);
238 /* See trad-frame.h. */
241 trad_frame_set_reg_value_bytes (struct trad_frame_cache
*this_trad_cache
,
242 int regnum
, const gdb_byte
*bytes
,
245 /* External interface for users of trad_frame_cache
246 (who cannot access the prev_regs object directly). */
247 trad_frame_set_value_bytes (this_trad_cache
->prev_regs
, regnum
, bytes
,
254 trad_frame_get_prev_register (struct frame_info
*this_frame
,
255 trad_frame_saved_reg this_saved_regs
[],
258 if (trad_frame_addr_p (this_saved_regs
, regnum
))
259 /* The register was saved in memory. */
260 return frame_unwind_got_memory (this_frame
, regnum
,
261 this_saved_regs
[regnum
].addr ());
262 else if (trad_frame_realreg_p (this_saved_regs
, regnum
))
263 return frame_unwind_got_register (this_frame
, regnum
,
264 this_saved_regs
[regnum
].realreg ());
265 else if (trad_frame_value_p (this_saved_regs
, regnum
))
266 /* The register's value is available. */
267 return frame_unwind_got_constant (this_frame
, regnum
,
268 this_saved_regs
[regnum
].value ());
269 else if (trad_frame_value_bytes_p (this_saved_regs
, regnum
))
270 /* The register's value is available as a sequence of bytes. */
271 return frame_unwind_got_bytes (this_frame
, regnum
,
272 this_saved_regs
[regnum
].value_bytes ());
274 return frame_unwind_got_optimized (this_frame
, regnum
);
278 trad_frame_get_register (struct trad_frame_cache
*this_trad_cache
,
279 struct frame_info
*this_frame
,
282 return trad_frame_get_prev_register (this_frame
, this_trad_cache
->prev_regs
,
287 trad_frame_set_id (struct trad_frame_cache
*this_trad_cache
,
288 struct frame_id this_id
)
290 this_trad_cache
->this_id
= this_id
;
294 trad_frame_get_id (struct trad_frame_cache
*this_trad_cache
,
295 struct frame_id
*this_id
)
297 (*this_id
) = this_trad_cache
->this_id
;
301 trad_frame_set_this_base (struct trad_frame_cache
*this_trad_cache
,
304 this_trad_cache
->this_base
= this_base
;
308 trad_frame_get_this_base (struct trad_frame_cache
*this_trad_cache
)
310 return this_trad_cache
->this_base
;