Commit | Line | Data |
---|---|---|
9c1412c1 AC |
1 | /* Code dealing with dummy stack frames, for GDB, the GNU debugger. |
2 | ||
3 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, | |
4 | 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software | |
5 | Foundation, Inc. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | ||
25 | #include "defs.h" | |
26 | #include "dummy-frame.h" | |
27 | #include "regcache.h" | |
28 | #include "frame.h" | |
29 | #include "inferior.h" | |
30 | #include "gdb_assert.h" | |
31 | ||
32 | /* Dummy frame. This saves the processor state just prior to setting | |
33 | up the inferior function call. Older targets save the registers | |
34 | on the target stack (but that really slows down function calls). */ | |
35 | ||
36 | struct dummy_frame | |
37 | { | |
38 | struct dummy_frame *next; | |
39 | ||
40 | CORE_ADDR pc; | |
41 | CORE_ADDR fp; | |
42 | CORE_ADDR sp; | |
43 | CORE_ADDR top; | |
44 | struct regcache *regcache; | |
45 | ||
46 | /* Address range of the call dummy code. Look for PC in the range | |
47 | [LO..HI) (after allowing for DECR_PC_AFTER_BREAK). */ | |
48 | CORE_ADDR call_lo; | |
49 | CORE_ADDR call_hi; | |
50 | }; | |
51 | ||
52 | static struct dummy_frame *dummy_frame_stack = NULL; | |
53 | ||
54 | /* Function: find_dummy_frame(pc, fp, sp) | |
55 | ||
56 | Search the stack of dummy frames for one matching the given PC and | |
57 | FP/SP. Unlike PC_IN_CALL_DUMMY, this function doesn't need to | |
58 | adjust for DECR_PC_AFTER_BREAK. This is because it is only legal | |
59 | to call this function after the PC has been adjusted. */ | |
60 | ||
61 | struct regcache * | |
62 | generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp) | |
63 | { | |
64 | struct dummy_frame *dummyframe; | |
65 | ||
66 | for (dummyframe = dummy_frame_stack; dummyframe != NULL; | |
67 | dummyframe = dummyframe->next) | |
68 | { | |
69 | /* Does the PC fall within the dummy frame's breakpoint | |
70 | instruction. If not, discard this one. */ | |
71 | if (!(pc >= dummyframe->call_lo && pc < dummyframe->call_hi)) | |
72 | continue; | |
73 | /* Does the FP match? */ | |
74 | if (dummyframe->top != 0) | |
75 | { | |
76 | /* If the target architecture explicitly saved the | |
77 | top-of-stack before the inferior function call, assume | |
78 | that that same architecture will always pass in an FP | |
79 | (frame base) value that eactly matches that saved TOS. | |
80 | Don't check the saved SP and SP as they can lead to false | |
81 | hits. */ | |
82 | if (fp != dummyframe->top) | |
83 | continue; | |
84 | } | |
85 | else | |
86 | { | |
87 | /* An older target that hasn't explicitly or implicitly | |
88 | saved the dummy frame's top-of-stack. Try matching the | |
89 | FP against the saved SP and FP. NOTE: If you're trying | |
90 | to fix a problem with GDB not correctly finding a dummy | |
91 | frame, check the comments that go with FRAME_ALIGN() and | |
92 | SAVE_DUMMY_FRAME_TOS(). */ | |
93 | if (fp != dummyframe->fp && fp != dummyframe->sp) | |
94 | continue; | |
95 | } | |
96 | /* The FP matches this dummy frame. */ | |
97 | return dummyframe->regcache; | |
98 | } | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
103 | char * | |
104 | deprecated_generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp) | |
105 | { | |
106 | struct regcache *regcache = generic_find_dummy_frame (pc, fp); | |
107 | if (regcache == NULL) | |
108 | return NULL; | |
109 | return deprecated_grub_regcache_for_registers (regcache); | |
110 | } | |
111 | ||
112 | /* Function: pc_in_call_dummy (pc, sp, fp) | |
113 | ||
114 | Return true if the PC falls in a dummy frame created by gdb for an | |
115 | inferior call. The code below which allows DECR_PC_AFTER_BREAK is | |
116 | for infrun.c, which may give the function a PC without that | |
117 | subtracted out. */ | |
118 | ||
119 | int | |
120 | generic_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp) | |
121 | { | |
122 | struct dummy_frame *dummyframe; | |
123 | for (dummyframe = dummy_frame_stack; | |
124 | dummyframe != NULL; | |
125 | dummyframe = dummyframe->next) | |
126 | { | |
127 | if ((pc >= dummyframe->call_lo) | |
128 | && (pc < dummyframe->call_hi + DECR_PC_AFTER_BREAK)) | |
129 | return 1; | |
130 | } | |
131 | return 0; | |
132 | } | |
133 | ||
134 | /* Function: read_register_dummy | |
135 | Find a saved register from before GDB calls a function in the inferior */ | |
136 | ||
137 | CORE_ADDR | |
138 | deprecated_read_register_dummy (CORE_ADDR pc, CORE_ADDR fp, int regno) | |
139 | { | |
140 | struct regcache *dummy_regs = generic_find_dummy_frame (pc, fp); | |
141 | ||
142 | if (dummy_regs) | |
143 | { | |
144 | /* NOTE: cagney/2002-08-12: Replaced a call to | |
145 | regcache_raw_read_as_address() with a call to | |
146 | regcache_cooked_read_unsigned(). The old, ...as_address | |
147 | function was eventually calling extract_unsigned_integer (via | |
148 | extract_address) to unpack the registers value. The below is | |
149 | doing an unsigned extract so that it is functionally | |
150 | equivalent. The read needs to be cooked as, otherwise, it | |
151 | will never correctly return the value of a register in the | |
152 | [NUM_REGS .. NUM_REGS+NUM_PSEUDO_REGS) range. */ | |
153 | ULONGEST val; | |
154 | regcache_cooked_read_unsigned (dummy_regs, regno, &val); | |
155 | return val; | |
156 | } | |
157 | else | |
158 | return 0; | |
159 | } | |
160 | ||
161 | /* Save all the registers on the dummy frame stack. Most ports save the | |
162 | registers on the target stack. This results in lots of unnecessary memory | |
163 | references, which are slow when debugging via a serial line. Instead, we | |
164 | save all the registers internally, and never write them to the stack. The | |
165 | registers get restored when the called function returns to the entry point, | |
166 | where a breakpoint is laying in wait. */ | |
167 | ||
168 | void | |
169 | generic_push_dummy_frame (void) | |
170 | { | |
171 | struct dummy_frame *dummy_frame; | |
172 | CORE_ADDR fp = (get_current_frame ())->frame; | |
173 | ||
174 | /* check to see if there are stale dummy frames, | |
175 | perhaps left over from when a longjump took us out of a | |
176 | function that was called by the debugger */ | |
177 | ||
178 | dummy_frame = dummy_frame_stack; | |
179 | while (dummy_frame) | |
180 | if (INNER_THAN (dummy_frame->fp, fp)) /* stale -- destroy! */ | |
181 | { | |
182 | dummy_frame_stack = dummy_frame->next; | |
183 | regcache_xfree (dummy_frame->regcache); | |
184 | xfree (dummy_frame); | |
185 | dummy_frame = dummy_frame_stack; | |
186 | } | |
187 | else | |
188 | dummy_frame = dummy_frame->next; | |
189 | ||
190 | dummy_frame = xmalloc (sizeof (struct dummy_frame)); | |
191 | dummy_frame->regcache = regcache_xmalloc (current_gdbarch); | |
192 | ||
193 | dummy_frame->pc = read_pc (); | |
194 | dummy_frame->sp = read_sp (); | |
195 | dummy_frame->top = 0; | |
196 | dummy_frame->fp = fp; | |
197 | regcache_cpy (dummy_frame->regcache, current_regcache); | |
198 | dummy_frame->next = dummy_frame_stack; | |
199 | dummy_frame_stack = dummy_frame; | |
200 | } | |
201 | ||
202 | void | |
203 | generic_save_dummy_frame_tos (CORE_ADDR sp) | |
204 | { | |
205 | dummy_frame_stack->top = sp; | |
206 | } | |
207 | ||
208 | /* Record the upper/lower bounds on the address of the call dummy. */ | |
209 | ||
210 | void | |
211 | generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi) | |
212 | { | |
213 | dummy_frame_stack->call_lo = lo; | |
214 | dummy_frame_stack->call_hi = hi; | |
215 | } | |
216 | ||
217 | /* Restore the machine state from either the saved dummy stack or a | |
218 | real stack frame. */ | |
219 | ||
220 | void | |
221 | generic_pop_current_frame (void (*popper) (struct frame_info * frame)) | |
222 | { | |
223 | struct frame_info *frame = get_current_frame (); | |
224 | ||
225 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) | |
226 | generic_pop_dummy_frame (); | |
227 | else | |
228 | (*popper) (frame); | |
229 | } | |
230 | ||
231 | /* Function: pop_dummy_frame | |
232 | Restore the machine state from a saved dummy stack frame. */ | |
233 | ||
234 | void | |
235 | generic_pop_dummy_frame (void) | |
236 | { | |
237 | struct dummy_frame *dummy_frame = dummy_frame_stack; | |
238 | ||
239 | /* FIXME: what if the first frame isn't the right one, eg.. | |
240 | because one call-by-hand function has done a longjmp into another one? */ | |
241 | ||
242 | if (!dummy_frame) | |
243 | error ("Can't pop dummy frame!"); | |
244 | dummy_frame_stack = dummy_frame->next; | |
245 | regcache_cpy (current_regcache, dummy_frame->regcache); | |
246 | flush_cached_frames (); | |
247 | ||
248 | regcache_xfree (dummy_frame->regcache); | |
249 | xfree (dummy_frame); | |
250 | } | |
251 | ||
252 | /* Function: fix_call_dummy | |
253 | Stub function. Generic dummy frames typically do not need to fix | |
254 | the frame being created */ | |
255 | ||
256 | void | |
257 | generic_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs, | |
258 | struct value **args, struct type *type, int gcc_p) | |
259 | { | |
260 | return; | |
261 | } | |
262 | ||
263 | /* Given a call-dummy dummy-frame, return the registers. Here the | |
264 | register value is taken from the local copy of the register buffer. */ | |
265 | ||
266 | void | |
267 | generic_call_dummy_register_unwind (struct frame_info *frame, void **cache, | |
268 | int regnum, int *optimized, | |
269 | enum lval_type *lvalp, CORE_ADDR *addrp, | |
270 | int *realnum, void *bufferp) | |
271 | { | |
272 | gdb_assert (frame != NULL); | |
273 | gdb_assert (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)); | |
274 | ||
275 | /* Describe the register's location. Generic dummy frames always | |
276 | have the register value in an ``expression''. */ | |
277 | *optimized = 0; | |
278 | *lvalp = not_lval; | |
279 | *addrp = 0; | |
280 | *realnum = -1; | |
281 | ||
282 | /* If needed, find and return the value of the register. */ | |
283 | if (bufferp != NULL) | |
284 | { | |
285 | struct regcache *registers; | |
286 | #if 1 | |
287 | /* Get the address of the register buffer that contains all the | |
288 | saved registers for this dummy frame. Cache that address. */ | |
289 | registers = (*cache); | |
290 | if (registers == NULL) | |
291 | { | |
292 | registers = generic_find_dummy_frame (frame->pc, frame->frame); | |
293 | (*cache) = registers; | |
294 | } | |
295 | #else | |
296 | /* Get the address of the register buffer that contains the | |
297 | saved registers and then extract the value from that. */ | |
298 | registers = generic_find_dummy_frame (frame->pc, frame->frame); | |
299 | #endif | |
300 | gdb_assert (registers != NULL); | |
301 | /* Return the actual value. */ | |
302 | /* Use the regcache_cooked_read() method so that it, on the fly, | |
303 | constructs either a raw or pseudo register from the raw | |
304 | register cache. */ | |
305 | regcache_cooked_read (registers, regnum, bufferp); | |
306 | } | |
307 | } | |
308 |