Fix building gdb with gcc-4.x
[deliverable/binutils-gdb.git] / gdb / trad-frame.c
1 /* Traditional frame unwind support, for GDB the GNU Debugger.
2
3 Copyright (C) 2003-2021 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "trad-frame.h"
23 #include "regcache.h"
24 #include "frame-unwind.h"
25 #include "target.h"
26 #include "value.h"
27 #include "gdbarch.h"
28 #include "gdbsupport/traits.h"
29
30 struct trad_frame_cache
31 {
32 struct frame_info *this_frame;
33 CORE_ADDR this_base;
34 trad_frame_saved_reg *prev_regs;
35 struct frame_id this_id;
36 };
37
38 struct trad_frame_cache *
39 trad_frame_cache_zalloc (struct frame_info *this_frame)
40 {
41 struct trad_frame_cache *this_trad_cache;
42
43 this_trad_cache = FRAME_OBSTACK_ZALLOC (struct trad_frame_cache);
44 this_trad_cache->prev_regs = trad_frame_alloc_saved_regs (this_frame);
45 this_trad_cache->this_frame = this_frame;
46 return this_trad_cache;
47 }
48
49 /* See trad-frame.h. */
50
51 void
52 trad_frame_reset_saved_regs (struct gdbarch *gdbarch,
53 trad_frame_saved_reg *regs)
54 {
55 int numregs = gdbarch_num_cooked_regs (gdbarch);
56
57 for (int regnum = 0; regnum < numregs; regnum++)
58 regs[regnum].set_realreg (regnum);
59 }
60
61 trad_frame_saved_reg *
62 trad_frame_alloc_saved_regs (struct gdbarch *gdbarch)
63 {
64 #ifdef HAVE_IS_TRIVIALLY_CONSTRUCTIBLE
65 gdb_static_assert (std::is_trivially_constructible<trad_frame_saved_reg>::value);
66 #endif
67
68 int numregs = gdbarch_num_cooked_regs (gdbarch);
69 trad_frame_saved_reg *this_saved_regs
70 = FRAME_OBSTACK_CALLOC (numregs, trad_frame_saved_reg);
71
72 trad_frame_reset_saved_regs (gdbarch, this_saved_regs);
73 return this_saved_regs;
74 }
75
76 /* A traditional frame is unwound by analysing the function prologue
77 and using the information gathered to track registers. For
78 non-optimized frames, the technique is reliable (just need to check
79 for all potential instruction sequences). */
80
81 trad_frame_saved_reg *
82 trad_frame_alloc_saved_regs (struct frame_info *this_frame)
83 {
84 struct gdbarch *gdbarch = get_frame_arch (this_frame);
85
86 return trad_frame_alloc_saved_regs (gdbarch);
87 }
88
89 int
90 trad_frame_value_p (trad_frame_saved_reg this_saved_regs[], int regnum)
91 {
92 return this_saved_regs[regnum].is_value ();
93 }
94
95 int
96 trad_frame_addr_p (trad_frame_saved_reg this_saved_regs[], int regnum)
97 {
98 return this_saved_regs[regnum].is_addr ();
99 }
100
101 int
102 trad_frame_realreg_p (trad_frame_saved_reg this_saved_regs[],
103 int regnum)
104 {
105 return this_saved_regs[regnum].is_realreg ();
106 }
107
108 /* See trad-frame.h. */
109
110 bool
111 trad_frame_value_bytes_p (trad_frame_saved_reg this_saved_regs[],
112 int regnum)
113 {
114 return this_saved_regs[regnum].is_value_bytes ();
115 }
116
117 void
118 trad_frame_set_value (trad_frame_saved_reg this_saved_regs[],
119 int regnum, LONGEST val)
120 {
121 this_saved_regs[regnum].set_value (val);
122 }
123
124 /* See trad-frame.h. */
125
126 void
127 trad_frame_set_realreg (trad_frame_saved_reg this_saved_regs[],
128 int regnum, int realreg)
129 {
130 this_saved_regs[regnum].set_realreg (realreg);
131 }
132
133 /* See trad-frame.h. */
134
135 void
136 trad_frame_set_addr (trad_frame_saved_reg this_saved_regs[],
137 int regnum, CORE_ADDR addr)
138 {
139 this_saved_regs[regnum].set_addr (addr);
140 }
141
142 void
143 trad_frame_set_reg_value (struct trad_frame_cache *this_trad_cache,
144 int regnum, LONGEST val)
145 {
146 /* External interface for users of trad_frame_cache
147 (who cannot access the prev_regs object directly). */
148 trad_frame_set_value (this_trad_cache->prev_regs, regnum, val);
149 }
150
151 void
152 trad_frame_set_reg_realreg (struct trad_frame_cache *this_trad_cache,
153 int regnum, int realreg)
154 {
155 trad_frame_set_realreg (this_trad_cache->prev_regs, regnum, realreg);
156 }
157
158 void
159 trad_frame_set_reg_addr (struct trad_frame_cache *this_trad_cache,
160 int regnum, CORE_ADDR addr)
161 {
162 trad_frame_set_addr (this_trad_cache->prev_regs, regnum, addr);
163 }
164
165 void
166 trad_frame_set_reg_regmap (struct trad_frame_cache *this_trad_cache,
167 const struct regcache_map_entry *regmap,
168 CORE_ADDR addr, size_t size)
169 {
170 struct gdbarch *gdbarch = get_frame_arch (this_trad_cache->this_frame);
171 int offs = 0, count;
172
173 for (; (count = regmap->count) != 0; regmap++)
174 {
175 int regno = regmap->regno;
176 int slot_size = regmap->size;
177
178 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
179 slot_size = register_size (gdbarch, regno);
180
181 if (offs + slot_size > size)
182 break;
183
184 if (regno == REGCACHE_MAP_SKIP)
185 offs += count * slot_size;
186 else
187 for (; count--; regno++, offs += slot_size)
188 {
189 /* Mimic the semantics of regcache::transfer_regset if a
190 register slot's size does not match the size of a
191 register.
192
193 If a register slot is larger than a register, assume
194 the register's value is stored in the first N bytes of
195 the slot and ignore the remaining bytes.
196
197 If the register slot is smaller than the register,
198 assume that the slot contains the low N bytes of the
199 register's value. Since trad_frame assumes that
200 registers stored by address are sized according to the
201 register, read the low N bytes and zero-extend them to
202 generate a register value. */
203 if (slot_size >= register_size (gdbarch, regno))
204 trad_frame_set_reg_addr (this_trad_cache, regno, addr + offs);
205 else
206 {
207 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
208 gdb_byte buf[slot_size];
209
210 if (target_read_memory (addr + offs, buf, sizeof buf) == 0)
211 {
212 LONGEST val
213 = extract_unsigned_integer (buf, sizeof buf, byte_order);
214 trad_frame_set_reg_value (this_trad_cache, regno, val);
215 }
216 }
217 }
218 }
219 }
220
221 void
222 trad_frame_set_unknown (trad_frame_saved_reg this_saved_regs[],
223 int regnum)
224 {
225 this_saved_regs[regnum].set_unknown ();
226 }
227
228 /* See trad-frame.h. */
229
230 void
231 trad_frame_set_value_bytes (trad_frame_saved_reg this_saved_regs[],
232 int regnum, const gdb_byte *bytes,
233 size_t size)
234 {
235 /* Allocate the space and copy the data bytes. */
236 gdb_byte *data = FRAME_OBSTACK_CALLOC (size, gdb_byte);
237 memcpy (data, bytes, size);
238 this_saved_regs[regnum].set_value_bytes (data);
239 }
240
241 /* See trad-frame.h. */
242
243 void
244 trad_frame_set_reg_value_bytes (struct trad_frame_cache *this_trad_cache,
245 int regnum, const gdb_byte *bytes,
246 size_t size)
247 {
248 /* External interface for users of trad_frame_cache
249 (who cannot access the prev_regs object directly). */
250 trad_frame_set_value_bytes (this_trad_cache->prev_regs, regnum, bytes,
251 size);
252 }
253
254
255
256 struct value *
257 trad_frame_get_prev_register (struct frame_info *this_frame,
258 trad_frame_saved_reg this_saved_regs[],
259 int regnum)
260 {
261 if (trad_frame_addr_p (this_saved_regs, regnum))
262 /* The register was saved in memory. */
263 return frame_unwind_got_memory (this_frame, regnum,
264 this_saved_regs[regnum].addr ());
265 else if (trad_frame_realreg_p (this_saved_regs, regnum))
266 return frame_unwind_got_register (this_frame, regnum,
267 this_saved_regs[regnum].realreg ());
268 else if (trad_frame_value_p (this_saved_regs, regnum))
269 /* The register's value is available. */
270 return frame_unwind_got_constant (this_frame, regnum,
271 this_saved_regs[regnum].value ());
272 else if (trad_frame_value_bytes_p (this_saved_regs, regnum))
273 /* The register's value is available as a sequence of bytes. */
274 return frame_unwind_got_bytes (this_frame, regnum,
275 this_saved_regs[regnum].value_bytes ());
276 else
277 return frame_unwind_got_optimized (this_frame, regnum);
278 }
279
280 struct value *
281 trad_frame_get_register (struct trad_frame_cache *this_trad_cache,
282 struct frame_info *this_frame,
283 int regnum)
284 {
285 return trad_frame_get_prev_register (this_frame, this_trad_cache->prev_regs,
286 regnum);
287 }
288
289 void
290 trad_frame_set_id (struct trad_frame_cache *this_trad_cache,
291 struct frame_id this_id)
292 {
293 this_trad_cache->this_id = this_id;
294 }
295
296 void
297 trad_frame_get_id (struct trad_frame_cache *this_trad_cache,
298 struct frame_id *this_id)
299 {
300 (*this_id) = this_trad_cache->this_id;
301 }
302
303 void
304 trad_frame_set_this_base (struct trad_frame_cache *this_trad_cache,
305 CORE_ADDR this_base)
306 {
307 this_trad_cache->this_base = this_base;
308 }
309
310 CORE_ADDR
311 trad_frame_get_this_base (struct trad_frame_cache *this_trad_cache)
312 {
313 return this_trad_cache->this_base;
314 }
This page took 0.037866 seconds and 5 git commands to generate.