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