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