| 1 | /* Support for printing Modula 2 values for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 1986, 1988, 1989, 1991, 1992, 1996, 1998, 2000, 2005, 2006, |
| 4 | 2007 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 3 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, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "symtab.h" |
| 23 | #include "gdbtypes.h" |
| 24 | #include "expression.h" |
| 25 | #include "value.h" |
| 26 | #include "valprint.h" |
| 27 | #include "language.h" |
| 28 | #include "typeprint.h" |
| 29 | #include "c-lang.h" |
| 30 | #include "m2-lang.h" |
| 31 | #include "target.h" |
| 32 | |
| 33 | int print_unpacked_pointer (struct type *type, |
| 34 | CORE_ADDR address, CORE_ADDR addr, |
| 35 | int format, struct ui_file *stream); |
| 36 | |
| 37 | |
| 38 | /* Print function pointer with inferior address ADDRESS onto stdio |
| 39 | stream STREAM. */ |
| 40 | |
| 41 | static void |
| 42 | print_function_pointer_address (CORE_ADDR address, struct ui_file *stream) |
| 43 | { |
| 44 | CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch, |
| 45 | address, |
| 46 | ¤t_target); |
| 47 | |
| 48 | /* If the function pointer is represented by a description, print the |
| 49 | address of the description. */ |
| 50 | if (addressprint && func_addr != address) |
| 51 | { |
| 52 | fputs_filtered ("@", stream); |
| 53 | fputs_filtered (paddress (address), stream); |
| 54 | fputs_filtered (": ", stream); |
| 55 | } |
| 56 | print_address_demangle (func_addr, stream, demangle); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * get_long_set_bounds - assigns the bounds of the long set to low and high. |
| 61 | */ |
| 62 | |
| 63 | int |
| 64 | get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high) |
| 65 | { |
| 66 | int len, i; |
| 67 | |
| 68 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
| 69 | { |
| 70 | len = TYPE_NFIELDS (type); |
| 71 | i = TYPE_N_BASECLASSES (type); |
| 72 | if (len == 0) |
| 73 | return 0; |
| 74 | *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i))); |
| 75 | *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, |
| 76 | len-1))); |
| 77 | return 1; |
| 78 | } |
| 79 | error (_("expecting long_set")); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | m2_print_long_set (struct type *type, const gdb_byte *valaddr, |
| 85 | int embedded_offset, CORE_ADDR address, |
| 86 | struct ui_file *stream, int format, |
| 87 | enum val_prettyprint pretty) |
| 88 | { |
| 89 | int empty_set = 1; |
| 90 | int element_seen = 0; |
| 91 | LONGEST previous_low = 0; |
| 92 | LONGEST previous_high= 0; |
| 93 | LONGEST i, low_bound, high_bound; |
| 94 | LONGEST field_low, field_high; |
| 95 | struct type *range; |
| 96 | int len, field; |
| 97 | struct type *target; |
| 98 | int bitval; |
| 99 | |
| 100 | CHECK_TYPEDEF (type); |
| 101 | |
| 102 | fprintf_filtered (stream, "{"); |
| 103 | len = TYPE_NFIELDS (type); |
| 104 | if (get_long_set_bounds (type, &low_bound, &high_bound)) |
| 105 | { |
| 106 | field = TYPE_N_BASECLASSES (type); |
| 107 | range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field)); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | fprintf_filtered (stream, " %s }", _("<unknown bounds of set>")); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | target = TYPE_TARGET_TYPE (range); |
| 116 | if (target == NULL) |
| 117 | target = builtin_type_int; |
| 118 | |
| 119 | if (get_discrete_bounds (range, &field_low, &field_high) >= 0) |
| 120 | { |
| 121 | for (i = low_bound; i <= high_bound; i++) |
| 122 | { |
| 123 | bitval = value_bit_index (TYPE_FIELD_TYPE (type, field), |
| 124 | (TYPE_FIELD_BITPOS (type, field) / 8) + |
| 125 | valaddr + embedded_offset, i); |
| 126 | if (bitval < 0) |
| 127 | error (_("bit test is out of range")); |
| 128 | else if (bitval > 0) |
| 129 | { |
| 130 | previous_high = i; |
| 131 | if (! element_seen) |
| 132 | { |
| 133 | if (! empty_set) |
| 134 | fprintf_filtered (stream, ", "); |
| 135 | print_type_scalar (target, i, stream); |
| 136 | empty_set = 0; |
| 137 | element_seen = 1; |
| 138 | previous_low = i; |
| 139 | } |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | /* bit is not set */ |
| 144 | if (element_seen) |
| 145 | { |
| 146 | if (previous_low+1 < previous_high) |
| 147 | fprintf_filtered (stream, ".."); |
| 148 | if (previous_low+1 < previous_high) |
| 149 | print_type_scalar (target, previous_high, stream); |
| 150 | element_seen = 0; |
| 151 | } |
| 152 | } |
| 153 | if (i == field_high) |
| 154 | { |
| 155 | field++; |
| 156 | if (field == len) |
| 157 | break; |
| 158 | range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field)); |
| 159 | if (get_discrete_bounds (range, &field_low, &field_high) < 0) |
| 160 | break; |
| 161 | target = TYPE_TARGET_TYPE (range); |
| 162 | if (target == NULL) |
| 163 | target = builtin_type_int; |
| 164 | } |
| 165 | } |
| 166 | if (element_seen) |
| 167 | { |
| 168 | if (previous_low+1 < previous_high) |
| 169 | { |
| 170 | fprintf_filtered (stream, ".."); |
| 171 | print_type_scalar (target, previous_high, stream); |
| 172 | } |
| 173 | element_seen = 0; |
| 174 | } |
| 175 | fprintf_filtered (stream, "}"); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | int |
| 180 | print_unpacked_pointer (struct type *type, |
| 181 | CORE_ADDR address, CORE_ADDR addr, |
| 182 | int format, struct ui_file *stream) |
| 183 | { |
| 184 | struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type)); |
| 185 | |
| 186 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) |
| 187 | { |
| 188 | /* Try to print what function it points to. */ |
| 189 | print_function_pointer_address (addr, stream); |
| 190 | /* Return value is irrelevant except for string pointers. */ |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | if (addressprint && format != 's') |
| 195 | fputs_filtered (paddress (address), stream); |
| 196 | |
| 197 | /* For a pointer to char or unsigned char, also print the string |
| 198 | pointed to, unless pointer is null. */ |
| 199 | |
| 200 | if (TYPE_LENGTH (elttype) == 1 |
| 201 | && TYPE_CODE (elttype) == TYPE_CODE_INT |
| 202 | && (format == 0 || format == 's') |
| 203 | && addr != 0) |
| 204 | return val_print_string (addr, -1, TYPE_LENGTH (elttype), stream); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static void |
| 210 | print_variable_at_address (struct type *type, const gdb_byte *valaddr, |
| 211 | struct ui_file *stream, int format, |
| 212 | int deref_ref, int recurse, |
| 213 | enum val_prettyprint pretty) |
| 214 | { |
| 215 | CORE_ADDR addr = unpack_pointer (type, valaddr); |
| 216 | struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type)); |
| 217 | |
| 218 | fprintf_filtered (stream, "["); |
| 219 | fputs_filtered (paddress (addr), stream); |
| 220 | fprintf_filtered (stream, "] : "); |
| 221 | |
| 222 | if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF) |
| 223 | { |
| 224 | struct value *deref_val = |
| 225 | value_at |
| 226 | (TYPE_TARGET_TYPE (type), |
| 227 | unpack_pointer (lookup_pointer_type (builtin_type_void), |
| 228 | valaddr)); |
| 229 | common_val_print (deref_val, stream, format, deref_ref, |
| 230 | recurse, pretty); |
| 231 | } |
| 232 | else |
| 233 | fputs_filtered ("???", stream); |
| 234 | } |
| 235 | |
| 236 | /* Print data of type TYPE located at VALADDR (within GDB), which came from |
| 237 | the inferior at address ADDRESS, onto stdio stream STREAM according to |
| 238 | FORMAT (a letter or 0 for natural format). The data at VALADDR is in |
| 239 | target byte order. |
| 240 | |
| 241 | If the data are a string pointer, returns the number of string characters |
| 242 | printed. |
| 243 | |
| 244 | If DEREF_REF is nonzero, then dereference references, otherwise just print |
| 245 | them like pointers. |
| 246 | |
| 247 | The PRETTY parameter controls prettyprinting. */ |
| 248 | |
| 249 | int |
| 250 | m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset, |
| 251 | CORE_ADDR address, struct ui_file *stream, int format, |
| 252 | int deref_ref, int recurse, enum val_prettyprint pretty) |
| 253 | { |
| 254 | unsigned int i = 0; /* Number of characters printed */ |
| 255 | unsigned len; |
| 256 | struct type *elttype; |
| 257 | unsigned eltlen; |
| 258 | int length_pos, length_size, string_pos; |
| 259 | int char_size; |
| 260 | LONGEST val; |
| 261 | CORE_ADDR addr; |
| 262 | |
| 263 | CHECK_TYPEDEF (type); |
| 264 | switch (TYPE_CODE (type)) |
| 265 | { |
| 266 | case TYPE_CODE_ARRAY: |
| 267 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0) |
| 268 | { |
| 269 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); |
| 270 | eltlen = TYPE_LENGTH (elttype); |
| 271 | len = TYPE_LENGTH (type) / eltlen; |
| 272 | if (prettyprint_arrays) |
| 273 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 274 | /* For an array of chars, print with string syntax. */ |
| 275 | if (eltlen == 1 && |
| 276 | ((TYPE_CODE (elttype) == TYPE_CODE_INT) |
| 277 | || ((current_language->la_language == language_m2) |
| 278 | && (TYPE_CODE (elttype) == TYPE_CODE_CHAR))) |
| 279 | && (format == 0 || format == 's')) |
| 280 | { |
| 281 | /* If requested, look for the first null char and only print |
| 282 | elements up to it. */ |
| 283 | if (stop_print_at_null) |
| 284 | { |
| 285 | unsigned int temp_len; |
| 286 | |
| 287 | /* Look for a NULL char. */ |
| 288 | for (temp_len = 0; |
| 289 | (valaddr + embedded_offset)[temp_len] |
| 290 | && temp_len < len && temp_len < print_max; |
| 291 | temp_len++); |
| 292 | len = temp_len; |
| 293 | } |
| 294 | |
| 295 | LA_PRINT_STRING (stream, valaddr + embedded_offset, len, 1, 0); |
| 296 | i = len; |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | fprintf_filtered (stream, "{"); |
| 301 | val_print_array_elements (type, valaddr + embedded_offset, |
| 302 | address, stream, format, deref_ref, |
| 303 | recurse, pretty, 0); |
| 304 | fprintf_filtered (stream, "}"); |
| 305 | } |
| 306 | break; |
| 307 | } |
| 308 | /* Array of unspecified length: treat like pointer to first elt. */ |
| 309 | print_unpacked_pointer (type, address, address, format, stream); |
| 310 | break; |
| 311 | |
| 312 | case TYPE_CODE_PTR: |
| 313 | if (TYPE_CONST (type)) |
| 314 | print_variable_at_address (type, valaddr + embedded_offset, |
| 315 | stream, format, deref_ref, recurse, |
| 316 | pretty); |
| 317 | else if (format && format != 's') |
| 318 | print_scalar_formatted (valaddr + embedded_offset, type, format, |
| 319 | 0, stream); |
| 320 | else |
| 321 | { |
| 322 | addr = unpack_pointer (type, valaddr + embedded_offset); |
| 323 | print_unpacked_pointer (type, addr, address, format, stream); |
| 324 | } |
| 325 | break; |
| 326 | |
| 327 | case TYPE_CODE_REF: |
| 328 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); |
| 329 | if (addressprint) |
| 330 | { |
| 331 | CORE_ADDR addr |
| 332 | = extract_typed_address (valaddr + embedded_offset, type); |
| 333 | fprintf_filtered (stream, "@"); |
| 334 | fputs_filtered (paddress (addr), stream); |
| 335 | if (deref_ref) |
| 336 | fputs_filtered (": ", stream); |
| 337 | } |
| 338 | /* De-reference the reference. */ |
| 339 | if (deref_ref) |
| 340 | { |
| 341 | if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF) |
| 342 | { |
| 343 | struct value *deref_val = |
| 344 | value_at |
| 345 | (TYPE_TARGET_TYPE (type), |
| 346 | unpack_pointer (lookup_pointer_type (builtin_type_void), |
| 347 | valaddr + embedded_offset)); |
| 348 | common_val_print (deref_val, stream, format, deref_ref, |
| 349 | recurse, pretty); |
| 350 | } |
| 351 | else |
| 352 | fputs_filtered ("???", stream); |
| 353 | } |
| 354 | break; |
| 355 | |
| 356 | case TYPE_CODE_UNION: |
| 357 | if (recurse && !unionprint) |
| 358 | { |
| 359 | fprintf_filtered (stream, "{...}"); |
| 360 | break; |
| 361 | } |
| 362 | /* Fall through. */ |
| 363 | case TYPE_CODE_STRUCT: |
| 364 | if (m2_is_long_set (type)) |
| 365 | m2_print_long_set (type, valaddr, embedded_offset, address, |
| 366 | stream, format, pretty); |
| 367 | else |
| 368 | cp_print_value_fields (type, type, valaddr, embedded_offset, |
| 369 | address, stream, format, |
| 370 | recurse, pretty, NULL, 0); |
| 371 | break; |
| 372 | |
| 373 | case TYPE_CODE_ENUM: |
| 374 | if (format) |
| 375 | { |
| 376 | print_scalar_formatted (valaddr + embedded_offset, type, |
| 377 | format, 0, stream); |
| 378 | break; |
| 379 | } |
| 380 | len = TYPE_NFIELDS (type); |
| 381 | val = unpack_long (type, valaddr + embedded_offset); |
| 382 | for (i = 0; i < len; i++) |
| 383 | { |
| 384 | QUIT; |
| 385 | if (val == TYPE_FIELD_BITPOS (type, i)) |
| 386 | { |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | if (i < len) |
| 391 | { |
| 392 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | print_longest (stream, 'd', 0, val); |
| 397 | } |
| 398 | break; |
| 399 | |
| 400 | case TYPE_CODE_FUNC: |
| 401 | if (format) |
| 402 | { |
| 403 | print_scalar_formatted (valaddr + embedded_offset, type, |
| 404 | format, 0, stream); |
| 405 | break; |
| 406 | } |
| 407 | /* FIXME, we should consider, at least for ANSI C language, eliminating |
| 408 | the distinction made between FUNCs and POINTERs to FUNCs. */ |
| 409 | fprintf_filtered (stream, "{"); |
| 410 | type_print (type, "", stream, -1); |
| 411 | fprintf_filtered (stream, "} "); |
| 412 | /* Try to print what function it points to, and its address. */ |
| 413 | print_address_demangle (address, stream, demangle); |
| 414 | break; |
| 415 | |
| 416 | case TYPE_CODE_BOOL: |
| 417 | format = format ? format : output_format; |
| 418 | if (format) |
| 419 | print_scalar_formatted (valaddr + embedded_offset, type, |
| 420 | format, 0, stream); |
| 421 | else |
| 422 | { |
| 423 | val = unpack_long (type, valaddr + embedded_offset); |
| 424 | if (val == 0) |
| 425 | fputs_filtered ("FALSE", stream); |
| 426 | else if (val == 1) |
| 427 | fputs_filtered ("TRUE", stream); |
| 428 | else |
| 429 | fprintf_filtered (stream, "%ld)", (long int) val); |
| 430 | } |
| 431 | break; |
| 432 | |
| 433 | case TYPE_CODE_RANGE: |
| 434 | if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type))) |
| 435 | { |
| 436 | m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset, |
| 437 | address, stream, format, deref_ref, recurse, pretty); |
| 438 | break; |
| 439 | } |
| 440 | /* FIXME: create_range_type does not set the unsigned bit in a |
| 441 | range type (I think it probably should copy it from the target |
| 442 | type), so we won't print values which are too large to |
| 443 | fit in a signed integer correctly. */ |
| 444 | /* FIXME: Doesn't handle ranges of enums correctly. (Can't just |
| 445 | print with the target type, though, because the size of our type |
| 446 | and the target type might differ). */ |
| 447 | /* FALLTHROUGH */ |
| 448 | |
| 449 | case TYPE_CODE_INT: |
| 450 | format = format ? format : output_format; |
| 451 | if (format) |
| 452 | print_scalar_formatted (valaddr + embedded_offset, type, format, |
| 453 | 0, stream); |
| 454 | else |
| 455 | val_print_type_code_int (type, valaddr + embedded_offset, stream); |
| 456 | break; |
| 457 | |
| 458 | case TYPE_CODE_CHAR: |
| 459 | format = format ? format : output_format; |
| 460 | if (format) |
| 461 | print_scalar_formatted (valaddr + embedded_offset, type, |
| 462 | format, 0, stream); |
| 463 | else |
| 464 | { |
| 465 | val = unpack_long (type, valaddr + embedded_offset); |
| 466 | if (TYPE_UNSIGNED (type)) |
| 467 | fprintf_filtered (stream, "%u", (unsigned int) val); |
| 468 | else |
| 469 | fprintf_filtered (stream, "%d", (int) val); |
| 470 | fputs_filtered (" ", stream); |
| 471 | LA_PRINT_CHAR ((unsigned char) val, stream); |
| 472 | } |
| 473 | break; |
| 474 | |
| 475 | case TYPE_CODE_FLT: |
| 476 | if (format) |
| 477 | print_scalar_formatted (valaddr + embedded_offset, type, |
| 478 | format, 0, stream); |
| 479 | else |
| 480 | print_floating (valaddr + embedded_offset, type, stream); |
| 481 | break; |
| 482 | |
| 483 | case TYPE_CODE_METHOD: |
| 484 | break; |
| 485 | |
| 486 | case TYPE_CODE_BITSTRING: |
| 487 | case TYPE_CODE_SET: |
| 488 | elttype = TYPE_INDEX_TYPE (type); |
| 489 | CHECK_TYPEDEF (elttype); |
| 490 | if (TYPE_STUB (elttype)) |
| 491 | { |
| 492 | fprintf_filtered (stream, _("<incomplete type>")); |
| 493 | gdb_flush (stream); |
| 494 | break; |
| 495 | } |
| 496 | else |
| 497 | { |
| 498 | struct type *range = elttype; |
| 499 | LONGEST low_bound, high_bound; |
| 500 | int i; |
| 501 | int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING; |
| 502 | int need_comma = 0; |
| 503 | |
| 504 | if (is_bitstring) |
| 505 | fputs_filtered ("B'", stream); |
| 506 | else |
| 507 | fputs_filtered ("{", stream); |
| 508 | |
| 509 | i = get_discrete_bounds (range, &low_bound, &high_bound); |
| 510 | maybe_bad_bstring: |
| 511 | if (i < 0) |
| 512 | { |
| 513 | fputs_filtered (_("<error value>"), stream); |
| 514 | goto done; |
| 515 | } |
| 516 | |
| 517 | for (i = low_bound; i <= high_bound; i++) |
| 518 | { |
| 519 | int element = value_bit_index (type, valaddr + embedded_offset, |
| 520 | i); |
| 521 | if (element < 0) |
| 522 | { |
| 523 | i = element; |
| 524 | goto maybe_bad_bstring; |
| 525 | } |
| 526 | if (is_bitstring) |
| 527 | fprintf_filtered (stream, "%d", element); |
| 528 | else if (element) |
| 529 | { |
| 530 | if (need_comma) |
| 531 | fputs_filtered (", ", stream); |
| 532 | print_type_scalar (range, i, stream); |
| 533 | need_comma = 1; |
| 534 | |
| 535 | if (i + 1 <= high_bound |
| 536 | && value_bit_index (type, valaddr + embedded_offset, |
| 537 | ++i)) |
| 538 | { |
| 539 | int j = i; |
| 540 | fputs_filtered ("..", stream); |
| 541 | while (i + 1 <= high_bound |
| 542 | && value_bit_index (type, |
| 543 | valaddr + embedded_offset, |
| 544 | ++i)) |
| 545 | j = i; |
| 546 | print_type_scalar (range, j, stream); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | done: |
| 551 | if (is_bitstring) |
| 552 | fputs_filtered ("'", stream); |
| 553 | else |
| 554 | fputs_filtered ("}", stream); |
| 555 | } |
| 556 | break; |
| 557 | |
| 558 | case TYPE_CODE_VOID: |
| 559 | fprintf_filtered (stream, "void"); |
| 560 | break; |
| 561 | |
| 562 | case TYPE_CODE_ERROR: |
| 563 | fprintf_filtered (stream, _("<error type>")); |
| 564 | break; |
| 565 | |
| 566 | case TYPE_CODE_UNDEF: |
| 567 | /* This happens (without TYPE_FLAG_STUB set) on systems which don't use |
| 568 | dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" |
| 569 | and no complete type for struct foo in that file. */ |
| 570 | fprintf_filtered (stream, _("<incomplete type>")); |
| 571 | break; |
| 572 | |
| 573 | default: |
| 574 | error (_("Invalid m2 type code %d in symbol table."), TYPE_CODE (type)); |
| 575 | } |
| 576 | gdb_flush (stream); |
| 577 | return (0); |
| 578 | } |