1*5796c8dcSSimon Schubert /* Support for printing C values for GDB, the GNU debugger. 2*5796c8dcSSimon Schubert 3*5796c8dcSSimon Schubert Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 4*5796c8dcSSimon Schubert 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007, 2008, 2009 5*5796c8dcSSimon Schubert Free Software Foundation, Inc. 6*5796c8dcSSimon Schubert 7*5796c8dcSSimon Schubert This file is part of GDB. 8*5796c8dcSSimon Schubert 9*5796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 10*5796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 11*5796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 12*5796c8dcSSimon Schubert (at your option) any later version. 13*5796c8dcSSimon Schubert 14*5796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 15*5796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 16*5796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17*5796c8dcSSimon Schubert GNU General Public License for more details. 18*5796c8dcSSimon Schubert 19*5796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 20*5796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21*5796c8dcSSimon Schubert 22*5796c8dcSSimon Schubert #include "defs.h" 23*5796c8dcSSimon Schubert #include "gdb_string.h" 24*5796c8dcSSimon Schubert #include "symtab.h" 25*5796c8dcSSimon Schubert #include "gdbtypes.h" 26*5796c8dcSSimon Schubert #include "expression.h" 27*5796c8dcSSimon Schubert #include "value.h" 28*5796c8dcSSimon Schubert #include "valprint.h" 29*5796c8dcSSimon Schubert #include "language.h" 30*5796c8dcSSimon Schubert #include "c-lang.h" 31*5796c8dcSSimon Schubert #include "cp-abi.h" 32*5796c8dcSSimon Schubert #include "target.h" 33*5796c8dcSSimon Schubert 34*5796c8dcSSimon Schubert 35*5796c8dcSSimon Schubert /* Print function pointer with inferior address ADDRESS onto stdio 36*5796c8dcSSimon Schubert stream STREAM. */ 37*5796c8dcSSimon Schubert 38*5796c8dcSSimon Schubert static void 39*5796c8dcSSimon Schubert print_function_pointer_address (struct gdbarch *gdbarch, CORE_ADDR address, 40*5796c8dcSSimon Schubert struct ui_file *stream, int addressprint) 41*5796c8dcSSimon Schubert { 42*5796c8dcSSimon Schubert CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, address, 43*5796c8dcSSimon Schubert ¤t_target); 44*5796c8dcSSimon Schubert 45*5796c8dcSSimon Schubert /* If the function pointer is represented by a description, print the 46*5796c8dcSSimon Schubert address of the description. */ 47*5796c8dcSSimon Schubert if (addressprint && func_addr != address) 48*5796c8dcSSimon Schubert { 49*5796c8dcSSimon Schubert fputs_filtered ("@", stream); 50*5796c8dcSSimon Schubert fputs_filtered (paddress (gdbarch, address), stream); 51*5796c8dcSSimon Schubert fputs_filtered (": ", stream); 52*5796c8dcSSimon Schubert } 53*5796c8dcSSimon Schubert print_address_demangle (gdbarch, func_addr, stream, demangle); 54*5796c8dcSSimon Schubert } 55*5796c8dcSSimon Schubert 56*5796c8dcSSimon Schubert 57*5796c8dcSSimon Schubert /* A helper for textual_element_type. This checks the name of the 58*5796c8dcSSimon Schubert typedef. This is bogus but it isn't apparent that the compiler 59*5796c8dcSSimon Schubert provides us the help we may need. */ 60*5796c8dcSSimon Schubert 61*5796c8dcSSimon Schubert static int 62*5796c8dcSSimon Schubert textual_name (const char *name) 63*5796c8dcSSimon Schubert { 64*5796c8dcSSimon Schubert return (!strcmp (name, "wchar_t") 65*5796c8dcSSimon Schubert || !strcmp (name, "char16_t") 66*5796c8dcSSimon Schubert || !strcmp (name, "char32_t")); 67*5796c8dcSSimon Schubert } 68*5796c8dcSSimon Schubert 69*5796c8dcSSimon Schubert /* Apply a heuristic to decide whether an array of TYPE or a pointer 70*5796c8dcSSimon Schubert to TYPE should be printed as a textual string. Return non-zero if 71*5796c8dcSSimon Schubert it should, or zero if it should be treated as an array of integers 72*5796c8dcSSimon Schubert or pointer to integers. FORMAT is the current format letter, 73*5796c8dcSSimon Schubert or 0 if none. 74*5796c8dcSSimon Schubert 75*5796c8dcSSimon Schubert We guess that "char" is a character. Explicitly signed and 76*5796c8dcSSimon Schubert unsigned character types are also characters. Integer data from 77*5796c8dcSSimon Schubert vector types is not. The user can override this by using the /s 78*5796c8dcSSimon Schubert format letter. */ 79*5796c8dcSSimon Schubert 80*5796c8dcSSimon Schubert static int 81*5796c8dcSSimon Schubert textual_element_type (struct type *type, char format) 82*5796c8dcSSimon Schubert { 83*5796c8dcSSimon Schubert struct type *true_type, *iter_type; 84*5796c8dcSSimon Schubert 85*5796c8dcSSimon Schubert if (format != 0 && format != 's') 86*5796c8dcSSimon Schubert return 0; 87*5796c8dcSSimon Schubert 88*5796c8dcSSimon Schubert /* We also rely on this for its side effect of setting up all the 89*5796c8dcSSimon Schubert typedef pointers. */ 90*5796c8dcSSimon Schubert true_type = check_typedef (type); 91*5796c8dcSSimon Schubert 92*5796c8dcSSimon Schubert /* TYPE_CODE_CHAR is always textual. */ 93*5796c8dcSSimon Schubert if (TYPE_CODE (true_type) == TYPE_CODE_CHAR) 94*5796c8dcSSimon Schubert return 1; 95*5796c8dcSSimon Schubert 96*5796c8dcSSimon Schubert /* Any other character-like types must be integral. */ 97*5796c8dcSSimon Schubert if (TYPE_CODE (true_type) != TYPE_CODE_INT) 98*5796c8dcSSimon Schubert return 0; 99*5796c8dcSSimon Schubert 100*5796c8dcSSimon Schubert /* We peel typedefs one by one, looking for a match. */ 101*5796c8dcSSimon Schubert iter_type = type; 102*5796c8dcSSimon Schubert while (iter_type) 103*5796c8dcSSimon Schubert { 104*5796c8dcSSimon Schubert /* Check the name of the type. */ 105*5796c8dcSSimon Schubert if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type))) 106*5796c8dcSSimon Schubert return 1; 107*5796c8dcSSimon Schubert 108*5796c8dcSSimon Schubert if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF) 109*5796c8dcSSimon Schubert break; 110*5796c8dcSSimon Schubert 111*5796c8dcSSimon Schubert /* Peel a single typedef. If the typedef doesn't have a target 112*5796c8dcSSimon Schubert type, we use check_typedef and hope the result is ok -- it 113*5796c8dcSSimon Schubert might be for C++, where wchar_t is a built-in type. */ 114*5796c8dcSSimon Schubert if (TYPE_TARGET_TYPE (iter_type)) 115*5796c8dcSSimon Schubert iter_type = TYPE_TARGET_TYPE (iter_type); 116*5796c8dcSSimon Schubert else 117*5796c8dcSSimon Schubert iter_type = check_typedef (iter_type); 118*5796c8dcSSimon Schubert } 119*5796c8dcSSimon Schubert 120*5796c8dcSSimon Schubert if (format == 's') 121*5796c8dcSSimon Schubert { 122*5796c8dcSSimon Schubert /* Print this as a string if we can manage it. For now, no 123*5796c8dcSSimon Schubert wide character support. */ 124*5796c8dcSSimon Schubert if (TYPE_CODE (true_type) == TYPE_CODE_INT 125*5796c8dcSSimon Schubert && TYPE_LENGTH (true_type) == 1) 126*5796c8dcSSimon Schubert return 1; 127*5796c8dcSSimon Schubert } 128*5796c8dcSSimon Schubert else 129*5796c8dcSSimon Schubert { 130*5796c8dcSSimon Schubert /* If a one-byte TYPE_CODE_INT is missing the not-a-character 131*5796c8dcSSimon Schubert flag, then we treat it as text; otherwise, we assume it's 132*5796c8dcSSimon Schubert being used as data. */ 133*5796c8dcSSimon Schubert if (TYPE_CODE (true_type) == TYPE_CODE_INT 134*5796c8dcSSimon Schubert && TYPE_LENGTH (true_type) == 1 135*5796c8dcSSimon Schubert && !TYPE_NOTTEXT (true_type)) 136*5796c8dcSSimon Schubert return 1; 137*5796c8dcSSimon Schubert } 138*5796c8dcSSimon Schubert 139*5796c8dcSSimon Schubert return 0; 140*5796c8dcSSimon Schubert } 141*5796c8dcSSimon Schubert 142*5796c8dcSSimon Schubert 143*5796c8dcSSimon Schubert /* Print data of type TYPE located at VALADDR (within GDB), which came from 144*5796c8dcSSimon Schubert the inferior at address ADDRESS, onto stdio stream STREAM according to 145*5796c8dcSSimon Schubert OPTIONS. The data at VALADDR is in target byte order. 146*5796c8dcSSimon Schubert 147*5796c8dcSSimon Schubert If the data are a string pointer, returns the number of string characters 148*5796c8dcSSimon Schubert printed. */ 149*5796c8dcSSimon Schubert 150*5796c8dcSSimon Schubert int 151*5796c8dcSSimon Schubert c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset, 152*5796c8dcSSimon Schubert CORE_ADDR address, struct ui_file *stream, int recurse, 153*5796c8dcSSimon Schubert const struct value_print_options *options) 154*5796c8dcSSimon Schubert { 155*5796c8dcSSimon Schubert struct gdbarch *gdbarch = get_type_arch (type); 156*5796c8dcSSimon Schubert enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 157*5796c8dcSSimon Schubert unsigned int i = 0; /* Number of characters printed */ 158*5796c8dcSSimon Schubert unsigned len; 159*5796c8dcSSimon Schubert struct type *elttype, *unresolved_elttype; 160*5796c8dcSSimon Schubert struct type *unresolved_type = type; 161*5796c8dcSSimon Schubert unsigned eltlen; 162*5796c8dcSSimon Schubert LONGEST val; 163*5796c8dcSSimon Schubert CORE_ADDR addr; 164*5796c8dcSSimon Schubert 165*5796c8dcSSimon Schubert CHECK_TYPEDEF (type); 166*5796c8dcSSimon Schubert switch (TYPE_CODE (type)) 167*5796c8dcSSimon Schubert { 168*5796c8dcSSimon Schubert case TYPE_CODE_ARRAY: 169*5796c8dcSSimon Schubert unresolved_elttype = TYPE_TARGET_TYPE (type); 170*5796c8dcSSimon Schubert elttype = check_typedef (unresolved_elttype); 171*5796c8dcSSimon Schubert if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0) 172*5796c8dcSSimon Schubert { 173*5796c8dcSSimon Schubert eltlen = TYPE_LENGTH (elttype); 174*5796c8dcSSimon Schubert len = TYPE_LENGTH (type) / eltlen; 175*5796c8dcSSimon Schubert if (options->prettyprint_arrays) 176*5796c8dcSSimon Schubert { 177*5796c8dcSSimon Schubert print_spaces_filtered (2 + 2 * recurse, stream); 178*5796c8dcSSimon Schubert } 179*5796c8dcSSimon Schubert 180*5796c8dcSSimon Schubert /* Print arrays of textual chars with a string syntax. */ 181*5796c8dcSSimon Schubert if (textual_element_type (unresolved_elttype, options->format)) 182*5796c8dcSSimon Schubert { 183*5796c8dcSSimon Schubert /* If requested, look for the first null char and only print 184*5796c8dcSSimon Schubert elements up to it. */ 185*5796c8dcSSimon Schubert if (options->stop_print_at_null) 186*5796c8dcSSimon Schubert { 187*5796c8dcSSimon Schubert unsigned int temp_len; 188*5796c8dcSSimon Schubert 189*5796c8dcSSimon Schubert for (temp_len = 0; 190*5796c8dcSSimon Schubert (temp_len < len 191*5796c8dcSSimon Schubert && temp_len < options->print_max 192*5796c8dcSSimon Schubert && extract_unsigned_integer (valaddr + embedded_offset 193*5796c8dcSSimon Schubert + temp_len * eltlen, 194*5796c8dcSSimon Schubert eltlen, byte_order) == 0); 195*5796c8dcSSimon Schubert ++temp_len) 196*5796c8dcSSimon Schubert ; 197*5796c8dcSSimon Schubert len = temp_len; 198*5796c8dcSSimon Schubert } 199*5796c8dcSSimon Schubert 200*5796c8dcSSimon Schubert LA_PRINT_STRING (stream, unresolved_elttype, 201*5796c8dcSSimon Schubert valaddr + embedded_offset, len, 0, options); 202*5796c8dcSSimon Schubert i = len; 203*5796c8dcSSimon Schubert } 204*5796c8dcSSimon Schubert else 205*5796c8dcSSimon Schubert { 206*5796c8dcSSimon Schubert fprintf_filtered (stream, "{"); 207*5796c8dcSSimon Schubert /* If this is a virtual function table, print the 0th 208*5796c8dcSSimon Schubert entry specially, and the rest of the members normally. */ 209*5796c8dcSSimon Schubert if (cp_is_vtbl_ptr_type (elttype)) 210*5796c8dcSSimon Schubert { 211*5796c8dcSSimon Schubert i = 1; 212*5796c8dcSSimon Schubert fprintf_filtered (stream, _("%d vtable entries"), len - 1); 213*5796c8dcSSimon Schubert } 214*5796c8dcSSimon Schubert else 215*5796c8dcSSimon Schubert { 216*5796c8dcSSimon Schubert i = 0; 217*5796c8dcSSimon Schubert } 218*5796c8dcSSimon Schubert val_print_array_elements (type, valaddr + embedded_offset, address, stream, 219*5796c8dcSSimon Schubert recurse, options, i); 220*5796c8dcSSimon Schubert fprintf_filtered (stream, "}"); 221*5796c8dcSSimon Schubert } 222*5796c8dcSSimon Schubert break; 223*5796c8dcSSimon Schubert } 224*5796c8dcSSimon Schubert /* Array of unspecified length: treat like pointer to first elt. */ 225*5796c8dcSSimon Schubert addr = address; 226*5796c8dcSSimon Schubert goto print_unpacked_pointer; 227*5796c8dcSSimon Schubert 228*5796c8dcSSimon Schubert case TYPE_CODE_MEMBERPTR: 229*5796c8dcSSimon Schubert if (options->format) 230*5796c8dcSSimon Schubert { 231*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 232*5796c8dcSSimon Schubert options, 0, stream); 233*5796c8dcSSimon Schubert break; 234*5796c8dcSSimon Schubert } 235*5796c8dcSSimon Schubert cp_print_class_member (valaddr + embedded_offset, type, stream, "&"); 236*5796c8dcSSimon Schubert break; 237*5796c8dcSSimon Schubert 238*5796c8dcSSimon Schubert case TYPE_CODE_METHODPTR: 239*5796c8dcSSimon Schubert cplus_print_method_ptr (valaddr + embedded_offset, type, stream); 240*5796c8dcSSimon Schubert break; 241*5796c8dcSSimon Schubert 242*5796c8dcSSimon Schubert case TYPE_CODE_PTR: 243*5796c8dcSSimon Schubert if (options->format && options->format != 's') 244*5796c8dcSSimon Schubert { 245*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 246*5796c8dcSSimon Schubert options, 0, stream); 247*5796c8dcSSimon Schubert break; 248*5796c8dcSSimon Schubert } 249*5796c8dcSSimon Schubert if (options->vtblprint && cp_is_vtbl_ptr_type (type)) 250*5796c8dcSSimon Schubert { 251*5796c8dcSSimon Schubert /* Print the unmangled name if desired. */ 252*5796c8dcSSimon Schubert /* Print vtable entry - we only get here if we ARE using 253*5796c8dcSSimon Schubert -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */ 254*5796c8dcSSimon Schubert CORE_ADDR addr 255*5796c8dcSSimon Schubert = extract_typed_address (valaddr + embedded_offset, type); 256*5796c8dcSSimon Schubert print_function_pointer_address (gdbarch, addr, stream, 257*5796c8dcSSimon Schubert options->addressprint); 258*5796c8dcSSimon Schubert break; 259*5796c8dcSSimon Schubert } 260*5796c8dcSSimon Schubert unresolved_elttype = TYPE_TARGET_TYPE (type); 261*5796c8dcSSimon Schubert elttype = check_typedef (unresolved_elttype); 262*5796c8dcSSimon Schubert { 263*5796c8dcSSimon Schubert addr = unpack_pointer (type, valaddr + embedded_offset); 264*5796c8dcSSimon Schubert print_unpacked_pointer: 265*5796c8dcSSimon Schubert 266*5796c8dcSSimon Schubert if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) 267*5796c8dcSSimon Schubert { 268*5796c8dcSSimon Schubert /* Try to print what function it points to. */ 269*5796c8dcSSimon Schubert print_function_pointer_address (gdbarch, addr, stream, 270*5796c8dcSSimon Schubert options->addressprint); 271*5796c8dcSSimon Schubert /* Return value is irrelevant except for string pointers. */ 272*5796c8dcSSimon Schubert return (0); 273*5796c8dcSSimon Schubert } 274*5796c8dcSSimon Schubert 275*5796c8dcSSimon Schubert if (options->addressprint) 276*5796c8dcSSimon Schubert fputs_filtered (paddress (gdbarch, addr), stream); 277*5796c8dcSSimon Schubert 278*5796c8dcSSimon Schubert /* For a pointer to a textual type, also print the string 279*5796c8dcSSimon Schubert pointed to, unless pointer is null. */ 280*5796c8dcSSimon Schubert 281*5796c8dcSSimon Schubert if (textual_element_type (unresolved_elttype, options->format) 282*5796c8dcSSimon Schubert && addr != 0) 283*5796c8dcSSimon Schubert { 284*5796c8dcSSimon Schubert i = val_print_string (unresolved_elttype, addr, -1, stream, 285*5796c8dcSSimon Schubert options); 286*5796c8dcSSimon Schubert } 287*5796c8dcSSimon Schubert else if (cp_is_vtbl_member (type)) 288*5796c8dcSSimon Schubert { 289*5796c8dcSSimon Schubert /* print vtbl's nicely */ 290*5796c8dcSSimon Schubert CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset); 291*5796c8dcSSimon Schubert 292*5796c8dcSSimon Schubert struct minimal_symbol *msymbol = 293*5796c8dcSSimon Schubert lookup_minimal_symbol_by_pc (vt_address); 294*5796c8dcSSimon Schubert if ((msymbol != NULL) && 295*5796c8dcSSimon Schubert (vt_address == SYMBOL_VALUE_ADDRESS (msymbol))) 296*5796c8dcSSimon Schubert { 297*5796c8dcSSimon Schubert fputs_filtered (" <", stream); 298*5796c8dcSSimon Schubert fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream); 299*5796c8dcSSimon Schubert fputs_filtered (">", stream); 300*5796c8dcSSimon Schubert } 301*5796c8dcSSimon Schubert if (vt_address && options->vtblprint) 302*5796c8dcSSimon Schubert { 303*5796c8dcSSimon Schubert struct value *vt_val; 304*5796c8dcSSimon Schubert struct symbol *wsym = (struct symbol *) NULL; 305*5796c8dcSSimon Schubert struct type *wtype; 306*5796c8dcSSimon Schubert struct block *block = (struct block *) NULL; 307*5796c8dcSSimon Schubert int is_this_fld; 308*5796c8dcSSimon Schubert 309*5796c8dcSSimon Schubert if (msymbol != NULL) 310*5796c8dcSSimon Schubert wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block, 311*5796c8dcSSimon Schubert VAR_DOMAIN, &is_this_fld); 312*5796c8dcSSimon Schubert 313*5796c8dcSSimon Schubert if (wsym) 314*5796c8dcSSimon Schubert { 315*5796c8dcSSimon Schubert wtype = SYMBOL_TYPE (wsym); 316*5796c8dcSSimon Schubert } 317*5796c8dcSSimon Schubert else 318*5796c8dcSSimon Schubert { 319*5796c8dcSSimon Schubert wtype = unresolved_elttype; 320*5796c8dcSSimon Schubert } 321*5796c8dcSSimon Schubert vt_val = value_at (wtype, vt_address); 322*5796c8dcSSimon Schubert common_val_print (vt_val, stream, recurse + 1, options, 323*5796c8dcSSimon Schubert current_language); 324*5796c8dcSSimon Schubert if (options->pretty) 325*5796c8dcSSimon Schubert { 326*5796c8dcSSimon Schubert fprintf_filtered (stream, "\n"); 327*5796c8dcSSimon Schubert print_spaces_filtered (2 + 2 * recurse, stream); 328*5796c8dcSSimon Schubert } 329*5796c8dcSSimon Schubert } 330*5796c8dcSSimon Schubert } 331*5796c8dcSSimon Schubert 332*5796c8dcSSimon Schubert /* Return number of characters printed, including the terminating 333*5796c8dcSSimon Schubert '\0' if we reached the end. val_print_string takes care including 334*5796c8dcSSimon Schubert the terminating '\0' if necessary. */ 335*5796c8dcSSimon Schubert return i; 336*5796c8dcSSimon Schubert } 337*5796c8dcSSimon Schubert break; 338*5796c8dcSSimon Schubert 339*5796c8dcSSimon Schubert case TYPE_CODE_REF: 340*5796c8dcSSimon Schubert elttype = check_typedef (TYPE_TARGET_TYPE (type)); 341*5796c8dcSSimon Schubert if (options->addressprint) 342*5796c8dcSSimon Schubert { 343*5796c8dcSSimon Schubert CORE_ADDR addr 344*5796c8dcSSimon Schubert = extract_typed_address (valaddr + embedded_offset, type); 345*5796c8dcSSimon Schubert fprintf_filtered (stream, "@"); 346*5796c8dcSSimon Schubert fputs_filtered (paddress (gdbarch, addr), stream); 347*5796c8dcSSimon Schubert if (options->deref_ref) 348*5796c8dcSSimon Schubert fputs_filtered (": ", stream); 349*5796c8dcSSimon Schubert } 350*5796c8dcSSimon Schubert /* De-reference the reference. */ 351*5796c8dcSSimon Schubert if (options->deref_ref) 352*5796c8dcSSimon Schubert { 353*5796c8dcSSimon Schubert if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF) 354*5796c8dcSSimon Schubert { 355*5796c8dcSSimon Schubert struct value *deref_val = 356*5796c8dcSSimon Schubert value_at 357*5796c8dcSSimon Schubert (TYPE_TARGET_TYPE (type), 358*5796c8dcSSimon Schubert unpack_pointer (type, valaddr + embedded_offset)); 359*5796c8dcSSimon Schubert common_val_print (deref_val, stream, recurse, options, 360*5796c8dcSSimon Schubert current_language); 361*5796c8dcSSimon Schubert } 362*5796c8dcSSimon Schubert else 363*5796c8dcSSimon Schubert fputs_filtered ("???", stream); 364*5796c8dcSSimon Schubert } 365*5796c8dcSSimon Schubert break; 366*5796c8dcSSimon Schubert 367*5796c8dcSSimon Schubert case TYPE_CODE_UNION: 368*5796c8dcSSimon Schubert if (recurse && !options->unionprint) 369*5796c8dcSSimon Schubert { 370*5796c8dcSSimon Schubert fprintf_filtered (stream, "{...}"); 371*5796c8dcSSimon Schubert break; 372*5796c8dcSSimon Schubert } 373*5796c8dcSSimon Schubert /* Fall through. */ 374*5796c8dcSSimon Schubert case TYPE_CODE_STRUCT: 375*5796c8dcSSimon Schubert /*FIXME: Abstract this away */ 376*5796c8dcSSimon Schubert if (options->vtblprint && cp_is_vtbl_ptr_type (type)) 377*5796c8dcSSimon Schubert { 378*5796c8dcSSimon Schubert /* Print the unmangled name if desired. */ 379*5796c8dcSSimon Schubert /* Print vtable entry - we only get here if NOT using 380*5796c8dcSSimon Schubert -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */ 381*5796c8dcSSimon Schubert int offset = (embedded_offset + 382*5796c8dcSSimon Schubert TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8); 383*5796c8dcSSimon Schubert struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET); 384*5796c8dcSSimon Schubert CORE_ADDR addr 385*5796c8dcSSimon Schubert = extract_typed_address (valaddr + offset, field_type); 386*5796c8dcSSimon Schubert 387*5796c8dcSSimon Schubert print_function_pointer_address (gdbarch, addr, stream, 388*5796c8dcSSimon Schubert options->addressprint); 389*5796c8dcSSimon Schubert } 390*5796c8dcSSimon Schubert else 391*5796c8dcSSimon Schubert cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, 392*5796c8dcSSimon Schubert recurse, options, NULL, 0); 393*5796c8dcSSimon Schubert break; 394*5796c8dcSSimon Schubert 395*5796c8dcSSimon Schubert case TYPE_CODE_ENUM: 396*5796c8dcSSimon Schubert if (options->format) 397*5796c8dcSSimon Schubert { 398*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 399*5796c8dcSSimon Schubert options, 0, stream); 400*5796c8dcSSimon Schubert break; 401*5796c8dcSSimon Schubert } 402*5796c8dcSSimon Schubert len = TYPE_NFIELDS (type); 403*5796c8dcSSimon Schubert val = unpack_long (type, valaddr + embedded_offset); 404*5796c8dcSSimon Schubert for (i = 0; i < len; i++) 405*5796c8dcSSimon Schubert { 406*5796c8dcSSimon Schubert QUIT; 407*5796c8dcSSimon Schubert if (val == TYPE_FIELD_BITPOS (type, i)) 408*5796c8dcSSimon Schubert { 409*5796c8dcSSimon Schubert break; 410*5796c8dcSSimon Schubert } 411*5796c8dcSSimon Schubert } 412*5796c8dcSSimon Schubert if (i < len) 413*5796c8dcSSimon Schubert { 414*5796c8dcSSimon Schubert fputs_filtered (TYPE_FIELD_NAME (type, i), stream); 415*5796c8dcSSimon Schubert } 416*5796c8dcSSimon Schubert else 417*5796c8dcSSimon Schubert { 418*5796c8dcSSimon Schubert print_longest (stream, 'd', 0, val); 419*5796c8dcSSimon Schubert } 420*5796c8dcSSimon Schubert break; 421*5796c8dcSSimon Schubert 422*5796c8dcSSimon Schubert case TYPE_CODE_FLAGS: 423*5796c8dcSSimon Schubert if (options->format) 424*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 425*5796c8dcSSimon Schubert options, 0, stream); 426*5796c8dcSSimon Schubert else 427*5796c8dcSSimon Schubert val_print_type_code_flags (type, valaddr + embedded_offset, stream); 428*5796c8dcSSimon Schubert break; 429*5796c8dcSSimon Schubert 430*5796c8dcSSimon Schubert case TYPE_CODE_FUNC: 431*5796c8dcSSimon Schubert case TYPE_CODE_METHOD: 432*5796c8dcSSimon Schubert if (options->format) 433*5796c8dcSSimon Schubert { 434*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 435*5796c8dcSSimon Schubert options, 0, stream); 436*5796c8dcSSimon Schubert break; 437*5796c8dcSSimon Schubert } 438*5796c8dcSSimon Schubert /* FIXME, we should consider, at least for ANSI C language, eliminating 439*5796c8dcSSimon Schubert the distinction made between FUNCs and POINTERs to FUNCs. */ 440*5796c8dcSSimon Schubert fprintf_filtered (stream, "{"); 441*5796c8dcSSimon Schubert type_print (type, "", stream, -1); 442*5796c8dcSSimon Schubert fprintf_filtered (stream, "} "); 443*5796c8dcSSimon Schubert /* Try to print what function it points to, and its address. */ 444*5796c8dcSSimon Schubert print_address_demangle (gdbarch, address, stream, demangle); 445*5796c8dcSSimon Schubert break; 446*5796c8dcSSimon Schubert 447*5796c8dcSSimon Schubert case TYPE_CODE_BOOL: 448*5796c8dcSSimon Schubert if (options->format || options->output_format) 449*5796c8dcSSimon Schubert { 450*5796c8dcSSimon Schubert struct value_print_options opts = *options; 451*5796c8dcSSimon Schubert opts.format = (options->format ? options->format 452*5796c8dcSSimon Schubert : options->output_format); 453*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 454*5796c8dcSSimon Schubert &opts, 0, stream); 455*5796c8dcSSimon Schubert } 456*5796c8dcSSimon Schubert else 457*5796c8dcSSimon Schubert { 458*5796c8dcSSimon Schubert val = unpack_long (type, valaddr + embedded_offset); 459*5796c8dcSSimon Schubert if (val == 0) 460*5796c8dcSSimon Schubert fputs_filtered ("false", stream); 461*5796c8dcSSimon Schubert else if (val == 1) 462*5796c8dcSSimon Schubert fputs_filtered ("true", stream); 463*5796c8dcSSimon Schubert else 464*5796c8dcSSimon Schubert print_longest (stream, 'd', 0, val); 465*5796c8dcSSimon Schubert } 466*5796c8dcSSimon Schubert break; 467*5796c8dcSSimon Schubert 468*5796c8dcSSimon Schubert case TYPE_CODE_RANGE: 469*5796c8dcSSimon Schubert /* FIXME: create_range_type does not set the unsigned bit in a 470*5796c8dcSSimon Schubert range type (I think it probably should copy it from the target 471*5796c8dcSSimon Schubert type), so we won't print values which are too large to 472*5796c8dcSSimon Schubert fit in a signed integer correctly. */ 473*5796c8dcSSimon Schubert /* FIXME: Doesn't handle ranges of enums correctly. (Can't just 474*5796c8dcSSimon Schubert print with the target type, though, because the size of our type 475*5796c8dcSSimon Schubert and the target type might differ). */ 476*5796c8dcSSimon Schubert /* FALLTHROUGH */ 477*5796c8dcSSimon Schubert 478*5796c8dcSSimon Schubert case TYPE_CODE_INT: 479*5796c8dcSSimon Schubert if (options->format || options->output_format) 480*5796c8dcSSimon Schubert { 481*5796c8dcSSimon Schubert struct value_print_options opts = *options; 482*5796c8dcSSimon Schubert opts.format = (options->format ? options->format 483*5796c8dcSSimon Schubert : options->output_format); 484*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 485*5796c8dcSSimon Schubert &opts, 0, stream); 486*5796c8dcSSimon Schubert } 487*5796c8dcSSimon Schubert else 488*5796c8dcSSimon Schubert { 489*5796c8dcSSimon Schubert val_print_type_code_int (type, valaddr + embedded_offset, stream); 490*5796c8dcSSimon Schubert /* C and C++ has no single byte int type, char is used instead. 491*5796c8dcSSimon Schubert Since we don't know whether the value is really intended to 492*5796c8dcSSimon Schubert be used as an integer or a character, print the character 493*5796c8dcSSimon Schubert equivalent as well. */ 494*5796c8dcSSimon Schubert if (textual_element_type (unresolved_type, options->format)) 495*5796c8dcSSimon Schubert { 496*5796c8dcSSimon Schubert fputs_filtered (" ", stream); 497*5796c8dcSSimon Schubert LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset), 498*5796c8dcSSimon Schubert unresolved_type, stream); 499*5796c8dcSSimon Schubert } 500*5796c8dcSSimon Schubert } 501*5796c8dcSSimon Schubert break; 502*5796c8dcSSimon Schubert 503*5796c8dcSSimon Schubert case TYPE_CODE_CHAR: 504*5796c8dcSSimon Schubert if (options->format || options->output_format) 505*5796c8dcSSimon Schubert { 506*5796c8dcSSimon Schubert struct value_print_options opts = *options; 507*5796c8dcSSimon Schubert opts.format = (options->format ? options->format 508*5796c8dcSSimon Schubert : options->output_format); 509*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 510*5796c8dcSSimon Schubert &opts, 0, stream); 511*5796c8dcSSimon Schubert } 512*5796c8dcSSimon Schubert else 513*5796c8dcSSimon Schubert { 514*5796c8dcSSimon Schubert val = unpack_long (type, valaddr + embedded_offset); 515*5796c8dcSSimon Schubert if (TYPE_UNSIGNED (type)) 516*5796c8dcSSimon Schubert fprintf_filtered (stream, "%u", (unsigned int) val); 517*5796c8dcSSimon Schubert else 518*5796c8dcSSimon Schubert fprintf_filtered (stream, "%d", (int) val); 519*5796c8dcSSimon Schubert fputs_filtered (" ", stream); 520*5796c8dcSSimon Schubert LA_PRINT_CHAR ((unsigned char) val, unresolved_type, stream); 521*5796c8dcSSimon Schubert } 522*5796c8dcSSimon Schubert break; 523*5796c8dcSSimon Schubert 524*5796c8dcSSimon Schubert case TYPE_CODE_FLT: 525*5796c8dcSSimon Schubert if (options->format) 526*5796c8dcSSimon Schubert { 527*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 528*5796c8dcSSimon Schubert options, 0, stream); 529*5796c8dcSSimon Schubert } 530*5796c8dcSSimon Schubert else 531*5796c8dcSSimon Schubert { 532*5796c8dcSSimon Schubert print_floating (valaddr + embedded_offset, type, stream); 533*5796c8dcSSimon Schubert } 534*5796c8dcSSimon Schubert break; 535*5796c8dcSSimon Schubert 536*5796c8dcSSimon Schubert case TYPE_CODE_DECFLOAT: 537*5796c8dcSSimon Schubert if (options->format) 538*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 539*5796c8dcSSimon Schubert options, 0, stream); 540*5796c8dcSSimon Schubert else 541*5796c8dcSSimon Schubert print_decimal_floating (valaddr + embedded_offset, type, stream); 542*5796c8dcSSimon Schubert break; 543*5796c8dcSSimon Schubert 544*5796c8dcSSimon Schubert case TYPE_CODE_VOID: 545*5796c8dcSSimon Schubert fprintf_filtered (stream, "void"); 546*5796c8dcSSimon Schubert break; 547*5796c8dcSSimon Schubert 548*5796c8dcSSimon Schubert case TYPE_CODE_ERROR: 549*5796c8dcSSimon Schubert fprintf_filtered (stream, _("<error type>")); 550*5796c8dcSSimon Schubert break; 551*5796c8dcSSimon Schubert 552*5796c8dcSSimon Schubert case TYPE_CODE_UNDEF: 553*5796c8dcSSimon Schubert /* This happens (without TYPE_FLAG_STUB set) on systems which don't use 554*5796c8dcSSimon Schubert dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" 555*5796c8dcSSimon Schubert and no complete type for struct foo in that file. */ 556*5796c8dcSSimon Schubert fprintf_filtered (stream, _("<incomplete type>")); 557*5796c8dcSSimon Schubert break; 558*5796c8dcSSimon Schubert 559*5796c8dcSSimon Schubert case TYPE_CODE_COMPLEX: 560*5796c8dcSSimon Schubert if (options->format) 561*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, 562*5796c8dcSSimon Schubert TYPE_TARGET_TYPE (type), 563*5796c8dcSSimon Schubert options, 0, stream); 564*5796c8dcSSimon Schubert else 565*5796c8dcSSimon Schubert print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type), 566*5796c8dcSSimon Schubert stream); 567*5796c8dcSSimon Schubert fprintf_filtered (stream, " + "); 568*5796c8dcSSimon Schubert if (options->format) 569*5796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset 570*5796c8dcSSimon Schubert + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), 571*5796c8dcSSimon Schubert TYPE_TARGET_TYPE (type), 572*5796c8dcSSimon Schubert options, 0, stream); 573*5796c8dcSSimon Schubert else 574*5796c8dcSSimon Schubert print_floating (valaddr + embedded_offset 575*5796c8dcSSimon Schubert + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), 576*5796c8dcSSimon Schubert TYPE_TARGET_TYPE (type), 577*5796c8dcSSimon Schubert stream); 578*5796c8dcSSimon Schubert fprintf_filtered (stream, " * I"); 579*5796c8dcSSimon Schubert break; 580*5796c8dcSSimon Schubert 581*5796c8dcSSimon Schubert default: 582*5796c8dcSSimon Schubert error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type)); 583*5796c8dcSSimon Schubert } 584*5796c8dcSSimon Schubert gdb_flush (stream); 585*5796c8dcSSimon Schubert return (0); 586*5796c8dcSSimon Schubert } 587*5796c8dcSSimon Schubert 588*5796c8dcSSimon Schubert int 589*5796c8dcSSimon Schubert c_value_print (struct value *val, struct ui_file *stream, 590*5796c8dcSSimon Schubert const struct value_print_options *options) 591*5796c8dcSSimon Schubert { 592*5796c8dcSSimon Schubert struct type *type, *real_type, *val_type; 593*5796c8dcSSimon Schubert int full, top, using_enc; 594*5796c8dcSSimon Schubert struct value_print_options opts = *options; 595*5796c8dcSSimon Schubert 596*5796c8dcSSimon Schubert opts.deref_ref = 1; 597*5796c8dcSSimon Schubert 598*5796c8dcSSimon Schubert /* If it is a pointer, indicate what it points to. 599*5796c8dcSSimon Schubert 600*5796c8dcSSimon Schubert Print type also if it is a reference. 601*5796c8dcSSimon Schubert 602*5796c8dcSSimon Schubert C++: if it is a member pointer, we will take care 603*5796c8dcSSimon Schubert of that when we print it. */ 604*5796c8dcSSimon Schubert 605*5796c8dcSSimon Schubert /* Preserve the original type before stripping typedefs. We prefer 606*5796c8dcSSimon Schubert to pass down the original type when possible, but for local 607*5796c8dcSSimon Schubert checks it is better to look past the typedefs. */ 608*5796c8dcSSimon Schubert val_type = value_type (val); 609*5796c8dcSSimon Schubert type = check_typedef (val_type); 610*5796c8dcSSimon Schubert 611*5796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_PTR 612*5796c8dcSSimon Schubert || TYPE_CODE (type) == TYPE_CODE_REF) 613*5796c8dcSSimon Schubert { 614*5796c8dcSSimon Schubert /* Hack: remove (char *) for char strings. Their 615*5796c8dcSSimon Schubert type is indicated by the quoted string anyway. 616*5796c8dcSSimon Schubert (Don't use textual_element_type here; quoted strings 617*5796c8dcSSimon Schubert are always exactly (char *), (wchar_t *), or the like. */ 618*5796c8dcSSimon Schubert if (TYPE_CODE (val_type) == TYPE_CODE_PTR 619*5796c8dcSSimon Schubert && TYPE_NAME (val_type) == NULL 620*5796c8dcSSimon Schubert && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL 621*5796c8dcSSimon Schubert && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), "char") == 0 622*5796c8dcSSimon Schubert || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type))))) 623*5796c8dcSSimon Schubert { 624*5796c8dcSSimon Schubert /* Print nothing */ 625*5796c8dcSSimon Schubert } 626*5796c8dcSSimon Schubert else if (options->objectprint 627*5796c8dcSSimon Schubert && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) 628*5796c8dcSSimon Schubert { 629*5796c8dcSSimon Schubert 630*5796c8dcSSimon Schubert if (TYPE_CODE(type) == TYPE_CODE_REF) 631*5796c8dcSSimon Schubert { 632*5796c8dcSSimon Schubert /* Copy value, change to pointer, so we don't get an 633*5796c8dcSSimon Schubert * error about a non-pointer type in value_rtti_target_type 634*5796c8dcSSimon Schubert */ 635*5796c8dcSSimon Schubert struct value *temparg; 636*5796c8dcSSimon Schubert temparg=value_copy(val); 637*5796c8dcSSimon Schubert deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type))); 638*5796c8dcSSimon Schubert val=temparg; 639*5796c8dcSSimon Schubert } 640*5796c8dcSSimon Schubert /* Pointer to class, check real type of object */ 641*5796c8dcSSimon Schubert fprintf_filtered (stream, "("); 642*5796c8dcSSimon Schubert real_type = value_rtti_target_type (val, &full, &top, &using_enc); 643*5796c8dcSSimon Schubert if (real_type) 644*5796c8dcSSimon Schubert { 645*5796c8dcSSimon Schubert /* RTTI entry found */ 646*5796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_PTR) 647*5796c8dcSSimon Schubert { 648*5796c8dcSSimon Schubert /* create a pointer type pointing to the real type */ 649*5796c8dcSSimon Schubert type = lookup_pointer_type (real_type); 650*5796c8dcSSimon Schubert } 651*5796c8dcSSimon Schubert else 652*5796c8dcSSimon Schubert { 653*5796c8dcSSimon Schubert /* create a reference type referencing the real type */ 654*5796c8dcSSimon Schubert type = lookup_reference_type (real_type); 655*5796c8dcSSimon Schubert } 656*5796c8dcSSimon Schubert /* JYG: Need to adjust pointer value. */ 657*5796c8dcSSimon Schubert /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */ 658*5796c8dcSSimon Schubert value_contents_writeable (val)[0] -= top; 659*5796c8dcSSimon Schubert 660*5796c8dcSSimon Schubert /* Note: When we look up RTTI entries, we don't get any 661*5796c8dcSSimon Schubert information on const or volatile attributes */ 662*5796c8dcSSimon Schubert } 663*5796c8dcSSimon Schubert type_print (type, "", stream, -1); 664*5796c8dcSSimon Schubert fprintf_filtered (stream, ") "); 665*5796c8dcSSimon Schubert val_type = type; 666*5796c8dcSSimon Schubert } 667*5796c8dcSSimon Schubert else 668*5796c8dcSSimon Schubert { 669*5796c8dcSSimon Schubert /* normal case */ 670*5796c8dcSSimon Schubert fprintf_filtered (stream, "("); 671*5796c8dcSSimon Schubert type_print (value_type (val), "", stream, -1); 672*5796c8dcSSimon Schubert fprintf_filtered (stream, ") "); 673*5796c8dcSSimon Schubert } 674*5796c8dcSSimon Schubert } 675*5796c8dcSSimon Schubert 676*5796c8dcSSimon Schubert if (!value_initialized (val)) 677*5796c8dcSSimon Schubert fprintf_filtered (stream, " [uninitialized] "); 678*5796c8dcSSimon Schubert 679*5796c8dcSSimon Schubert if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS)) 680*5796c8dcSSimon Schubert { 681*5796c8dcSSimon Schubert /* Attempt to determine real type of object */ 682*5796c8dcSSimon Schubert real_type = value_rtti_type (val, &full, &top, &using_enc); 683*5796c8dcSSimon Schubert if (real_type) 684*5796c8dcSSimon Schubert { 685*5796c8dcSSimon Schubert /* We have RTTI information, so use it */ 686*5796c8dcSSimon Schubert val = value_full_object (val, real_type, full, top, using_enc); 687*5796c8dcSSimon Schubert fprintf_filtered (stream, "(%s%s) ", 688*5796c8dcSSimon Schubert TYPE_NAME (real_type), 689*5796c8dcSSimon Schubert full ? "" : _(" [incomplete object]")); 690*5796c8dcSSimon Schubert /* Print out object: enclosing type is same as real_type if full */ 691*5796c8dcSSimon Schubert return val_print (value_enclosing_type (val), 692*5796c8dcSSimon Schubert value_contents_all (val), 0, 693*5796c8dcSSimon Schubert value_address (val), stream, 0, 694*5796c8dcSSimon Schubert &opts, current_language); 695*5796c8dcSSimon Schubert /* Note: When we look up RTTI entries, we don't get any information on 696*5796c8dcSSimon Schubert const or volatile attributes */ 697*5796c8dcSSimon Schubert } 698*5796c8dcSSimon Schubert else if (type != check_typedef (value_enclosing_type (val))) 699*5796c8dcSSimon Schubert { 700*5796c8dcSSimon Schubert /* No RTTI information, so let's do our best */ 701*5796c8dcSSimon Schubert fprintf_filtered (stream, "(%s ?) ", 702*5796c8dcSSimon Schubert TYPE_NAME (value_enclosing_type (val))); 703*5796c8dcSSimon Schubert return val_print (value_enclosing_type (val), 704*5796c8dcSSimon Schubert value_contents_all (val), 0, 705*5796c8dcSSimon Schubert value_address (val), stream, 0, 706*5796c8dcSSimon Schubert &opts, current_language); 707*5796c8dcSSimon Schubert } 708*5796c8dcSSimon Schubert /* Otherwise, we end up at the return outside this "if" */ 709*5796c8dcSSimon Schubert } 710*5796c8dcSSimon Schubert 711*5796c8dcSSimon Schubert return val_print (val_type, value_contents_all (val), 712*5796c8dcSSimon Schubert value_embedded_offset (val), 713*5796c8dcSSimon Schubert value_address (val), 714*5796c8dcSSimon Schubert stream, 0, &opts, current_language); 715*5796c8dcSSimon Schubert } 716