Home | History | Annotate | Line # | Download | only in cli
cli-setshow.c revision 1.1.1.3
      1 /* Handle set and show GDB commands.
      2 
      3    Copyright (C) 2000-2015 Free Software Foundation, Inc.
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     17 
     18 #include "defs.h"
     19 #include "readline/tilde.h"
     20 #include "value.h"
     21 #include <ctype.h>
     22 #include "arch-utils.h"
     23 #include "observer.h"
     24 
     25 #include "ui-out.h"
     26 
     27 #include "cli/cli-decode.h"
     28 #include "cli/cli-cmds.h"
     29 #include "cli/cli-setshow.h"
     30 #include "cli/cli-utils.h"
     31 
     32 /* Return true if the change of command parameter should be notified.  */
     33 
     34 static int
     35 notify_command_param_changed_p (int param_changed, struct cmd_list_element *c)
     36 {
     37   if (param_changed == 0)
     38     return 0;
     39 
     40   if (c->theclass == class_maintenance || c->theclass == class_deprecated
     41       || c->theclass == class_obscure)
     42     return 0;
     43 
     44   return 1;
     45 }
     46 
     47 
     48 static enum auto_boolean
     50 parse_auto_binary_operation (const char *arg)
     51 {
     52   if (arg != NULL && *arg != '\0')
     53     {
     54       int length = strlen (arg);
     55 
     56       while (isspace (arg[length - 1]) && length > 0)
     57 	length--;
     58       if (strncmp (arg, "on", length) == 0
     59 	  || strncmp (arg, "1", length) == 0
     60 	  || strncmp (arg, "yes", length) == 0
     61 	  || strncmp (arg, "enable", length) == 0)
     62 	return AUTO_BOOLEAN_TRUE;
     63       else if (strncmp (arg, "off", length) == 0
     64 	       || strncmp (arg, "0", length) == 0
     65 	       || strncmp (arg, "no", length) == 0
     66 	       || strncmp (arg, "disable", length) == 0)
     67 	return AUTO_BOOLEAN_FALSE;
     68       else if (strncmp (arg, "auto", length) == 0
     69 	       || (strncmp (arg, "-1", length) == 0 && length > 1))
     70 	return AUTO_BOOLEAN_AUTO;
     71     }
     72   error (_("\"on\", \"off\" or \"auto\" expected."));
     73   return AUTO_BOOLEAN_AUTO; /* Pacify GCC.  */
     74 }
     75 
     76 /* See cli-setshow.h.  */
     77 
     78 int
     79 parse_cli_boolean_value (const char *arg)
     80 {
     81   int length;
     82 
     83   if (!arg || !*arg)
     84     return 1;
     85 
     86   length = strlen (arg);
     87 
     88   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
     89     length--;
     90 
     91   if (strncmp (arg, "on", length) == 0
     92       || strncmp (arg, "1", length) == 0
     93       || strncmp (arg, "yes", length) == 0
     94       || strncmp (arg, "enable", length) == 0)
     95     return 1;
     96   else if (strncmp (arg, "off", length) == 0
     97 	   || strncmp (arg, "0", length) == 0
     98 	   || strncmp (arg, "no", length) == 0
     99 	   || strncmp (arg, "disable", length) == 0)
    100     return 0;
    101   else
    102     return -1;
    103 }
    104 
    105 void
    107 deprecated_show_value_hack (struct ui_file *ignore_file,
    108 			    int ignore_from_tty,
    109 			    struct cmd_list_element *c,
    110 			    const char *value)
    111 {
    112   /* If there's no command or value, don't try to print it out.  */
    113   if (c == NULL || value == NULL)
    114     return;
    115   /* Print doc minus "show" at start.  */
    116   print_doc_line (gdb_stdout, c->doc + 5);
    117   switch (c->var_type)
    118     {
    119     case var_string:
    120     case var_string_noescape:
    121     case var_optional_filename:
    122     case var_filename:
    123     case var_enum:
    124       printf_filtered ((" is \"%s\".\n"), value);
    125       break;
    126     default:
    127       printf_filtered ((" is %s.\n"), value);
    128       break;
    129     }
    130 }
    131 
    132 /* Returns true if ARG is "unlimited".  */
    133 
    134 static int
    135 is_unlimited_literal (const char *arg)
    136 {
    137   size_t len = sizeof ("unlimited") - 1;
    138 
    139   arg = skip_spaces_const (arg);
    140 
    141   return (strncmp (arg, "unlimited", len) == 0
    142 	  && (isspace (arg[len]) || arg[len] == '\0'));
    143 }
    144 
    145 
    146 /* Do a "set" command.  ARG is NULL if no argument, or the
    147    text of the argument, and FROM_TTY is nonzero if this command is
    148    being entered directly by the user (i.e. these are just like any
    149    other command).  C is the command list element for the command.  */
    150 
    151 void
    152 do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
    153 {
    154   /* A flag to indicate the option is changed or not.  */
    155   int option_changed = 0;
    156 
    157   gdb_assert (c->type == set_cmd);
    158 
    159   switch (c->var_type)
    160     {
    161     case var_string:
    162       {
    163 	char *newobj;
    164 	const char *p;
    165 	char *q;
    166 	int ch;
    167 
    168 	if (arg == NULL)
    169 	  arg = "";
    170 	newobj = (char *) xmalloc (strlen (arg) + 2);
    171 	p = arg;
    172 	q = newobj;
    173 	while ((ch = *p++) != '\000')
    174 	  {
    175 	    if (ch == '\\')
    176 	      {
    177 		/* \ at end of argument is used after spaces
    178 		   so they won't be lost.  */
    179 		/* This is obsolete now that we no longer strip
    180 		   trailing whitespace and actually, the backslash
    181 		   didn't get here in my test, readline or
    182 		   something did something funky with a backslash
    183 		   right before a newline.  */
    184 		if (*p == 0)
    185 		  break;
    186 		ch = parse_escape (get_current_arch (), &p);
    187 		if (ch == 0)
    188 		  break;	/* C loses */
    189 		else if (ch > 0)
    190 		  *q++ = ch;
    191 	      }
    192 	    else
    193 	      *q++ = ch;
    194 	  }
    195 #if 0
    196 	if (*(p - 1) != '\\')
    197 	  *q++ = ' ';
    198 #endif
    199 	*q++ = '\0';
    200 	newobj = (char *) xrealloc (newobj, q - newobj);
    201 
    202 	if (*(char **) c->var == NULL
    203 	    || strcmp (*(char **) c->var, newobj) != 0)
    204 	  {
    205 	    xfree (*(char **) c->var);
    206 	    *(char **) c->var = newobj;
    207 
    208 	    option_changed = 1;
    209 	  }
    210 	else
    211 	  xfree (newobj);
    212       }
    213       break;
    214     case var_string_noescape:
    215       if (arg == NULL)
    216 	arg = "";
    217 
    218       if (*(char **) c->var == NULL || strcmp (*(char **) c->var, arg) != 0)
    219 	{
    220 	  xfree (*(char **) c->var);
    221 	  *(char **) c->var = xstrdup (arg);
    222 
    223 	  option_changed = 1;
    224 	}
    225       break;
    226     case var_filename:
    227       if (arg == NULL)
    228 	error_no_arg (_("filename to set it to."));
    229       /* FALLTHROUGH */
    230     case var_optional_filename:
    231       {
    232 	char *val = NULL;
    233 
    234 	if (arg != NULL)
    235 	  {
    236 	    /* Clear trailing whitespace of filename.  */
    237 	    const char *ptr = arg + strlen (arg) - 1;
    238 	    char *copy;
    239 
    240 	    while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
    241 	      ptr--;
    242 	    copy = xstrndup (arg, ptr + 1 - arg);
    243 
    244 	    val = tilde_expand (copy);
    245 	    xfree (copy);
    246 	  }
    247 	else
    248 	  val = xstrdup ("");
    249 
    250 	if (*(char **) c->var == NULL
    251 	    || strcmp (*(char **) c->var, val) != 0)
    252 	  {
    253 	    xfree (*(char **) c->var);
    254 	    *(char **) c->var = val;
    255 
    256 	    option_changed = 1;
    257 	  }
    258 	else
    259 	  xfree (val);
    260       }
    261       break;
    262     case var_boolean:
    263       {
    264 	int val = parse_cli_boolean_value (arg);
    265 
    266 	if (val < 0)
    267 	  error (_("\"on\" or \"off\" expected."));
    268 	if (val != *(int *) c->var)
    269 	  {
    270 	    *(int *) c->var = val;
    271 
    272 	    option_changed = 1;
    273 	  }
    274       }
    275       break;
    276     case var_auto_boolean:
    277       {
    278 	enum auto_boolean val = parse_auto_binary_operation (arg);
    279 
    280 	if (*(enum auto_boolean *) c->var != val)
    281 	  {
    282 	    *(enum auto_boolean *) c->var = val;
    283 
    284 	    option_changed = 1;
    285 	  }
    286       }
    287       break;
    288     case var_uinteger:
    289     case var_zuinteger:
    290       {
    291 	LONGEST val;
    292 
    293 	if (arg == NULL)
    294 	  {
    295 	    if (c->var_type == var_uinteger)
    296 	      error_no_arg (_("integer to set it to, or \"unlimited\"."));
    297 	    else
    298 	      error_no_arg (_("integer to set it to."));
    299 	  }
    300 
    301 	if (c->var_type == var_uinteger && is_unlimited_literal (arg))
    302 	  val = 0;
    303 	else
    304 	  val = parse_and_eval_long (arg);
    305 
    306 	if (c->var_type == var_uinteger && val == 0)
    307 	  val = UINT_MAX;
    308 	else if (val < 0
    309 		 /* For var_uinteger, don't let the user set the value
    310 		    to UINT_MAX directly, as that exposes an
    311 		    implementation detail to the user interface.  */
    312 		 || (c->var_type == var_uinteger && val >= UINT_MAX)
    313 		 || (c->var_type == var_zuinteger && val > UINT_MAX))
    314 	  error (_("integer %s out of range"), plongest (val));
    315 
    316 	if (*(unsigned int *) c->var != val)
    317 	  {
    318 	    *(unsigned int *) c->var = val;
    319 
    320 	    option_changed = 1;
    321 	  }
    322       }
    323       break;
    324     case var_integer:
    325     case var_zinteger:
    326       {
    327 	LONGEST val;
    328 
    329 	if (arg == NULL)
    330 	  {
    331 	    if (c->var_type == var_integer)
    332 	      error_no_arg (_("integer to set it to, or \"unlimited\"."));
    333 	    else
    334 	      error_no_arg (_("integer to set it to."));
    335 	  }
    336 
    337 	if (c->var_type == var_integer && is_unlimited_literal (arg))
    338 	  val = 0;
    339 	else
    340 	  val = parse_and_eval_long (arg);
    341 
    342 	if (val == 0 && c->var_type == var_integer)
    343 	  val = INT_MAX;
    344 	else if (val < INT_MIN
    345 		 /* For var_integer, don't let the user set the value
    346 		    to INT_MAX directly, as that exposes an
    347 		    implementation detail to the user interface.  */
    348 		 || (c->var_type == var_integer && val >= INT_MAX)
    349 		 || (c->var_type == var_zinteger && val > INT_MAX))
    350 	  error (_("integer %s out of range"), plongest (val));
    351 
    352 	if (*(int *) c->var != val)
    353 	  {
    354 	    *(int *) c->var = val;
    355 
    356 	    option_changed = 1;
    357 	  }
    358 	break;
    359       }
    360     case var_enum:
    361       {
    362 	int i;
    363 	int len;
    364 	int nmatches;
    365 	const char *match = NULL;
    366 	char *p;
    367 
    368 	/* If no argument was supplied, print an informative error
    369 	   message.  */
    370 	if (arg == NULL)
    371 	  {
    372 	    char *msg;
    373 	    int msg_len = 0;
    374 
    375 	    for (i = 0; c->enums[i]; i++)
    376 	      msg_len += strlen (c->enums[i]) + 2;
    377 
    378 	    msg = xmalloc (msg_len);
    379 	    *msg = '\0';
    380 	    make_cleanup (xfree, msg);
    381 
    382 	    for (i = 0; c->enums[i]; i++)
    383 	      {
    384 		if (i != 0)
    385 		  strcat (msg, ", ");
    386 		strcat (msg, c->enums[i]);
    387 	      }
    388 	    error (_("Requires an argument. Valid arguments are %s."),
    389 		   msg);
    390 	  }
    391 
    392 	p = strchr (arg, ' ');
    393 
    394 	if (p)
    395 	  len = p - arg;
    396 	else
    397 	  len = strlen (arg);
    398 
    399 	nmatches = 0;
    400 	for (i = 0; c->enums[i]; i++)
    401 	  if (strncmp (arg, c->enums[i], len) == 0)
    402 	    {
    403 	      if (c->enums[i][len] == '\0')
    404 		{
    405 		  match = c->enums[i];
    406 		  nmatches = 1;
    407 		  break; /* Exact match.  */
    408 		}
    409 	      else
    410 		{
    411 		  match = c->enums[i];
    412 		  nmatches++;
    413 		}
    414 	    }
    415 
    416 	if (nmatches <= 0)
    417 	  error (_("Undefined item: \"%s\"."), arg);
    418 
    419 	if (nmatches > 1)
    420 	  error (_("Ambiguous item \"%s\"."), arg);
    421 
    422 	if (*(const char **) c->var != match)
    423 	  {
    424 	    *(const char **) c->var = match;
    425 
    426 	    option_changed = 1;
    427 	  }
    428       }
    429       break;
    430     case var_zuinteger_unlimited:
    431       {
    432 	LONGEST val;
    433 
    434 	if (arg == NULL)
    435 	  error_no_arg (_("integer to set it to, or \"unlimited\"."));
    436 
    437 	if (is_unlimited_literal (arg))
    438 	  val = -1;
    439 	else
    440 	  val = parse_and_eval_long (arg);
    441 
    442 	if (val > INT_MAX)
    443 	  error (_("integer %s out of range"), plongest (val));
    444 	else if (val < -1)
    445 	  error (_("only -1 is allowed to set as unlimited"));
    446 
    447 	if (*(int *) c->var != val)
    448 	  {
    449 	    *(int *) c->var = val;
    450 	    option_changed = 1;
    451 	  }
    452       }
    453       break;
    454     default:
    455       error (_("gdb internal error: bad var_type in do_setshow_command"));
    456     }
    457   c->func (c, NULL, from_tty);
    458 
    459   if (notify_command_param_changed_p (option_changed, c))
    460     {
    461       char *name, *cp;
    462       struct cmd_list_element **cmds;
    463       struct cmd_list_element *p;
    464       int i;
    465       int length = 0;
    466 
    467       /* Compute the whole multi-word command options.  If user types command
    468 	 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
    469 	 command option change notification, because it is confusing.  We can
    470 	 trace back through field 'prefix' to compute the whole options,
    471 	 and pass "foo bar baz" to notification.  */
    472 
    473       for (i = 0, p = c; p != NULL; i++)
    474 	{
    475 	  length += strlen (p->name);
    476 	  length++;
    477 
    478 	  p = p->prefix;
    479 	}
    480       cp = name = xmalloc (length);
    481       cmds = xmalloc (sizeof (struct cmd_list_element *) * i);
    482 
    483       /* Track back through filed 'prefix' and cache them in CMDS.  */
    484       for (i = 0, p = c; p != NULL; i++)
    485 	{
    486 	  cmds[i] = p;
    487 	  p = p->prefix;
    488 	}
    489 
    490       /* Don't trigger any observer notification if prefixlist is not
    491 	 setlist.  */
    492       i--;
    493       if (cmds[i]->prefixlist != &setlist)
    494 	{
    495 	  xfree (cmds);
    496 	  xfree (name);
    497 
    498 	  return;
    499 	}
    500       /* Traverse them in the reversed order, and copy their names into
    501 	 NAME.  */
    502       for (i--; i >= 0; i--)
    503 	{
    504 	  memcpy (cp, cmds[i]->name, strlen (cmds[i]->name));
    505 	  cp += strlen (cmds[i]->name);
    506 
    507 	  if (i != 0)
    508 	    {
    509 	      cp[0] = ' ';
    510 	      cp++;
    511 	    }
    512 	}
    513       cp[0] = 0;
    514 
    515       xfree (cmds);
    516 
    517       switch (c->var_type)
    518 	{
    519 	case var_string:
    520 	case var_string_noescape:
    521 	case var_filename:
    522 	case var_optional_filename:
    523 	case var_enum:
    524 	  observer_notify_command_param_changed (name, *(char **) c->var);
    525 	  break;
    526 	case var_boolean:
    527 	  {
    528 	    char *opt = *(int *) c->var ? "on" : "off";
    529 
    530 	    observer_notify_command_param_changed (name, opt);
    531 	  }
    532 	  break;
    533 	case var_auto_boolean:
    534 	  {
    535 	    const char *s = auto_boolean_enums[*(enum auto_boolean *) c->var];
    536 
    537 	    observer_notify_command_param_changed (name, s);
    538 	  }
    539 	  break;
    540 	case var_uinteger:
    541 	case var_zuinteger:
    542 	  {
    543 	    char s[64];
    544 
    545 	    xsnprintf (s, sizeof s, "%u", *(unsigned int *) c->var);
    546 	    observer_notify_command_param_changed (name, s);
    547 	  }
    548 	  break;
    549 	case var_integer:
    550 	case var_zinteger:
    551 	case var_zuinteger_unlimited:
    552 	  {
    553 	    char s[64];
    554 
    555 	    xsnprintf (s, sizeof s, "%d", *(int *) c->var);
    556 	    observer_notify_command_param_changed (name, s);
    557 	  }
    558 	  break;
    559 	}
    560       xfree (name);
    561     }
    562 }
    563 
    564 /* Do a "show" command.  ARG is NULL if no argument, or the
    565    text of the argument, and FROM_TTY is nonzero if this command is
    566    being entered directly by the user (i.e. these are just like any
    567    other command).  C is the command list element for the command.  */
    568 
    569 void
    570 do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
    571 {
    572   struct ui_out *uiout = current_uiout;
    573   struct cleanup *old_chain;
    574   struct ui_file *stb;
    575 
    576   gdb_assert (c->type == show_cmd);
    577 
    578   stb = mem_fileopen ();
    579   old_chain = make_cleanup_ui_file_delete (stb);
    580 
    581   /* Possibly call the pre hook.  */
    582   if (c->pre_show_hook)
    583     (c->pre_show_hook) (c);
    584 
    585   switch (c->var_type)
    586     {
    587     case var_string:
    588       if (*(char **) c->var)
    589 	fputstr_filtered (*(char **) c->var, '"', stb);
    590       break;
    591     case var_string_noescape:
    592     case var_optional_filename:
    593     case var_filename:
    594     case var_enum:
    595       if (*(char **) c->var)
    596 	fputs_filtered (*(char **) c->var, stb);
    597       break;
    598     case var_boolean:
    599       fputs_filtered (*(int *) c->var ? "on" : "off", stb);
    600       break;
    601     case var_auto_boolean:
    602       switch (*(enum auto_boolean*) c->var)
    603 	{
    604 	case AUTO_BOOLEAN_TRUE:
    605 	  fputs_filtered ("on", stb);
    606 	  break;
    607 	case AUTO_BOOLEAN_FALSE:
    608 	  fputs_filtered ("off", stb);
    609 	  break;
    610 	case AUTO_BOOLEAN_AUTO:
    611 	  fputs_filtered ("auto", stb);
    612 	  break;
    613 	default:
    614 	  internal_error (__FILE__, __LINE__,
    615 			  _("do_show_command: "
    616 			    "invalid var_auto_boolean"));
    617 	  break;
    618 	}
    619       break;
    620     case var_uinteger:
    621     case var_zuinteger:
    622       if (c->var_type == var_uinteger
    623 	  && *(unsigned int *) c->var == UINT_MAX)
    624 	fputs_filtered ("unlimited", stb);
    625       else
    626 	fprintf_filtered (stb, "%u", *(unsigned int *) c->var);
    627       break;
    628     case var_integer:
    629     case var_zinteger:
    630       if (c->var_type == var_integer
    631 	  && *(int *) c->var == INT_MAX)
    632 	fputs_filtered ("unlimited", stb);
    633       else
    634 	fprintf_filtered (stb, "%d", *(int *) c->var);
    635       break;
    636     case var_zuinteger_unlimited:
    637       {
    638 	if (*(int *) c->var == -1)
    639 	  fputs_filtered ("unlimited", stb);
    640 	else
    641 	  fprintf_filtered (stb, "%d", *(int *) c->var);
    642       }
    643       break;
    644     default:
    645       error (_("gdb internal error: bad var_type in do_show_command"));
    646     }
    647 
    648 
    649   /* FIXME: cagney/2005-02-10: Need to split this in half: code to
    650      convert the value into a string (esentially the above); and
    651      code to print the value out.  For the latter there should be
    652      MI and CLI specific versions.  */
    653 
    654   if (ui_out_is_mi_like_p (uiout))
    655     ui_out_field_stream (uiout, "value", stb);
    656   else
    657     {
    658       char *value = ui_file_xstrdup (stb, NULL);
    659 
    660       make_cleanup (xfree, value);
    661       if (c->show_value_func != NULL)
    662 	c->show_value_func (gdb_stdout, from_tty, c, value);
    663       else
    664 	deprecated_show_value_hack (gdb_stdout, from_tty, c, value);
    665     }
    666   do_cleanups (old_chain);
    667 
    668   c->func (c, NULL, from_tty);
    669 }
    670 
    671 /* Show all the settings in a list of show commands.  */
    672 
    673 void
    674 cmd_show_list (struct cmd_list_element *list, int from_tty, const char *prefix)
    675 {
    676   struct cleanup *showlist_chain;
    677   struct ui_out *uiout = current_uiout;
    678 
    679   showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
    680   for (; list != NULL; list = list->next)
    681     {
    682       /* If we find a prefix, run its list, prefixing our output by its
    683          prefix (with "show " skipped).  */
    684       if (list->prefixlist && !list->abbrev_flag)
    685 	{
    686 	  struct cleanup *optionlist_chain
    687 	    = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
    688 	  char *new_prefix = strstr (list->prefixname, "show ") + 5;
    689 
    690 	  if (ui_out_is_mi_like_p (uiout))
    691 	    ui_out_field_string (uiout, "prefix", new_prefix);
    692 	  cmd_show_list (*list->prefixlist, from_tty, new_prefix);
    693 	  /* Close the tuple.  */
    694 	  do_cleanups (optionlist_chain);
    695 	}
    696       else
    697 	{
    698 	  if (list->theclass != no_set_class)
    699 	    {
    700 	      struct cleanup *option_chain
    701 		= make_cleanup_ui_out_tuple_begin_end (uiout, "option");
    702 
    703 	      ui_out_text (uiout, prefix);
    704 	      ui_out_field_string (uiout, "name", list->name);
    705 	      ui_out_text (uiout, ":  ");
    706 	      if (list->type == show_cmd)
    707 		do_show_command ((char *) NULL, from_tty, list);
    708 	      else
    709 		cmd_func (list, NULL, from_tty);
    710 	      /* Close the tuple.  */
    711 	      do_cleanups (option_chain);
    712 	    }
    713 	}
    714     }
    715   /* Close the tuple.  */
    716   do_cleanups (showlist_chain);
    717 }
    718 
    719