| 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 | |
| 33 | /* Return a frame uniq ID that can be used to, later re-find the |
| 34 | frame. */ |
| 35 | |
| 36 | void |
| 37 | get_frame_id (struct frame_info *fi, struct frame_id *id) |
| 38 | { |
| 39 | if (fi == NULL) |
| 40 | { |
| 41 | id->base = 0; |
| 42 | id->pc = 0; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | id->base = FRAME_FP (fi); |
| 47 | id->pc = fi->pc; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | struct frame_info * |
| 52 | frame_find_by_id (struct frame_id id) |
| 53 | { |
| 54 | struct frame_info *frame; |
| 55 | |
| 56 | /* ZERO denotes the null frame, let the caller decide what to do |
| 57 | about it. Should it instead return get_current_frame()? */ |
| 58 | if (id.base == 0 && id.pc == 0) |
| 59 | return NULL; |
| 60 | |
| 61 | for (frame = get_current_frame (); |
| 62 | frame != NULL; |
| 63 | frame = get_prev_frame (frame)) |
| 64 | { |
| 65 | if (INNER_THAN (FRAME_FP (frame), id.base)) |
| 66 | /* ``inner/current < frame < id.base''. Keep looking along |
| 67 | the frame chain. */ |
| 68 | continue; |
| 69 | if (INNER_THAN (id.base, FRAME_FP (frame))) |
| 70 | /* ``inner/current < id.base < frame''. Oops, gone past it. |
| 71 | Just give up. */ |
| 72 | return NULL; |
| 73 | /* FIXME: cagney/2002-04-21: This isn't sufficient. It should |
| 74 | use id.pc to check that the two frames belong to the same |
| 75 | function. Otherwise we'll do things like match dummy frames |
| 76 | or mis-match frameless functions. However, until someone |
| 77 | notices, stick with the existing behavour. */ |
| 78 | return frame; |
| 79 | } |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | frame_register_unwind (struct frame_info *frame, int regnum, |
| 85 | int *optimizedp, enum lval_type *lvalp, |
| 86 | CORE_ADDR *addrp, int *realnump, void *bufferp) |
| 87 | { |
| 88 | struct frame_unwind_cache *cache; |
| 89 | |
| 90 | /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates |
| 91 | that the value proper does not need to be fetched. */ |
| 92 | gdb_assert (optimizedp != NULL); |
| 93 | gdb_assert (lvalp != NULL); |
| 94 | gdb_assert (addrp != NULL); |
| 95 | gdb_assert (realnump != NULL); |
| 96 | /* gdb_assert (bufferp != NULL); */ |
| 97 | |
| 98 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a |
| 99 | special case, there was always an inner frame dedicated to the |
| 100 | hardware registers. Unfortunatly, there is too much unwind code |
| 101 | around that looks up/down the frame chain while making the |
| 102 | assumption that each frame level is using the same unwind code. */ |
| 103 | |
| 104 | if (frame == NULL) |
| 105 | { |
| 106 | /* We're in the inner-most frame, get the value direct from the |
| 107 | register cache. */ |
| 108 | *optimizedp = 0; |
| 109 | *lvalp = lval_register; |
| 110 | /* ULGH! Code uses the offset into the raw register byte array |
| 111 | as a way of identifying a register. */ |
| 112 | *addrp = REGISTER_BYTE (regnum); |
| 113 | /* Should this code test ``register_cached (regnum) < 0'' and do |
| 114 | something like set realnum to -1 when the register isn't |
| 115 | available? */ |
| 116 | *realnump = regnum; |
| 117 | if (bufferp) |
| 118 | deprecated_read_register_gen (regnum, bufferp); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | /* Ask this frame to unwind its register. */ |
| 123 | frame->register_unwind (frame, &frame->register_unwind_cache, regnum, |
| 124 | optimizedp, lvalp, addrp, realnump, bufferp); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | frame_register (struct frame_info *frame, int regnum, |
| 129 | int *optimizedp, enum lval_type *lvalp, |
| 130 | CORE_ADDR *addrp, int *realnump, void *bufferp) |
| 131 | { |
| 132 | /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates |
| 133 | that the value proper does not need to be fetched. */ |
| 134 | gdb_assert (optimizedp != NULL); |
| 135 | gdb_assert (lvalp != NULL); |
| 136 | gdb_assert (addrp != NULL); |
| 137 | gdb_assert (realnump != NULL); |
| 138 | /* gdb_assert (bufferp != NULL); */ |
| 139 | |
| 140 | /* Ulgh! Old code that, for lval_register, sets ADDRP to the offset |
| 141 | of the register in the register cache. It should instead return |
| 142 | the REGNUM corresponding to that register. Translate the . */ |
| 143 | if (GET_SAVED_REGISTER_P ()) |
| 144 | { |
| 145 | GET_SAVED_REGISTER (bufferp, optimizedp, addrp, frame, regnum, lvalp); |
| 146 | /* Compute the REALNUM if the caller wants it. */ |
| 147 | if (*lvalp == lval_register) |
| 148 | { |
| 149 | int regnum; |
| 150 | for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++) |
| 151 | { |
| 152 | if (*addrp == register_offset_hack (current_gdbarch, regnum)) |
| 153 | { |
| 154 | *realnump = regnum; |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | internal_error (__FILE__, __LINE__, |
| 159 | "Failed to compute the register number corresponding" |
| 160 | " to 0x%s", paddr_d (*addrp)); |
| 161 | } |
| 162 | *realnump = -1; |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | /* Reached the the bottom (youngest, inner most) of the frame chain |
| 167 | (youngest, inner most) frame, go direct to the hardware register |
| 168 | cache (do not pass go, do not try to cache the value, ...). The |
| 169 | unwound value would have been cached in frame->next but that |
| 170 | doesn't exist. This doesn't matter as the hardware register |
| 171 | cache is stopping any unnecessary accesses to the target. */ |
| 172 | |
| 173 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a |
| 174 | special case, there was always an inner frame dedicated to the |
| 175 | hardware registers. Unfortunatly, there is too much unwind code |
| 176 | around that looks up/down the frame chain while making the |
| 177 | assumption that each frame level is using the same unwind code. */ |
| 178 | |
| 179 | if (frame == NULL) |
| 180 | frame_register_unwind (NULL, regnum, optimizedp, lvalp, addrp, realnump, |
| 181 | bufferp); |
| 182 | else |
| 183 | frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, |
| 184 | realnump, bufferp); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | frame_unwind_signed_register (struct frame_info *frame, int regnum, |
| 189 | LONGEST *val) |
| 190 | { |
| 191 | int optimized; |
| 192 | CORE_ADDR addr; |
| 193 | int realnum; |
| 194 | enum lval_type lval; |
| 195 | void *buf = alloca (MAX_REGISTER_RAW_SIZE); |
| 196 | frame_register_unwind (frame, regnum, &optimized, &lval, &addr, |
| 197 | &realnum, buf); |
| 198 | (*val) = extract_signed_integer (buf, REGISTER_VIRTUAL_SIZE (regnum)); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | frame_unwind_unsigned_register (struct frame_info *frame, int regnum, |
| 203 | ULONGEST *val) |
| 204 | { |
| 205 | int optimized; |
| 206 | CORE_ADDR addr; |
| 207 | int realnum; |
| 208 | enum lval_type lval; |
| 209 | void *buf = alloca (MAX_REGISTER_RAW_SIZE); |
| 210 | frame_register_unwind (frame, regnum, &optimized, &lval, &addr, |
| 211 | &realnum, buf); |
| 212 | (*val) = extract_unsigned_integer (buf, REGISTER_VIRTUAL_SIZE (regnum)); |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | frame_read_unsigned_register (struct frame_info *frame, int regnum, |
| 217 | ULONGEST *val) |
| 218 | { |
| 219 | /* NOTE: cagney/2002-10-31: There is a bit of dogma here - there is |
| 220 | always a frame. Both this, and the equivalent |
| 221 | frame_read_signed_register() function, can only be called with a |
| 222 | valid frame. If, for some reason, this function is called |
| 223 | without a frame then the problem isn't here, but rather in the |
| 224 | caller. It should of first created a frame and then passed that |
| 225 | in. */ |
| 226 | /* NOTE: cagney/2002-10-31: As a side bar, keep in mind that the |
| 227 | ``current_frame'' should not be treated as a special case. While |
| 228 | ``get_next_frame (current_frame) == NULL'' currently holds, it |
| 229 | should, as far as possible, not be relied upon. In the future, |
| 230 | ``get_next_frame (current_frame)'' may instead simply return a |
| 231 | normal frame object that simply always gets register values from |
| 232 | the register cache. Consequently, frame code should try to avoid |
| 233 | tests like ``if get_next_frame() == NULL'' and instead just rely |
| 234 | on recursive frame calls (like the below code) when manipulating |
| 235 | a frame chain. */ |
| 236 | gdb_assert (frame != NULL); |
| 237 | frame_unwind_unsigned_register (get_next_frame (frame), regnum, val); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | frame_read_signed_register (struct frame_info *frame, int regnum, |
| 242 | LONGEST *val) |
| 243 | { |
| 244 | /* See note in frame_read_unsigned_register(). */ |
| 245 | gdb_assert (frame != NULL); |
| 246 | frame_unwind_signed_register (get_next_frame (frame), regnum, val); |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | generic_unwind_get_saved_register (char *raw_buffer, |
| 251 | int *optimizedp, |
| 252 | CORE_ADDR *addrp, |
| 253 | struct frame_info *frame, |
| 254 | int regnum, |
| 255 | enum lval_type *lvalp) |
| 256 | { |
| 257 | int optimizedx; |
| 258 | CORE_ADDR addrx; |
| 259 | int realnumx; |
| 260 | enum lval_type lvalx; |
| 261 | |
| 262 | if (!target_has_registers) |
| 263 | error ("No registers."); |
| 264 | |
| 265 | /* Keep things simple, ensure that all the pointers (except valuep) |
| 266 | are non NULL. */ |
| 267 | if (optimizedp == NULL) |
| 268 | optimizedp = &optimizedx; |
| 269 | if (lvalp == NULL) |
| 270 | lvalp = &lvalx; |
| 271 | if (addrp == NULL) |
| 272 | addrp = &addrx; |
| 273 | |
| 274 | /* Reached the the bottom (youngest, inner most) of the frame chain |
| 275 | (youngest, inner most) frame, go direct to the hardware register |
| 276 | cache (do not pass go, do not try to cache the value, ...). The |
| 277 | unwound value would have been cached in frame->next but that |
| 278 | doesn't exist. This doesn't matter as the hardware register |
| 279 | cache is stopping any unnecessary accesses to the target. */ |
| 280 | |
| 281 | /* NOTE: cagney/2002-04-14: It would be nice if, instead of a |
| 282 | special case, there was always an inner frame dedicated to the |
| 283 | hardware registers. Unfortunatly, there is too much unwind code |
| 284 | around that looks up/down the frame chain while making the |
| 285 | assumption that each frame level is using the same unwind code. */ |
| 286 | |
| 287 | if (frame == NULL) |
| 288 | frame_register_unwind (NULL, regnum, optimizedp, lvalp, addrp, &realnumx, |
| 289 | raw_buffer); |
| 290 | else |
| 291 | frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, |
| 292 | &realnumx, raw_buffer); |
| 293 | } |
| 294 | |
| 295 | void |
| 296 | get_saved_register (char *raw_buffer, |
| 297 | int *optimized, |
| 298 | CORE_ADDR *addrp, |
| 299 | struct frame_info *frame, |
| 300 | int regnum, |
| 301 | enum lval_type *lval) |
| 302 | { |
| 303 | if (GET_SAVED_REGISTER_P ()) |
| 304 | { |
| 305 | GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval); |
| 306 | return; |
| 307 | } |
| 308 | generic_unwind_get_saved_register (raw_buffer, optimized, addrp, frame, |
| 309 | regnum, lval); |
| 310 | } |
| 311 | |
| 312 | /* frame_register_read () |
| 313 | |
| 314 | Find and return the value of REGNUM for the specified stack frame. |
| 315 | The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). |
| 316 | |
| 317 | Returns 0 if the register value could not be found. */ |
| 318 | |
| 319 | int |
| 320 | frame_register_read (struct frame_info *frame, int regnum, void *myaddr) |
| 321 | { |
| 322 | int optimized; |
| 323 | enum lval_type lval; |
| 324 | CORE_ADDR addr; |
| 325 | int realnum; |
| 326 | frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr); |
| 327 | |
| 328 | /* FIXME: cagney/2002-05-15: This test, is just bogus. |
| 329 | |
| 330 | It indicates that the target failed to supply a value for a |
| 331 | register because it was "not available" at this time. Problem |
| 332 | is, the target still has the register and so get saved_register() |
| 333 | may be returning a value saved on the stack. */ |
| 334 | |
| 335 | if (register_cached (regnum) < 0) |
| 336 | return 0; /* register value not available */ |
| 337 | |
| 338 | return !optimized; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /* Map between a frame register number and its name. A frame register |
| 343 | space is a superset of the cooked register space --- it also |
| 344 | includes builtin registers. */ |
| 345 | |
| 346 | int |
| 347 | frame_map_name_to_regnum (const char *name, int len) |
| 348 | { |
| 349 | int i; |
| 350 | |
| 351 | /* Search register name space. */ |
| 352 | for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++) |
| 353 | if (REGISTER_NAME (i) && len == strlen (REGISTER_NAME (i)) |
| 354 | && strncmp (name, REGISTER_NAME (i), len) == 0) |
| 355 | { |
| 356 | return i; |
| 357 | } |
| 358 | |
| 359 | /* Try builtin registers. */ |
| 360 | i = builtin_reg_map_name_to_regnum (name, len); |
| 361 | if (i >= 0) |
| 362 | { |
| 363 | /* A builtin register doesn't fall into the architecture's |
| 364 | register range. */ |
| 365 | gdb_assert (i >= NUM_REGS + NUM_PSEUDO_REGS); |
| 366 | return i; |
| 367 | } |
| 368 | |
| 369 | return -1; |
| 370 | } |
| 371 | |
| 372 | const char * |
| 373 | frame_map_regnum_to_name (int regnum) |
| 374 | { |
| 375 | if (regnum < 0) |
| 376 | return NULL; |
| 377 | if (regnum < NUM_REGS + NUM_PSEUDO_REGS) |
| 378 | return REGISTER_NAME (regnum); |
| 379 | return builtin_reg_map_regnum_to_name (regnum); |
| 380 | } |