1130803Smarcel/* Cache and manage frames for GDB, the GNU debugger.
298944Sobrien
3130803Smarcel   Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4130803Smarcel   2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5130803Smarcel
698944Sobrien   This file is part of GDB.
798944Sobrien
898944Sobrien   This program is free software; you can redistribute it and/or modify
998944Sobrien   it under the terms of the GNU General Public License as published by
1098944Sobrien   the Free Software Foundation; either version 2 of the License, or
1198944Sobrien   (at your option) any later version.
1298944Sobrien
1398944Sobrien   This program is distributed in the hope that it will be useful,
1498944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1598944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1698944Sobrien   GNU General Public License for more details.
1798944Sobrien
1898944Sobrien   You should have received a copy of the GNU General Public License
1998944Sobrien   along with this program; if not, write to the Free Software
2098944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2198944Sobrien   Boston, MA 02111-1307, USA.  */
2298944Sobrien
2398944Sobrien#include "defs.h"
2498944Sobrien#include "frame.h"
2598944Sobrien#include "target.h"
2698944Sobrien#include "value.h"
2798944Sobrien#include "inferior.h"	/* for inferior_ptid */
2898944Sobrien#include "regcache.h"
29130803Smarcel#include "gdb_assert.h"
30130803Smarcel#include "gdb_string.h"
31130803Smarcel#include "user-regs.h"
32130803Smarcel#include "gdb_obstack.h"
33130803Smarcel#include "dummy-frame.h"
34130803Smarcel#include "sentinel-frame.h"
35130803Smarcel#include "gdbcore.h"
36130803Smarcel#include "annotate.h"
37130803Smarcel#include "language.h"
38130803Smarcel#include "frame-unwind.h"
39130803Smarcel#include "frame-base.h"
40130803Smarcel#include "command.h"
41130803Smarcel#include "gdbcmd.h"
4298944Sobrien
43130803Smarcel/* We keep a cache of stack frames, each of which is a "struct
44130803Smarcel   frame_info".  The innermost one gets allocated (in
45130803Smarcel   wait_for_inferior) each time the inferior stops; current_frame
46130803Smarcel   points to it.  Additional frames get allocated (in get_prev_frame)
47130803Smarcel   as needed, and are chained through the next and prev fields.  Any
48130803Smarcel   time that the frame cache becomes invalid (most notably when we
49130803Smarcel   execute something, but also if we change how we interpret the
50130803Smarcel   frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
51130803Smarcel   which reads new symbols)), we should call reinit_frame_cache.  */
5298944Sobrien
53130803Smarcelstruct frame_info
54130803Smarcel{
55130803Smarcel  /* Level of this frame.  The inner-most (youngest) frame is at level
56130803Smarcel     0.  As you move towards the outer-most (oldest) frame, the level
57130803Smarcel     increases.  This is a cached value.  It could just as easily be
58130803Smarcel     computed by counting back from the selected frame to the inner
59130803Smarcel     most frame.  */
60130803Smarcel  /* NOTE: cagney/2002-04-05: Perhaphs a level of ``-1'' should be
61130803Smarcel     reserved to indicate a bogus frame - one that has been created
62130803Smarcel     just to keep GDB happy (GDB always needs a frame).  For the
63130803Smarcel     moment leave this as speculation.  */
64130803Smarcel  int level;
6598944Sobrien
66130803Smarcel  /* The frame's type.  */
67130803Smarcel  /* FIXME: cagney/2003-04-02: Should instead be returning
68130803Smarcel     ->unwind->type.  Unfortunately, legacy code is still explicitly
69130803Smarcel     setting the type using the method deprecated_set_frame_type.
70130803Smarcel     Eliminate that method and this field can be eliminated.  */
71130803Smarcel  enum frame_type type;
72130803Smarcel
73130803Smarcel  /* For each register, address of where it was saved on entry to the
74130803Smarcel     frame, or zero if it was not saved on entry to this frame.  This
75130803Smarcel     includes special registers such as pc and fp saved in special
76130803Smarcel     ways in the stack frame.  The SP_REGNUM is even more special, the
77130803Smarcel     address here is the sp for the previous frame, not the address
78130803Smarcel     where the sp was saved.  */
79130803Smarcel  /* Allocated by frame_saved_regs_zalloc () which is called /
80130803Smarcel     initialized by DEPRECATED_FRAME_INIT_SAVED_REGS(). */
81130803Smarcel  CORE_ADDR *saved_regs;	/*NUM_REGS + NUM_PSEUDO_REGS*/
82130803Smarcel
83130803Smarcel  /* Anything extra for this structure that may have been defined in
84130803Smarcel     the machine dependent files. */
85130803Smarcel  /* Allocated by frame_extra_info_zalloc () which is called /
86130803Smarcel     initialized by DEPRECATED_INIT_EXTRA_FRAME_INFO */
87130803Smarcel  struct frame_extra_info *extra_info;
88130803Smarcel
89130803Smarcel  /* The frame's low-level unwinder and corresponding cache.  The
90130803Smarcel     low-level unwinder is responsible for unwinding register values
91130803Smarcel     for the previous frame.  The low-level unwind methods are
92130803Smarcel     selected based on the presence, or otherwize, of register unwind
93130803Smarcel     information such as CFI.  */
94130803Smarcel  void *prologue_cache;
95130803Smarcel  const struct frame_unwind *unwind;
96130803Smarcel
97130803Smarcel  /* Cached copy of the previous frame's resume address.  */
98130803Smarcel  struct {
99130803Smarcel    int p;
100130803Smarcel    CORE_ADDR value;
101130803Smarcel  } prev_pc;
102130803Smarcel
103130803Smarcel  /* Cached copy of the previous frame's function address.  */
104130803Smarcel  struct
105130803Smarcel  {
106130803Smarcel    CORE_ADDR addr;
107130803Smarcel    int p;
108130803Smarcel  } prev_func;
109130803Smarcel
110130803Smarcel  /* This frame's ID.  */
111130803Smarcel  struct
112130803Smarcel  {
113130803Smarcel    int p;
114130803Smarcel    struct frame_id value;
115130803Smarcel  } this_id;
116130803Smarcel
117130803Smarcel  /* The frame's high-level base methods, and corresponding cache.
118130803Smarcel     The high level base methods are selected based on the frame's
119130803Smarcel     debug info.  */
120130803Smarcel  const struct frame_base *base;
121130803Smarcel  void *base_cache;
122130803Smarcel
123130803Smarcel  /* Pointers to the next (down, inner, younger) and previous (up,
124130803Smarcel     outer, older) frame_info's in the frame cache.  */
125130803Smarcel  struct frame_info *next; /* down, inner, younger */
126130803Smarcel  int prev_p;
127130803Smarcel  struct frame_info *prev; /* up, outer, older */
128130803Smarcel};
129130803Smarcel
130130803Smarcel/* Flag to control debugging.  */
131130803Smarcel
132130803Smarcelstatic int frame_debug;
133130803Smarcel
134130803Smarcel/* Flag to indicate whether backtraces should stop at main et.al.  */
135130803Smarcel
136130803Smarcelstatic int backtrace_past_main;
137130803Smarcelstatic unsigned int backtrace_limit = UINT_MAX;
138130803Smarcel
139130803Smarcel
140130803Smarcelvoid
141130803Smarcelfprint_frame_id (struct ui_file *file, struct frame_id id)
142130803Smarcel{
143130803Smarcel  fprintf_unfiltered (file, "{stack=0x%s,code=0x%s,special=0x%s}",
144130803Smarcel		      paddr_nz (id.stack_addr),
145130803Smarcel		      paddr_nz (id.code_addr),
146130803Smarcel		      paddr_nz (id.special_addr));
147130803Smarcel}
148130803Smarcel
149130803Smarcelstatic void
150130803Smarcelfprint_frame_type (struct ui_file *file, enum frame_type type)
151130803Smarcel{
152130803Smarcel  switch (type)
153130803Smarcel    {
154130803Smarcel    case UNKNOWN_FRAME:
155130803Smarcel      fprintf_unfiltered (file, "UNKNOWN_FRAME");
156130803Smarcel      return;
157130803Smarcel    case NORMAL_FRAME:
158130803Smarcel      fprintf_unfiltered (file, "NORMAL_FRAME");
159130803Smarcel      return;
160130803Smarcel    case DUMMY_FRAME:
161130803Smarcel      fprintf_unfiltered (file, "DUMMY_FRAME");
162130803Smarcel      return;
163130803Smarcel    case SIGTRAMP_FRAME:
164130803Smarcel      fprintf_unfiltered (file, "SIGTRAMP_FRAME");
165130803Smarcel      return;
166130803Smarcel    default:
167130803Smarcel      fprintf_unfiltered (file, "<unknown type>");
168130803Smarcel      return;
169130803Smarcel    };
170130803Smarcel}
171130803Smarcel
172130803Smarcelstatic void
173130803Smarcelfprint_frame (struct ui_file *file, struct frame_info *fi)
174130803Smarcel{
175130803Smarcel  if (fi == NULL)
176130803Smarcel    {
177130803Smarcel      fprintf_unfiltered (file, "<NULL frame>");
178130803Smarcel      return;
179130803Smarcel    }
180130803Smarcel  fprintf_unfiltered (file, "{");
181130803Smarcel  fprintf_unfiltered (file, "level=%d", fi->level);
182130803Smarcel  fprintf_unfiltered (file, ",");
183130803Smarcel  fprintf_unfiltered (file, "type=");
184130803Smarcel  fprint_frame_type (file, fi->type);
185130803Smarcel  fprintf_unfiltered (file, ",");
186130803Smarcel  fprintf_unfiltered (file, "unwind=");
187130803Smarcel  if (fi->unwind != NULL)
188130803Smarcel    gdb_print_host_address (fi->unwind, file);
189130803Smarcel  else
190130803Smarcel    fprintf_unfiltered (file, "<unknown>");
191130803Smarcel  fprintf_unfiltered (file, ",");
192130803Smarcel  fprintf_unfiltered (file, "pc=");
193130803Smarcel  if (fi->next != NULL && fi->next->prev_pc.p)
194130803Smarcel    fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_pc.value));
195130803Smarcel  else
196130803Smarcel    fprintf_unfiltered (file, "<unknown>");
197130803Smarcel  fprintf_unfiltered (file, ",");
198130803Smarcel  fprintf_unfiltered (file, "id=");
199130803Smarcel  if (fi->this_id.p)
200130803Smarcel    fprint_frame_id (file, fi->this_id.value);
201130803Smarcel  else
202130803Smarcel    fprintf_unfiltered (file, "<unknown>");
203130803Smarcel  fprintf_unfiltered (file, ",");
204130803Smarcel  fprintf_unfiltered (file, "func=");
205130803Smarcel  if (fi->next != NULL && fi->next->prev_func.p)
206130803Smarcel    fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_func.addr));
207130803Smarcel  else
208130803Smarcel    fprintf_unfiltered (file, "<unknown>");
209130803Smarcel  fprintf_unfiltered (file, "}");
210130803Smarcel}
211130803Smarcel
212130803Smarcel/* Return a frame uniq ID that can be used to, later, re-find the
213130803Smarcel   frame.  */
214130803Smarcel
215130803Smarcelstruct frame_id
216130803Smarcelget_frame_id (struct frame_info *fi)
217130803Smarcel{
218130803Smarcel  if (fi == NULL)
219130803Smarcel    {
220130803Smarcel      return null_frame_id;
221130803Smarcel    }
222130803Smarcel  if (!fi->this_id.p)
223130803Smarcel    {
224130803Smarcel      gdb_assert (!legacy_frame_p (current_gdbarch));
225130803Smarcel      if (frame_debug)
226130803Smarcel	fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
227130803Smarcel			    fi->level);
228130803Smarcel      /* Find the unwinder.  */
229130803Smarcel      if (fi->unwind == NULL)
230130803Smarcel	{
231130803Smarcel	  fi->unwind = frame_unwind_find_by_frame (fi->next);
232130803Smarcel	  /* FIXME: cagney/2003-04-02: Rather than storing the frame's
233130803Smarcel	     type in the frame, the unwinder's type should be returned
234130803Smarcel	     directly.  Unfortunately, legacy code, called by
235130803Smarcel	     legacy_get_prev_frame, explicitly set the frames type
236130803Smarcel	     using the method deprecated_set_frame_type().  */
237130803Smarcel	  fi->type = fi->unwind->type;
238130803Smarcel	}
239130803Smarcel      /* Find THIS frame's ID.  */
240130803Smarcel      fi->unwind->this_id (fi->next, &fi->prologue_cache, &fi->this_id.value);
241130803Smarcel      fi->this_id.p = 1;
242130803Smarcel      if (frame_debug)
243130803Smarcel	{
244130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "-> ");
245130803Smarcel	  fprint_frame_id (gdb_stdlog, fi->this_id.value);
246130803Smarcel	  fprintf_unfiltered (gdb_stdlog, " }\n");
247130803Smarcel	}
248130803Smarcel    }
249130803Smarcel  return fi->this_id.value;
250130803Smarcel}
251130803Smarcel
252130803Smarcelconst struct frame_id null_frame_id; /* All zeros.  */
253130803Smarcel
254130803Smarcelstruct frame_id
255130803Smarcelframe_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
256130803Smarcel                        CORE_ADDR special_addr)
257130803Smarcel{
258130803Smarcel  struct frame_id id;
259130803Smarcel  id.stack_addr = stack_addr;
260130803Smarcel  id.code_addr = code_addr;
261130803Smarcel  id.special_addr = special_addr;
262130803Smarcel  return id;
263130803Smarcel}
264130803Smarcel
265130803Smarcelstruct frame_id
266130803Smarcelframe_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
267130803Smarcel{
268130803Smarcel  return frame_id_build_special (stack_addr, code_addr, 0);
269130803Smarcel}
270130803Smarcel
271130803Smarcelint
272130803Smarcelframe_id_p (struct frame_id l)
273130803Smarcel{
274130803Smarcel  int p;
275130803Smarcel  /* The .code can be NULL but the .stack cannot.  */
276130803Smarcel  p = (l.stack_addr != 0);
277130803Smarcel  if (frame_debug)
278130803Smarcel    {
279130803Smarcel      fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
280130803Smarcel      fprint_frame_id (gdb_stdlog, l);
281130803Smarcel      fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
282130803Smarcel    }
283130803Smarcel  return p;
284130803Smarcel}
285130803Smarcel
286130803Smarcelint
287130803Smarcelframe_id_eq (struct frame_id l, struct frame_id r)
288130803Smarcel{
289130803Smarcel  int eq;
290130803Smarcel  if (l.stack_addr == 0 || r.stack_addr == 0)
291130803Smarcel    /* Like a NaN, if either ID is invalid, the result is false.  */
292130803Smarcel    eq = 0;
293130803Smarcel  else if (l.stack_addr != r.stack_addr)
294130803Smarcel    /* If .stack addresses are different, the frames are different.  */
295130803Smarcel    eq = 0;
296130803Smarcel  else if (l.code_addr == 0 || r.code_addr == 0)
297130803Smarcel    /* A zero code addr is a wild card, always succeed.  */
298130803Smarcel    eq = 1;
299130803Smarcel  else if (l.code_addr != r.code_addr)
300130803Smarcel    /* If .code addresses are different, the frames are different.  */
301130803Smarcel    eq = 0;
302130803Smarcel  else if (l.special_addr == 0 || r.special_addr == 0)
303130803Smarcel    /* A zero special addr is a wild card (or unused), always succeed.  */
304130803Smarcel    eq = 1;
305130803Smarcel  else if (l.special_addr == r.special_addr)
306130803Smarcel    /* Frames are equal.  */
307130803Smarcel    eq = 1;
308130803Smarcel  else
309130803Smarcel    /* No luck.  */
310130803Smarcel    eq = 0;
311130803Smarcel  if (frame_debug)
312130803Smarcel    {
313130803Smarcel      fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
314130803Smarcel      fprint_frame_id (gdb_stdlog, l);
315130803Smarcel      fprintf_unfiltered (gdb_stdlog, ",r=");
316130803Smarcel      fprint_frame_id (gdb_stdlog, r);
317130803Smarcel      fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
318130803Smarcel    }
319130803Smarcel  return eq;
320130803Smarcel}
321130803Smarcel
322130803Smarcelint
323130803Smarcelframe_id_inner (struct frame_id l, struct frame_id r)
324130803Smarcel{
325130803Smarcel  int inner;
326130803Smarcel  if (l.stack_addr == 0 || r.stack_addr == 0)
327130803Smarcel    /* Like NaN, any operation involving an invalid ID always fails.  */
328130803Smarcel    inner = 0;
329130803Smarcel  else
330130803Smarcel    /* Only return non-zero when strictly inner than.  Note that, per
331130803Smarcel       comment in "frame.h", there is some fuzz here.  Frameless
332130803Smarcel       functions are not strictly inner than (same .stack but
333130803Smarcel       different .code and/or .special address).  */
334130803Smarcel    inner = INNER_THAN (l.stack_addr, r.stack_addr);
335130803Smarcel  if (frame_debug)
336130803Smarcel    {
337130803Smarcel      fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
338130803Smarcel      fprint_frame_id (gdb_stdlog, l);
339130803Smarcel      fprintf_unfiltered (gdb_stdlog, ",r=");
340130803Smarcel      fprint_frame_id (gdb_stdlog, r);
341130803Smarcel      fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
342130803Smarcel    }
343130803Smarcel  return inner;
344130803Smarcel}
345130803Smarcel
346130803Smarcelstruct frame_info *
347130803Smarcelframe_find_by_id (struct frame_id id)
348130803Smarcel{
349130803Smarcel  struct frame_info *frame;
350130803Smarcel
351130803Smarcel  /* ZERO denotes the null frame, let the caller decide what to do
352130803Smarcel     about it.  Should it instead return get_current_frame()?  */
353130803Smarcel  if (!frame_id_p (id))
354130803Smarcel    return NULL;
355130803Smarcel
356130803Smarcel  for (frame = get_current_frame ();
357130803Smarcel       frame != NULL;
358130803Smarcel       frame = get_prev_frame (frame))
359130803Smarcel    {
360130803Smarcel      struct frame_id this = get_frame_id (frame);
361130803Smarcel      if (frame_id_eq (id, this))
362130803Smarcel	/* An exact match.  */
363130803Smarcel	return frame;
364130803Smarcel      if (frame_id_inner (id, this))
365130803Smarcel	/* Gone to far.  */
366130803Smarcel	return NULL;
367130803Smarcel      /* Either, we're not yet gone far enough out along the frame
368130803Smarcel         chain (inner(this,id), or we're comparing frameless functions
369130803Smarcel         (same .base, different .func, no test available).  Struggle
370130803Smarcel         on until we've definitly gone to far.  */
371130803Smarcel    }
372130803Smarcel  return NULL;
373130803Smarcel}
374130803Smarcel
37598944SobrienCORE_ADDR
376130803Smarcelframe_pc_unwind (struct frame_info *this_frame)
37798944Sobrien{
378130803Smarcel  if (!this_frame->prev_pc.p)
379130803Smarcel    {
380130803Smarcel      CORE_ADDR pc;
381130803Smarcel      if (gdbarch_unwind_pc_p (current_gdbarch))
382130803Smarcel	{
383130803Smarcel	  /* The right way.  The `pure' way.  The one true way.  This
384130803Smarcel	     method depends solely on the register-unwind code to
385130803Smarcel	     determine the value of registers in THIS frame, and hence
386130803Smarcel	     the value of this frame's PC (resume address).  A typical
387130803Smarcel	     implementation is no more than:
388130803Smarcel
389130803Smarcel	     frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
390130803Smarcel	     return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
39198944Sobrien
392130803Smarcel	     Note: this method is very heavily dependent on a correct
393130803Smarcel	     register-unwind implementation, it pays to fix that
394130803Smarcel	     method first; this method is frame type agnostic, since
395130803Smarcel	     it only deals with register values, it works with any
396130803Smarcel	     frame.  This is all in stark contrast to the old
397130803Smarcel	     FRAME_SAVED_PC which would try to directly handle all the
398130803Smarcel	     different ways that a PC could be unwound.  */
399130803Smarcel	  pc = gdbarch_unwind_pc (current_gdbarch, this_frame);
400130803Smarcel	}
401130803Smarcel      else if (this_frame->level < 0)
402130803Smarcel	{
403130803Smarcel	  /* FIXME: cagney/2003-03-06: Old code and and a sentinel
404130803Smarcel             frame.  Do like was always done.  Fetch the PC's value
405130803Smarcel             direct from the global registers array (via read_pc).
406130803Smarcel             This assumes that this frame belongs to the current
407130803Smarcel             global register cache.  The assumption is dangerous.  */
408130803Smarcel	  pc = read_pc ();
409130803Smarcel	}
410130803Smarcel      else if (DEPRECATED_FRAME_SAVED_PC_P ())
411130803Smarcel	{
412130803Smarcel	  /* FIXME: cagney/2003-03-06: Old code, but not a sentinel
413130803Smarcel             frame.  Do like was always done.  Note that this method,
414130803Smarcel             unlike unwind_pc(), tries to handle all the different
415130803Smarcel             frame cases directly.  It fails.  */
416130803Smarcel	  pc = DEPRECATED_FRAME_SAVED_PC (this_frame);
417130803Smarcel	}
418130803Smarcel      else
419130803Smarcel	internal_error (__FILE__, __LINE__, "No gdbarch_unwind_pc method");
420130803Smarcel      this_frame->prev_pc.value = pc;
421130803Smarcel      this_frame->prev_pc.p = 1;
422130803Smarcel      if (frame_debug)
423130803Smarcel	fprintf_unfiltered (gdb_stdlog,
424130803Smarcel			    "{ frame_pc_unwind (this_frame=%d) -> 0x%s }\n",
425130803Smarcel			    this_frame->level,
426130803Smarcel			    paddr_nz (this_frame->prev_pc.value));
427130803Smarcel    }
428130803Smarcel  return this_frame->prev_pc.value;
429130803Smarcel}
43098944Sobrien
431130803SmarcelCORE_ADDR
432130803Smarcelframe_func_unwind (struct frame_info *fi)
433130803Smarcel{
434130803Smarcel  if (!fi->prev_func.p)
435130803Smarcel    {
436130803Smarcel      /* Make certain that this, and not the adjacent, function is
437130803Smarcel         found.  */
438130803Smarcel      CORE_ADDR addr_in_block = frame_unwind_address_in_block (fi);
439130803Smarcel      fi->prev_func.p = 1;
440130803Smarcel      fi->prev_func.addr = get_pc_function_start (addr_in_block);
441130803Smarcel      if (frame_debug)
442130803Smarcel	fprintf_unfiltered (gdb_stdlog,
443130803Smarcel			    "{ frame_func_unwind (fi=%d) -> 0x%s }\n",
444130803Smarcel			    fi->level, paddr_nz (fi->prev_func.addr));
445130803Smarcel    }
446130803Smarcel  return fi->prev_func.addr;
447130803Smarcel}
44898944Sobrien
449130803SmarcelCORE_ADDR
450130803Smarcelget_frame_func (struct frame_info *fi)
451130803Smarcel{
452130803Smarcel  return frame_func_unwind (fi->next);
453130803Smarcel}
45498944Sobrien
455130803Smarcelstatic int
456130803Smarceldo_frame_unwind_register (void *src, int regnum, void *buf)
457130803Smarcel{
458130803Smarcel  frame_unwind_register (src, regnum, buf);
459130803Smarcel  return 1;
460130803Smarcel}
461130803Smarcel
462130803Smarcelvoid
463130803Smarcelframe_pop (struct frame_info *this_frame)
464130803Smarcel{
465130803Smarcel  struct regcache *scratch_regcache;
466130803Smarcel  struct cleanup *cleanups;
467130803Smarcel
468130803Smarcel  if (DEPRECATED_POP_FRAME_P ())
46998944Sobrien    {
470130803Smarcel      /* A legacy architecture that has implemented a custom pop
471130803Smarcel	 function.  All new architectures should instead be using the
472130803Smarcel	 generic code below.  */
473130803Smarcel      DEPRECATED_POP_FRAME;
474130803Smarcel    }
475130803Smarcel  else
476130803Smarcel    {
477130803Smarcel      /* Make a copy of all the register values unwound from this
478130803Smarcel	 frame.  Save them in a scratch buffer so that there isn't a
479130803Smarcel	 race betweening trying to extract the old values from the
480130803Smarcel	 current_regcache while, at the same time writing new values
481130803Smarcel	 into that same cache.  */
482130803Smarcel      struct regcache *scratch = regcache_xmalloc (current_gdbarch);
483130803Smarcel      struct cleanup *cleanups = make_cleanup_regcache_xfree (scratch);
484130803Smarcel      regcache_save (scratch, do_frame_unwind_register, this_frame);
485130803Smarcel      /* FIXME: cagney/2003-03-16: It should be possible to tell the
486130803Smarcel         target's register cache that it is about to be hit with a
487130803Smarcel         burst register transfer and that the sequence of register
488130803Smarcel         writes should be batched.  The pair target_prepare_to_store()
489130803Smarcel         and target_store_registers() kind of suggest this
490130803Smarcel         functionality.  Unfortunately, they don't implement it.  Their
491130803Smarcel         lack of a formal definition can lead to targets writing back
492130803Smarcel         bogus values (arguably a bug in the target code mind).  */
493130803Smarcel      /* Now copy those saved registers into the current regcache.
494130803Smarcel         Here, regcache_cpy() calls regcache_restore().  */
495130803Smarcel      regcache_cpy (current_regcache, scratch);
496130803Smarcel      do_cleanups (cleanups);
497130803Smarcel    }
498130803Smarcel  /* We've made right mess of GDB's local state, just discard
499130803Smarcel     everything.  */
500130803Smarcel  flush_cached_frames ();
501130803Smarcel}
50298944Sobrien
503130803Smarcelvoid
504130803Smarcelframe_register_unwind (struct frame_info *frame, int regnum,
505130803Smarcel		       int *optimizedp, enum lval_type *lvalp,
506130803Smarcel		       CORE_ADDR *addrp, int *realnump, void *bufferp)
507130803Smarcel{
508130803Smarcel  struct frame_unwind_cache *cache;
50998944Sobrien
510130803Smarcel  if (frame_debug)
511130803Smarcel    {
512130803Smarcel      fprintf_unfiltered (gdb_stdlog, "\
513130803Smarcel{ frame_register_unwind (frame=%d,regnum=%d(%s),...) ",
514130803Smarcel			  frame->level, regnum,
515130803Smarcel			  frame_map_regnum_to_name (frame, regnum));
51698944Sobrien    }
51798944Sobrien
518130803Smarcel  /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
519130803Smarcel     that the value proper does not need to be fetched.  */
520130803Smarcel  gdb_assert (optimizedp != NULL);
521130803Smarcel  gdb_assert (lvalp != NULL);
522130803Smarcel  gdb_assert (addrp != NULL);
523130803Smarcel  gdb_assert (realnump != NULL);
524130803Smarcel  /* gdb_assert (bufferp != NULL); */
525130803Smarcel
526130803Smarcel  /* NOTE: cagney/2002-11-27: A program trying to unwind a NULL frame
527130803Smarcel     is broken.  There is always a frame.  If there, for some reason,
528130803Smarcel     isn't, there is some pretty busted code as it should have
529130803Smarcel     detected the problem before calling here.  */
530130803Smarcel  gdb_assert (frame != NULL);
531130803Smarcel
532130803Smarcel  /* Find the unwinder.  */
533130803Smarcel  if (frame->unwind == NULL)
53498944Sobrien    {
535130803Smarcel      frame->unwind = frame_unwind_find_by_frame (frame->next);
536130803Smarcel      /* FIXME: cagney/2003-04-02: Rather than storing the frame's
537130803Smarcel	 type in the frame, the unwinder's type should be returned
538130803Smarcel	 directly.  Unfortunately, legacy code, called by
539130803Smarcel	 legacy_get_prev_frame, explicitly set the frames type using
540130803Smarcel	 the method deprecated_set_frame_type().  */
541130803Smarcel      frame->type = frame->unwind->type;
542130803Smarcel    }
543130803Smarcel
544130803Smarcel  /* Ask this frame to unwind its register.  See comment in
545130803Smarcel     "frame-unwind.h" for why NEXT frame and this unwind cace are
546130803Smarcel     passed in.  */
547130803Smarcel  frame->unwind->prev_register (frame->next, &frame->prologue_cache, regnum,
548130803Smarcel				optimizedp, lvalp, addrp, realnump, bufferp);
549130803Smarcel
550130803Smarcel  if (frame_debug)
551130803Smarcel    {
552130803Smarcel      fprintf_unfiltered (gdb_stdlog, "->");
553130803Smarcel      fprintf_unfiltered (gdb_stdlog, " *optimizedp=%d", (*optimizedp));
554130803Smarcel      fprintf_unfiltered (gdb_stdlog, " *lvalp=%d", (int) (*lvalp));
555130803Smarcel      fprintf_unfiltered (gdb_stdlog, " *addrp=0x%s", paddr_nz ((*addrp)));
556130803Smarcel      fprintf_unfiltered (gdb_stdlog, " *bufferp=");
557130803Smarcel      if (bufferp == NULL)
558130803Smarcel	fprintf_unfiltered (gdb_stdlog, "<NULL>");
559130803Smarcel      else
560130803Smarcel	{
561130803Smarcel	  int i;
562130803Smarcel	  const unsigned char *buf = bufferp;
563130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "[");
564130803Smarcel	  for (i = 0; i < register_size (current_gdbarch, regnum); i++)
565130803Smarcel	    fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
566130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "]");
567130803Smarcel	}
568130803Smarcel      fprintf_unfiltered (gdb_stdlog, " }\n");
569130803Smarcel    }
570130803Smarcel}
571130803Smarcel
572130803Smarcelvoid
573130803Smarcelframe_register (struct frame_info *frame, int regnum,
574130803Smarcel		int *optimizedp, enum lval_type *lvalp,
575130803Smarcel		CORE_ADDR *addrp, int *realnump, void *bufferp)
576130803Smarcel{
577130803Smarcel  /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
578130803Smarcel     that the value proper does not need to be fetched.  */
579130803Smarcel  gdb_assert (optimizedp != NULL);
580130803Smarcel  gdb_assert (lvalp != NULL);
581130803Smarcel  gdb_assert (addrp != NULL);
582130803Smarcel  gdb_assert (realnump != NULL);
583130803Smarcel  /* gdb_assert (bufferp != NULL); */
584130803Smarcel
585130803Smarcel  /* Ulgh!  Old code that, for lval_register, sets ADDRP to the offset
586130803Smarcel     of the register in the register cache.  It should instead return
587130803Smarcel     the REGNUM corresponding to that register.  Translate the .  */
588130803Smarcel  if (DEPRECATED_GET_SAVED_REGISTER_P ())
589130803Smarcel    {
590130803Smarcel      DEPRECATED_GET_SAVED_REGISTER (bufferp, optimizedp, addrp, frame,
591130803Smarcel				     regnum, lvalp);
592130803Smarcel      /* Compute the REALNUM if the caller wants it.  */
593130803Smarcel      if (*lvalp == lval_register)
594130803Smarcel	{
595130803Smarcel	  int regnum;
596130803Smarcel	  for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
597130803Smarcel	    {
598130803Smarcel	      if (*addrp == register_offset_hack (current_gdbarch, regnum))
599130803Smarcel		{
600130803Smarcel		  *realnump = regnum;
601130803Smarcel		  return;
602130803Smarcel		}
603130803Smarcel	    }
604130803Smarcel	  internal_error (__FILE__, __LINE__,
605130803Smarcel			  "Failed to compute the register number corresponding"
606130803Smarcel			  " to 0x%s", paddr_d (*addrp));
607130803Smarcel	}
608130803Smarcel      *realnump = -1;
609130803Smarcel      return;
610130803Smarcel    }
611130803Smarcel
612130803Smarcel  /* Obtain the register value by unwinding the register from the next
613130803Smarcel     (more inner frame).  */
614130803Smarcel  gdb_assert (frame != NULL && frame->next != NULL);
615130803Smarcel  frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
616130803Smarcel			 realnump, bufferp);
617130803Smarcel}
618130803Smarcel
619130803Smarcelvoid
620130803Smarcelframe_unwind_register (struct frame_info *frame, int regnum, void *buf)
621130803Smarcel{
622130803Smarcel  int optimized;
623130803Smarcel  CORE_ADDR addr;
624130803Smarcel  int realnum;
625130803Smarcel  enum lval_type lval;
626130803Smarcel  frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
627130803Smarcel			 &realnum, buf);
628130803Smarcel}
629130803Smarcel
630130803Smarcelvoid
631130803Smarcelget_frame_register (struct frame_info *frame,
632130803Smarcel		    int regnum, void *buf)
633130803Smarcel{
634130803Smarcel  frame_unwind_register (frame->next, regnum, buf);
635130803Smarcel}
636130803Smarcel
637130803SmarcelLONGEST
638130803Smarcelframe_unwind_register_signed (struct frame_info *frame, int regnum)
639130803Smarcel{
640130803Smarcel  char buf[MAX_REGISTER_SIZE];
641130803Smarcel  frame_unwind_register (frame, regnum, buf);
642130803Smarcel  return extract_signed_integer (buf, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
643130803Smarcel}
644130803Smarcel
645130803SmarcelLONGEST
646130803Smarcelget_frame_register_signed (struct frame_info *frame, int regnum)
647130803Smarcel{
648130803Smarcel  return frame_unwind_register_signed (frame->next, regnum);
649130803Smarcel}
650130803Smarcel
651130803SmarcelULONGEST
652130803Smarcelframe_unwind_register_unsigned (struct frame_info *frame, int regnum)
653130803Smarcel{
654130803Smarcel  char buf[MAX_REGISTER_SIZE];
655130803Smarcel  frame_unwind_register (frame, regnum, buf);
656130803Smarcel  return extract_unsigned_integer (buf, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
657130803Smarcel}
658130803Smarcel
659130803SmarcelULONGEST
660130803Smarcelget_frame_register_unsigned (struct frame_info *frame, int regnum)
661130803Smarcel{
662130803Smarcel  return frame_unwind_register_unsigned (frame->next, regnum);
663130803Smarcel}
664130803Smarcel
665130803Smarcelvoid
666130803Smarcelframe_unwind_unsigned_register (struct frame_info *frame, int regnum,
667130803Smarcel				ULONGEST *val)
668130803Smarcel{
669130803Smarcel  char buf[MAX_REGISTER_SIZE];
670130803Smarcel  frame_unwind_register (frame, regnum, buf);
671130803Smarcel  (*val) = extract_unsigned_integer (buf, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
672130803Smarcel}
673130803Smarcel
674130803Smarcelvoid
675130803Smarcelput_frame_register (struct frame_info *frame, int regnum, const void *buf)
676130803Smarcel{
677130803Smarcel  struct gdbarch *gdbarch = get_frame_arch (frame);
678130803Smarcel  int realnum;
679130803Smarcel  int optim;
680130803Smarcel  enum lval_type lval;
681130803Smarcel  CORE_ADDR addr;
682130803Smarcel  frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
683130803Smarcel  if (optim)
684130803Smarcel    error ("Attempt to assign to a value that was optimized out.");
685130803Smarcel  switch (lval)
686130803Smarcel    {
687130803Smarcel    case lval_memory:
688130803Smarcel      {
689130803Smarcel	/* FIXME: write_memory doesn't yet take constant buffers.
690130803Smarcel           Arrrg!  */
691130803Smarcel	char tmp[MAX_REGISTER_SIZE];
692130803Smarcel	memcpy (tmp, buf, register_size (gdbarch, regnum));
693130803Smarcel	write_memory (addr, tmp, register_size (gdbarch, regnum));
69498944Sobrien	break;
695130803Smarcel      }
696130803Smarcel    case lval_register:
697130803Smarcel      regcache_cooked_write (current_regcache, realnum, buf);
698130803Smarcel      break;
699130803Smarcel    default:
700130803Smarcel      error ("Attempt to assign to an unmodifiable value.");
70198944Sobrien    }
702130803Smarcel}
70398944Sobrien
704130803Smarcel/* frame_register_read ()
705130803Smarcel
706130803Smarcel   Find and return the value of REGNUM for the specified stack frame.
707130803Smarcel   The number of bytes copied is DEPRECATED_REGISTER_RAW_SIZE
708130803Smarcel   (REGNUM).
709130803Smarcel
710130803Smarcel   Returns 0 if the register value could not be found.  */
711130803Smarcel
712130803Smarcelint
713130803Smarcelframe_register_read (struct frame_info *frame, int regnum, void *myaddr)
714130803Smarcel{
715130803Smarcel  int optimized;
716130803Smarcel  enum lval_type lval;
717130803Smarcel  CORE_ADDR addr;
718130803Smarcel  int realnum;
719130803Smarcel  frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
720130803Smarcel
721130803Smarcel  /* FIXME: cagney/2002-05-15: This test, is just bogus.
722130803Smarcel
723130803Smarcel     It indicates that the target failed to supply a value for a
724130803Smarcel     register because it was "not available" at this time.  Problem
725130803Smarcel     is, the target still has the register and so get saved_register()
726130803Smarcel     may be returning a value saved on the stack.  */
727130803Smarcel
728130803Smarcel  if (register_cached (regnum) < 0)
729130803Smarcel    return 0;			/* register value not available */
730130803Smarcel
731130803Smarcel  return !optimized;
73298944Sobrien}
73398944Sobrien
73498944Sobrien
735130803Smarcel/* Map between a frame register number and its name.  A frame register
736130803Smarcel   space is a superset of the cooked register space --- it also
737130803Smarcel   includes builtin registers.  */
738130803Smarcel
739130803Smarcelint
740130803Smarcelframe_map_name_to_regnum (struct frame_info *frame, const char *name, int len)
741130803Smarcel{
742130803Smarcel  return user_reg_map_name_to_regnum (get_frame_arch (frame), name, len);
743130803Smarcel}
744130803Smarcel
745130803Smarcelconst char *
746130803Smarcelframe_map_regnum_to_name (struct frame_info *frame, int regnum)
747130803Smarcel{
748130803Smarcel  return user_reg_map_regnum_to_name (get_frame_arch (frame), regnum);
749130803Smarcel}
750130803Smarcel
751130803Smarcel/* Create a sentinel frame.  */
752130803Smarcel
753130803Smarcelstatic struct frame_info *
754130803Smarcelcreate_sentinel_frame (struct regcache *regcache)
755130803Smarcel{
756130803Smarcel  struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
757130803Smarcel  frame->type = NORMAL_FRAME;
758130803Smarcel  frame->level = -1;
759130803Smarcel  /* Explicitly initialize the sentinel frame's cache.  Provide it
760130803Smarcel     with the underlying regcache.  In the future additional
761130803Smarcel     information, such as the frame's thread will be added.  */
762130803Smarcel  frame->prologue_cache = sentinel_frame_cache (regcache);
763130803Smarcel  /* For the moment there is only one sentinel frame implementation.  */
764130803Smarcel  frame->unwind = sentinel_frame_unwind;
765130803Smarcel  /* Link this frame back to itself.  The frame is self referential
766130803Smarcel     (the unwound PC is the same as the pc), so make it so.  */
767130803Smarcel  frame->next = frame;
768130803Smarcel  /* Make the sentinel frame's ID valid, but invalid.  That way all
769130803Smarcel     comparisons with it should fail.  */
770130803Smarcel  frame->this_id.p = 1;
771130803Smarcel  frame->this_id.value = null_frame_id;
772130803Smarcel  if (frame_debug)
773130803Smarcel    {
774130803Smarcel      fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
775130803Smarcel      fprint_frame (gdb_stdlog, frame);
776130803Smarcel      fprintf_unfiltered (gdb_stdlog, " }\n");
777130803Smarcel    }
778130803Smarcel  return frame;
779130803Smarcel}
780130803Smarcel
781130803Smarcel/* Info about the innermost stack frame (contents of FP register) */
782130803Smarcel
783130803Smarcelstatic struct frame_info *current_frame;
784130803Smarcel
785130803Smarcel/* Cache for frame addresses already read by gdb.  Valid only while
786130803Smarcel   inferior is stopped.  Control variables for the frame cache should
787130803Smarcel   be local to this module.  */
788130803Smarcel
789130803Smarcelstatic struct obstack frame_cache_obstack;
790130803Smarcel
791130803Smarcelvoid *
792130803Smarcelframe_obstack_zalloc (unsigned long size)
793130803Smarcel{
794130803Smarcel  void *data = obstack_alloc (&frame_cache_obstack, size);
795130803Smarcel  memset (data, 0, size);
796130803Smarcel  return data;
797130803Smarcel}
798130803Smarcel
799130803SmarcelCORE_ADDR *
800130803Smarcelframe_saved_regs_zalloc (struct frame_info *fi)
801130803Smarcel{
802130803Smarcel  fi->saved_regs = (CORE_ADDR *)
803130803Smarcel    frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS);
804130803Smarcel  return fi->saved_regs;
805130803Smarcel}
806130803Smarcel
807130803SmarcelCORE_ADDR *
808130803Smarceldeprecated_get_frame_saved_regs (struct frame_info *fi)
809130803Smarcel{
810130803Smarcel  return fi->saved_regs;
811130803Smarcel}
812130803Smarcel
813130803Smarcel/* Return the innermost (currently executing) stack frame.  This is
814130803Smarcel   split into two functions.  The function unwind_to_current_frame()
815130803Smarcel   is wrapped in catch exceptions so that, even when the unwind of the
816130803Smarcel   sentinel frame fails, the function still returns a stack frame.  */
817130803Smarcel
818130803Smarcelstatic int
819130803Smarcelunwind_to_current_frame (struct ui_out *ui_out, void *args)
820130803Smarcel{
821130803Smarcel  struct frame_info *frame = get_prev_frame (args);
822130803Smarcel  /* A sentinel frame can fail to unwind, eg, because it's PC value
823130803Smarcel     lands in somewhere like start.  */
824130803Smarcel  if (frame == NULL)
825130803Smarcel    return 1;
826130803Smarcel  current_frame = frame;
827130803Smarcel  return 0;
828130803Smarcel}
829130803Smarcel
830130803Smarcelstruct frame_info *
831130803Smarcelget_current_frame (void)
832130803Smarcel{
833130803Smarcel  /* First check, and report, the lack of registers.  Having GDB
834130803Smarcel     report "No stack!" or "No memory" when the target doesn't even
835130803Smarcel     have registers is very confusing.  Besides, "printcmd.exp"
836130803Smarcel     explicitly checks that ``print $pc'' with no registers prints "No
837130803Smarcel     registers".  */
838130803Smarcel  if (!target_has_registers)
839130803Smarcel    error ("No registers.");
840130803Smarcel  if (!target_has_stack)
841130803Smarcel    error ("No stack.");
842130803Smarcel  if (!target_has_memory)
843130803Smarcel    error ("No memory.");
844130803Smarcel  if (current_frame == NULL)
845130803Smarcel    {
846130803Smarcel      struct frame_info *sentinel_frame =
847130803Smarcel	create_sentinel_frame (current_regcache);
848130803Smarcel      if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
849130803Smarcel			    NULL, RETURN_MASK_ERROR) != 0)
850130803Smarcel	{
851130803Smarcel	  /* Oops! Fake a current frame?  Is this useful?  It has a PC
852130803Smarcel             of zero, for instance.  */
853130803Smarcel	  current_frame = sentinel_frame;
854130803Smarcel	}
855130803Smarcel    }
856130803Smarcel  return current_frame;
857130803Smarcel}
858130803Smarcel
859130803Smarcel/* The "selected" stack frame is used by default for local and arg
860130803Smarcel   access.  May be zero, for no selected frame.  */
861130803Smarcel
862130803Smarcelstruct frame_info *deprecated_selected_frame;
863130803Smarcel
864130803Smarcel/* Return the selected frame.  Always non-null (unless there isn't an
865130803Smarcel   inferior sufficient for creating a frame) in which case an error is
866130803Smarcel   thrown.  */
867130803Smarcel
868130803Smarcelstruct frame_info *
869130803Smarcelget_selected_frame (void)
870130803Smarcel{
871130803Smarcel  if (deprecated_selected_frame == NULL)
872130803Smarcel    /* Hey!  Don't trust this.  It should really be re-finding the
873130803Smarcel       last selected frame of the currently selected thread.  This,
874130803Smarcel       though, is better than nothing.  */
875130803Smarcel    select_frame (get_current_frame ());
876130803Smarcel  /* There is always a frame.  */
877130803Smarcel  gdb_assert (deprecated_selected_frame != NULL);
878130803Smarcel  return deprecated_selected_frame;
879130803Smarcel}
880130803Smarcel
881130803Smarcel/* This is a variant of get_selected_frame which can be called when
882130803Smarcel   the inferior does not have a frame; in that case it will return
883130803Smarcel   NULL instead of calling error ().  */
884130803Smarcel
885130803Smarcelstruct frame_info *
886130803Smarceldeprecated_safe_get_selected_frame (void)
887130803Smarcel{
888130803Smarcel  if (!target_has_registers || !target_has_stack || !target_has_memory)
889130803Smarcel    return NULL;
890130803Smarcel  return get_selected_frame ();
891130803Smarcel}
892130803Smarcel
893130803Smarcel/* Select frame FI (or NULL - to invalidate the current frame).  */
894130803Smarcel
895130803Smarcelvoid
896130803Smarcelselect_frame (struct frame_info *fi)
897130803Smarcel{
898130803Smarcel  struct symtab *s;
899130803Smarcel
900130803Smarcel  deprecated_selected_frame = fi;
901130803Smarcel  /* NOTE: cagney/2002-05-04: FI can be NULL.  This occures when the
902130803Smarcel     frame is being invalidated.  */
903130803Smarcel  if (selected_frame_level_changed_hook)
904130803Smarcel    selected_frame_level_changed_hook (frame_relative_level (fi));
905130803Smarcel
906130803Smarcel  /* FIXME: kseitz/2002-08-28: It would be nice to call
907130803Smarcel     selected_frame_level_changed_event right here, but due to limitations
908130803Smarcel     in the current interfaces, we would end up flooding UIs with events
909130803Smarcel     because select_frame is used extensively internally.
910130803Smarcel
911130803Smarcel     Once we have frame-parameterized frame (and frame-related) commands,
912130803Smarcel     the event notification can be moved here, since this function will only
913130803Smarcel     be called when the users selected frame is being changed. */
914130803Smarcel
915130803Smarcel  /* Ensure that symbols for this frame are read in.  Also, determine the
916130803Smarcel     source language of this frame, and switch to it if desired.  */
917130803Smarcel  if (fi)
918130803Smarcel    {
919130803Smarcel      /* We retrieve the frame's symtab by using the frame PC.  However
920130803Smarcel         we cannot use the frame pc as is, because it usually points to
921130803Smarcel         the instruction following the "call", which is sometimes the
922130803Smarcel         first instruction of another function.  So we rely on
923130803Smarcel         get_frame_address_in_block() which provides us with a PC which
924130803Smarcel         is guaranteed to be inside the frame's code block.  */
925130803Smarcel      s = find_pc_symtab (get_frame_address_in_block (fi));
926130803Smarcel      if (s
927130803Smarcel	  && s->language != current_language->la_language
928130803Smarcel	  && s->language != language_unknown
929130803Smarcel	  && language_mode == language_mode_auto)
930130803Smarcel	{
931130803Smarcel	  set_language (s->language);
932130803Smarcel	}
933130803Smarcel    }
934130803Smarcel}
935130803Smarcel
936130803Smarcel/* Return the register saved in the simplistic ``saved_regs'' cache.
937130803Smarcel   If the value isn't here AND a value is needed, try the next inner
938130803Smarcel   most frame.  */
939130803Smarcel
940130803Smarcelstatic void
941130803Smarcellegacy_saved_regs_prev_register (struct frame_info *next_frame,
942130803Smarcel				 void **this_prologue_cache,
943130803Smarcel				 int regnum, int *optimizedp,
944130803Smarcel				 enum lval_type *lvalp, CORE_ADDR *addrp,
945130803Smarcel				 int *realnump, void *bufferp)
946130803Smarcel{
947130803Smarcel  /* HACK: New code is passed the next frame and this cache.
948130803Smarcel     Unfortunately, old code expects this frame.  Since this is a
949130803Smarcel     backward compatibility hack, cheat by walking one level along the
950130803Smarcel     prologue chain to the frame the old code expects.
951130803Smarcel
952130803Smarcel     Do not try this at home.  Professional driver, closed course.  */
953130803Smarcel  struct frame_info *frame = next_frame->prev;
954130803Smarcel  gdb_assert (frame != NULL);
955130803Smarcel
956130803Smarcel  if (deprecated_get_frame_saved_regs (frame) == NULL)
957130803Smarcel    {
958130803Smarcel      /* If nothing's initialized the saved regs, do it now.  */
959130803Smarcel      gdb_assert (DEPRECATED_FRAME_INIT_SAVED_REGS_P ());
960130803Smarcel      DEPRECATED_FRAME_INIT_SAVED_REGS (frame);
961130803Smarcel      gdb_assert (deprecated_get_frame_saved_regs (frame) != NULL);
962130803Smarcel    }
963130803Smarcel
964130803Smarcel  if (deprecated_get_frame_saved_regs (frame) != NULL
965130803Smarcel      && deprecated_get_frame_saved_regs (frame)[regnum] != 0)
966130803Smarcel    {
967130803Smarcel      if (regnum == SP_REGNUM)
968130803Smarcel	{
969130803Smarcel	  /* SP register treated specially.  */
970130803Smarcel	  *optimizedp = 0;
971130803Smarcel	  *lvalp = not_lval;
972130803Smarcel	  *addrp = 0;
973130803Smarcel	  *realnump = -1;
974130803Smarcel	  if (bufferp != NULL)
975130803Smarcel	    /* NOTE: cagney/2003-05-09: In-lined store_address with
976130803Smarcel               it's body - store_unsigned_integer.  */
977130803Smarcel	    store_unsigned_integer (bufferp, DEPRECATED_REGISTER_RAW_SIZE (regnum),
978130803Smarcel				    deprecated_get_frame_saved_regs (frame)[regnum]);
979130803Smarcel	}
980130803Smarcel      else
981130803Smarcel	{
982130803Smarcel	  /* Any other register is saved in memory, fetch it but cache
983130803Smarcel             a local copy of its value.  */
984130803Smarcel	  *optimizedp = 0;
985130803Smarcel	  *lvalp = lval_memory;
986130803Smarcel	  *addrp = deprecated_get_frame_saved_regs (frame)[regnum];
987130803Smarcel	  *realnump = -1;
988130803Smarcel	  if (bufferp != NULL)
989130803Smarcel	    {
990130803Smarcel#if 1
991130803Smarcel	      /* Save each register value, as it is read in, in a
992130803Smarcel                 frame based cache.  */
993130803Smarcel	      void **regs = (*this_prologue_cache);
994130803Smarcel	      if (regs == NULL)
995130803Smarcel		{
996130803Smarcel		  int sizeof_cache = ((NUM_REGS + NUM_PSEUDO_REGS)
997130803Smarcel				      * sizeof (void *));
998130803Smarcel		  regs = frame_obstack_zalloc (sizeof_cache);
999130803Smarcel		  (*this_prologue_cache) = regs;
1000130803Smarcel		}
1001130803Smarcel	      if (regs[regnum] == NULL)
1002130803Smarcel		{
1003130803Smarcel		  regs[regnum]
1004130803Smarcel		    = frame_obstack_zalloc (DEPRECATED_REGISTER_RAW_SIZE (regnum));
1005130803Smarcel		  read_memory (deprecated_get_frame_saved_regs (frame)[regnum], regs[regnum],
1006130803Smarcel			       DEPRECATED_REGISTER_RAW_SIZE (regnum));
1007130803Smarcel		}
1008130803Smarcel	      memcpy (bufferp, regs[regnum], DEPRECATED_REGISTER_RAW_SIZE (regnum));
1009130803Smarcel#else
1010130803Smarcel	      /* Read the value in from memory.  */
1011130803Smarcel	      read_memory (deprecated_get_frame_saved_regs (frame)[regnum], bufferp,
1012130803Smarcel			   DEPRECATED_REGISTER_RAW_SIZE (regnum));
1013130803Smarcel#endif
1014130803Smarcel	    }
1015130803Smarcel	}
1016130803Smarcel      return;
1017130803Smarcel    }
1018130803Smarcel
1019130803Smarcel  /* No luck.  Assume this and the next frame have the same register
1020130803Smarcel     value.  Pass the unwind request down the frame chain to the next
1021130803Smarcel     frame.  Hopefully that frame will find the register's location.  */
1022130803Smarcel  frame_register_unwind (next_frame, regnum, optimizedp, lvalp, addrp,
1023130803Smarcel			 realnump, bufferp);
1024130803Smarcel}
1025130803Smarcel
1026130803Smarcelstatic void
1027130803Smarcellegacy_saved_regs_this_id (struct frame_info *next_frame,
1028130803Smarcel			   void **this_prologue_cache,
1029130803Smarcel			   struct frame_id *id)
1030130803Smarcel{
1031130803Smarcel  /* A developer is trying to bring up a new architecture, help them
1032130803Smarcel     by providing a default unwinder that refuses to unwind anything
1033130803Smarcel     (the ID is always NULL).  In the case of legacy code,
1034130803Smarcel     legacy_get_prev_frame() will have previously set ->this_id.p, so
1035130803Smarcel     this code won't be called.  */
1036130803Smarcel  (*id) = null_frame_id;
1037130803Smarcel}
1038130803Smarcel
1039130803Smarcelconst struct frame_unwind legacy_saved_regs_unwinder = {
1040130803Smarcel  /* Not really.  It gets overridden by legacy_get_prev_frame.  */
1041130803Smarcel  UNKNOWN_FRAME,
1042130803Smarcel  legacy_saved_regs_this_id,
1043130803Smarcel  legacy_saved_regs_prev_register
1044130803Smarcel};
1045130803Smarcelconst struct frame_unwind *legacy_saved_regs_unwind = &legacy_saved_regs_unwinder;
1046130803Smarcel
1047130803Smarcel
1048130803Smarcel/* Function: deprecated_generic_get_saved_register
104998944Sobrien   Find register number REGNUM relative to FRAME and put its (raw,
1050130803Smarcel   target format) contents in *RAW_BUFFER.
105198944Sobrien
1052130803Smarcel   Set *OPTIMIZED if the variable was optimized out (and thus can't be
1053130803Smarcel   fetched).  Note that this is never set to anything other than zero
1054130803Smarcel   in this implementation.
105598944Sobrien
1056130803Smarcel   Set *LVAL to lval_memory, lval_register, or not_lval, depending on
1057130803Smarcel   whether the value was fetched from memory, from a register, or in a
1058130803Smarcel   strange and non-modifiable way (e.g. a frame pointer which was
1059130803Smarcel   calculated rather than fetched).  We will use not_lval for values
1060130803Smarcel   fetched from generic dummy frames.
1061130803Smarcel
1062130803Smarcel   Set *ADDRP to the address, either in memory or as a
1063130803Smarcel   DEPRECATED_REGISTER_BYTE offset into the registers array.  If the
1064130803Smarcel   value is stored in a dummy frame, set *ADDRP to zero.
1065130803Smarcel
106698944Sobrien   The argument RAW_BUFFER must point to aligned memory.  */
106798944Sobrien
1068130803Smarcelvoid
1069130803Smarceldeprecated_generic_get_saved_register (char *raw_buffer, int *optimized,
1070130803Smarcel				       CORE_ADDR *addrp,
1071130803Smarcel				       struct frame_info *frame, int regnum,
1072130803Smarcel				       enum lval_type *lval)
107398944Sobrien{
107498944Sobrien  if (!target_has_registers)
107598944Sobrien    error ("No registers.");
107698944Sobrien
107798944Sobrien  /* Normal systems don't optimize out things with register numbers.  */
107898944Sobrien  if (optimized != NULL)
107998944Sobrien    *optimized = 0;
1080130803Smarcel
1081130803Smarcel  if (addrp)			/* default assumption: not found in memory */
1082130803Smarcel    *addrp = 0;
1083130803Smarcel
1084130803Smarcel  /* Note: since the current frame's registers could only have been
1085130803Smarcel     saved by frames INTERIOR TO the current frame, we skip examining
1086130803Smarcel     the current frame itself: otherwise, we would be getting the
1087130803Smarcel     previous frame's registers which were saved by the current frame.  */
1088130803Smarcel
1089130803Smarcel  if (frame != NULL)
109098944Sobrien    {
1091130803Smarcel      for (frame = get_next_frame (frame);
1092130803Smarcel	   frame_relative_level (frame) >= 0;
1093130803Smarcel	   frame = get_next_frame (frame))
109498944Sobrien	{
1095130803Smarcel	  if (get_frame_type (frame) == DUMMY_FRAME)
109698944Sobrien	    {
1097130803Smarcel	      if (lval)		/* found it in a CALL_DUMMY frame */
1098130803Smarcel		*lval = not_lval;
1099130803Smarcel	      if (raw_buffer)
1100130803Smarcel		/* FIXME: cagney/2002-06-26: This should be via the
1101130803Smarcel		   gdbarch_register_read() method so that it, on the
1102130803Smarcel		   fly, constructs either a raw or pseudo register
1103130803Smarcel		   from the raw register cache.  */
1104130803Smarcel		regcache_raw_read
1105130803Smarcel		  (deprecated_find_dummy_frame_regcache (get_frame_pc (frame),
1106130803Smarcel							 get_frame_base (frame)),
1107130803Smarcel		   regnum, raw_buffer);
1108130803Smarcel	      return;
110998944Sobrien	    }
1110130803Smarcel
1111130803Smarcel	  DEPRECATED_FRAME_INIT_SAVED_REGS (frame);
1112130803Smarcel	  if (deprecated_get_frame_saved_regs (frame) != NULL
1113130803Smarcel	      && deprecated_get_frame_saved_regs (frame)[regnum] != 0)
1114130803Smarcel	    {
1115130803Smarcel	      if (lval)		/* found it saved on the stack */
1116130803Smarcel		*lval = lval_memory;
1117130803Smarcel	      if (regnum == SP_REGNUM)
1118130803Smarcel		{
1119130803Smarcel		  if (raw_buffer)	/* SP register treated specially */
1120130803Smarcel		    /* NOTE: cagney/2003-05-09: In-line store_address
1121130803Smarcel                       with it's body - store_unsigned_integer.  */
1122130803Smarcel		    store_unsigned_integer (raw_buffer,
1123130803Smarcel					    DEPRECATED_REGISTER_RAW_SIZE (regnum),
1124130803Smarcel					    deprecated_get_frame_saved_regs (frame)[regnum]);
1125130803Smarcel		}
1126130803Smarcel	      else
1127130803Smarcel		{
1128130803Smarcel		  if (addrp)	/* any other register */
1129130803Smarcel		    *addrp = deprecated_get_frame_saved_regs (frame)[regnum];
1130130803Smarcel		  if (raw_buffer)
1131130803Smarcel		    read_memory (deprecated_get_frame_saved_regs (frame)[regnum], raw_buffer,
1132130803Smarcel				 DEPRECATED_REGISTER_RAW_SIZE (regnum));
1133130803Smarcel		}
1134130803Smarcel	      return;
1135130803Smarcel	    }
113698944Sobrien	}
113798944Sobrien    }
1138130803Smarcel
1139130803Smarcel  /* If we get thru the loop to this point, it means the register was
1140130803Smarcel     not saved in any frame.  Return the actual live-register value.  */
1141130803Smarcel
1142130803Smarcel  if (lval)			/* found it in a live register */
1143130803Smarcel    *lval = lval_register;
1144130803Smarcel  if (addrp)
1145130803Smarcel    *addrp = DEPRECATED_REGISTER_BYTE (regnum);
1146130803Smarcel  if (raw_buffer)
1147130803Smarcel    deprecated_read_register_gen (regnum, raw_buffer);
1148130803Smarcel}
1149130803Smarcel
1150130803Smarcel/* Determine the frame's type based on its PC.  */
1151130803Smarcel
1152130803Smarcelstatic enum frame_type
1153130803Smarcelframe_type_from_pc (CORE_ADDR pc)
1154130803Smarcel{
1155130803Smarcel  /* FIXME: cagney/2002-11-24: Can't yet directly call
1156130803Smarcel     pc_in_dummy_frame() as some architectures don't set
1157130803Smarcel     PC_IN_CALL_DUMMY() to generic_pc_in_call_dummy() (remember the
1158130803Smarcel     latter is implemented by simply calling pc_in_dummy_frame).  */
1159130803Smarcel  if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
1160130803Smarcel      && DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0))
1161130803Smarcel    return DUMMY_FRAME;
116298944Sobrien  else
116398944Sobrien    {
1164130803Smarcel      char *name;
1165130803Smarcel      find_pc_partial_function (pc, &name, NULL, NULL);
1166130803Smarcel      if (PC_IN_SIGTRAMP (pc, name))
1167130803Smarcel	return SIGTRAMP_FRAME;
1168130803Smarcel      else
1169130803Smarcel	return NORMAL_FRAME;
117098944Sobrien    }
117198944Sobrien}
117298944Sobrien
1173130803Smarcel/* Create an arbitrary (i.e. address specified by user) or innermost frame.
1174130803Smarcel   Always returns a non-NULL value.  */
117598944Sobrien
1176130803Smarcelstruct frame_info *
1177130803Smarcelcreate_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1178130803Smarcel{
1179130803Smarcel  struct frame_info *fi;
1180130803Smarcel
1181130803Smarcel  if (frame_debug)
1182130803Smarcel    {
1183130803Smarcel      fprintf_unfiltered (gdb_stdlog,
1184130803Smarcel			  "{ create_new_frame (addr=0x%s, pc=0x%s) ",
1185130803Smarcel			  paddr_nz (addr), paddr_nz (pc));
1186130803Smarcel    }
1187130803Smarcel
1188130803Smarcel  fi = frame_obstack_zalloc (sizeof (struct frame_info));
1189130803Smarcel
1190130803Smarcel  fi->next = create_sentinel_frame (current_regcache);
1191130803Smarcel
1192130803Smarcel  /* Select/initialize both the unwind function and the frame's type
1193130803Smarcel     based on the PC.  */
1194130803Smarcel  fi->unwind = frame_unwind_find_by_frame (fi->next);
1195130803Smarcel  if (fi->unwind->type != UNKNOWN_FRAME)
1196130803Smarcel    fi->type = fi->unwind->type;
1197130803Smarcel  else
1198130803Smarcel    fi->type = frame_type_from_pc (pc);
1199130803Smarcel
1200130803Smarcel  fi->this_id.p = 1;
1201130803Smarcel  deprecated_update_frame_base_hack (fi, addr);
1202130803Smarcel  deprecated_update_frame_pc_hack (fi, pc);
1203130803Smarcel
1204130803Smarcel  if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ())
1205130803Smarcel    DEPRECATED_INIT_EXTRA_FRAME_INFO (0, fi);
1206130803Smarcel
1207130803Smarcel  if (frame_debug)
1208130803Smarcel    {
1209130803Smarcel      fprintf_unfiltered (gdb_stdlog, "-> ");
1210130803Smarcel      fprint_frame (gdb_stdlog, fi);
1211130803Smarcel      fprintf_unfiltered (gdb_stdlog, " }\n");
1212130803Smarcel    }
1213130803Smarcel
1214130803Smarcel  return fi;
1215130803Smarcel}
1216130803Smarcel
1217130803Smarcel/* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1218130803Smarcel   innermost frame).  Be careful to not fall off the bottom of the
1219130803Smarcel   frame chain and onto the sentinel frame.  */
1220130803Smarcel
1221130803Smarcelstruct frame_info *
1222130803Smarcelget_next_frame (struct frame_info *this_frame)
1223130803Smarcel{
1224130803Smarcel  if (this_frame->level > 0)
1225130803Smarcel    return this_frame->next;
1226130803Smarcel  else
1227130803Smarcel    return NULL;
1228130803Smarcel}
1229130803Smarcel
1230130803Smarcel/* Flush the entire frame cache.  */
1231130803Smarcel
123298944Sobrienvoid
1233130803Smarcelflush_cached_frames (void)
123498944Sobrien{
1235130803Smarcel  /* Since we can't really be sure what the first object allocated was */
1236130803Smarcel  obstack_free (&frame_cache_obstack, 0);
1237130803Smarcel  obstack_init (&frame_cache_obstack);
1238130803Smarcel
1239130803Smarcel  current_frame = NULL;		/* Invalidate cache */
1240130803Smarcel  select_frame (NULL);
1241130803Smarcel  annotate_frames_invalid ();
1242130803Smarcel  if (frame_debug)
1243130803Smarcel    fprintf_unfiltered (gdb_stdlog, "{ flush_cached_frames () }\n");
124498944Sobrien}
124598944Sobrien
1246130803Smarcel/* Flush the frame cache, and start a new one if necessary.  */
124798944Sobrien
1248130803Smarcelvoid
1249130803Smarcelreinit_frame_cache (void)
1250130803Smarcel{
1251130803Smarcel  flush_cached_frames ();
125298944Sobrien
1253130803Smarcel  /* FIXME: The inferior_ptid test is wrong if there is a corefile.  */
1254130803Smarcel  if (PIDGET (inferior_ptid) != 0)
1255130803Smarcel    {
1256130803Smarcel      select_frame (get_current_frame ());
1257130803Smarcel    }
1258130803Smarcel}
125998944Sobrien
1260130803Smarcel/* Create the previous frame using the deprecated methods
1261130803Smarcel   INIT_EXTRA_INFO, INIT_FRAME_PC and INIT_FRAME_PC_FIRST.  */
126298944Sobrien
1263130803Smarcelstatic struct frame_info *
1264130803Smarcellegacy_get_prev_frame (struct frame_info *this_frame)
1265130803Smarcel{
1266130803Smarcel  CORE_ADDR address = 0;
1267130803Smarcel  struct frame_info *prev;
1268130803Smarcel  int fromleaf;
1269130803Smarcel
1270130803Smarcel  /* Don't frame_debug print legacy_get_prev_frame() here, just
1271130803Smarcel     confuses the output.  */
1272130803Smarcel
1273130803Smarcel  /* Allocate the new frame.
1274130803Smarcel
1275130803Smarcel     There is no reason to worry about memory leaks, should the
1276130803Smarcel     remainder of the function fail.  The allocated memory will be
1277130803Smarcel     quickly reclaimed when the frame cache is flushed, and the `we've
1278130803Smarcel     been here before' check, in get_prev_frame will stop repeated
1279130803Smarcel     memory allocation calls.  */
1280130803Smarcel  prev = FRAME_OBSTACK_ZALLOC (struct frame_info);
1281130803Smarcel  prev->level = this_frame->level + 1;
1282130803Smarcel
1283130803Smarcel  /* Do not completely wire it in to the frame chain.  Some (bad) code
1284130803Smarcel     in INIT_FRAME_EXTRA_INFO tries to look along frame->prev to pull
1285130803Smarcel     some fancy tricks (of course such code is, by definition,
1286130803Smarcel     recursive).
1287130803Smarcel
1288130803Smarcel     On the other hand, methods, such as get_frame_pc() and
1289130803Smarcel     get_frame_base() rely on being able to walk along the frame
1290130803Smarcel     chain.  Make certain that at least they work by providing that
1291130803Smarcel     link.  Of course things manipulating prev can't go back.  */
1292130803Smarcel  prev->next = this_frame;
1293130803Smarcel
1294130803Smarcel  /* NOTE: cagney/2002-11-18: Should have been correctly setting the
1295130803Smarcel     frame's type here, before anything else, and not last, at the
1296130803Smarcel     bottom of this function.  The various
1297130803Smarcel     DEPRECATED_INIT_EXTRA_FRAME_INFO, DEPRECATED_INIT_FRAME_PC,
1298130803Smarcel     DEPRECATED_INIT_FRAME_PC_FIRST and
1299130803Smarcel     DEPRECATED_FRAME_INIT_SAVED_REGS methods are full of work-arounds
1300130803Smarcel     that handle the frame not being correctly set from the start.
1301130803Smarcel     Unfortunately those same work-arounds rely on the type defaulting
1302130803Smarcel     to NORMAL_FRAME.  Ulgh!  The new frame code does not have this
1303130803Smarcel     problem.  */
1304130803Smarcel  prev->type = UNKNOWN_FRAME;
1305130803Smarcel
1306130803Smarcel  /* A legacy frame's ID is always computed here.  Mark it as valid.  */
1307130803Smarcel  prev->this_id.p = 1;
1308130803Smarcel
1309130803Smarcel  /* Handle sentinel frame unwind as a special case.  */
1310130803Smarcel  if (this_frame->level < 0)
1311130803Smarcel    {
1312130803Smarcel      /* Try to unwind the PC.  If that doesn't work, assume we've reached
1313130803Smarcel	 the oldest frame and simply return.  Is there a better sentinal
1314130803Smarcel	 value?  The unwound PC value is then used to initialize the new
1315130803Smarcel	 previous frame's type.
1316130803Smarcel
1317130803Smarcel	 Note that the pc-unwind is intentionally performed before the
1318130803Smarcel	 frame chain.  This is ok since, for old targets, both
1319130803Smarcel	 frame_pc_unwind (nee, DEPRECATED_FRAME_SAVED_PC) and
1320130803Smarcel	 DEPRECATED_FRAME_CHAIN()) assume THIS_FRAME's data structures
1321130803Smarcel	 have already been initialized (using
1322130803Smarcel	 DEPRECATED_INIT_EXTRA_FRAME_INFO) and hence the call order
1323130803Smarcel	 doesn't matter.
1324130803Smarcel
1325130803Smarcel	 By unwinding the PC first, it becomes possible to, in the case of
1326130803Smarcel	 a dummy frame, avoid also unwinding the frame ID.  This is
1327130803Smarcel	 because (well ignoring the PPC) a dummy frame can be located
1328130803Smarcel	 using THIS_FRAME's frame ID.  */
1329130803Smarcel
1330130803Smarcel      deprecated_update_frame_pc_hack (prev, frame_pc_unwind (this_frame));
1331130803Smarcel      if (get_frame_pc (prev) == 0)
1332130803Smarcel	{
1333130803Smarcel	  /* The allocated PREV_FRAME will be reclaimed when the frame
1334130803Smarcel	     obstack is next purged.  */
1335130803Smarcel	  if (frame_debug)
1336130803Smarcel	    {
1337130803Smarcel	      fprintf_unfiltered (gdb_stdlog, "-> ");
1338130803Smarcel	      fprint_frame (gdb_stdlog, NULL);
1339130803Smarcel	      fprintf_unfiltered (gdb_stdlog,
1340130803Smarcel				  " // unwound legacy PC zero }\n");
1341130803Smarcel	    }
1342130803Smarcel	  return NULL;
1343130803Smarcel	}
1344130803Smarcel
1345130803Smarcel      /* Set the unwind functions based on that identified PC.  Ditto
1346130803Smarcel         for the "type" but strongly prefer the unwinder's frame type.  */
1347130803Smarcel      prev->unwind = frame_unwind_find_by_frame (prev->next);
1348130803Smarcel      if (prev->unwind->type == UNKNOWN_FRAME)
1349130803Smarcel	prev->type = frame_type_from_pc (get_frame_pc (prev));
1350130803Smarcel      else
1351130803Smarcel	prev->type = prev->unwind->type;
1352130803Smarcel
1353130803Smarcel      /* Find the prev's frame's ID.  */
1354130803Smarcel      if (prev->type == DUMMY_FRAME
1355130803Smarcel	  && gdbarch_unwind_dummy_id_p (current_gdbarch))
1356130803Smarcel	{
1357130803Smarcel	  /* When unwinding a normal frame, the stack structure is
1358130803Smarcel	     determined by analyzing the frame's function's code (be
1359130803Smarcel	     it using brute force prologue analysis, or the dwarf2
1360130803Smarcel	     CFI).  In the case of a dummy frame, that simply isn't
1361130803Smarcel	     possible.  The The PC is either the program entry point,
1362130803Smarcel	     or some random address on the stack.  Trying to use that
1363130803Smarcel	     PC to apply standard frame ID unwind techniques is just
1364130803Smarcel	     asking for trouble.  */
1365130803Smarcel	  /* Use an architecture specific method to extract the prev's
1366130803Smarcel	     dummy ID from the next frame.  Note that this method uses
1367130803Smarcel	     frame_register_unwind to obtain the register values
1368130803Smarcel	     needed to determine the dummy frame's ID.  */
1369130803Smarcel	  prev->this_id.value = gdbarch_unwind_dummy_id (current_gdbarch,
1370130803Smarcel							 this_frame);
1371130803Smarcel	}
1372130803Smarcel      else
1373130803Smarcel	{
1374130803Smarcel	  /* We're unwinding a sentinel frame, the PC of which is
1375130803Smarcel	     pointing at a stack dummy.  Fake up the dummy frame's ID
1376130803Smarcel	     using the same sequence as is found a traditional
1377130803Smarcel	     unwinder.  Once all architectures supply the
1378130803Smarcel	     unwind_dummy_id method, this code can go away.  */
1379130803Smarcel	  prev->this_id.value = frame_id_build (deprecated_read_fp (),
1380130803Smarcel						read_pc ());
1381130803Smarcel	}
1382130803Smarcel
1383130803Smarcel      /* Check that the unwound ID is valid.  */
1384130803Smarcel      if (!frame_id_p (prev->this_id.value))
1385130803Smarcel	{
1386130803Smarcel	  if (frame_debug)
1387130803Smarcel	    {
1388130803Smarcel	      fprintf_unfiltered (gdb_stdlog, "-> ");
1389130803Smarcel	      fprint_frame (gdb_stdlog, NULL);
1390130803Smarcel	      fprintf_unfiltered (gdb_stdlog,
1391130803Smarcel				  " // unwound legacy ID invalid }\n");
1392130803Smarcel	    }
1393130803Smarcel	  return NULL;
1394130803Smarcel	}
1395130803Smarcel
1396130803Smarcel      /* Check that the new frame isn't inner to (younger, below,
1397130803Smarcel	 next) the old frame.  If that happens the frame unwind is
1398130803Smarcel	 going backwards.  */
1399130803Smarcel      /* FIXME: cagney/2003-02-25: Ignore the sentinel frame since
1400130803Smarcel	 that doesn't have a valid frame ID.  Should instead set the
1401130803Smarcel	 sentinel frame's frame ID to a `sentinel'.  Leave it until
1402130803Smarcel	 after the switch to storing the frame ID, instead of the
1403130803Smarcel	 frame base, in the frame object.  */
1404130803Smarcel
1405130803Smarcel      /* Link it in.  */
1406130803Smarcel      this_frame->prev = prev;
1407130803Smarcel
1408130803Smarcel      /* FIXME: cagney/2002-01-19: This call will go away.  Instead of
1409130803Smarcel	 initializing extra info, all frames will use the frame_cache
1410130803Smarcel	 (passed to the unwind functions) to store additional frame
1411130803Smarcel	 info.  Unfortunately legacy targets can't use
1412130803Smarcel	 legacy_get_prev_frame() to unwind the sentinel frame and,
1413130803Smarcel	 consequently, are forced to take this code path and rely on
1414130803Smarcel	 the below call to DEPRECATED_INIT_EXTRA_FRAME_INFO to
1415130803Smarcel	 initialize the inner-most frame.  */
1416130803Smarcel      if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ())
1417130803Smarcel	{
1418130803Smarcel	  DEPRECATED_INIT_EXTRA_FRAME_INFO (0, prev);
1419130803Smarcel	}
1420130803Smarcel
1421130803Smarcel      if (prev->type == NORMAL_FRAME)
1422130803Smarcel	prev->this_id.value.code_addr
1423130803Smarcel	  = get_pc_function_start (prev->this_id.value.code_addr);
1424130803Smarcel
1425130803Smarcel      if (frame_debug)
1426130803Smarcel	{
1427130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "-> ");
1428130803Smarcel	  fprint_frame (gdb_stdlog, prev);
1429130803Smarcel	  fprintf_unfiltered (gdb_stdlog, " } // legacy innermost frame\n");
1430130803Smarcel	}
1431130803Smarcel      return prev;
1432130803Smarcel    }
1433130803Smarcel
1434130803Smarcel  /* This code only works on normal frames.  A sentinel frame, where
1435130803Smarcel     the level is -1, should never reach this code.  */
1436130803Smarcel  gdb_assert (this_frame->level >= 0);
1437130803Smarcel
1438130803Smarcel  /* On some machines it is possible to call a function without
1439130803Smarcel     setting up a stack frame for it.  On these machines, we
1440130803Smarcel     define this macro to take two args; a frameinfo pointer
1441130803Smarcel     identifying a frame and a variable to set or clear if it is
1442130803Smarcel     or isn't leafless.  */
1443130803Smarcel
1444130803Smarcel  /* Still don't want to worry about this except on the innermost
1445130803Smarcel     frame.  This macro will set FROMLEAF if THIS_FRAME is a frameless
1446130803Smarcel     function invocation.  */
1447130803Smarcel  if (this_frame->level == 0)
1448130803Smarcel    /* FIXME: 2002-11-09: Frameless functions can occure anywhere in
1449130803Smarcel       the frame chain, not just the inner most frame!  The generic,
1450130803Smarcel       per-architecture, frame code should handle this and the below
1451130803Smarcel       should simply be removed.  */
1452130803Smarcel    fromleaf = (DEPRECATED_FRAMELESS_FUNCTION_INVOCATION_P ()
1453130803Smarcel		&& DEPRECATED_FRAMELESS_FUNCTION_INVOCATION (this_frame));
1454130803Smarcel  else
1455130803Smarcel    fromleaf = 0;
1456130803Smarcel
1457130803Smarcel  if (fromleaf)
1458130803Smarcel    /* A frameless inner-most frame.  The `FP' (which isn't an
1459130803Smarcel       architecture frame-pointer register!) of the caller is the same
1460130803Smarcel       as the callee.  */
1461130803Smarcel    /* FIXME: 2002-11-09: There isn't any reason to special case this
1462130803Smarcel       edge condition.  Instead the per-architecture code should hande
1463130803Smarcel       it locally.  */
1464130803Smarcel    /* FIXME: cagney/2003-06-16: This returns the inner most stack
1465130803Smarcel       address for the previous frame, that, however, is wrong.  It
1466130803Smarcel       should be the inner most stack address for the previous to
1467130803Smarcel       previous frame.  This is because it is the previous to previous
1468130803Smarcel       frame's innermost stack address that is constant through out
1469130803Smarcel       the lifetime of the previous frame (trust me :-).  */
1470130803Smarcel    address = get_frame_base (this_frame);
1471130803Smarcel  else
1472130803Smarcel    {
1473130803Smarcel      /* Two macros defined in tm.h specify the machine-dependent
1474130803Smarcel         actions to be performed here.
1475130803Smarcel
1476130803Smarcel         First, get the frame's chain-pointer.
1477130803Smarcel
1478130803Smarcel         If that is zero, the frame is the outermost frame or a leaf
1479130803Smarcel         called by the outermost frame.  This means that if start
1480130803Smarcel         calls main without a frame, we'll return 0 (which is fine
1481130803Smarcel         anyway).
1482130803Smarcel
1483130803Smarcel         Nope; there's a problem.  This also returns when the current
1484130803Smarcel         routine is a leaf of main.  This is unacceptable.  We move
1485130803Smarcel         this to after the ffi test; I'd rather have backtraces from
1486130803Smarcel         start go curfluy than have an abort called from main not show
1487130803Smarcel         main.  */
1488130803Smarcel      if (DEPRECATED_FRAME_CHAIN_P ())
1489130803Smarcel	address = DEPRECATED_FRAME_CHAIN (this_frame);
1490130803Smarcel      else
1491130803Smarcel	{
1492130803Smarcel	  /* Someone is part way through coverting an old architecture
1493130803Smarcel             to the new frame code.  Implement FRAME_CHAIN the way the
1494130803Smarcel             new frame will.  */
1495130803Smarcel	  /* Find PREV frame's unwinder.  */
1496130803Smarcel	  prev->unwind = frame_unwind_find_by_frame (this_frame->next);
1497130803Smarcel	  /* FIXME: cagney/2003-04-02: Rather than storing the frame's
1498130803Smarcel	     type in the frame, the unwinder's type should be returned
1499130803Smarcel	     directly.  Unfortunately, legacy code, called by
1500130803Smarcel	     legacy_get_prev_frame, explicitly set the frames type
1501130803Smarcel	     using the method deprecated_set_frame_type().  */
1502130803Smarcel	  prev->type = prev->unwind->type;
1503130803Smarcel	  /* Find PREV frame's ID.  */
1504130803Smarcel	  prev->unwind->this_id (this_frame,
1505130803Smarcel				 &prev->prologue_cache,
1506130803Smarcel				 &prev->this_id.value);
1507130803Smarcel	  prev->this_id.p = 1;
1508130803Smarcel	  address = prev->this_id.value.stack_addr;
1509130803Smarcel	}
1510130803Smarcel
1511130803Smarcel      if (!legacy_frame_chain_valid (address, this_frame))
1512130803Smarcel	{
1513130803Smarcel	  if (frame_debug)
1514130803Smarcel	    {
1515130803Smarcel	      fprintf_unfiltered (gdb_stdlog, "-> ");
1516130803Smarcel	      fprint_frame (gdb_stdlog, NULL);
1517130803Smarcel	      fprintf_unfiltered (gdb_stdlog,
1518130803Smarcel				  " // legacy frame chain invalid }\n");
1519130803Smarcel	    }
1520130803Smarcel	  return NULL;
1521130803Smarcel	}
1522130803Smarcel    }
1523130803Smarcel  if (address == 0)
1524130803Smarcel    {
1525130803Smarcel      if (frame_debug)
1526130803Smarcel	{
1527130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "-> ");
1528130803Smarcel	  fprint_frame (gdb_stdlog, NULL);
1529130803Smarcel	  fprintf_unfiltered (gdb_stdlog,
1530130803Smarcel			      " // legacy frame chain NULL }\n");
1531130803Smarcel	}
1532130803Smarcel      return NULL;
1533130803Smarcel    }
1534130803Smarcel
1535130803Smarcel  /* Link in the already allocated prev frame.  */
1536130803Smarcel  this_frame->prev = prev;
1537130803Smarcel  deprecated_update_frame_base_hack (prev, address);
1538130803Smarcel
1539130803Smarcel  /* This change should not be needed, FIXME!  We should determine
1540130803Smarcel     whether any targets *need* DEPRECATED_INIT_FRAME_PC to happen
1541130803Smarcel     after DEPRECATED_INIT_EXTRA_FRAME_INFO and come up with a simple
1542130803Smarcel     way to express what goes on here.
1543130803Smarcel
1544130803Smarcel     DEPRECATED_INIT_EXTRA_FRAME_INFO is called from two places:
1545130803Smarcel     create_new_frame (where the PC is already set up) and here (where
1546130803Smarcel     it isn't).  DEPRECATED_INIT_FRAME_PC is only called from here,
1547130803Smarcel     always after DEPRECATED_INIT_EXTRA_FRAME_INFO.
1548130803Smarcel
1549130803Smarcel     The catch is the MIPS, where DEPRECATED_INIT_EXTRA_FRAME_INFO
1550130803Smarcel     requires the PC value (which hasn't been set yet).  Some other
1551130803Smarcel     machines appear to require DEPRECATED_INIT_EXTRA_FRAME_INFO
1552130803Smarcel     before they can do DEPRECATED_INIT_FRAME_PC.  Phoo.
1553130803Smarcel
1554130803Smarcel     We shouldn't need DEPRECATED_INIT_FRAME_PC_FIRST to add more
1555130803Smarcel     complication to an already overcomplicated part of GDB.
1556130803Smarcel     gnu@cygnus.com, 15Sep92.
1557130803Smarcel
1558130803Smarcel     Assuming that some machines need DEPRECATED_INIT_FRAME_PC after
1559130803Smarcel     DEPRECATED_INIT_EXTRA_FRAME_INFO, one possible scheme:
1560130803Smarcel
1561130803Smarcel     SETUP_INNERMOST_FRAME(): Default version is just create_new_frame
1562130803Smarcel     (deprecated_read_fp ()), read_pc ()).  Machines with extra frame
1563130803Smarcel     info would do that (or the local equivalent) and then set the
1564130803Smarcel     extra fields.
1565130803Smarcel
1566130803Smarcel     SETUP_ARBITRARY_FRAME(argc, argv): Only change here is that
1567130803Smarcel     create_new_frame would no longer init extra frame info;
1568130803Smarcel     SETUP_ARBITRARY_FRAME would have to do that.
1569130803Smarcel
1570130803Smarcel     INIT_PREV_FRAME(fromleaf, prev) Replace
1571130803Smarcel     DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC.
1572130803Smarcel     This should also return a flag saying whether to keep the new
1573130803Smarcel     frame, or whether to discard it, because on some machines (e.g.
1574130803Smarcel     mips) it is really awkward to have DEPRECATED_FRAME_CHAIN_VALID
1575130803Smarcel     called BEFORE DEPRECATED_INIT_EXTRA_FRAME_INFO (there is no good
1576130803Smarcel     way to get information deduced in DEPRECATED_FRAME_CHAIN_VALID
1577130803Smarcel     into the extra fields of the new frame).  std_frame_pc(fromleaf,
1578130803Smarcel     prev)
1579130803Smarcel
1580130803Smarcel     This is the default setting for INIT_PREV_FRAME.  It just does
1581130803Smarcel     what the default DEPRECATED_INIT_FRAME_PC does.  Some machines
1582130803Smarcel     will call it from INIT_PREV_FRAME (either at the beginning, the
1583130803Smarcel     end, or in the middle).  Some machines won't use it.
1584130803Smarcel
1585130803Smarcel     kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94.  */
1586130803Smarcel
1587130803Smarcel  /* NOTE: cagney/2002-11-09: Just ignore the above!  There is no
1588130803Smarcel     reason for things to be this complicated.
1589130803Smarcel
1590130803Smarcel     The trick is to assume that there is always a frame.  Instead of
1591130803Smarcel     special casing the inner-most frame, create fake frame
1592130803Smarcel     (containing the hardware registers) that is inner to the
1593130803Smarcel     user-visible inner-most frame (...) and then unwind from that.
1594130803Smarcel     That way architecture code can use use the standard
1595130803Smarcel     frame_XX_unwind() functions and not differentiate between the
1596130803Smarcel     inner most and any other case.
1597130803Smarcel
1598130803Smarcel     Since there is always a frame to unwind from, there is always
1599130803Smarcel     somewhere (THIS_FRAME) to store all the info needed to construct
1600130803Smarcel     a new (previous) frame without having to first create it.  This
1601130803Smarcel     means that the convolution below - needing to carefully order a
1602130803Smarcel     frame's initialization - isn't needed.
1603130803Smarcel
1604130803Smarcel     The irony here though, is that DEPRECATED_FRAME_CHAIN(), at least
1605130803Smarcel     for a more up-to-date architecture, always calls
1606130803Smarcel     FRAME_SAVED_PC(), and FRAME_SAVED_PC() computes the PC but
1607130803Smarcel     without first needing the frame!  Instead of the convolution
1608130803Smarcel     below, we could have simply called FRAME_SAVED_PC() and been done
1609130803Smarcel     with it!  Note that FRAME_SAVED_PC() is being superseed by
1610130803Smarcel     frame_pc_unwind() and that function does have somewhere to cache
1611130803Smarcel     that PC value.  */
1612130803Smarcel
1613130803Smarcel  if (DEPRECATED_INIT_FRAME_PC_FIRST_P ())
1614130803Smarcel    deprecated_update_frame_pc_hack (prev,
1615130803Smarcel				     DEPRECATED_INIT_FRAME_PC_FIRST (fromleaf,
1616130803Smarcel								     prev));
1617130803Smarcel
1618130803Smarcel  if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ())
1619130803Smarcel    DEPRECATED_INIT_EXTRA_FRAME_INFO (fromleaf, prev);
1620130803Smarcel
1621130803Smarcel  /* This entry is in the frame queue now, which is good since
1622130803Smarcel     FRAME_SAVED_PC may use that queue to figure out its value (see
1623130803Smarcel     tm-sparc.h).  We want the pc saved in the inferior frame. */
1624130803Smarcel  if (DEPRECATED_INIT_FRAME_PC_P ())
1625130803Smarcel    deprecated_update_frame_pc_hack (prev,
1626130803Smarcel				     DEPRECATED_INIT_FRAME_PC (fromleaf,
1627130803Smarcel							       prev));
1628130803Smarcel
1629130803Smarcel  /* If ->frame and ->pc are unchanged, we are in the process of
1630130803Smarcel     getting ourselves into an infinite backtrace.  Some architectures
1631130803Smarcel     check this in DEPRECATED_FRAME_CHAIN or thereabouts, but it seems
1632130803Smarcel     like there is no reason this can't be an architecture-independent
1633130803Smarcel     check.  */
1634130803Smarcel  if (get_frame_base (prev) == get_frame_base (this_frame)
1635130803Smarcel      && get_frame_pc (prev) == get_frame_pc (this_frame))
1636130803Smarcel    {
1637130803Smarcel      this_frame->prev = NULL;
1638130803Smarcel      obstack_free (&frame_cache_obstack, prev);
1639130803Smarcel      if (frame_debug)
1640130803Smarcel	{
1641130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "-> ");
1642130803Smarcel	  fprint_frame (gdb_stdlog, NULL);
1643130803Smarcel	  fprintf_unfiltered (gdb_stdlog,
1644130803Smarcel			      " // legacy this.id == prev.id }\n");
1645130803Smarcel	}
1646130803Smarcel      return NULL;
1647130803Smarcel    }
1648130803Smarcel
1649130803Smarcel  /* Initialize the code used to unwind the frame PREV based on the PC
1650130803Smarcel     (and probably other architectural information).  The PC lets you
1651130803Smarcel     check things like the debug info at that point (dwarf2cfi?) and
1652130803Smarcel     use that to decide how the frame should be unwound.
1653130803Smarcel
1654130803Smarcel     If there isn't a FRAME_CHAIN, the code above will have already
1655130803Smarcel     done this.  */
1656130803Smarcel  if (prev->unwind == NULL)
1657130803Smarcel    prev->unwind = frame_unwind_find_by_frame (prev->next);
1658130803Smarcel
1659130803Smarcel  /* If the unwinder provides a frame type, use it.  Otherwize
1660130803Smarcel     continue on to that heuristic mess.  */
1661130803Smarcel  if (prev->unwind->type != UNKNOWN_FRAME)
1662130803Smarcel    {
1663130803Smarcel      prev->type = prev->unwind->type;
1664130803Smarcel      if (prev->type == NORMAL_FRAME)
1665130803Smarcel	/* FIXME: cagney/2003-06-16: would get_frame_pc() be better?  */
1666130803Smarcel	prev->this_id.value.code_addr
1667130803Smarcel	  = get_pc_function_start (prev->this_id.value.code_addr);
1668130803Smarcel      if (frame_debug)
1669130803Smarcel	{
1670130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "-> ");
1671130803Smarcel	  fprint_frame (gdb_stdlog, prev);
1672130803Smarcel	  fprintf_unfiltered (gdb_stdlog, " } // legacy with unwound type\n");
1673130803Smarcel	}
1674130803Smarcel      return prev;
1675130803Smarcel    }
1676130803Smarcel
1677130803Smarcel  /* NOTE: cagney/2002-11-18: The code segments, found in
1678130803Smarcel     create_new_frame and get_prev_frame(), that initializes the
1679130803Smarcel     frames type is subtly different.  The latter only updates ->type
1680130803Smarcel     when it encounters a SIGTRAMP_FRAME or DUMMY_FRAME.  This stops
1681130803Smarcel     get_prev_frame() overriding the frame's type when the INIT code
1682130803Smarcel     has previously set it.  This is really somewhat bogus.  The
1683130803Smarcel     initialization, as seen in create_new_frame(), should occur
1684130803Smarcel     before the INIT function has been called.  */
1685130803Smarcel  if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
1686130803Smarcel      && (DEPRECATED_PC_IN_CALL_DUMMY_P ()
1687130803Smarcel	  ? DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (prev), 0, 0)
1688130803Smarcel	  : pc_in_dummy_frame (get_frame_pc (prev))))
1689130803Smarcel    prev->type = DUMMY_FRAME;
1690130803Smarcel  else
1691130803Smarcel    {
1692130803Smarcel      /* FIXME: cagney/2002-11-10: This should be moved to before the
1693130803Smarcel	 INIT code above so that the INIT code knows what the frame's
1694130803Smarcel	 type is (in fact, for a [generic] dummy-frame, the type can
1695130803Smarcel	 be set and then the entire initialization can be skipped.
1696130803Smarcel	 Unforunatly, its the INIT code that sets the PC (Hmm, catch
1697130803Smarcel	 22).  */
1698130803Smarcel      char *name;
1699130803Smarcel      find_pc_partial_function (get_frame_pc (prev), &name, NULL, NULL);
1700130803Smarcel      if (PC_IN_SIGTRAMP (get_frame_pc (prev), name))
1701130803Smarcel	prev->type = SIGTRAMP_FRAME;
1702130803Smarcel      /* FIXME: cagney/2002-11-11: Leave prev->type alone.  Some
1703130803Smarcel         architectures are forcing the frame's type in INIT so we
1704130803Smarcel         don't want to override it here.  Remember, NORMAL_FRAME == 0,
1705130803Smarcel         so it all works (just :-/).  Once this initialization is
1706130803Smarcel         moved to the start of this function, all this nastness will
1707130803Smarcel         go away.  */
1708130803Smarcel    }
1709130803Smarcel
1710130803Smarcel  if (prev->type == NORMAL_FRAME)
1711130803Smarcel    prev->this_id.value.code_addr
1712130803Smarcel      = get_pc_function_start (prev->this_id.value.code_addr);
1713130803Smarcel
1714130803Smarcel  if (frame_debug)
1715130803Smarcel    {
1716130803Smarcel      fprintf_unfiltered (gdb_stdlog, "-> ");
1717130803Smarcel      fprint_frame (gdb_stdlog, prev);
1718130803Smarcel      fprintf_unfiltered (gdb_stdlog, " } // legacy with confused type\n");
1719130803Smarcel    }
1720130803Smarcel
1721130803Smarcel  return prev;
1722130803Smarcel}
1723130803Smarcel
1724130803Smarcel/* Return a structure containing various interesting information
1725130803Smarcel   about the frame that called THIS_FRAME.  Returns NULL
1726130803Smarcel   if there is no such frame.
1727130803Smarcel
1728130803Smarcel   This function tests some target-independent conditions that should
1729130803Smarcel   terminate the frame chain, such as unwinding past main().  It
1730130803Smarcel   should not contain any target-dependent tests, such as checking
1731130803Smarcel   whether the program-counter is zero.  */
1732130803Smarcel
1733130803Smarcelstruct frame_info *
1734130803Smarcelget_prev_frame (struct frame_info *this_frame)
1735130803Smarcel{
1736130803Smarcel  struct frame_info *prev_frame;
1737130803Smarcel
1738130803Smarcel  if (frame_debug)
1739130803Smarcel    {
1740130803Smarcel      fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1741130803Smarcel      if (this_frame != NULL)
1742130803Smarcel	fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1743130803Smarcel      else
1744130803Smarcel	fprintf_unfiltered (gdb_stdlog, "<NULL>");
1745130803Smarcel      fprintf_unfiltered (gdb_stdlog, ") ");
1746130803Smarcel    }
1747130803Smarcel
1748130803Smarcel  /* Return the inner-most frame, when the caller passes in NULL.  */
1749130803Smarcel  /* NOTE: cagney/2002-11-09: Not sure how this would happen.  The
1750130803Smarcel     caller should have previously obtained a valid frame using
1751130803Smarcel     get_selected_frame() and then called this code - only possibility
1752130803Smarcel     I can think of is code behaving badly.
1753130803Smarcel
1754130803Smarcel     NOTE: cagney/2003-01-10: Talk about code behaving badly.  Check
1755130803Smarcel     block_innermost_frame().  It does the sequence: frame = NULL;
1756130803Smarcel     while (1) { frame = get_prev_frame (frame); .... }.  Ulgh!  Why
1757130803Smarcel     it couldn't be written better, I don't know.
1758130803Smarcel
1759130803Smarcel     NOTE: cagney/2003-01-11: I suspect what is happening is
1760130803Smarcel     block_innermost_frame() is, when the target has no state
1761130803Smarcel     (registers, memory, ...), still calling this function.  The
1762130803Smarcel     assumption being that this function will return NULL indicating
1763130803Smarcel     that a frame isn't possible, rather than checking that the target
1764130803Smarcel     has state and then calling get_current_frame() and
1765130803Smarcel     get_prev_frame().  This is a guess mind.  */
1766130803Smarcel  if (this_frame == NULL)
1767130803Smarcel    {
1768130803Smarcel      /* NOTE: cagney/2002-11-09: There was a code segment here that
1769130803Smarcel	 would error out when CURRENT_FRAME was NULL.  The comment
1770130803Smarcel	 that went with it made the claim ...
1771130803Smarcel
1772130803Smarcel	 ``This screws value_of_variable, which just wants a nice
1773130803Smarcel	 clean NULL return from block_innermost_frame if there are no
1774130803Smarcel	 frames.  I don't think I've ever seen this message happen
1775130803Smarcel	 otherwise.  And returning NULL here is a perfectly legitimate
1776130803Smarcel	 thing to do.''
1777130803Smarcel
1778130803Smarcel         Per the above, this code shouldn't even be called with a NULL
1779130803Smarcel         THIS_FRAME.  */
1780130803Smarcel      return current_frame;
1781130803Smarcel    }
1782130803Smarcel
1783130803Smarcel  /* There is always a frame.  If this assertion fails, suspect that
1784130803Smarcel     something should be calling get_selected_frame() or
1785130803Smarcel     get_current_frame().  */
1786130803Smarcel  gdb_assert (this_frame != NULL);
1787130803Smarcel
1788130803Smarcel  /* Make sure we pass an address within THIS_FRAME's code block to
1789130803Smarcel     inside_main_func.  Otherwise, we might stop unwinding at a
1790130803Smarcel     function which has a call instruction as its last instruction if
1791130803Smarcel     that function immediately precedes main().  */
1792130803Smarcel  if (this_frame->level >= 0
1793130803Smarcel      && !backtrace_past_main
1794130803Smarcel      && inside_main_func (get_frame_address_in_block (this_frame)))
1795130803Smarcel    /* Don't unwind past main(), bug always unwind the sentinel frame.
1796130803Smarcel       Note, this is done _before_ the frame has been marked as
1797130803Smarcel       previously unwound.  That way if the user later decides to
1798130803Smarcel       allow unwinds past main(), that just happens.  */
1799130803Smarcel    {
1800130803Smarcel      if (frame_debug)
1801130803Smarcel	fprintf_unfiltered (gdb_stdlog, "-> NULL // inside main func }\n");
1802130803Smarcel      return NULL;
1803130803Smarcel    }
1804130803Smarcel
1805130803Smarcel  if (this_frame->level > backtrace_limit)
1806130803Smarcel    {
1807130803Smarcel      error ("Backtrace limit of %d exceeded", backtrace_limit);
1808130803Smarcel    }
1809130803Smarcel
1810130803Smarcel  /* If we're already inside the entry function for the main objfile,
1811130803Smarcel     then it isn't valid.  Don't apply this test to a dummy frame -
1812130803Smarcel     dummy frame PC's typically land in the entry func.  Don't apply
1813130803Smarcel     this test to the sentinel frame.  Sentinel frames should always
1814130803Smarcel     be allowed to unwind.  */
1815130803Smarcel  /* NOTE: cagney/2003-02-25: Don't enable until someone has found
1816130803Smarcel     hard evidence that this is needed.  */
1817130803Smarcel  /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func - wasn't
1818130803Smarcel     checking for "main" in the minimal symbols.  With that fixed
1819130803Smarcel     asm-source tests now stop in "main" instead of halting the
1820130803Smarcel     backtrace in wierd and wonderful ways somewhere inside the entry
1821130803Smarcel     file.  Suspect that deprecated_inside_entry_file and
1822130803Smarcel     inside_entry_func tests were added to work around that (now
1823130803Smarcel     fixed) case.  */
1824130803Smarcel  /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1825130803Smarcel     suggested having the inside_entry_func test use the
1826130803Smarcel     inside_main_func msymbol trick (along with entry_point_address I
1827130803Smarcel     guess) to determine the address range of the start function.
1828130803Smarcel     That should provide a far better stopper than the current
1829130803Smarcel     heuristics.  */
1830130803Smarcel  /* NOTE: cagney/2003-07-15: Need to add a "set backtrace
1831130803Smarcel     beyond-entry-func" command so that this can be selectively
1832130803Smarcel     disabled.  */
1833130803Smarcel  if (0
1834130803Smarcel#if 0
1835130803Smarcel      && backtrace_beyond_entry_func
1836130803Smarcel#endif
1837130803Smarcel      && this_frame->type != DUMMY_FRAME && this_frame->level >= 0
1838130803Smarcel      && inside_entry_func (this_frame))
1839130803Smarcel    {
1840130803Smarcel      if (frame_debug)
1841130803Smarcel	{
1842130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "-> ");
1843130803Smarcel	  fprint_frame (gdb_stdlog, NULL);
1844130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "// inside entry func }\n");
1845130803Smarcel	}
1846130803Smarcel      return NULL;
1847130803Smarcel    }
1848130803Smarcel
1849242943Semaste  /* Assume that the only way to get a zero PC is through something
1850242943Semaste     like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1851242943Semaste     will never unwind a zero PC.  */
1852242943Semaste  if (this_frame->level > 0
1853242943Semaste      && get_frame_type (this_frame) == NORMAL_FRAME
1854242943Semaste      && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1855242943Semaste      && get_frame_pc (this_frame) == 0)
1856242943Semaste    {
1857242943Semaste      if (frame_debug)
1858242943Semaste	{
1859242943Semaste	  fprintf_unfiltered (gdb_stdlog, "-> ");
1860242943Semaste	  fprint_frame (gdb_stdlog, this_frame->prev);
1861242943Semaste	  fprintf_unfiltered (gdb_stdlog, " // zero PC \n");
1862242943Semaste	}
1863242943Semaste      return NULL;
1864242943Semaste    }
1865242943Semaste
1866130803Smarcel  /* Only try to do the unwind once.  */
1867130803Smarcel  if (this_frame->prev_p)
1868130803Smarcel    {
1869130803Smarcel      if (frame_debug)
1870130803Smarcel	{
1871130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "-> ");
1872130803Smarcel	  fprint_frame (gdb_stdlog, this_frame->prev);
1873130803Smarcel	  fprintf_unfiltered (gdb_stdlog, " // cached \n");
1874130803Smarcel	}
1875130803Smarcel      return this_frame->prev;
1876130803Smarcel    }
1877130803Smarcel  this_frame->prev_p = 1;
1878130803Smarcel
1879130803Smarcel  /* If we're inside the entry file, it isn't valid.  Don't apply this
1880130803Smarcel     test to a dummy frame - dummy frame PC's typically land in the
1881130803Smarcel     entry file.  Don't apply this test to the sentinel frame.
1882130803Smarcel     Sentinel frames should always be allowed to unwind.  */
1883130803Smarcel  /* NOTE: drow/2002-12-25: should there be a way to disable this
1884130803Smarcel     check?  It assumes a single small entry file, and the way some
1885130803Smarcel     debug readers (e.g.  dbxread) figure out which object is the
1886130803Smarcel     entry file is somewhat hokey.  */
1887130803Smarcel  /* NOTE: cagney/2003-01-10: If there is a way of disabling this test
1888130803Smarcel     then it should probably be moved to before the ->prev_p test,
1889130803Smarcel     above.  */
1890130803Smarcel  /* NOTE: vinschen/2003-04-01: Disabled.  It turns out that the call
1891130803Smarcel     to deprecated_inside_entry_file destroys a meaningful backtrace
1892130803Smarcel     under some conditions.  E. g. the backtrace tests in the
1893130803Smarcel     asm-source testcase are broken for some targets.  In this test
1894130803Smarcel     the functions are all implemented as part of one file and the
1895130803Smarcel     testcase is not necessarily linked with a start file (depending
1896130803Smarcel     on the target).  What happens is, that the first frame is printed
1897130803Smarcel     normaly and following frames are treated as being inside the
1898130803Smarcel     enttry file then.  This way, only the #0 frame is printed in the
1899130803Smarcel     backtrace output.  */
1900130803Smarcel  if (0
1901130803Smarcel      && this_frame->type != DUMMY_FRAME && this_frame->level >= 0
1902130803Smarcel      && deprecated_inside_entry_file (get_frame_pc (this_frame)))
1903130803Smarcel    {
1904130803Smarcel      if (frame_debug)
1905130803Smarcel	{
1906130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "-> ");
1907130803Smarcel	  fprint_frame (gdb_stdlog, NULL);
1908130803Smarcel	  fprintf_unfiltered (gdb_stdlog, " // inside entry file }\n");
1909130803Smarcel	}
1910130803Smarcel      return NULL;
1911130803Smarcel    }
1912130803Smarcel
1913130803Smarcel  /* If any of the old frame initialization methods are around, use
1914130803Smarcel     the legacy get_prev_frame method.  */
1915130803Smarcel  if (legacy_frame_p (current_gdbarch))
1916130803Smarcel    {
1917130803Smarcel      prev_frame = legacy_get_prev_frame (this_frame);
1918130803Smarcel      return prev_frame;
1919130803Smarcel    }
1920130803Smarcel
1921130803Smarcel  /* Check that this frame's ID was valid.  If it wasn't, don't try to
1922130803Smarcel     unwind to the prev frame.  Be careful to not apply this test to
1923130803Smarcel     the sentinel frame.  */
1924130803Smarcel  if (this_frame->level >= 0 && !frame_id_p (get_frame_id (this_frame)))
1925130803Smarcel    {
1926130803Smarcel      if (frame_debug)
1927130803Smarcel	{
1928130803Smarcel	  fprintf_unfiltered (gdb_stdlog, "-> ");
1929130803Smarcel	  fprint_frame (gdb_stdlog, NULL);
1930130803Smarcel	  fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1931130803Smarcel	}
1932130803Smarcel      return NULL;
1933130803Smarcel    }
1934130803Smarcel
1935130803Smarcel  /* Check that this frame's ID isn't inner to (younger, below, next)
1936130803Smarcel     the next frame.  This happens when a frame unwind goes backwards.
1937130803Smarcel     Since the sentinel frame doesn't really exist, don't compare the
1938130803Smarcel     inner-most against that sentinel.  */
1939130803Smarcel  if (this_frame->level > 0
1940130803Smarcel      && frame_id_inner (get_frame_id (this_frame),
1941130803Smarcel			 get_frame_id (this_frame->next)))
1942130803Smarcel    error ("Previous frame inner to this frame (corrupt stack?)");
1943130803Smarcel
1944130803Smarcel  /* Check that this and the next frame are not identical.  If they
1945130803Smarcel     are, there is most likely a stack cycle.  As with the inner-than
1946130803Smarcel     test above, avoid comparing the inner-most and sentinel frames.  */
1947130803Smarcel  if (this_frame->level > 0
1948130803Smarcel      && frame_id_eq (get_frame_id (this_frame),
1949130803Smarcel		      get_frame_id (this_frame->next)))
1950130803Smarcel    error ("Previous frame identical to this frame (corrupt stack?)");
1951130803Smarcel
1952130803Smarcel  /* Allocate the new frame but do not wire it in to the frame chain.
1953130803Smarcel     Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1954130803Smarcel     frame->next to pull some fancy tricks (of course such code is, by
1955130803Smarcel     definition, recursive).  Try to prevent it.
1956130803Smarcel
1957130803Smarcel     There is no reason to worry about memory leaks, should the
1958130803Smarcel     remainder of the function fail.  The allocated memory will be
1959130803Smarcel     quickly reclaimed when the frame cache is flushed, and the `we've
1960130803Smarcel     been here before' check above will stop repeated memory
1961130803Smarcel     allocation calls.  */
1962130803Smarcel  prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1963130803Smarcel  prev_frame->level = this_frame->level + 1;
1964130803Smarcel
1965130803Smarcel  /* Don't yet compute ->unwind (and hence ->type).  It is computed
1966130803Smarcel     on-demand in get_frame_type, frame_register_unwind, and
1967130803Smarcel     get_frame_id.  */
1968130803Smarcel
1969130803Smarcel  /* Don't yet compute the frame's ID.  It is computed on-demand by
1970130803Smarcel     get_frame_id().  */
1971130803Smarcel
1972130803Smarcel  /* The unwound frame ID is validate at the start of this function,
1973130803Smarcel     as part of the logic to decide if that frame should be further
1974130803Smarcel     unwound, and not here while the prev frame is being created.
1975130803Smarcel     Doing this makes it possible for the user to examine a frame that
1976130803Smarcel     has an invalid frame ID.
1977130803Smarcel
1978130803Smarcel     Some very old VAX code noted: [...]  For the sake of argument,
1979130803Smarcel     suppose that the stack is somewhat trashed (which is one reason
1980130803Smarcel     that "info frame" exists).  So, return 0 (indicating we don't
1981130803Smarcel     know the address of the arglist) if we don't know what frame this
1982130803Smarcel     frame calls.  */
1983130803Smarcel
1984130803Smarcel  /* Link it in.  */
1985130803Smarcel  this_frame->prev = prev_frame;
1986130803Smarcel  prev_frame->next = this_frame;
1987130803Smarcel
1988130803Smarcel  if (frame_debug)
1989130803Smarcel    {
1990130803Smarcel      fprintf_unfiltered (gdb_stdlog, "-> ");
1991130803Smarcel      fprint_frame (gdb_stdlog, prev_frame);
1992130803Smarcel      fprintf_unfiltered (gdb_stdlog, " }\n");
1993130803Smarcel    }
1994130803Smarcel
1995130803Smarcel  return prev_frame;
1996130803Smarcel}
1997130803Smarcel
1998130803SmarcelCORE_ADDR
1999130803Smarcelget_frame_pc (struct frame_info *frame)
2000130803Smarcel{
2001130803Smarcel  gdb_assert (frame->next != NULL);
2002130803Smarcel  return frame_pc_unwind (frame->next);
2003130803Smarcel}
2004130803Smarcel
2005130803Smarcel/* Return an address of that falls within the frame's code block.  */
2006130803Smarcel
2007130803SmarcelCORE_ADDR
2008130803Smarcelframe_unwind_address_in_block (struct frame_info *next_frame)
2009130803Smarcel{
2010130803Smarcel  /* A draft address.  */
2011130803Smarcel  CORE_ADDR pc = frame_pc_unwind (next_frame);
2012130803Smarcel
2013130803Smarcel  /* If THIS frame is not inner most (i.e., NEXT isn't the sentinel),
2014130803Smarcel     and NEXT is `normal' (i.e., not a sigtramp, dummy, ....) THIS
2015130803Smarcel     frame's PC ends up pointing at the instruction fallowing the
2016130803Smarcel     "call".  Adjust that PC value so that it falls on the call
2017130803Smarcel     instruction (which, hopefully, falls within THIS frame's code
2018130803Smarcel     block.  So far it's proved to be a very good approximation.  See
2019130803Smarcel     get_frame_type for why ->type can't be used.  */
2020130803Smarcel  if (next_frame->level >= 0
2021130803Smarcel      && get_frame_type (next_frame) == NORMAL_FRAME)
2022130803Smarcel    --pc;
2023130803Smarcel  return pc;
2024130803Smarcel}
2025130803Smarcel
2026130803SmarcelCORE_ADDR
2027130803Smarcelget_frame_address_in_block (struct frame_info *this_frame)
2028130803Smarcel{
2029130803Smarcel  return frame_unwind_address_in_block (this_frame->next);
2030130803Smarcel}
2031130803Smarcel
203298944Sobrienstatic int
2033130803Smarcelpc_notcurrent (struct frame_info *frame)
203498944Sobrien{
2035130803Smarcel  /* If FRAME is not the innermost frame, that normally means that
2036130803Smarcel     FRAME->pc points at the return instruction (which is *after* the
2037130803Smarcel     call instruction), and we want to get the line containing the
2038130803Smarcel     call (because the call is where the user thinks the program is).
2039130803Smarcel     However, if the next frame is either a SIGTRAMP_FRAME or a
2040130803Smarcel     DUMMY_FRAME, then the next frame will contain a saved interrupt
2041130803Smarcel     PC and such a PC indicates the current (rather than next)
2042130803Smarcel     instruction/line, consequently, for such cases, want to get the
2043130803Smarcel     line containing fi->pc.  */
2044130803Smarcel  struct frame_info *next = get_next_frame (frame);
2045130803Smarcel  int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME);
2046130803Smarcel  return notcurrent;
2047130803Smarcel}
2048130803Smarcel
2049130803Smarcelvoid
2050130803Smarcelfind_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
2051130803Smarcel{
2052130803Smarcel  (*sal) = find_pc_line (get_frame_pc (frame), pc_notcurrent (frame));
2053130803Smarcel}
2054130803Smarcel
2055130803Smarcel/* Per "frame.h", return the ``address'' of the frame.  Code should
2056130803Smarcel   really be using get_frame_id().  */
2057130803SmarcelCORE_ADDR
2058130803Smarcelget_frame_base (struct frame_info *fi)
2059130803Smarcel{
2060130803Smarcel  return get_frame_id (fi).stack_addr;
2061130803Smarcel}
2062130803Smarcel
2063130803Smarcel/* High-level offsets into the frame.  Used by the debug info.  */
2064130803Smarcel
2065130803SmarcelCORE_ADDR
2066130803Smarcelget_frame_base_address (struct frame_info *fi)
2067130803Smarcel{
2068130803Smarcel  if (get_frame_type (fi) != NORMAL_FRAME)
2069130803Smarcel    return 0;
2070130803Smarcel  if (fi->base == NULL)
2071130803Smarcel    fi->base = frame_base_find_by_frame (fi->next);
2072130803Smarcel  /* Sneaky: If the low-level unwind and high-level base code share a
2073130803Smarcel     common unwinder, let them share the prologue cache.  */
2074130803Smarcel  if (fi->base->unwind == fi->unwind)
2075130803Smarcel    return fi->base->this_base (fi->next, &fi->prologue_cache);
2076130803Smarcel  return fi->base->this_base (fi->next, &fi->base_cache);
2077130803Smarcel}
2078130803Smarcel
2079130803SmarcelCORE_ADDR
2080130803Smarcelget_frame_locals_address (struct frame_info *fi)
2081130803Smarcel{
2082130803Smarcel  void **cache;
2083130803Smarcel  if (get_frame_type (fi) != NORMAL_FRAME)
2084130803Smarcel    return 0;
2085130803Smarcel  /* If there isn't a frame address method, find it.  */
2086130803Smarcel  if (fi->base == NULL)
2087130803Smarcel    fi->base = frame_base_find_by_frame (fi->next);
2088130803Smarcel  /* Sneaky: If the low-level unwind and high-level base code share a
2089130803Smarcel     common unwinder, let them share the prologue cache.  */
2090130803Smarcel  if (fi->base->unwind == fi->unwind)
2091130803Smarcel    cache = &fi->prologue_cache;
2092130803Smarcel  else
2093130803Smarcel    cache = &fi->base_cache;
2094130803Smarcel  return fi->base->this_locals (fi->next, cache);
2095130803Smarcel}
2096130803Smarcel
2097130803SmarcelCORE_ADDR
2098130803Smarcelget_frame_args_address (struct frame_info *fi)
2099130803Smarcel{
2100130803Smarcel  void **cache;
2101130803Smarcel  if (get_frame_type (fi) != NORMAL_FRAME)
2102130803Smarcel    return 0;
2103130803Smarcel  /* If there isn't a frame address method, find it.  */
2104130803Smarcel  if (fi->base == NULL)
2105130803Smarcel    fi->base = frame_base_find_by_frame (fi->next);
2106130803Smarcel  /* Sneaky: If the low-level unwind and high-level base code share a
2107130803Smarcel     common unwinder, let them share the prologue cache.  */
2108130803Smarcel  if (fi->base->unwind == fi->unwind)
2109130803Smarcel    cache = &fi->prologue_cache;
2110130803Smarcel  else
2111130803Smarcel    cache = &fi->base_cache;
2112130803Smarcel  return fi->base->this_args (fi->next, cache);
2113130803Smarcel}
2114130803Smarcel
2115130803Smarcel/* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2116130803Smarcel   or -1 for a NULL frame.  */
2117130803Smarcel
2118130803Smarcelint
2119130803Smarcelframe_relative_level (struct frame_info *fi)
2120130803Smarcel{
2121130803Smarcel  if (fi == NULL)
2122130803Smarcel    return -1;
2123130803Smarcel  else
2124130803Smarcel    return fi->level;
2125130803Smarcel}
2126130803Smarcel
2127130803Smarcelenum frame_type
2128130803Smarcelget_frame_type (struct frame_info *frame)
2129130803Smarcel{
2130130803Smarcel  /* Some targets still don't use [generic] dummy frames.  Catch them
2131130803Smarcel     here.  */
2132130803Smarcel  if (!DEPRECATED_USE_GENERIC_DUMMY_FRAMES
2133130803Smarcel      && deprecated_frame_in_dummy (frame))
2134130803Smarcel    return DUMMY_FRAME;
2135130803Smarcel
2136130803Smarcel  /* Some legacy code, e.g, mips_init_extra_frame_info() wants
2137130803Smarcel     to determine the frame's type prior to it being completely
2138130803Smarcel     initialized.  Don't attempt to lazily initialize ->unwind for
2139130803Smarcel     legacy code.  It will be initialized in legacy_get_prev_frame().  */
2140130803Smarcel  if (frame->unwind == NULL && !legacy_frame_p (current_gdbarch))
214198944Sobrien    {
2142130803Smarcel      /* Initialize the frame's unwinder because it is that which
2143130803Smarcel         provides the frame's type.  */
2144130803Smarcel      frame->unwind = frame_unwind_find_by_frame (frame->next);
2145130803Smarcel      /* FIXME: cagney/2003-04-02: Rather than storing the frame's
2146130803Smarcel	 type in the frame, the unwinder's type should be returned
2147130803Smarcel	 directly.  Unfortunately, legacy code, called by
2148130803Smarcel	 legacy_get_prev_frame, explicitly set the frames type using
2149130803Smarcel	 the method deprecated_set_frame_type().  */
2150130803Smarcel      frame->type = frame->unwind->type;
2151130803Smarcel    }
2152130803Smarcel  if (frame->type == UNKNOWN_FRAME)
2153130803Smarcel    return NORMAL_FRAME;
2154130803Smarcel  else
2155130803Smarcel    return frame->type;
2156130803Smarcel}
215798944Sobrien
2158130803Smarcelvoid
2159130803Smarceldeprecated_set_frame_type (struct frame_info *frame, enum frame_type type)
2160130803Smarcel{
2161130803Smarcel  /* Arrrg!  See comment in "frame.h".  */
2162130803Smarcel  frame->type = type;
2163130803Smarcel}
2164130803Smarcel
2165130803Smarcelstruct frame_extra_info *
2166130803Smarcelget_frame_extra_info (struct frame_info *fi)
2167130803Smarcel{
2168130803Smarcel  return fi->extra_info;
2169130803Smarcel}
2170130803Smarcel
2171130803Smarcelstruct frame_extra_info *
2172130803Smarcelframe_extra_info_zalloc (struct frame_info *fi, long size)
2173130803Smarcel{
2174130803Smarcel  fi->extra_info = frame_obstack_zalloc (size);
2175130803Smarcel  return fi->extra_info;
2176130803Smarcel}
2177130803Smarcel
2178130803Smarcelvoid
2179130803Smarceldeprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc)
2180130803Smarcel{
2181130803Smarcel  if (frame_debug)
2182130803Smarcel    fprintf_unfiltered (gdb_stdlog,
2183130803Smarcel			"{ deprecated_update_frame_pc_hack (frame=%d,pc=0x%s) }\n",
2184130803Smarcel			frame->level, paddr_nz (pc));
2185130803Smarcel  /* NOTE: cagney/2003-03-11: Some architectures (e.g., Arm) are
2186130803Smarcel     maintaining a locally allocated frame object.  Since such frame's
2187130803Smarcel     are not in the frame chain, it isn't possible to assume that the
2188130803Smarcel     frame has a next.  Sigh.  */
2189130803Smarcel  if (frame->next != NULL)
2190130803Smarcel    {
2191130803Smarcel      /* While we're at it, update this frame's cached PC value, found
2192130803Smarcel	 in the next frame.  Oh for the day when "struct frame_info"
2193130803Smarcel	 is opaque and this hack on hack can just go away.  */
2194130803Smarcel      frame->next->prev_pc.value = pc;
2195130803Smarcel      frame->next->prev_pc.p = 1;
219698944Sobrien    }
2197130803Smarcel}
219898944Sobrien
2199130803Smarcelvoid
2200130803Smarceldeprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base)
2201130803Smarcel{
2202130803Smarcel  if (frame_debug)
2203130803Smarcel    fprintf_unfiltered (gdb_stdlog,
2204130803Smarcel			"{ deprecated_update_frame_base_hack (frame=%d,base=0x%s) }\n",
2205130803Smarcel			frame->level, paddr_nz (base));
2206130803Smarcel  /* See comment in "frame.h".  */
2207130803Smarcel  frame->this_id.value.stack_addr = base;
2208130803Smarcel}
220998944Sobrien
2210130803Smarcelstruct frame_info *
2211130803Smarceldeprecated_frame_xmalloc_with_cleanup (long sizeof_saved_regs,
2212130803Smarcel				       long sizeof_extra_info)
2213130803Smarcel{
2214130803Smarcel  struct frame_info *frame = XMALLOC (struct frame_info);
2215130803Smarcel  memset (frame, 0, sizeof (*frame));
2216130803Smarcel  frame->this_id.p = 1;
2217130803Smarcel  make_cleanup (xfree, frame);
2218130803Smarcel  if (sizeof_saved_regs > 0)
2219130803Smarcel    {
2220130803Smarcel      frame->saved_regs = xcalloc (1, sizeof_saved_regs);
2221130803Smarcel      make_cleanup (xfree, frame->saved_regs);
2222130803Smarcel    }
2223130803Smarcel  if (sizeof_extra_info > 0)
2224130803Smarcel    {
2225130803Smarcel      frame->extra_info = xcalloc (1, sizeof_extra_info);
2226130803Smarcel      make_cleanup (xfree, frame->extra_info);
2227130803Smarcel    }
2228130803Smarcel  return frame;
2229130803Smarcel}
223098944Sobrien
2231130803Smarcel/* Memory access methods.  */
2232130803Smarcel
2233130803Smarcelvoid
2234130803Smarcelget_frame_memory (struct frame_info *this_frame, CORE_ADDR addr, void *buf,
2235130803Smarcel		  int len)
2236130803Smarcel{
2237130803Smarcel  read_memory (addr, buf, len);
223898944Sobrien}
223998944Sobrien
2240130803SmarcelLONGEST
2241130803Smarcelget_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
2242130803Smarcel			 int len)
2243130803Smarcel{
2244130803Smarcel  return read_memory_integer (addr, len);
2245130803Smarcel}
224698944Sobrien
2247130803SmarcelULONGEST
2248130803Smarcelget_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
2249130803Smarcel			   int len)
2250130803Smarcel{
2251130803Smarcel  return read_memory_unsigned_integer (addr, len);
2252130803Smarcel}
225398944Sobrien
2254130803Smarcel/* Architecture method.  */
225598944Sobrien
2256130803Smarcelstruct gdbarch *
2257130803Smarcelget_frame_arch (struct frame_info *this_frame)
2258130803Smarcel{
2259130803Smarcel  return current_gdbarch;
2260130803Smarcel}
2261130803Smarcel
2262130803Smarcel/* Stack pointer methods.  */
2263130803Smarcel
2264130803SmarcelCORE_ADDR
2265130803Smarcelget_frame_sp (struct frame_info *this_frame)
2266130803Smarcel{
2267130803Smarcel  return frame_sp_unwind (this_frame->next);
2268130803Smarcel}
2269130803Smarcel
2270130803SmarcelCORE_ADDR
2271130803Smarcelframe_sp_unwind (struct frame_info *next_frame)
2272130803Smarcel{
2273130803Smarcel  /* Normality, an architecture that provides a way of obtaining any
2274130803Smarcel     frame inner-most address.  */
2275130803Smarcel  if (gdbarch_unwind_sp_p (current_gdbarch))
2276130803Smarcel    return gdbarch_unwind_sp (current_gdbarch, next_frame);
2277130803Smarcel  /* Things are looking grim.  If it's the inner-most frame and there
2278130803Smarcel     is a TARGET_READ_SP then that can be used.  */
2279130803Smarcel  if (next_frame->level < 0 && TARGET_READ_SP_P ())
2280130803Smarcel    return TARGET_READ_SP ();
2281130803Smarcel  /* Now things are really are grim.  Hope that the value returned by
2282130803Smarcel     the SP_REGNUM register is meaningful.  */
2283130803Smarcel  if (SP_REGNUM >= 0)
2284130803Smarcel    {
2285130803Smarcel      ULONGEST sp;
2286130803Smarcel      frame_unwind_unsigned_register (next_frame, SP_REGNUM, &sp);
2287130803Smarcel      return sp;
2288130803Smarcel    }
2289130803Smarcel  internal_error (__FILE__, __LINE__, "Missing unwind SP method");
2290130803Smarcel}
2291130803Smarcel
2292130803Smarcel
229398944Sobrienint
2294130803Smarcellegacy_frame_p (struct gdbarch *current_gdbarch)
229598944Sobrien{
2296130803Smarcel  if (DEPRECATED_INIT_FRAME_PC_P ()
2297130803Smarcel      || DEPRECATED_INIT_FRAME_PC_FIRST_P ()
2298130803Smarcel      || DEPRECATED_INIT_EXTRA_FRAME_INFO_P ()
2299130803Smarcel      || DEPRECATED_FRAME_CHAIN_P ())
2300130803Smarcel    /* No question, it's a legacy frame.  */
2301130803Smarcel    return 1;
2302130803Smarcel  if (gdbarch_unwind_dummy_id_p (current_gdbarch))
2303130803Smarcel    /* No question, it's not a legacy frame (provided none of the
2304130803Smarcel       deprecated methods checked above are present that is).  */
2305130803Smarcel    return 0;
2306130803Smarcel  if (DEPRECATED_TARGET_READ_FP_P ()
2307130803Smarcel      || DEPRECATED_FP_REGNUM >= 0)
2308130803Smarcel    /* Assume it's legacy.  If you're trying to convert a legacy frame
2309130803Smarcel       target to the new mechanism, get rid of these.  legacy
2310130803Smarcel       get_prev_frame requires these when unwind_frame_id isn't
2311130803Smarcel       available.  */
2312130803Smarcel    return 1;
2313130803Smarcel  /* Default to assuming that it's brand new code, and hence not
2314130803Smarcel     legacy.  Force it down the non-legacy path so that the new code
2315130803Smarcel     uses the new frame mechanism from day one.  Dummy frame's won't
2316130803Smarcel     work very well but we can live with that.  */
2317130803Smarcel  return 0;
231898944Sobrien}
2319130803Smarcel
2320130803Smarcelextern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
2321130803Smarcel
2322130803Smarcelstatic struct cmd_list_element *set_backtrace_cmdlist;
2323130803Smarcelstatic struct cmd_list_element *show_backtrace_cmdlist;
2324130803Smarcel
2325130803Smarcelstatic void
2326130803Smarcelset_backtrace_cmd (char *args, int from_tty)
2327130803Smarcel{
2328130803Smarcel  help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
2329130803Smarcel}
2330130803Smarcel
2331130803Smarcelstatic void
2332130803Smarcelshow_backtrace_cmd (char *args, int from_tty)
2333130803Smarcel{
2334130803Smarcel  cmd_show_list (show_backtrace_cmdlist, from_tty, "");
2335130803Smarcel}
2336130803Smarcel
2337130803Smarcelvoid
2338130803Smarcel_initialize_frame (void)
2339130803Smarcel{
2340130803Smarcel  obstack_init (&frame_cache_obstack);
2341130803Smarcel
2342130803Smarcel  add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, "\
2343130803SmarcelSet backtrace specific variables.\n\
2344130803SmarcelConfigure backtrace variables such as the backtrace limit",
2345130803Smarcel		  &set_backtrace_cmdlist, "set backtrace ",
2346130803Smarcel		  0/*allow-unknown*/, &setlist);
2347130803Smarcel  add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, "\
2348130803SmarcelShow backtrace specific variables\n\
2349130803SmarcelShow backtrace variables such as the backtrace limit",
2350130803Smarcel		  &show_backtrace_cmdlist, "show backtrace ",
2351130803Smarcel		  0/*allow-unknown*/, &showlist);
2352130803Smarcel
2353130803Smarcel  add_setshow_boolean_cmd ("past-main", class_obscure,
2354130803Smarcel			   &backtrace_past_main, "\
2355130803SmarcelSet whether backtraces should continue past \"main\".\n\
2356130803SmarcelNormally the caller of \"main\" is not of interest, so GDB will terminate\n\
2357130803Smarcelthe backtrace at \"main\".  Set this variable if you need to see the rest\n\
2358130803Smarcelof the stack trace.", "\
2359130803SmarcelShow whether backtraces should continue past \"main\".\n\
2360130803SmarcelNormally the caller of \"main\" is not of interest, so GDB will terminate\n\
2361130803Smarcelthe backtrace at \"main\".  Set this variable if you need to see the rest\n\
2362130803Smarcelof the stack trace.",
2363130803Smarcel			   NULL, NULL, &set_backtrace_cmdlist,
2364130803Smarcel			   &show_backtrace_cmdlist);
2365130803Smarcel
2366130803Smarcel  add_setshow_uinteger_cmd ("limit", class_obscure,
2367130803Smarcel			    &backtrace_limit, "\
2368130803SmarcelSet an upper bound on the number of backtrace levels.\n\
2369130803SmarcelNo more than the specified number of frames can be displayed or examined.\n\
2370130803SmarcelZero is unlimited.", "\
2371130803SmarcelShow the upper bound on the number of backtrace levels.",
2372130803Smarcel			    NULL, NULL, &set_backtrace_cmdlist,
2373130803Smarcel			    &show_backtrace_cmdlist);
2374130803Smarcel
2375130803Smarcel  /* Debug this files internals. */
2376130803Smarcel  add_show_from_set (add_set_cmd ("frame", class_maintenance, var_zinteger,
2377130803Smarcel				  &frame_debug, "Set frame debugging.\n\
2378130803SmarcelWhen non-zero, frame specific internal debugging is enabled.", &setdebuglist),
2379130803Smarcel		     &showdebuglist);
2380130803Smarcel}
2381