| 1 | /* Get info from stack frames; |
| 2 | convert between frames, blocks, functions and pc values. |
| 3 | Copyright 1986, 87, 88, 89, 91, 94, 95, 96, 97, 1998 |
| 4 | 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, Boston, MA 02111-1307, USA. */ |
| 21 | |
| 22 | #include "defs.h" |
| 23 | #include "symtab.h" |
| 24 | #include "bfd.h" |
| 25 | #include "symfile.h" |
| 26 | #include "objfiles.h" |
| 27 | #include "frame.h" |
| 28 | #include "gdbcore.h" |
| 29 | #include "value.h" /* for read_register */ |
| 30 | #include "target.h" /* for target_has_stack */ |
| 31 | #include "inferior.h" /* for read_pc */ |
| 32 | #include "annotate.h" |
| 33 | |
| 34 | /* Prototypes for exported functions. */ |
| 35 | |
| 36 | void _initialize_blockframe PARAMS ((void)); |
| 37 | |
| 38 | /* A default FRAME_CHAIN_VALID, in the form that is suitable for most |
| 39 | targets. If FRAME_CHAIN_VALID returns zero it means that the given |
| 40 | frame is the outermost one and has no caller. */ |
| 41 | |
| 42 | int |
| 43 | default_frame_chain_valid (chain, thisframe) |
| 44 | CORE_ADDR chain; |
| 45 | struct frame_info *thisframe; |
| 46 | { |
| 47 | return ((chain) != 0 |
| 48 | && !inside_main_func ((thisframe) -> pc) |
| 49 | && !inside_entry_func ((thisframe) -> pc)); |
| 50 | } |
| 51 | |
| 52 | /* Use the alternate method of avoiding running up off the end of the |
| 53 | frame chain or following frames back into the startup code. See |
| 54 | the comments in objfiles.h. */ |
| 55 | |
| 56 | int |
| 57 | alternate_frame_chain_valid (chain, thisframe) |
| 58 | CORE_ADDR chain; |
| 59 | struct frame_info *thisframe; |
| 60 | { |
| 61 | return ((chain) != 0 |
| 62 | && !inside_entry_file (FRAME_SAVED_PC (thisframe))); |
| 63 | } |
| 64 | |
| 65 | /* A very simple method of determining a valid frame */ |
| 66 | |
| 67 | int |
| 68 | nonnull_frame_chain_valid (chain, thisframe) |
| 69 | CORE_ADDR chain; |
| 70 | struct frame_info *thisframe; |
| 71 | { |
| 72 | return ((chain) != 0); |
| 73 | } |
| 74 | |
| 75 | /* Is ADDR inside the startup file? Note that if your machine |
| 76 | has a way to detect the bottom of the stack, there is no need |
| 77 | to call this function from FRAME_CHAIN_VALID; the reason for |
| 78 | doing so is that some machines have no way of detecting bottom |
| 79 | of stack. |
| 80 | |
| 81 | A PC of zero is always considered to be the bottom of the stack. */ |
| 82 | |
| 83 | int |
| 84 | inside_entry_file (addr) |
| 85 | CORE_ADDR addr; |
| 86 | { |
| 87 | if (addr == 0) |
| 88 | return 1; |
| 89 | if (symfile_objfile == 0) |
| 90 | return 0; |
| 91 | if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT) |
| 92 | { |
| 93 | /* Do not stop backtracing if the pc is in the call dummy |
| 94 | at the entry point. */ |
| 95 | /* FIXME: Won't always work with zeros for the last two arguments */ |
| 96 | if (PC_IN_CALL_DUMMY (addr, 0, 0)) |
| 97 | return 0; |
| 98 | } |
| 99 | return (addr >= symfile_objfile -> ei.entry_file_lowpc && |
| 100 | addr < symfile_objfile -> ei.entry_file_highpc); |
| 101 | } |
| 102 | |
| 103 | /* Test a specified PC value to see if it is in the range of addresses |
| 104 | that correspond to the main() function. See comments above for why |
| 105 | we might want to do this. |
| 106 | |
| 107 | Typically called from FRAME_CHAIN_VALID. |
| 108 | |
| 109 | A PC of zero is always considered to be the bottom of the stack. */ |
| 110 | |
| 111 | int |
| 112 | inside_main_func (pc) |
| 113 | CORE_ADDR pc; |
| 114 | { |
| 115 | if (pc == 0) |
| 116 | return 1; |
| 117 | if (symfile_objfile == 0) |
| 118 | return 0; |
| 119 | |
| 120 | /* If the addr range is not set up at symbol reading time, set it up now. |
| 121 | This is for FRAME_CHAIN_VALID_ALTERNATE. I do this for coff, because |
| 122 | it is unable to set it up and symbol reading time. */ |
| 123 | |
| 124 | if (symfile_objfile -> ei.main_func_lowpc == INVALID_ENTRY_LOWPC && |
| 125 | symfile_objfile -> ei.main_func_highpc == INVALID_ENTRY_HIGHPC) |
| 126 | { |
| 127 | struct symbol *mainsym; |
| 128 | |
| 129 | mainsym = lookup_symbol ("main", NULL, VAR_NAMESPACE, NULL, NULL); |
| 130 | if (mainsym && SYMBOL_CLASS(mainsym) == LOC_BLOCK) |
| 131 | { |
| 132 | symfile_objfile->ei.main_func_lowpc = |
| 133 | BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym)); |
| 134 | symfile_objfile->ei.main_func_highpc = |
| 135 | BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym)); |
| 136 | } |
| 137 | } |
| 138 | return (symfile_objfile -> ei.main_func_lowpc <= pc && |
| 139 | symfile_objfile -> ei.main_func_highpc > pc); |
| 140 | } |
| 141 | |
| 142 | /* Test a specified PC value to see if it is in the range of addresses |
| 143 | that correspond to the process entry point function. See comments |
| 144 | in objfiles.h for why we might want to do this. |
| 145 | |
| 146 | Typically called from FRAME_CHAIN_VALID. |
| 147 | |
| 148 | A PC of zero is always considered to be the bottom of the stack. */ |
| 149 | |
| 150 | int |
| 151 | inside_entry_func (pc) |
| 152 | CORE_ADDR pc; |
| 153 | { |
| 154 | if (pc == 0) |
| 155 | return 1; |
| 156 | if (symfile_objfile == 0) |
| 157 | return 0; |
| 158 | if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT) |
| 159 | { |
| 160 | /* Do not stop backtracing if the pc is in the call dummy |
| 161 | at the entry point. */ |
| 162 | /* FIXME: Won't always work with zeros for the last two arguments */ |
| 163 | if (PC_IN_CALL_DUMMY (pc, 0, 0)) |
| 164 | return 0; |
| 165 | } |
| 166 | return (symfile_objfile -> ei.entry_func_lowpc <= pc && |
| 167 | symfile_objfile -> ei.entry_func_highpc > pc); |
| 168 | } |
| 169 | |
| 170 | /* Info about the innermost stack frame (contents of FP register) */ |
| 171 | |
| 172 | static struct frame_info *current_frame; |
| 173 | |
| 174 | /* Cache for frame addresses already read by gdb. Valid only while |
| 175 | inferior is stopped. Control variables for the frame cache should |
| 176 | be local to this module. */ |
| 177 | |
| 178 | static struct obstack frame_cache_obstack; |
| 179 | |
| 180 | void * |
| 181 | frame_obstack_alloc (size) |
| 182 | unsigned long size; |
| 183 | { |
| 184 | return obstack_alloc (&frame_cache_obstack, size); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | frame_saved_regs_zalloc (fi) |
| 189 | struct frame_info *fi; |
| 190 | { |
| 191 | fi->saved_regs = (CORE_ADDR*) |
| 192 | frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS); |
| 193 | memset (fi->saved_regs, 0, SIZEOF_FRAME_SAVED_REGS); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /* Return the innermost (currently executing) stack frame. */ |
| 198 | |
| 199 | struct frame_info * |
| 200 | get_current_frame () |
| 201 | { |
| 202 | if (current_frame == NULL) |
| 203 | { |
| 204 | if (target_has_stack) |
| 205 | current_frame = create_new_frame (read_fp (), read_pc ()); |
| 206 | else |
| 207 | error ("No stack."); |
| 208 | } |
| 209 | return current_frame; |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | set_current_frame (frame) |
| 214 | struct frame_info *frame; |
| 215 | { |
| 216 | current_frame = frame; |
| 217 | } |
| 218 | |
| 219 | /* Create an arbitrary (i.e. address specified by user) or innermost frame. |
| 220 | Always returns a non-NULL value. */ |
| 221 | |
| 222 | struct frame_info * |
| 223 | create_new_frame (addr, pc) |
| 224 | CORE_ADDR addr; |
| 225 | CORE_ADDR pc; |
| 226 | { |
| 227 | struct frame_info *fi; |
| 228 | char *name; |
| 229 | |
| 230 | fi = (struct frame_info *) |
| 231 | obstack_alloc (&frame_cache_obstack, |
| 232 | sizeof (struct frame_info)); |
| 233 | |
| 234 | /* Arbitrary frame */ |
| 235 | fi->saved_regs = NULL; |
| 236 | fi->next = NULL; |
| 237 | fi->prev = NULL; |
| 238 | fi->frame = addr; |
| 239 | fi->pc = pc; |
| 240 | find_pc_partial_function (pc, &name, (CORE_ADDR *)NULL,(CORE_ADDR *)NULL); |
| 241 | fi->signal_handler_caller = IN_SIGTRAMP (fi->pc, name); |
| 242 | |
| 243 | #ifdef INIT_EXTRA_FRAME_INFO |
| 244 | INIT_EXTRA_FRAME_INFO (0, fi); |
| 245 | #endif |
| 246 | |
| 247 | return fi; |
| 248 | } |
| 249 | |
| 250 | /* Return the frame that FRAME calls (NULL if FRAME is the innermost |
| 251 | frame). */ |
| 252 | |
| 253 | struct frame_info * |
| 254 | get_next_frame (frame) |
| 255 | struct frame_info *frame; |
| 256 | { |
| 257 | return frame->next; |
| 258 | } |
| 259 | |
| 260 | /* Flush the entire frame cache. */ |
| 261 | |
| 262 | void |
| 263 | flush_cached_frames () |
| 264 | { |
| 265 | /* Since we can't really be sure what the first object allocated was */ |
| 266 | obstack_free (&frame_cache_obstack, 0); |
| 267 | obstack_init (&frame_cache_obstack); |
| 268 | |
| 269 | current_frame = NULL; /* Invalidate cache */ |
| 270 | select_frame (NULL, -1); |
| 271 | annotate_frames_invalid (); |
| 272 | } |
| 273 | |
| 274 | /* Flush the frame cache, and start a new one if necessary. */ |
| 275 | |
| 276 | void |
| 277 | reinit_frame_cache () |
| 278 | { |
| 279 | flush_cached_frames (); |
| 280 | |
| 281 | /* FIXME: The inferior_pid test is wrong if there is a corefile. */ |
| 282 | if (inferior_pid != 0) |
| 283 | { |
| 284 | select_frame (get_current_frame (), 0); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* If a machine allows frameless functions, it should define a macro |
| 289 | FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct |
| 290 | frame_info for the frame, and FRAMELESS should be set to nonzero |
| 291 | if it represents a frameless function invocation. */ |
| 292 | |
| 293 | /* Return nonzero if the function for this frame lacks a prologue. Many |
| 294 | machines can define FRAMELESS_FUNCTION_INVOCATION to just call this |
| 295 | function. */ |
| 296 | |
| 297 | int |
| 298 | frameless_look_for_prologue (frame) |
| 299 | struct frame_info *frame; |
| 300 | { |
| 301 | CORE_ADDR func_start, after_prologue; |
| 302 | func_start = get_pc_function_start (frame->pc); |
| 303 | if (func_start) |
| 304 | { |
| 305 | func_start += FUNCTION_START_OFFSET; |
| 306 | after_prologue = func_start; |
| 307 | #ifdef SKIP_PROLOGUE_FRAMELESS_P |
| 308 | /* This is faster, since only care whether there *is* a prologue, |
| 309 | not how long it is. */ |
| 310 | after_prologue = SKIP_PROLOGUE_FRAMELESS_P (after_prologue); |
| 311 | #else |
| 312 | after_prologue = SKIP_PROLOGUE (after_prologue); |
| 313 | #endif |
| 314 | return after_prologue == func_start; |
| 315 | } |
| 316 | else if (frame->pc == 0) |
| 317 | /* A frame with a zero PC is usually created by dereferencing a NULL |
| 318 | function pointer, normally causing an immediate core dump of the |
| 319 | inferior. Mark function as frameless, as the inferior has no chance |
| 320 | of setting up a stack frame. */ |
| 321 | return 1; |
| 322 | else |
| 323 | /* If we can't find the start of the function, we don't really |
| 324 | know whether the function is frameless, but we should be able |
| 325 | to get a reasonable (i.e. best we can do under the |
| 326 | circumstances) backtrace by saying that it isn't. */ |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* Default a few macros that people seldom redefine. */ |
| 331 | |
| 332 | #if !defined (INIT_FRAME_PC) |
| 333 | #define INIT_FRAME_PC(fromleaf, prev) \ |
| 334 | prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \ |
| 335 | prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ()); |
| 336 | #endif |
| 337 | |
| 338 | #ifndef FRAME_CHAIN_COMBINE |
| 339 | #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain) |
| 340 | #endif |
| 341 | |
| 342 | /* Return a structure containing various interesting information |
| 343 | about the frame that called NEXT_FRAME. Returns NULL |
| 344 | if there is no such frame. */ |
| 345 | |
| 346 | struct frame_info * |
| 347 | get_prev_frame (next_frame) |
| 348 | struct frame_info *next_frame; |
| 349 | { |
| 350 | CORE_ADDR address = 0; |
| 351 | struct frame_info *prev; |
| 352 | int fromleaf = 0; |
| 353 | char *name; |
| 354 | |
| 355 | /* If the requested entry is in the cache, return it. |
| 356 | Otherwise, figure out what the address should be for the entry |
| 357 | we're about to add to the cache. */ |
| 358 | |
| 359 | if (!next_frame) |
| 360 | { |
| 361 | #if 0 |
| 362 | /* This screws value_of_variable, which just wants a nice clean |
| 363 | NULL return from block_innermost_frame if there are no frames. |
| 364 | I don't think I've ever seen this message happen otherwise. |
| 365 | And returning NULL here is a perfectly legitimate thing to do. */ |
| 366 | if (!current_frame) |
| 367 | { |
| 368 | error ("You haven't set up a process's stack to examine."); |
| 369 | } |
| 370 | #endif |
| 371 | |
| 372 | return current_frame; |
| 373 | } |
| 374 | |
| 375 | /* If we have the prev one, return it */ |
| 376 | if (next_frame->prev) |
| 377 | return next_frame->prev; |
| 378 | |
| 379 | /* On some machines it is possible to call a function without |
| 380 | setting up a stack frame for it. On these machines, we |
| 381 | define this macro to take two args; a frameinfo pointer |
| 382 | identifying a frame and a variable to set or clear if it is |
| 383 | or isn't leafless. */ |
| 384 | |
| 385 | /* Still don't want to worry about this except on the innermost |
| 386 | frame. This macro will set FROMLEAF if NEXT_FRAME is a |
| 387 | frameless function invocation. */ |
| 388 | if (!(next_frame->next)) |
| 389 | { |
| 390 | fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame); |
| 391 | if (fromleaf) |
| 392 | address = FRAME_FP (next_frame); |
| 393 | } |
| 394 | |
| 395 | if (!fromleaf) |
| 396 | { |
| 397 | /* Two macros defined in tm.h specify the machine-dependent |
| 398 | actions to be performed here. |
| 399 | First, get the frame's chain-pointer. |
| 400 | If that is zero, the frame is the outermost frame or a leaf |
| 401 | called by the outermost frame. This means that if start |
| 402 | calls main without a frame, we'll return 0 (which is fine |
| 403 | anyway). |
| 404 | |
| 405 | Nope; there's a problem. This also returns when the current |
| 406 | routine is a leaf of main. This is unacceptable. We move |
| 407 | this to after the ffi test; I'd rather have backtraces from |
| 408 | start go curfluy than have an abort called from main not show |
| 409 | main. */ |
| 410 | address = FRAME_CHAIN (next_frame); |
| 411 | if (!FRAME_CHAIN_VALID (address, next_frame)) |
| 412 | return 0; |
| 413 | address = FRAME_CHAIN_COMBINE (address, next_frame); |
| 414 | } |
| 415 | if (address == 0) |
| 416 | return 0; |
| 417 | |
| 418 | prev = (struct frame_info *) |
| 419 | obstack_alloc (&frame_cache_obstack, |
| 420 | sizeof (struct frame_info)); |
| 421 | |
| 422 | prev->saved_regs = NULL; |
| 423 | if (next_frame) |
| 424 | next_frame->prev = prev; |
| 425 | prev->next = next_frame; |
| 426 | prev->prev = (struct frame_info *) 0; |
| 427 | prev->frame = address; |
| 428 | prev->signal_handler_caller = 0; |
| 429 | |
| 430 | /* This change should not be needed, FIXME! We should |
| 431 | determine whether any targets *need* INIT_FRAME_PC to happen |
| 432 | after INIT_EXTRA_FRAME_INFO and come up with a simple way to |
| 433 | express what goes on here. |
| 434 | |
| 435 | INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame |
| 436 | (where the PC is already set up) and here (where it isn't). |
| 437 | INIT_FRAME_PC is only called from here, always after |
| 438 | INIT_EXTRA_FRAME_INFO. |
| 439 | |
| 440 | The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC |
| 441 | value (which hasn't been set yet). Some other machines appear to |
| 442 | require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo. |
| 443 | |
| 444 | We shouldn't need INIT_FRAME_PC_FIRST to add more complication to |
| 445 | an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92. |
| 446 | |
| 447 | Assuming that some machines need INIT_FRAME_PC after |
| 448 | INIT_EXTRA_FRAME_INFO, one possible scheme: |
| 449 | |
| 450 | SETUP_INNERMOST_FRAME() |
| 451 | Default version is just create_new_frame (read_fp ()), |
| 452 | read_pc ()). Machines with extra frame info would do that (or the |
| 453 | local equivalent) and then set the extra fields. |
| 454 | SETUP_ARBITRARY_FRAME(argc, argv) |
| 455 | Only change here is that create_new_frame would no longer init extra |
| 456 | frame info; SETUP_ARBITRARY_FRAME would have to do that. |
| 457 | INIT_PREV_FRAME(fromleaf, prev) |
| 458 | Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC. This should |
| 459 | also return a flag saying whether to keep the new frame, or |
| 460 | whether to discard it, because on some machines (e.g. mips) it |
| 461 | is really awkward to have FRAME_CHAIN_VALID called *before* |
| 462 | INIT_EXTRA_FRAME_INFO (there is no good way to get information |
| 463 | deduced in FRAME_CHAIN_VALID into the extra fields of the new frame). |
| 464 | std_frame_pc(fromleaf, prev) |
| 465 | This is the default setting for INIT_PREV_FRAME. It just does what |
| 466 | the default INIT_FRAME_PC does. Some machines will call it from |
| 467 | INIT_PREV_FRAME (either at the beginning, the end, or in the middle). |
| 468 | Some machines won't use it. |
| 469 | kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */ |
| 470 | |
| 471 | #ifdef INIT_FRAME_PC_FIRST |
| 472 | INIT_FRAME_PC_FIRST (fromleaf, prev); |
| 473 | #endif |
| 474 | |
| 475 | #ifdef INIT_EXTRA_FRAME_INFO |
| 476 | INIT_EXTRA_FRAME_INFO(fromleaf, prev); |
| 477 | #endif |
| 478 | |
| 479 | /* This entry is in the frame queue now, which is good since |
| 480 | FRAME_SAVED_PC may use that queue to figure out its value |
| 481 | (see tm-sparc.h). We want the pc saved in the inferior frame. */ |
| 482 | INIT_FRAME_PC(fromleaf, prev); |
| 483 | |
| 484 | /* If ->frame and ->pc are unchanged, we are in the process of getting |
| 485 | ourselves into an infinite backtrace. Some architectures check this |
| 486 | in FRAME_CHAIN or thereabouts, but it seems like there is no reason |
| 487 | this can't be an architecture-independent check. */ |
| 488 | if (next_frame != NULL) |
| 489 | { |
| 490 | if (prev->frame == next_frame->frame |
| 491 | && prev->pc == next_frame->pc) |
| 492 | { |
| 493 | next_frame->prev = NULL; |
| 494 | obstack_free (&frame_cache_obstack, prev); |
| 495 | return NULL; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | find_pc_partial_function (prev->pc, &name, |
| 500 | (CORE_ADDR *)NULL,(CORE_ADDR *)NULL); |
| 501 | if (IN_SIGTRAMP (prev->pc, name)) |
| 502 | prev->signal_handler_caller = 1; |
| 503 | |
| 504 | return prev; |
| 505 | } |
| 506 | |
| 507 | CORE_ADDR |
| 508 | get_frame_pc (frame) |
| 509 | struct frame_info *frame; |
| 510 | { |
| 511 | return frame->pc; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | #ifdef FRAME_FIND_SAVED_REGS |
| 516 | /* XXX - deprecated. This is a compatibility function for targets |
| 517 | that do not yet implement FRAME_INIT_SAVED_REGS. */ |
| 518 | /* Find the addresses in which registers are saved in FRAME. */ |
| 519 | |
| 520 | void |
| 521 | get_frame_saved_regs (frame, saved_regs_addr) |
| 522 | struct frame_info *frame; |
| 523 | struct frame_saved_regs *saved_regs_addr; |
| 524 | { |
| 525 | if (frame->saved_regs == NULL) |
| 526 | { |
| 527 | frame->saved_regs = (CORE_ADDR*) |
| 528 | frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS); |
| 529 | } |
| 530 | if (saved_regs_addr == NULL) |
| 531 | { |
| 532 | struct frame_saved_regs saved_regs; |
| 533 | FRAME_FIND_SAVED_REGS (frame, saved_regs); |
| 534 | memcpy (frame->saved_regs, &saved_regs, SIZEOF_FRAME_SAVED_REGS); |
| 535 | } |
| 536 | else |
| 537 | { |
| 538 | FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr); |
| 539 | memcpy (frame->saved_regs, saved_regs_addr, SIZEOF_FRAME_SAVED_REGS); |
| 540 | } |
| 541 | } |
| 542 | #endif |
| 543 | |
| 544 | /* Return the innermost lexical block in execution |
| 545 | in a specified stack frame. The frame address is assumed valid. */ |
| 546 | |
| 547 | struct block * |
| 548 | get_frame_block (frame) |
| 549 | struct frame_info *frame; |
| 550 | { |
| 551 | CORE_ADDR pc; |
| 552 | |
| 553 | pc = frame->pc; |
| 554 | if (frame->next != 0 && frame->next->signal_handler_caller == 0) |
| 555 | /* We are not in the innermost frame and we were not interrupted |
| 556 | by a signal. We need to subtract one to get the correct block, |
| 557 | in case the call instruction was the last instruction of the block. |
| 558 | If there are any machines on which the saved pc does not point to |
| 559 | after the call insn, we probably want to make frame->pc point after |
| 560 | the call insn anyway. */ |
| 561 | --pc; |
| 562 | return block_for_pc (pc); |
| 563 | } |
| 564 | |
| 565 | struct block * |
| 566 | get_current_block () |
| 567 | { |
| 568 | return block_for_pc (read_pc ()); |
| 569 | } |
| 570 | |
| 571 | CORE_ADDR |
| 572 | get_pc_function_start (pc) |
| 573 | CORE_ADDR pc; |
| 574 | { |
| 575 | register struct block *bl; |
| 576 | register struct symbol *symbol; |
| 577 | register struct minimal_symbol *msymbol; |
| 578 | CORE_ADDR fstart; |
| 579 | |
| 580 | if ((bl = block_for_pc (pc)) != NULL && |
| 581 | (symbol = block_function (bl)) != NULL) |
| 582 | { |
| 583 | bl = SYMBOL_BLOCK_VALUE (symbol); |
| 584 | fstart = BLOCK_START (bl); |
| 585 | } |
| 586 | else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL) |
| 587 | { |
| 588 | fstart = SYMBOL_VALUE_ADDRESS (msymbol); |
| 589 | } |
| 590 | else |
| 591 | { |
| 592 | fstart = 0; |
| 593 | } |
| 594 | return (fstart); |
| 595 | } |
| 596 | |
| 597 | /* Return the symbol for the function executing in frame FRAME. */ |
| 598 | |
| 599 | struct symbol * |
| 600 | get_frame_function (frame) |
| 601 | struct frame_info *frame; |
| 602 | { |
| 603 | register struct block *bl = get_frame_block (frame); |
| 604 | if (bl == 0) |
| 605 | return 0; |
| 606 | return block_function (bl); |
| 607 | } |
| 608 | \f |
| 609 | |
| 610 | /* Return the blockvector immediately containing the innermost lexical block |
| 611 | containing the specified pc value and section, or 0 if there is none. |
| 612 | PINDEX is a pointer to the index value of the block. If PINDEX |
| 613 | is NULL, we don't pass this information back to the caller. */ |
| 614 | |
| 615 | struct blockvector * |
| 616 | blockvector_for_pc_sect (pc, section, pindex, symtab) |
| 617 | register CORE_ADDR pc; |
| 618 | struct sec *section; |
| 619 | int *pindex; |
| 620 | struct symtab *symtab; |
| 621 | |
| 622 | { |
| 623 | register struct block *b; |
| 624 | register int bot, top, half; |
| 625 | struct blockvector *bl; |
| 626 | |
| 627 | if (symtab == 0) /* if no symtab specified by caller */ |
| 628 | { |
| 629 | /* First search all symtabs for one whose file contains our pc */ |
| 630 | if ((symtab = find_pc_sect_symtab (pc, section)) == 0) |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | bl = BLOCKVECTOR (symtab); |
| 635 | b = BLOCKVECTOR_BLOCK (bl, 0); |
| 636 | |
| 637 | /* Then search that symtab for the smallest block that wins. */ |
| 638 | /* Use binary search to find the last block that starts before PC. */ |
| 639 | |
| 640 | bot = 0; |
| 641 | top = BLOCKVECTOR_NBLOCKS (bl); |
| 642 | |
| 643 | while (top - bot > 1) |
| 644 | { |
| 645 | half = (top - bot + 1) >> 1; |
| 646 | b = BLOCKVECTOR_BLOCK (bl, bot + half); |
| 647 | if (BLOCK_START (b) <= pc) |
| 648 | bot += half; |
| 649 | else |
| 650 | top = bot + half; |
| 651 | } |
| 652 | |
| 653 | /* Now search backward for a block that ends after PC. */ |
| 654 | |
| 655 | while (bot >= 0) |
| 656 | { |
| 657 | b = BLOCKVECTOR_BLOCK (bl, bot); |
| 658 | if (BLOCK_END (b) >= pc) |
| 659 | { |
| 660 | if (pindex) |
| 661 | *pindex = bot; |
| 662 | return bl; |
| 663 | } |
| 664 | bot--; |
| 665 | } |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | /* Return the blockvector immediately containing the innermost lexical block |
| 670 | containing the specified pc value, or 0 if there is none. |
| 671 | Backward compatibility, no section. */ |
| 672 | |
| 673 | struct blockvector * |
| 674 | blockvector_for_pc (pc, pindex) |
| 675 | register CORE_ADDR pc; |
| 676 | int *pindex; |
| 677 | { |
| 678 | return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc), |
| 679 | pindex, NULL); |
| 680 | } |
| 681 | |
| 682 | /* Return the innermost lexical block containing the specified pc value |
| 683 | in the specified section, or 0 if there is none. */ |
| 684 | |
| 685 | struct block * |
| 686 | block_for_pc_sect (pc, section) |
| 687 | register CORE_ADDR pc; |
| 688 | struct sec *section; |
| 689 | { |
| 690 | register struct blockvector *bl; |
| 691 | int index; |
| 692 | |
| 693 | bl = blockvector_for_pc_sect (pc, section, &index, NULL); |
| 694 | if (bl) |
| 695 | return BLOCKVECTOR_BLOCK (bl, index); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* Return the innermost lexical block containing the specified pc value, |
| 700 | or 0 if there is none. Backward compatibility, no section. */ |
| 701 | |
| 702 | struct block * |
| 703 | block_for_pc (pc) |
| 704 | register CORE_ADDR pc; |
| 705 | { |
| 706 | return block_for_pc_sect (pc, find_pc_mapped_section (pc)); |
| 707 | } |
| 708 | |
| 709 | /* Return the function containing pc value PC in section SECTION. |
| 710 | Returns 0 if function is not known. */ |
| 711 | |
| 712 | struct symbol * |
| 713 | find_pc_sect_function (pc, section) |
| 714 | CORE_ADDR pc; |
| 715 | struct sec *section; |
| 716 | { |
| 717 | register struct block *b = block_for_pc_sect (pc, section); |
| 718 | if (b == 0) |
| 719 | return 0; |
| 720 | return block_function (b); |
| 721 | } |
| 722 | |
| 723 | /* Return the function containing pc value PC. |
| 724 | Returns 0 if function is not known. Backward compatibility, no section */ |
| 725 | |
| 726 | struct symbol * |
| 727 | find_pc_function (pc) |
| 728 | CORE_ADDR pc; |
| 729 | { |
| 730 | return find_pc_sect_function (pc, find_pc_mapped_section (pc)); |
| 731 | } |
| 732 | |
| 733 | /* These variables are used to cache the most recent result |
| 734 | * of find_pc_partial_function. */ |
| 735 | |
| 736 | static CORE_ADDR cache_pc_function_low = 0; |
| 737 | static CORE_ADDR cache_pc_function_high = 0; |
| 738 | static char *cache_pc_function_name = 0; |
| 739 | static struct sec *cache_pc_function_section = NULL; |
| 740 | |
| 741 | /* Clear cache, e.g. when symbol table is discarded. */ |
| 742 | |
| 743 | void |
| 744 | clear_pc_function_cache() |
| 745 | { |
| 746 | cache_pc_function_low = 0; |
| 747 | cache_pc_function_high = 0; |
| 748 | cache_pc_function_name = (char *)0; |
| 749 | cache_pc_function_section = NULL; |
| 750 | } |
| 751 | |
| 752 | /* Finds the "function" (text symbol) that is smaller than PC but |
| 753 | greatest of all of the potential text symbols in SECTION. Sets |
| 754 | *NAME and/or *ADDRESS conditionally if that pointer is non-null. |
| 755 | If ENDADDR is non-null, then set *ENDADDR to be the end of the |
| 756 | function (exclusive), but passing ENDADDR as non-null means that |
| 757 | the function might cause symbols to be read. This function either |
| 758 | succeeds or fails (not halfway succeeds). If it succeeds, it sets |
| 759 | *NAME, *ADDRESS, and *ENDADDR to real information and returns 1. |
| 760 | If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and |
| 761 | returns 0. */ |
| 762 | |
| 763 | int |
| 764 | find_pc_sect_partial_function (pc, section, name, address, endaddr) |
| 765 | CORE_ADDR pc; |
| 766 | asection *section; |
| 767 | char **name; |
| 768 | CORE_ADDR *address; |
| 769 | CORE_ADDR *endaddr; |
| 770 | { |
| 771 | struct partial_symtab *pst; |
| 772 | struct symbol *f; |
| 773 | struct minimal_symbol *msymbol; |
| 774 | struct partial_symbol *psb; |
| 775 | struct obj_section *osect; |
| 776 | int i; |
| 777 | CORE_ADDR mapped_pc; |
| 778 | |
| 779 | mapped_pc = overlay_mapped_address (pc, section); |
| 780 | |
| 781 | if (mapped_pc >= cache_pc_function_low && |
| 782 | mapped_pc < cache_pc_function_high && |
| 783 | section == cache_pc_function_section) |
| 784 | goto return_cached_value; |
| 785 | |
| 786 | /* If sigtramp is in the u area, it counts as a function (especially |
| 787 | important for step_1). */ |
| 788 | #if defined SIGTRAMP_START |
| 789 | if (IN_SIGTRAMP (mapped_pc, (char *)NULL)) |
| 790 | { |
| 791 | cache_pc_function_low = SIGTRAMP_START (mapped_pc); |
| 792 | cache_pc_function_high = SIGTRAMP_END (mapped_pc); |
| 793 | cache_pc_function_name = "<sigtramp>"; |
| 794 | cache_pc_function_section = section; |
| 795 | goto return_cached_value; |
| 796 | } |
| 797 | #endif |
| 798 | |
| 799 | msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section); |
| 800 | pst = find_pc_sect_psymtab (mapped_pc, section); |
| 801 | if (pst) |
| 802 | { |
| 803 | /* Need to read the symbols to get a good value for the end address. */ |
| 804 | if (endaddr != NULL && !pst->readin) |
| 805 | { |
| 806 | /* Need to get the terminal in case symbol-reading produces |
| 807 | output. */ |
| 808 | target_terminal_ours_for_output (); |
| 809 | PSYMTAB_TO_SYMTAB (pst); |
| 810 | } |
| 811 | |
| 812 | if (pst->readin) |
| 813 | { |
| 814 | /* Checking whether the msymbol has a larger value is for the |
| 815 | "pathological" case mentioned in print_frame_info. */ |
| 816 | f = find_pc_sect_function (mapped_pc, section); |
| 817 | if (f != NULL |
| 818 | && (msymbol == NULL |
| 819 | || (BLOCK_START (SYMBOL_BLOCK_VALUE (f)) |
| 820 | >= SYMBOL_VALUE_ADDRESS (msymbol)))) |
| 821 | { |
| 822 | cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f)); |
| 823 | cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f)); |
| 824 | cache_pc_function_name = SYMBOL_NAME (f); |
| 825 | cache_pc_function_section = section; |
| 826 | goto return_cached_value; |
| 827 | } |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | /* Now that static symbols go in the minimal symbol table, perhaps |
| 832 | we could just ignore the partial symbols. But at least for now |
| 833 | we use the partial or minimal symbol, whichever is larger. */ |
| 834 | psb = find_pc_sect_psymbol (pst, mapped_pc, section); |
| 835 | |
| 836 | if (psb |
| 837 | && (msymbol == NULL || |
| 838 | (SYMBOL_VALUE_ADDRESS (psb) |
| 839 | >= SYMBOL_VALUE_ADDRESS (msymbol)))) |
| 840 | { |
| 841 | /* This case isn't being cached currently. */ |
| 842 | if (address) |
| 843 | *address = SYMBOL_VALUE_ADDRESS (psb); |
| 844 | if (name) |
| 845 | *name = SYMBOL_NAME (psb); |
| 846 | /* endaddr non-NULL can't happen here. */ |
| 847 | return 1; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | /* Not in the normal symbol tables, see if the pc is in a known section. |
| 853 | If it's not, then give up. This ensures that anything beyond the end |
| 854 | of the text seg doesn't appear to be part of the last function in the |
| 855 | text segment. */ |
| 856 | |
| 857 | osect = find_pc_sect_section (mapped_pc, section); |
| 858 | |
| 859 | if (!osect) |
| 860 | msymbol = NULL; |
| 861 | |
| 862 | /* Must be in the minimal symbol table. */ |
| 863 | if (msymbol == NULL) |
| 864 | { |
| 865 | /* No available symbol. */ |
| 866 | if (name != NULL) |
| 867 | *name = 0; |
| 868 | if (address != NULL) |
| 869 | *address = 0; |
| 870 | if (endaddr != NULL) |
| 871 | *endaddr = 0; |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol); |
| 876 | cache_pc_function_name = SYMBOL_NAME (msymbol); |
| 877 | cache_pc_function_section = section; |
| 878 | |
| 879 | /* Use the lesser of the next minimal symbol in the same section, or |
| 880 | the end of the section, as the end of the function. */ |
| 881 | |
| 882 | /* Step over other symbols at this same address, and symbols in |
| 883 | other sections, to find the next symbol in this section with |
| 884 | a different address. */ |
| 885 | |
| 886 | for (i=1; SYMBOL_NAME (msymbol+i) != NULL; i++) |
| 887 | { |
| 888 | if (SYMBOL_VALUE_ADDRESS (msymbol+i) != SYMBOL_VALUE_ADDRESS (msymbol) |
| 889 | && SYMBOL_BFD_SECTION (msymbol+i) == SYMBOL_BFD_SECTION (msymbol)) |
| 890 | break; |
| 891 | } |
| 892 | |
| 893 | if (SYMBOL_NAME (msymbol + i) != NULL |
| 894 | && SYMBOL_VALUE_ADDRESS (msymbol + i) < osect->endaddr) |
| 895 | cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i); |
| 896 | else |
| 897 | /* We got the start address from the last msymbol in the objfile. |
| 898 | So the end address is the end of the section. */ |
| 899 | cache_pc_function_high = osect->endaddr; |
| 900 | |
| 901 | return_cached_value: |
| 902 | |
| 903 | if (address) |
| 904 | { |
| 905 | if (pc_in_unmapped_range (pc, section)) |
| 906 | *address = overlay_unmapped_address (cache_pc_function_low, section); |
| 907 | else |
| 908 | *address = cache_pc_function_low; |
| 909 | } |
| 910 | |
| 911 | if (name) |
| 912 | *name = cache_pc_function_name; |
| 913 | |
| 914 | if (endaddr) |
| 915 | { |
| 916 | if (pc_in_unmapped_range (pc, section)) |
| 917 | { |
| 918 | /* Because the high address is actually beyond the end of |
| 919 | the function (and therefore possibly beyond the end of |
| 920 | the overlay), we must actually convert (high - 1) |
| 921 | and then add one to that. */ |
| 922 | |
| 923 | *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1, |
| 924 | section); |
| 925 | } |
| 926 | else |
| 927 | *endaddr = cache_pc_function_high; |
| 928 | } |
| 929 | |
| 930 | return 1; |
| 931 | } |
| 932 | |
| 933 | /* Backward compatibility, no section argument */ |
| 934 | |
| 935 | int |
| 936 | find_pc_partial_function (pc, name, address, endaddr) |
| 937 | CORE_ADDR pc; |
| 938 | char **name; |
| 939 | CORE_ADDR *address; |
| 940 | CORE_ADDR *endaddr; |
| 941 | { |
| 942 | asection *section; |
| 943 | |
| 944 | section = find_pc_overlay (pc); |
| 945 | return find_pc_sect_partial_function (pc, section, name, address, endaddr); |
| 946 | } |
| 947 | |
| 948 | /* Return the innermost stack frame executing inside of BLOCK, |
| 949 | or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */ |
| 950 | |
| 951 | struct frame_info * |
| 952 | block_innermost_frame (block) |
| 953 | struct block *block; |
| 954 | { |
| 955 | struct frame_info *frame; |
| 956 | register CORE_ADDR start; |
| 957 | register CORE_ADDR end; |
| 958 | |
| 959 | if (block == NULL) |
| 960 | return NULL; |
| 961 | |
| 962 | start = BLOCK_START (block); |
| 963 | end = BLOCK_END (block); |
| 964 | |
| 965 | frame = NULL; |
| 966 | while (1) |
| 967 | { |
| 968 | frame = get_prev_frame (frame); |
| 969 | if (frame == NULL) |
| 970 | return NULL; |
| 971 | if (frame->pc >= start && frame->pc < end) |
| 972 | return frame; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /* Return the full FRAME which corresponds to the given CORE_ADDR |
| 977 | or NULL if no FRAME on the chain corresponds to CORE_ADDR. */ |
| 978 | |
| 979 | struct frame_info * |
| 980 | find_frame_addr_in_frame_chain (frame_addr) |
| 981 | CORE_ADDR frame_addr; |
| 982 | { |
| 983 | struct frame_info *frame = NULL; |
| 984 | |
| 985 | if (frame_addr == (CORE_ADDR)0) |
| 986 | return NULL; |
| 987 | |
| 988 | while (1) |
| 989 | { |
| 990 | frame = get_prev_frame (frame); |
| 991 | if (frame == NULL) |
| 992 | return NULL; |
| 993 | if (FRAME_FP (frame) == frame_addr) |
| 994 | return frame; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | #ifdef SIGCONTEXT_PC_OFFSET |
| 999 | /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */ |
| 1000 | |
| 1001 | CORE_ADDR |
| 1002 | sigtramp_saved_pc (frame) |
| 1003 | struct frame_info *frame; |
| 1004 | { |
| 1005 | CORE_ADDR sigcontext_addr; |
| 1006 | char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT]; |
| 1007 | int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT; |
| 1008 | int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT; |
| 1009 | |
| 1010 | /* Get sigcontext address, it is the third parameter on the stack. */ |
| 1011 | if (frame->next) |
| 1012 | sigcontext_addr = read_memory_integer (FRAME_ARGS_ADDRESS (frame->next) |
| 1013 | + FRAME_ARGS_SKIP |
| 1014 | + sigcontext_offs, |
| 1015 | ptrbytes); |
| 1016 | else |
| 1017 | sigcontext_addr = read_memory_integer (read_register (SP_REGNUM) |
| 1018 | + sigcontext_offs, |
| 1019 | ptrbytes); |
| 1020 | |
| 1021 | /* Don't cause a memory_error when accessing sigcontext in case the stack |
| 1022 | layout has changed or the stack is corrupt. */ |
| 1023 | target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes); |
| 1024 | return extract_unsigned_integer (buf, ptrbytes); |
| 1025 | } |
| 1026 | #endif /* SIGCONTEXT_PC_OFFSET */ |
| 1027 | |
| 1028 | |
| 1029 | /* Are we in a call dummy? The code below which allows DECR_PC_AFTER_BREAK |
| 1030 | below is for infrun.c, which may give the macro a pc without that |
| 1031 | subtracted out. */ |
| 1032 | |
| 1033 | extern CORE_ADDR text_end; |
| 1034 | |
| 1035 | int |
| 1036 | pc_in_call_dummy_before_text_end (pc, sp, frame_address) |
| 1037 | CORE_ADDR pc; |
| 1038 | CORE_ADDR sp; |
| 1039 | CORE_ADDR frame_address; |
| 1040 | { |
| 1041 | return ((pc) >= text_end - CALL_DUMMY_LENGTH |
| 1042 | && (pc) <= text_end + DECR_PC_AFTER_BREAK); |
| 1043 | } |
| 1044 | |
| 1045 | int |
| 1046 | pc_in_call_dummy_after_text_end (pc, sp, frame_address) |
| 1047 | CORE_ADDR pc; |
| 1048 | CORE_ADDR sp; |
| 1049 | CORE_ADDR frame_address; |
| 1050 | { |
| 1051 | return ((pc) >= text_end |
| 1052 | && (pc) <= text_end + CALL_DUMMY_LENGTH + DECR_PC_AFTER_BREAK); |
| 1053 | } |
| 1054 | |
| 1055 | /* Is the PC in a call dummy? SP and FRAME_ADDRESS are the bottom and |
| 1056 | top of the stack frame which we are checking, where "bottom" and |
| 1057 | "top" refer to some section of memory which contains the code for |
| 1058 | the call dummy. Calls to this macro assume that the contents of |
| 1059 | SP_REGNUM and FP_REGNUM (or the saved values thereof), respectively, |
| 1060 | are the things to pass. |
| 1061 | |
| 1062 | This won't work on the 29k, where SP_REGNUM and FP_REGNUM don't |
| 1063 | have that meaning, but the 29k doesn't use ON_STACK. This could be |
| 1064 | fixed by generalizing this scheme, perhaps by passing in a frame |
| 1065 | and adding a few fields, at least on machines which need them for |
| 1066 | PC_IN_CALL_DUMMY. |
| 1067 | |
| 1068 | Something simpler, like checking for the stack segment, doesn't work, |
| 1069 | since various programs (threads implementations, gcc nested function |
| 1070 | stubs, etc) may either allocate stack frames in another segment, or |
| 1071 | allocate other kinds of code on the stack. */ |
| 1072 | |
| 1073 | int |
| 1074 | pc_in_call_dummy_on_stack (pc, sp, frame_address) |
| 1075 | CORE_ADDR pc; |
| 1076 | CORE_ADDR sp; |
| 1077 | CORE_ADDR frame_address; |
| 1078 | { |
| 1079 | return (INNER_THAN ((sp), (pc)) |
| 1080 | && (frame_address != 0) |
| 1081 | && INNER_THAN ((pc), (frame_address))); |
| 1082 | } |
| 1083 | |
| 1084 | int |
| 1085 | pc_in_call_dummy_at_entry_point (pc, sp, frame_address) |
| 1086 | CORE_ADDR pc; |
| 1087 | CORE_ADDR sp; |
| 1088 | CORE_ADDR frame_address; |
| 1089 | { |
| 1090 | return ((pc) >= CALL_DUMMY_ADDRESS () |
| 1091 | && (pc) <= (CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK)); |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | /* |
| 1096 | * GENERIC DUMMY FRAMES |
| 1097 | * |
| 1098 | * The following code serves to maintain the dummy stack frames for |
| 1099 | * inferior function calls (ie. when gdb calls into the inferior via |
| 1100 | * call_function_by_hand). This code saves the machine state before |
| 1101 | * the call in host memory, so we must maintain an independant stack |
| 1102 | * and keep it consistant etc. I am attempting to make this code |
| 1103 | * generic enough to be used by many targets. |
| 1104 | * |
| 1105 | * The cheapest and most generic way to do CALL_DUMMY on a new target |
| 1106 | * is probably to define CALL_DUMMY to be empty, CALL_DUMMY_LENGTH to |
| 1107 | * zero, and CALL_DUMMY_LOCATION to AT_ENTRY. Then you must remember |
| 1108 | * to define PUSH_RETURN_ADDRESS, because no call instruction will be |
| 1109 | * being executed by the target. Also FRAME_CHAIN_VALID as |
| 1110 | * generic_frame_chain_valid and FIX_CALL_DUMMY as |
| 1111 | * generic_fix_call_dummy. */ |
| 1112 | |
| 1113 | /* Dummy frame. This saves the processor state just prior to setting |
| 1114 | up the inferior function call. Older targets save the registers |
| 1115 | target stack (but that really slows down function calls). */ |
| 1116 | |
| 1117 | struct dummy_frame |
| 1118 | { |
| 1119 | struct dummy_frame *next; |
| 1120 | |
| 1121 | CORE_ADDR pc; |
| 1122 | CORE_ADDR fp; |
| 1123 | CORE_ADDR sp; |
| 1124 | CORE_ADDR top; |
| 1125 | char *registers; |
| 1126 | }; |
| 1127 | |
| 1128 | static struct dummy_frame *dummy_frame_stack = NULL; |
| 1129 | |
| 1130 | /* Function: find_dummy_frame(pc, fp, sp) |
| 1131 | Search the stack of dummy frames for one matching the given PC, FP and SP. |
| 1132 | This is the work-horse for pc_in_call_dummy and read_register_dummy */ |
| 1133 | |
| 1134 | char * |
| 1135 | generic_find_dummy_frame (pc, fp) |
| 1136 | CORE_ADDR pc; |
| 1137 | CORE_ADDR fp; |
| 1138 | { |
| 1139 | struct dummy_frame * dummyframe; |
| 1140 | |
| 1141 | if (pc != entry_point_address ()) |
| 1142 | return 0; |
| 1143 | |
| 1144 | for (dummyframe = dummy_frame_stack; dummyframe != NULL; |
| 1145 | dummyframe = dummyframe->next) |
| 1146 | if (fp == dummyframe->fp |
| 1147 | || fp == dummyframe->sp |
| 1148 | || fp == dummyframe->top) |
| 1149 | /* The frame in question lies between the saved fp and sp, inclusive */ |
| 1150 | return dummyframe->registers; |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | /* Function: pc_in_call_dummy (pc, fp) |
| 1156 | Return true if this is a dummy frame created by gdb for an inferior call */ |
| 1157 | |
| 1158 | int |
| 1159 | generic_pc_in_call_dummy (pc, sp, fp) |
| 1160 | CORE_ADDR pc; |
| 1161 | CORE_ADDR sp; |
| 1162 | CORE_ADDR fp; |
| 1163 | { |
| 1164 | /* if find_dummy_frame succeeds, then PC is in a call dummy */ |
| 1165 | /* Note: SP and not FP is passed on. */ |
| 1166 | return (generic_find_dummy_frame (pc, sp) != 0); |
| 1167 | } |
| 1168 | |
| 1169 | /* Function: read_register_dummy |
| 1170 | Find a saved register from before GDB calls a function in the inferior */ |
| 1171 | |
| 1172 | CORE_ADDR |
| 1173 | generic_read_register_dummy (pc, fp, regno) |
| 1174 | CORE_ADDR pc; |
| 1175 | CORE_ADDR fp; |
| 1176 | int regno; |
| 1177 | { |
| 1178 | char *dummy_regs = generic_find_dummy_frame (pc, fp); |
| 1179 | |
| 1180 | if (dummy_regs) |
| 1181 | return extract_address (&dummy_regs[REGISTER_BYTE (regno)], |
| 1182 | REGISTER_RAW_SIZE(regno)); |
| 1183 | else |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | /* Save all the registers on the dummy frame stack. Most ports save the |
| 1188 | registers on the target stack. This results in lots of unnecessary memory |
| 1189 | references, which are slow when debugging via a serial line. Instead, we |
| 1190 | save all the registers internally, and never write them to the stack. The |
| 1191 | registers get restored when the called function returns to the entry point, |
| 1192 | where a breakpoint is laying in wait. */ |
| 1193 | |
| 1194 | void |
| 1195 | generic_push_dummy_frame () |
| 1196 | { |
| 1197 | struct dummy_frame *dummy_frame; |
| 1198 | CORE_ADDR fp = (get_current_frame ())->frame; |
| 1199 | |
| 1200 | /* check to see if there are stale dummy frames, |
| 1201 | perhaps left over from when a longjump took us out of a |
| 1202 | function that was called by the debugger */ |
| 1203 | |
| 1204 | dummy_frame = dummy_frame_stack; |
| 1205 | while (dummy_frame) |
| 1206 | if (INNER_THAN (dummy_frame->fp, fp)) /* stale -- destroy! */ |
| 1207 | { |
| 1208 | dummy_frame_stack = dummy_frame->next; |
| 1209 | free (dummy_frame->registers); |
| 1210 | free (dummy_frame); |
| 1211 | dummy_frame = dummy_frame_stack; |
| 1212 | } |
| 1213 | else |
| 1214 | dummy_frame = dummy_frame->next; |
| 1215 | |
| 1216 | dummy_frame = xmalloc (sizeof (struct dummy_frame)); |
| 1217 | dummy_frame->registers = xmalloc (REGISTER_BYTES); |
| 1218 | |
| 1219 | dummy_frame->pc = read_register (PC_REGNUM); |
| 1220 | dummy_frame->sp = read_register (SP_REGNUM); |
| 1221 | dummy_frame->top = dummy_frame->sp; |
| 1222 | dummy_frame->fp = fp; |
| 1223 | read_register_bytes (0, dummy_frame->registers, REGISTER_BYTES); |
| 1224 | dummy_frame->next = dummy_frame_stack; |
| 1225 | dummy_frame_stack = dummy_frame; |
| 1226 | } |
| 1227 | |
| 1228 | void |
| 1229 | generic_save_dummy_frame_tos (sp) |
| 1230 | CORE_ADDR sp; |
| 1231 | { |
| 1232 | dummy_frame_stack->top = sp; |
| 1233 | } |
| 1234 | |
| 1235 | /* Function: pop_frame |
| 1236 | Restore the machine state from either the saved dummy stack or a |
| 1237 | real stack frame. */ |
| 1238 | |
| 1239 | void |
| 1240 | generic_pop_current_frame (pop) |
| 1241 | void (*pop) PARAMS ((struct frame_info *frame)); |
| 1242 | { |
| 1243 | struct frame_info *frame = get_current_frame (); |
| 1244 | if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame)) |
| 1245 | generic_pop_dummy_frame (); |
| 1246 | else |
| 1247 | pop (frame); |
| 1248 | } |
| 1249 | |
| 1250 | /* Function: pop_dummy_frame |
| 1251 | Restore the machine state from a saved dummy stack frame. */ |
| 1252 | |
| 1253 | void |
| 1254 | generic_pop_dummy_frame () |
| 1255 | { |
| 1256 | struct dummy_frame *dummy_frame = dummy_frame_stack; |
| 1257 | |
| 1258 | /* FIXME: what if the first frame isn't the right one, eg.. |
| 1259 | because one call-by-hand function has done a longjmp into another one? */ |
| 1260 | |
| 1261 | if (!dummy_frame) |
| 1262 | error ("Can't pop dummy frame!"); |
| 1263 | dummy_frame_stack = dummy_frame->next; |
| 1264 | write_register_bytes (0, dummy_frame->registers, REGISTER_BYTES); |
| 1265 | flush_cached_frames (); |
| 1266 | |
| 1267 | free (dummy_frame->registers); |
| 1268 | free (dummy_frame); |
| 1269 | } |
| 1270 | |
| 1271 | /* Function: frame_chain_valid |
| 1272 | Returns true for a user frame or a call_function_by_hand dummy frame, |
| 1273 | and false for the CRT0 start-up frame. Purpose is to terminate backtrace */ |
| 1274 | |
| 1275 | int |
| 1276 | generic_frame_chain_valid (fp, fi) |
| 1277 | CORE_ADDR fp; |
| 1278 | struct frame_info *fi; |
| 1279 | { |
| 1280 | if (PC_IN_CALL_DUMMY(FRAME_SAVED_PC(fi), fp, fp)) |
| 1281 | return 1; /* don't prune CALL_DUMMY frames */ |
| 1282 | else /* fall back to default algorithm (see frame.h) */ |
| 1283 | return (fp != 0 |
| 1284 | && (INNER_THAN (fi->frame, fp) || fi->frame == fp) |
| 1285 | && !inside_entry_file (FRAME_SAVED_PC(fi))); |
| 1286 | } |
| 1287 | |
| 1288 | /* Function: fix_call_dummy |
| 1289 | Stub function. Generic dumy frames typically do not need to fix |
| 1290 | the frame being created */ |
| 1291 | |
| 1292 | void |
| 1293 | generic_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p) |
| 1294 | char *dummy; |
| 1295 | CORE_ADDR pc; |
| 1296 | CORE_ADDR fun; |
| 1297 | int nargs; |
| 1298 | struct value **args; |
| 1299 | struct type *type; |
| 1300 | int gcc_p; |
| 1301 | { |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | /* Function: get_saved_register |
| 1306 | Find register number REGNUM relative to FRAME and put its (raw, |
| 1307 | target format) contents in *RAW_BUFFER. |
| 1308 | |
| 1309 | Set *OPTIMIZED if the variable was optimized out (and thus can't be |
| 1310 | fetched). Note that this is never set to anything other than zero |
| 1311 | in this implementation. |
| 1312 | |
| 1313 | Set *LVAL to lval_memory, lval_register, or not_lval, depending on |
| 1314 | whether the value was fetched from memory, from a register, or in a |
| 1315 | strange and non-modifiable way (e.g. a frame pointer which was |
| 1316 | calculated rather than fetched). We will use not_lval for values |
| 1317 | fetched from generic dummy frames. |
| 1318 | |
| 1319 | Set *ADDRP to the address, either in memory on as a REGISTER_BYTE |
| 1320 | offset into the registers array. If the value is stored in a dummy |
| 1321 | frame, set *ADDRP to zero. |
| 1322 | |
| 1323 | To use this implementation, define a function called |
| 1324 | "get_saved_register" in your target code, which simply passes all |
| 1325 | of its arguments to this function. |
| 1326 | |
| 1327 | The argument RAW_BUFFER must point to aligned memory. */ |
| 1328 | |
| 1329 | void |
| 1330 | generic_get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval) |
| 1331 | char *raw_buffer; |
| 1332 | int *optimized; |
| 1333 | CORE_ADDR *addrp; |
| 1334 | struct frame_info *frame; |
| 1335 | int regnum; |
| 1336 | enum lval_type *lval; |
| 1337 | { |
| 1338 | if (!target_has_registers) |
| 1339 | error ("No registers."); |
| 1340 | |
| 1341 | /* Normal systems don't optimize out things with register numbers. */ |
| 1342 | if (optimized != NULL) |
| 1343 | *optimized = 0; |
| 1344 | |
| 1345 | if (addrp) /* default assumption: not found in memory */ |
| 1346 | *addrp = 0; |
| 1347 | |
| 1348 | /* Note: since the current frame's registers could only have been |
| 1349 | saved by frames INTERIOR TO the current frame, we skip examining |
| 1350 | the current frame itself: otherwise, we would be getting the |
| 1351 | previous frame's registers which were saved by the current frame. */ |
| 1352 | |
| 1353 | while (frame && ((frame = frame->next) != NULL)) |
| 1354 | { |
| 1355 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
| 1356 | { |
| 1357 | if (lval) /* found it in a CALL_DUMMY frame */ |
| 1358 | *lval = not_lval; |
| 1359 | if (raw_buffer) |
| 1360 | memcpy (raw_buffer, |
| 1361 | generic_find_dummy_frame (frame->pc, frame->frame) + |
| 1362 | REGISTER_BYTE (regnum), |
| 1363 | REGISTER_RAW_SIZE (regnum)); |
| 1364 | return; |
| 1365 | } |
| 1366 | |
| 1367 | FRAME_INIT_SAVED_REGS (frame); |
| 1368 | if (frame->saved_regs != NULL |
| 1369 | && frame->saved_regs[regnum] != 0) |
| 1370 | { |
| 1371 | if (lval) /* found it saved on the stack */ |
| 1372 | *lval = lval_memory; |
| 1373 | if (regnum == SP_REGNUM) |
| 1374 | { |
| 1375 | if (raw_buffer) /* SP register treated specially */ |
| 1376 | store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), |
| 1377 | frame->saved_regs[regnum]); |
| 1378 | } |
| 1379 | else |
| 1380 | { |
| 1381 | if (addrp) /* any other register */ |
| 1382 | *addrp = frame->saved_regs[regnum]; |
| 1383 | if (raw_buffer) |
| 1384 | read_memory (frame->saved_regs[regnum], raw_buffer, |
| 1385 | REGISTER_RAW_SIZE (regnum)); |
| 1386 | } |
| 1387 | return; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | /* If we get thru the loop to this point, it means the register was |
| 1392 | not saved in any frame. Return the actual live-register value. */ |
| 1393 | |
| 1394 | if (lval) /* found it in a live register */ |
| 1395 | *lval = lval_register; |
| 1396 | if (addrp) |
| 1397 | *addrp = REGISTER_BYTE (regnum); |
| 1398 | if (raw_buffer) |
| 1399 | read_register_gen (regnum, raw_buffer); |
| 1400 | } |
| 1401 | |
| 1402 | void |
| 1403 | _initialize_blockframe () |
| 1404 | { |
| 1405 | obstack_init (&frame_cache_obstack); |
| 1406 | } |