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