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