b00eebc2ae8e3e5aa005067b22dcccf719ccdaa5
[deliverable/binutils-gdb.git] / gdb / frame.c
1 /* Cache and manage frames for GDB, the GNU debugger.
2
3 Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4 2001, 2002, 2003 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "target.h"
26 #include "value.h"
27 #include "inferior.h" /* for inferior_ptid */
28 #include "regcache.h"
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
31 #include "builtin-regs.h"
32 #include "gdb_obstack.h"
33 #include "dummy-frame.h"
34 #include "gdbcore.h"
35 #include "annotate.h"
36 #include "language.h"
37 #include "frame-unwind.h"
38
39 /* Return a frame uniq ID that can be used to, later, re-find the
40 frame. */
41
42 struct frame_id
43 get_frame_id (struct frame_info *fi)
44 {
45 if (fi == NULL)
46 {
47 return null_frame_id;
48 }
49 else
50 {
51 struct frame_id id;
52 id.base = fi->frame;
53 id.pc = fi->pc;
54 return id;
55 }
56 }
57
58 const struct frame_id null_frame_id; /* All zeros. */
59
60 struct frame_id
61 frame_id_build (CORE_ADDR base, CORE_ADDR func_or_pc)
62 {
63 struct frame_id id;
64 id.base = base;
65 id.pc = func_or_pc;
66 return id;
67 }
68
69 int
70 frame_id_p (struct frame_id l)
71 {
72 /* The .func can be NULL but the .base cannot. */
73 return (l.base != 0);
74 }
75
76 int
77 frame_id_eq (struct frame_id l, struct frame_id r)
78 {
79 /* If .base is different, the frames are different. */
80 if (l.base != r.base)
81 return 0;
82 /* Add a test to check that the frame ID's are for the same function
83 here. */
84 return 1;
85 }
86
87 int
88 frame_id_inner (struct frame_id l, struct frame_id r)
89 {
90 /* Only return non-zero when strictly inner than. Note that, per
91 comment in "frame.h", there is some fuzz here. Frameless
92 functions are not strictly inner than (same .base but different
93 .func). */
94 return INNER_THAN (l.base, r.base);
95 }
96
97 struct frame_info *
98 frame_find_by_id (struct frame_id id)
99 {
100 struct frame_info *frame;
101
102 /* ZERO denotes the null frame, let the caller decide what to do
103 about it. Should it instead return get_current_frame()? */
104 if (!frame_id_p (id))
105 return NULL;
106
107 for (frame = get_current_frame ();
108 frame != NULL;
109 frame = get_prev_frame (frame))
110 {
111 struct frame_id this = get_frame_id (frame);
112 if (frame_id_eq (id, this))
113 /* An exact match. */
114 return frame;
115 if (frame_id_inner (id, this))
116 /* Gone to far. */
117 return NULL;
118 /* Either, we're not yet gone far enough out along the frame
119 chain (inner(this,id), or we're comparing frameless functions
120 (same .base, different .func, no test available). Struggle
121 on until we've definitly gone to far. */
122 }
123 return NULL;
124 }
125
126 CORE_ADDR
127 frame_pc_unwind (struct frame_info *frame)
128 {
129 if (!frame->pc_unwind_cache_p)
130 {
131 frame->pc_unwind_cache = frame->unwind->pc (frame, &frame->unwind_cache);
132 frame->pc_unwind_cache_p = 1;
133 }
134 return frame->pc_unwind_cache;
135 }
136
137 struct frame_id
138 frame_id_unwind (struct frame_info *frame)
139 {
140 if (!frame->id_unwind_cache_p)
141 {
142 frame->unwind->id (frame, &frame->unwind_cache, &frame->id_unwind_cache);
143 frame->id_unwind_cache_p = 1;
144 }
145 return frame->id_unwind_cache;
146 }
147
148 void
149 frame_pop (struct frame_info *frame)
150 {
151 /* FIXME: cagney/2003-01-18: There is probably a chicken-egg problem
152 with passing in current_regcache. The pop function needs to be
153 written carefully so as to not overwrite registers whose [old]
154 values are needed to restore other registers. Instead, this code
155 should pass in a scratch cache and, as a second step, restore the
156 registers using that. */
157 frame->unwind->pop (frame, &frame->unwind_cache, current_regcache);
158 flush_cached_frames ();
159 }
160
161 void
162 frame_register_unwind (struct frame_info *frame, int regnum,
163 int *optimizedp, enum lval_type *lvalp,
164 CORE_ADDR *addrp, int *realnump, void *bufferp)
165 {
166 struct frame_unwind_cache *cache;
167
168 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
169 that the value proper does not need to be fetched. */
170 gdb_assert (optimizedp != NULL);
171 gdb_assert (lvalp != NULL);
172 gdb_assert (addrp != NULL);
173 gdb_assert (realnump != NULL);
174 /* gdb_assert (bufferp != NULL); */
175
176 /* NOTE: cagney/2002-04-14: It would be nice if, instead of a
177 special case, there was always an inner frame dedicated to the
178 hardware registers. Unfortunatly, there is too much unwind code
179 around that looks up/down the frame chain while making the
180 assumption that each frame level is using the same unwind code. */
181
182 if (frame == NULL)
183 {
184 /* We're in the inner-most frame, get the value direct from the
185 register cache. */
186 *optimizedp = 0;
187 *lvalp = lval_register;
188 /* ULGH! Code uses the offset into the raw register byte array
189 as a way of identifying a register. */
190 *addrp = REGISTER_BYTE (regnum);
191 /* Should this code test ``register_cached (regnum) < 0'' and do
192 something like set realnum to -1 when the register isn't
193 available? */
194 *realnump = regnum;
195 if (bufferp)
196 deprecated_read_register_gen (regnum, bufferp);
197 return;
198 }
199
200 /* Ask this frame to unwind its register. */
201 frame->unwind->reg (frame, &frame->unwind_cache, regnum,
202 optimizedp, lvalp, addrp, realnump, bufferp);
203 }
204
205 void
206 frame_register (struct frame_info *frame, int regnum,
207 int *optimizedp, enum lval_type *lvalp,
208 CORE_ADDR *addrp, int *realnump, void *bufferp)
209 {
210 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
211 that the value proper does not need to be fetched. */
212 gdb_assert (optimizedp != NULL);
213 gdb_assert (lvalp != NULL);
214 gdb_assert (addrp != NULL);
215 gdb_assert (realnump != NULL);
216 /* gdb_assert (bufferp != NULL); */
217
218 /* Ulgh! Old code that, for lval_register, sets ADDRP to the offset
219 of the register in the register cache. It should instead return
220 the REGNUM corresponding to that register. Translate the . */
221 if (GET_SAVED_REGISTER_P ())
222 {
223 GET_SAVED_REGISTER (bufferp, optimizedp, addrp, frame, regnum, lvalp);
224 /* Compute the REALNUM if the caller wants it. */
225 if (*lvalp == lval_register)
226 {
227 int regnum;
228 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
229 {
230 if (*addrp == register_offset_hack (current_gdbarch, regnum))
231 {
232 *realnump = regnum;
233 return;
234 }
235 }
236 internal_error (__FILE__, __LINE__,
237 "Failed to compute the register number corresponding"
238 " to 0x%s", paddr_d (*addrp));
239 }
240 *realnump = -1;
241 return;
242 }
243
244 /* Reached the the bottom (youngest, inner most) of the frame chain
245 (youngest, inner most) frame, go direct to the hardware register
246 cache (do not pass go, do not try to cache the value, ...). The
247 unwound value would have been cached in frame->next but that
248 doesn't exist. This doesn't matter as the hardware register
249 cache is stopping any unnecessary accesses to the target. */
250
251 /* NOTE: cagney/2002-04-14: It would be nice if, instead of a
252 special case, there was always an inner frame dedicated to the
253 hardware registers. Unfortunatly, there is too much unwind code
254 around that looks up/down the frame chain while making the
255 assumption that each frame level is using the same unwind code. */
256
257 if (frame == NULL)
258 frame_register_unwind (NULL, regnum, optimizedp, lvalp, addrp, realnump,
259 bufferp);
260 else
261 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
262 realnump, bufferp);
263 }
264
265 void
266 frame_unwind_signed_register (struct frame_info *frame, int regnum,
267 LONGEST *val)
268 {
269 int optimized;
270 CORE_ADDR addr;
271 int realnum;
272 enum lval_type lval;
273 void *buf = alloca (MAX_REGISTER_RAW_SIZE);
274 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
275 &realnum, buf);
276 (*val) = extract_signed_integer (buf, REGISTER_VIRTUAL_SIZE (regnum));
277 }
278
279 void
280 frame_unwind_unsigned_register (struct frame_info *frame, int regnum,
281 ULONGEST *val)
282 {
283 int optimized;
284 CORE_ADDR addr;
285 int realnum;
286 enum lval_type lval;
287 void *buf = alloca (MAX_REGISTER_RAW_SIZE);
288 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
289 &realnum, buf);
290 (*val) = extract_unsigned_integer (buf, REGISTER_VIRTUAL_SIZE (regnum));
291 }
292
293 void
294 frame_read_unsigned_register (struct frame_info *frame, int regnum,
295 ULONGEST *val)
296 {
297 /* NOTE: cagney/2002-10-31: There is a bit of dogma here - there is
298 always a frame. Both this, and the equivalent
299 frame_read_signed_register() function, can only be called with a
300 valid frame. If, for some reason, this function is called
301 without a frame then the problem isn't here, but rather in the
302 caller. It should of first created a frame and then passed that
303 in. */
304 /* NOTE: cagney/2002-10-31: As a side bar, keep in mind that the
305 ``current_frame'' should not be treated as a special case. While
306 ``get_next_frame (current_frame) == NULL'' currently holds, it
307 should, as far as possible, not be relied upon. In the future,
308 ``get_next_frame (current_frame)'' may instead simply return a
309 normal frame object that simply always gets register values from
310 the register cache. Consequently, frame code should try to avoid
311 tests like ``if get_next_frame() == NULL'' and instead just rely
312 on recursive frame calls (like the below code) when manipulating
313 a frame chain. */
314 gdb_assert (frame != NULL);
315 frame_unwind_unsigned_register (get_next_frame (frame), regnum, val);
316 }
317
318 void
319 frame_read_signed_register (struct frame_info *frame, int regnum,
320 LONGEST *val)
321 {
322 /* See note in frame_read_unsigned_register(). */
323 gdb_assert (frame != NULL);
324 frame_unwind_signed_register (get_next_frame (frame), regnum, val);
325 }
326
327 static void
328 generic_unwind_get_saved_register (char *raw_buffer,
329 int *optimizedp,
330 CORE_ADDR *addrp,
331 struct frame_info *frame,
332 int regnum,
333 enum lval_type *lvalp)
334 {
335 int optimizedx;
336 CORE_ADDR addrx;
337 int realnumx;
338 enum lval_type lvalx;
339
340 if (!target_has_registers)
341 error ("No registers.");
342
343 /* Keep things simple, ensure that all the pointers (except valuep)
344 are non NULL. */
345 if (optimizedp == NULL)
346 optimizedp = &optimizedx;
347 if (lvalp == NULL)
348 lvalp = &lvalx;
349 if (addrp == NULL)
350 addrp = &addrx;
351
352 /* Reached the the bottom (youngest, inner most) of the frame chain
353 (youngest, inner most) frame, go direct to the hardware register
354 cache (do not pass go, do not try to cache the value, ...). The
355 unwound value would have been cached in frame->next but that
356 doesn't exist. This doesn't matter as the hardware register
357 cache is stopping any unnecessary accesses to the target. */
358
359 /* NOTE: cagney/2002-04-14: It would be nice if, instead of a
360 special case, there was always an inner frame dedicated to the
361 hardware registers. Unfortunatly, there is too much unwind code
362 around that looks up/down the frame chain while making the
363 assumption that each frame level is using the same unwind code. */
364
365 if (frame == NULL)
366 frame_register_unwind (NULL, regnum, optimizedp, lvalp, addrp, &realnumx,
367 raw_buffer);
368 else
369 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
370 &realnumx, raw_buffer);
371 }
372
373 void
374 get_saved_register (char *raw_buffer,
375 int *optimized,
376 CORE_ADDR *addrp,
377 struct frame_info *frame,
378 int regnum,
379 enum lval_type *lval)
380 {
381 if (GET_SAVED_REGISTER_P ())
382 {
383 GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval);
384 return;
385 }
386 generic_unwind_get_saved_register (raw_buffer, optimized, addrp, frame,
387 regnum, lval);
388 }
389
390 /* frame_register_read ()
391
392 Find and return the value of REGNUM for the specified stack frame.
393 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
394
395 Returns 0 if the register value could not be found. */
396
397 int
398 frame_register_read (struct frame_info *frame, int regnum, void *myaddr)
399 {
400 int optimized;
401 enum lval_type lval;
402 CORE_ADDR addr;
403 int realnum;
404 frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
405
406 /* FIXME: cagney/2002-05-15: This test, is just bogus.
407
408 It indicates that the target failed to supply a value for a
409 register because it was "not available" at this time. Problem
410 is, the target still has the register and so get saved_register()
411 may be returning a value saved on the stack. */
412
413 if (register_cached (regnum) < 0)
414 return 0; /* register value not available */
415
416 return !optimized;
417 }
418
419
420 /* Map between a frame register number and its name. A frame register
421 space is a superset of the cooked register space --- it also
422 includes builtin registers. */
423
424 int
425 frame_map_name_to_regnum (const char *name, int len)
426 {
427 int i;
428
429 /* Search register name space. */
430 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
431 if (REGISTER_NAME (i) && len == strlen (REGISTER_NAME (i))
432 && strncmp (name, REGISTER_NAME (i), len) == 0)
433 {
434 return i;
435 }
436
437 /* Try builtin registers. */
438 i = builtin_reg_map_name_to_regnum (name, len);
439 if (i >= 0)
440 {
441 /* A builtin register doesn't fall into the architecture's
442 register range. */
443 gdb_assert (i >= NUM_REGS + NUM_PSEUDO_REGS);
444 return i;
445 }
446
447 return -1;
448 }
449
450 const char *
451 frame_map_regnum_to_name (int regnum)
452 {
453 if (regnum < 0)
454 return NULL;
455 if (regnum < NUM_REGS + NUM_PSEUDO_REGS)
456 return REGISTER_NAME (regnum);
457 return builtin_reg_map_regnum_to_name (regnum);
458 }
459
460 /* Info about the innermost stack frame (contents of FP register) */
461
462 static struct frame_info *current_frame;
463
464 /* Cache for frame addresses already read by gdb. Valid only while
465 inferior is stopped. Control variables for the frame cache should
466 be local to this module. */
467
468 static struct obstack frame_cache_obstack;
469
470 void *
471 frame_obstack_zalloc (unsigned long size)
472 {
473 void *data = obstack_alloc (&frame_cache_obstack, size);
474 memset (data, 0, size);
475 return data;
476 }
477
478 CORE_ADDR *
479 frame_saved_regs_zalloc (struct frame_info *fi)
480 {
481 fi->saved_regs = (CORE_ADDR *)
482 frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS);
483 return fi->saved_regs;
484 }
485
486 CORE_ADDR *
487 get_frame_saved_regs (struct frame_info *fi)
488 {
489 return fi->saved_regs;
490 }
491
492 /* Return the innermost (currently executing) stack frame. */
493
494 struct frame_info *
495 get_current_frame (void)
496 {
497 if (current_frame == NULL)
498 {
499 if (target_has_stack)
500 current_frame = create_new_frame (read_fp (), read_pc ());
501 else
502 error ("No stack.");
503 }
504 return current_frame;
505 }
506
507 /* The "selected" stack frame is used by default for local and arg
508 access. May be zero, for no selected frame. */
509
510 struct frame_info *deprecated_selected_frame;
511
512 /* Return the selected frame. Always non-null (unless there isn't an
513 inferior sufficient for creating a frame) in which case an error is
514 thrown. */
515
516 struct frame_info *
517 get_selected_frame (void)
518 {
519 if (deprecated_selected_frame == NULL)
520 /* Hey! Don't trust this. It should really be re-finding the
521 last selected frame of the currently selected thread. This,
522 though, is better than nothing. */
523 select_frame (get_current_frame ());
524 /* There is always a frame. */
525 gdb_assert (deprecated_selected_frame != NULL);
526 return deprecated_selected_frame;
527 }
528
529 /* Select frame FI (or NULL - to invalidate the current frame). */
530
531 void
532 select_frame (struct frame_info *fi)
533 {
534 register struct symtab *s;
535
536 deprecated_selected_frame = fi;
537 /* NOTE: cagney/2002-05-04: FI can be NULL. This occures when the
538 frame is being invalidated. */
539 if (selected_frame_level_changed_hook)
540 selected_frame_level_changed_hook (frame_relative_level (fi));
541
542 /* FIXME: kseitz/2002-08-28: It would be nice to call
543 selected_frame_level_changed_event right here, but due to limitations
544 in the current interfaces, we would end up flooding UIs with events
545 because select_frame is used extensively internally.
546
547 Once we have frame-parameterized frame (and frame-related) commands,
548 the event notification can be moved here, since this function will only
549 be called when the users selected frame is being changed. */
550
551 /* Ensure that symbols for this frame are read in. Also, determine the
552 source language of this frame, and switch to it if desired. */
553 if (fi)
554 {
555 s = find_pc_symtab (fi->pc);
556 if (s
557 && s->language != current_language->la_language
558 && s->language != language_unknown
559 && language_mode == language_mode_auto)
560 {
561 set_language (s->language);
562 }
563 }
564 }
565
566 /* Return the register saved in the simplistic ``saved_regs'' cache.
567 If the value isn't here AND a value is needed, try the next inner
568 most frame. */
569
570 static void
571 frame_saved_regs_register_unwind (struct frame_info *frame, void **cache,
572 int regnum, int *optimizedp,
573 enum lval_type *lvalp, CORE_ADDR *addrp,
574 int *realnump, void *bufferp)
575 {
576 /* There is always a frame at this point. And THIS is the frame
577 we're interested in. */
578 gdb_assert (frame != NULL);
579 /* If we're using generic dummy frames, we'd better not be in a call
580 dummy. (generic_call_dummy_register_unwind ought to have been called
581 instead.) */
582 gdb_assert (!(DEPRECATED_USE_GENERIC_DUMMY_FRAMES
583 && (get_frame_type (frame) == DUMMY_FRAME)));
584
585 /* Only (older) architectures that implement the
586 FRAME_INIT_SAVED_REGS method should be using this function. */
587 gdb_assert (FRAME_INIT_SAVED_REGS_P ());
588
589 /* Load the saved_regs register cache. */
590 if (frame->saved_regs == NULL)
591 FRAME_INIT_SAVED_REGS (frame);
592
593 if (frame->saved_regs != NULL
594 && frame->saved_regs[regnum] != 0)
595 {
596 if (regnum == SP_REGNUM)
597 {
598 /* SP register treated specially. */
599 *optimizedp = 0;
600 *lvalp = not_lval;
601 *addrp = 0;
602 *realnump = -1;
603 if (bufferp != NULL)
604 store_address (bufferp, REGISTER_RAW_SIZE (regnum),
605 frame->saved_regs[regnum]);
606 }
607 else
608 {
609 /* Any other register is saved in memory, fetch it but cache
610 a local copy of its value. */
611 *optimizedp = 0;
612 *lvalp = lval_memory;
613 *addrp = frame->saved_regs[regnum];
614 *realnump = -1;
615 if (bufferp != NULL)
616 {
617 #if 1
618 /* Save each register value, as it is read in, in a
619 frame based cache. */
620 void **regs = (*cache);
621 if (regs == NULL)
622 {
623 int sizeof_cache = ((NUM_REGS + NUM_PSEUDO_REGS)
624 * sizeof (void *));
625 regs = frame_obstack_zalloc (sizeof_cache);
626 (*cache) = regs;
627 }
628 if (regs[regnum] == NULL)
629 {
630 regs[regnum]
631 = frame_obstack_zalloc (REGISTER_RAW_SIZE (regnum));
632 read_memory (frame->saved_regs[regnum], regs[regnum],
633 REGISTER_RAW_SIZE (regnum));
634 }
635 memcpy (bufferp, regs[regnum], REGISTER_RAW_SIZE (regnum));
636 #else
637 /* Read the value in from memory. */
638 read_memory (frame->saved_regs[regnum], bufferp,
639 REGISTER_RAW_SIZE (regnum));
640 #endif
641 }
642 }
643 return;
644 }
645
646 /* No luck, assume this and the next frame have the same register
647 value. If a value is needed, pass the request on down the chain;
648 otherwise just return an indication that the value is in the same
649 register as the next frame. */
650 if (bufferp == NULL)
651 {
652 *optimizedp = 0;
653 *lvalp = lval_register;
654 *addrp = 0;
655 *realnump = regnum;
656 }
657 else
658 {
659 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
660 realnump, bufferp);
661 }
662 }
663
664 static CORE_ADDR
665 frame_saved_regs_pc_unwind (struct frame_info *frame, void **cache)
666 {
667 return FRAME_SAVED_PC (frame);
668 }
669
670 static void
671 frame_saved_regs_id_unwind (struct frame_info *next_frame, void **cache,
672 struct frame_id *id)
673 {
674 int fromleaf;
675 CORE_ADDR base;
676 CORE_ADDR pc;
677
678 /* Start out by assuming it's NULL. */
679 (*id) = null_frame_id;
680
681 if (next_frame->next == NULL)
682 /* FIXME: 2002-11-09: Frameless functions can occure anywhere in
683 the frame chain, not just the inner most frame! The generic,
684 per-architecture, frame code should handle this and the below
685 should simply be removed. */
686 fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame);
687 else
688 fromleaf = 0;
689
690 if (fromleaf)
691 /* A frameless inner-most frame. The `FP' (which isn't an
692 architecture frame-pointer register!) of the caller is the same
693 as the callee. */
694 /* FIXME: 2002-11-09: There isn't any reason to special case this
695 edge condition. Instead the per-architecture code should hande
696 it locally. */
697 base = get_frame_base (next_frame);
698 else
699 {
700 /* Two macros defined in tm.h specify the machine-dependent
701 actions to be performed here.
702
703 First, get the frame's chain-pointer.
704
705 If that is zero, the frame is the outermost frame or a leaf
706 called by the outermost frame. This means that if start
707 calls main without a frame, we'll return 0 (which is fine
708 anyway).
709
710 Nope; there's a problem. This also returns when the current
711 routine is a leaf of main. This is unacceptable. We move
712 this to after the ffi test; I'd rather have backtraces from
713 start go curfluy than have an abort called from main not show
714 main. */
715 base = FRAME_CHAIN (next_frame);
716
717 if (!frame_chain_valid (base, next_frame))
718 return;
719 }
720 if (base == 0)
721 return;
722
723 /* FIXME: cagney/2002-06-08: This should probably return the frame's
724 function and not the PC (a.k.a. resume address). */
725 pc = frame_pc_unwind (next_frame);
726 id->pc = pc;
727 id->base = base;
728 }
729
730 static void
731 frame_saved_regs_pop (struct frame_info *fi, void **cache,
732 struct regcache *regcache)
733 {
734 gdb_assert (POP_FRAME_P ());
735 POP_FRAME;
736 }
737
738 const struct frame_unwind trad_frame_unwinder = {
739 frame_saved_regs_pop,
740 frame_saved_regs_pc_unwind,
741 frame_saved_regs_id_unwind,
742 frame_saved_regs_register_unwind
743 };
744 const struct frame_unwind *trad_frame_unwind = &trad_frame_unwinder;
745
746
747 /* Function: get_saved_register
748 Find register number REGNUM relative to FRAME and put its (raw,
749 target format) contents in *RAW_BUFFER.
750
751 Set *OPTIMIZED if the variable was optimized out (and thus can't be
752 fetched). Note that this is never set to anything other than zero
753 in this implementation.
754
755 Set *LVAL to lval_memory, lval_register, or not_lval, depending on
756 whether the value was fetched from memory, from a register, or in a
757 strange and non-modifiable way (e.g. a frame pointer which was
758 calculated rather than fetched). We will use not_lval for values
759 fetched from generic dummy frames.
760
761 Set *ADDRP to the address, either in memory or as a REGISTER_BYTE
762 offset into the registers array. If the value is stored in a dummy
763 frame, set *ADDRP to zero.
764
765 To use this implementation, define a function called
766 "get_saved_register" in your target code, which simply passes all
767 of its arguments to this function.
768
769 The argument RAW_BUFFER must point to aligned memory. */
770
771 void
772 deprecated_generic_get_saved_register (char *raw_buffer, int *optimized,
773 CORE_ADDR *addrp,
774 struct frame_info *frame, int regnum,
775 enum lval_type *lval)
776 {
777 if (!target_has_registers)
778 error ("No registers.");
779
780 gdb_assert (FRAME_INIT_SAVED_REGS_P ());
781
782 /* Normal systems don't optimize out things with register numbers. */
783 if (optimized != NULL)
784 *optimized = 0;
785
786 if (addrp) /* default assumption: not found in memory */
787 *addrp = 0;
788
789 /* Note: since the current frame's registers could only have been
790 saved by frames INTERIOR TO the current frame, we skip examining
791 the current frame itself: otherwise, we would be getting the
792 previous frame's registers which were saved by the current frame. */
793
794 while (frame && ((frame = frame->next) != NULL))
795 {
796 if (get_frame_type (frame) == DUMMY_FRAME)
797 {
798 if (lval) /* found it in a CALL_DUMMY frame */
799 *lval = not_lval;
800 if (raw_buffer)
801 /* FIXME: cagney/2002-06-26: This should be via the
802 gdbarch_register_read() method so that it, on the fly,
803 constructs either a raw or pseudo register from the raw
804 register cache. */
805 regcache_raw_read (generic_find_dummy_frame (frame->pc,
806 frame->frame),
807 regnum, raw_buffer);
808 return;
809 }
810
811 FRAME_INIT_SAVED_REGS (frame);
812 if (frame->saved_regs != NULL
813 && frame->saved_regs[regnum] != 0)
814 {
815 if (lval) /* found it saved on the stack */
816 *lval = lval_memory;
817 if (regnum == SP_REGNUM)
818 {
819 if (raw_buffer) /* SP register treated specially */
820 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
821 frame->saved_regs[regnum]);
822 }
823 else
824 {
825 if (addrp) /* any other register */
826 *addrp = frame->saved_regs[regnum];
827 if (raw_buffer)
828 read_memory (frame->saved_regs[regnum], raw_buffer,
829 REGISTER_RAW_SIZE (regnum));
830 }
831 return;
832 }
833 }
834
835 /* If we get thru the loop to this point, it means the register was
836 not saved in any frame. Return the actual live-register value. */
837
838 if (lval) /* found it in a live register */
839 *lval = lval_register;
840 if (addrp)
841 *addrp = REGISTER_BYTE (regnum);
842 if (raw_buffer)
843 deprecated_read_register_gen (regnum, raw_buffer);
844 }
845
846 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
847 Always returns a non-NULL value. */
848
849 struct frame_info *
850 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
851 {
852 struct frame_info *fi;
853 enum frame_type type;
854
855 fi = frame_obstack_zalloc (sizeof (struct frame_info));
856
857 fi->frame = addr;
858 fi->pc = pc;
859 /* NOTE: cagney/2002-11-18: The code segments, found in
860 create_new_frame and get_prev_frame(), that initializes the
861 frames type is subtly different. The latter only updates ->type
862 when it encounters a SIGTRAMP_FRAME or DUMMY_FRAME. This stops
863 get_prev_frame() overriding the frame's type when the INIT code
864 has previously set it. This is really somewhat bogus. The
865 initialization, as seen in create_new_frame(), should occur
866 before the INIT function has been called. */
867 if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
868 && (DEPRECATED_PC_IN_CALL_DUMMY_P ()
869 ? DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0)
870 : pc_in_dummy_frame (pc)))
871 /* NOTE: cagney/2002-11-11: Does this even occure? */
872 type = DUMMY_FRAME;
873 else
874 {
875 char *name;
876 find_pc_partial_function (pc, &name, NULL, NULL);
877 if (PC_IN_SIGTRAMP (fi->pc, name))
878 type = SIGTRAMP_FRAME;
879 else
880 type = NORMAL_FRAME;
881 }
882 fi->type = type;
883
884 if (INIT_EXTRA_FRAME_INFO_P ())
885 INIT_EXTRA_FRAME_INFO (0, fi);
886
887 /* Select/initialize an unwind function. */
888 fi->unwind = frame_unwind_find_by_pc (current_gdbarch, fi->pc);
889
890 return fi;
891 }
892
893 /* Return the frame that FRAME calls (NULL if FRAME is the innermost
894 frame). */
895
896 struct frame_info *
897 get_next_frame (struct frame_info *frame)
898 {
899 return frame->next;
900 }
901
902 /* Flush the entire frame cache. */
903
904 void
905 flush_cached_frames (void)
906 {
907 /* Since we can't really be sure what the first object allocated was */
908 obstack_free (&frame_cache_obstack, 0);
909 obstack_init (&frame_cache_obstack);
910
911 current_frame = NULL; /* Invalidate cache */
912 select_frame (NULL);
913 annotate_frames_invalid ();
914 }
915
916 /* Flush the frame cache, and start a new one if necessary. */
917
918 void
919 reinit_frame_cache (void)
920 {
921 flush_cached_frames ();
922
923 /* FIXME: The inferior_ptid test is wrong if there is a corefile. */
924 if (PIDGET (inferior_ptid) != 0)
925 {
926 select_frame (get_current_frame ());
927 }
928 }
929
930 /* Return a structure containing various interesting information
931 about the frame that called NEXT_FRAME. Returns NULL
932 if there is no such frame. */
933
934 struct frame_info *
935 get_prev_frame (struct frame_info *next_frame)
936 {
937 CORE_ADDR address = 0;
938 struct frame_info *prev;
939 int fromleaf;
940
941 /* Return the inner-most frame, when the caller passes in NULL. */
942 /* NOTE: cagney/2002-11-09: Not sure how this would happen. The
943 caller should have previously obtained a valid frame using
944 get_selected_frame() and then called this code - only possibility
945 I can think of is code behaving badly. */
946 if (next_frame == NULL)
947 {
948 /* NOTE: cagney/2002-11-09: There was a code segment here that
949 would error out when CURRENT_FRAME was NULL. The comment
950 that went with it made the claim ...
951
952 ``This screws value_of_variable, which just wants a nice
953 clean NULL return from block_innermost_frame if there are no
954 frames. I don't think I've ever seen this message happen
955 otherwise. And returning NULL here is a perfectly legitimate
956 thing to do.''
957
958 Per the above, this code shouldn't even be called with a NULL
959 NEXT_FRAME. */
960 return current_frame;
961 }
962
963 /* Only try to do the unwind once. */
964 if (next_frame->prev_p)
965 return next_frame->prev;
966 next_frame->prev_p = 1;
967
968 /* On some machines it is possible to call a function without
969 setting up a stack frame for it. On these machines, we
970 define this macro to take two args; a frameinfo pointer
971 identifying a frame and a variable to set or clear if it is
972 or isn't leafless. */
973
974 /* Still don't want to worry about this except on the innermost
975 frame. This macro will set FROMLEAF if NEXT_FRAME is a frameless
976 function invocation. */
977 if (next_frame->next == NULL)
978 /* FIXME: 2002-11-09: Frameless functions can occure anywhere in
979 the frame chain, not just the inner most frame! The generic,
980 per-architecture, frame code should handle this and the below
981 should simply be removed. */
982 fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame);
983 else
984 fromleaf = 0;
985
986 if (fromleaf)
987 /* A frameless inner-most frame. The `FP' (which isn't an
988 architecture frame-pointer register!) of the caller is the same
989 as the callee. */
990 /* FIXME: 2002-11-09: There isn't any reason to special case this
991 edge condition. Instead the per-architecture code should hande
992 it locally. */
993 address = get_frame_base (next_frame);
994 else
995 {
996 /* Two macros defined in tm.h specify the machine-dependent
997 actions to be performed here.
998
999 First, get the frame's chain-pointer.
1000
1001 If that is zero, the frame is the outermost frame or a leaf
1002 called by the outermost frame. This means that if start
1003 calls main without a frame, we'll return 0 (which is fine
1004 anyway).
1005
1006 Nope; there's a problem. This also returns when the current
1007 routine is a leaf of main. This is unacceptable. We move
1008 this to after the ffi test; I'd rather have backtraces from
1009 start go curfluy than have an abort called from main not show
1010 main. */
1011 address = FRAME_CHAIN (next_frame);
1012
1013 if (!frame_chain_valid (address, next_frame))
1014 return 0;
1015 }
1016 if (address == 0)
1017 return 0;
1018
1019 /* Create an initially zero previous frame. */
1020 prev = frame_obstack_zalloc (sizeof (struct frame_info));
1021
1022 /* Link it in. */
1023 next_frame->prev = prev;
1024 prev->next = next_frame;
1025 prev->frame = address;
1026 prev->level = next_frame->level + 1;
1027 /* FIXME: cagney/2002-11-18: Should be setting the frame's type
1028 here, before anything else, and not last. Various INIT functions
1029 are full of work-arounds for the frames type not being set
1030 correctly from the word go. Ulgh! */
1031 prev->type = NORMAL_FRAME;
1032
1033 /* This change should not be needed, FIXME! We should determine
1034 whether any targets *need* DEPRECATED_INIT_FRAME_PC to happen
1035 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
1036 express what goes on here.
1037
1038 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
1039 (where the PC is already set up) and here (where it isn't).
1040 DEPRECATED_INIT_FRAME_PC is only called from here, always after
1041 INIT_EXTRA_FRAME_INFO.
1042
1043 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the
1044 PC value (which hasn't been set yet). Some other machines appear
1045 to require INIT_EXTRA_FRAME_INFO before they can do
1046 DEPRECATED_INIT_FRAME_PC. Phoo.
1047
1048 We shouldn't need DEPRECATED_INIT_FRAME_PC_FIRST to add more
1049 complication to an already overcomplicated part of GDB.
1050 gnu@cygnus.com, 15Sep92.
1051
1052 Assuming that some machines need DEPRECATED_INIT_FRAME_PC after
1053 INIT_EXTRA_FRAME_INFO, one possible scheme:
1054
1055 SETUP_INNERMOST_FRAME(): Default version is just create_new_frame
1056 (read_fp ()), read_pc ()). Machines with extra frame info would
1057 do that (or the local equivalent) and then set the extra fields.
1058
1059 SETUP_ARBITRARY_FRAME(argc, argv): Only change here is that
1060 create_new_frame would no longer init extra frame info;
1061 SETUP_ARBITRARY_FRAME would have to do that.
1062
1063 INIT_PREV_FRAME(fromleaf, prev) Replace INIT_EXTRA_FRAME_INFO and
1064 DEPRECATED_INIT_FRAME_PC. This should also return a flag saying
1065 whether to keep the new frame, or whether to discard it, because
1066 on some machines (e.g. mips) it is really awkward to have
1067 FRAME_CHAIN_VALID called *before* INIT_EXTRA_FRAME_INFO (there is
1068 no good way to get information deduced in FRAME_CHAIN_VALID into
1069 the extra fields of the new frame). std_frame_pc(fromleaf, prev)
1070
1071 This is the default setting for INIT_PREV_FRAME. It just does
1072 what the default DEPRECATED_INIT_FRAME_PC does. Some machines
1073 will call it from INIT_PREV_FRAME (either at the beginning, the
1074 end, or in the middle). Some machines won't use it.
1075
1076 kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */
1077
1078 /* NOTE: cagney/2002-11-09: Just ignore the above! There is no
1079 reason for things to be this complicated.
1080
1081 The trick is to assume that there is always a frame. Instead of
1082 special casing the inner-most frame, create fake frame
1083 (containing the hardware registers) that is inner to the
1084 user-visible inner-most frame (...) and then unwind from that.
1085 That way architecture code can use use the standard
1086 frame_XX_unwind() functions and not differentiate between the
1087 inner most and any other case.
1088
1089 Since there is always a frame to unwind from, there is always
1090 somewhere (NEXT_FRAME) to store all the info needed to construct
1091 a new (previous) frame without having to first create it. This
1092 means that the convolution below - needing to carefully order a
1093 frame's initialization - isn't needed.
1094
1095 The irony here though, is that FRAME_CHAIN(), at least for a more
1096 up-to-date architecture, always calls FRAME_SAVED_PC(), and
1097 FRAME_SAVED_PC() computes the PC but without first needing the
1098 frame! Instead of the convolution below, we could have simply
1099 called FRAME_SAVED_PC() and been done with it! Note that
1100 FRAME_SAVED_PC() is being superseed by frame_pc_unwind() and that
1101 function does have somewhere to cache that PC value. */
1102
1103 if (DEPRECATED_INIT_FRAME_PC_FIRST_P ())
1104 prev->pc = (DEPRECATED_INIT_FRAME_PC_FIRST (fromleaf, prev));
1105
1106 if (INIT_EXTRA_FRAME_INFO_P ())
1107 INIT_EXTRA_FRAME_INFO (fromleaf, prev);
1108
1109 /* This entry is in the frame queue now, which is good since
1110 FRAME_SAVED_PC may use that queue to figure out its value (see
1111 tm-sparc.h). We want the pc saved in the inferior frame. */
1112 if (DEPRECATED_INIT_FRAME_PC_P ())
1113 prev->pc = DEPRECATED_INIT_FRAME_PC (fromleaf, prev);
1114
1115 /* If ->frame and ->pc are unchanged, we are in the process of
1116 getting ourselves into an infinite backtrace. Some architectures
1117 check this in FRAME_CHAIN or thereabouts, but it seems like there
1118 is no reason this can't be an architecture-independent check. */
1119 if (prev->frame == next_frame->frame
1120 && prev->pc == next_frame->pc)
1121 {
1122 next_frame->prev = NULL;
1123 obstack_free (&frame_cache_obstack, prev);
1124 return NULL;
1125 }
1126
1127 /* Initialize the code used to unwind the frame PREV based on the PC
1128 (and probably other architectural information). The PC lets you
1129 check things like the debug info at that point (dwarf2cfi?) and
1130 use that to decide how the frame should be unwound. */
1131 prev->unwind = frame_unwind_find_by_pc (current_gdbarch, prev->pc);
1132
1133 /* NOTE: cagney/2002-11-18: The code segments, found in
1134 create_new_frame and get_prev_frame(), that initializes the
1135 frames type is subtly different. The latter only updates ->type
1136 when it encounters a SIGTRAMP_FRAME or DUMMY_FRAME. This stops
1137 get_prev_frame() overriding the frame's type when the INIT code
1138 has previously set it. This is really somewhat bogus. The
1139 initialization, as seen in create_new_frame(), should occur
1140 before the INIT function has been called. */
1141 if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
1142 && (DEPRECATED_PC_IN_CALL_DUMMY_P ()
1143 ? DEPRECATED_PC_IN_CALL_DUMMY (prev->pc, 0, 0)
1144 : pc_in_dummy_frame (prev->pc)))
1145 prev->type = DUMMY_FRAME;
1146 else
1147 {
1148 /* FIXME: cagney/2002-11-10: This should be moved to before the
1149 INIT code above so that the INIT code knows what the frame's
1150 type is (in fact, for a [generic] dummy-frame, the type can
1151 be set and then the entire initialization can be skipped.
1152 Unforunatly, its the INIT code that sets the PC (Hmm, catch
1153 22). */
1154 char *name;
1155 find_pc_partial_function (prev->pc, &name, NULL, NULL);
1156 if (PC_IN_SIGTRAMP (prev->pc, name))
1157 prev->type = SIGTRAMP_FRAME;
1158 /* FIXME: cagney/2002-11-11: Leave prev->type alone. Some
1159 architectures are forcing the frame's type in INIT so we
1160 don't want to override it here. Remember, NORMAL_FRAME == 0,
1161 so it all works (just :-/). Once this initialization is
1162 moved to the start of this function, all this nastness will
1163 go away. */
1164 }
1165
1166 return prev;
1167 }
1168
1169 CORE_ADDR
1170 get_frame_pc (struct frame_info *frame)
1171 {
1172 return frame->pc;
1173 }
1174
1175 static int
1176 pc_notcurrent (struct frame_info *frame)
1177 {
1178 /* If FRAME is not the innermost frame, that normally means that
1179 FRAME->pc points at the return instruction (which is *after* the
1180 call instruction), and we want to get the line containing the
1181 call (because the call is where the user thinks the program is).
1182 However, if the next frame is either a SIGTRAMP_FRAME or a
1183 DUMMY_FRAME, then the next frame will contain a saved interrupt
1184 PC and such a PC indicates the current (rather than next)
1185 instruction/line, consequently, for such cases, want to get the
1186 line containing fi->pc. */
1187 struct frame_info *next = get_next_frame (frame);
1188 int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME);
1189 return notcurrent;
1190 }
1191
1192 void
1193 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1194 {
1195 (*sal) = find_pc_line (frame->pc, pc_notcurrent (frame));
1196 }
1197
1198 /* Per "frame.h", return the ``address'' of the frame. Code should
1199 really be using get_frame_id(). */
1200 CORE_ADDR
1201 get_frame_base (struct frame_info *fi)
1202 {
1203 return fi->frame;
1204 }
1205
1206 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1207 or -1 for a NULL frame. */
1208
1209 int
1210 frame_relative_level (struct frame_info *fi)
1211 {
1212 if (fi == NULL)
1213 return -1;
1214 else
1215 return fi->level;
1216 }
1217
1218 enum frame_type
1219 get_frame_type (struct frame_info *frame)
1220 {
1221 /* Some targets still don't use [generic] dummy frames. Catch them
1222 here. */
1223 if (!DEPRECATED_USE_GENERIC_DUMMY_FRAMES
1224 && deprecated_frame_in_dummy (frame))
1225 return DUMMY_FRAME;
1226 return frame->type;
1227 }
1228
1229 void
1230 deprecated_set_frame_type (struct frame_info *frame, enum frame_type type)
1231 {
1232 /* Arrrg! See comment in "frame.h". */
1233 frame->type = type;
1234 }
1235
1236 #ifdef FRAME_FIND_SAVED_REGS
1237 /* XXX - deprecated. This is a compatibility function for targets
1238 that do not yet implement FRAME_INIT_SAVED_REGS. */
1239 /* Find the addresses in which registers are saved in FRAME. */
1240
1241 void
1242 deprecated_get_frame_saved_regs (struct frame_info *frame,
1243 struct frame_saved_regs *saved_regs_addr)
1244 {
1245 if (frame->saved_regs == NULL)
1246 {
1247 frame->saved_regs = (CORE_ADDR *)
1248 frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS);
1249 }
1250 if (saved_regs_addr == NULL)
1251 {
1252 struct frame_saved_regs saved_regs;
1253 FRAME_FIND_SAVED_REGS (frame, saved_regs);
1254 memcpy (frame->saved_regs, &saved_regs, SIZEOF_FRAME_SAVED_REGS);
1255 }
1256 else
1257 {
1258 FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr);
1259 memcpy (frame->saved_regs, saved_regs_addr, SIZEOF_FRAME_SAVED_REGS);
1260 }
1261 }
1262 #endif
1263
1264 struct frame_extra_info *
1265 get_frame_extra_info (struct frame_info *fi)
1266 {
1267 return fi->extra_info;
1268 }
1269
1270 struct frame_extra_info *
1271 frame_extra_info_zalloc (struct frame_info *fi, long size)
1272 {
1273 fi->extra_info = frame_obstack_zalloc (size);
1274 return fi->extra_info;
1275 }
1276
1277 void
1278 deprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc)
1279 {
1280 /* See comment in "frame.h". */
1281 frame->pc = pc;
1282 }
1283
1284 void
1285 deprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base)
1286 {
1287 /* See comment in "frame.h". */
1288 frame->frame = base;
1289 }
1290
1291 void
1292 deprecated_set_frame_saved_regs_hack (struct frame_info *frame,
1293 CORE_ADDR *saved_regs)
1294 {
1295 frame->saved_regs = saved_regs;
1296 }
1297
1298 void
1299 deprecated_set_frame_extra_info_hack (struct frame_info *frame,
1300 struct frame_extra_info *extra_info)
1301 {
1302 frame->extra_info = extra_info;
1303 }
1304
1305 void
1306 deprecated_set_frame_next_hack (struct frame_info *fi,
1307 struct frame_info *next)
1308 {
1309 fi->next = next;
1310 }
1311
1312 void
1313 deprecated_set_frame_prev_hack (struct frame_info *fi,
1314 struct frame_info *prev)
1315 {
1316 fi->prev = prev;
1317 }
1318
1319 struct context *
1320 deprecated_get_frame_context (struct frame_info *fi)
1321 {
1322 return fi->context;
1323 }
1324
1325 void
1326 deprecated_set_frame_context (struct frame_info *fi,
1327 struct context *context)
1328 {
1329 fi->context = context;
1330 }
1331
1332 struct frame_info *
1333 deprecated_frame_xmalloc (void)
1334 {
1335 struct frame_info *frame = XMALLOC (struct frame_info);
1336 memset (frame, 0, sizeof (struct frame_info));
1337 return frame;
1338 }
1339
1340 struct frame_info *
1341 deprecated_frame_xmalloc_with_cleanup (long sizeof_saved_regs,
1342 long sizeof_extra_info)
1343 {
1344 struct frame_info *frame = deprecated_frame_xmalloc ();
1345 make_cleanup (xfree, frame);
1346 if (sizeof_saved_regs > 0)
1347 {
1348 frame->saved_regs = xcalloc (1, sizeof_saved_regs);
1349 make_cleanup (xfree, frame->saved_regs);
1350 }
1351 if (sizeof_extra_info > 0)
1352 {
1353 frame->extra_info = xcalloc (1, sizeof_extra_info);
1354 make_cleanup (xfree, frame->extra_info);
1355 }
1356 return frame;
1357 }
1358
1359 void
1360 _initialize_frame (void)
1361 {
1362 obstack_init (&frame_cache_obstack);
1363 }
This page took 0.212111 seconds and 4 git commands to generate.