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