| 1 | /* Support for printing Chill values for GDB, the GNU debugger. |
| 2 | Copyright 1986, 1988, 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 "obstack.h" |
| 22 | #include "symtab.h" |
| 23 | #include "gdbtypes.h" |
| 24 | #include "valprint.h" |
| 25 | #include "expression.h" |
| 26 | #include "value.h" |
| 27 | #include "language.h" |
| 28 | #include "demangle.h" |
| 29 | #include "c-lang.h" /* For c_val_print */ |
| 30 | |
| 31 | static void |
| 32 | chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int, |
| 33 | enum val_prettyprint, struct type **)); |
| 34 | |
| 35 | \f |
| 36 | /* Print data of type TYPE located at VALADDR (within GDB), which came from |
| 37 | the inferior at address ADDRESS, onto stdio stream STREAM according to |
| 38 | FORMAT (a letter or 0 for natural format). The data at VALADDR is in |
| 39 | target byte order. |
| 40 | |
| 41 | If the data are a string pointer, returns the number of string characters |
| 42 | printed. |
| 43 | |
| 44 | If DEREF_REF is nonzero, then dereference references, otherwise just print |
| 45 | them like pointers. |
| 46 | |
| 47 | The PRETTY parameter controls prettyprinting. */ |
| 48 | |
| 49 | int |
| 50 | chill_val_print (type, valaddr, address, stream, format, deref_ref, recurse, |
| 51 | pretty) |
| 52 | struct type *type; |
| 53 | char *valaddr; |
| 54 | CORE_ADDR address; |
| 55 | GDB_FILE *stream; |
| 56 | int format; |
| 57 | int deref_ref; |
| 58 | int recurse; |
| 59 | enum val_prettyprint pretty; |
| 60 | { |
| 61 | LONGEST val; |
| 62 | unsigned int i = 0; /* Number of characters printed. */ |
| 63 | struct type *elttype; |
| 64 | CORE_ADDR addr; |
| 65 | |
| 66 | switch (TYPE_CODE (type)) |
| 67 | { |
| 68 | case TYPE_CODE_ARRAY: |
| 69 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0) |
| 70 | { |
| 71 | if (prettyprint_arrays) |
| 72 | { |
| 73 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 74 | } |
| 75 | fprintf_filtered (stream, "["); |
| 76 | val_print_array_elements (type, valaddr, address, stream, format, |
| 77 | deref_ref, recurse, pretty, 0); |
| 78 | fprintf_filtered (stream, "]"); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | error ("unimplemented in chill_val_print; unspecified array length"); |
| 83 | } |
| 84 | break; |
| 85 | |
| 86 | case TYPE_CODE_INT: |
| 87 | format = format ? format : output_format; |
| 88 | if (format) |
| 89 | { |
| 90 | print_scalar_formatted (valaddr, type, format, 0, stream); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | val_print_type_code_int (type, valaddr, stream); |
| 95 | } |
| 96 | break; |
| 97 | |
| 98 | case TYPE_CODE_CHAR: |
| 99 | format = format ? format : output_format; |
| 100 | if (format) |
| 101 | { |
| 102 | print_scalar_formatted (valaddr, type, format, 0, stream); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), |
| 107 | stream); |
| 108 | } |
| 109 | break; |
| 110 | |
| 111 | case TYPE_CODE_FLT: |
| 112 | if (format) |
| 113 | { |
| 114 | print_scalar_formatted (valaddr, type, format, 0, stream); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | print_floating (valaddr, type, stream); |
| 119 | } |
| 120 | break; |
| 121 | |
| 122 | case TYPE_CODE_BOOL: |
| 123 | format = format ? format : output_format; |
| 124 | if (format) |
| 125 | { |
| 126 | print_scalar_formatted (valaddr, type, format, 0, stream); |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | val = unpack_long (builtin_type_chill_bool, valaddr); |
| 131 | fprintf_filtered (stream, val ? "TRUE" : "FALSE"); |
| 132 | } |
| 133 | break; |
| 134 | |
| 135 | case TYPE_CODE_UNDEF: |
| 136 | /* This happens (without TYPE_FLAG_STUB set) on systems which don't use |
| 137 | dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" |
| 138 | and no complete type for struct foo in that file. */ |
| 139 | fprintf_filtered (stream, "<incomplete type>"); |
| 140 | break; |
| 141 | |
| 142 | case TYPE_CODE_PTR: |
| 143 | if (format && format != 's') |
| 144 | { |
| 145 | print_scalar_formatted (valaddr, type, format, 0, stream); |
| 146 | break; |
| 147 | } |
| 148 | addr = unpack_pointer (type, valaddr); |
| 149 | elttype = TYPE_TARGET_TYPE (type); |
| 150 | |
| 151 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) |
| 152 | { |
| 153 | /* Try to print what function it points to. */ |
| 154 | print_address_demangle (addr, stream, demangle); |
| 155 | /* Return value is irrelevant except for string pointers. */ |
| 156 | return (0); |
| 157 | } |
| 158 | if (addressprint && format != 's') |
| 159 | { |
| 160 | fprintf_filtered (stream, "H'%lx", (unsigned long) addr); |
| 161 | } |
| 162 | |
| 163 | /* For a pointer to char or unsigned char, also print the string |
| 164 | pointed to, unless pointer is null. */ |
| 165 | if (TYPE_LENGTH (elttype) == 1 |
| 166 | && TYPE_CODE (elttype) == TYPE_CODE_CHAR |
| 167 | && (format == 0 || format == 's') |
| 168 | && addr != 0 |
| 169 | && /* If print_max is UINT_MAX, the alloca below will fail. |
| 170 | In that case don't try to print the string. */ |
| 171 | print_max < UINT_MAX) |
| 172 | { |
| 173 | i = val_print_string (addr, 0, stream); |
| 174 | } |
| 175 | /* Return number of characters printed, plus one for the |
| 176 | terminating null if we have "reached the end". */ |
| 177 | return (i + (print_max && i != print_max)); |
| 178 | break; |
| 179 | |
| 180 | case TYPE_CODE_STRING: |
| 181 | if (format && format != 's') |
| 182 | { |
| 183 | print_scalar_formatted (valaddr, type, format, 0, stream); |
| 184 | break; |
| 185 | } |
| 186 | i = TYPE_LENGTH (type); |
| 187 | LA_PRINT_STRING (stream, valaddr, i, 0); |
| 188 | /* Return number of characters printed, plus one for the terminating |
| 189 | null if we have "reached the end". */ |
| 190 | return (i + (print_max && i != print_max)); |
| 191 | break; |
| 192 | |
| 193 | case TYPE_CODE_BITSTRING: |
| 194 | case TYPE_CODE_SET: |
| 195 | { |
| 196 | struct type *range = TYPE_FIELD_TYPE (type, 0); |
| 197 | int low_bound = TYPE_LOW_BOUND (range); |
| 198 | int high_bound = TYPE_HIGH_BOUND (range); |
| 199 | int i; |
| 200 | int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING; |
| 201 | int need_comma = 0; |
| 202 | int in_range = 0; |
| 203 | |
| 204 | if (is_bitstring) |
| 205 | fputs_filtered ("B'", stream); |
| 206 | else |
| 207 | fputs_filtered ("[", stream); |
| 208 | for (i = low_bound; i <= high_bound; i++) |
| 209 | { |
| 210 | int element = value_bit_index (type, valaddr, i); |
| 211 | if (is_bitstring) |
| 212 | fprintf_filtered (stream, "%d", element); |
| 213 | else if (element) |
| 214 | { |
| 215 | if (need_comma) |
| 216 | fputs_filtered (", ", stream); |
| 217 | print_type_scalar (TYPE_TARGET_TYPE (range), i, stream); |
| 218 | need_comma = 1; |
| 219 | |
| 220 | /* Look for a continuous range of true elements. */ |
| 221 | if (i+1 <= high_bound && value_bit_index (type, valaddr, ++i)) |
| 222 | { |
| 223 | int j = i; /* j is the upper bound so far of the range */ |
| 224 | fputs_filtered (":", stream); |
| 225 | while (i+1 <= high_bound |
| 226 | && value_bit_index (type, valaddr, ++i)) |
| 227 | j = i; |
| 228 | print_type_scalar (TYPE_TARGET_TYPE (range), j, stream); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | if (is_bitstring) |
| 233 | fputs_filtered ("'", stream); |
| 234 | else |
| 235 | fputs_filtered ("]", stream); |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | case TYPE_CODE_STRUCT: |
| 240 | if (chill_is_varying_struct (type)) |
| 241 | { |
| 242 | struct type *inner = TYPE_FIELD_TYPE (type, 1); |
| 243 | long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr); |
| 244 | char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8; |
| 245 | |
| 246 | switch (TYPE_CODE (inner)) |
| 247 | { |
| 248 | case TYPE_CODE_STRING: |
| 249 | if (length > TYPE_LENGTH (type)) |
| 250 | { |
| 251 | fprintf_filtered (stream, |
| 252 | "<dynamic length %d > static length %d>", |
| 253 | length, TYPE_LENGTH (type)); |
| 254 | length > TYPE_LENGTH (type); |
| 255 | } |
| 256 | LA_PRINT_STRING (stream, data_addr, length, 0); |
| 257 | return length; |
| 258 | } |
| 259 | } |
| 260 | chill_print_value_fields (type, valaddr, stream, format, recurse, pretty, |
| 261 | 0); |
| 262 | break; |
| 263 | |
| 264 | case TYPE_CODE_REF: |
| 265 | if (addressprint) |
| 266 | { |
| 267 | fprintf_filtered (stream, "LOC(H'%lx)", |
| 268 | unpack_long (builtin_type_int, valaddr)); |
| 269 | if (deref_ref) |
| 270 | fputs_filtered (": ", stream); |
| 271 | } |
| 272 | /* De-reference the reference. */ |
| 273 | if (deref_ref) |
| 274 | { |
| 275 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF) |
| 276 | { |
| 277 | value deref_val = |
| 278 | value_at |
| 279 | (TYPE_TARGET_TYPE (type), |
| 280 | unpack_pointer (lookup_pointer_type (builtin_type_void), |
| 281 | valaddr)); |
| 282 | val_print (VALUE_TYPE (deref_val), |
| 283 | VALUE_CONTENTS (deref_val), |
| 284 | VALUE_ADDRESS (deref_val), stream, format, |
| 285 | deref_ref, recurse + 1, pretty); |
| 286 | } |
| 287 | else |
| 288 | fputs_filtered ("???", stream); |
| 289 | } |
| 290 | break; |
| 291 | |
| 292 | case TYPE_CODE_ENUM: |
| 293 | c_val_print (type, valaddr, address, stream, format, |
| 294 | deref_ref, recurse, pretty); |
| 295 | break; |
| 296 | |
| 297 | case TYPE_CODE_RANGE: |
| 298 | if (TYPE_TARGET_TYPE (type)) |
| 299 | chill_val_print (TYPE_TARGET_TYPE (type), valaddr, address, stream, |
| 300 | format, deref_ref, recurse, pretty); |
| 301 | break; |
| 302 | |
| 303 | case TYPE_CODE_MEMBER: |
| 304 | case TYPE_CODE_UNION: |
| 305 | case TYPE_CODE_FUNC: |
| 306 | case TYPE_CODE_VOID: |
| 307 | case TYPE_CODE_ERROR: |
| 308 | default: |
| 309 | /* Let's defer printing to the C printer, rather than |
| 310 | print an error message. FIXME! */ |
| 311 | c_val_print (type, valaddr, address, stream, format, |
| 312 | deref_ref, recurse, pretty); |
| 313 | } |
| 314 | gdb_flush (stream); |
| 315 | return (0); |
| 316 | } |
| 317 | |
| 318 | /* Mutually recursive subroutines of cplus_print_value and c_val_print to |
| 319 | print out a structure's fields: cp_print_value_fields and cplus_print_value. |
| 320 | |
| 321 | TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the |
| 322 | same meanings as in cplus_print_value and c_val_print. |
| 323 | |
| 324 | DONT_PRINT is an array of baseclass types that we |
| 325 | should not print, or zero if called from top level. */ |
| 326 | |
| 327 | static void |
| 328 | chill_print_value_fields (type, valaddr, stream, format, recurse, pretty, |
| 329 | dont_print) |
| 330 | struct type *type; |
| 331 | char *valaddr; |
| 332 | GDB_FILE *stream; |
| 333 | int format; |
| 334 | int recurse; |
| 335 | enum val_prettyprint pretty; |
| 336 | struct type **dont_print; |
| 337 | { |
| 338 | int i, len; |
| 339 | int fields_seen = 0; |
| 340 | |
| 341 | check_stub_type (type); |
| 342 | |
| 343 | fprintf_filtered (stream, "["); |
| 344 | len = TYPE_NFIELDS (type); |
| 345 | if (len == 0) |
| 346 | { |
| 347 | fprintf_filtered (stream, "<No data fields>"); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | for (i = 0; i < len; i++) |
| 352 | { |
| 353 | if (fields_seen) |
| 354 | { |
| 355 | fprintf_filtered (stream, ", "); |
| 356 | } |
| 357 | fields_seen = 1; |
| 358 | if (pretty) |
| 359 | { |
| 360 | fprintf_filtered (stream, "\n"); |
| 361 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | wrap_here (n_spaces (2 + 2 * recurse)); |
| 366 | } |
| 367 | fputs_filtered (".", stream); |
| 368 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), |
| 369 | language_chill, DMGL_NO_OPTS); |
| 370 | fputs_filtered (": ", stream); |
| 371 | if (TYPE_FIELD_PACKED (type, i)) |
| 372 | { |
| 373 | value v; |
| 374 | |
| 375 | /* Bitfields require special handling, especially due to byte |
| 376 | order problems. */ |
| 377 | v = value_from_longest (TYPE_FIELD_TYPE (type, i), |
| 378 | unpack_field_as_long (type, valaddr, i)); |
| 379 | |
| 380 | chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, |
| 381 | stream, format, 0, recurse + 1, pretty); |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | chill_val_print (TYPE_FIELD_TYPE (type, i), |
| 386 | valaddr + TYPE_FIELD_BITPOS (type, i) / 8, |
| 387 | 0, stream, format, 0, recurse + 1, pretty); |
| 388 | } |
| 389 | } |
| 390 | if (pretty) |
| 391 | { |
| 392 | fprintf_filtered (stream, "\n"); |
| 393 | print_spaces_filtered (2 * recurse, stream); |
| 394 | } |
| 395 | } |
| 396 | fprintf_filtered (stream, "]"); |
| 397 | } |