fix operand mask in the "moveml" entries for the coldfire.
[deliverable/binutils-gdb.git] / gdb / blockframe.c
CommitLineData
bd5635a1
RP
1/* Get info from stack frames;
2 convert between frames, blocks, functions and pc values.
dc1b349d
MS
3 Copyright 1986, 1987, 1988, 1989, 1991, 1994, 1995, 1996
4 Free Software Foundation, Inc.
bd5635a1
RP
5
6This file is part of GDB.
7
5259796b 8This program is free software; you can redistribute it and/or modify
bd5635a1 9it under the terms of the GNU General Public License as published by
5259796b
JG
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
bd5635a1 12
5259796b 13This program is distributed in the hope that it will be useful,
bd5635a1
RP
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
5259796b 19along with this program; if not, write to the Free Software
0e2c2c1e 20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bd5635a1
RP
21
22#include "defs.h"
bd5635a1 23#include "symtab.h"
23a8e291
JK
24#include "bfd.h"
25#include "symfile.h"
26#include "objfiles.h"
bd5635a1
RP
27#include "frame.h"
28#include "gdbcore.h"
29#include "value.h" /* for read_register */
30#include "target.h" /* for target_has_stack */
23a8e291 31#include "inferior.h" /* for read_pc */
16726dd1 32#include "annotate.h"
bd5635a1 33
23a8e291 34/* Is ADDR inside the startup file? Note that if your machine
bd5635a1
RP
35 has a way to detect the bottom of the stack, there is no need
36 to call this function from FRAME_CHAIN_VALID; the reason for
37 doing so is that some machines have no way of detecting bottom
23a8e291
JK
38 of stack.
39
40 A PC of zero is always considered to be the bottom of the stack. */
41
bd5635a1 42int
23a8e291 43inside_entry_file (addr)
bd5635a1
RP
44 CORE_ADDR addr;
45{
23a8e291
JK
46 if (addr == 0)
47 return 1;
48 if (symfile_objfile == 0)
49 return 0;
cef4c2e7
PS
50#if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
51 /* Do not stop backtracing if the pc is in the call dummy
52 at the entry point. */
dc1b349d
MS
53/* FIXME: Won't always work with zeros for the last two arguments */
54 if (PC_IN_CALL_DUMMY (addr, 0, 0))
cef4c2e7
PS
55 return 0;
56#endif
23a8e291
JK
57 return (addr >= symfile_objfile -> ei.entry_file_lowpc &&
58 addr < symfile_objfile -> ei.entry_file_highpc);
bd5635a1
RP
59}
60
e140f1da
JG
61/* Test a specified PC value to see if it is in the range of addresses
62 that correspond to the main() function. See comments above for why
63 we might want to do this.
64
23a8e291
JK
65 Typically called from FRAME_CHAIN_VALID.
66
67 A PC of zero is always considered to be the bottom of the stack. */
e140f1da
JG
68
69int
23a8e291 70inside_main_func (pc)
e140f1da
JG
71CORE_ADDR pc;
72{
23a8e291
JK
73 if (pc == 0)
74 return 1;
75 if (symfile_objfile == 0)
76 return 0;
0e2c2c1e 77
dc1b349d
MS
78 /* If the addr range is not set up at symbol reading time, set it up now.
79 This is for FRAME_CHAIN_VALID_ALTERNATE. I do this for coff, because
80 it is unable to set it up and symbol reading time. */
81
82 if (symfile_objfile -> ei.main_func_lowpc == INVALID_ENTRY_LOWPC &&
83 symfile_objfile -> ei.main_func_highpc == INVALID_ENTRY_HIGHPC)
0e2c2c1e 84 {
dc1b349d
MS
85 struct symbol *mainsym;
86
0e2c2c1e
KH
87 mainsym = lookup_symbol ("main", NULL, VAR_NAMESPACE, NULL, NULL);
88 if (mainsym && SYMBOL_CLASS(mainsym) == LOC_BLOCK)
89 {
90 symfile_objfile->ei.main_func_lowpc = BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym));
91 symfile_objfile->ei.main_func_highpc = BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym));
92 }
0e2c2c1e 93 }
23a8e291
JK
94 return (symfile_objfile -> ei.main_func_lowpc <= pc &&
95 symfile_objfile -> ei.main_func_highpc > pc);
e140f1da
JG
96}
97
98/* Test a specified PC value to see if it is in the range of addresses
23a8e291
JK
99 that correspond to the process entry point function. See comments
100 in objfiles.h for why we might want to do this.
101
102 Typically called from FRAME_CHAIN_VALID.
e140f1da 103
23a8e291 104 A PC of zero is always considered to be the bottom of the stack. */
e140f1da
JG
105
106int
23a8e291 107inside_entry_func (pc)
e140f1da
JG
108CORE_ADDR pc;
109{
23a8e291
JK
110 if (pc == 0)
111 return 1;
112 if (symfile_objfile == 0)
113 return 0;
cef4c2e7
PS
114#if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
115 /* Do not stop backtracing if the pc is in the call dummy
116 at the entry point. */
dc1b349d 117/* FIXME: Won't always work with zeros for the last two arguments */
cef4c2e7
PS
118 if (PC_IN_CALL_DUMMY (pc, 0, 0))
119 return 0;
120#endif
23a8e291
JK
121 return (symfile_objfile -> ei.entry_func_lowpc <= pc &&
122 symfile_objfile -> ei.entry_func_highpc > pc);
e140f1da
JG
123}
124
0e2c2c1e
KH
125/* Info about the innermost stack frame (contents of FP register) */
126
127static struct frame_info *current_frame;
bd5635a1 128
0e2c2c1e
KH
129/* Cache for frame addresses already read by gdb. Valid only while
130 inferior is stopped. Control variables for the frame cache should
131 be local to this module. */
bd5635a1 132
bd5635a1
RP
133struct obstack frame_cache_obstack;
134
135/* Return the innermost (currently executing) stack frame. */
136
0e2c2c1e 137struct frame_info *
bd5635a1
RP
138get_current_frame ()
139{
16726dd1
JK
140 if (current_frame == NULL)
141 {
142 if (target_has_stack)
143 current_frame = create_new_frame (read_fp (), read_pc ());
144 else
145 error ("No stack.");
146 }
bd5635a1
RP
147 return current_frame;
148}
149
150void
151set_current_frame (frame)
0e2c2c1e 152 struct frame_info *frame;
bd5635a1
RP
153{
154 current_frame = frame;
155}
156
16726dd1
JK
157/* Create an arbitrary (i.e. address specified by user) or innermost frame.
158 Always returns a non-NULL value. */
159
0e2c2c1e 160struct frame_info *
bd5635a1 161create_new_frame (addr, pc)
0e2c2c1e 162 CORE_ADDR addr;
bd5635a1
RP
163 CORE_ADDR pc;
164{
0e2c2c1e 165 struct frame_info *fi;
d541211d 166 char *name;
bd5635a1 167
0e2c2c1e 168 fi = (struct frame_info *)
bd5635a1
RP
169 obstack_alloc (&frame_cache_obstack,
170 sizeof (struct frame_info));
171
172 /* Arbitrary frame */
0e2c2c1e
KH
173 fi->next = NULL;
174 fi->prev = NULL;
175 fi->frame = addr;
176 fi->pc = pc;
d541211d 177 find_pc_partial_function (pc, &name, (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
0e2c2c1e 178 fi->signal_handler_caller = IN_SIGTRAMP (fi->pc, name);
bd5635a1
RP
179
180#ifdef INIT_EXTRA_FRAME_INFO
0e2c2c1e 181 INIT_EXTRA_FRAME_INFO (0, fi);
bd5635a1
RP
182#endif
183
0e2c2c1e 184 return fi;
bd5635a1
RP
185}
186
0e2c2c1e
KH
187/* Return the frame that called FI.
188 If FI is the original frame (it has no caller), return 0. */
bd5635a1 189
0e2c2c1e 190struct frame_info *
bd5635a1 191get_prev_frame (frame)
0e2c2c1e 192 struct frame_info *frame;
bd5635a1 193{
bd5635a1
RP
194 return get_prev_frame_info (frame);
195}
196
0e2c2c1e 197/* Return the frame that FRAME calls (NULL if FRAME is the innermost
bd5635a1
RP
198 frame). */
199
0e2c2c1e 200struct frame_info *
bd5635a1 201get_next_frame (frame)
0e2c2c1e 202 struct frame_info *frame;
bd5635a1 203{
bd5635a1
RP
204 return frame->next;
205}
206
0e2c2c1e
KH
207/* Flush the entire frame cache. */
208
bd5635a1
RP
209void
210flush_cached_frames ()
211{
212 /* Since we can't really be sure what the first object allocated was */
213 obstack_free (&frame_cache_obstack, 0);
214 obstack_init (&frame_cache_obstack);
215
0e2c2c1e
KH
216 current_frame = NULL; /* Invalidate cache */
217 select_frame (NULL, -1);
16726dd1 218 annotate_frames_invalid ();
bd5635a1
RP
219}
220
2403f49b 221/* Flush the frame cache, and start a new one if necessary. */
16726dd1 222
2403f49b
JK
223void
224reinit_frame_cache ()
225{
2403f49b 226 flush_cached_frames ();
0e2c2c1e
KH
227
228 /* FIXME: The inferior_pid test is wrong if there is a corefile. */
16726dd1 229 if (inferior_pid != 0)
2289e1c3 230 {
2289e1c3
JK
231 select_frame (get_current_frame (), 0);
232 }
bd5635a1
RP
233}
234
235/* If a machine allows frameless functions, it should define a macro
236 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
237 frame_info for the frame, and FRAMELESS should be set to nonzero
238 if it represents a frameless function invocation. */
239
23a8e291 240/* Return nonzero if the function for this frame lacks a prologue. Many
bd5635a1
RP
241 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
242 function. */
243
244int
245frameless_look_for_prologue (frame)
0e2c2c1e 246 struct frame_info *frame;
bd5635a1
RP
247{
248 CORE_ADDR func_start, after_prologue;
dc1b349d 249 func_start = get_pc_function_start (frame->pc);
bd5635a1
RP
250 if (func_start)
251 {
dc1b349d 252 func_start += FUNCTION_START_OFFSET;
bd5635a1 253 after_prologue = func_start;
5259796b
JG
254#ifdef SKIP_PROLOGUE_FRAMELESS_P
255 /* This is faster, since only care whether there *is* a prologue,
256 not how long it is. */
257 SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
258#else
bd5635a1 259 SKIP_PROLOGUE (after_prologue);
5259796b 260#endif
bd5635a1
RP
261 return after_prologue == func_start;
262 }
263 else
264 /* If we can't find the start of the function, we don't really
265 know whether the function is frameless, but we should be able
266 to get a reasonable (i.e. best we can do under the
267 circumstances) backtrace by saying that it isn't. */
268 return 0;
269}
270
e140f1da
JG
271/* Default a few macros that people seldom redefine. */
272
bd5635a1
RP
273#if !defined (INIT_FRAME_PC)
274#define INIT_FRAME_PC(fromleaf, prev) \
275 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
276 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
277#endif
278
e140f1da
JG
279#ifndef FRAME_CHAIN_COMBINE
280#define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
281#endif
282
bd5635a1
RP
283/* Return a structure containing various interesting information
284 about the frame that called NEXT_FRAME. Returns NULL
285 if there is no such frame. */
286
287struct frame_info *
288get_prev_frame_info (next_frame)
0e2c2c1e 289 struct frame_info *next_frame;
bd5635a1 290{
0e2c2c1e 291 CORE_ADDR address = 0;
bd5635a1
RP
292 struct frame_info *prev;
293 int fromleaf = 0;
d541211d 294 char *name;
bd5635a1
RP
295
296 /* If the requested entry is in the cache, return it.
297 Otherwise, figure out what the address should be for the entry
298 we're about to add to the cache. */
299
300 if (!next_frame)
301 {
9e837b37
PS
302#if 0
303 /* This screws value_of_variable, which just wants a nice clean
304 NULL return from block_innermost_frame if there are no frames.
305 I don't think I've ever seen this message happen otherwise.
306 And returning NULL here is a perfectly legitimate thing to do. */
bd5635a1
RP
307 if (!current_frame)
308 {
309 error ("You haven't set up a process's stack to examine.");
310 }
9e837b37 311#endif
bd5635a1
RP
312
313 return current_frame;
314 }
315
316 /* If we have the prev one, return it */
317 if (next_frame->prev)
318 return next_frame->prev;
319
320 /* On some machines it is possible to call a function without
321 setting up a stack frame for it. On these machines, we
322 define this macro to take two args; a frameinfo pointer
323 identifying a frame and a variable to set or clear if it is
324 or isn't leafless. */
325#ifdef FRAMELESS_FUNCTION_INVOCATION
326 /* Still don't want to worry about this except on the innermost
327 frame. This macro will set FROMLEAF if NEXT_FRAME is a
328 frameless function invocation. */
329 if (!(next_frame->next))
330 {
331 FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
332 if (fromleaf)
0e2c2c1e 333 address = FRAME_FP (next_frame);
bd5635a1
RP
334 }
335#endif
336
337 if (!fromleaf)
338 {
339 /* Two macros defined in tm.h specify the machine-dependent
340 actions to be performed here.
341 First, get the frame's chain-pointer.
342 If that is zero, the frame is the outermost frame or a leaf
343 called by the outermost frame. This means that if start
344 calls main without a frame, we'll return 0 (which is fine
345 anyway).
346
347 Nope; there's a problem. This also returns when the current
348 routine is a leaf of main. This is unacceptable. We move
349 this to after the ffi test; I'd rather have backtraces from
350 start go curfluy than have an abort called from main not show
351 main. */
352 address = FRAME_CHAIN (next_frame);
353 if (!FRAME_CHAIN_VALID (address, next_frame))
354 return 0;
355 address = FRAME_CHAIN_COMBINE (address, next_frame);
356 }
e140f1da
JG
357 if (address == 0)
358 return 0;
bd5635a1
RP
359
360 prev = (struct frame_info *)
361 obstack_alloc (&frame_cache_obstack,
362 sizeof (struct frame_info));
363
364 if (next_frame)
365 next_frame->prev = prev;
366 prev->next = next_frame;
367 prev->prev = (struct frame_info *) 0;
368 prev->frame = address;
23a8e291
JK
369 prev->signal_handler_caller = 0;
370
371/* This change should not be needed, FIXME! We should
372 determine whether any targets *need* INIT_FRAME_PC to happen
373 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
374 express what goes on here.
375
376 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
377 (where the PC is already set up) and here (where it isn't).
378 INIT_FRAME_PC is only called from here, always after
379 INIT_EXTRA_FRAME_INFO.
380
381 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
382 value (which hasn't been set yet). Some other machines appear to
383 require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo.
384
385 We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
386 an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92.
387
0e2c2c1e
KH
388 Assuming that some machines need INIT_FRAME_PC after
389 INIT_EXTRA_FRAME_INFO, one possible scheme:
23a8e291
JK
390
391 SETUP_INNERMOST_FRAME()
392 Default version is just create_new_frame (read_fp ()),
393 read_pc ()). Machines with extra frame info would do that (or the
394 local equivalent) and then set the extra fields.
395 SETUP_ARBITRARY_FRAME(argc, argv)
396 Only change here is that create_new_frame would no longer init extra
397 frame info; SETUP_ARBITRARY_FRAME would have to do that.
398 INIT_PREV_FRAME(fromleaf, prev)
9e837b37
PS
399 Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC. This should
400 also return a flag saying whether to keep the new frame, or
401 whether to discard it, because on some machines (e.g. mips) it
402 is really awkward to have FRAME_CHAIN_VALID called *before*
403 INIT_EXTRA_FRAME_INFO (there is no good way to get information
404 deduced in FRAME_CHAIN_VALID into the extra fields of the new frame).
23a8e291
JK
405 std_frame_pc(fromleaf, prev)
406 This is the default setting for INIT_PREV_FRAME. It just does what
407 the default INIT_FRAME_PC does. Some machines will call it from
408 INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
409 Some machines won't use it.
0e2c2c1e 410 kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */
23a8e291
JK
411
412#ifdef INIT_FRAME_PC_FIRST
413 INIT_FRAME_PC_FIRST (fromleaf, prev);
414#endif
bd5635a1
RP
415
416#ifdef INIT_EXTRA_FRAME_INFO
e140f1da 417 INIT_EXTRA_FRAME_INFO(fromleaf, prev);
bd5635a1
RP
418#endif
419
420 /* This entry is in the frame queue now, which is good since
9e837b37 421 FRAME_SAVED_PC may use that queue to figure out its value
e140f1da 422 (see tm-sparc.h). We want the pc saved in the inferior frame. */
bd5635a1
RP
423 INIT_FRAME_PC(fromleaf, prev);
424
9e837b37
PS
425 /* If ->frame and ->pc are unchanged, we are in the process of getting
426 ourselves into an infinite backtrace. Some architectures check this
427 in FRAME_CHAIN or thereabouts, but it seems like there is no reason
428 this can't be an architecture-independent check. */
429 if (next_frame != NULL)
430 {
431 if (prev->frame == next_frame->frame
432 && prev->pc == next_frame->pc)
433 {
434 next_frame->prev = NULL;
435 obstack_free (&frame_cache_obstack, prev);
436 return NULL;
437 }
438 }
439
d541211d
PS
440 find_pc_partial_function (prev->pc, &name,
441 (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
442 if (IN_SIGTRAMP (prev->pc, name))
23a8e291
JK
443 prev->signal_handler_caller = 1;
444
bd5635a1
RP
445 return prev;
446}
447
448CORE_ADDR
449get_frame_pc (frame)
0e2c2c1e 450 struct frame_info *frame;
bd5635a1 451{
0e2c2c1e 452 return frame->pc;
bd5635a1
RP
453}
454
455#if defined (FRAME_FIND_SAVED_REGS)
456/* Find the addresses in which registers are saved in FRAME. */
457
458void
0e2c2c1e
KH
459get_frame_saved_regs (frame, saved_regs_addr)
460 struct frame_info *frame;
bd5635a1
RP
461 struct frame_saved_regs *saved_regs_addr;
462{
0e2c2c1e 463 FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr);
bd5635a1
RP
464}
465#endif
466
467/* Return the innermost lexical block in execution
468 in a specified stack frame. The frame address is assumed valid. */
469
470struct block *
471get_frame_block (frame)
0e2c2c1e 472 struct frame_info *frame;
bd5635a1 473{
bd5635a1
RP
474 CORE_ADDR pc;
475
0e2c2c1e
KH
476 pc = frame->pc;
477 if (frame->next != 0 && frame->next->signal_handler_caller == 0)
747a6329
PS
478 /* We are not in the innermost frame and we were not interrupted
479 by a signal. We need to subtract one to get the correct block,
480 in case the call instruction was the last instruction of the block.
481 If there are any machines on which the saved pc does not point to
0e2c2c1e 482 after the call insn, we probably want to make frame->pc point after
747a6329 483 the call insn anyway. */
bd5635a1
RP
484 --pc;
485 return block_for_pc (pc);
486}
487
488struct block *
489get_current_block ()
490{
491 return block_for_pc (read_pc ());
492}
493
494CORE_ADDR
495get_pc_function_start (pc)
496 CORE_ADDR pc;
497{
23a8e291 498 register struct block *bl;
bd5635a1 499 register struct symbol *symbol;
23a8e291
JK
500 register struct minimal_symbol *msymbol;
501 CORE_ADDR fstart;
502
503 if ((bl = block_for_pc (pc)) != NULL &&
504 (symbol = block_function (bl)) != NULL)
bd5635a1 505 {
23a8e291
JK
506 bl = SYMBOL_BLOCK_VALUE (symbol);
507 fstart = BLOCK_START (bl);
bd5635a1 508 }
23a8e291
JK
509 else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
510 {
511 fstart = SYMBOL_VALUE_ADDRESS (msymbol);
512 }
513 else
514 {
515 fstart = 0;
516 }
517 return (fstart);
bd5635a1
RP
518}
519
520/* Return the symbol for the function executing in frame FRAME. */
521
522struct symbol *
523get_frame_function (frame)
0e2c2c1e 524 struct frame_info *frame;
bd5635a1
RP
525{
526 register struct block *bl = get_frame_block (frame);
527 if (bl == 0)
528 return 0;
529 return block_function (bl);
530}
531\f
532/* Return the blockvector immediately containing the innermost lexical block
533 containing the specified pc value, or 0 if there is none.
534 PINDEX is a pointer to the index value of the block. If PINDEX
535 is NULL, we don't pass this information back to the caller. */
536
537struct blockvector *
538blockvector_for_pc (pc, pindex)
539 register CORE_ADDR pc;
540 int *pindex;
541{
542 register struct block *b;
543 register int bot, top, half;
544 register struct symtab *s;
545 struct blockvector *bl;
546
547 /* First search all symtabs for one whose file contains our pc */
548 s = find_pc_symtab (pc);
549 if (s == 0)
550 return 0;
551
552 bl = BLOCKVECTOR (s);
553 b = BLOCKVECTOR_BLOCK (bl, 0);
554
555 /* Then search that symtab for the smallest block that wins. */
556 /* Use binary search to find the last block that starts before PC. */
557
558 bot = 0;
559 top = BLOCKVECTOR_NBLOCKS (bl);
560
561 while (top - bot > 1)
562 {
563 half = (top - bot + 1) >> 1;
564 b = BLOCKVECTOR_BLOCK (bl, bot + half);
565 if (BLOCK_START (b) <= pc)
566 bot += half;
567 else
568 top = bot + half;
569 }
570
571 /* Now search backward for a block that ends after PC. */
572
573 while (bot >= 0)
574 {
575 b = BLOCKVECTOR_BLOCK (bl, bot);
576 if (BLOCK_END (b) > pc)
577 {
578 if (pindex)
579 *pindex = bot;
580 return bl;
581 }
582 bot--;
583 }
584
585 return 0;
586}
587
588/* Return the innermost lexical block containing the specified pc value,
589 or 0 if there is none. */
590
591struct block *
592block_for_pc (pc)
593 register CORE_ADDR pc;
594{
595 register struct blockvector *bl;
596 int index;
597
598 bl = blockvector_for_pc (pc, &index);
599 if (bl)
600 return BLOCKVECTOR_BLOCK (bl, index);
601 return 0;
602}
603
604/* Return the function containing pc value PC.
605 Returns 0 if function is not known. */
606
607struct symbol *
608find_pc_function (pc)
609 CORE_ADDR pc;
610{
611 register struct block *b = block_for_pc (pc);
612 if (b == 0)
613 return 0;
614 return block_function (b);
615}
616
617/* These variables are used to cache the most recent result
618 * of find_pc_partial_function. */
619
620static CORE_ADDR cache_pc_function_low = 0;
621static CORE_ADDR cache_pc_function_high = 0;
622static char *cache_pc_function_name = 0;
623
624/* Clear cache, e.g. when symbol table is discarded. */
625
626void
627clear_pc_function_cache()
628{
629 cache_pc_function_low = 0;
630 cache_pc_function_high = 0;
631 cache_pc_function_name = (char *)0;
632}
633
d541211d
PS
634/* Finds the "function" (text symbol) that is smaller than PC but
635 greatest of all of the potential text symbols. Sets *NAME and/or
636 *ADDRESS conditionally if that pointer is non-null. If ENDADDR is
637 non-null, then set *ENDADDR to be the end of the function
638 (exclusive), but passing ENDADDR as non-null means that the
639 function might cause symbols to be read. This function either
640 succeeds or fails (not halfway succeeds). If it succeeds, it sets
641 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
642 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero
643 and returns 0. */
bd5635a1
RP
644
645int
d541211d 646find_pc_partial_function (pc, name, address, endaddr)
bd5635a1
RP
647 CORE_ADDR pc;
648 char **name;
649 CORE_ADDR *address;
d541211d 650 CORE_ADDR *endaddr;
bd5635a1
RP
651{
652 struct partial_symtab *pst;
653 struct symbol *f;
23a8e291 654 struct minimal_symbol *msymbol;
bd5635a1 655 struct partial_symbol *psb;
981a3309 656 struct obj_section *sec;
bd5635a1
RP
657
658 if (pc >= cache_pc_function_low && pc < cache_pc_function_high)
d541211d
PS
659 goto return_cached_value;
660
661 /* If sigtramp is in the u area, it counts as a function (especially
662 important for step_1). */
663#if defined SIGTRAMP_START
664 if (IN_SIGTRAMP (pc, (char *)NULL))
bd5635a1 665 {
dc1b349d
MS
666 cache_pc_function_low = SIGTRAMP_START (pc);
667 cache_pc_function_high = SIGTRAMP_END (pc);
d541211d
PS
668 cache_pc_function_name = "<sigtramp>";
669
670 goto return_cached_value;
bd5635a1 671 }
d541211d 672#endif
bd5635a1 673
d541211d 674 msymbol = lookup_minimal_symbol_by_pc (pc);
bd5635a1
RP
675 pst = find_pc_psymtab (pc);
676 if (pst)
677 {
d541211d
PS
678 /* Need to read the symbols to get a good value for the end address. */
679 if (endaddr != NULL && !pst->readin)
2f1c7c3f
JK
680 {
681 /* Need to get the terminal in case symbol-reading produces
682 output. */
683 target_terminal_ours_for_output ();
684 PSYMTAB_TO_SYMTAB (pst);
685 }
d541211d 686
bd5635a1
RP
687 if (pst->readin)
688 {
d541211d
PS
689 /* Checking whether the msymbol has a larger value is for the
690 "pathological" case mentioned in print_frame_info. */
bd5635a1 691 f = find_pc_function (pc);
d541211d
PS
692 if (f != NULL
693 && (msymbol == NULL
694 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
695 >= SYMBOL_VALUE_ADDRESS (msymbol))))
bd5635a1 696 {
d541211d
PS
697 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
698 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
699 cache_pc_function_name = SYMBOL_NAME (f);
700 goto return_cached_value;
bd5635a1 701 }
bd5635a1 702 }
cef4c2e7 703 else
bd5635a1 704 {
cef4c2e7
PS
705 /* Now that static symbols go in the minimal symbol table, perhaps
706 we could just ignore the partial symbols. But at least for now
707 we use the partial or minimal symbol, whichever is larger. */
708 psb = find_pc_psymbol (pst, pc);
709
710 if (psb
711 && (msymbol == NULL ||
712 (SYMBOL_VALUE_ADDRESS (psb)
713 >= SYMBOL_VALUE_ADDRESS (msymbol))))
714 {
715 /* This case isn't being cached currently. */
716 if (address)
717 *address = SYMBOL_VALUE_ADDRESS (psb);
718 if (name)
719 *name = SYMBOL_NAME (psb);
720 /* endaddr non-NULL can't happen here. */
721 return 1;
722 }
bd5635a1
RP
723 }
724 }
d541211d 725
981a3309
SG
726 /* Not in the normal symbol tables, see if the pc is in a known section.
727 If it's not, then give up. This ensures that anything beyond the end
728 of the text seg doesn't appear to be part of the last function in the
729 text segment. */
730
731 sec = find_pc_section (pc);
732
733 if (!sec)
734 msymbol = NULL;
735
d541211d
PS
736 /* Must be in the minimal symbol table. */
737 if (msymbol == NULL)
bd5635a1 738 {
d541211d
PS
739 /* No available symbol. */
740 if (name != NULL)
741 *name = 0;
742 if (address != NULL)
743 *address = 0;
744 if (endaddr != NULL)
745 *endaddr = 0;
746 return 0;
bd5635a1
RP
747 }
748
dc1b349d 749 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
23a8e291 750 cache_pc_function_name = SYMBOL_NAME (msymbol);
d541211d 751
981a3309
SG
752 /* Use the lesser of the next minimal symbol, or the end of the section, as
753 the end of the function. */
754
755 if (SYMBOL_NAME (msymbol + 1) != NULL
756 && SYMBOL_VALUE_ADDRESS (msymbol + 1) < sec->endaddr)
23a8e291 757 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + 1);
bd5635a1 758 else
981a3309
SG
759 /* We got the start address from the last msymbol in the objfile.
760 So the end address is the end of the section. */
761 cache_pc_function_high = sec->endaddr;
d541211d
PS
762
763 return_cached_value:
bd5635a1
RP
764 if (address)
765 *address = cache_pc_function_low;
766 if (name)
767 *name = cache_pc_function_name;
d541211d
PS
768 if (endaddr)
769 *endaddr = cache_pc_function_high;
bd5635a1
RP
770 return 1;
771}
772
479fdd26 773/* Return the innermost stack frame executing inside of BLOCK,
2289e1c3 774 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
23a8e291 775
0e2c2c1e 776struct frame_info *
bd5635a1
RP
777block_innermost_frame (block)
778 struct block *block;
779{
0e2c2c1e 780 struct frame_info *frame;
2289e1c3
JK
781 register CORE_ADDR start;
782 register CORE_ADDR end;
bd5635a1 783
479fdd26
JK
784 if (block == NULL)
785 return NULL;
786
2289e1c3
JK
787 start = BLOCK_START (block);
788 end = BLOCK_END (block);
789
0e2c2c1e 790 frame = NULL;
bd5635a1
RP
791 while (1)
792 {
793 frame = get_prev_frame (frame);
0e2c2c1e
KH
794 if (frame == NULL)
795 return NULL;
796 if (frame->pc >= start && frame->pc < end)
bd5635a1
RP
797 return frame;
798 }
799}
800
0e2c2c1e
KH
801/* Return the full FRAME which corresponds to the given CORE_ADDR
802 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
999dd04b 803
0e2c2c1e 804struct frame_info *
999dd04b 805find_frame_addr_in_frame_chain (frame_addr)
0e2c2c1e 806 CORE_ADDR frame_addr;
999dd04b 807{
0e2c2c1e 808 struct frame_info *frame = NULL;
999dd04b 809
16726dd1 810 if (frame_addr == (CORE_ADDR)0)
999dd04b
JL
811 return NULL;
812
813 while (1)
814 {
815 frame = get_prev_frame (frame);
816 if (frame == NULL)
817 return NULL;
999dd04b
JL
818 if (FRAME_FP (frame) == frame_addr)
819 return frame;
820 }
821}
822
d541211d
PS
823#ifdef SIGCONTEXT_PC_OFFSET
824/* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */
825
826CORE_ADDR
827sigtramp_saved_pc (frame)
0e2c2c1e 828 struct frame_info *frame;
d541211d
PS
829{
830 CORE_ADDR sigcontext_addr;
831 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
832 int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT;
833 int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT;
834
835 /* Get sigcontext address, it is the third parameter on the stack. */
836 if (frame->next)
837 sigcontext_addr = read_memory_integer (FRAME_ARGS_ADDRESS (frame->next)
0e2c2c1e
KH
838 + FRAME_ARGS_SKIP
839 + sigcontext_offs,
d541211d
PS
840 ptrbytes);
841 else
842 sigcontext_addr = read_memory_integer (read_register (SP_REGNUM)
843 + sigcontext_offs,
844 ptrbytes);
845
846 /* Don't cause a memory_error when accessing sigcontext in case the stack
847 layout has changed or the stack is corrupt. */
848 target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes);
849 return extract_unsigned_integer (buf, ptrbytes);
850}
851#endif /* SIGCONTEXT_PC_OFFSET */
852
dc1b349d
MS
853/*
854 * DUMMY FRAMES
855 *
856 * The following code serves to maintain the dummy stack frames for
857 * inferior function calls (ie. when gdb calls into the inferior via
858 * call_function_by_hand). This code saves the machine state before
859 * the call in host memory, so it must maintain an independant stack
860 * and keep it consistant etc. I am attempting to make this code
861 * generic enough to be used by many targets.
862 *
863 * The cheapest and most generic way to do CALL_DUMMY on a new target
864 * is probably to define CALL_DUMMY to be empty, CALL_DUMMY_LENGTH to zero,
865 * and CALL_DUMMY_LOCATION to AT_ENTRY. Then you must remember to define
866 * PUSH_RETURN_ADDRESS, because there won't be a call instruction to do it.
867 */
868
869static struct dummy_frame *dummy_frame_stack = NULL;
870
871/* Function: find_dummy_frame(pc, fp, sp)
872 Search the stack of dummy frames for one matching the given PC, FP and SP.
873 This is the work-horse for pc_in_call_dummy and read_register_dummy */
874
875char *
876generic_find_dummy_frame (pc, fp)
877 CORE_ADDR pc;
878 CORE_ADDR fp;
879{
880 struct dummy_frame * dummyframe;
881#ifdef NEED_TEXT_START_END
882 CORE_ADDR bkpt_address;
883 extern CORE_ADDR text_end;
884#endif
885
886#if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
887 if (pc != entry_point_address ())
888 return 0;
889#endif /* AT_ENTRY_POINT */
890
891#if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
892 bkpt_address = text_end - CALL_DUMMY_LENGTH + CALL_DUMMY_BREAKPOINT_OFFSET;
893 if (pc != bkpt_address)
894 return 0;
895#endif /* BEFORE_TEXT_END */
896
897#if CALL_DUMMY_LOCATION == AFTER_TEXT_END
898 bkpt_address = text_end + CALL_DUMMY_BREAKPOINT_OFFSET;
899 if (pc != bkpt_address)
900 return 0;
901#endif /* AFTER_TEXT_END */
902
903#if CALL_DUMMY_LOCATION == ON_STACK
904 /* compute the displacement from the CALL_DUMMY breakpoint
905 to the frame pointer */
906 if (1 INNER_THAN 2)
907 pc += CALL_DUMMY_LENGTH - CALL_DUMMY_BREAKPOINT_OFFSET;
908 else
909 pc += CALL_DUMMY_BREAKPOINT_OFFSET;
910#endif /* ON_STACK */
911
912 for (dummyframe = dummy_frame_stack; dummyframe != NULL;
913 dummyframe = dummyframe->next)
914 if (fp == dummyframe->fp || fp == dummyframe->sp)
915 {
916 /* The frame in question lies between the saved fp and sp, inclusive */
917#if CALL_DUMMY_LOCATION == ON_STACK
918 /* NOTE: a better way to do this might be simply to test whether
919 the pc lies between the saved (sp, fp) and CALL_DUMMY_LENGTH.
920 */
921
922 if (pc == dummyframe->fp || pc == dummyframe->sp)
923#endif /* ON_STACK */
924 return dummyframe->regs;
925 }
926 return 0;
927}
928
929/* Function: pc_in_call_dummy (pc, fp)
930 Return true if this is a dummy frame created by gdb for an inferior call */
931
932int
933generic_pc_in_call_dummy (pc, fp)
934 CORE_ADDR pc;
935 CORE_ADDR fp;
936{
937 /* if find_dummy_frame succeeds, then PC is in a call dummy */
938 return (generic_find_dummy_frame (pc, fp) != 0);
939}
940
941/* Function: read_register_dummy
942 Find a saved register from before GDB calls a function in the inferior */
943
944CORE_ADDR
945generic_read_register_dummy (pc, fp, regno)
946 CORE_ADDR pc;
947 CORE_ADDR fp;
948 int regno;
949{
950 char *dummy_regs = generic_find_dummy_frame (pc, fp);
951
952 if (dummy_regs)
953 return extract_address (&dummy_regs[REGISTER_BYTE (regno)],
954 REGISTER_RAW_SIZE(regno));
955 else
956 return 0;
957}
958
959/* Save all the registers on the dummy frame stack. Most ports save the
960 registers on the target stack. This results in lots of unnecessary memory
961 references, which are slow when debugging via a serial line. Instead, we
962 save all the registers internally, and never write them to the stack. The
963 registers get restored when the called function returns to the entry point,
964 where a breakpoint is laying in wait. */
965
966void
967generic_push_dummy_frame ()
968{
969 struct dummy_frame *dummy_frame;
970 CORE_ADDR fp = (get_current_frame ())->frame;
971
972 /* check to see if there are stale dummy frames,
973 perhaps left over from when a longjump took us out of a
974 function that was called by the debugger */
975
976 dummy_frame = dummy_frame_stack;
977 while (dummy_frame)
978 if (dummy_frame->fp INNER_THAN fp) /* stale -- destroy! */
979 {
980 dummy_frame_stack = dummy_frame->next;
981 free (dummy_frame);
982 dummy_frame = dummy_frame_stack;
983 }
984 else
985 dummy_frame = dummy_frame->next;
986
987 dummy_frame = xmalloc (sizeof (struct dummy_frame));
988 dummy_frame->pc = read_register (PC_REGNUM);
989 dummy_frame->sp = read_register (SP_REGNUM);
990 dummy_frame->fp = fp;
991 read_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
992 dummy_frame->next = dummy_frame_stack;
993 dummy_frame_stack = dummy_frame;
994}
995
996/* Function: pop_dummy_frame
997 Restore the machine state from a saved dummy stack frame. */
998
999void
1000generic_pop_dummy_frame ()
1001{
1002 struct dummy_frame *dummy_frame = dummy_frame_stack;
1003
1004 /* FIXME: what if the first frame isn't the right one, eg..
1005 because one call-by-hand function has done a longjmp into another one? */
1006
1007 if (!dummy_frame)
1008 error ("Can't pop dummy frame!");
1009 dummy_frame_stack = dummy_frame->next;
1010 write_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
1011 free (dummy_frame);
1012}
1013
1014/* Function: frame_chain_valid
1015 Returns true for a user frame or a call_function_by_hand dummy frame,
1016 and false for the CRT0 start-up frame. Purpose is to terminate backtrace */
1017
1018int
1019generic_frame_chain_valid (fp, fi)
1020 CORE_ADDR fp;
1021 struct frame_info *fi;
1022{
1023#if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
1024 if (PC_IN_CALL_DUMMY(FRAME_SAVED_PC(fi), fp, fp))
1025 return 1; /* don't prune CALL_DUMMY frames */
1026 else /* fall back to default algorithm (see frame.h) */
1027#endif
1028 return (fp != 0 && !inside_entry_file (FRAME_SAVED_PC(fi)));
1029}
1030
1031/* Function: get_saved_register
1032 Find register number REGNUM relative to FRAME and put its (raw,
1033 target format) contents in *RAW_BUFFER.
1034
1035 Set *OPTIMIZED if the variable was optimized out (and thus can't be
1036 fetched). Note that this is never set to anything other than zero
1037 in this implementation.
1038
1039 Set *LVAL to lval_memory, lval_register, or not_lval, depending on
1040 whether the value was fetched from memory, from a register, or in a
1041 strange and non-modifiable way (e.g. a frame pointer which was
1042 calculated rather than fetched). We will use not_lval for values
1043 fetched from generic dummy frames.
1044
1045 Set *ADDRP to the address, either in memory on as a REGISTER_BYTE
1046 offset into the registers array. If the value is stored in a dummy
1047 frame, set *ADDRP to zero.
1048
1049 To use this implementation, define a function called
1050 "get_saved_register" in your target code, which simply passes all
1051 of its arguments to this function.
1052
1053 The argument RAW_BUFFER must point to aligned memory. */
1054
1055void
1056generic_get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
1057 char *raw_buffer;
1058 int *optimized;
1059 CORE_ADDR *addrp;
1060 struct frame_info *frame;
1061 int regnum;
1062 enum lval_type *lval;
1063{
1064 CORE_ADDR addr;
1065 struct frame_saved_regs fsr;
1066
1067 if (!target_has_registers)
1068 error ("No registers.");
1069
1070 /* Normal systems don't optimize out things with register numbers. */
1071 if (optimized != NULL)
1072 *optimized = 0;
1073
1074 if (addrp) /* default assumption: not found in memory */
1075 *addrp = 0;
1076
1077 /* Note: since the current frame's registers could only have been
1078 saved by frames INTERIOR TO the current frame, we skip examining
1079 the current frame itself: otherwise, we would be getting the
1080 previous frame's registers which were saved by the current frame. */
1081
1082 while ((frame = frame->next) != NULL)
1083 {
1084 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
1085 {
1086 if (lval) /* found it in a CALL_DUMMY frame */
1087 *lval = not_lval;
1088 if (raw_buffer)
1089 memcpy (raw_buffer,
1090 generic_find_dummy_frame (frame->pc, frame->frame) +
1091 REGISTER_BYTE (regnum),
1092 REGISTER_RAW_SIZE (regnum));
1093 return;
1094 }
1095
1096 FRAME_FIND_SAVED_REGS(frame, fsr);
1097 if (fsr.regs[regnum] != 0)
1098 {
1099 if (lval) /* found it saved on the stack */
1100 *lval = lval_memory;
1101 if (regnum == SP_REGNUM)
1102 {
1103 if (raw_buffer) /* SP register treated specially */
1104 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
1105 fsr.regs[regnum]);
1106 }
1107 else
1108 {
1109 if (addrp) /* any other register */
1110 *addrp = fsr.regs[regnum];
1111 if (raw_buffer)
1112 read_memory (fsr.regs[regnum], raw_buffer,
1113 REGISTER_RAW_SIZE (regnum));
1114 }
1115 return;
1116 }
1117 }
1118
1119 /* If we get thru the loop to this point, it means the register was
1120 not saved in any frame. Return the actual live-register value. */
1121
1122 if (lval) /* found it in a live register */
1123 *lval = lval_register;
1124 if (addrp)
1125 *addrp = REGISTER_BYTE (regnum);
1126 if (raw_buffer)
1127 read_register_gen (regnum, raw_buffer);
1128}
1129
bd5635a1
RP
1130void
1131_initialize_blockframe ()
1132{
1133 obstack_init (&frame_cache_obstack);
1134}
This page took 0.333162 seconds and 4 git commands to generate.