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