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