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