Phase 1 of the ptid_t changes.
[external/binutils.git] / gdb / frame.c
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2    Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "target.h"
25 #include "value.h"
26 #include "inferior.h"   /* for inferior_ptid */
27 #include "regcache.h"
28
29 /* FIND_SAVED_REGISTER ()
30
31    Return the address in which frame FRAME's value of register REGNUM
32    has been saved in memory.  Or return zero if it has not been saved.
33    If REGNUM specifies the SP, the value we return is actually
34    the SP value, not an address where it was saved.  */
35
36 CORE_ADDR
37 find_saved_register (struct frame_info *frame, int regnum)
38 {
39   register struct frame_info *frame1 = NULL;
40   register CORE_ADDR addr = 0;
41
42   if (frame == NULL)            /* No regs saved if want current frame */
43     return 0;
44
45 #ifdef HAVE_REGISTER_WINDOWS
46   /* We assume that a register in a register window will only be saved
47      in one place (since the name changes and/or disappears as you go
48      towards inner frames), so we only call get_frame_saved_regs on
49      the current frame.  This is directly in contradiction to the
50      usage below, which assumes that registers used in a frame must be
51      saved in a lower (more interior) frame.  This change is a result
52      of working on a register window machine; get_frame_saved_regs
53      always returns the registers saved within a frame, within the
54      context (register namespace) of that frame. */
55
56   /* However, note that we don't want this to return anything if
57      nothing is saved (if there's a frame inside of this one).  Also,
58      callers to this routine asking for the stack pointer want the
59      stack pointer saved for *this* frame; this is returned from the
60      next frame.  */
61
62   if (REGISTER_IN_WINDOW_P (regnum))
63     {
64       frame1 = get_next_frame (frame);
65       if (!frame1)
66         return 0;               /* Registers of this frame are active.  */
67
68       /* Get the SP from the next frame in; it will be this
69          current frame.  */
70       if (regnum != SP_REGNUM)
71         frame1 = frame;
72
73       FRAME_INIT_SAVED_REGS (frame1);
74       return frame1->saved_regs[regnum];        /* ... which might be zero */
75     }
76 #endif /* HAVE_REGISTER_WINDOWS */
77
78   /* Note that this next routine assumes that registers used in
79      frame x will be saved only in the frame that x calls and
80      frames interior to it.  This is not true on the sparc, but the
81      above macro takes care of it, so we should be all right. */
82   while (1)
83     {
84       QUIT;
85       frame1 = get_prev_frame (frame1);
86       if (frame1 == 0 || frame1 == frame)
87         break;
88       FRAME_INIT_SAVED_REGS (frame1);
89       if (frame1->saved_regs[regnum])
90         addr = frame1->saved_regs[regnum];
91     }
92
93   return addr;
94 }
95
96 /* DEFAULT_GET_SAVED_REGISTER ()
97
98    Find register number REGNUM relative to FRAME and put its (raw,
99    target format) contents in *RAW_BUFFER.  Set *OPTIMIZED if the
100    variable was optimized out (and thus can't be fetched).  Set *LVAL
101    to lval_memory, lval_register, or not_lval, depending on whether
102    the value was fetched from memory, from a register, or in a strange
103    and non-modifiable way (e.g. a frame pointer which was calculated
104    rather than fetched).  Set *ADDRP to the address, either in memory
105    on as a REGISTER_BYTE offset into the registers array.
106
107    Note that this implementation never sets *LVAL to not_lval.  But
108    it can be replaced by defining GET_SAVED_REGISTER and supplying
109    your own.
110
111    The argument RAW_BUFFER must point to aligned memory.  */
112
113 static void
114 default_get_saved_register (char *raw_buffer,
115                             int *optimized,
116                             CORE_ADDR *addrp,
117                             struct frame_info *frame,
118                             int regnum,
119                             enum lval_type *lval)
120 {
121   CORE_ADDR addr;
122
123   if (!target_has_registers)
124     error ("No registers.");
125
126   /* Normal systems don't optimize out things with register numbers.  */
127   if (optimized != NULL)
128     *optimized = 0;
129   addr = find_saved_register (frame, regnum);
130   if (addr != 0)
131     {
132       if (lval != NULL)
133         *lval = lval_memory;
134       if (regnum == SP_REGNUM)
135         {
136           if (raw_buffer != NULL)
137             {
138               /* Put it back in target format.  */
139               store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
140                              (LONGEST) addr);
141             }
142           if (addrp != NULL)
143             *addrp = 0;
144           return;
145         }
146       if (raw_buffer != NULL)
147         target_read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
148     }
149   else
150     {
151       if (lval != NULL)
152         *lval = lval_register;
153       addr = REGISTER_BYTE (regnum);
154       if (raw_buffer != NULL)
155         read_register_gen (regnum, raw_buffer);
156     }
157   if (addrp != NULL)
158     *addrp = addr;
159 }
160
161 #if !defined (GET_SAVED_REGISTER)
162 #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
163   default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
164 #endif
165
166 void
167 get_saved_register (char *raw_buffer,
168                     int *optimized,
169                     CORE_ADDR *addrp,
170                     struct frame_info *frame,
171                     int regnum,
172                     enum lval_type *lval)
173 {
174   GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval);
175 }
176
177 /* READ_RELATIVE_REGISTER_RAW_BYTES_FOR_FRAME
178
179    Copy the bytes of register REGNUM, relative to the input stack frame,
180    into our memory at MYADDR, in target byte order.
181    The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
182
183    Returns 1 if could not be read, 0 if could.  */
184
185 /* FIXME: This function increases the confusion between FP_REGNUM
186    and the virtual/pseudo-frame pointer.  */
187
188 static int
189 read_relative_register_raw_bytes_for_frame (int regnum,
190                                             char *myaddr,
191                                             struct frame_info *frame)
192 {
193   int optim;
194   if (regnum == FP_REGNUM && frame)
195     {
196       /* Put it back in target format. */
197       store_address (myaddr, REGISTER_RAW_SIZE (FP_REGNUM),
198                      (LONGEST) FRAME_FP (frame));
199
200       return 0;
201     }
202
203   get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame,
204                       regnum, (enum lval_type *) NULL);
205
206   if (register_cached (regnum) < 0)
207     return 1;                   /* register value not available */
208
209   return optim;
210 }
211
212 /* READ_RELATIVE_REGISTER_RAW_BYTES
213
214    Copy the bytes of register REGNUM, relative to the current stack
215    frame, into our memory at MYADDR, in target byte order.  
216    The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
217
218    Returns 1 if could not be read, 0 if could.  */
219
220 int
221 read_relative_register_raw_bytes (int regnum, char *myaddr)
222 {
223   return read_relative_register_raw_bytes_for_frame (regnum, myaddr,
224                                                      selected_frame);
225 }