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