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