GDB (API)
|
00001 /* Support for printing C values for GDB, the GNU debugger. 00002 00003 Copyright (C) 1986-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "gdb_string.h" 00022 #include "symtab.h" 00023 #include "gdbtypes.h" 00024 #include "expression.h" 00025 #include "value.h" 00026 #include "valprint.h" 00027 #include "language.h" 00028 #include "c-lang.h" 00029 #include "cp-abi.h" 00030 #include "target.h" 00031 00032 00033 /* A helper for c_textual_element_type. This checks the name of the 00034 typedef. This is bogus but it isn't apparent that the compiler 00035 provides us the help we may need. */ 00036 00037 static int 00038 textual_name (const char *name) 00039 { 00040 return (!strcmp (name, "wchar_t") 00041 || !strcmp (name, "char16_t") 00042 || !strcmp (name, "char32_t")); 00043 } 00044 00045 /* Apply a heuristic to decide whether an array of TYPE or a pointer 00046 to TYPE should be printed as a textual string. Return non-zero if 00047 it should, or zero if it should be treated as an array of integers 00048 or pointer to integers. FORMAT is the current format letter, or 0 00049 if none. 00050 00051 We guess that "char" is a character. Explicitly signed and 00052 unsigned character types are also characters. Integer data from 00053 vector types is not. The user can override this by using the /s 00054 format letter. */ 00055 00056 int 00057 c_textual_element_type (struct type *type, char format) 00058 { 00059 struct type *true_type, *iter_type; 00060 00061 if (format != 0 && format != 's') 00062 return 0; 00063 00064 /* We also rely on this for its side effect of setting up all the 00065 typedef pointers. */ 00066 true_type = check_typedef (type); 00067 00068 /* TYPE_CODE_CHAR is always textual. */ 00069 if (TYPE_CODE (true_type) == TYPE_CODE_CHAR) 00070 return 1; 00071 00072 /* Any other character-like types must be integral. */ 00073 if (TYPE_CODE (true_type) != TYPE_CODE_INT) 00074 return 0; 00075 00076 /* We peel typedefs one by one, looking for a match. */ 00077 iter_type = type; 00078 while (iter_type) 00079 { 00080 /* Check the name of the type. */ 00081 if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type))) 00082 return 1; 00083 00084 if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF) 00085 break; 00086 00087 /* Peel a single typedef. If the typedef doesn't have a target 00088 type, we use check_typedef and hope the result is ok -- it 00089 might be for C++, where wchar_t is a built-in type. */ 00090 if (TYPE_TARGET_TYPE (iter_type)) 00091 iter_type = TYPE_TARGET_TYPE (iter_type); 00092 else 00093 iter_type = check_typedef (iter_type); 00094 } 00095 00096 if (format == 's') 00097 { 00098 /* Print this as a string if we can manage it. For now, no wide 00099 character support. */ 00100 if (TYPE_CODE (true_type) == TYPE_CODE_INT 00101 && TYPE_LENGTH (true_type) == 1) 00102 return 1; 00103 } 00104 else 00105 { 00106 /* If a one-byte TYPE_CODE_INT is missing the not-a-character 00107 flag, then we treat it as text; otherwise, we assume it's 00108 being used as data. */ 00109 if (TYPE_CODE (true_type) == TYPE_CODE_INT 00110 && TYPE_LENGTH (true_type) == 1 00111 && !TYPE_NOTTEXT (true_type)) 00112 return 1; 00113 } 00114 00115 return 0; 00116 } 00117 00118 /* Decorations for C. */ 00119 00120 static const struct generic_val_print_decorations c_decorations = 00121 { 00122 "", 00123 " + ", 00124 " * I", 00125 "true", 00126 "false", 00127 "void" 00128 }; 00129 00130 /* See val_print for a description of the various parameters of this 00131 function; they are identical. */ 00132 00133 void 00134 c_val_print (struct type *type, const gdb_byte *valaddr, 00135 int embedded_offset, CORE_ADDR address, 00136 struct ui_file *stream, int recurse, 00137 const struct value *original_value, 00138 const struct value_print_options *options) 00139 { 00140 struct gdbarch *gdbarch = get_type_arch (type); 00141 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 00142 unsigned int i = 0; /* Number of characters printed. */ 00143 unsigned len; 00144 struct type *elttype, *unresolved_elttype; 00145 struct type *unresolved_type = type; 00146 unsigned eltlen; 00147 CORE_ADDR addr; 00148 00149 CHECK_TYPEDEF (type); 00150 switch (TYPE_CODE (type)) 00151 { 00152 case TYPE_CODE_ARRAY: 00153 unresolved_elttype = TYPE_TARGET_TYPE (type); 00154 elttype = check_typedef (unresolved_elttype); 00155 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0) 00156 { 00157 LONGEST low_bound, high_bound; 00158 00159 if (!get_array_bounds (type, &low_bound, &high_bound)) 00160 error (_("Could not determine the array high bound")); 00161 00162 eltlen = TYPE_LENGTH (elttype); 00163 len = high_bound - low_bound + 1; 00164 if (options->prettyformat_arrays) 00165 { 00166 print_spaces_filtered (2 + 2 * recurse, stream); 00167 } 00168 00169 /* Print arrays of textual chars with a string syntax, as 00170 long as the entire array is valid. */ 00171 if (c_textual_element_type (unresolved_elttype, 00172 options->format) 00173 && value_bytes_available (original_value, embedded_offset, 00174 TYPE_LENGTH (type)) 00175 && value_bits_valid (original_value, 00176 TARGET_CHAR_BIT * embedded_offset, 00177 TARGET_CHAR_BIT * TYPE_LENGTH (type))) 00178 { 00179 int force_ellipses = 0; 00180 00181 /* If requested, look for the first null char and only 00182 print elements up to it. */ 00183 if (options->stop_print_at_null) 00184 { 00185 unsigned int temp_len; 00186 00187 for (temp_len = 0; 00188 (temp_len < len 00189 && temp_len < options->print_max 00190 && extract_unsigned_integer (valaddr + embedded_offset 00191 + temp_len * eltlen, 00192 eltlen, byte_order) != 0); 00193 ++temp_len) 00194 ; 00195 00196 /* Force LA_PRINT_STRING to print ellipses if 00197 we've printed the maximum characters and 00198 the next character is not \000. */ 00199 if (temp_len == options->print_max && temp_len < len) 00200 { 00201 ULONGEST val 00202 = extract_unsigned_integer (valaddr + embedded_offset 00203 + temp_len * eltlen, 00204 eltlen, byte_order); 00205 if (val != 0) 00206 force_ellipses = 1; 00207 } 00208 00209 len = temp_len; 00210 } 00211 00212 LA_PRINT_STRING (stream, unresolved_elttype, 00213 valaddr + embedded_offset, len, 00214 NULL, force_ellipses, options); 00215 i = len; 00216 } 00217 else 00218 { 00219 fprintf_filtered (stream, "{"); 00220 /* If this is a virtual function table, print the 0th 00221 entry specially, and the rest of the members 00222 normally. */ 00223 if (cp_is_vtbl_ptr_type (elttype)) 00224 { 00225 i = 1; 00226 fprintf_filtered (stream, _("%d vtable entries"), 00227 len - 1); 00228 } 00229 else 00230 { 00231 i = 0; 00232 } 00233 val_print_array_elements (type, valaddr, embedded_offset, 00234 address, stream, 00235 recurse, original_value, options, i); 00236 fprintf_filtered (stream, "}"); 00237 } 00238 break; 00239 } 00240 /* Array of unspecified length: treat like pointer to first 00241 elt. */ 00242 addr = address + embedded_offset; 00243 goto print_unpacked_pointer; 00244 00245 case TYPE_CODE_METHODPTR: 00246 cplus_print_method_ptr (valaddr + embedded_offset, type, stream); 00247 break; 00248 00249 case TYPE_CODE_PTR: 00250 if (options->format && options->format != 's') 00251 { 00252 val_print_scalar_formatted (type, valaddr, embedded_offset, 00253 original_value, options, 0, stream); 00254 break; 00255 } 00256 if (options->vtblprint && cp_is_vtbl_ptr_type (type)) 00257 { 00258 /* Print the unmangled name if desired. */ 00259 /* Print vtable entry - we only get here if we ARE using 00260 -fvtable_thunks. (Otherwise, look under 00261 TYPE_CODE_STRUCT.) */ 00262 CORE_ADDR addr 00263 = extract_typed_address (valaddr + embedded_offset, type); 00264 00265 print_function_pointer_address (options, gdbarch, addr, stream); 00266 break; 00267 } 00268 unresolved_elttype = TYPE_TARGET_TYPE (type); 00269 elttype = check_typedef (unresolved_elttype); 00270 { 00271 int want_space; 00272 00273 addr = unpack_pointer (type, valaddr + embedded_offset); 00274 print_unpacked_pointer: 00275 00276 want_space = 0; 00277 00278 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) 00279 { 00280 /* Try to print what function it points to. */ 00281 print_function_pointer_address (options, gdbarch, addr, stream); 00282 return; 00283 } 00284 00285 if (options->symbol_print) 00286 want_space = print_address_demangle (options, gdbarch, addr, 00287 stream, demangle); 00288 else if (options->addressprint) 00289 { 00290 fputs_filtered (paddress (gdbarch, addr), stream); 00291 want_space = 1; 00292 } 00293 00294 /* For a pointer to a textual type, also print the string 00295 pointed to, unless pointer is null. */ 00296 00297 if (c_textual_element_type (unresolved_elttype, 00298 options->format) 00299 && addr != 0) 00300 { 00301 if (want_space) 00302 fputs_filtered (" ", stream); 00303 i = val_print_string (unresolved_elttype, NULL, 00304 addr, -1, 00305 stream, options); 00306 } 00307 else if (cp_is_vtbl_member (type)) 00308 { 00309 /* Print vtbl's nicely. */ 00310 CORE_ADDR vt_address = unpack_pointer (type, 00311 valaddr 00312 + embedded_offset); 00313 struct bound_minimal_symbol msymbol = 00314 lookup_minimal_symbol_by_pc (vt_address); 00315 00316 /* If 'symbol_print' is set, we did the work above. */ 00317 if (!options->symbol_print 00318 && (msymbol.minsym != NULL) 00319 && (vt_address == SYMBOL_VALUE_ADDRESS (msymbol.minsym))) 00320 { 00321 if (want_space) 00322 fputs_filtered (" ", stream); 00323 fputs_filtered (" <", stream); 00324 fputs_filtered (SYMBOL_PRINT_NAME (msymbol.minsym), stream); 00325 fputs_filtered (">", stream); 00326 want_space = 1; 00327 } 00328 00329 if (vt_address && options->vtblprint) 00330 { 00331 struct value *vt_val; 00332 struct symbol *wsym = (struct symbol *) NULL; 00333 struct type *wtype; 00334 struct block *block = (struct block *) NULL; 00335 struct field_of_this_result is_this_fld; 00336 00337 if (want_space) 00338 fputs_filtered (" ", stream); 00339 00340 if (msymbol.minsym != NULL) 00341 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol.minsym), 00342 block, VAR_DOMAIN, 00343 &is_this_fld); 00344 00345 if (wsym) 00346 { 00347 wtype = SYMBOL_TYPE (wsym); 00348 } 00349 else 00350 { 00351 wtype = unresolved_elttype; 00352 } 00353 vt_val = value_at (wtype, vt_address); 00354 common_val_print (vt_val, stream, recurse + 1, 00355 options, current_language); 00356 if (options->prettyformat) 00357 { 00358 fprintf_filtered (stream, "\n"); 00359 print_spaces_filtered (2 + 2 * recurse, stream); 00360 } 00361 } 00362 } 00363 return; 00364 } 00365 break; 00366 00367 case TYPE_CODE_UNION: 00368 if (recurse && !options->unionprint) 00369 { 00370 fprintf_filtered (stream, "{...}"); 00371 break; 00372 } 00373 /* Fall through. */ 00374 case TYPE_CODE_STRUCT: 00375 /*FIXME: Abstract this away. */ 00376 if (options->vtblprint && cp_is_vtbl_ptr_type (type)) 00377 { 00378 /* Print the unmangled name if desired. */ 00379 /* Print vtable entry - we only get here if NOT using 00380 -fvtable_thunks. (Otherwise, look under 00381 TYPE_CODE_PTR.) */ 00382 int offset = (embedded_offset 00383 + TYPE_FIELD_BITPOS (type, 00384 VTBL_FNADDR_OFFSET) / 8); 00385 struct type *field_type = TYPE_FIELD_TYPE (type, 00386 VTBL_FNADDR_OFFSET); 00387 CORE_ADDR addr 00388 = extract_typed_address (valaddr + offset, field_type); 00389 00390 print_function_pointer_address (options, gdbarch, addr, stream); 00391 } 00392 else 00393 cp_print_value_fields_rtti (type, valaddr, 00394 embedded_offset, address, 00395 stream, recurse, 00396 original_value, options, 00397 NULL, 0); 00398 break; 00399 00400 case TYPE_CODE_INT: 00401 if (options->format || options->output_format) 00402 { 00403 struct value_print_options opts = *options; 00404 00405 opts.format = (options->format ? options->format 00406 : options->output_format); 00407 val_print_scalar_formatted (type, valaddr, embedded_offset, 00408 original_value, &opts, 0, stream); 00409 } 00410 else 00411 { 00412 val_print_type_code_int (type, valaddr + embedded_offset, 00413 stream); 00414 /* C and C++ has no single byte int type, char is used 00415 instead. Since we don't know whether the value is really 00416 intended to be used as an integer or a character, print 00417 the character equivalent as well. */ 00418 if (c_textual_element_type (unresolved_type, options->format)) 00419 { 00420 fputs_filtered (" ", stream); 00421 LA_PRINT_CHAR (unpack_long (type, valaddr + embedded_offset), 00422 unresolved_type, stream); 00423 } 00424 } 00425 break; 00426 00427 case TYPE_CODE_MEMBERPTR: 00428 if (!options->format) 00429 { 00430 cp_print_class_member (valaddr + embedded_offset, type, stream, "&"); 00431 break; 00432 } 00433 /* FALLTHROUGH */ 00434 00435 case TYPE_CODE_REF: 00436 case TYPE_CODE_ENUM: 00437 case TYPE_CODE_FLAGS: 00438 case TYPE_CODE_FUNC: 00439 case TYPE_CODE_METHOD: 00440 case TYPE_CODE_BOOL: 00441 case TYPE_CODE_RANGE: 00442 case TYPE_CODE_FLT: 00443 case TYPE_CODE_DECFLOAT: 00444 case TYPE_CODE_VOID: 00445 case TYPE_CODE_ERROR: 00446 case TYPE_CODE_UNDEF: 00447 case TYPE_CODE_COMPLEX: 00448 case TYPE_CODE_CHAR: 00449 default: 00450 generic_val_print (type, valaddr, embedded_offset, address, 00451 stream, recurse, original_value, options, 00452 &c_decorations); 00453 break; 00454 } 00455 gdb_flush (stream); 00456 } 00457 00458 void 00459 c_value_print (struct value *val, struct ui_file *stream, 00460 const struct value_print_options *options) 00461 { 00462 struct type *type, *real_type, *val_type; 00463 int full, top, using_enc; 00464 struct value_print_options opts = *options; 00465 00466 opts.deref_ref = 1; 00467 00468 /* If it is a pointer, indicate what it points to. 00469 00470 Print type also if it is a reference. 00471 00472 C++: if it is a member pointer, we will take care 00473 of that when we print it. */ 00474 00475 /* Preserve the original type before stripping typedefs. We prefer 00476 to pass down the original type when possible, but for local 00477 checks it is better to look past the typedefs. */ 00478 val_type = value_type (val); 00479 type = check_typedef (val_type); 00480 00481 if (TYPE_CODE (type) == TYPE_CODE_PTR 00482 || TYPE_CODE (type) == TYPE_CODE_REF) 00483 { 00484 /* Hack: remove (char *) for char strings. Their 00485 type is indicated by the quoted string anyway. 00486 (Don't use c_textual_element_type here; quoted strings 00487 are always exactly (char *), (wchar_t *), or the like. */ 00488 if (TYPE_CODE (val_type) == TYPE_CODE_PTR 00489 && TYPE_NAME (val_type) == NULL 00490 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL 00491 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), 00492 "char") == 0 00493 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type))))) 00494 { 00495 /* Print nothing. */ 00496 } 00497 else if (options->objectprint 00498 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) 00499 { 00500 int is_ref = TYPE_CODE (type) == TYPE_CODE_REF; 00501 00502 if (is_ref) 00503 val = value_addr (val); 00504 00505 /* Pointer to class, check real type of object. */ 00506 fprintf_filtered (stream, "("); 00507 00508 if (value_entirely_available (val)) 00509 { 00510 real_type = value_rtti_indirect_type (val, &full, &top, 00511 &using_enc); 00512 if (real_type) 00513 { 00514 /* RTTI entry found. */ 00515 type = real_type; 00516 00517 /* Need to adjust pointer value. */ 00518 val = value_from_pointer (real_type, 00519 value_as_address (val) - top); 00520 00521 if (is_ref) 00522 { 00523 val = value_ref (value_ind (val)); 00524 type = value_type (val); 00525 } 00526 00527 /* Note: When we look up RTTI entries, we don't get 00528 any information on const or volatile 00529 attributes. */ 00530 } 00531 } 00532 type_print (type, "", stream, -1); 00533 fprintf_filtered (stream, ") "); 00534 val_type = type; 00535 } 00536 else 00537 { 00538 /* normal case */ 00539 fprintf_filtered (stream, "("); 00540 type_print (value_type (val), "", stream, -1); 00541 fprintf_filtered (stream, ") "); 00542 } 00543 } 00544 00545 if (!value_initialized (val)) 00546 fprintf_filtered (stream, " [uninitialized] "); 00547 00548 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS)) 00549 { 00550 /* Attempt to determine real type of object. */ 00551 real_type = value_rtti_type (val, &full, &top, &using_enc); 00552 if (real_type) 00553 { 00554 /* We have RTTI information, so use it. */ 00555 val = value_full_object (val, real_type, 00556 full, top, using_enc); 00557 fprintf_filtered (stream, "(%s%s) ", 00558 TYPE_NAME (real_type), 00559 full ? "" : _(" [incomplete object]")); 00560 /* Print out object: enclosing type is same as real_type if 00561 full. */ 00562 val_print (value_enclosing_type (val), 00563 value_contents_for_printing (val), 0, 00564 value_address (val), stream, 0, 00565 val, &opts, current_language); 00566 return; 00567 /* Note: When we look up RTTI entries, we don't get any 00568 information on const or volatile attributes. */ 00569 } 00570 else if (type != check_typedef (value_enclosing_type (val))) 00571 { 00572 /* No RTTI information, so let's do our best. */ 00573 fprintf_filtered (stream, "(%s ?) ", 00574 TYPE_NAME (value_enclosing_type (val))); 00575 val_print (value_enclosing_type (val), 00576 value_contents_for_printing (val), 0, 00577 value_address (val), stream, 0, 00578 val, &opts, current_language); 00579 return; 00580 } 00581 /* Otherwise, we end up at the return outside this "if". */ 00582 } 00583 00584 val_print (val_type, value_contents_for_printing (val), 00585 value_embedded_offset (val), 00586 value_address (val), 00587 stream, 0, 00588 val, &opts, current_language); 00589 }