]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Print values for GDB, the GNU debugger. |
5c1c87f0 | 2 | |
61baf725 | 3 | Copyright (C) 1986-2017 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "defs.h" | |
c906108c SS |
21 | #include "symtab.h" |
22 | #include "gdbtypes.h" | |
23 | #include "value.h" | |
24 | #include "gdbcore.h" | |
25 | #include "gdbcmd.h" | |
26 | #include "target.h" | |
c906108c | 27 | #include "language.h" |
c906108c SS |
28 | #include "annotate.h" |
29 | #include "valprint.h" | |
f69fdf9b | 30 | #include "target-float.h" |
6dddc817 | 31 | #include "extension.h" |
0c3acc09 | 32 | #include "ada-lang.h" |
3b2b8fea TT |
33 | #include "gdb_obstack.h" |
34 | #include "charset.h" | |
3f2f83dd | 35 | #include "typeprint.h" |
3b2b8fea | 36 | #include <ctype.h> |
325fac50 | 37 | #include <algorithm> |
d5722aa2 | 38 | #include "common/byte-vector.h" |
c906108c | 39 | |
0d63ecda KS |
40 | /* Maximum number of wchars returned from wchar_iterate. */ |
41 | #define MAX_WCHARS 4 | |
42 | ||
43 | /* A convenience macro to compute the size of a wchar_t buffer containing X | |
44 | characters. */ | |
45 | #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t)) | |
46 | ||
47 | /* Character buffer size saved while iterating over wchars. */ | |
48 | #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS) | |
49 | ||
50 | /* A structure to encapsulate state information from iterated | |
51 | character conversions. */ | |
52 | struct converted_character | |
53 | { | |
54 | /* The number of characters converted. */ | |
55 | int num_chars; | |
56 | ||
57 | /* The result of the conversion. See charset.h for more. */ | |
58 | enum wchar_iterate_result result; | |
59 | ||
60 | /* The (saved) converted character(s). */ | |
61 | gdb_wchar_t chars[WCHAR_BUFLEN_MAX]; | |
62 | ||
63 | /* The first converted target byte. */ | |
64 | const gdb_byte *buf; | |
65 | ||
66 | /* The number of bytes converted. */ | |
67 | size_t buflen; | |
68 | ||
69 | /* How many times this character(s) is repeated. */ | |
70 | int repeat_count; | |
71 | }; | |
72 | ||
73 | typedef struct converted_character converted_character_d; | |
74 | DEF_VEC_O (converted_character_d); | |
75 | ||
e7045703 DE |
76 | /* Command lists for set/show print raw. */ |
77 | struct cmd_list_element *setprintrawlist; | |
78 | struct cmd_list_element *showprintrawlist; | |
0d63ecda | 79 | |
c906108c SS |
80 | /* Prototypes for local functions */ |
81 | ||
777ea8f1 | 82 | static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, |
578d3588 | 83 | int len, int *errptr); |
917317f4 | 84 | |
a14ed312 | 85 | static void set_input_radix (char *, int, struct cmd_list_element *); |
c906108c | 86 | |
a14ed312 | 87 | static void set_input_radix_1 (int, unsigned); |
c906108c | 88 | |
a14ed312 | 89 | static void set_output_radix (char *, int, struct cmd_list_element *); |
c906108c | 90 | |
a14ed312 | 91 | static void set_output_radix_1 (int, unsigned); |
c906108c | 92 | |
81516450 DE |
93 | static void val_print_type_code_flags (struct type *type, |
94 | const gdb_byte *valaddr, | |
95 | struct ui_file *stream); | |
96 | ||
581e13c1 | 97 | #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */ |
79a45b7d TT |
98 | |
99 | struct value_print_options user_print_options = | |
100 | { | |
2a998fc0 DE |
101 | Val_prettyformat_default, /* prettyformat */ |
102 | 0, /* prettyformat_arrays */ | |
103 | 0, /* prettyformat_structs */ | |
79a45b7d TT |
104 | 0, /* vtblprint */ |
105 | 1, /* unionprint */ | |
106 | 1, /* addressprint */ | |
107 | 0, /* objectprint */ | |
108 | PRINT_MAX_DEFAULT, /* print_max */ | |
109 | 10, /* repeat_count_threshold */ | |
110 | 0, /* output_format */ | |
111 | 0, /* format */ | |
112 | 0, /* stop_print_at_null */ | |
79a45b7d TT |
113 | 0, /* print_array_indexes */ |
114 | 0, /* deref_ref */ | |
115 | 1, /* static_field_print */ | |
a6bac58e TT |
116 | 1, /* pascal_static_field_print */ |
117 | 0, /* raw */ | |
9cb709b6 TT |
118 | 0, /* summary */ |
119 | 1 /* symbol_print */ | |
79a45b7d TT |
120 | }; |
121 | ||
122 | /* Initialize *OPTS to be a copy of the user print options. */ | |
123 | void | |
124 | get_user_print_options (struct value_print_options *opts) | |
125 | { | |
126 | *opts = user_print_options; | |
127 | } | |
128 | ||
129 | /* Initialize *OPTS to be a copy of the user print options, but with | |
2a998fc0 | 130 | pretty-formatting disabled. */ |
79a45b7d | 131 | void |
2a998fc0 | 132 | get_no_prettyformat_print_options (struct value_print_options *opts) |
79a45b7d TT |
133 | { |
134 | *opts = user_print_options; | |
2a998fc0 | 135 | opts->prettyformat = Val_no_prettyformat; |
79a45b7d TT |
136 | } |
137 | ||
138 | /* Initialize *OPTS to be a copy of the user print options, but using | |
139 | FORMAT as the formatting option. */ | |
140 | void | |
141 | get_formatted_print_options (struct value_print_options *opts, | |
142 | char format) | |
143 | { | |
144 | *opts = user_print_options; | |
145 | opts->format = format; | |
146 | } | |
147 | ||
920d2a44 AC |
148 | static void |
149 | show_print_max (struct ui_file *file, int from_tty, | |
150 | struct cmd_list_element *c, const char *value) | |
151 | { | |
3e43a32a MS |
152 | fprintf_filtered (file, |
153 | _("Limit on string chars or array " | |
154 | "elements to print is %s.\n"), | |
920d2a44 AC |
155 | value); |
156 | } | |
157 | ||
c906108c SS |
158 | |
159 | /* Default input and output radixes, and output format letter. */ | |
160 | ||
161 | unsigned input_radix = 10; | |
920d2a44 AC |
162 | static void |
163 | show_input_radix (struct ui_file *file, int from_tty, | |
164 | struct cmd_list_element *c, const char *value) | |
165 | { | |
3e43a32a MS |
166 | fprintf_filtered (file, |
167 | _("Default input radix for entering numbers is %s.\n"), | |
920d2a44 AC |
168 | value); |
169 | } | |
170 | ||
c906108c | 171 | unsigned output_radix = 10; |
920d2a44 AC |
172 | static void |
173 | show_output_radix (struct ui_file *file, int from_tty, | |
174 | struct cmd_list_element *c, const char *value) | |
175 | { | |
3e43a32a MS |
176 | fprintf_filtered (file, |
177 | _("Default output radix for printing of values is %s.\n"), | |
920d2a44 AC |
178 | value); |
179 | } | |
c906108c | 180 | |
e79af960 JB |
181 | /* By default we print arrays without printing the index of each element in |
182 | the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */ | |
183 | ||
e79af960 JB |
184 | static void |
185 | show_print_array_indexes (struct ui_file *file, int from_tty, | |
186 | struct cmd_list_element *c, const char *value) | |
187 | { | |
188 | fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value); | |
189 | } | |
190 | ||
c906108c SS |
191 | /* Print repeat counts if there are more than this many repetitions of an |
192 | element in an array. Referenced by the low level language dependent | |
581e13c1 | 193 | print routines. */ |
c906108c | 194 | |
920d2a44 AC |
195 | static void |
196 | show_repeat_count_threshold (struct ui_file *file, int from_tty, | |
197 | struct cmd_list_element *c, const char *value) | |
198 | { | |
199 | fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"), | |
200 | value); | |
201 | } | |
c906108c | 202 | |
581e13c1 | 203 | /* If nonzero, stops printing of char arrays at first null. */ |
c906108c | 204 | |
920d2a44 AC |
205 | static void |
206 | show_stop_print_at_null (struct ui_file *file, int from_tty, | |
207 | struct cmd_list_element *c, const char *value) | |
208 | { | |
3e43a32a MS |
209 | fprintf_filtered (file, |
210 | _("Printing of char arrays to stop " | |
211 | "at first null char is %s.\n"), | |
920d2a44 AC |
212 | value); |
213 | } | |
c906108c | 214 | |
581e13c1 | 215 | /* Controls pretty printing of structures. */ |
c906108c | 216 | |
920d2a44 | 217 | static void |
2a998fc0 | 218 | show_prettyformat_structs (struct ui_file *file, int from_tty, |
920d2a44 AC |
219 | struct cmd_list_element *c, const char *value) |
220 | { | |
2a998fc0 | 221 | fprintf_filtered (file, _("Pretty formatting of structures is %s.\n"), value); |
920d2a44 | 222 | } |
c906108c SS |
223 | |
224 | /* Controls pretty printing of arrays. */ | |
225 | ||
920d2a44 | 226 | static void |
2a998fc0 | 227 | show_prettyformat_arrays (struct ui_file *file, int from_tty, |
920d2a44 AC |
228 | struct cmd_list_element *c, const char *value) |
229 | { | |
2a998fc0 | 230 | fprintf_filtered (file, _("Pretty formatting of arrays is %s.\n"), value); |
920d2a44 | 231 | } |
c906108c SS |
232 | |
233 | /* If nonzero, causes unions inside structures or other unions to be | |
581e13c1 | 234 | printed. */ |
c906108c | 235 | |
920d2a44 AC |
236 | static void |
237 | show_unionprint (struct ui_file *file, int from_tty, | |
238 | struct cmd_list_element *c, const char *value) | |
239 | { | |
3e43a32a MS |
240 | fprintf_filtered (file, |
241 | _("Printing of unions interior to structures is %s.\n"), | |
920d2a44 AC |
242 | value); |
243 | } | |
c906108c | 244 | |
581e13c1 | 245 | /* If nonzero, causes machine addresses to be printed in certain contexts. */ |
c906108c | 246 | |
920d2a44 AC |
247 | static void |
248 | show_addressprint (struct ui_file *file, int from_tty, | |
249 | struct cmd_list_element *c, const char *value) | |
250 | { | |
251 | fprintf_filtered (file, _("Printing of addresses is %s.\n"), value); | |
252 | } | |
9cb709b6 TT |
253 | |
254 | static void | |
255 | show_symbol_print (struct ui_file *file, int from_tty, | |
256 | struct cmd_list_element *c, const char *value) | |
257 | { | |
258 | fprintf_filtered (file, | |
259 | _("Printing of symbols when printing pointers is %s.\n"), | |
260 | value); | |
261 | } | |
262 | ||
c906108c | 263 | \f |
c5aa993b | 264 | |
a6bac58e TT |
265 | /* A helper function for val_print. When printing in "summary" mode, |
266 | we want to print scalar arguments, but not aggregate arguments. | |
267 | This function distinguishes between the two. */ | |
268 | ||
6211c335 YQ |
269 | int |
270 | val_print_scalar_type_p (struct type *type) | |
a6bac58e | 271 | { |
f168693b | 272 | type = check_typedef (type); |
aa006118 | 273 | while (TYPE_IS_REFERENCE (type)) |
a6bac58e TT |
274 | { |
275 | type = TYPE_TARGET_TYPE (type); | |
f168693b | 276 | type = check_typedef (type); |
a6bac58e TT |
277 | } |
278 | switch (TYPE_CODE (type)) | |
279 | { | |
280 | case TYPE_CODE_ARRAY: | |
281 | case TYPE_CODE_STRUCT: | |
282 | case TYPE_CODE_UNION: | |
283 | case TYPE_CODE_SET: | |
284 | case TYPE_CODE_STRING: | |
a6bac58e TT |
285 | return 0; |
286 | default: | |
287 | return 1; | |
288 | } | |
289 | } | |
290 | ||
a72c8f6a | 291 | /* See its definition in value.h. */ |
0e03807e | 292 | |
a72c8f6a | 293 | int |
0e03807e TT |
294 | valprint_check_validity (struct ui_file *stream, |
295 | struct type *type, | |
6b850546 | 296 | LONGEST embedded_offset, |
0e03807e TT |
297 | const struct value *val) |
298 | { | |
f168693b | 299 | type = check_typedef (type); |
0e03807e | 300 | |
3f2f83dd KB |
301 | if (type_not_associated (type)) |
302 | { | |
303 | val_print_not_associated (stream); | |
304 | return 0; | |
305 | } | |
306 | ||
307 | if (type_not_allocated (type)) | |
308 | { | |
309 | val_print_not_allocated (stream); | |
310 | return 0; | |
311 | } | |
312 | ||
0e03807e TT |
313 | if (TYPE_CODE (type) != TYPE_CODE_UNION |
314 | && TYPE_CODE (type) != TYPE_CODE_STRUCT | |
315 | && TYPE_CODE (type) != TYPE_CODE_ARRAY) | |
316 | { | |
9a0dc9e3 PA |
317 | if (value_bits_any_optimized_out (val, |
318 | TARGET_CHAR_BIT * embedded_offset, | |
319 | TARGET_CHAR_BIT * TYPE_LENGTH (type))) | |
0e03807e | 320 | { |
901461f8 | 321 | val_print_optimized_out (val, stream); |
0e03807e TT |
322 | return 0; |
323 | } | |
8cf6f0b1 | 324 | |
4e07d55f | 325 | if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset, |
8cf6f0b1 TT |
326 | TARGET_CHAR_BIT * TYPE_LENGTH (type))) |
327 | { | |
3326303b MG |
328 | const int is_ref = TYPE_CODE (type) == TYPE_CODE_REF; |
329 | int ref_is_addressable = 0; | |
330 | ||
331 | if (is_ref) | |
332 | { | |
333 | const struct value *deref_val = coerce_ref_if_computed (val); | |
334 | ||
335 | if (deref_val != NULL) | |
336 | ref_is_addressable = value_lval_const (deref_val) == lval_memory; | |
337 | } | |
338 | ||
339 | if (!is_ref || !ref_is_addressable) | |
340 | fputs_filtered (_("<synthetic pointer>"), stream); | |
341 | ||
342 | /* C++ references should be valid even if they're synthetic. */ | |
343 | return is_ref; | |
8cf6f0b1 | 344 | } |
4e07d55f PA |
345 | |
346 | if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) | |
347 | { | |
348 | val_print_unavailable (stream); | |
349 | return 0; | |
350 | } | |
0e03807e TT |
351 | } |
352 | ||
353 | return 1; | |
354 | } | |
355 | ||
585fdaa1 | 356 | void |
901461f8 | 357 | val_print_optimized_out (const struct value *val, struct ui_file *stream) |
585fdaa1 | 358 | { |
901461f8 | 359 | if (val != NULL && value_lval_const (val) == lval_register) |
782d47df | 360 | val_print_not_saved (stream); |
901461f8 PA |
361 | else |
362 | fprintf_filtered (stream, _("<optimized out>")); | |
585fdaa1 PA |
363 | } |
364 | ||
782d47df PA |
365 | void |
366 | val_print_not_saved (struct ui_file *stream) | |
367 | { | |
368 | fprintf_filtered (stream, _("<not saved>")); | |
369 | } | |
370 | ||
4e07d55f PA |
371 | void |
372 | val_print_unavailable (struct ui_file *stream) | |
373 | { | |
374 | fprintf_filtered (stream, _("<unavailable>")); | |
375 | } | |
376 | ||
8af8e3bc PA |
377 | void |
378 | val_print_invalid_address (struct ui_file *stream) | |
379 | { | |
380 | fprintf_filtered (stream, _("<invalid address>")); | |
381 | } | |
382 | ||
9f436164 SM |
383 | /* Print a pointer based on the type of its target. |
384 | ||
385 | Arguments to this functions are roughly the same as those in | |
386 | generic_val_print. A difference is that ADDRESS is the address to print, | |
387 | with embedded_offset already added. ELTTYPE represents | |
388 | the pointed type after check_typedef. */ | |
389 | ||
390 | static void | |
391 | print_unpacked_pointer (struct type *type, struct type *elttype, | |
392 | CORE_ADDR address, struct ui_file *stream, | |
393 | const struct value_print_options *options) | |
394 | { | |
395 | struct gdbarch *gdbarch = get_type_arch (type); | |
396 | ||
397 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) | |
398 | { | |
399 | /* Try to print what function it points to. */ | |
400 | print_function_pointer_address (options, gdbarch, address, stream); | |
401 | return; | |
402 | } | |
403 | ||
404 | if (options->symbol_print) | |
405 | print_address_demangle (options, gdbarch, address, stream, demangle); | |
406 | else if (options->addressprint) | |
407 | fputs_filtered (paddress (gdbarch, address), stream); | |
408 | } | |
409 | ||
557dbe8a SM |
410 | /* generic_val_print helper for TYPE_CODE_ARRAY. */ |
411 | ||
412 | static void | |
e8b24d9f | 413 | generic_val_print_array (struct type *type, |
00272ec4 TT |
414 | int embedded_offset, CORE_ADDR address, |
415 | struct ui_file *stream, int recurse, | |
e8b24d9f | 416 | struct value *original_value, |
00272ec4 TT |
417 | const struct value_print_options *options, |
418 | const struct | |
419 | generic_val_print_decorations *decorations) | |
557dbe8a SM |
420 | { |
421 | struct type *unresolved_elttype = TYPE_TARGET_TYPE (type); | |
422 | struct type *elttype = check_typedef (unresolved_elttype); | |
423 | ||
424 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0) | |
425 | { | |
426 | LONGEST low_bound, high_bound; | |
427 | ||
428 | if (!get_array_bounds (type, &low_bound, &high_bound)) | |
429 | error (_("Could not determine the array high bound")); | |
430 | ||
431 | if (options->prettyformat_arrays) | |
432 | { | |
433 | print_spaces_filtered (2 + 2 * recurse, stream); | |
434 | } | |
435 | ||
00272ec4 | 436 | fputs_filtered (decorations->array_start, stream); |
e8b24d9f | 437 | val_print_array_elements (type, embedded_offset, |
557dbe8a SM |
438 | address, stream, |
439 | recurse, original_value, options, 0); | |
00272ec4 | 440 | fputs_filtered (decorations->array_end, stream); |
557dbe8a SM |
441 | } |
442 | else | |
443 | { | |
444 | /* Array of unspecified length: treat like pointer to first elt. */ | |
445 | print_unpacked_pointer (type, elttype, address + embedded_offset, stream, | |
446 | options); | |
447 | } | |
448 | ||
449 | } | |
450 | ||
81eb921a SM |
451 | /* generic_val_print helper for TYPE_CODE_PTR. */ |
452 | ||
453 | static void | |
e8b24d9f | 454 | generic_val_print_ptr (struct type *type, |
81eb921a | 455 | int embedded_offset, struct ui_file *stream, |
e8b24d9f | 456 | struct value *original_value, |
81eb921a SM |
457 | const struct value_print_options *options) |
458 | { | |
3ae385af SM |
459 | struct gdbarch *gdbarch = get_type_arch (type); |
460 | int unit_size = gdbarch_addressable_memory_unit_size (gdbarch); | |
461 | ||
81eb921a SM |
462 | if (options->format && options->format != 's') |
463 | { | |
e8b24d9f | 464 | val_print_scalar_formatted (type, embedded_offset, |
81eb921a SM |
465 | original_value, options, 0, stream); |
466 | } | |
467 | else | |
468 | { | |
469 | struct type *unresolved_elttype = TYPE_TARGET_TYPE(type); | |
470 | struct type *elttype = check_typedef (unresolved_elttype); | |
e8b24d9f | 471 | const gdb_byte *valaddr = value_contents_for_printing (original_value); |
3ae385af SM |
472 | CORE_ADDR addr = unpack_pointer (type, |
473 | valaddr + embedded_offset * unit_size); | |
81eb921a SM |
474 | |
475 | print_unpacked_pointer (type, elttype, addr, stream, options); | |
476 | } | |
477 | } | |
478 | ||
45000ea2 SM |
479 | |
480 | /* generic_val_print helper for TYPE_CODE_MEMBERPTR. */ | |
481 | ||
482 | static void | |
e8b24d9f | 483 | generic_val_print_memberptr (struct type *type, |
45000ea2 | 484 | int embedded_offset, struct ui_file *stream, |
e8b24d9f | 485 | struct value *original_value, |
45000ea2 SM |
486 | const struct value_print_options *options) |
487 | { | |
e8b24d9f | 488 | val_print_scalar_formatted (type, embedded_offset, |
45000ea2 SM |
489 | original_value, options, 0, stream); |
490 | } | |
491 | ||
3326303b MG |
492 | /* Print '@' followed by the address contained in ADDRESS_BUFFER. */ |
493 | ||
494 | static void | |
495 | print_ref_address (struct type *type, const gdb_byte *address_buffer, | |
496 | int embedded_offset, struct ui_file *stream) | |
497 | { | |
498 | struct gdbarch *gdbarch = get_type_arch (type); | |
499 | ||
500 | if (address_buffer != NULL) | |
501 | { | |
502 | CORE_ADDR address | |
503 | = extract_typed_address (address_buffer + embedded_offset, type); | |
504 | ||
505 | fprintf_filtered (stream, "@"); | |
506 | fputs_filtered (paddress (gdbarch, address), stream); | |
507 | } | |
508 | /* Else: we have a non-addressable value, such as a DW_AT_const_value. */ | |
509 | } | |
510 | ||
511 | /* If VAL is addressable, return the value contents buffer of a value that | |
512 | represents a pointer to VAL. Otherwise return NULL. */ | |
513 | ||
514 | static const gdb_byte * | |
515 | get_value_addr_contents (struct value *deref_val) | |
516 | { | |
517 | gdb_assert (deref_val != NULL); | |
518 | ||
519 | if (value_lval_const (deref_val) == lval_memory) | |
520 | return value_contents_for_printing_const (value_addr (deref_val)); | |
521 | else | |
522 | { | |
523 | /* We have a non-addressable value, such as a DW_AT_const_value. */ | |
524 | return NULL; | |
525 | } | |
526 | } | |
527 | ||
aa006118 | 528 | /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */ |
fe43fede SM |
529 | |
530 | static void | |
e8b24d9f | 531 | generic_val_print_ref (struct type *type, |
fe43fede | 532 | int embedded_offset, struct ui_file *stream, int recurse, |
e8b24d9f | 533 | struct value *original_value, |
fe43fede SM |
534 | const struct value_print_options *options) |
535 | { | |
fe43fede | 536 | struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type)); |
3326303b MG |
537 | struct value *deref_val = NULL; |
538 | const int value_is_synthetic | |
539 | = value_bits_synthetic_pointer (original_value, | |
540 | TARGET_CHAR_BIT * embedded_offset, | |
541 | TARGET_CHAR_BIT * TYPE_LENGTH (type)); | |
542 | const int must_coerce_ref = ((options->addressprint && value_is_synthetic) | |
543 | || options->deref_ref); | |
544 | const int type_is_defined = TYPE_CODE (elttype) != TYPE_CODE_UNDEF; | |
e8b24d9f | 545 | const gdb_byte *valaddr = value_contents_for_printing (original_value); |
3326303b MG |
546 | |
547 | if (must_coerce_ref && type_is_defined) | |
548 | { | |
549 | deref_val = coerce_ref_if_computed (original_value); | |
550 | ||
551 | if (deref_val != NULL) | |
552 | { | |
553 | /* More complicated computed references are not supported. */ | |
554 | gdb_assert (embedded_offset == 0); | |
555 | } | |
556 | else | |
557 | deref_val = value_at (TYPE_TARGET_TYPE (type), | |
558 | unpack_pointer (type, valaddr + embedded_offset)); | |
559 | } | |
560 | /* Else, original_value isn't a synthetic reference or we don't have to print | |
561 | the reference's contents. | |
562 | ||
563 | Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will | |
564 | cause original_value to be a not_lval instead of an lval_computed, | |
565 | which will make value_bits_synthetic_pointer return false. | |
566 | This happens because if options->objectprint is true, c_value_print will | |
567 | overwrite original_value's contents with the result of coercing | |
568 | the reference through value_addr, and then set its type back to | |
569 | TYPE_CODE_REF. In that case we don't have to coerce the reference again; | |
570 | we can simply treat it as non-synthetic and move on. */ | |
fe43fede SM |
571 | |
572 | if (options->addressprint) | |
573 | { | |
3326303b MG |
574 | const gdb_byte *address = (value_is_synthetic && type_is_defined |
575 | ? get_value_addr_contents (deref_val) | |
576 | : valaddr); | |
577 | ||
578 | print_ref_address (type, address, embedded_offset, stream); | |
fe43fede | 579 | |
fe43fede SM |
580 | if (options->deref_ref) |
581 | fputs_filtered (": ", stream); | |
582 | } | |
3326303b | 583 | |
fe43fede SM |
584 | if (options->deref_ref) |
585 | { | |
3326303b MG |
586 | if (type_is_defined) |
587 | common_val_print (deref_val, stream, recurse, options, | |
588 | current_language); | |
fe43fede SM |
589 | else |
590 | fputs_filtered ("???", stream); | |
591 | } | |
592 | } | |
593 | ||
81516450 DE |
594 | /* Helper function for generic_val_print_enum. |
595 | This is also used to print enums in TYPE_CODE_FLAGS values. */ | |
ef0bc0dd SM |
596 | |
597 | static void | |
81516450 DE |
598 | generic_val_print_enum_1 (struct type *type, LONGEST val, |
599 | struct ui_file *stream) | |
ef0bc0dd SM |
600 | { |
601 | unsigned int i; | |
602 | unsigned int len; | |
ef0bc0dd | 603 | |
ef0bc0dd | 604 | len = TYPE_NFIELDS (type); |
ef0bc0dd SM |
605 | for (i = 0; i < len; i++) |
606 | { | |
607 | QUIT; | |
608 | if (val == TYPE_FIELD_ENUMVAL (type, i)) | |
609 | { | |
610 | break; | |
611 | } | |
612 | } | |
613 | if (i < len) | |
614 | { | |
615 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
616 | } | |
617 | else if (TYPE_FLAG_ENUM (type)) | |
618 | { | |
619 | int first = 1; | |
620 | ||
621 | /* We have a "flag" enum, so we try to decompose it into | |
622 | pieces as appropriate. A flag enum has disjoint | |
623 | constants by definition. */ | |
624 | fputs_filtered ("(", stream); | |
625 | for (i = 0; i < len; ++i) | |
626 | { | |
627 | QUIT; | |
628 | ||
629 | if ((val & TYPE_FIELD_ENUMVAL (type, i)) != 0) | |
630 | { | |
631 | if (!first) | |
632 | fputs_filtered (" | ", stream); | |
633 | first = 0; | |
634 | ||
635 | val &= ~TYPE_FIELD_ENUMVAL (type, i); | |
636 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
637 | } | |
638 | } | |
639 | ||
640 | if (first || val != 0) | |
641 | { | |
642 | if (!first) | |
643 | fputs_filtered (" | ", stream); | |
644 | fputs_filtered ("unknown: ", stream); | |
645 | print_longest (stream, 'd', 0, val); | |
646 | } | |
647 | ||
648 | fputs_filtered (")", stream); | |
649 | } | |
650 | else | |
651 | print_longest (stream, 'd', 0, val); | |
652 | } | |
653 | ||
81516450 DE |
654 | /* generic_val_print helper for TYPE_CODE_ENUM. */ |
655 | ||
656 | static void | |
e8b24d9f | 657 | generic_val_print_enum (struct type *type, |
81516450 | 658 | int embedded_offset, struct ui_file *stream, |
e8b24d9f | 659 | struct value *original_value, |
81516450 DE |
660 | const struct value_print_options *options) |
661 | { | |
662 | LONGEST val; | |
663 | struct gdbarch *gdbarch = get_type_arch (type); | |
664 | int unit_size = gdbarch_addressable_memory_unit_size (gdbarch); | |
665 | ||
666 | if (options->format) | |
667 | { | |
e8b24d9f | 668 | val_print_scalar_formatted (type, embedded_offset, |
81516450 | 669 | original_value, options, 0, stream); |
81516450 | 670 | } |
e8b24d9f YQ |
671 | else |
672 | { | |
673 | const gdb_byte *valaddr = value_contents_for_printing (original_value); | |
674 | ||
675 | val = unpack_long (type, valaddr + embedded_offset * unit_size); | |
81516450 | 676 | |
e8b24d9f YQ |
677 | generic_val_print_enum_1 (type, val, stream); |
678 | } | |
81516450 DE |
679 | } |
680 | ||
d93880bd SM |
681 | /* generic_val_print helper for TYPE_CODE_FLAGS. */ |
682 | ||
683 | static void | |
e8b24d9f | 684 | generic_val_print_flags (struct type *type, |
d93880bd | 685 | int embedded_offset, struct ui_file *stream, |
e8b24d9f | 686 | struct value *original_value, |
d93880bd SM |
687 | const struct value_print_options *options) |
688 | ||
689 | { | |
690 | if (options->format) | |
e8b24d9f | 691 | val_print_scalar_formatted (type, embedded_offset, original_value, |
d93880bd SM |
692 | options, 0, stream); |
693 | else | |
e8b24d9f YQ |
694 | { |
695 | const gdb_byte *valaddr = value_contents_for_printing (original_value); | |
696 | ||
697 | val_print_type_code_flags (type, valaddr + embedded_offset, stream); | |
698 | } | |
d93880bd SM |
699 | } |
700 | ||
4a8c372f SM |
701 | /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */ |
702 | ||
703 | static void | |
e8b24d9f | 704 | generic_val_print_func (struct type *type, |
4a8c372f SM |
705 | int embedded_offset, CORE_ADDR address, |
706 | struct ui_file *stream, | |
e8b24d9f | 707 | struct value *original_value, |
4a8c372f SM |
708 | const struct value_print_options *options) |
709 | { | |
710 | struct gdbarch *gdbarch = get_type_arch (type); | |
711 | ||
712 | if (options->format) | |
713 | { | |
e8b24d9f | 714 | val_print_scalar_formatted (type, embedded_offset, |
4a8c372f SM |
715 | original_value, options, 0, stream); |
716 | } | |
717 | else | |
718 | { | |
719 | /* FIXME, we should consider, at least for ANSI C language, | |
720 | eliminating the distinction made between FUNCs and POINTERs | |
721 | to FUNCs. */ | |
722 | fprintf_filtered (stream, "{"); | |
723 | type_print (type, "", stream, -1); | |
724 | fprintf_filtered (stream, "} "); | |
725 | /* Try to print what function it points to, and its address. */ | |
726 | print_address_demangle (options, gdbarch, address, stream, demangle); | |
727 | } | |
728 | } | |
729 | ||
e5bead4b SM |
730 | /* generic_val_print helper for TYPE_CODE_BOOL. */ |
731 | ||
732 | static void | |
e8b24d9f | 733 | generic_val_print_bool (struct type *type, |
e5bead4b | 734 | int embedded_offset, struct ui_file *stream, |
e8b24d9f | 735 | struct value *original_value, |
e5bead4b SM |
736 | const struct value_print_options *options, |
737 | const struct generic_val_print_decorations *decorations) | |
738 | { | |
739 | LONGEST val; | |
3ae385af SM |
740 | struct gdbarch *gdbarch = get_type_arch (type); |
741 | int unit_size = gdbarch_addressable_memory_unit_size (gdbarch); | |
e5bead4b SM |
742 | |
743 | if (options->format || options->output_format) | |
744 | { | |
745 | struct value_print_options opts = *options; | |
746 | opts.format = (options->format ? options->format | |
747 | : options->output_format); | |
e8b24d9f | 748 | val_print_scalar_formatted (type, embedded_offset, |
e5bead4b SM |
749 | original_value, &opts, 0, stream); |
750 | } | |
751 | else | |
752 | { | |
e8b24d9f YQ |
753 | const gdb_byte *valaddr = value_contents_for_printing (original_value); |
754 | ||
3ae385af | 755 | val = unpack_long (type, valaddr + embedded_offset * unit_size); |
e5bead4b SM |
756 | if (val == 0) |
757 | fputs_filtered (decorations->false_name, stream); | |
758 | else if (val == 1) | |
759 | fputs_filtered (decorations->true_name, stream); | |
760 | else | |
761 | print_longest (stream, 'd', 0, val); | |
762 | } | |
763 | } | |
764 | ||
b21b6342 SM |
765 | /* generic_val_print helper for TYPE_CODE_INT. */ |
766 | ||
767 | static void | |
e8b24d9f | 768 | generic_val_print_int (struct type *type, |
b21b6342 | 769 | int embedded_offset, struct ui_file *stream, |
e8b24d9f | 770 | struct value *original_value, |
b21b6342 SM |
771 | const struct value_print_options *options) |
772 | { | |
f12f6bad | 773 | struct value_print_options opts = *options; |
3ae385af | 774 | |
f12f6bad TT |
775 | opts.format = (options->format ? options->format |
776 | : options->output_format); | |
777 | val_print_scalar_formatted (type, embedded_offset, | |
778 | original_value, &opts, 0, stream); | |
b21b6342 SM |
779 | } |
780 | ||
385f5aff SM |
781 | /* generic_val_print helper for TYPE_CODE_CHAR. */ |
782 | ||
783 | static void | |
784 | generic_val_print_char (struct type *type, struct type *unresolved_type, | |
e8b24d9f | 785 | int embedded_offset, |
385f5aff | 786 | struct ui_file *stream, |
e8b24d9f | 787 | struct value *original_value, |
385f5aff SM |
788 | const struct value_print_options *options) |
789 | { | |
790 | LONGEST val; | |
3ae385af SM |
791 | struct gdbarch *gdbarch = get_type_arch (type); |
792 | int unit_size = gdbarch_addressable_memory_unit_size (gdbarch); | |
385f5aff SM |
793 | |
794 | if (options->format || options->output_format) | |
795 | { | |
796 | struct value_print_options opts = *options; | |
797 | ||
798 | opts.format = (options->format ? options->format | |
799 | : options->output_format); | |
e8b24d9f | 800 | val_print_scalar_formatted (type, embedded_offset, |
385f5aff SM |
801 | original_value, &opts, 0, stream); |
802 | } | |
803 | else | |
804 | { | |
e8b24d9f YQ |
805 | const gdb_byte *valaddr = value_contents_for_printing (original_value); |
806 | ||
3ae385af | 807 | val = unpack_long (type, valaddr + embedded_offset * unit_size); |
385f5aff SM |
808 | if (TYPE_UNSIGNED (type)) |
809 | fprintf_filtered (stream, "%u", (unsigned int) val); | |
810 | else | |
811 | fprintf_filtered (stream, "%d", (int) val); | |
812 | fputs_filtered (" ", stream); | |
813 | LA_PRINT_CHAR (val, unresolved_type, stream); | |
814 | } | |
815 | } | |
816 | ||
fdf0cbc2 | 817 | /* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */ |
7784724b SM |
818 | |
819 | static void | |
e8b24d9f | 820 | generic_val_print_float (struct type *type, |
7784724b | 821 | int embedded_offset, struct ui_file *stream, |
e8b24d9f | 822 | struct value *original_value, |
7784724b SM |
823 | const struct value_print_options *options) |
824 | { | |
3ae385af SM |
825 | struct gdbarch *gdbarch = get_type_arch (type); |
826 | int unit_size = gdbarch_addressable_memory_unit_size (gdbarch); | |
827 | ||
7784724b SM |
828 | if (options->format) |
829 | { | |
e8b24d9f | 830 | val_print_scalar_formatted (type, embedded_offset, |
7784724b SM |
831 | original_value, options, 0, stream); |
832 | } | |
833 | else | |
834 | { | |
e8b24d9f YQ |
835 | const gdb_byte *valaddr = value_contents_for_printing (original_value); |
836 | ||
3ae385af | 837 | print_floating (valaddr + embedded_offset * unit_size, type, stream); |
7784724b SM |
838 | } |
839 | } | |
840 | ||
0c87c0bf SM |
841 | /* generic_val_print helper for TYPE_CODE_COMPLEX. */ |
842 | ||
843 | static void | |
e8b24d9f | 844 | generic_val_print_complex (struct type *type, |
0c87c0bf | 845 | int embedded_offset, struct ui_file *stream, |
e8b24d9f | 846 | struct value *original_value, |
0c87c0bf SM |
847 | const struct value_print_options *options, |
848 | const struct generic_val_print_decorations | |
849 | *decorations) | |
850 | { | |
3ae385af SM |
851 | struct gdbarch *gdbarch = get_type_arch (type); |
852 | int unit_size = gdbarch_addressable_memory_unit_size (gdbarch); | |
e8b24d9f | 853 | const gdb_byte *valaddr = value_contents_for_printing (original_value); |
3ae385af | 854 | |
0c87c0bf SM |
855 | fprintf_filtered (stream, "%s", decorations->complex_prefix); |
856 | if (options->format) | |
e8b24d9f | 857 | val_print_scalar_formatted (TYPE_TARGET_TYPE (type), |
0c87c0bf SM |
858 | embedded_offset, original_value, options, 0, |
859 | stream); | |
860 | else | |
3ae385af SM |
861 | print_floating (valaddr + embedded_offset * unit_size, |
862 | TYPE_TARGET_TYPE (type), stream); | |
0c87c0bf SM |
863 | fprintf_filtered (stream, "%s", decorations->complex_infix); |
864 | if (options->format) | |
e8b24d9f | 865 | val_print_scalar_formatted (TYPE_TARGET_TYPE (type), |
0c87c0bf | 866 | embedded_offset |
3ae385af | 867 | + type_length_units (TYPE_TARGET_TYPE (type)), |
0c87c0bf SM |
868 | original_value, options, 0, stream); |
869 | else | |
3ae385af | 870 | print_floating (valaddr + embedded_offset * unit_size |
0c87c0bf SM |
871 | + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), |
872 | TYPE_TARGET_TYPE (type), stream); | |
873 | fprintf_filtered (stream, "%s", decorations->complex_suffix); | |
874 | } | |
875 | ||
e88acd96 TT |
876 | /* A generic val_print that is suitable for use by language |
877 | implementations of the la_val_print method. This function can | |
878 | handle most type codes, though not all, notably exception | |
879 | TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by | |
880 | the caller. | |
881 | ||
882 | Most arguments are as to val_print. | |
883 | ||
884 | The additional DECORATIONS argument can be used to customize the | |
885 | output in some small, language-specific ways. */ | |
886 | ||
887 | void | |
e8b24d9f | 888 | generic_val_print (struct type *type, |
e88acd96 TT |
889 | int embedded_offset, CORE_ADDR address, |
890 | struct ui_file *stream, int recurse, | |
e8b24d9f | 891 | struct value *original_value, |
e88acd96 TT |
892 | const struct value_print_options *options, |
893 | const struct generic_val_print_decorations *decorations) | |
894 | { | |
e88acd96 | 895 | struct type *unresolved_type = type; |
e88acd96 | 896 | |
f168693b | 897 | type = check_typedef (type); |
e88acd96 TT |
898 | switch (TYPE_CODE (type)) |
899 | { | |
900 | case TYPE_CODE_ARRAY: | |
e8b24d9f | 901 | generic_val_print_array (type, embedded_offset, address, stream, |
00272ec4 | 902 | recurse, original_value, options, decorations); |
9f436164 | 903 | break; |
e88acd96 TT |
904 | |
905 | case TYPE_CODE_MEMBERPTR: | |
e8b24d9f | 906 | generic_val_print_memberptr (type, embedded_offset, stream, |
45000ea2 | 907 | original_value, options); |
e88acd96 TT |
908 | break; |
909 | ||
910 | case TYPE_CODE_PTR: | |
e8b24d9f | 911 | generic_val_print_ptr (type, embedded_offset, stream, |
81eb921a | 912 | original_value, options); |
e88acd96 TT |
913 | break; |
914 | ||
915 | case TYPE_CODE_REF: | |
aa006118 | 916 | case TYPE_CODE_RVALUE_REF: |
e8b24d9f | 917 | generic_val_print_ref (type, embedded_offset, stream, recurse, |
fe43fede | 918 | original_value, options); |
e88acd96 TT |
919 | break; |
920 | ||
921 | case TYPE_CODE_ENUM: | |
e8b24d9f | 922 | generic_val_print_enum (type, embedded_offset, stream, |
ef0bc0dd | 923 | original_value, options); |
e88acd96 TT |
924 | break; |
925 | ||
926 | case TYPE_CODE_FLAGS: | |
e8b24d9f | 927 | generic_val_print_flags (type, embedded_offset, stream, |
d93880bd | 928 | original_value, options); |
e88acd96 TT |
929 | break; |
930 | ||
931 | case TYPE_CODE_FUNC: | |
932 | case TYPE_CODE_METHOD: | |
e8b24d9f | 933 | generic_val_print_func (type, embedded_offset, address, stream, |
4a8c372f | 934 | original_value, options); |
e88acd96 TT |
935 | break; |
936 | ||
937 | case TYPE_CODE_BOOL: | |
e8b24d9f | 938 | generic_val_print_bool (type, embedded_offset, stream, |
e5bead4b | 939 | original_value, options, decorations); |
e88acd96 TT |
940 | break; |
941 | ||
942 | case TYPE_CODE_RANGE: | |
0c9c3474 | 943 | /* FIXME: create_static_range_type does not set the unsigned bit in a |
e88acd96 TT |
944 | range type (I think it probably should copy it from the |
945 | target type), so we won't print values which are too large to | |
946 | fit in a signed integer correctly. */ | |
947 | /* FIXME: Doesn't handle ranges of enums correctly. (Can't just | |
948 | print with the target type, though, because the size of our | |
949 | type and the target type might differ). */ | |
950 | ||
951 | /* FALLTHROUGH */ | |
952 | ||
953 | case TYPE_CODE_INT: | |
e8b24d9f | 954 | generic_val_print_int (type, embedded_offset, stream, |
b21b6342 | 955 | original_value, options); |
e88acd96 TT |
956 | break; |
957 | ||
958 | case TYPE_CODE_CHAR: | |
e8b24d9f | 959 | generic_val_print_char (type, unresolved_type, embedded_offset, |
385f5aff | 960 | stream, original_value, options); |
e88acd96 TT |
961 | break; |
962 | ||
963 | case TYPE_CODE_FLT: | |
fdf0cbc2 | 964 | case TYPE_CODE_DECFLOAT: |
e8b24d9f | 965 | generic_val_print_float (type, embedded_offset, stream, |
7784724b | 966 | original_value, options); |
e88acd96 TT |
967 | break; |
968 | ||
e88acd96 TT |
969 | case TYPE_CODE_VOID: |
970 | fputs_filtered (decorations->void_name, stream); | |
971 | break; | |
972 | ||
973 | case TYPE_CODE_ERROR: | |
974 | fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type)); | |
975 | break; | |
976 | ||
977 | case TYPE_CODE_UNDEF: | |
a9ff5f12 UW |
978 | /* This happens (without TYPE_STUB set) on systems which don't use |
979 | dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" | |
980 | and no complete type for struct foo in that file. */ | |
e88acd96 TT |
981 | fprintf_filtered (stream, _("<incomplete type>")); |
982 | break; | |
983 | ||
984 | case TYPE_CODE_COMPLEX: | |
e8b24d9f | 985 | generic_val_print_complex (type, embedded_offset, stream, |
0c87c0bf | 986 | original_value, options, decorations); |
e88acd96 TT |
987 | break; |
988 | ||
989 | case TYPE_CODE_UNION: | |
990 | case TYPE_CODE_STRUCT: | |
991 | case TYPE_CODE_METHODPTR: | |
992 | default: | |
993 | error (_("Unhandled type code %d in symbol table."), | |
994 | TYPE_CODE (type)); | |
995 | } | |
996 | gdb_flush (stream); | |
997 | } | |
998 | ||
32b72a42 | 999 | /* Print using the given LANGUAGE the data of type TYPE located at |
e8b24d9f YQ |
1000 | VAL's contents buffer + EMBEDDED_OFFSET (within GDB), which came |
1001 | from the inferior at address ADDRESS + EMBEDDED_OFFSET, onto | |
1002 | stdio stream STREAM according to OPTIONS. VAL is the whole object | |
1003 | that came from ADDRESS. | |
32b72a42 PA |
1004 | |
1005 | The language printers will pass down an adjusted EMBEDDED_OFFSET to | |
1006 | further helper subroutines as subfields of TYPE are printed. In | |
e8b24d9f | 1007 | such cases, VAL is passed down unadjusted, so |
32b72a42 PA |
1008 | that VAL can be queried for metadata about the contents data being |
1009 | printed, using EMBEDDED_OFFSET as an offset into VAL's contents | |
1010 | buffer. For example: "has this field been optimized out", or "I'm | |
1011 | printing an object while inspecting a traceframe; has this | |
1012 | particular piece of data been collected?". | |
1013 | ||
1014 | RECURSE indicates the amount of indentation to supply before | |
1015 | continuation lines; this amount is roughly twice the value of | |
35c0084b | 1016 | RECURSE. */ |
32b72a42 | 1017 | |
35c0084b | 1018 | void |
e8b24d9f | 1019 | val_print (struct type *type, LONGEST embedded_offset, |
79a45b7d | 1020 | CORE_ADDR address, struct ui_file *stream, int recurse, |
e8b24d9f | 1021 | struct value *val, |
79a45b7d | 1022 | const struct value_print_options *options, |
d8ca156b | 1023 | const struct language_defn *language) |
c906108c | 1024 | { |
19ca80ba | 1025 | int ret = 0; |
79a45b7d | 1026 | struct value_print_options local_opts = *options; |
c906108c | 1027 | struct type *real_type = check_typedef (type); |
79a45b7d | 1028 | |
2a998fc0 DE |
1029 | if (local_opts.prettyformat == Val_prettyformat_default) |
1030 | local_opts.prettyformat = (local_opts.prettyformat_structs | |
1031 | ? Val_prettyformat : Val_no_prettyformat); | |
c5aa993b | 1032 | |
c906108c SS |
1033 | QUIT; |
1034 | ||
1035 | /* Ensure that the type is complete and not just a stub. If the type is | |
1036 | only a stub and we can't find and substitute its complete type, then | |
1037 | print appropriate string and return. */ | |
1038 | ||
74a9bb82 | 1039 | if (TYPE_STUB (real_type)) |
c906108c | 1040 | { |
0e03807e | 1041 | fprintf_filtered (stream, _("<incomplete type>")); |
c906108c | 1042 | gdb_flush (stream); |
35c0084b | 1043 | return; |
c906108c | 1044 | } |
c5aa993b | 1045 | |
0e03807e | 1046 | if (!valprint_check_validity (stream, real_type, embedded_offset, val)) |
35c0084b | 1047 | return; |
0e03807e | 1048 | |
a6bac58e TT |
1049 | if (!options->raw) |
1050 | { | |
668e1674 | 1051 | ret = apply_ext_lang_val_pretty_printer (type, embedded_offset, |
6dddc817 DE |
1052 | address, stream, recurse, |
1053 | val, options, language); | |
a6bac58e | 1054 | if (ret) |
35c0084b | 1055 | return; |
a6bac58e TT |
1056 | } |
1057 | ||
1058 | /* Handle summary mode. If the value is a scalar, print it; | |
1059 | otherwise, print an ellipsis. */ | |
6211c335 | 1060 | if (options->summary && !val_print_scalar_type_p (type)) |
a6bac58e TT |
1061 | { |
1062 | fprintf_filtered (stream, "..."); | |
35c0084b | 1063 | return; |
a6bac58e TT |
1064 | } |
1065 | ||
492d29ea | 1066 | TRY |
19ca80ba | 1067 | { |
e8b24d9f | 1068 | language->la_val_print (type, embedded_offset, address, |
d3eab38a TT |
1069 | stream, recurse, val, |
1070 | &local_opts); | |
19ca80ba | 1071 | } |
492d29ea PA |
1072 | CATCH (except, RETURN_MASK_ERROR) |
1073 | { | |
1074 | fprintf_filtered (stream, _("<error reading variable>")); | |
1075 | } | |
1076 | END_CATCH | |
c906108c SS |
1077 | } |
1078 | ||
806048c6 | 1079 | /* Check whether the value VAL is printable. Return 1 if it is; |
6501578c YQ |
1080 | return 0 and print an appropriate error message to STREAM according to |
1081 | OPTIONS if it is not. */ | |
c906108c | 1082 | |
806048c6 | 1083 | static int |
6501578c YQ |
1084 | value_check_printable (struct value *val, struct ui_file *stream, |
1085 | const struct value_print_options *options) | |
c906108c SS |
1086 | { |
1087 | if (val == 0) | |
1088 | { | |
806048c6 | 1089 | fprintf_filtered (stream, _("<address of value unknown>")); |
c906108c SS |
1090 | return 0; |
1091 | } | |
806048c6 | 1092 | |
0e03807e | 1093 | if (value_entirely_optimized_out (val)) |
c906108c | 1094 | { |
6211c335 | 1095 | if (options->summary && !val_print_scalar_type_p (value_type (val))) |
6501578c YQ |
1096 | fprintf_filtered (stream, "..."); |
1097 | else | |
901461f8 | 1098 | val_print_optimized_out (val, stream); |
c906108c SS |
1099 | return 0; |
1100 | } | |
806048c6 | 1101 | |
eebc056c AB |
1102 | if (value_entirely_unavailable (val)) |
1103 | { | |
1104 | if (options->summary && !val_print_scalar_type_p (value_type (val))) | |
1105 | fprintf_filtered (stream, "..."); | |
1106 | else | |
1107 | val_print_unavailable (stream); | |
1108 | return 0; | |
1109 | } | |
1110 | ||
bc3b79fd TJB |
1111 | if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION) |
1112 | { | |
1113 | fprintf_filtered (stream, _("<internal function %s>"), | |
1114 | value_internal_function_name (val)); | |
1115 | return 0; | |
1116 | } | |
1117 | ||
3f2f83dd KB |
1118 | if (type_not_associated (value_type (val))) |
1119 | { | |
1120 | val_print_not_associated (stream); | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | if (type_not_allocated (value_type (val))) | |
1125 | { | |
1126 | val_print_not_allocated (stream); | |
1127 | return 0; | |
1128 | } | |
1129 | ||
806048c6 DJ |
1130 | return 1; |
1131 | } | |
1132 | ||
d8ca156b | 1133 | /* Print using the given LANGUAGE the value VAL onto stream STREAM according |
79a45b7d | 1134 | to OPTIONS. |
806048c6 | 1135 | |
806048c6 DJ |
1136 | This is a preferable interface to val_print, above, because it uses |
1137 | GDB's value mechanism. */ | |
1138 | ||
a1f5dd1b | 1139 | void |
79a45b7d TT |
1140 | common_val_print (struct value *val, struct ui_file *stream, int recurse, |
1141 | const struct value_print_options *options, | |
d8ca156b | 1142 | const struct language_defn *language) |
806048c6 | 1143 | { |
6501578c | 1144 | if (!value_check_printable (val, stream, options)) |
a1f5dd1b | 1145 | return; |
806048c6 | 1146 | |
0c3acc09 JB |
1147 | if (language->la_language == language_ada) |
1148 | /* The value might have a dynamic type, which would cause trouble | |
1149 | below when trying to extract the value contents (since the value | |
1150 | size is determined from the type size which is unknown). So | |
1151 | get a fixed representation of our value. */ | |
1152 | val = ada_to_fixed_value (val); | |
1153 | ||
7d45f3df YQ |
1154 | if (value_lazy (val)) |
1155 | value_fetch_lazy (val); | |
1156 | ||
e8b24d9f | 1157 | val_print (value_type (val), |
a1f5dd1b TT |
1158 | value_embedded_offset (val), value_address (val), |
1159 | stream, recurse, | |
1160 | val, options, language); | |
806048c6 DJ |
1161 | } |
1162 | ||
7348c5e1 | 1163 | /* Print on stream STREAM the value VAL according to OPTIONS. The value |
8e069a98 | 1164 | is printed using the current_language syntax. */ |
7348c5e1 | 1165 | |
8e069a98 | 1166 | void |
79a45b7d TT |
1167 | value_print (struct value *val, struct ui_file *stream, |
1168 | const struct value_print_options *options) | |
806048c6 | 1169 | { |
6501578c | 1170 | if (!value_check_printable (val, stream, options)) |
8e069a98 | 1171 | return; |
806048c6 | 1172 | |
a6bac58e TT |
1173 | if (!options->raw) |
1174 | { | |
6dddc817 DE |
1175 | int r |
1176 | = apply_ext_lang_val_pretty_printer (value_type (val), | |
6dddc817 DE |
1177 | value_embedded_offset (val), |
1178 | value_address (val), | |
1179 | stream, 0, | |
1180 | val, options, current_language); | |
a109c7c1 | 1181 | |
a6bac58e | 1182 | if (r) |
8e069a98 | 1183 | return; |
a6bac58e TT |
1184 | } |
1185 | ||
8e069a98 | 1186 | LA_VALUE_PRINT (val, stream, options); |
c906108c SS |
1187 | } |
1188 | ||
81516450 | 1189 | static void |
4f2aea11 MK |
1190 | val_print_type_code_flags (struct type *type, const gdb_byte *valaddr, |
1191 | struct ui_file *stream) | |
1192 | { | |
befae759 | 1193 | ULONGEST val = unpack_long (type, valaddr); |
81516450 DE |
1194 | int field, nfields = TYPE_NFIELDS (type); |
1195 | struct gdbarch *gdbarch = get_type_arch (type); | |
1196 | struct type *bool_type = builtin_type (gdbarch)->builtin_bool; | |
4f2aea11 | 1197 | |
81516450 DE |
1198 | fputs_filtered ("[", stream); |
1199 | for (field = 0; field < nfields; field++) | |
4f2aea11 | 1200 | { |
81516450 | 1201 | if (TYPE_FIELD_NAME (type, field)[0] != '\0') |
4f2aea11 | 1202 | { |
81516450 DE |
1203 | struct type *field_type = TYPE_FIELD_TYPE (type, field); |
1204 | ||
1205 | if (field_type == bool_type | |
1206 | /* We require boolean types here to be one bit wide. This is a | |
1207 | problematic place to notify the user of an internal error | |
1208 | though. Instead just fall through and print the field as an | |
1209 | int. */ | |
1210 | && TYPE_FIELD_BITSIZE (type, field) == 1) | |
1211 | { | |
1212 | if (val & ((ULONGEST)1 << TYPE_FIELD_BITPOS (type, field))) | |
1213 | fprintf_filtered (stream, " %s", | |
1214 | TYPE_FIELD_NAME (type, field)); | |
1215 | } | |
4f2aea11 | 1216 | else |
81516450 DE |
1217 | { |
1218 | unsigned field_len = TYPE_FIELD_BITSIZE (type, field); | |
1219 | ULONGEST field_val | |
1220 | = val >> (TYPE_FIELD_BITPOS (type, field) - field_len + 1); | |
1221 | ||
1222 | if (field_len < sizeof (ULONGEST) * TARGET_CHAR_BIT) | |
1223 | field_val &= ((ULONGEST) 1 << field_len) - 1; | |
1224 | fprintf_filtered (stream, " %s=", | |
1225 | TYPE_FIELD_NAME (type, field)); | |
1226 | if (TYPE_CODE (field_type) == TYPE_CODE_ENUM) | |
1227 | generic_val_print_enum_1 (field_type, field_val, stream); | |
1228 | else | |
1229 | print_longest (stream, 'd', 0, field_val); | |
1230 | } | |
4f2aea11 MK |
1231 | } |
1232 | } | |
81516450 | 1233 | fputs_filtered (" ]", stream); |
19c37f24 | 1234 | } |
ab2188aa PA |
1235 | |
1236 | /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR, | |
1237 | according to OPTIONS and SIZE on STREAM. Format i is not supported | |
1238 | at this level. | |
1239 | ||
1240 | This is how the elements of an array or structure are printed | |
1241 | with a format. */ | |
ab2188aa PA |
1242 | |
1243 | void | |
1244 | val_print_scalar_formatted (struct type *type, | |
e8b24d9f YQ |
1245 | LONGEST embedded_offset, |
1246 | struct value *val, | |
ab2188aa PA |
1247 | const struct value_print_options *options, |
1248 | int size, | |
1249 | struct ui_file *stream) | |
1250 | { | |
3ae385af SM |
1251 | struct gdbarch *arch = get_type_arch (type); |
1252 | int unit_size = gdbarch_addressable_memory_unit_size (arch); | |
1253 | ||
ab2188aa | 1254 | gdb_assert (val != NULL); |
ab2188aa PA |
1255 | |
1256 | /* If we get here with a string format, try again without it. Go | |
1257 | all the way back to the language printers, which may call us | |
1258 | again. */ | |
1259 | if (options->format == 's') | |
1260 | { | |
1261 | struct value_print_options opts = *options; | |
1262 | opts.format = 0; | |
1263 | opts.deref_ref = 0; | |
e8b24d9f | 1264 | val_print (type, embedded_offset, 0, stream, 0, val, &opts, |
ab2188aa PA |
1265 | current_language); |
1266 | return; | |
1267 | } | |
1268 | ||
e8b24d9f YQ |
1269 | /* value_contents_for_printing fetches all VAL's contents. They are |
1270 | needed to check whether VAL is optimized-out or unavailable | |
1271 | below. */ | |
1272 | const gdb_byte *valaddr = value_contents_for_printing (val); | |
1273 | ||
ab2188aa PA |
1274 | /* A scalar object that does not have all bits available can't be |
1275 | printed, because all bits contribute to its representation. */ | |
9a0dc9e3 PA |
1276 | if (value_bits_any_optimized_out (val, |
1277 | TARGET_CHAR_BIT * embedded_offset, | |
1278 | TARGET_CHAR_BIT * TYPE_LENGTH (type))) | |
901461f8 | 1279 | val_print_optimized_out (val, stream); |
4e07d55f PA |
1280 | else if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) |
1281 | val_print_unavailable (stream); | |
ab2188aa | 1282 | else |
3ae385af | 1283 | print_scalar_formatted (valaddr + embedded_offset * unit_size, type, |
ab2188aa | 1284 | options, size, stream); |
4f2aea11 MK |
1285 | } |
1286 | ||
c906108c SS |
1287 | /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g. |
1288 | The raison d'etre of this function is to consolidate printing of | |
581e13c1 | 1289 | LONG_LONG's into this one function. The format chars b,h,w,g are |
bb599908 | 1290 | from print_scalar_formatted(). Numbers are printed using C |
581e13c1 | 1291 | format. |
bb599908 PH |
1292 | |
1293 | USE_C_FORMAT means to use C format in all cases. Without it, | |
1294 | 'o' and 'x' format do not include the standard C radix prefix | |
1295 | (leading 0 or 0x). | |
1296 | ||
1297 | Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL | |
1298 | and was intended to request formating according to the current | |
1299 | language and would be used for most integers that GDB prints. The | |
1300 | exceptional cases were things like protocols where the format of | |
1301 | the integer is a protocol thing, not a user-visible thing). The | |
1302 | parameter remains to preserve the information of what things might | |
1303 | be printed with language-specific format, should we ever resurrect | |
581e13c1 | 1304 | that capability. */ |
c906108c SS |
1305 | |
1306 | void | |
bb599908 | 1307 | print_longest (struct ui_file *stream, int format, int use_c_format, |
fba45db2 | 1308 | LONGEST val_long) |
c906108c | 1309 | { |
2bfb72ee AC |
1310 | const char *val; |
1311 | ||
c906108c SS |
1312 | switch (format) |
1313 | { | |
1314 | case 'd': | |
bb599908 | 1315 | val = int_string (val_long, 10, 1, 0, 1); break; |
c906108c | 1316 | case 'u': |
bb599908 | 1317 | val = int_string (val_long, 10, 0, 0, 1); break; |
c906108c | 1318 | case 'x': |
bb599908 | 1319 | val = int_string (val_long, 16, 0, 0, use_c_format); break; |
c906108c | 1320 | case 'b': |
bb599908 | 1321 | val = int_string (val_long, 16, 0, 2, 1); break; |
c906108c | 1322 | case 'h': |
bb599908 | 1323 | val = int_string (val_long, 16, 0, 4, 1); break; |
c906108c | 1324 | case 'w': |
bb599908 | 1325 | val = int_string (val_long, 16, 0, 8, 1); break; |
c906108c | 1326 | case 'g': |
bb599908 | 1327 | val = int_string (val_long, 16, 0, 16, 1); break; |
c906108c SS |
1328 | break; |
1329 | case 'o': | |
bb599908 | 1330 | val = int_string (val_long, 8, 0, 0, use_c_format); break; |
c906108c | 1331 | default: |
3e43a32a MS |
1332 | internal_error (__FILE__, __LINE__, |
1333 | _("failed internal consistency check")); | |
bb599908 | 1334 | } |
2bfb72ee | 1335 | fputs_filtered (val, stream); |
c906108c SS |
1336 | } |
1337 | ||
c906108c SS |
1338 | /* This used to be a macro, but I don't think it is called often enough |
1339 | to merit such treatment. */ | |
1340 | /* Convert a LONGEST to an int. This is used in contexts (e.g. number of | |
1341 | arguments to a function, number in a value history, register number, etc.) | |
1342 | where the value must not be larger than can fit in an int. */ | |
1343 | ||
1344 | int | |
fba45db2 | 1345 | longest_to_int (LONGEST arg) |
c906108c | 1346 | { |
581e13c1 | 1347 | /* Let the compiler do the work. */ |
c906108c SS |
1348 | int rtnval = (int) arg; |
1349 | ||
581e13c1 | 1350 | /* Check for overflows or underflows. */ |
c906108c SS |
1351 | if (sizeof (LONGEST) > sizeof (int)) |
1352 | { | |
1353 | if (rtnval != arg) | |
1354 | { | |
8a3fe4f8 | 1355 | error (_("Value out of range.")); |
c906108c SS |
1356 | } |
1357 | } | |
1358 | return (rtnval); | |
1359 | } | |
1360 | ||
fdf0cbc2 UW |
1361 | /* Print a floating point value of floating-point type TYPE, |
1362 | pointed to in GDB by VALADDR, on STREAM. */ | |
c906108c SS |
1363 | |
1364 | void | |
fc1a4b47 | 1365 | print_floating (const gdb_byte *valaddr, struct type *type, |
c84141d6 | 1366 | struct ui_file *stream) |
c906108c | 1367 | { |
f69fdf9b | 1368 | std::string str = target_float_to_string (valaddr, type); |
3b4b2f16 | 1369 | fputs_filtered (str.c_str (), stream); |
7678ef8f TJB |
1370 | } |
1371 | ||
c5aa993b | 1372 | void |
fc1a4b47 | 1373 | print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr, |
30a25466 | 1374 | unsigned len, enum bfd_endian byte_order, bool zero_pad) |
c906108c | 1375 | { |
fc1a4b47 | 1376 | const gdb_byte *p; |
745b8ca0 | 1377 | unsigned int i; |
c5aa993b | 1378 | int b; |
30a25466 | 1379 | bool seen_a_one = false; |
c906108c SS |
1380 | |
1381 | /* Declared "int" so it will be signed. | |
581e13c1 MS |
1382 | This ensures that right shift will shift in zeros. */ |
1383 | ||
c5aa993b | 1384 | const int mask = 0x080; |
c906108c | 1385 | |
d44e8473 | 1386 | if (byte_order == BFD_ENDIAN_BIG) |
c906108c SS |
1387 | { |
1388 | for (p = valaddr; | |
1389 | p < valaddr + len; | |
1390 | p++) | |
1391 | { | |
c5aa993b | 1392 | /* Every byte has 8 binary characters; peel off |
581e13c1 MS |
1393 | and print from the MSB end. */ |
1394 | ||
d3abe1c8 | 1395 | for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++) |
c5aa993b JM |
1396 | { |
1397 | if (*p & (mask >> i)) | |
30a25466 | 1398 | b = '1'; |
c5aa993b | 1399 | else |
30a25466 | 1400 | b = '0'; |
c5aa993b | 1401 | |
30a25466 TT |
1402 | if (zero_pad || seen_a_one || b == '1') |
1403 | fputc_filtered (b, stream); | |
1404 | if (b == '1') | |
1405 | seen_a_one = true; | |
c5aa993b | 1406 | } |
c906108c SS |
1407 | } |
1408 | } | |
1409 | else | |
1410 | { | |
1411 | for (p = valaddr + len - 1; | |
1412 | p >= valaddr; | |
1413 | p--) | |
1414 | { | |
d3abe1c8 | 1415 | for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++) |
c5aa993b JM |
1416 | { |
1417 | if (*p & (mask >> i)) | |
30a25466 | 1418 | b = '1'; |
c5aa993b | 1419 | else |
30a25466 | 1420 | b = '0'; |
c5aa993b | 1421 | |
30a25466 TT |
1422 | if (zero_pad || seen_a_one || b == '1') |
1423 | fputc_filtered (b, stream); | |
1424 | if (b == '1') | |
1425 | seen_a_one = true; | |
c5aa993b | 1426 | } |
c906108c SS |
1427 | } |
1428 | } | |
30a25466 TT |
1429 | |
1430 | /* When not zero-padding, ensure that something is printed when the | |
1431 | input is 0. */ | |
1432 | if (!zero_pad && !seen_a_one) | |
1433 | fputc_filtered ('0', stream); | |
1434 | } | |
1435 | ||
1436 | /* A helper for print_octal_chars that emits a single octal digit, | |
1437 | optionally suppressing it if is zero and updating SEEN_A_ONE. */ | |
1438 | ||
1439 | static void | |
1440 | emit_octal_digit (struct ui_file *stream, bool *seen_a_one, int digit) | |
1441 | { | |
1442 | if (*seen_a_one || digit != 0) | |
1443 | fprintf_filtered (stream, "%o", digit); | |
1444 | if (digit != 0) | |
1445 | *seen_a_one = true; | |
c906108c SS |
1446 | } |
1447 | ||
1448 | /* VALADDR points to an integer of LEN bytes. | |
581e13c1 MS |
1449 | Print it in octal on stream or format it in buf. */ |
1450 | ||
c906108c | 1451 | void |
fc1a4b47 | 1452 | print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr, |
d44e8473 | 1453 | unsigned len, enum bfd_endian byte_order) |
c906108c | 1454 | { |
fc1a4b47 | 1455 | const gdb_byte *p; |
c906108c | 1456 | unsigned char octa1, octa2, octa3, carry; |
c5aa993b JM |
1457 | int cycle; |
1458 | ||
c906108c SS |
1459 | /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track |
1460 | * the extra bits, which cycle every three bytes: | |
1461 | * | |
1462 | * Byte side: 0 1 2 3 | |
1463 | * | | | | | |
1464 | * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 | | |
1465 | * | |
1466 | * Octal side: 0 1 carry 3 4 carry ... | |
1467 | * | |
1468 | * Cycle number: 0 1 2 | |
1469 | * | |
1470 | * But of course we are printing from the high side, so we have to | |
1471 | * figure out where in the cycle we are so that we end up with no | |
1472 | * left over bits at the end. | |
1473 | */ | |
1474 | #define BITS_IN_OCTAL 3 | |
1475 | #define HIGH_ZERO 0340 | |
d6382fff | 1476 | #define LOW_ZERO 0034 |
c906108c | 1477 | #define CARRY_ZERO 0003 |
d6382fff TT |
1478 | static_assert (HIGH_ZERO + LOW_ZERO + CARRY_ZERO == 0xff, |
1479 | "cycle zero constants are wrong"); | |
c906108c SS |
1480 | #define HIGH_ONE 0200 |
1481 | #define MID_ONE 0160 | |
1482 | #define LOW_ONE 0016 | |
1483 | #define CARRY_ONE 0001 | |
d6382fff TT |
1484 | static_assert (HIGH_ONE + MID_ONE + LOW_ONE + CARRY_ONE == 0xff, |
1485 | "cycle one constants are wrong"); | |
c906108c SS |
1486 | #define HIGH_TWO 0300 |
1487 | #define MID_TWO 0070 | |
1488 | #define LOW_TWO 0007 | |
d6382fff TT |
1489 | static_assert (HIGH_TWO + MID_TWO + LOW_TWO == 0xff, |
1490 | "cycle two constants are wrong"); | |
c906108c SS |
1491 | |
1492 | /* For 32 we start in cycle 2, with two bits and one bit carry; | |
581e13c1 MS |
1493 | for 64 in cycle in cycle 1, with one bit and a two bit carry. */ |
1494 | ||
d3abe1c8 | 1495 | cycle = (len * HOST_CHAR_BIT) % BITS_IN_OCTAL; |
c906108c | 1496 | carry = 0; |
c5aa993b | 1497 | |
bb599908 | 1498 | fputs_filtered ("0", stream); |
30a25466 | 1499 | bool seen_a_one = false; |
d44e8473 | 1500 | if (byte_order == BFD_ENDIAN_BIG) |
c906108c SS |
1501 | { |
1502 | for (p = valaddr; | |
1503 | p < valaddr + len; | |
1504 | p++) | |
1505 | { | |
c5aa993b JM |
1506 | switch (cycle) |
1507 | { | |
1508 | case 0: | |
581e13c1 MS |
1509 | /* No carry in, carry out two bits. */ |
1510 | ||
c5aa993b JM |
1511 | octa1 = (HIGH_ZERO & *p) >> 5; |
1512 | octa2 = (LOW_ZERO & *p) >> 2; | |
1513 | carry = (CARRY_ZERO & *p); | |
30a25466 TT |
1514 | emit_octal_digit (stream, &seen_a_one, octa1); |
1515 | emit_octal_digit (stream, &seen_a_one, octa2); | |
c5aa993b JM |
1516 | break; |
1517 | ||
1518 | case 1: | |
581e13c1 MS |
1519 | /* Carry in two bits, carry out one bit. */ |
1520 | ||
c5aa993b JM |
1521 | octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7); |
1522 | octa2 = (MID_ONE & *p) >> 4; | |
1523 | octa3 = (LOW_ONE & *p) >> 1; | |
1524 | carry = (CARRY_ONE & *p); | |
30a25466 TT |
1525 | emit_octal_digit (stream, &seen_a_one, octa1); |
1526 | emit_octal_digit (stream, &seen_a_one, octa2); | |
1527 | emit_octal_digit (stream, &seen_a_one, octa3); | |
c5aa993b JM |
1528 | break; |
1529 | ||
1530 | case 2: | |
581e13c1 MS |
1531 | /* Carry in one bit, no carry out. */ |
1532 | ||
c5aa993b JM |
1533 | octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6); |
1534 | octa2 = (MID_TWO & *p) >> 3; | |
1535 | octa3 = (LOW_TWO & *p); | |
1536 | carry = 0; | |
30a25466 TT |
1537 | emit_octal_digit (stream, &seen_a_one, octa1); |
1538 | emit_octal_digit (stream, &seen_a_one, octa2); | |
1539 | emit_octal_digit (stream, &seen_a_one, octa3); | |
c5aa993b JM |
1540 | break; |
1541 | ||
1542 | default: | |
8a3fe4f8 | 1543 | error (_("Internal error in octal conversion;")); |
c5aa993b JM |
1544 | } |
1545 | ||
1546 | cycle++; | |
1547 | cycle = cycle % BITS_IN_OCTAL; | |
c906108c SS |
1548 | } |
1549 | } | |
1550 | else | |
1551 | { | |
1552 | for (p = valaddr + len - 1; | |
1553 | p >= valaddr; | |
1554 | p--) | |
1555 | { | |
c5aa993b JM |
1556 | switch (cycle) |
1557 | { | |
1558 | case 0: | |
1559 | /* Carry out, no carry in */ | |
581e13c1 | 1560 | |
c5aa993b JM |
1561 | octa1 = (HIGH_ZERO & *p) >> 5; |
1562 | octa2 = (LOW_ZERO & *p) >> 2; | |
1563 | carry = (CARRY_ZERO & *p); | |
30a25466 TT |
1564 | emit_octal_digit (stream, &seen_a_one, octa1); |
1565 | emit_octal_digit (stream, &seen_a_one, octa2); | |
c5aa993b JM |
1566 | break; |
1567 | ||
1568 | case 1: | |
1569 | /* Carry in, carry out */ | |
581e13c1 | 1570 | |
c5aa993b JM |
1571 | octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7); |
1572 | octa2 = (MID_ONE & *p) >> 4; | |
1573 | octa3 = (LOW_ONE & *p) >> 1; | |
1574 | carry = (CARRY_ONE & *p); | |
30a25466 TT |
1575 | emit_octal_digit (stream, &seen_a_one, octa1); |
1576 | emit_octal_digit (stream, &seen_a_one, octa2); | |
1577 | emit_octal_digit (stream, &seen_a_one, octa3); | |
c5aa993b JM |
1578 | break; |
1579 | ||
1580 | case 2: | |
1581 | /* Carry in, no carry out */ | |
581e13c1 | 1582 | |
c5aa993b JM |
1583 | octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6); |
1584 | octa2 = (MID_TWO & *p) >> 3; | |
1585 | octa3 = (LOW_TWO & *p); | |
1586 | carry = 0; | |
30a25466 TT |
1587 | emit_octal_digit (stream, &seen_a_one, octa1); |
1588 | emit_octal_digit (stream, &seen_a_one, octa2); | |
1589 | emit_octal_digit (stream, &seen_a_one, octa3); | |
c5aa993b JM |
1590 | break; |
1591 | ||
1592 | default: | |
8a3fe4f8 | 1593 | error (_("Internal error in octal conversion;")); |
c5aa993b JM |
1594 | } |
1595 | ||
1596 | cycle++; | |
1597 | cycle = cycle % BITS_IN_OCTAL; | |
c906108c SS |
1598 | } |
1599 | } | |
1600 | ||
c906108c SS |
1601 | } |
1602 | ||
4ac0cb1c TT |
1603 | /* Possibly negate the integer represented by BYTES. It contains LEN |
1604 | bytes in the specified byte order. If the integer is negative, | |
1605 | copy it into OUT_VEC, negate it, and return true. Otherwise, do | |
1606 | nothing and return false. */ | |
1607 | ||
1608 | static bool | |
1609 | maybe_negate_by_bytes (const gdb_byte *bytes, unsigned len, | |
1610 | enum bfd_endian byte_order, | |
d5722aa2 | 1611 | gdb::byte_vector *out_vec) |
4ac0cb1c TT |
1612 | { |
1613 | gdb_byte sign_byte; | |
1614 | if (byte_order == BFD_ENDIAN_BIG) | |
1615 | sign_byte = bytes[0]; | |
1616 | else | |
1617 | sign_byte = bytes[len - 1]; | |
1618 | if ((sign_byte & 0x80) == 0) | |
1619 | return false; | |
1620 | ||
1621 | out_vec->resize (len); | |
1622 | ||
1623 | /* Compute -x == 1 + ~x. */ | |
1624 | if (byte_order == BFD_ENDIAN_LITTLE) | |
1625 | { | |
1626 | unsigned carry = 1; | |
1627 | for (unsigned i = 0; i < len; ++i) | |
1628 | { | |
1629 | unsigned tem = (0xff & ~bytes[i]) + carry; | |
1630 | (*out_vec)[i] = tem & 0xff; | |
1631 | carry = tem / 256; | |
1632 | } | |
1633 | } | |
1634 | else | |
1635 | { | |
1636 | unsigned carry = 1; | |
1637 | for (unsigned i = len; i > 0; --i) | |
1638 | { | |
1639 | unsigned tem = (0xff & ~bytes[i - 1]) + carry; | |
1640 | (*out_vec)[i - 1] = tem & 0xff; | |
1641 | carry = tem / 256; | |
1642 | } | |
1643 | } | |
1644 | ||
1645 | return true; | |
1646 | } | |
1647 | ||
c906108c | 1648 | /* VALADDR points to an integer of LEN bytes. |
581e13c1 MS |
1649 | Print it in decimal on stream or format it in buf. */ |
1650 | ||
c906108c | 1651 | void |
fc1a4b47 | 1652 | print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr, |
4ac0cb1c TT |
1653 | unsigned len, bool is_signed, |
1654 | enum bfd_endian byte_order) | |
c906108c SS |
1655 | { |
1656 | #define TEN 10 | |
c5aa993b | 1657 | #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */ |
c906108c SS |
1658 | #define CARRY_LEFT( x ) ((x) % TEN) |
1659 | #define SHIFT( x ) ((x) << 4) | |
c906108c SS |
1660 | #define LOW_NIBBLE( x ) ( (x) & 0x00F) |
1661 | #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4) | |
1662 | ||
fc1a4b47 | 1663 | const gdb_byte *p; |
c5aa993b JM |
1664 | int carry; |
1665 | int decimal_len; | |
1666 | int i, j, decimal_digits; | |
1667 | int dummy; | |
1668 | int flip; | |
1669 | ||
d5722aa2 | 1670 | gdb::byte_vector negated_bytes; |
4ac0cb1c TT |
1671 | if (is_signed |
1672 | && maybe_negate_by_bytes (valaddr, len, byte_order, &negated_bytes)) | |
1673 | { | |
1674 | fputs_filtered ("-", stream); | |
1675 | valaddr = negated_bytes.data (); | |
1676 | } | |
1677 | ||
c906108c | 1678 | /* Base-ten number is less than twice as many digits |
581e13c1 MS |
1679 | as the base 16 number, which is 2 digits per byte. */ |
1680 | ||
c906108c | 1681 | decimal_len = len * 2 * 2; |
30a25466 | 1682 | std::vector<unsigned char> digits (decimal_len, 0); |
c906108c | 1683 | |
c906108c SS |
1684 | /* Ok, we have an unknown number of bytes of data to be printed in |
1685 | * decimal. | |
1686 | * | |
1687 | * Given a hex number (in nibbles) as XYZ, we start by taking X and | |
1688 | * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply | |
1689 | * the nibbles by 16, add Y and re-decimalize. Repeat with Z. | |
1690 | * | |
1691 | * The trick is that "digits" holds a base-10 number, but sometimes | |
581e13c1 | 1692 | * the individual digits are > 10. |
c906108c SS |
1693 | * |
1694 | * Outer loop is per nibble (hex digit) of input, from MSD end to | |
1695 | * LSD end. | |
1696 | */ | |
c5aa993b | 1697 | decimal_digits = 0; /* Number of decimal digits so far */ |
d44e8473 | 1698 | p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1; |
c906108c | 1699 | flip = 0; |
d44e8473 | 1700 | while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr)) |
c5aa993b | 1701 | { |
c906108c SS |
1702 | /* |
1703 | * Multiply current base-ten number by 16 in place. | |
1704 | * Each digit was between 0 and 9, now is between | |
1705 | * 0 and 144. | |
1706 | */ | |
c5aa993b JM |
1707 | for (j = 0; j < decimal_digits; j++) |
1708 | { | |
1709 | digits[j] = SHIFT (digits[j]); | |
1710 | } | |
1711 | ||
c906108c SS |
1712 | /* Take the next nibble off the input and add it to what |
1713 | * we've got in the LSB position. Bottom 'digit' is now | |
1714 | * between 0 and 159. | |
1715 | * | |
1716 | * "flip" is used to run this loop twice for each byte. | |
1717 | */ | |
c5aa993b JM |
1718 | if (flip == 0) |
1719 | { | |
581e13c1 MS |
1720 | /* Take top nibble. */ |
1721 | ||
c5aa993b JM |
1722 | digits[0] += HIGH_NIBBLE (*p); |
1723 | flip = 1; | |
1724 | } | |
1725 | else | |
1726 | { | |
581e13c1 MS |
1727 | /* Take low nibble and bump our pointer "p". */ |
1728 | ||
c5aa993b | 1729 | digits[0] += LOW_NIBBLE (*p); |
d44e8473 MD |
1730 | if (byte_order == BFD_ENDIAN_BIG) |
1731 | p++; | |
1732 | else | |
1733 | p--; | |
c5aa993b JM |
1734 | flip = 0; |
1735 | } | |
c906108c SS |
1736 | |
1737 | /* Re-decimalize. We have to do this often enough | |
1738 | * that we don't overflow, but once per nibble is | |
1739 | * overkill. Easier this way, though. Note that the | |
1740 | * carry is often larger than 10 (e.g. max initial | |
1741 | * carry out of lowest nibble is 15, could bubble all | |
1742 | * the way up greater than 10). So we have to do | |
1743 | * the carrying beyond the last current digit. | |
1744 | */ | |
1745 | carry = 0; | |
c5aa993b JM |
1746 | for (j = 0; j < decimal_len - 1; j++) |
1747 | { | |
1748 | digits[j] += carry; | |
1749 | ||
1750 | /* "/" won't handle an unsigned char with | |
1751 | * a value that if signed would be negative. | |
1752 | * So extend to longword int via "dummy". | |
1753 | */ | |
1754 | dummy = digits[j]; | |
1755 | carry = CARRY_OUT (dummy); | |
1756 | digits[j] = CARRY_LEFT (dummy); | |
1757 | ||
1758 | if (j >= decimal_digits && carry == 0) | |
1759 | { | |
1760 | /* | |
1761 | * All higher digits are 0 and we | |
1762 | * no longer have a carry. | |
1763 | * | |
1764 | * Note: "j" is 0-based, "decimal_digits" is | |
1765 | * 1-based. | |
1766 | */ | |
1767 | decimal_digits = j + 1; | |
1768 | break; | |
1769 | } | |
1770 | } | |
1771 | } | |
c906108c SS |
1772 | |
1773 | /* Ok, now "digits" is the decimal representation, with | |
581e13c1 MS |
1774 | the "decimal_digits" actual digits. Print! */ |
1775 | ||
30a25466 TT |
1776 | for (i = decimal_digits - 1; i > 0 && digits[i] == 0; --i) |
1777 | ; | |
1778 | ||
1779 | for (; i >= 0; i--) | |
c5aa993b JM |
1780 | { |
1781 | fprintf_filtered (stream, "%1d", digits[i]); | |
1782 | } | |
c906108c SS |
1783 | } |
1784 | ||
1785 | /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */ | |
1786 | ||
6b9acc27 | 1787 | void |
fc1a4b47 | 1788 | print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr, |
30a25466 TT |
1789 | unsigned len, enum bfd_endian byte_order, |
1790 | bool zero_pad) | |
c906108c | 1791 | { |
fc1a4b47 | 1792 | const gdb_byte *p; |
c906108c | 1793 | |
bb599908 | 1794 | fputs_filtered ("0x", stream); |
d44e8473 | 1795 | if (byte_order == BFD_ENDIAN_BIG) |
c906108c | 1796 | { |
30a25466 TT |
1797 | p = valaddr; |
1798 | ||
1799 | if (!zero_pad) | |
1800 | { | |
1801 | /* Strip leading 0 bytes, but be sure to leave at least a | |
1802 | single byte at the end. */ | |
1803 | for (; p < valaddr + len - 1 && !*p; ++p) | |
1804 | ; | |
1805 | } | |
1806 | ||
1807 | const gdb_byte *first = p; | |
1808 | for (; | |
c906108c SS |
1809 | p < valaddr + len; |
1810 | p++) | |
1811 | { | |
30a25466 TT |
1812 | /* When not zero-padding, use a different format for the |
1813 | very first byte printed. */ | |
1814 | if (!zero_pad && p == first) | |
1815 | fprintf_filtered (stream, "%x", *p); | |
1816 | else | |
1817 | fprintf_filtered (stream, "%02x", *p); | |
c906108c SS |
1818 | } |
1819 | } | |
1820 | else | |
1821 | { | |
30a25466 TT |
1822 | p = valaddr + len - 1; |
1823 | ||
1824 | if (!zero_pad) | |
1825 | { | |
1826 | /* Strip leading 0 bytes, but be sure to leave at least a | |
1827 | single byte at the end. */ | |
1828 | for (; p >= valaddr + 1 && !*p; --p) | |
1829 | ; | |
1830 | } | |
1831 | ||
1832 | const gdb_byte *first = p; | |
1833 | for (; | |
c906108c SS |
1834 | p >= valaddr; |
1835 | p--) | |
1836 | { | |
30a25466 TT |
1837 | /* When not zero-padding, use a different format for the |
1838 | very first byte printed. */ | |
1839 | if (!zero_pad && p == first) | |
1840 | fprintf_filtered (stream, "%x", *p); | |
1841 | else | |
1842 | fprintf_filtered (stream, "%02x", *p); | |
c906108c SS |
1843 | } |
1844 | } | |
c906108c SS |
1845 | } |
1846 | ||
3e43a32a | 1847 | /* VALADDR points to a char integer of LEN bytes. |
581e13c1 | 1848 | Print it out in appropriate language form on stream. |
6b9acc27 JJ |
1849 | Omit any leading zero chars. */ |
1850 | ||
1851 | void | |
6c7a06a3 TT |
1852 | print_char_chars (struct ui_file *stream, struct type *type, |
1853 | const gdb_byte *valaddr, | |
d44e8473 | 1854 | unsigned len, enum bfd_endian byte_order) |
6b9acc27 | 1855 | { |
fc1a4b47 | 1856 | const gdb_byte *p; |
6b9acc27 | 1857 | |
d44e8473 | 1858 | if (byte_order == BFD_ENDIAN_BIG) |
6b9acc27 JJ |
1859 | { |
1860 | p = valaddr; | |
1861 | while (p < valaddr + len - 1 && *p == 0) | |
1862 | ++p; | |
1863 | ||
1864 | while (p < valaddr + len) | |
1865 | { | |
6c7a06a3 | 1866 | LA_EMIT_CHAR (*p, type, stream, '\''); |
6b9acc27 JJ |
1867 | ++p; |
1868 | } | |
1869 | } | |
1870 | else | |
1871 | { | |
1872 | p = valaddr + len - 1; | |
1873 | while (p > valaddr && *p == 0) | |
1874 | --p; | |
1875 | ||
1876 | while (p >= valaddr) | |
1877 | { | |
6c7a06a3 | 1878 | LA_EMIT_CHAR (*p, type, stream, '\''); |
6b9acc27 JJ |
1879 | --p; |
1880 | } | |
1881 | } | |
1882 | } | |
1883 | ||
132c57b4 TT |
1884 | /* Print function pointer with inferior address ADDRESS onto stdio |
1885 | stream STREAM. */ | |
1886 | ||
1887 | void | |
edf0c1b7 TT |
1888 | print_function_pointer_address (const struct value_print_options *options, |
1889 | struct gdbarch *gdbarch, | |
132c57b4 | 1890 | CORE_ADDR address, |
edf0c1b7 | 1891 | struct ui_file *stream) |
132c57b4 TT |
1892 | { |
1893 | CORE_ADDR func_addr | |
1894 | = gdbarch_convert_from_func_ptr_addr (gdbarch, address, | |
1895 | ¤t_target); | |
1896 | ||
1897 | /* If the function pointer is represented by a description, print | |
1898 | the address of the description. */ | |
edf0c1b7 | 1899 | if (options->addressprint && func_addr != address) |
132c57b4 TT |
1900 | { |
1901 | fputs_filtered ("@", stream); | |
1902 | fputs_filtered (paddress (gdbarch, address), stream); | |
1903 | fputs_filtered (": ", stream); | |
1904 | } | |
edf0c1b7 | 1905 | print_address_demangle (options, gdbarch, func_addr, stream, demangle); |
132c57b4 TT |
1906 | } |
1907 | ||
1908 | ||
79a45b7d | 1909 | /* Print on STREAM using the given OPTIONS the index for the element |
e79af960 JB |
1910 | at INDEX of an array whose index type is INDEX_TYPE. */ |
1911 | ||
1912 | void | |
1913 | maybe_print_array_index (struct type *index_type, LONGEST index, | |
79a45b7d TT |
1914 | struct ui_file *stream, |
1915 | const struct value_print_options *options) | |
e79af960 JB |
1916 | { |
1917 | struct value *index_value; | |
1918 | ||
79a45b7d | 1919 | if (!options->print_array_indexes) |
e79af960 JB |
1920 | return; |
1921 | ||
1922 | index_value = value_from_longest (index_type, index); | |
1923 | ||
79a45b7d TT |
1924 | LA_PRINT_ARRAY_INDEX (index_value, stream, options); |
1925 | } | |
e79af960 | 1926 | |
c906108c | 1927 | /* Called by various <lang>_val_print routines to print elements of an |
c5aa993b | 1928 | array in the form "<elem1>, <elem2>, <elem3>, ...". |
c906108c | 1929 | |
c5aa993b JM |
1930 | (FIXME?) Assumes array element separator is a comma, which is correct |
1931 | for all languages currently handled. | |
1932 | (FIXME?) Some languages have a notation for repeated array elements, | |
581e13c1 | 1933 | perhaps we should try to use that notation when appropriate. */ |
c906108c SS |
1934 | |
1935 | void | |
490f124f | 1936 | val_print_array_elements (struct type *type, |
e8b24d9f | 1937 | LONGEST embedded_offset, |
a2bd3dcd | 1938 | CORE_ADDR address, struct ui_file *stream, |
79a45b7d | 1939 | int recurse, |
e8b24d9f | 1940 | struct value *val, |
79a45b7d | 1941 | const struct value_print_options *options, |
fba45db2 | 1942 | unsigned int i) |
c906108c SS |
1943 | { |
1944 | unsigned int things_printed = 0; | |
1945 | unsigned len; | |
aa715135 | 1946 | struct type *elttype, *index_type, *base_index_type; |
c906108c SS |
1947 | unsigned eltlen; |
1948 | /* Position of the array element we are examining to see | |
1949 | whether it is repeated. */ | |
1950 | unsigned int rep1; | |
1951 | /* Number of repetitions we have detected so far. */ | |
1952 | unsigned int reps; | |
dbc98a8b | 1953 | LONGEST low_bound, high_bound; |
aa715135 | 1954 | LONGEST low_pos, high_pos; |
c5aa993b | 1955 | |
c906108c | 1956 | elttype = TYPE_TARGET_TYPE (type); |
3ae385af | 1957 | eltlen = type_length_units (check_typedef (elttype)); |
e79af960 | 1958 | index_type = TYPE_INDEX_TYPE (type); |
c906108c | 1959 | |
dbc98a8b | 1960 | if (get_array_bounds (type, &low_bound, &high_bound)) |
75be741b | 1961 | { |
aa715135 JG |
1962 | if (TYPE_CODE (index_type) == TYPE_CODE_RANGE) |
1963 | base_index_type = TYPE_TARGET_TYPE (index_type); | |
1964 | else | |
1965 | base_index_type = index_type; | |
1966 | ||
1967 | /* Non-contiguous enumerations types can by used as index types | |
1968 | in some languages (e.g. Ada). In this case, the array length | |
1969 | shall be computed from the positions of the first and last | |
1970 | literal in the enumeration type, and not from the values | |
1971 | of these literals. */ | |
1972 | if (!discrete_position (base_index_type, low_bound, &low_pos) | |
1973 | || !discrete_position (base_index_type, high_bound, &high_pos)) | |
1974 | { | |
1975 | warning (_("unable to get positions in array, use bounds instead")); | |
1976 | low_pos = low_bound; | |
1977 | high_pos = high_bound; | |
1978 | } | |
1979 | ||
1980 | /* The array length should normally be HIGH_POS - LOW_POS + 1. | |
75be741b | 1981 | But we have to be a little extra careful, because some languages |
aa715135 | 1982 | such as Ada allow LOW_POS to be greater than HIGH_POS for |
75be741b JB |
1983 | empty arrays. In that situation, the array length is just zero, |
1984 | not negative! */ | |
aa715135 | 1985 | if (low_pos > high_pos) |
75be741b JB |
1986 | len = 0; |
1987 | else | |
aa715135 | 1988 | len = high_pos - low_pos + 1; |
75be741b | 1989 | } |
e936309c JB |
1990 | else |
1991 | { | |
dbc98a8b KW |
1992 | warning (_("unable to get bounds of array, assuming null array")); |
1993 | low_bound = 0; | |
1994 | len = 0; | |
168de233 JB |
1995 | } |
1996 | ||
c906108c SS |
1997 | annotate_array_section_begin (i, elttype); |
1998 | ||
79a45b7d | 1999 | for (; i < len && things_printed < options->print_max; i++) |
c906108c SS |
2000 | { |
2001 | if (i != 0) | |
2002 | { | |
2a998fc0 | 2003 | if (options->prettyformat_arrays) |
c906108c SS |
2004 | { |
2005 | fprintf_filtered (stream, ",\n"); | |
2006 | print_spaces_filtered (2 + 2 * recurse, stream); | |
2007 | } | |
2008 | else | |
2009 | { | |
2010 | fprintf_filtered (stream, ", "); | |
2011 | } | |
2012 | } | |
2013 | wrap_here (n_spaces (2 + 2 * recurse)); | |
dbc98a8b | 2014 | maybe_print_array_index (index_type, i + low_bound, |
79a45b7d | 2015 | stream, options); |
c906108c SS |
2016 | |
2017 | rep1 = i + 1; | |
2018 | reps = 1; | |
35bef4fd TT |
2019 | /* Only check for reps if repeat_count_threshold is not set to |
2020 | UINT_MAX (unlimited). */ | |
2021 | if (options->repeat_count_threshold < UINT_MAX) | |
c906108c | 2022 | { |
35bef4fd | 2023 | while (rep1 < len |
9a0dc9e3 PA |
2024 | && value_contents_eq (val, |
2025 | embedded_offset + i * eltlen, | |
2026 | val, | |
2027 | (embedded_offset | |
2028 | + rep1 * eltlen), | |
2029 | eltlen)) | |
35bef4fd TT |
2030 | { |
2031 | ++reps; | |
2032 | ++rep1; | |
2033 | } | |
c906108c SS |
2034 | } |
2035 | ||
79a45b7d | 2036 | if (reps > options->repeat_count_threshold) |
c906108c | 2037 | { |
e8b24d9f | 2038 | val_print (elttype, embedded_offset + i * eltlen, |
490f124f PA |
2039 | address, stream, recurse + 1, val, options, |
2040 | current_language); | |
c906108c SS |
2041 | annotate_elt_rep (reps); |
2042 | fprintf_filtered (stream, " <repeats %u times>", reps); | |
2043 | annotate_elt_rep_end (); | |
2044 | ||
2045 | i = rep1 - 1; | |
79a45b7d | 2046 | things_printed += options->repeat_count_threshold; |
c906108c SS |
2047 | } |
2048 | else | |
2049 | { | |
e8b24d9f | 2050 | val_print (elttype, embedded_offset + i * eltlen, |
490f124f | 2051 | address, |
0e03807e | 2052 | stream, recurse + 1, val, options, current_language); |
c906108c SS |
2053 | annotate_elt (); |
2054 | things_printed++; | |
2055 | } | |
2056 | } | |
2057 | annotate_array_section_end (); | |
2058 | if (i < len) | |
2059 | { | |
2060 | fprintf_filtered (stream, "..."); | |
2061 | } | |
2062 | } | |
2063 | ||
917317f4 JM |
2064 | /* Read LEN bytes of target memory at address MEMADDR, placing the |
2065 | results in GDB's memory at MYADDR. Returns a count of the bytes | |
9b409511 | 2066 | actually read, and optionally a target_xfer_status value in the |
578d3588 | 2067 | location pointed to by ERRPTR if ERRPTR is non-null. */ |
917317f4 JM |
2068 | |
2069 | /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this | |
2070 | function be eliminated. */ | |
2071 | ||
2072 | static int | |
3e43a32a | 2073 | partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, |
578d3588 | 2074 | int len, int *errptr) |
917317f4 | 2075 | { |
581e13c1 MS |
2076 | int nread; /* Number of bytes actually read. */ |
2077 | int errcode; /* Error from last read. */ | |
917317f4 | 2078 | |
581e13c1 | 2079 | /* First try a complete read. */ |
917317f4 JM |
2080 | errcode = target_read_memory (memaddr, myaddr, len); |
2081 | if (errcode == 0) | |
2082 | { | |
581e13c1 | 2083 | /* Got it all. */ |
917317f4 JM |
2084 | nread = len; |
2085 | } | |
2086 | else | |
2087 | { | |
581e13c1 | 2088 | /* Loop, reading one byte at a time until we get as much as we can. */ |
917317f4 JM |
2089 | for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--) |
2090 | { | |
2091 | errcode = target_read_memory (memaddr++, myaddr++, 1); | |
2092 | } | |
581e13c1 | 2093 | /* If an error, the last read was unsuccessful, so adjust count. */ |
917317f4 JM |
2094 | if (errcode != 0) |
2095 | { | |
2096 | nread--; | |
2097 | } | |
2098 | } | |
578d3588 | 2099 | if (errptr != NULL) |
917317f4 | 2100 | { |
578d3588 | 2101 | *errptr = errcode; |
917317f4 JM |
2102 | } |
2103 | return (nread); | |
2104 | } | |
2105 | ||
ae6a3a4c TJB |
2106 | /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes |
2107 | each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly | |
2108 | allocated buffer containing the string, which the caller is responsible to | |
2109 | free, and BYTES_READ will be set to the number of bytes read. Returns 0 on | |
9b409511 | 2110 | success, or a target_xfer_status on failure. |
ae6a3a4c | 2111 | |
f380848e SA |
2112 | If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters |
2113 | (including eventual NULs in the middle or end of the string). | |
2114 | ||
2115 | If LEN is -1, stops at the first null character (not necessarily | |
2116 | the first null byte) up to a maximum of FETCHLIMIT characters. Set | |
2117 | FETCHLIMIT to UINT_MAX to read as many characters as possible from | |
2118 | the string. | |
ae6a3a4c TJB |
2119 | |
2120 | Unless an exception is thrown, BUFFER will always be allocated, even on | |
2121 | failure. In this case, some characters might have been read before the | |
2122 | failure happened. Check BYTES_READ to recognize this situation. | |
2123 | ||
2124 | Note: There was a FIXME asking to make this code use target_read_string, | |
2125 | but this function is more general (can read past null characters, up to | |
581e13c1 | 2126 | given LEN). Besides, it is used much more often than target_read_string |
ae6a3a4c TJB |
2127 | so it is more tested. Perhaps callers of target_read_string should use |
2128 | this function instead? */ | |
c906108c SS |
2129 | |
2130 | int | |
ae6a3a4c | 2131 | read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit, |
e17a4113 | 2132 | enum bfd_endian byte_order, gdb_byte **buffer, int *bytes_read) |
c906108c | 2133 | { |
ae6a3a4c TJB |
2134 | int errcode; /* Errno returned from bad reads. */ |
2135 | unsigned int nfetch; /* Chars to fetch / chars fetched. */ | |
3e43a32a MS |
2136 | gdb_byte *bufptr; /* Pointer to next available byte in |
2137 | buffer. */ | |
ae6a3a4c TJB |
2138 | struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */ |
2139 | ||
ae6a3a4c TJB |
2140 | /* Loop until we either have all the characters, or we encounter |
2141 | some error, such as bumping into the end of the address space. */ | |
c906108c | 2142 | |
b5096abe PM |
2143 | *buffer = NULL; |
2144 | ||
2145 | old_chain = make_cleanup (free_current_contents, buffer); | |
c906108c SS |
2146 | |
2147 | if (len > 0) | |
2148 | { | |
88db67ef YQ |
2149 | /* We want fetchlimit chars, so we might as well read them all in |
2150 | one operation. */ | |
325fac50 | 2151 | unsigned int fetchlen = std::min ((unsigned) len, fetchlimit); |
f380848e SA |
2152 | |
2153 | *buffer = (gdb_byte *) xmalloc (fetchlen * width); | |
ae6a3a4c | 2154 | bufptr = *buffer; |
c906108c | 2155 | |
f380848e | 2156 | nfetch = partial_memory_read (addr, bufptr, fetchlen * width, &errcode) |
c906108c SS |
2157 | / width; |
2158 | addr += nfetch * width; | |
2159 | bufptr += nfetch * width; | |
2160 | } | |
2161 | else if (len == -1) | |
2162 | { | |
2163 | unsigned long bufsize = 0; | |
88db67ef YQ |
2164 | unsigned int chunksize; /* Size of each fetch, in chars. */ |
2165 | int found_nul; /* Non-zero if we found the nul char. */ | |
2166 | gdb_byte *limit; /* First location past end of fetch buffer. */ | |
2167 | ||
2168 | found_nul = 0; | |
2169 | /* We are looking for a NUL terminator to end the fetching, so we | |
2170 | might as well read in blocks that are large enough to be efficient, | |
2171 | but not so large as to be slow if fetchlimit happens to be large. | |
2172 | So we choose the minimum of 8 and fetchlimit. We used to use 200 | |
2173 | instead of 8 but 200 is way too big for remote debugging over a | |
2174 | serial line. */ | |
325fac50 | 2175 | chunksize = std::min (8u, fetchlimit); |
ae6a3a4c | 2176 | |
c906108c SS |
2177 | do |
2178 | { | |
2179 | QUIT; | |
325fac50 | 2180 | nfetch = std::min ((unsigned long) chunksize, fetchlimit - bufsize); |
c906108c | 2181 | |
ae6a3a4c TJB |
2182 | if (*buffer == NULL) |
2183 | *buffer = (gdb_byte *) xmalloc (nfetch * width); | |
c906108c | 2184 | else |
b5096abe PM |
2185 | *buffer = (gdb_byte *) xrealloc (*buffer, |
2186 | (nfetch + bufsize) * width); | |
c906108c | 2187 | |
ae6a3a4c | 2188 | bufptr = *buffer + bufsize * width; |
c906108c SS |
2189 | bufsize += nfetch; |
2190 | ||
ae6a3a4c | 2191 | /* Read as much as we can. */ |
917317f4 | 2192 | nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode) |
ae6a3a4c | 2193 | / width; |
c906108c | 2194 | |
ae6a3a4c | 2195 | /* Scan this chunk for the null character that terminates the string |
c906108c SS |
2196 | to print. If found, we don't need to fetch any more. Note |
2197 | that bufptr is explicitly left pointing at the next character | |
ae6a3a4c TJB |
2198 | after the null character, or at the next character after the end |
2199 | of the buffer. */ | |
c906108c SS |
2200 | |
2201 | limit = bufptr + nfetch * width; | |
2202 | while (bufptr < limit) | |
2203 | { | |
2204 | unsigned long c; | |
2205 | ||
e17a4113 | 2206 | c = extract_unsigned_integer (bufptr, width, byte_order); |
c906108c SS |
2207 | addr += width; |
2208 | bufptr += width; | |
2209 | if (c == 0) | |
2210 | { | |
2211 | /* We don't care about any error which happened after | |
ae6a3a4c | 2212 | the NUL terminator. */ |
c906108c SS |
2213 | errcode = 0; |
2214 | found_nul = 1; | |
2215 | break; | |
2216 | } | |
2217 | } | |
2218 | } | |
c5aa993b | 2219 | while (errcode == 0 /* no error */ |
ae6a3a4c TJB |
2220 | && bufptr - *buffer < fetchlimit * width /* no overrun */ |
2221 | && !found_nul); /* haven't found NUL yet */ | |
c906108c SS |
2222 | } |
2223 | else | |
ae6a3a4c TJB |
2224 | { /* Length of string is really 0! */ |
2225 | /* We always allocate *buffer. */ | |
224c3ddb | 2226 | *buffer = bufptr = (gdb_byte *) xmalloc (1); |
c906108c SS |
2227 | errcode = 0; |
2228 | } | |
2229 | ||
2230 | /* bufptr and addr now point immediately beyond the last byte which we | |
2231 | consider part of the string (including a '\0' which ends the string). */ | |
ae6a3a4c TJB |
2232 | *bytes_read = bufptr - *buffer; |
2233 | ||
2234 | QUIT; | |
2235 | ||
2236 | discard_cleanups (old_chain); | |
2237 | ||
2238 | return errcode; | |
2239 | } | |
2240 | ||
3b2b8fea TT |
2241 | /* Return true if print_wchar can display W without resorting to a |
2242 | numeric escape, false otherwise. */ | |
2243 | ||
2244 | static int | |
2245 | wchar_printable (gdb_wchar_t w) | |
2246 | { | |
2247 | return (gdb_iswprint (w) | |
2248 | || w == LCST ('\a') || w == LCST ('\b') | |
2249 | || w == LCST ('\f') || w == LCST ('\n') | |
2250 | || w == LCST ('\r') || w == LCST ('\t') | |
2251 | || w == LCST ('\v')); | |
2252 | } | |
2253 | ||
2254 | /* A helper function that converts the contents of STRING to wide | |
2255 | characters and then appends them to OUTPUT. */ | |
2256 | ||
2257 | static void | |
2258 | append_string_as_wide (const char *string, | |
2259 | struct obstack *output) | |
2260 | { | |
2261 | for (; *string; ++string) | |
2262 | { | |
2263 | gdb_wchar_t w = gdb_btowc (*string); | |
2264 | obstack_grow (output, &w, sizeof (gdb_wchar_t)); | |
2265 | } | |
2266 | } | |
2267 | ||
2268 | /* Print a wide character W to OUTPUT. ORIG is a pointer to the | |
2269 | original (target) bytes representing the character, ORIG_LEN is the | |
2270 | number of valid bytes. WIDTH is the number of bytes in a base | |
2271 | characters of the type. OUTPUT is an obstack to which wide | |
2272 | characters are emitted. QUOTER is a (narrow) character indicating | |
2273 | the style of quotes surrounding the character to be printed. | |
2274 | NEED_ESCAPE is an in/out flag which is used to track numeric | |
2275 | escapes across calls. */ | |
2276 | ||
2277 | static void | |
2278 | print_wchar (gdb_wint_t w, const gdb_byte *orig, | |
2279 | int orig_len, int width, | |
2280 | enum bfd_endian byte_order, | |
2281 | struct obstack *output, | |
2282 | int quoter, int *need_escapep) | |
2283 | { | |
2284 | int need_escape = *need_escapep; | |
2285 | ||
2286 | *need_escapep = 0; | |
3b2b8fea | 2287 | |
95c64f92 YQ |
2288 | /* iswprint implementation on Windows returns 1 for tab character. |
2289 | In order to avoid different printout on this host, we explicitly | |
2290 | use wchar_printable function. */ | |
2291 | switch (w) | |
3b2b8fea | 2292 | { |
95c64f92 YQ |
2293 | case LCST ('\a'): |
2294 | obstack_grow_wstr (output, LCST ("\\a")); | |
2295 | break; | |
2296 | case LCST ('\b'): | |
2297 | obstack_grow_wstr (output, LCST ("\\b")); | |
2298 | break; | |
2299 | case LCST ('\f'): | |
2300 | obstack_grow_wstr (output, LCST ("\\f")); | |
2301 | break; | |
2302 | case LCST ('\n'): | |
2303 | obstack_grow_wstr (output, LCST ("\\n")); | |
2304 | break; | |
2305 | case LCST ('\r'): | |
2306 | obstack_grow_wstr (output, LCST ("\\r")); | |
2307 | break; | |
2308 | case LCST ('\t'): | |
2309 | obstack_grow_wstr (output, LCST ("\\t")); | |
2310 | break; | |
2311 | case LCST ('\v'): | |
2312 | obstack_grow_wstr (output, LCST ("\\v")); | |
2313 | break; | |
2314 | default: | |
3b2b8fea | 2315 | { |
95c64f92 YQ |
2316 | if (wchar_printable (w) && (!need_escape || (!gdb_iswdigit (w) |
2317 | && w != LCST ('8') | |
2318 | && w != LCST ('9')))) | |
2319 | { | |
2320 | gdb_wchar_t wchar = w; | |
3b2b8fea | 2321 | |
95c64f92 YQ |
2322 | if (w == gdb_btowc (quoter) || w == LCST ('\\')) |
2323 | obstack_grow_wstr (output, LCST ("\\")); | |
2324 | obstack_grow (output, &wchar, sizeof (gdb_wchar_t)); | |
2325 | } | |
2326 | else | |
2327 | { | |
2328 | int i; | |
3b2b8fea | 2329 | |
95c64f92 YQ |
2330 | for (i = 0; i + width <= orig_len; i += width) |
2331 | { | |
2332 | char octal[30]; | |
2333 | ULONGEST value; | |
2334 | ||
2335 | value = extract_unsigned_integer (&orig[i], width, | |
3b2b8fea | 2336 | byte_order); |
95c64f92 YQ |
2337 | /* If the value fits in 3 octal digits, print it that |
2338 | way. Otherwise, print it as a hex escape. */ | |
2339 | if (value <= 0777) | |
2340 | xsnprintf (octal, sizeof (octal), "\\%.3o", | |
2341 | (int) (value & 0777)); | |
2342 | else | |
2343 | xsnprintf (octal, sizeof (octal), "\\x%lx", (long) value); | |
2344 | append_string_as_wide (octal, output); | |
2345 | } | |
2346 | /* If we somehow have extra bytes, print them now. */ | |
2347 | while (i < orig_len) | |
2348 | { | |
2349 | char octal[5]; | |
2350 | ||
2351 | xsnprintf (octal, sizeof (octal), "\\%.3o", orig[i] & 0xff); | |
2352 | append_string_as_wide (octal, output); | |
2353 | ++i; | |
2354 | } | |
2355 | ||
2356 | *need_escapep = 1; | |
2357 | } | |
3b2b8fea TT |
2358 | break; |
2359 | } | |
2360 | } | |
2361 | } | |
2362 | ||
2363 | /* Print the character C on STREAM as part of the contents of a | |
2364 | literal string whose delimiter is QUOTER. ENCODING names the | |
2365 | encoding of C. */ | |
2366 | ||
2367 | void | |
2368 | generic_emit_char (int c, struct type *type, struct ui_file *stream, | |
2369 | int quoter, const char *encoding) | |
2370 | { | |
2371 | enum bfd_endian byte_order | |
2372 | = gdbarch_byte_order (get_type_arch (type)); | |
3b2b8fea | 2373 | gdb_byte *buf; |
3b2b8fea TT |
2374 | int need_escape = 0; |
2375 | ||
224c3ddb | 2376 | buf = (gdb_byte *) alloca (TYPE_LENGTH (type)); |
3b2b8fea TT |
2377 | pack_long (buf, type, c); |
2378 | ||
cda6c55b | 2379 | wchar_iterator iter (buf, TYPE_LENGTH (type), encoding, TYPE_LENGTH (type)); |
3b2b8fea TT |
2380 | |
2381 | /* This holds the printable form of the wchar_t data. */ | |
8268c778 | 2382 | auto_obstack wchar_buf; |
3b2b8fea TT |
2383 | |
2384 | while (1) | |
2385 | { | |
2386 | int num_chars; | |
2387 | gdb_wchar_t *chars; | |
2388 | const gdb_byte *buf; | |
2389 | size_t buflen; | |
2390 | int print_escape = 1; | |
2391 | enum wchar_iterate_result result; | |
2392 | ||
cda6c55b | 2393 | num_chars = iter.iterate (&result, &chars, &buf, &buflen); |
3b2b8fea TT |
2394 | if (num_chars < 0) |
2395 | break; | |
2396 | if (num_chars > 0) | |
2397 | { | |
2398 | /* If all characters are printable, print them. Otherwise, | |
2399 | we're going to have to print an escape sequence. We | |
2400 | check all characters because we want to print the target | |
2401 | bytes in the escape sequence, and we don't know character | |
2402 | boundaries there. */ | |
2403 | int i; | |
2404 | ||
2405 | print_escape = 0; | |
2406 | for (i = 0; i < num_chars; ++i) | |
2407 | if (!wchar_printable (chars[i])) | |
2408 | { | |
2409 | print_escape = 1; | |
2410 | break; | |
2411 | } | |
2412 | ||
2413 | if (!print_escape) | |
2414 | { | |
2415 | for (i = 0; i < num_chars; ++i) | |
2416 | print_wchar (chars[i], buf, buflen, | |
2417 | TYPE_LENGTH (type), byte_order, | |
2418 | &wchar_buf, quoter, &need_escape); | |
2419 | } | |
2420 | } | |
2421 | ||
2422 | /* This handles the NUM_CHARS == 0 case as well. */ | |
2423 | if (print_escape) | |
2424 | print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type), | |
2425 | byte_order, &wchar_buf, quoter, &need_escape); | |
2426 | } | |
2427 | ||
2428 | /* The output in the host encoding. */ | |
8268c778 | 2429 | auto_obstack output; |
3b2b8fea TT |
2430 | |
2431 | convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), | |
ac91cd70 | 2432 | (gdb_byte *) obstack_base (&wchar_buf), |
3b2b8fea | 2433 | obstack_object_size (&wchar_buf), |
fff10684 | 2434 | sizeof (gdb_wchar_t), &output, translit_char); |
3b2b8fea TT |
2435 | obstack_1grow (&output, '\0'); |
2436 | ||
79f33898 | 2437 | fputs_filtered ((const char *) obstack_base (&output), stream); |
3b2b8fea TT |
2438 | } |
2439 | ||
0d63ecda KS |
2440 | /* Return the repeat count of the next character/byte in ITER, |
2441 | storing the result in VEC. */ | |
2442 | ||
2443 | static int | |
cda6c55b | 2444 | count_next_character (wchar_iterator *iter, |
0d63ecda KS |
2445 | VEC (converted_character_d) **vec) |
2446 | { | |
2447 | struct converted_character *current; | |
2448 | ||
2449 | if (VEC_empty (converted_character_d, *vec)) | |
2450 | { | |
2451 | struct converted_character tmp; | |
2452 | gdb_wchar_t *chars; | |
2453 | ||
2454 | tmp.num_chars | |
cda6c55b | 2455 | = iter->iterate (&tmp.result, &chars, &tmp.buf, &tmp.buflen); |
0d63ecda KS |
2456 | if (tmp.num_chars > 0) |
2457 | { | |
2458 | gdb_assert (tmp.num_chars < MAX_WCHARS); | |
2459 | memcpy (tmp.chars, chars, tmp.num_chars * sizeof (gdb_wchar_t)); | |
2460 | } | |
2461 | VEC_safe_push (converted_character_d, *vec, &tmp); | |
2462 | } | |
2463 | ||
2464 | current = VEC_last (converted_character_d, *vec); | |
2465 | ||
2466 | /* Count repeated characters or bytes. */ | |
2467 | current->repeat_count = 1; | |
2468 | if (current->num_chars == -1) | |
2469 | { | |
2470 | /* EOF */ | |
2471 | return -1; | |
2472 | } | |
2473 | else | |
2474 | { | |
2475 | gdb_wchar_t *chars; | |
2476 | struct converted_character d; | |
2477 | int repeat; | |
2478 | ||
2479 | d.repeat_count = 0; | |
2480 | ||
2481 | while (1) | |
2482 | { | |
2483 | /* Get the next character. */ | |
cda6c55b | 2484 | d.num_chars = iter->iterate (&d.result, &chars, &d.buf, &d.buflen); |
0d63ecda KS |
2485 | |
2486 | /* If a character was successfully converted, save the character | |
2487 | into the converted character. */ | |
2488 | if (d.num_chars > 0) | |
2489 | { | |
2490 | gdb_assert (d.num_chars < MAX_WCHARS); | |
2491 | memcpy (d.chars, chars, WCHAR_BUFLEN (d.num_chars)); | |
2492 | } | |
2493 | ||
2494 | /* Determine if the current character is the same as this | |
2495 | new character. */ | |
2496 | if (d.num_chars == current->num_chars && d.result == current->result) | |
2497 | { | |
2498 | /* There are two cases to consider: | |
2499 | ||
2500 | 1) Equality of converted character (num_chars > 0) | |
2501 | 2) Equality of non-converted character (num_chars == 0) */ | |
2502 | if ((current->num_chars > 0 | |
2503 | && memcmp (current->chars, d.chars, | |
2504 | WCHAR_BUFLEN (current->num_chars)) == 0) | |
2505 | || (current->num_chars == 0 | |
2506 | && current->buflen == d.buflen | |
2507 | && memcmp (current->buf, d.buf, current->buflen) == 0)) | |
2508 | ++current->repeat_count; | |
2509 | else | |
2510 | break; | |
2511 | } | |
2512 | else | |
2513 | break; | |
2514 | } | |
2515 | ||
2516 | /* Push this next converted character onto the result vector. */ | |
2517 | repeat = current->repeat_count; | |
2518 | VEC_safe_push (converted_character_d, *vec, &d); | |
2519 | return repeat; | |
2520 | } | |
2521 | } | |
2522 | ||
2523 | /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote | |
2524 | character to use with string output. WIDTH is the size of the output | |
2525 | character type. BYTE_ORDER is the the target byte order. OPTIONS | |
2526 | is the user's print options. */ | |
2527 | ||
2528 | static void | |
2529 | print_converted_chars_to_obstack (struct obstack *obstack, | |
2530 | VEC (converted_character_d) *chars, | |
2531 | int quote_char, int width, | |
2532 | enum bfd_endian byte_order, | |
2533 | const struct value_print_options *options) | |
2534 | { | |
2535 | unsigned int idx; | |
2536 | struct converted_character *elem; | |
2537 | enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last; | |
2538 | gdb_wchar_t wide_quote_char = gdb_btowc (quote_char); | |
2539 | int need_escape = 0; | |
2540 | ||
2541 | /* Set the start state. */ | |
2542 | idx = 0; | |
2543 | last = state = START; | |
2544 | elem = NULL; | |
2545 | ||
2546 | while (1) | |
2547 | { | |
2548 | switch (state) | |
2549 | { | |
2550 | case START: | |
2551 | /* Nothing to do. */ | |
2552 | break; | |
2553 | ||
2554 | case SINGLE: | |
2555 | { | |
2556 | int j; | |
2557 | ||
2558 | /* We are outputting a single character | |
2559 | (< options->repeat_count_threshold). */ | |
2560 | ||
2561 | if (last != SINGLE) | |
2562 | { | |
2563 | /* We were outputting some other type of content, so we | |
2564 | must output and a comma and a quote. */ | |
2565 | if (last != START) | |
2566 | obstack_grow_wstr (obstack, LCST (", ")); | |
0d63ecda KS |
2567 | obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t)); |
2568 | } | |
2569 | /* Output the character. */ | |
2570 | for (j = 0; j < elem->repeat_count; ++j) | |
2571 | { | |
2572 | if (elem->result == wchar_iterate_ok) | |
2573 | print_wchar (elem->chars[0], elem->buf, elem->buflen, width, | |
2574 | byte_order, obstack, quote_char, &need_escape); | |
2575 | else | |
2576 | print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, | |
2577 | byte_order, obstack, quote_char, &need_escape); | |
2578 | } | |
2579 | } | |
2580 | break; | |
2581 | ||
2582 | case REPEAT: | |
2583 | { | |
2584 | int j; | |
2585 | char *s; | |
2586 | ||
2587 | /* We are outputting a character with a repeat count | |
2588 | greater than options->repeat_count_threshold. */ | |
2589 | ||
2590 | if (last == SINGLE) | |
2591 | { | |
2592 | /* We were outputting a single string. Terminate the | |
2593 | string. */ | |
0d63ecda KS |
2594 | obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t)); |
2595 | } | |
2596 | if (last != START) | |
2597 | obstack_grow_wstr (obstack, LCST (", ")); | |
2598 | ||
2599 | /* Output the character and repeat string. */ | |
2600 | obstack_grow_wstr (obstack, LCST ("'")); | |
2601 | if (elem->result == wchar_iterate_ok) | |
2602 | print_wchar (elem->chars[0], elem->buf, elem->buflen, width, | |
2603 | byte_order, obstack, quote_char, &need_escape); | |
2604 | else | |
2605 | print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, | |
2606 | byte_order, obstack, quote_char, &need_escape); | |
2607 | obstack_grow_wstr (obstack, LCST ("'")); | |
2608 | s = xstrprintf (_(" <repeats %u times>"), elem->repeat_count); | |
2609 | for (j = 0; s[j]; ++j) | |
2610 | { | |
2611 | gdb_wchar_t w = gdb_btowc (s[j]); | |
2612 | obstack_grow (obstack, &w, sizeof (gdb_wchar_t)); | |
2613 | } | |
2614 | xfree (s); | |
2615 | } | |
2616 | break; | |
2617 | ||
2618 | case INCOMPLETE: | |
2619 | /* We are outputting an incomplete sequence. */ | |
2620 | if (last == SINGLE) | |
2621 | { | |
2622 | /* If we were outputting a string of SINGLE characters, | |
2623 | terminate the quote. */ | |
0d63ecda KS |
2624 | obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t)); |
2625 | } | |
2626 | if (last != START) | |
2627 | obstack_grow_wstr (obstack, LCST (", ")); | |
2628 | ||
2629 | /* Output the incomplete sequence string. */ | |
2630 | obstack_grow_wstr (obstack, LCST ("<incomplete sequence ")); | |
2631 | print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order, | |
2632 | obstack, 0, &need_escape); | |
2633 | obstack_grow_wstr (obstack, LCST (">")); | |
2634 | ||
2635 | /* We do not attempt to outupt anything after this. */ | |
2636 | state = FINISH; | |
2637 | break; | |
2638 | ||
2639 | case FINISH: | |
2640 | /* All done. If we were outputting a string of SINGLE | |
2641 | characters, the string must be terminated. Otherwise, | |
2642 | REPEAT and INCOMPLETE are always left properly terminated. */ | |
2643 | if (last == SINGLE) | |
e93a8774 | 2644 | obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t)); |
0d63ecda KS |
2645 | |
2646 | return; | |
2647 | } | |
2648 | ||
2649 | /* Get the next element and state. */ | |
2650 | last = state; | |
2651 | if (state != FINISH) | |
2652 | { | |
2653 | elem = VEC_index (converted_character_d, chars, idx++); | |
2654 | switch (elem->result) | |
2655 | { | |
2656 | case wchar_iterate_ok: | |
2657 | case wchar_iterate_invalid: | |
2658 | if (elem->repeat_count > options->repeat_count_threshold) | |
2659 | state = REPEAT; | |
2660 | else | |
2661 | state = SINGLE; | |
2662 | break; | |
2663 | ||
2664 | case wchar_iterate_incomplete: | |
2665 | state = INCOMPLETE; | |
2666 | break; | |
2667 | ||
2668 | case wchar_iterate_eof: | |
2669 | state = FINISH; | |
2670 | break; | |
2671 | } | |
2672 | } | |
2673 | } | |
2674 | } | |
2675 | ||
3b2b8fea TT |
2676 | /* Print the character string STRING, printing at most LENGTH |
2677 | characters. LENGTH is -1 if the string is nul terminated. TYPE is | |
2678 | the type of each character. OPTIONS holds the printing options; | |
2679 | printing stops early if the number hits print_max; repeat counts | |
2680 | are printed as appropriate. Print ellipses at the end if we had to | |
2681 | stop before printing LENGTH characters, or if FORCE_ELLIPSES. | |
2682 | QUOTE_CHAR is the character to print at each end of the string. If | |
2683 | C_STYLE_TERMINATOR is true, and the last character is 0, then it is | |
2684 | omitted. */ | |
2685 | ||
2686 | void | |
2687 | generic_printstr (struct ui_file *stream, struct type *type, | |
2688 | const gdb_byte *string, unsigned int length, | |
2689 | const char *encoding, int force_ellipses, | |
2690 | int quote_char, int c_style_terminator, | |
2691 | const struct value_print_options *options) | |
2692 | { | |
2693 | enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); | |
2694 | unsigned int i; | |
3b2b8fea | 2695 | int width = TYPE_LENGTH (type); |
3b2b8fea | 2696 | struct cleanup *cleanup; |
3b2b8fea | 2697 | int finished = 0; |
0d63ecda KS |
2698 | struct converted_character *last; |
2699 | VEC (converted_character_d) *converted_chars; | |
3b2b8fea TT |
2700 | |
2701 | if (length == -1) | |
2702 | { | |
2703 | unsigned long current_char = 1; | |
2704 | ||
2705 | for (i = 0; current_char; ++i) | |
2706 | { | |
2707 | QUIT; | |
2708 | current_char = extract_unsigned_integer (string + i * width, | |
2709 | width, byte_order); | |
2710 | } | |
2711 | length = i; | |
2712 | } | |
2713 | ||
2714 | /* If the string was not truncated due to `set print elements', and | |
2715 | the last byte of it is a null, we don't print that, in | |
2716 | traditional C style. */ | |
2717 | if (c_style_terminator | |
2718 | && !force_ellipses | |
2719 | && length > 0 | |
2720 | && (extract_unsigned_integer (string + (length - 1) * width, | |
2721 | width, byte_order) == 0)) | |
2722 | length--; | |
2723 | ||
2724 | if (length == 0) | |
2725 | { | |
2726 | fputs_filtered ("\"\"", stream); | |
2727 | return; | |
2728 | } | |
2729 | ||
2730 | /* Arrange to iterate over the characters, in wchar_t form. */ | |
cda6c55b | 2731 | wchar_iterator iter (string, length * width, encoding, width); |
0d63ecda | 2732 | converted_chars = NULL; |
cda6c55b TT |
2733 | cleanup = make_cleanup (VEC_cleanup (converted_character_d), |
2734 | &converted_chars); | |
3b2b8fea | 2735 | |
0d63ecda KS |
2736 | /* Convert characters until the string is over or the maximum |
2737 | number of printed characters has been reached. */ | |
2738 | i = 0; | |
2739 | while (i < options->print_max) | |
3b2b8fea | 2740 | { |
0d63ecda | 2741 | int r; |
3b2b8fea TT |
2742 | |
2743 | QUIT; | |
2744 | ||
0d63ecda | 2745 | /* Grab the next character and repeat count. */ |
cda6c55b | 2746 | r = count_next_character (&iter, &converted_chars); |
3b2b8fea | 2747 | |
0d63ecda KS |
2748 | /* If less than zero, the end of the input string was reached. */ |
2749 | if (r < 0) | |
2750 | break; | |
3b2b8fea | 2751 | |
0d63ecda KS |
2752 | /* Otherwise, add the count to the total print count and get |
2753 | the next character. */ | |
2754 | i += r; | |
2755 | } | |
3b2b8fea | 2756 | |
0d63ecda KS |
2757 | /* Get the last element and determine if the entire string was |
2758 | processed. */ | |
2759 | last = VEC_last (converted_character_d, converted_chars); | |
2760 | finished = (last->result == wchar_iterate_eof); | |
3b2b8fea | 2761 | |
0d63ecda KS |
2762 | /* Ensure that CONVERTED_CHARS is terminated. */ |
2763 | last->result = wchar_iterate_eof; | |
3b2b8fea | 2764 | |
0d63ecda KS |
2765 | /* WCHAR_BUF is the obstack we use to represent the string in |
2766 | wchar_t form. */ | |
8268c778 | 2767 | auto_obstack wchar_buf; |
3b2b8fea | 2768 | |
0d63ecda KS |
2769 | /* Print the output string to the obstack. */ |
2770 | print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char, | |
2771 | width, byte_order, options); | |
3b2b8fea TT |
2772 | |
2773 | if (force_ellipses || !finished) | |
2774 | obstack_grow_wstr (&wchar_buf, LCST ("...")); | |
2775 | ||
2776 | /* OUTPUT is where we collect `char's for printing. */ | |
8268c778 | 2777 | auto_obstack output; |
3b2b8fea TT |
2778 | |
2779 | convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), | |
ac91cd70 | 2780 | (gdb_byte *) obstack_base (&wchar_buf), |
3b2b8fea | 2781 | obstack_object_size (&wchar_buf), |
fff10684 | 2782 | sizeof (gdb_wchar_t), &output, translit_char); |
3b2b8fea TT |
2783 | obstack_1grow (&output, '\0'); |
2784 | ||
79f33898 | 2785 | fputs_filtered ((const char *) obstack_base (&output), stream); |
3b2b8fea TT |
2786 | |
2787 | do_cleanups (cleanup); | |
2788 | } | |
2789 | ||
ae6a3a4c TJB |
2790 | /* Print a string from the inferior, starting at ADDR and printing up to LEN |
2791 | characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing | |
2792 | stops at the first null byte, otherwise printing proceeds (including null | |
2793 | bytes) until either print_max or LEN characters have been printed, | |
09ca9e2e TT |
2794 | whichever is smaller. ENCODING is the name of the string's |
2795 | encoding. It can be NULL, in which case the target encoding is | |
2796 | assumed. */ | |
ae6a3a4c TJB |
2797 | |
2798 | int | |
09ca9e2e TT |
2799 | val_print_string (struct type *elttype, const char *encoding, |
2800 | CORE_ADDR addr, int len, | |
6c7a06a3 | 2801 | struct ui_file *stream, |
ae6a3a4c TJB |
2802 | const struct value_print_options *options) |
2803 | { | |
2804 | int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */ | |
d09f2c3f | 2805 | int err; /* Non-zero if we got a bad read. */ |
581e13c1 | 2806 | int found_nul; /* Non-zero if we found the nul char. */ |
ae6a3a4c TJB |
2807 | unsigned int fetchlimit; /* Maximum number of chars to print. */ |
2808 | int bytes_read; | |
2809 | gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */ | |
2810 | struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */ | |
5af949e3 | 2811 | struct gdbarch *gdbarch = get_type_arch (elttype); |
e17a4113 | 2812 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
6c7a06a3 | 2813 | int width = TYPE_LENGTH (elttype); |
ae6a3a4c TJB |
2814 | |
2815 | /* First we need to figure out the limit on the number of characters we are | |
2816 | going to attempt to fetch and print. This is actually pretty simple. If | |
2817 | LEN >= zero, then the limit is the minimum of LEN and print_max. If | |
2818 | LEN is -1, then the limit is print_max. This is true regardless of | |
2819 | whether print_max is zero, UINT_MAX (unlimited), or something in between, | |
2820 | because finding the null byte (or available memory) is what actually | |
2821 | limits the fetch. */ | |
2822 | ||
325fac50 PA |
2823 | fetchlimit = (len == -1 ? options->print_max : std::min ((unsigned) len, |
2824 | options->print_max)); | |
ae6a3a4c | 2825 | |
d09f2c3f PA |
2826 | err = read_string (addr, len, width, fetchlimit, byte_order, |
2827 | &buffer, &bytes_read); | |
ae6a3a4c TJB |
2828 | old_chain = make_cleanup (xfree, buffer); |
2829 | ||
2830 | addr += bytes_read; | |
c906108c | 2831 | |
3e43a32a MS |
2832 | /* We now have either successfully filled the buffer to fetchlimit, |
2833 | or terminated early due to an error or finding a null char when | |
2834 | LEN is -1. */ | |
ae6a3a4c TJB |
2835 | |
2836 | /* Determine found_nul by looking at the last character read. */ | |
6694c411 JK |
2837 | found_nul = 0; |
2838 | if (bytes_read >= width) | |
2839 | found_nul = extract_unsigned_integer (buffer + bytes_read - width, width, | |
2840 | byte_order) == 0; | |
c906108c SS |
2841 | if (len == -1 && !found_nul) |
2842 | { | |
777ea8f1 | 2843 | gdb_byte *peekbuf; |
c906108c | 2844 | |
ae6a3a4c | 2845 | /* We didn't find a NUL terminator we were looking for. Attempt |
c5aa993b JM |
2846 | to peek at the next character. If not successful, or it is not |
2847 | a null byte, then force ellipsis to be printed. */ | |
c906108c | 2848 | |
777ea8f1 | 2849 | peekbuf = (gdb_byte *) alloca (width); |
c906108c SS |
2850 | |
2851 | if (target_read_memory (addr, peekbuf, width) == 0 | |
e17a4113 | 2852 | && extract_unsigned_integer (peekbuf, width, byte_order) != 0) |
c906108c SS |
2853 | force_ellipsis = 1; |
2854 | } | |
d09f2c3f | 2855 | else if ((len >= 0 && err != 0) || (len > bytes_read / width)) |
c906108c SS |
2856 | { |
2857 | /* Getting an error when we have a requested length, or fetching less | |
c5aa993b | 2858 | than the number of characters actually requested, always make us |
ae6a3a4c | 2859 | print ellipsis. */ |
c906108c SS |
2860 | force_ellipsis = 1; |
2861 | } | |
2862 | ||
c906108c SS |
2863 | /* If we get an error before fetching anything, don't print a string. |
2864 | But if we fetch something and then get an error, print the string | |
2865 | and then the error message. */ | |
d09f2c3f | 2866 | if (err == 0 || bytes_read > 0) |
c906108c | 2867 | { |
be759fcf | 2868 | LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width, |
3a772aa4 | 2869 | encoding, force_ellipsis, options); |
c906108c SS |
2870 | } |
2871 | ||
d09f2c3f | 2872 | if (err != 0) |
c906108c | 2873 | { |
1ccbe998 | 2874 | std::string str = memory_error_message (TARGET_XFER_E_IO, gdbarch, addr); |
578d3588 PA |
2875 | |
2876 | fprintf_filtered (stream, "<error: "); | |
1ccbe998 | 2877 | fputs_filtered (str.c_str (), stream); |
578d3588 | 2878 | fprintf_filtered (stream, ">"); |
c906108c | 2879 | } |
ae6a3a4c | 2880 | |
c906108c SS |
2881 | gdb_flush (stream); |
2882 | do_cleanups (old_chain); | |
ae6a3a4c TJB |
2883 | |
2884 | return (bytes_read / width); | |
c906108c | 2885 | } |
c906108c | 2886 | \f |
c5aa993b | 2887 | |
09e6485f PA |
2888 | /* The 'set input-radix' command writes to this auxiliary variable. |
2889 | If the requested radix is valid, INPUT_RADIX is updated; otherwise, | |
2890 | it is left unchanged. */ | |
2891 | ||
2892 | static unsigned input_radix_1 = 10; | |
2893 | ||
c906108c SS |
2894 | /* Validate an input or output radix setting, and make sure the user |
2895 | knows what they really did here. Radix setting is confusing, e.g. | |
2896 | setting the input radix to "10" never changes it! */ | |
2897 | ||
c906108c | 2898 | static void |
fba45db2 | 2899 | set_input_radix (char *args, int from_tty, struct cmd_list_element *c) |
c906108c | 2900 | { |
09e6485f | 2901 | set_input_radix_1 (from_tty, input_radix_1); |
c906108c SS |
2902 | } |
2903 | ||
c906108c | 2904 | static void |
fba45db2 | 2905 | set_input_radix_1 (int from_tty, unsigned radix) |
c906108c SS |
2906 | { |
2907 | /* We don't currently disallow any input radix except 0 or 1, which don't | |
2908 | make any mathematical sense. In theory, we can deal with any input | |
2909 | radix greater than 1, even if we don't have unique digits for every | |
2910 | value from 0 to radix-1, but in practice we lose on large radix values. | |
2911 | We should either fix the lossage or restrict the radix range more. | |
581e13c1 | 2912 | (FIXME). */ |
c906108c SS |
2913 | |
2914 | if (radix < 2) | |
2915 | { | |
09e6485f | 2916 | input_radix_1 = input_radix; |
8a3fe4f8 | 2917 | error (_("Nonsense input radix ``decimal %u''; input radix unchanged."), |
c906108c SS |
2918 | radix); |
2919 | } | |
09e6485f | 2920 | input_radix_1 = input_radix = radix; |
c906108c SS |
2921 | if (from_tty) |
2922 | { | |
3e43a32a MS |
2923 | printf_filtered (_("Input radix now set to " |
2924 | "decimal %u, hex %x, octal %o.\n"), | |
c906108c SS |
2925 | radix, radix, radix); |
2926 | } | |
2927 | } | |
2928 | ||
09e6485f PA |
2929 | /* The 'set output-radix' command writes to this auxiliary variable. |
2930 | If the requested radix is valid, OUTPUT_RADIX is updated, | |
2931 | otherwise, it is left unchanged. */ | |
2932 | ||
2933 | static unsigned output_radix_1 = 10; | |
2934 | ||
c906108c | 2935 | static void |
fba45db2 | 2936 | set_output_radix (char *args, int from_tty, struct cmd_list_element *c) |
c906108c | 2937 | { |
09e6485f | 2938 | set_output_radix_1 (from_tty, output_radix_1); |
c906108c SS |
2939 | } |
2940 | ||
2941 | static void | |
fba45db2 | 2942 | set_output_radix_1 (int from_tty, unsigned radix) |
c906108c SS |
2943 | { |
2944 | /* Validate the radix and disallow ones that we aren't prepared to | |
581e13c1 | 2945 | handle correctly, leaving the radix unchanged. */ |
c906108c SS |
2946 | switch (radix) |
2947 | { | |
2948 | case 16: | |
79a45b7d | 2949 | user_print_options.output_format = 'x'; /* hex */ |
c906108c SS |
2950 | break; |
2951 | case 10: | |
79a45b7d | 2952 | user_print_options.output_format = 0; /* decimal */ |
c906108c SS |
2953 | break; |
2954 | case 8: | |
79a45b7d | 2955 | user_print_options.output_format = 'o'; /* octal */ |
c906108c SS |
2956 | break; |
2957 | default: | |
09e6485f | 2958 | output_radix_1 = output_radix; |
3e43a32a MS |
2959 | error (_("Unsupported output radix ``decimal %u''; " |
2960 | "output radix unchanged."), | |
c906108c SS |
2961 | radix); |
2962 | } | |
09e6485f | 2963 | output_radix_1 = output_radix = radix; |
c906108c SS |
2964 | if (from_tty) |
2965 | { | |
3e43a32a MS |
2966 | printf_filtered (_("Output radix now set to " |
2967 | "decimal %u, hex %x, octal %o.\n"), | |
c906108c SS |
2968 | radix, radix, radix); |
2969 | } | |
2970 | } | |
2971 | ||
2972 | /* Set both the input and output radix at once. Try to set the output radix | |
2973 | first, since it has the most restrictive range. An radix that is valid as | |
2974 | an output radix is also valid as an input radix. | |
2975 | ||
2976 | It may be useful to have an unusual input radix. If the user wishes to | |
2977 | set an input radix that is not valid as an output radix, he needs to use | |
581e13c1 | 2978 | the 'set input-radix' command. */ |
c906108c SS |
2979 | |
2980 | static void | |
b0a8e6c4 | 2981 | set_radix (const char *arg, int from_tty) |
c906108c SS |
2982 | { |
2983 | unsigned radix; | |
2984 | ||
bb518678 | 2985 | radix = (arg == NULL) ? 10 : parse_and_eval_long (arg); |
c906108c SS |
2986 | set_output_radix_1 (0, radix); |
2987 | set_input_radix_1 (0, radix); | |
2988 | if (from_tty) | |
2989 | { | |
3e43a32a MS |
2990 | printf_filtered (_("Input and output radices now set to " |
2991 | "decimal %u, hex %x, octal %o.\n"), | |
c906108c SS |
2992 | radix, radix, radix); |
2993 | } | |
2994 | } | |
2995 | ||
581e13c1 | 2996 | /* Show both the input and output radices. */ |
c906108c | 2997 | |
c906108c | 2998 | static void |
b0a8e6c4 | 2999 | show_radix (const char *arg, int from_tty) |
c906108c SS |
3000 | { |
3001 | if (from_tty) | |
3002 | { | |
3003 | if (input_radix == output_radix) | |
3004 | { | |
3e43a32a MS |
3005 | printf_filtered (_("Input and output radices set to " |
3006 | "decimal %u, hex %x, octal %o.\n"), | |
c906108c SS |
3007 | input_radix, input_radix, input_radix); |
3008 | } | |
3009 | else | |
3010 | { | |
3e43a32a MS |
3011 | printf_filtered (_("Input radix set to decimal " |
3012 | "%u, hex %x, octal %o.\n"), | |
c906108c | 3013 | input_radix, input_radix, input_radix); |
3e43a32a MS |
3014 | printf_filtered (_("Output radix set to decimal " |
3015 | "%u, hex %x, octal %o.\n"), | |
c906108c SS |
3016 | output_radix, output_radix, output_radix); |
3017 | } | |
3018 | } | |
3019 | } | |
c906108c | 3020 | \f |
c5aa993b | 3021 | |
c906108c | 3022 | static void |
981a3fb3 | 3023 | set_print (const char *arg, int from_tty) |
c906108c SS |
3024 | { |
3025 | printf_unfiltered ( | |
c5aa993b | 3026 | "\"set print\" must be followed by the name of a print subcommand.\n"); |
635c7e8a | 3027 | help_list (setprintlist, "set print ", all_commands, gdb_stdout); |
c906108c SS |
3028 | } |
3029 | ||
c906108c | 3030 | static void |
981a3fb3 | 3031 | show_print (const char *args, int from_tty) |
c906108c SS |
3032 | { |
3033 | cmd_show_list (showprintlist, from_tty, ""); | |
3034 | } | |
e7045703 DE |
3035 | |
3036 | static void | |
981a3fb3 | 3037 | set_print_raw (const char *arg, int from_tty) |
e7045703 DE |
3038 | { |
3039 | printf_unfiltered ( | |
3040 | "\"set print raw\" must be followed by the name of a \"print raw\" subcommand.\n"); | |
635c7e8a | 3041 | help_list (setprintrawlist, "set print raw ", all_commands, gdb_stdout); |
e7045703 DE |
3042 | } |
3043 | ||
3044 | static void | |
981a3fb3 | 3045 | show_print_raw (const char *args, int from_tty) |
e7045703 DE |
3046 | { |
3047 | cmd_show_list (showprintrawlist, from_tty, ""); | |
3048 | } | |
3049 | ||
c906108c SS |
3050 | \f |
3051 | void | |
fba45db2 | 3052 | _initialize_valprint (void) |
c906108c | 3053 | { |
c906108c | 3054 | add_prefix_cmd ("print", no_class, set_print, |
1bedd215 | 3055 | _("Generic command for setting how things print."), |
c906108c | 3056 | &setprintlist, "set print ", 0, &setlist); |
c5aa993b | 3057 | add_alias_cmd ("p", "print", no_class, 1, &setlist); |
581e13c1 | 3058 | /* Prefer set print to set prompt. */ |
c906108c SS |
3059 | add_alias_cmd ("pr", "print", no_class, 1, &setlist); |
3060 | ||
3061 | add_prefix_cmd ("print", no_class, show_print, | |
1bedd215 | 3062 | _("Generic command for showing print settings."), |
c906108c | 3063 | &showprintlist, "show print ", 0, &showlist); |
c5aa993b JM |
3064 | add_alias_cmd ("p", "print", no_class, 1, &showlist); |
3065 | add_alias_cmd ("pr", "print", no_class, 1, &showlist); | |
c906108c | 3066 | |
e7045703 DE |
3067 | add_prefix_cmd ("raw", no_class, set_print_raw, |
3068 | _("\ | |
3069 | Generic command for setting what things to print in \"raw\" mode."), | |
3070 | &setprintrawlist, "set print raw ", 0, &setprintlist); | |
3071 | add_prefix_cmd ("raw", no_class, show_print_raw, | |
3072 | _("Generic command for showing \"print raw\" settings."), | |
3073 | &showprintrawlist, "show print raw ", 0, &showprintlist); | |
3074 | ||
79a45b7d TT |
3075 | add_setshow_uinteger_cmd ("elements", no_class, |
3076 | &user_print_options.print_max, _("\ | |
35096d9d AC |
3077 | Set limit on string chars or array elements to print."), _("\ |
3078 | Show limit on string chars or array elements to print."), _("\ | |
f81d1120 | 3079 | \"set print elements unlimited\" causes there to be no limit."), |
35096d9d | 3080 | NULL, |
920d2a44 | 3081 | show_print_max, |
35096d9d | 3082 | &setprintlist, &showprintlist); |
c906108c | 3083 | |
79a45b7d TT |
3084 | add_setshow_boolean_cmd ("null-stop", no_class, |
3085 | &user_print_options.stop_print_at_null, _("\ | |
5bf193a2 AC |
3086 | Set printing of char arrays to stop at first null char."), _("\ |
3087 | Show printing of char arrays to stop at first null char."), NULL, | |
3088 | NULL, | |
920d2a44 | 3089 | show_stop_print_at_null, |
5bf193a2 | 3090 | &setprintlist, &showprintlist); |
c906108c | 3091 | |
35096d9d | 3092 | add_setshow_uinteger_cmd ("repeats", no_class, |
79a45b7d | 3093 | &user_print_options.repeat_count_threshold, _("\ |
35096d9d AC |
3094 | Set threshold for repeated print elements."), _("\ |
3095 | Show threshold for repeated print elements."), _("\ | |
f81d1120 | 3096 | \"set print repeats unlimited\" causes all elements to be individually printed."), |
35096d9d | 3097 | NULL, |
920d2a44 | 3098 | show_repeat_count_threshold, |
35096d9d | 3099 | &setprintlist, &showprintlist); |
c906108c | 3100 | |
79a45b7d | 3101 | add_setshow_boolean_cmd ("pretty", class_support, |
2a998fc0 DE |
3102 | &user_print_options.prettyformat_structs, _("\ |
3103 | Set pretty formatting of structures."), _("\ | |
3104 | Show pretty formatting of structures."), NULL, | |
5bf193a2 | 3105 | NULL, |
2a998fc0 | 3106 | show_prettyformat_structs, |
5bf193a2 AC |
3107 | &setprintlist, &showprintlist); |
3108 | ||
79a45b7d TT |
3109 | add_setshow_boolean_cmd ("union", class_support, |
3110 | &user_print_options.unionprint, _("\ | |
5bf193a2 AC |
3111 | Set printing of unions interior to structures."), _("\ |
3112 | Show printing of unions interior to structures."), NULL, | |
3113 | NULL, | |
920d2a44 | 3114 | show_unionprint, |
5bf193a2 AC |
3115 | &setprintlist, &showprintlist); |
3116 | ||
79a45b7d | 3117 | add_setshow_boolean_cmd ("array", class_support, |
2a998fc0 DE |
3118 | &user_print_options.prettyformat_arrays, _("\ |
3119 | Set pretty formatting of arrays."), _("\ | |
3120 | Show pretty formatting of arrays."), NULL, | |
5bf193a2 | 3121 | NULL, |
2a998fc0 | 3122 | show_prettyformat_arrays, |
5bf193a2 AC |
3123 | &setprintlist, &showprintlist); |
3124 | ||
79a45b7d TT |
3125 | add_setshow_boolean_cmd ("address", class_support, |
3126 | &user_print_options.addressprint, _("\ | |
5bf193a2 AC |
3127 | Set printing of addresses."), _("\ |
3128 | Show printing of addresses."), NULL, | |
3129 | NULL, | |
920d2a44 | 3130 | show_addressprint, |
5bf193a2 | 3131 | &setprintlist, &showprintlist); |
c906108c | 3132 | |
9cb709b6 TT |
3133 | add_setshow_boolean_cmd ("symbol", class_support, |
3134 | &user_print_options.symbol_print, _("\ | |
3135 | Set printing of symbol names when printing pointers."), _("\ | |
3136 | Show printing of symbol names when printing pointers."), | |
3137 | NULL, NULL, | |
3138 | show_symbol_print, | |
3139 | &setprintlist, &showprintlist); | |
3140 | ||
1e8fb976 PA |
3141 | add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1, |
3142 | _("\ | |
35096d9d AC |
3143 | Set default input radix for entering numbers."), _("\ |
3144 | Show default input radix for entering numbers."), NULL, | |
1e8fb976 PA |
3145 | set_input_radix, |
3146 | show_input_radix, | |
3147 | &setlist, &showlist); | |
35096d9d | 3148 | |
1e8fb976 PA |
3149 | add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1, |
3150 | _("\ | |
35096d9d AC |
3151 | Set default output radix for printing of values."), _("\ |
3152 | Show default output radix for printing of values."), NULL, | |
1e8fb976 PA |
3153 | set_output_radix, |
3154 | show_output_radix, | |
3155 | &setlist, &showlist); | |
c906108c | 3156 | |
cb1a6d5f AC |
3157 | /* The "set radix" and "show radix" commands are special in that |
3158 | they are like normal set and show commands but allow two normally | |
3159 | independent variables to be either set or shown with a single | |
b66df561 | 3160 | command. So the usual deprecated_add_set_cmd() and [deleted] |
581e13c1 | 3161 | add_show_from_set() commands aren't really appropriate. */ |
b66df561 AC |
3162 | /* FIXME: i18n: With the new add_setshow_integer command, that is no |
3163 | longer true - show can display anything. */ | |
1a966eab AC |
3164 | add_cmd ("radix", class_support, set_radix, _("\ |
3165 | Set default input and output number radices.\n\ | |
c906108c | 3166 | Use 'set input-radix' or 'set output-radix' to independently set each.\n\ |
1a966eab | 3167 | Without an argument, sets both radices back to the default value of 10."), |
c906108c | 3168 | &setlist); |
1a966eab AC |
3169 | add_cmd ("radix", class_support, show_radix, _("\ |
3170 | Show the default input and output number radices.\n\ | |
3171 | Use 'show input-radix' or 'show output-radix' to independently show each."), | |
c906108c SS |
3172 | &showlist); |
3173 | ||
e79af960 | 3174 | add_setshow_boolean_cmd ("array-indexes", class_support, |
79a45b7d | 3175 | &user_print_options.print_array_indexes, _("\ |
e79af960 JB |
3176 | Set printing of array indexes."), _("\ |
3177 | Show printing of array indexes"), NULL, NULL, show_print_array_indexes, | |
3178 | &setprintlist, &showprintlist); | |
c906108c | 3179 | } |