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