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/>. */
19 #include "readline/tilde.h"
22 #include "arch-utils.h"
23 #include "observable.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. */
35 notify_command_param_changed_p (bool param_changed
, struct cmd_list_element
*c
)
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)
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. */
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
);
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
);
104 /* See cli-setshow.h. */
107 parse_cli_boolean_value (const char *arg
)
112 int b
= parse_cli_boolean_value (&arg
);
113 if (b
>= 0 && *arg
!= '\0')
121 deprecated_show_value_hack (struct ui_file
*ignore_file
,
123 struct cmd_list_element
*c
,
126 /* If there's no command or value, don't try to print it out. */
127 if (c
== NULL
|| value
== NULL
)
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 ())
139 case var_string_noescape
:
140 case var_optional_filename
:
143 gdb_printf ((" is \"%s\".\n"), value
);
147 gdb_printf ((" is %s.\n"), value
);
152 /* Returns true if ARG is "unlimited". */
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)
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
175 const char *after
= skip_spaces (*arg
);
177 error (_("Junk after \"%.*s\": %s"),
178 (int) len
, unl_start
, after
);
187 /* See cli-setshow.h. */
190 parse_cli_var_uinteger (var_types var_type
, const char **arg
,
195 if (*arg
== nullptr || **arg
== '\0')
197 if (var_type
== var_uinteger
)
198 error_no_arg (_("integer to set it to, or \"unlimited\"."));
200 error_no_arg (_("integer to set it to."));
203 if (var_type
== var_uinteger
&& is_unlimited_literal (arg
, expression
))
206 val
= parse_and_eval_long (*arg
);
208 val
= get_ulongest (arg
);
210 if (var_type
== var_uinteger
&& 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
));
223 /* See cli-setshow.h. */
226 parse_cli_var_zuinteger_unlimited (const char **arg
, bool expression
)
230 if (*arg
== nullptr || **arg
== '\0')
231 error_no_arg (_("integer to set it to, or \"unlimited\"."));
233 if (is_unlimited_literal (arg
, expression
))
236 val
= parse_and_eval_long (*arg
);
238 val
= get_ulongest (arg
);
241 error (_("integer %s out of range"), plongest (val
));
243 error (_("only -1 is allowed to set as unlimited"));
248 /* See cli-setshow.h. */
251 parse_cli_var_enum (const char **args
, const char *const *enums
)
253 /* If no argument was supplied, print an informative error
255 if (args
== NULL
|| *args
== NULL
|| **args
== '\0')
259 for (size_t i
= 0; enums
[i
]; i
++)
265 error (_("Requires an argument. Valid arguments are %s."),
269 const char *p
= skip_to_space (*args
);
270 size_t len
= p
- *args
;
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')
281 break; /* Exact match. */
291 error (_("Undefined item: \"%.*s\"."), (int) len
, *args
);
294 error (_("Ambiguous item \"%.*s\"."), (int) len
, *args
);
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. */
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
);
316 gdb_assert (c
->var
.has_value ());
318 switch (c
->var
->type ())
327 newobj
= (char *) xmalloc (strlen (arg
) + 2);
330 while ((ch
= *p
++) != '\000')
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. */
343 ch
= parse_escape (get_current_arch (), &p
);
353 if (*(p
- 1) != '\\')
357 newobj
= (char *) xrealloc (newobj
, q
- newobj
);
359 option_changed
= c
->var
->set
<std::string
> (std::string (newobj
));
363 case var_string_noescape
:
364 option_changed
= c
->var
->set
<std::string
> (std::string (arg
));
368 error_no_arg (_("filename to set it to."));
370 case var_optional_filename
:
376 /* Clear trailing whitespace of filename. */
377 const char *ptr
= arg
+ strlen (arg
) - 1;
379 while (ptr
>= arg
&& (*ptr
== ' ' || *ptr
== '\t'))
381 gdb::unique_xmalloc_ptr
<char> copy
382 = make_unique_xstrndup (arg
, ptr
+ 1 - arg
);
384 val
= tilde_expand (copy
.get ());
390 = c
->var
->set
<std::string
> (std::string (val
));
396 int val
= parse_cli_boolean_value (arg
);
399 error (_("\"on\" or \"off\" expected."));
401 option_changed
= c
->var
->set
<bool> (val
);
404 case var_auto_boolean
:
405 option_changed
= c
->var
->set
<enum auto_boolean
> (parse_auto_binary_operation (arg
));
410 = c
->var
->set
<unsigned int> (parse_cli_var_uinteger (c
->var
->type (),
420 if (c
->var
->type () == var_integer
)
421 error_no_arg (_("integer to set it to, or \"unlimited\"."));
423 error_no_arg (_("integer to set it to."));
426 if (c
->var
->type () == var_integer
&& is_unlimited_literal (&arg
, true))
429 val
= parse_and_eval_long (arg
);
431 if (val
== 0 && c
->var
->type () == var_integer
)
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
);
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
);
452 error (_("Junk after item \"%.*s\": %s"), len
, arg
, after
);
454 option_changed
= c
->var
->set
<const char *> (match
);
457 case var_zuinteger_unlimited
:
458 option_changed
= c
->var
->set
<int>
459 (parse_cli_var_zuinteger_unlimited (&arg
, true));
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
))
470 struct cmd_list_element
**cmds
;
471 struct cmd_list_element
*p
;
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
);
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
++)
498 /* Don't trigger any observer notification if subcommands is not
501 if (cmds
[i
]->subcommands
!= &setlist
)
508 /* Traverse them in the reversed order, and copy their names into
510 for (i
--; i
>= 0; i
--)
512 memcpy (cp
, cmds
[i
]->name
, strlen (cmds
[i
]->name
));
513 cp
+= strlen (cmds
[i
]->name
);
525 switch (c
->var
->type ())
528 case var_string_noescape
:
530 case var_optional_filename
:
531 gdb::observers::command_param_changed
.notify
532 (name
, c
->var
->get
<std::string
> ().c_str ());
535 gdb::observers::command_param_changed
.notify
536 (name
, c
->var
->get
<const char *> ());
540 const char *opt
= c
->var
->get
<bool> () ? "on" : "off";
542 gdb::observers::command_param_changed
.notify (name
, opt
);
545 case var_auto_boolean
:
548 = auto_boolean_enums
[c
->var
->get
<enum auto_boolean
> ()];
550 gdb::observers::command_param_changed
.notify (name
, s
);
558 xsnprintf (s
, sizeof s
, "%u", c
->var
->get
<unsigned int> ());
559 gdb::observers::command_param_changed
.notify (name
, s
);
564 case var_zuinteger_unlimited
:
568 xsnprintf (s
, sizeof s
, "%d", c
->var
->get
<int> ());
569 gdb::observers::command_param_changed
.notify (name
, s
);
577 /* See cli/cli-setshow.h. */
580 get_setshow_command_value_string (const setting
&var
)
588 std::string value
= var
.get
<std::string
> ();
590 stb
.putstr (value
.c_str (), '"');
593 case var_string_noescape
:
594 case var_optional_filename
:
596 stb
.puts (var
.get
<std::string
> ().c_str ());
600 const char *value
= var
.get
<const char *> ();
601 if (value
!= nullptr)
606 stb
.puts (var
.get
<bool> () ? "on" : "off");
608 case var_auto_boolean
:
609 switch (var
.get
<enum auto_boolean
> ())
611 case AUTO_BOOLEAN_TRUE
:
614 case AUTO_BOOLEAN_FALSE
:
617 case AUTO_BOOLEAN_AUTO
:
621 gdb_assert_not_reached ("invalid var_auto_boolean");
628 const unsigned int value
= var
.get
<unsigned int> ();
630 if (var
.type () == var_uinteger
631 && value
== UINT_MAX
)
632 stb
.puts ("unlimited");
634 stb
.printf ("%u", value
);
640 const int value
= var
.get
<int> ();
642 if (var
.type () == var_integer
644 stb
.puts ("unlimited");
646 stb
.printf ("%d", value
);
649 case var_zuinteger_unlimited
:
651 const int value
= var
.get
<int> ();
653 stb
.puts ("unlimited");
655 stb
.printf ("%d", value
);
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. */
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
);
688 if (c
->show_value_func
!= NULL
)
689 c
->show_value_func (gdb_stdout
, from_tty
, c
, val
.c_str ());
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. */
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
);
735 if (list
->type
== show_cmd
)
736 do_show_command (NULL
, from_tty
, list
);
738 cmd_func (list
, NULL
, from_tty
);