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