* frame.h, blockframe.c, stack.c, a29k-tdep.c,
[binutils-gdb.git] / gdb / blockframe.c
1 /* Get info from stack frames;
2 convert between frames, blocks, functions and pc values.
3 Copyright 1986, 1987, 1988, 1989, 1991 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "bfd.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "frame.h"
27 #include "gdbcore.h"
28 #include "value.h" /* for read_register */
29 #include "target.h" /* for target_has_stack */
30 #include "inferior.h" /* for read_pc */
31
32 /* Is ADDR inside the startup file? Note that if your machine
33 has a way to detect the bottom of the stack, there is no need
34 to call this function from FRAME_CHAIN_VALID; the reason for
35 doing so is that some machines have no way of detecting bottom
36 of stack.
37
38 A PC of zero is always considered to be the bottom of the stack. */
39
40 int
41 inside_entry_file (addr)
42 CORE_ADDR addr;
43 {
44 if (addr == 0)
45 return 1;
46 if (symfile_objfile == 0)
47 return 0;
48 return (addr >= symfile_objfile -> ei.entry_file_lowpc &&
49 addr < symfile_objfile -> ei.entry_file_highpc);
50 }
51
52 /* Test a specified PC value to see if it is in the range of addresses
53 that correspond to the main() function. See comments above for why
54 we might want to do this.
55
56 Typically called from FRAME_CHAIN_VALID.
57
58 A PC of zero is always considered to be the bottom of the stack. */
59
60 int
61 inside_main_func (pc)
62 CORE_ADDR pc;
63 {
64 if (pc == 0)
65 return 1;
66 if (symfile_objfile == 0)
67 return 0;
68 return (symfile_objfile -> ei.main_func_lowpc <= pc &&
69 symfile_objfile -> ei.main_func_highpc > pc);
70 }
71
72 /* Test a specified PC value to see if it is in the range of addresses
73 that correspond to the process entry point function. See comments
74 in objfiles.h for why we might want to do this.
75
76 Typically called from FRAME_CHAIN_VALID.
77
78 A PC of zero is always considered to be the bottom of the stack. */
79
80 int
81 inside_entry_func (pc)
82 CORE_ADDR pc;
83 {
84 if (pc == 0)
85 return 1;
86 if (symfile_objfile == 0)
87 return 0;
88 return (symfile_objfile -> ei.entry_func_lowpc <= pc &&
89 symfile_objfile -> ei.entry_func_highpc > pc);
90 }
91
92 /* Address of innermost stack frame (contents of FP register) */
93
94 static FRAME current_frame;
95
96 /*
97 * Cache for frame addresses already read by gdb. Valid only while
98 * inferior is stopped. Control variables for the frame cache should
99 * be local to this module.
100 */
101 struct obstack frame_cache_obstack;
102
103 /* Return the innermost (currently executing) stack frame. */
104
105 FRAME
106 get_current_frame ()
107 {
108 /* We assume its address is kept in a general register;
109 param.h says which register. */
110
111 return current_frame;
112 }
113
114 void
115 set_current_frame (frame)
116 FRAME frame;
117 {
118 current_frame = frame;
119 }
120
121 FRAME
122 create_new_frame (addr, pc)
123 FRAME_ADDR addr;
124 CORE_ADDR pc;
125 {
126 struct frame_info *fci; /* Same type as FRAME */
127
128 fci = (struct frame_info *)
129 obstack_alloc (&frame_cache_obstack,
130 sizeof (struct frame_info));
131
132 /* Arbitrary frame */
133 fci->next = (struct frame_info *) 0;
134 fci->prev = (struct frame_info *) 0;
135 fci->frame = addr;
136 fci->pc = pc;
137 fci->signal_handler_caller = IN_SIGTRAMP (fci->pc, (char *)NULL);
138
139 #ifdef INIT_EXTRA_FRAME_INFO
140 INIT_EXTRA_FRAME_INFO (0, fci);
141 #endif
142
143 return fci;
144 }
145
146 /* Return the frame that called FRAME.
147 If FRAME is the original frame (it has no caller), return 0. */
148
149 FRAME
150 get_prev_frame (frame)
151 FRAME frame;
152 {
153 /* We're allowed to know that FRAME and "struct frame_info *" are
154 the same */
155 return get_prev_frame_info (frame);
156 }
157
158 /* Return the frame that FRAME calls (0 if FRAME is the innermost
159 frame). */
160
161 FRAME
162 get_next_frame (frame)
163 FRAME frame;
164 {
165 /* We're allowed to know that FRAME and "struct frame_info *" are
166 the same */
167 return frame->next;
168 }
169
170 /*
171 * Flush the entire frame cache.
172 */
173 void
174 flush_cached_frames ()
175 {
176 /* Since we can't really be sure what the first object allocated was */
177 obstack_free (&frame_cache_obstack, 0);
178 obstack_init (&frame_cache_obstack);
179
180 current_frame = (struct frame_info *) 0; /* Invalidate cache */
181 }
182
183 /* Flush the frame cache, and start a new one if necessary. */
184 void
185 reinit_frame_cache ()
186 {
187 FRAME fr = current_frame;
188 flush_cached_frames ();
189 if (fr)
190 set_current_frame ( create_new_frame (read_fp (), read_pc ()));
191 }
192
193 /* Return a structure containing various interesting information
194 about a specified stack frame. */
195 /* How do I justify including this function? Well, the FRAME
196 identifier format has gone through several changes recently, and
197 it's not completely inconceivable that it could happen again. If
198 it does, have this routine around will help */
199
200 struct frame_info *
201 get_frame_info (frame)
202 FRAME frame;
203 {
204 return frame;
205 }
206
207 /* If a machine allows frameless functions, it should define a macro
208 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
209 frame_info for the frame, and FRAMELESS should be set to nonzero
210 if it represents a frameless function invocation. */
211
212 /* Return nonzero if the function for this frame lacks a prologue. Many
213 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
214 function. */
215
216 int
217 frameless_look_for_prologue (frame)
218 FRAME frame;
219 {
220 CORE_ADDR func_start, after_prologue;
221 func_start = (get_pc_function_start (frame->pc) +
222 FUNCTION_START_OFFSET);
223 if (func_start)
224 {
225 after_prologue = func_start;
226 #ifdef SKIP_PROLOGUE_FRAMELESS_P
227 /* This is faster, since only care whether there *is* a prologue,
228 not how long it is. */
229 SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
230 #else
231 SKIP_PROLOGUE (after_prologue);
232 #endif
233 return after_prologue == func_start;
234 }
235 else
236 /* If we can't find the start of the function, we don't really
237 know whether the function is frameless, but we should be able
238 to get a reasonable (i.e. best we can do under the
239 circumstances) backtrace by saying that it isn't. */
240 return 0;
241 }
242
243 /* Default a few macros that people seldom redefine. */
244
245 #if !defined (INIT_FRAME_PC)
246 #define INIT_FRAME_PC(fromleaf, prev) \
247 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
248 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
249 #endif
250
251 #ifndef FRAME_CHAIN_COMBINE
252 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
253 #endif
254
255 /* Return a structure containing various interesting information
256 about the frame that called NEXT_FRAME. Returns NULL
257 if there is no such frame. */
258
259 struct frame_info *
260 get_prev_frame_info (next_frame)
261 FRAME next_frame;
262 {
263 FRAME_ADDR address;
264 struct frame_info *prev;
265 int fromleaf = 0;
266
267 /* If the requested entry is in the cache, return it.
268 Otherwise, figure out what the address should be for the entry
269 we're about to add to the cache. */
270
271 if (!next_frame)
272 {
273 if (!current_frame)
274 {
275 error ("You haven't set up a process's stack to examine.");
276 }
277
278 return current_frame;
279 }
280
281 /* If we have the prev one, return it */
282 if (next_frame->prev)
283 return next_frame->prev;
284
285 /* On some machines it is possible to call a function without
286 setting up a stack frame for it. On these machines, we
287 define this macro to take two args; a frameinfo pointer
288 identifying a frame and a variable to set or clear if it is
289 or isn't leafless. */
290 #ifdef FRAMELESS_FUNCTION_INVOCATION
291 /* Still don't want to worry about this except on the innermost
292 frame. This macro will set FROMLEAF if NEXT_FRAME is a
293 frameless function invocation. */
294 if (!(next_frame->next))
295 {
296 FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
297 if (fromleaf)
298 address = next_frame->frame;
299 }
300 #endif
301
302 if (!fromleaf)
303 {
304 /* Two macros defined in tm.h specify the machine-dependent
305 actions to be performed here.
306 First, get the frame's chain-pointer.
307 If that is zero, the frame is the outermost frame or a leaf
308 called by the outermost frame. This means that if start
309 calls main without a frame, we'll return 0 (which is fine
310 anyway).
311
312 Nope; there's a problem. This also returns when the current
313 routine is a leaf of main. This is unacceptable. We move
314 this to after the ffi test; I'd rather have backtraces from
315 start go curfluy than have an abort called from main not show
316 main. */
317 address = FRAME_CHAIN (next_frame);
318 if (!FRAME_CHAIN_VALID (address, next_frame))
319 return 0;
320 address = FRAME_CHAIN_COMBINE (address, next_frame);
321 }
322 if (address == 0)
323 return 0;
324
325 prev = (struct frame_info *)
326 obstack_alloc (&frame_cache_obstack,
327 sizeof (struct frame_info));
328
329 if (next_frame)
330 next_frame->prev = prev;
331 prev->next = next_frame;
332 prev->prev = (struct frame_info *) 0;
333 prev->frame = address;
334 prev->signal_handler_caller = 0;
335
336 /* This change should not be needed, FIXME! We should
337 determine whether any targets *need* INIT_FRAME_PC to happen
338 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
339 express what goes on here.
340
341 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
342 (where the PC is already set up) and here (where it isn't).
343 INIT_FRAME_PC is only called from here, always after
344 INIT_EXTRA_FRAME_INFO.
345
346 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
347 value (which hasn't been set yet). Some other machines appear to
348 require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo.
349
350 We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
351 an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92.
352
353 To answer the question, yes the sparc needs INIT_FRAME_PC after
354 INIT_EXTRA_FRAME_INFO. Suggested scheme:
355
356 SETUP_INNERMOST_FRAME()
357 Default version is just create_new_frame (read_fp ()),
358 read_pc ()). Machines with extra frame info would do that (or the
359 local equivalent) and then set the extra fields.
360 SETUP_ARBITRARY_FRAME(argc, argv)
361 Only change here is that create_new_frame would no longer init extra
362 frame info; SETUP_ARBITRARY_FRAME would have to do that.
363 INIT_PREV_FRAME(fromleaf, prev)
364 Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC.
365 std_frame_pc(fromleaf, prev)
366 This is the default setting for INIT_PREV_FRAME. It just does what
367 the default INIT_FRAME_PC does. Some machines will call it from
368 INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
369 Some machines won't use it.
370 kingdon@cygnus.com, 13Apr93. */
371
372 #ifdef INIT_FRAME_PC_FIRST
373 INIT_FRAME_PC_FIRST (fromleaf, prev);
374 #endif
375
376 #ifdef INIT_EXTRA_FRAME_INFO
377 INIT_EXTRA_FRAME_INFO(fromleaf, prev);
378 #endif
379
380 /* This entry is in the frame queue now, which is good since
381 FRAME_SAVED_PC may use that queue to figure out it's value
382 (see tm-sparc.h). We want the pc saved in the inferior frame. */
383 INIT_FRAME_PC(fromleaf, prev);
384
385 if (IN_SIGTRAMP (prev->pc, (char *)NULL))
386 prev->signal_handler_caller = 1;
387
388 return prev;
389 }
390
391 CORE_ADDR
392 get_frame_pc (frame)
393 FRAME frame;
394 {
395 struct frame_info *fi;
396 fi = get_frame_info (frame);
397 return fi->pc;
398 }
399
400 #if defined (FRAME_FIND_SAVED_REGS)
401 /* Find the addresses in which registers are saved in FRAME. */
402
403 void
404 get_frame_saved_regs (frame_info_addr, saved_regs_addr)
405 struct frame_info *frame_info_addr;
406 struct frame_saved_regs *saved_regs_addr;
407 {
408 FRAME_FIND_SAVED_REGS (frame_info_addr, *saved_regs_addr);
409 }
410 #endif
411
412 /* Return the innermost lexical block in execution
413 in a specified stack frame. The frame address is assumed valid. */
414
415 struct block *
416 get_frame_block (frame)
417 FRAME frame;
418 {
419 struct frame_info *fi;
420 CORE_ADDR pc;
421
422 fi = get_frame_info (frame);
423
424 pc = fi->pc;
425 if (fi->next != 0)
426 /* We are not in the innermost frame. We need to subtract one to
427 get the correct block, in case the call instruction was the
428 last instruction of the block. If there are any machines on
429 which the saved pc does not point to after the call insn, we
430 probably want to make fi->pc point after the call insn anyway. */
431 --pc;
432 return block_for_pc (pc);
433 }
434
435 struct block *
436 get_current_block ()
437 {
438 return block_for_pc (read_pc ());
439 }
440
441 CORE_ADDR
442 get_pc_function_start (pc)
443 CORE_ADDR pc;
444 {
445 register struct block *bl;
446 register struct symbol *symbol;
447 register struct minimal_symbol *msymbol;
448 CORE_ADDR fstart;
449
450 if ((bl = block_for_pc (pc)) != NULL &&
451 (symbol = block_function (bl)) != NULL)
452 {
453 bl = SYMBOL_BLOCK_VALUE (symbol);
454 fstart = BLOCK_START (bl);
455 }
456 else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
457 {
458 fstart = SYMBOL_VALUE_ADDRESS (msymbol);
459 }
460 else
461 {
462 fstart = 0;
463 }
464 return (fstart);
465 }
466
467 /* Return the symbol for the function executing in frame FRAME. */
468
469 struct symbol *
470 get_frame_function (frame)
471 FRAME frame;
472 {
473 register struct block *bl = get_frame_block (frame);
474 if (bl == 0)
475 return 0;
476 return block_function (bl);
477 }
478 \f
479 /* Return the blockvector immediately containing the innermost lexical block
480 containing the specified pc value, or 0 if there is none.
481 PINDEX is a pointer to the index value of the block. If PINDEX
482 is NULL, we don't pass this information back to the caller. */
483
484 struct blockvector *
485 blockvector_for_pc (pc, pindex)
486 register CORE_ADDR pc;
487 int *pindex;
488 {
489 register struct block *b;
490 register int bot, top, half;
491 register struct symtab *s;
492 struct blockvector *bl;
493
494 /* First search all symtabs for one whose file contains our pc */
495 s = find_pc_symtab (pc);
496 if (s == 0)
497 return 0;
498
499 bl = BLOCKVECTOR (s);
500 b = BLOCKVECTOR_BLOCK (bl, 0);
501
502 /* Then search that symtab for the smallest block that wins. */
503 /* Use binary search to find the last block that starts before PC. */
504
505 bot = 0;
506 top = BLOCKVECTOR_NBLOCKS (bl);
507
508 while (top - bot > 1)
509 {
510 half = (top - bot + 1) >> 1;
511 b = BLOCKVECTOR_BLOCK (bl, bot + half);
512 if (BLOCK_START (b) <= pc)
513 bot += half;
514 else
515 top = bot + half;
516 }
517
518 /* Now search backward for a block that ends after PC. */
519
520 while (bot >= 0)
521 {
522 b = BLOCKVECTOR_BLOCK (bl, bot);
523 if (BLOCK_END (b) > pc)
524 {
525 if (pindex)
526 *pindex = bot;
527 return bl;
528 }
529 bot--;
530 }
531
532 return 0;
533 }
534
535 /* Return the innermost lexical block containing the specified pc value,
536 or 0 if there is none. */
537
538 struct block *
539 block_for_pc (pc)
540 register CORE_ADDR pc;
541 {
542 register struct blockvector *bl;
543 int index;
544
545 bl = blockvector_for_pc (pc, &index);
546 if (bl)
547 return BLOCKVECTOR_BLOCK (bl, index);
548 return 0;
549 }
550
551 /* Return the function containing pc value PC.
552 Returns 0 if function is not known. */
553
554 struct symbol *
555 find_pc_function (pc)
556 CORE_ADDR pc;
557 {
558 register struct block *b = block_for_pc (pc);
559 if (b == 0)
560 return 0;
561 return block_function (b);
562 }
563
564 /* These variables are used to cache the most recent result
565 * of find_pc_partial_function. */
566
567 static CORE_ADDR cache_pc_function_low = 0;
568 static CORE_ADDR cache_pc_function_high = 0;
569 static char *cache_pc_function_name = 0;
570
571 /* Clear cache, e.g. when symbol table is discarded. */
572
573 void
574 clear_pc_function_cache()
575 {
576 cache_pc_function_low = 0;
577 cache_pc_function_high = 0;
578 cache_pc_function_name = (char *)0;
579 }
580
581 /* Finds the "function" (text symbol) that is smaller than PC
582 but greatest of all of the potential text symbols. Sets
583 *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
584 Returns 0 if it couldn't find anything, 1 if it did. On a zero
585 return, *NAME and *ADDRESS are always set to zero. On a 1 return,
586 *NAME and *ADDRESS contain real information. */
587
588 int
589 find_pc_partial_function (pc, name, address)
590 CORE_ADDR pc;
591 char **name;
592 CORE_ADDR *address;
593 {
594 struct partial_symtab *pst;
595 struct symbol *f;
596 struct minimal_symbol *msymbol;
597 struct partial_symbol *psb;
598
599 if (pc >= cache_pc_function_low && pc < cache_pc_function_high)
600 {
601 if (address)
602 *address = cache_pc_function_low;
603 if (name)
604 *name = cache_pc_function_name;
605 return 1;
606 }
607
608 pst = find_pc_psymtab (pc);
609 if (pst)
610 {
611 if (pst->readin)
612 {
613 /* The information we want has already been read in.
614 We can go to the already readin symbols and we'll get
615 the best possible answer. */
616 f = find_pc_function (pc);
617 if (!f)
618 {
619 return_error:
620 /* No available symbol. */
621 if (name != 0)
622 *name = 0;
623 if (address != 0)
624 *address = 0;
625 return 0;
626 }
627
628 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
629 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
630 cache_pc_function_name = SYMBOL_NAME (f);
631 if (name)
632 *name = cache_pc_function_name;
633 if (address)
634 *address = cache_pc_function_low;
635 return 1;
636 }
637
638 /* Get the information from a combination of the pst
639 (static symbols), and the minimal symbol table (extern
640 symbols). */
641 msymbol = lookup_minimal_symbol_by_pc (pc);
642 psb = find_pc_psymbol (pst, pc);
643
644 if (!psb && (msymbol == NULL))
645 {
646 goto return_error;
647 }
648 if (psb
649 && (msymbol == NULL ||
650 (SYMBOL_VALUE_ADDRESS (psb) >= SYMBOL_VALUE_ADDRESS (msymbol))))
651 {
652 /* This case isn't being cached currently. */
653 if (address)
654 *address = SYMBOL_VALUE_ADDRESS (psb);
655 if (name)
656 *name = SYMBOL_NAME (psb);
657 return 1;
658 }
659 }
660 else
661 /* Must be in the minimal symbol table. */
662 {
663 msymbol = lookup_minimal_symbol_by_pc (pc);
664 if (msymbol == NULL)
665 goto return_error;
666 }
667
668 {
669 if (msymbol -> type == mst_text)
670 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
671 else
672 /* It is a transfer table for Sun shared libraries. */
673 cache_pc_function_low = pc - FUNCTION_START_OFFSET;
674 }
675 cache_pc_function_name = SYMBOL_NAME (msymbol);
676 /* FIXME: Deal with bumping into end of minimal symbols for a given
677 objfile, and what about testing for mst_text again? */
678 if (SYMBOL_NAME (msymbol + 1) != NULL)
679 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + 1);
680 else
681 cache_pc_function_high = cache_pc_function_low + 1;
682 if (address)
683 *address = cache_pc_function_low;
684 if (name)
685 *name = cache_pc_function_name;
686 return 1;
687 }
688
689 /* Return the innermost stack frame executing inside of the specified block,
690 or zero if there is no such frame. */
691
692 #if 0 /* Currently unused */
693
694 FRAME
695 block_innermost_frame (block)
696 struct block *block;
697 {
698 struct frame_info *fi;
699 register FRAME frame;
700 register CORE_ADDR start = BLOCK_START (block);
701 register CORE_ADDR end = BLOCK_END (block);
702
703 frame = 0;
704 while (1)
705 {
706 frame = get_prev_frame (frame);
707 if (frame == 0)
708 return 0;
709 fi = get_frame_info (frame);
710 if (fi->pc >= start && fi->pc < end)
711 return frame;
712 }
713 }
714
715 #endif /* 0 */
716
717 void
718 _initialize_blockframe ()
719 {
720 obstack_init (&frame_cache_obstack);
721 }