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