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