Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Get info from stack frames; |
2 | convert between frames, blocks, functions and pc values. | |
b6ba6518 KB |
3 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
4 | 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
24 | #include "symtab.h" | |
25 | #include "bfd.h" | |
26 | #include "symfile.h" | |
27 | #include "objfiles.h" | |
28 | #include "frame.h" | |
29 | #include "gdbcore.h" | |
30 | #include "value.h" /* for read_register */ | |
31 | #include "target.h" /* for target_has_stack */ | |
32 | #include "inferior.h" /* for read_pc */ | |
33 | #include "annotate.h" | |
4e052eda | 34 | #include "regcache.h" |
c906108c SS |
35 | |
36 | /* Prototypes for exported functions. */ | |
37 | ||
53a5351d | 38 | void _initialize_blockframe (void); |
c906108c SS |
39 | |
40 | /* A default FRAME_CHAIN_VALID, in the form that is suitable for most | |
41 | targets. If FRAME_CHAIN_VALID returns zero it means that the given | |
42 | frame is the outermost one and has no caller. */ | |
43 | ||
44 | int | |
fba45db2 | 45 | file_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe) |
c906108c SS |
46 | { |
47 | return ((chain) != 0 | |
c4093a6a | 48 | && !inside_entry_file (FRAME_SAVED_PC (thisframe))); |
c906108c SS |
49 | } |
50 | ||
51 | /* Use the alternate method of avoiding running up off the end of the | |
52 | frame chain or following frames back into the startup code. See | |
53 | the comments in objfiles.h. */ | |
c5aa993b | 54 | |
c906108c | 55 | int |
fba45db2 | 56 | func_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe) |
c906108c SS |
57 | { |
58 | return ((chain) != 0 | |
c4093a6a JM |
59 | && !inside_main_func ((thisframe)->pc) |
60 | && !inside_entry_func ((thisframe)->pc)); | |
c906108c SS |
61 | } |
62 | ||
63 | /* A very simple method of determining a valid frame */ | |
c5aa993b | 64 | |
c906108c | 65 | int |
fba45db2 | 66 | nonnull_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe) |
c906108c SS |
67 | { |
68 | return ((chain) != 0); | |
69 | } | |
70 | ||
71 | /* Is ADDR inside the startup file? Note that if your machine | |
72 | has a way to detect the bottom of the stack, there is no need | |
73 | to call this function from FRAME_CHAIN_VALID; the reason for | |
74 | doing so is that some machines have no way of detecting bottom | |
75 | of stack. | |
76 | ||
77 | A PC of zero is always considered to be the bottom of the stack. */ | |
78 | ||
79 | int | |
fba45db2 | 80 | inside_entry_file (CORE_ADDR addr) |
c906108c SS |
81 | { |
82 | if (addr == 0) | |
83 | return 1; | |
84 | if (symfile_objfile == 0) | |
85 | return 0; | |
7a292a7a SS |
86 | if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT) |
87 | { | |
88 | /* Do not stop backtracing if the pc is in the call dummy | |
c5aa993b | 89 | at the entry point. */ |
7a292a7a | 90 | /* FIXME: Won't always work with zeros for the last two arguments */ |
c5aa993b | 91 | if (PC_IN_CALL_DUMMY (addr, 0, 0)) |
7a292a7a SS |
92 | return 0; |
93 | } | |
c5aa993b JM |
94 | return (addr >= symfile_objfile->ei.entry_file_lowpc && |
95 | addr < symfile_objfile->ei.entry_file_highpc); | |
c906108c SS |
96 | } |
97 | ||
98 | /* Test a specified PC value to see if it is in the range of addresses | |
99 | that correspond to the main() function. See comments above for why | |
100 | we might want to do this. | |
101 | ||
102 | Typically called from FRAME_CHAIN_VALID. | |
103 | ||
104 | A PC of zero is always considered to be the bottom of the stack. */ | |
105 | ||
106 | int | |
fba45db2 | 107 | inside_main_func (CORE_ADDR pc) |
c906108c SS |
108 | { |
109 | if (pc == 0) | |
110 | return 1; | |
111 | if (symfile_objfile == 0) | |
112 | return 0; | |
113 | ||
114 | /* If the addr range is not set up at symbol reading time, set it up now. | |
115 | This is for FRAME_CHAIN_VALID_ALTERNATE. I do this for coff, because | |
116 | it is unable to set it up and symbol reading time. */ | |
117 | ||
c5aa993b JM |
118 | if (symfile_objfile->ei.main_func_lowpc == INVALID_ENTRY_LOWPC && |
119 | symfile_objfile->ei.main_func_highpc == INVALID_ENTRY_HIGHPC) | |
c906108c SS |
120 | { |
121 | struct symbol *mainsym; | |
122 | ||
51cc5b07 | 123 | mainsym = lookup_symbol (main_name (), NULL, VAR_NAMESPACE, NULL, NULL); |
c5aa993b JM |
124 | if (mainsym && SYMBOL_CLASS (mainsym) == LOC_BLOCK) |
125 | { | |
126 | symfile_objfile->ei.main_func_lowpc = | |
c906108c | 127 | BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym)); |
c5aa993b | 128 | symfile_objfile->ei.main_func_highpc = |
c906108c | 129 | BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym)); |
c5aa993b | 130 | } |
c906108c | 131 | } |
c5aa993b JM |
132 | return (symfile_objfile->ei.main_func_lowpc <= pc && |
133 | symfile_objfile->ei.main_func_highpc > pc); | |
c906108c SS |
134 | } |
135 | ||
136 | /* Test a specified PC value to see if it is in the range of addresses | |
137 | that correspond to the process entry point function. See comments | |
138 | in objfiles.h for why we might want to do this. | |
139 | ||
140 | Typically called from FRAME_CHAIN_VALID. | |
141 | ||
142 | A PC of zero is always considered to be the bottom of the stack. */ | |
143 | ||
144 | int | |
fba45db2 | 145 | inside_entry_func (CORE_ADDR pc) |
c906108c SS |
146 | { |
147 | if (pc == 0) | |
148 | return 1; | |
149 | if (symfile_objfile == 0) | |
150 | return 0; | |
7a292a7a SS |
151 | if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT) |
152 | { | |
153 | /* Do not stop backtracing if the pc is in the call dummy | |
c5aa993b | 154 | at the entry point. */ |
7a292a7a SS |
155 | /* FIXME: Won't always work with zeros for the last two arguments */ |
156 | if (PC_IN_CALL_DUMMY (pc, 0, 0)) | |
157 | return 0; | |
158 | } | |
c5aa993b JM |
159 | return (symfile_objfile->ei.entry_func_lowpc <= pc && |
160 | symfile_objfile->ei.entry_func_highpc > pc); | |
c906108c SS |
161 | } |
162 | ||
163 | /* Info about the innermost stack frame (contents of FP register) */ | |
164 | ||
165 | static struct frame_info *current_frame; | |
166 | ||
167 | /* Cache for frame addresses already read by gdb. Valid only while | |
168 | inferior is stopped. Control variables for the frame cache should | |
169 | be local to this module. */ | |
170 | ||
171 | static struct obstack frame_cache_obstack; | |
172 | ||
173 | void * | |
fba45db2 | 174 | frame_obstack_alloc (unsigned long size) |
c906108c SS |
175 | { |
176 | return obstack_alloc (&frame_cache_obstack, size); | |
177 | } | |
178 | ||
179 | void | |
fba45db2 | 180 | frame_saved_regs_zalloc (struct frame_info *fi) |
c906108c | 181 | { |
c5aa993b | 182 | fi->saved_regs = (CORE_ADDR *) |
c906108c SS |
183 | frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS); |
184 | memset (fi->saved_regs, 0, SIZEOF_FRAME_SAVED_REGS); | |
185 | } | |
186 | ||
187 | ||
188 | /* Return the innermost (currently executing) stack frame. */ | |
189 | ||
190 | struct frame_info * | |
fba45db2 | 191 | get_current_frame (void) |
c906108c SS |
192 | { |
193 | if (current_frame == NULL) | |
194 | { | |
195 | if (target_has_stack) | |
196 | current_frame = create_new_frame (read_fp (), read_pc ()); | |
197 | else | |
198 | error ("No stack."); | |
199 | } | |
200 | return current_frame; | |
201 | } | |
202 | ||
203 | void | |
fba45db2 | 204 | set_current_frame (struct frame_info *frame) |
c906108c SS |
205 | { |
206 | current_frame = frame; | |
207 | } | |
208 | ||
209 | /* Create an arbitrary (i.e. address specified by user) or innermost frame. | |
210 | Always returns a non-NULL value. */ | |
211 | ||
212 | struct frame_info * | |
fba45db2 | 213 | create_new_frame (CORE_ADDR addr, CORE_ADDR pc) |
c906108c SS |
214 | { |
215 | struct frame_info *fi; | |
216 | char *name; | |
217 | ||
218 | fi = (struct frame_info *) | |
219 | obstack_alloc (&frame_cache_obstack, | |
220 | sizeof (struct frame_info)); | |
221 | ||
736d0890 MS |
222 | /* Zero all fields by default. */ |
223 | memset (fi, 0, sizeof (struct frame_info)); | |
224 | ||
c906108c SS |
225 | fi->frame = addr; |
226 | fi->pc = pc; | |
c5aa993b | 227 | find_pc_partial_function (pc, &name, (CORE_ADDR *) NULL, (CORE_ADDR *) NULL); |
c906108c SS |
228 | fi->signal_handler_caller = IN_SIGTRAMP (fi->pc, name); |
229 | ||
5fdff426 AC |
230 | if (INIT_EXTRA_FRAME_INFO_P ()) |
231 | INIT_EXTRA_FRAME_INFO (0, fi); | |
c906108c SS |
232 | |
233 | return fi; | |
234 | } | |
235 | ||
c906108c SS |
236 | /* Return the frame that FRAME calls (NULL if FRAME is the innermost |
237 | frame). */ | |
238 | ||
239 | struct frame_info * | |
fba45db2 | 240 | get_next_frame (struct frame_info *frame) |
c906108c SS |
241 | { |
242 | return frame->next; | |
243 | } | |
244 | ||
245 | /* Flush the entire frame cache. */ | |
246 | ||
247 | void | |
fba45db2 | 248 | flush_cached_frames (void) |
c906108c SS |
249 | { |
250 | /* Since we can't really be sure what the first object allocated was */ | |
251 | obstack_free (&frame_cache_obstack, 0); | |
252 | obstack_init (&frame_cache_obstack); | |
253 | ||
c5aa993b | 254 | current_frame = NULL; /* Invalidate cache */ |
c906108c SS |
255 | select_frame (NULL, -1); |
256 | annotate_frames_invalid (); | |
257 | } | |
258 | ||
259 | /* Flush the frame cache, and start a new one if necessary. */ | |
260 | ||
261 | void | |
fba45db2 | 262 | reinit_frame_cache (void) |
c906108c SS |
263 | { |
264 | flush_cached_frames (); | |
265 | ||
39f77062 KB |
266 | /* FIXME: The inferior_ptid test is wrong if there is a corefile. */ |
267 | if (PIDGET (inferior_ptid) != 0) | |
c906108c SS |
268 | { |
269 | select_frame (get_current_frame (), 0); | |
270 | } | |
271 | } | |
272 | ||
c906108c SS |
273 | /* Return nonzero if the function for this frame lacks a prologue. Many |
274 | machines can define FRAMELESS_FUNCTION_INVOCATION to just call this | |
275 | function. */ | |
276 | ||
277 | int | |
fba45db2 | 278 | frameless_look_for_prologue (struct frame_info *frame) |
c906108c SS |
279 | { |
280 | CORE_ADDR func_start, after_prologue; | |
53a5351d | 281 | |
c906108c SS |
282 | func_start = get_pc_function_start (frame->pc); |
283 | if (func_start) | |
284 | { | |
285 | func_start += FUNCTION_START_OFFSET; | |
53a5351d JM |
286 | /* This is faster, since only care whether there *is* a |
287 | prologue, not how long it is. */ | |
dad41f9a | 288 | return PROLOGUE_FRAMELESS_P (func_start); |
c906108c SS |
289 | } |
290 | else if (frame->pc == 0) | |
53a5351d JM |
291 | /* A frame with a zero PC is usually created by dereferencing a |
292 | NULL function pointer, normally causing an immediate core dump | |
293 | of the inferior. Mark function as frameless, as the inferior | |
294 | has no chance of setting up a stack frame. */ | |
c906108c SS |
295 | return 1; |
296 | else | |
297 | /* If we can't find the start of the function, we don't really | |
298 | know whether the function is frameless, but we should be able | |
299 | to get a reasonable (i.e. best we can do under the | |
300 | circumstances) backtrace by saying that it isn't. */ | |
301 | return 0; | |
302 | } | |
303 | ||
304 | /* Default a few macros that people seldom redefine. */ | |
305 | ||
c906108c SS |
306 | #ifndef FRAME_CHAIN_COMBINE |
307 | #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain) | |
308 | #endif | |
309 | ||
310 | /* Return a structure containing various interesting information | |
311 | about the frame that called NEXT_FRAME. Returns NULL | |
312 | if there is no such frame. */ | |
313 | ||
314 | struct frame_info * | |
fba45db2 | 315 | get_prev_frame (struct frame_info *next_frame) |
c906108c SS |
316 | { |
317 | CORE_ADDR address = 0; | |
318 | struct frame_info *prev; | |
319 | int fromleaf = 0; | |
320 | char *name; | |
321 | ||
322 | /* If the requested entry is in the cache, return it. | |
323 | Otherwise, figure out what the address should be for the entry | |
324 | we're about to add to the cache. */ | |
325 | ||
326 | if (!next_frame) | |
327 | { | |
328 | #if 0 | |
329 | /* This screws value_of_variable, which just wants a nice clean | |
c5aa993b JM |
330 | NULL return from block_innermost_frame if there are no frames. |
331 | I don't think I've ever seen this message happen otherwise. | |
332 | And returning NULL here is a perfectly legitimate thing to do. */ | |
c906108c SS |
333 | if (!current_frame) |
334 | { | |
335 | error ("You haven't set up a process's stack to examine."); | |
336 | } | |
337 | #endif | |
338 | ||
339 | return current_frame; | |
340 | } | |
341 | ||
342 | /* If we have the prev one, return it */ | |
343 | if (next_frame->prev) | |
344 | return next_frame->prev; | |
345 | ||
346 | /* On some machines it is possible to call a function without | |
347 | setting up a stack frame for it. On these machines, we | |
348 | define this macro to take two args; a frameinfo pointer | |
349 | identifying a frame and a variable to set or clear if it is | |
350 | or isn't leafless. */ | |
392a587b | 351 | |
c906108c SS |
352 | /* Still don't want to worry about this except on the innermost |
353 | frame. This macro will set FROMLEAF if NEXT_FRAME is a | |
354 | frameless function invocation. */ | |
355 | if (!(next_frame->next)) | |
356 | { | |
392a587b | 357 | fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame); |
c906108c SS |
358 | if (fromleaf) |
359 | address = FRAME_FP (next_frame); | |
360 | } | |
c906108c SS |
361 | |
362 | if (!fromleaf) | |
363 | { | |
364 | /* Two macros defined in tm.h specify the machine-dependent | |
c5aa993b JM |
365 | actions to be performed here. |
366 | First, get the frame's chain-pointer. | |
367 | If that is zero, the frame is the outermost frame or a leaf | |
368 | called by the outermost frame. This means that if start | |
369 | calls main without a frame, we'll return 0 (which is fine | |
370 | anyway). | |
371 | ||
372 | Nope; there's a problem. This also returns when the current | |
373 | routine is a leaf of main. This is unacceptable. We move | |
374 | this to after the ffi test; I'd rather have backtraces from | |
375 | start go curfluy than have an abort called from main not show | |
376 | main. */ | |
c906108c SS |
377 | address = FRAME_CHAIN (next_frame); |
378 | if (!FRAME_CHAIN_VALID (address, next_frame)) | |
379 | return 0; | |
380 | address = FRAME_CHAIN_COMBINE (address, next_frame); | |
381 | } | |
382 | if (address == 0) | |
383 | return 0; | |
384 | ||
385 | prev = (struct frame_info *) | |
386 | obstack_alloc (&frame_cache_obstack, | |
387 | sizeof (struct frame_info)); | |
388 | ||
bb30608f | 389 | /* Zero all fields by default. */ |
0c8053b6 | 390 | memset (prev, 0, sizeof (struct frame_info)); |
bb30608f | 391 | |
c906108c SS |
392 | if (next_frame) |
393 | next_frame->prev = prev; | |
394 | prev->next = next_frame; | |
c906108c | 395 | prev->frame = address; |
c906108c SS |
396 | |
397 | /* This change should not be needed, FIXME! We should | |
398 | determine whether any targets *need* INIT_FRAME_PC to happen | |
399 | after INIT_EXTRA_FRAME_INFO and come up with a simple way to | |
400 | express what goes on here. | |
401 | ||
c5aa993b JM |
402 | INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame |
403 | (where the PC is already set up) and here (where it isn't). | |
404 | INIT_FRAME_PC is only called from here, always after | |
405 | INIT_EXTRA_FRAME_INFO. | |
406 | ||
c906108c SS |
407 | The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC |
408 | value (which hasn't been set yet). Some other machines appear to | |
409 | require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo. | |
410 | ||
411 | We shouldn't need INIT_FRAME_PC_FIRST to add more complication to | |
412 | an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92. | |
413 | ||
414 | Assuming that some machines need INIT_FRAME_PC after | |
415 | INIT_EXTRA_FRAME_INFO, one possible scheme: | |
416 | ||
417 | SETUP_INNERMOST_FRAME() | |
c5aa993b JM |
418 | Default version is just create_new_frame (read_fp ()), |
419 | read_pc ()). Machines with extra frame info would do that (or the | |
420 | local equivalent) and then set the extra fields. | |
c906108c | 421 | SETUP_ARBITRARY_FRAME(argc, argv) |
c5aa993b JM |
422 | Only change here is that create_new_frame would no longer init extra |
423 | frame info; SETUP_ARBITRARY_FRAME would have to do that. | |
c906108c | 424 | INIT_PREV_FRAME(fromleaf, prev) |
c5aa993b JM |
425 | Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC. This should |
426 | also return a flag saying whether to keep the new frame, or | |
427 | whether to discard it, because on some machines (e.g. mips) it | |
428 | is really awkward to have FRAME_CHAIN_VALID called *before* | |
429 | INIT_EXTRA_FRAME_INFO (there is no good way to get information | |
430 | deduced in FRAME_CHAIN_VALID into the extra fields of the new frame). | |
c906108c | 431 | std_frame_pc(fromleaf, prev) |
c5aa993b JM |
432 | This is the default setting for INIT_PREV_FRAME. It just does what |
433 | the default INIT_FRAME_PC does. Some machines will call it from | |
434 | INIT_PREV_FRAME (either at the beginning, the end, or in the middle). | |
435 | Some machines won't use it. | |
c906108c SS |
436 | kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */ |
437 | ||
c906108c | 438 | INIT_FRAME_PC_FIRST (fromleaf, prev); |
c906108c | 439 | |
e6b47f07 AC |
440 | if (INIT_EXTRA_FRAME_INFO_P ()) |
441 | INIT_EXTRA_FRAME_INFO (fromleaf, prev); | |
c906108c SS |
442 | |
443 | /* This entry is in the frame queue now, which is good since | |
444 | FRAME_SAVED_PC may use that queue to figure out its value | |
445 | (see tm-sparc.h). We want the pc saved in the inferior frame. */ | |
c5aa993b | 446 | INIT_FRAME_PC (fromleaf, prev); |
c906108c SS |
447 | |
448 | /* If ->frame and ->pc are unchanged, we are in the process of getting | |
449 | ourselves into an infinite backtrace. Some architectures check this | |
450 | in FRAME_CHAIN or thereabouts, but it seems like there is no reason | |
451 | this can't be an architecture-independent check. */ | |
452 | if (next_frame != NULL) | |
453 | { | |
454 | if (prev->frame == next_frame->frame | |
455 | && prev->pc == next_frame->pc) | |
456 | { | |
457 | next_frame->prev = NULL; | |
458 | obstack_free (&frame_cache_obstack, prev); | |
459 | return NULL; | |
460 | } | |
461 | } | |
462 | ||
463 | find_pc_partial_function (prev->pc, &name, | |
c5aa993b | 464 | (CORE_ADDR *) NULL, (CORE_ADDR *) NULL); |
c906108c SS |
465 | if (IN_SIGTRAMP (prev->pc, name)) |
466 | prev->signal_handler_caller = 1; | |
467 | ||
468 | return prev; | |
469 | } | |
470 | ||
471 | CORE_ADDR | |
fba45db2 | 472 | get_frame_pc (struct frame_info *frame) |
c906108c SS |
473 | { |
474 | return frame->pc; | |
475 | } | |
476 | ||
477 | ||
478 | #ifdef FRAME_FIND_SAVED_REGS | |
479 | /* XXX - deprecated. This is a compatibility function for targets | |
480 | that do not yet implement FRAME_INIT_SAVED_REGS. */ | |
481 | /* Find the addresses in which registers are saved in FRAME. */ | |
482 | ||
483 | void | |
fba45db2 KB |
484 | get_frame_saved_regs (struct frame_info *frame, |
485 | struct frame_saved_regs *saved_regs_addr) | |
c906108c SS |
486 | { |
487 | if (frame->saved_regs == NULL) | |
488 | { | |
c5aa993b | 489 | frame->saved_regs = (CORE_ADDR *) |
c906108c SS |
490 | frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS); |
491 | } | |
492 | if (saved_regs_addr == NULL) | |
493 | { | |
494 | struct frame_saved_regs saved_regs; | |
495 | FRAME_FIND_SAVED_REGS (frame, saved_regs); | |
496 | memcpy (frame->saved_regs, &saved_regs, SIZEOF_FRAME_SAVED_REGS); | |
497 | } | |
498 | else | |
499 | { | |
500 | FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr); | |
501 | memcpy (frame->saved_regs, saved_regs_addr, SIZEOF_FRAME_SAVED_REGS); | |
502 | } | |
503 | } | |
504 | #endif | |
505 | ||
506 | /* Return the innermost lexical block in execution | |
ae767bfb JB |
507 | in a specified stack frame. The frame address is assumed valid. |
508 | ||
509 | If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code | |
510 | address we used to choose the block. We use this to find a source | |
511 | line, to decide which macro definitions are in scope. | |
512 | ||
513 | The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's | |
514 | PC, and may not really be a valid PC at all. For example, in the | |
515 | caller of a function declared to never return, the code at the | |
516 | return address will never be reached, so the call instruction may | |
517 | be the very last instruction in the block. So the address we use | |
518 | to choose the block is actually one byte before the return address | |
519 | --- hopefully pointing us at the call instruction, or its delay | |
520 | slot instruction. */ | |
c906108c SS |
521 | |
522 | struct block * | |
ae767bfb | 523 | get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block) |
c906108c SS |
524 | { |
525 | CORE_ADDR pc; | |
526 | ||
527 | pc = frame->pc; | |
528 | if (frame->next != 0 && frame->next->signal_handler_caller == 0) | |
529 | /* We are not in the innermost frame and we were not interrupted | |
530 | by a signal. We need to subtract one to get the correct block, | |
531 | in case the call instruction was the last instruction of the block. | |
532 | If there are any machines on which the saved pc does not point to | |
533 | after the call insn, we probably want to make frame->pc point after | |
534 | the call insn anyway. */ | |
535 | --pc; | |
ae767bfb JB |
536 | |
537 | if (addr_in_block) | |
538 | *addr_in_block = pc; | |
539 | ||
c906108c SS |
540 | return block_for_pc (pc); |
541 | } | |
542 | ||
543 | struct block * | |
ae767bfb | 544 | get_current_block (CORE_ADDR *addr_in_block) |
c906108c | 545 | { |
ae767bfb JB |
546 | CORE_ADDR pc = read_pc (); |
547 | ||
548 | if (addr_in_block) | |
549 | *addr_in_block = pc; | |
550 | ||
551 | return block_for_pc (pc); | |
c906108c SS |
552 | } |
553 | ||
554 | CORE_ADDR | |
fba45db2 | 555 | get_pc_function_start (CORE_ADDR pc) |
c906108c SS |
556 | { |
557 | register struct block *bl; | |
558 | register struct symbol *symbol; | |
559 | register struct minimal_symbol *msymbol; | |
560 | CORE_ADDR fstart; | |
561 | ||
562 | if ((bl = block_for_pc (pc)) != NULL && | |
563 | (symbol = block_function (bl)) != NULL) | |
564 | { | |
565 | bl = SYMBOL_BLOCK_VALUE (symbol); | |
566 | fstart = BLOCK_START (bl); | |
567 | } | |
568 | else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL) | |
569 | { | |
570 | fstart = SYMBOL_VALUE_ADDRESS (msymbol); | |
571 | } | |
572 | else | |
573 | { | |
574 | fstart = 0; | |
575 | } | |
576 | return (fstart); | |
577 | } | |
578 | ||
579 | /* Return the symbol for the function executing in frame FRAME. */ | |
580 | ||
581 | struct symbol * | |
fba45db2 | 582 | get_frame_function (struct frame_info *frame) |
c906108c | 583 | { |
ae767bfb | 584 | register struct block *bl = get_frame_block (frame, 0); |
c906108c SS |
585 | if (bl == 0) |
586 | return 0; | |
587 | return block_function (bl); | |
588 | } | |
589 | \f | |
590 | ||
591 | /* Return the blockvector immediately containing the innermost lexical block | |
592 | containing the specified pc value and section, or 0 if there is none. | |
593 | PINDEX is a pointer to the index value of the block. If PINDEX | |
594 | is NULL, we don't pass this information back to the caller. */ | |
595 | ||
596 | struct blockvector * | |
fba45db2 KB |
597 | blockvector_for_pc_sect (register CORE_ADDR pc, struct sec *section, |
598 | int *pindex, struct symtab *symtab) | |
c906108c SS |
599 | { |
600 | register struct block *b; | |
601 | register int bot, top, half; | |
602 | struct blockvector *bl; | |
603 | ||
c5aa993b | 604 | if (symtab == 0) /* if no symtab specified by caller */ |
c906108c SS |
605 | { |
606 | /* First search all symtabs for one whose file contains our pc */ | |
607 | if ((symtab = find_pc_sect_symtab (pc, section)) == 0) | |
608 | return 0; | |
609 | } | |
610 | ||
611 | bl = BLOCKVECTOR (symtab); | |
612 | b = BLOCKVECTOR_BLOCK (bl, 0); | |
613 | ||
614 | /* Then search that symtab for the smallest block that wins. */ | |
615 | /* Use binary search to find the last block that starts before PC. */ | |
616 | ||
617 | bot = 0; | |
618 | top = BLOCKVECTOR_NBLOCKS (bl); | |
619 | ||
620 | while (top - bot > 1) | |
621 | { | |
622 | half = (top - bot + 1) >> 1; | |
623 | b = BLOCKVECTOR_BLOCK (bl, bot + half); | |
624 | if (BLOCK_START (b) <= pc) | |
625 | bot += half; | |
626 | else | |
627 | top = bot + half; | |
628 | } | |
629 | ||
630 | /* Now search backward for a block that ends after PC. */ | |
631 | ||
632 | while (bot >= 0) | |
633 | { | |
634 | b = BLOCKVECTOR_BLOCK (bl, bot); | |
43e526b9 | 635 | if (BLOCK_END (b) > pc) |
c906108c SS |
636 | { |
637 | if (pindex) | |
638 | *pindex = bot; | |
639 | return bl; | |
640 | } | |
641 | bot--; | |
642 | } | |
643 | return 0; | |
644 | } | |
645 | ||
646 | /* Return the blockvector immediately containing the innermost lexical block | |
647 | containing the specified pc value, or 0 if there is none. | |
648 | Backward compatibility, no section. */ | |
649 | ||
650 | struct blockvector * | |
fba45db2 | 651 | blockvector_for_pc (register CORE_ADDR pc, int *pindex) |
c906108c SS |
652 | { |
653 | return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc), | |
654 | pindex, NULL); | |
655 | } | |
656 | ||
657 | /* Return the innermost lexical block containing the specified pc value | |
658 | in the specified section, or 0 if there is none. */ | |
659 | ||
660 | struct block * | |
fba45db2 | 661 | block_for_pc_sect (register CORE_ADDR pc, struct sec *section) |
c906108c SS |
662 | { |
663 | register struct blockvector *bl; | |
664 | int index; | |
665 | ||
666 | bl = blockvector_for_pc_sect (pc, section, &index, NULL); | |
667 | if (bl) | |
668 | return BLOCKVECTOR_BLOCK (bl, index); | |
669 | return 0; | |
670 | } | |
671 | ||
672 | /* Return the innermost lexical block containing the specified pc value, | |
673 | or 0 if there is none. Backward compatibility, no section. */ | |
674 | ||
675 | struct block * | |
fba45db2 | 676 | block_for_pc (register CORE_ADDR pc) |
c906108c SS |
677 | { |
678 | return block_for_pc_sect (pc, find_pc_mapped_section (pc)); | |
679 | } | |
680 | ||
681 | /* Return the function containing pc value PC in section SECTION. | |
682 | Returns 0 if function is not known. */ | |
683 | ||
684 | struct symbol * | |
fba45db2 | 685 | find_pc_sect_function (CORE_ADDR pc, struct sec *section) |
c906108c SS |
686 | { |
687 | register struct block *b = block_for_pc_sect (pc, section); | |
688 | if (b == 0) | |
689 | return 0; | |
690 | return block_function (b); | |
691 | } | |
692 | ||
693 | /* Return the function containing pc value PC. | |
694 | Returns 0 if function is not known. Backward compatibility, no section */ | |
695 | ||
696 | struct symbol * | |
fba45db2 | 697 | find_pc_function (CORE_ADDR pc) |
c906108c SS |
698 | { |
699 | return find_pc_sect_function (pc, find_pc_mapped_section (pc)); | |
700 | } | |
701 | ||
702 | /* These variables are used to cache the most recent result | |
703 | * of find_pc_partial_function. */ | |
704 | ||
c5aa993b JM |
705 | static CORE_ADDR cache_pc_function_low = 0; |
706 | static CORE_ADDR cache_pc_function_high = 0; | |
707 | static char *cache_pc_function_name = 0; | |
c906108c SS |
708 | static struct sec *cache_pc_function_section = NULL; |
709 | ||
710 | /* Clear cache, e.g. when symbol table is discarded. */ | |
711 | ||
712 | void | |
fba45db2 | 713 | clear_pc_function_cache (void) |
c906108c SS |
714 | { |
715 | cache_pc_function_low = 0; | |
716 | cache_pc_function_high = 0; | |
c5aa993b | 717 | cache_pc_function_name = (char *) 0; |
c906108c SS |
718 | cache_pc_function_section = NULL; |
719 | } | |
720 | ||
721 | /* Finds the "function" (text symbol) that is smaller than PC but | |
722 | greatest of all of the potential text symbols in SECTION. Sets | |
723 | *NAME and/or *ADDRESS conditionally if that pointer is non-null. | |
724 | If ENDADDR is non-null, then set *ENDADDR to be the end of the | |
725 | function (exclusive), but passing ENDADDR as non-null means that | |
726 | the function might cause symbols to be read. This function either | |
727 | succeeds or fails (not halfway succeeds). If it succeeds, it sets | |
728 | *NAME, *ADDRESS, and *ENDADDR to real information and returns 1. | |
729 | If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and | |
730 | returns 0. */ | |
731 | ||
732 | int | |
fba45db2 KB |
733 | find_pc_sect_partial_function (CORE_ADDR pc, asection *section, char **name, |
734 | CORE_ADDR *address, CORE_ADDR *endaddr) | |
c906108c SS |
735 | { |
736 | struct partial_symtab *pst; | |
c5aa993b | 737 | struct symbol *f; |
c906108c SS |
738 | struct minimal_symbol *msymbol; |
739 | struct partial_symbol *psb; | |
c5aa993b | 740 | struct obj_section *osect; |
c906108c SS |
741 | int i; |
742 | CORE_ADDR mapped_pc; | |
743 | ||
744 | mapped_pc = overlay_mapped_address (pc, section); | |
745 | ||
c5aa993b | 746 | if (mapped_pc >= cache_pc_function_low && |
c906108c SS |
747 | mapped_pc < cache_pc_function_high && |
748 | section == cache_pc_function_section) | |
749 | goto return_cached_value; | |
750 | ||
751 | /* If sigtramp is in the u area, it counts as a function (especially | |
752 | important for step_1). */ | |
753 | #if defined SIGTRAMP_START | |
c5aa993b | 754 | if (IN_SIGTRAMP (mapped_pc, (char *) NULL)) |
c906108c | 755 | { |
c5aa993b JM |
756 | cache_pc_function_low = SIGTRAMP_START (mapped_pc); |
757 | cache_pc_function_high = SIGTRAMP_END (mapped_pc); | |
758 | cache_pc_function_name = "<sigtramp>"; | |
c906108c SS |
759 | cache_pc_function_section = section; |
760 | goto return_cached_value; | |
761 | } | |
762 | #endif | |
763 | ||
764 | msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section); | |
765 | pst = find_pc_sect_psymtab (mapped_pc, section); | |
766 | if (pst) | |
767 | { | |
768 | /* Need to read the symbols to get a good value for the end address. */ | |
769 | if (endaddr != NULL && !pst->readin) | |
770 | { | |
771 | /* Need to get the terminal in case symbol-reading produces | |
772 | output. */ | |
773 | target_terminal_ours_for_output (); | |
774 | PSYMTAB_TO_SYMTAB (pst); | |
775 | } | |
776 | ||
777 | if (pst->readin) | |
778 | { | |
779 | /* Checking whether the msymbol has a larger value is for the | |
780 | "pathological" case mentioned in print_frame_info. */ | |
781 | f = find_pc_sect_function (mapped_pc, section); | |
782 | if (f != NULL | |
783 | && (msymbol == NULL | |
784 | || (BLOCK_START (SYMBOL_BLOCK_VALUE (f)) | |
785 | >= SYMBOL_VALUE_ADDRESS (msymbol)))) | |
786 | { | |
c5aa993b JM |
787 | cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f)); |
788 | cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f)); | |
789 | cache_pc_function_name = SYMBOL_NAME (f); | |
c906108c SS |
790 | cache_pc_function_section = section; |
791 | goto return_cached_value; | |
792 | } | |
793 | } | |
794 | else | |
795 | { | |
796 | /* Now that static symbols go in the minimal symbol table, perhaps | |
797 | we could just ignore the partial symbols. But at least for now | |
798 | we use the partial or minimal symbol, whichever is larger. */ | |
799 | psb = find_pc_sect_psymbol (pst, mapped_pc, section); | |
800 | ||
801 | if (psb | |
802 | && (msymbol == NULL || | |
803 | (SYMBOL_VALUE_ADDRESS (psb) | |
804 | >= SYMBOL_VALUE_ADDRESS (msymbol)))) | |
805 | { | |
806 | /* This case isn't being cached currently. */ | |
807 | if (address) | |
808 | *address = SYMBOL_VALUE_ADDRESS (psb); | |
809 | if (name) | |
810 | *name = SYMBOL_NAME (psb); | |
811 | /* endaddr non-NULL can't happen here. */ | |
812 | return 1; | |
813 | } | |
814 | } | |
815 | } | |
816 | ||
817 | /* Not in the normal symbol tables, see if the pc is in a known section. | |
818 | If it's not, then give up. This ensures that anything beyond the end | |
819 | of the text seg doesn't appear to be part of the last function in the | |
820 | text segment. */ | |
821 | ||
822 | osect = find_pc_sect_section (mapped_pc, section); | |
823 | ||
824 | if (!osect) | |
825 | msymbol = NULL; | |
826 | ||
827 | /* Must be in the minimal symbol table. */ | |
828 | if (msymbol == NULL) | |
829 | { | |
830 | /* No available symbol. */ | |
831 | if (name != NULL) | |
832 | *name = 0; | |
833 | if (address != NULL) | |
834 | *address = 0; | |
835 | if (endaddr != NULL) | |
836 | *endaddr = 0; | |
837 | return 0; | |
838 | } | |
839 | ||
c5aa993b JM |
840 | cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol); |
841 | cache_pc_function_name = SYMBOL_NAME (msymbol); | |
c906108c SS |
842 | cache_pc_function_section = section; |
843 | ||
844 | /* Use the lesser of the next minimal symbol in the same section, or | |
845 | the end of the section, as the end of the function. */ | |
c5aa993b | 846 | |
c906108c SS |
847 | /* Step over other symbols at this same address, and symbols in |
848 | other sections, to find the next symbol in this section with | |
849 | a different address. */ | |
850 | ||
c5aa993b | 851 | for (i = 1; SYMBOL_NAME (msymbol + i) != NULL; i++) |
c906108c | 852 | { |
c5aa993b JM |
853 | if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol) |
854 | && SYMBOL_BFD_SECTION (msymbol + i) == SYMBOL_BFD_SECTION (msymbol)) | |
c906108c SS |
855 | break; |
856 | } | |
857 | ||
858 | if (SYMBOL_NAME (msymbol + i) != NULL | |
859 | && SYMBOL_VALUE_ADDRESS (msymbol + i) < osect->endaddr) | |
860 | cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i); | |
861 | else | |
862 | /* We got the start address from the last msymbol in the objfile. | |
863 | So the end address is the end of the section. */ | |
864 | cache_pc_function_high = osect->endaddr; | |
865 | ||
c5aa993b | 866 | return_cached_value: |
c906108c SS |
867 | |
868 | if (address) | |
869 | { | |
870 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 871 | *address = overlay_unmapped_address (cache_pc_function_low, section); |
c906108c | 872 | else |
c5aa993b | 873 | *address = cache_pc_function_low; |
c906108c | 874 | } |
c5aa993b | 875 | |
c906108c SS |
876 | if (name) |
877 | *name = cache_pc_function_name; | |
878 | ||
879 | if (endaddr) | |
880 | { | |
881 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 882 | { |
c906108c SS |
883 | /* Because the high address is actually beyond the end of |
884 | the function (and therefore possibly beyond the end of | |
885 | the overlay), we must actually convert (high - 1) | |
886 | and then add one to that. */ | |
887 | ||
c5aa993b | 888 | *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1, |
c906108c | 889 | section); |
c5aa993b | 890 | } |
c906108c | 891 | else |
c5aa993b | 892 | *endaddr = cache_pc_function_high; |
c906108c SS |
893 | } |
894 | ||
895 | return 1; | |
896 | } | |
897 | ||
898 | /* Backward compatibility, no section argument */ | |
899 | ||
900 | int | |
fba45db2 KB |
901 | find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address, |
902 | CORE_ADDR *endaddr) | |
c906108c | 903 | { |
c5aa993b | 904 | asection *section; |
c906108c SS |
905 | |
906 | section = find_pc_overlay (pc); | |
907 | return find_pc_sect_partial_function (pc, section, name, address, endaddr); | |
908 | } | |
909 | ||
910 | /* Return the innermost stack frame executing inside of BLOCK, | |
911 | or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */ | |
912 | ||
913 | struct frame_info * | |
fba45db2 | 914 | block_innermost_frame (struct block *block) |
c906108c SS |
915 | { |
916 | struct frame_info *frame; | |
917 | register CORE_ADDR start; | |
918 | register CORE_ADDR end; | |
919 | ||
920 | if (block == NULL) | |
921 | return NULL; | |
922 | ||
923 | start = BLOCK_START (block); | |
924 | end = BLOCK_END (block); | |
925 | ||
926 | frame = NULL; | |
927 | while (1) | |
928 | { | |
929 | frame = get_prev_frame (frame); | |
930 | if (frame == NULL) | |
931 | return NULL; | |
932 | if (frame->pc >= start && frame->pc < end) | |
933 | return frame; | |
934 | } | |
935 | } | |
936 | ||
937 | /* Return the full FRAME which corresponds to the given CORE_ADDR | |
938 | or NULL if no FRAME on the chain corresponds to CORE_ADDR. */ | |
939 | ||
940 | struct frame_info * | |
fba45db2 | 941 | find_frame_addr_in_frame_chain (CORE_ADDR frame_addr) |
c906108c SS |
942 | { |
943 | struct frame_info *frame = NULL; | |
944 | ||
c5aa993b | 945 | if (frame_addr == (CORE_ADDR) 0) |
c906108c SS |
946 | return NULL; |
947 | ||
948 | while (1) | |
949 | { | |
950 | frame = get_prev_frame (frame); | |
951 | if (frame == NULL) | |
952 | return NULL; | |
953 | if (FRAME_FP (frame) == frame_addr) | |
954 | return frame; | |
955 | } | |
956 | } | |
957 | ||
958 | #ifdef SIGCONTEXT_PC_OFFSET | |
959 | /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */ | |
960 | ||
961 | CORE_ADDR | |
fba45db2 | 962 | sigtramp_saved_pc (struct frame_info *frame) |
c906108c SS |
963 | { |
964 | CORE_ADDR sigcontext_addr; | |
35fc8285 | 965 | char *buf; |
c906108c SS |
966 | int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT; |
967 | int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT; | |
968 | ||
35fc8285 | 969 | buf = alloca (ptrbytes); |
c906108c SS |
970 | /* Get sigcontext address, it is the third parameter on the stack. */ |
971 | if (frame->next) | |
972 | sigcontext_addr = read_memory_integer (FRAME_ARGS_ADDRESS (frame->next) | |
973 | + FRAME_ARGS_SKIP | |
974 | + sigcontext_offs, | |
975 | ptrbytes); | |
976 | else | |
977 | sigcontext_addr = read_memory_integer (read_register (SP_REGNUM) | |
c5aa993b | 978 | + sigcontext_offs, |
c906108c SS |
979 | ptrbytes); |
980 | ||
981 | /* Don't cause a memory_error when accessing sigcontext in case the stack | |
982 | layout has changed or the stack is corrupt. */ | |
983 | target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes); | |
984 | return extract_unsigned_integer (buf, ptrbytes); | |
985 | } | |
986 | #endif /* SIGCONTEXT_PC_OFFSET */ | |
987 | ||
7a292a7a SS |
988 | |
989 | /* Are we in a call dummy? The code below which allows DECR_PC_AFTER_BREAK | |
990 | below is for infrun.c, which may give the macro a pc without that | |
991 | subtracted out. */ | |
992 | ||
993 | extern CORE_ADDR text_end; | |
994 | ||
995 | int | |
fba45db2 KB |
996 | pc_in_call_dummy_before_text_end (CORE_ADDR pc, CORE_ADDR sp, |
997 | CORE_ADDR frame_address) | |
7a292a7a SS |
998 | { |
999 | return ((pc) >= text_end - CALL_DUMMY_LENGTH | |
1000 | && (pc) <= text_end + DECR_PC_AFTER_BREAK); | |
1001 | } | |
1002 | ||
1003 | int | |
fba45db2 KB |
1004 | pc_in_call_dummy_after_text_end (CORE_ADDR pc, CORE_ADDR sp, |
1005 | CORE_ADDR frame_address) | |
7a292a7a SS |
1006 | { |
1007 | return ((pc) >= text_end | |
1008 | && (pc) <= text_end + CALL_DUMMY_LENGTH + DECR_PC_AFTER_BREAK); | |
1009 | } | |
1010 | ||
1011 | /* Is the PC in a call dummy? SP and FRAME_ADDRESS are the bottom and | |
1012 | top of the stack frame which we are checking, where "bottom" and | |
1013 | "top" refer to some section of memory which contains the code for | |
1014 | the call dummy. Calls to this macro assume that the contents of | |
1015 | SP_REGNUM and FP_REGNUM (or the saved values thereof), respectively, | |
1016 | are the things to pass. | |
1017 | ||
1018 | This won't work on the 29k, where SP_REGNUM and FP_REGNUM don't | |
1019 | have that meaning, but the 29k doesn't use ON_STACK. This could be | |
1020 | fixed by generalizing this scheme, perhaps by passing in a frame | |
1021 | and adding a few fields, at least on machines which need them for | |
1022 | PC_IN_CALL_DUMMY. | |
1023 | ||
1024 | Something simpler, like checking for the stack segment, doesn't work, | |
1025 | since various programs (threads implementations, gcc nested function | |
1026 | stubs, etc) may either allocate stack frames in another segment, or | |
1027 | allocate other kinds of code on the stack. */ | |
1028 | ||
1029 | int | |
fba45db2 | 1030 | pc_in_call_dummy_on_stack (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR frame_address) |
7a292a7a SS |
1031 | { |
1032 | return (INNER_THAN ((sp), (pc)) | |
1033 | && (frame_address != 0) | |
1034 | && INNER_THAN ((pc), (frame_address))); | |
1035 | } | |
1036 | ||
1037 | int | |
fba45db2 KB |
1038 | pc_in_call_dummy_at_entry_point (CORE_ADDR pc, CORE_ADDR sp, |
1039 | CORE_ADDR frame_address) | |
7a292a7a SS |
1040 | { |
1041 | return ((pc) >= CALL_DUMMY_ADDRESS () | |
1042 | && (pc) <= (CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK)); | |
1043 | } | |
1044 | ||
c906108c SS |
1045 | |
1046 | /* | |
1047 | * GENERIC DUMMY FRAMES | |
1048 | * | |
1049 | * The following code serves to maintain the dummy stack frames for | |
1050 | * inferior function calls (ie. when gdb calls into the inferior via | |
1051 | * call_function_by_hand). This code saves the machine state before | |
b7d6b182 | 1052 | * the call in host memory, so we must maintain an independent stack |
c906108c SS |
1053 | * and keep it consistant etc. I am attempting to make this code |
1054 | * generic enough to be used by many targets. | |
1055 | * | |
1056 | * The cheapest and most generic way to do CALL_DUMMY on a new target | |
1057 | * is probably to define CALL_DUMMY to be empty, CALL_DUMMY_LENGTH to | |
1058 | * zero, and CALL_DUMMY_LOCATION to AT_ENTRY. Then you must remember | |
1059 | * to define PUSH_RETURN_ADDRESS, because no call instruction will be | |
1060 | * being executed by the target. Also FRAME_CHAIN_VALID as | |
c4093a6a | 1061 | * generic_{file,func}_frame_chain_valid and FIX_CALL_DUMMY as |
cce74817 | 1062 | * generic_fix_call_dummy. */ |
c906108c | 1063 | |
7a292a7a SS |
1064 | /* Dummy frame. This saves the processor state just prior to setting |
1065 | up the inferior function call. Older targets save the registers | |
72229eb7 | 1066 | on the target stack (but that really slows down function calls). */ |
7a292a7a SS |
1067 | |
1068 | struct dummy_frame | |
1069 | { | |
1070 | struct dummy_frame *next; | |
1071 | ||
1072 | CORE_ADDR pc; | |
1073 | CORE_ADDR fp; | |
1074 | CORE_ADDR sp; | |
43ff13b4 | 1075 | CORE_ADDR top; |
7a292a7a SS |
1076 | char *registers; |
1077 | }; | |
1078 | ||
c906108c SS |
1079 | static struct dummy_frame *dummy_frame_stack = NULL; |
1080 | ||
1081 | /* Function: find_dummy_frame(pc, fp, sp) | |
1082 | Search the stack of dummy frames for one matching the given PC, FP and SP. | |
1083 | This is the work-horse for pc_in_call_dummy and read_register_dummy */ | |
1084 | ||
c5aa993b | 1085 | char * |
fba45db2 | 1086 | generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp) |
c906108c | 1087 | { |
c5aa993b | 1088 | struct dummy_frame *dummyframe; |
c906108c SS |
1089 | |
1090 | if (pc != entry_point_address ()) | |
1091 | return 0; | |
1092 | ||
1093 | for (dummyframe = dummy_frame_stack; dummyframe != NULL; | |
1094 | dummyframe = dummyframe->next) | |
43ff13b4 JM |
1095 | if (fp == dummyframe->fp |
1096 | || fp == dummyframe->sp | |
1097 | || fp == dummyframe->top) | |
c906108c | 1098 | /* The frame in question lies between the saved fp and sp, inclusive */ |
7a292a7a | 1099 | return dummyframe->registers; |
c906108c SS |
1100 | |
1101 | return 0; | |
1102 | } | |
1103 | ||
1104 | /* Function: pc_in_call_dummy (pc, fp) | |
1105 | Return true if this is a dummy frame created by gdb for an inferior call */ | |
1106 | ||
1107 | int | |
fba45db2 | 1108 | generic_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp) |
c906108c SS |
1109 | { |
1110 | /* if find_dummy_frame succeeds, then PC is in a call dummy */ | |
7a292a7a SS |
1111 | /* Note: SP and not FP is passed on. */ |
1112 | return (generic_find_dummy_frame (pc, sp) != 0); | |
c906108c SS |
1113 | } |
1114 | ||
1115 | /* Function: read_register_dummy | |
1116 | Find a saved register from before GDB calls a function in the inferior */ | |
1117 | ||
1118 | CORE_ADDR | |
fba45db2 | 1119 | generic_read_register_dummy (CORE_ADDR pc, CORE_ADDR fp, int regno) |
c906108c SS |
1120 | { |
1121 | char *dummy_regs = generic_find_dummy_frame (pc, fp); | |
1122 | ||
1123 | if (dummy_regs) | |
1124 | return extract_address (&dummy_regs[REGISTER_BYTE (regno)], | |
c5aa993b | 1125 | REGISTER_RAW_SIZE (regno)); |
c906108c SS |
1126 | else |
1127 | return 0; | |
1128 | } | |
1129 | ||
1130 | /* Save all the registers on the dummy frame stack. Most ports save the | |
1131 | registers on the target stack. This results in lots of unnecessary memory | |
1132 | references, which are slow when debugging via a serial line. Instead, we | |
1133 | save all the registers internally, and never write them to the stack. The | |
1134 | registers get restored when the called function returns to the entry point, | |
1135 | where a breakpoint is laying in wait. */ | |
1136 | ||
1137 | void | |
fba45db2 | 1138 | generic_push_dummy_frame (void) |
c906108c SS |
1139 | { |
1140 | struct dummy_frame *dummy_frame; | |
1141 | CORE_ADDR fp = (get_current_frame ())->frame; | |
1142 | ||
1143 | /* check to see if there are stale dummy frames, | |
1144 | perhaps left over from when a longjump took us out of a | |
1145 | function that was called by the debugger */ | |
1146 | ||
1147 | dummy_frame = dummy_frame_stack; | |
1148 | while (dummy_frame) | |
1149 | if (INNER_THAN (dummy_frame->fp, fp)) /* stale -- destroy! */ | |
1150 | { | |
1151 | dummy_frame_stack = dummy_frame->next; | |
b8c9b27d KB |
1152 | xfree (dummy_frame->registers); |
1153 | xfree (dummy_frame); | |
c906108c SS |
1154 | dummy_frame = dummy_frame_stack; |
1155 | } | |
1156 | else | |
1157 | dummy_frame = dummy_frame->next; | |
1158 | ||
1159 | dummy_frame = xmalloc (sizeof (struct dummy_frame)); | |
7a292a7a SS |
1160 | dummy_frame->registers = xmalloc (REGISTER_BYTES); |
1161 | ||
4478b372 JB |
1162 | dummy_frame->pc = read_pc (); |
1163 | dummy_frame->sp = read_sp (); | |
c5aa993b JM |
1164 | dummy_frame->top = dummy_frame->sp; |
1165 | dummy_frame->fp = fp; | |
7a292a7a | 1166 | read_register_bytes (0, dummy_frame->registers, REGISTER_BYTES); |
c906108c SS |
1167 | dummy_frame->next = dummy_frame_stack; |
1168 | dummy_frame_stack = dummy_frame; | |
1169 | } | |
1170 | ||
43ff13b4 | 1171 | void |
fba45db2 | 1172 | generic_save_dummy_frame_tos (CORE_ADDR sp) |
43ff13b4 JM |
1173 | { |
1174 | dummy_frame_stack->top = sp; | |
1175 | } | |
1176 | ||
ed9a39eb | 1177 | /* Restore the machine state from either the saved dummy stack or a |
c906108c SS |
1178 | real stack frame. */ |
1179 | ||
1180 | void | |
ed9a39eb | 1181 | generic_pop_current_frame (void (*popper) (struct frame_info * frame)) |
c906108c SS |
1182 | { |
1183 | struct frame_info *frame = get_current_frame (); | |
ed9a39eb | 1184 | |
c5aa993b | 1185 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
c906108c SS |
1186 | generic_pop_dummy_frame (); |
1187 | else | |
ed9a39eb | 1188 | (*popper) (frame); |
c906108c SS |
1189 | } |
1190 | ||
1191 | /* Function: pop_dummy_frame | |
1192 | Restore the machine state from a saved dummy stack frame. */ | |
1193 | ||
1194 | void | |
fba45db2 | 1195 | generic_pop_dummy_frame (void) |
c906108c SS |
1196 | { |
1197 | struct dummy_frame *dummy_frame = dummy_frame_stack; | |
1198 | ||
1199 | /* FIXME: what if the first frame isn't the right one, eg.. | |
1200 | because one call-by-hand function has done a longjmp into another one? */ | |
1201 | ||
1202 | if (!dummy_frame) | |
1203 | error ("Can't pop dummy frame!"); | |
1204 | dummy_frame_stack = dummy_frame->next; | |
7a292a7a | 1205 | write_register_bytes (0, dummy_frame->registers, REGISTER_BYTES); |
c906108c | 1206 | flush_cached_frames (); |
7a292a7a | 1207 | |
b8c9b27d KB |
1208 | xfree (dummy_frame->registers); |
1209 | xfree (dummy_frame); | |
c906108c SS |
1210 | } |
1211 | ||
1212 | /* Function: frame_chain_valid | |
1213 | Returns true for a user frame or a call_function_by_hand dummy frame, | |
1214 | and false for the CRT0 start-up frame. Purpose is to terminate backtrace */ | |
c5aa993b | 1215 | |
c906108c | 1216 | int |
fba45db2 | 1217 | generic_file_frame_chain_valid (CORE_ADDR fp, struct frame_info *fi) |
c906108c | 1218 | { |
c5aa993b JM |
1219 | if (PC_IN_CALL_DUMMY (FRAME_SAVED_PC (fi), fp, fp)) |
1220 | return 1; /* don't prune CALL_DUMMY frames */ | |
1221 | else /* fall back to default algorithm (see frame.h) */ | |
c906108c SS |
1222 | return (fp != 0 |
1223 | && (INNER_THAN (fi->frame, fp) || fi->frame == fp) | |
c5aa993b | 1224 | && !inside_entry_file (FRAME_SAVED_PC (fi))); |
c906108c | 1225 | } |
c5aa993b | 1226 | |
c4093a6a | 1227 | int |
fba45db2 | 1228 | generic_func_frame_chain_valid (CORE_ADDR fp, struct frame_info *fi) |
c4093a6a JM |
1229 | { |
1230 | if (PC_IN_CALL_DUMMY ((fi)->pc, fp, fp)) | |
1231 | return 1; /* don't prune CALL_DUMMY frames */ | |
1232 | else /* fall back to default algorithm (see frame.h) */ | |
1233 | return (fp != 0 | |
1234 | && (INNER_THAN (fi->frame, fp) || fi->frame == fp) | |
1235 | && !inside_main_func ((fi)->pc) | |
1236 | && !inside_entry_func ((fi)->pc)); | |
1237 | } | |
1238 | ||
cce74817 | 1239 | /* Function: fix_call_dummy |
c570663e | 1240 | Stub function. Generic dummy frames typically do not need to fix |
cce74817 JM |
1241 | the frame being created */ |
1242 | ||
1243 | void | |
fba45db2 KB |
1244 | generic_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs, |
1245 | struct value **args, struct type *type, int gcc_p) | |
cce74817 JM |
1246 | { |
1247 | return; | |
1248 | } | |
1249 | ||
c906108c SS |
1250 | /* Function: get_saved_register |
1251 | Find register number REGNUM relative to FRAME and put its (raw, | |
1252 | target format) contents in *RAW_BUFFER. | |
1253 | ||
1254 | Set *OPTIMIZED if the variable was optimized out (and thus can't be | |
1255 | fetched). Note that this is never set to anything other than zero | |
1256 | in this implementation. | |
1257 | ||
1258 | Set *LVAL to lval_memory, lval_register, or not_lval, depending on | |
1259 | whether the value was fetched from memory, from a register, or in a | |
1260 | strange and non-modifiable way (e.g. a frame pointer which was | |
1261 | calculated rather than fetched). We will use not_lval for values | |
1262 | fetched from generic dummy frames. | |
1263 | ||
7036d6ce | 1264 | Set *ADDRP to the address, either in memory or as a REGISTER_BYTE |
c906108c SS |
1265 | offset into the registers array. If the value is stored in a dummy |
1266 | frame, set *ADDRP to zero. | |
1267 | ||
1268 | To use this implementation, define a function called | |
1269 | "get_saved_register" in your target code, which simply passes all | |
1270 | of its arguments to this function. | |
1271 | ||
1272 | The argument RAW_BUFFER must point to aligned memory. */ | |
1273 | ||
1274 | void | |
fba45db2 KB |
1275 | generic_get_saved_register (char *raw_buffer, int *optimized, CORE_ADDR *addrp, |
1276 | struct frame_info *frame, int regnum, | |
1277 | enum lval_type *lval) | |
c906108c SS |
1278 | { |
1279 | if (!target_has_registers) | |
1280 | error ("No registers."); | |
1281 | ||
1282 | /* Normal systems don't optimize out things with register numbers. */ | |
1283 | if (optimized != NULL) | |
1284 | *optimized = 0; | |
1285 | ||
c5aa993b | 1286 | if (addrp) /* default assumption: not found in memory */ |
c906108c SS |
1287 | *addrp = 0; |
1288 | ||
1289 | /* Note: since the current frame's registers could only have been | |
1290 | saved by frames INTERIOR TO the current frame, we skip examining | |
1291 | the current frame itself: otherwise, we would be getting the | |
1292 | previous frame's registers which were saved by the current frame. */ | |
1293 | ||
1294 | while (frame && ((frame = frame->next) != NULL)) | |
1295 | { | |
1296 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) | |
1297 | { | |
c5aa993b | 1298 | if (lval) /* found it in a CALL_DUMMY frame */ |
c906108c SS |
1299 | *lval = not_lval; |
1300 | if (raw_buffer) | |
c5aa993b JM |
1301 | memcpy (raw_buffer, |
1302 | generic_find_dummy_frame (frame->pc, frame->frame) + | |
c906108c SS |
1303 | REGISTER_BYTE (regnum), |
1304 | REGISTER_RAW_SIZE (regnum)); | |
c5aa993b | 1305 | return; |
c906108c SS |
1306 | } |
1307 | ||
1308 | FRAME_INIT_SAVED_REGS (frame); | |
1309 | if (frame->saved_regs != NULL | |
1310 | && frame->saved_regs[regnum] != 0) | |
1311 | { | |
c5aa993b | 1312 | if (lval) /* found it saved on the stack */ |
c906108c SS |
1313 | *lval = lval_memory; |
1314 | if (regnum == SP_REGNUM) | |
1315 | { | |
c5aa993b JM |
1316 | if (raw_buffer) /* SP register treated specially */ |
1317 | store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), | |
c906108c SS |
1318 | frame->saved_regs[regnum]); |
1319 | } | |
1320 | else | |
1321 | { | |
c5aa993b | 1322 | if (addrp) /* any other register */ |
c906108c SS |
1323 | *addrp = frame->saved_regs[regnum]; |
1324 | if (raw_buffer) | |
c5aa993b | 1325 | read_memory (frame->saved_regs[regnum], raw_buffer, |
c906108c SS |
1326 | REGISTER_RAW_SIZE (regnum)); |
1327 | } | |
1328 | return; | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | /* If we get thru the loop to this point, it means the register was | |
1333 | not saved in any frame. Return the actual live-register value. */ | |
1334 | ||
c5aa993b | 1335 | if (lval) /* found it in a live register */ |
c906108c SS |
1336 | *lval = lval_register; |
1337 | if (addrp) | |
1338 | *addrp = REGISTER_BYTE (regnum); | |
1339 | if (raw_buffer) | |
1340 | read_register_gen (regnum, raw_buffer); | |
1341 | } | |
c906108c SS |
1342 | |
1343 | void | |
53a5351d | 1344 | _initialize_blockframe (void) |
c906108c SS |
1345 | { |
1346 | obstack_init (&frame_cache_obstack); | |
1347 | } |