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