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