]>
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, | |
4ea2acf0 AC |
4 | 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free |
5 | Software Foundation, Inc. | |
9c1412c1 AC |
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" | |
494cca16 | 31 | #include "frame-unwind.h" |
00905d52 AC |
32 | #include "command.h" |
33 | #include "gdbcmd.h" | |
9c1412c1 | 34 | |
6dc42492 AC |
35 | static void dummy_frame_this_id (struct frame_info *next_frame, |
36 | void **this_prologue_cache, | |
37 | struct frame_id *this_id); | |
38 | ||
90ba813f AC |
39 | static int pc_in_dummy_frame (CORE_ADDR pc); |
40 | ||
9c1412c1 AC |
41 | /* Dummy frame. This saves the processor state just prior to setting |
42 | up the inferior function call. Older targets save the registers | |
43 | on the target stack (but that really slows down function calls). */ | |
44 | ||
45 | struct dummy_frame | |
46 | { | |
47 | struct dummy_frame *next; | |
48 | ||
f18c5a73 AC |
49 | /* These values belong to the caller (the previous frame, the frame |
50 | that this unwinds back to). */ | |
9c1412c1 | 51 | CORE_ADDR pc; |
9c1412c1 | 52 | CORE_ADDR top; |
c689142b | 53 | struct frame_id id; |
9c1412c1 AC |
54 | struct regcache *regcache; |
55 | ||
56 | /* Address range of the call dummy code. Look for PC in the range | |
57 | [LO..HI) (after allowing for DECR_PC_AFTER_BREAK). */ | |
58 | CORE_ADDR call_lo; | |
59 | CORE_ADDR call_hi; | |
60 | }; | |
61 | ||
62 | static struct dummy_frame *dummy_frame_stack = NULL; | |
63 | ||
64 | /* Function: find_dummy_frame(pc, fp, sp) | |
65 | ||
66 | Search the stack of dummy frames for one matching the given PC and | |
5e0f933e | 67 | FP/SP. Unlike pc_in_dummy_frame(), this function doesn't need to |
9c1412c1 AC |
68 | adjust for DECR_PC_AFTER_BREAK. This is because it is only legal |
69 | to call this function after the PC has been adjusted. */ | |
70 | ||
8779790c AC |
71 | static struct dummy_frame * |
72 | find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp) | |
9c1412c1 AC |
73 | { |
74 | struct dummy_frame *dummyframe; | |
75 | ||
76 | for (dummyframe = dummy_frame_stack; dummyframe != NULL; | |
77 | dummyframe = dummyframe->next) | |
78 | { | |
79 | /* Does the PC fall within the dummy frame's breakpoint | |
80 | instruction. If not, discard this one. */ | |
81 | if (!(pc >= dummyframe->call_lo && pc < dummyframe->call_hi)) | |
82 | continue; | |
83 | /* Does the FP match? */ | |
ff65ac78 AC |
84 | /* "infcall.c" explicitly saved the top-of-stack before the |
85 | inferior function call, assume unwind_dummy_id() returns that | |
86 | same stack value. */ | |
87 | if (fp != dummyframe->top) | |
88 | continue; | |
9c1412c1 | 89 | /* The FP matches this dummy frame. */ |
8779790c | 90 | return dummyframe; |
9c1412c1 AC |
91 | } |
92 | ||
8779790c AC |
93 | return NULL; |
94 | } | |
95 | ||
30a4a8e0 | 96 | /* Function: pc_in_call_dummy (pc) |
9c1412c1 AC |
97 | |
98 | Return true if the PC falls in a dummy frame created by gdb for an | |
99 | inferior call. The code below which allows DECR_PC_AFTER_BREAK is | |
100 | for infrun.c, which may give the function a PC without that | |
101 | subtracted out. */ | |
102 | ||
103 | int | |
30a4a8e0 | 104 | deprecated_pc_in_call_dummy (CORE_ADDR pc) |
5e0f933e AC |
105 | { |
106 | return pc_in_dummy_frame (pc); | |
107 | } | |
108 | ||
109 | /* Return non-zero if the PC falls in a dummy frame. | |
110 | ||
111 | The code below which allows DECR_PC_AFTER_BREAK is for infrun.c, | |
112 | which may give the function a PC without that subtracted out. | |
113 | ||
114 | FIXME: cagney/2002-11-23: This is silly. Surely "infrun.c" can | |
115 | figure out what the real PC (as in the resume address) is BEFORE | |
e4a2df64 | 116 | calling this function. */ |
5e0f933e | 117 | |
90ba813f | 118 | static int |
5e0f933e | 119 | pc_in_dummy_frame (CORE_ADDR pc) |
9c1412c1 AC |
120 | { |
121 | struct dummy_frame *dummyframe; | |
122 | for (dummyframe = dummy_frame_stack; | |
123 | dummyframe != NULL; | |
124 | dummyframe = dummyframe->next) | |
125 | { | |
126 | if ((pc >= dummyframe->call_lo) | |
127 | && (pc < dummyframe->call_hi + DECR_PC_AFTER_BREAK)) | |
128 | return 1; | |
129 | } | |
130 | return 0; | |
131 | } | |
132 | ||
9c1412c1 AC |
133 | /* Save all the registers on the dummy frame stack. Most ports save the |
134 | registers on the target stack. This results in lots of unnecessary memory | |
135 | references, which are slow when debugging via a serial line. Instead, we | |
136 | save all the registers internally, and never write them to the stack. The | |
137 | registers get restored when the called function returns to the entry point, | |
138 | where a breakpoint is laying in wait. */ | |
139 | ||
140 | void | |
141 | generic_push_dummy_frame (void) | |
142 | { | |
143 | struct dummy_frame *dummy_frame; | |
8b36eed8 | 144 | CORE_ADDR fp = get_frame_base (get_current_frame ()); |
9c1412c1 AC |
145 | |
146 | /* check to see if there are stale dummy frames, | |
147 | perhaps left over from when a longjump took us out of a | |
148 | function that was called by the debugger */ | |
149 | ||
150 | dummy_frame = dummy_frame_stack; | |
151 | while (dummy_frame) | |
ff65ac78 AC |
152 | if (gdbarch_inner_than (current_gdbarch, dummy_frame->top, fp)) |
153 | /* stale -- destroy! */ | |
9c1412c1 AC |
154 | { |
155 | dummy_frame_stack = dummy_frame->next; | |
156 | regcache_xfree (dummy_frame->regcache); | |
157 | xfree (dummy_frame); | |
158 | dummy_frame = dummy_frame_stack; | |
159 | } | |
160 | else | |
161 | dummy_frame = dummy_frame->next; | |
162 | ||
163 | dummy_frame = xmalloc (sizeof (struct dummy_frame)); | |
a81dcb05 | 164 | dummy_frame->regcache = frame_save_as_regcache (get_current_frame ()); |
9c1412c1 AC |
165 | |
166 | dummy_frame->pc = read_pc (); | |
9c1412c1 | 167 | dummy_frame->top = 0; |
c689142b | 168 | dummy_frame->id = get_frame_id (get_current_frame ()); |
9c1412c1 AC |
169 | dummy_frame->next = dummy_frame_stack; |
170 | dummy_frame_stack = dummy_frame; | |
171 | } | |
172 | ||
173 | void | |
174 | generic_save_dummy_frame_tos (CORE_ADDR sp) | |
175 | { | |
176 | dummy_frame_stack->top = sp; | |
177 | } | |
178 | ||
179 | /* Record the upper/lower bounds on the address of the call dummy. */ | |
180 | ||
181 | void | |
182 | generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi) | |
183 | { | |
184 | dummy_frame_stack->call_lo = lo; | |
185 | dummy_frame_stack->call_hi = hi; | |
186 | } | |
187 | ||
9c1412c1 AC |
188 | /* Given a call-dummy dummy-frame, return the registers. Here the |
189 | register value is taken from the local copy of the register buffer. */ | |
190 | ||
494cca16 | 191 | static void |
6dc42492 AC |
192 | dummy_frame_prev_register (struct frame_info *next_frame, |
193 | void **this_prologue_cache, | |
194 | int regnum, int *optimized, | |
195 | enum lval_type *lvalp, CORE_ADDR *addrp, | |
196 | int *realnum, void *bufferp) | |
9c1412c1 | 197 | { |
6dc42492 AC |
198 | struct dummy_frame *dummy; |
199 | struct frame_id id; | |
200 | ||
201 | /* Call the ID method which, if at all possible, will set the | |
202 | prologue cache. */ | |
203 | dummy_frame_this_id (next_frame, this_prologue_cache, &id); | |
204 | dummy = (*this_prologue_cache); | |
8779790c | 205 | gdb_assert (dummy != NULL); |
9c1412c1 AC |
206 | |
207 | /* Describe the register's location. Generic dummy frames always | |
208 | have the register value in an ``expression''. */ | |
209 | *optimized = 0; | |
210 | *lvalp = not_lval; | |
211 | *addrp = 0; | |
212 | *realnum = -1; | |
213 | ||
214 | /* If needed, find and return the value of the register. */ | |
215 | if (bufferp != NULL) | |
216 | { | |
9c1412c1 AC |
217 | /* Return the actual value. */ |
218 | /* Use the regcache_cooked_read() method so that it, on the fly, | |
219 | constructs either a raw or pseudo register from the raw | |
220 | register cache. */ | |
8779790c | 221 | regcache_cooked_read (dummy->regcache, regnum, bufferp); |
9c1412c1 AC |
222 | } |
223 | } | |
224 | ||
6dc42492 AC |
225 | /* Assuming that THIS frame is a dummy (remember, the NEXT and not |
226 | THIS frame is passed in), return the ID of THIS frame. That ID is | |
227 | determined by examining the NEXT frame's unwound registers using | |
228 | the method unwind_dummy_id(). As a side effect, THIS dummy frame's | |
229 | dummy cache is located and and saved in THIS_PROLOGUE_CACHE. */ | |
494cca16 AC |
230 | |
231 | static void | |
6dc42492 AC |
232 | dummy_frame_this_id (struct frame_info *next_frame, |
233 | void **this_prologue_cache, | |
234 | struct frame_id *this_id) | |
c689142b | 235 | { |
6dc42492 AC |
236 | struct dummy_frame *dummy = (*this_prologue_cache); |
237 | if (dummy != NULL) | |
238 | { | |
239 | (*this_id) = dummy->id; | |
240 | return; | |
241 | } | |
242 | /* When unwinding a normal frame, the stack structure is determined | |
243 | by analyzing the frame's function's code (be it using brute force | |
244 | prologue analysis, or the dwarf2 CFI). In the case of a dummy | |
ff65ac78 AC |
245 | frame, that simply isn't possible. The PC is either the program |
246 | entry point, or some random address on the stack. Trying to use | |
247 | that PC to apply standard frame ID unwind techniques is just | |
248 | asking for trouble. */ | |
249 | /* Use an architecture specific method to extract the prev's dummy | |
250 | ID from the next frame. Note that this method uses | |
251 | frame_register_unwind to obtain the register values needed to | |
252 | determine the dummy frame's ID. */ | |
253 | gdb_assert (gdbarch_unwind_dummy_id_p (current_gdbarch)); | |
254 | (*this_id) = gdbarch_unwind_dummy_id (current_gdbarch, next_frame); | |
d0a55772 AC |
255 | (*this_prologue_cache) = find_dummy_frame ((*this_id).code_addr, |
256 | (*this_id).stack_addr); | |
c689142b AC |
257 | } |
258 | ||
494cca16 AC |
259 | static struct frame_unwind dummy_frame_unwind = |
260 | { | |
7df05f2b | 261 | DUMMY_FRAME, |
6dc42492 AC |
262 | dummy_frame_this_id, |
263 | dummy_frame_prev_register | |
494cca16 AC |
264 | }; |
265 | ||
266 | const struct frame_unwind * | |
336d1bba | 267 | dummy_frame_sniffer (struct frame_info *next_frame) |
494cca16 | 268 | { |
336d1bba | 269 | CORE_ADDR pc = frame_pc_unwind (next_frame); |
90ba813f | 270 | if (pc_in_dummy_frame (pc)) |
494cca16 AC |
271 | return &dummy_frame_unwind; |
272 | else | |
273 | return NULL; | |
274 | } | |
00905d52 AC |
275 | |
276 | static void | |
277 | fprint_dummy_frames (struct ui_file *file) | |
278 | { | |
279 | struct dummy_frame *s; | |
280 | for (s = dummy_frame_stack; s != NULL; s = s->next) | |
281 | { | |
282 | gdb_print_host_address (s, file); | |
283 | fprintf_unfiltered (file, ":"); | |
284 | fprintf_unfiltered (file, " pc=0x%s", paddr (s->pc)); | |
00905d52 AC |
285 | fprintf_unfiltered (file, " top=0x%s", paddr (s->top)); |
286 | fprintf_unfiltered (file, " id="); | |
287 | fprint_frame_id (file, s->id); | |
288 | fprintf_unfiltered (file, " call_lo=0x%s", paddr (s->call_lo)); | |
289 | fprintf_unfiltered (file, " call_hi=0x%s", paddr (s->call_hi)); | |
290 | fprintf_unfiltered (file, "\n"); | |
291 | } | |
292 | } | |
293 | ||
294 | static void | |
295 | maintenance_print_dummy_frames (char *args, int from_tty) | |
296 | { | |
297 | if (args == NULL) | |
298 | fprint_dummy_frames (gdb_stdout); | |
299 | else | |
300 | { | |
301 | struct ui_file *file = gdb_fopen (args, "w"); | |
302 | if (file == NULL) | |
303 | perror_with_name ("maintenance print dummy-frames"); | |
304 | fprint_dummy_frames (file); | |
305 | ui_file_delete (file); | |
306 | } | |
307 | } | |
308 | ||
309 | extern void _initialize_dummy_frame (void); | |
310 | ||
311 | void | |
312 | _initialize_dummy_frame (void) | |
313 | { | |
314 | add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames, | |
315 | "Print the contents of the internal dummy-frame stack.", | |
316 | &maintenanceprintlist); | |
317 | ||
318 | } |