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