| 1 | /* Low level packing and unpacking of values for GDB, the GNU Debugger. |
| 2 | Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include <string.h> |
| 22 | #include "symtab.h" |
| 23 | #include "gdbtypes.h" |
| 24 | #include "value.h" |
| 25 | #include "gdbcore.h" |
| 26 | #include "frame.h" |
| 27 | #include "command.h" |
| 28 | #include "gdbcmd.h" |
| 29 | #include "target.h" |
| 30 | #include "demangle.h" |
| 31 | |
| 32 | /* Local function prototypes. */ |
| 33 | |
| 34 | static value |
| 35 | value_headof PARAMS ((value, struct type *, struct type *)); |
| 36 | |
| 37 | static void |
| 38 | show_values PARAMS ((char *, int)); |
| 39 | |
| 40 | static void |
| 41 | show_convenience PARAMS ((char *, int)); |
| 42 | |
| 43 | /* The value-history records all the values printed |
| 44 | by print commands during this session. Each chunk |
| 45 | records 60 consecutive values. The first chunk on |
| 46 | the chain records the most recent values. |
| 47 | The total number of values is in value_history_count. */ |
| 48 | |
| 49 | #define VALUE_HISTORY_CHUNK 60 |
| 50 | |
| 51 | struct value_history_chunk |
| 52 | { |
| 53 | struct value_history_chunk *next; |
| 54 | value values[VALUE_HISTORY_CHUNK]; |
| 55 | }; |
| 56 | |
| 57 | /* Chain of chunks now in use. */ |
| 58 | |
| 59 | static struct value_history_chunk *value_history_chain; |
| 60 | |
| 61 | static int value_history_count; /* Abs number of last entry stored */ |
| 62 | \f |
| 63 | /* List of all value objects currently allocated |
| 64 | (except for those released by calls to release_value) |
| 65 | This is so they can be freed after each command. */ |
| 66 | |
| 67 | static value all_values; |
| 68 | |
| 69 | /* Allocate a value that has the correct length for type TYPE. */ |
| 70 | |
| 71 | value |
| 72 | allocate_value (type) |
| 73 | struct type *type; |
| 74 | { |
| 75 | register value val; |
| 76 | |
| 77 | check_stub_type (type); |
| 78 | |
| 79 | val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type)); |
| 80 | VALUE_NEXT (val) = all_values; |
| 81 | all_values = val; |
| 82 | VALUE_TYPE (val) = type; |
| 83 | VALUE_LVAL (val) = not_lval; |
| 84 | VALUE_ADDRESS (val) = 0; |
| 85 | VALUE_FRAME (val) = 0; |
| 86 | VALUE_OFFSET (val) = 0; |
| 87 | VALUE_BITPOS (val) = 0; |
| 88 | VALUE_BITSIZE (val) = 0; |
| 89 | VALUE_REPEATED (val) = 0; |
| 90 | VALUE_REPETITIONS (val) = 0; |
| 91 | VALUE_REGNO (val) = -1; |
| 92 | VALUE_LAZY (val) = 0; |
| 93 | VALUE_OPTIMIZED_OUT (val) = 0; |
| 94 | return val; |
| 95 | } |
| 96 | |
| 97 | /* Allocate a value that has the correct length |
| 98 | for COUNT repetitions type TYPE. */ |
| 99 | |
| 100 | value |
| 101 | allocate_repeat_value (type, count) |
| 102 | struct type *type; |
| 103 | int count; |
| 104 | { |
| 105 | register value val; |
| 106 | |
| 107 | val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count); |
| 108 | VALUE_NEXT (val) = all_values; |
| 109 | all_values = val; |
| 110 | VALUE_TYPE (val) = type; |
| 111 | VALUE_LVAL (val) = not_lval; |
| 112 | VALUE_ADDRESS (val) = 0; |
| 113 | VALUE_FRAME (val) = 0; |
| 114 | VALUE_OFFSET (val) = 0; |
| 115 | VALUE_BITPOS (val) = 0; |
| 116 | VALUE_BITSIZE (val) = 0; |
| 117 | VALUE_REPEATED (val) = 1; |
| 118 | VALUE_REPETITIONS (val) = count; |
| 119 | VALUE_REGNO (val) = -1; |
| 120 | VALUE_LAZY (val) = 0; |
| 121 | VALUE_OPTIMIZED_OUT (val) = 0; |
| 122 | return val; |
| 123 | } |
| 124 | |
| 125 | /* Return a mark in the value chain. All values allocated after the |
| 126 | mark is obtained (except for those released) are subject to being freed |
| 127 | if a subsequent value_free_to_mark is passed the mark. */ |
| 128 | value |
| 129 | value_mark () |
| 130 | { |
| 131 | return all_values; |
| 132 | } |
| 133 | |
| 134 | /* Free all values allocated since MARK was obtained by value_mark |
| 135 | (except for those released). */ |
| 136 | void |
| 137 | value_free_to_mark (mark) |
| 138 | value mark; |
| 139 | { |
| 140 | value val, next; |
| 141 | |
| 142 | for (val = all_values; val && val != mark; val = next) |
| 143 | { |
| 144 | next = VALUE_NEXT (val); |
| 145 | value_free (val); |
| 146 | } |
| 147 | all_values = val; |
| 148 | } |
| 149 | |
| 150 | /* Free all the values that have been allocated (except for those released). |
| 151 | Called after each command, successful or not. */ |
| 152 | |
| 153 | void |
| 154 | free_all_values () |
| 155 | { |
| 156 | register value val, next; |
| 157 | |
| 158 | for (val = all_values; val; val = next) |
| 159 | { |
| 160 | next = VALUE_NEXT (val); |
| 161 | value_free (val); |
| 162 | } |
| 163 | |
| 164 | all_values = 0; |
| 165 | } |
| 166 | |
| 167 | /* Remove VAL from the chain all_values |
| 168 | so it will not be freed automatically. */ |
| 169 | |
| 170 | void |
| 171 | release_value (val) |
| 172 | register value val; |
| 173 | { |
| 174 | register value v; |
| 175 | |
| 176 | if (all_values == val) |
| 177 | { |
| 178 | all_values = val->next; |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | for (v = all_values; v; v = v->next) |
| 183 | { |
| 184 | if (v->next == val) |
| 185 | { |
| 186 | v->next = val->next; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /* Return a copy of the value ARG. |
| 193 | It contains the same contents, for same memory address, |
| 194 | but it's a different block of storage. */ |
| 195 | |
| 196 | value |
| 197 | value_copy (arg) |
| 198 | value arg; |
| 199 | { |
| 200 | register value val; |
| 201 | register struct type *type = VALUE_TYPE (arg); |
| 202 | if (VALUE_REPEATED (arg)) |
| 203 | val = allocate_repeat_value (type, VALUE_REPETITIONS (arg)); |
| 204 | else |
| 205 | val = allocate_value (type); |
| 206 | VALUE_LVAL (val) = VALUE_LVAL (arg); |
| 207 | VALUE_ADDRESS (val) = VALUE_ADDRESS (arg); |
| 208 | VALUE_OFFSET (val) = VALUE_OFFSET (arg); |
| 209 | VALUE_BITPOS (val) = VALUE_BITPOS (arg); |
| 210 | VALUE_BITSIZE (val) = VALUE_BITSIZE (arg); |
| 211 | VALUE_REGNO (val) = VALUE_REGNO (arg); |
| 212 | VALUE_LAZY (val) = VALUE_LAZY (arg); |
| 213 | if (!VALUE_LAZY (val)) |
| 214 | { |
| 215 | memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS_RAW (arg), |
| 216 | TYPE_LENGTH (VALUE_TYPE (arg)) |
| 217 | * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1)); |
| 218 | } |
| 219 | return val; |
| 220 | } |
| 221 | \f |
| 222 | /* Access to the value history. */ |
| 223 | |
| 224 | /* Record a new value in the value history. |
| 225 | Returns the absolute history index of the entry. |
| 226 | Result of -1 indicates the value was not saved; otherwise it is the |
| 227 | value history index of this new item. */ |
| 228 | |
| 229 | int |
| 230 | record_latest_value (val) |
| 231 | value val; |
| 232 | { |
| 233 | int i; |
| 234 | |
| 235 | /* Check error now if about to store an invalid float. We return -1 |
| 236 | to the caller, but allow them to continue, e.g. to print it as "Nan". */ |
| 237 | if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) |
| 238 | { |
| 239 | unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i); |
| 240 | if (i) return -1; /* Indicate value not saved in history */ |
| 241 | } |
| 242 | |
| 243 | /* Here we treat value_history_count as origin-zero |
| 244 | and applying to the value being stored now. */ |
| 245 | |
| 246 | i = value_history_count % VALUE_HISTORY_CHUNK; |
| 247 | if (i == 0) |
| 248 | { |
| 249 | register struct value_history_chunk *new |
| 250 | = (struct value_history_chunk *) |
| 251 | xmalloc (sizeof (struct value_history_chunk)); |
| 252 | memset (new->values, 0, sizeof new->values); |
| 253 | new->next = value_history_chain; |
| 254 | value_history_chain = new; |
| 255 | } |
| 256 | |
| 257 | value_history_chain->values[i] = val; |
| 258 | |
| 259 | /* We don't want this value to have anything to do with the inferior anymore. |
| 260 | In particular, "set $1 = 50" should not affect the variable from which |
| 261 | the value was taken, and fast watchpoints should be able to assume that |
| 262 | a value on the value history never changes. */ |
| 263 | if (VALUE_LAZY (val)) |
| 264 | value_fetch_lazy (val); |
| 265 | VALUE_LVAL (val) = not_lval; |
| 266 | release_value (val); |
| 267 | |
| 268 | /* Now we regard value_history_count as origin-one |
| 269 | and applying to the value just stored. */ |
| 270 | |
| 271 | return ++value_history_count; |
| 272 | } |
| 273 | |
| 274 | /* Return a copy of the value in the history with sequence number NUM. */ |
| 275 | |
| 276 | value |
| 277 | access_value_history (num) |
| 278 | int num; |
| 279 | { |
| 280 | register struct value_history_chunk *chunk; |
| 281 | register int i; |
| 282 | register int absnum = num; |
| 283 | |
| 284 | if (absnum <= 0) |
| 285 | absnum += value_history_count; |
| 286 | |
| 287 | if (absnum <= 0) |
| 288 | { |
| 289 | if (num == 0) |
| 290 | error ("The history is empty."); |
| 291 | else if (num == 1) |
| 292 | error ("There is only one value in the history."); |
| 293 | else |
| 294 | error ("History does not go back to $$%d.", -num); |
| 295 | } |
| 296 | if (absnum > value_history_count) |
| 297 | error ("History has not yet reached $%d.", absnum); |
| 298 | |
| 299 | absnum--; |
| 300 | |
| 301 | /* Now absnum is always absolute and origin zero. */ |
| 302 | |
| 303 | chunk = value_history_chain; |
| 304 | for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK; |
| 305 | i > 0; i--) |
| 306 | chunk = chunk->next; |
| 307 | |
| 308 | return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]); |
| 309 | } |
| 310 | |
| 311 | /* Clear the value history entirely. |
| 312 | Must be done when new symbol tables are loaded, |
| 313 | because the type pointers become invalid. */ |
| 314 | |
| 315 | void |
| 316 | clear_value_history () |
| 317 | { |
| 318 | register struct value_history_chunk *next; |
| 319 | register int i; |
| 320 | register value val; |
| 321 | |
| 322 | while (value_history_chain) |
| 323 | { |
| 324 | for (i = 0; i < VALUE_HISTORY_CHUNK; i++) |
| 325 | if ((val = value_history_chain->values[i]) != NULL) |
| 326 | free ((PTR)val); |
| 327 | next = value_history_chain->next; |
| 328 | free ((PTR)value_history_chain); |
| 329 | value_history_chain = next; |
| 330 | } |
| 331 | value_history_count = 0; |
| 332 | } |
| 333 | |
| 334 | static void |
| 335 | show_values (num_exp, from_tty) |
| 336 | char *num_exp; |
| 337 | int from_tty; |
| 338 | { |
| 339 | register int i; |
| 340 | register value val; |
| 341 | static int num = 1; |
| 342 | |
| 343 | if (num_exp) |
| 344 | { |
| 345 | /* "info history +" should print from the stored position. |
| 346 | "info history <exp>" should print around value number <exp>. */ |
| 347 | if (num_exp[0] != '+' || num_exp[1] != '\0') |
| 348 | num = parse_and_eval_address (num_exp) - 5; |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | /* "info history" means print the last 10 values. */ |
| 353 | num = value_history_count - 9; |
| 354 | } |
| 355 | |
| 356 | if (num <= 0) |
| 357 | num = 1; |
| 358 | |
| 359 | for (i = num; i < num + 10 && i <= value_history_count; i++) |
| 360 | { |
| 361 | val = access_value_history (i); |
| 362 | printf_filtered ("$%d = ", i); |
| 363 | value_print (val, gdb_stdout, 0, Val_pretty_default); |
| 364 | printf_filtered ("\n"); |
| 365 | } |
| 366 | |
| 367 | /* The next "info history +" should start after what we just printed. */ |
| 368 | num += 10; |
| 369 | |
| 370 | /* Hitting just return after this command should do the same thing as |
| 371 | "info history +". If num_exp is null, this is unnecessary, since |
| 372 | "info history +" is not useful after "info history". */ |
| 373 | if (from_tty && num_exp) |
| 374 | { |
| 375 | num_exp[0] = '+'; |
| 376 | num_exp[1] = '\0'; |
| 377 | } |
| 378 | } |
| 379 | \f |
| 380 | /* Internal variables. These are variables within the debugger |
| 381 | that hold values assigned by debugger commands. |
| 382 | The user refers to them with a '$' prefix |
| 383 | that does not appear in the variable names stored internally. */ |
| 384 | |
| 385 | static struct internalvar *internalvars; |
| 386 | |
| 387 | /* Look up an internal variable with name NAME. NAME should not |
| 388 | normally include a dollar sign. |
| 389 | |
| 390 | If the specified internal variable does not exist, |
| 391 | one is created, with a void value. */ |
| 392 | |
| 393 | struct internalvar * |
| 394 | lookup_internalvar (name) |
| 395 | char *name; |
| 396 | { |
| 397 | register struct internalvar *var; |
| 398 | |
| 399 | for (var = internalvars; var; var = var->next) |
| 400 | if (STREQ (var->name, name)) |
| 401 | return var; |
| 402 | |
| 403 | var = (struct internalvar *) xmalloc (sizeof (struct internalvar)); |
| 404 | var->name = concat (name, NULL); |
| 405 | var->value = allocate_value (builtin_type_void); |
| 406 | release_value (var->value); |
| 407 | var->next = internalvars; |
| 408 | internalvars = var; |
| 409 | return var; |
| 410 | } |
| 411 | |
| 412 | value |
| 413 | value_of_internalvar (var) |
| 414 | struct internalvar *var; |
| 415 | { |
| 416 | register value val; |
| 417 | |
| 418 | #ifdef IS_TRAPPED_INTERNALVAR |
| 419 | if (IS_TRAPPED_INTERNALVAR (var->name)) |
| 420 | return VALUE_OF_TRAPPED_INTERNALVAR (var); |
| 421 | #endif |
| 422 | |
| 423 | val = value_copy (var->value); |
| 424 | if (VALUE_LAZY (val)) |
| 425 | value_fetch_lazy (val); |
| 426 | VALUE_LVAL (val) = lval_internalvar; |
| 427 | VALUE_INTERNALVAR (val) = var; |
| 428 | return val; |
| 429 | } |
| 430 | |
| 431 | void |
| 432 | set_internalvar_component (var, offset, bitpos, bitsize, newval) |
| 433 | struct internalvar *var; |
| 434 | int offset, bitpos, bitsize; |
| 435 | value newval; |
| 436 | { |
| 437 | register char *addr = VALUE_CONTENTS (var->value) + offset; |
| 438 | |
| 439 | #ifdef IS_TRAPPED_INTERNALVAR |
| 440 | if (IS_TRAPPED_INTERNALVAR (var->name)) |
| 441 | SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset); |
| 442 | #endif |
| 443 | |
| 444 | if (bitsize) |
| 445 | modify_field (addr, value_as_long (newval), |
| 446 | bitpos, bitsize); |
| 447 | else |
| 448 | memcpy (addr, VALUE_CONTENTS (newval), TYPE_LENGTH (VALUE_TYPE (newval))); |
| 449 | } |
| 450 | |
| 451 | void |
| 452 | set_internalvar (var, val) |
| 453 | struct internalvar *var; |
| 454 | value val; |
| 455 | { |
| 456 | #ifdef IS_TRAPPED_INTERNALVAR |
| 457 | if (IS_TRAPPED_INTERNALVAR (var->name)) |
| 458 | SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0); |
| 459 | #endif |
| 460 | |
| 461 | free ((PTR)var->value); |
| 462 | var->value = value_copy (val); |
| 463 | /* Force the value to be fetched from the target now, to avoid problems |
| 464 | later when this internalvar is referenced and the target is gone or |
| 465 | has changed. */ |
| 466 | if (VALUE_LAZY (var->value)) |
| 467 | value_fetch_lazy (var->value); |
| 468 | release_value (var->value); |
| 469 | } |
| 470 | |
| 471 | char * |
| 472 | internalvar_name (var) |
| 473 | struct internalvar *var; |
| 474 | { |
| 475 | return var->name; |
| 476 | } |
| 477 | |
| 478 | /* Free all internalvars. Done when new symtabs are loaded, |
| 479 | because that makes the values invalid. */ |
| 480 | |
| 481 | void |
| 482 | clear_internalvars () |
| 483 | { |
| 484 | register struct internalvar *var; |
| 485 | |
| 486 | while (internalvars) |
| 487 | { |
| 488 | var = internalvars; |
| 489 | internalvars = var->next; |
| 490 | free ((PTR)var->name); |
| 491 | free ((PTR)var->value); |
| 492 | free ((PTR)var); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | static void |
| 497 | show_convenience (ignore, from_tty) |
| 498 | char *ignore; |
| 499 | int from_tty; |
| 500 | { |
| 501 | register struct internalvar *var; |
| 502 | int varseen = 0; |
| 503 | |
| 504 | for (var = internalvars; var; var = var->next) |
| 505 | { |
| 506 | #ifdef IS_TRAPPED_INTERNALVAR |
| 507 | if (IS_TRAPPED_INTERNALVAR (var->name)) |
| 508 | continue; |
| 509 | #endif |
| 510 | if (!varseen) |
| 511 | { |
| 512 | varseen = 1; |
| 513 | } |
| 514 | printf_filtered ("$%s = ", var->name); |
| 515 | value_print (var->value, gdb_stdout, 0, Val_pretty_default); |
| 516 | printf_filtered ("\n"); |
| 517 | } |
| 518 | if (!varseen) |
| 519 | printf_unfiltered ("No debugger convenience variables now defined.\n\ |
| 520 | Convenience variables have names starting with \"$\";\n\ |
| 521 | use \"set\" as in \"set $foo = 5\" to define them.\n"); |
| 522 | } |
| 523 | \f |
| 524 | /* Extract a value as a C number (either long or double). |
| 525 | Knows how to convert fixed values to double, or |
| 526 | floating values to long. |
| 527 | Does not deallocate the value. */ |
| 528 | |
| 529 | LONGEST |
| 530 | value_as_long (val) |
| 531 | register value val; |
| 532 | { |
| 533 | /* This coerces arrays and functions, which is necessary (e.g. |
| 534 | in disassemble_command). It also dereferences references, which |
| 535 | I suspect is the most logical thing to do. */ |
| 536 | if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM) |
| 537 | COERCE_ARRAY (val); |
| 538 | return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val)); |
| 539 | } |
| 540 | |
| 541 | double |
| 542 | value_as_double (val) |
| 543 | register value val; |
| 544 | { |
| 545 | double foo; |
| 546 | int inv; |
| 547 | |
| 548 | foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv); |
| 549 | if (inv) |
| 550 | error ("Invalid floating value found in program."); |
| 551 | return foo; |
| 552 | } |
| 553 | /* Extract a value as a C pointer. |
| 554 | Does not deallocate the value. */ |
| 555 | CORE_ADDR |
| 556 | value_as_pointer (val) |
| 557 | value val; |
| 558 | { |
| 559 | /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure |
| 560 | whether we want this to be true eventually. */ |
| 561 | #if 0 |
| 562 | /* ADDR_BITS_REMOVE is wrong if we are being called for a |
| 563 | non-address (e.g. argument to "signal", "info break", etc.), or |
| 564 | for pointers to char, in which the low bits *are* significant. */ |
| 565 | return ADDR_BITS_REMOVE(value_as_long (val)); |
| 566 | #else |
| 567 | return value_as_long (val); |
| 568 | #endif |
| 569 | } |
| 570 | \f |
| 571 | /* Unpack raw data (copied from debugee, target byte order) at VALADDR |
| 572 | as a long, or as a double, assuming the raw data is described |
| 573 | by type TYPE. Knows how to convert different sizes of values |
| 574 | and can convert between fixed and floating point. We don't assume |
| 575 | any alignment for the raw data. Return value is in host byte order. |
| 576 | |
| 577 | If you want functions and arrays to be coerced to pointers, and |
| 578 | references to be dereferenced, call value_as_long() instead. |
| 579 | |
| 580 | C++: It is assumed that the front-end has taken care of |
| 581 | all matters concerning pointers to members. A pointer |
| 582 | to member which reaches here is considered to be equivalent |
| 583 | to an INT (or some size). After all, it is only an offset. */ |
| 584 | |
| 585 | /* FIXME: This should be rewritten as a switch statement for speed and |
| 586 | ease of comprehension. */ |
| 587 | |
| 588 | LONGEST |
| 589 | unpack_long (type, valaddr) |
| 590 | struct type *type; |
| 591 | char *valaddr; |
| 592 | { |
| 593 | register enum type_code code = TYPE_CODE (type); |
| 594 | register int len = TYPE_LENGTH (type); |
| 595 | register int nosign = TYPE_UNSIGNED (type); |
| 596 | |
| 597 | switch (code) |
| 598 | { |
| 599 | case TYPE_CODE_ENUM: |
| 600 | case TYPE_CODE_BOOL: |
| 601 | case TYPE_CODE_INT: |
| 602 | case TYPE_CODE_CHAR: |
| 603 | if (nosign) |
| 604 | return extract_unsigned_integer (valaddr, len); |
| 605 | else |
| 606 | return extract_signed_integer (valaddr, len); |
| 607 | |
| 608 | case TYPE_CODE_FLT: |
| 609 | return extract_floating (valaddr, len); |
| 610 | |
| 611 | case TYPE_CODE_PTR: |
| 612 | case TYPE_CODE_REF: |
| 613 | /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure |
| 614 | whether we want this to be true eventually. */ |
| 615 | return extract_address (valaddr, len); |
| 616 | |
| 617 | case TYPE_CODE_MEMBER: |
| 618 | error ("not implemented: member types in unpack_long"); |
| 619 | |
| 620 | default: |
| 621 | error ("Value can't be converted to integer."); |
| 622 | } |
| 623 | return 0; /* Placate lint. */ |
| 624 | } |
| 625 | |
| 626 | /* Return a double value from the specified type and address. |
| 627 | INVP points to an int which is set to 0 for valid value, |
| 628 | 1 for invalid value (bad float format). In either case, |
| 629 | the returned double is OK to use. Argument is in target |
| 630 | format, result is in host format. */ |
| 631 | |
| 632 | double |
| 633 | unpack_double (type, valaddr, invp) |
| 634 | struct type *type; |
| 635 | char *valaddr; |
| 636 | int *invp; |
| 637 | { |
| 638 | register enum type_code code = TYPE_CODE (type); |
| 639 | register int len = TYPE_LENGTH (type); |
| 640 | register int nosign = TYPE_UNSIGNED (type); |
| 641 | |
| 642 | *invp = 0; /* Assume valid. */ |
| 643 | if (code == TYPE_CODE_FLT) |
| 644 | { |
| 645 | if (INVALID_FLOAT (valaddr, len)) |
| 646 | { |
| 647 | *invp = 1; |
| 648 | return 1.234567891011121314; |
| 649 | } |
| 650 | return extract_floating (valaddr, len); |
| 651 | } |
| 652 | else if (nosign) |
| 653 | { |
| 654 | /* Unsigned -- be sure we compensate for signed LONGEST. */ |
| 655 | return (unsigned LONGEST) unpack_long (type, valaddr); |
| 656 | } |
| 657 | else |
| 658 | { |
| 659 | /* Signed -- we are OK with unpack_long. */ |
| 660 | return unpack_long (type, valaddr); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /* Unpack raw data (copied from debugee, target byte order) at VALADDR |
| 665 | as a CORE_ADDR, assuming the raw data is described by type TYPE. |
| 666 | We don't assume any alignment for the raw data. Return value is in |
| 667 | host byte order. |
| 668 | |
| 669 | If you want functions and arrays to be coerced to pointers, and |
| 670 | references to be dereferenced, call value_as_pointer() instead. |
| 671 | |
| 672 | C++: It is assumed that the front-end has taken care of |
| 673 | all matters concerning pointers to members. A pointer |
| 674 | to member which reaches here is considered to be equivalent |
| 675 | to an INT (or some size). After all, it is only an offset. */ |
| 676 | |
| 677 | CORE_ADDR |
| 678 | unpack_pointer (type, valaddr) |
| 679 | struct type *type; |
| 680 | char *valaddr; |
| 681 | { |
| 682 | /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure |
| 683 | whether we want this to be true eventually. */ |
| 684 | return unpack_long (type, valaddr); |
| 685 | } |
| 686 | \f |
| 687 | /* Given a value ARG1 (offset by OFFSET bytes) |
| 688 | of a struct or union type ARG_TYPE, |
| 689 | extract and return the value of one of its fields. |
| 690 | FIELDNO says which field. |
| 691 | |
| 692 | For C++, must also be able to return values from static fields */ |
| 693 | |
| 694 | value |
| 695 | value_primitive_field (arg1, offset, fieldno, arg_type) |
| 696 | register value arg1; |
| 697 | int offset; |
| 698 | register int fieldno; |
| 699 | register struct type *arg_type; |
| 700 | { |
| 701 | register value v; |
| 702 | register struct type *type; |
| 703 | |
| 704 | check_stub_type (arg_type); |
| 705 | type = TYPE_FIELD_TYPE (arg_type, fieldno); |
| 706 | |
| 707 | /* Handle packed fields */ |
| 708 | |
| 709 | offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8; |
| 710 | if (TYPE_FIELD_BITSIZE (arg_type, fieldno)) |
| 711 | { |
| 712 | v = value_from_longest (type, |
| 713 | unpack_field_as_long (arg_type, |
| 714 | VALUE_CONTENTS (arg1), |
| 715 | fieldno)); |
| 716 | VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8; |
| 717 | VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno); |
| 718 | } |
| 719 | else |
| 720 | { |
| 721 | v = allocate_value (type); |
| 722 | if (VALUE_LAZY (arg1)) |
| 723 | VALUE_LAZY (v) = 1; |
| 724 | else |
| 725 | memcpy (VALUE_CONTENTS_RAW (v), VALUE_CONTENTS_RAW (arg1) + offset, |
| 726 | TYPE_LENGTH (type)); |
| 727 | } |
| 728 | VALUE_LVAL (v) = VALUE_LVAL (arg1); |
| 729 | if (VALUE_LVAL (arg1) == lval_internalvar) |
| 730 | VALUE_LVAL (v) = lval_internalvar_component; |
| 731 | VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1); |
| 732 | VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1); |
| 733 | return v; |
| 734 | } |
| 735 | |
| 736 | /* Given a value ARG1 of a struct or union type, |
| 737 | extract and return the value of one of its fields. |
| 738 | FIELDNO says which field. |
| 739 | |
| 740 | For C++, must also be able to return values from static fields */ |
| 741 | |
| 742 | value |
| 743 | value_field (arg1, fieldno) |
| 744 | register value arg1; |
| 745 | register int fieldno; |
| 746 | { |
| 747 | return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1)); |
| 748 | } |
| 749 | |
| 750 | /* Return a non-virtual function as a value. |
| 751 | F is the list of member functions which contains the desired method. |
| 752 | J is an index into F which provides the desired method. */ |
| 753 | |
| 754 | value |
| 755 | value_fn_field (arg1p, f, j, type, offset) |
| 756 | value *arg1p; |
| 757 | struct fn_field *f; |
| 758 | int j; |
| 759 | struct type *type; |
| 760 | int offset; |
| 761 | { |
| 762 | register value v; |
| 763 | register struct type *ftype = TYPE_FN_FIELD_TYPE (f, j); |
| 764 | struct symbol *sym; |
| 765 | |
| 766 | sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j), |
| 767 | 0, VAR_NAMESPACE, 0, NULL); |
| 768 | if (! sym) |
| 769 | return (value)NULL; |
| 770 | /* |
| 771 | error ("Internal error: could not find physical method named %s", |
| 772 | TYPE_FN_FIELD_PHYSNAME (f, j)); |
| 773 | */ |
| 774 | |
| 775 | v = allocate_value (ftype); |
| 776 | VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)); |
| 777 | VALUE_TYPE (v) = ftype; |
| 778 | |
| 779 | if (arg1p) |
| 780 | { |
| 781 | if (type != VALUE_TYPE (*arg1p)) |
| 782 | *arg1p = value_ind (value_cast (lookup_pointer_type (type), |
| 783 | value_addr (*arg1p))); |
| 784 | |
| 785 | /* Move the `this' pointer according to the offset. |
| 786 | VALUE_OFFSET (*arg1p) += offset; |
| 787 | */ |
| 788 | } |
| 789 | |
| 790 | return v; |
| 791 | } |
| 792 | |
| 793 | /* Return a virtual function as a value. |
| 794 | ARG1 is the object which provides the virtual function |
| 795 | table pointer. *ARG1P is side-effected in calling this function. |
| 796 | F is the list of member functions which contains the desired virtual |
| 797 | function. |
| 798 | J is an index into F which provides the desired virtual function. |
| 799 | |
| 800 | TYPE is the type in which F is located. */ |
| 801 | value |
| 802 | value_virtual_fn_field (arg1p, f, j, type, offset) |
| 803 | value *arg1p; |
| 804 | struct fn_field *f; |
| 805 | int j; |
| 806 | struct type *type; |
| 807 | int offset; |
| 808 | { |
| 809 | value arg1 = *arg1p; |
| 810 | /* First, get the virtual function table pointer. That comes |
| 811 | with a strange type, so cast it to type `pointer to long' (which |
| 812 | should serve just fine as a function type). Then, index into |
| 813 | the table, and convert final value to appropriate function type. */ |
| 814 | value entry, vfn, vtbl; |
| 815 | value vi = value_from_longest (builtin_type_int, |
| 816 | (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j)); |
| 817 | struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j); |
| 818 | struct type *context; |
| 819 | if (fcontext == NULL) |
| 820 | /* We don't have an fcontext (e.g. the program was compiled with |
| 821 | g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE. |
| 822 | This won't work right for multiple inheritance, but at least we |
| 823 | should do as well as GDB 3.x did. */ |
| 824 | fcontext = TYPE_VPTR_BASETYPE (type); |
| 825 | context = lookup_pointer_type (fcontext); |
| 826 | /* Now context is a pointer to the basetype containing the vtbl. */ |
| 827 | if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1)) |
| 828 | arg1 = value_ind (value_cast (context, value_addr (arg1))); |
| 829 | |
| 830 | context = VALUE_TYPE (arg1); |
| 831 | /* Now context is the basetype containing the vtbl. */ |
| 832 | |
| 833 | /* This type may have been defined before its virtual function table |
| 834 | was. If so, fill in the virtual function table entry for the |
| 835 | type now. */ |
| 836 | if (TYPE_VPTR_FIELDNO (context) < 0) |
| 837 | fill_in_vptr_fieldno (context); |
| 838 | |
| 839 | /* The virtual function table is now an array of structures |
| 840 | which have the form { int16 offset, delta; void *pfn; }. */ |
| 841 | vtbl = value_ind (value_primitive_field (arg1, 0, |
| 842 | TYPE_VPTR_FIELDNO (context), |
| 843 | TYPE_VPTR_BASETYPE (context))); |
| 844 | |
| 845 | /* Index into the virtual function table. This is hard-coded because |
| 846 | looking up a field is not cheap, and it may be important to save |
| 847 | time, e.g. if the user has set a conditional breakpoint calling |
| 848 | a virtual function. */ |
| 849 | entry = value_subscript (vtbl, vi); |
| 850 | |
| 851 | /* Move the `this' pointer according to the virtual function table. */ |
| 852 | VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0))/* + offset*/; |
| 853 | |
| 854 | if (! VALUE_LAZY (arg1)) |
| 855 | { |
| 856 | VALUE_LAZY (arg1) = 1; |
| 857 | value_fetch_lazy (arg1); |
| 858 | } |
| 859 | |
| 860 | vfn = value_field (entry, 2); |
| 861 | /* Reinstantiate the function pointer with the correct type. */ |
| 862 | VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)); |
| 863 | |
| 864 | *arg1p = arg1; |
| 865 | return vfn; |
| 866 | } |
| 867 | |
| 868 | /* ARG is a pointer to an object we know to be at least |
| 869 | a DTYPE. BTYPE is the most derived basetype that has |
| 870 | already been searched (and need not be searched again). |
| 871 | After looking at the vtables between BTYPE and DTYPE, |
| 872 | return the most derived type we find. The caller must |
| 873 | be satisfied when the return value == DTYPE. |
| 874 | |
| 875 | FIXME-tiemann: should work with dossier entries as well. */ |
| 876 | |
| 877 | static value |
| 878 | value_headof (in_arg, btype, dtype) |
| 879 | value in_arg; |
| 880 | struct type *btype, *dtype; |
| 881 | { |
| 882 | /* First collect the vtables we must look at for this object. */ |
| 883 | /* FIXME-tiemann: right now, just look at top-most vtable. */ |
| 884 | value arg, vtbl, entry, best_entry = 0; |
| 885 | int i, nelems; |
| 886 | int offset, best_offset = 0; |
| 887 | struct symbol *sym; |
| 888 | CORE_ADDR pc_for_sym; |
| 889 | char *demangled_name; |
| 890 | struct minimal_symbol *msymbol; |
| 891 | |
| 892 | btype = TYPE_VPTR_BASETYPE (dtype); |
| 893 | check_stub_type (btype); |
| 894 | arg = in_arg; |
| 895 | if (btype != dtype) |
| 896 | arg = value_cast (lookup_pointer_type (btype), arg); |
| 897 | vtbl = value_ind (value_field (value_ind (arg), TYPE_VPTR_FIELDNO (btype))); |
| 898 | |
| 899 | /* Check that VTBL looks like it points to a virtual function table. */ |
| 900 | msymbol = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl)); |
| 901 | if (msymbol == NULL |
| 902 | || !VTBL_PREFIX_P (demangled_name = SYMBOL_NAME (msymbol))) |
| 903 | { |
| 904 | /* If we expected to find a vtable, but did not, let the user |
| 905 | know that we aren't happy, but don't throw an error. |
| 906 | FIXME: there has to be a better way to do this. */ |
| 907 | struct type *error_type = (struct type *)xmalloc (sizeof (struct type)); |
| 908 | memcpy (error_type, VALUE_TYPE (in_arg), sizeof (struct type)); |
| 909 | TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *")); |
| 910 | VALUE_TYPE (in_arg) = error_type; |
| 911 | return in_arg; |
| 912 | } |
| 913 | |
| 914 | /* Now search through the virtual function table. */ |
| 915 | entry = value_ind (vtbl); |
| 916 | nelems = longest_to_int (value_as_long (value_field (entry, 2))); |
| 917 | for (i = 1; i <= nelems; i++) |
| 918 | { |
| 919 | entry = value_subscript (vtbl, value_from_longest (builtin_type_int, |
| 920 | (LONGEST) i)); |
| 921 | offset = longest_to_int (value_as_long (value_field (entry, 0))); |
| 922 | /* If we use '<=' we can handle single inheritance |
| 923 | * where all offsets are zero - just use the first entry found. */ |
| 924 | if (offset <= best_offset) |
| 925 | { |
| 926 | best_offset = offset; |
| 927 | best_entry = entry; |
| 928 | } |
| 929 | } |
| 930 | /* Move the pointer according to BEST_ENTRY's offset, and figure |
| 931 | out what type we should return as the new pointer. */ |
| 932 | if (best_entry == 0) |
| 933 | { |
| 934 | /* An alternative method (which should no longer be necessary). |
| 935 | * But we leave it in for future use, when we will hopefully |
| 936 | * have optimizes the vtable to use thunks instead of offsets. */ |
| 937 | /* Use the name of vtable itself to extract a base type. */ |
| 938 | demangled_name += 4; /* Skip _vt$ prefix. */ |
| 939 | } |
| 940 | else |
| 941 | { |
| 942 | pc_for_sym = value_as_pointer (value_field (best_entry, 2)); |
| 943 | sym = find_pc_function (pc_for_sym); |
| 944 | demangled_name = cplus_demangle (SYMBOL_NAME (sym), DMGL_ANSI); |
| 945 | *(strchr (demangled_name, ':')) = '\0'; |
| 946 | } |
| 947 | sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0); |
| 948 | if (sym == NULL) |
| 949 | error ("could not find type declaration for `%s'", demangled_name); |
| 950 | if (best_entry) |
| 951 | { |
| 952 | free (demangled_name); |
| 953 | arg = value_add (value_cast (builtin_type_int, arg), |
| 954 | value_field (best_entry, 0)); |
| 955 | } |
| 956 | else arg = in_arg; |
| 957 | VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym)); |
| 958 | return arg; |
| 959 | } |
| 960 | |
| 961 | /* ARG is a pointer object of type TYPE. If TYPE has virtual |
| 962 | function tables, probe ARG's tables (including the vtables |
| 963 | of its baseclasses) to figure out the most derived type that ARG |
| 964 | could actually be a pointer to. */ |
| 965 | |
| 966 | value |
| 967 | value_from_vtable_info (arg, type) |
| 968 | value arg; |
| 969 | struct type *type; |
| 970 | { |
| 971 | /* Take care of preliminaries. */ |
| 972 | if (TYPE_VPTR_FIELDNO (type) < 0) |
| 973 | fill_in_vptr_fieldno (type); |
| 974 | if (TYPE_VPTR_FIELDNO (type) < 0 || VALUE_REPEATED (arg)) |
| 975 | return 0; |
| 976 | |
| 977 | return value_headof (arg, 0, type); |
| 978 | } |
| 979 | |
| 980 | /* Return true if the INDEXth field of TYPE is a virtual baseclass |
| 981 | pointer which is for the base class whose type is BASECLASS. */ |
| 982 | |
| 983 | static int |
| 984 | vb_match (type, index, basetype) |
| 985 | struct type *type; |
| 986 | int index; |
| 987 | struct type *basetype; |
| 988 | { |
| 989 | struct type *fieldtype; |
| 990 | char *name = TYPE_FIELD_NAME (type, index); |
| 991 | char *field_class_name = NULL; |
| 992 | |
| 993 | if (*name != '_') |
| 994 | return 0; |
| 995 | /* gcc 2.4 uses _vb$. */ |
| 996 | if (name[1] == 'v' && name[2] == 'b' && name[3] == CPLUS_MARKER) |
| 997 | field_class_name = name + 4; |
| 998 | /* gcc 2.5 will use __vb_. */ |
| 999 | if (name[1] == '_' && name[2] == 'v' && name[3] == 'b' && name[4] == '_') |
| 1000 | field_class_name = name + 5; |
| 1001 | |
| 1002 | if (field_class_name == NULL) |
| 1003 | /* This field is not a virtual base class pointer. */ |
| 1004 | return 0; |
| 1005 | |
| 1006 | /* It's a virtual baseclass pointer, now we just need to find out whether |
| 1007 | it is for this baseclass. */ |
| 1008 | fieldtype = TYPE_FIELD_TYPE (type, index); |
| 1009 | if (fieldtype == NULL |
| 1010 | || TYPE_CODE (fieldtype) != TYPE_CODE_PTR) |
| 1011 | /* "Can't happen". */ |
| 1012 | return 0; |
| 1013 | |
| 1014 | /* What we check for is that either the types are equal (needed for |
| 1015 | nameless types) or have the same name. This is ugly, and a more |
| 1016 | elegant solution should be devised (which would probably just push |
| 1017 | the ugliness into symbol reading unless we change the stabs format). */ |
| 1018 | if (TYPE_TARGET_TYPE (fieldtype) == basetype) |
| 1019 | return 1; |
| 1020 | |
| 1021 | if (TYPE_NAME (basetype) != NULL |
| 1022 | && TYPE_NAME (TYPE_TARGET_TYPE (fieldtype)) != NULL |
| 1023 | && STREQ (TYPE_NAME (basetype), |
| 1024 | TYPE_NAME (TYPE_TARGET_TYPE (fieldtype)))) |
| 1025 | return 1; |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | /* Compute the offset of the baseclass which is |
| 1030 | the INDEXth baseclass of class TYPE, for a value ARG, |
| 1031 | wih extra offset of OFFSET. |
| 1032 | The result is the offste of the baseclass value relative |
| 1033 | to (the address of)(ARG) + OFFSET. |
| 1034 | |
| 1035 | -1 is returned on error. */ |
| 1036 | |
| 1037 | int |
| 1038 | baseclass_offset (type, index, arg, offset) |
| 1039 | struct type *type; |
| 1040 | int index; |
| 1041 | value arg; |
| 1042 | int offset; |
| 1043 | { |
| 1044 | struct type *basetype = TYPE_BASECLASS (type, index); |
| 1045 | |
| 1046 | if (BASETYPE_VIA_VIRTUAL (type, index)) |
| 1047 | { |
| 1048 | /* Must hunt for the pointer to this virtual baseclass. */ |
| 1049 | register int i, len = TYPE_NFIELDS (type); |
| 1050 | register int n_baseclasses = TYPE_N_BASECLASSES (type); |
| 1051 | |
| 1052 | /* First look for the virtual baseclass pointer |
| 1053 | in the fields. */ |
| 1054 | for (i = n_baseclasses; i < len; i++) |
| 1055 | { |
| 1056 | if (vb_match (type, i, basetype)) |
| 1057 | { |
| 1058 | CORE_ADDR addr |
| 1059 | = unpack_pointer (TYPE_FIELD_TYPE (type, i), |
| 1060 | VALUE_CONTENTS (arg) + VALUE_OFFSET (arg) |
| 1061 | + offset |
| 1062 | + (TYPE_FIELD_BITPOS (type, i) / 8)); |
| 1063 | |
| 1064 | if (VALUE_LVAL (arg) != lval_memory) |
| 1065 | return -1; |
| 1066 | |
| 1067 | return addr - |
| 1068 | (LONGEST) (VALUE_ADDRESS (arg) + VALUE_OFFSET (arg) + offset); |
| 1069 | } |
| 1070 | } |
| 1071 | /* Not in the fields, so try looking through the baseclasses. */ |
| 1072 | for (i = index+1; i < n_baseclasses; i++) |
| 1073 | { |
| 1074 | int boffset = |
| 1075 | baseclass_offset (type, i, arg, offset); |
| 1076 | if (boffset) |
| 1077 | return boffset; |
| 1078 | } |
| 1079 | /* Not found. */ |
| 1080 | return -1; |
| 1081 | } |
| 1082 | |
| 1083 | /* Baseclass is easily computed. */ |
| 1084 | return TYPE_BASECLASS_BITPOS (type, index) / 8; |
| 1085 | } |
| 1086 | |
| 1087 | /* Compute the address of the baseclass which is |
| 1088 | the INDEXth baseclass of class TYPE. The TYPE base |
| 1089 | of the object is at VALADDR. |
| 1090 | |
| 1091 | If ERRP is non-NULL, set *ERRP to be the errno code of any error, |
| 1092 | or 0 if no error. In that case the return value is not the address |
| 1093 | of the baseclasss, but the address which could not be read |
| 1094 | successfully. */ |
| 1095 | |
| 1096 | /* FIXME Fix remaining uses of baseclass_addr to use baseclass_offset */ |
| 1097 | |
| 1098 | char * |
| 1099 | baseclass_addr (type, index, valaddr, valuep, errp) |
| 1100 | struct type *type; |
| 1101 | int index; |
| 1102 | char *valaddr; |
| 1103 | value *valuep; |
| 1104 | int *errp; |
| 1105 | { |
| 1106 | struct type *basetype = TYPE_BASECLASS (type, index); |
| 1107 | |
| 1108 | if (errp) |
| 1109 | *errp = 0; |
| 1110 | |
| 1111 | if (BASETYPE_VIA_VIRTUAL (type, index)) |
| 1112 | { |
| 1113 | /* Must hunt for the pointer to this virtual baseclass. */ |
| 1114 | register int i, len = TYPE_NFIELDS (type); |
| 1115 | register int n_baseclasses = TYPE_N_BASECLASSES (type); |
| 1116 | |
| 1117 | /* First look for the virtual baseclass pointer |
| 1118 | in the fields. */ |
| 1119 | for (i = n_baseclasses; i < len; i++) |
| 1120 | { |
| 1121 | if (vb_match (type, i, basetype)) |
| 1122 | { |
| 1123 | value val = allocate_value (basetype); |
| 1124 | CORE_ADDR addr; |
| 1125 | int status; |
| 1126 | |
| 1127 | addr |
| 1128 | = unpack_pointer (TYPE_FIELD_TYPE (type, i), |
| 1129 | valaddr + (TYPE_FIELD_BITPOS (type, i) / 8)); |
| 1130 | |
| 1131 | status = target_read_memory (addr, |
| 1132 | VALUE_CONTENTS_RAW (val), |
| 1133 | TYPE_LENGTH (basetype)); |
| 1134 | VALUE_LVAL (val) = lval_memory; |
| 1135 | VALUE_ADDRESS (val) = addr; |
| 1136 | |
| 1137 | if (status != 0) |
| 1138 | { |
| 1139 | if (valuep) |
| 1140 | *valuep = NULL; |
| 1141 | release_value (val); |
| 1142 | value_free (val); |
| 1143 | if (errp) |
| 1144 | *errp = status; |
| 1145 | return (char *)addr; |
| 1146 | } |
| 1147 | else |
| 1148 | { |
| 1149 | if (valuep) |
| 1150 | *valuep = val; |
| 1151 | return (char *) VALUE_CONTENTS (val); |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | /* Not in the fields, so try looking through the baseclasses. */ |
| 1156 | for (i = index+1; i < n_baseclasses; i++) |
| 1157 | { |
| 1158 | char *baddr; |
| 1159 | |
| 1160 | baddr = baseclass_addr (type, i, valaddr, valuep, errp); |
| 1161 | if (baddr) |
| 1162 | return baddr; |
| 1163 | } |
| 1164 | /* Not found. */ |
| 1165 | if (valuep) |
| 1166 | *valuep = 0; |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
| 1170 | /* Baseclass is easily computed. */ |
| 1171 | if (valuep) |
| 1172 | *valuep = 0; |
| 1173 | return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8; |
| 1174 | } |
| 1175 | \f |
| 1176 | /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at |
| 1177 | VALADDR. |
| 1178 | |
| 1179 | Extracting bits depends on endianness of the machine. Compute the |
| 1180 | number of least significant bits to discard. For big endian machines, |
| 1181 | we compute the total number of bits in the anonymous object, subtract |
| 1182 | off the bit count from the MSB of the object to the MSB of the |
| 1183 | bitfield, then the size of the bitfield, which leaves the LSB discard |
| 1184 | count. For little endian machines, the discard count is simply the |
| 1185 | number of bits from the LSB of the anonymous object to the LSB of the |
| 1186 | bitfield. |
| 1187 | |
| 1188 | If the field is signed, we also do sign extension. */ |
| 1189 | |
| 1190 | LONGEST |
| 1191 | unpack_field_as_long (type, valaddr, fieldno) |
| 1192 | struct type *type; |
| 1193 | char *valaddr; |
| 1194 | int fieldno; |
| 1195 | { |
| 1196 | unsigned LONGEST val; |
| 1197 | unsigned LONGEST valmask; |
| 1198 | int bitpos = TYPE_FIELD_BITPOS (type, fieldno); |
| 1199 | int bitsize = TYPE_FIELD_BITSIZE (type, fieldno); |
| 1200 | int lsbcount; |
| 1201 | |
| 1202 | val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val)); |
| 1203 | |
| 1204 | /* Extract bits. See comment above. */ |
| 1205 | |
| 1206 | #if BITS_BIG_ENDIAN |
| 1207 | lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize); |
| 1208 | #else |
| 1209 | lsbcount = (bitpos % 8); |
| 1210 | #endif |
| 1211 | val >>= lsbcount; |
| 1212 | |
| 1213 | /* If the field does not entirely fill a LONGEST, then zero the sign bits. |
| 1214 | If the field is signed, and is negative, then sign extend. */ |
| 1215 | |
| 1216 | if ((bitsize > 0) && (bitsize < 8 * sizeof (val))) |
| 1217 | { |
| 1218 | valmask = (((unsigned LONGEST) 1) << bitsize) - 1; |
| 1219 | val &= valmask; |
| 1220 | if (!TYPE_UNSIGNED (TYPE_FIELD_TYPE (type, fieldno))) |
| 1221 | { |
| 1222 | if (val & (valmask ^ (valmask >> 1))) |
| 1223 | { |
| 1224 | val |= ~valmask; |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | return (val); |
| 1229 | } |
| 1230 | |
| 1231 | /* Modify the value of a bitfield. ADDR points to a block of memory in |
| 1232 | target byte order; the bitfield starts in the byte pointed to. FIELDVAL |
| 1233 | is the desired value of the field, in host byte order. BITPOS and BITSIZE |
| 1234 | indicate which bits (in target bit order) comprise the bitfield. */ |
| 1235 | |
| 1236 | void |
| 1237 | modify_field (addr, fieldval, bitpos, bitsize) |
| 1238 | char *addr; |
| 1239 | LONGEST fieldval; |
| 1240 | int bitpos, bitsize; |
| 1241 | { |
| 1242 | LONGEST oword; |
| 1243 | |
| 1244 | /* Reject values too big to fit in the field in question, |
| 1245 | otherwise adjoining fields may be corrupted. */ |
| 1246 | if (bitsize < (8 * sizeof (fieldval)) |
| 1247 | && 0 != (fieldval & ~((1<<bitsize)-1))) |
| 1248 | { |
| 1249 | /* FIXME: would like to include fieldval in the message, but |
| 1250 | we don't have a sprintf_longest. */ |
| 1251 | error ("Value does not fit in %d bits.", bitsize); |
| 1252 | } |
| 1253 | |
| 1254 | oword = extract_signed_integer (addr, sizeof oword); |
| 1255 | |
| 1256 | /* Shifting for bit field depends on endianness of the target machine. */ |
| 1257 | #if BITS_BIG_ENDIAN |
| 1258 | bitpos = sizeof (oword) * 8 - bitpos - bitsize; |
| 1259 | #endif |
| 1260 | |
| 1261 | /* Mask out old value, while avoiding shifts >= size of oword */ |
| 1262 | if (bitsize < 8 * sizeof (oword)) |
| 1263 | oword &= ~(((((unsigned LONGEST)1) << bitsize) - 1) << bitpos); |
| 1264 | else |
| 1265 | oword &= ~((~(unsigned LONGEST)0) << bitpos); |
| 1266 | oword |= fieldval << bitpos; |
| 1267 | |
| 1268 | store_signed_integer (addr, sizeof oword, oword); |
| 1269 | } |
| 1270 | \f |
| 1271 | /* Convert C numbers into newly allocated values */ |
| 1272 | |
| 1273 | value |
| 1274 | value_from_longest (type, num) |
| 1275 | struct type *type; |
| 1276 | register LONGEST num; |
| 1277 | { |
| 1278 | register value val = allocate_value (type); |
| 1279 | register enum type_code code = TYPE_CODE (type); |
| 1280 | register int len = TYPE_LENGTH (type); |
| 1281 | |
| 1282 | switch (code) |
| 1283 | { |
| 1284 | case TYPE_CODE_INT: |
| 1285 | case TYPE_CODE_CHAR: |
| 1286 | case TYPE_CODE_ENUM: |
| 1287 | case TYPE_CODE_BOOL: |
| 1288 | store_signed_integer (VALUE_CONTENTS_RAW (val), len, num); |
| 1289 | break; |
| 1290 | |
| 1291 | case TYPE_CODE_REF: |
| 1292 | case TYPE_CODE_PTR: |
| 1293 | /* This assumes that all pointers of a given length |
| 1294 | have the same form. */ |
| 1295 | store_address (VALUE_CONTENTS_RAW (val), len, (CORE_ADDR) num); |
| 1296 | break; |
| 1297 | |
| 1298 | default: |
| 1299 | error ("Unexpected type encountered for integer constant."); |
| 1300 | } |
| 1301 | return val; |
| 1302 | } |
| 1303 | |
| 1304 | value |
| 1305 | value_from_double (type, num) |
| 1306 | struct type *type; |
| 1307 | double num; |
| 1308 | { |
| 1309 | register value val = allocate_value (type); |
| 1310 | register enum type_code code = TYPE_CODE (type); |
| 1311 | register int len = TYPE_LENGTH (type); |
| 1312 | |
| 1313 | if (code == TYPE_CODE_FLT) |
| 1314 | { |
| 1315 | store_floating (VALUE_CONTENTS_RAW (val), len, num); |
| 1316 | } |
| 1317 | else |
| 1318 | error ("Unexpected type encountered for floating constant."); |
| 1319 | |
| 1320 | return val; |
| 1321 | } |
| 1322 | \f |
| 1323 | /* Deal with the value that is "about to be returned". */ |
| 1324 | |
| 1325 | /* Return the value that a function returning now |
| 1326 | would be returning to its caller, assuming its type is VALTYPE. |
| 1327 | RETBUF is where we look for what ought to be the contents |
| 1328 | of the registers (in raw form). This is because it is often |
| 1329 | desirable to restore old values to those registers |
| 1330 | after saving the contents of interest, and then call |
| 1331 | this function using the saved values. |
| 1332 | struct_return is non-zero when the function in question is |
| 1333 | using the structure return conventions on the machine in question; |
| 1334 | 0 when it is using the value returning conventions (this often |
| 1335 | means returning pointer to where structure is vs. returning value). */ |
| 1336 | |
| 1337 | value |
| 1338 | value_being_returned (valtype, retbuf, struct_return) |
| 1339 | register struct type *valtype; |
| 1340 | char retbuf[REGISTER_BYTES]; |
| 1341 | int struct_return; |
| 1342 | /*ARGSUSED*/ |
| 1343 | { |
| 1344 | register value val; |
| 1345 | CORE_ADDR addr; |
| 1346 | |
| 1347 | #if defined (EXTRACT_STRUCT_VALUE_ADDRESS) |
| 1348 | /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */ |
| 1349 | if (struct_return) { |
| 1350 | addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf); |
| 1351 | if (!addr) |
| 1352 | error ("Function return value unknown"); |
| 1353 | return value_at (valtype, addr); |
| 1354 | } |
| 1355 | #endif |
| 1356 | |
| 1357 | val = allocate_value (valtype); |
| 1358 | EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val)); |
| 1359 | |
| 1360 | return val; |
| 1361 | } |
| 1362 | |
| 1363 | /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of |
| 1364 | EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc |
| 1365 | and TYPE is the type (which is known to be struct, union or array). |
| 1366 | |
| 1367 | On most machines, the struct convention is used unless we are |
| 1368 | using gcc and the type is of a special size. */ |
| 1369 | /* As of about 31 Mar 93, GCC was changed to be compatible with the |
| 1370 | native compiler. GCC 2.3.3 was the last release that did it the |
| 1371 | old way. Since gcc2_compiled was not changed, we have no |
| 1372 | way to correctly win in all cases, so we just do the right thing |
| 1373 | for gcc1 and for gcc2 after this change. Thus it loses for gcc |
| 1374 | 2.0-2.3.3. This is somewhat unfortunate, but changing gcc2_compiled |
| 1375 | would cause more chaos than dealing with some struct returns being |
| 1376 | handled wrong. */ |
| 1377 | #if !defined (USE_STRUCT_CONVENTION) |
| 1378 | #define USE_STRUCT_CONVENTION(gcc_p, type)\ |
| 1379 | (!((gcc_p == 1) && (TYPE_LENGTH (value_type) == 1 \ |
| 1380 | || TYPE_LENGTH (value_type) == 2 \ |
| 1381 | || TYPE_LENGTH (value_type) == 4 \ |
| 1382 | || TYPE_LENGTH (value_type) == 8 \ |
| 1383 | ) \ |
| 1384 | )) |
| 1385 | #endif |
| 1386 | |
| 1387 | /* Return true if the function specified is using the structure returning |
| 1388 | convention on this machine to return arguments, or 0 if it is using |
| 1389 | the value returning convention. FUNCTION is the value representing |
| 1390 | the function, FUNCADDR is the address of the function, and VALUE_TYPE |
| 1391 | is the type returned by the function. GCC_P is nonzero if compiled |
| 1392 | with GCC. */ |
| 1393 | |
| 1394 | int |
| 1395 | using_struct_return (function, funcaddr, value_type, gcc_p) |
| 1396 | value function; |
| 1397 | CORE_ADDR funcaddr; |
| 1398 | struct type *value_type; |
| 1399 | int gcc_p; |
| 1400 | /*ARGSUSED*/ |
| 1401 | { |
| 1402 | register enum type_code code = TYPE_CODE (value_type); |
| 1403 | |
| 1404 | if (code == TYPE_CODE_ERROR) |
| 1405 | error ("Function return type unknown."); |
| 1406 | |
| 1407 | if (code == TYPE_CODE_STRUCT || |
| 1408 | code == TYPE_CODE_UNION || |
| 1409 | code == TYPE_CODE_ARRAY) |
| 1410 | return USE_STRUCT_CONVENTION (gcc_p, value_type); |
| 1411 | |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | /* Store VAL so it will be returned if a function returns now. |
| 1416 | Does not verify that VAL's type matches what the current |
| 1417 | function wants to return. */ |
| 1418 | |
| 1419 | void |
| 1420 | set_return_value (val) |
| 1421 | value val; |
| 1422 | { |
| 1423 | register enum type_code code = TYPE_CODE (VALUE_TYPE (val)); |
| 1424 | double dbuf; |
| 1425 | LONGEST lbuf; |
| 1426 | |
| 1427 | if (code == TYPE_CODE_ERROR) |
| 1428 | error ("Function return type unknown."); |
| 1429 | |
| 1430 | if ( code == TYPE_CODE_STRUCT |
| 1431 | || code == TYPE_CODE_UNION) /* FIXME, implement struct return. */ |
| 1432 | error ("GDB does not support specifying a struct or union return value."); |
| 1433 | |
| 1434 | /* FIXME, this is bogus. We don't know what the return conventions |
| 1435 | are, or how values should be promoted.... */ |
| 1436 | if (code == TYPE_CODE_FLT) |
| 1437 | { |
| 1438 | dbuf = value_as_double (val); |
| 1439 | |
| 1440 | STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf); |
| 1441 | } |
| 1442 | else |
| 1443 | { |
| 1444 | lbuf = value_as_long (val); |
| 1445 | STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf); |
| 1446 | } |
| 1447 | } |
| 1448 | \f |
| 1449 | void |
| 1450 | _initialize_values () |
| 1451 | { |
| 1452 | add_cmd ("convenience", no_class, show_convenience, |
| 1453 | "Debugger convenience (\"$foo\") variables.\n\ |
| 1454 | These variables are created when you assign them values;\n\ |
| 1455 | thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\ |
| 1456 | A few convenience variables are given values automatically:\n\ |
| 1457 | \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\ |
| 1458 | \"$__\" holds the contents of the last address examined with \"x\".", |
| 1459 | &showlist); |
| 1460 | |
| 1461 | add_cmd ("values", no_class, show_values, |
| 1462 | "Elements of value history around item number IDX (or last ten).", |
| 1463 | &showlist); |
| 1464 | } |