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