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