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