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