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