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