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