| 1 | /* Find a variable's value in memory, for GDB, the GNU debugger. |
| 2 | Copyright (C) 1986, 1987 Free Software Foundation, Inc. |
| 3 | |
| 4 | GDB is distributed in the hope that it will be useful, but WITHOUT ANY |
| 5 | WARRANTY. No author or distributor accepts responsibility to anyone |
| 6 | for the consequences of using it or for whether it serves any |
| 7 | particular purpose or works at all, unless he says so in writing. |
| 8 | Refer to the GDB General Public License for full details. |
| 9 | |
| 10 | Everyone is granted permission to copy, modify and redistribute GDB, |
| 11 | but only under the conditions described in the GDB General Public |
| 12 | License. A copy of this license is supposed to have been given to you |
| 13 | along with GDB so you can know your rights and responsibilities. It |
| 14 | should be in a file named COPYING. Among other things, the copyright |
| 15 | notice and this notice must be preserved on all copies. |
| 16 | |
| 17 | In other words, go ahead and share GDB, but don't try to stop |
| 18 | anyone else from sharing it farther. Help stamp out software hoarding! |
| 19 | */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "initialize.h" |
| 23 | #include "param.h" |
| 24 | #include "symtab.h" |
| 25 | #include "frame.h" |
| 26 | #include "value.h" |
| 27 | |
| 28 | CORE_ADDR read_register (); |
| 29 | |
| 30 | START_FILE |
| 31 | \f |
| 32 | /* Return the address in which frame FRAME's value of register REGNUM |
| 33 | has been saved in memory. Or return zero if it has not been saved. |
| 34 | If REGNUM specifies the SP, the value we return is actually |
| 35 | the SP value, not an address where it was saved. */ |
| 36 | |
| 37 | static CORE_ADDR |
| 38 | find_saved_register (frame, regnum) |
| 39 | FRAME frame; |
| 40 | int regnum; |
| 41 | { |
| 42 | struct frame_info fi; |
| 43 | struct frame_saved_regs saved_regs; |
| 44 | |
| 45 | register FRAME frame1 = 0; |
| 46 | register CORE_ADDR addr = 0; |
| 47 | |
| 48 | while (1) |
| 49 | { |
| 50 | QUIT; |
| 51 | fi = get_prev_frame_info (frame1); |
| 52 | if (fi.frame == 0 || fi.frame == frame) |
| 53 | break; |
| 54 | get_frame_saved_regs (&fi, &saved_regs); |
| 55 | if (saved_regs.regs[regnum]) |
| 56 | addr = saved_regs.regs[regnum]; |
| 57 | frame1 = fi.frame; |
| 58 | } |
| 59 | |
| 60 | return addr; |
| 61 | } |
| 62 | |
| 63 | /* Copy the bytes of register REGNUM, relative to the current stack frame, |
| 64 | into our memory at MYADDR. |
| 65 | The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). */ |
| 66 | |
| 67 | void |
| 68 | read_relative_register_raw_bytes (regnum, myaddr) |
| 69 | int regnum; |
| 70 | char *myaddr; |
| 71 | { |
| 72 | register CORE_ADDR addr; |
| 73 | |
| 74 | if (regnum == FP_REGNUM) |
| 75 | { |
| 76 | bcopy (&selected_frame, myaddr, sizeof (CORE_ADDR)); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | addr = find_saved_register (selected_frame, regnum); |
| 81 | |
| 82 | if (addr) |
| 83 | { |
| 84 | if (regnum == SP_REGNUM) |
| 85 | { |
| 86 | CORE_ADDR buffer = addr; |
| 87 | bcopy (&buffer, myaddr, sizeof (CORE_ADDR)); |
| 88 | } |
| 89 | else |
| 90 | read_memory (addr, myaddr, REGISTER_RAW_SIZE (regnum)); |
| 91 | return; |
| 92 | } |
| 93 | read_register_bytes (REGISTER_BYTE (regnum), |
| 94 | myaddr, REGISTER_RAW_SIZE (regnum)); |
| 95 | } |
| 96 | |
| 97 | /* Return a `value' with the contents of register REGNUM |
| 98 | in its virtual format, with the type specified by |
| 99 | REGISTER_VIRTUAL_TYPE. */ |
| 100 | |
| 101 | value |
| 102 | value_of_register (regnum) |
| 103 | int regnum; |
| 104 | { |
| 105 | register CORE_ADDR addr = find_saved_register (selected_frame, regnum); |
| 106 | register value val; |
| 107 | char raw_buffer[MAX_REGISTER_RAW_SIZE]; |
| 108 | char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE]; |
| 109 | |
| 110 | if (addr) |
| 111 | { |
| 112 | if (regnum == SP_REGNUM) |
| 113 | return value_from_long (builtin_type_int, addr); |
| 114 | read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum)); |
| 115 | } |
| 116 | else |
| 117 | read_register_bytes (REGISTER_BYTE (regnum), raw_buffer, |
| 118 | REGISTER_RAW_SIZE (regnum)); |
| 119 | |
| 120 | REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer); |
| 121 | val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum)); |
| 122 | bcopy (virtual_buffer, VALUE_CONTENTS (val), REGISTER_VIRTUAL_SIZE (regnum)); |
| 123 | VALUE_LVAL (val) = addr ? lval_memory : lval_register; |
| 124 | VALUE_ADDRESS (val) = addr ? addr : REGISTER_BYTE (regnum); |
| 125 | VALUE_REGNO (val) = regnum; |
| 126 | return val; |
| 127 | } |
| 128 | \f |
| 129 | /* Low level examining and depositing of registers. |
| 130 | |
| 131 | Note that you must call `fetch_registers' once |
| 132 | before examining or depositing any registers. */ |
| 133 | |
| 134 | char registers[REGISTER_BYTES]; |
| 135 | |
| 136 | /* Copy LEN bytes of consecutive data from registers |
| 137 | starting with the REGBYTE'th byte of register data |
| 138 | into memory at MYADDR. */ |
| 139 | |
| 140 | read_register_bytes (regbyte, myaddr, len) |
| 141 | int regbyte; |
| 142 | char *myaddr; |
| 143 | int len; |
| 144 | { |
| 145 | bcopy (®isters[regbyte], myaddr, len); |
| 146 | } |
| 147 | |
| 148 | /* Copy LEN bytes of consecutive data from memory at MYADDR |
| 149 | into registers starting with the REGBYTE'th byte of register data. */ |
| 150 | |
| 151 | write_register_bytes (regbyte, myaddr, len) |
| 152 | int regbyte; |
| 153 | char *myaddr; |
| 154 | int len; |
| 155 | { |
| 156 | bcopy (myaddr, ®isters[regbyte], len); |
| 157 | if (have_inferior_p ()) |
| 158 | store_inferior_registers (-1); |
| 159 | } |
| 160 | |
| 161 | /* Return the contents of register REGNO, |
| 162 | regarding it as an integer. */ |
| 163 | |
| 164 | CORE_ADDR |
| 165 | read_register (regno) |
| 166 | int regno; |
| 167 | { |
| 168 | /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */ |
| 169 | return *(int *) ®isters[REGISTER_BYTE (regno)]; |
| 170 | } |
| 171 | |
| 172 | /* Store VALUE in the register number REGNO, regarded as an integer. */ |
| 173 | |
| 174 | void |
| 175 | write_register (regno, val) |
| 176 | int regno, val; |
| 177 | { |
| 178 | /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */ |
| 179 | *(int *) ®isters[REGISTER_BYTE (regno)] = val; |
| 180 | |
| 181 | if (have_inferior_p ()) |
| 182 | store_inferior_registers (regno); |
| 183 | } |
| 184 | |
| 185 | /* Record that register REGNO contains VAL. |
| 186 | This is used when the value is obtained from the inferior or core dump, |
| 187 | so there is no need to store the value there. */ |
| 188 | |
| 189 | void |
| 190 | supply_register (regno, val) |
| 191 | int regno; |
| 192 | char *val; |
| 193 | { |
| 194 | bcopy (val, ®isters[REGISTER_BYTE (regno)], REGISTER_RAW_SIZE (regno)); |
| 195 | } |
| 196 | \f |
| 197 | /* Given a struct symbol for a variable, |
| 198 | and a stack frame address, read the value of the variable |
| 199 | and return a (pointer to a) struct value containing the value. */ |
| 200 | |
| 201 | value |
| 202 | read_var_value (var, frame) |
| 203 | register struct symbol *var; |
| 204 | FRAME frame; |
| 205 | { |
| 206 | register value v; |
| 207 | |
| 208 | struct frame_info fi; |
| 209 | |
| 210 | struct type *type = SYMBOL_TYPE (var); |
| 211 | register CORE_ADDR addr = 0; |
| 212 | int val = SYMBOL_VALUE (var); |
| 213 | register int len; |
| 214 | |
| 215 | v = allocate_value (type); |
| 216 | VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */ |
| 217 | len = TYPE_LENGTH (type); |
| 218 | |
| 219 | if (frame == 0) frame = selected_frame; |
| 220 | |
| 221 | switch (SYMBOL_CLASS (var)) |
| 222 | { |
| 223 | case LOC_CONST: |
| 224 | case LOC_LABEL: |
| 225 | bcopy (&val, VALUE_CONTENTS (v), len); |
| 226 | VALUE_LVAL (v) = not_lval; |
| 227 | return v; |
| 228 | |
| 229 | case LOC_CONST_BYTES: |
| 230 | bcopy (val, VALUE_CONTENTS (v), len); |
| 231 | VALUE_LVAL (v) = not_lval; |
| 232 | return v; |
| 233 | |
| 234 | case LOC_STATIC: |
| 235 | addr = val; |
| 236 | break; |
| 237 | |
| 238 | case LOC_ARG: |
| 239 | fi = get_frame_info (frame); |
| 240 | addr = val + FRAME_ARGS_ADDRESS (fi); |
| 241 | break; |
| 242 | |
| 243 | case LOC_LOCAL: |
| 244 | fi = get_frame_info (frame); |
| 245 | addr = val + FRAME_LOCALS_ADDRESS (fi); |
| 246 | break; |
| 247 | |
| 248 | case LOC_TYPEDEF: |
| 249 | error ("Cannot look up value of a typedef"); |
| 250 | |
| 251 | case LOC_BLOCK: |
| 252 | VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var)); |
| 253 | return v; |
| 254 | |
| 255 | case LOC_REGISTER: |
| 256 | { |
| 257 | char raw_buffer[MAX_REGISTER_RAW_SIZE]; |
| 258 | char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE]; |
| 259 | |
| 260 | VALUE_REGNO (v) = val; |
| 261 | |
| 262 | /* Locate the register's contents in a real register or in core; |
| 263 | read the data in raw format. */ |
| 264 | |
| 265 | addr = find_saved_register (frame, val); |
| 266 | if (addr == 0) |
| 267 | { |
| 268 | /* Value is really in a register. */ |
| 269 | |
| 270 | VALUE_LVAL (v) = lval_register; |
| 271 | VALUE_ADDRESS (v) = REGISTER_BYTE (val); |
| 272 | |
| 273 | read_register_bytes (REGISTER_BYTE (val), |
| 274 | raw_buffer, REGISTER_RAW_SIZE (val)); |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | /* Value was in a register that has been saved in memory. */ |
| 279 | |
| 280 | read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (val)); |
| 281 | VALUE_ADDRESS (v) = addr; |
| 282 | } |
| 283 | |
| 284 | /* Convert the raw contents to virtual contents. |
| 285 | (Just copy them if the formats are the same.) */ |
| 286 | |
| 287 | REGISTER_CONVERT_TO_VIRTUAL (val, raw_buffer, virtual_buffer); |
| 288 | |
| 289 | if (REGISTER_CONVERTIBLE (val)) |
| 290 | { |
| 291 | /* When the raw and virtual formats differ, the virtual format |
| 292 | corresponds to a specific data type. If we want that type, |
| 293 | copy the data into the value. |
| 294 | Otherwise, do a type-conversion. */ |
| 295 | |
| 296 | if (type != REGISTER_VIRTUAL_TYPE (val)) |
| 297 | { |
| 298 | /* eg a variable of type `float' in a 68881 register |
| 299 | with raw type `extended' and virtual type `double'. |
| 300 | Fetch it as a `double' and then convert to `float'. */ |
| 301 | v = allocate_value (REGISTER_VIRTUAL_TYPE (val)); |
| 302 | bcopy (virtual_buffer, VALUE_CONTENTS (v), len); |
| 303 | v = value_cast (type, v); |
| 304 | } |
| 305 | else |
| 306 | bcopy (virtual_buffer, VALUE_CONTENTS (v), len); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | /* Raw and virtual formats are the same for this register. */ |
| 311 | |
| 312 | union { int i; char c; } test; |
| 313 | /* If we want less than the full size, we need to |
| 314 | test for a big-endian or little-endian machine. */ |
| 315 | test.i = 1; |
| 316 | if (test.c != 1 && len < REGISTER_RAW_SIZE (val)) |
| 317 | { |
| 318 | /* Big-endian, and we want less than full size. */ |
| 319 | VALUE_OFFSET (v) = REGISTER_RAW_SIZE (val) - len; |
| 320 | } |
| 321 | |
| 322 | bcopy (virtual_buffer + VALUE_OFFSET (v), |
| 323 | VALUE_CONTENTS (v), len); |
| 324 | } |
| 325 | |
| 326 | return v; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | read_memory (addr, VALUE_CONTENTS (v), len); |
| 331 | VALUE_ADDRESS (v) = addr; |
| 332 | return v; |
| 333 | } |
| 334 | \f |
| 335 | /* Given a struct symbol for a variable, |
| 336 | and a stack frame address, |
| 337 | return a (pointer to a) struct value containing the variable's address. */ |
| 338 | |
| 339 | value |
| 340 | locate_var_value (var, frame) |
| 341 | register struct symbol *var; |
| 342 | FRAME frame; |
| 343 | { |
| 344 | register CORE_ADDR addr = 0; |
| 345 | int val = SYMBOL_VALUE (var); |
| 346 | struct frame_info fi; |
| 347 | struct type *type = SYMBOL_TYPE (var); |
| 348 | |
| 349 | if (frame == 0) frame = selected_frame; |
| 350 | |
| 351 | switch (SYMBOL_CLASS (var)) |
| 352 | { |
| 353 | case LOC_CONST: |
| 354 | case LOC_CONST_BYTES: |
| 355 | error ("Address requested for identifier \"%s\" which is a constant.", |
| 356 | SYMBOL_NAME (var)); |
| 357 | |
| 358 | case LOC_REGISTER: |
| 359 | addr = find_saved_register (frame, val); |
| 360 | if (addr != 0) |
| 361 | { |
| 362 | union { int i; char c; } test; |
| 363 | int len = TYPE_LENGTH (type); |
| 364 | /* If var is less than the full size of register, we need to |
| 365 | test for a big-endian or little-endian machine. */ |
| 366 | test.i = 1; |
| 367 | if (test.c != 1 && len < REGISTER_RAW_SIZE (val)) |
| 368 | /* Big-endian, and we want less than full size. */ |
| 369 | addr += REGISTER_RAW_SIZE (val) - len; |
| 370 | break; |
| 371 | } |
| 372 | error ("Address requested for identifier \"%s\" which is in a register.", |
| 373 | SYMBOL_NAME (var)); |
| 374 | |
| 375 | case LOC_STATIC: |
| 376 | case LOC_LABEL: |
| 377 | addr = val; |
| 378 | break; |
| 379 | |
| 380 | case LOC_ARG: |
| 381 | fi = get_frame_info (frame); |
| 382 | addr = val + FRAME_ARGS_ADDRESS (fi); |
| 383 | break; |
| 384 | |
| 385 | case LOC_LOCAL: |
| 386 | fi = get_frame_info (frame); |
| 387 | addr = val + FRAME_LOCALS_ADDRESS (fi); |
| 388 | break; |
| 389 | |
| 390 | case LOC_TYPEDEF: |
| 391 | error ("Address requested for identifier \"%s\" which is a typedef.", |
| 392 | SYMBOL_NAME (var)); |
| 393 | |
| 394 | case LOC_BLOCK: |
| 395 | addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var)); |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | return value_cast (lookup_pointer_type (type), |
| 400 | value_from_long (builtin_type_long, addr)); |
| 401 | } |
| 402 | |
| 403 | static |
| 404 | initialize () |
| 405 | {} |
| 406 | |
| 407 | END_FILE |