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