home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / gdb / printcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-08  |  46.5 KB  |  1,868 lines

  1. /*-
  2.  * This code is derived from software copyrighted by the Free Software
  3.  * Foundation.
  4.  *
  5.  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
  6.  * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
  7.  */
  8.  
  9. #ifndef lint
  10. static char sccsid[] = "@(#)printcmd.c    6.5 (Berkeley) 5/8/91";
  11. #endif /* not lint */
  12.  
  13. /* Print values for GNU debugger GDB.
  14.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  15.  
  16. This file is part of GDB.
  17.  
  18. GDB is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 1, or (at your option)
  21. any later version.
  22.  
  23. GDB is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. GNU General Public License for more details.
  27.  
  28. You should have received a copy of the GNU General Public License
  29. along with GDB; see the file COPYING.  If not, write to
  30. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  31.  
  32. #include <stdio.h>
  33. #include "defs.h"
  34. #include "param.h"
  35. #include "frame.h"
  36. #include "symtab.h"
  37. #include "value.h"
  38. #include "expression.h"
  39.  
  40. struct format_data
  41. {
  42.   int count;
  43.   char format;
  44.   char size;
  45. };
  46.  
  47. /* Last specified output format.  */
  48.  
  49. static char last_format = 'x';
  50.  
  51. /* Last specified examination size.  'b', 'h', 'w' or `q'.  */
  52.  
  53. static char last_size = 'w';
  54.  
  55. /* Default address to examine next.  */
  56.  
  57. static CORE_ADDR next_address;
  58.  
  59. /* Last address examined.  */
  60.  
  61. static CORE_ADDR last_examine_address;
  62.  
  63. /* Contents of last address examined.
  64.    This is not valid past the end of the `x' command!  */
  65.  
  66. static value last_examine_value;
  67.  
  68. /* Number of auto-display expression currently being displayed.
  69.    So that we can deleted it if we get an error or a signal within it.
  70.    -1 when not doing one.  */
  71.  
  72. int current_display_number;
  73.  
  74. static void do_one_display ();
  75.  
  76. void do_displays ();
  77. void print_address ();
  78. void print_floating ();
  79. void print_scalar_formatted ();
  80. void print_formatted_address ();
  81.  
  82.  
  83. /* Decode a format specification.  *STRING_PTR should point to it.
  84.    OFORMAT and OSIZE are used as defaults for the format and size
  85.    if none are given in the format specification.
  86.    If OSIZE is zero, then the size field of the returned value
  87.    should be set only if a size is explicitly specified by the
  88.    user.
  89.    The structure returned describes all the data
  90.    found in the specification.  In addition, *STRING_PTR is advanced
  91.    past the specification and past all whitespace following it.  */
  92.  
  93. struct format_data
  94. decode_format (string_ptr, oformat, osize)
  95.      char **string_ptr;
  96.      char oformat;
  97.      char osize;
  98. {
  99.   struct format_data val;
  100.   register char *p = *string_ptr;
  101.  
  102.   val.format = '?';
  103.   val.size = '?';
  104.   val.count = 1;
  105.  
  106.   if (*p >= '0' && *p <= '9')
  107.     val.count = atoi (p);
  108.   while (*p >= '0' && *p <= '9') p++;
  109.  
  110.   /* Now process size or format letters that follow.  */
  111.  
  112.   while (1)
  113.     {
  114.       if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
  115.     val.size = *p++;
  116. #ifdef LONG_LONG
  117.       else if (*p == 'l')
  118.     {
  119.       val.size = 'g';
  120.       p++;
  121.     }
  122. #endif
  123.       else if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z'))
  124.     val.format = *p++;
  125.       else
  126.     break;
  127.     }
  128.  
  129. #ifndef LONG_LONG
  130.   /* Make sure 'g' size is not used on integer types.
  131.      Well, actually, we can handle hex.  */
  132.   if (val.size == 'g' && val.format != 'f' && val.format != 'x')
  133.     val.size = 'w';
  134. #endif
  135.  
  136.   while (*p == ' ' || *p == '\t') p++;
  137.   *string_ptr = p;
  138.  
  139.   /* Set defaults for format and size if not specified.  */
  140.   if (val.format == '?')
  141.     {
  142.       if (val.size == '?')
  143.     {
  144.       /* Neither has been specified.  */
  145.       val.format = oformat;
  146.       val.size = osize;
  147.     }
  148.       else
  149.     /* If a size is specified, any format makes a reasonable
  150.        default except 'i'.  */
  151.     val.format = oformat == 'i' ? 'x' : oformat;
  152.     }
  153.   else if (val.size == '?')
  154.     switch (val.format)
  155.       {
  156.       case 'a':
  157.       case 's':
  158.       case 'A':
  159.     /* Addresses must be words.  */
  160.     val.size = osize ? 'w' : osize;
  161.     break;
  162.       case 'f':
  163.     /* Floating point has to be word or giantword.  */
  164.     if (osize == 'w' || osize == 'g')
  165.       val.size = osize;
  166.     else
  167.       /* Default it to giantword if the last used size is not
  168.          appropriate.  */
  169.       val.size = osize ? 'g' : osize;
  170.     break;
  171.       case 'c':
  172.     /* Characters default to one byte.  */
  173.     val.size = osize ? 'b' : osize;
  174.     break;
  175.       default:
  176.     /* The default is the size most recently specified.  */
  177.     val.size = osize;
  178.       }
  179.  
  180.   return val;
  181. }
  182.  
  183. /* Print value VAL on stdout according to FORMAT, a letter or 0.
  184.    Do not end with a newline.
  185.    0 means print VAL according to its own type.
  186.    SIZE is the letter for the size of datum being printed.
  187.    This is used to pad hex numbers so they line up.  */
  188.  
  189. static void
  190. print_formatted (val, format, size)
  191.      register value val;
  192.      register char format;
  193.      char size;
  194. {
  195.   int len = TYPE_LENGTH (VALUE_TYPE (val));
  196.  
  197.   if (VALUE_LVAL (val) == lval_memory)
  198.     next_address = VALUE_ADDRESS (val) + len;
  199.  
  200.   switch (format)
  201.     {
  202.     case 's':
  203.       next_address = VALUE_ADDRESS (val)
  204.     + value_print (value_addr (val), stdout, 0, Val_pretty_default);
  205.       break;
  206.  
  207.     case 'i':
  208.       next_address = VALUE_ADDRESS (val)
  209.     + print_insn (VALUE_ADDRESS (val), stdout);
  210.       break;
  211.  
  212.     default:
  213.       if (format == 0
  214.       || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_ARRAY
  215.       || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_STRUCT
  216.       || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_UNION
  217.       || VALUE_REPEATED (val))
  218.     value_print (val, stdout, format, Val_pretty_default);
  219.       else
  220.     print_scalar_formatted (VALUE_CONTENTS (val), VALUE_TYPE (val),
  221.                 format, size, stdout);
  222.     }
  223. }
  224.  
  225. /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
  226.    according to letters FORMAT and SIZE on STREAM.
  227.    FORMAT may not be zero.  Formats s and i are not supported at this level.
  228.  
  229.    This is how the elements of an array or structure are printed
  230.    with a format.  */
  231.  
  232. void
  233. print_scalar_formatted (valaddr, type, format, size, stream)
  234.      char *valaddr;
  235.      struct type *type;
  236.      char format;
  237.      int size;
  238.      FILE *stream;
  239. {
  240.   LONGEST val_long;
  241.   int len = TYPE_LENGTH (type);
  242.  
  243.   if (size == 'g' && sizeof (LONGEST) < 8
  244.       && format == 'x')
  245.     {
  246.       /* ok, we're going to have to get fancy here.  Assumption: a
  247.          long is four bytes.  */
  248.       unsigned long v1, v2, tmp;
  249.  
  250.       v1 = unpack_long (builtin_type_long, valaddr);
  251.       v2 = unpack_long (builtin_type_long, valaddr + 4);
  252.  
  253. #ifdef BYTES_BIG_ENDIAN
  254. #else
  255.       /* Little endian -- swap the two for printing */
  256.       tmp = v1;
  257.       v1 = v2;
  258.       v2 = tmp;
  259. #endif
  260.   
  261.       switch (format)
  262.     {
  263.     case 'x':
  264.       fprintf_filtered (stream, "0x%08x%08x", v1, v2);
  265.       break;
  266.     default:
  267.       error ("Output size \"g\" unimplemented for format \"%c\".",
  268.          format);
  269.     }
  270.       return;
  271.     }
  272.       
  273.   val_long = unpack_long (type, valaddr);
  274.  
  275.   /* If value is unsigned, truncate it in case negative.  */
  276.   if (format != 'd')
  277.     {
  278.       if (len == sizeof (char))
  279.     val_long &= (1 << 8 * sizeof(char)) - 1;
  280.       else if (len == sizeof (short))
  281.     val_long &= (1 << 8 * sizeof(short)) - 1;
  282.       else if (len == sizeof (long))
  283.     val_long &= (unsigned long) - 1;
  284.     }
  285.  
  286.   switch (format)
  287.     {
  288.     case 'x':
  289. #ifdef LONG_LONG
  290.       if (!size)
  291.     size = (len < sizeof (long long) ? 'w' : 'g');
  292.       switch (size)
  293.     {
  294.     case 'b':
  295.       fprintf_filtered (stream, "0x%02llx", val_long);
  296.       break;
  297.     case 'h':
  298.       fprintf_filtered (stream, "0x%04llx", val_long);
  299.       break;
  300.     case 0:        /* no size specified, like in print */
  301.     case 'w':
  302.       fprintf_filtered (stream, "0x%08llx", val_long);
  303.       break;
  304.     case 'g':
  305.       fprintf_filtered (stream, "0x%016llx", val_long);
  306.       break;
  307.     default:
  308.       error ("Undefined output size \"%c\".", size);
  309.     }
  310. #else 
  311.       switch (size)
  312.     {
  313.     case 'b':
  314.       fprintf_filtered (stream, "0x%02x", val_long);
  315.       break;
  316.     case 'h':
  317.       fprintf_filtered (stream, "0x%04x", val_long);
  318.       break;
  319.     case 0:        /* no size specified, like in print */
  320.     case 'w':
  321.       fprintf_filtered (stream, "0x%08x", val_long);
  322.       break;
  323.     case 'g':
  324.       fprintf_filtered (stream, "0x%o16x", val_long);
  325.       break;
  326.     default:
  327.       error ("Undefined output size \"%c\".", size);
  328.     }
  329. #endif /* not LONG_LONG */
  330.       break;
  331.  
  332.     case 'd':
  333. #ifdef LONG_LONG
  334.       fprintf_filtered (stream, "%lld", val_long);
  335. #else
  336.       fprintf_filtered (stream, "%d", val_long);
  337. #endif
  338.       break;
  339.  
  340.     case 'u':
  341. #ifdef LONG_LONG
  342.       fprintf_filtered (stream, "%llu", val_long);
  343. #else
  344.       fprintf_filtered (stream, "%u", val_long);
  345. #endif
  346.       break;
  347.  
  348.     case 'o':
  349.       if (val_long)
  350. #ifdef LONG_LONG
  351.     fprintf_filtered (stream, "0%llo", val_long);
  352. #else
  353.     fprintf_filtered (stream, "0%o", val_long);
  354. #endif
  355.       else
  356.     fprintf_filtered (stream, "0");
  357.       break;
  358.  
  359.     case 'a':
  360.       print_address ((CORE_ADDR) val_long, stream);
  361.       break;
  362.  
  363.     case 'A':
  364.       print_formatted_address ((CORE_ADDR) val_long, stream);
  365.       break;
  366.  
  367.     case 'c':
  368.       value_print (value_from_long (builtin_type_char, val_long), stream, 0,
  369.            Val_pretty_default);
  370.       break;
  371.  
  372.     case 'f':
  373.       if (len == sizeof (float))
  374.     type = builtin_type_float;
  375.       else if (len == sizeof (double))
  376.     type = builtin_type_double;
  377.       print_floating(valaddr, type, stream);
  378.       break;
  379.  
  380.     case 0:
  381.       abort ();
  382.  
  383.     default:
  384.       error ("Undefined output format \"%c\".", format);
  385.     }
  386. }
  387.  
  388. /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
  389.    on STREAM.  */
  390.  
  391. void
  392. print_floating(valaddr, type, stream)
  393.      char *valaddr;
  394.      struct type *type;
  395.      FILE *stream;
  396. {
  397.   double doub;
  398.   int inv;
  399.   int len = TYPE_LENGTH (type);
  400.   
  401.   doub = unpack_double (type, valaddr, &inv);
  402.   if (inv)
  403.     fprintf_filtered (stream, "Invalid float value");
  404.   else if (doub != doub)
  405.     {
  406.       /* Surely it is an IEEE floating point NaN. */
  407.  
  408.       long low, high, *arg = (long *)valaddr;    /* ASSUMED 32 BITS */
  409.       int nonneg;
  410.  
  411.       if (len <= sizeof(float))
  412.     {
  413.       /* It's single precision. */
  414.       low = *arg;
  415.       nonneg  =  low >= 0;
  416.       low &= 0x7fffff;
  417.       high = 0;
  418.     }
  419.       else
  420.     {
  421.       /* It's double precision.
  422.          Get the high and low words of the fraction.
  423.          Distinguish big and little-endian machines.  */
  424. #ifdef WORDS_BIG_ENDIAN
  425.       low = arg[1], high = arg[0];
  426. #else
  427.       low = arg[0], high = arg[1];
  428. #endif
  429.       nonneg  =  high >= 0;
  430.       high &= 0xfffff;
  431.     }
  432.       if (high)
  433.     fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonneg, high, low);
  434.       else
  435.     fprintf_filtered (stream, "-NaN(0x%lx)" + nonneg, low);
  436.     }
  437.   else
  438.     fprintf_filtered (stream, len <= sizeof(float) ? "%.6g" : "%.17g", doub);
  439. }
  440.  
  441. /* Specify default address for `x' command.
  442.    `info lines' uses this.  */
  443.  
  444. void
  445. set_next_address (addr)
  446.      CORE_ADDR addr;
  447. {
  448.   next_address = addr;
  449.  
  450.   /* Make address available to the user as $_.  */
  451.   set_internalvar (lookup_internalvar ("_"),
  452.            value_from_long (builtin_type_int, (LONGEST) addr));
  453. }
  454.  
  455. /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM. */
  456.  
  457. void
  458. print_address_symbolic (addr, stream)
  459.      CORE_ADDR addr;
  460.      FILE *stream;
  461. {
  462.   register char *format;
  463.   int name_location;
  464.   register int i = find_pc_misc_function (addr);
  465.  
  466.   /* If nothing comes out, don't print anything symbolic.  */
  467.   if (i < 0) return;
  468.   name_location = misc_function_vector[i].address;
  469.  
  470.   if (addr - name_location)
  471.     format = " <%s+%d>";
  472.   else
  473.     format = " <%s>";
  474.  
  475.   fprintf_filtered (stream, format,
  476.             misc_function_vector[i].name, addr - name_location);
  477. }
  478.  
  479. /* Print address ADDR symbolically on STREAM.
  480.    First print it as a number.  Then perhaps print
  481.    <SYMBOL + OFFSET> after the number.  */
  482.  
  483. void
  484. print_address (addr, stream)
  485.      CORE_ADDR addr;
  486.      FILE *stream;
  487. {
  488.   fprintf_filtered (stream, "0x%x", addr);
  489.   print_address_symbolic (addr, stream);
  490. }
  491.  
  492. /* Like print_address but opnly prints symbolically. */
  493.  
  494. void
  495. print_formatted_address (addr, stream)
  496.      CORE_ADDR addr;
  497.      FILE *stream;
  498. {
  499.   register int i = 0;
  500.   register char *format;
  501.   register struct symbol *fs;
  502.   char *name;
  503.   int name_location;
  504.  
  505.   i = find_pc_partial_function (addr, &name, &name_location);
  506.  
  507.   /* If nothing comes out, don't print anything symbolic.  */
  508.   
  509.   if (i == 0)
  510.     fprintf_filtered (stream, "0x%x", addr);
  511.   else if (addr - name_location)
  512.     fprintf_filtered (stream, "%s+%d", name, addr - name_location);
  513.   else
  514.     fprintf_filtered (stream, "%s", name);
  515. }
  516.  
  517. /* Examine data at address ADDR in format FMT.
  518.    Fetch it from memory and print on stdout.  */
  519.  
  520. static void
  521. do_examine (fmt, addr)
  522.      struct format_data fmt;
  523.      CORE_ADDR addr;
  524. {
  525.   register char format = 0;
  526.   register char size;
  527.   register int count = 1;
  528.   struct type *val_type;
  529.   register int i;
  530.   register int maxelts;
  531.  
  532.   format = fmt.format;
  533.   size = fmt.size;
  534.   count = fmt.count;
  535.   next_address = addr;
  536.  
  537.   /* String or instruction format implies fetch single bytes
  538.      regardless of the specified size.  */
  539.   if (format == 's' || format == 'i')
  540.     size = 'b';
  541.  
  542.   if (size == 'b')
  543.     val_type = builtin_type_char;
  544.   else if (size == 'h')
  545.     val_type = builtin_type_short;
  546.   else if (size == 'w')
  547.     val_type = builtin_type_long;
  548.   else if (size == 'g')
  549. #ifndef LONG_LONG
  550.     val_type = builtin_type_double;
  551. #else
  552.     val_type = builtin_type_long_long;
  553. #endif
  554.  
  555.   maxelts = 8;
  556.   if (size == 'w')
  557.     maxelts = 4;
  558.   if (size == 'g')
  559.     maxelts = 2;
  560.   if (format == 's' || format == 'i')
  561.     maxelts = 1;
  562.  
  563.   /* Print as many objects as specified in COUNT, at most maxelts per line,
  564.      with the address of the next one at the start of each line.  */
  565.  
  566.   while (count > 0)
  567.     {
  568.       print_address (next_address, stdout);
  569.       printf_filtered (":");
  570.       for (i = maxelts;
  571.        i > 0 && count > 0;
  572.        i--, count--)
  573.     {
  574.       printf_filtered ("\t");
  575.       /* Note that print_formatted sets next_address for the next
  576.          object.  */
  577.       last_examine_address = next_address;
  578.       last_examine_value = value_at (val_type, next_address);
  579.       print_formatted (last_examine_value, format, size);
  580.     }
  581.       printf_filtered ("\n");
  582.       fflush (stdout);
  583.     }
  584. }
  585.  
  586. static void
  587. validate_format (fmt, cmdname)
  588.      struct format_data fmt;
  589.      char *cmdname;
  590. {
  591.   if (fmt.size != 0)
  592.     error ("Size letters are meaningless in \"%s\" command.", cmdname);
  593.   if (fmt.count != 1)
  594.     error ("Item count other than 1 is meaningless in \"%s\" command.",
  595.        cmdname);
  596.   if (fmt.format == 'i' || fmt.format == 's')
  597.     error ("Format letter \"%c\" is meaningless in \"%s\" command.",
  598.        fmt.format, cmdname);
  599. }
  600.  
  601. static void
  602. print_command (exp)
  603.      char *exp;
  604. {
  605.   struct expression *expr;
  606.   register struct cleanup *old_chain = 0;
  607.   register char format = 0;
  608.   register value val;
  609.   struct format_data fmt;
  610.   int histindex;
  611.   int cleanup = 0;
  612.  
  613.   if (exp && *exp == '/')
  614.     {
  615.       exp++;
  616.       fmt = decode_format (&exp, last_format, 0);
  617.       validate_format (fmt, "print");
  618.       last_format = format = fmt.format;
  619.     }
  620.  
  621.   if (exp && *exp)
  622.     {
  623.       expr = parse_c_expression (exp);
  624.       old_chain = make_cleanup (free_current_contents, &expr);
  625.       cleanup = 1;
  626.       val = evaluate_expression (expr);
  627.     }
  628.   else
  629.     val = access_value_history (0);
  630.  
  631.   histindex = record_latest_value (val);
  632.   if (histindex >= 0) printf_filtered ("$%d = ", histindex);
  633.  
  634.   print_formatted (val, format, fmt.size);
  635.   printf_filtered ("\n");
  636.  
  637.   if (cleanup)
  638.     do_cleanups (old_chain);
  639. }
  640.  
  641. static void
  642. output_command (exp)
  643.      char *exp;
  644. {
  645.   struct expression *expr;
  646.   register struct cleanup *old_chain;
  647.   register char format = 0;
  648.   register value val;
  649.   struct format_data fmt;
  650.  
  651.   if (exp && *exp == '/')
  652.     {
  653.       exp++;
  654.       fmt = decode_format (&exp, 0, 0);
  655.       validate_format (fmt, "print");
  656.       format = fmt.format;
  657.     }
  658.  
  659.   expr = parse_c_expression (exp);
  660.   old_chain = make_cleanup (free_current_contents, &expr);
  661.  
  662.   val = evaluate_expression (expr);
  663.  
  664.   print_formatted (val, format, fmt.size);
  665.  
  666.   do_cleanups (old_chain);
  667. }
  668.  
  669. static void
  670. set_command (exp)
  671.      char *exp;
  672. {
  673.   struct expression *expr = parse_c_expression (exp);
  674.   register struct cleanup *old_chain
  675.     = make_cleanup (free_current_contents, &expr);
  676.   evaluate_expression (expr);
  677.   do_cleanups (old_chain);
  678. }
  679.  
  680. static void
  681. address_info (exp)
  682.      char *exp;
  683. {
  684.   register struct symbol *sym;
  685.   register CORE_ADDR val;
  686.   int is_a_field_of_this;    /* C++: lookup_symbol sets this to nonzero
  687.                    if exp is a field of `this'. */
  688.  
  689.   if (exp == 0)
  690.     error ("Argument required.");
  691.  
  692.   sym = lookup_symbol (exp, get_selected_block (), VAR_NAMESPACE, 
  693.                &is_a_field_of_this);
  694.   if (sym == 0)
  695.     {
  696.       register int i;
  697.  
  698.       if (is_a_field_of_this)
  699.     {
  700.       printf ("Symbol \"%s\" is a field of the local class variable `this'\n", exp);
  701.       return;
  702.     }
  703.  
  704.       for (i = 0; i < misc_function_count; i++)
  705.     if (!strcmp (misc_function_vector[i].name, exp))
  706.       break;
  707.  
  708.       if (i < misc_function_count)
  709.     printf ("Symbol \"%s\" is at 0x%x in a file compiled without -g.\n",
  710.         exp, misc_function_vector[i].address);
  711.       else
  712.     error ("No symbol \"%s\" in current context.", exp);
  713.       return;
  714.     }
  715.  
  716.   printf ("Symbol \"%s\" is ", SYMBOL_NAME (sym));
  717.   val = SYMBOL_VALUE (sym);
  718.  
  719.   switch (SYMBOL_CLASS (sym))
  720.     {
  721.     case LOC_CONST:
  722.     case LOC_CONST_BYTES:
  723.       printf ("constant");
  724.       break;
  725.  
  726.     case LOC_LABEL:
  727.       printf ("a label at address 0x%x", val);
  728.       break;
  729.  
  730.     case LOC_REGISTER:
  731.       printf ("a variable in register %s", reg_names[val]);
  732.       break;
  733.  
  734.     case LOC_STATIC:
  735.       printf ("static at address 0x%x", val);
  736.       break;
  737.  
  738.     case LOC_REGPARM:
  739.       printf ("an argument in register %s", reg_names[val]);
  740.       break;
  741.       
  742.     case LOC_ARG:
  743.       printf ("an argument at offset %d", val);
  744.       break;
  745.  
  746.     case LOC_LOCAL:
  747.       printf ("a local variable at frame offset %d", val);
  748.       break;
  749.  
  750.     case LOC_REF_ARG:
  751.       printf ("a reference argument at offset %d", val);
  752.       break;
  753.  
  754.     case LOC_TYPEDEF:
  755.       printf ("a typedef");
  756.       break;
  757.  
  758.     case LOC_BLOCK:
  759.       printf ("a function at address 0x%x",
  760.           BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
  761.       break;
  762.     }
  763.   printf (".\n");
  764. }
  765.  
  766. static void
  767. x_command (exp, from_tty)
  768.      char *exp;
  769.      int from_tty;
  770. {
  771.   struct expression *expr;
  772.   struct format_data fmt;
  773.   struct cleanup *old_chain;
  774.   struct value *val;
  775.  
  776.   fmt.format = last_format;
  777.   fmt.size = last_size;
  778.   fmt.count = 1;
  779.  
  780.   if (exp && *exp == '/')
  781.     {
  782.       exp++;
  783.       fmt = decode_format (&exp, last_format, last_size);
  784.       last_size = fmt.size;
  785.       last_format = fmt.format;
  786.     }
  787.  
  788.   /* If we have an expression, evaluate it and use it as the address.  */
  789.  
  790.   if (exp != 0 && *exp != 0)
  791.     {
  792.       expr = parse_c_expression (exp);
  793.       /* Cause expression not to be there any more
  794.      if this command is repeated with Newline.
  795.      But don't clobber a user-defined command's definition.  */
  796.       if (from_tty)
  797.     *exp = 0;
  798.       old_chain = make_cleanup (free_current_contents, &expr);
  799.       val = evaluate_expression (expr);
  800.       /* In rvalue contexts, such as this, functions are coerced into
  801.      pointers to functions.  This makes "x/i main" work.  */
  802.       if (/* last_format == 'i'
  803.       && */ TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC
  804.       && VALUE_LVAL (val) == lval_memory)
  805.     next_address = VALUE_ADDRESS (val);
  806.       else
  807.     next_address = (CORE_ADDR) value_as_long (val);
  808.       do_cleanups (old_chain);
  809.     }
  810.  
  811.   do_examine (fmt, next_address);
  812.  
  813.   /* Set a couple of internal variables if appropriate. */
  814.   if (last_examine_value)
  815.     {
  816.       /* Make last address examined available to the user as $_.  */
  817.       set_internalvar (lookup_internalvar ("_"),
  818.                value_from_long (builtin_type_int, 
  819.                     (LONGEST) last_examine_address));
  820.       
  821.       /* Make contents of last address examined available to the user as $__.*/
  822.       set_internalvar (lookup_internalvar ("__"), last_examine_value);
  823.     }
  824. }
  825.  
  826. /* Commands for printing types of things.  */
  827.  
  828. static void
  829. whatis_command (exp)
  830.      char *exp;
  831. {
  832.   struct expression *expr;
  833.   register value val;
  834.   register struct cleanup *old_chain;
  835.  
  836.   if (exp)
  837.     {
  838.       expr = parse_c_expression (exp);
  839.       old_chain = make_cleanup (free_current_contents, &expr);
  840.       val = evaluate_type (expr);
  841.     }
  842.   else
  843.     val = access_value_history (0);
  844.  
  845.   printf_filtered ("type = ");
  846.   /* Most of the time users do not want to see all the fields
  847.      in a structure.  If they do they can use the "ptype" command.
  848.      Hence the "-1" below.  */
  849.   type_print (VALUE_TYPE (val), "", stdout, -1);
  850.   printf_filtered ("\n");
  851.  
  852.   if (exp)
  853.     do_cleanups (old_chain);
  854. }
  855.  
  856. static void
  857. ptype_command (typename)
  858.      char *typename;
  859. {
  860.   register char *p = typename;
  861.   register int len;
  862.   extern struct block *get_current_block ();
  863.   register struct block *b
  864.     = (have_inferior_p () || have_core_file_p ()) ? get_current_block () : 0;
  865.   register struct type *type;
  866.  
  867.   if (typename == 0)
  868.     error_no_arg ("type name");
  869.  
  870.   while (*p && *p != ' ' && *p != '\t') p++;
  871.   len = p - typename;
  872.   while (*p == ' ' || *p == '\t') p++;
  873.  
  874.   if (len == 6 && !strncmp (typename, "struct", 6))
  875.     type = lookup_struct (p, b);
  876.   else if (len == 5 && !strncmp (typename, "union", 5))
  877.     type = lookup_union (p, b);
  878.   else if (len == 4 && !strncmp (typename, "enum", 4))
  879.     type = lookup_enum (p, b);
  880.   else
  881.     {
  882.       type = lookup_typename (typename, b, 1);
  883.       if (type == 0)
  884.     {
  885.       register struct symbol *sym
  886.         = lookup_symbol (typename, b, STRUCT_NAMESPACE, 0);
  887.       if (sym == 0)
  888.         error ("No type named %s.", typename);
  889.       printf_filtered ("No type named %s, but there is a ",
  890.           typename);
  891.       switch (TYPE_CODE (SYMBOL_TYPE (sym)))
  892.         {
  893.         case TYPE_CODE_STRUCT:
  894.           printf_filtered ("struct");
  895.           break;
  896.  
  897.         case TYPE_CODE_UNION:
  898.           printf_filtered ("union");
  899.           break;
  900.  
  901.         case TYPE_CODE_ENUM:
  902.           printf_filtered ("enum");
  903.         }
  904.       printf_filtered (" %s.  Type \"help ptype\".\n", typename);
  905.       type = SYMBOL_TYPE (sym);
  906.     }
  907.     }
  908.  
  909.   type_print (type, "", stdout, 1);
  910.   printf_filtered ("\n");
  911. }
  912.  
  913. enum display_status {disabled, enabled};
  914.  
  915. struct display
  916. {
  917.   /* Chain link to next auto-display item.  */
  918.   struct display *next;
  919.   /* Expression to be evaluated and displayed.  */
  920.   struct expression *exp;
  921.   /* Item number of this auto-display item.  */
  922.   int number;
  923.   /* Display format specified.  */
  924.   struct format_data format;
  925.   /* Innermost block required by this expression when evaluated */
  926.   struct block *block;
  927.   /* Status of this display (enabled or disabled) */
  928.   enum display_status status;
  929. };
  930.  
  931. /* Chain of expressions whose values should be displayed
  932.    automatically each time the program stops.  */
  933.  
  934. static struct display *display_chain;
  935.  
  936. static int display_number;
  937.  
  938. /* Add an expression to the auto-display chain.
  939.    Specify the expression.  */
  940.  
  941. static void
  942. display_command (exp, from_tty)
  943.      char *exp;
  944.      int from_tty;
  945. {
  946.   struct format_data fmt;
  947.   register struct expression *expr;
  948.   register struct display *new;
  949.   extern struct block *innermost_block;
  950.  
  951.   if (exp == 0)
  952.     {
  953.       do_displays ();
  954.       return;
  955.     }
  956.  
  957.   if (*exp == '/')
  958.     {
  959.       exp++;
  960.       fmt = decode_format (&exp, 0, 0);
  961.       if (fmt.size && fmt.format == 0)
  962.     fmt.format = 'x';
  963.       if (fmt.format == 'i' || fmt.format == 's')
  964.     fmt.size = 'b';
  965.     }
  966.   else
  967.     {
  968.       fmt.format = 0;
  969.       fmt.size = 0;
  970.       fmt.count = 0;
  971.     }
  972.  
  973.   innermost_block = 0;
  974.   expr = parse_c_expression (exp);
  975.  
  976.   new = (struct display *) xmalloc (sizeof (struct display));
  977.  
  978.   new->exp = expr;
  979.   new->block = innermost_block;
  980.   new->next = display_chain;
  981.   new->number = ++display_number;
  982.   new->format = fmt;
  983.   new->status = enabled;
  984.   display_chain = new;
  985.  
  986.   if (from_tty && have_inferior_p ())
  987.     do_one_display (new);
  988.  
  989.   dont_repeat ();
  990. }
  991.  
  992. static void
  993. free_display (d)
  994.      struct display *d;
  995. {
  996.   free (d->exp);
  997.   free (d);
  998. }
  999.  
  1000. /* Clear out the display_chain.
  1001.    Done when new symtabs are loaded, since this invalidates
  1002.    the types stored in many expressions.  */
  1003.  
  1004. void
  1005. clear_displays ()
  1006. {
  1007.   register struct display *d;
  1008.  
  1009.   while (d = display_chain)
  1010.     {
  1011.       free (d->exp);
  1012.       display_chain = d->next;
  1013.       free (d);
  1014.     }
  1015. }
  1016.  
  1017. /* Delete the auto-display number NUM.  */
  1018.  
  1019. void
  1020. delete_display (num)
  1021.      int num;
  1022. {
  1023.   register struct display *d1, *d;
  1024.  
  1025.   if (!display_chain)
  1026.     error ("No display number %d.", num);
  1027.  
  1028.   if (display_chain->number == num)
  1029.     {
  1030.       d1 = display_chain;
  1031.       display_chain = d1->next;
  1032.       free_display (d1);
  1033.     }
  1034.   else
  1035.     for (d = display_chain; ; d = d->next)
  1036.       {
  1037.     if (d->next == 0)
  1038.       error ("No display number %d.", num);
  1039.     if (d->next->number == num)
  1040.       {
  1041.         d1 = d->next;
  1042.         d->next = d1->next;
  1043.         free_display (d1);
  1044.         break;
  1045.       }
  1046.       }
  1047. }
  1048.  
  1049. /* Delete some values from the auto-display chain.
  1050.    Specify the element numbers.  */
  1051.  
  1052. static void
  1053. undisplay_command (args)
  1054.      char *args;
  1055. {
  1056.   register char *p = args;
  1057.   register char *p1;
  1058.   register int num;
  1059.   register struct display *d, *d1;
  1060.  
  1061.   if (args == 0)
  1062.     {
  1063.       if (query ("Delete all auto-display expressions? "))
  1064.     clear_displays ();
  1065.       dont_repeat ();
  1066.       return;
  1067.     }
  1068.  
  1069.   while (*p)
  1070.     {
  1071.       p1 = p;
  1072.       while (*p1 >= '0' && *p1 <= '9') p1++;
  1073.       if (*p1 && *p1 != ' ' && *p1 != '\t')
  1074.     error ("Arguments must be display numbers.");
  1075.  
  1076.       num = atoi (p);
  1077.  
  1078.       delete_display (num);
  1079.  
  1080.       p = p1;
  1081.       while (*p == ' ' || *p == '\t') p++;
  1082.     }
  1083.   dont_repeat ();
  1084. }
  1085.  
  1086. /* Display a single auto-display.  
  1087.    Do nothing if the display cannot be printed in the current context,
  1088.    or if the display is disabled. */
  1089.  
  1090. static void
  1091. do_one_display (d)
  1092.      struct display *d;
  1093. {
  1094.   int within_current_scope;
  1095.  
  1096.   if (d->status == disabled)
  1097.     return;
  1098.  
  1099.   if (d->block)
  1100.     within_current_scope = contained_in (get_selected_block (), d->block);
  1101.   else
  1102.     within_current_scope = 1;
  1103.   if (!within_current_scope)
  1104.     return;
  1105.  
  1106.   current_display_number = d->number;
  1107.  
  1108.   printf_filtered ("%d: ", d->number);
  1109.   if (d->format.size)
  1110.     {
  1111.       printf_filtered ("x/");
  1112.       if (d->format.count != 1)
  1113.     printf_filtered ("%d", d->format.count);
  1114.       printf_filtered ("%c", d->format.format);
  1115.       if (d->format.format != 'i' && d->format.format != 's')
  1116.     printf_filtered ("%c", d->format.size);
  1117.       printf_filtered (" ");
  1118.       print_expression (d->exp, stdout);
  1119.       if (d->format.count != 1)
  1120.     printf_filtered ("\n");
  1121.       else
  1122.     printf_filtered ("  ");
  1123.       do_examine (d->format,
  1124.           (CORE_ADDR) value_as_long (evaluate_expression (d->exp)));
  1125.  
  1126.     }
  1127.   else
  1128.     {
  1129.       if (d->format.format)
  1130.     printf_filtered ("/%c ", d->format.format);
  1131.       print_expression (d->exp, stdout);
  1132.       printf_filtered (" = ");
  1133.       print_formatted (evaluate_expression (d->exp),
  1134.                d->format.format, d->format.size);
  1135.       printf_filtered ("\n");
  1136.     }
  1137.  
  1138.   fflush (stdout);
  1139.   current_display_number = -1;
  1140. }
  1141.  
  1142. /* Display all of the values on the auto-display chain which can be
  1143.    evaluated in the current scope.  */
  1144.  
  1145. void
  1146. do_displays ()
  1147. {
  1148.   register struct display *d;
  1149.  
  1150.   for (d = display_chain; d; d = d->next)
  1151.     do_one_display (d);
  1152. }
  1153.  
  1154. /* Delete the auto-display which we were in the process of displaying.
  1155.    This is done when there is an error or a signal.  */
  1156.  
  1157. void
  1158. disable_display (num)
  1159.      int num;
  1160. {
  1161.   register struct display *d;
  1162.  
  1163.   for (d = display_chain; d; d = d->next)
  1164.     if (d->number == num)
  1165.       {
  1166.     d->status = disabled;
  1167.     return;
  1168.       }
  1169.   printf ("No display number %d.\n", num);
  1170. }
  1171.   
  1172. void
  1173. disable_current_display ()
  1174. {
  1175.   if (current_display_number >= 0)
  1176.     {
  1177.       disable_display (current_display_number);
  1178.       fprintf (stderr, "Disabling display %d to avoid infinite recursion.\n",
  1179.            current_display_number);
  1180.     }
  1181.   current_display_number = -1;
  1182. }
  1183.  
  1184. static void
  1185. display_info ()
  1186. {
  1187.   register struct display *d;
  1188.  
  1189.   if (!display_chain)
  1190.     printf ("There are no auto-display expressions now.\n");
  1191.   else
  1192.       printf_filtered ("Auto-display expressions now in effect:\n\
  1193. Num Enb Expression\n");
  1194.  
  1195.   for (d = display_chain; d; d = d->next)
  1196.     {
  1197.       printf_filtered ("%d:   %c  ", d->number, "ny"[(int)d->status]);
  1198.       if (d->format.size)
  1199.     printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
  1200.         d->format.format);
  1201.       else if (d->format.format)
  1202.     printf_filtered ("/%c ", d->format.format);
  1203.       print_expression (d->exp, stdout);
  1204.       if (d->block && !contained_in (get_selected_block (), d->block))
  1205.     printf_filtered (" (cannot be evaluated in the current context)");
  1206.       printf_filtered ("\n");
  1207.       fflush (stdout);
  1208.     }
  1209. }
  1210.  
  1211. void
  1212. enable_display (args)
  1213.      char *args;
  1214. {
  1215.   register char *p = args;
  1216.   register char *p1;
  1217.   register int num;
  1218.   register struct display *d;
  1219.  
  1220.   if (p == 0)
  1221.     {
  1222.       for (d = display_chain; d; d = d->next)
  1223.     d->status = enabled;
  1224.     }
  1225.   else
  1226.     while (*p)
  1227.       {
  1228.     p1 = p;
  1229.     while (*p1 >= '0' && *p1 <= '9')
  1230.       p1++;
  1231.     if (*p1 && *p1 != ' ' && *p1 != '\t')
  1232.       error ("Arguments must be display numbers.");
  1233.     
  1234.     num = atoi (p);
  1235.     
  1236.     for (d = display_chain; d; d = d->next)
  1237.       if (d->number == num)
  1238.         {
  1239.           d->status = enabled;
  1240.           goto win;
  1241.         }
  1242.     printf ("No display number %d.\n", num);
  1243.       win:
  1244.     p = p1;
  1245.     while (*p == ' ' || *p == '\t')
  1246.       p++;
  1247.       }
  1248. }
  1249.  
  1250. void
  1251. disable_display_command (args, from_tty)
  1252.      char *args;
  1253.      int from_tty;
  1254. {
  1255.   register char *p = args;
  1256.   register char *p1;
  1257.   register int num;
  1258.   register struct display *d;
  1259.  
  1260.   if (p == 0)
  1261.     {
  1262.       for (d = display_chain; d; d = d->next)
  1263.     d->status = disabled;
  1264.     }
  1265.   else
  1266.     while (*p)
  1267.       {
  1268.     p1 = p;
  1269.     while (*p1 >= '0' && *p1 <= '9')
  1270.       p1++;
  1271.     if (*p1 && *p1 != ' ' && *p1 != '\t')
  1272.       error ("Arguments must be display numbers.");
  1273.     
  1274.     num = atoi (p);
  1275.     
  1276.     disable_display (atoi (p));
  1277.  
  1278.     p = p1;
  1279.     while (*p == ' ' || *p == '\t')
  1280.       p++;
  1281.       }
  1282. }
  1283.  
  1284.  
  1285. /* Print the value in stack frame FRAME of a variable
  1286.    specified by a struct symbol.  */
  1287.  
  1288. void
  1289. print_variable_value (var, frame, stream)
  1290.      struct symbol *var;
  1291.      FRAME frame;
  1292.      FILE *stream;
  1293. {
  1294.   value val = read_var_value (var, frame);
  1295.   value_print (val, stream, 0, Val_pretty_default);
  1296. }
  1297.  
  1298. static int
  1299. compare_ints (i, j)
  1300.      int *i, *j;
  1301. {
  1302.   return *i - *j;
  1303. }
  1304.  
  1305. /* Print the arguments of a stack frame, given the function FUNC
  1306.    running in that frame (as a symbol), the info on the frame,
  1307.    and the number of args according to the stack frame (or -1 if unknown).  */
  1308.  
  1309. static void print_frame_nameless_args ();
  1310.  
  1311. void
  1312. print_frame_args (func, fi, num, stream)
  1313.      struct symbol *func;
  1314.      struct frame_info *fi;
  1315.      int num;
  1316.      FILE *stream;
  1317. {
  1318.   struct block *b;
  1319.   int nsyms = 0;
  1320.   int first = 1;
  1321.   register int i;
  1322.   register int last_regparm = 0;
  1323.   register struct symbol *lastsym, *sym, *nextsym;
  1324.   register value val;
  1325.   /* Offset of stack argument that is at the highest offset.
  1326.      -1 if we haven't come to a stack argument yet.  */
  1327.   CORE_ADDR highest_offset = (CORE_ADDR) -1;
  1328.   register CORE_ADDR addr = FRAME_ARGS_ADDRESS (fi);
  1329.  
  1330.   if (func)
  1331.     {
  1332.       b = SYMBOL_BLOCK_VALUE (func);
  1333.       nsyms = BLOCK_NSYMS (b);
  1334.     }
  1335.  
  1336.   for (i = 0; i < nsyms; i++)
  1337.     {
  1338.       QUIT;
  1339.       sym = BLOCK_SYM (b, i);
  1340.  
  1341.       if (SYMBOL_CLASS (sym) != LOC_REGPARM
  1342.       && SYMBOL_CLASS (sym) != LOC_ARG
  1343.       && SYMBOL_CLASS (sym) != LOC_REF_ARG)
  1344.     continue;
  1345.  
  1346.       /* Print the next arg.  */
  1347.       if (SYMBOL_CLASS (sym) == LOC_REGPARM)
  1348.     val = value_from_register (SYMBOL_TYPE (sym),
  1349.                    SYMBOL_VALUE (sym),
  1350.                    FRAME_INFO_ID (fi));
  1351.       else
  1352.     {
  1353.       int current_offset = SYMBOL_VALUE (sym);
  1354.       int arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
  1355.       
  1356.       if (SYMBOL_CLASS (sym) == LOC_REF_ARG)
  1357.         val = value_at (SYMBOL_TYPE (sym),
  1358.                 read_memory_integer (addr + current_offset,
  1359.                          sizeof (CORE_ADDR)));
  1360.       else
  1361.         val = value_at (SYMBOL_TYPE (sym), addr + current_offset);
  1362.  
  1363.       /* Round up address of next arg to multiple of size of int.  */
  1364.       current_offset
  1365.         = (((current_offset + sizeof (int) - 1) / sizeof (int))
  1366.            * sizeof (int));
  1367.  
  1368.       /* If this is the highest offset seen yet, set highest_offset.  */
  1369.       if (highest_offset == (CORE_ADDR)-1
  1370.           || ((current_offset
  1371.            + (arg_size - sizeof (int) + 3) / (sizeof (int)))
  1372.           > highest_offset))
  1373.         highest_offset = current_offset;
  1374.     }
  1375.  
  1376.       if (! first)
  1377.     fprintf_filtered (stream, ", ");
  1378.       fputs_filtered (SYMBOL_NAME (sym), stream);
  1379.       fputs_filtered ("=", stream);
  1380.  
  1381. /* Nonzero if a LOC_ARG which is a struct is useless.  */
  1382. #if !defined (STRUCT_ARG_SYM_GARBAGE)
  1383. #define STRUCT_ARG_SYM_GARBAGE(gcc_p) 0
  1384. #endif
  1385.  
  1386.       if (STRUCT_ARG_SYM_GARBAGE (b->gcc_compile_flag)
  1387.       && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
  1388.       && SYMBOL_CLASS (sym) == LOC_ARG)
  1389.     {
  1390.       /* Try looking up that name.  SunOS4 puts out a usable
  1391.          symbol as a local variable (in addition to the one
  1392.          for the arg).  */
  1393.       struct symbol *sym2 =
  1394.         lookup_symbol (SYMBOL_NAME (sym), b, VAR_NAMESPACE, 0);
  1395.  
  1396.       if (sym2 != NULL)
  1397.         val = value_of_variable (sym2);
  1398.       else
  1399.         {
  1400.           fputs_filtered ("?", stream);
  1401.           first = 0;
  1402.           continue;
  1403.         }
  1404.     }
  1405.  
  1406.       value_print (val, stream, 0, Val_no_prettyprint);
  1407.       first = 0;
  1408.     }
  1409.  
  1410.   /* Don't print nameless args in situations where we don't know
  1411.      enough about the stack to find them.  */
  1412.   if (num != -1)
  1413.     {
  1414.       if (highest_offset != (CORE_ADDR) -1
  1415.       && num * sizeof (int) + FRAME_ARGS_SKIP > highest_offset)
  1416.     print_frame_nameless_args (fi, addr,
  1417.                    highest_offset + sizeof (int),
  1418.                    num * sizeof (int) + FRAME_ARGS_SKIP,
  1419.                    stream);
  1420.       else 
  1421.     print_frame_nameless_args (fi, addr, FRAME_ARGS_SKIP,
  1422.                    num * sizeof (int) + FRAME_ARGS_SKIP,
  1423.                    stream);
  1424.     }
  1425. }
  1426.  
  1427. static void
  1428. print_frame_nameless_args (fi, argsaddr, start, end, stream)
  1429.      struct frame_info *fi;
  1430.      CORE_ADDR argsaddr;
  1431.      int start;
  1432.      int end;
  1433.      FILE *stream;
  1434. {
  1435.     extern void (*default_scalar_print)();
  1436.     LONGEST v;
  1437.     int p = start;
  1438.     char *s = "";
  1439.  
  1440.     for (p = start; p < end; p += sizeof(int)) {
  1441.         QUIT;
  1442. #if defined(NAMELESS_ARG)
  1443.         v = NAMELESS_ARG(fi, (p - start) / sizeof(int));
  1444. #else
  1445.         v = read_memory_integer (argsaddr + p, sizeof (int));
  1446. #endif
  1447.         fprintf_filtered (stream, s);
  1448.         s = ", ";
  1449.         (*default_scalar_print) (stream, builtin_type_int, v);
  1450.     }
  1451. }
  1452.  
  1453. static void
  1454. printf_command (arg)
  1455.      char *arg;
  1456. {
  1457.   register char *f;
  1458.   register char *s = arg;
  1459.   char *string;
  1460.   value *val_args;
  1461.   int nargs = 0;
  1462.   int allocated_args = 20;
  1463.   char *arg_bytes;
  1464.  
  1465.   val_args = (value *) xmalloc (allocated_args * sizeof (value));
  1466.  
  1467.   if (s == 0)
  1468.     error_no_arg ("format-control string and values to print");
  1469.  
  1470.   /* Skip white space before format string */
  1471.   while (*s == ' ' || *s == '\t') s++;
  1472.  
  1473.   /* A format string should follow, enveloped in double quotes */
  1474.   if (*s++ != '"')
  1475.     error ("Bad format string, missing '\"'.");
  1476.  
  1477.   /* Parse the format-control string and copy it into the string STRING,
  1478.      processing some kinds of escape sequence.  */
  1479.  
  1480.   f = string = (char *) alloca (strlen (s) + 1);
  1481.   while (*s != '"')
  1482.     {
  1483.       int c = *s++;
  1484.       switch (c)
  1485.     {
  1486.     case '\0':
  1487.       error ("Bad format string, non-terminated '\"'.");
  1488.       /* doesn't return */
  1489.  
  1490.     case '\\':
  1491.       switch (c = *s++)
  1492.         {
  1493.         case '\\':
  1494.           *f++ = '\\';
  1495.           break;
  1496.         case 'n':
  1497.           *f++ = '\n';
  1498.           break;
  1499.         case 't':
  1500.           *f++ = '\t';
  1501.           break;
  1502.         case 'r':
  1503.           *f++ = '\r';
  1504.           break;
  1505.         case '"':
  1506.           *f++ = '"';
  1507.           break;
  1508.         default:
  1509.           /* ??? TODO: handle other escape sequences */
  1510.           error ("Unrecognized \\ escape character in format string.");
  1511.         }
  1512.       break;
  1513.  
  1514.     default:
  1515.       *f++ = c;
  1516.     }
  1517.     }
  1518.  
  1519.   /* Skip over " and following space and comma.  */
  1520.   s++;
  1521.   *f++ = '\0';
  1522.   while (*s == ' ' || *s == '\t') s++;
  1523.  
  1524.   if (*s != ',' && *s != 0)
  1525.     error ("Invalid argument syntax");
  1526.  
  1527.   if (*s == ',') s++;
  1528.   while (*s == ' ' || *s == '\t') s++;
  1529.  
  1530.   {
  1531.     /* Now scan the string for %-specs and see what kinds of args they want.
  1532.        argclass[I] classifies the %-specs so we can give vprintf something
  1533.        of the right size.  */
  1534.  
  1535.     enum argclass {int_arg, string_arg, double_arg, long_long_arg};
  1536.     enum argclass *argclass;
  1537.     int nargs_wanted;
  1538.     int argindex;
  1539.     int lcount;
  1540.     int i;
  1541.  
  1542.     argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
  1543.     nargs_wanted = 0;
  1544.     f = string;
  1545.     while (*f)
  1546.       if (*f++ == '%')
  1547.     {
  1548.       lcount = 0;
  1549.       while (index ("0123456789.hlL-+ #", *f)) 
  1550.         {
  1551.           if (*f == 'l' || *f == 'L')
  1552.         lcount++;
  1553.           f++;
  1554.         }
  1555.       if (*f == 's')
  1556.         argclass[nargs_wanted++] = string_arg;
  1557.       else if (*f == 'e' || *f == 'f' || *f == 'g')
  1558.         argclass[nargs_wanted++] = double_arg;
  1559.       else if (lcount > 1)
  1560.         argclass[nargs_wanted++] = long_long_arg;
  1561.       else if (*f != '%')
  1562.         argclass[nargs_wanted++] = int_arg;
  1563.       f++;
  1564.     }
  1565.  
  1566.     /* Now, parse all arguments and evaluate them.
  1567.        Store the VALUEs in VAL_ARGS.  */
  1568.  
  1569.     while (*s != '\0')
  1570.       {
  1571.     char *s1;
  1572.     if (nargs == allocated_args)
  1573.       val_args = (value *) xrealloc (val_args,
  1574.                      (allocated_args *= 2)
  1575.                      * sizeof (value));
  1576.     s1 = s;
  1577.     val_args[nargs] = parse_to_comma_and_eval (&s1);
  1578.  
  1579.     /* If format string wants a float, unchecked-convert the value to
  1580.        floating point of the same size */
  1581.  
  1582.     if (argclass[nargs] == double_arg)
  1583.       {
  1584.         if (TYPE_LENGTH (VALUE_TYPE (val_args[nargs])) == sizeof (float))
  1585.           VALUE_TYPE (val_args[nargs]) = builtin_type_float;
  1586.         if (TYPE_LENGTH (VALUE_TYPE (val_args[nargs])) == sizeof (double))
  1587.           VALUE_TYPE (val_args[nargs]) = builtin_type_double;
  1588.       }
  1589.     nargs++;
  1590.     s = s1;
  1591.     if (*s == ',')
  1592.       s++;
  1593.       }
  1594.  
  1595.     if (nargs != nargs_wanted)
  1596.       error ("Wrong number of arguments for specified format-string");
  1597.  
  1598.     /* Now lay out an argument-list containing the arguments
  1599.        as doubles, integers and C pointers.  */
  1600.  
  1601.     arg_bytes = (char *) alloca (sizeof (double) * nargs);
  1602.     argindex = 0;
  1603.     for (i = 0; i < nargs; i++)
  1604.       {
  1605.     if (argclass[i] == string_arg)
  1606.       {
  1607.         char *str;
  1608.         int tem, j;
  1609.         tem = value_as_long (val_args[i]);
  1610.  
  1611.         /* This is a %s argument.  Find the length of the string.  */
  1612.         for (j = 0; ; j++)
  1613.           {
  1614.         char c;
  1615.         QUIT;
  1616.         read_memory (tem + j, &c, 1);
  1617.         if (c == 0)
  1618.           break;
  1619.           }
  1620.  
  1621.         /* Copy the string contents into a string inside GDB.  */
  1622.         str = (char *) alloca (j + 1);
  1623.         read_memory (tem, str, j);
  1624.         str[j] = 0;
  1625.  
  1626.         /* Pass address of internal copy as the arg to vprintf.  */
  1627.         *((int *) &arg_bytes[argindex]) = (int) str;
  1628.         argindex += sizeof (int);
  1629.       }
  1630.     else if (VALUE_TYPE (val_args[i])->code == TYPE_CODE_FLT)
  1631.       {
  1632.         *((double *) &arg_bytes[argindex]) = value_as_double (val_args[i]);
  1633.         argindex += sizeof (double);
  1634.       }
  1635.     else
  1636. #ifdef LONG_LONG
  1637.       if (argclass[i] == long_long_arg)
  1638.         {
  1639.           *(long long *) &arg_bytes[argindex] = value_as_long (val_args[i]);
  1640.           argindex += sizeof (long long);
  1641.         }
  1642.       else
  1643. #endif
  1644.         {
  1645.           *((int *) &arg_bytes[argindex]) = value_as_long (val_args[i]);
  1646.           argindex += sizeof (int);
  1647.         }
  1648.       }
  1649.   }
  1650.   vprintf (string, arg_bytes);
  1651. }
  1652.  
  1653. /* Helper function for asdump_command.  Finds the bounds of a function
  1654.    for a specified section of text.  PC is an address within the
  1655.    function which you want bounds for; *LOW and *HIGH are set to the
  1656.    beginning (inclusive) and end (exclusive) of the function.  This
  1657.    function returns 1 on success and 0 on failure.  */
  1658.  
  1659. static int
  1660. containing_function_bounds (pc, low, high)
  1661.      CORE_ADDR pc, *low, *high;
  1662. {
  1663.   int scan;
  1664.  
  1665.   if (!find_pc_partial_function (pc, 0, low))
  1666.     return 0;
  1667.  
  1668.   scan = *low;
  1669.   do {
  1670.     scan++;
  1671.     if (!find_pc_partial_function (scan, 0, high))
  1672.       return 0;
  1673.   } while (*low == *high);
  1674.  
  1675.   return 1;
  1676. }
  1677.  
  1678. /* Dump a specified section of assembly code.  With no command line
  1679.    arguments, this command will dump the assembly code for the
  1680.    function surrounding the pc value in the selected frame.  With one
  1681.    argument, it will dump the assembly code surrounding that pc value.
  1682.    Two arguments are interpeted as bounds within which to dump
  1683.    assembly.  */
  1684.  
  1685. static void
  1686. disassemble_command (arg, from_tty)
  1687.      char *arg;
  1688.      int from_tty;
  1689. {
  1690.   CORE_ADDR low, high;
  1691.   CORE_ADDR pc;
  1692.   char *space_index;
  1693.  
  1694.   if (!arg)
  1695.     {
  1696.       if (!selected_frame)
  1697.     error ("No frame selected.\n");
  1698.  
  1699.       pc = get_frame_pc (selected_frame);
  1700.       if (!containing_function_bounds (pc, &low, &high))
  1701.     error ("No function contains pc specified by selected frame.\n");
  1702.     }
  1703.   else if (!(space_index = (char *) index (arg, ' ')))
  1704.     {
  1705.       /* One argument.  */
  1706.       pc = parse_and_eval_address (arg);
  1707.       if (!containing_function_bounds (pc, &low, &high))
  1708.     error ("No function contains specified pc.\n");
  1709.     }
  1710.   else
  1711.     {
  1712.       /* Two arguments.  */
  1713.       *space_index = '\0';
  1714.       low = parse_and_eval_address (arg);
  1715.       high = parse_and_eval_address (space_index + 1);
  1716.     }
  1717.  
  1718.   printf_filtered ("Dump of assembler code ");
  1719.   if (!space_index)
  1720.     {
  1721.       char *name;
  1722.       find_pc_partial_function (pc, &name, 0);
  1723.       printf_filtered ("for function %s:\n", name);
  1724.     }
  1725.   else
  1726.     printf_filtered ("from 0x%x to 0x%x:\n", low, high);
  1727.  
  1728.   /* Dump the specified range.  */
  1729.   for (pc = low; pc < high; )
  1730.     {
  1731.       QUIT;
  1732.       print_address (pc, stdout);
  1733.       printf_filtered (":\t");
  1734.       pc += print_insn (pc, stdout);
  1735.       printf_filtered ("\n");
  1736.     }
  1737.   printf_filtered ("End of assembler dump.\n");
  1738.   fflush (stdout);
  1739. }
  1740.  
  1741.  
  1742. extern struct cmd_list_element *enablelist, *disablelist, *deletelist;
  1743. extern struct cmd_list_element *cmdlist, *setlist;
  1744.  
  1745. void
  1746. _initialize_printcmd ()
  1747. {
  1748.   current_display_number = -1;
  1749.  
  1750.   add_info ("address", address_info,
  1751.        "Describe where variable VAR is stored.");
  1752.  
  1753.   add_com ("x", class_vars, x_command,
  1754.        "Examine memory: x/FMT ADDRESS.\n\
  1755. ADDRESS is an expression for the memory address to examine.\n\
  1756. FMT is a repeat count followed by a format letter and a size letter.\n\
  1757. Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
  1758.  f(float), a(address), i(instruction), c(char) and s(string).\n\
  1759. Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
  1760.   g is meaningful only with f, for type double.\n\
  1761. The specified number of objects of the specified size are printed\n\
  1762. according to the format.\n\n\
  1763. Defaults for format and size letters are those previously used.\n\
  1764. Default count is 1.  Default address is following last thing printed\n\
  1765. with this command or \"print\".");
  1766.  
  1767.   add_com ("disassemble", class_vars, disassemble_command,
  1768.        "Disassemble a specified section of memory.\n\
  1769. Default is the function surrounding the pc of the selected frame.\n\
  1770. With a single argument, the function surrounding that address is dumped.\n\
  1771. Two arguments are taken as a range of memory to dump.");
  1772.  
  1773.   add_com ("ptype", class_vars, ptype_command,
  1774.        "Print definition of type TYPE.\n\
  1775. Argument may be a type name defined by typedef, or \"struct STRUCTNAME\"\n\
  1776. or \"union UNIONNAME\" or \"enum ENUMNAME\".\n\
  1777. The selected stack frame's lexical context is used to look up the name.");
  1778.  
  1779.   add_com ("whatis", class_vars, whatis_command,
  1780.        "Print data type of expression EXP.");
  1781.  
  1782.   add_info ("display", display_info,
  1783.         "Expressions to display when program stops, with code numbers.");
  1784.  
  1785.   add_cmd ("undisplay", class_vars, undisplay_command,
  1786.        "Cancel some expressions to be displayed when program stops.\n\
  1787. Arguments are the code numbers of the expressions to stop displaying.\n\
  1788. No argument means cancel all automatic-display expressions.\n\
  1789. \"delete display\" has the same effect as this command.\n\
  1790. Do \"info display\" to see current list of code numbers.",
  1791.           &cmdlist);
  1792.  
  1793.   add_com ("display", class_vars, display_command,
  1794.        "Print value of expression EXP each time the program stops.\n\
  1795. /FMT may be used before EXP as in the \"print\" command.\n\
  1796. /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
  1797. as in the \"x\" command, and then EXP is used to get the address to examine\n\
  1798. and examining is done as in the \"x\" command.\n\n\
  1799. With no argument, display all currently requested auto-display expressions.\n\
  1800. Use \"undisplay\" to cancel display requests previously made.");
  1801.  
  1802.   add_cmd ("display", class_vars, enable_display, 
  1803.        "Enable some expressions to be displayed when program stops.\n\
  1804. Arguments are the code numbers of the expressions to resume displaying.\n\
  1805. No argument means enable all automatic-display expressions.\n\
  1806. Do \"info display\" to see current list of code numbers.", &enablelist);
  1807.  
  1808.   add_cmd ("display", class_vars, disable_display_command, 
  1809.        "Disable some expressions to be displayed when program stops.\n\
  1810. Arguments are the code numbers of the expressions to stop displaying.\n\
  1811. No argument means disable all automatic-display expressions.\n\
  1812. Do \"info display\" to see current list of code numbers.", &disablelist);
  1813.  
  1814.   add_cmd ("display", class_vars, undisplay_command, 
  1815.        "Cancel some expressions to be displayed when program stops.\n\
  1816. Arguments are the code numbers of the expressions to stop displaying.\n\
  1817. No argument means cancel all automatic-display expressions.\n\
  1818. Do \"info display\" to see current list of code numbers.", &deletelist);
  1819.  
  1820.   add_com ("printf", class_vars, printf_command,
  1821.     "printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
  1822. This is useful for formatted output in user-defined commands.");
  1823.   add_com ("output", class_vars, output_command,
  1824.        "Like \"print\" but don't put in value history and don't print newline.\n\
  1825. This is useful in user-defined commands.");
  1826.  
  1827.   add_prefix_cmd ("set", class_vars, set_command,
  1828. "Perform an assignment VAR = EXP.\n\
  1829. You must type the \"=\".  VAR may be a debugger \"convenience\" variable\n\
  1830. (names starting with $), a register (a few standard names starting with $),\n\
  1831. or an actual variable in the program being debugged.  EXP is any expression.\n\
  1832. Use \"set variable\" for variables with names identical to set subcommands.\n\
  1833. \nWith a subcommand, this command modifies parts of the gdb environment",
  1834.                   &setlist, "set ", 1, &cmdlist);
  1835.  
  1836.   add_cmd ("variable", class_vars, set_command,
  1837.            "Perform an assignment VAR = EXP.\n\
  1838. You must type the \"=\".  VAR may be a debugger \"convenience\" variable\n\
  1839. (names starting with $), a register (a few standard names starting with $),\n\
  1840. or an actual variable in the program being debugged.  EXP is any expression.\n\
  1841. This may usually be abbreviated to simply \"set\".",
  1842.            &setlist);
  1843.  
  1844.   add_com ("print", class_vars, print_command,
  1845.        concat ("Print value of expression EXP.\n\
  1846. Variables accessible are those of the lexical environment of the selected\n\
  1847. stack frame, plus all those whose scope is global or an entire file.\n\
  1848. \n\
  1849. $NUM gets previous value number NUM.  $ and $$ are the last two values.\n\
  1850. $$NUM refers to NUM'th value back from the last one.\n\
  1851. Names starting with $ refer to registers (with the values they would have\n\
  1852. if the program were to return to the stack frame now selected, restoring\n\
  1853. all registers saved by frames farther in) or else to debugger\n\
  1854. \"convenience\" variables (any such name not a known register).\n\
  1855. Use assignment expressions to give values to convenience variables.\n",
  1856.            "\n\
  1857. \{TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
  1858. @ is a binary operator for treating consecutive data objects\n\
  1859. anywhere in memory as an array.  FOO@NUM gives an array whose first\n\
  1860. element is FOO, whose second element is stored in the space following\n\
  1861. where FOO is stored, etc.  FOO must be an expression whose value\n\
  1862. resides in memory.\n",
  1863.            "\n\
  1864. EXP may be preceded with /FMT, where FMT is a format letter\n\
  1865. but no count or size letter (see \"x\" command)."));
  1866.   add_com_alias ("p", "print", class_vars, 1);
  1867. }
  1868.