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" |
e36180d7 AC |
30 | #include "gdb_string.h" |
31 | #include "builtin-regs.h" | |
4c1e7e9d AC |
32 | #include "gdb_obstack.h" |
33 | #include "dummy-frame.h" | |
34 | #include "gdbcore.h" | |
35 | #include "annotate.h" | |
d65fe839 | 36 | |
101dcfbe AC |
37 | /* Return a frame uniq ID that can be used to, later re-find the |
38 | frame. */ | |
39 | ||
40 | void | |
41 | get_frame_id (struct frame_info *fi, struct frame_id *id) | |
42 | { | |
43 | if (fi == NULL) | |
44 | { | |
45 | id->base = 0; | |
46 | id->pc = 0; | |
47 | } | |
48 | else | |
49 | { | |
50 | id->base = FRAME_FP (fi); | |
51 | id->pc = fi->pc; | |
52 | } | |
53 | } | |
54 | ||
55 | struct frame_info * | |
56 | frame_find_by_id (struct frame_id id) | |
57 | { | |
58 | struct frame_info *frame; | |
59 | ||
60 | /* ZERO denotes the null frame, let the caller decide what to do | |
61 | about it. Should it instead return get_current_frame()? */ | |
62 | if (id.base == 0 && id.pc == 0) | |
63 | return NULL; | |
64 | ||
65 | for (frame = get_current_frame (); | |
66 | frame != NULL; | |
67 | frame = get_prev_frame (frame)) | |
68 | { | |
69 | if (INNER_THAN (FRAME_FP (frame), id.base)) | |
70 | /* ``inner/current < frame < id.base''. Keep looking along | |
71 | the frame chain. */ | |
72 | continue; | |
73 | if (INNER_THAN (id.base, FRAME_FP (frame))) | |
74 | /* ``inner/current < id.base < frame''. Oops, gone past it. | |
75 | Just give up. */ | |
76 | return NULL; | |
77 | /* FIXME: cagney/2002-04-21: This isn't sufficient. It should | |
78 | use id.pc to check that the two frames belong to the same | |
79 | function. Otherwise we'll do things like match dummy frames | |
80 | or mis-match frameless functions. However, until someone | |
81 | notices, stick with the existing behavour. */ | |
82 | return frame; | |
83 | } | |
84 | return NULL; | |
85 | } | |
86 | ||
f18c5a73 AC |
87 | CORE_ADDR |
88 | frame_pc_unwind (struct frame_info *frame) | |
89 | { | |
90 | if (!frame->pc_unwind_cache_p) | |
91 | { | |
92 | frame->pc_unwind_cache = frame->pc_unwind (frame, &frame->unwind_cache); | |
93 | frame->pc_unwind_cache_p = 1; | |
94 | } | |
95 | return frame->pc_unwind_cache; | |
96 | } | |
97 | ||
4f460812 AC |
98 | void |
99 | frame_register_unwind (struct frame_info *frame, int regnum, | |
100 | int *optimizedp, enum lval_type *lvalp, | |
101 | CORE_ADDR *addrp, int *realnump, void *bufferp) | |
102 | { | |
103 | struct frame_unwind_cache *cache; | |
104 | ||
105 | /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates | |
106 | that the value proper does not need to be fetched. */ | |
107 | gdb_assert (optimizedp != NULL); | |
108 | gdb_assert (lvalp != NULL); | |
109 | gdb_assert (addrp != NULL); | |
110 | gdb_assert (realnump != NULL); | |
111 | /* gdb_assert (bufferp != NULL); */ | |
112 | ||
113 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a | |
114 | special case, there was always an inner frame dedicated to the | |
115 | hardware registers. Unfortunatly, there is too much unwind code | |
116 | around that looks up/down the frame chain while making the | |
117 | assumption that each frame level is using the same unwind code. */ | |
118 | ||
119 | if (frame == NULL) | |
120 | { | |
121 | /* We're in the inner-most frame, get the value direct from the | |
122 | register cache. */ | |
123 | *optimizedp = 0; | |
124 | *lvalp = lval_register; | |
fa5f27c7 AC |
125 | /* ULGH! Code uses the offset into the raw register byte array |
126 | as a way of identifying a register. */ | |
127 | *addrp = REGISTER_BYTE (regnum); | |
4f460812 AC |
128 | /* Should this code test ``register_cached (regnum) < 0'' and do |
129 | something like set realnum to -1 when the register isn't | |
130 | available? */ | |
131 | *realnump = regnum; | |
132 | if (bufferp) | |
4caf0990 | 133 | deprecated_read_register_gen (regnum, bufferp); |
4f460812 AC |
134 | return; |
135 | } | |
136 | ||
137 | /* Ask this frame to unwind its register. */ | |
f18c5a73 | 138 | frame->register_unwind (frame, &frame->unwind_cache, regnum, |
4f460812 AC |
139 | optimizedp, lvalp, addrp, realnump, bufferp); |
140 | } | |
141 | ||
a216a322 AC |
142 | void |
143 | frame_register (struct frame_info *frame, int regnum, | |
144 | int *optimizedp, enum lval_type *lvalp, | |
145 | CORE_ADDR *addrp, int *realnump, void *bufferp) | |
146 | { | |
147 | /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates | |
148 | that the value proper does not need to be fetched. */ | |
149 | gdb_assert (optimizedp != NULL); | |
150 | gdb_assert (lvalp != NULL); | |
151 | gdb_assert (addrp != NULL); | |
152 | gdb_assert (realnump != NULL); | |
153 | /* gdb_assert (bufferp != NULL); */ | |
154 | ||
155 | /* Ulgh! Old code that, for lval_register, sets ADDRP to the offset | |
156 | of the register in the register cache. It should instead return | |
157 | the REGNUM corresponding to that register. Translate the . */ | |
158 | if (GET_SAVED_REGISTER_P ()) | |
159 | { | |
160 | GET_SAVED_REGISTER (bufferp, optimizedp, addrp, frame, regnum, lvalp); | |
161 | /* Compute the REALNUM if the caller wants it. */ | |
162 | if (*lvalp == lval_register) | |
163 | { | |
164 | int regnum; | |
165 | for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++) | |
166 | { | |
167 | if (*addrp == register_offset_hack (current_gdbarch, regnum)) | |
168 | { | |
169 | *realnump = regnum; | |
170 | return; | |
171 | } | |
172 | } | |
173 | internal_error (__FILE__, __LINE__, | |
174 | "Failed to compute the register number corresponding" | |
175 | " to 0x%s", paddr_d (*addrp)); | |
176 | } | |
177 | *realnump = -1; | |
178 | return; | |
179 | } | |
180 | ||
181 | /* Reached the the bottom (youngest, inner most) of the frame chain | |
182 | (youngest, inner most) frame, go direct to the hardware register | |
183 | cache (do not pass go, do not try to cache the value, ...). The | |
184 | unwound value would have been cached in frame->next but that | |
185 | doesn't exist. This doesn't matter as the hardware register | |
186 | cache is stopping any unnecessary accesses to the target. */ | |
187 | ||
188 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a | |
189 | special case, there was always an inner frame dedicated to the | |
190 | hardware registers. Unfortunatly, there is too much unwind code | |
191 | around that looks up/down the frame chain while making the | |
192 | assumption that each frame level is using the same unwind code. */ | |
193 | ||
194 | if (frame == NULL) | |
195 | frame_register_unwind (NULL, regnum, optimizedp, lvalp, addrp, realnump, | |
196 | bufferp); | |
197 | else | |
198 | frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, | |
199 | realnump, bufferp); | |
200 | } | |
201 | ||
135c175f AC |
202 | void |
203 | frame_unwind_signed_register (struct frame_info *frame, int regnum, | |
204 | LONGEST *val) | |
205 | { | |
206 | int optimized; | |
207 | CORE_ADDR addr; | |
208 | int realnum; | |
209 | enum lval_type lval; | |
210 | void *buf = alloca (MAX_REGISTER_RAW_SIZE); | |
211 | frame_register_unwind (frame, regnum, &optimized, &lval, &addr, | |
212 | &realnum, buf); | |
213 | (*val) = extract_signed_integer (buf, REGISTER_VIRTUAL_SIZE (regnum)); | |
214 | } | |
215 | ||
216 | void | |
217 | frame_unwind_unsigned_register (struct frame_info *frame, int regnum, | |
218 | ULONGEST *val) | |
219 | { | |
220 | int optimized; | |
221 | CORE_ADDR addr; | |
222 | int realnum; | |
223 | enum lval_type lval; | |
224 | void *buf = alloca (MAX_REGISTER_RAW_SIZE); | |
225 | frame_register_unwind (frame, regnum, &optimized, &lval, &addr, | |
226 | &realnum, buf); | |
227 | (*val) = extract_unsigned_integer (buf, REGISTER_VIRTUAL_SIZE (regnum)); | |
228 | } | |
4f460812 | 229 | |
f908a0eb AC |
230 | void |
231 | frame_read_unsigned_register (struct frame_info *frame, int regnum, | |
232 | ULONGEST *val) | |
233 | { | |
234 | /* NOTE: cagney/2002-10-31: There is a bit of dogma here - there is | |
235 | always a frame. Both this, and the equivalent | |
236 | frame_read_signed_register() function, can only be called with a | |
237 | valid frame. If, for some reason, this function is called | |
238 | without a frame then the problem isn't here, but rather in the | |
239 | caller. It should of first created a frame and then passed that | |
240 | in. */ | |
241 | /* NOTE: cagney/2002-10-31: As a side bar, keep in mind that the | |
242 | ``current_frame'' should not be treated as a special case. While | |
243 | ``get_next_frame (current_frame) == NULL'' currently holds, it | |
244 | should, as far as possible, not be relied upon. In the future, | |
245 | ``get_next_frame (current_frame)'' may instead simply return a | |
246 | normal frame object that simply always gets register values from | |
247 | the register cache. Consequently, frame code should try to avoid | |
248 | tests like ``if get_next_frame() == NULL'' and instead just rely | |
249 | on recursive frame calls (like the below code) when manipulating | |
250 | a frame chain. */ | |
251 | gdb_assert (frame != NULL); | |
252 | frame_unwind_unsigned_register (get_next_frame (frame), regnum, val); | |
253 | } | |
254 | ||
255 | void | |
256 | frame_read_signed_register (struct frame_info *frame, int regnum, | |
257 | LONGEST *val) | |
258 | { | |
259 | /* See note in frame_read_unsigned_register(). */ | |
260 | gdb_assert (frame != NULL); | |
261 | frame_unwind_signed_register (get_next_frame (frame), regnum, val); | |
262 | } | |
263 | ||
18cde8d5 | 264 | static void |
4f460812 AC |
265 | generic_unwind_get_saved_register (char *raw_buffer, |
266 | int *optimizedp, | |
267 | CORE_ADDR *addrp, | |
268 | struct frame_info *frame, | |
269 | int regnum, | |
270 | enum lval_type *lvalp) | |
271 | { | |
272 | int optimizedx; | |
273 | CORE_ADDR addrx; | |
274 | int realnumx; | |
275 | enum lval_type lvalx; | |
276 | ||
277 | if (!target_has_registers) | |
278 | error ("No registers."); | |
279 | ||
280 | /* Keep things simple, ensure that all the pointers (except valuep) | |
281 | are non NULL. */ | |
282 | if (optimizedp == NULL) | |
283 | optimizedp = &optimizedx; | |
284 | if (lvalp == NULL) | |
285 | lvalp = &lvalx; | |
286 | if (addrp == NULL) | |
287 | addrp = &addrx; | |
288 | ||
289 | /* Reached the the bottom (youngest, inner most) of the frame chain | |
290 | (youngest, inner most) frame, go direct to the hardware register | |
291 | cache (do not pass go, do not try to cache the value, ...). The | |
292 | unwound value would have been cached in frame->next but that | |
293 | doesn't exist. This doesn't matter as the hardware register | |
294 | cache is stopping any unnecessary accesses to the target. */ | |
295 | ||
296 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a | |
297 | special case, there was always an inner frame dedicated to the | |
298 | hardware registers. Unfortunatly, there is too much unwind code | |
299 | around that looks up/down the frame chain while making the | |
300 | assumption that each frame level is using the same unwind code. */ | |
301 | ||
302 | if (frame == NULL) | |
303 | frame_register_unwind (NULL, regnum, optimizedp, lvalp, addrp, &realnumx, | |
304 | raw_buffer); | |
305 | else | |
306 | frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, | |
307 | &realnumx, raw_buffer); | |
308 | } | |
309 | ||
d65fe839 AC |
310 | void |
311 | get_saved_register (char *raw_buffer, | |
312 | int *optimized, | |
313 | CORE_ADDR *addrp, | |
314 | struct frame_info *frame, | |
315 | int regnum, | |
316 | enum lval_type *lval) | |
317 | { | |
a216a322 AC |
318 | if (GET_SAVED_REGISTER_P ()) |
319 | { | |
320 | GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval); | |
321 | return; | |
322 | } | |
323 | generic_unwind_get_saved_register (raw_buffer, optimized, addrp, frame, | |
324 | regnum, lval); | |
d65fe839 AC |
325 | } |
326 | ||
cda5a58a | 327 | /* frame_register_read () |
d65fe839 | 328 | |
cda5a58a | 329 | Find and return the value of REGNUM for the specified stack frame. |
d65fe839 AC |
330 | The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). |
331 | ||
cda5a58a | 332 | Returns 0 if the register value could not be found. */ |
d65fe839 | 333 | |
cda5a58a AC |
334 | int |
335 | frame_register_read (struct frame_info *frame, int regnum, void *myaddr) | |
d65fe839 | 336 | { |
a216a322 AC |
337 | int optimized; |
338 | enum lval_type lval; | |
339 | CORE_ADDR addr; | |
340 | int realnum; | |
341 | frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr); | |
d65fe839 | 342 | |
c97dcfc7 AC |
343 | /* FIXME: cagney/2002-05-15: This test, is just bogus. |
344 | ||
345 | It indicates that the target failed to supply a value for a | |
346 | register because it was "not available" at this time. Problem | |
347 | is, the target still has the register and so get saved_register() | |
348 | may be returning a value saved on the stack. */ | |
349 | ||
d65fe839 | 350 | if (register_cached (regnum) < 0) |
cda5a58a | 351 | return 0; /* register value not available */ |
d65fe839 | 352 | |
a216a322 | 353 | return !optimized; |
d65fe839 | 354 | } |
e36180d7 AC |
355 | |
356 | ||
357 | /* Map between a frame register number and its name. A frame register | |
358 | space is a superset of the cooked register space --- it also | |
359 | includes builtin registers. */ | |
360 | ||
361 | int | |
362 | frame_map_name_to_regnum (const char *name, int len) | |
363 | { | |
364 | int i; | |
365 | ||
366 | /* Search register name space. */ | |
367 | for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++) | |
368 | if (REGISTER_NAME (i) && len == strlen (REGISTER_NAME (i)) | |
369 | && strncmp (name, REGISTER_NAME (i), len) == 0) | |
370 | { | |
371 | return i; | |
372 | } | |
373 | ||
374 | /* Try builtin registers. */ | |
375 | i = builtin_reg_map_name_to_regnum (name, len); | |
376 | if (i >= 0) | |
377 | { | |
378 | /* A builtin register doesn't fall into the architecture's | |
379 | register range. */ | |
380 | gdb_assert (i >= NUM_REGS + NUM_PSEUDO_REGS); | |
381 | return i; | |
382 | } | |
383 | ||
384 | return -1; | |
385 | } | |
386 | ||
387 | const char * | |
388 | frame_map_regnum_to_name (int regnum) | |
389 | { | |
390 | if (regnum < 0) | |
391 | return NULL; | |
392 | if (regnum < NUM_REGS + NUM_PSEUDO_REGS) | |
393 | return REGISTER_NAME (regnum); | |
394 | return builtin_reg_map_regnum_to_name (regnum); | |
395 | } | |
4c1e7e9d AC |
396 | |
397 | /* Info about the innermost stack frame (contents of FP register) */ | |
398 | ||
399 | static struct frame_info *current_frame; | |
400 | ||
401 | /* Cache for frame addresses already read by gdb. Valid only while | |
402 | inferior is stopped. Control variables for the frame cache should | |
403 | be local to this module. */ | |
404 | ||
405 | static struct obstack frame_cache_obstack; | |
406 | ||
407 | void * | |
408 | frame_obstack_alloc (unsigned long size) | |
409 | { | |
410 | return obstack_alloc (&frame_cache_obstack, size); | |
411 | } | |
412 | ||
413 | void | |
414 | frame_saved_regs_zalloc (struct frame_info *fi) | |
415 | { | |
416 | fi->saved_regs = (CORE_ADDR *) | |
417 | frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS); | |
418 | memset (fi->saved_regs, 0, SIZEOF_FRAME_SAVED_REGS); | |
419 | } | |
420 | ||
421 | ||
422 | /* Return the innermost (currently executing) stack frame. */ | |
423 | ||
424 | struct frame_info * | |
425 | get_current_frame (void) | |
426 | { | |
427 | if (current_frame == NULL) | |
428 | { | |
429 | if (target_has_stack) | |
430 | current_frame = create_new_frame (read_fp (), read_pc ()); | |
431 | else | |
432 | error ("No stack."); | |
433 | } | |
434 | return current_frame; | |
435 | } | |
436 | ||
437 | void | |
438 | set_current_frame (struct frame_info *frame) | |
439 | { | |
440 | current_frame = frame; | |
441 | } | |
442 | ||
443 | /* Return the register saved in the simplistic ``saved_regs'' cache. | |
444 | If the value isn't here AND a value is needed, try the next inner | |
445 | most frame. */ | |
446 | ||
447 | static void | |
448 | frame_saved_regs_register_unwind (struct frame_info *frame, void **cache, | |
449 | int regnum, int *optimizedp, | |
450 | enum lval_type *lvalp, CORE_ADDR *addrp, | |
451 | int *realnump, void *bufferp) | |
452 | { | |
453 | /* There is always a frame at this point. And THIS is the frame | |
454 | we're interested in. */ | |
455 | gdb_assert (frame != NULL); | |
456 | /* If we're using generic dummy frames, we'd better not be in a call | |
457 | dummy. (generic_call_dummy_register_unwind ought to have been called | |
458 | instead.) */ | |
459 | gdb_assert (!(USE_GENERIC_DUMMY_FRAMES | |
460 | && PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))); | |
461 | ||
462 | /* Load the saved_regs register cache. */ | |
463 | if (frame->saved_regs == NULL) | |
464 | FRAME_INIT_SAVED_REGS (frame); | |
465 | ||
466 | if (frame->saved_regs != NULL | |
467 | && frame->saved_regs[regnum] != 0) | |
468 | { | |
469 | if (regnum == SP_REGNUM) | |
470 | { | |
471 | /* SP register treated specially. */ | |
472 | *optimizedp = 0; | |
473 | *lvalp = not_lval; | |
474 | *addrp = 0; | |
475 | *realnump = -1; | |
476 | if (bufferp != NULL) | |
477 | store_address (bufferp, REGISTER_RAW_SIZE (regnum), | |
478 | frame->saved_regs[regnum]); | |
479 | } | |
480 | else | |
481 | { | |
482 | /* Any other register is saved in memory, fetch it but cache | |
483 | a local copy of its value. */ | |
484 | *optimizedp = 0; | |
485 | *lvalp = lval_memory; | |
486 | *addrp = frame->saved_regs[regnum]; | |
487 | *realnump = -1; | |
488 | if (bufferp != NULL) | |
489 | { | |
490 | #if 1 | |
491 | /* Save each register value, as it is read in, in a | |
492 | frame based cache. */ | |
493 | void **regs = (*cache); | |
494 | if (regs == NULL) | |
495 | { | |
496 | int sizeof_cache = ((NUM_REGS + NUM_PSEUDO_REGS) | |
497 | * sizeof (void *)); | |
498 | regs = frame_obstack_alloc (sizeof_cache); | |
499 | memset (regs, 0, sizeof_cache); | |
500 | (*cache) = regs; | |
501 | } | |
502 | if (regs[regnum] == NULL) | |
503 | { | |
504 | regs[regnum] | |
505 | = frame_obstack_alloc (REGISTER_RAW_SIZE (regnum)); | |
506 | read_memory (frame->saved_regs[regnum], regs[regnum], | |
507 | REGISTER_RAW_SIZE (regnum)); | |
508 | } | |
509 | memcpy (bufferp, regs[regnum], REGISTER_RAW_SIZE (regnum)); | |
510 | #else | |
511 | /* Read the value in from memory. */ | |
512 | read_memory (frame->saved_regs[regnum], bufferp, | |
513 | REGISTER_RAW_SIZE (regnum)); | |
514 | #endif | |
515 | } | |
516 | } | |
517 | return; | |
518 | } | |
519 | ||
520 | /* No luck, assume this and the next frame have the same register | |
521 | value. If a value is needed, pass the request on down the chain; | |
522 | otherwise just return an indication that the value is in the same | |
523 | register as the next frame. */ | |
524 | if (bufferp == NULL) | |
525 | { | |
526 | *optimizedp = 0; | |
527 | *lvalp = lval_register; | |
528 | *addrp = 0; | |
529 | *realnump = regnum; | |
530 | } | |
531 | else | |
532 | { | |
533 | frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, | |
534 | realnump, bufferp); | |
535 | } | |
536 | } | |
537 | ||
f18c5a73 AC |
538 | static CORE_ADDR |
539 | frame_saved_regs_pc_unwind (struct frame_info *frame, void **cache) | |
540 | { | |
541 | return FRAME_SAVED_PC (frame); | |
542 | } | |
543 | ||
4c1e7e9d AC |
544 | /* Function: get_saved_register |
545 | Find register number REGNUM relative to FRAME and put its (raw, | |
546 | target format) contents in *RAW_BUFFER. | |
547 | ||
548 | Set *OPTIMIZED if the variable was optimized out (and thus can't be | |
549 | fetched). Note that this is never set to anything other than zero | |
550 | in this implementation. | |
551 | ||
552 | Set *LVAL to lval_memory, lval_register, or not_lval, depending on | |
553 | whether the value was fetched from memory, from a register, or in a | |
554 | strange and non-modifiable way (e.g. a frame pointer which was | |
555 | calculated rather than fetched). We will use not_lval for values | |
556 | fetched from generic dummy frames. | |
557 | ||
558 | Set *ADDRP to the address, either in memory or as a REGISTER_BYTE | |
559 | offset into the registers array. If the value is stored in a dummy | |
560 | frame, set *ADDRP to zero. | |
561 | ||
562 | To use this implementation, define a function called | |
563 | "get_saved_register" in your target code, which simply passes all | |
564 | of its arguments to this function. | |
565 | ||
566 | The argument RAW_BUFFER must point to aligned memory. */ | |
567 | ||
568 | void | |
569 | deprecated_generic_get_saved_register (char *raw_buffer, int *optimized, | |
570 | CORE_ADDR *addrp, | |
571 | struct frame_info *frame, int regnum, | |
572 | enum lval_type *lval) | |
573 | { | |
574 | if (!target_has_registers) | |
575 | error ("No registers."); | |
576 | ||
577 | /* Normal systems don't optimize out things with register numbers. */ | |
578 | if (optimized != NULL) | |
579 | *optimized = 0; | |
580 | ||
581 | if (addrp) /* default assumption: not found in memory */ | |
582 | *addrp = 0; | |
583 | ||
584 | /* Note: since the current frame's registers could only have been | |
585 | saved by frames INTERIOR TO the current frame, we skip examining | |
586 | the current frame itself: otherwise, we would be getting the | |
587 | previous frame's registers which were saved by the current frame. */ | |
588 | ||
589 | while (frame && ((frame = frame->next) != NULL)) | |
590 | { | |
591 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) | |
592 | { | |
593 | if (lval) /* found it in a CALL_DUMMY frame */ | |
594 | *lval = not_lval; | |
595 | if (raw_buffer) | |
596 | /* FIXME: cagney/2002-06-26: This should be via the | |
597 | gdbarch_register_read() method so that it, on the fly, | |
598 | constructs either a raw or pseudo register from the raw | |
599 | register cache. */ | |
600 | regcache_raw_read (generic_find_dummy_frame (frame->pc, | |
601 | frame->frame), | |
602 | regnum, raw_buffer); | |
603 | return; | |
604 | } | |
605 | ||
606 | FRAME_INIT_SAVED_REGS (frame); | |
607 | if (frame->saved_regs != NULL | |
608 | && frame->saved_regs[regnum] != 0) | |
609 | { | |
610 | if (lval) /* found it saved on the stack */ | |
611 | *lval = lval_memory; | |
612 | if (regnum == SP_REGNUM) | |
613 | { | |
614 | if (raw_buffer) /* SP register treated specially */ | |
615 | store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), | |
616 | frame->saved_regs[regnum]); | |
617 | } | |
618 | else | |
619 | { | |
620 | if (addrp) /* any other register */ | |
621 | *addrp = frame->saved_regs[regnum]; | |
622 | if (raw_buffer) | |
623 | read_memory (frame->saved_regs[regnum], raw_buffer, | |
624 | REGISTER_RAW_SIZE (regnum)); | |
625 | } | |
626 | return; | |
627 | } | |
628 | } | |
629 | ||
630 | /* If we get thru the loop to this point, it means the register was | |
631 | not saved in any frame. Return the actual live-register value. */ | |
632 | ||
633 | if (lval) /* found it in a live register */ | |
634 | *lval = lval_register; | |
635 | if (addrp) | |
636 | *addrp = REGISTER_BYTE (regnum); | |
637 | if (raw_buffer) | |
638 | deprecated_read_register_gen (regnum, raw_buffer); | |
639 | } | |
640 | ||
641 | /* Using the PC, select a mechanism for unwinding a frame returning | |
642 | the previous frame. The register unwind function should, on | |
643 | demand, initialize the ->context object. */ | |
644 | ||
645 | static void | |
646 | set_unwind_by_pc (CORE_ADDR pc, CORE_ADDR fp, | |
f18c5a73 AC |
647 | frame_register_unwind_ftype **unwind_register, |
648 | frame_pc_unwind_ftype **unwind_pc) | |
4c1e7e9d AC |
649 | { |
650 | if (!USE_GENERIC_DUMMY_FRAMES) | |
f18c5a73 AC |
651 | { |
652 | /* Still need to set this to something. The ``info frame'' code | |
653 | calls this function to find out where the saved registers are. | |
654 | Hopefully this is robust enough to stop any core dumps and | |
655 | return vaguely correct values.. */ | |
656 | *unwind_register = frame_saved_regs_register_unwind; | |
657 | *unwind_pc = frame_saved_regs_pc_unwind; | |
658 | } | |
4c1e7e9d | 659 | else if (PC_IN_CALL_DUMMY (pc, fp, fp)) |
f18c5a73 AC |
660 | { |
661 | *unwind_register = dummy_frame_register_unwind; | |
662 | *unwind_pc = dummy_frame_pc_unwind; | |
663 | } | |
4c1e7e9d | 664 | else |
f18c5a73 AC |
665 | { |
666 | *unwind_register = frame_saved_regs_register_unwind; | |
667 | *unwind_pc = frame_saved_regs_pc_unwind; | |
668 | } | |
4c1e7e9d AC |
669 | } |
670 | ||
671 | /* Create an arbitrary (i.e. address specified by user) or innermost frame. | |
672 | Always returns a non-NULL value. */ | |
673 | ||
674 | struct frame_info * | |
675 | create_new_frame (CORE_ADDR addr, CORE_ADDR pc) | |
676 | { | |
677 | struct frame_info *fi; | |
5a203e44 | 678 | enum frame_type type; |
4c1e7e9d AC |
679 | |
680 | fi = (struct frame_info *) | |
681 | obstack_alloc (&frame_cache_obstack, | |
682 | sizeof (struct frame_info)); | |
683 | ||
684 | /* Zero all fields by default. */ | |
685 | memset (fi, 0, sizeof (struct frame_info)); | |
686 | ||
687 | fi->frame = addr; | |
688 | fi->pc = pc; | |
5a203e44 AC |
689 | /* NOTE: cagney/2002-11-18: The code segments, found in |
690 | create_new_frame and get_prev_frame(), that initializes the | |
691 | frames type is subtly different. The latter only updates ->type | |
692 | when it encounters a SIGTRAMP_FRAME or DUMMY_FRAME. This stops | |
693 | get_prev_frame() overriding the frame's type when the INIT code | |
694 | has previously set it. This is really somewhat bogus. The | |
695 | initialization, as seen in create_new_frame(), should occur | |
696 | before the INIT function has been called. */ | |
697 | if (USE_GENERIC_DUMMY_FRAMES && PC_IN_CALL_DUMMY (fi->pc, 0, 0)) | |
698 | /* NOTE: cagney/2002-11-11: Does this even occure? */ | |
699 | type = DUMMY_FRAME; | |
700 | else | |
701 | { | |
702 | char *name; | |
703 | find_pc_partial_function (pc, &name, NULL, NULL); | |
704 | if (PC_IN_SIGTRAMP (fi->pc, name)) | |
705 | type = SIGTRAMP_FRAME; | |
706 | else | |
707 | type = NORMAL_FRAME; | |
708 | } | |
709 | fi->type = type; | |
4c1e7e9d AC |
710 | |
711 | if (INIT_EXTRA_FRAME_INFO_P ()) | |
712 | INIT_EXTRA_FRAME_INFO (0, fi); | |
713 | ||
714 | /* Select/initialize an unwind function. */ | |
f18c5a73 AC |
715 | set_unwind_by_pc (fi->pc, fi->frame, &fi->register_unwind, |
716 | &fi->pc_unwind); | |
4c1e7e9d AC |
717 | |
718 | return fi; | |
719 | } | |
720 | ||
721 | /* Return the frame that FRAME calls (NULL if FRAME is the innermost | |
722 | frame). */ | |
723 | ||
724 | struct frame_info * | |
725 | get_next_frame (struct frame_info *frame) | |
726 | { | |
727 | return frame->next; | |
728 | } | |
729 | ||
730 | /* Flush the entire frame cache. */ | |
731 | ||
732 | void | |
733 | flush_cached_frames (void) | |
734 | { | |
735 | /* Since we can't really be sure what the first object allocated was */ | |
736 | obstack_free (&frame_cache_obstack, 0); | |
737 | obstack_init (&frame_cache_obstack); | |
738 | ||
739 | current_frame = NULL; /* Invalidate cache */ | |
740 | select_frame (NULL); | |
741 | annotate_frames_invalid (); | |
742 | } | |
743 | ||
744 | /* Flush the frame cache, and start a new one if necessary. */ | |
745 | ||
746 | void | |
747 | reinit_frame_cache (void) | |
748 | { | |
749 | flush_cached_frames (); | |
750 | ||
751 | /* FIXME: The inferior_ptid test is wrong if there is a corefile. */ | |
752 | if (PIDGET (inferior_ptid) != 0) | |
753 | { | |
754 | select_frame (get_current_frame ()); | |
755 | } | |
756 | } | |
757 | ||
758 | /* Return a structure containing various interesting information | |
759 | about the frame that called NEXT_FRAME. Returns NULL | |
760 | if there is no such frame. */ | |
761 | ||
762 | struct frame_info * | |
763 | get_prev_frame (struct frame_info *next_frame) | |
764 | { | |
765 | CORE_ADDR address = 0; | |
766 | struct frame_info *prev; | |
95adb866 | 767 | int fromleaf; |
4c1e7e9d | 768 | |
95adb866 AC |
769 | /* Return the inner-most frame, when the caller passes in NULL. */ |
770 | /* NOTE: cagney/2002-11-09: Not sure how this would happen. The | |
771 | caller should have previously obtained a valid frame using | |
772 | get_selected_frame() and then called this code - only possibility | |
773 | I can think of is code behaving badly. */ | |
774 | if (next_frame == NULL) | |
4c1e7e9d | 775 | { |
95adb866 AC |
776 | /* NOTE: cagney/2002-11-09: There was a code segment here that |
777 | would error out when CURRENT_FRAME was NULL. The comment | |
778 | that went with it made the claim ... | |
779 | ||
780 | ``This screws value_of_variable, which just wants a nice | |
781 | clean NULL return from block_innermost_frame if there are no | |
782 | frames. I don't think I've ever seen this message happen | |
783 | otherwise. And returning NULL here is a perfectly legitimate | |
784 | thing to do.'' | |
785 | ||
786 | Per the above, this code shouldn't even be called with a NULL | |
787 | NEXT_FRAME. */ | |
4c1e7e9d AC |
788 | return current_frame; |
789 | } | |
790 | ||
15220c65 AC |
791 | /* Only try to do the unwind once. */ |
792 | if (next_frame->prev_p) | |
4c1e7e9d | 793 | return next_frame->prev; |
15220c65 | 794 | next_frame->prev_p = 1; |
4c1e7e9d AC |
795 | |
796 | /* On some machines it is possible to call a function without | |
797 | setting up a stack frame for it. On these machines, we | |
798 | define this macro to take two args; a frameinfo pointer | |
799 | identifying a frame and a variable to set or clear if it is | |
800 | or isn't leafless. */ | |
801 | ||
802 | /* Still don't want to worry about this except on the innermost | |
95adb866 AC |
803 | frame. This macro will set FROMLEAF if NEXT_FRAME is a frameless |
804 | function invocation. */ | |
805 | if (next_frame->next == NULL) | |
806 | /* FIXME: 2002-11-09: Frameless functions can occure anywhere in | |
807 | the frame chain, not just the inner most frame! The generic, | |
808 | per-architecture, frame code should handle this and the below | |
809 | should simply be removed. */ | |
810 | fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame); | |
811 | else | |
812 | fromleaf = 0; | |
813 | ||
814 | if (fromleaf) | |
815 | /* A frameless inner-most frame. The `FP' (which isn't an | |
816 | architecture frame-pointer register!) of the caller is the same | |
817 | as the callee. */ | |
818 | /* FIXME: 2002-11-09: There isn't any reason to special case this | |
819 | edge condition. Instead the per-architecture code should hande | |
820 | it locally. */ | |
821 | address = FRAME_FP (next_frame); | |
822 | else | |
4c1e7e9d AC |
823 | { |
824 | /* Two macros defined in tm.h specify the machine-dependent | |
825 | actions to be performed here. | |
95adb866 | 826 | |
4c1e7e9d | 827 | First, get the frame's chain-pointer. |
95adb866 | 828 | |
4c1e7e9d AC |
829 | If that is zero, the frame is the outermost frame or a leaf |
830 | called by the outermost frame. This means that if start | |
831 | calls main without a frame, we'll return 0 (which is fine | |
832 | anyway). | |
833 | ||
834 | Nope; there's a problem. This also returns when the current | |
835 | routine is a leaf of main. This is unacceptable. We move | |
836 | this to after the ffi test; I'd rather have backtraces from | |
837 | start go curfluy than have an abort called from main not show | |
838 | main. */ | |
839 | address = FRAME_CHAIN (next_frame); | |
840 | ||
841 | /* FIXME: cagney/2002-06-08: There should be two tests here. | |
842 | The first would check for a valid frame chain based on a user | |
843 | selectable policy. The default being ``stop at main'' (as | |
844 | implemented by generic_func_frame_chain_valid()). Other | |
845 | policies would be available - stop at NULL, .... The second | |
846 | test, if provided by the target architecture, would check for | |
847 | more exotic cases - most target architectures wouldn't bother | |
848 | with this second case. */ | |
849 | if (!FRAME_CHAIN_VALID (address, next_frame)) | |
850 | return 0; | |
851 | } | |
852 | if (address == 0) | |
853 | return 0; | |
854 | ||
95adb866 | 855 | /* Create an initially zero previous frame. */ |
4c1e7e9d AC |
856 | prev = (struct frame_info *) |
857 | obstack_alloc (&frame_cache_obstack, | |
858 | sizeof (struct frame_info)); | |
4c1e7e9d AC |
859 | memset (prev, 0, sizeof (struct frame_info)); |
860 | ||
95adb866 AC |
861 | /* Link it in. */ |
862 | next_frame->prev = prev; | |
4c1e7e9d AC |
863 | prev->next = next_frame; |
864 | prev->frame = address; | |
865 | prev->level = next_frame->level + 1; | |
5a203e44 AC |
866 | /* FIXME: cagney/2002-11-18: Should be setting the frame's type |
867 | here, before anything else, and not last. Various INIT functions | |
868 | are full of work-arounds for the frames type not being set | |
869 | correctly from the word go. Ulgh! */ | |
870 | prev->type = NORMAL_FRAME; | |
4c1e7e9d | 871 | |
95adb866 AC |
872 | /* This change should not be needed, FIXME! We should determine |
873 | whether any targets *need* INIT_FRAME_PC to happen after | |
874 | INIT_EXTRA_FRAME_INFO and come up with a simple way to express | |
875 | what goes on here. | |
876 | ||
877 | INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame | |
878 | (where the PC is already set up) and here (where it isn't). | |
879 | INIT_FRAME_PC is only called from here, always after | |
880 | INIT_EXTRA_FRAME_INFO. | |
881 | ||
882 | The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the | |
883 | PC value (which hasn't been set yet). Some other machines appear | |
884 | to require INIT_EXTRA_FRAME_INFO before they can do | |
885 | INIT_FRAME_PC. Phoo. | |
886 | ||
887 | We shouldn't need INIT_FRAME_PC_FIRST to add more complication to | |
888 | an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92. | |
889 | ||
890 | Assuming that some machines need INIT_FRAME_PC after | |
891 | INIT_EXTRA_FRAME_INFO, one possible scheme: | |
892 | ||
893 | SETUP_INNERMOST_FRAME(): Default version is just create_new_frame | |
894 | (read_fp ()), read_pc ()). Machines with extra frame info would | |
895 | do that (or the local equivalent) and then set the extra fields. | |
896 | ||
897 | SETUP_ARBITRARY_FRAME(argc, argv): Only change here is that | |
898 | create_new_frame would no longer init extra frame info; | |
899 | SETUP_ARBITRARY_FRAME would have to do that. | |
900 | ||
901 | INIT_PREV_FRAME(fromleaf, prev) Replace INIT_EXTRA_FRAME_INFO and | |
902 | INIT_FRAME_PC. This should also return a flag saying whether to | |
903 | keep the new frame, or whether to discard it, because on some | |
904 | machines (e.g. mips) it is really awkward to have | |
905 | FRAME_CHAIN_VALID called *before* INIT_EXTRA_FRAME_INFO (there is | |
906 | no good way to get information deduced in FRAME_CHAIN_VALID into | |
907 | the extra fields of the new frame). std_frame_pc(fromleaf, prev) | |
908 | ||
909 | This is the default setting for INIT_PREV_FRAME. It just does | |
910 | what the default INIT_FRAME_PC does. Some machines will call it | |
911 | from INIT_PREV_FRAME (either at the beginning, the end, or in the | |
912 | middle). Some machines won't use it. | |
913 | ||
914 | kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */ | |
915 | ||
916 | /* NOTE: cagney/2002-11-09: Just ignore the above! There is no | |
917 | reason for things to be this complicated. | |
918 | ||
919 | The trick is to assume that there is always a frame. Instead of | |
920 | special casing the inner-most frame, create fake frame | |
921 | (containing the hardware registers) that is inner to the | |
922 | user-visible inner-most frame (...) and then unwind from that. | |
923 | That way architecture code can use use the standard | |
924 | frame_XX_unwind() functions and not differentiate between the | |
925 | inner most and any other case. | |
926 | ||
927 | Since there is always a frame to unwind from, there is always | |
928 | somewhere (NEXT_FRAME) to store all the info needed to construct | |
929 | a new (previous) frame without having to first create it. This | |
930 | means that the convolution below - needing to carefully order a | |
931 | frame's initialization - isn't needed. | |
932 | ||
933 | The irony here though, is that FRAME_CHAIN(), at least for a more | |
934 | up-to-date architecture, always calls FRAME_SAVED_PC(), and | |
935 | FRAME_SAVED_PC() computes the PC but without first needing the | |
936 | frame! Instead of the convolution below, we could have simply | |
937 | called FRAME_SAVED_PC() and been done with it! Note that | |
938 | FRAME_SAVED_PC() is being superseed by frame_pc_unwind() and that | |
939 | function does have somewhere to cache that PC value. */ | |
4c1e7e9d AC |
940 | |
941 | INIT_FRAME_PC_FIRST (fromleaf, prev); | |
942 | ||
943 | if (INIT_EXTRA_FRAME_INFO_P ()) | |
944 | INIT_EXTRA_FRAME_INFO (fromleaf, prev); | |
945 | ||
946 | /* This entry is in the frame queue now, which is good since | |
95adb866 AC |
947 | FRAME_SAVED_PC may use that queue to figure out its value (see |
948 | tm-sparc.h). We want the pc saved in the inferior frame. */ | |
4c1e7e9d AC |
949 | INIT_FRAME_PC (fromleaf, prev); |
950 | ||
95adb866 AC |
951 | /* If ->frame and ->pc are unchanged, we are in the process of |
952 | getting ourselves into an infinite backtrace. Some architectures | |
953 | check this in FRAME_CHAIN or thereabouts, but it seems like there | |
954 | is no reason this can't be an architecture-independent check. */ | |
955 | if (prev->frame == next_frame->frame | |
956 | && prev->pc == next_frame->pc) | |
4c1e7e9d | 957 | { |
95adb866 AC |
958 | next_frame->prev = NULL; |
959 | obstack_free (&frame_cache_obstack, prev); | |
960 | return NULL; | |
4c1e7e9d AC |
961 | } |
962 | ||
963 | /* Initialize the code used to unwind the frame PREV based on the PC | |
964 | (and probably other architectural information). The PC lets you | |
965 | check things like the debug info at that point (dwarf2cfi?) and | |
966 | use that to decide how the frame should be unwound. */ | |
f18c5a73 AC |
967 | set_unwind_by_pc (prev->pc, prev->frame, &prev->register_unwind, |
968 | &prev->pc_unwind); | |
4c1e7e9d | 969 | |
5a203e44 AC |
970 | /* NOTE: cagney/2002-11-18: The code segments, found in |
971 | create_new_frame and get_prev_frame(), that initializes the | |
972 | frames type is subtly different. The latter only updates ->type | |
973 | when it encounters a SIGTRAMP_FRAME or DUMMY_FRAME. This stops | |
974 | get_prev_frame() overriding the frame's type when the INIT code | |
975 | has previously set it. This is really somewhat bogus. The | |
976 | initialization, as seen in create_new_frame(), should occur | |
977 | before the INIT function has been called. */ | |
978 | if (USE_GENERIC_DUMMY_FRAMES | |
979 | && PC_IN_CALL_DUMMY (prev->pc, 0, 0)) | |
980 | prev->type = DUMMY_FRAME; | |
981 | else | |
982 | { | |
983 | /* FIXME: cagney/2002-11-10: This should be moved to before the | |
984 | INIT code above so that the INIT code knows what the frame's | |
985 | type is (in fact, for a [generic] dummy-frame, the type can | |
986 | be set and then the entire initialization can be skipped. | |
987 | Unforunatly, its the INIT code that sets the PC (Hmm, catch | |
988 | 22). */ | |
989 | char *name; | |
990 | find_pc_partial_function (prev->pc, &name, NULL, NULL); | |
991 | if (PC_IN_SIGTRAMP (prev->pc, name)) | |
992 | prev->type = SIGTRAMP_FRAME; | |
993 | /* FIXME: cagney/2002-11-11: Leave prev->type alone. Some | |
994 | architectures are forcing the frame's type in INIT so we | |
995 | don't want to override it here. Remember, NORMAL_FRAME == 0, | |
996 | so it all works (just :-/). Once this initialization is | |
997 | moved to the start of this function, all this nastness will | |
998 | go away. */ | |
999 | } | |
4c1e7e9d AC |
1000 | |
1001 | return prev; | |
1002 | } | |
1003 | ||
1004 | CORE_ADDR | |
1005 | get_frame_pc (struct frame_info *frame) | |
1006 | { | |
1007 | return frame->pc; | |
1008 | } | |
1009 | ||
5a203e44 AC |
1010 | enum frame_type |
1011 | get_frame_type (struct frame_info *frame) | |
1012 | { | |
1013 | /* Some targets still don't use [generic] dummy frames. Catch them | |
1014 | here. */ | |
1015 | if (!USE_GENERIC_DUMMY_FRAMES | |
1016 | && deprecated_frame_in_dummy (frame)) | |
1017 | return DUMMY_FRAME; | |
1018 | return frame->type; | |
1019 | } | |
1020 | ||
1021 | void | |
1022 | deprecated_set_frame_type (struct frame_info *frame, enum frame_type type) | |
1023 | { | |
1024 | /* Arrrg! See comment in "frame.h". */ | |
1025 | frame->type = type; | |
1026 | } | |
1027 | ||
4c1e7e9d AC |
1028 | #ifdef FRAME_FIND_SAVED_REGS |
1029 | /* XXX - deprecated. This is a compatibility function for targets | |
1030 | that do not yet implement FRAME_INIT_SAVED_REGS. */ | |
1031 | /* Find the addresses in which registers are saved in FRAME. */ | |
1032 | ||
1033 | void | |
1034 | get_frame_saved_regs (struct frame_info *frame, | |
1035 | struct frame_saved_regs *saved_regs_addr) | |
1036 | { | |
1037 | if (frame->saved_regs == NULL) | |
1038 | { | |
1039 | frame->saved_regs = (CORE_ADDR *) | |
1040 | frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS); | |
1041 | } | |
1042 | if (saved_regs_addr == NULL) | |
1043 | { | |
1044 | struct frame_saved_regs saved_regs; | |
1045 | FRAME_FIND_SAVED_REGS (frame, saved_regs); | |
1046 | memcpy (frame->saved_regs, &saved_regs, SIZEOF_FRAME_SAVED_REGS); | |
1047 | } | |
1048 | else | |
1049 | { | |
1050 | FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr); | |
1051 | memcpy (frame->saved_regs, saved_regs_addr, SIZEOF_FRAME_SAVED_REGS); | |
1052 | } | |
1053 | } | |
1054 | #endif | |
1055 | ||
1056 | void | |
1057 | _initialize_frame (void) | |
1058 | { | |
1059 | obstack_init (&frame_cache_obstack); | |
1060 | } |