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