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