2003-04-09 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / frame.c
1 /* Cache and manage frames for GDB, the GNU debugger.
2
3 Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4 2001, 2002, 2003 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "target.h"
26 #include "value.h"
27 #include "inferior.h" /* for inferior_ptid */
28 #include "regcache.h"
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
31 #include "builtin-regs.h"
32 #include "gdb_obstack.h"
33 #include "dummy-frame.h"
34 #include "sentinel-frame.h"
35 #include "gdbcore.h"
36 #include "annotate.h"
37 #include "language.h"
38 #include "frame-unwind.h"
39 #include "frame-base.h"
40 #include "command.h"
41 #include "gdbcmd.h"
42
43 /* Flag to control debugging. */
44
45 static int frame_debug;
46
47 /* Flag to indicate whether backtraces should stop at main. */
48
49 static int backtrace_below_main;
50
51 /* Return a frame uniq ID that can be used to, later, re-find the
52 frame. */
53
54 struct frame_id
55 get_frame_id (struct frame_info *fi)
56 {
57 if (fi == NULL)
58 {
59 return null_frame_id;
60 }
61 if (!fi->id_p)
62 {
63 gdb_assert (!legacy_frame_p (current_gdbarch));
64 /* Find THIS frame's ID. */
65 fi->unwind->this_id (fi->next, &fi->prologue_cache, &fi->id);
66 fi->id_p = 1;
67 }
68 return frame_id_build (fi->id.base, get_frame_pc (fi));
69 }
70
71 const struct frame_id null_frame_id; /* All zeros. */
72
73 struct frame_id
74 frame_id_build (CORE_ADDR base, CORE_ADDR func_or_pc)
75 {
76 struct frame_id id;
77 id.base = base;
78 id.pc = func_or_pc;
79 return id;
80 }
81
82 int
83 frame_id_p (struct frame_id l)
84 {
85 /* The .func can be NULL but the .base cannot. */
86 return (l.base != 0);
87 }
88
89 int
90 frame_id_eq (struct frame_id l, struct frame_id r)
91 {
92 /* If .base is different, the frames are different. */
93 if (l.base != r.base)
94 return 0;
95 /* Add a test to check that the frame ID's are for the same function
96 here. */
97 return 1;
98 }
99
100 int
101 frame_id_inner (struct frame_id l, struct frame_id r)
102 {
103 /* Only return non-zero when strictly inner than. Note that, per
104 comment in "frame.h", there is some fuzz here. Frameless
105 functions are not strictly inner than (same .base but different
106 .func). */
107 return INNER_THAN (l.base, r.base);
108 }
109
110 struct frame_info *
111 frame_find_by_id (struct frame_id id)
112 {
113 struct frame_info *frame;
114
115 /* ZERO denotes the null frame, let the caller decide what to do
116 about it. Should it instead return get_current_frame()? */
117 if (!frame_id_p (id))
118 return NULL;
119
120 for (frame = get_current_frame ();
121 frame != NULL;
122 frame = get_prev_frame (frame))
123 {
124 struct frame_id this = get_frame_id (frame);
125 if (frame_id_eq (id, this))
126 /* An exact match. */
127 return frame;
128 if (frame_id_inner (id, this))
129 /* Gone to far. */
130 return NULL;
131 /* Either, we're not yet gone far enough out along the frame
132 chain (inner(this,id), or we're comparing frameless functions
133 (same .base, different .func, no test available). Struggle
134 on until we've definitly gone to far. */
135 }
136 return NULL;
137 }
138
139 CORE_ADDR
140 frame_pc_unwind (struct frame_info *this_frame)
141 {
142 if (!this_frame->prev_pc.p)
143 {
144 CORE_ADDR pc;
145 if (gdbarch_unwind_pc_p (current_gdbarch))
146 {
147 /* The right way. The `pure' way. The one true way. This
148 method depends solely on the register-unwind code to
149 determine the value of registers in THIS frame, and hence
150 the value of this frame's PC (resume address). A typical
151 implementation is no more than:
152
153 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
154 return extract_address (buf, size of ISA_PC_REGNUM);
155
156 Note: this method is very heavily dependent on a correct
157 register-unwind implementation, it pays to fix that
158 method first; this method is frame type agnostic, since
159 it only deals with register values, it works with any
160 frame. This is all in stark contrast to the old
161 FRAME_SAVED_PC which would try to directly handle all the
162 different ways that a PC could be unwound. */
163 pc = gdbarch_unwind_pc (current_gdbarch, this_frame);
164 }
165 else if (this_frame->level < 0)
166 {
167 /* FIXME: cagney/2003-03-06: Old code and and a sentinel
168 frame. Do like was always done. Fetch the PC's value
169 direct from the global registers array (via read_pc).
170 This assumes that this frame belongs to the current
171 global register cache. The assumption is dangerous. */
172 pc = read_pc ();
173 }
174 else if (DEPRECATED_FRAME_SAVED_PC_P ())
175 {
176 /* FIXME: cagney/2003-03-06: Old code, but not a sentinel
177 frame. Do like was always done. Note that this method,
178 unlike unwind_pc(), tries to handle all the different
179 frame cases directly. It fails. */
180 pc = DEPRECATED_FRAME_SAVED_PC (this_frame);
181 }
182 else
183 internal_error (__FILE__, __LINE__, "No gdbarch_unwind_pc method");
184 this_frame->prev_pc.value = pc;
185 this_frame->prev_pc.p = 1;
186 }
187 return this_frame->prev_pc.value;
188 }
189
190 CORE_ADDR
191 frame_func_unwind (struct frame_info *fi)
192 {
193 if (!fi->prev_func.p)
194 {
195 fi->prev_func.p = 1;
196 fi->prev_func.addr = get_pc_function_start (frame_pc_unwind (fi));
197 }
198 return fi->prev_func.addr;
199 }
200
201 CORE_ADDR
202 get_frame_func (struct frame_info *fi)
203 {
204 return frame_func_unwind (fi->next);
205 }
206
207 static int
208 do_frame_unwind_register (void *src, int regnum, void *buf)
209 {
210 frame_unwind_register (src, regnum, buf);
211 return 1;
212 }
213
214 void
215 frame_pop (struct frame_info *this_frame)
216 {
217 struct regcache *scratch_regcache;
218 struct cleanup *cleanups;
219
220 if (DEPRECATED_POP_FRAME_P ())
221 {
222 /* A legacy architecture that has implemented a custom pop
223 function. All new architectures should instead be using the
224 generic code below. */
225 DEPRECATED_POP_FRAME;
226 }
227 else
228 {
229 /* Make a copy of all the register values unwound from this
230 frame. Save them in a scratch buffer so that there isn't a
231 race betweening trying to extract the old values from the
232 current_regcache while, at the same time writing new values
233 into that same cache. */
234 struct regcache *scratch = regcache_xmalloc (current_gdbarch);
235 struct cleanup *cleanups = make_cleanup_regcache_xfree (scratch);
236 regcache_save (scratch, do_frame_unwind_register, this_frame);
237 /* FIXME: cagney/2003-03-16: It should be possible to tell the
238 target's register cache that it is about to be hit with a
239 burst register transfer and that the sequence of register
240 writes should be batched. The pair target_prepare_to_store()
241 and target_store_registers() kind of suggest this
242 functionality. Unfortunatly, they don't implement it. Their
243 lack of a formal definition can lead to targets writing back
244 bogus values (arguably a bug in the target code mind). */
245 /* Now copy those saved registers into the current regcache.
246 Here, regcache_cpy() calls regcache_restore(). */
247 regcache_cpy (current_regcache, scratch);
248 do_cleanups (cleanups);
249 }
250 /* We've made right mess of GDB's local state, just discard
251 everything. */
252 flush_cached_frames ();
253 }
254
255 void
256 frame_register_unwind (struct frame_info *frame, int regnum,
257 int *optimizedp, enum lval_type *lvalp,
258 CORE_ADDR *addrp, int *realnump, void *bufferp)
259 {
260 struct frame_unwind_cache *cache;
261
262 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
263 that the value proper does not need to be fetched. */
264 gdb_assert (optimizedp != NULL);
265 gdb_assert (lvalp != NULL);
266 gdb_assert (addrp != NULL);
267 gdb_assert (realnump != NULL);
268 /* gdb_assert (bufferp != NULL); */
269
270 /* NOTE: cagney/2002-11-27: A program trying to unwind a NULL frame
271 is broken. There is always a frame. If there, for some reason,
272 isn't, there is some pretty busted code as it should have
273 detected the problem before calling here. */
274 gdb_assert (frame != NULL);
275
276 /* Ask this frame to unwind its register. See comment in
277 "frame-unwind.h" for why NEXT frame and this unwind cace are
278 passed in. */
279 frame->unwind->prev_register (frame->next, &frame->prologue_cache, regnum,
280 optimizedp, lvalp, addrp, realnump, bufferp);
281
282 }
283
284 void
285 frame_register (struct frame_info *frame, int regnum,
286 int *optimizedp, enum lval_type *lvalp,
287 CORE_ADDR *addrp, int *realnump, void *bufferp)
288 {
289 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
290 that the value proper does not need to be fetched. */
291 gdb_assert (optimizedp != NULL);
292 gdb_assert (lvalp != NULL);
293 gdb_assert (addrp != NULL);
294 gdb_assert (realnump != NULL);
295 /* gdb_assert (bufferp != NULL); */
296
297 /* Ulgh! Old code that, for lval_register, sets ADDRP to the offset
298 of the register in the register cache. It should instead return
299 the REGNUM corresponding to that register. Translate the . */
300 if (DEPRECATED_GET_SAVED_REGISTER_P ())
301 {
302 DEPRECATED_GET_SAVED_REGISTER (bufferp, optimizedp, addrp, frame,
303 regnum, lvalp);
304 /* Compute the REALNUM if the caller wants it. */
305 if (*lvalp == lval_register)
306 {
307 int regnum;
308 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
309 {
310 if (*addrp == register_offset_hack (current_gdbarch, regnum))
311 {
312 *realnump = regnum;
313 return;
314 }
315 }
316 internal_error (__FILE__, __LINE__,
317 "Failed to compute the register number corresponding"
318 " to 0x%s", paddr_d (*addrp));
319 }
320 *realnump = -1;
321 return;
322 }
323
324 /* Obtain the register value by unwinding the register from the next
325 (more inner frame). */
326 gdb_assert (frame != NULL && frame->next != NULL);
327 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
328 realnump, bufferp);
329 }
330
331 void
332 frame_unwind_register (struct frame_info *frame, int regnum, void *buf)
333 {
334 int optimized;
335 CORE_ADDR addr;
336 int realnum;
337 enum lval_type lval;
338 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
339 &realnum, buf);
340 }
341
342 void
343 frame_unwind_signed_register (struct frame_info *frame, int regnum,
344 LONGEST *val)
345 {
346 void *buf = alloca (MAX_REGISTER_RAW_SIZE);
347 frame_unwind_register (frame, regnum, buf);
348 (*val) = extract_signed_integer (buf, REGISTER_VIRTUAL_SIZE (regnum));
349 }
350
351 void
352 frame_unwind_unsigned_register (struct frame_info *frame, int regnum,
353 ULONGEST *val)
354 {
355 void *buf = alloca (MAX_REGISTER_RAW_SIZE);
356 frame_unwind_register (frame, regnum, buf);
357 (*val) = extract_unsigned_integer (buf, REGISTER_VIRTUAL_SIZE (regnum));
358 }
359
360 void
361 frame_read_register (struct frame_info *frame, int regnum, void *buf)
362 {
363 gdb_assert (frame != NULL && frame->next != NULL);
364 frame_unwind_register (frame->next, regnum, buf);
365 }
366
367 void
368 frame_read_unsigned_register (struct frame_info *frame, int regnum,
369 ULONGEST *val)
370 {
371 /* NOTE: cagney/2002-10-31: There is a bit of dogma here - there is
372 always a frame. Both this, and the equivalent
373 frame_read_signed_register() function, can only be called with a
374 valid frame. If, for some reason, this function is called
375 without a frame then the problem isn't here, but rather in the
376 caller. It should of first created a frame and then passed that
377 in. */
378 /* NOTE: cagney/2002-10-31: As a side bar, keep in mind that the
379 ``current_frame'' should not be treated as a special case. While
380 ``get_next_frame (current_frame) == NULL'' currently holds, it
381 should, as far as possible, not be relied upon. In the future,
382 ``get_next_frame (current_frame)'' may instead simply return a
383 normal frame object that simply always gets register values from
384 the register cache. Consequently, frame code should try to avoid
385 tests like ``if get_next_frame() == NULL'' and instead just rely
386 on recursive frame calls (like the below code) when manipulating
387 a frame chain. */
388 gdb_assert (frame != NULL && frame->next != NULL);
389 frame_unwind_unsigned_register (frame->next, regnum, val);
390 }
391
392 void
393 frame_read_signed_register (struct frame_info *frame, int regnum,
394 LONGEST *val)
395 {
396 /* See note above in frame_read_unsigned_register(). */
397 gdb_assert (frame != NULL && frame->next != NULL);
398 frame_unwind_signed_register (frame->next, regnum, val);
399 }
400
401 void
402 generic_unwind_get_saved_register (char *raw_buffer,
403 int *optimizedp,
404 CORE_ADDR *addrp,
405 struct frame_info *frame,
406 int regnum,
407 enum lval_type *lvalp)
408 {
409 int optimizedx;
410 CORE_ADDR addrx;
411 int realnumx;
412 enum lval_type lvalx;
413
414 if (!target_has_registers)
415 error ("No registers.");
416
417 /* Keep things simple, ensure that all the pointers (except valuep)
418 are non NULL. */
419 if (optimizedp == NULL)
420 optimizedp = &optimizedx;
421 if (lvalp == NULL)
422 lvalp = &lvalx;
423 if (addrp == NULL)
424 addrp = &addrx;
425
426 gdb_assert (frame != NULL && frame->next != NULL);
427 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
428 &realnumx, raw_buffer);
429 }
430
431 /* frame_register_read ()
432
433 Find and return the value of REGNUM for the specified stack frame.
434 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
435
436 Returns 0 if the register value could not be found. */
437
438 int
439 frame_register_read (struct frame_info *frame, int regnum, void *myaddr)
440 {
441 int optimized;
442 enum lval_type lval;
443 CORE_ADDR addr;
444 int realnum;
445 frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
446
447 /* FIXME: cagney/2002-05-15: This test, is just bogus.
448
449 It indicates that the target failed to supply a value for a
450 register because it was "not available" at this time. Problem
451 is, the target still has the register and so get saved_register()
452 may be returning a value saved on the stack. */
453
454 if (register_cached (regnum) < 0)
455 return 0; /* register value not available */
456
457 return !optimized;
458 }
459
460
461 /* Map between a frame register number and its name. A frame register
462 space is a superset of the cooked register space --- it also
463 includes builtin registers. */
464
465 int
466 frame_map_name_to_regnum (const char *name, int len)
467 {
468 int i;
469
470 if (len < 0)
471 len = strlen (name);
472
473 /* Search register name space. */
474 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
475 if (REGISTER_NAME (i) && len == strlen (REGISTER_NAME (i))
476 && strncmp (name, REGISTER_NAME (i), len) == 0)
477 {
478 return i;
479 }
480
481 /* Try builtin registers. */
482 i = builtin_reg_map_name_to_regnum (name, len);
483 if (i >= 0)
484 {
485 /* A builtin register doesn't fall into the architecture's
486 register range. */
487 gdb_assert (i >= NUM_REGS + NUM_PSEUDO_REGS);
488 return i;
489 }
490
491 return -1;
492 }
493
494 const char *
495 frame_map_regnum_to_name (int regnum)
496 {
497 if (regnum < 0)
498 return NULL;
499 if (regnum < NUM_REGS + NUM_PSEUDO_REGS)
500 return REGISTER_NAME (regnum);
501 return builtin_reg_map_regnum_to_name (regnum);
502 }
503
504 /* Create a sentinel frame. */
505
506 struct frame_info *
507 create_sentinel_frame (struct regcache *regcache)
508 {
509 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
510 frame->type = NORMAL_FRAME;
511 frame->level = -1;
512 /* Explicitly initialize the sentinel frame's cache. Provide it
513 with the underlying regcache. In the future additional
514 information, such as the frame's thread will be added. */
515 frame->prologue_cache = sentinel_frame_cache (regcache);
516 /* For the moment there is only one sentinel frame implementation. */
517 frame->unwind = sentinel_frame_unwind;
518 /* Link this frame back to itself. The frame is self referential
519 (the unwound PC is the same as the pc), so make it so. */
520 frame->next = frame;
521 /* Make the sentinel frame's ID valid, but invalid. That way all
522 comparisons with it should fail. */
523 frame->id_p = 1;
524 frame->id = null_frame_id;
525 return frame;
526 }
527
528 /* Info about the innermost stack frame (contents of FP register) */
529
530 static struct frame_info *current_frame;
531
532 /* Cache for frame addresses already read by gdb. Valid only while
533 inferior is stopped. Control variables for the frame cache should
534 be local to this module. */
535
536 static struct obstack frame_cache_obstack;
537
538 void *
539 frame_obstack_zalloc (unsigned long size)
540 {
541 void *data = obstack_alloc (&frame_cache_obstack, size);
542 memset (data, 0, size);
543 return data;
544 }
545
546 CORE_ADDR *
547 frame_saved_regs_zalloc (struct frame_info *fi)
548 {
549 fi->saved_regs = (CORE_ADDR *)
550 frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS);
551 return fi->saved_regs;
552 }
553
554 CORE_ADDR *
555 get_frame_saved_regs (struct frame_info *fi)
556 {
557 return fi->saved_regs;
558 }
559
560 /* Return the innermost (currently executing) stack frame. This is
561 split into two functions. The function unwind_to_current_frame()
562 is wrapped in catch exceptions so that, even when the unwind of the
563 sentinel frame fails, the function still returns a stack frame. */
564
565 static int
566 unwind_to_current_frame (struct ui_out *ui_out, void *args)
567 {
568 struct frame_info *frame = get_prev_frame (args);
569 /* A sentinel frame can fail to unwind, eg, because it's PC value
570 lands in somewhere like start. */
571 if (frame == NULL)
572 return 1;
573 current_frame = frame;
574 return 0;
575 }
576
577 struct frame_info *
578 get_current_frame (void)
579 {
580 /* First check, and report, the lack of registers. Having GDB
581 report "No stack!" or "No memory" when the target doesn't even
582 have registers is very confusing. Besides, "printcmd.exp"
583 explicitly checks that ``print $pc'' with no registers prints "No
584 registers". */
585 if (!target_has_registers)
586 error ("No registers.");
587 if (!target_has_stack)
588 error ("No stack.");
589 if (!target_has_memory)
590 error ("No memory.");
591 if (current_frame == NULL)
592 {
593 struct frame_info *sentinel_frame =
594 create_sentinel_frame (current_regcache);
595 if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
596 NULL, RETURN_MASK_ERROR) != 0)
597 {
598 /* Oops! Fake a current frame? Is this useful? It has a PC
599 of zero, for instance. */
600 current_frame = sentinel_frame;
601 }
602 }
603 return current_frame;
604 }
605
606 /* The "selected" stack frame is used by default for local and arg
607 access. May be zero, for no selected frame. */
608
609 struct frame_info *deprecated_selected_frame;
610
611 /* Return the selected frame. Always non-null (unless there isn't an
612 inferior sufficient for creating a frame) in which case an error is
613 thrown. */
614
615 struct frame_info *
616 get_selected_frame (void)
617 {
618 if (deprecated_selected_frame == NULL)
619 /* Hey! Don't trust this. It should really be re-finding the
620 last selected frame of the currently selected thread. This,
621 though, is better than nothing. */
622 select_frame (get_current_frame ());
623 /* There is always a frame. */
624 gdb_assert (deprecated_selected_frame != NULL);
625 return deprecated_selected_frame;
626 }
627
628 /* Select frame FI (or NULL - to invalidate the current frame). */
629
630 void
631 select_frame (struct frame_info *fi)
632 {
633 register struct symtab *s;
634
635 deprecated_selected_frame = fi;
636 /* NOTE: cagney/2002-05-04: FI can be NULL. This occures when the
637 frame is being invalidated. */
638 if (selected_frame_level_changed_hook)
639 selected_frame_level_changed_hook (frame_relative_level (fi));
640
641 /* FIXME: kseitz/2002-08-28: It would be nice to call
642 selected_frame_level_changed_event right here, but due to limitations
643 in the current interfaces, we would end up flooding UIs with events
644 because select_frame is used extensively internally.
645
646 Once we have frame-parameterized frame (and frame-related) commands,
647 the event notification can be moved here, since this function will only
648 be called when the users selected frame is being changed. */
649
650 /* Ensure that symbols for this frame are read in. Also, determine the
651 source language of this frame, and switch to it if desired. */
652 if (fi)
653 {
654 s = find_pc_symtab (get_frame_pc (fi));
655 if (s
656 && s->language != current_language->la_language
657 && s->language != language_unknown
658 && language_mode == language_mode_auto)
659 {
660 set_language (s->language);
661 }
662 }
663 }
664
665 /* Return the register saved in the simplistic ``saved_regs'' cache.
666 If the value isn't here AND a value is needed, try the next inner
667 most frame. */
668
669 static void
670 legacy_saved_regs_prev_register (struct frame_info *next_frame,
671 void **this_prologue_cache,
672 int regnum, int *optimizedp,
673 enum lval_type *lvalp, CORE_ADDR *addrp,
674 int *realnump, void *bufferp)
675 {
676 /* HACK: New code is passed the next frame and this cache.
677 Unfortunatly, old code expects this frame. Since this is a
678 backward compatibility hack, cheat by walking one level along the
679 prologue chain to the frame the old code expects.
680
681 Do not try this at home. Professional driver, closed course. */
682 struct frame_info *frame = next_frame->prev;
683 gdb_assert (frame != NULL);
684
685 /* Only (older) architectures that implement the
686 DEPRECATED_FRAME_INIT_SAVED_REGS method should be using this
687 function. */
688 gdb_assert (DEPRECATED_FRAME_INIT_SAVED_REGS_P ());
689
690 /* Load the saved_regs register cache. */
691 if (get_frame_saved_regs (frame) == NULL)
692 DEPRECATED_FRAME_INIT_SAVED_REGS (frame);
693
694 if (get_frame_saved_regs (frame) != NULL
695 && get_frame_saved_regs (frame)[regnum] != 0)
696 {
697 if (regnum == SP_REGNUM)
698 {
699 /* SP register treated specially. */
700 *optimizedp = 0;
701 *lvalp = not_lval;
702 *addrp = 0;
703 *realnump = -1;
704 if (bufferp != NULL)
705 store_address (bufferp, REGISTER_RAW_SIZE (regnum),
706 get_frame_saved_regs (frame)[regnum]);
707 }
708 else
709 {
710 /* Any other register is saved in memory, fetch it but cache
711 a local copy of its value. */
712 *optimizedp = 0;
713 *lvalp = lval_memory;
714 *addrp = get_frame_saved_regs (frame)[regnum];
715 *realnump = -1;
716 if (bufferp != NULL)
717 {
718 #if 1
719 /* Save each register value, as it is read in, in a
720 frame based cache. */
721 void **regs = (*this_prologue_cache);
722 if (regs == NULL)
723 {
724 int sizeof_cache = ((NUM_REGS + NUM_PSEUDO_REGS)
725 * sizeof (void *));
726 regs = frame_obstack_zalloc (sizeof_cache);
727 (*this_prologue_cache) = regs;
728 }
729 if (regs[regnum] == NULL)
730 {
731 regs[regnum]
732 = frame_obstack_zalloc (REGISTER_RAW_SIZE (regnum));
733 read_memory (get_frame_saved_regs (frame)[regnum], regs[regnum],
734 REGISTER_RAW_SIZE (regnum));
735 }
736 memcpy (bufferp, regs[regnum], REGISTER_RAW_SIZE (regnum));
737 #else
738 /* Read the value in from memory. */
739 read_memory (get_frame_saved_regs (frame)[regnum], bufferp,
740 REGISTER_RAW_SIZE (regnum));
741 #endif
742 }
743 }
744 return;
745 }
746
747 /* No luck. Assume this and the next frame have the same register
748 value. Pass the unwind request down the frame chain to the next
749 frame. Hopefully that frame will find the register's location. */
750 frame_register_unwind (next_frame, regnum, optimizedp, lvalp, addrp,
751 realnump, bufferp);
752 }
753
754 static void
755 legacy_saved_regs_this_id (struct frame_info *next_frame,
756 void **this_prologue_cache,
757 struct frame_id *id)
758 {
759 int fromleaf;
760 CORE_ADDR base;
761 CORE_ADDR pc;
762
763 if (frame_relative_level (next_frame) < 0)
764 {
765 /* FIXME: cagney/2003-03-14: We've got the extra special case of
766 unwinding a sentinel frame, the PC of which is pointing at a
767 stack dummy. Fake up the dummy frame's ID using the same
768 sequence as is found a traditional unwinder. */
769 (*id) = frame_id_build (read_fp (), read_pc ());
770 return;
771 }
772
773 /* Start out by assuming it's NULL. */
774 (*id) = null_frame_id;
775
776 if (frame_relative_level (next_frame) <= 0)
777 /* FIXME: 2002-11-09: Frameless functions can occure anywhere in
778 the frame chain, not just the inner most frame! The generic,
779 per-architecture, frame code should handle this and the below
780 should simply be removed. */
781 fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame);
782 else
783 fromleaf = 0;
784
785 if (fromleaf)
786 /* A frameless inner-most frame. The `FP' (which isn't an
787 architecture frame-pointer register!) of the caller is the same
788 as the callee. */
789 /* FIXME: 2002-11-09: There isn't any reason to special case this
790 edge condition. Instead the per-architecture code should hande
791 it locally. */
792 base = get_frame_base (next_frame);
793 else
794 {
795 /* Two macros defined in tm.h specify the machine-dependent
796 actions to be performed here.
797
798 First, get the frame's chain-pointer.
799
800 If that is zero, the frame is the outermost frame or a leaf
801 called by the outermost frame. This means that if start
802 calls main without a frame, we'll return 0 (which is fine
803 anyway).
804
805 Nope; there's a problem. This also returns when the current
806 routine is a leaf of main. This is unacceptable. We move
807 this to after the ffi test; I'd rather have backtraces from
808 start go curfluy than have an abort called from main not show
809 main. */
810 gdb_assert (DEPRECATED_FRAME_CHAIN_P ());
811 base = DEPRECATED_FRAME_CHAIN (next_frame);
812
813 if (!legacy_frame_chain_valid (base, next_frame))
814 return;
815 }
816 if (base == 0)
817 return;
818
819 /* FIXME: cagney/2002-06-08: This should probably return the frame's
820 function and not the PC (a.k.a. resume address). */
821 pc = frame_pc_unwind (next_frame);
822 (*id) = frame_id_build (base, pc);
823 }
824
825 const struct frame_unwind legacy_saved_regs_unwinder = {
826 /* Not really. It gets overridden by legacy_get_prev_frame. */
827 UNKNOWN_FRAME,
828 legacy_saved_regs_this_id,
829 legacy_saved_regs_prev_register
830 };
831 const struct frame_unwind *legacy_saved_regs_unwind = &legacy_saved_regs_unwinder;
832
833
834 /* Function: deprecated_generic_get_saved_register
835 Find register number REGNUM relative to FRAME and put its (raw,
836 target format) contents in *RAW_BUFFER.
837
838 Set *OPTIMIZED if the variable was optimized out (and thus can't be
839 fetched). Note that this is never set to anything other than zero
840 in this implementation.
841
842 Set *LVAL to lval_memory, lval_register, or not_lval, depending on
843 whether the value was fetched from memory, from a register, or in a
844 strange and non-modifiable way (e.g. a frame pointer which was
845 calculated rather than fetched). We will use not_lval for values
846 fetched from generic dummy frames.
847
848 Set *ADDRP to the address, either in memory or as a REGISTER_BYTE
849 offset into the registers array. If the value is stored in a dummy
850 frame, set *ADDRP to zero.
851
852 The argument RAW_BUFFER must point to aligned memory. */
853
854 void
855 deprecated_generic_get_saved_register (char *raw_buffer, int *optimized,
856 CORE_ADDR *addrp,
857 struct frame_info *frame, int regnum,
858 enum lval_type *lval)
859 {
860 if (!target_has_registers)
861 error ("No registers.");
862
863 gdb_assert (DEPRECATED_FRAME_INIT_SAVED_REGS_P ());
864
865 /* Normal systems don't optimize out things with register numbers. */
866 if (optimized != NULL)
867 *optimized = 0;
868
869 if (addrp) /* default assumption: not found in memory */
870 *addrp = 0;
871
872 /* Note: since the current frame's registers could only have been
873 saved by frames INTERIOR TO the current frame, we skip examining
874 the current frame itself: otherwise, we would be getting the
875 previous frame's registers which were saved by the current frame. */
876
877 if (frame != NULL)
878 {
879 for (frame = get_next_frame (frame);
880 frame_relative_level (frame) >= 0;
881 frame = get_next_frame (frame))
882 {
883 if (get_frame_type (frame) == DUMMY_FRAME)
884 {
885 if (lval) /* found it in a CALL_DUMMY frame */
886 *lval = not_lval;
887 if (raw_buffer)
888 /* FIXME: cagney/2002-06-26: This should be via the
889 gdbarch_register_read() method so that it, on the
890 fly, constructs either a raw or pseudo register
891 from the raw register cache. */
892 regcache_raw_read
893 (generic_find_dummy_frame (get_frame_pc (frame),
894 get_frame_base (frame)),
895 regnum, raw_buffer);
896 return;
897 }
898
899 DEPRECATED_FRAME_INIT_SAVED_REGS (frame);
900 if (get_frame_saved_regs (frame) != NULL
901 && get_frame_saved_regs (frame)[regnum] != 0)
902 {
903 if (lval) /* found it saved on the stack */
904 *lval = lval_memory;
905 if (regnum == SP_REGNUM)
906 {
907 if (raw_buffer) /* SP register treated specially */
908 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
909 get_frame_saved_regs (frame)[regnum]);
910 }
911 else
912 {
913 if (addrp) /* any other register */
914 *addrp = get_frame_saved_regs (frame)[regnum];
915 if (raw_buffer)
916 read_memory (get_frame_saved_regs (frame)[regnum], raw_buffer,
917 REGISTER_RAW_SIZE (regnum));
918 }
919 return;
920 }
921 }
922 }
923
924 /* If we get thru the loop to this point, it means the register was
925 not saved in any frame. Return the actual live-register value. */
926
927 if (lval) /* found it in a live register */
928 *lval = lval_register;
929 if (addrp)
930 *addrp = REGISTER_BYTE (regnum);
931 if (raw_buffer)
932 deprecated_read_register_gen (regnum, raw_buffer);
933 }
934
935 /* Determine the frame's type based on its PC. */
936
937 static enum frame_type
938 frame_type_from_pc (CORE_ADDR pc)
939 {
940 /* FIXME: cagney/2002-11-24: Can't yet directly call
941 pc_in_dummy_frame() as some architectures don't set
942 PC_IN_CALL_DUMMY() to generic_pc_in_call_dummy() (remember the
943 latter is implemented by simply calling pc_in_dummy_frame). */
944 if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
945 && DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0))
946 return DUMMY_FRAME;
947 else
948 {
949 char *name;
950 find_pc_partial_function (pc, &name, NULL, NULL);
951 if (PC_IN_SIGTRAMP (pc, name))
952 return SIGTRAMP_FRAME;
953 else
954 return NORMAL_FRAME;
955 }
956 }
957
958 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
959 Always returns a non-NULL value. */
960
961 struct frame_info *
962 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
963 {
964 struct frame_info *fi;
965
966 fi = frame_obstack_zalloc (sizeof (struct frame_info));
967
968 fi->next = create_sentinel_frame (current_regcache);
969
970 /* Select/initialize both the unwind function and the frame's type
971 based on the PC. */
972 fi->unwind = frame_unwind_find_by_pc (current_gdbarch, pc);
973 if (fi->unwind->type != UNKNOWN_FRAME)
974 fi->type = fi->unwind->type;
975 else
976 fi->type = frame_type_from_pc (pc);
977
978 deprecated_update_frame_base_hack (fi, addr);
979 deprecated_update_frame_pc_hack (fi, pc);
980
981 if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ())
982 DEPRECATED_INIT_EXTRA_FRAME_INFO (0, fi);
983
984 return fi;
985 }
986
987 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
988 innermost frame). Be careful to not fall off the bottom of the
989 frame chain and onto the sentinel frame. */
990
991 struct frame_info *
992 get_next_frame (struct frame_info *this_frame)
993 {
994 if (this_frame->level > 0)
995 return this_frame->next;
996 else
997 return NULL;
998 }
999
1000 /* Flush the entire frame cache. */
1001
1002 void
1003 flush_cached_frames (void)
1004 {
1005 /* Since we can't really be sure what the first object allocated was */
1006 obstack_free (&frame_cache_obstack, 0);
1007 obstack_init (&frame_cache_obstack);
1008
1009 current_frame = NULL; /* Invalidate cache */
1010 select_frame (NULL);
1011 annotate_frames_invalid ();
1012 }
1013
1014 /* Flush the frame cache, and start a new one if necessary. */
1015
1016 void
1017 reinit_frame_cache (void)
1018 {
1019 flush_cached_frames ();
1020
1021 /* FIXME: The inferior_ptid test is wrong if there is a corefile. */
1022 if (PIDGET (inferior_ptid) != 0)
1023 {
1024 select_frame (get_current_frame ());
1025 }
1026 }
1027
1028 /* Create the previous frame using the deprecated methods
1029 INIT_EXTRA_INFO, INIT_FRAME_PC and INIT_FRAME_PC_FIRST. */
1030
1031 static struct frame_info *
1032 legacy_get_prev_frame (struct frame_info *this_frame)
1033 {
1034 CORE_ADDR address = 0;
1035 struct frame_info *prev;
1036 int fromleaf;
1037
1038 /* Allocate the new frame.
1039
1040 There is no reason to worry about memory leaks, should the
1041 remainder of the function fail. The allocated memory will be
1042 quickly reclaimed when the frame cache is flushed, and the `we've
1043 been here before' check, in get_prev_frame will stop repeated
1044 memory allocation calls. */
1045 prev = FRAME_OBSTACK_ZALLOC (struct frame_info);
1046 prev->level = this_frame->level + 1;
1047
1048 /* Do not completly wire it in to the frame chain. Some (bad) code
1049 in INIT_FRAME_EXTRA_INFO tries to look along frame->prev to pull
1050 some fancy tricks (of course such code is, by definition,
1051 recursive).
1052
1053 On the other hand, methods, such as get_frame_pc() and
1054 get_frame_base() rely on being able to walk along the frame
1055 chain. Make certain that at least they work by providing that
1056 link. Of course things manipulating prev can't go back. */
1057 prev->next = this_frame;
1058
1059 /* NOTE: cagney/2002-11-18: Should have been correctly setting the
1060 frame's type here, before anything else, and not last, at the
1061 bottom of this function. The various
1062 DEPRECATED_INIT_EXTRA_FRAME_INFO, DEPRECATED_INIT_FRAME_PC,
1063 DEPRECATED_INIT_FRAME_PC_FIRST and
1064 DEPRECATED_FRAME_INIT_SAVED_REGS methods are full of work-arounds
1065 that handle the frame not being correctly set from the start.
1066 Unfortunatly those same work-arounds rely on the type defaulting
1067 to NORMAL_FRAME. Ulgh! The new frame code does not have this
1068 problem. */
1069 prev->type = UNKNOWN_FRAME;
1070
1071 /* A legacy frame's ID is always computed here. Mark it as valid. */
1072 prev->id_p = 1;
1073
1074 /* Handle sentinel frame unwind as a special case. */
1075 if (this_frame->level < 0)
1076 {
1077 /* Try to unwind the PC. If that doesn't work, assume we've reached
1078 the oldest frame and simply return. Is there a better sentinal
1079 value? The unwound PC value is then used to initialize the new
1080 previous frame's type.
1081
1082 Note that the pc-unwind is intentionally performed before the
1083 frame chain. This is ok since, for old targets, both
1084 frame_pc_unwind (nee, DEPRECATED_FRAME_SAVED_PC) and
1085 DEPRECATED_FRAME_CHAIN()) assume THIS_FRAME's data structures
1086 have already been initialized (using
1087 DEPRECATED_INIT_EXTRA_FRAME_INFO) and hence the call order
1088 doesn't matter.
1089
1090 By unwinding the PC first, it becomes possible to, in the case of
1091 a dummy frame, avoid also unwinding the frame ID. This is
1092 because (well ignoring the PPC) a dummy frame can be located
1093 using THIS_FRAME's frame ID. */
1094
1095 deprecated_update_frame_pc_hack (prev, frame_pc_unwind (this_frame));
1096 if (get_frame_pc (prev) == 0)
1097 {
1098 /* The allocated PREV_FRAME will be reclaimed when the frame
1099 obstack is next purged. */
1100 if (frame_debug)
1101 fprintf_unfiltered (gdb_stdlog,
1102 "Outermost frame - unwound PC zero\n");
1103 return NULL;
1104 }
1105
1106 /* Set the unwind functions based on that identified PC. Ditto
1107 for the "type" but strongly prefer the unwinder's frame type. */
1108 prev->unwind = frame_unwind_find_by_pc (current_gdbarch,
1109 get_frame_pc (prev));
1110 if (prev->unwind->type == UNKNOWN_FRAME)
1111 prev->type = frame_type_from_pc (get_frame_pc (prev));
1112 else
1113 prev->type = prev->unwind->type;
1114
1115 /* Find the prev's frame's ID. */
1116 if (prev->type == DUMMY_FRAME
1117 && gdbarch_unwind_dummy_id_p (current_gdbarch))
1118 {
1119 /* When unwinding a normal frame, the stack structure is
1120 determined by analyzing the frame's function's code (be
1121 it using brute force prologue analysis, or the dwarf2
1122 CFI). In the case of a dummy frame, that simply isn't
1123 possible. The The PC is either the program entry point,
1124 or some random address on the stack. Trying to use that
1125 PC to apply standard frame ID unwind techniques is just
1126 asking for trouble. */
1127 /* Assume call_function_by_hand(), via SAVE_DUMMY_FRAME_TOS,
1128 previously saved the dummy frame's ID. Things only work
1129 if the two return the same value. */
1130 gdb_assert (SAVE_DUMMY_FRAME_TOS_P ());
1131 /* Use an architecture specific method to extract the prev's
1132 dummy ID from the next frame. Note that this method uses
1133 frame_register_unwind to obtain the register values
1134 needed to determine the dummy frame's ID. */
1135 prev->id = gdbarch_unwind_dummy_id (current_gdbarch, this_frame);
1136 }
1137 else
1138 {
1139 /* We're unwinding a sentinel frame, the PC of which is
1140 pointing at a stack dummy. Fake up the dummy frame's ID
1141 using the same sequence as is found a traditional
1142 unwinder. Once all architectures supply the
1143 unwind_dummy_id method, this code can go away. */
1144 prev->id = frame_id_build (read_fp (), read_pc ());
1145 }
1146
1147 /* Check that the unwound ID is valid. */
1148 if (!frame_id_p (prev->id))
1149 {
1150 if (frame_debug)
1151 fprintf_unfiltered (gdb_stdlog,
1152 "Outermost legacy sentinel frame - unwound frame ID invalid\n");
1153 return NULL;
1154 }
1155
1156 /* Check that the new frame isn't inner to (younger, below,
1157 next) the old frame. If that happens the frame unwind is
1158 going backwards. */
1159 /* FIXME: cagney/2003-02-25: Ignore the sentinel frame since
1160 that doesn't have a valid frame ID. Should instead set the
1161 sentinel frame's frame ID to a `sentinel'. Leave it until
1162 after the switch to storing the frame ID, instead of the
1163 frame base, in the frame object. */
1164
1165 /* Link it in. */
1166 this_frame->prev = prev;
1167
1168 /* FIXME: cagney/2002-01-19: This call will go away. Instead of
1169 initializing extra info, all frames will use the frame_cache
1170 (passed to the unwind functions) to store additional frame
1171 info. Unfortunatly legacy targets can't use
1172 legacy_get_prev_frame() to unwind the sentinel frame and,
1173 consequently, are forced to take this code path and rely on
1174 the below call to DEPRECATED_INIT_EXTRA_FRAME_INFO to
1175 initialize the inner-most frame. */
1176 if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ())
1177 {
1178 DEPRECATED_INIT_EXTRA_FRAME_INFO (0, prev);
1179 }
1180 return prev;
1181 }
1182
1183 /* This code only works on normal frames. A sentinel frame, where
1184 the level is -1, should never reach this code. */
1185 gdb_assert (this_frame->level >= 0);
1186
1187 /* On some machines it is possible to call a function without
1188 setting up a stack frame for it. On these machines, we
1189 define this macro to take two args; a frameinfo pointer
1190 identifying a frame and a variable to set or clear if it is
1191 or isn't leafless. */
1192
1193 /* Still don't want to worry about this except on the innermost
1194 frame. This macro will set FROMLEAF if THIS_FRAME is a frameless
1195 function invocation. */
1196 if (this_frame->level == 0)
1197 /* FIXME: 2002-11-09: Frameless functions can occure anywhere in
1198 the frame chain, not just the inner most frame! The generic,
1199 per-architecture, frame code should handle this and the below
1200 should simply be removed. */
1201 fromleaf = FRAMELESS_FUNCTION_INVOCATION (this_frame);
1202 else
1203 fromleaf = 0;
1204
1205 if (fromleaf)
1206 /* A frameless inner-most frame. The `FP' (which isn't an
1207 architecture frame-pointer register!) of the caller is the same
1208 as the callee. */
1209 /* FIXME: 2002-11-09: There isn't any reason to special case this
1210 edge condition. Instead the per-architecture code should hande
1211 it locally. */
1212 address = get_frame_base (this_frame);
1213 else
1214 {
1215 /* Two macros defined in tm.h specify the machine-dependent
1216 actions to be performed here.
1217
1218 First, get the frame's chain-pointer.
1219
1220 If that is zero, the frame is the outermost frame or a leaf
1221 called by the outermost frame. This means that if start
1222 calls main without a frame, we'll return 0 (which is fine
1223 anyway).
1224
1225 Nope; there's a problem. This also returns when the current
1226 routine is a leaf of main. This is unacceptable. We move
1227 this to after the ffi test; I'd rather have backtraces from
1228 start go curfluy than have an abort called from main not show
1229 main. */
1230 gdb_assert (DEPRECATED_FRAME_CHAIN_P ());
1231 address = DEPRECATED_FRAME_CHAIN (this_frame);
1232
1233 if (!legacy_frame_chain_valid (address, this_frame))
1234 return 0;
1235 }
1236 if (address == 0)
1237 return 0;
1238
1239 /* Link in the already allocated prev frame. */
1240 this_frame->prev = prev;
1241 deprecated_update_frame_base_hack (prev, address);
1242
1243 /* This change should not be needed, FIXME! We should determine
1244 whether any targets *need* DEPRECATED_INIT_FRAME_PC to happen
1245 after DEPRECATED_INIT_EXTRA_FRAME_INFO and come up with a simple
1246 way to express what goes on here.
1247
1248 DEPRECATED_INIT_EXTRA_FRAME_INFO is called from two places:
1249 create_new_frame (where the PC is already set up) and here (where
1250 it isn't). DEPRECATED_INIT_FRAME_PC is only called from here,
1251 always after DEPRECATED_INIT_EXTRA_FRAME_INFO.
1252
1253 The catch is the MIPS, where DEPRECATED_INIT_EXTRA_FRAME_INFO
1254 requires the PC value (which hasn't been set yet). Some other
1255 machines appear to require DEPRECATED_INIT_EXTRA_FRAME_INFO
1256 before they can do DEPRECATED_INIT_FRAME_PC. Phoo.
1257
1258 We shouldn't need DEPRECATED_INIT_FRAME_PC_FIRST to add more
1259 complication to an already overcomplicated part of GDB.
1260 gnu@cygnus.com, 15Sep92.
1261
1262 Assuming that some machines need DEPRECATED_INIT_FRAME_PC after
1263 DEPRECATED_INIT_EXTRA_FRAME_INFO, one possible scheme:
1264
1265 SETUP_INNERMOST_FRAME(): Default version is just create_new_frame
1266 (read_fp ()), read_pc ()). Machines with extra frame info would
1267 do that (or the local equivalent) and then set the extra fields.
1268
1269 SETUP_ARBITRARY_FRAME(argc, argv): Only change here is that
1270 create_new_frame would no longer init extra frame info;
1271 SETUP_ARBITRARY_FRAME would have to do that.
1272
1273 INIT_PREV_FRAME(fromleaf, prev) Replace
1274 DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC.
1275 This should also return a flag saying whether to keep the new
1276 frame, or whether to discard it, because on some machines (e.g.
1277 mips) it is really awkward to have DEPRECATED_FRAME_CHAIN_VALID
1278 called BEFORE DEPRECATED_INIT_EXTRA_FRAME_INFO (there is no good
1279 way to get information deduced in DEPRECATED_FRAME_CHAIN_VALID
1280 into the extra fields of the new frame). std_frame_pc(fromleaf,
1281 prev)
1282
1283 This is the default setting for INIT_PREV_FRAME. It just does
1284 what the default DEPRECATED_INIT_FRAME_PC does. Some machines
1285 will call it from INIT_PREV_FRAME (either at the beginning, the
1286 end, or in the middle). Some machines won't use it.
1287
1288 kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */
1289
1290 /* NOTE: cagney/2002-11-09: Just ignore the above! There is no
1291 reason for things to be this complicated.
1292
1293 The trick is to assume that there is always a frame. Instead of
1294 special casing the inner-most frame, create fake frame
1295 (containing the hardware registers) that is inner to the
1296 user-visible inner-most frame (...) and then unwind from that.
1297 That way architecture code can use use the standard
1298 frame_XX_unwind() functions and not differentiate between the
1299 inner most and any other case.
1300
1301 Since there is always a frame to unwind from, there is always
1302 somewhere (THIS_FRAME) to store all the info needed to construct
1303 a new (previous) frame without having to first create it. This
1304 means that the convolution below - needing to carefully order a
1305 frame's initialization - isn't needed.
1306
1307 The irony here though, is that DEPRECATED_FRAME_CHAIN(), at least
1308 for a more up-to-date architecture, always calls
1309 FRAME_SAVED_PC(), and FRAME_SAVED_PC() computes the PC but
1310 without first needing the frame! Instead of the convolution
1311 below, we could have simply called FRAME_SAVED_PC() and been done
1312 with it! Note that FRAME_SAVED_PC() is being superseed by
1313 frame_pc_unwind() and that function does have somewhere to cache
1314 that PC value. */
1315
1316 if (DEPRECATED_INIT_FRAME_PC_FIRST_P ())
1317 deprecated_update_frame_pc_hack (prev,
1318 DEPRECATED_INIT_FRAME_PC_FIRST (fromleaf,
1319 prev));
1320
1321 if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ())
1322 DEPRECATED_INIT_EXTRA_FRAME_INFO (fromleaf, prev);
1323
1324 /* This entry is in the frame queue now, which is good since
1325 FRAME_SAVED_PC may use that queue to figure out its value (see
1326 tm-sparc.h). We want the pc saved in the inferior frame. */
1327 if (DEPRECATED_INIT_FRAME_PC_P ())
1328 deprecated_update_frame_pc_hack (prev,
1329 DEPRECATED_INIT_FRAME_PC (fromleaf,
1330 prev));
1331
1332 /* If ->frame and ->pc are unchanged, we are in the process of
1333 getting ourselves into an infinite backtrace. Some architectures
1334 check this in DEPRECATED_FRAME_CHAIN or thereabouts, but it seems
1335 like there is no reason this can't be an architecture-independent
1336 check. */
1337 if (get_frame_base (prev) == get_frame_base (this_frame)
1338 && get_frame_pc (prev) == get_frame_pc (this_frame))
1339 {
1340 this_frame->prev = NULL;
1341 obstack_free (&frame_cache_obstack, prev);
1342 return NULL;
1343 }
1344
1345 /* Initialize the code used to unwind the frame PREV based on the PC
1346 (and probably other architectural information). The PC lets you
1347 check things like the debug info at that point (dwarf2cfi?) and
1348 use that to decide how the frame should be unwound. */
1349 prev->unwind = frame_unwind_find_by_pc (current_gdbarch,
1350 get_frame_pc (prev));
1351
1352 /* If the unwinder provides a frame type, use it. Otherwize
1353 continue on to that heuristic mess. */
1354 if (prev->unwind->type != UNKNOWN_FRAME)
1355 {
1356 prev->type = prev->unwind->type;
1357 return prev;
1358 }
1359
1360 /* NOTE: cagney/2002-11-18: The code segments, found in
1361 create_new_frame and get_prev_frame(), that initializes the
1362 frames type is subtly different. The latter only updates ->type
1363 when it encounters a SIGTRAMP_FRAME or DUMMY_FRAME. This stops
1364 get_prev_frame() overriding the frame's type when the INIT code
1365 has previously set it. This is really somewhat bogus. The
1366 initialization, as seen in create_new_frame(), should occur
1367 before the INIT function has been called. */
1368 if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
1369 && (DEPRECATED_PC_IN_CALL_DUMMY_P ()
1370 ? DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (prev), 0, 0)
1371 : pc_in_dummy_frame (get_frame_pc (prev))))
1372 prev->type = DUMMY_FRAME;
1373 else
1374 {
1375 /* FIXME: cagney/2002-11-10: This should be moved to before the
1376 INIT code above so that the INIT code knows what the frame's
1377 type is (in fact, for a [generic] dummy-frame, the type can
1378 be set and then the entire initialization can be skipped.
1379 Unforunatly, its the INIT code that sets the PC (Hmm, catch
1380 22). */
1381 char *name;
1382 find_pc_partial_function (get_frame_pc (prev), &name, NULL, NULL);
1383 if (PC_IN_SIGTRAMP (get_frame_pc (prev), name))
1384 prev->type = SIGTRAMP_FRAME;
1385 /* FIXME: cagney/2002-11-11: Leave prev->type alone. Some
1386 architectures are forcing the frame's type in INIT so we
1387 don't want to override it here. Remember, NORMAL_FRAME == 0,
1388 so it all works (just :-/). Once this initialization is
1389 moved to the start of this function, all this nastness will
1390 go away. */
1391 }
1392
1393 return prev;
1394 }
1395
1396 /* Return a structure containing various interesting information
1397 about the frame that called THIS_FRAME. Returns NULL
1398 if there is no such frame. */
1399
1400 struct frame_info *
1401 get_prev_frame (struct frame_info *this_frame)
1402 {
1403 struct frame_info *prev_frame;
1404
1405 /* Return the inner-most frame, when the caller passes in NULL. */
1406 /* NOTE: cagney/2002-11-09: Not sure how this would happen. The
1407 caller should have previously obtained a valid frame using
1408 get_selected_frame() and then called this code - only possibility
1409 I can think of is code behaving badly.
1410
1411 NOTE: cagney/2003-01-10: Talk about code behaving badly. Check
1412 block_innermost_frame(). It does the sequence: frame = NULL;
1413 while (1) { frame = get_prev_frame (frame); .... }. Ulgh! Why
1414 it couldn't be written better, I don't know.
1415
1416 NOTE: cagney/2003-01-11: I suspect what is happening is
1417 block_innermost_frame() is, when the target has no state
1418 (registers, memory, ...), still calling this function. The
1419 assumption being that this function will return NULL indicating
1420 that a frame isn't possible, rather than checking that the target
1421 has state and then calling get_current_frame() and
1422 get_prev_frame(). This is a guess mind. */
1423 if (this_frame == NULL)
1424 {
1425 /* NOTE: cagney/2002-11-09: There was a code segment here that
1426 would error out when CURRENT_FRAME was NULL. The comment
1427 that went with it made the claim ...
1428
1429 ``This screws value_of_variable, which just wants a nice
1430 clean NULL return from block_innermost_frame if there are no
1431 frames. I don't think I've ever seen this message happen
1432 otherwise. And returning NULL here is a perfectly legitimate
1433 thing to do.''
1434
1435 Per the above, this code shouldn't even be called with a NULL
1436 THIS_FRAME. */
1437 return current_frame;
1438 }
1439
1440 /* There is always a frame. If this assertion fails, suspect that
1441 something should be calling get_selected_frame() or
1442 get_current_frame(). */
1443 gdb_assert (this_frame != NULL);
1444
1445 if (this_frame->level >= 0
1446 && !backtrace_below_main
1447 && inside_main_func (get_frame_pc (this_frame)))
1448 /* Don't unwind past main(), bug always unwind the sentinel frame.
1449 Note, this is done _before_ the frame has been marked as
1450 previously unwound. That way if the user later decides to
1451 allow unwinds past main(), that just happens. */
1452 {
1453 if (frame_debug)
1454 fprintf_unfiltered (gdb_stdlog,
1455 "Outermost frame - inside main func.\n");
1456 return NULL;
1457 }
1458
1459 /* Only try to do the unwind once. */
1460 if (this_frame->prev_p)
1461 return this_frame->prev;
1462 this_frame->prev_p = 1;
1463
1464 #if 0
1465 /* If we're inside the entry file, it isn't valid. Don't apply this
1466 test to a dummy frame - dummy frame PC's typically land in the
1467 entry file. Don't apply this test to the sentinel frame.
1468 Sentinel frames should always be allowed to unwind. */
1469 /* NOTE: drow/2002-12-25: should there be a way to disable this
1470 check? It assumes a single small entry file, and the way some
1471 debug readers (e.g. dbxread) figure out which object is the
1472 entry file is somewhat hokey. */
1473 /* NOTE: cagney/2003-01-10: If there is a way of disabling this test
1474 then it should probably be moved to before the ->prev_p test,
1475 above. */
1476 /* NOTE: vinschen/2003-04-01: Disabled. It turns out that the call to
1477 inside_entry_file destroys a meaningful backtrace under some
1478 conditions. E. g. the backtrace tests in the asm-source testcase
1479 are broken for some targets. In this test the functions are all
1480 implemented as part of one file and the testcase is not necessarily
1481 linked with a start file (depending on the target). What happens is,
1482 that the first frame is printed normaly and following frames are
1483 treated as being inside the enttry file then. This way, only the
1484 #0 frame is printed in the backtrace output. */
1485 if (this_frame->type != DUMMY_FRAME && this_frame->level >= 0
1486 && inside_entry_file (get_frame_pc (this_frame)))
1487 {
1488 if (frame_debug)
1489 fprintf_unfiltered (gdb_stdlog,
1490 "Outermost frame - inside entry file\n");
1491 return NULL;
1492 }
1493 #endif
1494
1495 /* If we're already inside the entry function for the main objfile,
1496 then it isn't valid. Don't apply this test to a dummy frame -
1497 dummy frame PC's typically land in the entry func. Don't apply
1498 this test to the sentinel frame. Sentinel frames should always
1499 be allowed to unwind. */
1500 /* NOTE: cagney/2003-02-25: Don't enable until someone has found
1501 hard evidence that this is needed. */
1502 if (0
1503 && this_frame->type != DUMMY_FRAME && this_frame->level >= 0
1504 && inside_entry_func (get_frame_pc (this_frame)))
1505 {
1506 if (frame_debug)
1507 fprintf_unfiltered (gdb_stdlog,
1508 "Outermost frame - inside entry func\n");
1509 return NULL;
1510 }
1511
1512 /* If any of the old frame initialization methods are around, use
1513 the legacy get_prev_frame method. */
1514 if (legacy_frame_p (current_gdbarch))
1515 {
1516 prev_frame = legacy_get_prev_frame (this_frame);
1517 if (frame_debug && prev_frame == NULL)
1518 fprintf_unfiltered (gdb_stdlog,
1519 "Outermost frame - legacy_get_prev_frame NULL.\n");
1520 return prev_frame;
1521 }
1522
1523 /* Check that this frame's ID was valid. If it wasn't, don't try to
1524 unwind to the prev frame. Be careful to not apply this test to
1525 the sentinel frame. */
1526 if (this_frame->level >= 0 && !frame_id_p (get_frame_id (this_frame)))
1527 {
1528 if (frame_debug)
1529 fprintf_filtered (gdb_stdlog,
1530 "Outermost frame - this ID is NULL\n");
1531 return NULL;
1532 }
1533
1534 /* Check that this frame's ID isn't inner to (younger, below, next)
1535 the next frame. This happens when frame unwind goes backwards.
1536 Since the sentinel frame isn't valid, don't apply this if this
1537 frame is entier the inner-most or sentinel frame. */
1538 if (this_frame->level > 0
1539 && frame_id_inner (get_frame_id (this_frame),
1540 get_frame_id (this_frame->next)))
1541 error ("This frame inner-to next frame (corrupt stack?)");
1542
1543 /* Check that this and the next frame are different. If they are
1544 not, there is most likely a stack cycle. As with the inner-than
1545 test, avoid the inner-most and sentinel frames. */
1546 /* FIXME: cagney/2003-03-17: Can't yet enable this this check. The
1547 frame_id_eq() method doesn't yet use function addresses when
1548 comparing frame IDs. */
1549 if (0
1550 && this_frame->level > 0
1551 && frame_id_eq (get_frame_id (this_frame),
1552 get_frame_id (this_frame->next)))
1553 error ("This frame identical to next frame (corrupt stack?)");
1554
1555 /* Allocate the new frame but do not wire it in to the frame chain.
1556 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1557 frame->next to pull some fancy tricks (of course such code is, by
1558 definition, recursive). Try to prevent it.
1559
1560 There is no reason to worry about memory leaks, should the
1561 remainder of the function fail. The allocated memory will be
1562 quickly reclaimed when the frame cache is flushed, and the `we've
1563 been here before' check above will stop repeated memory
1564 allocation calls. */
1565 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1566 prev_frame->level = this_frame->level + 1;
1567
1568 /* Try to unwind the PC. If that doesn't work, assume we've reached
1569 the oldest frame and simply return. Is there a better sentinal
1570 value? The unwound PC value is then used to initialize the new
1571 previous frame's type.
1572
1573 Note that the pc-unwind is intentionally performed before the
1574 frame chain. This is ok since, for old targets, both
1575 frame_pc_unwind (nee, FRAME_SAVED_PC) and
1576 DEPRECATED_FRAME_CHAIN()) assume THIS_FRAME's data structures
1577 have already been initialized (using
1578 DEPRECATED_INIT_EXTRA_FRAME_INFO) and hence the call order
1579 doesn't matter.
1580
1581 By unwinding the PC first, it becomes possible to, in the case of
1582 a dummy frame, avoid also unwinding the frame ID. This is
1583 because (well ignoring the PPC) a dummy frame can be located
1584 using THIS_FRAME's frame ID. */
1585
1586 if (frame_pc_unwind (this_frame) == 0)
1587 {
1588 /* The allocated PREV_FRAME will be reclaimed when the frame
1589 obstack is next purged. */
1590 if (frame_debug)
1591 fprintf_unfiltered (gdb_stdlog,
1592 "Outermost frame - unwound PC zero\n");
1593 return NULL;
1594 }
1595
1596 /* Set the unwind functions based on that identified PC. */
1597 prev_frame->unwind = frame_unwind_find_by_pc (current_gdbarch,
1598 frame_pc_unwind (this_frame));
1599
1600 /* FIXME: cagney/2003-04-02: Rather than storing the frame's type in
1601 the frame, the unwinder's type should be returned directly.
1602 Unfortunatly, legacy code, called by legacy_get_prev_frame,
1603 explicitly set the frames type using the method
1604 deprecated_set_frame_type(). */
1605 gdb_assert (prev_frame->unwind->type != UNKNOWN_FRAME);
1606 prev_frame->type = prev_frame->unwind->type;
1607
1608 /* Can the frame's type and unwinder be computed on demand? That
1609 would make a frame's creation really really lite! */
1610
1611 /* The prev's frame's ID is computed by demand in get_frame_id(). */
1612
1613 /* The unwound frame ID is validate at the start of this function,
1614 as part of the logic to decide if that frame should be further
1615 unwound, and not here while the prev frame is being created.
1616 Doing this makes it possible for the user to examine a frame that
1617 has an invalid frame ID.
1618
1619 The very old VAX frame_args_address_correct() method noted: [...]
1620 For the sake of argument, suppose that the stack is somewhat
1621 trashed (which is one reason that "info frame" exists). So,
1622 return 0 (indicating we don't know the address of the arglist) if
1623 we don't know what frame this frame calls. */
1624
1625 /* Link it in. */
1626 this_frame->prev = prev_frame;
1627 prev_frame->next = this_frame;
1628
1629 return prev_frame;
1630 }
1631
1632 CORE_ADDR
1633 get_frame_pc (struct frame_info *frame)
1634 {
1635 gdb_assert (frame->next != NULL);
1636 return frame_pc_unwind (frame->next);
1637 }
1638
1639 static int
1640 pc_notcurrent (struct frame_info *frame)
1641 {
1642 /* If FRAME is not the innermost frame, that normally means that
1643 FRAME->pc points at the return instruction (which is *after* the
1644 call instruction), and we want to get the line containing the
1645 call (because the call is where the user thinks the program is).
1646 However, if the next frame is either a SIGTRAMP_FRAME or a
1647 DUMMY_FRAME, then the next frame will contain a saved interrupt
1648 PC and such a PC indicates the current (rather than next)
1649 instruction/line, consequently, for such cases, want to get the
1650 line containing fi->pc. */
1651 struct frame_info *next = get_next_frame (frame);
1652 int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME);
1653 return notcurrent;
1654 }
1655
1656 void
1657 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1658 {
1659 (*sal) = find_pc_line (get_frame_pc (frame), pc_notcurrent (frame));
1660 }
1661
1662 /* Per "frame.h", return the ``address'' of the frame. Code should
1663 really be using get_frame_id(). */
1664 CORE_ADDR
1665 get_frame_base (struct frame_info *fi)
1666 {
1667 return get_frame_id (fi).base;
1668 }
1669
1670 /* High-level offsets into the frame. Used by the debug info. */
1671
1672 CORE_ADDR
1673 get_frame_base_address (struct frame_info *fi)
1674 {
1675 if (get_frame_type (fi) != NORMAL_FRAME)
1676 return 0;
1677 if (fi->base == NULL)
1678 fi->base = frame_base_find_by_pc (current_gdbarch, get_frame_pc (fi));
1679 /* Sneaky: If the low-level unwind and high-level base code share a
1680 common unwinder, let them share the prologue cache. */
1681 if (fi->base->unwind == fi->unwind)
1682 return fi->base->this_base (fi->next, &fi->prologue_cache);
1683 return fi->base->this_base (fi->next, &fi->base_cache);
1684 }
1685
1686 CORE_ADDR
1687 get_frame_locals_address (struct frame_info *fi)
1688 {
1689 void **cache;
1690 if (get_frame_type (fi) != NORMAL_FRAME)
1691 return 0;
1692 /* If there isn't a frame address method, find it. */
1693 if (fi->base == NULL)
1694 fi->base = frame_base_find_by_pc (current_gdbarch, get_frame_pc (fi));
1695 /* Sneaky: If the low-level unwind and high-level base code share a
1696 common unwinder, let them share the prologue cache. */
1697 if (fi->base->unwind == fi->unwind)
1698 cache = &fi->prologue_cache;
1699 else
1700 cache = &fi->base_cache;
1701 return fi->base->this_locals (fi->next, cache);
1702 }
1703
1704 CORE_ADDR
1705 get_frame_args_address (struct frame_info *fi)
1706 {
1707 void **cache;
1708 if (get_frame_type (fi) != NORMAL_FRAME)
1709 return 0;
1710 /* If there isn't a frame address method, find it. */
1711 if (fi->base == NULL)
1712 fi->base = frame_base_find_by_pc (current_gdbarch, get_frame_pc (fi));
1713 /* Sneaky: If the low-level unwind and high-level base code share a
1714 common unwinder, let them share the prologue cache. */
1715 if (fi->base->unwind == fi->unwind)
1716 cache = &fi->prologue_cache;
1717 else
1718 cache = &fi->base_cache;
1719 return fi->base->this_args (fi->next, cache);
1720 }
1721
1722 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1723 or -1 for a NULL frame. */
1724
1725 int
1726 frame_relative_level (struct frame_info *fi)
1727 {
1728 if (fi == NULL)
1729 return -1;
1730 else
1731 return fi->level;
1732 }
1733
1734 enum frame_type
1735 get_frame_type (struct frame_info *frame)
1736 {
1737 /* Some targets still don't use [generic] dummy frames. Catch them
1738 here. */
1739 if (!DEPRECATED_USE_GENERIC_DUMMY_FRAMES
1740 && deprecated_frame_in_dummy (frame))
1741 return DUMMY_FRAME;
1742 if (frame->type == UNKNOWN_FRAME)
1743 return NORMAL_FRAME;
1744 else
1745 return frame->type;
1746 }
1747
1748 void
1749 deprecated_set_frame_type (struct frame_info *frame, enum frame_type type)
1750 {
1751 /* Arrrg! See comment in "frame.h". */
1752 frame->type = type;
1753 }
1754
1755 struct frame_extra_info *
1756 get_frame_extra_info (struct frame_info *fi)
1757 {
1758 return fi->extra_info;
1759 }
1760
1761 struct frame_extra_info *
1762 frame_extra_info_zalloc (struct frame_info *fi, long size)
1763 {
1764 fi->extra_info = frame_obstack_zalloc (size);
1765 return fi->extra_info;
1766 }
1767
1768 void
1769 deprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc)
1770 {
1771 /* NOTE: cagney/2003-03-11: Some architectures (e.g., Arm) are
1772 maintaining a locally allocated frame object. Since such frame's
1773 are not in the frame chain, it isn't possible to assume that the
1774 frame has a next. Sigh. */
1775 if (frame->next != NULL)
1776 {
1777 /* While we're at it, update this frame's cached PC value, found
1778 in the next frame. Oh for the day when "struct frame_info"
1779 is opaque and this hack on hack can just go away. */
1780 frame->next->prev_pc.value = pc;
1781 frame->next->prev_pc.p = 1;
1782 }
1783 }
1784
1785 void
1786 deprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base)
1787 {
1788 /* See comment in "frame.h". */
1789 frame->id.base = base;
1790 }
1791
1792 void
1793 deprecated_set_frame_saved_regs_hack (struct frame_info *frame,
1794 CORE_ADDR *saved_regs)
1795 {
1796 frame->saved_regs = saved_regs;
1797 }
1798
1799 void
1800 deprecated_set_frame_extra_info_hack (struct frame_info *frame,
1801 struct frame_extra_info *extra_info)
1802 {
1803 frame->extra_info = extra_info;
1804 }
1805
1806 void
1807 deprecated_set_frame_next_hack (struct frame_info *fi,
1808 struct frame_info *next)
1809 {
1810 fi->next = next;
1811 }
1812
1813 void
1814 deprecated_set_frame_prev_hack (struct frame_info *fi,
1815 struct frame_info *prev)
1816 {
1817 fi->prev = prev;
1818 }
1819
1820 struct context *
1821 deprecated_get_frame_context (struct frame_info *fi)
1822 {
1823 return fi->context;
1824 }
1825
1826 void
1827 deprecated_set_frame_context (struct frame_info *fi,
1828 struct context *context)
1829 {
1830 fi->context = context;
1831 }
1832
1833 struct frame_info *
1834 deprecated_frame_xmalloc (void)
1835 {
1836 struct frame_info *frame = XMALLOC (struct frame_info);
1837 memset (frame, 0, sizeof (struct frame_info));
1838 return frame;
1839 }
1840
1841 struct frame_info *
1842 deprecated_frame_xmalloc_with_cleanup (long sizeof_saved_regs,
1843 long sizeof_extra_info)
1844 {
1845 struct frame_info *frame = deprecated_frame_xmalloc ();
1846 make_cleanup (xfree, frame);
1847 if (sizeof_saved_regs > 0)
1848 {
1849 frame->saved_regs = xcalloc (1, sizeof_saved_regs);
1850 make_cleanup (xfree, frame->saved_regs);
1851 }
1852 if (sizeof_extra_info > 0)
1853 {
1854 frame->extra_info = xcalloc (1, sizeof_extra_info);
1855 make_cleanup (xfree, frame->extra_info);
1856 }
1857 return frame;
1858 }
1859
1860 int
1861 legacy_frame_p (struct gdbarch *current_gdbarch)
1862 {
1863 return (DEPRECATED_INIT_FRAME_PC_P ()
1864 || DEPRECATED_INIT_FRAME_PC_FIRST_P ()
1865 || DEPRECATED_INIT_EXTRA_FRAME_INFO_P ()
1866 || DEPRECATED_FRAME_CHAIN_P ()
1867 || !gdbarch_unwind_dummy_id_p (current_gdbarch)
1868 || !SAVE_DUMMY_FRAME_TOS_P ());
1869 }
1870
1871 void
1872 _initialize_frame (void)
1873 {
1874 obstack_init (&frame_cache_obstack);
1875
1876 /* FIXME: cagney/2003-01-19: This command needs a rename. Suggest
1877 `set backtrace {past,beyond,...}-main'. Also suggest adding `set
1878 backtrace ...-start' to control backtraces past start. The
1879 problem with `below' is that it stops the `up' command. */
1880
1881 add_setshow_boolean_cmd ("backtrace-below-main", class_obscure,
1882 &backtrace_below_main, "\
1883 Set whether backtraces should continue past \"main\".\n\
1884 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
1885 the backtrace at \"main\". Set this variable if you need to see the rest\n\
1886 of the stack trace.", "\
1887 Show whether backtraces should continue past \"main\".\n\
1888 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
1889 the backtrace at \"main\". Set this variable if you need to see the rest\n\
1890 of the stack trace.",
1891 NULL, NULL, &setlist, &showlist);
1892
1893
1894 /* Debug this files internals. */
1895 add_show_from_set (add_set_cmd ("frame", class_maintenance, var_zinteger,
1896 &frame_debug, "Set frame debugging.\n\
1897 When non-zero, frame specific internal debugging is enabled.", &setdebuglist),
1898 &showdebuglist);
1899 }
This page took 0.070974 seconds and 4 git commands to generate.