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