* hppah-tdep.c (skip_trampoline_code): Use new macros for
[deliverable/binutils-gdb.git] / gdb / valprint.c
1 /* Print values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include <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 "obstack.h"
29 #include "language.h"
30 #include "demangle.h"
31
32 #include <errno.h>
33
34 /* Prototypes for local functions */
35
36 static void
37 print_hex_chars PARAMS ((FILE *, unsigned char *, unsigned int));
38
39 static void
40 show_print PARAMS ((char *, int));
41
42 static void
43 set_print PARAMS ((char *, int));
44
45 static void
46 set_radix PARAMS ((char *, int, struct cmd_list_element *));
47
48 static void
49 set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
50
51 static void
52 value_print_array_elements PARAMS ((value, FILE *, int, enum val_prettyprint));
53
54 /* Maximum number of chars to print for a string pointer value
55 or vector contents, or UINT_MAX for no limit. */
56
57 unsigned int print_max;
58
59 /* Default input and output radixes, and output format letter. */
60
61 unsigned input_radix = 10;
62 unsigned output_radix = 10;
63 int output_format = 0;
64
65 /* Print repeat counts if there are more than this many repetitions of an
66 element in an array. Referenced by the low level language dependent
67 print routines. */
68
69 unsigned int repeat_count_threshold = 10;
70
71 int prettyprint_structs; /* Controls pretty printing of structures */
72 int prettyprint_arrays; /* Controls pretty printing of arrays. */
73
74 /* If nonzero, causes unions inside structures or other unions to be
75 printed. */
76
77 int unionprint; /* Controls printing of nested unions. */
78
79 /* If nonzero, causes machine addresses to be printed in certain contexts. */
80
81 int addressprint; /* Controls printing of machine addresses */
82
83 \f
84 /* Print data of type TYPE located at VALADDR (within GDB),
85 which came from the inferior at address ADDRESS,
86 onto stdio stream STREAM according to FORMAT
87 (a letter or 0 for natural format). The data at VALADDR
88 is in target byte order.
89
90 If the data are a string pointer, returns the number of
91 sting characters printed.
92
93 if DEREF_REF is nonzero, then dereference references,
94 otherwise just print them like pointers.
95
96 The PRETTY parameter controls prettyprinting. */
97
98 int
99 val_print (type, valaddr, address, stream, format, deref_ref, recurse,
100 pretty)
101 struct type *type;
102 char *valaddr;
103 CORE_ADDR address;
104 FILE *stream;
105 int format;
106 int deref_ref;
107 int recurse;
108 enum val_prettyprint pretty;
109 {
110 if (pretty == Val_pretty_default)
111 {
112 pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
113 }
114
115 QUIT;
116
117 /* Ensure that the type is complete and not just a stub. If the type is
118 only a stub and we can't find and substitute its complete type, then
119 print appropriate string and return. Typical types that my be stubs
120 are structs, unions, and C++ methods. */
121
122 check_stub_type (type);
123 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
124 {
125 fprintf_filtered (stream, "<incomplete type>");
126 fflush (stream);
127 return (0);
128 }
129
130 return (LA_VAL_PRINT (type, valaddr, address, stream, format, deref_ref,
131 recurse, pretty));
132 }
133
134 /* Print the value VAL in C-ish syntax on stream STREAM.
135 FORMAT is a format-letter, or 0 for print in natural format of data type.
136 If the object printed is a string pointer, returns
137 the number of string bytes printed. */
138
139 int
140 value_print (val, stream, format, pretty)
141 value val;
142 FILE *stream;
143 int format;
144 enum val_prettyprint pretty;
145 {
146 register unsigned int n, typelen;
147
148 if (val == 0)
149 {
150 printf_filtered ("<address of value unknown>");
151 return 0;
152 }
153 if (VALUE_OPTIMIZED_OUT (val))
154 {
155 printf_filtered ("<value optimized out>");
156 return 0;
157 }
158
159 /* A "repeated" value really contains several values in a row.
160 They are made by the @ operator.
161 Print such values as if they were arrays. */
162
163 if (VALUE_REPEATED (val))
164 {
165 n = VALUE_REPETITIONS (val);
166 typelen = TYPE_LENGTH (VALUE_TYPE (val));
167 fprintf_filtered (stream, "{");
168 /* Print arrays of characters using string syntax. */
169 if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
170 && format == 0)
171 LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0);
172 else
173 {
174 value_print_array_elements (val, stream, format, pretty);
175 }
176 fprintf_filtered (stream, "}");
177 return (n * typelen);
178 }
179 else
180 {
181 struct type *type = VALUE_TYPE (val);
182
183 /* If it is a pointer, indicate what it points to.
184
185 Print type also if it is a reference.
186
187 C++: if it is a member pointer, we will take care
188 of that when we print it. */
189 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
190 TYPE_CODE (type) == TYPE_CODE_REF)
191 {
192 /* Hack: remove (char *) for char strings. Their
193 type is indicated by the quoted string anyway. */
194 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
195 TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof(char) &&
196 TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT &&
197 !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
198 {
199 /* Print nothing */
200 }
201 else
202 {
203 fprintf_filtered (stream, "(");
204 type_print (type, "", stream, -1);
205 fprintf_filtered (stream, ") ");
206 }
207 }
208 return (val_print (type, VALUE_CONTENTS (val),
209 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
210 }
211 }
212
213 /* Called by various <lang>_val_print routines to print TYPE_CODE_INT's */
214
215 void
216 val_print_type_code_int (type, valaddr, stream)
217 struct type *type;
218 char *valaddr;
219 FILE *stream;
220 {
221 char *p;
222 /* Pointer to first (i.e. lowest address) nonzero character. */
223 char *first_addr;
224 unsigned int len;
225
226 if (TYPE_LENGTH (type) > sizeof (LONGEST))
227 {
228 if (TYPE_UNSIGNED (type))
229 {
230 /* First figure out whether the number in fact has zeros
231 in all its bytes more significant than least significant
232 sizeof (LONGEST) ones. */
233 len = TYPE_LENGTH (type);
234
235 #if TARGET_BYTE_ORDER == BIG_ENDIAN
236 for (p = valaddr;
237 len > sizeof (LONGEST) && p < valaddr + TYPE_LENGTH (type);
238 p++)
239 #else /* Little endian. */
240 first_addr = valaddr;
241 for (p = valaddr + TYPE_LENGTH (type);
242 len > sizeof (LONGEST) && p >= valaddr;
243 p--)
244 #endif /* Little endian. */
245 {
246 if (*p == 0)
247 {
248 len--;
249 }
250 else
251 {
252 break;
253 }
254 }
255 #if TARGET_BYTE_ORDER == BIG_ENDIAN
256 first_addr = p;
257 #endif
258 if (len <= sizeof (LONGEST))
259 {
260 /* We can print it in decimal. */
261 fprintf_filtered
262 (stream,
263 #if defined (LONG_LONG)
264 "%llu",
265 #else
266 "%lu",
267 #endif
268 unpack_long (BUILTIN_TYPE_LONGEST, first_addr));
269 }
270 else
271 {
272 /* It is big, so print it in hex. */
273 print_hex_chars (stream, (unsigned char *) first_addr, len);
274 }
275 }
276 else
277 {
278 /* Signed. One could assume two's complement (a reasonable
279 assumption, I think) and do better than this. */
280 print_hex_chars (stream, (unsigned char *) valaddr,
281 TYPE_LENGTH (type));
282 }
283 }
284 else
285 {
286 #ifdef PRINT_TYPELESS_INTEGER
287 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
288 #else
289 fprintf_filtered (stream, TYPE_UNSIGNED (type) ?
290 #if defined (LONG_LONG)
291 "%llu" : "%lld",
292 #else
293 "%u" : "%d",
294 #endif
295 unpack_long (type, valaddr));
296 #endif
297 }
298 }
299
300 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
301 on STREAM. */
302
303 void
304 print_floating (valaddr, type, stream)
305 char *valaddr;
306 struct type *type;
307 FILE *stream;
308 {
309 double doub;
310 int inv;
311 unsigned len = TYPE_LENGTH (type);
312
313 #if defined (IEEE_FLOAT)
314
315 /* Check for NaN's. Note that this code does not depend on us being
316 on an IEEE conforming system. It only depends on the target
317 machine using IEEE representation. This means (a)
318 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
319 be defined for systems like the 68881, which uses IEEE
320 representation, but is not IEEE conforming. */
321
322 {
323 long low, high;
324 /* Is the sign bit 0? */
325 int nonnegative;
326 /* Is it is a NaN (i.e. the exponent is all ones and
327 the fraction is nonzero)? */
328 int is_nan;
329
330 if (len == sizeof (float))
331 {
332 /* It's single precision. */
333 memcpy ((char *) &low, valaddr, sizeof (low));
334 /* target -> host. */
335 SWAP_TARGET_AND_HOST (&low, sizeof (float));
336 nonnegative = low >= 0;
337 is_nan = ((((low >> 23) & 0xFF) == 0xFF)
338 && 0 != (low & 0x7FFFFF));
339 low &= 0x7fffff;
340 high = 0;
341 }
342 else
343 {
344 /* It's double precision. Get the high and low words. */
345
346 #if TARGET_BYTE_ORDER == BIG_ENDIAN
347 memcpy (&low, valaddr+4, sizeof (low));
348 memcpy (&high, valaddr+0, sizeof (high));
349 #else
350 memcpy (&low, valaddr+0, sizeof (low));
351 memcpy (&high, valaddr+4, sizeof (high));
352 #endif
353 SWAP_TARGET_AND_HOST (&low, sizeof (low));
354 SWAP_TARGET_AND_HOST (&high, sizeof (high));
355 nonnegative = high >= 0;
356 is_nan = (((high >> 20) & 0x7ff) == 0x7ff
357 && ! ((((high & 0xfffff) == 0)) && (low == 0)));
358 high &= 0xfffff;
359 }
360
361 if (is_nan)
362 {
363 /* The meaning of the sign and fraction is not defined by IEEE.
364 But the user might know what they mean. For example, they
365 (in an implementation-defined manner) distinguish between
366 signaling and quiet NaN's. */
367 if (high)
368 fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
369 high, low);
370 else
371 fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
372 return;
373 }
374 }
375 #endif /* IEEE_FLOAT. */
376
377 doub = unpack_double (type, valaddr, &inv);
378 if (inv)
379 fprintf_filtered (stream, "<invalid float value>");
380 else
381 fprintf_filtered (stream, len <= sizeof(float) ? "%.9g" : "%.17g", doub);
382 }
383
384 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
385
386 static void
387 print_hex_chars (stream, valaddr, len)
388 FILE *stream;
389 unsigned char *valaddr;
390 unsigned len;
391 {
392 unsigned char *p;
393
394 fprintf_filtered (stream, "0x");
395 #if TARGET_BYTE_ORDER == BIG_ENDIAN
396 for (p = valaddr;
397 p < valaddr + len;
398 p++)
399 #else /* Little endian. */
400 for (p = valaddr + len - 1;
401 p >= valaddr;
402 p--)
403 #endif
404 {
405 fprintf_filtered (stream, "%02x", *p);
406 }
407 }
408
409 /* Called by various <lang>_val_print routines to print elements of an
410 array in the form "<elem1>, <elem2>, <elem3>, ...".
411
412 (FIXME?) Assumes array element separator is a comma, which is correct
413 for all languages currently handled.
414 (FIXME?) Some languages have a notation for repeated array elements,
415 perhaps we should try to use that notation when appropriate.
416 */
417
418 void
419 val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
420 recurse, pretty, i)
421 struct type *type;
422 char *valaddr;
423 CORE_ADDR address;
424 FILE *stream;
425 int format;
426 int deref_ref;
427 int recurse;
428 enum val_prettyprint pretty;
429 unsigned int i;
430 {
431 unsigned int things_printed = 0;
432 unsigned len;
433 struct type *elttype;
434 unsigned eltlen;
435 /* Position of the array element we are examining to see
436 whether it is repeated. */
437 unsigned int rep1;
438 /* Number of repetitions we have detected so far. */
439 unsigned int reps;
440
441 elttype = TYPE_TARGET_TYPE (type);
442 eltlen = TYPE_LENGTH (elttype);
443 len = TYPE_LENGTH (type) / eltlen;
444
445 for (; i < len && things_printed < print_max; i++)
446 {
447 if (i != 0)
448 {
449 if (prettyprint_arrays)
450 {
451 fprintf_filtered (stream, ",\n");
452 print_spaces_filtered (2 + 2 * recurse, stream);
453 }
454 else
455 {
456 fprintf_filtered (stream, ", ");
457 }
458 }
459 wrap_here (n_spaces (2 + 2 * recurse));
460
461 rep1 = i + 1;
462 reps = 1;
463 while ((rep1 < len) &&
464 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
465 {
466 ++reps;
467 ++rep1;
468 }
469
470 if (reps > repeat_count_threshold)
471 {
472 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
473 deref_ref, recurse + 1, pretty);
474 fprintf_filtered (stream, " <repeats %u times>", reps);
475 i = rep1 - 1;
476 things_printed += repeat_count_threshold;
477 }
478 else
479 {
480 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
481 deref_ref, recurse + 1, pretty);
482 things_printed++;
483 }
484 }
485 if (i < len)
486 {
487 fprintf_filtered (stream, "...");
488 }
489 }
490
491 static void
492 value_print_array_elements (val, stream, format, pretty)
493 value val;
494 FILE *stream;
495 int format;
496 enum val_prettyprint pretty;
497 {
498 unsigned int things_printed = 0;
499 register unsigned int i, n, typelen;
500 /* Position of the array elem we are examining to see if it is repeated. */
501 unsigned int rep1;
502 /* Number of repetitions we have detected so far. */
503 unsigned int reps;
504
505 n = VALUE_REPETITIONS (val);
506 typelen = TYPE_LENGTH (VALUE_TYPE (val));
507 for (i = 0; i < n && things_printed < print_max; i++)
508 {
509 if (i != 0)
510 {
511 fprintf_filtered (stream, ", ");
512 }
513 wrap_here ("");
514
515 rep1 = i + 1;
516 reps = 1;
517 while (rep1 < n && !memcmp (VALUE_CONTENTS (val) + typelen * i,
518 VALUE_CONTENTS (val) + typelen * rep1,
519 typelen))
520 {
521 ++reps;
522 ++rep1;
523 }
524
525 if (reps > repeat_count_threshold)
526 {
527 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
528 VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
529 0, pretty);
530 fprintf (stream, " <repeats %u times>", reps);
531 i = rep1 - 1;
532 things_printed += repeat_count_threshold;
533 }
534 else
535 {
536 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
537 VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
538 0, pretty);
539 things_printed++;
540 }
541 }
542 if (i < n)
543 {
544 fprintf_filtered (stream, "...");
545 }
546 }
547
548 \f
549 #if 0
550 /* Validate an input or output radix setting, and make sure the user
551 knows what they really did here. Radix setting is confusing, e.g.
552 setting the input radix to "10" never changes it! */
553
554 /* ARGSUSED */
555 static void
556 set_input_radix (args, from_tty, c)
557 char *args;
558 int from_tty;
559 struct cmd_list_element *c;
560 {
561 unsigned radix = *(unsigned *)c->var;
562
563 if (from_tty)
564 printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n",
565 radix, radix, radix);
566 }
567 #endif
568
569 /* ARGSUSED */
570 static void
571 set_output_radix (args, from_tty, c)
572 char *args;
573 int from_tty;
574 struct cmd_list_element *c;
575 {
576 unsigned radix = *(unsigned *)c->var;
577
578 if (from_tty)
579 printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n",
580 radix, radix, radix);
581
582 /* FIXME, we really should be able to validate the setting BEFORE
583 it takes effect. */
584 switch (radix)
585 {
586 case 16:
587 output_format = 'x';
588 break;
589 case 10:
590 output_format = 0;
591 break;
592 case 8:
593 output_format = 'o'; /* octal */
594 break;
595 default:
596 output_format = 0;
597 error ("Unsupported radix ``decimal %d''; using decimal output",
598 radix);
599 }
600 }
601
602 /* Both at once */
603 static void
604 set_radix (arg, from_tty, c)
605 char *arg;
606 int from_tty;
607 struct cmd_list_element *c;
608 {
609 unsigned radix = *(unsigned *)c->var;
610
611 if (from_tty)
612 printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n",
613 radix, radix, radix);
614
615 input_radix = radix;
616 output_radix = radix;
617
618 set_output_radix (arg, 0, c);
619 }
620 \f
621 /*ARGSUSED*/
622 static void
623 set_print (arg, from_tty)
624 char *arg;
625 int from_tty;
626 {
627 printf (
628 "\"set print\" must be followed by the name of a print subcommand.\n");
629 help_list (setprintlist, "set print ", -1, stdout);
630 }
631
632 /*ARGSUSED*/
633 static void
634 show_print (args, from_tty)
635 char *args;
636 int from_tty;
637 {
638 cmd_show_list (showprintlist, from_tty, "");
639 }
640 \f
641 void
642 _initialize_valprint ()
643 {
644 struct cmd_list_element *c;
645
646 add_prefix_cmd ("print", no_class, set_print,
647 "Generic command for setting how things print.",
648 &setprintlist, "set print ", 0, &setlist);
649 add_alias_cmd ("p", "print", no_class, 1, &setlist);
650 add_alias_cmd ("pr", "print", no_class, 1, &setlist); /* prefer set print
651 to set prompt */
652 add_prefix_cmd ("print", no_class, show_print,
653 "Generic command for showing print settings.",
654 &showprintlist, "show print ", 0, &showlist);
655 add_alias_cmd ("p", "print", no_class, 1, &showlist);
656 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
657
658 add_show_from_set
659 (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
660 "Set limit on string chars or array elements to print.\n\
661 \"set print elements 0\" causes there to be no limit.",
662 &setprintlist),
663 &showprintlist);
664
665 add_show_from_set
666 (add_set_cmd ("repeats", no_class, var_uinteger,
667 (char *)&repeat_count_threshold,
668 "Set threshold for repeated print elements.\n\
669 \"set print repeats 0\" causes all elements to be individually printed.",
670 &setprintlist),
671 &showprintlist);
672
673 add_show_from_set
674 (add_set_cmd ("pretty", class_support, var_boolean,
675 (char *)&prettyprint_structs,
676 "Set prettyprinting of structures.",
677 &setprintlist),
678 &showprintlist);
679
680 add_show_from_set
681 (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
682 "Set printing of unions interior to structures.",
683 &setprintlist),
684 &showprintlist);
685
686 add_show_from_set
687 (add_set_cmd ("array", class_support, var_boolean,
688 (char *)&prettyprint_arrays,
689 "Set prettyprinting of arrays.",
690 &setprintlist),
691 &showprintlist);
692
693 add_show_from_set
694 (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
695 "Set printing of addresses.",
696 &setprintlist),
697 &showprintlist);
698
699 #if 0
700 /* The "show radix" cmd isn't good enough to show two separate values.
701 The rest of the code works, but the show part is confusing, so don't
702 let them be set separately 'til we work out "show". */
703 c = add_set_cmd ("input-radix", class_support, var_uinteger,
704 (char *)&input_radix,
705 "Set default input radix for entering numbers.",
706 &setlist);
707 add_show_from_set (c, &showlist);
708 c->function = set_input_radix;
709
710 c = add_set_cmd ("output-radix", class_support, var_uinteger,
711 (char *)&output_radix,
712 "Set default output radix for printing of values.",
713 &setlist);
714 add_show_from_set (c, &showlist);
715 c->function = set_output_radix;
716 #endif
717
718 c = add_set_cmd ("radix", class_support, var_uinteger,
719 (char *)&output_radix,
720 "Set default input and output number radix.",
721 &setlist);
722 add_show_from_set (c, &showlist);
723 c->function.sfunc = set_radix;
724
725 /* Give people the defaults which they are used to. */
726 prettyprint_structs = 0;
727 prettyprint_arrays = 0;
728 unionprint = 1;
729 addressprint = 1;
730 print_max = 200;
731 }
This page took 0.044253 seconds and 4 git commands to generate.