Update
[gdb.git] / gdb / trad-frame.c
blob0a27906a19616ea8ff3af1f7823617eefbf264ac
1 /* Traditional frame unwind support, for GDB the GNU Debugger.
3 Copyright (C) 2003, 2004, 2007, 2008 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/>. */
20 #include "defs.h"
21 #include "frame.h"
22 #include "trad-frame.h"
23 #include "regcache.h"
25 struct trad_frame_cache
27 struct frame_info *next_frame;
28 CORE_ADDR this_base;
29 struct trad_frame_saved_reg *prev_regs;
30 struct frame_id this_id;
33 struct trad_frame_cache *
34 trad_frame_cache_zalloc (struct frame_info *next_frame)
36 struct trad_frame_cache *this_trad_cache;
38 this_trad_cache = FRAME_OBSTACK_ZALLOC (struct trad_frame_cache);
39 this_trad_cache->prev_regs = trad_frame_alloc_saved_regs (next_frame);
40 this_trad_cache->next_frame = next_frame;
41 return this_trad_cache;
44 /* A traditional frame is unwound by analysing the function prologue
45 and using the information gathered to track registers. For
46 non-optimized frames, the technique is reliable (just need to check
47 for all potential instruction sequences). */
49 struct trad_frame_saved_reg *
50 trad_frame_alloc_saved_regs (struct frame_info *next_frame)
52 int regnum;
53 struct gdbarch *gdbarch = get_frame_arch (next_frame);
54 int numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
55 struct trad_frame_saved_reg *this_saved_regs
56 = FRAME_OBSTACK_CALLOC (numregs, struct trad_frame_saved_reg);
57 for (regnum = 0; regnum < numregs; regnum++)
59 this_saved_regs[regnum].realreg = regnum;
60 this_saved_regs[regnum].addr = -1;
62 return this_saved_regs;
65 enum { REG_VALUE = -1, REG_UNKNOWN = -2 };
67 int
68 trad_frame_value_p (struct trad_frame_saved_reg this_saved_regs[], int regnum)
70 return (this_saved_regs[regnum].realreg == REG_VALUE);
73 int
74 trad_frame_addr_p (struct trad_frame_saved_reg this_saved_regs[], int regnum)
76 return (this_saved_regs[regnum].realreg >= 0
77 && this_saved_regs[regnum].addr != -1);
80 int
81 trad_frame_realreg_p (struct trad_frame_saved_reg this_saved_regs[],
82 int regnum)
84 return (this_saved_regs[regnum].realreg >= 0
85 && this_saved_regs[regnum].addr == -1);
88 void
89 trad_frame_set_value (struct trad_frame_saved_reg this_saved_regs[],
90 int regnum, LONGEST val)
92 /* Make the REALREG invalid, indicating that the ADDR contains the
93 register's value. */
94 this_saved_regs[regnum].realreg = REG_VALUE;
95 this_saved_regs[regnum].addr = val;
98 void
99 trad_frame_set_reg_value (struct trad_frame_cache *this_trad_cache,
100 int regnum, LONGEST val)
102 /* External interface for users of trad_frame_cache
103 (who cannot access the prev_regs object directly). */
104 trad_frame_set_value (this_trad_cache->prev_regs, regnum, val);
107 void
108 trad_frame_set_reg_realreg (struct trad_frame_cache *this_trad_cache,
109 int regnum, int realreg)
111 this_trad_cache->prev_regs[regnum].realreg = realreg;
112 this_trad_cache->prev_regs[regnum].addr = -1;
115 void
116 trad_frame_set_reg_addr (struct trad_frame_cache *this_trad_cache,
117 int regnum, CORE_ADDR addr)
119 this_trad_cache->prev_regs[regnum].addr = addr;
122 void
123 trad_frame_set_unknown (struct trad_frame_saved_reg this_saved_regs[],
124 int regnum)
126 /* Make the REALREG invalid, indicating that the value is not known. */
127 this_saved_regs[regnum].realreg = REG_UNKNOWN;
128 this_saved_regs[regnum].addr = -1;
131 void
132 trad_frame_get_prev_register (struct frame_info *next_frame,
133 struct trad_frame_saved_reg this_saved_regs[],
134 int regnum, int *optimizedp,
135 enum lval_type *lvalp, CORE_ADDR *addrp,
136 int *realregp, gdb_byte *bufferp)
138 struct gdbarch *gdbarch = get_frame_arch (next_frame);
139 if (trad_frame_addr_p (this_saved_regs, regnum))
141 /* The register was saved in memory. */
142 *optimizedp = 0;
143 *lvalp = lval_memory;
144 *addrp = this_saved_regs[regnum].addr;
145 *realregp = -1;
146 if (bufferp != NULL)
148 /* Read the value in from memory. */
149 get_frame_memory (next_frame, this_saved_regs[regnum].addr, bufferp,
150 register_size (gdbarch, regnum));
153 else if (trad_frame_realreg_p (this_saved_regs, regnum))
155 *optimizedp = 0;
156 *lvalp = lval_register;
157 *addrp = 0;
158 *realregp = this_saved_regs[regnum].realreg;
159 /* Ask the next frame to return the value of the register. */
160 if (bufferp)
161 frame_unwind_register (next_frame, (*realregp), bufferp);
163 else if (trad_frame_value_p (this_saved_regs, regnum))
165 /* The register's value is available. */
166 *optimizedp = 0;
167 *lvalp = not_lval;
168 *addrp = 0;
169 *realregp = -1;
170 if (bufferp != NULL)
171 store_unsigned_integer (bufferp, register_size (gdbarch, regnum),
172 this_saved_regs[regnum].addr);
174 else
176 error (_("Register %s not available"),
177 gdbarch_register_name (gdbarch, regnum));
181 void
182 trad_frame_get_register (struct trad_frame_cache *this_trad_cache,
183 struct frame_info *next_frame,
184 int regnum, int *optimizedp,
185 enum lval_type *lvalp, CORE_ADDR *addrp,
186 int *realregp, gdb_byte *bufferp)
188 trad_frame_get_prev_register (next_frame, this_trad_cache->prev_regs,
189 regnum, optimizedp, lvalp, addrp, realregp,
190 bufferp);
193 void
194 trad_frame_set_id (struct trad_frame_cache *this_trad_cache,
195 struct frame_id this_id)
197 this_trad_cache->this_id = this_id;
200 void
201 trad_frame_get_id (struct trad_frame_cache *this_trad_cache,
202 struct frame_id *this_id)
204 (*this_id) = this_trad_cache->this_id;
207 void
208 trad_frame_set_this_base (struct trad_frame_cache *this_trad_cache,
209 CORE_ADDR this_base)
211 this_trad_cache->this_base = this_base;
214 CORE_ADDR
215 trad_frame_get_this_base (struct trad_frame_cache *this_trad_cache)
217 return this_trad_cache->this_base;