]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/printcmd.c
Fix typo in changelog entry
[thirdparty/binutils-gdb.git] / gdb / printcmd.c
CommitLineData
c906108c 1/* Print values for GNU debugger GDB.
e2ad119d 2
618f726f 3 Copyright (C) 1986-2016 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"
c906108c
SS
21#include "frame.h"
22#include "symtab.h"
23#include "gdbtypes.h"
24#include "value.h"
25#include "language.h"
26#include "expression.h"
27#include "gdbcore.h"
28#include "gdbcmd.h"
29#include "target.h"
30#include "breakpoint.h"
31#include "demangle.h"
50f182aa 32#include "gdb-demangle.h"
c906108c
SS
33#include "valprint.h"
34#include "annotate.h"
c5aa993b
JM
35#include "symfile.h" /* for overlay functions */
36#include "objfiles.h" /* ditto */
c94fdfd0 37#include "completer.h" /* for completion functions */
8b93c638 38#include "ui-out.h"
fe898f56 39#include "block.h"
92bf2b80 40#include "disasm.h"
1a619819 41#include "dfp.h"
a3247a22
PP
42#include "observer.h"
43#include "solist.h"
a3247a22 44#include "parser-defs.h"
6c7a06a3 45#include "charset.h"
704e9165 46#include "arch-utils.h"
e9cafbcc 47#include "cli/cli-utils.h"
d3ce09f5 48#include "format.h"
05cba821 49#include "source.h"
c906108c 50
6a83354a 51#ifdef TUI
0df8b418 52#include "tui/tui.h" /* For tui_active et al. */
6a83354a
AC
53#endif
54
c906108c
SS
55/* Last specified output format. */
56
a6bac58e 57static char last_format = 0;
c906108c
SS
58
59/* Last specified examination size. 'b', 'h', 'w' or `q'. */
60
61static char last_size = 'w';
62
5d3729b5 63/* Default address to examine next, and associated architecture. */
c906108c 64
5d3729b5 65static struct gdbarch *next_gdbarch;
c906108c
SS
66static CORE_ADDR next_address;
67
a4642986
MR
68/* Number of delay instructions following current disassembled insn. */
69
70static int branch_delay_insns;
71
c906108c
SS
72/* Last address examined. */
73
74static CORE_ADDR last_examine_address;
75
76/* Contents of last address examined.
77 This is not valid past the end of the `x' command! */
78
3d6d86c6 79static struct value *last_examine_value;
c906108c
SS
80
81/* Largest offset between a symbolic value and an address, that will be
82 printed as `0x1234 <symbol+offset>'. */
83
84static unsigned int max_symbolic_offset = UINT_MAX;
920d2a44
AC
85static void
86show_max_symbolic_offset (struct ui_file *file, int from_tty,
87 struct cmd_list_element *c, const char *value)
88{
3e43a32a
MS
89 fprintf_filtered (file,
90 _("The largest offset that will be "
91 "printed in <symbol+1234> form is %s.\n"),
920d2a44
AC
92 value);
93}
c906108c
SS
94
95/* Append the source filename and linenumber of the symbol when
96 printing a symbolic value as `<symbol at filename:linenum>' if set. */
97static int print_symbol_filename = 0;
920d2a44
AC
98static void
99show_print_symbol_filename (struct ui_file *file, int from_tty,
100 struct cmd_list_element *c, const char *value)
101{
3e43a32a
MS
102 fprintf_filtered (file, _("Printing of source filename and "
103 "line number with <symbol> is %s.\n"),
920d2a44
AC
104 value);
105}
c906108c
SS
106
107/* Number of auto-display expression currently being displayed.
9d8fa392 108 So that we can disable it if we get a signal within it.
c906108c
SS
109 -1 when not doing one. */
110
5a18e302 111static int current_display_number;
c906108c 112
c906108c 113struct display
c5aa993b
JM
114 {
115 /* Chain link to next auto-display item. */
116 struct display *next;
6c95b8df 117
fa8a61dc
TT
118 /* The expression as the user typed it. */
119 char *exp_string;
6c95b8df 120
c5aa993b 121 /* Expression to be evaluated and displayed. */
4d01a485 122 expression_up exp;
6c95b8df 123
c5aa993b
JM
124 /* Item number of this auto-display item. */
125 int number;
6c95b8df 126
c5aa993b
JM
127 /* Display format specified. */
128 struct format_data format;
6c95b8df
PA
129
130 /* Program space associated with `block'. */
131 struct program_space *pspace;
132
0df8b418 133 /* Innermost block required by this expression when evaluated. */
270140bd 134 const struct block *block;
6c95b8df 135
0df8b418 136 /* Status of this display (enabled or disabled). */
b5de0fa7 137 int enabled_p;
c5aa993b 138 };
c906108c
SS
139
140/* Chain of expressions whose values should be displayed
141 automatically each time the program stops. */
142
143static struct display *display_chain;
144
145static int display_number;
146
c9174737
PA
147/* Walk the following statement or block through all displays.
148 ALL_DISPLAYS_SAFE does so even if the statement deletes the current
149 display. */
3c3fe74c
PA
150
151#define ALL_DISPLAYS(B) \
152 for (B = display_chain; B; B = B->next)
153
c9174737
PA
154#define ALL_DISPLAYS_SAFE(B,TMP) \
155 for (B = display_chain; \
156 B ? (TMP = B->next, 1): 0; \
157 B = TMP)
158
0df8b418 159/* Prototypes for exported functions. */
c906108c 160
a14ed312 161void _initialize_printcmd (void);
c906108c 162
0df8b418 163/* Prototypes for local functions. */
c906108c 164
a14ed312 165static void do_one_display (struct display *);
c906108c 166\f
c5aa993b 167
c906108c
SS
168/* Decode a format specification. *STRING_PTR should point to it.
169 OFORMAT and OSIZE are used as defaults for the format and size
170 if none are given in the format specification.
171 If OSIZE is zero, then the size field of the returned value
172 should be set only if a size is explicitly specified by the
173 user.
174 The structure returned describes all the data
175 found in the specification. In addition, *STRING_PTR is advanced
176 past the specification and past all whitespace following it. */
177
178static struct format_data
6f937416 179decode_format (const char **string_ptr, int oformat, int osize)
c906108c
SS
180{
181 struct format_data val;
6f937416 182 const char *p = *string_ptr;
c906108c
SS
183
184 val.format = '?';
185 val.size = '?';
186 val.count = 1;
a6bac58e 187 val.raw = 0;
c906108c 188
bb556f1f
TK
189 if (*p == '-')
190 {
191 val.count = -1;
192 p++;
193 }
c906108c 194 if (*p >= '0' && *p <= '9')
bb556f1f 195 val.count *= atoi (p);
c5aa993b
JM
196 while (*p >= '0' && *p <= '9')
197 p++;
c906108c
SS
198
199 /* Now process size or format letters that follow. */
200
201 while (1)
202 {
203 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
204 val.size = *p++;
a6bac58e
TT
205 else if (*p == 'r')
206 {
207 val.raw = 1;
208 p++;
209 }
c906108c
SS
210 else if (*p >= 'a' && *p <= 'z')
211 val.format = *p++;
212 else
213 break;
214 }
215
c5aa993b
JM
216 while (*p == ' ' || *p == '\t')
217 p++;
c906108c
SS
218 *string_ptr = p;
219
220 /* Set defaults for format and size if not specified. */
221 if (val.format == '?')
222 {
223 if (val.size == '?')
224 {
225 /* Neither has been specified. */
226 val.format = oformat;
227 val.size = osize;
228 }
229 else
230 /* If a size is specified, any format makes a reasonable
231 default except 'i'. */
232 val.format = oformat == 'i' ? 'x' : oformat;
233 }
234 else if (val.size == '?')
235 switch (val.format)
236 {
237 case 'a':
5d3729b5
UW
238 /* Pick the appropriate size for an address. This is deferred
239 until do_examine when we know the actual architecture to use.
240 A special size value of 'a' is used to indicate this case. */
241 val.size = osize ? 'a' : osize;
c906108c
SS
242 break;
243 case 'f':
244 /* Floating point has to be word or giantword. */
245 if (osize == 'w' || osize == 'g')
246 val.size = osize;
247 else
248 /* Default it to giantword if the last used size is not
249 appropriate. */
250 val.size = osize ? 'g' : osize;
251 break;
252 case 'c':
253 /* Characters default to one byte. */
254 val.size = osize ? 'b' : osize;
255 break;
9a22f0d0 256 case 's':
3e43a32a
MS
257 /* Display strings with byte size chars unless explicitly
258 specified. */
9a22f0d0
PM
259 val.size = '\0';
260 break;
261
c906108c
SS
262 default:
263 /* The default is the size most recently specified. */
264 val.size = osize;
265 }
266
267 return val;
268}
269\f
79a45b7d 270/* Print value VAL on stream according to OPTIONS.
c906108c 271 Do not end with a newline.
c906108c 272 SIZE is the letter for the size of datum being printed.
ea37ba09
DJ
273 This is used to pad hex numbers so they line up. SIZE is 0
274 for print / output and set for examine. */
c906108c
SS
275
276static void
79a45b7d
TT
277print_formatted (struct value *val, int size,
278 const struct value_print_options *options,
fba45db2 279 struct ui_file *stream)
c906108c 280{
df407dfe 281 struct type *type = check_typedef (value_type (val));
c906108c
SS
282 int len = TYPE_LENGTH (type);
283
284 if (VALUE_LVAL (val) == lval_memory)
42ae5230 285 next_address = value_address (val) + len;
c906108c 286
ea37ba09 287 if (size)
c906108c 288 {
79a45b7d 289 switch (options->format)
ea37ba09
DJ
290 {
291 case 's':
6c7a06a3
TT
292 {
293 struct type *elttype = value_type (val);
ad3bbd48 294
42ae5230 295 next_address = (value_address (val)
09ca9e2e 296 + val_print_string (elttype, NULL,
42ae5230 297 value_address (val), -1,
9a22f0d0 298 stream, options) * len);
6c7a06a3 299 }
ea37ba09 300 return;
c906108c 301
ea37ba09
DJ
302 case 'i':
303 /* We often wrap here if there are long symbolic names. */
304 wrap_here (" ");
42ae5230 305 next_address = (value_address (val)
13274fc3
UW
306 + gdb_print_insn (get_type_arch (type),
307 value_address (val), stream,
ea37ba09
DJ
308 &branch_delay_insns));
309 return;
310 }
c906108c 311 }
ea37ba09 312
79a45b7d 313 if (options->format == 0 || options->format == 's'
4e885b20 314 || TYPE_CODE (type) == TYPE_CODE_REF
ea37ba09
DJ
315 || TYPE_CODE (type) == TYPE_CODE_ARRAY
316 || TYPE_CODE (type) == TYPE_CODE_STRING
317 || TYPE_CODE (type) == TYPE_CODE_STRUCT
318 || TYPE_CODE (type) == TYPE_CODE_UNION
319 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
79a45b7d 320 value_print (val, stream, options);
ea37ba09 321 else
b021a221
MS
322 /* User specified format, so don't look to the type to tell us
323 what to do. */
ab2188aa 324 val_print_scalar_formatted (type,
ab2188aa
PA
325 value_embedded_offset (val),
326 val,
327 options, size, stream);
c906108c
SS
328}
329
b806fb9a
UW
330/* Return builtin floating point type of same length as TYPE.
331 If no such type is found, return TYPE itself. */
332static struct type *
50810684 333float_type_from_length (struct type *type)
b806fb9a 334{
50810684 335 struct gdbarch *gdbarch = get_type_arch (type);
b806fb9a 336 const struct builtin_type *builtin = builtin_type (gdbarch);
b806fb9a 337
744a8059 338 if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_float))
b806fb9a 339 type = builtin->builtin_float;
744a8059 340 else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_double))
b806fb9a 341 type = builtin->builtin_double;
744a8059 342 else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_long_double))
b806fb9a
UW
343 type = builtin->builtin_long_double;
344
345 return type;
346}
347
c906108c 348/* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
ab2188aa
PA
349 according to OPTIONS and SIZE on STREAM. Formats s and i are not
350 supported at this level. */
c906108c
SS
351
352void
7c543f7b 353print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
79a45b7d
TT
354 const struct value_print_options *options,
355 int size, struct ui_file *stream)
c906108c 356{
50810684 357 struct gdbarch *gdbarch = get_type_arch (type);
81cb7cc9 358 LONGEST val_long = 0;
c906108c 359 unsigned int len = TYPE_LENGTH (type);
69feb676 360 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c906108c 361
ab2188aa
PA
362 /* String printing should go through val_print_scalar_formatted. */
363 gdb_assert (options->format != 's');
ea37ba09 364
be56871e
JB
365 if (len > sizeof(LONGEST)
366 && (TYPE_CODE (type) == TYPE_CODE_INT
367 || TYPE_CODE (type) == TYPE_CODE_ENUM))
6b9acc27 368 {
79a45b7d 369 switch (options->format)
6b9acc27
JJ
370 {
371 case 'o':
d44e8473 372 print_octal_chars (stream, valaddr, len, byte_order);
6b9acc27
JJ
373 return;
374 case 'u':
375 case 'd':
d44e8473 376 print_decimal_chars (stream, valaddr, len, byte_order);
6b9acc27
JJ
377 return;
378 case 't':
d44e8473 379 print_binary_chars (stream, valaddr, len, byte_order);
6b9acc27
JJ
380 return;
381 case 'x':
d44e8473 382 print_hex_chars (stream, valaddr, len, byte_order);
6b9acc27
JJ
383 return;
384 case 'c':
6c7a06a3 385 print_char_chars (stream, type, valaddr, len, byte_order);
6b9acc27
JJ
386 return;
387 default:
388 break;
389 };
390 }
391
79a45b7d 392 if (options->format != 'f')
c906108c
SS
393 val_long = unpack_long (type, valaddr);
394
ef166cf4 395 /* If the value is a pointer, and pointers and addresses are not the
d0aee0c4 396 same, then at this point, the value's length (in target bytes) is
17a912b6 397 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
ef166cf4 398 if (TYPE_CODE (type) == TYPE_CODE_PTR)
69feb676 399 len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
ef166cf4 400
c906108c
SS
401 /* If we are printing it as unsigned, truncate it in case it is actually
402 a negative signed value (e.g. "print/u (short)-1" should print 65535
403 (if shorts are 16 bits) instead of 4294967295). */
1fac167a 404 if (options->format != 'd' || TYPE_UNSIGNED (type))
c906108c
SS
405 {
406 if (len < sizeof (LONGEST))
407 val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
408 }
409
79a45b7d 410 switch (options->format)
c906108c
SS
411 {
412 case 'x':
413 if (!size)
414 {
675dcf4f 415 /* No size specified, like in print. Print varying # of digits. */
c906108c
SS
416 print_longest (stream, 'x', 1, val_long);
417 }
418 else
419 switch (size)
420 {
421 case 'b':
422 case 'h':
423 case 'w':
424 case 'g':
425 print_longest (stream, size, 1, val_long);
426 break;
427 default:
8a3fe4f8 428 error (_("Undefined output size \"%c\"."), size);
c906108c
SS
429 }
430 break;
431
432 case 'd':
433 print_longest (stream, 'd', 1, val_long);
434 break;
435
436 case 'u':
437 print_longest (stream, 'u', 0, val_long);
438 break;
439
440 case 'o':
441 if (val_long)
442 print_longest (stream, 'o', 1, val_long);
443 else
444 fprintf_filtered (stream, "0");
445 break;
446
447 case 'a':
593de6a6 448 {
593de6a6 449 CORE_ADDR addr = unpack_pointer (type, valaddr);
ad3bbd48 450
5af949e3 451 print_address (gdbarch, addr, stream);
593de6a6 452 }
c906108c
SS
453 break;
454
455 case 'c':
79a45b7d
TT
456 {
457 struct value_print_options opts = *options;
69feb676 458
ad3bbd48 459 opts.format = 0;
79a45b7d 460 if (TYPE_UNSIGNED (type))
69feb676
UW
461 type = builtin_type (gdbarch)->builtin_true_unsigned_char;
462 else
463 type = builtin_type (gdbarch)->builtin_true_char;
464
465 value_print (value_from_longest (type, val_long), stream, &opts);
79a45b7d 466 }
c906108c
SS
467 break;
468
469 case 'f':
50810684 470 type = float_type_from_length (type);
c906108c
SS
471 print_floating (valaddr, type, stream);
472 break;
473
474 case 0:
675dcf4f
MK
475 internal_error (__FILE__, __LINE__,
476 _("failed internal consistency check"));
c906108c
SS
477
478 case 't':
479 /* Binary; 't' stands for "two". */
480 {
c5aa993b
JM
481 char bits[8 * (sizeof val_long) + 1];
482 char buf[8 * (sizeof val_long) + 32];
c906108c
SS
483 char *cp = bits;
484 int width;
485
c5aa993b
JM
486 if (!size)
487 width = 8 * (sizeof val_long);
488 else
489 switch (size)
c906108c
SS
490 {
491 case 'b':
492 width = 8;
493 break;
494 case 'h':
495 width = 16;
496 break;
497 case 'w':
498 width = 32;
499 break;
500 case 'g':
501 width = 64;
502 break;
503 default:
8a3fe4f8 504 error (_("Undefined output size \"%c\"."), size);
c906108c
SS
505 }
506
c5aa993b
JM
507 bits[width] = '\0';
508 while (width-- > 0)
509 {
510 bits[width] = (val_long & 1) ? '1' : '0';
511 val_long >>= 1;
512 }
c906108c
SS
513 if (!size)
514 {
515 while (*cp && *cp == '0')
516 cp++;
517 if (*cp == '\0')
518 cp--;
519 }
daac021a 520 strncpy (buf, cp, sizeof (bits));
306d9ac5 521 fputs_filtered (buf, stream);
c906108c
SS
522 }
523 break;
524
6fbe845e
AB
525 case 'z':
526 print_hex_chars (stream, valaddr, len, byte_order);
527 break;
528
c906108c 529 default:
79a45b7d 530 error (_("Undefined output format \"%c\"."), options->format);
c906108c
SS
531 }
532}
533
534/* Specify default address for `x' command.
675dcf4f 535 The `info lines' command uses this. */
c906108c
SS
536
537void
8b9b9e1a 538set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
c906108c 539{
8b9b9e1a
UW
540 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
541
5d3729b5 542 next_gdbarch = gdbarch;
c906108c
SS
543 next_address = addr;
544
545 /* Make address available to the user as $_. */
546 set_internalvar (lookup_internalvar ("_"),
8b9b9e1a 547 value_from_pointer (ptr_type, addr));
c906108c
SS
548}
549
550/* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
551 after LEADIN. Print nothing if no symbolic name is found nearby.
552 Optionally also print source file and line number, if available.
553 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
554 or to interpret it as a possible C++ name and convert it back to source
555 form. However note that DO_DEMANGLE can be overridden by the specific
9cb709b6
TT
556 settings of the demangle and asm_demangle variables. Returns
557 non-zero if anything was printed; zero otherwise. */
c906108c 558
9cb709b6 559int
22e722e1
DJ
560print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
561 struct ui_file *stream,
675dcf4f 562 int do_demangle, char *leadin)
dfcd3bfb
JM
563{
564 char *name = NULL;
565 char *filename = NULL;
566 int unmapped = 0;
567 int offset = 0;
568 int line = 0;
569
675dcf4f 570 /* Throw away both name and filename. */
2f9429ae
AC
571 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
572 make_cleanup (free_current_contents, &filename);
dfcd3bfb 573
22e722e1 574 if (build_address_symbolic (gdbarch, addr, do_demangle, &name, &offset,
675dcf4f 575 &filename, &line, &unmapped))
2f9429ae
AC
576 {
577 do_cleanups (cleanup_chain);
9cb709b6 578 return 0;
2f9429ae 579 }
dfcd3bfb
JM
580
581 fputs_filtered (leadin, stream);
582 if (unmapped)
583 fputs_filtered ("<*", stream);
584 else
585 fputs_filtered ("<", stream);
586 fputs_filtered (name, stream);
587 if (offset != 0)
588 fprintf_filtered (stream, "+%u", (unsigned int) offset);
589
590 /* Append source filename and line number if desired. Give specific
591 line # of this addr, if we have it; else line # of the nearest symbol. */
592 if (print_symbol_filename && filename != NULL)
593 {
594 if (line != -1)
595 fprintf_filtered (stream, " at %s:%d", filename, line);
596 else
597 fprintf_filtered (stream, " in %s", filename);
598 }
599 if (unmapped)
600 fputs_filtered ("*>", stream);
601 else
602 fputs_filtered (">", stream);
603
604 do_cleanups (cleanup_chain);
9cb709b6 605 return 1;
dfcd3bfb
JM
606}
607
608/* Given an address ADDR return all the elements needed to print the
0df8b418 609 address in a symbolic form. NAME can be mangled or not depending
dfcd3bfb 610 on DO_DEMANGLE (and also on the asm_demangle global variable,
0df8b418
MS
611 manipulated via ''set print asm-demangle''). Return 0 in case of
612 success, when all the info in the OUT paramters is valid. Return 1
613 otherwise. */
dfcd3bfb 614int
22e722e1
DJ
615build_address_symbolic (struct gdbarch *gdbarch,
616 CORE_ADDR addr, /* IN */
dfcd3bfb
JM
617 int do_demangle, /* IN */
618 char **name, /* OUT */
619 int *offset, /* OUT */
620 char **filename, /* OUT */
621 int *line, /* OUT */
622 int *unmapped) /* OUT */
c906108c 623{
77e371c0 624 struct bound_minimal_symbol msymbol;
c906108c 625 struct symbol *symbol;
c906108c 626 CORE_ADDR name_location = 0;
714835d5 627 struct obj_section *section = NULL;
0d5cff50 628 const char *name_temp = "";
dfcd3bfb 629
89c83b10 630 /* Let's say it is mapped (not unmapped). */
dfcd3bfb 631 *unmapped = 0;
c906108c 632
dfcd3bfb 633 /* Determine if the address is in an overlay, and whether it is
675dcf4f 634 mapped. */
c906108c
SS
635 if (overlay_debugging)
636 {
637 section = find_pc_overlay (addr);
638 if (pc_in_unmapped_range (addr, section))
639 {
dfcd3bfb 640 *unmapped = 1;
c906108c
SS
641 addr = overlay_mapped_address (addr, section);
642 }
643 }
644
c906108c
SS
645 /* First try to find the address in the symbol table, then
646 in the minsyms. Take the closest one. */
647
648 /* This is defective in the sense that it only finds text symbols. So
649 really this is kind of pointless--we should make sure that the
650 minimal symbols have everything we need (by changing that we could
651 save some memory, but for many debug format--ELF/DWARF or
652 anything/stabs--it would be inconvenient to eliminate those minimal
653 symbols anyway). */
77e371c0 654 msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
c906108c
SS
655 symbol = find_pc_sect_function (addr, section);
656
657 if (symbol)
658 {
22e722e1
DJ
659 /* If this is a function (i.e. a code address), strip out any
660 non-address bits. For instance, display a pointer to the
661 first instruction of a Thumb function as <function>; the
662 second instruction will be <function+2>, even though the
663 pointer is <function+3>. This matches the ISA behavior. */
664 addr = gdbarch_addr_bits_remove (gdbarch, addr);
665
c906108c 666 name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
406fc7fb 667 if (do_demangle || asm_demangle)
de5ad195 668 name_temp = SYMBOL_PRINT_NAME (symbol);
c906108c 669 else
3567439c 670 name_temp = SYMBOL_LINKAGE_NAME (symbol);
c906108c
SS
671 }
672
77e371c0
TT
673 if (msymbol.minsym != NULL
674 && MSYMBOL_HAS_SIZE (msymbol.minsym)
675 && MSYMBOL_SIZE (msymbol.minsym) == 0
676 && MSYMBOL_TYPE (msymbol.minsym) != mst_text
677 && MSYMBOL_TYPE (msymbol.minsym) != mst_text_gnu_ifunc
678 && MSYMBOL_TYPE (msymbol.minsym) != mst_file_text)
679 msymbol.minsym = NULL;
9cb709b6 680
77e371c0 681 if (msymbol.minsym != NULL)
c906108c 682 {
77e371c0 683 if (BMSYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
c906108c 684 {
fe8400b4
WN
685 /* If this is a function (i.e. a code address), strip out any
686 non-address bits. For instance, display a pointer to the
687 first instruction of a Thumb function as <function>; the
688 second instruction will be <function+2>, even though the
689 pointer is <function+3>. This matches the ISA behavior. */
77e371c0
TT
690 if (MSYMBOL_TYPE (msymbol.minsym) == mst_text
691 || MSYMBOL_TYPE (msymbol.minsym) == mst_text_gnu_ifunc
692 || MSYMBOL_TYPE (msymbol.minsym) == mst_file_text
693 || MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
fe8400b4
WN
694 addr = gdbarch_addr_bits_remove (gdbarch, addr);
695
c906108c
SS
696 /* The msymbol is closer to the address than the symbol;
697 use the msymbol instead. */
698 symbol = 0;
77e371c0 699 name_location = BMSYMBOL_VALUE_ADDRESS (msymbol);
406fc7fb 700 if (do_demangle || asm_demangle)
77e371c0 701 name_temp = MSYMBOL_PRINT_NAME (msymbol.minsym);
c906108c 702 else
77e371c0 703 name_temp = MSYMBOL_LINKAGE_NAME (msymbol.minsym);
c906108c
SS
704 }
705 }
77e371c0 706 if (symbol == NULL && msymbol.minsym == NULL)
dfcd3bfb 707 return 1;
c906108c 708
c906108c
SS
709 /* If the nearest symbol is too far away, don't print anything symbolic. */
710
711 /* For when CORE_ADDR is larger than unsigned int, we do math in
712 CORE_ADDR. But when we detect unsigned wraparound in the
713 CORE_ADDR math, we ignore this test and print the offset,
714 because addr+max_symbolic_offset has wrapped through the end
715 of the address space back to the beginning, giving bogus comparison. */
716 if (addr > name_location + max_symbolic_offset
717 && name_location + max_symbolic_offset > name_location)
dfcd3bfb 718 return 1;
c906108c 719
dfcd3bfb
JM
720 *offset = addr - name_location;
721
722 *name = xstrdup (name_temp);
c906108c 723
c906108c
SS
724 if (print_symbol_filename)
725 {
726 struct symtab_and_line sal;
727
728 sal = find_pc_sect_line (addr, section, 0);
729
730 if (sal.symtab)
dfcd3bfb 731 {
05cba821 732 *filename = xstrdup (symtab_to_filename_for_display (sal.symtab));
dfcd3bfb
JM
733 *line = sal.line;
734 }
c906108c 735 }
dfcd3bfb 736 return 0;
c906108c
SS
737}
738
c906108c
SS
739
740/* Print address ADDR symbolically on STREAM.
741 First print it as a number. Then perhaps print
742 <SYMBOL + OFFSET> after the number. */
743
744void
5af949e3
UW
745print_address (struct gdbarch *gdbarch,
746 CORE_ADDR addr, struct ui_file *stream)
c906108c 747{
5af949e3 748 fputs_filtered (paddress (gdbarch, addr), stream);
22e722e1 749 print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
c906108c
SS
750}
751
2b28d209
PP
752/* Return a prefix for instruction address:
753 "=> " for current instruction, else " ". */
754
755const char *
756pc_prefix (CORE_ADDR addr)
757{
758 if (has_stack_frames ())
759 {
760 struct frame_info *frame;
761 CORE_ADDR pc;
762
763 frame = get_selected_frame (NULL);
ce406537 764 if (get_frame_pc_if_available (frame, &pc) && pc == addr)
2b28d209
PP
765 return "=> ";
766 }
767 return " ";
768}
769
c906108c
SS
770/* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
771 controls whether to print the symbolic name "raw" or demangled.
9cb709b6 772 Return non-zero if anything was printed; zero otherwise. */
c906108c 773
9cb709b6 774int
edf0c1b7
TT
775print_address_demangle (const struct value_print_options *opts,
776 struct gdbarch *gdbarch, CORE_ADDR addr,
5af949e3 777 struct ui_file *stream, int do_demangle)
c906108c 778{
1d51a733 779 if (opts->addressprint)
c906108c 780 {
5af949e3 781 fputs_filtered (paddress (gdbarch, addr), stream);
22e722e1 782 print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
c906108c
SS
783 }
784 else
785 {
9cb709b6 786 return print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
c906108c 787 }
9cb709b6 788 return 1;
c906108c
SS
789}
790\f
791
bb556f1f
TK
792/* Find the address of the instruction that is INST_COUNT instructions before
793 the instruction at ADDR.
794 Since some architectures have variable-length instructions, we can't just
795 simply subtract INST_COUNT * INSN_LEN from ADDR. Instead, we use line
796 number information to locate the nearest known instruction boundary,
797 and disassemble forward from there. If we go out of the symbol range
798 during disassembling, we return the lowest address we've got so far and
799 set the number of instructions read to INST_READ. */
800
801static CORE_ADDR
802find_instruction_backward (struct gdbarch *gdbarch, CORE_ADDR addr,
803 int inst_count, int *inst_read)
804{
805 /* The vector PCS is used to store instruction addresses within
806 a pc range. */
807 CORE_ADDR loop_start, loop_end, p;
808 VEC (CORE_ADDR) *pcs = NULL;
809 struct symtab_and_line sal;
810 struct cleanup *cleanup = make_cleanup (VEC_cleanup (CORE_ADDR), &pcs);
811
812 *inst_read = 0;
813 loop_start = loop_end = addr;
814
815 /* In each iteration of the outer loop, we get a pc range that ends before
816 LOOP_START, then we count and store every instruction address of the range
817 iterated in the loop.
818 If the number of instructions counted reaches INST_COUNT, return the
819 stored address that is located INST_COUNT instructions back from ADDR.
820 If INST_COUNT is not reached, we subtract the number of counted
821 instructions from INST_COUNT, and go to the next iteration. */
822 do
823 {
824 VEC_truncate (CORE_ADDR, pcs, 0);
825 sal = find_pc_sect_line (loop_start, NULL, 1);
826 if (sal.line <= 0)
827 {
828 /* We reach here when line info is not available. In this case,
829 we print a message and just exit the loop. The return value
830 is calculated after the loop. */
831 printf_filtered (_("No line number information available "
832 "for address "));
833 wrap_here (" ");
834 print_address (gdbarch, loop_start - 1, gdb_stdout);
835 printf_filtered ("\n");
836 break;
837 }
838
839 loop_end = loop_start;
840 loop_start = sal.pc;
841
842 /* This loop pushes instruction addresses in the range from
843 LOOP_START to LOOP_END. */
844 for (p = loop_start; p < loop_end;)
845 {
846 VEC_safe_push (CORE_ADDR, pcs, p);
847 p += gdb_insn_length (gdbarch, p);
848 }
849
850 inst_count -= VEC_length (CORE_ADDR, pcs);
851 *inst_read += VEC_length (CORE_ADDR, pcs);
852 }
853 while (inst_count > 0);
854
855 /* After the loop, the vector PCS has instruction addresses of the last
856 source line we processed, and INST_COUNT has a negative value.
857 We return the address at the index of -INST_COUNT in the vector for
858 the reason below.
859 Let's assume the following instruction addresses and run 'x/-4i 0x400e'.
860 Line X of File
861 0x4000
862 0x4001
863 0x4005
864 Line Y of File
865 0x4009
866 0x400c
867 => 0x400e
868 0x4011
869 find_instruction_backward is called with INST_COUNT = 4 and expected to
870 return 0x4001. When we reach here, INST_COUNT is set to -1 because
871 it was subtracted by 2 (from Line Y) and 3 (from Line X). The value
872 4001 is located at the index 1 of the last iterated line (= Line X),
873 which is simply calculated by -INST_COUNT.
874 The case when the length of PCS is 0 means that we reached an area for
875 which line info is not available. In such case, we return LOOP_START,
876 which was the lowest instruction address that had line info. */
877 p = VEC_length (CORE_ADDR, pcs) > 0
878 ? VEC_index (CORE_ADDR, pcs, -inst_count)
879 : loop_start;
880
881 /* INST_READ includes all instruction addresses in a pc range. Need to
882 exclude the beginning part up to the address we're returning. That
883 is, exclude {0x4000} in the example above. */
884 if (inst_count < 0)
885 *inst_read += inst_count;
886
887 do_cleanups (cleanup);
888 return p;
889}
890
891/* Backward read LEN bytes of target memory from address MEMADDR + LEN,
892 placing the results in GDB's memory from MYADDR + LEN. Returns
893 a count of the bytes actually read. */
894
895static int
896read_memory_backward (struct gdbarch *gdbarch,
897 CORE_ADDR memaddr, gdb_byte *myaddr, int len)
898{
899 int errcode;
900 int nread; /* Number of bytes actually read. */
901
902 /* First try a complete read. */
903 errcode = target_read_memory (memaddr, myaddr, len);
904 if (errcode == 0)
905 {
906 /* Got it all. */
907 nread = len;
908 }
909 else
910 {
911 /* Loop, reading one byte at a time until we get as much as we can. */
912 memaddr += len;
913 myaddr += len;
914 for (nread = 0; nread < len; ++nread)
915 {
916 errcode = target_read_memory (--memaddr, --myaddr, 1);
917 if (errcode != 0)
918 {
919 /* The read was unsuccessful, so exit the loop. */
920 printf_filtered (_("Cannot access memory at address %s\n"),
921 paddress (gdbarch, memaddr));
922 break;
923 }
924 }
925 }
926 return nread;
927}
928
929/* Returns true if X (which is LEN bytes wide) is the number zero. */
930
931static int
932integer_is_zero (const gdb_byte *x, int len)
933{
934 int i = 0;
935
936 while (i < len && x[i] == 0)
937 ++i;
938 return (i == len);
939}
940
941/* Find the start address of a string in which ADDR is included.
942 Basically we search for '\0' and return the next address,
943 but if OPTIONS->PRINT_MAX is smaller than the length of a string,
944 we stop searching and return the address to print characters as many as
945 PRINT_MAX from the string. */
946
947static CORE_ADDR
948find_string_backward (struct gdbarch *gdbarch,
949 CORE_ADDR addr, int count, int char_size,
950 const struct value_print_options *options,
951 int *strings_counted)
952{
953 const int chunk_size = 0x20;
954 gdb_byte *buffer = NULL;
955 struct cleanup *cleanup = NULL;
956 int read_error = 0;
957 int chars_read = 0;
958 int chars_to_read = chunk_size;
959 int chars_counted = 0;
960 int count_original = count;
961 CORE_ADDR string_start_addr = addr;
962
963 gdb_assert (char_size == 1 || char_size == 2 || char_size == 4);
964 buffer = (gdb_byte *) xmalloc (chars_to_read * char_size);
965 cleanup = make_cleanup (xfree, buffer);
966 while (count > 0 && read_error == 0)
967 {
968 int i;
969
970 addr -= chars_to_read * char_size;
971 chars_read = read_memory_backward (gdbarch, addr, buffer,
972 chars_to_read * char_size);
973 chars_read /= char_size;
974 read_error = (chars_read == chars_to_read) ? 0 : 1;
975 /* Searching for '\0' from the end of buffer in backward direction. */
976 for (i = 0; i < chars_read && count > 0 ; ++i, ++chars_counted)
977 {
978 int offset = (chars_to_read - i - 1) * char_size;
979
980 if (integer_is_zero (buffer + offset, char_size)
981 || chars_counted == options->print_max)
982 {
983 /* Found '\0' or reached print_max. As OFFSET is the offset to
984 '\0', we add CHAR_SIZE to return the start address of
985 a string. */
986 --count;
987 string_start_addr = addr + offset + char_size;
988 chars_counted = 0;
989 }
990 }
991 }
992
993 /* Update STRINGS_COUNTED with the actual number of loaded strings. */
994 *strings_counted = count_original - count;
995
996 if (read_error != 0)
997 {
998 /* In error case, STRING_START_ADDR is pointing to the string that
999 was last successfully loaded. Rewind the partially loaded string. */
1000 string_start_addr -= chars_counted * char_size;
1001 }
1002
1003 do_cleanups (cleanup);
1004 return string_start_addr;
1005}
1006
c906108c
SS
1007/* Examine data at address ADDR in format FMT.
1008 Fetch it from memory and print on gdb_stdout. */
1009
1010static void
5d3729b5 1011do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
c906108c 1012{
52f0bd74
AC
1013 char format = 0;
1014 char size;
1015 int count = 1;
c906108c 1016 struct type *val_type = NULL;
52f0bd74
AC
1017 int i;
1018 int maxelts;
79a45b7d 1019 struct value_print_options opts;
bb556f1f
TK
1020 int need_to_update_next_address = 0;
1021 CORE_ADDR addr_rewound = 0;
c906108c
SS
1022
1023 format = fmt.format;
1024 size = fmt.size;
1025 count = fmt.count;
5d3729b5 1026 next_gdbarch = gdbarch;
c906108c 1027 next_address = addr;
c906108c 1028
9a22f0d0
PM
1029 /* Instruction format implies fetch single bytes
1030 regardless of the specified size.
1031 The case of strings is handled in decode_format, only explicit
1032 size operator are not changed to 'b'. */
1033 if (format == 'i')
c906108c
SS
1034 size = 'b';
1035
5d3729b5
UW
1036 if (size == 'a')
1037 {
1038 /* Pick the appropriate size for an address. */
1039 if (gdbarch_ptr_bit (next_gdbarch) == 64)
1040 size = 'g';
1041 else if (gdbarch_ptr_bit (next_gdbarch) == 32)
1042 size = 'w';
1043 else if (gdbarch_ptr_bit (next_gdbarch) == 16)
1044 size = 'h';
1045 else
1046 /* Bad value for gdbarch_ptr_bit. */
1047 internal_error (__FILE__, __LINE__,
1048 _("failed internal consistency check"));
1049 }
1050
1051 if (size == 'b')
df4df182 1052 val_type = builtin_type (next_gdbarch)->builtin_int8;
c906108c 1053 else if (size == 'h')
df4df182 1054 val_type = builtin_type (next_gdbarch)->builtin_int16;
c906108c 1055 else if (size == 'w')
df4df182 1056 val_type = builtin_type (next_gdbarch)->builtin_int32;
c906108c 1057 else if (size == 'g')
df4df182 1058 val_type = builtin_type (next_gdbarch)->builtin_int64;
c906108c 1059
9a22f0d0
PM
1060 if (format == 's')
1061 {
1062 struct type *char_type = NULL;
ad3bbd48 1063
9a22f0d0
PM
1064 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
1065 if type is not found. */
1066 if (size == 'h')
1067 char_type = builtin_type (next_gdbarch)->builtin_char16;
1068 else if (size == 'w')
1069 char_type = builtin_type (next_gdbarch)->builtin_char32;
1070 if (char_type)
1071 val_type = char_type;
1072 else
1073 {
1074 if (size != '\0' && size != 'b')
0df8b418
MS
1075 warning (_("Unable to display strings with "
1076 "size '%c', using 'b' instead."), size);
9a22f0d0
PM
1077 size = 'b';
1078 val_type = builtin_type (next_gdbarch)->builtin_int8;
1079 }
1080 }
1081
c906108c
SS
1082 maxelts = 8;
1083 if (size == 'w')
1084 maxelts = 4;
1085 if (size == 'g')
1086 maxelts = 2;
1087 if (format == 's' || format == 'i')
1088 maxelts = 1;
1089
79a45b7d
TT
1090 get_formatted_print_options (&opts, format);
1091
bb556f1f
TK
1092 if (count < 0)
1093 {
1094 /* This is the negative repeat count case.
1095 We rewind the address based on the given repeat count and format,
1096 then examine memory from there in forward direction. */
1097
1098 count = -count;
1099 if (format == 'i')
1100 {
1101 next_address = find_instruction_backward (gdbarch, addr, count,
1102 &count);
1103 }
1104 else if (format == 's')
1105 {
1106 next_address = find_string_backward (gdbarch, addr, count,
1107 TYPE_LENGTH (val_type),
1108 &opts, &count);
1109 }
1110 else
1111 {
1112 next_address = addr - count * TYPE_LENGTH (val_type);
1113 }
1114
1115 /* The following call to print_formatted updates next_address in every
1116 iteration. In backward case, we store the start address here
1117 and update next_address with it before exiting the function. */
1118 addr_rewound = (format == 's'
1119 ? next_address - TYPE_LENGTH (val_type)
1120 : next_address);
1121 need_to_update_next_address = 1;
1122 }
1123
c906108c
SS
1124 /* Print as many objects as specified in COUNT, at most maxelts per line,
1125 with the address of the next one at the start of each line. */
1126
1127 while (count > 0)
1128 {
1129 QUIT;
2b28d209
PP
1130 if (format == 'i')
1131 fputs_filtered (pc_prefix (next_address), gdb_stdout);
5af949e3 1132 print_address (next_gdbarch, next_address, gdb_stdout);
c906108c
SS
1133 printf_filtered (":");
1134 for (i = maxelts;
1135 i > 0 && count > 0;
1136 i--, count--)
1137 {
1138 printf_filtered ("\t");
1139 /* Note that print_formatted sets next_address for the next
1140 object. */
1141 last_examine_address = next_address;
1142
1143 if (last_examine_value)
1144 value_free (last_examine_value);
1145
1146 /* The value to be displayed is not fetched greedily.
5d51a2db
MR
1147 Instead, to avoid the possibility of a fetched value not
1148 being used, its retrieval is delayed until the print code
c5aa993b
JM
1149 uses it. When examining an instruction stream, the
1150 disassembler will perform its own memory fetch using just
1151 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
1152 the disassembler be modified so that LAST_EXAMINE_VALUE
1153 is left with the byte sequence from the last complete
0df8b418 1154 instruction fetched from memory? */
00a4c844 1155 last_examine_value = value_at_lazy (val_type, next_address);
c906108c
SS
1156
1157 if (last_examine_value)
1158 release_value (last_examine_value);
1159
79a45b7d 1160 print_formatted (last_examine_value, size, &opts, gdb_stdout);
a4642986
MR
1161
1162 /* Display any branch delay slots following the final insn. */
1163 if (format == 'i' && count == 1)
1164 count += branch_delay_insns;
c906108c
SS
1165 }
1166 printf_filtered ("\n");
1167 gdb_flush (gdb_stdout);
1168 }
bb556f1f
TK
1169
1170 if (need_to_update_next_address)
1171 next_address = addr_rewound;
c906108c
SS
1172}
1173\f
1174static void
8d89f51a 1175validate_format (struct format_data fmt, const char *cmdname)
c906108c
SS
1176{
1177 if (fmt.size != 0)
8a3fe4f8 1178 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
c906108c 1179 if (fmt.count != 1)
8a3fe4f8 1180 error (_("Item count other than 1 is meaningless in \"%s\" command."),
c906108c 1181 cmdname);
ea37ba09 1182 if (fmt.format == 'i')
8a3fe4f8 1183 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
c906108c
SS
1184 fmt.format, cmdname);
1185}
1186
1c88ceb1
JK
1187/* Parse print command format string into *FMTP and update *EXPP.
1188 CMDNAME should name the current command. */
1189
1190void
1191print_command_parse_format (const char **expp, const char *cmdname,
1192 struct format_data *fmtp)
1193{
1194 const char *exp = *expp;
1195
1196 if (exp && *exp == '/')
1197 {
1198 exp++;
1199 *fmtp = decode_format (&exp, last_format, 0);
1200 validate_format (*fmtp, cmdname);
1201 last_format = fmtp->format;
1202 }
1203 else
1204 {
1205 fmtp->count = 1;
1206 fmtp->format = 0;
1207 fmtp->size = 0;
1208 fmtp->raw = 0;
1209 }
1210
1211 *expp = exp;
1212}
1213
1214/* Print VAL to console according to *FMTP, including recording it to
1215 the history. */
1216
1217void
1218print_value (struct value *val, const struct format_data *fmtp)
1219{
1220 struct value_print_options opts;
1221 int histindex = record_latest_value (val);
1222
1223 annotate_value_history_begin (histindex, value_type (val));
1224
1225 printf_filtered ("$%d = ", histindex);
1226
1227 annotate_value_history_value ();
1228
1229 get_formatted_print_options (&opts, fmtp->format);
1230 opts.raw = fmtp->raw;
1231
1232 print_formatted (val, fmtp->size, &opts, gdb_stdout);
1233 printf_filtered ("\n");
1234
1235 annotate_value_history_end ();
1236}
1237
675dcf4f 1238/* Evaluate string EXP as an expression in the current language and
c5aa993b 1239 print the resulting value. EXP may contain a format specifier as the
675dcf4f 1240 first argument ("/x myvar" for example, to print myvar in hex). */
c906108c
SS
1241
1242static void
6f937416 1243print_command_1 (const char *exp, int voidprint)
c906108c 1244{
3d6d86c6 1245 struct value *val;
c906108c 1246 struct format_data fmt;
c906108c 1247
1c88ceb1 1248 print_command_parse_format (&exp, "print", &fmt);
c906108c
SS
1249
1250 if (exp && *exp)
1251 {
4d01a485
PA
1252 expression_up expr = parse_expression (exp);
1253 val = evaluate_expression (expr.get ());
c906108c
SS
1254 }
1255 else
1256 val = access_value_history (0);
1257
df407dfe
AC
1258 if (voidprint || (val && value_type (val) &&
1259 TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
1c88ceb1 1260 print_value (val, &fmt);
c906108c
SS
1261}
1262
c906108c 1263static void
fba45db2 1264print_command (char *exp, int from_tty)
c906108c 1265{
e93a8774 1266 print_command_1 (exp, 1);
c906108c
SS
1267}
1268
675dcf4f 1269/* Same as print, except it doesn't print void results. */
c906108c 1270static void
fba45db2 1271call_command (char *exp, int from_tty)
c906108c 1272{
e93a8774 1273 print_command_1 (exp, 0);
c906108c
SS
1274}
1275
6f937416
PA
1276/* Implementation of the "output" command. */
1277
1278static void
fba45db2 1279output_command (char *exp, int from_tty)
6f937416
PA
1280{
1281 output_command_const (exp, from_tty);
1282}
1283
1284/* Like output_command, but takes a const string as argument. */
1285
1286void
1287output_command_const (const char *exp, int from_tty)
c906108c 1288{
52f0bd74 1289 char format = 0;
3d6d86c6 1290 struct value *val;
c906108c 1291 struct format_data fmt;
79a45b7d 1292 struct value_print_options opts;
c906108c 1293
777ea8f1 1294 fmt.size = 0;
a6bac58e 1295 fmt.raw = 0;
777ea8f1 1296
c906108c
SS
1297 if (exp && *exp == '/')
1298 {
1299 exp++;
1300 fmt = decode_format (&exp, 0, 0);
1301 validate_format (fmt, "output");
1302 format = fmt.format;
1303 }
1304
4d01a485 1305 expression_up expr = parse_expression (exp);
c906108c 1306
4d01a485 1307 val = evaluate_expression (expr.get ());
c906108c 1308
df407dfe 1309 annotate_value_begin (value_type (val));
c906108c 1310
79a45b7d 1311 get_formatted_print_options (&opts, format);
a6bac58e 1312 opts.raw = fmt.raw;
79a45b7d 1313 print_formatted (val, fmt.size, &opts, gdb_stdout);
c906108c
SS
1314
1315 annotate_value_end ();
1316
2acceee2
JM
1317 wrap_here ("");
1318 gdb_flush (gdb_stdout);
c906108c
SS
1319}
1320
c906108c 1321static void
fba45db2 1322set_command (char *exp, int from_tty)
c906108c 1323{
4d01a485 1324 expression_up expr = parse_expression (exp);
ad3bbd48 1325
0ece64fd
TG
1326 if (expr->nelts >= 1)
1327 switch (expr->elts[0].opcode)
1328 {
1329 case UNOP_PREINCREMENT:
1330 case UNOP_POSTINCREMENT:
1331 case UNOP_PREDECREMENT:
1332 case UNOP_POSTDECREMENT:
1333 case BINOP_ASSIGN:
1334 case BINOP_ASSIGN_MODIFY:
1335 case BINOP_COMMA:
1336 break;
1337 default:
1338 warning
1339 (_("Expression is not an assignment (and might have no effect)"));
1340 }
52b3699b 1341
4d01a485 1342 evaluate_expression (expr.get ());
c906108c
SS
1343}
1344
c906108c 1345static void
fba45db2 1346sym_info (char *arg, int from_tty)
c906108c
SS
1347{
1348 struct minimal_symbol *msymbol;
c5aa993b
JM
1349 struct objfile *objfile;
1350 struct obj_section *osect;
c5aa993b
JM
1351 CORE_ADDR addr, sect_addr;
1352 int matches = 0;
1353 unsigned int offset;
c906108c
SS
1354
1355 if (!arg)
e2e0b3e5 1356 error_no_arg (_("address"));
c906108c
SS
1357
1358 addr = parse_and_eval_address (arg);
1359 ALL_OBJSECTIONS (objfile, osect)
c5aa993b 1360 {
94277a38
DJ
1361 /* Only process each object file once, even if there's a separate
1362 debug file. */
1363 if (objfile->separate_debug_objfile_backlink)
1364 continue;
1365
714835d5 1366 sect_addr = overlay_mapped_address (addr, osect);
c906108c 1367
f1f6aadf
PA
1368 if (obj_section_addr (osect) <= sect_addr
1369 && sect_addr < obj_section_endaddr (osect)
7cbd4a93
TT
1370 && (msymbol
1371 = lookup_minimal_symbol_by_pc_section (sect_addr, osect).minsym))
c5aa993b 1372 {
c14c28ba 1373 const char *obj_name, *mapped, *sec_name, *msym_name;
e2fd701e
DE
1374 char *loc_string;
1375 struct cleanup *old_chain;
c14c28ba 1376
c5aa993b 1377 matches = 1;
77e371c0 1378 offset = sect_addr - MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
c14c28ba
PP
1379 mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
1380 sec_name = osect->the_bfd_section->name;
efd66ac6 1381 msym_name = MSYMBOL_PRINT_NAME (msymbol);
c14c28ba 1382
e2fd701e
DE
1383 /* Don't print the offset if it is zero.
1384 We assume there's no need to handle i18n of "sym + offset". */
1385 if (offset)
549ba0f8 1386 loc_string = xstrprintf ("%s + %u", msym_name, offset);
e2fd701e 1387 else
549ba0f8 1388 loc_string = xstrprintf ("%s", msym_name);
e2fd701e
DE
1389
1390 /* Use a cleanup to free loc_string in case the user quits
1391 a pagination request inside printf_filtered. */
1392 old_chain = make_cleanup (xfree, loc_string);
1393
4262abfb
JK
1394 gdb_assert (osect->objfile && objfile_name (osect->objfile));
1395 obj_name = objfile_name (osect->objfile);
c14c28ba
PP
1396
1397 if (MULTI_OBJFILE_P ())
1398 if (pc_in_unmapped_range (addr, osect))
1399 if (section_is_overlay (osect))
e2fd701e 1400 printf_filtered (_("%s in load address range of "
c14c28ba 1401 "%s overlay section %s of %s\n"),
e2fd701e 1402 loc_string, mapped, sec_name, obj_name);
c14c28ba 1403 else
e2fd701e 1404 printf_filtered (_("%s in load address range of "
c14c28ba 1405 "section %s of %s\n"),
e2fd701e 1406 loc_string, sec_name, obj_name);
c14c28ba
PP
1407 else
1408 if (section_is_overlay (osect))
e2fd701e
DE
1409 printf_filtered (_("%s in %s overlay section %s of %s\n"),
1410 loc_string, mapped, sec_name, obj_name);
c14c28ba 1411 else
e2fd701e
DE
1412 printf_filtered (_("%s in section %s of %s\n"),
1413 loc_string, sec_name, obj_name);
c5aa993b 1414 else
c14c28ba
PP
1415 if (pc_in_unmapped_range (addr, osect))
1416 if (section_is_overlay (osect))
e2fd701e 1417 printf_filtered (_("%s in load address range of %s overlay "
c14c28ba 1418 "section %s\n"),
e2fd701e 1419 loc_string, mapped, sec_name);
c14c28ba 1420 else
e2fd701e
DE
1421 printf_filtered (_("%s in load address range of section %s\n"),
1422 loc_string, sec_name);
c14c28ba
PP
1423 else
1424 if (section_is_overlay (osect))
e2fd701e
DE
1425 printf_filtered (_("%s in %s overlay section %s\n"),
1426 loc_string, mapped, sec_name);
c14c28ba 1427 else
e2fd701e
DE
1428 printf_filtered (_("%s in section %s\n"),
1429 loc_string, sec_name);
1430
1431 do_cleanups (old_chain);
c5aa993b
JM
1432 }
1433 }
c906108c 1434 if (matches == 0)
a3f17187 1435 printf_filtered (_("No symbol matches %s.\n"), arg);
c906108c
SS
1436}
1437
c906108c 1438static void
fba45db2 1439address_info (char *exp, int from_tty)
c906108c 1440{
768a979c
UW
1441 struct gdbarch *gdbarch;
1442 int regno;
52f0bd74 1443 struct symbol *sym;
7c7b6655 1444 struct bound_minimal_symbol msymbol;
52f0bd74 1445 long val;
714835d5 1446 struct obj_section *section;
08922a10 1447 CORE_ADDR load_addr, context_pc = 0;
1993b719 1448 struct field_of_this_result is_a_field_of_this;
c906108c
SS
1449
1450 if (exp == 0)
8a3fe4f8 1451 error (_("Argument required."));
c906108c 1452
08922a10 1453 sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
d12307c1 1454 &is_a_field_of_this).symbol;
c906108c
SS
1455 if (sym == NULL)
1456 {
1993b719 1457 if (is_a_field_of_this.type != NULL)
c906108c
SS
1458 {
1459 printf_filtered ("Symbol \"");
1460 fprintf_symbol_filtered (gdb_stdout, exp,
1461 current_language->la_language, DMGL_ANSI);
e2b23ee9
AF
1462 printf_filtered ("\" is a field of the local class variable ");
1463 if (current_language->la_language == language_objc)
2625d86c 1464 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
e2b23ee9 1465 else
2625d86c 1466 printf_filtered ("`this'\n");
c906108c
SS
1467 return;
1468 }
1469
7c7b6655 1470 msymbol = lookup_bound_minimal_symbol (exp);
c906108c 1471
7c7b6655 1472 if (msymbol.minsym != NULL)
c906108c 1473 {
7c7b6655 1474 struct objfile *objfile = msymbol.objfile;
e27d198c
TT
1475
1476 gdbarch = get_objfile_arch (objfile);
77e371c0 1477 load_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
c906108c
SS
1478
1479 printf_filtered ("Symbol \"");
1480 fprintf_symbol_filtered (gdb_stdout, exp,
1481 current_language->la_language, DMGL_ANSI);
1482 printf_filtered ("\" is at ");
5af949e3 1483 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
c906108c 1484 printf_filtered (" in a file compiled without debugging");
efd66ac6 1485 section = MSYMBOL_OBJ_SECTION (objfile, msymbol.minsym);
c906108c
SS
1486 if (section_is_overlay (section))
1487 {
1488 load_addr = overlay_unmapped_address (load_addr, section);
1489 printf_filtered (",\n -- loaded at ");
5af949e3 1490 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
714835d5
UW
1491 printf_filtered (" in overlay section %s",
1492 section->the_bfd_section->name);
c906108c
SS
1493 }
1494 printf_filtered (".\n");
1495 }
1496 else
8a3fe4f8 1497 error (_("No symbol \"%s\" in current context."), exp);
c906108c
SS
1498 return;
1499 }
1500
1501 printf_filtered ("Symbol \"");
3567439c 1502 fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
c906108c
SS
1503 current_language->la_language, DMGL_ANSI);
1504 printf_filtered ("\" is ");
c5aa993b 1505 val = SYMBOL_VALUE (sym);
1994afbf
DE
1506 if (SYMBOL_OBJFILE_OWNED (sym))
1507 section = SYMBOL_OBJ_SECTION (symbol_objfile (sym), sym);
1508 else
1509 section = NULL;
08be3fe3 1510 gdbarch = symbol_arch (sym);
c906108c 1511
24d6c2a0
TT
1512 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
1513 {
1514 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
1515 gdb_stdout);
1516 printf_filtered (".\n");
1517 return;
1518 }
1519
c906108c
SS
1520 switch (SYMBOL_CLASS (sym))
1521 {
1522 case LOC_CONST:
1523 case LOC_CONST_BYTES:
1524 printf_filtered ("constant");
1525 break;
1526
1527 case LOC_LABEL:
1528 printf_filtered ("a label at address ");
5af949e3
UW
1529 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1530 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
c906108c
SS
1531 if (section_is_overlay (section))
1532 {
1533 load_addr = overlay_unmapped_address (load_addr, section);
1534 printf_filtered (",\n -- loaded at ");
5af949e3 1535 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
714835d5
UW
1536 printf_filtered (" in overlay section %s",
1537 section->the_bfd_section->name);
c906108c
SS
1538 }
1539 break;
1540
4c2df51b 1541 case LOC_COMPUTED:
24d6c2a0 1542 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
4c2df51b 1543
c906108c 1544 case LOC_REGISTER:
768a979c
UW
1545 /* GDBARCH is the architecture associated with the objfile the symbol
1546 is defined in; the target architecture may be different, and may
1547 provide additional registers. However, we do not know the target
1548 architecture at this point. We assume the objfile architecture
1549 will contain all the standard registers that occur in debug info
1550 in that objfile. */
1551 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1552
2a2d4dc3
AS
1553 if (SYMBOL_IS_ARGUMENT (sym))
1554 printf_filtered (_("an argument in register %s"),
768a979c 1555 gdbarch_register_name (gdbarch, regno));
2a2d4dc3
AS
1556 else
1557 printf_filtered (_("a variable in register %s"),
768a979c 1558 gdbarch_register_name (gdbarch, regno));
c906108c
SS
1559 break;
1560
1561 case LOC_STATIC:
a3f17187 1562 printf_filtered (_("static storage at address "));
5af949e3
UW
1563 load_addr = SYMBOL_VALUE_ADDRESS (sym);
1564 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
c906108c
SS
1565 if (section_is_overlay (section))
1566 {
1567 load_addr = overlay_unmapped_address (load_addr, section);
a3f17187 1568 printf_filtered (_(",\n -- loaded at "));
5af949e3 1569 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
714835d5
UW
1570 printf_filtered (_(" in overlay section %s"),
1571 section->the_bfd_section->name);
c906108c
SS
1572 }
1573 break;
1574
c906108c 1575 case LOC_REGPARM_ADDR:
768a979c
UW
1576 /* Note comment at LOC_REGISTER. */
1577 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
675dcf4f 1578 printf_filtered (_("address of an argument in register %s"),
768a979c 1579 gdbarch_register_name (gdbarch, regno));
c906108c
SS
1580 break;
1581
1582 case LOC_ARG:
a3f17187 1583 printf_filtered (_("an argument at offset %ld"), val);
c906108c
SS
1584 break;
1585
c906108c 1586 case LOC_LOCAL:
a3f17187 1587 printf_filtered (_("a local variable at frame offset %ld"), val);
c906108c
SS
1588 break;
1589
1590 case LOC_REF_ARG:
a3f17187 1591 printf_filtered (_("a reference argument at offset %ld"), val);
c906108c
SS
1592 break;
1593
c906108c 1594 case LOC_TYPEDEF:
a3f17187 1595 printf_filtered (_("a typedef"));
c906108c
SS
1596 break;
1597
1598 case LOC_BLOCK:
a3f17187 1599 printf_filtered (_("a function at address "));
675dcf4f 1600 load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
5af949e3 1601 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
c906108c
SS
1602 if (section_is_overlay (section))
1603 {
1604 load_addr = overlay_unmapped_address (load_addr, section);
a3f17187 1605 printf_filtered (_(",\n -- loaded at "));
5af949e3 1606 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
714835d5
UW
1607 printf_filtered (_(" in overlay section %s"),
1608 section->the_bfd_section->name);
c906108c
SS
1609 }
1610 break;
1611
1612 case LOC_UNRESOLVED:
1613 {
e27d198c 1614 struct bound_minimal_symbol msym;
c906108c 1615
e27d198c
TT
1616 msym = lookup_minimal_symbol_and_objfile (SYMBOL_LINKAGE_NAME (sym));
1617 if (msym.minsym == NULL)
c906108c
SS
1618 printf_filtered ("unresolved");
1619 else
1620 {
efd66ac6 1621 section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
e0740f77
JK
1622
1623 if (section
1624 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
5382cfab
PW
1625 {
1626 load_addr = MSYMBOL_VALUE_RAW_ADDRESS (msym.minsym);
1627 printf_filtered (_("a thread-local variable at offset %s "
1628 "in the thread-local storage for `%s'"),
1629 paddress (gdbarch, load_addr),
1630 objfile_name (section->objfile));
1631 }
e0740f77 1632 else
c906108c 1633 {
5382cfab 1634 load_addr = BMSYMBOL_VALUE_ADDRESS (msym);
e0740f77 1635 printf_filtered (_("static storage at address "));
5af949e3 1636 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
e0740f77
JK
1637 if (section_is_overlay (section))
1638 {
1639 load_addr = overlay_unmapped_address (load_addr, section);
1640 printf_filtered (_(",\n -- loaded at "));
5af949e3 1641 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
e0740f77
JK
1642 printf_filtered (_(" in overlay section %s"),
1643 section->the_bfd_section->name);
1644 }
c906108c
SS
1645 }
1646 }
1647 }
1648 break;
1649
c906108c 1650 case LOC_OPTIMIZED_OUT:
a3f17187 1651 printf_filtered (_("optimized out"));
c906108c 1652 break;
c5aa993b 1653
c906108c 1654 default:
a3f17187 1655 printf_filtered (_("of unknown (botched) type"));
c906108c
SS
1656 break;
1657 }
1658 printf_filtered (".\n");
1659}
1660\f
675dcf4f
MK
1661
1662static void
fba45db2 1663x_command (char *exp, int from_tty)
c906108c 1664{
c906108c
SS
1665 struct format_data fmt;
1666 struct cleanup *old_chain;
1667 struct value *val;
1668
a6bac58e 1669 fmt.format = last_format ? last_format : 'x';
c906108c
SS
1670 fmt.size = last_size;
1671 fmt.count = 1;
a6bac58e 1672 fmt.raw = 0;
c906108c
SS
1673
1674 if (exp && *exp == '/')
1675 {
6f937416
PA
1676 const char *tmp = exp + 1;
1677
1678 fmt = decode_format (&tmp, last_format, last_size);
1679 exp = (char *) tmp;
c906108c
SS
1680 }
1681
1682 /* If we have an expression, evaluate it and use it as the address. */
1683
1684 if (exp != 0 && *exp != 0)
1685 {
4d01a485 1686 expression_up expr = parse_expression (exp);
675dcf4f
MK
1687 /* Cause expression not to be there any more if this command is
1688 repeated with Newline. But don't clobber a user-defined
1689 command's definition. */
c906108c
SS
1690 if (from_tty)
1691 *exp = 0;
4d01a485 1692 val = evaluate_expression (expr.get ());
df407dfe 1693 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
e1c34c5d 1694 val = coerce_ref (val);
c906108c 1695 /* In rvalue contexts, such as this, functions are coerced into
c5aa993b 1696 pointers to functions. This makes "x/i main" work. */
c0d8fd9a 1697 if (/* last_format == 'i' && */
df407dfe 1698 TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
c5aa993b 1699 && VALUE_LVAL (val) == lval_memory)
42ae5230 1700 next_address = value_address (val);
c906108c 1701 else
1aa20aa8 1702 next_address = value_as_address (val);
5d3729b5
UW
1703
1704 next_gdbarch = expr->gdbarch;
c906108c
SS
1705 }
1706
5d3729b5
UW
1707 if (!next_gdbarch)
1708 error_no_arg (_("starting display address"));
1709
1710 do_examine (fmt, next_gdbarch, next_address);
c906108c 1711
675dcf4f 1712 /* If the examine succeeds, we remember its size and format for next
9a22f0d0
PM
1713 time. Set last_size to 'b' for strings. */
1714 if (fmt.format == 's')
1715 last_size = 'b';
1716 else
1717 last_size = fmt.size;
c906108c
SS
1718 last_format = fmt.format;
1719
0df8b418 1720 /* Set a couple of internal variables if appropriate. */
c906108c
SS
1721 if (last_examine_value)
1722 {
1723 /* Make last address examined available to the user as $_. Use
c5aa993b 1724 the correct pointer type. */
4478b372 1725 struct type *pointer_type
df407dfe 1726 = lookup_pointer_type (value_type (last_examine_value));
c906108c 1727 set_internalvar (lookup_internalvar ("_"),
4478b372
JB
1728 value_from_pointer (pointer_type,
1729 last_examine_address));
c5aa993b 1730
675dcf4f
MK
1731 /* Make contents of last address examined available to the user
1732 as $__. If the last value has not been fetched from memory
1733 then don't fetch it now; instead mark it by voiding the $__
1734 variable. */
d69fe07e 1735 if (value_lazy (last_examine_value))
4fa62494 1736 clear_internalvar (lookup_internalvar ("__"));
c906108c
SS
1737 else
1738 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1739 }
1740}
c906108c 1741\f
c5aa993b 1742
c906108c
SS
1743/* Add an expression to the auto-display chain.
1744 Specify the expression. */
1745
1746static void
6f937416 1747display_command (char *arg, int from_tty)
c906108c
SS
1748{
1749 struct format_data fmt;
fe978cb0 1750 struct display *newobj;
6f937416 1751 const char *exp = arg;
c906108c 1752
7bd0be3a 1753 if (exp == 0)
c906108c 1754 {
7bd0be3a
AB
1755 do_displays ();
1756 return;
1757 }
c906108c 1758
7bd0be3a
AB
1759 if (*exp == '/')
1760 {
1761 exp++;
1762 fmt = decode_format (&exp, 0, 0);
1763 if (fmt.size && fmt.format == 0)
1764 fmt.format = 'x';
1765 if (fmt.format == 'i' || fmt.format == 's')
1766 fmt.size = 'b';
1767 }
1768 else
1769 {
1770 fmt.format = 0;
1771 fmt.size = 0;
1772 fmt.count = 0;
1773 fmt.raw = 0;
1774 }
c906108c 1775
7bd0be3a 1776 innermost_block = NULL;
4d01a485 1777 expression_up expr = parse_expression (exp);
c906108c 1778
4d01a485 1779 newobj = new display ();
c906108c 1780
7bd0be3a 1781 newobj->exp_string = xstrdup (exp);
b22e99fd 1782 newobj->exp = std::move (expr);
7bd0be3a
AB
1783 newobj->block = innermost_block;
1784 newobj->pspace = current_program_space;
7bd0be3a
AB
1785 newobj->number = ++display_number;
1786 newobj->format = fmt;
1787 newobj->enabled_p = 1;
62147a22
PA
1788 newobj->next = NULL;
1789
1790 if (display_chain == NULL)
1791 display_chain = newobj;
1792 else
1793 {
1794 struct display *last;
1795
1796 for (last = display_chain; last->next != NULL; last = last->next)
1797 ;
1798 last->next = newobj;
1799 }
c906108c 1800
7bd0be3a
AB
1801 if (from_tty)
1802 do_one_display (newobj);
c906108c 1803
7bd0be3a 1804 dont_repeat ();
c906108c
SS
1805}
1806
1807static void
fba45db2 1808free_display (struct display *d)
c906108c 1809{
fa8a61dc 1810 xfree (d->exp_string);
4d01a485 1811 delete d;
c906108c
SS
1812}
1813
675dcf4f
MK
1814/* Clear out the display_chain. Done when new symtabs are loaded,
1815 since this invalidates the types stored in many expressions. */
c906108c
SS
1816
1817void
fba45db2 1818clear_displays (void)
c906108c 1819{
52f0bd74 1820 struct display *d;
c906108c
SS
1821
1822 while ((d = display_chain) != NULL)
1823 {
c906108c 1824 display_chain = d->next;
fa8a61dc 1825 free_display (d);
c906108c
SS
1826 }
1827}
1828
3c3fe74c 1829/* Delete the auto-display DISPLAY. */
c906108c
SS
1830
1831static void
3c3fe74c 1832delete_display (struct display *display)
c906108c 1833{
3c3fe74c 1834 struct display *d;
c906108c 1835
3c3fe74c 1836 gdb_assert (display != NULL);
c906108c 1837
3c3fe74c
PA
1838 if (display_chain == display)
1839 display_chain = display->next;
1840
1841 ALL_DISPLAYS (d)
1842 if (d->next == display)
c906108c 1843 {
3c3fe74c
PA
1844 d->next = display->next;
1845 break;
c906108c 1846 }
3c3fe74c
PA
1847
1848 free_display (display);
c906108c
SS
1849}
1850
c9174737
PA
1851/* Call FUNCTION on each of the displays whose numbers are given in
1852 ARGS. DATA is passed unmodified to FUNCTION. */
c906108c
SS
1853
1854static void
c9174737
PA
1855map_display_numbers (char *args,
1856 void (*function) (struct display *,
1857 void *),
1858 void *data)
c906108c 1859{
c9174737 1860 int num;
c906108c 1861
c9174737
PA
1862 if (args == NULL)
1863 error_no_arg (_("one or more display numbers"));
c906108c 1864
bfd28288 1865 number_or_range_parser parser (args);
c9174737 1866
bfd28288 1867 while (!parser.finished ())
c906108c 1868 {
bfd28288 1869 const char *p = parser.cur_tok ();
c906108c 1870
bfd28288 1871 num = parser.get_number ();
3c3fe74c
PA
1872 if (num == 0)
1873 warning (_("bad display number at or near '%s'"), p);
1874 else
1875 {
c9174737 1876 struct display *d, *tmp;
c906108c 1877
c9174737 1878 ALL_DISPLAYS_SAFE (d, tmp)
3c3fe74c
PA
1879 if (d->number == num)
1880 break;
1881 if (d == NULL)
1882 printf_unfiltered (_("No display number %d.\n"), num);
1883 else
c9174737 1884 function (d, data);
3c3fe74c 1885 }
c906108c 1886 }
c9174737
PA
1887}
1888
1889/* Callback for map_display_numbers, that deletes a display. */
1890
1891static void
1892do_delete_display (struct display *d, void *data)
1893{
1894 delete_display (d);
1895}
1896
1897/* "undisplay" command. */
1898
1899static void
1900undisplay_command (char *args, int from_tty)
1901{
c9174737
PA
1902 if (args == NULL)
1903 {
1904 if (query (_("Delete all auto-display expressions? ")))
1905 clear_displays ();
1906 dont_repeat ();
1907 return;
1908 }
1909
1910 map_display_numbers (args, do_delete_display, NULL);
c906108c
SS
1911 dont_repeat ();
1912}
1913
1914/* Display a single auto-display.
1915 Do nothing if the display cannot be printed in the current context,
0df8b418 1916 or if the display is disabled. */
c906108c
SS
1917
1918static void
fba45db2 1919do_one_display (struct display *d)
c906108c
SS
1920{
1921 int within_current_scope;
1922
b5de0fa7 1923 if (d->enabled_p == 0)
c906108c
SS
1924 return;
1925
704e9165
UW
1926 /* The expression carries the architecture that was used at parse time.
1927 This is a problem if the expression depends on architecture features
1928 (e.g. register numbers), and the current architecture is now different.
1929 For example, a display statement like "display/i $pc" is expected to
1930 display the PC register of the current architecture, not the arch at
1931 the time the display command was given. Therefore, we re-parse the
1932 expression if the current architecture has changed. */
1933 if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
1934 {
4d01a485 1935 d->exp.reset ();
704e9165
UW
1936 d->block = NULL;
1937 }
1938
a3247a22
PP
1939 if (d->exp == NULL)
1940 {
ad3bbd48 1941
492d29ea 1942 TRY
a3247a22
PP
1943 {
1944 innermost_block = NULL;
1945 d->exp = parse_expression (d->exp_string);
1946 d->block = innermost_block;
1947 }
492d29ea 1948 CATCH (ex, RETURN_MASK_ALL)
a3247a22
PP
1949 {
1950 /* Can't re-parse the expression. Disable this display item. */
1951 d->enabled_p = 0;
1952 warning (_("Unable to display \"%s\": %s"),
1953 d->exp_string, ex.message);
1954 return;
1955 }
492d29ea 1956 END_CATCH
a3247a22
PP
1957 }
1958
c906108c 1959 if (d->block)
6c95b8df
PA
1960 {
1961 if (d->pspace == current_program_space)
1962 within_current_scope = contained_in (get_selected_block (0), d->block);
1963 else
1964 within_current_scope = 0;
1965 }
c906108c
SS
1966 else
1967 within_current_scope = 1;
1968 if (!within_current_scope)
1969 return;
1970
b7b633e9
TT
1971 scoped_restore save_display_number
1972 = make_scoped_restore (&current_display_number, d->number);
c906108c
SS
1973
1974 annotate_display_begin ();
1975 printf_filtered ("%d", d->number);
1976 annotate_display_number_end ();
1977 printf_filtered (": ");
1978 if (d->format.size)
1979 {
c906108c
SS
1980
1981 annotate_display_format ();
1982
1983 printf_filtered ("x/");
1984 if (d->format.count != 1)
1985 printf_filtered ("%d", d->format.count);
1986 printf_filtered ("%c", d->format.format);
1987 if (d->format.format != 'i' && d->format.format != 's')
1988 printf_filtered ("%c", d->format.size);
1989 printf_filtered (" ");
1990
1991 annotate_display_expression ();
1992
fa8a61dc 1993 puts_filtered (d->exp_string);
c906108c
SS
1994 annotate_display_expression_end ();
1995
6a2eb474 1996 if (d->format.count != 1 || d->format.format == 'i')
c906108c
SS
1997 printf_filtered ("\n");
1998 else
1999 printf_filtered (" ");
c5aa993b 2000
c906108c
SS
2001 annotate_display_value ();
2002
492d29ea 2003 TRY
9d8fa392
PA
2004 {
2005 struct value *val;
2006 CORE_ADDR addr;
2007
4d01a485 2008 val = evaluate_expression (d->exp.get ());
9d8fa392
PA
2009 addr = value_as_address (val);
2010 if (d->format.format == 'i')
2011 addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
2012 do_examine (d->format, d->exp->gdbarch, addr);
2013 }
492d29ea
PA
2014 CATCH (ex, RETURN_MASK_ERROR)
2015 {
2016 fprintf_filtered (gdb_stdout, _("<error: %s>\n"), ex.message);
2017 }
2018 END_CATCH
c906108c
SS
2019 }
2020 else
2021 {
79a45b7d
TT
2022 struct value_print_options opts;
2023
c906108c
SS
2024 annotate_display_format ();
2025
2026 if (d->format.format)
2027 printf_filtered ("/%c ", d->format.format);
2028
2029 annotate_display_expression ();
2030
fa8a61dc 2031 puts_filtered (d->exp_string);
c906108c
SS
2032 annotate_display_expression_end ();
2033
2034 printf_filtered (" = ");
2035
2036 annotate_display_expression ();
2037
79a45b7d 2038 get_formatted_print_options (&opts, d->format.format);
a6bac58e 2039 opts.raw = d->format.raw;
9d8fa392 2040
492d29ea 2041 TRY
9d8fa392
PA
2042 {
2043 struct value *val;
2044
4d01a485 2045 val = evaluate_expression (d->exp.get ());
9d8fa392
PA
2046 print_formatted (val, d->format.size, &opts, gdb_stdout);
2047 }
492d29ea
PA
2048 CATCH (ex, RETURN_MASK_ERROR)
2049 {
2050 fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
2051 }
2052 END_CATCH
2053
c906108c
SS
2054 printf_filtered ("\n");
2055 }
2056
2057 annotate_display_end ();
2058
2059 gdb_flush (gdb_stdout);
c906108c
SS
2060}
2061
2062/* Display all of the values on the auto-display chain which can be
2063 evaluated in the current scope. */
2064
2065void
fba45db2 2066do_displays (void)
c906108c 2067{
52f0bd74 2068 struct display *d;
c906108c
SS
2069
2070 for (d = display_chain; d; d = d->next)
2071 do_one_display (d);
2072}
2073
2074/* Delete the auto-display which we were in the process of displaying.
2075 This is done when there is an error or a signal. */
2076
2077void
fba45db2 2078disable_display (int num)
c906108c 2079{
52f0bd74 2080 struct display *d;
c906108c
SS
2081
2082 for (d = display_chain; d; d = d->next)
2083 if (d->number == num)
2084 {
b5de0fa7 2085 d->enabled_p = 0;
c906108c
SS
2086 return;
2087 }
a3f17187 2088 printf_unfiltered (_("No display number %d.\n"), num);
c906108c 2089}
c5aa993b 2090
c906108c 2091void
fba45db2 2092disable_current_display (void)
c906108c
SS
2093{
2094 if (current_display_number >= 0)
2095 {
2096 disable_display (current_display_number);
3e43a32a
MS
2097 fprintf_unfiltered (gdb_stderr,
2098 _("Disabling display %d to "
2099 "avoid infinite recursion.\n"),
c5aa993b 2100 current_display_number);
c906108c
SS
2101 }
2102 current_display_number = -1;
2103}
2104
2105static void
fba45db2 2106display_info (char *ignore, int from_tty)
c906108c 2107{
52f0bd74 2108 struct display *d;
c906108c
SS
2109
2110 if (!display_chain)
a3f17187 2111 printf_unfiltered (_("There are no auto-display expressions now.\n"));
c906108c 2112 else
a3f17187
AC
2113 printf_filtered (_("Auto-display expressions now in effect:\n\
2114Num Enb Expression\n"));
c906108c
SS
2115
2116 for (d = display_chain; d; d = d->next)
2117 {
b5de0fa7 2118 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
c906108c
SS
2119 if (d->format.size)
2120 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
c5aa993b 2121 d->format.format);
c906108c
SS
2122 else if (d->format.format)
2123 printf_filtered ("/%c ", d->format.format);
fa8a61dc 2124 puts_filtered (d->exp_string);
ae767bfb 2125 if (d->block && !contained_in (get_selected_block (0), d->block))
a3f17187 2126 printf_filtered (_(" (cannot be evaluated in the current context)"));
c906108c
SS
2127 printf_filtered ("\n");
2128 gdb_flush (gdb_stdout);
2129 }
2130}
2131
c9174737
PA
2132/* Callback fo map_display_numbers, that enables or disables the
2133 passed in display D. */
2134
c906108c 2135static void
c9174737 2136do_enable_disable_display (struct display *d, void *data)
c906108c 2137{
c9174737
PA
2138 d->enabled_p = *(int *) data;
2139}
c906108c 2140
c9174737
PA
2141/* Implamentation of both the "disable display" and "enable display"
2142 commands. ENABLE decides what to do. */
2143
2144static void
2145enable_disable_display_command (char *args, int from_tty, int enable)
2146{
2147 if (args == NULL)
c906108c 2148 {
c9174737 2149 struct display *d;
c5aa993b 2150
c9174737
PA
2151 ALL_DISPLAYS (d)
2152 d->enabled_p = enable;
2153 return;
2154 }
c5aa993b 2155
c9174737 2156 map_display_numbers (args, do_enable_disable_display, &enable);
c906108c
SS
2157}
2158
c9174737
PA
2159/* The "enable display" command. */
2160
c906108c 2161static void
c9174737 2162enable_display_command (char *args, int from_tty)
c906108c 2163{
c9174737
PA
2164 enable_disable_display_command (args, from_tty, 1);
2165}
c5aa993b 2166
c9174737 2167/* The "disable display" command. */
c906108c 2168
c9174737
PA
2169static void
2170disable_display_command (char *args, int from_tty)
2171{
2172 enable_disable_display_command (args, from_tty, 0);
c906108c 2173}
a3247a22 2174
a3247a22
PP
2175/* display_chain items point to blocks and expressions. Some expressions in
2176 turn may point to symbols.
2177 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
2178 obstack_free'd when a shared library is unloaded.
2179 Clear pointers that are about to become dangling.
2180 Both .exp and .block fields will be restored next time we need to display
2181 an item by re-parsing .exp_string field in the new execution context. */
2182
2183static void
63644780 2184clear_dangling_display_expressions (struct objfile *objfile)
a3247a22
PP
2185{
2186 struct display *d;
63644780 2187 struct program_space *pspace;
a3247a22 2188
c0201579
JK
2189 /* With no symbol file we cannot have a block or expression from it. */
2190 if (objfile == NULL)
2191 return;
63644780 2192 pspace = objfile->pspace;
c0201579 2193 if (objfile->separate_debug_objfile_backlink)
63644780
NB
2194 {
2195 objfile = objfile->separate_debug_objfile_backlink;
2196 gdb_assert (objfile->pspace == pspace);
2197 }
c0201579
JK
2198
2199 for (d = display_chain; d != NULL; d = d->next)
a3247a22 2200 {
63644780 2201 if (d->pspace != pspace)
c0201579
JK
2202 continue;
2203
2204 if (lookup_objfile_from_block (d->block) == objfile
4d01a485 2205 || (d->exp != NULL && exp_uses_objfile (d->exp.get (), objfile)))
c0201579 2206 {
4d01a485 2207 d->exp.reset ();
c0201579
JK
2208 d->block = NULL;
2209 }
a3247a22
PP
2210 }
2211}
c906108c 2212\f
c5aa993b 2213
675dcf4f 2214/* Print the value in stack frame FRAME of a variable specified by a
aad95b57
TT
2215 struct symbol. NAME is the name to print; if NULL then VAR's print
2216 name will be used. STREAM is the ui_file on which to print the
2217 value. INDENT specifies the number of indent levels to print
8f043999
JK
2218 before printing the variable name.
2219
2220 This function invalidates FRAME. */
c906108c
SS
2221
2222void
aad95b57
TT
2223print_variable_and_value (const char *name, struct symbol *var,
2224 struct frame_info *frame,
2225 struct ui_file *stream, int indent)
c906108c 2226{
c906108c 2227
aad95b57
TT
2228 if (!name)
2229 name = SYMBOL_PRINT_NAME (var);
2230
2231 fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
492d29ea 2232 TRY
0f6a939d
PM
2233 {
2234 struct value *val;
2235 struct value_print_options opts;
aad95b57 2236
63e43d3a
PMR
2237 /* READ_VAR_VALUE needs a block in order to deal with non-local
2238 references (i.e. to handle nested functions). In this context, we
2239 print variables that are local to this frame, so we can avoid passing
2240 a block to it. */
2241 val = read_var_value (var, NULL, frame);
0f6a939d 2242 get_user_print_options (&opts);
3343315b 2243 opts.deref_ref = 1;
0f6a939d 2244 common_val_print (val, stream, indent, &opts, current_language);
8f043999
JK
2245
2246 /* common_val_print invalidates FRAME when a pretty printer calls inferior
2247 function. */
2248 frame = NULL;
0f6a939d 2249 }
492d29ea
PA
2250 CATCH (except, RETURN_MASK_ERROR)
2251 {
2252 fprintf_filtered(stream, "<error reading variable %s (%s)>", name,
2253 except.message);
2254 }
2255 END_CATCH
2256
aad95b57 2257 fprintf_filtered (stream, "\n");
c906108c
SS
2258}
2259
c2792f5a
DE
2260/* Subroutine of ui_printf to simplify it.
2261 Print VALUE to STREAM using FORMAT.
e12f57ab 2262 VALUE is a C-style string on the target. */
c2792f5a
DE
2263
2264static void
2265printf_c_string (struct ui_file *stream, const char *format,
2266 struct value *value)
2267{
2268 gdb_byte *str;
2269 CORE_ADDR tem;
2270 int j;
2271
2272 tem = value_as_address (value);
2273
2274 /* This is a %s argument. Find the length of the string. */
2275 for (j = 0;; j++)
2276 {
2277 gdb_byte c;
2278
2279 QUIT;
2280 read_memory (tem + j, &c, 1);
2281 if (c == 0)
2282 break;
2283 }
2284
2285 /* Copy the string contents into a string inside GDB. */
2286 str = (gdb_byte *) alloca (j + 1);
2287 if (j != 0)
2288 read_memory (tem, str, j);
2289 str[j] = 0;
2290
2291 fprintf_filtered (stream, format, (char *) str);
2292}
2293
2294/* Subroutine of ui_printf to simplify it.
2295 Print VALUE to STREAM using FORMAT.
e12f57ab 2296 VALUE is a wide C-style string on the target. */
c2792f5a
DE
2297
2298static void
2299printf_wide_c_string (struct ui_file *stream, const char *format,
2300 struct value *value)
2301{
2302 gdb_byte *str;
2303 CORE_ADDR tem;
2304 int j;
2305 struct gdbarch *gdbarch = get_type_arch (value_type (value));
2306 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2307 struct type *wctype = lookup_typename (current_language, gdbarch,
2308 "wchar_t", NULL, 0);
2309 int wcwidth = TYPE_LENGTH (wctype);
224c3ddb 2310 gdb_byte *buf = (gdb_byte *) alloca (wcwidth);
c2792f5a
DE
2311 struct obstack output;
2312 struct cleanup *inner_cleanup;
2313
2314 tem = value_as_address (value);
2315
2316 /* This is a %s argument. Find the length of the string. */
2317 for (j = 0;; j += wcwidth)
2318 {
2319 QUIT;
2320 read_memory (tem + j, buf, wcwidth);
2321 if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
2322 break;
2323 }
2324
2325 /* Copy the string contents into a string inside GDB. */
2326 str = (gdb_byte *) alloca (j + wcwidth);
2327 if (j != 0)
2328 read_memory (tem, str, j);
2329 memset (&str[j], 0, wcwidth);
2330
2331 obstack_init (&output);
2332 inner_cleanup = make_cleanup_obstack_free (&output);
2333
2334 convert_between_encodings (target_wide_charset (gdbarch),
2335 host_charset (),
2336 str, j, wcwidth,
2337 &output, translit_char);
2338 obstack_grow_str0 (&output, "");
2339
2340 fprintf_filtered (stream, format, obstack_base (&output));
2341 do_cleanups (inner_cleanup);
2342}
2343
2344/* Subroutine of ui_printf to simplify it.
2345 Print VALUE, a decimal floating point value, to STREAM using FORMAT. */
2346
2347static void
2348printf_decfloat (struct ui_file *stream, const char *format,
2349 struct value *value)
2350{
2351 const gdb_byte *param_ptr = value_contents (value);
2352
2353#if defined (PRINTF_HAS_DECFLOAT)
2354 /* If we have native support for Decimal floating
2355 printing, handle it here. */
2356 fprintf_filtered (stream, format, param_ptr);
2357#else
2358 /* As a workaround until vasprintf has native support for DFP
2359 we convert the DFP values to string and print them using
2360 the %s format specifier. */
2361 const char *p;
2362
2363 /* Parameter data. */
2364 struct type *param_type = value_type (value);
2365 struct gdbarch *gdbarch = get_type_arch (param_type);
2366 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2367
2368 /* DFP output data. */
2369 struct value *dfp_value = NULL;
2370 gdb_byte *dfp_ptr;
2371 int dfp_len = 16;
2372 gdb_byte dec[16];
2373 struct type *dfp_type = NULL;
2374 char decstr[MAX_DECIMAL_STRING];
2375
2376 /* Points to the end of the string so that we can go back
2377 and check for DFP length modifiers. */
2378 p = format + strlen (format);
2379
2380 /* Look for the float/double format specifier. */
2381 while (*p != 'f' && *p != 'e' && *p != 'E'
2382 && *p != 'g' && *p != 'G')
2383 p--;
2384
2385 /* Search for the '%' char and extract the size and type of
2386 the output decimal value based on its modifiers
2387 (%Hf, %Df, %DDf). */
2388 while (*--p != '%')
2389 {
2390 if (*p == 'H')
2391 {
2392 dfp_len = 4;
2393 dfp_type = builtin_type (gdbarch)->builtin_decfloat;
2394 }
2395 else if (*p == 'D' && *(p - 1) == 'D')
2396 {
2397 dfp_len = 16;
2398 dfp_type = builtin_type (gdbarch)->builtin_declong;
2399 p--;
2400 }
2401 else
2402 {
2403 dfp_len = 8;
2404 dfp_type = builtin_type (gdbarch)->builtin_decdouble;
2405 }
2406 }
2407
2408 /* Conversion between different DFP types. */
2409 if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
2410 decimal_convert (param_ptr, TYPE_LENGTH (param_type),
2411 byte_order, dec, dfp_len, byte_order);
2412 else
2413 /* If this is a non-trivial conversion, just output 0.
2414 A correct converted value can be displayed by explicitly
2415 casting to a DFP type. */
2416 decimal_from_string (dec, dfp_len, byte_order, "0");
2417
2418 dfp_value = value_from_decfloat (dfp_type, dec);
2419
2420 dfp_ptr = (gdb_byte *) value_contents (dfp_value);
2421
2422 decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr);
2423
2424 /* Print the DFP value. */
2425 fprintf_filtered (stream, "%s", decstr);
2426#endif
2427}
2428
2429/* Subroutine of ui_printf to simplify it.
2430 Print VALUE, a target pointer, to STREAM using FORMAT. */
2431
2432static void
2433printf_pointer (struct ui_file *stream, const char *format,
2434 struct value *value)
2435{
2436 /* We avoid the host's %p because pointers are too
2437 likely to be the wrong size. The only interesting
2438 modifier for %p is a width; extract that, and then
2439 handle %p as glibc would: %#x or a literal "(nil)". */
2440
2441 const char *p;
2442 char *fmt, *fmt_p;
2443#ifdef PRINTF_HAS_LONG_LONG
2444 long long val = value_as_long (value);
2445#else
2446 long val = value_as_long (value);
2447#endif
2448
224c3ddb 2449 fmt = (char *) alloca (strlen (format) + 5);
c2792f5a
DE
2450
2451 /* Copy up to the leading %. */
2452 p = format;
2453 fmt_p = fmt;
2454 while (*p)
2455 {
2456 int is_percent = (*p == '%');
2457
2458 *fmt_p++ = *p++;
2459 if (is_percent)
2460 {
2461 if (*p == '%')
2462 *fmt_p++ = *p++;
2463 else
2464 break;
2465 }
2466 }
2467
2468 if (val != 0)
2469 *fmt_p++ = '#';
2470
2471 /* Copy any width. */
2472 while (*p >= '0' && *p < '9')
2473 *fmt_p++ = *p++;
2474
2475 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2476 if (val != 0)
2477 {
2478#ifdef PRINTF_HAS_LONG_LONG
2479 *fmt_p++ = 'l';
2480#endif
2481 *fmt_p++ = 'l';
2482 *fmt_p++ = 'x';
2483 *fmt_p++ = '\0';
2484 fprintf_filtered (stream, fmt, val);
2485 }
2486 else
2487 {
2488 *fmt_p++ = 's';
2489 *fmt_p++ = '\0';
2490 fprintf_filtered (stream, fmt, "(nil)");
2491 }
2492}
2493
a04b0428
JB
2494/* printf "printf format string" ARG to STREAM. */
2495
2496static void
bbc13ae3 2497ui_printf (const char *arg, struct ui_file *stream)
c906108c 2498{
d3ce09f5 2499 struct format_piece *fpieces;
bbc13ae3 2500 const char *s = arg;
3d6d86c6 2501 struct value **val_args;
c906108c
SS
2502 int allocated_args = 20;
2503 struct cleanup *old_cleanups;
2504
8d749320 2505 val_args = XNEWVEC (struct value *, allocated_args);
c13c43fd 2506 old_cleanups = make_cleanup (free_current_contents, &val_args);
c906108c
SS
2507
2508 if (s == 0)
e2e0b3e5 2509 error_no_arg (_("format-control string and values to print"));
c906108c 2510
bbc13ae3 2511 s = skip_spaces_const (s);
c906108c 2512
675dcf4f 2513 /* A format string should follow, enveloped in double quotes. */
c906108c 2514 if (*s++ != '"')
8a3fe4f8 2515 error (_("Bad format string, missing '\"'."));
c906108c 2516
d3ce09f5 2517 fpieces = parse_format_string (&s);
c906108c 2518
d3ce09f5 2519 make_cleanup (free_format_pieces_cleanup, &fpieces);
c906108c 2520
d3ce09f5
SS
2521 if (*s++ != '"')
2522 error (_("Bad format string, non-terminated '\"'."));
2523
bbc13ae3 2524 s = skip_spaces_const (s);
c906108c
SS
2525
2526 if (*s != ',' && *s != 0)
8a3fe4f8 2527 error (_("Invalid argument syntax"));
c906108c 2528
c5aa993b
JM
2529 if (*s == ',')
2530 s++;
bbc13ae3 2531 s = skip_spaces_const (s);
c906108c 2532
c906108c 2533 {
d3ce09f5 2534 int nargs = 0;
c906108c 2535 int nargs_wanted;
d3ce09f5
SS
2536 int i, fr;
2537 char *current_substring;
c906108c 2538
c906108c 2539 nargs_wanted = 0;
d3ce09f5
SS
2540 for (fr = 0; fpieces[fr].string != NULL; fr++)
2541 if (fpieces[fr].argclass != literal_piece)
2542 ++nargs_wanted;
c906108c
SS
2543
2544 /* Now, parse all arguments and evaluate them.
2545 Store the VALUEs in VAL_ARGS. */
2546
2547 while (*s != '\0')
2548 {
bbc13ae3 2549 const char *s1;
ad3bbd48 2550
c906108c 2551 if (nargs == allocated_args)
f976f6d4
AC
2552 val_args = (struct value **) xrealloc ((char *) val_args,
2553 (allocated_args *= 2)
2554 * sizeof (struct value *));
a04b0428
JB
2555 s1 = s;
2556 val_args[nargs] = parse_to_comma_and_eval (&s1);
c5aa993b 2557
c906108c
SS
2558 nargs++;
2559 s = s1;
2560 if (*s == ',')
2561 s++;
2562 }
c5aa993b 2563
c906108c 2564 if (nargs != nargs_wanted)
8a3fe4f8 2565 error (_("Wrong number of arguments for specified format-string"));
c906108c
SS
2566
2567 /* Now actually print them. */
d3ce09f5
SS
2568 i = 0;
2569 for (fr = 0; fpieces[fr].string != NULL; fr++)
c906108c 2570 {
d3ce09f5
SS
2571 current_substring = fpieces[fr].string;
2572 switch (fpieces[fr].argclass)
c906108c
SS
2573 {
2574 case string_arg:
c2792f5a 2575 printf_c_string (stream, current_substring, val_args[i]);
c906108c 2576 break;
6c7a06a3 2577 case wide_string_arg:
c2792f5a 2578 printf_wide_c_string (stream, current_substring, val_args[i]);
6c7a06a3
TT
2579 break;
2580 case wide_char_arg:
2581 {
50810684
UW
2582 struct gdbarch *gdbarch
2583 = get_type_arch (value_type (val_args[i]));
2584 struct type *wctype = lookup_typename (current_language, gdbarch,
e6c014f2 2585 "wchar_t", NULL, 0);
6c7a06a3
TT
2586 struct type *valtype;
2587 struct obstack output;
2588 struct cleanup *inner_cleanup;
2589 const gdb_byte *bytes;
2590
2591 valtype = value_type (val_args[i]);
2592 if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
2593 || TYPE_CODE (valtype) != TYPE_CODE_INT)
2594 error (_("expected wchar_t argument for %%lc"));
2595
2596 bytes = value_contents (val_args[i]);
2597
2598 obstack_init (&output);
2599 inner_cleanup = make_cleanup_obstack_free (&output);
2600
f870a310 2601 convert_between_encodings (target_wide_charset (gdbarch),
6c7a06a3
TT
2602 host_charset (),
2603 bytes, TYPE_LENGTH (valtype),
2604 TYPE_LENGTH (valtype),
2605 &output, translit_char);
2606 obstack_grow_str0 (&output, "");
2607
f1421989
HZ
2608 fprintf_filtered (stream, current_substring,
2609 obstack_base (&output));
6c7a06a3
TT
2610 do_cleanups (inner_cleanup);
2611 }
2612 break;
c906108c
SS
2613 case double_arg:
2614 {
b806fb9a
UW
2615 struct type *type = value_type (val_args[i]);
2616 DOUBLEST val;
2617 int inv;
2618
2619 /* If format string wants a float, unchecked-convert the value
2620 to floating point of the same size. */
50810684 2621 type = float_type_from_length (type);
b806fb9a
UW
2622 val = unpack_double (type, value_contents (val_args[i]), &inv);
2623 if (inv)
2624 error (_("Invalid floating value found in program."));
2625
f1421989 2626 fprintf_filtered (stream, current_substring, (double) val);
c906108c
SS
2627 break;
2628 }
46e9880c
DJ
2629 case long_double_arg:
2630#ifdef HAVE_LONG_DOUBLE
2631 {
b806fb9a
UW
2632 struct type *type = value_type (val_args[i]);
2633 DOUBLEST val;
2634 int inv;
2635
2636 /* If format string wants a float, unchecked-convert the value
2637 to floating point of the same size. */
50810684 2638 type = float_type_from_length (type);
b806fb9a
UW
2639 val = unpack_double (type, value_contents (val_args[i]), &inv);
2640 if (inv)
2641 error (_("Invalid floating value found in program."));
2642
f1421989
HZ
2643 fprintf_filtered (stream, current_substring,
2644 (long double) val);
46e9880c
DJ
2645 break;
2646 }
2647#else
2648 error (_("long double not supported in printf"));
2649#endif
c906108c 2650 case long_long_arg:
74a0d9f6 2651#ifdef PRINTF_HAS_LONG_LONG
c906108c
SS
2652 {
2653 long long val = value_as_long (val_args[i]);
ad3bbd48 2654
f1421989 2655 fprintf_filtered (stream, current_substring, val);
c906108c
SS
2656 break;
2657 }
2658#else
8a3fe4f8 2659 error (_("long long not supported in printf"));
c906108c
SS
2660#endif
2661 case int_arg:
2662 {
46e9880c 2663 int val = value_as_long (val_args[i]);
ad3bbd48 2664
f1421989 2665 fprintf_filtered (stream, current_substring, val);
46e9880c
DJ
2666 break;
2667 }
2668 case long_arg:
2669 {
c906108c 2670 long val = value_as_long (val_args[i]);
ad3bbd48 2671
f1421989 2672 fprintf_filtered (stream, current_substring, val);
c906108c
SS
2673 break;
2674 }
0aea4bf3 2675 /* Handles decimal floating values. */
c2792f5a
DE
2676 case decfloat_arg:
2677 printf_decfloat (stream, current_substring, val_args[i]);
2678 break;
2025a643 2679 case ptr_arg:
c2792f5a
DE
2680 printf_pointer (stream, current_substring, val_args[i]);
2681 break;
d3ce09f5
SS
2682 case literal_piece:
2683 /* Print a portion of the format string that has no
2684 directives. Note that this will not include any
2685 ordinary %-specs, but it might include "%%". That is
2686 why we use printf_filtered and not puts_filtered here.
2687 Also, we pass a dummy argument because some platforms
2688 have modified GCC to include -Wformat-security by
2689 default, which will warn here if there is no
2690 argument. */
2691 fprintf_filtered (stream, current_substring, 0);
2692 break;
675dcf4f
MK
2693 default:
2694 internal_error (__FILE__, __LINE__,
2025a643 2695 _("failed internal consistency check"));
c906108c 2696 }
d3ce09f5
SS
2697 /* Maybe advance to the next argument. */
2698 if (fpieces[fr].argclass != literal_piece)
2699 ++i;
c906108c 2700 }
c906108c
SS
2701 }
2702 do_cleanups (old_cleanups);
2703}
c906108c 2704
f1421989
HZ
2705/* Implement the "printf" command. */
2706
a04b0428 2707static void
f1421989
HZ
2708printf_command (char *arg, int from_tty)
2709{
a04b0428 2710 ui_printf (arg, gdb_stdout);
50b34a18 2711 gdb_flush (gdb_stdout);
f1421989
HZ
2712}
2713
2714/* Implement the "eval" command. */
2715
2716static void
2717eval_command (char *arg, int from_tty)
2718{
2719 struct ui_file *ui_out = mem_fileopen ();
2720 struct cleanup *cleanups = make_cleanup_ui_file_delete (ui_out);
f1421989 2721
a04b0428 2722 ui_printf (arg, ui_out);
f1421989 2723
02030646 2724 std::string expanded = ui_file_as_string (ui_out);
f1421989 2725
02030646 2726 execute_command (&expanded[0], from_tty);
f1421989
HZ
2727
2728 do_cleanups (cleanups);
2729}
2730
c906108c 2731void
fba45db2 2732_initialize_printcmd (void)
c906108c 2733{
c94fdfd0
EZ
2734 struct cmd_list_element *c;
2735
c906108c
SS
2736 current_display_number = -1;
2737
63644780 2738 observer_attach_free_objfile (clear_dangling_display_expressions);
a3247a22 2739
c906108c 2740 add_info ("address", address_info,
1bedd215 2741 _("Describe where symbol SYM is stored."));
c906108c 2742
1bedd215
AC
2743 add_info ("symbol", sym_info, _("\
2744Describe what symbol is at location ADDR.\n\
2745Only for symbols with fixed locations (global or static scope)."));
c906108c 2746
1bedd215
AC
2747 add_com ("x", class_vars, x_command, _("\
2748Examine memory: x/FMT ADDRESS.\n\
c906108c
SS
2749ADDRESS is an expression for the memory address to examine.\n\
2750FMT is a repeat count followed by a format letter and a size letter.\n\
2751Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
6fbe845e
AB
2752 t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
2753 and z(hex, zero padded on the left).\n\
1bedd215 2754Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
c906108c 2755The specified number of objects of the specified size are printed\n\
bb556f1f
TK
2756according to the format. If a negative number is specified, memory is\n\
2757examined backward from the address.\n\n\
c906108c
SS
2758Defaults for format and size letters are those previously used.\n\
2759Default count is 1. Default address is following last thing printed\n\
1bedd215 2760with this command or \"print\"."));
c906108c 2761
c906108c
SS
2762#if 0
2763 add_com ("whereis", class_vars, whereis_command,
1bedd215 2764 _("Print line number and file of definition of variable."));
c906108c 2765#endif
c5aa993b 2766
1bedd215
AC
2767 add_info ("display", display_info, _("\
2768Expressions to display when program stops, with code numbers."));
c906108c 2769
1a966eab
AC
2770 add_cmd ("undisplay", class_vars, undisplay_command, _("\
2771Cancel some expressions to be displayed when program stops.\n\
c906108c
SS
2772Arguments are the code numbers of the expressions to stop displaying.\n\
2773No argument means cancel all automatic-display expressions.\n\
2774\"delete display\" has the same effect as this command.\n\
1a966eab 2775Do \"info display\" to see current list of code numbers."),
c5aa993b 2776 &cmdlist);
c906108c 2777
1bedd215
AC
2778 add_com ("display", class_vars, display_command, _("\
2779Print value of expression EXP each time the program stops.\n\
c906108c
SS
2780/FMT may be used before EXP as in the \"print\" command.\n\
2781/FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2782as in the \"x\" command, and then EXP is used to get the address to examine\n\
2783and examining is done as in the \"x\" command.\n\n\
2784With no argument, display all currently requested auto-display expressions.\n\
1bedd215 2785Use \"undisplay\" to cancel display requests previously made."));
c906108c 2786
c9174737 2787 add_cmd ("display", class_vars, enable_display_command, _("\
1a966eab 2788Enable some expressions to be displayed when program stops.\n\
c906108c
SS
2789Arguments are the code numbers of the expressions to resume displaying.\n\
2790No argument means enable all automatic-display expressions.\n\
1a966eab 2791Do \"info display\" to see current list of code numbers."), &enablelist);
c906108c 2792
1a966eab
AC
2793 add_cmd ("display", class_vars, disable_display_command, _("\
2794Disable some expressions to be displayed when program stops.\n\
c906108c
SS
2795Arguments are the code numbers of the expressions to stop displaying.\n\
2796No argument means disable all automatic-display expressions.\n\
1a966eab 2797Do \"info display\" to see current list of code numbers."), &disablelist);
c906108c 2798
1a966eab
AC
2799 add_cmd ("display", class_vars, undisplay_command, _("\
2800Cancel some expressions to be displayed when program stops.\n\
c906108c
SS
2801Arguments are the code numbers of the expressions to stop displaying.\n\
2802No argument means cancel all automatic-display expressions.\n\
1a966eab 2803Do \"info display\" to see current list of code numbers."), &deletelist);
c906108c 2804
1bedd215
AC
2805 add_com ("printf", class_vars, printf_command, _("\
2806printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2807This is useful for formatted output in user-defined commands."));
c906108c 2808
1bedd215
AC
2809 add_com ("output", class_vars, output_command, _("\
2810Like \"print\" but don't put in value history and don't print newline.\n\
2811This is useful in user-defined commands."));
c906108c 2812
1bedd215
AC
2813 add_prefix_cmd ("set", class_vars, set_command, _("\
2814Evaluate expression EXP and assign result to variable VAR, using assignment\n\
c906108c
SS
2815syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2816example). VAR may be a debugger \"convenience\" variable (names starting\n\
2817with $), a register (a few standard names starting with $), or an actual\n\
1bedd215
AC
2818variable in the program being debugged. EXP is any valid expression.\n\
2819Use \"set variable\" for variables with names identical to set subcommands.\n\
2820\n\
2821With a subcommand, this command modifies parts of the gdb environment.\n\
2822You can see these environment settings with the \"show\" command."),
c5aa993b 2823 &setlist, "set ", 1, &cmdlist);
c906108c 2824 if (dbx_commands)
1bedd215
AC
2825 add_com ("assign", class_vars, set_command, _("\
2826Evaluate expression EXP and assign result to variable VAR, using assignment\n\
c906108c
SS
2827syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2828example). VAR may be a debugger \"convenience\" variable (names starting\n\
2829with $), a register (a few standard names starting with $), or an actual\n\
1bedd215
AC
2830variable in the program being debugged. EXP is any valid expression.\n\
2831Use \"set variable\" for variables with names identical to set subcommands.\n\
c906108c 2832\nWith a subcommand, this command modifies parts of the gdb environment.\n\
1bedd215 2833You can see these environment settings with the \"show\" command."));
c906108c 2834
0df8b418 2835 /* "call" is the same as "set", but handy for dbx users to call fns. */
1bedd215
AC
2836 c = add_com ("call", class_vars, call_command, _("\
2837Call a function in the program.\n\
c906108c
SS
2838The argument is the function name and arguments, in the notation of the\n\
2839current working language. The result is printed and saved in the value\n\
1bedd215 2840history, if it is not void."));
65d12d83 2841 set_cmd_completer (c, expression_completer);
c906108c 2842
1a966eab
AC
2843 add_cmd ("variable", class_vars, set_command, _("\
2844Evaluate expression EXP and assign result to variable VAR, using assignment\n\
c906108c
SS
2845syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2846example). VAR may be a debugger \"convenience\" variable (names starting\n\
2847with $), a register (a few standard names starting with $), or an actual\n\
2848variable in the program being debugged. EXP is any valid expression.\n\
1a966eab 2849This may usually be abbreviated to simply \"set\"."),
c5aa993b 2850 &setlist);
c906108c 2851
1bedd215
AC
2852 c = add_com ("print", class_vars, print_command, _("\
2853Print value of expression EXP.\n\
c906108c
SS
2854Variables accessible are those of the lexical environment of the selected\n\
2855stack frame, plus all those whose scope is global or an entire file.\n\
2856\n\
2857$NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2858$$NUM refers to NUM'th value back from the last one.\n\
1bedd215
AC
2859Names starting with $ refer to registers (with the values they would have\n\
2860if the program were to return to the stack frame now selected, restoring\n\
c906108c
SS
2861all registers saved by frames farther in) or else to debugger\n\
2862\"convenience\" variables (any such name not a known register).\n\
1bedd215
AC
2863Use assignment expressions to give values to convenience variables.\n\
2864\n\
c906108c
SS
2865{TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2866@ is a binary operator for treating consecutive data objects\n\
2867anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2868element is FOO, whose second element is stored in the space following\n\
2869where FOO is stored, etc. FOO must be an expression whose value\n\
1bedd215
AC
2870resides in memory.\n\
2871\n\
c906108c 2872EXP may be preceded with /FMT, where FMT is a format letter\n\
1bedd215 2873but no count or size letter (see \"x\" command)."));
65d12d83 2874 set_cmd_completer (c, expression_completer);
c906108c 2875 add_com_alias ("p", "print", class_vars, 1);
e93a8774 2876 add_com_alias ("inspect", "print", class_vars, 1);
c906108c 2877
35096d9d
AC
2878 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2879 &max_symbolic_offset, _("\
2880Set the largest offset that will be printed in <symbol+1234> form."), _("\
f81d1120
PA
2881Show the largest offset that will be printed in <symbol+1234> form."), _("\
2882Tell GDB to only display the symbolic form of an address if the\n\
2883offset between the closest earlier symbol and the address is less than\n\
2884the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
2885to always print the symbolic form of an address if any symbol precedes\n\
2886it. Zero is equivalent to \"unlimited\"."),
35096d9d 2887 NULL,
920d2a44 2888 show_max_symbolic_offset,
35096d9d 2889 &setprintlist, &showprintlist);
5bf193a2
AC
2890 add_setshow_boolean_cmd ("symbol-filename", no_class,
2891 &print_symbol_filename, _("\
2892Set printing of source filename and line number with <symbol>."), _("\
2893Show printing of source filename and line number with <symbol>."), NULL,
2894 NULL,
920d2a44 2895 show_print_symbol_filename,
5bf193a2 2896 &setprintlist, &showprintlist);
f1421989
HZ
2897
2898 add_com ("eval", no_class, eval_command, _("\
2899Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
2900a command line, and call it."));
c906108c 2901}