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