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