Fix "info frame" in the outermost frame.
[deliverable/binutils-gdb.git] / gdb / frame.c
1 /* Cache and manage frames for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "target.h"
23 #include "value.h"
24 #include "inferior.h" /* for inferior_ptid */
25 #include "regcache.h"
26 #include "gdb_assert.h"
27 #include <string.h>
28 #include "user-regs.h"
29 #include "gdb_obstack.h"
30 #include "dummy-frame.h"
31 #include "sentinel-frame.h"
32 #include "gdbcore.h"
33 #include "annotate.h"
34 #include "language.h"
35 #include "frame-unwind.h"
36 #include "frame-base.h"
37 #include "command.h"
38 #include "gdbcmd.h"
39 #include "observer.h"
40 #include "objfiles.h"
41 #include "exceptions.h"
42 #include "gdbthread.h"
43 #include "block.h"
44 #include "inline-frame.h"
45 #include "tracepoint.h"
46 #include "hashtab.h"
47 #include "valprint.h"
48
49 static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
50 static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
51 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason);
52
53 /* Status of some values cached in the frame_info object. */
54
55 enum cached_copy_status
56 {
57 /* Value is unknown. */
58 CC_UNKNOWN,
59
60 /* We have a value. */
61 CC_VALUE,
62
63 /* Value was not saved. */
64 CC_NOT_SAVED,
65
66 /* Value is unavailable. */
67 CC_UNAVAILABLE
68 };
69
70 /* We keep a cache of stack frames, each of which is a "struct
71 frame_info". The innermost one gets allocated (in
72 wait_for_inferior) each time the inferior stops; current_frame
73 points to it. Additional frames get allocated (in get_prev_frame)
74 as needed, and are chained through the next and prev fields. Any
75 time that the frame cache becomes invalid (most notably when we
76 execute something, but also if we change how we interpret the
77 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
78 which reads new symbols)), we should call reinit_frame_cache. */
79
80 struct frame_info
81 {
82 /* Level of this frame. The inner-most (youngest) frame is at level
83 0. As you move towards the outer-most (oldest) frame, the level
84 increases. This is a cached value. It could just as easily be
85 computed by counting back from the selected frame to the inner
86 most frame. */
87 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
88 reserved to indicate a bogus frame - one that has been created
89 just to keep GDB happy (GDB always needs a frame). For the
90 moment leave this as speculation. */
91 int level;
92
93 /* The frame's program space. */
94 struct program_space *pspace;
95
96 /* The frame's address space. */
97 struct address_space *aspace;
98
99 /* The frame's low-level unwinder and corresponding cache. The
100 low-level unwinder is responsible for unwinding register values
101 for the previous frame. The low-level unwind methods are
102 selected based on the presence, or otherwise, of register unwind
103 information such as CFI. */
104 void *prologue_cache;
105 const struct frame_unwind *unwind;
106
107 /* Cached copy of the previous frame's architecture. */
108 struct
109 {
110 int p;
111 struct gdbarch *arch;
112 } prev_arch;
113
114 /* Cached copy of the previous frame's resume address. */
115 struct {
116 enum cached_copy_status status;
117 CORE_ADDR value;
118 } prev_pc;
119
120 /* Cached copy of the previous frame's function address. */
121 struct
122 {
123 CORE_ADDR addr;
124 int p;
125 } prev_func;
126
127 /* This frame's ID. */
128 struct
129 {
130 int p;
131 struct frame_id value;
132 } this_id;
133
134 /* The frame's high-level base methods, and corresponding cache.
135 The high level base methods are selected based on the frame's
136 debug info. */
137 const struct frame_base *base;
138 void *base_cache;
139
140 /* Pointers to the next (down, inner, younger) and previous (up,
141 outer, older) frame_info's in the frame cache. */
142 struct frame_info *next; /* down, inner, younger */
143 int prev_p;
144 struct frame_info *prev; /* up, outer, older */
145
146 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
147 could. Only valid when PREV_P is set. */
148 enum unwind_stop_reason stop_reason;
149 };
150
151 /* A frame stash used to speed up frame lookups. Create a hash table
152 to stash frames previously accessed from the frame cache for
153 quicker subsequent retrieval. The hash table is emptied whenever
154 the frame cache is invalidated. */
155
156 static htab_t frame_stash;
157
158 /* Internal function to calculate a hash from the frame_id addresses,
159 using as many valid addresses as possible. Frames below level 0
160 are not stored in the hash table. */
161
162 static hashval_t
163 frame_addr_hash (const void *ap)
164 {
165 const struct frame_info *frame = ap;
166 const struct frame_id f_id = frame->this_id.value;
167 hashval_t hash = 0;
168
169 gdb_assert (f_id.stack_addr_p || f_id.code_addr_p
170 || f_id.special_addr_p);
171
172 if (f_id.stack_addr_p)
173 hash = iterative_hash (&f_id.stack_addr,
174 sizeof (f_id.stack_addr), hash);
175 if (f_id.code_addr_p)
176 hash = iterative_hash (&f_id.code_addr,
177 sizeof (f_id.code_addr), hash);
178 if (f_id.special_addr_p)
179 hash = iterative_hash (&f_id.special_addr,
180 sizeof (f_id.special_addr), hash);
181
182 return hash;
183 }
184
185 /* Internal equality function for the hash table. This function
186 defers equality operations to frame_id_eq. */
187
188 static int
189 frame_addr_hash_eq (const void *a, const void *b)
190 {
191 const struct frame_info *f_entry = a;
192 const struct frame_info *f_element = b;
193
194 return frame_id_eq (f_entry->this_id.value,
195 f_element->this_id.value);
196 }
197
198 /* Internal function to create the frame_stash hash table. 100 seems
199 to be a good compromise to start the hash table at. */
200
201 static void
202 frame_stash_create (void)
203 {
204 frame_stash = htab_create (100,
205 frame_addr_hash,
206 frame_addr_hash_eq,
207 NULL);
208 }
209
210 /* Internal function to add a frame to the frame_stash hash table.
211 Returns false if a frame with the same ID was already stashed, true
212 otherwise. */
213
214 static int
215 frame_stash_add (struct frame_info *frame)
216 {
217 struct frame_info **slot;
218
219 /* Do not try to stash the sentinel frame. */
220 gdb_assert (frame->level >= 0);
221
222 slot = (struct frame_info **) htab_find_slot (frame_stash,
223 frame,
224 INSERT);
225
226 /* If we already have a frame in the stack with the same id, we
227 either have a stack cycle (corrupted stack?), or some bug
228 elsewhere in GDB. In any case, ignore the duplicate and return
229 an indication to the caller. */
230 if (*slot != NULL)
231 return 0;
232
233 *slot = frame;
234 return 1;
235 }
236
237 /* Internal function to search the frame stash for an entry with the
238 given frame ID. If found, return that frame. Otherwise return
239 NULL. */
240
241 static struct frame_info *
242 frame_stash_find (struct frame_id id)
243 {
244 struct frame_info dummy;
245 struct frame_info *frame;
246
247 dummy.this_id.value = id;
248 frame = htab_find (frame_stash, &dummy);
249 return frame;
250 }
251
252 /* Internal function to invalidate the frame stash by removing all
253 entries in it. This only occurs when the frame cache is
254 invalidated. */
255
256 static void
257 frame_stash_invalidate (void)
258 {
259 htab_empty (frame_stash);
260 }
261
262 /* Flag to control debugging. */
263
264 unsigned int frame_debug;
265 static void
266 show_frame_debug (struct ui_file *file, int from_tty,
267 struct cmd_list_element *c, const char *value)
268 {
269 fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
270 }
271
272 /* Flag to indicate whether backtraces should stop at main et.al. */
273
274 static int backtrace_past_main;
275 static void
276 show_backtrace_past_main (struct ui_file *file, int from_tty,
277 struct cmd_list_element *c, const char *value)
278 {
279 fprintf_filtered (file,
280 _("Whether backtraces should "
281 "continue past \"main\" is %s.\n"),
282 value);
283 }
284
285 static int backtrace_past_entry;
286 static void
287 show_backtrace_past_entry (struct ui_file *file, int from_tty,
288 struct cmd_list_element *c, const char *value)
289 {
290 fprintf_filtered (file, _("Whether backtraces should continue past the "
291 "entry point of a program is %s.\n"),
292 value);
293 }
294
295 static unsigned int backtrace_limit = UINT_MAX;
296 static void
297 show_backtrace_limit (struct ui_file *file, int from_tty,
298 struct cmd_list_element *c, const char *value)
299 {
300 fprintf_filtered (file,
301 _("An upper bound on the number "
302 "of backtrace levels is %s.\n"),
303 value);
304 }
305
306
307 static void
308 fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
309 {
310 if (p)
311 fprintf_unfiltered (file, "%s=%s", name, hex_string (addr));
312 else
313 fprintf_unfiltered (file, "!%s", name);
314 }
315
316 void
317 fprint_frame_id (struct ui_file *file, struct frame_id id)
318 {
319 fprintf_unfiltered (file, "{");
320 fprint_field (file, "stack", id.stack_addr_p, id.stack_addr);
321 fprintf_unfiltered (file, ",");
322 fprint_field (file, "code", id.code_addr_p, id.code_addr);
323 fprintf_unfiltered (file, ",");
324 fprint_field (file, "special", id.special_addr_p, id.special_addr);
325 if (id.artificial_depth)
326 fprintf_unfiltered (file, ",artificial=%d", id.artificial_depth);
327 fprintf_unfiltered (file, "}");
328 }
329
330 static void
331 fprint_frame_type (struct ui_file *file, enum frame_type type)
332 {
333 switch (type)
334 {
335 case NORMAL_FRAME:
336 fprintf_unfiltered (file, "NORMAL_FRAME");
337 return;
338 case DUMMY_FRAME:
339 fprintf_unfiltered (file, "DUMMY_FRAME");
340 return;
341 case INLINE_FRAME:
342 fprintf_unfiltered (file, "INLINE_FRAME");
343 return;
344 case TAILCALL_FRAME:
345 fprintf_unfiltered (file, "TAILCALL_FRAME");
346 return;
347 case SIGTRAMP_FRAME:
348 fprintf_unfiltered (file, "SIGTRAMP_FRAME");
349 return;
350 case ARCH_FRAME:
351 fprintf_unfiltered (file, "ARCH_FRAME");
352 return;
353 case SENTINEL_FRAME:
354 fprintf_unfiltered (file, "SENTINEL_FRAME");
355 return;
356 default:
357 fprintf_unfiltered (file, "<unknown type>");
358 return;
359 };
360 }
361
362 static void
363 fprint_frame (struct ui_file *file, struct frame_info *fi)
364 {
365 if (fi == NULL)
366 {
367 fprintf_unfiltered (file, "<NULL frame>");
368 return;
369 }
370 fprintf_unfiltered (file, "{");
371 fprintf_unfiltered (file, "level=%d", fi->level);
372 fprintf_unfiltered (file, ",");
373 fprintf_unfiltered (file, "type=");
374 if (fi->unwind != NULL)
375 fprint_frame_type (file, fi->unwind->type);
376 else
377 fprintf_unfiltered (file, "<unknown>");
378 fprintf_unfiltered (file, ",");
379 fprintf_unfiltered (file, "unwind=");
380 if (fi->unwind != NULL)
381 gdb_print_host_address (fi->unwind, file);
382 else
383 fprintf_unfiltered (file, "<unknown>");
384 fprintf_unfiltered (file, ",");
385 fprintf_unfiltered (file, "pc=");
386 if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN)
387 fprintf_unfiltered (file, "<unknown>");
388 else if (fi->next->prev_pc.status == CC_VALUE)
389 fprintf_unfiltered (file, "%s",
390 hex_string (fi->next->prev_pc.value));
391 else if (fi->next->prev_pc.status == CC_NOT_SAVED)
392 val_print_not_saved (file);
393 else if (fi->next->prev_pc.status == CC_UNAVAILABLE)
394 val_print_unavailable (file);
395 fprintf_unfiltered (file, ",");
396 fprintf_unfiltered (file, "id=");
397 if (fi->this_id.p)
398 fprint_frame_id (file, fi->this_id.value);
399 else
400 fprintf_unfiltered (file, "<unknown>");
401 fprintf_unfiltered (file, ",");
402 fprintf_unfiltered (file, "func=");
403 if (fi->next != NULL && fi->next->prev_func.p)
404 fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_func.addr));
405 else
406 fprintf_unfiltered (file, "<unknown>");
407 fprintf_unfiltered (file, "}");
408 }
409
410 /* Given FRAME, return the enclosing frame as found in real frames read-in from
411 inferior memory. Skip any previous frames which were made up by GDB.
412 Return the original frame if no immediate previous frames exist. */
413
414 static struct frame_info *
415 skip_artificial_frames (struct frame_info *frame)
416 {
417 while (get_frame_type (frame) == INLINE_FRAME
418 || get_frame_type (frame) == TAILCALL_FRAME)
419 frame = get_prev_frame (frame);
420
421 return frame;
422 }
423
424 /* Compute the frame's uniq ID that can be used to, later, re-find the
425 frame. */
426
427 static void
428 compute_frame_id (struct frame_info *fi)
429 {
430 gdb_assert (!fi->this_id.p);
431
432 if (frame_debug)
433 fprintf_unfiltered (gdb_stdlog, "{ compute_frame_id (fi=%d) ",
434 fi->level);
435 /* Find the unwinder. */
436 if (fi->unwind == NULL)
437 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
438 /* Find THIS frame's ID. */
439 /* Default to outermost if no ID is found. */
440 fi->this_id.value = outer_frame_id;
441 fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
442 gdb_assert (frame_id_p (fi->this_id.value));
443 fi->this_id.p = 1;
444 if (frame_debug)
445 {
446 fprintf_unfiltered (gdb_stdlog, "-> ");
447 fprint_frame_id (gdb_stdlog, fi->this_id.value);
448 fprintf_unfiltered (gdb_stdlog, " }\n");
449 }
450 }
451
452 /* Return a frame uniq ID that can be used to, later, re-find the
453 frame. */
454
455 struct frame_id
456 get_frame_id (struct frame_info *fi)
457 {
458 if (fi == NULL)
459 return null_frame_id;
460
461 gdb_assert (fi->this_id.p);
462 return fi->this_id.value;
463 }
464
465 struct frame_id
466 get_stack_frame_id (struct frame_info *next_frame)
467 {
468 return get_frame_id (skip_artificial_frames (next_frame));
469 }
470
471 struct frame_id
472 frame_unwind_caller_id (struct frame_info *next_frame)
473 {
474 struct frame_info *this_frame;
475
476 /* Use get_prev_frame_1, and not get_prev_frame. The latter will truncate
477 the frame chain, leading to this function unintentionally
478 returning a null_frame_id (e.g., when a caller requests the frame
479 ID of "main()"s caller. */
480
481 next_frame = skip_artificial_frames (next_frame);
482 this_frame = get_prev_frame_1 (next_frame);
483 if (this_frame)
484 return get_frame_id (skip_artificial_frames (this_frame));
485 else
486 return null_frame_id;
487 }
488
489 const struct frame_id null_frame_id; /* All zeros. */
490 const struct frame_id outer_frame_id = { 0, 0, 0, 0, 0, 1, 0 };
491
492 struct frame_id
493 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
494 CORE_ADDR special_addr)
495 {
496 struct frame_id id = null_frame_id;
497
498 id.stack_addr = stack_addr;
499 id.stack_addr_p = 1;
500 id.code_addr = code_addr;
501 id.code_addr_p = 1;
502 id.special_addr = special_addr;
503 id.special_addr_p = 1;
504 return id;
505 }
506
507 struct frame_id
508 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
509 {
510 struct frame_id id = null_frame_id;
511
512 id.stack_addr = stack_addr;
513 id.stack_addr_p = 1;
514 id.code_addr = code_addr;
515 id.code_addr_p = 1;
516 return id;
517 }
518
519 struct frame_id
520 frame_id_build_wild (CORE_ADDR stack_addr)
521 {
522 struct frame_id id = null_frame_id;
523
524 id.stack_addr = stack_addr;
525 id.stack_addr_p = 1;
526 return id;
527 }
528
529 int
530 frame_id_p (struct frame_id l)
531 {
532 int p;
533
534 /* The frame is valid iff it has a valid stack address. */
535 p = l.stack_addr_p;
536 /* outer_frame_id is also valid. */
537 if (!p && memcmp (&l, &outer_frame_id, sizeof (l)) == 0)
538 p = 1;
539 if (frame_debug)
540 {
541 fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
542 fprint_frame_id (gdb_stdlog, l);
543 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
544 }
545 return p;
546 }
547
548 int
549 frame_id_artificial_p (struct frame_id l)
550 {
551 if (!frame_id_p (l))
552 return 0;
553
554 return (l.artificial_depth != 0);
555 }
556
557 int
558 frame_id_eq (struct frame_id l, struct frame_id r)
559 {
560 int eq;
561
562 if (!l.stack_addr_p && l.special_addr_p
563 && !r.stack_addr_p && r.special_addr_p)
564 /* The outermost frame marker is equal to itself. This is the
565 dodgy thing about outer_frame_id, since between execution steps
566 we might step into another function - from which we can't
567 unwind either. More thought required to get rid of
568 outer_frame_id. */
569 eq = 1;
570 else if (!l.stack_addr_p || !r.stack_addr_p)
571 /* Like a NaN, if either ID is invalid, the result is false.
572 Note that a frame ID is invalid iff it is the null frame ID. */
573 eq = 0;
574 else if (l.stack_addr != r.stack_addr)
575 /* If .stack addresses are different, the frames are different. */
576 eq = 0;
577 else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
578 /* An invalid code addr is a wild card. If .code addresses are
579 different, the frames are different. */
580 eq = 0;
581 else if (l.special_addr_p && r.special_addr_p
582 && l.special_addr != r.special_addr)
583 /* An invalid special addr is a wild card (or unused). Otherwise
584 if special addresses are different, the frames are different. */
585 eq = 0;
586 else if (l.artificial_depth != r.artificial_depth)
587 /* If artifical depths are different, the frames must be different. */
588 eq = 0;
589 else
590 /* Frames are equal. */
591 eq = 1;
592
593 if (frame_debug)
594 {
595 fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
596 fprint_frame_id (gdb_stdlog, l);
597 fprintf_unfiltered (gdb_stdlog, ",r=");
598 fprint_frame_id (gdb_stdlog, r);
599 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
600 }
601 return eq;
602 }
603
604 /* Safety net to check whether frame ID L should be inner to
605 frame ID R, according to their stack addresses.
606
607 This method cannot be used to compare arbitrary frames, as the
608 ranges of valid stack addresses may be discontiguous (e.g. due
609 to sigaltstack).
610
611 However, it can be used as safety net to discover invalid frame
612 IDs in certain circumstances. Assuming that NEXT is the immediate
613 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
614
615 * The stack address of NEXT must be inner-than-or-equal to the stack
616 address of THIS.
617
618 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
619 error has occurred.
620
621 * If NEXT and THIS have different stack addresses, no other frame
622 in the frame chain may have a stack address in between.
623
624 Therefore, if frame_id_inner (TEST, THIS) holds, but
625 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
626 to a valid frame in the frame chain.
627
628 The sanity checks above cannot be performed when a SIGTRAMP frame
629 is involved, because signal handlers might be executed on a different
630 stack than the stack used by the routine that caused the signal
631 to be raised. This can happen for instance when a thread exceeds
632 its maximum stack size. In this case, certain compilers implement
633 a stack overflow strategy that cause the handler to be run on a
634 different stack. */
635
636 static int
637 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
638 {
639 int inner;
640
641 if (!l.stack_addr_p || !r.stack_addr_p)
642 /* Like NaN, any operation involving an invalid ID always fails. */
643 inner = 0;
644 else if (l.artificial_depth > r.artificial_depth
645 && l.stack_addr == r.stack_addr
646 && l.code_addr_p == r.code_addr_p
647 && l.special_addr_p == r.special_addr_p
648 && l.special_addr == r.special_addr)
649 {
650 /* Same function, different inlined functions. */
651 struct block *lb, *rb;
652
653 gdb_assert (l.code_addr_p && r.code_addr_p);
654
655 lb = block_for_pc (l.code_addr);
656 rb = block_for_pc (r.code_addr);
657
658 if (lb == NULL || rb == NULL)
659 /* Something's gone wrong. */
660 inner = 0;
661 else
662 /* This will return true if LB and RB are the same block, or
663 if the block with the smaller depth lexically encloses the
664 block with the greater depth. */
665 inner = contained_in (lb, rb);
666 }
667 else
668 /* Only return non-zero when strictly inner than. Note that, per
669 comment in "frame.h", there is some fuzz here. Frameless
670 functions are not strictly inner than (same .stack but
671 different .code and/or .special address). */
672 inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
673 if (frame_debug)
674 {
675 fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
676 fprint_frame_id (gdb_stdlog, l);
677 fprintf_unfiltered (gdb_stdlog, ",r=");
678 fprint_frame_id (gdb_stdlog, r);
679 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
680 }
681 return inner;
682 }
683
684 struct frame_info *
685 frame_find_by_id (struct frame_id id)
686 {
687 struct frame_info *frame, *prev_frame;
688
689 /* ZERO denotes the null frame, let the caller decide what to do
690 about it. Should it instead return get_current_frame()? */
691 if (!frame_id_p (id))
692 return NULL;
693
694 /* Try using the frame stash first. Finding it there removes the need
695 to perform the search by looping over all frames, which can be very
696 CPU-intensive if the number of frames is very high (the loop is O(n)
697 and get_prev_frame performs a series of checks that are relatively
698 expensive). This optimization is particularly useful when this function
699 is called from another function (such as value_fetch_lazy, case
700 VALUE_LVAL (val) == lval_register) which already loops over all frames,
701 making the overall behavior O(n^2). */
702 frame = frame_stash_find (id);
703 if (frame)
704 return frame;
705
706 for (frame = get_current_frame (); ; frame = prev_frame)
707 {
708 struct frame_id this = get_frame_id (frame);
709
710 if (frame_id_eq (id, this))
711 /* An exact match. */
712 return frame;
713
714 prev_frame = get_prev_frame (frame);
715 if (!prev_frame)
716 return NULL;
717
718 /* As a safety net to avoid unnecessary backtracing while trying
719 to find an invalid ID, we check for a common situation where
720 we can detect from comparing stack addresses that no other
721 frame in the current frame chain can have this ID. See the
722 comment at frame_id_inner for details. */
723 if (get_frame_type (frame) == NORMAL_FRAME
724 && !frame_id_inner (get_frame_arch (frame), id, this)
725 && frame_id_inner (get_frame_arch (prev_frame), id,
726 get_frame_id (prev_frame)))
727 return NULL;
728 }
729 return NULL;
730 }
731
732 static CORE_ADDR
733 frame_unwind_pc (struct frame_info *this_frame)
734 {
735 if (this_frame->prev_pc.status == CC_UNKNOWN)
736 {
737 if (gdbarch_unwind_pc_p (frame_unwind_arch (this_frame)))
738 {
739 volatile struct gdb_exception ex;
740 struct gdbarch *prev_gdbarch;
741 CORE_ADDR pc = 0;
742
743 /* The right way. The `pure' way. The one true way. This
744 method depends solely on the register-unwind code to
745 determine the value of registers in THIS frame, and hence
746 the value of this frame's PC (resume address). A typical
747 implementation is no more than:
748
749 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
750 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
751
752 Note: this method is very heavily dependent on a correct
753 register-unwind implementation, it pays to fix that
754 method first; this method is frame type agnostic, since
755 it only deals with register values, it works with any
756 frame. This is all in stark contrast to the old
757 FRAME_SAVED_PC which would try to directly handle all the
758 different ways that a PC could be unwound. */
759 prev_gdbarch = frame_unwind_arch (this_frame);
760
761 TRY_CATCH (ex, RETURN_MASK_ERROR)
762 {
763 pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
764 }
765 if (ex.reason < 0)
766 {
767 if (ex.error == NOT_AVAILABLE_ERROR)
768 {
769 this_frame->prev_pc.status = CC_UNAVAILABLE;
770
771 if (frame_debug)
772 fprintf_unfiltered (gdb_stdlog,
773 "{ frame_unwind_pc (this_frame=%d)"
774 " -> <unavailable> }\n",
775 this_frame->level);
776 }
777 else if (ex.error == OPTIMIZED_OUT_ERROR)
778 {
779 this_frame->prev_pc.status = CC_NOT_SAVED;
780
781 if (frame_debug)
782 fprintf_unfiltered (gdb_stdlog,
783 "{ frame_unwind_pc (this_frame=%d)"
784 " -> <not saved> }\n",
785 this_frame->level);
786 }
787 else
788 throw_exception (ex);
789 }
790 else
791 {
792 this_frame->prev_pc.value = pc;
793 this_frame->prev_pc.status = CC_VALUE;
794 if (frame_debug)
795 fprintf_unfiltered (gdb_stdlog,
796 "{ frame_unwind_pc (this_frame=%d) "
797 "-> %s }\n",
798 this_frame->level,
799 hex_string (this_frame->prev_pc.value));
800 }
801 }
802 else
803 internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
804 }
805
806 if (this_frame->prev_pc.status == CC_VALUE)
807 return this_frame->prev_pc.value;
808 else if (this_frame->prev_pc.status == CC_UNAVAILABLE)
809 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
810 else if (this_frame->prev_pc.status == CC_NOT_SAVED)
811 throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved"));
812 else
813 internal_error (__FILE__, __LINE__,
814 "unexpected prev_pc status: %d",
815 (int) this_frame->prev_pc.status);
816 }
817
818 CORE_ADDR
819 frame_unwind_caller_pc (struct frame_info *this_frame)
820 {
821 return frame_unwind_pc (skip_artificial_frames (this_frame));
822 }
823
824 int
825 get_frame_func_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
826 {
827 struct frame_info *next_frame = this_frame->next;
828
829 if (!next_frame->prev_func.p)
830 {
831 CORE_ADDR addr_in_block;
832
833 /* Make certain that this, and not the adjacent, function is
834 found. */
835 if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
836 {
837 next_frame->prev_func.p = -1;
838 if (frame_debug)
839 fprintf_unfiltered (gdb_stdlog,
840 "{ get_frame_func (this_frame=%d)"
841 " -> unavailable }\n",
842 this_frame->level);
843 }
844 else
845 {
846 next_frame->prev_func.p = 1;
847 next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
848 if (frame_debug)
849 fprintf_unfiltered (gdb_stdlog,
850 "{ get_frame_func (this_frame=%d) -> %s }\n",
851 this_frame->level,
852 hex_string (next_frame->prev_func.addr));
853 }
854 }
855
856 if (next_frame->prev_func.p < 0)
857 {
858 *pc = -1;
859 return 0;
860 }
861 else
862 {
863 *pc = next_frame->prev_func.addr;
864 return 1;
865 }
866 }
867
868 CORE_ADDR
869 get_frame_func (struct frame_info *this_frame)
870 {
871 CORE_ADDR pc;
872
873 if (!get_frame_func_if_available (this_frame, &pc))
874 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
875
876 return pc;
877 }
878
879 static enum register_status
880 do_frame_register_read (void *src, int regnum, gdb_byte *buf)
881 {
882 if (!deprecated_frame_register_read (src, regnum, buf))
883 return REG_UNAVAILABLE;
884 else
885 return REG_VALID;
886 }
887
888 struct regcache *
889 frame_save_as_regcache (struct frame_info *this_frame)
890 {
891 struct address_space *aspace = get_frame_address_space (this_frame);
892 struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame),
893 aspace);
894 struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
895
896 regcache_save (regcache, do_frame_register_read, this_frame);
897 discard_cleanups (cleanups);
898 return regcache;
899 }
900
901 void
902 frame_pop (struct frame_info *this_frame)
903 {
904 struct frame_info *prev_frame;
905 struct regcache *scratch;
906 struct cleanup *cleanups;
907
908 if (get_frame_type (this_frame) == DUMMY_FRAME)
909 {
910 /* Popping a dummy frame involves restoring more than just registers.
911 dummy_frame_pop does all the work. */
912 dummy_frame_pop (get_frame_id (this_frame));
913 return;
914 }
915
916 /* Ensure that we have a frame to pop to. */
917 prev_frame = get_prev_frame_1 (this_frame);
918
919 if (!prev_frame)
920 error (_("Cannot pop the initial frame."));
921
922 /* Ignore TAILCALL_FRAME type frames, they were executed already before
923 entering THISFRAME. */
924 while (get_frame_type (prev_frame) == TAILCALL_FRAME)
925 prev_frame = get_prev_frame (prev_frame);
926
927 /* Make a copy of all the register values unwound from this frame.
928 Save them in a scratch buffer so that there isn't a race between
929 trying to extract the old values from the current regcache while
930 at the same time writing new values into that same cache. */
931 scratch = frame_save_as_regcache (prev_frame);
932 cleanups = make_cleanup_regcache_xfree (scratch);
933
934 /* FIXME: cagney/2003-03-16: It should be possible to tell the
935 target's register cache that it is about to be hit with a burst
936 register transfer and that the sequence of register writes should
937 be batched. The pair target_prepare_to_store() and
938 target_store_registers() kind of suggest this functionality.
939 Unfortunately, they don't implement it. Their lack of a formal
940 definition can lead to targets writing back bogus values
941 (arguably a bug in the target code mind). */
942 /* Now copy those saved registers into the current regcache.
943 Here, regcache_cpy() calls regcache_restore(). */
944 regcache_cpy (get_current_regcache (), scratch);
945 do_cleanups (cleanups);
946
947 /* We've made right mess of GDB's local state, just discard
948 everything. */
949 reinit_frame_cache ();
950 }
951
952 void
953 frame_register_unwind (struct frame_info *frame, int regnum,
954 int *optimizedp, int *unavailablep,
955 enum lval_type *lvalp, CORE_ADDR *addrp,
956 int *realnump, gdb_byte *bufferp)
957 {
958 struct value *value;
959
960 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
961 that the value proper does not need to be fetched. */
962 gdb_assert (optimizedp != NULL);
963 gdb_assert (lvalp != NULL);
964 gdb_assert (addrp != NULL);
965 gdb_assert (realnump != NULL);
966 /* gdb_assert (bufferp != NULL); */
967
968 value = frame_unwind_register_value (frame, regnum);
969
970 gdb_assert (value != NULL);
971
972 *optimizedp = value_optimized_out (value);
973 *unavailablep = !value_entirely_available (value);
974 *lvalp = VALUE_LVAL (value);
975 *addrp = value_address (value);
976 *realnump = VALUE_REGNUM (value);
977
978 if (bufferp)
979 {
980 if (!*optimizedp && !*unavailablep)
981 memcpy (bufferp, value_contents_all (value),
982 TYPE_LENGTH (value_type (value)));
983 else
984 memset (bufferp, 0, TYPE_LENGTH (value_type (value)));
985 }
986
987 /* Dispose of the new value. This prevents watchpoints from
988 trying to watch the saved frame pointer. */
989 release_value (value);
990 value_free (value);
991 }
992
993 void
994 frame_register (struct frame_info *frame, int regnum,
995 int *optimizedp, int *unavailablep, enum lval_type *lvalp,
996 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
997 {
998 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
999 that the value proper does not need to be fetched. */
1000 gdb_assert (optimizedp != NULL);
1001 gdb_assert (lvalp != NULL);
1002 gdb_assert (addrp != NULL);
1003 gdb_assert (realnump != NULL);
1004 /* gdb_assert (bufferp != NULL); */
1005
1006 /* Obtain the register value by unwinding the register from the next
1007 (more inner frame). */
1008 gdb_assert (frame != NULL && frame->next != NULL);
1009 frame_register_unwind (frame->next, regnum, optimizedp, unavailablep,
1010 lvalp, addrp, realnump, bufferp);
1011 }
1012
1013 void
1014 frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
1015 {
1016 int optimized;
1017 int unavailable;
1018 CORE_ADDR addr;
1019 int realnum;
1020 enum lval_type lval;
1021
1022 frame_register_unwind (frame, regnum, &optimized, &unavailable,
1023 &lval, &addr, &realnum, buf);
1024
1025 if (optimized)
1026 throw_error (OPTIMIZED_OUT_ERROR,
1027 _("Register %d was not saved"), regnum);
1028 if (unavailable)
1029 throw_error (NOT_AVAILABLE_ERROR,
1030 _("Register %d is not available"), regnum);
1031 }
1032
1033 void
1034 get_frame_register (struct frame_info *frame,
1035 int regnum, gdb_byte *buf)
1036 {
1037 frame_unwind_register (frame->next, regnum, buf);
1038 }
1039
1040 struct value *
1041 frame_unwind_register_value (struct frame_info *frame, int regnum)
1042 {
1043 struct gdbarch *gdbarch;
1044 struct value *value;
1045
1046 gdb_assert (frame != NULL);
1047 gdbarch = frame_unwind_arch (frame);
1048
1049 if (frame_debug)
1050 {
1051 fprintf_unfiltered (gdb_stdlog,
1052 "{ frame_unwind_register_value "
1053 "(frame=%d,regnum=%d(%s),...) ",
1054 frame->level, regnum,
1055 user_reg_map_regnum_to_name (gdbarch, regnum));
1056 }
1057
1058 /* Find the unwinder. */
1059 if (frame->unwind == NULL)
1060 frame_unwind_find_by_frame (frame, &frame->prologue_cache);
1061
1062 /* Ask this frame to unwind its register. */
1063 value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);
1064
1065 if (frame_debug)
1066 {
1067 fprintf_unfiltered (gdb_stdlog, "->");
1068 if (value_optimized_out (value))
1069 {
1070 fprintf_unfiltered (gdb_stdlog, " ");
1071 val_print_optimized_out (value, gdb_stdlog);
1072 }
1073 else
1074 {
1075 if (VALUE_LVAL (value) == lval_register)
1076 fprintf_unfiltered (gdb_stdlog, " register=%d",
1077 VALUE_REGNUM (value));
1078 else if (VALUE_LVAL (value) == lval_memory)
1079 fprintf_unfiltered (gdb_stdlog, " address=%s",
1080 paddress (gdbarch,
1081 value_address (value)));
1082 else
1083 fprintf_unfiltered (gdb_stdlog, " computed");
1084
1085 if (value_lazy (value))
1086 fprintf_unfiltered (gdb_stdlog, " lazy");
1087 else
1088 {
1089 int i;
1090 const gdb_byte *buf = value_contents (value);
1091
1092 fprintf_unfiltered (gdb_stdlog, " bytes=");
1093 fprintf_unfiltered (gdb_stdlog, "[");
1094 for (i = 0; i < register_size (gdbarch, regnum); i++)
1095 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1096 fprintf_unfiltered (gdb_stdlog, "]");
1097 }
1098 }
1099
1100 fprintf_unfiltered (gdb_stdlog, " }\n");
1101 }
1102
1103 return value;
1104 }
1105
1106 struct value *
1107 get_frame_register_value (struct frame_info *frame, int regnum)
1108 {
1109 return frame_unwind_register_value (frame->next, regnum);
1110 }
1111
1112 LONGEST
1113 frame_unwind_register_signed (struct frame_info *frame, int regnum)
1114 {
1115 struct gdbarch *gdbarch = frame_unwind_arch (frame);
1116 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1117 int size = register_size (gdbarch, regnum);
1118 gdb_byte buf[MAX_REGISTER_SIZE];
1119
1120 frame_unwind_register (frame, regnum, buf);
1121 return extract_signed_integer (buf, size, byte_order);
1122 }
1123
1124 LONGEST
1125 get_frame_register_signed (struct frame_info *frame, int regnum)
1126 {
1127 return frame_unwind_register_signed (frame->next, regnum);
1128 }
1129
1130 ULONGEST
1131 frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
1132 {
1133 struct gdbarch *gdbarch = frame_unwind_arch (frame);
1134 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1135 int size = register_size (gdbarch, regnum);
1136 gdb_byte buf[MAX_REGISTER_SIZE];
1137
1138 frame_unwind_register (frame, regnum, buf);
1139 return extract_unsigned_integer (buf, size, byte_order);
1140 }
1141
1142 ULONGEST
1143 get_frame_register_unsigned (struct frame_info *frame, int regnum)
1144 {
1145 return frame_unwind_register_unsigned (frame->next, regnum);
1146 }
1147
1148 int
1149 read_frame_register_unsigned (struct frame_info *frame, int regnum,
1150 ULONGEST *val)
1151 {
1152 struct value *regval = get_frame_register_value (frame, regnum);
1153
1154 if (!value_optimized_out (regval)
1155 && value_entirely_available (regval))
1156 {
1157 struct gdbarch *gdbarch = get_frame_arch (frame);
1158 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1159 int size = register_size (gdbarch, VALUE_REGNUM (regval));
1160
1161 *val = extract_unsigned_integer (value_contents (regval), size, byte_order);
1162 return 1;
1163 }
1164
1165 return 0;
1166 }
1167
1168 void
1169 put_frame_register (struct frame_info *frame, int regnum,
1170 const gdb_byte *buf)
1171 {
1172 struct gdbarch *gdbarch = get_frame_arch (frame);
1173 int realnum;
1174 int optim;
1175 int unavail;
1176 enum lval_type lval;
1177 CORE_ADDR addr;
1178
1179 frame_register (frame, regnum, &optim, &unavail,
1180 &lval, &addr, &realnum, NULL);
1181 if (optim)
1182 error (_("Attempt to assign to a register that was not saved."));
1183 switch (lval)
1184 {
1185 case lval_memory:
1186 {
1187 write_memory (addr, buf, register_size (gdbarch, regnum));
1188 break;
1189 }
1190 case lval_register:
1191 regcache_cooked_write (get_current_regcache (), realnum, buf);
1192 break;
1193 default:
1194 error (_("Attempt to assign to an unmodifiable value."));
1195 }
1196 }
1197
1198 /* This function is deprecated. Use get_frame_register_value instead,
1199 which provides more accurate information.
1200
1201 Find and return the value of REGNUM for the specified stack frame.
1202 The number of bytes copied is REGISTER_SIZE (REGNUM).
1203
1204 Returns 0 if the register value could not be found. */
1205
1206 int
1207 deprecated_frame_register_read (struct frame_info *frame, int regnum,
1208 gdb_byte *myaddr)
1209 {
1210 int optimized;
1211 int unavailable;
1212 enum lval_type lval;
1213 CORE_ADDR addr;
1214 int realnum;
1215
1216 frame_register (frame, regnum, &optimized, &unavailable,
1217 &lval, &addr, &realnum, myaddr);
1218
1219 return !optimized && !unavailable;
1220 }
1221
1222 int
1223 get_frame_register_bytes (struct frame_info *frame, int regnum,
1224 CORE_ADDR offset, int len, gdb_byte *myaddr,
1225 int *optimizedp, int *unavailablep)
1226 {
1227 struct gdbarch *gdbarch = get_frame_arch (frame);
1228 int i;
1229 int maxsize;
1230 int numregs;
1231
1232 /* Skip registers wholly inside of OFFSET. */
1233 while (offset >= register_size (gdbarch, regnum))
1234 {
1235 offset -= register_size (gdbarch, regnum);
1236 regnum++;
1237 }
1238
1239 /* Ensure that we will not read beyond the end of the register file.
1240 This can only ever happen if the debug information is bad. */
1241 maxsize = -offset;
1242 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1243 for (i = regnum; i < numregs; i++)
1244 {
1245 int thissize = register_size (gdbarch, i);
1246
1247 if (thissize == 0)
1248 break; /* This register is not available on this architecture. */
1249 maxsize += thissize;
1250 }
1251 if (len > maxsize)
1252 error (_("Bad debug information detected: "
1253 "Attempt to read %d bytes from registers."), len);
1254
1255 /* Copy the data. */
1256 while (len > 0)
1257 {
1258 int curr_len = register_size (gdbarch, regnum) - offset;
1259
1260 if (curr_len > len)
1261 curr_len = len;
1262
1263 if (curr_len == register_size (gdbarch, regnum))
1264 {
1265 enum lval_type lval;
1266 CORE_ADDR addr;
1267 int realnum;
1268
1269 frame_register (frame, regnum, optimizedp, unavailablep,
1270 &lval, &addr, &realnum, myaddr);
1271 if (*optimizedp || *unavailablep)
1272 return 0;
1273 }
1274 else
1275 {
1276 gdb_byte buf[MAX_REGISTER_SIZE];
1277 enum lval_type lval;
1278 CORE_ADDR addr;
1279 int realnum;
1280
1281 frame_register (frame, regnum, optimizedp, unavailablep,
1282 &lval, &addr, &realnum, buf);
1283 if (*optimizedp || *unavailablep)
1284 return 0;
1285 memcpy (myaddr, buf + offset, curr_len);
1286 }
1287
1288 myaddr += curr_len;
1289 len -= curr_len;
1290 offset = 0;
1291 regnum++;
1292 }
1293
1294 *optimizedp = 0;
1295 *unavailablep = 0;
1296 return 1;
1297 }
1298
1299 void
1300 put_frame_register_bytes (struct frame_info *frame, int regnum,
1301 CORE_ADDR offset, int len, const gdb_byte *myaddr)
1302 {
1303 struct gdbarch *gdbarch = get_frame_arch (frame);
1304
1305 /* Skip registers wholly inside of OFFSET. */
1306 while (offset >= register_size (gdbarch, regnum))
1307 {
1308 offset -= register_size (gdbarch, regnum);
1309 regnum++;
1310 }
1311
1312 /* Copy the data. */
1313 while (len > 0)
1314 {
1315 int curr_len = register_size (gdbarch, regnum) - offset;
1316
1317 if (curr_len > len)
1318 curr_len = len;
1319
1320 if (curr_len == register_size (gdbarch, regnum))
1321 {
1322 put_frame_register (frame, regnum, myaddr);
1323 }
1324 else
1325 {
1326 gdb_byte buf[MAX_REGISTER_SIZE];
1327
1328 deprecated_frame_register_read (frame, regnum, buf);
1329 memcpy (buf + offset, myaddr, curr_len);
1330 put_frame_register (frame, regnum, buf);
1331 }
1332
1333 myaddr += curr_len;
1334 len -= curr_len;
1335 offset = 0;
1336 regnum++;
1337 }
1338 }
1339
1340 /* Create a sentinel frame. */
1341
1342 static struct frame_info *
1343 create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
1344 {
1345 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1346
1347 frame->level = -1;
1348 frame->pspace = pspace;
1349 frame->aspace = get_regcache_aspace (regcache);
1350 /* Explicitly initialize the sentinel frame's cache. Provide it
1351 with the underlying regcache. In the future additional
1352 information, such as the frame's thread will be added. */
1353 frame->prologue_cache = sentinel_frame_cache (regcache);
1354 /* For the moment there is only one sentinel frame implementation. */
1355 frame->unwind = &sentinel_frame_unwind;
1356 /* Link this frame back to itself. The frame is self referential
1357 (the unwound PC is the same as the pc), so make it so. */
1358 frame->next = frame;
1359 /* Make the sentinel frame's ID valid, but invalid. That way all
1360 comparisons with it should fail. */
1361 frame->this_id.p = 1;
1362 frame->this_id.value = null_frame_id;
1363 if (frame_debug)
1364 {
1365 fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
1366 fprint_frame (gdb_stdlog, frame);
1367 fprintf_unfiltered (gdb_stdlog, " }\n");
1368 }
1369 return frame;
1370 }
1371
1372 /* Info about the innermost stack frame (contents of FP register). */
1373
1374 static struct frame_info *current_frame;
1375
1376 /* Cache for frame addresses already read by gdb. Valid only while
1377 inferior is stopped. Control variables for the frame cache should
1378 be local to this module. */
1379
1380 static struct obstack frame_cache_obstack;
1381
1382 void *
1383 frame_obstack_zalloc (unsigned long size)
1384 {
1385 void *data = obstack_alloc (&frame_cache_obstack, size);
1386
1387 memset (data, 0, size);
1388 return data;
1389 }
1390
1391 /* Return the innermost (currently executing) stack frame. This is
1392 split into two functions. The function unwind_to_current_frame()
1393 is wrapped in catch exceptions so that, even when the unwind of the
1394 sentinel frame fails, the function still returns a stack frame. */
1395
1396 static int
1397 unwind_to_current_frame (struct ui_out *ui_out, void *args)
1398 {
1399 struct frame_info *frame = get_prev_frame (args);
1400
1401 /* A sentinel frame can fail to unwind, e.g., because its PC value
1402 lands in somewhere like start. */
1403 if (frame == NULL)
1404 return 1;
1405 current_frame = frame;
1406 return 0;
1407 }
1408
1409 struct frame_info *
1410 get_current_frame (void)
1411 {
1412 /* First check, and report, the lack of registers. Having GDB
1413 report "No stack!" or "No memory" when the target doesn't even
1414 have registers is very confusing. Besides, "printcmd.exp"
1415 explicitly checks that ``print $pc'' with no registers prints "No
1416 registers". */
1417 if (!target_has_registers)
1418 error (_("No registers."));
1419 if (!target_has_stack)
1420 error (_("No stack."));
1421 if (!target_has_memory)
1422 error (_("No memory."));
1423 /* Traceframes are effectively a substitute for the live inferior. */
1424 if (get_traceframe_number () < 0)
1425 {
1426 if (ptid_equal (inferior_ptid, null_ptid))
1427 error (_("No selected thread."));
1428 if (is_exited (inferior_ptid))
1429 error (_("Invalid selected thread."));
1430 if (is_executing (inferior_ptid))
1431 error (_("Target is executing."));
1432 }
1433
1434 if (current_frame == NULL)
1435 {
1436 struct frame_info *sentinel_frame =
1437 create_sentinel_frame (current_program_space, get_current_regcache ());
1438 if (catch_exceptions (current_uiout, unwind_to_current_frame,
1439 sentinel_frame, RETURN_MASK_ERROR) != 0)
1440 {
1441 /* Oops! Fake a current frame? Is this useful? It has a PC
1442 of zero, for instance. */
1443 current_frame = sentinel_frame;
1444 }
1445 }
1446 return current_frame;
1447 }
1448
1449 /* The "selected" stack frame is used by default for local and arg
1450 access. May be zero, for no selected frame. */
1451
1452 static struct frame_info *selected_frame;
1453
1454 int
1455 has_stack_frames (void)
1456 {
1457 if (!target_has_registers || !target_has_stack || !target_has_memory)
1458 return 0;
1459
1460 /* Traceframes are effectively a substitute for the live inferior. */
1461 if (get_traceframe_number () < 0)
1462 {
1463 /* No current inferior, no frame. */
1464 if (ptid_equal (inferior_ptid, null_ptid))
1465 return 0;
1466
1467 /* Don't try to read from a dead thread. */
1468 if (is_exited (inferior_ptid))
1469 return 0;
1470
1471 /* ... or from a spinning thread. */
1472 if (is_executing (inferior_ptid))
1473 return 0;
1474 }
1475
1476 return 1;
1477 }
1478
1479 /* Return the selected frame. Always non-NULL (unless there isn't an
1480 inferior sufficient for creating a frame) in which case an error is
1481 thrown. */
1482
1483 struct frame_info *
1484 get_selected_frame (const char *message)
1485 {
1486 if (selected_frame == NULL)
1487 {
1488 if (message != NULL && !has_stack_frames ())
1489 error (("%s"), message);
1490 /* Hey! Don't trust this. It should really be re-finding the
1491 last selected frame of the currently selected thread. This,
1492 though, is better than nothing. */
1493 select_frame (get_current_frame ());
1494 }
1495 /* There is always a frame. */
1496 gdb_assert (selected_frame != NULL);
1497 return selected_frame;
1498 }
1499
1500 /* If there is a selected frame, return it. Otherwise, return NULL. */
1501
1502 struct frame_info *
1503 get_selected_frame_if_set (void)
1504 {
1505 return selected_frame;
1506 }
1507
1508 /* This is a variant of get_selected_frame() which can be called when
1509 the inferior does not have a frame; in that case it will return
1510 NULL instead of calling error(). */
1511
1512 struct frame_info *
1513 deprecated_safe_get_selected_frame (void)
1514 {
1515 if (!has_stack_frames ())
1516 return NULL;
1517 return get_selected_frame (NULL);
1518 }
1519
1520 /* Select frame FI (or NULL - to invalidate the current frame). */
1521
1522 void
1523 select_frame (struct frame_info *fi)
1524 {
1525 selected_frame = fi;
1526 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1527 frame is being invalidated. */
1528 if (deprecated_selected_frame_level_changed_hook)
1529 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
1530
1531 /* FIXME: kseitz/2002-08-28: It would be nice to call
1532 selected_frame_level_changed_event() right here, but due to limitations
1533 in the current interfaces, we would end up flooding UIs with events
1534 because select_frame() is used extensively internally.
1535
1536 Once we have frame-parameterized frame (and frame-related) commands,
1537 the event notification can be moved here, since this function will only
1538 be called when the user's selected frame is being changed. */
1539
1540 /* Ensure that symbols for this frame are read in. Also, determine the
1541 source language of this frame, and switch to it if desired. */
1542 if (fi)
1543 {
1544 CORE_ADDR pc;
1545
1546 /* We retrieve the frame's symtab by using the frame PC.
1547 However we cannot use the frame PC as-is, because it usually
1548 points to the instruction following the "call", which is
1549 sometimes the first instruction of another function. So we
1550 rely on get_frame_address_in_block() which provides us with a
1551 PC which is guaranteed to be inside the frame's code
1552 block. */
1553 if (get_frame_address_in_block_if_available (fi, &pc))
1554 {
1555 struct symtab *s = find_pc_symtab (pc);
1556
1557 if (s
1558 && s->language != current_language->la_language
1559 && s->language != language_unknown
1560 && language_mode == language_mode_auto)
1561 set_language (s->language);
1562 }
1563 }
1564 }
1565
1566 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1567 Always returns a non-NULL value. */
1568
1569 struct frame_info *
1570 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1571 {
1572 struct frame_info *fi;
1573
1574 if (frame_debug)
1575 {
1576 fprintf_unfiltered (gdb_stdlog,
1577 "{ create_new_frame (addr=%s, pc=%s) ",
1578 hex_string (addr), hex_string (pc));
1579 }
1580
1581 fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
1582
1583 fi->next = create_sentinel_frame (current_program_space,
1584 get_current_regcache ());
1585
1586 /* Set/update this frame's cached PC value, found in the next frame.
1587 Do this before looking for this frame's unwinder. A sniffer is
1588 very likely to read this, and the corresponding unwinder is
1589 entitled to rely that the PC doesn't magically change. */
1590 fi->next->prev_pc.value = pc;
1591 fi->next->prev_pc.status = CC_VALUE;
1592
1593 /* We currently assume that frame chain's can't cross spaces. */
1594 fi->pspace = fi->next->pspace;
1595 fi->aspace = fi->next->aspace;
1596
1597 /* Select/initialize both the unwind function and the frame's type
1598 based on the PC. */
1599 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1600
1601 fi->this_id.p = 1;
1602 fi->this_id.value = frame_id_build (addr, pc);
1603
1604 if (frame_debug)
1605 {
1606 fprintf_unfiltered (gdb_stdlog, "-> ");
1607 fprint_frame (gdb_stdlog, fi);
1608 fprintf_unfiltered (gdb_stdlog, " }\n");
1609 }
1610
1611 return fi;
1612 }
1613
1614 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1615 innermost frame). Be careful to not fall off the bottom of the
1616 frame chain and onto the sentinel frame. */
1617
1618 struct frame_info *
1619 get_next_frame (struct frame_info *this_frame)
1620 {
1621 if (this_frame->level > 0)
1622 return this_frame->next;
1623 else
1624 return NULL;
1625 }
1626
1627 /* Observer for the target_changed event. */
1628
1629 static void
1630 frame_observer_target_changed (struct target_ops *target)
1631 {
1632 reinit_frame_cache ();
1633 }
1634
1635 /* Flush the entire frame cache. */
1636
1637 void
1638 reinit_frame_cache (void)
1639 {
1640 struct frame_info *fi;
1641
1642 /* Tear down all frame caches. */
1643 for (fi = current_frame; fi != NULL; fi = fi->prev)
1644 {
1645 if (fi->prologue_cache && fi->unwind->dealloc_cache)
1646 fi->unwind->dealloc_cache (fi, fi->prologue_cache);
1647 if (fi->base_cache && fi->base->unwind->dealloc_cache)
1648 fi->base->unwind->dealloc_cache (fi, fi->base_cache);
1649 }
1650
1651 /* Since we can't really be sure what the first object allocated was. */
1652 obstack_free (&frame_cache_obstack, 0);
1653 obstack_init (&frame_cache_obstack);
1654
1655 if (current_frame != NULL)
1656 annotate_frames_invalid ();
1657
1658 current_frame = NULL; /* Invalidate cache */
1659 select_frame (NULL);
1660 frame_stash_invalidate ();
1661 if (frame_debug)
1662 fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
1663 }
1664
1665 /* Find where a register is saved (in memory or another register).
1666 The result of frame_register_unwind is just where it is saved
1667 relative to this particular frame. */
1668
1669 static void
1670 frame_register_unwind_location (struct frame_info *this_frame, int regnum,
1671 int *optimizedp, enum lval_type *lvalp,
1672 CORE_ADDR *addrp, int *realnump)
1673 {
1674 gdb_assert (this_frame == NULL || this_frame->level >= 0);
1675
1676 while (this_frame != NULL)
1677 {
1678 int unavailable;
1679
1680 frame_register_unwind (this_frame, regnum, optimizedp, &unavailable,
1681 lvalp, addrp, realnump, NULL);
1682
1683 if (*optimizedp)
1684 break;
1685
1686 if (*lvalp != lval_register)
1687 break;
1688
1689 regnum = *realnump;
1690 this_frame = get_next_frame (this_frame);
1691 }
1692 }
1693
1694 /* Get the previous raw frame, and check that it is not identical to
1695 same other frame frame already in the chain. If it is, there is
1696 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1697 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1698 validity tests, that compare THIS_FRAME and the next frame, we do
1699 this right after creating the previous frame, to avoid ever ending
1700 up with two frames with the same id in the frame chain. */
1701
1702 static struct frame_info *
1703 get_prev_frame_if_no_cycle (struct frame_info *this_frame)
1704 {
1705 struct frame_info *prev_frame;
1706
1707 prev_frame = get_prev_frame_raw (this_frame);
1708 if (prev_frame == NULL)
1709 return NULL;
1710
1711 compute_frame_id (prev_frame);
1712 if (frame_stash_add (prev_frame))
1713 return prev_frame;
1714
1715 /* Another frame with the same id was already in the stash. We just
1716 detected a cycle. */
1717 if (frame_debug)
1718 {
1719 fprintf_unfiltered (gdb_stdlog, "-> ");
1720 fprint_frame (gdb_stdlog, NULL);
1721 fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
1722 }
1723 this_frame->stop_reason = UNWIND_SAME_ID;
1724 /* Unlink. */
1725 prev_frame->next = NULL;
1726 this_frame->prev = NULL;
1727 return NULL;
1728 }
1729
1730 /* Return a "struct frame_info" corresponding to the frame that called
1731 THIS_FRAME. Returns NULL if there is no such frame.
1732
1733 Unlike get_prev_frame, this function always tries to unwind the
1734 frame. */
1735
1736 static struct frame_info *
1737 get_prev_frame_1 (struct frame_info *this_frame)
1738 {
1739 struct gdbarch *gdbarch;
1740
1741 gdb_assert (this_frame != NULL);
1742 gdbarch = get_frame_arch (this_frame);
1743
1744 if (frame_debug)
1745 {
1746 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
1747 if (this_frame != NULL)
1748 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1749 else
1750 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1751 fprintf_unfiltered (gdb_stdlog, ") ");
1752 }
1753
1754 /* Only try to do the unwind once. */
1755 if (this_frame->prev_p)
1756 {
1757 if (frame_debug)
1758 {
1759 fprintf_unfiltered (gdb_stdlog, "-> ");
1760 fprint_frame (gdb_stdlog, this_frame->prev);
1761 fprintf_unfiltered (gdb_stdlog, " // cached \n");
1762 }
1763 return this_frame->prev;
1764 }
1765
1766 /* If the frame unwinder hasn't been selected yet, we must do so
1767 before setting prev_p; otherwise the check for misbehaved
1768 sniffers will think that this frame's sniffer tried to unwind
1769 further (see frame_cleanup_after_sniffer). */
1770 if (this_frame->unwind == NULL)
1771 frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
1772
1773 this_frame->prev_p = 1;
1774 this_frame->stop_reason = UNWIND_NO_REASON;
1775
1776 /* If we are unwinding from an inline frame, all of the below tests
1777 were already performed when we unwound from the next non-inline
1778 frame. We must skip them, since we can not get THIS_FRAME's ID
1779 until we have unwound all the way down to the previous non-inline
1780 frame. */
1781 if (get_frame_type (this_frame) == INLINE_FRAME)
1782 return get_prev_frame_if_no_cycle (this_frame);
1783
1784 /* Check that this frame is unwindable. If it isn't, don't try to
1785 unwind to the prev frame. */
1786 this_frame->stop_reason
1787 = this_frame->unwind->stop_reason (this_frame,
1788 &this_frame->prologue_cache);
1789
1790 if (this_frame->stop_reason != UNWIND_NO_REASON)
1791 {
1792 if (frame_debug)
1793 {
1794 enum unwind_stop_reason reason = this_frame->stop_reason;
1795
1796 fprintf_unfiltered (gdb_stdlog, "-> ");
1797 fprint_frame (gdb_stdlog, NULL);
1798 fprintf_unfiltered (gdb_stdlog, " // %s }\n",
1799 frame_stop_reason_symbol_string (reason));
1800 }
1801 return NULL;
1802 }
1803
1804 /* Check that this frame's ID isn't inner to (younger, below, next)
1805 the next frame. This happens when a frame unwind goes backwards.
1806 This check is valid only if this frame and the next frame are NORMAL.
1807 See the comment at frame_id_inner for details. */
1808 if (get_frame_type (this_frame) == NORMAL_FRAME
1809 && this_frame->next->unwind->type == NORMAL_FRAME
1810 && frame_id_inner (get_frame_arch (this_frame->next),
1811 get_frame_id (this_frame),
1812 get_frame_id (this_frame->next)))
1813 {
1814 CORE_ADDR this_pc_in_block;
1815 struct minimal_symbol *morestack_msym;
1816 const char *morestack_name = NULL;
1817
1818 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
1819 this_pc_in_block = get_frame_address_in_block (this_frame);
1820 morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym;
1821 if (morestack_msym)
1822 morestack_name = SYMBOL_LINKAGE_NAME (morestack_msym);
1823 if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
1824 {
1825 if (frame_debug)
1826 {
1827 fprintf_unfiltered (gdb_stdlog, "-> ");
1828 fprint_frame (gdb_stdlog, NULL);
1829 fprintf_unfiltered (gdb_stdlog,
1830 " // this frame ID is inner }\n");
1831 }
1832 this_frame->stop_reason = UNWIND_INNER_ID;
1833 return NULL;
1834 }
1835 }
1836
1837 /* Check that this and the next frame do not unwind the PC register
1838 to the same memory location. If they do, then even though they
1839 have different frame IDs, the new frame will be bogus; two
1840 functions can't share a register save slot for the PC. This can
1841 happen when the prologue analyzer finds a stack adjustment, but
1842 no PC save.
1843
1844 This check does assume that the "PC register" is roughly a
1845 traditional PC, even if the gdbarch_unwind_pc method adjusts
1846 it (we do not rely on the value, only on the unwound PC being
1847 dependent on this value). A potential improvement would be
1848 to have the frame prev_pc method and the gdbarch unwind_pc
1849 method set the same lval and location information as
1850 frame_register_unwind. */
1851 if (this_frame->level > 0
1852 && gdbarch_pc_regnum (gdbarch) >= 0
1853 && get_frame_type (this_frame) == NORMAL_FRAME
1854 && (get_frame_type (this_frame->next) == NORMAL_FRAME
1855 || get_frame_type (this_frame->next) == INLINE_FRAME))
1856 {
1857 int optimized, realnum, nrealnum;
1858 enum lval_type lval, nlval;
1859 CORE_ADDR addr, naddr;
1860
1861 frame_register_unwind_location (this_frame,
1862 gdbarch_pc_regnum (gdbarch),
1863 &optimized, &lval, &addr, &realnum);
1864 frame_register_unwind_location (get_next_frame (this_frame),
1865 gdbarch_pc_regnum (gdbarch),
1866 &optimized, &nlval, &naddr, &nrealnum);
1867
1868 if ((lval == lval_memory && lval == nlval && addr == naddr)
1869 || (lval == lval_register && lval == nlval && realnum == nrealnum))
1870 {
1871 if (frame_debug)
1872 {
1873 fprintf_unfiltered (gdb_stdlog, "-> ");
1874 fprint_frame (gdb_stdlog, NULL);
1875 fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
1876 }
1877
1878 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
1879 this_frame->prev = NULL;
1880 return NULL;
1881 }
1882 }
1883
1884 return get_prev_frame_if_no_cycle (this_frame);
1885 }
1886
1887 /* Construct a new "struct frame_info" and link it previous to
1888 this_frame. */
1889
1890 static struct frame_info *
1891 get_prev_frame_raw (struct frame_info *this_frame)
1892 {
1893 struct frame_info *prev_frame;
1894
1895 /* Allocate the new frame but do not wire it in to the frame chain.
1896 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1897 frame->next to pull some fancy tricks (of course such code is, by
1898 definition, recursive). Try to prevent it.
1899
1900 There is no reason to worry about memory leaks, should the
1901 remainder of the function fail. The allocated memory will be
1902 quickly reclaimed when the frame cache is flushed, and the `we've
1903 been here before' check above will stop repeated memory
1904 allocation calls. */
1905 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1906 prev_frame->level = this_frame->level + 1;
1907
1908 /* For now, assume we don't have frame chains crossing address
1909 spaces. */
1910 prev_frame->pspace = this_frame->pspace;
1911 prev_frame->aspace = this_frame->aspace;
1912
1913 /* Don't yet compute ->unwind (and hence ->type). It is computed
1914 on-demand in get_frame_type, frame_register_unwind, and
1915 get_frame_id. */
1916
1917 /* Don't yet compute the frame's ID. It is computed on-demand by
1918 get_frame_id(). */
1919
1920 /* The unwound frame ID is validate at the start of this function,
1921 as part of the logic to decide if that frame should be further
1922 unwound, and not here while the prev frame is being created.
1923 Doing this makes it possible for the user to examine a frame that
1924 has an invalid frame ID.
1925
1926 Some very old VAX code noted: [...] For the sake of argument,
1927 suppose that the stack is somewhat trashed (which is one reason
1928 that "info frame" exists). So, return 0 (indicating we don't
1929 know the address of the arglist) if we don't know what frame this
1930 frame calls. */
1931
1932 /* Link it in. */
1933 this_frame->prev = prev_frame;
1934 prev_frame->next = this_frame;
1935
1936 if (frame_debug)
1937 {
1938 fprintf_unfiltered (gdb_stdlog, "-> ");
1939 fprint_frame (gdb_stdlog, prev_frame);
1940 fprintf_unfiltered (gdb_stdlog, " }\n");
1941 }
1942
1943 return prev_frame;
1944 }
1945
1946 /* Debug routine to print a NULL frame being returned. */
1947
1948 static void
1949 frame_debug_got_null_frame (struct frame_info *this_frame,
1950 const char *reason)
1951 {
1952 if (frame_debug)
1953 {
1954 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1955 if (this_frame != NULL)
1956 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1957 else
1958 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1959 fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1960 }
1961 }
1962
1963 /* Is this (non-sentinel) frame in the "main"() function? */
1964
1965 static int
1966 inside_main_func (struct frame_info *this_frame)
1967 {
1968 struct minimal_symbol *msymbol;
1969 CORE_ADDR maddr;
1970
1971 if (symfile_objfile == 0)
1972 return 0;
1973 msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
1974 if (msymbol == NULL)
1975 return 0;
1976 /* Make certain that the code, and not descriptor, address is
1977 returned. */
1978 maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
1979 SYMBOL_VALUE_ADDRESS (msymbol),
1980 &current_target);
1981 return maddr == get_frame_func (this_frame);
1982 }
1983
1984 /* Test whether THIS_FRAME is inside the process entry point function. */
1985
1986 static int
1987 inside_entry_func (struct frame_info *this_frame)
1988 {
1989 CORE_ADDR entry_point;
1990
1991 if (!entry_point_address_query (&entry_point))
1992 return 0;
1993
1994 return get_frame_func (this_frame) == entry_point;
1995 }
1996
1997 /* Return a structure containing various interesting information about
1998 the frame that called THIS_FRAME. Returns NULL if there is entier
1999 no such frame or the frame fails any of a set of target-independent
2000 condition that should terminate the frame chain (e.g., as unwinding
2001 past main()).
2002
2003 This function should not contain target-dependent tests, such as
2004 checking whether the program-counter is zero. */
2005
2006 struct frame_info *
2007 get_prev_frame (struct frame_info *this_frame)
2008 {
2009 CORE_ADDR frame_pc;
2010 int frame_pc_p;
2011
2012 /* There is always a frame. If this assertion fails, suspect that
2013 something should be calling get_selected_frame() or
2014 get_current_frame(). */
2015 gdb_assert (this_frame != NULL);
2016 frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);
2017
2018 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2019 sense to stop unwinding at a dummy frame. One place where a dummy
2020 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2021 pcsqh register (space register for the instruction at the head of the
2022 instruction queue) cannot be written directly; the only way to set it
2023 is to branch to code that is in the target space. In order to implement
2024 frame dummies on HPUX, the called function is made to jump back to where
2025 the inferior was when the user function was called. If gdb was inside
2026 the main function when we created the dummy frame, the dummy frame will
2027 point inside the main function. */
2028 if (this_frame->level >= 0
2029 && get_frame_type (this_frame) == NORMAL_FRAME
2030 && !backtrace_past_main
2031 && frame_pc_p
2032 && inside_main_func (this_frame))
2033 /* Don't unwind past main(). Note, this is done _before_ the
2034 frame has been marked as previously unwound. That way if the
2035 user later decides to enable unwinds past main(), that will
2036 automatically happen. */
2037 {
2038 frame_debug_got_null_frame (this_frame, "inside main func");
2039 return NULL;
2040 }
2041
2042 /* If the user's backtrace limit has been exceeded, stop. We must
2043 add two to the current level; one of those accounts for backtrace_limit
2044 being 1-based and the level being 0-based, and the other accounts for
2045 the level of the new frame instead of the level of the current
2046 frame. */
2047 if (this_frame->level + 2 > backtrace_limit)
2048 {
2049 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
2050 return NULL;
2051 }
2052
2053 /* If we're already inside the entry function for the main objfile,
2054 then it isn't valid. Don't apply this test to a dummy frame -
2055 dummy frame PCs typically land in the entry func. Don't apply
2056 this test to the sentinel frame. Sentinel frames should always
2057 be allowed to unwind. */
2058 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2059 wasn't checking for "main" in the minimal symbols. With that
2060 fixed asm-source tests now stop in "main" instead of halting the
2061 backtrace in weird and wonderful ways somewhere inside the entry
2062 file. Suspect that tests for inside the entry file/func were
2063 added to work around that (now fixed) case. */
2064 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2065 suggested having the inside_entry_func test use the
2066 inside_main_func() msymbol trick (along with entry_point_address()
2067 I guess) to determine the address range of the start function.
2068 That should provide a far better stopper than the current
2069 heuristics. */
2070 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2071 applied tail-call optimizations to main so that a function called
2072 from main returns directly to the caller of main. Since we don't
2073 stop at main, we should at least stop at the entry point of the
2074 application. */
2075 if (this_frame->level >= 0
2076 && get_frame_type (this_frame) == NORMAL_FRAME
2077 && !backtrace_past_entry
2078 && frame_pc_p
2079 && inside_entry_func (this_frame))
2080 {
2081 frame_debug_got_null_frame (this_frame, "inside entry func");
2082 return NULL;
2083 }
2084
2085 /* Assume that the only way to get a zero PC is through something
2086 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2087 will never unwind a zero PC. */
2088 if (this_frame->level > 0
2089 && (get_frame_type (this_frame) == NORMAL_FRAME
2090 || get_frame_type (this_frame) == INLINE_FRAME)
2091 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
2092 && frame_pc_p && frame_pc == 0)
2093 {
2094 frame_debug_got_null_frame (this_frame, "zero PC");
2095 return NULL;
2096 }
2097
2098 return get_prev_frame_1 (this_frame);
2099 }
2100
2101 CORE_ADDR
2102 get_frame_pc (struct frame_info *frame)
2103 {
2104 gdb_assert (frame->next != NULL);
2105 return frame_unwind_pc (frame->next);
2106 }
2107
2108 int
2109 get_frame_pc_if_available (struct frame_info *frame, CORE_ADDR *pc)
2110 {
2111 volatile struct gdb_exception ex;
2112
2113 gdb_assert (frame->next != NULL);
2114
2115 TRY_CATCH (ex, RETURN_MASK_ERROR)
2116 {
2117 *pc = frame_unwind_pc (frame->next);
2118 }
2119 if (ex.reason < 0)
2120 {
2121 if (ex.error == NOT_AVAILABLE_ERROR)
2122 return 0;
2123 else
2124 throw_exception (ex);
2125 }
2126
2127 return 1;
2128 }
2129
2130 /* Return an address that falls within THIS_FRAME's code block. */
2131
2132 CORE_ADDR
2133 get_frame_address_in_block (struct frame_info *this_frame)
2134 {
2135 /* A draft address. */
2136 CORE_ADDR pc = get_frame_pc (this_frame);
2137
2138 struct frame_info *next_frame = this_frame->next;
2139
2140 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2141 Normally the resume address is inside the body of the function
2142 associated with THIS_FRAME, but there is a special case: when
2143 calling a function which the compiler knows will never return
2144 (for instance abort), the call may be the very last instruction
2145 in the calling function. The resume address will point after the
2146 call and may be at the beginning of a different function
2147 entirely.
2148
2149 If THIS_FRAME is a signal frame or dummy frame, then we should
2150 not adjust the unwound PC. For a dummy frame, GDB pushed the
2151 resume address manually onto the stack. For a signal frame, the
2152 OS may have pushed the resume address manually and invoked the
2153 handler (e.g. GNU/Linux), or invoked the trampoline which called
2154 the signal handler - but in either case the signal handler is
2155 expected to return to the trampoline. So in both of these
2156 cases we know that the resume address is executable and
2157 related. So we only need to adjust the PC if THIS_FRAME
2158 is a normal function.
2159
2160 If the program has been interrupted while THIS_FRAME is current,
2161 then clearly the resume address is inside the associated
2162 function. There are three kinds of interruption: debugger stop
2163 (next frame will be SENTINEL_FRAME), operating system
2164 signal or exception (next frame will be SIGTRAMP_FRAME),
2165 or debugger-induced function call (next frame will be
2166 DUMMY_FRAME). So we only need to adjust the PC if
2167 NEXT_FRAME is a normal function.
2168
2169 We check the type of NEXT_FRAME first, since it is already
2170 known; frame type is determined by the unwinder, and since
2171 we have THIS_FRAME we've already selected an unwinder for
2172 NEXT_FRAME.
2173
2174 If the next frame is inlined, we need to keep going until we find
2175 the real function - for instance, if a signal handler is invoked
2176 while in an inlined function, then the code address of the
2177 "calling" normal function should not be adjusted either. */
2178
2179 while (get_frame_type (next_frame) == INLINE_FRAME)
2180 next_frame = next_frame->next;
2181
2182 if ((get_frame_type (next_frame) == NORMAL_FRAME
2183 || get_frame_type (next_frame) == TAILCALL_FRAME)
2184 && (get_frame_type (this_frame) == NORMAL_FRAME
2185 || get_frame_type (this_frame) == TAILCALL_FRAME
2186 || get_frame_type (this_frame) == INLINE_FRAME))
2187 return pc - 1;
2188
2189 return pc;
2190 }
2191
2192 int
2193 get_frame_address_in_block_if_available (struct frame_info *this_frame,
2194 CORE_ADDR *pc)
2195 {
2196 volatile struct gdb_exception ex;
2197
2198 TRY_CATCH (ex, RETURN_MASK_ERROR)
2199 {
2200 *pc = get_frame_address_in_block (this_frame);
2201 }
2202 if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
2203 return 0;
2204 else if (ex.reason < 0)
2205 throw_exception (ex);
2206 else
2207 return 1;
2208 }
2209
2210 void
2211 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
2212 {
2213 struct frame_info *next_frame;
2214 int notcurrent;
2215 CORE_ADDR pc;
2216
2217 /* If the next frame represents an inlined function call, this frame's
2218 sal is the "call site" of that inlined function, which can not
2219 be inferred from get_frame_pc. */
2220 next_frame = get_next_frame (frame);
2221 if (frame_inlined_callees (frame) > 0)
2222 {
2223 struct symbol *sym;
2224
2225 if (next_frame)
2226 sym = get_frame_function (next_frame);
2227 else
2228 sym = inline_skipped_symbol (inferior_ptid);
2229
2230 /* If frame is inline, it certainly has symbols. */
2231 gdb_assert (sym);
2232 init_sal (sal);
2233 if (SYMBOL_LINE (sym) != 0)
2234 {
2235 sal->symtab = SYMBOL_SYMTAB (sym);
2236 sal->line = SYMBOL_LINE (sym);
2237 }
2238 else
2239 /* If the symbol does not have a location, we don't know where
2240 the call site is. Do not pretend to. This is jarring, but
2241 we can't do much better. */
2242 sal->pc = get_frame_pc (frame);
2243
2244 sal->pspace = get_frame_program_space (frame);
2245
2246 return;
2247 }
2248
2249 /* If FRAME is not the innermost frame, that normally means that
2250 FRAME->pc points at the return instruction (which is *after* the
2251 call instruction), and we want to get the line containing the
2252 call (because the call is where the user thinks the program is).
2253 However, if the next frame is either a SIGTRAMP_FRAME or a
2254 DUMMY_FRAME, then the next frame will contain a saved interrupt
2255 PC and such a PC indicates the current (rather than next)
2256 instruction/line, consequently, for such cases, want to get the
2257 line containing fi->pc. */
2258 if (!get_frame_pc_if_available (frame, &pc))
2259 {
2260 init_sal (sal);
2261 return;
2262 }
2263
2264 notcurrent = (pc != get_frame_address_in_block (frame));
2265 (*sal) = find_pc_line (pc, notcurrent);
2266 }
2267
2268 /* Per "frame.h", return the ``address'' of the frame. Code should
2269 really be using get_frame_id(). */
2270 CORE_ADDR
2271 get_frame_base (struct frame_info *fi)
2272 {
2273 return get_frame_id (fi).stack_addr;
2274 }
2275
2276 /* High-level offsets into the frame. Used by the debug info. */
2277
2278 CORE_ADDR
2279 get_frame_base_address (struct frame_info *fi)
2280 {
2281 if (get_frame_type (fi) != NORMAL_FRAME)
2282 return 0;
2283 if (fi->base == NULL)
2284 fi->base = frame_base_find_by_frame (fi);
2285 /* Sneaky: If the low-level unwind and high-level base code share a
2286 common unwinder, let them share the prologue cache. */
2287 if (fi->base->unwind == fi->unwind)
2288 return fi->base->this_base (fi, &fi->prologue_cache);
2289 return fi->base->this_base (fi, &fi->base_cache);
2290 }
2291
2292 CORE_ADDR
2293 get_frame_locals_address (struct frame_info *fi)
2294 {
2295 if (get_frame_type (fi) != NORMAL_FRAME)
2296 return 0;
2297 /* If there isn't a frame address method, find it. */
2298 if (fi->base == NULL)
2299 fi->base = frame_base_find_by_frame (fi);
2300 /* Sneaky: If the low-level unwind and high-level base code share a
2301 common unwinder, let them share the prologue cache. */
2302 if (fi->base->unwind == fi->unwind)
2303 return fi->base->this_locals (fi, &fi->prologue_cache);
2304 return fi->base->this_locals (fi, &fi->base_cache);
2305 }
2306
2307 CORE_ADDR
2308 get_frame_args_address (struct frame_info *fi)
2309 {
2310 if (get_frame_type (fi) != NORMAL_FRAME)
2311 return 0;
2312 /* If there isn't a frame address method, find it. */
2313 if (fi->base == NULL)
2314 fi->base = frame_base_find_by_frame (fi);
2315 /* Sneaky: If the low-level unwind and high-level base code share a
2316 common unwinder, let them share the prologue cache. */
2317 if (fi->base->unwind == fi->unwind)
2318 return fi->base->this_args (fi, &fi->prologue_cache);
2319 return fi->base->this_args (fi, &fi->base_cache);
2320 }
2321
2322 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2323 otherwise. */
2324
2325 int
2326 frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
2327 {
2328 if (fi->unwind == NULL)
2329 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
2330 return fi->unwind == unwinder;
2331 }
2332
2333 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2334 or -1 for a NULL frame. */
2335
2336 int
2337 frame_relative_level (struct frame_info *fi)
2338 {
2339 if (fi == NULL)
2340 return -1;
2341 else
2342 return fi->level;
2343 }
2344
2345 enum frame_type
2346 get_frame_type (struct frame_info *frame)
2347 {
2348 if (frame->unwind == NULL)
2349 /* Initialize the frame's unwinder because that's what
2350 provides the frame's type. */
2351 frame_unwind_find_by_frame (frame, &frame->prologue_cache);
2352 return frame->unwind->type;
2353 }
2354
2355 struct program_space *
2356 get_frame_program_space (struct frame_info *frame)
2357 {
2358 return frame->pspace;
2359 }
2360
2361 struct program_space *
2362 frame_unwind_program_space (struct frame_info *this_frame)
2363 {
2364 gdb_assert (this_frame);
2365
2366 /* This is really a placeholder to keep the API consistent --- we
2367 assume for now that we don't have frame chains crossing
2368 spaces. */
2369 return this_frame->pspace;
2370 }
2371
2372 struct address_space *
2373 get_frame_address_space (struct frame_info *frame)
2374 {
2375 return frame->aspace;
2376 }
2377
2378 /* Memory access methods. */
2379
2380 void
2381 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
2382 gdb_byte *buf, int len)
2383 {
2384 read_memory (addr, buf, len);
2385 }
2386
2387 LONGEST
2388 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
2389 int len)
2390 {
2391 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2392 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2393
2394 return read_memory_integer (addr, len, byte_order);
2395 }
2396
2397 ULONGEST
2398 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
2399 int len)
2400 {
2401 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2402 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2403
2404 return read_memory_unsigned_integer (addr, len, byte_order);
2405 }
2406
2407 int
2408 safe_frame_unwind_memory (struct frame_info *this_frame,
2409 CORE_ADDR addr, gdb_byte *buf, int len)
2410 {
2411 /* NOTE: target_read_memory returns zero on success! */
2412 return !target_read_memory (addr, buf, len);
2413 }
2414
2415 /* Architecture methods. */
2416
2417 struct gdbarch *
2418 get_frame_arch (struct frame_info *this_frame)
2419 {
2420 return frame_unwind_arch (this_frame->next);
2421 }
2422
2423 struct gdbarch *
2424 frame_unwind_arch (struct frame_info *next_frame)
2425 {
2426 if (!next_frame->prev_arch.p)
2427 {
2428 struct gdbarch *arch;
2429
2430 if (next_frame->unwind == NULL)
2431 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
2432
2433 if (next_frame->unwind->prev_arch != NULL)
2434 arch = next_frame->unwind->prev_arch (next_frame,
2435 &next_frame->prologue_cache);
2436 else
2437 arch = get_frame_arch (next_frame);
2438
2439 next_frame->prev_arch.arch = arch;
2440 next_frame->prev_arch.p = 1;
2441 if (frame_debug)
2442 fprintf_unfiltered (gdb_stdlog,
2443 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2444 next_frame->level,
2445 gdbarch_bfd_arch_info (arch)->printable_name);
2446 }
2447
2448 return next_frame->prev_arch.arch;
2449 }
2450
2451 struct gdbarch *
2452 frame_unwind_caller_arch (struct frame_info *next_frame)
2453 {
2454 return frame_unwind_arch (skip_artificial_frames (next_frame));
2455 }
2456
2457 /* Stack pointer methods. */
2458
2459 CORE_ADDR
2460 get_frame_sp (struct frame_info *this_frame)
2461 {
2462 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2463
2464 /* Normality - an architecture that provides a way of obtaining any
2465 frame inner-most address. */
2466 if (gdbarch_unwind_sp_p (gdbarch))
2467 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2468 operate on THIS_FRAME now. */
2469 return gdbarch_unwind_sp (gdbarch, this_frame->next);
2470 /* Now things are really are grim. Hope that the value returned by
2471 the gdbarch_sp_regnum register is meaningful. */
2472 if (gdbarch_sp_regnum (gdbarch) >= 0)
2473 return get_frame_register_unsigned (this_frame,
2474 gdbarch_sp_regnum (gdbarch));
2475 internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
2476 }
2477
2478 /* Return the reason why we can't unwind past FRAME. */
2479
2480 enum unwind_stop_reason
2481 get_frame_unwind_stop_reason (struct frame_info *frame)
2482 {
2483 /* If we haven't tried to unwind past this point yet, then assume
2484 that unwinding would succeed. */
2485 if (frame->prev_p == 0)
2486 return UNWIND_NO_REASON;
2487
2488 /* Otherwise, we set a reason when we succeeded (or failed) to
2489 unwind. */
2490 return frame->stop_reason;
2491 }
2492
2493 /* Return a string explaining REASON. */
2494
2495 const char *
2496 frame_stop_reason_string (enum unwind_stop_reason reason)
2497 {
2498 switch (reason)
2499 {
2500 #define SET(name, description) \
2501 case name: return _(description);
2502 #include "unwind_stop_reasons.def"
2503 #undef SET
2504
2505 default:
2506 internal_error (__FILE__, __LINE__,
2507 "Invalid frame stop reason");
2508 }
2509 }
2510
2511 /* Return the enum symbol name of REASON as a string, to use in debug
2512 output. */
2513
2514 static const char *
2515 frame_stop_reason_symbol_string (enum unwind_stop_reason reason)
2516 {
2517 switch (reason)
2518 {
2519 #define SET(name, description) \
2520 case name: return #name;
2521 #include "unwind_stop_reasons.def"
2522 #undef SET
2523
2524 default:
2525 internal_error (__FILE__, __LINE__,
2526 "Invalid frame stop reason");
2527 }
2528 }
2529
2530 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2531 FRAME. */
2532
2533 static void
2534 frame_cleanup_after_sniffer (void *arg)
2535 {
2536 struct frame_info *frame = arg;
2537
2538 /* The sniffer should not allocate a prologue cache if it did not
2539 match this frame. */
2540 gdb_assert (frame->prologue_cache == NULL);
2541
2542 /* No sniffer should extend the frame chain; sniff based on what is
2543 already certain. */
2544 gdb_assert (!frame->prev_p);
2545
2546 /* The sniffer should not check the frame's ID; that's circular. */
2547 gdb_assert (!frame->this_id.p);
2548
2549 /* Clear cached fields dependent on the unwinder.
2550
2551 The previous PC is independent of the unwinder, but the previous
2552 function is not (see get_frame_address_in_block). */
2553 frame->prev_func.p = 0;
2554 frame->prev_func.addr = 0;
2555
2556 /* Discard the unwinder last, so that we can easily find it if an assertion
2557 in this function triggers. */
2558 frame->unwind = NULL;
2559 }
2560
2561 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2562 Return a cleanup which should be called if unwinding fails, and
2563 discarded if it succeeds. */
2564
2565 struct cleanup *
2566 frame_prepare_for_sniffer (struct frame_info *frame,
2567 const struct frame_unwind *unwind)
2568 {
2569 gdb_assert (frame->unwind == NULL);
2570 frame->unwind = unwind;
2571 return make_cleanup (frame_cleanup_after_sniffer, frame);
2572 }
2573
2574 extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
2575
2576 static struct cmd_list_element *set_backtrace_cmdlist;
2577 static struct cmd_list_element *show_backtrace_cmdlist;
2578
2579 static void
2580 set_backtrace_cmd (char *args, int from_tty)
2581 {
2582 help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
2583 }
2584
2585 static void
2586 show_backtrace_cmd (char *args, int from_tty)
2587 {
2588 cmd_show_list (show_backtrace_cmdlist, from_tty, "");
2589 }
2590
2591 void
2592 _initialize_frame (void)
2593 {
2594 obstack_init (&frame_cache_obstack);
2595
2596 frame_stash_create ();
2597
2598 observer_attach_target_changed (frame_observer_target_changed);
2599
2600 add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
2601 Set backtrace specific variables.\n\
2602 Configure backtrace variables such as the backtrace limit"),
2603 &set_backtrace_cmdlist, "set backtrace ",
2604 0/*allow-unknown*/, &setlist);
2605 add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
2606 Show backtrace specific variables\n\
2607 Show backtrace variables such as the backtrace limit"),
2608 &show_backtrace_cmdlist, "show backtrace ",
2609 0/*allow-unknown*/, &showlist);
2610
2611 add_setshow_boolean_cmd ("past-main", class_obscure,
2612 &backtrace_past_main, _("\
2613 Set whether backtraces should continue past \"main\"."), _("\
2614 Show whether backtraces should continue past \"main\"."), _("\
2615 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2616 the backtrace at \"main\". Set this variable if you need to see the rest\n\
2617 of the stack trace."),
2618 NULL,
2619 show_backtrace_past_main,
2620 &set_backtrace_cmdlist,
2621 &show_backtrace_cmdlist);
2622
2623 add_setshow_boolean_cmd ("past-entry", class_obscure,
2624 &backtrace_past_entry, _("\
2625 Set whether backtraces should continue past the entry point of a program."),
2626 _("\
2627 Show whether backtraces should continue past the entry point of a program."),
2628 _("\
2629 Normally there are no callers beyond the entry point of a program, so GDB\n\
2630 will terminate the backtrace there. Set this variable if you need to see\n\
2631 the rest of the stack trace."),
2632 NULL,
2633 show_backtrace_past_entry,
2634 &set_backtrace_cmdlist,
2635 &show_backtrace_cmdlist);
2636
2637 add_setshow_uinteger_cmd ("limit", class_obscure,
2638 &backtrace_limit, _("\
2639 Set an upper bound on the number of backtrace levels."), _("\
2640 Show the upper bound on the number of backtrace levels."), _("\
2641 No more than the specified number of frames can be displayed or examined.\n\
2642 Literal \"unlimited\" or zero means no limit."),
2643 NULL,
2644 show_backtrace_limit,
2645 &set_backtrace_cmdlist,
2646 &show_backtrace_cmdlist);
2647
2648 /* Debug this files internals. */
2649 add_setshow_zuinteger_cmd ("frame", class_maintenance, &frame_debug, _("\
2650 Set frame debugging."), _("\
2651 Show frame debugging."), _("\
2652 When non-zero, frame specific internal debugging is enabled."),
2653 NULL,
2654 show_frame_debug,
2655 &setdebuglist, &showdebuglist);
2656 }
This page took 0.170679 seconds and 5 git commands to generate.