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