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