15796c8dcSSimon Schubert /* Support for printing C values for GDB, the GNU debugger. 25796c8dcSSimon Schubert 35796c8dcSSimon Schubert Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 4*cf7f2e2dSJohn Marino 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007, 2008, 2009, 2010 55796c8dcSSimon Schubert Free Software Foundation, Inc. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This file is part of GDB. 85796c8dcSSimon Schubert 95796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 105796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 115796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 125796c8dcSSimon Schubert (at your option) any later version. 135796c8dcSSimon Schubert 145796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 155796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 165796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 175796c8dcSSimon Schubert GNU General Public License for more details. 185796c8dcSSimon Schubert 195796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 205796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 215796c8dcSSimon Schubert 225796c8dcSSimon Schubert #include "defs.h" 235796c8dcSSimon Schubert #include "gdb_string.h" 245796c8dcSSimon Schubert #include "symtab.h" 255796c8dcSSimon Schubert #include "gdbtypes.h" 265796c8dcSSimon Schubert #include "expression.h" 275796c8dcSSimon Schubert #include "value.h" 285796c8dcSSimon Schubert #include "valprint.h" 295796c8dcSSimon Schubert #include "language.h" 305796c8dcSSimon Schubert #include "c-lang.h" 315796c8dcSSimon Schubert #include "cp-abi.h" 325796c8dcSSimon Schubert #include "target.h" 335796c8dcSSimon Schubert 345796c8dcSSimon Schubert 355796c8dcSSimon Schubert /* Print function pointer with inferior address ADDRESS onto stdio 365796c8dcSSimon Schubert stream STREAM. */ 375796c8dcSSimon Schubert 385796c8dcSSimon Schubert static void 395796c8dcSSimon Schubert print_function_pointer_address (struct gdbarch *gdbarch, CORE_ADDR address, 405796c8dcSSimon Schubert struct ui_file *stream, int addressprint) 415796c8dcSSimon Schubert { 425796c8dcSSimon Schubert CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, address, 435796c8dcSSimon Schubert ¤t_target); 445796c8dcSSimon Schubert 455796c8dcSSimon Schubert /* If the function pointer is represented by a description, print the 465796c8dcSSimon Schubert address of the description. */ 475796c8dcSSimon Schubert if (addressprint && func_addr != address) 485796c8dcSSimon Schubert { 495796c8dcSSimon Schubert fputs_filtered ("@", stream); 505796c8dcSSimon Schubert fputs_filtered (paddress (gdbarch, address), stream); 515796c8dcSSimon Schubert fputs_filtered (": ", stream); 525796c8dcSSimon Schubert } 535796c8dcSSimon Schubert print_address_demangle (gdbarch, func_addr, stream, demangle); 545796c8dcSSimon Schubert } 555796c8dcSSimon Schubert 565796c8dcSSimon Schubert 57*cf7f2e2dSJohn Marino /* A helper for c_textual_element_type. This checks the name of the 585796c8dcSSimon Schubert typedef. This is bogus but it isn't apparent that the compiler 595796c8dcSSimon Schubert provides us the help we may need. */ 605796c8dcSSimon Schubert 615796c8dcSSimon Schubert static int 625796c8dcSSimon Schubert textual_name (const char *name) 635796c8dcSSimon Schubert { 645796c8dcSSimon Schubert return (!strcmp (name, "wchar_t") 655796c8dcSSimon Schubert || !strcmp (name, "char16_t") 665796c8dcSSimon Schubert || !strcmp (name, "char32_t")); 675796c8dcSSimon Schubert } 685796c8dcSSimon Schubert 695796c8dcSSimon Schubert /* Apply a heuristic to decide whether an array of TYPE or a pointer 705796c8dcSSimon Schubert to TYPE should be printed as a textual string. Return non-zero if 715796c8dcSSimon Schubert it should, or zero if it should be treated as an array of integers 725796c8dcSSimon Schubert or pointer to integers. FORMAT is the current format letter, 735796c8dcSSimon Schubert or 0 if none. 745796c8dcSSimon Schubert 755796c8dcSSimon Schubert We guess that "char" is a character. Explicitly signed and 765796c8dcSSimon Schubert unsigned character types are also characters. Integer data from 775796c8dcSSimon Schubert vector types is not. The user can override this by using the /s 785796c8dcSSimon Schubert format letter. */ 795796c8dcSSimon Schubert 80*cf7f2e2dSJohn Marino int 81*cf7f2e2dSJohn Marino c_textual_element_type (struct type *type, char format) 825796c8dcSSimon Schubert { 835796c8dcSSimon Schubert struct type *true_type, *iter_type; 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert if (format != 0 && format != 's') 865796c8dcSSimon Schubert return 0; 875796c8dcSSimon Schubert 885796c8dcSSimon Schubert /* We also rely on this for its side effect of setting up all the 895796c8dcSSimon Schubert typedef pointers. */ 905796c8dcSSimon Schubert true_type = check_typedef (type); 915796c8dcSSimon Schubert 925796c8dcSSimon Schubert /* TYPE_CODE_CHAR is always textual. */ 935796c8dcSSimon Schubert if (TYPE_CODE (true_type) == TYPE_CODE_CHAR) 945796c8dcSSimon Schubert return 1; 955796c8dcSSimon Schubert 965796c8dcSSimon Schubert /* Any other character-like types must be integral. */ 975796c8dcSSimon Schubert if (TYPE_CODE (true_type) != TYPE_CODE_INT) 985796c8dcSSimon Schubert return 0; 995796c8dcSSimon Schubert 1005796c8dcSSimon Schubert /* We peel typedefs one by one, looking for a match. */ 1015796c8dcSSimon Schubert iter_type = type; 1025796c8dcSSimon Schubert while (iter_type) 1035796c8dcSSimon Schubert { 1045796c8dcSSimon Schubert /* Check the name of the type. */ 1055796c8dcSSimon Schubert if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type))) 1065796c8dcSSimon Schubert return 1; 1075796c8dcSSimon Schubert 1085796c8dcSSimon Schubert if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF) 1095796c8dcSSimon Schubert break; 1105796c8dcSSimon Schubert 1115796c8dcSSimon Schubert /* Peel a single typedef. If the typedef doesn't have a target 1125796c8dcSSimon Schubert type, we use check_typedef and hope the result is ok -- it 1135796c8dcSSimon Schubert might be for C++, where wchar_t is a built-in type. */ 1145796c8dcSSimon Schubert if (TYPE_TARGET_TYPE (iter_type)) 1155796c8dcSSimon Schubert iter_type = TYPE_TARGET_TYPE (iter_type); 1165796c8dcSSimon Schubert else 1175796c8dcSSimon Schubert iter_type = check_typedef (iter_type); 1185796c8dcSSimon Schubert } 1195796c8dcSSimon Schubert 1205796c8dcSSimon Schubert if (format == 's') 1215796c8dcSSimon Schubert { 1225796c8dcSSimon Schubert /* Print this as a string if we can manage it. For now, no 1235796c8dcSSimon Schubert wide character support. */ 1245796c8dcSSimon Schubert if (TYPE_CODE (true_type) == TYPE_CODE_INT 1255796c8dcSSimon Schubert && TYPE_LENGTH (true_type) == 1) 1265796c8dcSSimon Schubert return 1; 1275796c8dcSSimon Schubert } 1285796c8dcSSimon Schubert else 1295796c8dcSSimon Schubert { 1305796c8dcSSimon Schubert /* If a one-byte TYPE_CODE_INT is missing the not-a-character 1315796c8dcSSimon Schubert flag, then we treat it as text; otherwise, we assume it's 1325796c8dcSSimon Schubert being used as data. */ 1335796c8dcSSimon Schubert if (TYPE_CODE (true_type) == TYPE_CODE_INT 1345796c8dcSSimon Schubert && TYPE_LENGTH (true_type) == 1 1355796c8dcSSimon Schubert && !TYPE_NOTTEXT (true_type)) 1365796c8dcSSimon Schubert return 1; 1375796c8dcSSimon Schubert } 1385796c8dcSSimon Schubert 1395796c8dcSSimon Schubert return 0; 1405796c8dcSSimon Schubert } 1415796c8dcSSimon Schubert 1425796c8dcSSimon Schubert 1435796c8dcSSimon Schubert /* Print data of type TYPE located at VALADDR (within GDB), which came from 1445796c8dcSSimon Schubert the inferior at address ADDRESS, onto stdio stream STREAM according to 1455796c8dcSSimon Schubert OPTIONS. The data at VALADDR is in target byte order. 1465796c8dcSSimon Schubert 1475796c8dcSSimon Schubert If the data are a string pointer, returns the number of string characters 1485796c8dcSSimon Schubert printed. */ 1495796c8dcSSimon Schubert 1505796c8dcSSimon Schubert int 1515796c8dcSSimon Schubert c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset, 1525796c8dcSSimon Schubert CORE_ADDR address, struct ui_file *stream, int recurse, 153*cf7f2e2dSJohn Marino const struct value *original_value, 1545796c8dcSSimon Schubert const struct value_print_options *options) 1555796c8dcSSimon Schubert { 1565796c8dcSSimon Schubert struct gdbarch *gdbarch = get_type_arch (type); 1575796c8dcSSimon Schubert enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 1585796c8dcSSimon Schubert unsigned int i = 0; /* Number of characters printed */ 1595796c8dcSSimon Schubert unsigned len; 1605796c8dcSSimon Schubert struct type *elttype, *unresolved_elttype; 1615796c8dcSSimon Schubert struct type *unresolved_type = type; 1625796c8dcSSimon Schubert unsigned eltlen; 1635796c8dcSSimon Schubert LONGEST val; 1645796c8dcSSimon Schubert CORE_ADDR addr; 1655796c8dcSSimon Schubert 1665796c8dcSSimon Schubert CHECK_TYPEDEF (type); 1675796c8dcSSimon Schubert switch (TYPE_CODE (type)) 1685796c8dcSSimon Schubert { 1695796c8dcSSimon Schubert case TYPE_CODE_ARRAY: 1705796c8dcSSimon Schubert unresolved_elttype = TYPE_TARGET_TYPE (type); 1715796c8dcSSimon Schubert elttype = check_typedef (unresolved_elttype); 1725796c8dcSSimon Schubert if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0) 1735796c8dcSSimon Schubert { 1745796c8dcSSimon Schubert eltlen = TYPE_LENGTH (elttype); 1755796c8dcSSimon Schubert len = TYPE_LENGTH (type) / eltlen; 1765796c8dcSSimon Schubert if (options->prettyprint_arrays) 1775796c8dcSSimon Schubert { 1785796c8dcSSimon Schubert print_spaces_filtered (2 + 2 * recurse, stream); 1795796c8dcSSimon Schubert } 1805796c8dcSSimon Schubert 181*cf7f2e2dSJohn Marino /* Print arrays of textual chars with a string syntax, as 182*cf7f2e2dSJohn Marino long as the entire array is valid. */ 183*cf7f2e2dSJohn Marino if (!TYPE_VECTOR (type) 184*cf7f2e2dSJohn Marino && c_textual_element_type (unresolved_elttype, options->format) 185*cf7f2e2dSJohn Marino && value_bits_valid (original_value, 186*cf7f2e2dSJohn Marino TARGET_CHAR_BIT * embedded_offset, 187*cf7f2e2dSJohn Marino TARGET_CHAR_BIT * TYPE_LENGTH (type))) 1885796c8dcSSimon Schubert { 1895796c8dcSSimon Schubert /* If requested, look for the first null char and only print 1905796c8dcSSimon Schubert elements up to it. */ 1915796c8dcSSimon Schubert if (options->stop_print_at_null) 1925796c8dcSSimon Schubert { 1935796c8dcSSimon Schubert unsigned int temp_len; 1945796c8dcSSimon Schubert 1955796c8dcSSimon Schubert for (temp_len = 0; 1965796c8dcSSimon Schubert (temp_len < len 1975796c8dcSSimon Schubert && temp_len < options->print_max 1985796c8dcSSimon Schubert && extract_unsigned_integer (valaddr + embedded_offset 1995796c8dcSSimon Schubert + temp_len * eltlen, 200*cf7f2e2dSJohn Marino eltlen, byte_order) != 0); 2015796c8dcSSimon Schubert ++temp_len) 2025796c8dcSSimon Schubert ; 2035796c8dcSSimon Schubert len = temp_len; 2045796c8dcSSimon Schubert } 2055796c8dcSSimon Schubert 2065796c8dcSSimon Schubert LA_PRINT_STRING (stream, unresolved_elttype, 207*cf7f2e2dSJohn Marino valaddr + embedded_offset, len, 208*cf7f2e2dSJohn Marino NULL, 0, options); 2095796c8dcSSimon Schubert i = len; 2105796c8dcSSimon Schubert } 2115796c8dcSSimon Schubert else 2125796c8dcSSimon Schubert { 2135796c8dcSSimon Schubert fprintf_filtered (stream, "{"); 2145796c8dcSSimon Schubert /* If this is a virtual function table, print the 0th 2155796c8dcSSimon Schubert entry specially, and the rest of the members normally. */ 2165796c8dcSSimon Schubert if (cp_is_vtbl_ptr_type (elttype)) 2175796c8dcSSimon Schubert { 2185796c8dcSSimon Schubert i = 1; 2195796c8dcSSimon Schubert fprintf_filtered (stream, _("%d vtable entries"), len - 1); 2205796c8dcSSimon Schubert } 2215796c8dcSSimon Schubert else 2225796c8dcSSimon Schubert { 2235796c8dcSSimon Schubert i = 0; 2245796c8dcSSimon Schubert } 225*cf7f2e2dSJohn Marino val_print_array_elements (type, valaddr + embedded_offset, 226*cf7f2e2dSJohn Marino address + embedded_offset, stream, 227*cf7f2e2dSJohn Marino recurse, original_value, options, i); 2285796c8dcSSimon Schubert fprintf_filtered (stream, "}"); 2295796c8dcSSimon Schubert } 2305796c8dcSSimon Schubert break; 2315796c8dcSSimon Schubert } 2325796c8dcSSimon Schubert /* Array of unspecified length: treat like pointer to first elt. */ 2335796c8dcSSimon Schubert addr = address; 2345796c8dcSSimon Schubert goto print_unpacked_pointer; 2355796c8dcSSimon Schubert 2365796c8dcSSimon Schubert case TYPE_CODE_MEMBERPTR: 2375796c8dcSSimon Schubert if (options->format) 2385796c8dcSSimon Schubert { 2395796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 2405796c8dcSSimon Schubert options, 0, stream); 2415796c8dcSSimon Schubert break; 2425796c8dcSSimon Schubert } 2435796c8dcSSimon Schubert cp_print_class_member (valaddr + embedded_offset, type, stream, "&"); 2445796c8dcSSimon Schubert break; 2455796c8dcSSimon Schubert 2465796c8dcSSimon Schubert case TYPE_CODE_METHODPTR: 2475796c8dcSSimon Schubert cplus_print_method_ptr (valaddr + embedded_offset, type, stream); 2485796c8dcSSimon Schubert break; 2495796c8dcSSimon Schubert 2505796c8dcSSimon Schubert case TYPE_CODE_PTR: 2515796c8dcSSimon Schubert if (options->format && options->format != 's') 2525796c8dcSSimon Schubert { 2535796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 2545796c8dcSSimon Schubert options, 0, stream); 2555796c8dcSSimon Schubert break; 2565796c8dcSSimon Schubert } 2575796c8dcSSimon Schubert if (options->vtblprint && cp_is_vtbl_ptr_type (type)) 2585796c8dcSSimon Schubert { 2595796c8dcSSimon Schubert /* Print the unmangled name if desired. */ 2605796c8dcSSimon Schubert /* Print vtable entry - we only get here if we ARE using 2615796c8dcSSimon Schubert -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */ 2625796c8dcSSimon Schubert CORE_ADDR addr 2635796c8dcSSimon Schubert = extract_typed_address (valaddr + embedded_offset, type); 264*cf7f2e2dSJohn Marino 2655796c8dcSSimon Schubert print_function_pointer_address (gdbarch, addr, stream, 2665796c8dcSSimon Schubert options->addressprint); 2675796c8dcSSimon Schubert break; 2685796c8dcSSimon Schubert } 2695796c8dcSSimon Schubert unresolved_elttype = TYPE_TARGET_TYPE (type); 2705796c8dcSSimon Schubert elttype = check_typedef (unresolved_elttype); 2715796c8dcSSimon Schubert { 2725796c8dcSSimon Schubert addr = unpack_pointer (type, valaddr + embedded_offset); 2735796c8dcSSimon Schubert print_unpacked_pointer: 2745796c8dcSSimon Schubert 2755796c8dcSSimon Schubert if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) 2765796c8dcSSimon Schubert { 2775796c8dcSSimon Schubert /* Try to print what function it points to. */ 2785796c8dcSSimon Schubert print_function_pointer_address (gdbarch, addr, stream, 2795796c8dcSSimon Schubert options->addressprint); 2805796c8dcSSimon Schubert /* Return value is irrelevant except for string pointers. */ 2815796c8dcSSimon Schubert return (0); 2825796c8dcSSimon Schubert } 2835796c8dcSSimon Schubert 2845796c8dcSSimon Schubert if (options->addressprint) 2855796c8dcSSimon Schubert fputs_filtered (paddress (gdbarch, addr), stream); 2865796c8dcSSimon Schubert 2875796c8dcSSimon Schubert /* For a pointer to a textual type, also print the string 2885796c8dcSSimon Schubert pointed to, unless pointer is null. */ 2895796c8dcSSimon Schubert 290*cf7f2e2dSJohn Marino if (c_textual_element_type (unresolved_elttype, options->format) 2915796c8dcSSimon Schubert && addr != 0) 2925796c8dcSSimon Schubert { 2935796c8dcSSimon Schubert i = val_print_string (unresolved_elttype, addr, -1, stream, 2945796c8dcSSimon Schubert options); 2955796c8dcSSimon Schubert } 2965796c8dcSSimon Schubert else if (cp_is_vtbl_member (type)) 2975796c8dcSSimon Schubert { 2985796c8dcSSimon Schubert /* print vtbl's nicely */ 2995796c8dcSSimon Schubert CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset); 3005796c8dcSSimon Schubert 3015796c8dcSSimon Schubert struct minimal_symbol *msymbol = 3025796c8dcSSimon Schubert lookup_minimal_symbol_by_pc (vt_address); 303*cf7f2e2dSJohn Marino if ((msymbol != NULL) 304*cf7f2e2dSJohn Marino && (vt_address == SYMBOL_VALUE_ADDRESS (msymbol))) 3055796c8dcSSimon Schubert { 3065796c8dcSSimon Schubert fputs_filtered (" <", stream); 3075796c8dcSSimon Schubert fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream); 3085796c8dcSSimon Schubert fputs_filtered (">", stream); 3095796c8dcSSimon Schubert } 3105796c8dcSSimon Schubert if (vt_address && options->vtblprint) 3115796c8dcSSimon Schubert { 3125796c8dcSSimon Schubert struct value *vt_val; 3135796c8dcSSimon Schubert struct symbol *wsym = (struct symbol *) NULL; 3145796c8dcSSimon Schubert struct type *wtype; 3155796c8dcSSimon Schubert struct block *block = (struct block *) NULL; 3165796c8dcSSimon Schubert int is_this_fld; 3175796c8dcSSimon Schubert 3185796c8dcSSimon Schubert if (msymbol != NULL) 3195796c8dcSSimon Schubert wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block, 3205796c8dcSSimon Schubert VAR_DOMAIN, &is_this_fld); 3215796c8dcSSimon Schubert 3225796c8dcSSimon Schubert if (wsym) 3235796c8dcSSimon Schubert { 3245796c8dcSSimon Schubert wtype = SYMBOL_TYPE (wsym); 3255796c8dcSSimon Schubert } 3265796c8dcSSimon Schubert else 3275796c8dcSSimon Schubert { 3285796c8dcSSimon Schubert wtype = unresolved_elttype; 3295796c8dcSSimon Schubert } 3305796c8dcSSimon Schubert vt_val = value_at (wtype, vt_address); 3315796c8dcSSimon Schubert common_val_print (vt_val, stream, recurse + 1, options, 3325796c8dcSSimon Schubert current_language); 3335796c8dcSSimon Schubert if (options->pretty) 3345796c8dcSSimon Schubert { 3355796c8dcSSimon Schubert fprintf_filtered (stream, "\n"); 3365796c8dcSSimon Schubert print_spaces_filtered (2 + 2 * recurse, stream); 3375796c8dcSSimon Schubert } 3385796c8dcSSimon Schubert } 3395796c8dcSSimon Schubert } 3405796c8dcSSimon Schubert 3415796c8dcSSimon Schubert /* Return number of characters printed, including the terminating 3425796c8dcSSimon Schubert '\0' if we reached the end. val_print_string takes care including 3435796c8dcSSimon Schubert the terminating '\0' if necessary. */ 3445796c8dcSSimon Schubert return i; 3455796c8dcSSimon Schubert } 3465796c8dcSSimon Schubert break; 3475796c8dcSSimon Schubert 3485796c8dcSSimon Schubert case TYPE_CODE_REF: 3495796c8dcSSimon Schubert elttype = check_typedef (TYPE_TARGET_TYPE (type)); 3505796c8dcSSimon Schubert if (options->addressprint) 3515796c8dcSSimon Schubert { 3525796c8dcSSimon Schubert CORE_ADDR addr 3535796c8dcSSimon Schubert = extract_typed_address (valaddr + embedded_offset, type); 354*cf7f2e2dSJohn Marino 3555796c8dcSSimon Schubert fprintf_filtered (stream, "@"); 3565796c8dcSSimon Schubert fputs_filtered (paddress (gdbarch, addr), stream); 3575796c8dcSSimon Schubert if (options->deref_ref) 3585796c8dcSSimon Schubert fputs_filtered (": ", stream); 3595796c8dcSSimon Schubert } 3605796c8dcSSimon Schubert /* De-reference the reference. */ 3615796c8dcSSimon Schubert if (options->deref_ref) 3625796c8dcSSimon Schubert { 3635796c8dcSSimon Schubert if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF) 3645796c8dcSSimon Schubert { 3655796c8dcSSimon Schubert struct value *deref_val = 3665796c8dcSSimon Schubert value_at 3675796c8dcSSimon Schubert (TYPE_TARGET_TYPE (type), 3685796c8dcSSimon Schubert unpack_pointer (type, valaddr + embedded_offset)); 369*cf7f2e2dSJohn Marino 3705796c8dcSSimon Schubert common_val_print (deref_val, stream, recurse, options, 3715796c8dcSSimon Schubert current_language); 3725796c8dcSSimon Schubert } 3735796c8dcSSimon Schubert else 3745796c8dcSSimon Schubert fputs_filtered ("???", stream); 3755796c8dcSSimon Schubert } 3765796c8dcSSimon Schubert break; 3775796c8dcSSimon Schubert 3785796c8dcSSimon Schubert case TYPE_CODE_UNION: 3795796c8dcSSimon Schubert if (recurse && !options->unionprint) 3805796c8dcSSimon Schubert { 3815796c8dcSSimon Schubert fprintf_filtered (stream, "{...}"); 3825796c8dcSSimon Schubert break; 3835796c8dcSSimon Schubert } 3845796c8dcSSimon Schubert /* Fall through. */ 3855796c8dcSSimon Schubert case TYPE_CODE_STRUCT: 3865796c8dcSSimon Schubert /*FIXME: Abstract this away */ 3875796c8dcSSimon Schubert if (options->vtblprint && cp_is_vtbl_ptr_type (type)) 3885796c8dcSSimon Schubert { 3895796c8dcSSimon Schubert /* Print the unmangled name if desired. */ 3905796c8dcSSimon Schubert /* Print vtable entry - we only get here if NOT using 3915796c8dcSSimon Schubert -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */ 3925796c8dcSSimon Schubert int offset = (embedded_offset + 3935796c8dcSSimon Schubert TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8); 3945796c8dcSSimon Schubert struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET); 3955796c8dcSSimon Schubert CORE_ADDR addr 3965796c8dcSSimon Schubert = extract_typed_address (valaddr + offset, field_type); 3975796c8dcSSimon Schubert 3985796c8dcSSimon Schubert print_function_pointer_address (gdbarch, addr, stream, 3995796c8dcSSimon Schubert options->addressprint); 4005796c8dcSSimon Schubert } 4015796c8dcSSimon Schubert else 402*cf7f2e2dSJohn Marino cp_print_value_fields_rtti (type, valaddr, 403*cf7f2e2dSJohn Marino embedded_offset, address, stream, 404*cf7f2e2dSJohn Marino recurse, original_value, options, NULL, 0); 4055796c8dcSSimon Schubert break; 4065796c8dcSSimon Schubert 4075796c8dcSSimon Schubert case TYPE_CODE_ENUM: 4085796c8dcSSimon Schubert if (options->format) 4095796c8dcSSimon Schubert { 4105796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 4115796c8dcSSimon Schubert options, 0, stream); 4125796c8dcSSimon Schubert break; 4135796c8dcSSimon Schubert } 4145796c8dcSSimon Schubert len = TYPE_NFIELDS (type); 4155796c8dcSSimon Schubert val = unpack_long (type, valaddr + embedded_offset); 4165796c8dcSSimon Schubert for (i = 0; i < len; i++) 4175796c8dcSSimon Schubert { 4185796c8dcSSimon Schubert QUIT; 4195796c8dcSSimon Schubert if (val == TYPE_FIELD_BITPOS (type, i)) 4205796c8dcSSimon Schubert { 4215796c8dcSSimon Schubert break; 4225796c8dcSSimon Schubert } 4235796c8dcSSimon Schubert } 4245796c8dcSSimon Schubert if (i < len) 4255796c8dcSSimon Schubert { 4265796c8dcSSimon Schubert fputs_filtered (TYPE_FIELD_NAME (type, i), stream); 4275796c8dcSSimon Schubert } 4285796c8dcSSimon Schubert else 4295796c8dcSSimon Schubert { 4305796c8dcSSimon Schubert print_longest (stream, 'd', 0, val); 4315796c8dcSSimon Schubert } 4325796c8dcSSimon Schubert break; 4335796c8dcSSimon Schubert 4345796c8dcSSimon Schubert case TYPE_CODE_FLAGS: 4355796c8dcSSimon Schubert if (options->format) 4365796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 4375796c8dcSSimon Schubert options, 0, stream); 4385796c8dcSSimon Schubert else 4395796c8dcSSimon Schubert val_print_type_code_flags (type, valaddr + embedded_offset, stream); 4405796c8dcSSimon Schubert break; 4415796c8dcSSimon Schubert 4425796c8dcSSimon Schubert case TYPE_CODE_FUNC: 4435796c8dcSSimon Schubert case TYPE_CODE_METHOD: 4445796c8dcSSimon Schubert if (options->format) 4455796c8dcSSimon Schubert { 4465796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 4475796c8dcSSimon Schubert options, 0, stream); 4485796c8dcSSimon Schubert break; 4495796c8dcSSimon Schubert } 4505796c8dcSSimon Schubert /* FIXME, we should consider, at least for ANSI C language, eliminating 4515796c8dcSSimon Schubert the distinction made between FUNCs and POINTERs to FUNCs. */ 4525796c8dcSSimon Schubert fprintf_filtered (stream, "{"); 4535796c8dcSSimon Schubert type_print (type, "", stream, -1); 4545796c8dcSSimon Schubert fprintf_filtered (stream, "} "); 4555796c8dcSSimon Schubert /* Try to print what function it points to, and its address. */ 4565796c8dcSSimon Schubert print_address_demangle (gdbarch, address, stream, demangle); 4575796c8dcSSimon Schubert break; 4585796c8dcSSimon Schubert 4595796c8dcSSimon Schubert case TYPE_CODE_BOOL: 4605796c8dcSSimon Schubert if (options->format || options->output_format) 4615796c8dcSSimon Schubert { 4625796c8dcSSimon Schubert struct value_print_options opts = *options; 4635796c8dcSSimon Schubert opts.format = (options->format ? options->format 4645796c8dcSSimon Schubert : options->output_format); 4655796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 4665796c8dcSSimon Schubert &opts, 0, stream); 4675796c8dcSSimon Schubert } 4685796c8dcSSimon Schubert else 4695796c8dcSSimon Schubert { 4705796c8dcSSimon Schubert val = unpack_long (type, valaddr + embedded_offset); 4715796c8dcSSimon Schubert if (val == 0) 4725796c8dcSSimon Schubert fputs_filtered ("false", stream); 4735796c8dcSSimon Schubert else if (val == 1) 4745796c8dcSSimon Schubert fputs_filtered ("true", stream); 4755796c8dcSSimon Schubert else 4765796c8dcSSimon Schubert print_longest (stream, 'd', 0, val); 4775796c8dcSSimon Schubert } 4785796c8dcSSimon Schubert break; 4795796c8dcSSimon Schubert 4805796c8dcSSimon Schubert case TYPE_CODE_RANGE: 4815796c8dcSSimon Schubert /* FIXME: create_range_type does not set the unsigned bit in a 4825796c8dcSSimon Schubert range type (I think it probably should copy it from the target 4835796c8dcSSimon Schubert type), so we won't print values which are too large to 4845796c8dcSSimon Schubert fit in a signed integer correctly. */ 4855796c8dcSSimon Schubert /* FIXME: Doesn't handle ranges of enums correctly. (Can't just 4865796c8dcSSimon Schubert print with the target type, though, because the size of our type 4875796c8dcSSimon Schubert and the target type might differ). */ 4885796c8dcSSimon Schubert /* FALLTHROUGH */ 4895796c8dcSSimon Schubert 4905796c8dcSSimon Schubert case TYPE_CODE_INT: 4915796c8dcSSimon Schubert if (options->format || options->output_format) 4925796c8dcSSimon Schubert { 4935796c8dcSSimon Schubert struct value_print_options opts = *options; 494*cf7f2e2dSJohn Marino 4955796c8dcSSimon Schubert opts.format = (options->format ? options->format 4965796c8dcSSimon Schubert : options->output_format); 4975796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 4985796c8dcSSimon Schubert &opts, 0, stream); 4995796c8dcSSimon Schubert } 5005796c8dcSSimon Schubert else 5015796c8dcSSimon Schubert { 5025796c8dcSSimon Schubert val_print_type_code_int (type, valaddr + embedded_offset, stream); 5035796c8dcSSimon Schubert /* C and C++ has no single byte int type, char is used instead. 5045796c8dcSSimon Schubert Since we don't know whether the value is really intended to 5055796c8dcSSimon Schubert be used as an integer or a character, print the character 5065796c8dcSSimon Schubert equivalent as well. */ 507*cf7f2e2dSJohn Marino if (c_textual_element_type (unresolved_type, options->format)) 5085796c8dcSSimon Schubert { 5095796c8dcSSimon Schubert fputs_filtered (" ", stream); 5105796c8dcSSimon Schubert LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset), 5115796c8dcSSimon Schubert unresolved_type, stream); 5125796c8dcSSimon Schubert } 5135796c8dcSSimon Schubert } 5145796c8dcSSimon Schubert break; 5155796c8dcSSimon Schubert 5165796c8dcSSimon Schubert case TYPE_CODE_CHAR: 5175796c8dcSSimon Schubert if (options->format || options->output_format) 5185796c8dcSSimon Schubert { 5195796c8dcSSimon Schubert struct value_print_options opts = *options; 5205796c8dcSSimon Schubert opts.format = (options->format ? options->format 5215796c8dcSSimon Schubert : options->output_format); 5225796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 5235796c8dcSSimon Schubert &opts, 0, stream); 5245796c8dcSSimon Schubert } 5255796c8dcSSimon Schubert else 5265796c8dcSSimon Schubert { 5275796c8dcSSimon Schubert val = unpack_long (type, valaddr + embedded_offset); 5285796c8dcSSimon Schubert if (TYPE_UNSIGNED (type)) 5295796c8dcSSimon Schubert fprintf_filtered (stream, "%u", (unsigned int) val); 5305796c8dcSSimon Schubert else 5315796c8dcSSimon Schubert fprintf_filtered (stream, "%d", (int) val); 5325796c8dcSSimon Schubert fputs_filtered (" ", stream); 5335796c8dcSSimon Schubert LA_PRINT_CHAR ((unsigned char) val, unresolved_type, stream); 5345796c8dcSSimon Schubert } 5355796c8dcSSimon Schubert break; 5365796c8dcSSimon Schubert 5375796c8dcSSimon Schubert case TYPE_CODE_FLT: 5385796c8dcSSimon Schubert if (options->format) 5395796c8dcSSimon Schubert { 5405796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 5415796c8dcSSimon Schubert options, 0, stream); 5425796c8dcSSimon Schubert } 5435796c8dcSSimon Schubert else 5445796c8dcSSimon Schubert { 5455796c8dcSSimon Schubert print_floating (valaddr + embedded_offset, type, stream); 5465796c8dcSSimon Schubert } 5475796c8dcSSimon Schubert break; 5485796c8dcSSimon Schubert 5495796c8dcSSimon Schubert case TYPE_CODE_DECFLOAT: 5505796c8dcSSimon Schubert if (options->format) 5515796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, type, 5525796c8dcSSimon Schubert options, 0, stream); 5535796c8dcSSimon Schubert else 5545796c8dcSSimon Schubert print_decimal_floating (valaddr + embedded_offset, type, stream); 5555796c8dcSSimon Schubert break; 5565796c8dcSSimon Schubert 5575796c8dcSSimon Schubert case TYPE_CODE_VOID: 5585796c8dcSSimon Schubert fprintf_filtered (stream, "void"); 5595796c8dcSSimon Schubert break; 5605796c8dcSSimon Schubert 5615796c8dcSSimon Schubert case TYPE_CODE_ERROR: 562*cf7f2e2dSJohn Marino fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type)); 5635796c8dcSSimon Schubert break; 5645796c8dcSSimon Schubert 5655796c8dcSSimon Schubert case TYPE_CODE_UNDEF: 5665796c8dcSSimon Schubert /* This happens (without TYPE_FLAG_STUB set) on systems which don't use 5675796c8dcSSimon Schubert dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" 5685796c8dcSSimon Schubert and no complete type for struct foo in that file. */ 5695796c8dcSSimon Schubert fprintf_filtered (stream, _("<incomplete type>")); 5705796c8dcSSimon Schubert break; 5715796c8dcSSimon Schubert 5725796c8dcSSimon Schubert case TYPE_CODE_COMPLEX: 5735796c8dcSSimon Schubert if (options->format) 5745796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset, 5755796c8dcSSimon Schubert TYPE_TARGET_TYPE (type), 5765796c8dcSSimon Schubert options, 0, stream); 5775796c8dcSSimon Schubert else 5785796c8dcSSimon Schubert print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type), 5795796c8dcSSimon Schubert stream); 5805796c8dcSSimon Schubert fprintf_filtered (stream, " + "); 5815796c8dcSSimon Schubert if (options->format) 5825796c8dcSSimon Schubert print_scalar_formatted (valaddr + embedded_offset 5835796c8dcSSimon Schubert + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), 5845796c8dcSSimon Schubert TYPE_TARGET_TYPE (type), 5855796c8dcSSimon Schubert options, 0, stream); 5865796c8dcSSimon Schubert else 5875796c8dcSSimon Schubert print_floating (valaddr + embedded_offset 5885796c8dcSSimon Schubert + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), 5895796c8dcSSimon Schubert TYPE_TARGET_TYPE (type), 5905796c8dcSSimon Schubert stream); 5915796c8dcSSimon Schubert fprintf_filtered (stream, " * I"); 5925796c8dcSSimon Schubert break; 5935796c8dcSSimon Schubert 5945796c8dcSSimon Schubert default: 5955796c8dcSSimon Schubert error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type)); 5965796c8dcSSimon Schubert } 5975796c8dcSSimon Schubert gdb_flush (stream); 5985796c8dcSSimon Schubert return (0); 5995796c8dcSSimon Schubert } 6005796c8dcSSimon Schubert 6015796c8dcSSimon Schubert int 6025796c8dcSSimon Schubert c_value_print (struct value *val, struct ui_file *stream, 6035796c8dcSSimon Schubert const struct value_print_options *options) 6045796c8dcSSimon Schubert { 6055796c8dcSSimon Schubert struct type *type, *real_type, *val_type; 6065796c8dcSSimon Schubert int full, top, using_enc; 6075796c8dcSSimon Schubert struct value_print_options opts = *options; 6085796c8dcSSimon Schubert 6095796c8dcSSimon Schubert opts.deref_ref = 1; 6105796c8dcSSimon Schubert 6115796c8dcSSimon Schubert /* If it is a pointer, indicate what it points to. 6125796c8dcSSimon Schubert 6135796c8dcSSimon Schubert Print type also if it is a reference. 6145796c8dcSSimon Schubert 6155796c8dcSSimon Schubert C++: if it is a member pointer, we will take care 6165796c8dcSSimon Schubert of that when we print it. */ 6175796c8dcSSimon Schubert 6185796c8dcSSimon Schubert /* Preserve the original type before stripping typedefs. We prefer 6195796c8dcSSimon Schubert to pass down the original type when possible, but for local 6205796c8dcSSimon Schubert checks it is better to look past the typedefs. */ 6215796c8dcSSimon Schubert val_type = value_type (val); 6225796c8dcSSimon Schubert type = check_typedef (val_type); 6235796c8dcSSimon Schubert 6245796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_PTR 6255796c8dcSSimon Schubert || TYPE_CODE (type) == TYPE_CODE_REF) 6265796c8dcSSimon Schubert { 6275796c8dcSSimon Schubert /* Hack: remove (char *) for char strings. Their 6285796c8dcSSimon Schubert type is indicated by the quoted string anyway. 629*cf7f2e2dSJohn Marino (Don't use c_textual_element_type here; quoted strings 6305796c8dcSSimon Schubert are always exactly (char *), (wchar_t *), or the like. */ 6315796c8dcSSimon Schubert if (TYPE_CODE (val_type) == TYPE_CODE_PTR 6325796c8dcSSimon Schubert && TYPE_NAME (val_type) == NULL 6335796c8dcSSimon Schubert && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL 6345796c8dcSSimon Schubert && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), "char") == 0 6355796c8dcSSimon Schubert || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type))))) 6365796c8dcSSimon Schubert { 6375796c8dcSSimon Schubert /* Print nothing */ 6385796c8dcSSimon Schubert } 6395796c8dcSSimon Schubert else if (options->objectprint 6405796c8dcSSimon Schubert && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) 6415796c8dcSSimon Schubert { 6425796c8dcSSimon Schubert 6435796c8dcSSimon Schubert if (TYPE_CODE(type) == TYPE_CODE_REF) 6445796c8dcSSimon Schubert { 6455796c8dcSSimon Schubert /* Copy value, change to pointer, so we don't get an 6465796c8dcSSimon Schubert * error about a non-pointer type in value_rtti_target_type 6475796c8dcSSimon Schubert */ 6485796c8dcSSimon Schubert struct value *temparg; 6495796c8dcSSimon Schubert temparg=value_copy(val); 6505796c8dcSSimon Schubert deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type))); 6515796c8dcSSimon Schubert val=temparg; 6525796c8dcSSimon Schubert } 6535796c8dcSSimon Schubert /* Pointer to class, check real type of object */ 6545796c8dcSSimon Schubert fprintf_filtered (stream, "("); 6555796c8dcSSimon Schubert real_type = value_rtti_target_type (val, &full, &top, &using_enc); 6565796c8dcSSimon Schubert if (real_type) 6575796c8dcSSimon Schubert { 6585796c8dcSSimon Schubert /* RTTI entry found */ 6595796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_PTR) 6605796c8dcSSimon Schubert { 6615796c8dcSSimon Schubert /* create a pointer type pointing to the real type */ 6625796c8dcSSimon Schubert type = lookup_pointer_type (real_type); 6635796c8dcSSimon Schubert } 6645796c8dcSSimon Schubert else 6655796c8dcSSimon Schubert { 6665796c8dcSSimon Schubert /* create a reference type referencing the real type */ 6675796c8dcSSimon Schubert type = lookup_reference_type (real_type); 6685796c8dcSSimon Schubert } 6695796c8dcSSimon Schubert /* JYG: Need to adjust pointer value. */ 6705796c8dcSSimon Schubert /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */ 6715796c8dcSSimon Schubert value_contents_writeable (val)[0] -= top; 6725796c8dcSSimon Schubert 6735796c8dcSSimon Schubert /* Note: When we look up RTTI entries, we don't get any 6745796c8dcSSimon Schubert information on const or volatile attributes */ 6755796c8dcSSimon Schubert } 6765796c8dcSSimon Schubert type_print (type, "", stream, -1); 6775796c8dcSSimon Schubert fprintf_filtered (stream, ") "); 6785796c8dcSSimon Schubert val_type = type; 6795796c8dcSSimon Schubert } 6805796c8dcSSimon Schubert else 6815796c8dcSSimon Schubert { 6825796c8dcSSimon Schubert /* normal case */ 6835796c8dcSSimon Schubert fprintf_filtered (stream, "("); 6845796c8dcSSimon Schubert type_print (value_type (val), "", stream, -1); 6855796c8dcSSimon Schubert fprintf_filtered (stream, ") "); 6865796c8dcSSimon Schubert } 6875796c8dcSSimon Schubert } 6885796c8dcSSimon Schubert 6895796c8dcSSimon Schubert if (!value_initialized (val)) 6905796c8dcSSimon Schubert fprintf_filtered (stream, " [uninitialized] "); 6915796c8dcSSimon Schubert 6925796c8dcSSimon Schubert if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS)) 6935796c8dcSSimon Schubert { 6945796c8dcSSimon Schubert /* Attempt to determine real type of object */ 6955796c8dcSSimon Schubert real_type = value_rtti_type (val, &full, &top, &using_enc); 6965796c8dcSSimon Schubert if (real_type) 6975796c8dcSSimon Schubert { 6985796c8dcSSimon Schubert /* We have RTTI information, so use it */ 6995796c8dcSSimon Schubert val = value_full_object (val, real_type, full, top, using_enc); 7005796c8dcSSimon Schubert fprintf_filtered (stream, "(%s%s) ", 7015796c8dcSSimon Schubert TYPE_NAME (real_type), 7025796c8dcSSimon Schubert full ? "" : _(" [incomplete object]")); 7035796c8dcSSimon Schubert /* Print out object: enclosing type is same as real_type if full */ 7045796c8dcSSimon Schubert return val_print (value_enclosing_type (val), 705*cf7f2e2dSJohn Marino value_contents_for_printing (val), 0, 7065796c8dcSSimon Schubert value_address (val), stream, 0, 707*cf7f2e2dSJohn Marino val, &opts, current_language); 7085796c8dcSSimon Schubert /* Note: When we look up RTTI entries, we don't get any information on 7095796c8dcSSimon Schubert const or volatile attributes */ 7105796c8dcSSimon Schubert } 7115796c8dcSSimon Schubert else if (type != check_typedef (value_enclosing_type (val))) 7125796c8dcSSimon Schubert { 7135796c8dcSSimon Schubert /* No RTTI information, so let's do our best */ 7145796c8dcSSimon Schubert fprintf_filtered (stream, "(%s ?) ", 7155796c8dcSSimon Schubert TYPE_NAME (value_enclosing_type (val))); 7165796c8dcSSimon Schubert return val_print (value_enclosing_type (val), 717*cf7f2e2dSJohn Marino value_contents_for_printing (val), 0, 7185796c8dcSSimon Schubert value_address (val), stream, 0, 719*cf7f2e2dSJohn Marino val, &opts, current_language); 7205796c8dcSSimon Schubert } 7215796c8dcSSimon Schubert /* Otherwise, we end up at the return outside this "if" */ 7225796c8dcSSimon Schubert } 7235796c8dcSSimon Schubert 724*cf7f2e2dSJohn Marino return val_print (val_type, value_contents_for_printing (val), 7255796c8dcSSimon Schubert value_embedded_offset (val), 7265796c8dcSSimon Schubert value_address (val), 727*cf7f2e2dSJohn Marino stream, 0, 728*cf7f2e2dSJohn Marino val, &opts, current_language); 7295796c8dcSSimon Schubert } 730