ld TDIRS substitution
[external/binutils.git] / gdb / trad-frame.c
1 /* Traditional frame unwind support, for GDB the GNU Debugger.
2
3    Copyright (C) 2003-2019 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
28 struct trad_frame_cache
29 {
30   struct frame_info *this_frame;
31   CORE_ADDR this_base;
32   struct trad_frame_saved_reg *prev_regs;
33   struct frame_id this_id;
34 };
35
36 struct trad_frame_cache *
37 trad_frame_cache_zalloc (struct frame_info *this_frame)
38 {
39   struct trad_frame_cache *this_trad_cache;
40
41   this_trad_cache = FRAME_OBSTACK_ZALLOC (struct trad_frame_cache);
42   this_trad_cache->prev_regs = trad_frame_alloc_saved_regs (this_frame);
43   this_trad_cache->this_frame = this_frame;
44   return this_trad_cache;
45 }
46
47 /* See trad-frame.h.  */
48
49 void
50 trad_frame_reset_saved_regs (struct gdbarch *gdbarch,
51                              struct trad_frame_saved_reg *regs)
52 {
53   int numregs = gdbarch_num_cooked_regs (gdbarch);
54   for (int regnum = 0; regnum < numregs; regnum++)
55     {
56       regs[regnum].realreg = regnum;
57       regs[regnum].addr = -1;
58     }
59 }
60
61 struct trad_frame_saved_reg *
62 trad_frame_alloc_saved_regs (struct gdbarch *gdbarch)
63 {
64   int numregs = gdbarch_num_cooked_regs (gdbarch);
65   struct trad_frame_saved_reg *this_saved_regs
66     = FRAME_OBSTACK_CALLOC (numregs, struct trad_frame_saved_reg);
67
68   trad_frame_reset_saved_regs (gdbarch, this_saved_regs);
69   return this_saved_regs;
70 }
71
72 /* A traditional frame is unwound by analysing the function prologue
73    and using the information gathered to track registers.  For
74    non-optimized frames, the technique is reliable (just need to check
75    for all potential instruction sequences).  */
76
77 struct trad_frame_saved_reg *
78 trad_frame_alloc_saved_regs (struct frame_info *this_frame)
79 {
80   struct gdbarch *gdbarch = get_frame_arch (this_frame);
81
82   return trad_frame_alloc_saved_regs (gdbarch);
83 }
84
85 enum { TF_REG_VALUE = -1, TF_REG_UNKNOWN = -2 };
86
87 int
88 trad_frame_value_p (struct trad_frame_saved_reg this_saved_regs[], int regnum)
89 {
90   return (this_saved_regs[regnum].realreg == TF_REG_VALUE);
91 }
92
93 int
94 trad_frame_addr_p (struct trad_frame_saved_reg this_saved_regs[], int regnum)
95 {
96   return (this_saved_regs[regnum].realreg >= 0
97           && this_saved_regs[regnum].addr != -1);
98 }
99
100 int
101 trad_frame_realreg_p (struct trad_frame_saved_reg this_saved_regs[],
102                       int regnum)
103 {
104   return (this_saved_regs[regnum].realreg >= 0
105           && this_saved_regs[regnum].addr == -1);
106 }
107
108 void
109 trad_frame_set_value (struct trad_frame_saved_reg this_saved_regs[],
110                       int regnum, LONGEST val)
111 {
112   /* Make the REALREG invalid, indicating that the ADDR contains the
113      register's value.  */
114   this_saved_regs[regnum].realreg = TF_REG_VALUE;
115   this_saved_regs[regnum].addr = val;
116 }
117
118 /* See trad-frame.h.  */
119
120 void
121 trad_frame_set_realreg (struct trad_frame_saved_reg this_saved_regs[],
122                         int regnum, int realreg)
123 {
124   this_saved_regs[regnum].realreg = realreg;
125   this_saved_regs[regnum].addr = -1;
126 }
127
128 /* See trad-frame.h.  */
129
130 void
131 trad_frame_set_addr (struct trad_frame_saved_reg this_saved_regs[],
132                      int regnum, CORE_ADDR addr)
133 {
134   this_saved_regs[regnum].realreg = regnum;
135   this_saved_regs[regnum].addr = addr;
136 }
137
138 void
139 trad_frame_set_reg_value (struct trad_frame_cache *this_trad_cache,
140                           int regnum, LONGEST val)
141 {
142   /* External interface for users of trad_frame_cache
143      (who cannot access the prev_regs object directly).  */
144   trad_frame_set_value (this_trad_cache->prev_regs, regnum, val);
145 }
146
147 void
148 trad_frame_set_reg_realreg (struct trad_frame_cache *this_trad_cache,
149                             int regnum, int realreg)
150 {
151   trad_frame_set_realreg (this_trad_cache->prev_regs, regnum, realreg);
152 }
153
154 void
155 trad_frame_set_reg_addr (struct trad_frame_cache *this_trad_cache,
156                          int regnum, CORE_ADDR addr)
157 {
158   trad_frame_set_addr (this_trad_cache->prev_regs, regnum, addr);
159 }
160
161 void
162 trad_frame_set_reg_regmap (struct trad_frame_cache *this_trad_cache,
163                            const struct regcache_map_entry *regmap,
164                            CORE_ADDR addr, size_t size)
165 {
166   struct gdbarch *gdbarch = get_frame_arch (this_trad_cache->this_frame);
167   int offs = 0, count;
168
169   for (; (count = regmap->count) != 0; regmap++)
170     {
171       int regno = regmap->regno;
172       int slot_size = regmap->size;
173
174       if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
175         slot_size = register_size (gdbarch, regno);
176
177       if (offs + slot_size > size)
178         break;
179
180       if (regno == REGCACHE_MAP_SKIP)
181         offs += count * slot_size;
182       else
183         for (; count--; regno++, offs += slot_size)
184           {
185             /* Mimic the semantics of regcache::transfer_regset if a
186                register slot's size does not match the size of a
187                register.
188
189                If a register slot is larger than a register, assume
190                the register's value is stored in the first N bytes of
191                the slot and ignore the remaining bytes.
192
193                If the register slot is smaller than the register,
194                assume that the slot contains the low N bytes of the
195                register's value.  Since trad_frame assumes that
196                registers stored by address are sized according to the
197                register, read the low N bytes and zero-extend them to
198                generate a register value.  */
199             if (slot_size >= register_size (gdbarch, regno))
200               trad_frame_set_reg_addr (this_trad_cache, regno, addr + offs);
201             else
202               {
203                 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
204                 gdb_byte buf[slot_size];
205
206                 if (target_read_memory (addr + offs, buf, sizeof buf) == 0)
207                   {
208                     LONGEST val
209                       = extract_unsigned_integer (buf, sizeof buf, byte_order);
210                     trad_frame_set_reg_value (this_trad_cache, regno, val);
211                   }
212               }
213           }
214     }
215 }
216
217 void
218 trad_frame_set_unknown (struct trad_frame_saved_reg this_saved_regs[],
219                         int regnum)
220 {
221   /* Make the REALREG invalid, indicating that the value is not known.  */
222   this_saved_regs[regnum].realreg = TF_REG_UNKNOWN;
223   this_saved_regs[regnum].addr = -1;
224 }
225
226 struct value *
227 trad_frame_get_prev_register (struct frame_info *this_frame,
228                               struct trad_frame_saved_reg this_saved_regs[],
229                               int regnum)
230 {
231   if (trad_frame_addr_p (this_saved_regs, regnum))
232     /* The register was saved in memory.  */
233     return frame_unwind_got_memory (this_frame, regnum,
234                                     this_saved_regs[regnum].addr);
235   else if (trad_frame_realreg_p (this_saved_regs, regnum))
236     return frame_unwind_got_register (this_frame, regnum,
237                                       this_saved_regs[regnum].realreg);
238   else if (trad_frame_value_p (this_saved_regs, regnum))
239     /* The register's value is available.  */
240     return frame_unwind_got_constant (this_frame, regnum,
241                                       this_saved_regs[regnum].addr);
242   else
243     return frame_unwind_got_optimized (this_frame, regnum);
244 }
245
246 struct value *
247 trad_frame_get_register (struct trad_frame_cache *this_trad_cache,
248                          struct frame_info *this_frame,
249                          int regnum)
250 {
251   return trad_frame_get_prev_register (this_frame, this_trad_cache->prev_regs,
252                                        regnum);
253 }
254
255 void
256 trad_frame_set_id (struct trad_frame_cache *this_trad_cache,
257                    struct frame_id this_id)
258 {
259   this_trad_cache->this_id = this_id;
260 }
261
262 void
263 trad_frame_get_id (struct trad_frame_cache *this_trad_cache,
264                    struct frame_id *this_id)
265 {
266   (*this_id) = this_trad_cache->this_id;
267 }
268
269 void
270 trad_frame_set_this_base (struct trad_frame_cache *this_trad_cache,
271                           CORE_ADDR this_base)
272 {
273   this_trad_cache->this_base = this_base;
274 }
275
276 CORE_ADDR
277 trad_frame_get_this_base (struct trad_frame_cache *this_trad_cache)
278 {
279   return this_trad_cache->this_base;
280 }