GDB (API)
/home/stan/gdb/src/gdb/printcmd.c
Go to the documentation of this file.
00001 /* Print values for GNU debugger GDB.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdb_string.h"
00022 #include "frame.h"
00023 #include "symtab.h"
00024 #include "gdbtypes.h"
00025 #include "value.h"
00026 #include "language.h"
00027 #include "expression.h"
00028 #include "gdbcore.h"
00029 #include "gdbcmd.h"
00030 #include "target.h"
00031 #include "breakpoint.h"
00032 #include "demangle.h"
00033 #include "gdb-demangle.h"
00034 #include "valprint.h"
00035 #include "annotate.h"
00036 #include "symfile.h"            /* for overlay functions */
00037 #include "objfiles.h"           /* ditto */
00038 #include "completer.h"          /* for completion functions */
00039 #include "ui-out.h"
00040 #include "gdb_assert.h"
00041 #include "block.h"
00042 #include "disasm.h"
00043 #include "dfp.h"
00044 #include "valprint.h"
00045 #include "exceptions.h"
00046 #include "observer.h"
00047 #include "solist.h"
00048 #include "parser-defs.h"
00049 #include "charset.h"
00050 #include "arch-utils.h"
00051 #include "cli/cli-utils.h"
00052 #include "format.h"
00053 #include "source.h"
00054 
00055 #ifdef TUI
00056 #include "tui/tui.h"            /* For tui_active et al.   */
00057 #endif
00058 
00059 struct format_data
00060   {
00061     int count;
00062     char format;
00063     char size;
00064 
00065     /* True if the value should be printed raw -- that is, bypassing
00066        python-based formatters.  */
00067     unsigned char raw;
00068   };
00069 
00070 /* Last specified output format.  */
00071 
00072 static char last_format = 0;
00073 
00074 /* Last specified examination size.  'b', 'h', 'w' or `q'.  */
00075 
00076 static char last_size = 'w';
00077 
00078 /* Default address to examine next, and associated architecture.  */
00079 
00080 static struct gdbarch *next_gdbarch;
00081 static CORE_ADDR next_address;
00082 
00083 /* Number of delay instructions following current disassembled insn.  */
00084 
00085 static int branch_delay_insns;
00086 
00087 /* Last address examined.  */
00088 
00089 static CORE_ADDR last_examine_address;
00090 
00091 /* Contents of last address examined.
00092    This is not valid past the end of the `x' command!  */
00093 
00094 static struct value *last_examine_value;
00095 
00096 /* Largest offset between a symbolic value and an address, that will be
00097    printed as `0x1234 <symbol+offset>'.  */
00098 
00099 static unsigned int max_symbolic_offset = UINT_MAX;
00100 static void
00101 show_max_symbolic_offset (struct ui_file *file, int from_tty,
00102                           struct cmd_list_element *c, const char *value)
00103 {
00104   fprintf_filtered (file,
00105                     _("The largest offset that will be "
00106                       "printed in <symbol+1234> form is %s.\n"),
00107                     value);
00108 }
00109 
00110 /* Append the source filename and linenumber of the symbol when
00111    printing a symbolic value as `<symbol at filename:linenum>' if set.  */
00112 static int print_symbol_filename = 0;
00113 static void
00114 show_print_symbol_filename (struct ui_file *file, int from_tty,
00115                             struct cmd_list_element *c, const char *value)
00116 {
00117   fprintf_filtered (file, _("Printing of source filename and "
00118                             "line number with <symbol> is %s.\n"),
00119                     value);
00120 }
00121 
00122 /* Number of auto-display expression currently being displayed.
00123    So that we can disable it if we get a signal within it.
00124    -1 when not doing one.  */
00125 
00126 static int current_display_number;
00127 
00128 struct display
00129   {
00130     /* Chain link to next auto-display item.  */
00131     struct display *next;
00132 
00133     /* The expression as the user typed it.  */
00134     char *exp_string;
00135 
00136     /* Expression to be evaluated and displayed.  */
00137     struct expression *exp;
00138 
00139     /* Item number of this auto-display item.  */
00140     int number;
00141 
00142     /* Display format specified.  */
00143     struct format_data format;
00144 
00145     /* Program space associated with `block'.  */
00146     struct program_space *pspace;
00147 
00148     /* Innermost block required by this expression when evaluated.  */
00149     const struct block *block;
00150 
00151     /* Status of this display (enabled or disabled).  */
00152     int enabled_p;
00153   };
00154 
00155 /* Chain of expressions whose values should be displayed
00156    automatically each time the program stops.  */
00157 
00158 static struct display *display_chain;
00159 
00160 static int display_number;
00161 
00162 /* Walk the following statement or block through all displays.
00163    ALL_DISPLAYS_SAFE does so even if the statement deletes the current
00164    display.  */
00165 
00166 #define ALL_DISPLAYS(B)                         \
00167   for (B = display_chain; B; B = B->next)
00168 
00169 #define ALL_DISPLAYS_SAFE(B,TMP)                \
00170   for (B = display_chain;                       \
00171        B ? (TMP = B->next, 1): 0;               \
00172        B = TMP)
00173 
00174 /* Prototypes for exported functions.  */
00175 
00176 void _initialize_printcmd (void);
00177 
00178 /* Prototypes for local functions.  */
00179 
00180 static void do_one_display (struct display *);
00181 
00182 
00183 /* Decode a format specification.  *STRING_PTR should point to it.
00184    OFORMAT and OSIZE are used as defaults for the format and size
00185    if none are given in the format specification.
00186    If OSIZE is zero, then the size field of the returned value
00187    should be set only if a size is explicitly specified by the
00188    user.
00189    The structure returned describes all the data
00190    found in the specification.  In addition, *STRING_PTR is advanced
00191    past the specification and past all whitespace following it.  */
00192 
00193 static struct format_data
00194 decode_format (const char **string_ptr, int oformat, int osize)
00195 {
00196   struct format_data val;
00197   const char *p = *string_ptr;
00198 
00199   val.format = '?';
00200   val.size = '?';
00201   val.count = 1;
00202   val.raw = 0;
00203 
00204   if (*p >= '0' && *p <= '9')
00205     val.count = atoi (p);
00206   while (*p >= '0' && *p <= '9')
00207     p++;
00208 
00209   /* Now process size or format letters that follow.  */
00210 
00211   while (1)
00212     {
00213       if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
00214         val.size = *p++;
00215       else if (*p == 'r')
00216         {
00217           val.raw = 1;
00218           p++;
00219         }
00220       else if (*p >= 'a' && *p <= 'z')
00221         val.format = *p++;
00222       else
00223         break;
00224     }
00225 
00226   while (*p == ' ' || *p == '\t')
00227     p++;
00228   *string_ptr = p;
00229 
00230   /* Set defaults for format and size if not specified.  */
00231   if (val.format == '?')
00232     {
00233       if (val.size == '?')
00234         {
00235           /* Neither has been specified.  */
00236           val.format = oformat;
00237           val.size = osize;
00238         }
00239       else
00240         /* If a size is specified, any format makes a reasonable
00241            default except 'i'.  */
00242         val.format = oformat == 'i' ? 'x' : oformat;
00243     }
00244   else if (val.size == '?')
00245     switch (val.format)
00246       {
00247       case 'a':
00248         /* Pick the appropriate size for an address.  This is deferred
00249            until do_examine when we know the actual architecture to use.
00250            A special size value of 'a' is used to indicate this case.  */
00251         val.size = osize ? 'a' : osize;
00252         break;
00253       case 'f':
00254         /* Floating point has to be word or giantword.  */
00255         if (osize == 'w' || osize == 'g')
00256           val.size = osize;
00257         else
00258           /* Default it to giantword if the last used size is not
00259              appropriate.  */
00260           val.size = osize ? 'g' : osize;
00261         break;
00262       case 'c':
00263         /* Characters default to one byte.  */
00264         val.size = osize ? 'b' : osize;
00265         break;
00266       case 's':
00267         /* Display strings with byte size chars unless explicitly
00268            specified.  */
00269         val.size = '\0';
00270         break;
00271 
00272       default:
00273         /* The default is the size most recently specified.  */
00274         val.size = osize;
00275       }
00276 
00277   return val;
00278 }
00279 
00280 /* Print value VAL on stream according to OPTIONS.
00281    Do not end with a newline.
00282    SIZE is the letter for the size of datum being printed.
00283    This is used to pad hex numbers so they line up.  SIZE is 0
00284    for print / output and set for examine.  */
00285 
00286 static void
00287 print_formatted (struct value *val, int size,
00288                  const struct value_print_options *options,
00289                  struct ui_file *stream)
00290 {
00291   struct type *type = check_typedef (value_type (val));
00292   int len = TYPE_LENGTH (type);
00293 
00294   if (VALUE_LVAL (val) == lval_memory)
00295     next_address = value_address (val) + len;
00296 
00297   if (size)
00298     {
00299       switch (options->format)
00300         {
00301         case 's':
00302           {
00303             struct type *elttype = value_type (val);
00304 
00305             next_address = (value_address (val)
00306                             + val_print_string (elttype, NULL,
00307                                                 value_address (val), -1,
00308                                                 stream, options) * len);
00309           }
00310           return;
00311 
00312         case 'i':
00313           /* We often wrap here if there are long symbolic names.  */
00314           wrap_here ("    ");
00315           next_address = (value_address (val)
00316                           + gdb_print_insn (get_type_arch (type),
00317                                             value_address (val), stream,
00318                                             &branch_delay_insns));
00319           return;
00320         }
00321     }
00322 
00323   if (options->format == 0 || options->format == 's'
00324       || TYPE_CODE (type) == TYPE_CODE_REF
00325       || TYPE_CODE (type) == TYPE_CODE_ARRAY
00326       || TYPE_CODE (type) == TYPE_CODE_STRING
00327       || TYPE_CODE (type) == TYPE_CODE_STRUCT
00328       || TYPE_CODE (type) == TYPE_CODE_UNION
00329       || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
00330     value_print (val, stream, options);
00331   else
00332     /* User specified format, so don't look to the type to tell us
00333        what to do.  */
00334     val_print_scalar_formatted (type,
00335                                 value_contents_for_printing (val),
00336                                 value_embedded_offset (val),
00337                                 val,
00338                                 options, size, stream);
00339 }
00340 
00341 /* Return builtin floating point type of same length as TYPE.
00342    If no such type is found, return TYPE itself.  */
00343 static struct type *
00344 float_type_from_length (struct type *type)
00345 {
00346   struct gdbarch *gdbarch = get_type_arch (type);
00347   const struct builtin_type *builtin = builtin_type (gdbarch);
00348 
00349   if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_float))
00350     type = builtin->builtin_float;
00351   else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_double))
00352     type = builtin->builtin_double;
00353   else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_long_double))
00354     type = builtin->builtin_long_double;
00355 
00356   return type;
00357 }
00358 
00359 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
00360    according to OPTIONS and SIZE on STREAM.  Formats s and i are not
00361    supported at this level.  */
00362 
00363 void
00364 print_scalar_formatted (const void *valaddr, struct type *type,
00365                         const struct value_print_options *options,
00366                         int size, struct ui_file *stream)
00367 {
00368   struct gdbarch *gdbarch = get_type_arch (type);
00369   LONGEST val_long = 0;
00370   unsigned int len = TYPE_LENGTH (type);
00371   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00372 
00373   /* String printing should go through val_print_scalar_formatted.  */
00374   gdb_assert (options->format != 's');
00375 
00376   if (len > sizeof(LONGEST) &&
00377       (TYPE_CODE (type) == TYPE_CODE_INT
00378        || TYPE_CODE (type) == TYPE_CODE_ENUM))
00379     {
00380       switch (options->format)
00381         {
00382         case 'o':
00383           print_octal_chars (stream, valaddr, len, byte_order);
00384           return;
00385         case 'u':
00386         case 'd':
00387           print_decimal_chars (stream, valaddr, len, byte_order);
00388           return;
00389         case 't':
00390           print_binary_chars (stream, valaddr, len, byte_order);
00391           return;
00392         case 'x':
00393           print_hex_chars (stream, valaddr, len, byte_order);
00394           return;
00395         case 'c':
00396           print_char_chars (stream, type, valaddr, len, byte_order);
00397           return;
00398         default:
00399           break;
00400         };
00401     }
00402 
00403   if (options->format != 'f')
00404     val_long = unpack_long (type, valaddr);
00405 
00406   /* If the value is a pointer, and pointers and addresses are not the
00407      same, then at this point, the value's length (in target bytes) is
00408      gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type).  */
00409   if (TYPE_CODE (type) == TYPE_CODE_PTR)
00410     len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
00411 
00412   /* If we are printing it as unsigned, truncate it in case it is actually
00413      a negative signed value (e.g. "print/u (short)-1" should print 65535
00414      (if shorts are 16 bits) instead of 4294967295).  */
00415   if (options->format != 'd' || TYPE_UNSIGNED (type))
00416     {
00417       if (len < sizeof (LONGEST))
00418         val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
00419     }
00420 
00421   switch (options->format)
00422     {
00423     case 'x':
00424       if (!size)
00425         {
00426           /* No size specified, like in print.  Print varying # of digits.  */
00427           print_longest (stream, 'x', 1, val_long);
00428         }
00429       else
00430         switch (size)
00431           {
00432           case 'b':
00433           case 'h':
00434           case 'w':
00435           case 'g':
00436             print_longest (stream, size, 1, val_long);
00437             break;
00438           default:
00439             error (_("Undefined output size \"%c\"."), size);
00440           }
00441       break;
00442 
00443     case 'd':
00444       print_longest (stream, 'd', 1, val_long);
00445       break;
00446 
00447     case 'u':
00448       print_longest (stream, 'u', 0, val_long);
00449       break;
00450 
00451     case 'o':
00452       if (val_long)
00453         print_longest (stream, 'o', 1, val_long);
00454       else
00455         fprintf_filtered (stream, "0");
00456       break;
00457 
00458     case 'a':
00459       {
00460         CORE_ADDR addr = unpack_pointer (type, valaddr);
00461 
00462         print_address (gdbarch, addr, stream);
00463       }
00464       break;
00465 
00466     case 'c':
00467       {
00468         struct value_print_options opts = *options;
00469 
00470         opts.format = 0;
00471         if (TYPE_UNSIGNED (type))
00472           type = builtin_type (gdbarch)->builtin_true_unsigned_char;
00473         else
00474           type = builtin_type (gdbarch)->builtin_true_char;
00475 
00476         value_print (value_from_longest (type, val_long), stream, &opts);
00477       }
00478       break;
00479 
00480     case 'f':
00481       type = float_type_from_length (type);
00482       print_floating (valaddr, type, stream);
00483       break;
00484 
00485     case 0:
00486       internal_error (__FILE__, __LINE__,
00487                       _("failed internal consistency check"));
00488 
00489     case 't':
00490       /* Binary; 't' stands for "two".  */
00491       {
00492         char bits[8 * (sizeof val_long) + 1];
00493         char buf[8 * (sizeof val_long) + 32];
00494         char *cp = bits;
00495         int width;
00496 
00497         if (!size)
00498           width = 8 * (sizeof val_long);
00499         else
00500           switch (size)
00501             {
00502             case 'b':
00503               width = 8;
00504               break;
00505             case 'h':
00506               width = 16;
00507               break;
00508             case 'w':
00509               width = 32;
00510               break;
00511             case 'g':
00512               width = 64;
00513               break;
00514             default:
00515               error (_("Undefined output size \"%c\"."), size);
00516             }
00517 
00518         bits[width] = '\0';
00519         while (width-- > 0)
00520           {
00521             bits[width] = (val_long & 1) ? '1' : '0';
00522             val_long >>= 1;
00523           }
00524         if (!size)
00525           {
00526             while (*cp && *cp == '0')
00527               cp++;
00528             if (*cp == '\0')
00529               cp--;
00530           }
00531         strncpy (buf, cp, sizeof (bits));
00532         fputs_filtered (buf, stream);
00533       }
00534       break;
00535 
00536     case 'z':
00537       print_hex_chars (stream, valaddr, len, byte_order);
00538       break;
00539 
00540     default:
00541       error (_("Undefined output format \"%c\"."), options->format);
00542     }
00543 }
00544 
00545 /* Specify default address for `x' command.
00546    The `info lines' command uses this.  */
00547 
00548 void
00549 set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
00550 {
00551   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
00552 
00553   next_gdbarch = gdbarch;
00554   next_address = addr;
00555 
00556   /* Make address available to the user as $_.  */
00557   set_internalvar (lookup_internalvar ("_"),
00558                    value_from_pointer (ptr_type, addr));
00559 }
00560 
00561 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
00562    after LEADIN.  Print nothing if no symbolic name is found nearby.
00563    Optionally also print source file and line number, if available.
00564    DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
00565    or to interpret it as a possible C++ name and convert it back to source
00566    form.  However note that DO_DEMANGLE can be overridden by the specific
00567    settings of the demangle and asm_demangle variables.  Returns
00568    non-zero if anything was printed; zero otherwise.  */
00569 
00570 int
00571 print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
00572                         struct ui_file *stream,
00573                         int do_demangle, char *leadin)
00574 {
00575   char *name = NULL;
00576   char *filename = NULL;
00577   int unmapped = 0;
00578   int offset = 0;
00579   int line = 0;
00580 
00581   /* Throw away both name and filename.  */
00582   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
00583   make_cleanup (free_current_contents, &filename);
00584 
00585   if (build_address_symbolic (gdbarch, addr, do_demangle, &name, &offset,
00586                               &filename, &line, &unmapped))
00587     {
00588       do_cleanups (cleanup_chain);
00589       return 0;
00590     }
00591 
00592   fputs_filtered (leadin, stream);
00593   if (unmapped)
00594     fputs_filtered ("<*", stream);
00595   else
00596     fputs_filtered ("<", stream);
00597   fputs_filtered (name, stream);
00598   if (offset != 0)
00599     fprintf_filtered (stream, "+%u", (unsigned int) offset);
00600 
00601   /* Append source filename and line number if desired.  Give specific
00602      line # of this addr, if we have it; else line # of the nearest symbol.  */
00603   if (print_symbol_filename && filename != NULL)
00604     {
00605       if (line != -1)
00606         fprintf_filtered (stream, " at %s:%d", filename, line);
00607       else
00608         fprintf_filtered (stream, " in %s", filename);
00609     }
00610   if (unmapped)
00611     fputs_filtered ("*>", stream);
00612   else
00613     fputs_filtered (">", stream);
00614 
00615   do_cleanups (cleanup_chain);
00616   return 1;
00617 }
00618 
00619 /* Given an address ADDR return all the elements needed to print the
00620    address in a symbolic form.  NAME can be mangled or not depending
00621    on DO_DEMANGLE (and also on the asm_demangle global variable,
00622    manipulated via ''set print asm-demangle'').  Return 0 in case of
00623    success, when all the info in the OUT paramters is valid.  Return 1
00624    otherwise.  */
00625 int
00626 build_address_symbolic (struct gdbarch *gdbarch,
00627                         CORE_ADDR addr,  /* IN */
00628                         int do_demangle, /* IN */
00629                         char **name,     /* OUT */
00630                         int *offset,     /* OUT */
00631                         char **filename, /* OUT */
00632                         int *line,       /* OUT */
00633                         int *unmapped)   /* OUT */
00634 {
00635   struct minimal_symbol *msymbol;
00636   struct symbol *symbol;
00637   CORE_ADDR name_location = 0;
00638   struct obj_section *section = NULL;
00639   const char *name_temp = "";
00640   
00641   /* Let's say it is mapped (not unmapped).  */
00642   *unmapped = 0;
00643 
00644   /* Determine if the address is in an overlay, and whether it is
00645      mapped.  */
00646   if (overlay_debugging)
00647     {
00648       section = find_pc_overlay (addr);
00649       if (pc_in_unmapped_range (addr, section))
00650         {
00651           *unmapped = 1;
00652           addr = overlay_mapped_address (addr, section);
00653         }
00654     }
00655 
00656   /* First try to find the address in the symbol table, then
00657      in the minsyms.  Take the closest one.  */
00658 
00659   /* This is defective in the sense that it only finds text symbols.  So
00660      really this is kind of pointless--we should make sure that the
00661      minimal symbols have everything we need (by changing that we could
00662      save some memory, but for many debug format--ELF/DWARF or
00663      anything/stabs--it would be inconvenient to eliminate those minimal
00664      symbols anyway).  */
00665   msymbol = lookup_minimal_symbol_by_pc_section (addr, section).minsym;
00666   symbol = find_pc_sect_function (addr, section);
00667 
00668   if (symbol)
00669     {
00670       /* If this is a function (i.e. a code address), strip out any
00671          non-address bits.  For instance, display a pointer to the
00672          first instruction of a Thumb function as <function>; the
00673          second instruction will be <function+2>, even though the
00674          pointer is <function+3>.  This matches the ISA behavior.  */
00675       addr = gdbarch_addr_bits_remove (gdbarch, addr);
00676 
00677       name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
00678       if (do_demangle || asm_demangle)
00679         name_temp = SYMBOL_PRINT_NAME (symbol);
00680       else
00681         name_temp = SYMBOL_LINKAGE_NAME (symbol);
00682     }
00683 
00684   if (msymbol != NULL
00685       && MSYMBOL_HAS_SIZE (msymbol)
00686       && MSYMBOL_SIZE (msymbol) == 0
00687       && MSYMBOL_TYPE (msymbol) != mst_text
00688       && MSYMBOL_TYPE (msymbol) != mst_text_gnu_ifunc
00689       && MSYMBOL_TYPE (msymbol) != mst_file_text)
00690     msymbol = NULL;
00691 
00692   if (msymbol != NULL)
00693     {
00694       if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
00695         {
00696           /* If this is a function (i.e. a code address), strip out any
00697              non-address bits.  For instance, display a pointer to the
00698              first instruction of a Thumb function as <function>; the
00699              second instruction will be <function+2>, even though the
00700              pointer is <function+3>.  This matches the ISA behavior.  */
00701           if (MSYMBOL_TYPE (msymbol) == mst_text
00702               || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
00703               || MSYMBOL_TYPE (msymbol) == mst_file_text
00704               || MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
00705             addr = gdbarch_addr_bits_remove (gdbarch, addr);
00706 
00707           /* The msymbol is closer to the address than the symbol;
00708              use the msymbol instead.  */
00709           symbol = 0;
00710           name_location = SYMBOL_VALUE_ADDRESS (msymbol);
00711           if (do_demangle || asm_demangle)
00712             name_temp = SYMBOL_PRINT_NAME (msymbol);
00713           else
00714             name_temp = SYMBOL_LINKAGE_NAME (msymbol);
00715         }
00716     }
00717   if (symbol == NULL && msymbol == NULL)
00718     return 1;
00719 
00720   /* If the nearest symbol is too far away, don't print anything symbolic.  */
00721 
00722   /* For when CORE_ADDR is larger than unsigned int, we do math in
00723      CORE_ADDR.  But when we detect unsigned wraparound in the
00724      CORE_ADDR math, we ignore this test and print the offset,
00725      because addr+max_symbolic_offset has wrapped through the end
00726      of the address space back to the beginning, giving bogus comparison.  */
00727   if (addr > name_location + max_symbolic_offset
00728       && name_location + max_symbolic_offset > name_location)
00729     return 1;
00730 
00731   *offset = addr - name_location;
00732 
00733   *name = xstrdup (name_temp);
00734 
00735   if (print_symbol_filename)
00736     {
00737       struct symtab_and_line sal;
00738 
00739       sal = find_pc_sect_line (addr, section, 0);
00740 
00741       if (sal.symtab)
00742         {
00743           *filename = xstrdup (symtab_to_filename_for_display (sal.symtab));
00744           *line = sal.line;
00745         }
00746     }
00747   return 0;
00748 }
00749 
00750 
00751 /* Print address ADDR symbolically on STREAM.
00752    First print it as a number.  Then perhaps print
00753    <SYMBOL + OFFSET> after the number.  */
00754 
00755 void
00756 print_address (struct gdbarch *gdbarch,
00757                CORE_ADDR addr, struct ui_file *stream)
00758 {
00759   fputs_filtered (paddress (gdbarch, addr), stream);
00760   print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
00761 }
00762 
00763 /* Return a prefix for instruction address:
00764    "=> " for current instruction, else "   ".  */
00765 
00766 const char *
00767 pc_prefix (CORE_ADDR addr)
00768 {
00769   if (has_stack_frames ())
00770     {
00771       struct frame_info *frame;
00772       CORE_ADDR pc;
00773 
00774       frame = get_selected_frame (NULL);
00775       if (get_frame_pc_if_available (frame, &pc) && pc == addr)
00776         return "=> ";
00777     }
00778   return "   ";
00779 }
00780 
00781 /* Print address ADDR symbolically on STREAM.  Parameter DEMANGLE
00782    controls whether to print the symbolic name "raw" or demangled.
00783    Return non-zero if anything was printed; zero otherwise.  */
00784 
00785 int
00786 print_address_demangle (const struct value_print_options *opts,
00787                         struct gdbarch *gdbarch, CORE_ADDR addr,
00788                         struct ui_file *stream, int do_demangle)
00789 {
00790   if (opts->addressprint)
00791     {
00792       fputs_filtered (paddress (gdbarch, addr), stream);
00793       print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
00794     }
00795   else
00796     {
00797       return print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
00798     }
00799   return 1;
00800 }
00801 
00802 
00803 /* Examine data at address ADDR in format FMT.
00804    Fetch it from memory and print on gdb_stdout.  */
00805 
00806 static void
00807 do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
00808 {
00809   char format = 0;
00810   char size;
00811   int count = 1;
00812   struct type *val_type = NULL;
00813   int i;
00814   int maxelts;
00815   struct value_print_options opts;
00816 
00817   format = fmt.format;
00818   size = fmt.size;
00819   count = fmt.count;
00820   next_gdbarch = gdbarch;
00821   next_address = addr;
00822 
00823   /* Instruction format implies fetch single bytes
00824      regardless of the specified size.
00825      The case of strings is handled in decode_format, only explicit
00826      size operator are not changed to 'b'.  */
00827   if (format == 'i')
00828     size = 'b';
00829 
00830   if (size == 'a')
00831     {
00832       /* Pick the appropriate size for an address.  */
00833       if (gdbarch_ptr_bit (next_gdbarch) == 64)
00834         size = 'g';
00835       else if (gdbarch_ptr_bit (next_gdbarch) == 32)
00836         size = 'w';
00837       else if (gdbarch_ptr_bit (next_gdbarch) == 16)
00838         size = 'h';
00839       else
00840         /* Bad value for gdbarch_ptr_bit.  */
00841         internal_error (__FILE__, __LINE__,
00842                         _("failed internal consistency check"));
00843     }
00844 
00845   if (size == 'b')
00846     val_type = builtin_type (next_gdbarch)->builtin_int8;
00847   else if (size == 'h')
00848     val_type = builtin_type (next_gdbarch)->builtin_int16;
00849   else if (size == 'w')
00850     val_type = builtin_type (next_gdbarch)->builtin_int32;
00851   else if (size == 'g')
00852     val_type = builtin_type (next_gdbarch)->builtin_int64;
00853 
00854   if (format == 's')
00855     {
00856       struct type *char_type = NULL;
00857 
00858       /* Search for "char16_t"  or "char32_t" types or fall back to 8-bit char
00859          if type is not found.  */
00860       if (size == 'h')
00861         char_type = builtin_type (next_gdbarch)->builtin_char16;
00862       else if (size == 'w')
00863         char_type = builtin_type (next_gdbarch)->builtin_char32;
00864       if (char_type)
00865         val_type = char_type;
00866       else
00867         {
00868           if (size != '\0' && size != 'b')
00869             warning (_("Unable to display strings with "
00870                        "size '%c', using 'b' instead."), size);
00871           size = 'b';
00872           val_type = builtin_type (next_gdbarch)->builtin_int8;
00873         }
00874     }
00875 
00876   maxelts = 8;
00877   if (size == 'w')
00878     maxelts = 4;
00879   if (size == 'g')
00880     maxelts = 2;
00881   if (format == 's' || format == 'i')
00882     maxelts = 1;
00883 
00884   get_formatted_print_options (&opts, format);
00885 
00886   /* Print as many objects as specified in COUNT, at most maxelts per line,
00887      with the address of the next one at the start of each line.  */
00888 
00889   while (count > 0)
00890     {
00891       QUIT;
00892       if (format == 'i')
00893         fputs_filtered (pc_prefix (next_address), gdb_stdout);
00894       print_address (next_gdbarch, next_address, gdb_stdout);
00895       printf_filtered (":");
00896       for (i = maxelts;
00897            i > 0 && count > 0;
00898            i--, count--)
00899         {
00900           printf_filtered ("\t");
00901           /* Note that print_formatted sets next_address for the next
00902              object.  */
00903           last_examine_address = next_address;
00904 
00905           if (last_examine_value)
00906             value_free (last_examine_value);
00907 
00908           /* The value to be displayed is not fetched greedily.
00909              Instead, to avoid the possibility of a fetched value not
00910              being used, its retrieval is delayed until the print code
00911              uses it.  When examining an instruction stream, the
00912              disassembler will perform its own memory fetch using just
00913              the address stored in LAST_EXAMINE_VALUE.  FIXME: Should
00914              the disassembler be modified so that LAST_EXAMINE_VALUE
00915              is left with the byte sequence from the last complete
00916              instruction fetched from memory?  */
00917           last_examine_value = value_at_lazy (val_type, next_address);
00918 
00919           if (last_examine_value)
00920             release_value (last_examine_value);
00921 
00922           print_formatted (last_examine_value, size, &opts, gdb_stdout);
00923 
00924           /* Display any branch delay slots following the final insn.  */
00925           if (format == 'i' && count == 1)
00926             count += branch_delay_insns;
00927         }
00928       printf_filtered ("\n");
00929       gdb_flush (gdb_stdout);
00930     }
00931 }
00932 
00933 static void
00934 validate_format (struct format_data fmt, char *cmdname)
00935 {
00936   if (fmt.size != 0)
00937     error (_("Size letters are meaningless in \"%s\" command."), cmdname);
00938   if (fmt.count != 1)
00939     error (_("Item count other than 1 is meaningless in \"%s\" command."),
00940            cmdname);
00941   if (fmt.format == 'i')
00942     error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
00943            fmt.format, cmdname);
00944 }
00945 
00946 /* Evaluate string EXP as an expression in the current language and
00947    print the resulting value.  EXP may contain a format specifier as the
00948    first argument ("/x myvar" for example, to print myvar in hex).  */
00949 
00950 static void
00951 print_command_1 (const char *exp, int voidprint)
00952 {
00953   struct expression *expr;
00954   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
00955   char format = 0;
00956   struct value *val;
00957   struct format_data fmt;
00958 
00959   if (exp && *exp == '/')
00960     {
00961       exp++;
00962       fmt = decode_format (&exp, last_format, 0);
00963       validate_format (fmt, "print");
00964       last_format = format = fmt.format;
00965     }
00966   else
00967     {
00968       fmt.count = 1;
00969       fmt.format = 0;
00970       fmt.size = 0;
00971       fmt.raw = 0;
00972     }
00973 
00974   if (exp && *exp)
00975     {
00976       expr = parse_expression (exp);
00977       make_cleanup (free_current_contents, &expr);
00978       val = evaluate_expression (expr);
00979     }
00980   else
00981     val = access_value_history (0);
00982 
00983   if (voidprint || (val && value_type (val) &&
00984                     TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
00985     {
00986       struct value_print_options opts;
00987       int histindex = record_latest_value (val);
00988 
00989       if (histindex >= 0)
00990         annotate_value_history_begin (histindex, value_type (val));
00991       else
00992         annotate_value_begin (value_type (val));
00993 
00994       if (histindex >= 0)
00995         printf_filtered ("$%d = ", histindex);
00996 
00997       if (histindex >= 0)
00998         annotate_value_history_value ();
00999 
01000       get_formatted_print_options (&opts, format);
01001       opts.raw = fmt.raw;
01002 
01003       print_formatted (val, fmt.size, &opts, gdb_stdout);
01004       printf_filtered ("\n");
01005 
01006       if (histindex >= 0)
01007         annotate_value_history_end ();
01008       else
01009         annotate_value_end ();
01010     }
01011 
01012   do_cleanups (old_chain);
01013 }
01014 
01015 static void
01016 print_command (char *exp, int from_tty)
01017 {
01018   print_command_1 (exp, 1);
01019 }
01020 
01021 /* Same as print, except it doesn't print void results.  */
01022 static void
01023 call_command (char *exp, int from_tty)
01024 {
01025   print_command_1 (exp, 0);
01026 }
01027 
01028 /* Implementation of the "output" command.  */
01029 
01030 static void
01031 output_command (char *exp, int from_tty)
01032 {
01033   output_command_const (exp, from_tty);
01034 }
01035 
01036 /* Like output_command, but takes a const string as argument.  */
01037 
01038 void
01039 output_command_const (const char *exp, int from_tty)
01040 {
01041   struct expression *expr;
01042   struct cleanup *old_chain;
01043   char format = 0;
01044   struct value *val;
01045   struct format_data fmt;
01046   struct value_print_options opts;
01047 
01048   fmt.size = 0;
01049   fmt.raw = 0;
01050 
01051   if (exp && *exp == '/')
01052     {
01053       exp++;
01054       fmt = decode_format (&exp, 0, 0);
01055       validate_format (fmt, "output");
01056       format = fmt.format;
01057     }
01058 
01059   expr = parse_expression (exp);
01060   old_chain = make_cleanup (free_current_contents, &expr);
01061 
01062   val = evaluate_expression (expr);
01063 
01064   annotate_value_begin (value_type (val));
01065 
01066   get_formatted_print_options (&opts, format);
01067   opts.raw = fmt.raw;
01068   print_formatted (val, fmt.size, &opts, gdb_stdout);
01069 
01070   annotate_value_end ();
01071 
01072   wrap_here ("");
01073   gdb_flush (gdb_stdout);
01074 
01075   do_cleanups (old_chain);
01076 }
01077 
01078 static void
01079 set_command (char *exp, int from_tty)
01080 {
01081   struct expression *expr = parse_expression (exp);
01082   struct cleanup *old_chain =
01083     make_cleanup (free_current_contents, &expr);
01084 
01085   if (expr->nelts >= 1)
01086     switch (expr->elts[0].opcode)
01087       {
01088       case UNOP_PREINCREMENT:
01089       case UNOP_POSTINCREMENT:
01090       case UNOP_PREDECREMENT:
01091       case UNOP_POSTDECREMENT:
01092       case BINOP_ASSIGN:
01093       case BINOP_ASSIGN_MODIFY:
01094       case BINOP_COMMA:
01095         break;
01096       default:
01097         warning
01098           (_("Expression is not an assignment (and might have no effect)"));
01099       }
01100 
01101   evaluate_expression (expr);
01102   do_cleanups (old_chain);
01103 }
01104 
01105 static void
01106 sym_info (char *arg, int from_tty)
01107 {
01108   struct minimal_symbol *msymbol;
01109   struct objfile *objfile;
01110   struct obj_section *osect;
01111   CORE_ADDR addr, sect_addr;
01112   int matches = 0;
01113   unsigned int offset;
01114 
01115   if (!arg)
01116     error_no_arg (_("address"));
01117 
01118   addr = parse_and_eval_address (arg);
01119   ALL_OBJSECTIONS (objfile, osect)
01120   {
01121     /* Only process each object file once, even if there's a separate
01122        debug file.  */
01123     if (objfile->separate_debug_objfile_backlink)
01124       continue;
01125 
01126     sect_addr = overlay_mapped_address (addr, osect);
01127 
01128     if (obj_section_addr (osect) <= sect_addr
01129         && sect_addr < obj_section_endaddr (osect)
01130         && (msymbol
01131             = lookup_minimal_symbol_by_pc_section (sect_addr, osect).minsym))
01132       {
01133         const char *obj_name, *mapped, *sec_name, *msym_name;
01134         char *loc_string;
01135         struct cleanup *old_chain;
01136 
01137         matches = 1;
01138         offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
01139         mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
01140         sec_name = osect->the_bfd_section->name;
01141         msym_name = SYMBOL_PRINT_NAME (msymbol);
01142 
01143         /* Don't print the offset if it is zero.
01144            We assume there's no need to handle i18n of "sym + offset".  */
01145         if (offset)
01146           loc_string = xstrprintf ("%s + %u", msym_name, offset);
01147         else
01148           loc_string = xstrprintf ("%s", msym_name);
01149 
01150         /* Use a cleanup to free loc_string in case the user quits
01151            a pagination request inside printf_filtered.  */
01152         old_chain = make_cleanup (xfree, loc_string);
01153 
01154         gdb_assert (osect->objfile && objfile_name (osect->objfile));
01155         obj_name = objfile_name (osect->objfile);
01156 
01157         if (MULTI_OBJFILE_P ())
01158           if (pc_in_unmapped_range (addr, osect))
01159             if (section_is_overlay (osect))
01160               printf_filtered (_("%s in load address range of "
01161                                  "%s overlay section %s of %s\n"),
01162                                loc_string, mapped, sec_name, obj_name);
01163             else
01164               printf_filtered (_("%s in load address range of "
01165                                  "section %s of %s\n"),
01166                                loc_string, sec_name, obj_name);
01167           else
01168             if (section_is_overlay (osect))
01169               printf_filtered (_("%s in %s overlay section %s of %s\n"),
01170                                loc_string, mapped, sec_name, obj_name);
01171             else
01172               printf_filtered (_("%s in section %s of %s\n"),
01173                                loc_string, sec_name, obj_name);
01174         else
01175           if (pc_in_unmapped_range (addr, osect))
01176             if (section_is_overlay (osect))
01177               printf_filtered (_("%s in load address range of %s overlay "
01178                                  "section %s\n"),
01179                                loc_string, mapped, sec_name);
01180             else
01181               printf_filtered (_("%s in load address range of section %s\n"),
01182                                loc_string, sec_name);
01183           else
01184             if (section_is_overlay (osect))
01185               printf_filtered (_("%s in %s overlay section %s\n"),
01186                                loc_string, mapped, sec_name);
01187             else
01188               printf_filtered (_("%s in section %s\n"),
01189                                loc_string, sec_name);
01190 
01191         do_cleanups (old_chain);
01192       }
01193   }
01194   if (matches == 0)
01195     printf_filtered (_("No symbol matches %s.\n"), arg);
01196 }
01197 
01198 static void
01199 address_info (char *exp, int from_tty)
01200 {
01201   struct gdbarch *gdbarch;
01202   int regno;
01203   struct symbol *sym;
01204   struct bound_minimal_symbol msymbol;
01205   long val;
01206   struct obj_section *section;
01207   CORE_ADDR load_addr, context_pc = 0;
01208   struct field_of_this_result is_a_field_of_this;
01209 
01210   if (exp == 0)
01211     error (_("Argument required."));
01212 
01213   sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
01214                        &is_a_field_of_this);
01215   if (sym == NULL)
01216     {
01217       if (is_a_field_of_this.type != NULL)
01218         {
01219           printf_filtered ("Symbol \"");
01220           fprintf_symbol_filtered (gdb_stdout, exp,
01221                                    current_language->la_language, DMGL_ANSI);
01222           printf_filtered ("\" is a field of the local class variable ");
01223           if (current_language->la_language == language_objc)
01224             printf_filtered ("`self'\n");       /* ObjC equivalent of "this" */
01225           else
01226             printf_filtered ("`this'\n");
01227           return;
01228         }
01229 
01230       msymbol = lookup_bound_minimal_symbol (exp);
01231 
01232       if (msymbol.minsym != NULL)
01233         {
01234           struct objfile *objfile = msymbol.objfile;
01235 
01236           gdbarch = get_objfile_arch (objfile);
01237           load_addr = SYMBOL_VALUE_ADDRESS (msymbol.minsym);
01238 
01239           printf_filtered ("Symbol \"");
01240           fprintf_symbol_filtered (gdb_stdout, exp,
01241                                    current_language->la_language, DMGL_ANSI);
01242           printf_filtered ("\" is at ");
01243           fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01244           printf_filtered (" in a file compiled without debugging");
01245           section = SYMBOL_OBJ_SECTION (objfile, msymbol.minsym);
01246           if (section_is_overlay (section))
01247             {
01248               load_addr = overlay_unmapped_address (load_addr, section);
01249               printf_filtered (",\n -- loaded at ");
01250               fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01251               printf_filtered (" in overlay section %s",
01252                                section->the_bfd_section->name);
01253             }
01254           printf_filtered (".\n");
01255         }
01256       else
01257         error (_("No symbol \"%s\" in current context."), exp);
01258       return;
01259     }
01260 
01261   printf_filtered ("Symbol \"");
01262   fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
01263                            current_language->la_language, DMGL_ANSI);
01264   printf_filtered ("\" is ");
01265   val = SYMBOL_VALUE (sym);
01266   section = SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (sym), sym);
01267   gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
01268 
01269   if (SYMBOL_COMPUTED_OPS (sym) != NULL)
01270     {
01271       SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
01272                                                     gdb_stdout);
01273       printf_filtered (".\n");
01274       return;
01275     }
01276 
01277   switch (SYMBOL_CLASS (sym))
01278     {
01279     case LOC_CONST:
01280     case LOC_CONST_BYTES:
01281       printf_filtered ("constant");
01282       break;
01283 
01284     case LOC_LABEL:
01285       printf_filtered ("a label at address ");
01286       load_addr = SYMBOL_VALUE_ADDRESS (sym);
01287       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01288       if (section_is_overlay (section))
01289         {
01290           load_addr = overlay_unmapped_address (load_addr, section);
01291           printf_filtered (",\n -- loaded at ");
01292           fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01293           printf_filtered (" in overlay section %s",
01294                            section->the_bfd_section->name);
01295         }
01296       break;
01297 
01298     case LOC_COMPUTED:
01299       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
01300 
01301     case LOC_REGISTER:
01302       /* GDBARCH is the architecture associated with the objfile the symbol
01303          is defined in; the target architecture may be different, and may
01304          provide additional registers.  However, we do not know the target
01305          architecture at this point.  We assume the objfile architecture
01306          will contain all the standard registers that occur in debug info
01307          in that objfile.  */
01308       regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
01309 
01310       if (SYMBOL_IS_ARGUMENT (sym))
01311         printf_filtered (_("an argument in register %s"),
01312                          gdbarch_register_name (gdbarch, regno));
01313       else
01314         printf_filtered (_("a variable in register %s"),
01315                          gdbarch_register_name (gdbarch, regno));
01316       break;
01317 
01318     case LOC_STATIC:
01319       printf_filtered (_("static storage at address "));
01320       load_addr = SYMBOL_VALUE_ADDRESS (sym);
01321       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01322       if (section_is_overlay (section))
01323         {
01324           load_addr = overlay_unmapped_address (load_addr, section);
01325           printf_filtered (_(",\n -- loaded at "));
01326           fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01327           printf_filtered (_(" in overlay section %s"),
01328                            section->the_bfd_section->name);
01329         }
01330       break;
01331 
01332     case LOC_REGPARM_ADDR:
01333       /* Note comment at LOC_REGISTER.  */
01334       regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
01335       printf_filtered (_("address of an argument in register %s"),
01336                        gdbarch_register_name (gdbarch, regno));
01337       break;
01338 
01339     case LOC_ARG:
01340       printf_filtered (_("an argument at offset %ld"), val);
01341       break;
01342 
01343     case LOC_LOCAL:
01344       printf_filtered (_("a local variable at frame offset %ld"), val);
01345       break;
01346 
01347     case LOC_REF_ARG:
01348       printf_filtered (_("a reference argument at offset %ld"), val);
01349       break;
01350 
01351     case LOC_TYPEDEF:
01352       printf_filtered (_("a typedef"));
01353       break;
01354 
01355     case LOC_BLOCK:
01356       printf_filtered (_("a function at address "));
01357       load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
01358       fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01359       if (section_is_overlay (section))
01360         {
01361           load_addr = overlay_unmapped_address (load_addr, section);
01362           printf_filtered (_(",\n -- loaded at "));
01363           fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01364           printf_filtered (_(" in overlay section %s"),
01365                            section->the_bfd_section->name);
01366         }
01367       break;
01368 
01369     case LOC_UNRESOLVED:
01370       {
01371         struct bound_minimal_symbol msym;
01372 
01373         msym = lookup_minimal_symbol_and_objfile (SYMBOL_LINKAGE_NAME (sym));
01374         if (msym.minsym == NULL)
01375           printf_filtered ("unresolved");
01376         else
01377           {
01378             section = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
01379             load_addr = SYMBOL_VALUE_ADDRESS (msym.minsym);
01380 
01381             if (section
01382                 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
01383               printf_filtered (_("a thread-local variable at offset %s "
01384                                  "in the thread-local storage for `%s'"),
01385                                paddress (gdbarch, load_addr),
01386                                objfile_name (section->objfile));
01387             else
01388               {
01389                 printf_filtered (_("static storage at address "));
01390                 fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01391                 if (section_is_overlay (section))
01392                   {
01393                     load_addr = overlay_unmapped_address (load_addr, section);
01394                     printf_filtered (_(",\n -- loaded at "));
01395                     fputs_filtered (paddress (gdbarch, load_addr), gdb_stdout);
01396                     printf_filtered (_(" in overlay section %s"),
01397                                      section->the_bfd_section->name);
01398                   }
01399               }
01400           }
01401       }
01402       break;
01403 
01404     case LOC_OPTIMIZED_OUT:
01405       printf_filtered (_("optimized out"));
01406       break;
01407 
01408     default:
01409       printf_filtered (_("of unknown (botched) type"));
01410       break;
01411     }
01412   printf_filtered (".\n");
01413 }
01414 
01415 
01416 static void
01417 x_command (char *exp, int from_tty)
01418 {
01419   struct expression *expr;
01420   struct format_data fmt;
01421   struct cleanup *old_chain;
01422   struct value *val;
01423 
01424   fmt.format = last_format ? last_format : 'x';
01425   fmt.size = last_size;
01426   fmt.count = 1;
01427   fmt.raw = 0;
01428 
01429   if (exp && *exp == '/')
01430     {
01431       const char *tmp = exp + 1;
01432 
01433       fmt = decode_format (&tmp, last_format, last_size);
01434       exp = (char *) tmp;
01435     }
01436 
01437   /* If we have an expression, evaluate it and use it as the address.  */
01438 
01439   if (exp != 0 && *exp != 0)
01440     {
01441       expr = parse_expression (exp);
01442       /* Cause expression not to be there any more if this command is
01443          repeated with Newline.  But don't clobber a user-defined
01444          command's definition.  */
01445       if (from_tty)
01446         *exp = 0;
01447       old_chain = make_cleanup (free_current_contents, &expr);
01448       val = evaluate_expression (expr);
01449       if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
01450         val = coerce_ref (val);
01451       /* In rvalue contexts, such as this, functions are coerced into
01452          pointers to functions.  This makes "x/i main" work.  */
01453       if (/* last_format == 'i'  && */ 
01454           TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
01455            && VALUE_LVAL (val) == lval_memory)
01456         next_address = value_address (val);
01457       else
01458         next_address = value_as_address (val);
01459 
01460       next_gdbarch = expr->gdbarch;
01461       do_cleanups (old_chain);
01462     }
01463 
01464   if (!next_gdbarch)
01465     error_no_arg (_("starting display address"));
01466 
01467   do_examine (fmt, next_gdbarch, next_address);
01468 
01469   /* If the examine succeeds, we remember its size and format for next
01470      time.  Set last_size to 'b' for strings.  */
01471   if (fmt.format == 's')
01472     last_size = 'b';
01473   else
01474     last_size = fmt.size;
01475   last_format = fmt.format;
01476 
01477   /* Set a couple of internal variables if appropriate.  */
01478   if (last_examine_value)
01479     {
01480       /* Make last address examined available to the user as $_.  Use
01481          the correct pointer type.  */
01482       struct type *pointer_type
01483         = lookup_pointer_type (value_type (last_examine_value));
01484       set_internalvar (lookup_internalvar ("_"),
01485                        value_from_pointer (pointer_type,
01486                                            last_examine_address));
01487 
01488       /* Make contents of last address examined available to the user
01489          as $__.  If the last value has not been fetched from memory
01490          then don't fetch it now; instead mark it by voiding the $__
01491          variable.  */
01492       if (value_lazy (last_examine_value))
01493         clear_internalvar (lookup_internalvar ("__"));
01494       else
01495         set_internalvar (lookup_internalvar ("__"), last_examine_value);
01496     }
01497 }
01498 
01499 
01500 /* Add an expression to the auto-display chain.
01501    Specify the expression.  */
01502 
01503 static void
01504 display_command (char *arg, int from_tty)
01505 {
01506   struct format_data fmt;
01507   struct expression *expr;
01508   struct display *new;
01509   int display_it = 1;
01510   const char *exp = arg;
01511 
01512 #if defined(TUI)
01513   /* NOTE: cagney/2003-02-13 The `tui_active' was previously
01514      `tui_version'.  */
01515   if (tui_active && exp != NULL && *exp == '$')
01516     display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
01517 #endif
01518 
01519   if (display_it)
01520     {
01521       if (exp == 0)
01522         {
01523           do_displays ();
01524           return;
01525         }
01526 
01527       if (*exp == '/')
01528         {
01529           exp++;
01530           fmt = decode_format (&exp, 0, 0);
01531           if (fmt.size && fmt.format == 0)
01532             fmt.format = 'x';
01533           if (fmt.format == 'i' || fmt.format == 's')
01534             fmt.size = 'b';
01535         }
01536       else
01537         {
01538           fmt.format = 0;
01539           fmt.size = 0;
01540           fmt.count = 0;
01541           fmt.raw = 0;
01542         }
01543 
01544       innermost_block = NULL;
01545       expr = parse_expression (exp);
01546 
01547       new = (struct display *) xmalloc (sizeof (struct display));
01548 
01549       new->exp_string = xstrdup (exp);
01550       new->exp = expr;
01551       new->block = innermost_block;
01552       new->pspace = current_program_space;
01553       new->next = display_chain;
01554       new->number = ++display_number;
01555       new->format = fmt;
01556       new->enabled_p = 1;
01557       display_chain = new;
01558 
01559       if (from_tty && target_has_execution)
01560         do_one_display (new);
01561 
01562       dont_repeat ();
01563     }
01564 }
01565 
01566 static void
01567 free_display (struct display *d)
01568 {
01569   xfree (d->exp_string);
01570   xfree (d->exp);
01571   xfree (d);
01572 }
01573 
01574 /* Clear out the display_chain.  Done when new symtabs are loaded,
01575    since this invalidates the types stored in many expressions.  */
01576 
01577 void
01578 clear_displays (void)
01579 {
01580   struct display *d;
01581 
01582   while ((d = display_chain) != NULL)
01583     {
01584       display_chain = d->next;
01585       free_display (d);
01586     }
01587 }
01588 
01589 /* Delete the auto-display DISPLAY.  */
01590 
01591 static void
01592 delete_display (struct display *display)
01593 {
01594   struct display *d;
01595 
01596   gdb_assert (display != NULL);
01597 
01598   if (display_chain == display)
01599     display_chain = display->next;
01600 
01601   ALL_DISPLAYS (d)
01602     if (d->next == display)
01603       {
01604         d->next = display->next;
01605         break;
01606       }
01607 
01608   free_display (display);
01609 }
01610 
01611 /* Call FUNCTION on each of the displays whose numbers are given in
01612    ARGS.  DATA is passed unmodified to FUNCTION.  */
01613 
01614 static void
01615 map_display_numbers (char *args,
01616                      void (*function) (struct display *,
01617                                        void *),
01618                      void *data)
01619 {
01620   struct get_number_or_range_state state;
01621   int num;
01622 
01623   if (args == NULL)
01624     error_no_arg (_("one or more display numbers"));
01625 
01626   init_number_or_range (&state, args);
01627 
01628   while (!state.finished)
01629     {
01630       char *p = state.string;
01631 
01632       num = get_number_or_range (&state);
01633       if (num == 0)
01634         warning (_("bad display number at or near '%s'"), p);
01635       else
01636         {
01637           struct display *d, *tmp;
01638 
01639           ALL_DISPLAYS_SAFE (d, tmp)
01640             if (d->number == num)
01641               break;
01642           if (d == NULL)
01643             printf_unfiltered (_("No display number %d.\n"), num);
01644           else
01645             function (d, data);
01646         }
01647     }
01648 }
01649 
01650 /* Callback for map_display_numbers, that deletes a display.  */
01651 
01652 static void
01653 do_delete_display (struct display *d, void *data)
01654 {
01655   delete_display (d);
01656 }
01657 
01658 /* "undisplay" command.  */
01659 
01660 static void
01661 undisplay_command (char *args, int from_tty)
01662 {
01663   if (args == NULL)
01664     {
01665       if (query (_("Delete all auto-display expressions? ")))
01666         clear_displays ();
01667       dont_repeat ();
01668       return;
01669     }
01670 
01671   map_display_numbers (args, do_delete_display, NULL);
01672   dont_repeat ();
01673 }
01674 
01675 /* Display a single auto-display.  
01676    Do nothing if the display cannot be printed in the current context,
01677    or if the display is disabled.  */
01678 
01679 static void
01680 do_one_display (struct display *d)
01681 {
01682   struct cleanup *old_chain;
01683   int within_current_scope;
01684 
01685   if (d->enabled_p == 0)
01686     return;
01687 
01688   /* The expression carries the architecture that was used at parse time.
01689      This is a problem if the expression depends on architecture features
01690      (e.g. register numbers), and the current architecture is now different.
01691      For example, a display statement like "display/i $pc" is expected to
01692      display the PC register of the current architecture, not the arch at
01693      the time the display command was given.  Therefore, we re-parse the
01694      expression if the current architecture has changed.  */
01695   if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
01696     {
01697       xfree (d->exp);
01698       d->exp = NULL;
01699       d->block = NULL;
01700     }
01701 
01702   if (d->exp == NULL)
01703     {
01704       volatile struct gdb_exception ex;
01705 
01706       TRY_CATCH (ex, RETURN_MASK_ALL)
01707         {
01708           innermost_block = NULL;
01709           d->exp = parse_expression (d->exp_string);
01710           d->block = innermost_block;
01711         }
01712       if (ex.reason < 0)
01713         {
01714           /* Can't re-parse the expression.  Disable this display item.  */
01715           d->enabled_p = 0;
01716           warning (_("Unable to display \"%s\": %s"),
01717                    d->exp_string, ex.message);
01718           return;
01719         }
01720     }
01721 
01722   if (d->block)
01723     {
01724       if (d->pspace == current_program_space)
01725         within_current_scope = contained_in (get_selected_block (0), d->block);
01726       else
01727         within_current_scope = 0;
01728     }
01729   else
01730     within_current_scope = 1;
01731   if (!within_current_scope)
01732     return;
01733 
01734   old_chain = make_cleanup_restore_integer (&current_display_number);
01735   current_display_number = d->number;
01736 
01737   annotate_display_begin ();
01738   printf_filtered ("%d", d->number);
01739   annotate_display_number_end ();
01740   printf_filtered (": ");
01741   if (d->format.size)
01742     {
01743       volatile struct gdb_exception ex;
01744 
01745       annotate_display_format ();
01746 
01747       printf_filtered ("x/");
01748       if (d->format.count != 1)
01749         printf_filtered ("%d", d->format.count);
01750       printf_filtered ("%c", d->format.format);
01751       if (d->format.format != 'i' && d->format.format != 's')
01752         printf_filtered ("%c", d->format.size);
01753       printf_filtered (" ");
01754 
01755       annotate_display_expression ();
01756 
01757       puts_filtered (d->exp_string);
01758       annotate_display_expression_end ();
01759 
01760       if (d->format.count != 1 || d->format.format == 'i')
01761         printf_filtered ("\n");
01762       else
01763         printf_filtered ("  ");
01764 
01765       annotate_display_value ();
01766 
01767       TRY_CATCH (ex, RETURN_MASK_ERROR)
01768         {
01769           struct value *val;
01770           CORE_ADDR addr;
01771 
01772           val = evaluate_expression (d->exp);
01773           addr = value_as_address (val);
01774           if (d->format.format == 'i')
01775             addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
01776           do_examine (d->format, d->exp->gdbarch, addr);
01777         }
01778       if (ex.reason < 0)
01779         fprintf_filtered (gdb_stdout, _("<error: %s>\n"), ex.message);
01780     }
01781   else
01782     {
01783       struct value_print_options opts;
01784       volatile struct gdb_exception ex;
01785 
01786       annotate_display_format ();
01787 
01788       if (d->format.format)
01789         printf_filtered ("/%c ", d->format.format);
01790 
01791       annotate_display_expression ();
01792 
01793       puts_filtered (d->exp_string);
01794       annotate_display_expression_end ();
01795 
01796       printf_filtered (" = ");
01797 
01798       annotate_display_expression ();
01799 
01800       get_formatted_print_options (&opts, d->format.format);
01801       opts.raw = d->format.raw;
01802 
01803       TRY_CATCH (ex, RETURN_MASK_ERROR)
01804         {
01805           struct value *val;
01806 
01807           val = evaluate_expression (d->exp);
01808           print_formatted (val, d->format.size, &opts, gdb_stdout);
01809         }
01810       if (ex.reason < 0)
01811         fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
01812       printf_filtered ("\n");
01813     }
01814 
01815   annotate_display_end ();
01816 
01817   gdb_flush (gdb_stdout);
01818   do_cleanups (old_chain);
01819 }
01820 
01821 /* Display all of the values on the auto-display chain which can be
01822    evaluated in the current scope.  */
01823 
01824 void
01825 do_displays (void)
01826 {
01827   struct display *d;
01828 
01829   for (d = display_chain; d; d = d->next)
01830     do_one_display (d);
01831 }
01832 
01833 /* Delete the auto-display which we were in the process of displaying.
01834    This is done when there is an error or a signal.  */
01835 
01836 void
01837 disable_display (int num)
01838 {
01839   struct display *d;
01840 
01841   for (d = display_chain; d; d = d->next)
01842     if (d->number == num)
01843       {
01844         d->enabled_p = 0;
01845         return;
01846       }
01847   printf_unfiltered (_("No display number %d.\n"), num);
01848 }
01849 
01850 void
01851 disable_current_display (void)
01852 {
01853   if (current_display_number >= 0)
01854     {
01855       disable_display (current_display_number);
01856       fprintf_unfiltered (gdb_stderr,
01857                           _("Disabling display %d to "
01858                             "avoid infinite recursion.\n"),
01859                           current_display_number);
01860     }
01861   current_display_number = -1;
01862 }
01863 
01864 static void
01865 display_info (char *ignore, int from_tty)
01866 {
01867   struct display *d;
01868 
01869   if (!display_chain)
01870     printf_unfiltered (_("There are no auto-display expressions now.\n"));
01871   else
01872     printf_filtered (_("Auto-display expressions now in effect:\n\
01873 Num Enb Expression\n"));
01874 
01875   for (d = display_chain; d; d = d->next)
01876     {
01877       printf_filtered ("%d:   %c  ", d->number, "ny"[(int) d->enabled_p]);
01878       if (d->format.size)
01879         printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
01880                          d->format.format);
01881       else if (d->format.format)
01882         printf_filtered ("/%c ", d->format.format);
01883       puts_filtered (d->exp_string);
01884       if (d->block && !contained_in (get_selected_block (0), d->block))
01885         printf_filtered (_(" (cannot be evaluated in the current context)"));
01886       printf_filtered ("\n");
01887       gdb_flush (gdb_stdout);
01888     }
01889 }
01890 
01891 /* Callback fo map_display_numbers, that enables or disables the
01892    passed in display D.  */
01893 
01894 static void
01895 do_enable_disable_display (struct display *d, void *data)
01896 {
01897   d->enabled_p = *(int *) data;
01898 }
01899 
01900 /* Implamentation of both the "disable display" and "enable display"
01901    commands.  ENABLE decides what to do.  */
01902 
01903 static void
01904 enable_disable_display_command (char *args, int from_tty, int enable)
01905 {
01906   if (args == NULL)
01907     {
01908       struct display *d;
01909 
01910       ALL_DISPLAYS (d)
01911         d->enabled_p = enable;
01912       return;
01913     }
01914 
01915   map_display_numbers (args, do_enable_disable_display, &enable);
01916 }
01917 
01918 /* The "enable display" command.  */
01919 
01920 static void
01921 enable_display_command (char *args, int from_tty)
01922 {
01923   enable_disable_display_command (args, from_tty, 1);
01924 }
01925 
01926 /* The "disable display" command.  */
01927 
01928 static void
01929 disable_display_command (char *args, int from_tty)
01930 {
01931   enable_disable_display_command (args, from_tty, 0);
01932 }
01933 
01934 /* display_chain items point to blocks and expressions.  Some expressions in
01935    turn may point to symbols.
01936    Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
01937    obstack_free'd when a shared library is unloaded.
01938    Clear pointers that are about to become dangling.
01939    Both .exp and .block fields will be restored next time we need to display
01940    an item by re-parsing .exp_string field in the new execution context.  */
01941 
01942 static void
01943 clear_dangling_display_expressions (struct so_list *solib)
01944 {
01945   struct objfile *objfile = solib->objfile;
01946   struct display *d;
01947 
01948   /* With no symbol file we cannot have a block or expression from it.  */
01949   if (objfile == NULL)
01950     return;
01951   if (objfile->separate_debug_objfile_backlink)
01952     objfile = objfile->separate_debug_objfile_backlink;
01953   gdb_assert (objfile->pspace == solib->pspace);
01954 
01955   for (d = display_chain; d != NULL; d = d->next)
01956     {
01957       if (d->pspace != solib->pspace)
01958         continue;
01959 
01960       if (lookup_objfile_from_block (d->block) == objfile
01961           || (d->exp && exp_uses_objfile (d->exp, objfile)))
01962       {
01963         xfree (d->exp);
01964         d->exp = NULL;
01965         d->block = NULL;
01966       }
01967     }
01968 }
01969 
01970 
01971 /* Print the value in stack frame FRAME of a variable specified by a
01972    struct symbol.  NAME is the name to print; if NULL then VAR's print
01973    name will be used.  STREAM is the ui_file on which to print the
01974    value.  INDENT specifies the number of indent levels to print
01975    before printing the variable name.
01976 
01977    This function invalidates FRAME.  */
01978 
01979 void
01980 print_variable_and_value (const char *name, struct symbol *var,
01981                           struct frame_info *frame,
01982                           struct ui_file *stream, int indent)
01983 {
01984   volatile struct gdb_exception except;
01985 
01986   if (!name)
01987     name = SYMBOL_PRINT_NAME (var);
01988 
01989   fprintf_filtered (stream, "%s%s = ", n_spaces (2 * indent), name);
01990   TRY_CATCH (except, RETURN_MASK_ERROR)
01991     {
01992       struct value *val;
01993       struct value_print_options opts;
01994 
01995       val = read_var_value (var, frame);
01996       get_user_print_options (&opts);
01997       opts.deref_ref = 1;
01998       common_val_print (val, stream, indent, &opts, current_language);
01999 
02000       /* common_val_print invalidates FRAME when a pretty printer calls inferior
02001          function.  */
02002       frame = NULL;
02003     }
02004   if (except.reason < 0)
02005     fprintf_filtered(stream, "<error reading variable %s (%s)>", name,
02006                      except.message);
02007   fprintf_filtered (stream, "\n");
02008 }
02009 
02010 /* Subroutine of ui_printf to simplify it.
02011    Print VALUE to STREAM using FORMAT.
02012    VALUE is a C-style string on the target.  */
02013 
02014 static void
02015 printf_c_string (struct ui_file *stream, const char *format,
02016                  struct value *value)
02017 {
02018   gdb_byte *str;
02019   CORE_ADDR tem;
02020   int j;
02021 
02022   tem = value_as_address (value);
02023 
02024   /* This is a %s argument.  Find the length of the string.  */
02025   for (j = 0;; j++)
02026     {
02027       gdb_byte c;
02028 
02029       QUIT;
02030       read_memory (tem + j, &c, 1);
02031       if (c == 0)
02032         break;
02033     }
02034 
02035   /* Copy the string contents into a string inside GDB.  */
02036   str = (gdb_byte *) alloca (j + 1);
02037   if (j != 0)
02038     read_memory (tem, str, j);
02039   str[j] = 0;
02040 
02041   fprintf_filtered (stream, format, (char *) str);
02042 }
02043 
02044 /* Subroutine of ui_printf to simplify it.
02045    Print VALUE to STREAM using FORMAT.
02046    VALUE is a wide C-style string on the target.  */
02047 
02048 static void
02049 printf_wide_c_string (struct ui_file *stream, const char *format,
02050                       struct value *value)
02051 {
02052   gdb_byte *str;
02053   CORE_ADDR tem;
02054   int j;
02055   struct gdbarch *gdbarch = get_type_arch (value_type (value));
02056   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02057   struct type *wctype = lookup_typename (current_language, gdbarch,
02058                                          "wchar_t", NULL, 0);
02059   int wcwidth = TYPE_LENGTH (wctype);
02060   gdb_byte *buf = alloca (wcwidth);
02061   struct obstack output;
02062   struct cleanup *inner_cleanup;
02063 
02064   tem = value_as_address (value);
02065 
02066   /* This is a %s argument.  Find the length of the string.  */
02067   for (j = 0;; j += wcwidth)
02068     {
02069       QUIT;
02070       read_memory (tem + j, buf, wcwidth);
02071       if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
02072         break;
02073     }
02074 
02075   /* Copy the string contents into a string inside GDB.  */
02076   str = (gdb_byte *) alloca (j + wcwidth);
02077   if (j != 0)
02078     read_memory (tem, str, j);
02079   memset (&str[j], 0, wcwidth);
02080 
02081   obstack_init (&output);
02082   inner_cleanup = make_cleanup_obstack_free (&output);
02083 
02084   convert_between_encodings (target_wide_charset (gdbarch),
02085                              host_charset (),
02086                              str, j, wcwidth,
02087                              &output, translit_char);
02088   obstack_grow_str0 (&output, "");
02089 
02090   fprintf_filtered (stream, format, obstack_base (&output));
02091   do_cleanups (inner_cleanup);
02092 }
02093 
02094 /* Subroutine of ui_printf to simplify it.
02095    Print VALUE, a decimal floating point value, to STREAM using FORMAT.  */
02096 
02097 static void
02098 printf_decfloat (struct ui_file *stream, const char *format,
02099                  struct value *value)
02100 {
02101   const gdb_byte *param_ptr = value_contents (value);
02102 
02103 #if defined (PRINTF_HAS_DECFLOAT)
02104   /* If we have native support for Decimal floating
02105      printing, handle it here.  */
02106   fprintf_filtered (stream, format, param_ptr);
02107 #else
02108   /* As a workaround until vasprintf has native support for DFP
02109      we convert the DFP values to string and print them using
02110      the %s format specifier.  */
02111   const char *p;
02112 
02113   /* Parameter data.  */
02114   struct type *param_type = value_type (value);
02115   struct gdbarch *gdbarch = get_type_arch (param_type);
02116   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02117 
02118   /* DFP output data.  */
02119   struct value *dfp_value = NULL;
02120   gdb_byte *dfp_ptr;
02121   int dfp_len = 16;
02122   gdb_byte dec[16];
02123   struct type *dfp_type = NULL;
02124   char decstr[MAX_DECIMAL_STRING];
02125 
02126   /* Points to the end of the string so that we can go back
02127      and check for DFP length modifiers.  */
02128   p = format + strlen (format);
02129 
02130   /* Look for the float/double format specifier.  */
02131   while (*p != 'f' && *p != 'e' && *p != 'E'
02132          && *p != 'g' && *p != 'G')
02133     p--;
02134 
02135   /* Search for the '%' char and extract the size and type of
02136      the output decimal value based on its modifiers
02137      (%Hf, %Df, %DDf).  */
02138   while (*--p != '%')
02139     {
02140       if (*p == 'H')
02141         {
02142           dfp_len = 4;
02143           dfp_type = builtin_type (gdbarch)->builtin_decfloat;
02144         }
02145       else if (*p == 'D' && *(p - 1) == 'D')
02146         {
02147           dfp_len = 16;
02148           dfp_type = builtin_type (gdbarch)->builtin_declong;
02149           p--;
02150         }
02151       else
02152         {
02153           dfp_len = 8;
02154           dfp_type = builtin_type (gdbarch)->builtin_decdouble;
02155         }
02156     }
02157 
02158   /* Conversion between different DFP types.  */
02159   if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
02160     decimal_convert (param_ptr, TYPE_LENGTH (param_type),
02161                      byte_order, dec, dfp_len, byte_order);
02162   else
02163     /* If this is a non-trivial conversion, just output 0.
02164        A correct converted value can be displayed by explicitly
02165        casting to a DFP type.  */
02166     decimal_from_string (dec, dfp_len, byte_order, "0");
02167 
02168   dfp_value = value_from_decfloat (dfp_type, dec);
02169 
02170   dfp_ptr = (gdb_byte *) value_contents (dfp_value);
02171 
02172   decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr);
02173 
02174   /* Print the DFP value.  */
02175   fprintf_filtered (stream, "%s", decstr);
02176 #endif
02177 }
02178 
02179 /* Subroutine of ui_printf to simplify it.
02180    Print VALUE, a target pointer, to STREAM using FORMAT.  */
02181 
02182 static void
02183 printf_pointer (struct ui_file *stream, const char *format,
02184                 struct value *value)
02185 {
02186   /* We avoid the host's %p because pointers are too
02187      likely to be the wrong size.  The only interesting
02188      modifier for %p is a width; extract that, and then
02189      handle %p as glibc would: %#x or a literal "(nil)".  */
02190 
02191   const char *p;
02192   char *fmt, *fmt_p;
02193 #ifdef PRINTF_HAS_LONG_LONG
02194   long long val = value_as_long (value);
02195 #else
02196   long val = value_as_long (value);
02197 #endif
02198 
02199   fmt = alloca (strlen (format) + 5);
02200 
02201   /* Copy up to the leading %.  */
02202   p = format;
02203   fmt_p = fmt;
02204   while (*p)
02205     {
02206       int is_percent = (*p == '%');
02207 
02208       *fmt_p++ = *p++;
02209       if (is_percent)
02210         {
02211           if (*p == '%')
02212             *fmt_p++ = *p++;
02213           else
02214             break;
02215         }
02216     }
02217 
02218   if (val != 0)
02219     *fmt_p++ = '#';
02220 
02221   /* Copy any width.  */
02222   while (*p >= '0' && *p < '9')
02223     *fmt_p++ = *p++;
02224 
02225   gdb_assert (*p == 'p' && *(p + 1) == '\0');
02226   if (val != 0)
02227     {
02228 #ifdef PRINTF_HAS_LONG_LONG
02229       *fmt_p++ = 'l';
02230 #endif
02231       *fmt_p++ = 'l';
02232       *fmt_p++ = 'x';
02233       *fmt_p++ = '\0';
02234       fprintf_filtered (stream, fmt, val);
02235     }
02236   else
02237     {
02238       *fmt_p++ = 's';
02239       *fmt_p++ = '\0';
02240       fprintf_filtered (stream, fmt, "(nil)");
02241     }
02242 }
02243 
02244 /* printf "printf format string" ARG to STREAM.  */
02245 
02246 static void
02247 ui_printf (const char *arg, struct ui_file *stream)
02248 {
02249   struct format_piece *fpieces;
02250   const char *s = arg;
02251   struct value **val_args;
02252   int allocated_args = 20;
02253   struct cleanup *old_cleanups;
02254 
02255   val_args = xmalloc (allocated_args * sizeof (struct value *));
02256   old_cleanups = make_cleanup (free_current_contents, &val_args);
02257 
02258   if (s == 0)
02259     error_no_arg (_("format-control string and values to print"));
02260 
02261   s = skip_spaces_const (s);
02262 
02263   /* A format string should follow, enveloped in double quotes.  */
02264   if (*s++ != '"')
02265     error (_("Bad format string, missing '\"'."));
02266 
02267   fpieces = parse_format_string (&s);
02268 
02269   make_cleanup (free_format_pieces_cleanup, &fpieces);
02270 
02271   if (*s++ != '"')
02272     error (_("Bad format string, non-terminated '\"'."));
02273   
02274   s = skip_spaces_const (s);
02275 
02276   if (*s != ',' && *s != 0)
02277     error (_("Invalid argument syntax"));
02278 
02279   if (*s == ',')
02280     s++;
02281   s = skip_spaces_const (s);
02282 
02283   {
02284     int nargs = 0;
02285     int nargs_wanted;
02286     int i, fr;
02287     char *current_substring;
02288 
02289     nargs_wanted = 0;
02290     for (fr = 0; fpieces[fr].string != NULL; fr++)
02291       if (fpieces[fr].argclass != literal_piece)
02292         ++nargs_wanted;
02293 
02294     /* Now, parse all arguments and evaluate them.
02295        Store the VALUEs in VAL_ARGS.  */
02296 
02297     while (*s != '\0')
02298       {
02299         const char *s1;
02300 
02301         if (nargs == allocated_args)
02302           val_args = (struct value **) xrealloc ((char *) val_args,
02303                                                  (allocated_args *= 2)
02304                                                  * sizeof (struct value *));
02305         s1 = s;
02306         val_args[nargs] = parse_to_comma_and_eval (&s1);
02307 
02308         nargs++;
02309         s = s1;
02310         if (*s == ',')
02311           s++;
02312       }
02313 
02314     if (nargs != nargs_wanted)
02315       error (_("Wrong number of arguments for specified format-string"));
02316 
02317     /* Now actually print them.  */
02318     i = 0;
02319     for (fr = 0; fpieces[fr].string != NULL; fr++)
02320       {
02321         current_substring = fpieces[fr].string;
02322         switch (fpieces[fr].argclass)
02323           {
02324           case string_arg:
02325             printf_c_string (stream, current_substring, val_args[i]);
02326             break;
02327           case wide_string_arg:
02328             printf_wide_c_string (stream, current_substring, val_args[i]);
02329             break;
02330           case wide_char_arg:
02331             {
02332               struct gdbarch *gdbarch
02333                 = get_type_arch (value_type (val_args[i]));
02334               struct type *wctype = lookup_typename (current_language, gdbarch,
02335                                                      "wchar_t", NULL, 0);
02336               struct type *valtype;
02337               struct obstack output;
02338               struct cleanup *inner_cleanup;
02339               const gdb_byte *bytes;
02340 
02341               valtype = value_type (val_args[i]);
02342               if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
02343                   || TYPE_CODE (valtype) != TYPE_CODE_INT)
02344                 error (_("expected wchar_t argument for %%lc"));
02345 
02346               bytes = value_contents (val_args[i]);
02347 
02348               obstack_init (&output);
02349               inner_cleanup = make_cleanup_obstack_free (&output);
02350 
02351               convert_between_encodings (target_wide_charset (gdbarch),
02352                                          host_charset (),
02353                                          bytes, TYPE_LENGTH (valtype),
02354                                          TYPE_LENGTH (valtype),
02355                                          &output, translit_char);
02356               obstack_grow_str0 (&output, "");
02357 
02358               fprintf_filtered (stream, current_substring,
02359                                 obstack_base (&output));
02360               do_cleanups (inner_cleanup);
02361             }
02362             break;
02363           case double_arg:
02364             {
02365               struct type *type = value_type (val_args[i]);
02366               DOUBLEST val;
02367               int inv;
02368 
02369               /* If format string wants a float, unchecked-convert the value
02370                  to floating point of the same size.  */
02371               type = float_type_from_length (type);
02372               val = unpack_double (type, value_contents (val_args[i]), &inv);
02373               if (inv)
02374                 error (_("Invalid floating value found in program."));
02375 
02376               fprintf_filtered (stream, current_substring, (double) val);
02377               break;
02378             }
02379           case long_double_arg:
02380 #ifdef HAVE_LONG_DOUBLE
02381             {
02382               struct type *type = value_type (val_args[i]);
02383               DOUBLEST val;
02384               int inv;
02385 
02386               /* If format string wants a float, unchecked-convert the value
02387                  to floating point of the same size.  */
02388               type = float_type_from_length (type);
02389               val = unpack_double (type, value_contents (val_args[i]), &inv);
02390               if (inv)
02391                 error (_("Invalid floating value found in program."));
02392 
02393               fprintf_filtered (stream, current_substring,
02394                                 (long double) val);
02395               break;
02396             }
02397 #else
02398             error (_("long double not supported in printf"));
02399 #endif
02400           case long_long_arg:
02401 #ifdef PRINTF_HAS_LONG_LONG
02402             {
02403               long long val = value_as_long (val_args[i]);
02404 
02405               fprintf_filtered (stream, current_substring, val);
02406               break;
02407             }
02408 #else
02409             error (_("long long not supported in printf"));
02410 #endif
02411           case int_arg:
02412             {
02413               int val = value_as_long (val_args[i]);
02414 
02415               fprintf_filtered (stream, current_substring, val);
02416               break;
02417             }
02418           case long_arg:
02419             {
02420               long val = value_as_long (val_args[i]);
02421 
02422               fprintf_filtered (stream, current_substring, val);
02423               break;
02424             }
02425           /* Handles decimal floating values.  */
02426           case decfloat_arg:
02427             printf_decfloat (stream, current_substring, val_args[i]);
02428             break;
02429           case ptr_arg:
02430             printf_pointer (stream, current_substring, val_args[i]);
02431             break;
02432           case literal_piece:
02433             /* Print a portion of the format string that has no
02434                directives.  Note that this will not include any
02435                ordinary %-specs, but it might include "%%".  That is
02436                why we use printf_filtered and not puts_filtered here.
02437                Also, we pass a dummy argument because some platforms
02438                have modified GCC to include -Wformat-security by
02439                default, which will warn here if there is no
02440                argument.  */
02441             fprintf_filtered (stream, current_substring, 0);
02442             break;
02443           default:
02444             internal_error (__FILE__, __LINE__,
02445                             _("failed internal consistency check"));
02446           }
02447         /* Maybe advance to the next argument.  */
02448         if (fpieces[fr].argclass != literal_piece)
02449           ++i;
02450       }
02451   }
02452   do_cleanups (old_cleanups);
02453 }
02454 
02455 /* Implement the "printf" command.  */
02456 
02457 static void
02458 printf_command (char *arg, int from_tty)
02459 {
02460   ui_printf (arg, gdb_stdout);
02461 }
02462 
02463 /* Implement the "eval" command.  */
02464 
02465 static void
02466 eval_command (char *arg, int from_tty)
02467 {
02468   struct ui_file *ui_out = mem_fileopen ();
02469   struct cleanup *cleanups = make_cleanup_ui_file_delete (ui_out);
02470   char *expanded;
02471 
02472   ui_printf (arg, ui_out);
02473 
02474   expanded = ui_file_xstrdup (ui_out, NULL);
02475   make_cleanup (xfree, expanded);
02476 
02477   execute_command (expanded, from_tty);
02478 
02479   do_cleanups (cleanups);
02480 }
02481 
02482 void
02483 _initialize_printcmd (void)
02484 {
02485   struct cmd_list_element *c;
02486 
02487   current_display_number = -1;
02488 
02489   observer_attach_solib_unloaded (clear_dangling_display_expressions);
02490 
02491   add_info ("address", address_info,
02492             _("Describe where symbol SYM is stored."));
02493 
02494   add_info ("symbol", sym_info, _("\
02495 Describe what symbol is at location ADDR.\n\
02496 Only for symbols with fixed locations (global or static scope)."));
02497 
02498   add_com ("x", class_vars, x_command, _("\
02499 Examine memory: x/FMT ADDRESS.\n\
02500 ADDRESS is an expression for the memory address to examine.\n\
02501 FMT is a repeat count followed by a format letter and a size letter.\n\
02502 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
02503   t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
02504   and z(hex, zero padded on the left).\n\
02505 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
02506 The specified number of objects of the specified size are printed\n\
02507 according to the format.\n\n\
02508 Defaults for format and size letters are those previously used.\n\
02509 Default count is 1.  Default address is following last thing printed\n\
02510 with this command or \"print\"."));
02511 
02512 #if 0
02513   add_com ("whereis", class_vars, whereis_command,
02514            _("Print line number and file of definition of variable."));
02515 #endif
02516 
02517   add_info ("display", display_info, _("\
02518 Expressions to display when program stops, with code numbers."));
02519 
02520   add_cmd ("undisplay", class_vars, undisplay_command, _("\
02521 Cancel some expressions to be displayed when program stops.\n\
02522 Arguments are the code numbers of the expressions to stop displaying.\n\
02523 No argument means cancel all automatic-display expressions.\n\
02524 \"delete display\" has the same effect as this command.\n\
02525 Do \"info display\" to see current list of code numbers."),
02526            &cmdlist);
02527 
02528   add_com ("display", class_vars, display_command, _("\
02529 Print value of expression EXP each time the program stops.\n\
02530 /FMT may be used before EXP as in the \"print\" command.\n\
02531 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
02532 as in the \"x\" command, and then EXP is used to get the address to examine\n\
02533 and examining is done as in the \"x\" command.\n\n\
02534 With no argument, display all currently requested auto-display expressions.\n\
02535 Use \"undisplay\" to cancel display requests previously made."));
02536 
02537   add_cmd ("display", class_vars, enable_display_command, _("\
02538 Enable some expressions to be displayed when program stops.\n\
02539 Arguments are the code numbers of the expressions to resume displaying.\n\
02540 No argument means enable all automatic-display expressions.\n\
02541 Do \"info display\" to see current list of code numbers."), &enablelist);
02542 
02543   add_cmd ("display", class_vars, disable_display_command, _("\
02544 Disable some expressions to be displayed when program stops.\n\
02545 Arguments are the code numbers of the expressions to stop displaying.\n\
02546 No argument means disable all automatic-display expressions.\n\
02547 Do \"info display\" to see current list of code numbers."), &disablelist);
02548 
02549   add_cmd ("display", class_vars, undisplay_command, _("\
02550 Cancel some expressions to be displayed when program stops.\n\
02551 Arguments are the code numbers of the expressions to stop displaying.\n\
02552 No argument means cancel all automatic-display expressions.\n\
02553 Do \"info display\" to see current list of code numbers."), &deletelist);
02554 
02555   add_com ("printf", class_vars, printf_command, _("\
02556 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
02557 This is useful for formatted output in user-defined commands."));
02558 
02559   add_com ("output", class_vars, output_command, _("\
02560 Like \"print\" but don't put in value history and don't print newline.\n\
02561 This is useful in user-defined commands."));
02562 
02563   add_prefix_cmd ("set", class_vars, set_command, _("\
02564 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
02565 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
02566 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
02567 with $), a register (a few standard names starting with $), or an actual\n\
02568 variable in the program being debugged.  EXP is any valid expression.\n\
02569 Use \"set variable\" for variables with names identical to set subcommands.\n\
02570 \n\
02571 With a subcommand, this command modifies parts of the gdb environment.\n\
02572 You can see these environment settings with the \"show\" command."),
02573                   &setlist, "set ", 1, &cmdlist);
02574   if (dbx_commands)
02575     add_com ("assign", class_vars, set_command, _("\
02576 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
02577 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
02578 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
02579 with $), a register (a few standard names starting with $), or an actual\n\
02580 variable in the program being debugged.  EXP is any valid expression.\n\
02581 Use \"set variable\" for variables with names identical to set subcommands.\n\
02582 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
02583 You can see these environment settings with the \"show\" command."));
02584 
02585   /* "call" is the same as "set", but handy for dbx users to call fns.  */
02586   c = add_com ("call", class_vars, call_command, _("\
02587 Call a function in the program.\n\
02588 The argument is the function name and arguments, in the notation of the\n\
02589 current working language.  The result is printed and saved in the value\n\
02590 history, if it is not void."));
02591   set_cmd_completer (c, expression_completer);
02592 
02593   add_cmd ("variable", class_vars, set_command, _("\
02594 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
02595 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
02596 example).  VAR may be a debugger \"convenience\" variable (names starting\n\
02597 with $), a register (a few standard names starting with $), or an actual\n\
02598 variable in the program being debugged.  EXP is any valid expression.\n\
02599 This may usually be abbreviated to simply \"set\"."),
02600            &setlist);
02601 
02602   c = add_com ("print", class_vars, print_command, _("\
02603 Print value of expression EXP.\n\
02604 Variables accessible are those of the lexical environment of the selected\n\
02605 stack frame, plus all those whose scope is global or an entire file.\n\
02606 \n\
02607 $NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
02608 $$NUM refers to NUM'th value back from the last one.\n\
02609 Names starting with $ refer to registers (with the values they would have\n\
02610 if the program were to return to the stack frame now selected, restoring\n\
02611 all registers saved by frames farther in) or else to debugger\n\
02612 \"convenience\" variables (any such name not a known register).\n\
02613 Use assignment expressions to give values to convenience variables.\n\
02614 \n\
02615 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
02616 @ is a binary operator for treating consecutive data objects\n\
02617 anywhere in memory as an array.  FOO@NUM gives an array whose first\n\
02618 element is FOO, whose second element is stored in the space following\n\
02619 where FOO is stored, etc.  FOO must be an expression whose value\n\
02620 resides in memory.\n\
02621 \n\
02622 EXP may be preceded with /FMT, where FMT is a format letter\n\
02623 but no count or size letter (see \"x\" command)."));
02624   set_cmd_completer (c, expression_completer);
02625   add_com_alias ("p", "print", class_vars, 1);
02626   add_com_alias ("inspect", "print", class_vars, 1);
02627 
02628   add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
02629                             &max_symbolic_offset, _("\
02630 Set the largest offset that will be printed in <symbol+1234> form."), _("\
02631 Show the largest offset that will be printed in <symbol+1234> form."), _("\
02632 Tell GDB to only display the symbolic form of an address if the\n\
02633 offset between the closest earlier symbol and the address is less than\n\
02634 the specified maximum offset.  The default is \"unlimited\", which tells GDB\n\
02635 to always print the symbolic form of an address if any symbol precedes\n\
02636 it.  Zero is equivalent to \"unlimited\"."),
02637                             NULL,
02638                             show_max_symbolic_offset,
02639                             &setprintlist, &showprintlist);
02640   add_setshow_boolean_cmd ("symbol-filename", no_class,
02641                            &print_symbol_filename, _("\
02642 Set printing of source filename and line number with <symbol>."), _("\
02643 Show printing of source filename and line number with <symbol>."), NULL,
02644                            NULL,
02645                            show_print_symbol_filename,
02646                            &setprintlist, &showprintlist);
02647 
02648   add_com ("eval", no_class, eval_command, _("\
02649 Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
02650 a command line, and call it."));
02651 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines