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