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