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