Commit | Line | Data |
---|---|---|
4f460812 | 1 | /* Cache and manage frames for GDB, the GNU debugger. |
96cb11df AC |
2 | |
3 | Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, | |
4 | 2001, 2002 Free Software Foundation, Inc. | |
d65fe839 AC |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "defs.h" | |
24 | #include "frame.h" | |
25 | #include "target.h" | |
26 | #include "value.h" | |
39f77062 | 27 | #include "inferior.h" /* for inferior_ptid */ |
4e052eda | 28 | #include "regcache.h" |
4f460812 | 29 | #include "gdb_assert.h" |
d65fe839 AC |
30 | |
31 | /* FIND_SAVED_REGISTER () | |
32 | ||
33 | Return the address in which frame FRAME's value of register REGNUM | |
34 | has been saved in memory. Or return zero if it has not been saved. | |
35 | If REGNUM specifies the SP, the value we return is actually | |
36 | the SP value, not an address where it was saved. */ | |
37 | ||
38 | CORE_ADDR | |
39 | find_saved_register (struct frame_info *frame, int regnum) | |
40 | { | |
41 | register struct frame_info *frame1 = NULL; | |
42 | register CORE_ADDR addr = 0; | |
43 | ||
44 | if (frame == NULL) /* No regs saved if want current frame */ | |
45 | return 0; | |
46 | ||
d65fe839 AC |
47 | /* Note that this next routine assumes that registers used in |
48 | frame x will be saved only in the frame that x calls and | |
49 | frames interior to it. This is not true on the sparc, but the | |
50 | above macro takes care of it, so we should be all right. */ | |
51 | while (1) | |
52 | { | |
53 | QUIT; | |
aa40ec90 JT |
54 | frame1 = get_next_frame (frame); |
55 | if (frame1 == 0) | |
d65fe839 | 56 | break; |
aa40ec90 | 57 | frame = frame1; |
d65fe839 AC |
58 | FRAME_INIT_SAVED_REGS (frame1); |
59 | if (frame1->saved_regs[regnum]) | |
60 | addr = frame1->saved_regs[regnum]; | |
61 | } | |
62 | ||
63 | return addr; | |
64 | } | |
65 | ||
66 | /* DEFAULT_GET_SAVED_REGISTER () | |
67 | ||
68 | Find register number REGNUM relative to FRAME and put its (raw, | |
69 | target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the | |
70 | variable was optimized out (and thus can't be fetched). Set *LVAL | |
71 | to lval_memory, lval_register, or not_lval, depending on whether | |
72 | the value was fetched from memory, from a register, or in a strange | |
73 | and non-modifiable way (e.g. a frame pointer which was calculated | |
74 | rather than fetched). Set *ADDRP to the address, either in memory | |
75 | on as a REGISTER_BYTE offset into the registers array. | |
76 | ||
77 | Note that this implementation never sets *LVAL to not_lval. But | |
78 | it can be replaced by defining GET_SAVED_REGISTER and supplying | |
79 | your own. | |
80 | ||
81 | The argument RAW_BUFFER must point to aligned memory. */ | |
82 | ||
83 | static void | |
84 | default_get_saved_register (char *raw_buffer, | |
85 | int *optimized, | |
86 | CORE_ADDR *addrp, | |
87 | struct frame_info *frame, | |
88 | int regnum, | |
89 | enum lval_type *lval) | |
90 | { | |
91 | CORE_ADDR addr; | |
92 | ||
93 | if (!target_has_registers) | |
94 | error ("No registers."); | |
95 | ||
96 | /* Normal systems don't optimize out things with register numbers. */ | |
97 | if (optimized != NULL) | |
98 | *optimized = 0; | |
99 | addr = find_saved_register (frame, regnum); | |
100 | if (addr != 0) | |
101 | { | |
102 | if (lval != NULL) | |
103 | *lval = lval_memory; | |
104 | if (regnum == SP_REGNUM) | |
105 | { | |
106 | if (raw_buffer != NULL) | |
107 | { | |
108 | /* Put it back in target format. */ | |
109 | store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), | |
110 | (LONGEST) addr); | |
111 | } | |
112 | if (addrp != NULL) | |
113 | *addrp = 0; | |
114 | return; | |
115 | } | |
116 | if (raw_buffer != NULL) | |
117 | target_read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum)); | |
118 | } | |
119 | else | |
120 | { | |
121 | if (lval != NULL) | |
122 | *lval = lval_register; | |
123 | addr = REGISTER_BYTE (regnum); | |
124 | if (raw_buffer != NULL) | |
125 | read_register_gen (regnum, raw_buffer); | |
126 | } | |
127 | if (addrp != NULL) | |
128 | *addrp = addr; | |
129 | } | |
130 | ||
4f460812 AC |
131 | void |
132 | frame_register_unwind (struct frame_info *frame, int regnum, | |
133 | int *optimizedp, enum lval_type *lvalp, | |
134 | CORE_ADDR *addrp, int *realnump, void *bufferp) | |
135 | { | |
136 | struct frame_unwind_cache *cache; | |
137 | ||
138 | /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates | |
139 | that the value proper does not need to be fetched. */ | |
140 | gdb_assert (optimizedp != NULL); | |
141 | gdb_assert (lvalp != NULL); | |
142 | gdb_assert (addrp != NULL); | |
143 | gdb_assert (realnump != NULL); | |
144 | /* gdb_assert (bufferp != NULL); */ | |
145 | ||
146 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a | |
147 | special case, there was always an inner frame dedicated to the | |
148 | hardware registers. Unfortunatly, there is too much unwind code | |
149 | around that looks up/down the frame chain while making the | |
150 | assumption that each frame level is using the same unwind code. */ | |
151 | ||
152 | if (frame == NULL) | |
153 | { | |
154 | /* We're in the inner-most frame, get the value direct from the | |
155 | register cache. */ | |
156 | *optimizedp = 0; | |
157 | *lvalp = lval_register; | |
158 | *addrp = 0; | |
159 | /* Should this code test ``register_cached (regnum) < 0'' and do | |
160 | something like set realnum to -1 when the register isn't | |
161 | available? */ | |
162 | *realnump = regnum; | |
163 | if (bufferp) | |
164 | read_register_gen (regnum, bufferp); | |
165 | return; | |
166 | } | |
167 | ||
168 | /* Ask this frame to unwind its register. */ | |
169 | frame->register_unwind (frame, &frame->register_unwind_cache, regnum, | |
170 | optimizedp, lvalp, addrp, realnump, bufferp); | |
171 | } | |
172 | ||
173 | ||
174 | void | |
175 | generic_unwind_get_saved_register (char *raw_buffer, | |
176 | int *optimizedp, | |
177 | CORE_ADDR *addrp, | |
178 | struct frame_info *frame, | |
179 | int regnum, | |
180 | enum lval_type *lvalp) | |
181 | { | |
182 | int optimizedx; | |
183 | CORE_ADDR addrx; | |
184 | int realnumx; | |
185 | enum lval_type lvalx; | |
186 | ||
187 | if (!target_has_registers) | |
188 | error ("No registers."); | |
189 | ||
190 | /* Keep things simple, ensure that all the pointers (except valuep) | |
191 | are non NULL. */ | |
192 | if (optimizedp == NULL) | |
193 | optimizedp = &optimizedx; | |
194 | if (lvalp == NULL) | |
195 | lvalp = &lvalx; | |
196 | if (addrp == NULL) | |
197 | addrp = &addrx; | |
198 | ||
199 | /* Reached the the bottom (youngest, inner most) of the frame chain | |
200 | (youngest, inner most) frame, go direct to the hardware register | |
201 | cache (do not pass go, do not try to cache the value, ...). The | |
202 | unwound value would have been cached in frame->next but that | |
203 | doesn't exist. This doesn't matter as the hardware register | |
204 | cache is stopping any unnecessary accesses to the target. */ | |
205 | ||
206 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a | |
207 | special case, there was always an inner frame dedicated to the | |
208 | hardware registers. Unfortunatly, there is too much unwind code | |
209 | around that looks up/down the frame chain while making the | |
210 | assumption that each frame level is using the same unwind code. */ | |
211 | ||
212 | if (frame == NULL) | |
213 | frame_register_unwind (NULL, regnum, optimizedp, lvalp, addrp, &realnumx, | |
214 | raw_buffer); | |
215 | else | |
216 | frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, | |
217 | &realnumx, raw_buffer); | |
218 | } | |
219 | ||
d65fe839 AC |
220 | #if !defined (GET_SAVED_REGISTER) |
221 | #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \ | |
222 | default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval) | |
223 | #endif | |
224 | ||
225 | void | |
226 | get_saved_register (char *raw_buffer, | |
227 | int *optimized, | |
228 | CORE_ADDR *addrp, | |
229 | struct frame_info *frame, | |
230 | int regnum, | |
231 | enum lval_type *lval) | |
232 | { | |
233 | GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval); | |
234 | } | |
235 | ||
cda5a58a | 236 | /* frame_register_read () |
d65fe839 | 237 | |
cda5a58a | 238 | Find and return the value of REGNUM for the specified stack frame. |
d65fe839 AC |
239 | The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). |
240 | ||
cda5a58a | 241 | Returns 0 if the register value could not be found. */ |
d65fe839 | 242 | |
cda5a58a AC |
243 | int |
244 | frame_register_read (struct frame_info *frame, int regnum, void *myaddr) | |
d65fe839 AC |
245 | { |
246 | int optim; | |
d65fe839 AC |
247 | get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame, |
248 | regnum, (enum lval_type *) NULL); | |
249 | ||
c97dcfc7 AC |
250 | /* FIXME: cagney/2002-05-15: This test, is just bogus. |
251 | ||
252 | It indicates that the target failed to supply a value for a | |
253 | register because it was "not available" at this time. Problem | |
254 | is, the target still has the register and so get saved_register() | |
255 | may be returning a value saved on the stack. */ | |
256 | ||
d65fe839 | 257 | if (register_cached (regnum) < 0) |
cda5a58a | 258 | return 0; /* register value not available */ |
d65fe839 | 259 | |
cda5a58a | 260 | return !optim; |
d65fe839 | 261 | } |