| 1 | /* Cache and manage frames for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, |
| 4 | 2001, 2002 Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 21 | Boston, MA 02111-1307, USA. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "frame.h" |
| 25 | #include "target.h" |
| 26 | #include "value.h" |
| 27 | #include "inferior.h" /* for inferior_ptid */ |
| 28 | #include "regcache.h" |
| 29 | #include "gdb_assert.h" |
| 30 | #include "gdb_string.h" |
| 31 | #include "builtin-regs.h" |
| 32 | #include "gdb_obstack.h" |
| 33 | #include "dummy-frame.h" |
| 34 | #include "gdbcore.h" |
| 35 | #include "annotate.h" |
| 36 | |
| 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 | |
| 87 | void |
| 88 | frame_register_unwind (struct frame_info *frame, int regnum, |
| 89 | int *optimizedp, enum lval_type *lvalp, |
| 90 | CORE_ADDR *addrp, int *realnump, void *bufferp) |
| 91 | { |
| 92 | struct frame_unwind_cache *cache; |
| 93 | |
| 94 | /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates |
| 95 | that the value proper does not need to be fetched. */ |
| 96 | gdb_assert (optimizedp != NULL); |
| 97 | gdb_assert (lvalp != NULL); |
| 98 | gdb_assert (addrp != NULL); |
| 99 | gdb_assert (realnump != NULL); |
| 100 | /* gdb_assert (bufferp != NULL); */ |
| 101 | |
| 102 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a |
| 103 | special case, there was always an inner frame dedicated to the |
| 104 | hardware registers. Unfortunatly, there is too much unwind code |
| 105 | around that looks up/down the frame chain while making the |
| 106 | assumption that each frame level is using the same unwind code. */ |
| 107 | |
| 108 | if (frame == NULL) |
| 109 | { |
| 110 | /* We're in the inner-most frame, get the value direct from the |
| 111 | register cache. */ |
| 112 | *optimizedp = 0; |
| 113 | *lvalp = lval_register; |
| 114 | /* ULGH! Code uses the offset into the raw register byte array |
| 115 | as a way of identifying a register. */ |
| 116 | *addrp = REGISTER_BYTE (regnum); |
| 117 | /* Should this code test ``register_cached (regnum) < 0'' and do |
| 118 | something like set realnum to -1 when the register isn't |
| 119 | available? */ |
| 120 | *realnump = regnum; |
| 121 | if (bufferp) |
| 122 | deprecated_read_register_gen (regnum, bufferp); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | /* Ask this frame to unwind its register. */ |
| 127 | frame->register_unwind (frame, &frame->register_unwind_cache, regnum, |
| 128 | optimizedp, lvalp, addrp, realnump, bufferp); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | frame_register (struct frame_info *frame, int regnum, |
| 133 | int *optimizedp, enum lval_type *lvalp, |
| 134 | CORE_ADDR *addrp, int *realnump, void *bufferp) |
| 135 | { |
| 136 | /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates |
| 137 | that the value proper does not need to be fetched. */ |
| 138 | gdb_assert (optimizedp != NULL); |
| 139 | gdb_assert (lvalp != NULL); |
| 140 | gdb_assert (addrp != NULL); |
| 141 | gdb_assert (realnump != NULL); |
| 142 | /* gdb_assert (bufferp != NULL); */ |
| 143 | |
| 144 | /* Ulgh! Old code that, for lval_register, sets ADDRP to the offset |
| 145 | of the register in the register cache. It should instead return |
| 146 | the REGNUM corresponding to that register. Translate the . */ |
| 147 | if (GET_SAVED_REGISTER_P ()) |
| 148 | { |
| 149 | GET_SAVED_REGISTER (bufferp, optimizedp, addrp, frame, regnum, lvalp); |
| 150 | /* Compute the REALNUM if the caller wants it. */ |
| 151 | if (*lvalp == lval_register) |
| 152 | { |
| 153 | int regnum; |
| 154 | for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++) |
| 155 | { |
| 156 | if (*addrp == register_offset_hack (current_gdbarch, regnum)) |
| 157 | { |
| 158 | *realnump = regnum; |
| 159 | return; |
| 160 | } |
| 161 | } |
| 162 | internal_error (__FILE__, __LINE__, |
| 163 | "Failed to compute the register number corresponding" |
| 164 | " to 0x%s", paddr_d (*addrp)); |
| 165 | } |
| 166 | *realnump = -1; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | /* Reached the the bottom (youngest, inner most) of the frame chain |
| 171 | (youngest, inner most) frame, go direct to the hardware register |
| 172 | cache (do not pass go, do not try to cache the value, ...). The |
| 173 | unwound value would have been cached in frame->next but that |
| 174 | doesn't exist. This doesn't matter as the hardware register |
| 175 | cache is stopping any unnecessary accesses to the target. */ |
| 176 | |
| 177 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a |
| 178 | special case, there was always an inner frame dedicated to the |
| 179 | hardware registers. Unfortunatly, there is too much unwind code |
| 180 | around that looks up/down the frame chain while making the |
| 181 | assumption that each frame level is using the same unwind code. */ |
| 182 | |
| 183 | if (frame == NULL) |
| 184 | frame_register_unwind (NULL, regnum, optimizedp, lvalp, addrp, realnump, |
| 185 | bufferp); |
| 186 | else |
| 187 | frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, |
| 188 | realnump, bufferp); |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | frame_unwind_signed_register (struct frame_info *frame, int regnum, |
| 193 | LONGEST *val) |
| 194 | { |
| 195 | int optimized; |
| 196 | CORE_ADDR addr; |
| 197 | int realnum; |
| 198 | enum lval_type lval; |
| 199 | void *buf = alloca (MAX_REGISTER_RAW_SIZE); |
| 200 | frame_register_unwind (frame, regnum, &optimized, &lval, &addr, |
| 201 | &realnum, buf); |
| 202 | (*val) = extract_signed_integer (buf, REGISTER_VIRTUAL_SIZE (regnum)); |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | frame_unwind_unsigned_register (struct frame_info *frame, int regnum, |
| 207 | ULONGEST *val) |
| 208 | { |
| 209 | int optimized; |
| 210 | CORE_ADDR addr; |
| 211 | int realnum; |
| 212 | enum lval_type lval; |
| 213 | void *buf = alloca (MAX_REGISTER_RAW_SIZE); |
| 214 | frame_register_unwind (frame, regnum, &optimized, &lval, &addr, |
| 215 | &realnum, buf); |
| 216 | (*val) = extract_unsigned_integer (buf, REGISTER_VIRTUAL_SIZE (regnum)); |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | frame_read_unsigned_register (struct frame_info *frame, int regnum, |
| 221 | ULONGEST *val) |
| 222 | { |
| 223 | /* NOTE: cagney/2002-10-31: There is a bit of dogma here - there is |
| 224 | always a frame. Both this, and the equivalent |
| 225 | frame_read_signed_register() function, can only be called with a |
| 226 | valid frame. If, for some reason, this function is called |
| 227 | without a frame then the problem isn't here, but rather in the |
| 228 | caller. It should of first created a frame and then passed that |
| 229 | in. */ |
| 230 | /* NOTE: cagney/2002-10-31: As a side bar, keep in mind that the |
| 231 | ``current_frame'' should not be treated as a special case. While |
| 232 | ``get_next_frame (current_frame) == NULL'' currently holds, it |
| 233 | should, as far as possible, not be relied upon. In the future, |
| 234 | ``get_next_frame (current_frame)'' may instead simply return a |
| 235 | normal frame object that simply always gets register values from |
| 236 | the register cache. Consequently, frame code should try to avoid |
| 237 | tests like ``if get_next_frame() == NULL'' and instead just rely |
| 238 | on recursive frame calls (like the below code) when manipulating |
| 239 | a frame chain. */ |
| 240 | gdb_assert (frame != NULL); |
| 241 | frame_unwind_unsigned_register (get_next_frame (frame), regnum, val); |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | frame_read_signed_register (struct frame_info *frame, int regnum, |
| 246 | LONGEST *val) |
| 247 | { |
| 248 | /* See note in frame_read_unsigned_register(). */ |
| 249 | gdb_assert (frame != NULL); |
| 250 | frame_unwind_signed_register (get_next_frame (frame), regnum, val); |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | generic_unwind_get_saved_register (char *raw_buffer, |
| 255 | int *optimizedp, |
| 256 | CORE_ADDR *addrp, |
| 257 | struct frame_info *frame, |
| 258 | int regnum, |
| 259 | enum lval_type *lvalp) |
| 260 | { |
| 261 | int optimizedx; |
| 262 | CORE_ADDR addrx; |
| 263 | int realnumx; |
| 264 | enum lval_type lvalx; |
| 265 | |
| 266 | if (!target_has_registers) |
| 267 | error ("No registers."); |
| 268 | |
| 269 | /* Keep things simple, ensure that all the pointers (except valuep) |
| 270 | are non NULL. */ |
| 271 | if (optimizedp == NULL) |
| 272 | optimizedp = &optimizedx; |
| 273 | if (lvalp == NULL) |
| 274 | lvalp = &lvalx; |
| 275 | if (addrp == NULL) |
| 276 | addrp = &addrx; |
| 277 | |
| 278 | /* Reached the the bottom (youngest, inner most) of the frame chain |
| 279 | (youngest, inner most) frame, go direct to the hardware register |
| 280 | cache (do not pass go, do not try to cache the value, ...). The |
| 281 | unwound value would have been cached in frame->next but that |
| 282 | doesn't exist. This doesn't matter as the hardware register |
| 283 | cache is stopping any unnecessary accesses to the target. */ |
| 284 | |
| 285 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a |
| 286 | special case, there was always an inner frame dedicated to the |
| 287 | hardware registers. Unfortunatly, there is too much unwind code |
| 288 | around that looks up/down the frame chain while making the |
| 289 | assumption that each frame level is using the same unwind code. */ |
| 290 | |
| 291 | if (frame == NULL) |
| 292 | frame_register_unwind (NULL, regnum, optimizedp, lvalp, addrp, &realnumx, |
| 293 | raw_buffer); |
| 294 | else |
| 295 | frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, |
| 296 | &realnumx, raw_buffer); |
| 297 | } |
| 298 | |
| 299 | void |
| 300 | get_saved_register (char *raw_buffer, |
| 301 | int *optimized, |
| 302 | CORE_ADDR *addrp, |
| 303 | struct frame_info *frame, |
| 304 | int regnum, |
| 305 | enum lval_type *lval) |
| 306 | { |
| 307 | if (GET_SAVED_REGISTER_P ()) |
| 308 | { |
| 309 | GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval); |
| 310 | return; |
| 311 | } |
| 312 | generic_unwind_get_saved_register (raw_buffer, optimized, addrp, frame, |
| 313 | regnum, lval); |
| 314 | } |
| 315 | |
| 316 | /* frame_register_read () |
| 317 | |
| 318 | Find and return the value of REGNUM for the specified stack frame. |
| 319 | The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). |
| 320 | |
| 321 | Returns 0 if the register value could not be found. */ |
| 322 | |
| 323 | int |
| 324 | frame_register_read (struct frame_info *frame, int regnum, void *myaddr) |
| 325 | { |
| 326 | int optimized; |
| 327 | enum lval_type lval; |
| 328 | CORE_ADDR addr; |
| 329 | int realnum; |
| 330 | frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr); |
| 331 | |
| 332 | /* FIXME: cagney/2002-05-15: This test, is just bogus. |
| 333 | |
| 334 | It indicates that the target failed to supply a value for a |
| 335 | register because it was "not available" at this time. Problem |
| 336 | is, the target still has the register and so get saved_register() |
| 337 | may be returning a value saved on the stack. */ |
| 338 | |
| 339 | if (register_cached (regnum) < 0) |
| 340 | return 0; /* register value not available */ |
| 341 | |
| 342 | return !optimized; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /* Map between a frame register number and its name. A frame register |
| 347 | space is a superset of the cooked register space --- it also |
| 348 | includes builtin registers. */ |
| 349 | |
| 350 | int |
| 351 | frame_map_name_to_regnum (const char *name, int len) |
| 352 | { |
| 353 | int i; |
| 354 | |
| 355 | /* Search register name space. */ |
| 356 | for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++) |
| 357 | if (REGISTER_NAME (i) && len == strlen (REGISTER_NAME (i)) |
| 358 | && strncmp (name, REGISTER_NAME (i), len) == 0) |
| 359 | { |
| 360 | return i; |
| 361 | } |
| 362 | |
| 363 | /* Try builtin registers. */ |
| 364 | i = builtin_reg_map_name_to_regnum (name, len); |
| 365 | if (i >= 0) |
| 366 | { |
| 367 | /* A builtin register doesn't fall into the architecture's |
| 368 | register range. */ |
| 369 | gdb_assert (i >= NUM_REGS + NUM_PSEUDO_REGS); |
| 370 | return i; |
| 371 | } |
| 372 | |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | const char * |
| 377 | frame_map_regnum_to_name (int regnum) |
| 378 | { |
| 379 | if (regnum < 0) |
| 380 | return NULL; |
| 381 | if (regnum < NUM_REGS + NUM_PSEUDO_REGS) |
| 382 | return REGISTER_NAME (regnum); |
| 383 | return builtin_reg_map_regnum_to_name (regnum); |
| 384 | } |
| 385 | |
| 386 | /* Info about the innermost stack frame (contents of FP register) */ |
| 387 | |
| 388 | static struct frame_info *current_frame; |
| 389 | |
| 390 | /* Cache for frame addresses already read by gdb. Valid only while |
| 391 | inferior is stopped. Control variables for the frame cache should |
| 392 | be local to this module. */ |
| 393 | |
| 394 | static struct obstack frame_cache_obstack; |
| 395 | |
| 396 | void * |
| 397 | frame_obstack_alloc (unsigned long size) |
| 398 | { |
| 399 | return obstack_alloc (&frame_cache_obstack, size); |
| 400 | } |
| 401 | |
| 402 | void |
| 403 | frame_saved_regs_zalloc (struct frame_info *fi) |
| 404 | { |
| 405 | fi->saved_regs = (CORE_ADDR *) |
| 406 | frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS); |
| 407 | memset (fi->saved_regs, 0, SIZEOF_FRAME_SAVED_REGS); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | /* Return the innermost (currently executing) stack frame. */ |
| 412 | |
| 413 | struct frame_info * |
| 414 | get_current_frame (void) |
| 415 | { |
| 416 | if (current_frame == NULL) |
| 417 | { |
| 418 | if (target_has_stack) |
| 419 | current_frame = create_new_frame (read_fp (), read_pc ()); |
| 420 | else |
| 421 | error ("No stack."); |
| 422 | } |
| 423 | return current_frame; |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | set_current_frame (struct frame_info *frame) |
| 428 | { |
| 429 | current_frame = frame; |
| 430 | } |
| 431 | |
| 432 | /* Return the register saved in the simplistic ``saved_regs'' cache. |
| 433 | If the value isn't here AND a value is needed, try the next inner |
| 434 | most frame. */ |
| 435 | |
| 436 | static void |
| 437 | frame_saved_regs_register_unwind (struct frame_info *frame, void **cache, |
| 438 | int regnum, int *optimizedp, |
| 439 | enum lval_type *lvalp, CORE_ADDR *addrp, |
| 440 | int *realnump, void *bufferp) |
| 441 | { |
| 442 | /* There is always a frame at this point. And THIS is the frame |
| 443 | we're interested in. */ |
| 444 | gdb_assert (frame != NULL); |
| 445 | /* If we're using generic dummy frames, we'd better not be in a call |
| 446 | dummy. (generic_call_dummy_register_unwind ought to have been called |
| 447 | instead.) */ |
| 448 | gdb_assert (!(USE_GENERIC_DUMMY_FRAMES |
| 449 | && PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))); |
| 450 | |
| 451 | /* Load the saved_regs register cache. */ |
| 452 | if (frame->saved_regs == NULL) |
| 453 | FRAME_INIT_SAVED_REGS (frame); |
| 454 | |
| 455 | if (frame->saved_regs != NULL |
| 456 | && frame->saved_regs[regnum] != 0) |
| 457 | { |
| 458 | if (regnum == SP_REGNUM) |
| 459 | { |
| 460 | /* SP register treated specially. */ |
| 461 | *optimizedp = 0; |
| 462 | *lvalp = not_lval; |
| 463 | *addrp = 0; |
| 464 | *realnump = -1; |
| 465 | if (bufferp != NULL) |
| 466 | store_address (bufferp, REGISTER_RAW_SIZE (regnum), |
| 467 | frame->saved_regs[regnum]); |
| 468 | } |
| 469 | else |
| 470 | { |
| 471 | /* Any other register is saved in memory, fetch it but cache |
| 472 | a local copy of its value. */ |
| 473 | *optimizedp = 0; |
| 474 | *lvalp = lval_memory; |
| 475 | *addrp = frame->saved_regs[regnum]; |
| 476 | *realnump = -1; |
| 477 | if (bufferp != NULL) |
| 478 | { |
| 479 | #if 1 |
| 480 | /* Save each register value, as it is read in, in a |
| 481 | frame based cache. */ |
| 482 | void **regs = (*cache); |
| 483 | if (regs == NULL) |
| 484 | { |
| 485 | int sizeof_cache = ((NUM_REGS + NUM_PSEUDO_REGS) |
| 486 | * sizeof (void *)); |
| 487 | regs = frame_obstack_alloc (sizeof_cache); |
| 488 | memset (regs, 0, sizeof_cache); |
| 489 | (*cache) = regs; |
| 490 | } |
| 491 | if (regs[regnum] == NULL) |
| 492 | { |
| 493 | regs[regnum] |
| 494 | = frame_obstack_alloc (REGISTER_RAW_SIZE (regnum)); |
| 495 | read_memory (frame->saved_regs[regnum], regs[regnum], |
| 496 | REGISTER_RAW_SIZE (regnum)); |
| 497 | } |
| 498 | memcpy (bufferp, regs[regnum], REGISTER_RAW_SIZE (regnum)); |
| 499 | #else |
| 500 | /* Read the value in from memory. */ |
| 501 | read_memory (frame->saved_regs[regnum], bufferp, |
| 502 | REGISTER_RAW_SIZE (regnum)); |
| 503 | #endif |
| 504 | } |
| 505 | } |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | /* No luck, assume this and the next frame have the same register |
| 510 | value. If a value is needed, pass the request on down the chain; |
| 511 | otherwise just return an indication that the value is in the same |
| 512 | register as the next frame. */ |
| 513 | if (bufferp == NULL) |
| 514 | { |
| 515 | *optimizedp = 0; |
| 516 | *lvalp = lval_register; |
| 517 | *addrp = 0; |
| 518 | *realnump = regnum; |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, |
| 523 | realnump, bufferp); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /* Function: get_saved_register |
| 528 | Find register number REGNUM relative to FRAME and put its (raw, |
| 529 | target format) contents in *RAW_BUFFER. |
| 530 | |
| 531 | Set *OPTIMIZED if the variable was optimized out (and thus can't be |
| 532 | fetched). Note that this is never set to anything other than zero |
| 533 | in this implementation. |
| 534 | |
| 535 | Set *LVAL to lval_memory, lval_register, or not_lval, depending on |
| 536 | whether the value was fetched from memory, from a register, or in a |
| 537 | strange and non-modifiable way (e.g. a frame pointer which was |
| 538 | calculated rather than fetched). We will use not_lval for values |
| 539 | fetched from generic dummy frames. |
| 540 | |
| 541 | Set *ADDRP to the address, either in memory or as a REGISTER_BYTE |
| 542 | offset into the registers array. If the value is stored in a dummy |
| 543 | frame, set *ADDRP to zero. |
| 544 | |
| 545 | To use this implementation, define a function called |
| 546 | "get_saved_register" in your target code, which simply passes all |
| 547 | of its arguments to this function. |
| 548 | |
| 549 | The argument RAW_BUFFER must point to aligned memory. */ |
| 550 | |
| 551 | void |
| 552 | deprecated_generic_get_saved_register (char *raw_buffer, int *optimized, |
| 553 | CORE_ADDR *addrp, |
| 554 | struct frame_info *frame, int regnum, |
| 555 | enum lval_type *lval) |
| 556 | { |
| 557 | if (!target_has_registers) |
| 558 | error ("No registers."); |
| 559 | |
| 560 | /* Normal systems don't optimize out things with register numbers. */ |
| 561 | if (optimized != NULL) |
| 562 | *optimized = 0; |
| 563 | |
| 564 | if (addrp) /* default assumption: not found in memory */ |
| 565 | *addrp = 0; |
| 566 | |
| 567 | /* Note: since the current frame's registers could only have been |
| 568 | saved by frames INTERIOR TO the current frame, we skip examining |
| 569 | the current frame itself: otherwise, we would be getting the |
| 570 | previous frame's registers which were saved by the current frame. */ |
| 571 | |
| 572 | while (frame && ((frame = frame->next) != NULL)) |
| 573 | { |
| 574 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
| 575 | { |
| 576 | if (lval) /* found it in a CALL_DUMMY frame */ |
| 577 | *lval = not_lval; |
| 578 | if (raw_buffer) |
| 579 | /* FIXME: cagney/2002-06-26: This should be via the |
| 580 | gdbarch_register_read() method so that it, on the fly, |
| 581 | constructs either a raw or pseudo register from the raw |
| 582 | register cache. */ |
| 583 | regcache_raw_read (generic_find_dummy_frame (frame->pc, |
| 584 | frame->frame), |
| 585 | regnum, raw_buffer); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | FRAME_INIT_SAVED_REGS (frame); |
| 590 | if (frame->saved_regs != NULL |
| 591 | && frame->saved_regs[regnum] != 0) |
| 592 | { |
| 593 | if (lval) /* found it saved on the stack */ |
| 594 | *lval = lval_memory; |
| 595 | if (regnum == SP_REGNUM) |
| 596 | { |
| 597 | if (raw_buffer) /* SP register treated specially */ |
| 598 | store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), |
| 599 | frame->saved_regs[regnum]); |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | if (addrp) /* any other register */ |
| 604 | *addrp = frame->saved_regs[regnum]; |
| 605 | if (raw_buffer) |
| 606 | read_memory (frame->saved_regs[regnum], raw_buffer, |
| 607 | REGISTER_RAW_SIZE (regnum)); |
| 608 | } |
| 609 | return; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* If we get thru the loop to this point, it means the register was |
| 614 | not saved in any frame. Return the actual live-register value. */ |
| 615 | |
| 616 | if (lval) /* found it in a live register */ |
| 617 | *lval = lval_register; |
| 618 | if (addrp) |
| 619 | *addrp = REGISTER_BYTE (regnum); |
| 620 | if (raw_buffer) |
| 621 | deprecated_read_register_gen (regnum, raw_buffer); |
| 622 | } |
| 623 | |
| 624 | /* Using the PC, select a mechanism for unwinding a frame returning |
| 625 | the previous frame. The register unwind function should, on |
| 626 | demand, initialize the ->context object. */ |
| 627 | |
| 628 | static void |
| 629 | set_unwind_by_pc (CORE_ADDR pc, CORE_ADDR fp, |
| 630 | frame_register_unwind_ftype **unwind) |
| 631 | { |
| 632 | if (!USE_GENERIC_DUMMY_FRAMES) |
| 633 | /* Still need to set this to something. The ``info frame'' code |
| 634 | calls this function to find out where the saved registers are. |
| 635 | Hopefully this is robust enough to stop any core dumps and |
| 636 | return vaguely correct values.. */ |
| 637 | *unwind = frame_saved_regs_register_unwind; |
| 638 | else if (PC_IN_CALL_DUMMY (pc, fp, fp)) |
| 639 | *unwind = dummy_frame_register_unwind; |
| 640 | else |
| 641 | *unwind = frame_saved_regs_register_unwind; |
| 642 | } |
| 643 | |
| 644 | /* Create an arbitrary (i.e. address specified by user) or innermost frame. |
| 645 | Always returns a non-NULL value. */ |
| 646 | |
| 647 | struct frame_info * |
| 648 | create_new_frame (CORE_ADDR addr, CORE_ADDR pc) |
| 649 | { |
| 650 | struct frame_info *fi; |
| 651 | char *name; |
| 652 | |
| 653 | fi = (struct frame_info *) |
| 654 | obstack_alloc (&frame_cache_obstack, |
| 655 | sizeof (struct frame_info)); |
| 656 | |
| 657 | /* Zero all fields by default. */ |
| 658 | memset (fi, 0, sizeof (struct frame_info)); |
| 659 | |
| 660 | fi->frame = addr; |
| 661 | fi->pc = pc; |
| 662 | find_pc_partial_function (pc, &name, (CORE_ADDR *) NULL, (CORE_ADDR *) NULL); |
| 663 | fi->signal_handler_caller = PC_IN_SIGTRAMP (fi->pc, name); |
| 664 | |
| 665 | if (INIT_EXTRA_FRAME_INFO_P ()) |
| 666 | INIT_EXTRA_FRAME_INFO (0, fi); |
| 667 | |
| 668 | /* Select/initialize an unwind function. */ |
| 669 | set_unwind_by_pc (fi->pc, fi->frame, &fi->register_unwind); |
| 670 | |
| 671 | return fi; |
| 672 | } |
| 673 | |
| 674 | /* Return the frame that FRAME calls (NULL if FRAME is the innermost |
| 675 | frame). */ |
| 676 | |
| 677 | struct frame_info * |
| 678 | get_next_frame (struct frame_info *frame) |
| 679 | { |
| 680 | return frame->next; |
| 681 | } |
| 682 | |
| 683 | /* Flush the entire frame cache. */ |
| 684 | |
| 685 | void |
| 686 | flush_cached_frames (void) |
| 687 | { |
| 688 | /* Since we can't really be sure what the first object allocated was */ |
| 689 | obstack_free (&frame_cache_obstack, 0); |
| 690 | obstack_init (&frame_cache_obstack); |
| 691 | |
| 692 | current_frame = NULL; /* Invalidate cache */ |
| 693 | select_frame (NULL); |
| 694 | annotate_frames_invalid (); |
| 695 | } |
| 696 | |
| 697 | /* Flush the frame cache, and start a new one if necessary. */ |
| 698 | |
| 699 | void |
| 700 | reinit_frame_cache (void) |
| 701 | { |
| 702 | flush_cached_frames (); |
| 703 | |
| 704 | /* FIXME: The inferior_ptid test is wrong if there is a corefile. */ |
| 705 | if (PIDGET (inferior_ptid) != 0) |
| 706 | { |
| 707 | select_frame (get_current_frame ()); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | /* Return a structure containing various interesting information |
| 712 | about the frame that called NEXT_FRAME. Returns NULL |
| 713 | if there is no such frame. */ |
| 714 | |
| 715 | struct frame_info * |
| 716 | get_prev_frame (struct frame_info *next_frame) |
| 717 | { |
| 718 | CORE_ADDR address = 0; |
| 719 | struct frame_info *prev; |
| 720 | int fromleaf = 0; |
| 721 | char *name; |
| 722 | |
| 723 | /* If the requested entry is in the cache, return it. |
| 724 | Otherwise, figure out what the address should be for the entry |
| 725 | we're about to add to the cache. */ |
| 726 | |
| 727 | if (!next_frame) |
| 728 | { |
| 729 | #if 0 |
| 730 | /* This screws value_of_variable, which just wants a nice clean |
| 731 | NULL return from block_innermost_frame if there are no frames. |
| 732 | I don't think I've ever seen this message happen otherwise. |
| 733 | And returning NULL here is a perfectly legitimate thing to do. */ |
| 734 | if (!current_frame) |
| 735 | { |
| 736 | error ("You haven't set up a process's stack to examine."); |
| 737 | } |
| 738 | #endif |
| 739 | |
| 740 | return current_frame; |
| 741 | } |
| 742 | |
| 743 | /* If we have the prev one, return it */ |
| 744 | if (next_frame->prev) |
| 745 | return next_frame->prev; |
| 746 | |
| 747 | /* On some machines it is possible to call a function without |
| 748 | setting up a stack frame for it. On these machines, we |
| 749 | define this macro to take two args; a frameinfo pointer |
| 750 | identifying a frame and a variable to set or clear if it is |
| 751 | or isn't leafless. */ |
| 752 | |
| 753 | /* Still don't want to worry about this except on the innermost |
| 754 | frame. This macro will set FROMLEAF if NEXT_FRAME is a |
| 755 | frameless function invocation. */ |
| 756 | if (!(next_frame->next)) |
| 757 | { |
| 758 | fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame); |
| 759 | if (fromleaf) |
| 760 | address = FRAME_FP (next_frame); |
| 761 | } |
| 762 | |
| 763 | if (!fromleaf) |
| 764 | { |
| 765 | /* Two macros defined in tm.h specify the machine-dependent |
| 766 | actions to be performed here. |
| 767 | First, get the frame's chain-pointer. |
| 768 | If that is zero, the frame is the outermost frame or a leaf |
| 769 | called by the outermost frame. This means that if start |
| 770 | calls main without a frame, we'll return 0 (which is fine |
| 771 | anyway). |
| 772 | |
| 773 | Nope; there's a problem. This also returns when the current |
| 774 | routine is a leaf of main. This is unacceptable. We move |
| 775 | this to after the ffi test; I'd rather have backtraces from |
| 776 | start go curfluy than have an abort called from main not show |
| 777 | main. */ |
| 778 | address = FRAME_CHAIN (next_frame); |
| 779 | |
| 780 | /* FIXME: cagney/2002-06-08: There should be two tests here. |
| 781 | The first would check for a valid frame chain based on a user |
| 782 | selectable policy. The default being ``stop at main'' (as |
| 783 | implemented by generic_func_frame_chain_valid()). Other |
| 784 | policies would be available - stop at NULL, .... The second |
| 785 | test, if provided by the target architecture, would check for |
| 786 | more exotic cases - most target architectures wouldn't bother |
| 787 | with this second case. */ |
| 788 | if (!FRAME_CHAIN_VALID (address, next_frame)) |
| 789 | return 0; |
| 790 | } |
| 791 | if (address == 0) |
| 792 | return 0; |
| 793 | |
| 794 | prev = (struct frame_info *) |
| 795 | obstack_alloc (&frame_cache_obstack, |
| 796 | sizeof (struct frame_info)); |
| 797 | |
| 798 | /* Zero all fields by default. */ |
| 799 | memset (prev, 0, sizeof (struct frame_info)); |
| 800 | |
| 801 | if (next_frame) |
| 802 | next_frame->prev = prev; |
| 803 | prev->next = next_frame; |
| 804 | prev->frame = address; |
| 805 | prev->level = next_frame->level + 1; |
| 806 | |
| 807 | /* This change should not be needed, FIXME! We should |
| 808 | determine whether any targets *need* INIT_FRAME_PC to happen |
| 809 | after INIT_EXTRA_FRAME_INFO and come up with a simple way to |
| 810 | express what goes on here. |
| 811 | |
| 812 | INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame |
| 813 | (where the PC is already set up) and here (where it isn't). |
| 814 | INIT_FRAME_PC is only called from here, always after |
| 815 | INIT_EXTRA_FRAME_INFO. |
| 816 | |
| 817 | The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC |
| 818 | value (which hasn't been set yet). Some other machines appear to |
| 819 | require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo. |
| 820 | |
| 821 | We shouldn't need INIT_FRAME_PC_FIRST to add more complication to |
| 822 | an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92. |
| 823 | |
| 824 | Assuming that some machines need INIT_FRAME_PC after |
| 825 | INIT_EXTRA_FRAME_INFO, one possible scheme: |
| 826 | |
| 827 | SETUP_INNERMOST_FRAME() |
| 828 | Default version is just create_new_frame (read_fp ()), |
| 829 | read_pc ()). Machines with extra frame info would do that (or the |
| 830 | local equivalent) and then set the extra fields. |
| 831 | SETUP_ARBITRARY_FRAME(argc, argv) |
| 832 | Only change here is that create_new_frame would no longer init extra |
| 833 | frame info; SETUP_ARBITRARY_FRAME would have to do that. |
| 834 | INIT_PREV_FRAME(fromleaf, prev) |
| 835 | Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC. This should |
| 836 | also return a flag saying whether to keep the new frame, or |
| 837 | whether to discard it, because on some machines (e.g. mips) it |
| 838 | is really awkward to have FRAME_CHAIN_VALID called *before* |
| 839 | INIT_EXTRA_FRAME_INFO (there is no good way to get information |
| 840 | deduced in FRAME_CHAIN_VALID into the extra fields of the new frame). |
| 841 | std_frame_pc(fromleaf, prev) |
| 842 | This is the default setting for INIT_PREV_FRAME. It just does what |
| 843 | the default INIT_FRAME_PC does. Some machines will call it from |
| 844 | INIT_PREV_FRAME (either at the beginning, the end, or in the middle). |
| 845 | Some machines won't use it. |
| 846 | kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */ |
| 847 | |
| 848 | INIT_FRAME_PC_FIRST (fromleaf, prev); |
| 849 | |
| 850 | if (INIT_EXTRA_FRAME_INFO_P ()) |
| 851 | INIT_EXTRA_FRAME_INFO (fromleaf, prev); |
| 852 | |
| 853 | /* This entry is in the frame queue now, which is good since |
| 854 | FRAME_SAVED_PC may use that queue to figure out its value |
| 855 | (see tm-sparc.h). We want the pc saved in the inferior frame. */ |
| 856 | INIT_FRAME_PC (fromleaf, prev); |
| 857 | |
| 858 | /* If ->frame and ->pc are unchanged, we are in the process of getting |
| 859 | ourselves into an infinite backtrace. Some architectures check this |
| 860 | in FRAME_CHAIN or thereabouts, but it seems like there is no reason |
| 861 | this can't be an architecture-independent check. */ |
| 862 | if (next_frame != NULL) |
| 863 | { |
| 864 | if (prev->frame == next_frame->frame |
| 865 | && prev->pc == next_frame->pc) |
| 866 | { |
| 867 | next_frame->prev = NULL; |
| 868 | obstack_free (&frame_cache_obstack, prev); |
| 869 | return NULL; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | /* Initialize the code used to unwind the frame PREV based on the PC |
| 874 | (and probably other architectural information). The PC lets you |
| 875 | check things like the debug info at that point (dwarf2cfi?) and |
| 876 | use that to decide how the frame should be unwound. */ |
| 877 | set_unwind_by_pc (prev->pc, prev->frame, &prev->register_unwind); |
| 878 | |
| 879 | find_pc_partial_function (prev->pc, &name, |
| 880 | (CORE_ADDR *) NULL, (CORE_ADDR *) NULL); |
| 881 | if (PC_IN_SIGTRAMP (prev->pc, name)) |
| 882 | prev->signal_handler_caller = 1; |
| 883 | |
| 884 | return prev; |
| 885 | } |
| 886 | |
| 887 | CORE_ADDR |
| 888 | get_frame_pc (struct frame_info *frame) |
| 889 | { |
| 890 | return frame->pc; |
| 891 | } |
| 892 | |
| 893 | #ifdef FRAME_FIND_SAVED_REGS |
| 894 | /* XXX - deprecated. This is a compatibility function for targets |
| 895 | that do not yet implement FRAME_INIT_SAVED_REGS. */ |
| 896 | /* Find the addresses in which registers are saved in FRAME. */ |
| 897 | |
| 898 | void |
| 899 | get_frame_saved_regs (struct frame_info *frame, |
| 900 | struct frame_saved_regs *saved_regs_addr) |
| 901 | { |
| 902 | if (frame->saved_regs == NULL) |
| 903 | { |
| 904 | frame->saved_regs = (CORE_ADDR *) |
| 905 | frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS); |
| 906 | } |
| 907 | if (saved_regs_addr == NULL) |
| 908 | { |
| 909 | struct frame_saved_regs saved_regs; |
| 910 | FRAME_FIND_SAVED_REGS (frame, saved_regs); |
| 911 | memcpy (frame->saved_regs, &saved_regs, SIZEOF_FRAME_SAVED_REGS); |
| 912 | } |
| 913 | else |
| 914 | { |
| 915 | FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr); |
| 916 | memcpy (frame->saved_regs, saved_regs_addr, SIZEOF_FRAME_SAVED_REGS); |
| 917 | } |
| 918 | } |
| 919 | #endif |
| 920 | |
| 921 | void |
| 922 | _initialize_frame (void) |
| 923 | { |
| 924 | obstack_init (&frame_cache_obstack); |
| 925 | } |