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