home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / printcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  51.8 KB  |  2,054 lines

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