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