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