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