gdb/testsuite: fix gdb.trace/signal.exp on x86
[binutils-gdb/blckswan.git] / gdb / cli / cli-setshow.c
blob213573e443e9fa653b5a943ad93b5cdb0a38053f
1 /* Handle set and show GDB commands.
3 Copyright (C) 2000-2022 Free Software Foundation, Inc.
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.
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.
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/>. */
18 #include "defs.h"
19 #include "readline/tilde.h"
20 #include "value.h"
21 #include <ctype.h>
22 #include "arch-utils.h"
23 #include "observable.h"
25 #include "ui-out.h"
27 #include "cli/cli-decode.h"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-setshow.h"
30 #include "cli/cli-utils.h"
32 /* Return true if the change of command parameter should be notified. */
34 static bool
35 notify_command_param_changed_p (bool param_changed, struct cmd_list_element *c)
37 if (!param_changed)
38 return false;
40 return c->theclass != class_maintenance && c->theclass != class_obscure;
44 static enum auto_boolean
45 parse_auto_binary_operation (const char *arg)
47 if (arg != NULL && *arg != '\0')
49 int length = strlen (arg);
51 while (isspace (arg[length - 1]) && length > 0)
52 length--;
54 /* Note that "o" is ambiguous. */
56 if ((length == 2 && strncmp (arg, "on", length) == 0)
57 || strncmp (arg, "1", length) == 0
58 || strncmp (arg, "yes", length) == 0
59 || strncmp (arg, "enable", length) == 0)
60 return AUTO_BOOLEAN_TRUE;
61 else if ((length >= 2 && strncmp (arg, "off", length) == 0)
62 || strncmp (arg, "0", length) == 0
63 || strncmp (arg, "no", length) == 0
64 || strncmp (arg, "disable", length) == 0)
65 return AUTO_BOOLEAN_FALSE;
66 else if (strncmp (arg, "auto", length) == 0
67 || (length > 1 && strncmp (arg, "-1", length) == 0))
68 return AUTO_BOOLEAN_AUTO;
70 error (_("\"on\", \"off\" or \"auto\" expected."));
71 return AUTO_BOOLEAN_AUTO; /* Pacify GCC. */
74 /* See cli-setshow.h. */
76 int
77 parse_cli_boolean_value (const char **arg)
79 const char *p = skip_to_space (*arg);
80 size_t length = p - *arg;
82 /* Note that "o" is ambiguous. */
84 if ((length == 2 && strncmp (*arg, "on", length) == 0)
85 || strncmp (*arg, "1", length) == 0
86 || strncmp (*arg, "yes", length) == 0
87 || strncmp (*arg, "enable", length) == 0)
89 *arg = skip_spaces (*arg + length);
90 return 1;
92 else if ((length >= 2 && strncmp (*arg, "off", length) == 0)
93 || strncmp (*arg, "0", length) == 0
94 || strncmp (*arg, "no", length) == 0
95 || strncmp (*arg, "disable", length) == 0)
97 *arg = skip_spaces (*arg + length);
98 return 0;
100 else
101 return -1;
104 /* See cli-setshow.h. */
107 parse_cli_boolean_value (const char *arg)
109 if (!arg || !*arg)
110 return 1;
112 int b = parse_cli_boolean_value (&arg);
113 if (b >= 0 && *arg != '\0')
114 return -1;
116 return b;
120 void
121 deprecated_show_value_hack (struct ui_file *ignore_file,
122 int ignore_from_tty,
123 struct cmd_list_element *c,
124 const char *value)
126 /* If there's no command or value, don't try to print it out. */
127 if (c == NULL || value == NULL)
128 return;
130 /* Print doc minus "Show " at start. Tell print_doc_line that
131 this is for a 'show value' prefix. */
132 print_doc_line (gdb_stdout, c->doc + 5, true);
134 gdb_assert (c->var.has_value ());
136 switch (c->var->type ())
138 case var_string:
139 case var_string_noescape:
140 case var_optional_filename:
141 case var_filename:
142 case var_enum:
143 gdb_printf ((" is \"%s\".\n"), value);
144 break;
146 default:
147 gdb_printf ((" is %s.\n"), value);
148 break;
152 /* Returns true if ARG is "unlimited". */
154 static bool
155 is_unlimited_literal (const char **arg, bool expression)
157 *arg = skip_spaces (*arg);
159 const char *unl_start = *arg;
161 const char *p = skip_to_space (*arg);
163 size_t len = p - *arg;
165 if (len > 0 && strncmp ("unlimited", *arg, len) == 0)
167 *arg += len;
169 /* If parsing an expression (i.e., parsing for a "set" command),
170 anything after "unlimited" is junk. For options, anything
171 after "unlimited" might be a command argument or another
172 option. */
173 if (expression)
175 const char *after = skip_spaces (*arg);
176 if (*after != '\0')
177 error (_("Junk after \"%.*s\": %s"),
178 (int) len, unl_start, after);
181 return true;
184 return false;
187 /* See cli-setshow.h. */
189 unsigned int
190 parse_cli_var_uinteger (var_types var_type, const char **arg,
191 bool expression)
193 LONGEST val;
195 if (*arg == nullptr || **arg == '\0')
197 if (var_type == var_uinteger)
198 error_no_arg (_("integer to set it to, or \"unlimited\"."));
199 else
200 error_no_arg (_("integer to set it to."));
203 if (var_type == var_uinteger && is_unlimited_literal (arg, expression))
204 val = 0;
205 else if (expression)
206 val = parse_and_eval_long (*arg);
207 else
208 val = get_ulongest (arg);
210 if (var_type == var_uinteger && val == 0)
211 val = UINT_MAX;
212 else if (val < 0
213 /* For var_uinteger, don't let the user set the value
214 to UINT_MAX directly, as that exposes an
215 implementation detail to the user interface. */
216 || (var_type == var_uinteger && val >= UINT_MAX)
217 || (var_type == var_zuinteger && val > UINT_MAX))
218 error (_("integer %s out of range"), plongest (val));
220 return val;
223 /* See cli-setshow.h. */
226 parse_cli_var_zuinteger_unlimited (const char **arg, bool expression)
228 LONGEST val;
230 if (*arg == nullptr || **arg == '\0')
231 error_no_arg (_("integer to set it to, or \"unlimited\"."));
233 if (is_unlimited_literal (arg, expression))
234 val = -1;
235 else if (expression)
236 val = parse_and_eval_long (*arg);
237 else
238 val = get_ulongest (arg);
240 if (val > INT_MAX)
241 error (_("integer %s out of range"), plongest (val));
242 else if (val < -1)
243 error (_("only -1 is allowed to set as unlimited"));
245 return val;
248 /* See cli-setshow.h. */
250 const char *
251 parse_cli_var_enum (const char **args, const char *const *enums)
253 /* If no argument was supplied, print an informative error
254 message. */
255 if (args == NULL || *args == NULL || **args == '\0')
257 std::string msg;
259 for (size_t i = 0; enums[i]; i++)
261 if (i != 0)
262 msg += ", ";
263 msg += enums[i];
265 error (_("Requires an argument. Valid arguments are %s."),
266 msg.c_str ());
269 const char *p = skip_to_space (*args);
270 size_t len = p - *args;
272 int nmatches = 0;
273 const char *match = NULL;
274 for (size_t i = 0; enums[i]; i++)
275 if (strncmp (*args, enums[i], len) == 0)
277 if (enums[i][len] == '\0')
279 match = enums[i];
280 nmatches = 1;
281 break; /* Exact match. */
283 else
285 match = enums[i];
286 nmatches++;
290 if (nmatches == 0)
291 error (_("Undefined item: \"%.*s\"."), (int) len, *args);
293 if (nmatches > 1)
294 error (_("Ambiguous item \"%.*s\"."), (int) len, *args);
296 *args += len;
297 return match;
300 /* Do a "set" command. ARG is NULL if no argument, or the
301 text of the argument, and FROM_TTY is nonzero if this command is
302 being entered directly by the user (i.e. these are just like any
303 other command). C is the command list element for the command. */
305 void
306 do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
308 /* A flag to indicate the option is changed or not. */
309 bool option_changed = false;
311 gdb_assert (c->type == set_cmd);
313 if (arg == NULL)
314 arg = "";
316 gdb_assert (c->var.has_value ());
318 switch (c->var->type ())
320 case var_string:
322 char *newobj;
323 const char *p;
324 char *q;
325 int ch;
327 newobj = (char *) xmalloc (strlen (arg) + 2);
328 p = arg;
329 q = newobj;
330 while ((ch = *p++) != '\000')
332 if (ch == '\\')
334 /* \ at end of argument is used after spaces
335 so they won't be lost. */
336 /* This is obsolete now that we no longer strip
337 trailing whitespace and actually, the backslash
338 didn't get here in my test, readline or
339 something did something funky with a backslash
340 right before a newline. */
341 if (*p == 0)
342 break;
343 ch = parse_escape (get_current_arch (), &p);
344 if (ch == 0)
345 break; /* C loses */
346 else if (ch > 0)
347 *q++ = ch;
349 else
350 *q++ = ch;
352 #if 0
353 if (*(p - 1) != '\\')
354 *q++ = ' ';
355 #endif
356 *q++ = '\0';
357 newobj = (char *) xrealloc (newobj, q - newobj);
359 option_changed = c->var->set<std::string> (std::string (newobj));
360 xfree (newobj);
362 break;
363 case var_string_noescape:
364 option_changed = c->var->set<std::string> (std::string (arg));
365 break;
366 case var_filename:
367 if (*arg == '\0')
368 error_no_arg (_("filename to set it to."));
369 /* FALLTHROUGH */
370 case var_optional_filename:
372 char *val = NULL;
374 if (*arg != '\0')
376 /* Clear trailing whitespace of filename. */
377 const char *ptr = arg + strlen (arg) - 1;
379 while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
380 ptr--;
381 gdb::unique_xmalloc_ptr<char> copy
382 = make_unique_xstrndup (arg, ptr + 1 - arg);
384 val = tilde_expand (copy.get ());
386 else
387 val = xstrdup ("");
389 option_changed
390 = c->var->set<std::string> (std::string (val));
391 xfree (val);
393 break;
394 case var_boolean:
396 int val = parse_cli_boolean_value (arg);
398 if (val < 0)
399 error (_("\"on\" or \"off\" expected."));
401 option_changed = c->var->set<bool> (val);
403 break;
404 case var_auto_boolean:
405 option_changed = c->var->set<enum auto_boolean> (parse_auto_binary_operation (arg));
406 break;
407 case var_uinteger:
408 case var_zuinteger:
409 option_changed
410 = c->var->set<unsigned int> (parse_cli_var_uinteger (c->var->type (),
411 &arg, true));
412 break;
413 case var_integer:
414 case var_zinteger:
416 LONGEST val;
418 if (*arg == '\0')
420 if (c->var->type () == var_integer)
421 error_no_arg (_("integer to set it to, or \"unlimited\"."));
422 else
423 error_no_arg (_("integer to set it to."));
426 if (c->var->type () == var_integer && is_unlimited_literal (&arg, true))
427 val = 0;
428 else
429 val = parse_and_eval_long (arg);
431 if (val == 0 && c->var->type () == var_integer)
432 val = INT_MAX;
433 else if (val < INT_MIN
434 /* For var_integer, don't let the user set the value
435 to INT_MAX directly, as that exposes an
436 implementation detail to the user interface. */
437 || (c->var->type () == var_integer && val >= INT_MAX)
438 || (c->var->type () == var_zinteger && val > INT_MAX))
439 error (_("integer %s out of range"), plongest (val));
441 option_changed = c->var->set<int> (val);
443 break;
444 case var_enum:
446 const char *end_arg = arg;
447 const char *match = parse_cli_var_enum (&end_arg, c->enums);
449 int len = end_arg - arg;
450 const char *after = skip_spaces (end_arg);
451 if (*after != '\0')
452 error (_("Junk after item \"%.*s\": %s"), len, arg, after);
454 option_changed = c->var->set<const char *> (match);
456 break;
457 case var_zuinteger_unlimited:
458 option_changed = c->var->set<int>
459 (parse_cli_var_zuinteger_unlimited (&arg, true));
460 break;
461 default:
462 error (_("gdb internal error: bad var_type in do_setshow_command"));
465 c->func (NULL, from_tty, c);
467 if (notify_command_param_changed_p (option_changed, c))
469 char *name, *cp;
470 struct cmd_list_element **cmds;
471 struct cmd_list_element *p;
472 int i;
473 int length = 0;
475 /* Compute the whole multi-word command options. If user types command
476 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
477 command option change notification, because it is confusing. We can
478 trace back through field 'prefix' to compute the whole options,
479 and pass "foo bar baz" to notification. */
481 for (i = 0, p = c; p != NULL; i++)
483 length += strlen (p->name);
484 length++;
486 p = p->prefix;
488 cp = name = (char *) xmalloc (length);
489 cmds = XNEWVEC (struct cmd_list_element *, i);
491 /* Track back through filed 'prefix' and cache them in CMDS. */
492 for (i = 0, p = c; p != NULL; i++)
494 cmds[i] = p;
495 p = p->prefix;
498 /* Don't trigger any observer notification if subcommands is not
499 setlist. */
500 i--;
501 if (cmds[i]->subcommands != &setlist)
503 xfree (cmds);
504 xfree (name);
506 return;
508 /* Traverse them in the reversed order, and copy their names into
509 NAME. */
510 for (i--; i >= 0; i--)
512 memcpy (cp, cmds[i]->name, strlen (cmds[i]->name));
513 cp += strlen (cmds[i]->name);
515 if (i != 0)
517 cp[0] = ' ';
518 cp++;
521 cp[0] = 0;
523 xfree (cmds);
525 switch (c->var->type ())
527 case var_string:
528 case var_string_noescape:
529 case var_filename:
530 case var_optional_filename:
531 gdb::observers::command_param_changed.notify
532 (name, c->var->get<std::string> ().c_str ());
533 break;
534 case var_enum:
535 gdb::observers::command_param_changed.notify
536 (name, c->var->get<const char *> ());
537 break;
538 case var_boolean:
540 const char *opt = c->var->get<bool> () ? "on" : "off";
542 gdb::observers::command_param_changed.notify (name, opt);
544 break;
545 case var_auto_boolean:
547 const char *s
548 = auto_boolean_enums[c->var->get<enum auto_boolean> ()];
550 gdb::observers::command_param_changed.notify (name, s);
552 break;
553 case var_uinteger:
554 case var_zuinteger:
556 char s[64];
558 xsnprintf (s, sizeof s, "%u", c->var->get<unsigned int> ());
559 gdb::observers::command_param_changed.notify (name, s);
561 break;
562 case var_integer:
563 case var_zinteger:
564 case var_zuinteger_unlimited:
566 char s[64];
568 xsnprintf (s, sizeof s, "%d", c->var->get<int> ());
569 gdb::observers::command_param_changed.notify (name, s);
571 break;
573 xfree (name);
577 /* See cli/cli-setshow.h. */
579 std::string
580 get_setshow_command_value_string (const setting &var)
582 string_file stb;
584 switch (var.type ())
586 case var_string:
588 std::string value = var.get<std::string> ();
589 if (!value.empty ())
590 stb.putstr (value.c_str (), '"');
592 break;
593 case var_string_noescape:
594 case var_optional_filename:
595 case var_filename:
596 stb.puts (var.get<std::string> ().c_str ());
597 break;
598 case var_enum:
600 const char *value = var.get<const char *> ();
601 if (value != nullptr)
602 stb.puts (value);
604 break;
605 case var_boolean:
606 stb.puts (var.get<bool> () ? "on" : "off");
607 break;
608 case var_auto_boolean:
609 switch (var.get<enum auto_boolean> ())
611 case AUTO_BOOLEAN_TRUE:
612 stb.puts ("on");
613 break;
614 case AUTO_BOOLEAN_FALSE:
615 stb.puts ("off");
616 break;
617 case AUTO_BOOLEAN_AUTO:
618 stb.puts ("auto");
619 break;
620 default:
621 gdb_assert_not_reached ("invalid var_auto_boolean");
622 break;
624 break;
625 case var_uinteger:
626 case var_zuinteger:
628 const unsigned int value = var.get<unsigned int> ();
630 if (var.type () == var_uinteger
631 && value == UINT_MAX)
632 stb.puts ("unlimited");
633 else
634 stb.printf ("%u", value);
636 break;
637 case var_integer:
638 case var_zinteger:
640 const int value = var.get<int> ();
642 if (var.type () == var_integer
643 && value == INT_MAX)
644 stb.puts ("unlimited");
645 else
646 stb.printf ("%d", value);
648 break;
649 case var_zuinteger_unlimited:
651 const int value = var.get<int> ();
652 if (value == -1)
653 stb.puts ("unlimited");
654 else
655 stb.printf ("%d", value);
657 break;
658 default:
659 gdb_assert_not_reached ("bad var_type");
662 return stb.release ();
666 /* Do a "show" command. ARG is NULL if no argument, or the
667 text of the argument, and FROM_TTY is nonzero if this command is
668 being entered directly by the user (i.e. these are just like any
669 other command). C is the command list element for the command. */
671 void
672 do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
674 struct ui_out *uiout = current_uiout;
676 gdb_assert (c->type == show_cmd);
677 gdb_assert (c->var.has_value ());
679 std::string val = get_setshow_command_value_string (*c->var);
681 /* FIXME: cagney/2005-02-10: There should be MI and CLI specific
682 versions of code to print the value out. */
684 if (uiout->is_mi_like_p ())
685 uiout->field_string ("value", val);
686 else
688 if (c->show_value_func != NULL)
689 c->show_value_func (gdb_stdout, from_tty, c, val.c_str ());
690 else
691 deprecated_show_value_hack (gdb_stdout, from_tty, c, val.c_str ());
694 c->func (NULL, from_tty, c);
697 /* Show all the settings in a list of show commands. */
699 void
700 cmd_show_list (struct cmd_list_element *list, int from_tty)
702 struct ui_out *uiout = current_uiout;
704 ui_out_emit_tuple tuple_emitter (uiout, "showlist");
705 for (; list != NULL; list = list->next)
707 /* We skip show command aliases to avoid showing duplicated values. */
709 /* If we find a prefix, run its list, prefixing our output by its
710 prefix (with "show " skipped). */
711 if (list->is_prefix () && !list->is_alias ())
713 ui_out_emit_tuple optionlist_emitter (uiout, "optionlist");
714 std::string prefixname = list->prefixname ();
715 const char *new_prefix = strstr (prefixname.c_str (), "show ") + 5;
717 if (uiout->is_mi_like_p ())
718 uiout->field_string ("prefix", new_prefix);
719 cmd_show_list (*list->subcommands, from_tty);
721 else if (list->theclass != no_set_class && !list->is_alias ())
723 ui_out_emit_tuple option_emitter (uiout, "option");
725 if (list->prefix != nullptr)
727 /* If we find a prefix, output it (with "show " skipped). */
728 std::string prefixname = list->prefix->prefixname ();
729 prefixname = (!list->prefix->is_prefix () ? ""
730 : strstr (prefixname.c_str (), "show ") + 5);
731 uiout->text (prefixname);
733 uiout->field_string ("name", list->name);
734 uiout->text (": ");
735 if (list->type == show_cmd)
736 do_show_command (NULL, from_tty, list);
737 else
738 cmd_func (list, NULL, from_tty);