*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / frame.c
CommitLineData
4f460812 1/* Cache and manage frames for GDB, the GNU debugger.
96cb11df
AC
2
3 Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
51603483 4 2001, 2002, 2003 Free Software Foundation, Inc.
d65fe839
AC
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"
39f77062 27#include "inferior.h" /* for inferior_ptid */
4e052eda 28#include "regcache.h"
4f460812 29#include "gdb_assert.h"
e36180d7
AC
30#include "gdb_string.h"
31#include "builtin-regs.h"
4c1e7e9d
AC
32#include "gdb_obstack.h"
33#include "dummy-frame.h"
a94dd1fd 34#include "sentinel-frame.h"
4c1e7e9d
AC
35#include "gdbcore.h"
36#include "annotate.h"
6e7f8b9c 37#include "language.h"
494cca16 38#include "frame-unwind.h"
eb4f72c5
AC
39#include "command.h"
40#include "gdbcmd.h"
41
ac2bd0a9
AC
42/* Flag to control debugging. */
43
44static int frame_debug;
45
eb4f72c5
AC
46/* Flag to indicate whether backtraces should stop at main. */
47
48static int backtrace_below_main;
d65fe839 49
7a424e99 50/* Return a frame uniq ID that can be used to, later, re-find the
101dcfbe
AC
51 frame. */
52
7a424e99
AC
53struct frame_id
54get_frame_id (struct frame_info *fi)
101dcfbe
AC
55{
56 if (fi == NULL)
57 {
7a424e99 58 return null_frame_id;
101dcfbe
AC
59 }
60 else
61 {
7a424e99
AC
62 struct frame_id id;
63 id.base = fi->frame;
64 id.pc = fi->pc;
65 return id;
101dcfbe
AC
66 }
67}
68
7a424e99
AC
69const struct frame_id null_frame_id; /* All zeros. */
70
71struct frame_id
72frame_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
80int
81frame_id_p (struct frame_id l)
82{
83 /* The .func can be NULL but the .base cannot. */
84 return (l.base != 0);
85}
86
87int
88frame_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
98int
99frame_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
101dcfbe
AC
108struct frame_info *
109frame_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()? */
7a424e99 115 if (!frame_id_p (id))
101dcfbe
AC
116 return NULL;
117
118 for (frame = get_current_frame ();
119 frame != NULL;
120 frame = get_prev_frame (frame))
121 {
7a424e99
AC
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. */
101dcfbe 128 return NULL;
7a424e99
AC
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. */
101dcfbe
AC
133 }
134 return NULL;
135}
136
f18c5a73
AC
137CORE_ADDR
138frame_pc_unwind (struct frame_info *frame)
139{
140 if (!frame->pc_unwind_cache_p)
141 {
494cca16 142 frame->pc_unwind_cache = frame->unwind->pc (frame, &frame->unwind_cache);
f18c5a73
AC
143 frame->pc_unwind_cache_p = 1;
144 }
145 return frame->pc_unwind_cache;
146}
147
c689142b
AC
148struct frame_id
149frame_id_unwind (struct frame_info *frame)
150{
151 if (!frame->id_unwind_cache_p)
152 {
494cca16 153 frame->unwind->id (frame, &frame->unwind_cache, &frame->id_unwind_cache);
c689142b
AC
154 frame->id_unwind_cache_p = 1;
155 }
156 return frame->id_unwind_cache;
157}
158
dbe9fe58
AC
159void
160frame_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}
c689142b 171
4f460812
AC
172void
173frame_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
a94dd1fd
AC
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);
4f460812
AC
192
193 /* Ask this frame to unwind its register. */
494cca16
AC
194 frame->unwind->reg (frame, &frame->unwind_cache, regnum,
195 optimizedp, lvalp, addrp, realnump, bufferp);
4f460812
AC
196}
197
a216a322
AC
198void
199frame_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
a94dd1fd
AC
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);
a216a322
AC
242}
243
135c175f 244void
5b181d62 245frame_unwind_register (struct frame_info *frame, int regnum, void *buf)
135c175f
AC
246{
247 int optimized;
248 CORE_ADDR addr;
249 int realnum;
250 enum lval_type lval;
135c175f
AC
251 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
252 &realnum, buf);
5b181d62
AC
253}
254
255void
256frame_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);
135c175f
AC
261 (*val) = extract_signed_integer (buf, REGISTER_VIRTUAL_SIZE (regnum));
262}
263
264void
265frame_unwind_unsigned_register (struct frame_info *frame, int regnum,
266 ULONGEST *val)
267{
135c175f 268 void *buf = alloca (MAX_REGISTER_RAW_SIZE);
5b181d62 269 frame_unwind_register (frame, regnum, buf);
135c175f
AC
270 (*val) = extract_unsigned_integer (buf, REGISTER_VIRTUAL_SIZE (regnum));
271}
4f460812 272
5b181d62
AC
273void
274frame_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
f908a0eb
AC
280void
281frame_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. */
a94dd1fd
AC
301 gdb_assert (frame != NULL && frame->next != NULL);
302 frame_unwind_unsigned_register (frame->next, regnum, val);
f908a0eb
AC
303}
304
305void
306frame_read_signed_register (struct frame_info *frame, int regnum,
307 LONGEST *val)
308{
a94dd1fd
AC
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);
f908a0eb
AC
312}
313
f796e4be 314void
4f460812
AC
315generic_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
a94dd1fd
AC
339 gdb_assert (frame != NULL && frame->next != NULL);
340 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
341 &realnumx, raw_buffer);
4f460812
AC
342}
343
d65fe839
AC
344void
345get_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{
a216a322
AC
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);
d65fe839
AC
359}
360
cda5a58a 361/* frame_register_read ()
d65fe839 362
cda5a58a 363 Find and return the value of REGNUM for the specified stack frame.
d65fe839
AC
364 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
365
cda5a58a 366 Returns 0 if the register value could not be found. */
d65fe839 367
cda5a58a
AC
368int
369frame_register_read (struct frame_info *frame, int regnum, void *myaddr)
d65fe839 370{
a216a322
AC
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);
d65fe839 376
c97dcfc7
AC
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
d65fe839 384 if (register_cached (regnum) < 0)
cda5a58a 385 return 0; /* register value not available */
d65fe839 386
a216a322 387 return !optimized;
d65fe839 388}
e36180d7
AC
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
395int
396frame_map_name_to_regnum (const char *name, int len)
397{
398 int i;
399
5f601589
AC
400 if (len < 0)
401 len = strlen (name);
402
e36180d7
AC
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
424const char *
425frame_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}
4c1e7e9d 433
a94dd1fd
AC
434/* Create a sentinel frame. */
435
436struct frame_info *
437create_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
4c1e7e9d
AC
460/* Info about the innermost stack frame (contents of FP register) */
461
462static 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
468static struct obstack frame_cache_obstack;
469
470void *
479ab5a0 471frame_obstack_zalloc (unsigned long size)
4c1e7e9d 472{
479ab5a0
AC
473 void *data = obstack_alloc (&frame_cache_obstack, size);
474 memset (data, 0, size);
475 return data;
4c1e7e9d
AC
476}
477
6baff1d2 478CORE_ADDR *
4c1e7e9d
AC
479frame_saved_regs_zalloc (struct frame_info *fi)
480{
481 fi->saved_regs = (CORE_ADDR *)
479ab5a0 482 frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS);
6baff1d2 483 return fi->saved_regs;
4c1e7e9d
AC
484}
485
6baff1d2
AC
486CORE_ADDR *
487get_frame_saved_regs (struct frame_info *fi)
488{
489 return fi->saved_regs;
490}
4c1e7e9d 491
a94dd1fd
AC
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
497static int
498unwind_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}
4c1e7e9d
AC
508
509struct frame_info *
510get_current_frame (void)
511{
a94dd1fd
AC
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.");
4c1e7e9d
AC
518 if (current_frame == NULL)
519 {
a94dd1fd
AC
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 }
4c1e7e9d
AC
529 }
530 return current_frame;
531}
532
6e7f8b9c
AC
533/* The "selected" stack frame is used by default for local and arg
534 access. May be zero, for no selected frame. */
535
536struct 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
542struct frame_info *
543get_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
557void
558select_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
4c1e7e9d
AC
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
596static void
597frame_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.) */
07555a72 608 gdb_assert (!(DEPRECATED_USE_GENERIC_DUMMY_FRAMES
5e0f933e 609 && (get_frame_type (frame) == DUMMY_FRAME)));
4c1e7e9d 610
8f871025
AC
611 /* Only (older) architectures that implement the
612 FRAME_INIT_SAVED_REGS method should be using this function. */
613 gdb_assert (FRAME_INIT_SAVED_REGS_P ());
614
4c1e7e9d 615 /* Load the saved_regs register cache. */
a94dd1fd 616 if (get_frame_saved_regs (frame) == NULL)
4c1e7e9d
AC
617 FRAME_INIT_SAVED_REGS (frame);
618
a94dd1fd
AC
619 if (get_frame_saved_regs (frame) != NULL
620 && get_frame_saved_regs (frame)[regnum] != 0)
4c1e7e9d
AC
621 {
622 if (regnum == SP_REGNUM)
623 {
624 /* SP register treated specially. */
625 *optimizedp = 0;
626 *lvalp = not_lval;
627 *addrp = 0;
628 *realnump = -1;
629 if (bufferp != NULL)
630 store_address (bufferp, REGISTER_RAW_SIZE (regnum),
a94dd1fd 631 get_frame_saved_regs (frame)[regnum]);
4c1e7e9d
AC
632 }
633 else
634 {
635 /* Any other register is saved in memory, fetch it but cache
636 a local copy of its value. */
637 *optimizedp = 0;
638 *lvalp = lval_memory;
a94dd1fd 639 *addrp = get_frame_saved_regs (frame)[regnum];
4c1e7e9d
AC
640 *realnump = -1;
641 if (bufferp != NULL)
642 {
643#if 1
644 /* Save each register value, as it is read in, in a
645 frame based cache. */
646 void **regs = (*cache);
647 if (regs == NULL)
648 {
649 int sizeof_cache = ((NUM_REGS + NUM_PSEUDO_REGS)
650 * sizeof (void *));
479ab5a0 651 regs = frame_obstack_zalloc (sizeof_cache);
4c1e7e9d
AC
652 (*cache) = regs;
653 }
654 if (regs[regnum] == NULL)
655 {
656 regs[regnum]
479ab5a0 657 = frame_obstack_zalloc (REGISTER_RAW_SIZE (regnum));
a94dd1fd 658 read_memory (get_frame_saved_regs (frame)[regnum], regs[regnum],
4c1e7e9d
AC
659 REGISTER_RAW_SIZE (regnum));
660 }
661 memcpy (bufferp, regs[regnum], REGISTER_RAW_SIZE (regnum));
662#else
663 /* Read the value in from memory. */
a94dd1fd 664 read_memory (get_frame_saved_regs (frame)[regnum], bufferp,
4c1e7e9d
AC
665 REGISTER_RAW_SIZE (regnum));
666#endif
667 }
668 }
669 return;
670 }
671
672 /* No luck, assume this and the next frame have the same register
a94dd1fd
AC
673 value. Pass the request down the frame chain to the next frame.
674 Hopefully that will find the register's location, either in a
675 register or in memory. */
676 frame_register (frame, regnum, optimizedp, lvalp, addrp, realnump,
677 bufferp);
4c1e7e9d
AC
678}
679
f18c5a73
AC
680static CORE_ADDR
681frame_saved_regs_pc_unwind (struct frame_info *frame, void **cache)
682{
d62d1979 683 gdb_assert (FRAME_SAVED_PC_P ());
f18c5a73
AC
684 return FRAME_SAVED_PC (frame);
685}
686
c170fb60
AC
687static void
688frame_saved_regs_id_unwind (struct frame_info *next_frame, void **cache,
689 struct frame_id *id)
c689142b
AC
690{
691 int fromleaf;
c170fb60
AC
692 CORE_ADDR base;
693 CORE_ADDR pc;
694
695 /* Start out by assuming it's NULL. */
696 (*id) = null_frame_id;
c689142b 697
a94dd1fd 698 if (frame_relative_level (next_frame) <= 0)
c689142b
AC
699 /* FIXME: 2002-11-09: Frameless functions can occure anywhere in
700 the frame chain, not just the inner most frame! The generic,
701 per-architecture, frame code should handle this and the below
702 should simply be removed. */
703 fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame);
704 else
705 fromleaf = 0;
706
707 if (fromleaf)
708 /* A frameless inner-most frame. The `FP' (which isn't an
709 architecture frame-pointer register!) of the caller is the same
710 as the callee. */
711 /* FIXME: 2002-11-09: There isn't any reason to special case this
712 edge condition. Instead the per-architecture code should hande
713 it locally. */
c170fb60 714 base = get_frame_base (next_frame);
c689142b
AC
715 else
716 {
717 /* Two macros defined in tm.h specify the machine-dependent
718 actions to be performed here.
719
720 First, get the frame's chain-pointer.
721
722 If that is zero, the frame is the outermost frame or a leaf
723 called by the outermost frame. This means that if start
724 calls main without a frame, we'll return 0 (which is fine
725 anyway).
726
727 Nope; there's a problem. This also returns when the current
728 routine is a leaf of main. This is unacceptable. We move
729 this to after the ffi test; I'd rather have backtraces from
730 start go curfluy than have an abort called from main not show
731 main. */
d62d1979 732 gdb_assert (FRAME_CHAIN_P ());
c170fb60 733 base = FRAME_CHAIN (next_frame);
c689142b 734
c170fb60
AC
735 if (!frame_chain_valid (base, next_frame))
736 return;
c689142b 737 }
c170fb60
AC
738 if (base == 0)
739 return;
c689142b
AC
740
741 /* FIXME: cagney/2002-06-08: This should probably return the frame's
742 function and not the PC (a.k.a. resume address). */
c170fb60
AC
743 pc = frame_pc_unwind (next_frame);
744 id->pc = pc;
745 id->base = base;
c689142b
AC
746}
747
dbe9fe58
AC
748static void
749frame_saved_regs_pop (struct frame_info *fi, void **cache,
750 struct regcache *regcache)
751{
dedc2a2b 752 gdb_assert (POP_FRAME_P ());
dbe9fe58
AC
753 POP_FRAME;
754}
755
494cca16 756const struct frame_unwind trad_frame_unwinder = {
dbe9fe58 757 frame_saved_regs_pop,
494cca16
AC
758 frame_saved_regs_pc_unwind,
759 frame_saved_regs_id_unwind,
760 frame_saved_regs_register_unwind
761};
762const struct frame_unwind *trad_frame_unwind = &trad_frame_unwinder;
763
764
4c1e7e9d
AC
765/* Function: get_saved_register
766 Find register number REGNUM relative to FRAME and put its (raw,
767 target format) contents in *RAW_BUFFER.
768
769 Set *OPTIMIZED if the variable was optimized out (and thus can't be
770 fetched). Note that this is never set to anything other than zero
771 in this implementation.
772
773 Set *LVAL to lval_memory, lval_register, or not_lval, depending on
774 whether the value was fetched from memory, from a register, or in a
775 strange and non-modifiable way (e.g. a frame pointer which was
776 calculated rather than fetched). We will use not_lval for values
777 fetched from generic dummy frames.
778
779 Set *ADDRP to the address, either in memory or as a REGISTER_BYTE
780 offset into the registers array. If the value is stored in a dummy
781 frame, set *ADDRP to zero.
782
783 To use this implementation, define a function called
784 "get_saved_register" in your target code, which simply passes all
785 of its arguments to this function.
786
787 The argument RAW_BUFFER must point to aligned memory. */
788
789void
790deprecated_generic_get_saved_register (char *raw_buffer, int *optimized,
791 CORE_ADDR *addrp,
792 struct frame_info *frame, int regnum,
793 enum lval_type *lval)
794{
795 if (!target_has_registers)
796 error ("No registers.");
797
8f871025
AC
798 gdb_assert (FRAME_INIT_SAVED_REGS_P ());
799
4c1e7e9d
AC
800 /* Normal systems don't optimize out things with register numbers. */
801 if (optimized != NULL)
802 *optimized = 0;
803
804 if (addrp) /* default assumption: not found in memory */
805 *addrp = 0;
806
807 /* Note: since the current frame's registers could only have been
808 saved by frames INTERIOR TO the current frame, we skip examining
809 the current frame itself: otherwise, we would be getting the
810 previous frame's registers which were saved by the current frame. */
811
a94dd1fd 812 if (frame != NULL)
4c1e7e9d 813 {
a94dd1fd
AC
814 for (frame = get_next_frame (frame);
815 frame_relative_level (frame) >= 0;
816 frame = get_next_frame (frame))
4c1e7e9d 817 {
a94dd1fd 818 if (get_frame_type (frame) == DUMMY_FRAME)
4c1e7e9d 819 {
a94dd1fd
AC
820 if (lval) /* found it in a CALL_DUMMY frame */
821 *lval = not_lval;
822 if (raw_buffer)
823 /* FIXME: cagney/2002-06-26: This should be via the
824 gdbarch_register_read() method so that it, on the
825 fly, constructs either a raw or pseudo register
826 from the raw register cache. */
827 regcache_raw_read
828 (generic_find_dummy_frame (get_frame_pc (frame),
829 get_frame_base (frame)),
830 regnum, raw_buffer);
831 return;
4c1e7e9d 832 }
a94dd1fd
AC
833
834 FRAME_INIT_SAVED_REGS (frame);
835 if (get_frame_saved_regs (frame) != NULL
836 && get_frame_saved_regs (frame)[regnum] != 0)
4c1e7e9d 837 {
a94dd1fd
AC
838 if (lval) /* found it saved on the stack */
839 *lval = lval_memory;
840 if (regnum == SP_REGNUM)
841 {
842 if (raw_buffer) /* SP register treated specially */
843 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
844 get_frame_saved_regs (frame)[regnum]);
845 }
846 else
847 {
848 if (addrp) /* any other register */
849 *addrp = get_frame_saved_regs (frame)[regnum];
850 if (raw_buffer)
851 read_memory (get_frame_saved_regs (frame)[regnum], raw_buffer,
852 REGISTER_RAW_SIZE (regnum));
853 }
854 return;
4c1e7e9d 855 }
4c1e7e9d
AC
856 }
857 }
858
859 /* If we get thru the loop to this point, it means the register was
860 not saved in any frame. Return the actual live-register value. */
861
862 if (lval) /* found it in a live register */
863 *lval = lval_register;
864 if (addrp)
865 *addrp = REGISTER_BYTE (regnum);
866 if (raw_buffer)
867 deprecated_read_register_gen (regnum, raw_buffer);
868}
869
eb4f72c5
AC
870/* Determine the frame's type based on its PC. */
871
872static enum frame_type
873frame_type_from_pc (CORE_ADDR pc)
874{
875 /* FIXME: cagney/2002-11-24: Can't yet directly call
876 pc_in_dummy_frame() as some architectures don't set
877 PC_IN_CALL_DUMMY() to generic_pc_in_call_dummy() (remember the
878 latter is implemented by simply calling pc_in_dummy_frame). */
879 if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
880 && DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0))
881 return DUMMY_FRAME;
882 else
883 {
884 char *name;
885 find_pc_partial_function (pc, &name, NULL, NULL);
886 if (PC_IN_SIGTRAMP (pc, name))
887 return SIGTRAMP_FRAME;
888 else
889 return NORMAL_FRAME;
890 }
891}
892
4c1e7e9d
AC
893/* Create an arbitrary (i.e. address specified by user) or innermost frame.
894 Always returns a non-NULL value. */
895
896struct frame_info *
897create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
898{
899 struct frame_info *fi;
4c1e7e9d 900
479ab5a0 901 fi = frame_obstack_zalloc (sizeof (struct frame_info));
4c1e7e9d
AC
902
903 fi->frame = addr;
904 fi->pc = pc;
a94dd1fd 905 fi->next = create_sentinel_frame (current_regcache);
eb4f72c5 906 fi->type = frame_type_from_pc (pc);
4c1e7e9d
AC
907
908 if (INIT_EXTRA_FRAME_INFO_P ())
909 INIT_EXTRA_FRAME_INFO (0, fi);
910
911 /* Select/initialize an unwind function. */
494cca16 912 fi->unwind = frame_unwind_find_by_pc (current_gdbarch, fi->pc);
4c1e7e9d
AC
913
914 return fi;
915}
916
917/* Return the frame that FRAME calls (NULL if FRAME is the innermost
a94dd1fd
AC
918 frame). Be careful to not fall off the bottom of the frame chain
919 and onto the sentinel frame. */
4c1e7e9d
AC
920
921struct frame_info *
922get_next_frame (struct frame_info *frame)
923{
a94dd1fd
AC
924 if (frame->level > 0)
925 return frame->next;
926 else
927 return NULL;
4c1e7e9d
AC
928}
929
930/* Flush the entire frame cache. */
931
932void
933flush_cached_frames (void)
934{
935 /* Since we can't really be sure what the first object allocated was */
936 obstack_free (&frame_cache_obstack, 0);
937 obstack_init (&frame_cache_obstack);
938
939 current_frame = NULL; /* Invalidate cache */
940 select_frame (NULL);
941 annotate_frames_invalid ();
942}
943
944/* Flush the frame cache, and start a new one if necessary. */
945
946void
947reinit_frame_cache (void)
948{
949 flush_cached_frames ();
950
951 /* FIXME: The inferior_ptid test is wrong if there is a corefile. */
952 if (PIDGET (inferior_ptid) != 0)
953 {
954 select_frame (get_current_frame ());
955 }
956}
957
eb4f72c5
AC
958/* Create the previous frame using the deprecated methods
959 INIT_EXTRA_INFO, INIT_FRAME_PC and INIT_FRAME_PC_FIRST. */
4c1e7e9d 960
eb4f72c5
AC
961static struct frame_info *
962legacy_get_prev_frame (struct frame_info *next_frame)
4c1e7e9d
AC
963{
964 CORE_ADDR address = 0;
965 struct frame_info *prev;
95adb866 966 int fromleaf;
4c1e7e9d 967
eb4f72c5
AC
968 /* This code only works on normal frames. A sentinel frame, where
969 the level is -1, should never reach this code. */
970 gdb_assert (next_frame->level >= 0);
4c1e7e9d
AC
971
972 /* On some machines it is possible to call a function without
973 setting up a stack frame for it. On these machines, we
974 define this macro to take two args; a frameinfo pointer
975 identifying a frame and a variable to set or clear if it is
976 or isn't leafless. */
977
978 /* Still don't want to worry about this except on the innermost
95adb866
AC
979 frame. This macro will set FROMLEAF if NEXT_FRAME is a frameless
980 function invocation. */
eb4f72c5 981 if (next_frame->level == 0)
95adb866
AC
982 /* FIXME: 2002-11-09: Frameless functions can occure anywhere in
983 the frame chain, not just the inner most frame! The generic,
984 per-architecture, frame code should handle this and the below
985 should simply be removed. */
986 fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame);
987 else
988 fromleaf = 0;
989
990 if (fromleaf)
991 /* A frameless inner-most frame. The `FP' (which isn't an
992 architecture frame-pointer register!) of the caller is the same
993 as the callee. */
994 /* FIXME: 2002-11-09: There isn't any reason to special case this
995 edge condition. Instead the per-architecture code should hande
996 it locally. */
c193f6ac 997 address = get_frame_base (next_frame);
95adb866 998 else
4c1e7e9d
AC
999 {
1000 /* Two macros defined in tm.h specify the machine-dependent
1001 actions to be performed here.
95adb866 1002
4c1e7e9d 1003 First, get the frame's chain-pointer.
95adb866 1004
4c1e7e9d
AC
1005 If that is zero, the frame is the outermost frame or a leaf
1006 called by the outermost frame. This means that if start
1007 calls main without a frame, we'll return 0 (which is fine
1008 anyway).
1009
1010 Nope; there's a problem. This also returns when the current
1011 routine is a leaf of main. This is unacceptable. We move
1012 this to after the ffi test; I'd rather have backtraces from
1013 start go curfluy than have an abort called from main not show
1014 main. */
d62d1979 1015 gdb_assert (FRAME_CHAIN_P ());
4c1e7e9d
AC
1016 address = FRAME_CHAIN (next_frame);
1017
51603483 1018 if (!frame_chain_valid (address, next_frame))
4c1e7e9d
AC
1019 return 0;
1020 }
1021 if (address == 0)
1022 return 0;
1023
95adb866 1024 /* Create an initially zero previous frame. */
479ab5a0 1025 prev = frame_obstack_zalloc (sizeof (struct frame_info));
4c1e7e9d 1026
95adb866
AC
1027 /* Link it in. */
1028 next_frame->prev = prev;
4c1e7e9d
AC
1029 prev->next = next_frame;
1030 prev->frame = address;
1031 prev->level = next_frame->level + 1;
5a203e44
AC
1032 /* FIXME: cagney/2002-11-18: Should be setting the frame's type
1033 here, before anything else, and not last. Various INIT functions
1034 are full of work-arounds for the frames type not being set
1035 correctly from the word go. Ulgh! */
1036 prev->type = NORMAL_FRAME;
4c1e7e9d 1037
95adb866 1038 /* This change should not be needed, FIXME! We should determine
a5afb99f
AC
1039 whether any targets *need* DEPRECATED_INIT_FRAME_PC to happen
1040 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
1041 express what goes on here.
95adb866
AC
1042
1043 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
1044 (where the PC is already set up) and here (where it isn't).
a5afb99f 1045 DEPRECATED_INIT_FRAME_PC is only called from here, always after
95adb866
AC
1046 INIT_EXTRA_FRAME_INFO.
1047
1048 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the
1049 PC value (which hasn't been set yet). Some other machines appear
1050 to require INIT_EXTRA_FRAME_INFO before they can do
a5afb99f 1051 DEPRECATED_INIT_FRAME_PC. Phoo.
95adb866 1052
2ca6c561
AC
1053 We shouldn't need DEPRECATED_INIT_FRAME_PC_FIRST to add more
1054 complication to an already overcomplicated part of GDB.
1055 gnu@cygnus.com, 15Sep92.
95adb866 1056
a5afb99f 1057 Assuming that some machines need DEPRECATED_INIT_FRAME_PC after
95adb866
AC
1058 INIT_EXTRA_FRAME_INFO, one possible scheme:
1059
1060 SETUP_INNERMOST_FRAME(): Default version is just create_new_frame
1061 (read_fp ()), read_pc ()). Machines with extra frame info would
1062 do that (or the local equivalent) and then set the extra fields.
1063
1064 SETUP_ARBITRARY_FRAME(argc, argv): Only change here is that
1065 create_new_frame would no longer init extra frame info;
1066 SETUP_ARBITRARY_FRAME would have to do that.
1067
1068 INIT_PREV_FRAME(fromleaf, prev) Replace INIT_EXTRA_FRAME_INFO and
a5afb99f
AC
1069 DEPRECATED_INIT_FRAME_PC. This should also return a flag saying
1070 whether to keep the new frame, or whether to discard it, because
1071 on some machines (e.g. mips) it is really awkward to have
95adb866
AC
1072 FRAME_CHAIN_VALID called *before* INIT_EXTRA_FRAME_INFO (there is
1073 no good way to get information deduced in FRAME_CHAIN_VALID into
1074 the extra fields of the new frame). std_frame_pc(fromleaf, prev)
1075
1076 This is the default setting for INIT_PREV_FRAME. It just does
a5afb99f
AC
1077 what the default DEPRECATED_INIT_FRAME_PC does. Some machines
1078 will call it from INIT_PREV_FRAME (either at the beginning, the
1079 end, or in the middle). Some machines won't use it.
95adb866
AC
1080
1081 kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */
1082
1083 /* NOTE: cagney/2002-11-09: Just ignore the above! There is no
1084 reason for things to be this complicated.
1085
1086 The trick is to assume that there is always a frame. Instead of
1087 special casing the inner-most frame, create fake frame
1088 (containing the hardware registers) that is inner to the
1089 user-visible inner-most frame (...) and then unwind from that.
1090 That way architecture code can use use the standard
1091 frame_XX_unwind() functions and not differentiate between the
1092 inner most and any other case.
1093
1094 Since there is always a frame to unwind from, there is always
1095 somewhere (NEXT_FRAME) to store all the info needed to construct
1096 a new (previous) frame without having to first create it. This
1097 means that the convolution below - needing to carefully order a
1098 frame's initialization - isn't needed.
1099
1100 The irony here though, is that FRAME_CHAIN(), at least for a more
1101 up-to-date architecture, always calls FRAME_SAVED_PC(), and
1102 FRAME_SAVED_PC() computes the PC but without first needing the
1103 frame! Instead of the convolution below, we could have simply
1104 called FRAME_SAVED_PC() and been done with it! Note that
1105 FRAME_SAVED_PC() is being superseed by frame_pc_unwind() and that
1106 function does have somewhere to cache that PC value. */
4c1e7e9d 1107
2ca6c561 1108 if (DEPRECATED_INIT_FRAME_PC_FIRST_P ())
97f46953 1109 prev->pc = (DEPRECATED_INIT_FRAME_PC_FIRST (fromleaf, prev));
4c1e7e9d
AC
1110
1111 if (INIT_EXTRA_FRAME_INFO_P ())
1112 INIT_EXTRA_FRAME_INFO (fromleaf, prev);
1113
1114 /* This entry is in the frame queue now, which is good since
95adb866
AC
1115 FRAME_SAVED_PC may use that queue to figure out its value (see
1116 tm-sparc.h). We want the pc saved in the inferior frame. */
a5afb99f
AC
1117 if (DEPRECATED_INIT_FRAME_PC_P ())
1118 prev->pc = DEPRECATED_INIT_FRAME_PC (fromleaf, prev);
4c1e7e9d 1119
95adb866
AC
1120 /* If ->frame and ->pc are unchanged, we are in the process of
1121 getting ourselves into an infinite backtrace. Some architectures
1122 check this in FRAME_CHAIN or thereabouts, but it seems like there
1123 is no reason this can't be an architecture-independent check. */
1124 if (prev->frame == next_frame->frame
1125 && prev->pc == next_frame->pc)
4c1e7e9d 1126 {
95adb866
AC
1127 next_frame->prev = NULL;
1128 obstack_free (&frame_cache_obstack, prev);
1129 return NULL;
4c1e7e9d
AC
1130 }
1131
1132 /* Initialize the code used to unwind the frame PREV based on the PC
1133 (and probably other architectural information). The PC lets you
1134 check things like the debug info at that point (dwarf2cfi?) and
1135 use that to decide how the frame should be unwound. */
494cca16 1136 prev->unwind = frame_unwind_find_by_pc (current_gdbarch, prev->pc);
4c1e7e9d 1137
5a203e44
AC
1138 /* NOTE: cagney/2002-11-18: The code segments, found in
1139 create_new_frame and get_prev_frame(), that initializes the
1140 frames type is subtly different. The latter only updates ->type
1141 when it encounters a SIGTRAMP_FRAME or DUMMY_FRAME. This stops
1142 get_prev_frame() overriding the frame's type when the INIT code
1143 has previously set it. This is really somewhat bogus. The
1144 initialization, as seen in create_new_frame(), should occur
1145 before the INIT function has been called. */
07555a72 1146 if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
ae45cd16
AC
1147 && (DEPRECATED_PC_IN_CALL_DUMMY_P ()
1148 ? DEPRECATED_PC_IN_CALL_DUMMY (prev->pc, 0, 0)
1149 : pc_in_dummy_frame (prev->pc)))
5a203e44
AC
1150 prev->type = DUMMY_FRAME;
1151 else
1152 {
1153 /* FIXME: cagney/2002-11-10: This should be moved to before the
1154 INIT code above so that the INIT code knows what the frame's
1155 type is (in fact, for a [generic] dummy-frame, the type can
1156 be set and then the entire initialization can be skipped.
1157 Unforunatly, its the INIT code that sets the PC (Hmm, catch
1158 22). */
1159 char *name;
1160 find_pc_partial_function (prev->pc, &name, NULL, NULL);
1161 if (PC_IN_SIGTRAMP (prev->pc, name))
1162 prev->type = SIGTRAMP_FRAME;
1163 /* FIXME: cagney/2002-11-11: Leave prev->type alone. Some
1164 architectures are forcing the frame's type in INIT so we
1165 don't want to override it here. Remember, NORMAL_FRAME == 0,
1166 so it all works (just :-/). Once this initialization is
1167 moved to the start of this function, all this nastness will
1168 go away. */
1169 }
4c1e7e9d
AC
1170
1171 return prev;
1172}
1173
eb4f72c5
AC
1174/* Return a structure containing various interesting information
1175 about the frame that called NEXT_FRAME. Returns NULL
1176 if there is no such frame. */
1177
1178struct frame_info *
1179get_prev_frame (struct frame_info *next_frame)
1180{
1181 struct frame_info *prev_frame;
1182
1183 /* Return the inner-most frame, when the caller passes in NULL. */
1184 /* NOTE: cagney/2002-11-09: Not sure how this would happen. The
1185 caller should have previously obtained a valid frame using
1186 get_selected_frame() and then called this code - only possibility
1187 I can think of is code behaving badly.
1188
1189 NOTE: cagney/2003-01-10: Talk about code behaving badly. Check
1190 block_innermost_frame(). It does the sequence: frame = NULL;
1191 while (1) { frame = get_prev_frame (frame); .... }. Ulgh! Why
1192 it couldn't be written better, I don't know.
1193
1194 NOTE: cagney/2003-01-11: I suspect what is happening is
1195 block_innermost_frame() is, when the target has no state
1196 (registers, memory, ...), still calling this function. The
1197 assumption being that this function will return NULL indicating
1198 that a frame isn't possible, rather than checking that the target
1199 has state and then calling get_current_frame() and
1200 get_prev_frame(). This is a guess mind. */
1201 if (next_frame == NULL)
1202 {
1203 /* NOTE: cagney/2002-11-09: There was a code segment here that
1204 would error out when CURRENT_FRAME was NULL. The comment
1205 that went with it made the claim ...
1206
1207 ``This screws value_of_variable, which just wants a nice
1208 clean NULL return from block_innermost_frame if there are no
1209 frames. I don't think I've ever seen this message happen
1210 otherwise. And returning NULL here is a perfectly legitimate
1211 thing to do.''
1212
1213 Per the above, this code shouldn't even be called with a NULL
1214 NEXT_FRAME. */
1215 return current_frame;
1216 }
1217
1218 /* There is always a frame. If this assertion fails, suspect that
1219 something should be calling get_selected_frame() or
1220 get_current_frame(). */
1221 gdb_assert (next_frame != NULL);
1222
1223 if (next_frame->level >= 0
1224 && !backtrace_below_main
1225 && inside_main_func (get_frame_pc (next_frame)))
1226 /* Don't unwind past main(), bug always unwind the sentinel frame.
1227 Note, this is done _before_ the frame has been marked as
1228 previously unwound. That way if the user later decides to
1229 allow unwinds past main(), that just happens. */
ac2bd0a9
AC
1230 {
1231 if (frame_debug)
1232 fprintf_unfiltered (gdb_stdlog,
1233 "Outermost frame - inside main func.\n");
1234 return NULL;
1235 }
eb4f72c5
AC
1236
1237 /* Only try to do the unwind once. */
1238 if (next_frame->prev_p)
1239 return next_frame->prev;
1240 next_frame->prev_p = 1;
1241
b14185ce
AC
1242 /* If we're inside the entry file, it isn't valid. Don't apply this
1243 test to a dummy frame - dummy frame PC's typically land in the
1244 entry file. Don't apply this test to the sentinel frame.
1245 Sentinel frames should always be allowed to unwind. */
eb4f72c5
AC
1246 /* NOTE: drow/2002-12-25: should there be a way to disable this
1247 check? It assumes a single small entry file, and the way some
1248 debug readers (e.g. dbxread) figure out which object is the
1249 entry file is somewhat hokey. */
1250 /* NOTE: cagney/2003-01-10: If there is a way of disabling this test
1251 then it should probably be moved to before the ->prev_p test,
1252 above. */
b14185ce
AC
1253 if (next_frame->type != DUMMY_FRAME && next_frame->level >= 0
1254 && inside_entry_file (get_frame_pc (next_frame)))
ac2bd0a9
AC
1255 {
1256 if (frame_debug)
1257 fprintf_unfiltered (gdb_stdlog,
1258 "Outermost frame - inside entry file\n");
eb4f72c5 1259 return NULL;
ac2bd0a9 1260 }
eb4f72c5 1261
b14185ce
AC
1262 /* If we're already inside the entry function for the main objfile,
1263 then it isn't valid. Don't apply this test to a dummy frame -
1264 dummy frame PC's typically land in the entry func. Don't apply
1265 this test to the sentinel frame. Sentinel frames should always
1266 be allowed to unwind. */
1267 /* NOTE: cagney/2003-02-25: Don't enable until someone has found
1268 hard evidence that this is needed. */
1269 if (0
1270 && next_frame->type != DUMMY_FRAME && next_frame->level >= 0
1271 && inside_entry_func (get_frame_pc (next_frame)))
1272 {
1273 if (frame_debug)
1274 fprintf_unfiltered (gdb_stdlog,
1275 "Outermost frame - inside entry func\n");
1276 return NULL;
1277 }
1278
eb4f72c5
AC
1279 /* If any of the old frame initialization methods are around, use
1280 the legacy get_prev_frame method. Just don't try to unwind a
1281 sentinel frame using that method - it doesn't work. All sentinal
1282 frames use the new unwind code. */
1283 if ((DEPRECATED_INIT_FRAME_PC_P ()
1284 || DEPRECATED_INIT_FRAME_PC_FIRST_P ()
d62d1979
AC
1285 || INIT_EXTRA_FRAME_INFO_P ()
1286 || FRAME_CHAIN_P ())
eb4f72c5 1287 && next_frame->level >= 0)
ac2bd0a9
AC
1288 {
1289 prev_frame = legacy_get_prev_frame (next_frame);
1290 if (frame_debug && prev_frame == NULL)
1291 fprintf_unfiltered (gdb_stdlog,
1292 "Outermost frame - legacy_get_prev_frame NULL.\n");
1293 return prev_frame;
1294 }
eb4f72c5
AC
1295
1296 /* Allocate the new frame but do not wire it in to the frame chain.
1297 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1298 frame->next to pull some fancy tricks (of course such code is, by
1299 definition, recursive). Try to prevent it.
1300
1301 There is no reason to worry about memory leaks, should the
1302 remainder of the function fail. The allocated memory will be
1303 quickly reclaimed when the frame cache is flushed, and the `we've
1304 been here before' check above will stop repeated memory
1305 allocation calls. */
1306 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1307 prev_frame->level = next_frame->level + 1;
1308
1309 /* Try to unwind the PC. If that doesn't work, assume we've reached
1310 the oldest frame and simply return. Is there a better sentinal
1311 value? The unwound PC value is then used to initialize the new
1312 previous frame's type.
1313
1314 Note that the pc-unwind is intentionally performed before the
1315 frame chain. This is ok since, for old targets, both
1316 frame_pc_unwind (nee, FRAME_SAVED_PC) and FRAME_CHAIN()) assume
1317 NEXT_FRAME's data structures have already been initialized (using
1318 INIT_EXTRA_FRAME_INFO) and hence the call order doesn't matter.
1319
1320 By unwinding the PC first, it becomes possible to, in the case of
1321 a dummy frame, avoid also unwinding the frame ID. This is
1322 because (well ignoring the PPC) a dummy frame can be located
1323 using NEXT_FRAME's frame ID. */
1324
1325 prev_frame->pc = frame_pc_unwind (next_frame);
1326 if (prev_frame->pc == 0)
ac2bd0a9
AC
1327 {
1328 /* The allocated PREV_FRAME will be reclaimed when the frame
1329 obstack is next purged. */
1330 if (frame_debug)
1331 fprintf_unfiltered (gdb_stdlog,
1332 "Outermost frame - unwound PC zero\n");
1333 return NULL;
1334 }
eb4f72c5
AC
1335 prev_frame->type = frame_type_from_pc (prev_frame->pc);
1336
1337 /* Set the unwind functions based on that identified PC. */
1338 prev_frame->unwind = frame_unwind_find_by_pc (current_gdbarch,
1339 prev_frame->pc);
1340
1341 /* FIXME: cagney/2003-01-13: A dummy frame doesn't need to unwind
1342 the frame ID because the frame ID comes from the previous frame.
1343 The other frames do though. True? */
1344 {
1345 /* FIXME: cagney/2002-12-18: Instead of this hack, should just
1346 save the frame ID directly. */
1347 struct frame_id id = frame_id_unwind (next_frame);
b14185ce
AC
1348 /* Check that the unwound ID is valid. As of 2003-02-24 the
1349 x86-64 was returning an invalid frame ID when trying to do an
1350 unwind a sentinel frame that belonged to a frame dummy. */
eb4f72c5 1351 if (!frame_id_p (id))
ac2bd0a9
AC
1352 {
1353 if (frame_debug)
1354 fprintf_unfiltered (gdb_stdlog,
1355 "Outermost frame - unwound frame ID invalid\n");
1356 return NULL;
1357 }
b14185ce
AC
1358 /* Check that the new frame isn't inner to (younger, below, next)
1359 the old frame. If that happens the frame unwind is going
1360 backwards. */
1361 /* FIXME: cagney/2003-02-25: Ignore the sentinel frame since that
1362 doesn't have a valid frame ID. Should instead set the sentinel
1363 frame's frame ID to a `sentinel'. Leave it until after the
1364 switch to storing the frame ID, instead of the frame base, in
1365 the frame object. */
1366 if (next_frame->level >= 0
1367 && frame_id_inner (id, get_frame_id (next_frame)))
1368 error ("Unwound frame inner-to selected frame (corrupt stack?)");
1369 /* Note that, due to frameless functions, the stronger test of the
1370 new frame being outer to the old frame can't be used -
1371 frameless functions differ by only their PC value. */
eb4f72c5
AC
1372 prev_frame->frame = id.base;
1373 }
1374
1375 /* Link it in. */
1376 next_frame->prev = prev_frame;
1377 prev_frame->next = next_frame;
1378
1379 /* FIXME: cagney/2002-01-19: This call will go away. Instead of
1380 initializing extra info, all frames will use the frame_cache
1381 (passed to the unwind functions) to store additional frame info.
1382 Unfortunatly legacy targets can't use legacy_get_prev_frame() to
1383 unwind the sentinel frame and, consequently, are forced to take
1384 this code path and rely on the below call to INIT_EXTR_FRAME_INFO
1385 to initialize the inner-most frame. */
1386 if (INIT_EXTRA_FRAME_INFO_P ())
1387 {
1388 gdb_assert (prev_frame->level == 0);
1389 INIT_EXTRA_FRAME_INFO (0, prev_frame);
1390 }
1391
1392 return prev_frame;
1393}
1394
4c1e7e9d
AC
1395CORE_ADDR
1396get_frame_pc (struct frame_info *frame)
1397{
1398 return frame->pc;
1399}
1400
1058bca7
AC
1401static int
1402pc_notcurrent (struct frame_info *frame)
1403{
1404 /* If FRAME is not the innermost frame, that normally means that
1405 FRAME->pc points at the return instruction (which is *after* the
1406 call instruction), and we want to get the line containing the
1407 call (because the call is where the user thinks the program is).
1408 However, if the next frame is either a SIGTRAMP_FRAME or a
1409 DUMMY_FRAME, then the next frame will contain a saved interrupt
1410 PC and such a PC indicates the current (rather than next)
1411 instruction/line, consequently, for such cases, want to get the
1412 line containing fi->pc. */
1413 struct frame_info *next = get_next_frame (frame);
1414 int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME);
1415 return notcurrent;
1416}
1417
1418void
1419find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1420{
1421 (*sal) = find_pc_line (frame->pc, pc_notcurrent (frame));
1422}
1423
c193f6ac
AC
1424/* Per "frame.h", return the ``address'' of the frame. Code should
1425 really be using get_frame_id(). */
1426CORE_ADDR
1427get_frame_base (struct frame_info *fi)
1428{
1429 return fi->frame;
1430}
1431
85cf597a
AC
1432/* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1433 or -1 for a NULL frame. */
1434
1435int
1436frame_relative_level (struct frame_info *fi)
1437{
1438 if (fi == NULL)
1439 return -1;
1440 else
1441 return fi->level;
1442}
1443
5a203e44
AC
1444enum frame_type
1445get_frame_type (struct frame_info *frame)
1446{
1447 /* Some targets still don't use [generic] dummy frames. Catch them
1448 here. */
07555a72 1449 if (!DEPRECATED_USE_GENERIC_DUMMY_FRAMES
5a203e44
AC
1450 && deprecated_frame_in_dummy (frame))
1451 return DUMMY_FRAME;
1452 return frame->type;
1453}
1454
1455void
1456deprecated_set_frame_type (struct frame_info *frame, enum frame_type type)
1457{
1458 /* Arrrg! See comment in "frame.h". */
1459 frame->type = type;
1460}
1461
4c1e7e9d
AC
1462#ifdef FRAME_FIND_SAVED_REGS
1463/* XXX - deprecated. This is a compatibility function for targets
1464 that do not yet implement FRAME_INIT_SAVED_REGS. */
1465/* Find the addresses in which registers are saved in FRAME. */
1466
1467void
95486978
AC
1468deprecated_get_frame_saved_regs (struct frame_info *frame,
1469 struct frame_saved_regs *saved_regs_addr)
4c1e7e9d
AC
1470{
1471 if (frame->saved_regs == NULL)
1472 {
1473 frame->saved_regs = (CORE_ADDR *)
479ab5a0 1474 frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS);
4c1e7e9d
AC
1475 }
1476 if (saved_regs_addr == NULL)
1477 {
1478 struct frame_saved_regs saved_regs;
1479 FRAME_FIND_SAVED_REGS (frame, saved_regs);
1480 memcpy (frame->saved_regs, &saved_regs, SIZEOF_FRAME_SAVED_REGS);
1481 }
1482 else
1483 {
1484 FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr);
1485 memcpy (frame->saved_regs, saved_regs_addr, SIZEOF_FRAME_SAVED_REGS);
1486 }
1487}
1488#endif
1489
0394eb2a
AC
1490struct frame_extra_info *
1491get_frame_extra_info (struct frame_info *fi)
1492{
1493 return fi->extra_info;
1494}
1495
2c517d0e
AC
1496struct frame_extra_info *
1497frame_extra_info_zalloc (struct frame_info *fi, long size)
1498{
479ab5a0 1499 fi->extra_info = frame_obstack_zalloc (size);
2c517d0e
AC
1500 return fi->extra_info;
1501}
1502
b87efeee 1503void
2f107107 1504deprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc)
b87efeee 1505{
2f107107 1506 /* See comment in "frame.h". */
a94dd1fd 1507 gdb_assert (frame->next != NULL);
2f107107
AC
1508 frame->pc = pc;
1509}
1510
1511void
1512deprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base)
1513{
1514 /* See comment in "frame.h". */
1515 frame->frame = base;
b87efeee
AC
1516}
1517
c8b8a898
AC
1518void
1519deprecated_set_frame_saved_regs_hack (struct frame_info *frame,
1520 CORE_ADDR *saved_regs)
1521{
1522 frame->saved_regs = saved_regs;
1523}
1524
1525void
1526deprecated_set_frame_extra_info_hack (struct frame_info *frame,
1527 struct frame_extra_info *extra_info)
1528{
1529 frame->extra_info = extra_info;
1530}
1531
483d36b2
AC
1532void
1533deprecated_set_frame_next_hack (struct frame_info *fi,
1534 struct frame_info *next)
1535{
1536 fi->next = next;
1537}
1538
1539void
1540deprecated_set_frame_prev_hack (struct frame_info *fi,
1541 struct frame_info *prev)
1542{
1543 fi->prev = prev;
1544}
1545
2d75187b
AC
1546struct context *
1547deprecated_get_frame_context (struct frame_info *fi)
1548{
1549 return fi->context;
1550}
1551
1552void
1553deprecated_set_frame_context (struct frame_info *fi,
1554 struct context *context)
1555{
1556 fi->context = context;
1557}
1558
c8b8a898
AC
1559struct frame_info *
1560deprecated_frame_xmalloc (void)
1561{
1562 struct frame_info *frame = XMALLOC (struct frame_info);
1563 memset (frame, 0, sizeof (struct frame_info));
1564 return frame;
1565}
1566
f6c609c4
AC
1567struct frame_info *
1568deprecated_frame_xmalloc_with_cleanup (long sizeof_saved_regs,
1569 long sizeof_extra_info)
1570{
1571 struct frame_info *frame = deprecated_frame_xmalloc ();
1572 make_cleanup (xfree, frame);
1573 if (sizeof_saved_regs > 0)
1574 {
1575 frame->saved_regs = xcalloc (1, sizeof_saved_regs);
1576 make_cleanup (xfree, frame->saved_regs);
1577 }
1578 if (sizeof_extra_info > 0)
1579 {
1580 frame->extra_info = xcalloc (1, sizeof_extra_info);
1581 make_cleanup (xfree, frame->extra_info);
1582 }
1583 return frame;
1584}
c8b8a898 1585
4c1e7e9d
AC
1586void
1587_initialize_frame (void)
1588{
1589 obstack_init (&frame_cache_obstack);
eb4f72c5
AC
1590
1591 /* FIXME: cagney/2003-01-19: This command needs a rename. Suggest
1592 `set backtrace {past,beyond,...}-main'. Also suggest adding `set
1593 backtrace ...-start' to control backtraces past start. The
1594 problem with `below' is that it stops the `up' command. */
1595
1596 add_setshow_boolean_cmd ("backtrace-below-main", class_obscure,
1597 &backtrace_below_main, "\
1598Set whether backtraces should continue past \"main\".\n\
1599Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
1600the backtrace at \"main\". Set this variable if you need to see the rest\n\
1601of the stack trace.", "\
1602Show whether backtraces should continue past \"main\".\n\
1603Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
1604the backtrace at \"main\". Set this variable if you need to see the rest\n\
1605of the stack trace.",
1606 NULL, NULL, &setlist, &showlist);
ac2bd0a9
AC
1607
1608
1609 /* Debug this files internals. */
1610 add_show_from_set (add_set_cmd ("frame", class_maintenance, var_zinteger,
1611 &frame_debug, "Set frame debugging.\n\
1612When non-zero, frame specific internal debugging is enabled.", &setdebuglist),
1613 &showdebuglist);
4c1e7e9d 1614}
This page took 0.256913 seconds and 4 git commands to generate.