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