1 /* Handle set and show GDB commands.
3 Copyright (C) 2000-2019 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 (int param_changed
, struct cmd_list_element
*c
)
37 if (param_changed
== 0)
40 if (c
->theclass
== class_maintenance
|| c
->theclass
== class_deprecated
41 || c
->theclass
== class_obscure
)
48 static enum auto_boolean
49 parse_auto_binary_operation (const char *arg
)
51 if (arg
!= NULL
&& *arg
!= '\0')
53 int length
= strlen (arg
);
55 while (isspace (arg
[length
- 1]) && length
> 0)
58 /* Note that "o" is ambiguous. */
60 if ((length
== 2 && strncmp (arg
, "on", length
) == 0)
61 || strncmp (arg
, "1", length
) == 0
62 || strncmp (arg
, "yes", length
) == 0
63 || strncmp (arg
, "enable", length
) == 0)
64 return AUTO_BOOLEAN_TRUE
;
65 else if ((length
>= 2 && strncmp (arg
, "off", length
) == 0)
66 || strncmp (arg
, "0", length
) == 0
67 || strncmp (arg
, "no", length
) == 0
68 || strncmp (arg
, "disable", length
) == 0)
69 return AUTO_BOOLEAN_FALSE
;
70 else if (strncmp (arg
, "auto", length
) == 0
71 || (length
> 1 && strncmp (arg
, "-1", length
) == 0))
72 return AUTO_BOOLEAN_AUTO
;
74 error (_("\"on\", \"off\" or \"auto\" expected."));
75 return AUTO_BOOLEAN_AUTO
; /* Pacify GCC. */
78 /* See cli-setshow.h. */
81 parse_cli_boolean_value (const char **arg
)
83 const char *p
= skip_to_space (*arg
);
84 size_t length
= p
- *arg
;
86 /* Note that "o" is ambiguous. */
88 if ((length
== 2 && strncmp (*arg
, "on", length
) == 0)
89 || strncmp (*arg
, "1", length
) == 0
90 || strncmp (*arg
, "yes", length
) == 0
91 || strncmp (*arg
, "enable", length
) == 0)
93 *arg
= skip_spaces (*arg
+ length
);
96 else if ((length
>= 2 && strncmp (*arg
, "off", length
) == 0)
97 || strncmp (*arg
, "0", length
) == 0
98 || strncmp (*arg
, "no", length
) == 0
99 || strncmp (*arg
, "disable", length
) == 0)
101 *arg
= skip_spaces (*arg
+ length
);
108 /* See cli-setshow.h. */
111 parse_cli_boolean_value (const char *arg
)
116 int b
= parse_cli_boolean_value (&arg
);
117 if (b
>= 0 && *arg
!= '\0')
125 deprecated_show_value_hack (struct ui_file
*ignore_file
,
127 struct cmd_list_element
*c
,
130 /* If there's no command or value, don't try to print it out. */
131 if (c
== NULL
|| value
== NULL
)
133 /* Print doc minus "show" at start. */
134 print_doc_line (gdb_stdout
, c
->doc
+ 5);
138 case var_string_noescape
:
139 case var_optional_filename
:
142 printf_filtered ((" is \"%s\".\n"), value
);
145 printf_filtered ((" is %s.\n"), value
);
150 /* Returns true if ARG is "unlimited". */
153 is_unlimited_literal (const char **arg
, bool expression
)
155 *arg
= skip_spaces (*arg
);
157 const char *unl_start
= *arg
;
159 const char *p
= skip_to_space (*arg
);
161 size_t len
= p
- *arg
;
163 if (len
> 0 && strncmp ("unlimited", *arg
, len
) == 0)
167 /* If parsing an expression (i.e., parsing for a "set" command),
168 anything after "unlimited" is junk. For options, anything
169 after "unlimited" might be a command argument or another
173 const char *after
= skip_spaces (*arg
);
175 error (_("Junk after \"%.*s\": %s"),
176 (int) len
, unl_start
, after
);
185 /* See cli-setshow.h. */
188 parse_cli_var_uinteger (var_types var_type
, const char **arg
,
195 if (var_type
== var_uinteger
)
196 error_no_arg (_("integer to set it to, or \"unlimited\"."));
198 error_no_arg (_("integer to set it to."));
201 if (var_type
== var_uinteger
&& is_unlimited_literal (arg
, expression
))
204 val
= parse_and_eval_long (*arg
);
206 val
= get_ulongest (arg
);
208 if (var_type
== var_uinteger
&& val
== 0)
211 /* For var_uinteger, don't let the user set the value
212 to UINT_MAX directly, as that exposes an
213 implementation detail to the user interface. */
214 || (var_type
== var_uinteger
&& val
>= UINT_MAX
)
215 || (var_type
== var_zuinteger
&& val
> UINT_MAX
))
216 error (_("integer %s out of range"), plongest (val
));
221 /* See cli-setshow.h. */
224 parse_cli_var_zuinteger_unlimited (const char **arg
, bool expression
)
229 error_no_arg (_("integer to set it to, or \"unlimited\"."));
231 if (is_unlimited_literal (arg
, expression
))
234 val
= parse_and_eval_long (*arg
);
236 val
= get_ulongest (arg
);
239 error (_("integer %s out of range"), plongest (val
));
241 error (_("only -1 is allowed to set as unlimited"));
246 /* See cli-setshow.h. */
249 parse_cli_var_enum (const char **args
, const char *const *enums
)
251 /* If no argument was supplied, print an informative error
253 if (args
== NULL
|| *args
== NULL
|| **args
== '\0')
257 for (size_t i
= 0; enums
[i
]; i
++)
263 error (_("Requires an argument. Valid arguments are %s."),
267 const char *p
= skip_to_space (*args
);
268 size_t len
= p
- *args
;
271 const char *match
= NULL
;
272 for (size_t i
= 0; enums
[i
]; i
++)
273 if (strncmp (*args
, enums
[i
], len
) == 0)
275 if (enums
[i
][len
] == '\0')
279 break; /* Exact match. */
289 error (_("Undefined item: \"%.*s\"."), (int) len
, *args
);
292 error (_("Ambiguous item \"%.*s\"."), (int) len
, *args
);
298 /* Do a "set" command. ARG is NULL if no argument, or the
299 text of the argument, and FROM_TTY is nonzero if this command is
300 being entered directly by the user (i.e. these are just like any
301 other command). C is the command list element for the command. */
304 do_set_command (const char *arg
, int from_tty
, struct cmd_list_element
*c
)
306 /* A flag to indicate the option is changed or not. */
307 int option_changed
= 0;
309 gdb_assert (c
->type
== set_cmd
);
322 newobj
= (char *) xmalloc (strlen (arg
) + 2);
325 while ((ch
= *p
++) != '\000')
329 /* \ at end of argument is used after spaces
330 so they won't be lost. */
331 /* This is obsolete now that we no longer strip
332 trailing whitespace and actually, the backslash
333 didn't get here in my test, readline or
334 something did something funky with a backslash
335 right before a newline. */
338 ch
= parse_escape (get_current_arch (), &p
);
348 if (*(p
- 1) != '\\')
352 newobj
= (char *) xrealloc (newobj
, q
- newobj
);
354 if (*(char **) c
->var
== NULL
355 || strcmp (*(char **) c
->var
, newobj
) != 0)
357 xfree (*(char **) c
->var
);
358 *(char **) c
->var
= newobj
;
366 case var_string_noescape
:
370 if (*(char **) c
->var
== NULL
|| strcmp (*(char **) c
->var
, arg
) != 0)
372 xfree (*(char **) c
->var
);
373 *(char **) c
->var
= xstrdup (arg
);
380 error_no_arg (_("filename to set it to."));
382 case var_optional_filename
:
388 /* Clear trailing whitespace of filename. */
389 const char *ptr
= arg
+ strlen (arg
) - 1;
392 while (ptr
>= arg
&& (*ptr
== ' ' || *ptr
== '\t'))
394 copy
= xstrndup (arg
, ptr
+ 1 - arg
);
396 val
= tilde_expand (copy
);
402 if (*(char **) c
->var
== NULL
403 || strcmp (*(char **) c
->var
, val
) != 0)
405 xfree (*(char **) c
->var
);
406 *(char **) c
->var
= val
;
416 int val
= parse_cli_boolean_value (arg
);
419 error (_("\"on\" or \"off\" expected."));
420 if (val
!= *(int *) c
->var
)
422 *(int *) c
->var
= val
;
428 case var_auto_boolean
:
430 enum auto_boolean val
= parse_auto_binary_operation (arg
);
432 if (*(enum auto_boolean
*) c
->var
!= val
)
434 *(enum auto_boolean
*) c
->var
= val
;
443 unsigned int val
= parse_cli_var_uinteger (c
->var_type
, &arg
, true);
445 if (*(unsigned int *) c
->var
!= val
)
447 *(unsigned int *) c
->var
= val
;
460 if (c
->var_type
== var_integer
)
461 error_no_arg (_("integer to set it to, or \"unlimited\"."));
463 error_no_arg (_("integer to set it to."));
466 if (c
->var_type
== var_integer
&& is_unlimited_literal (&arg
, true))
469 val
= parse_and_eval_long (arg
);
471 if (val
== 0 && c
->var_type
== var_integer
)
473 else if (val
< INT_MIN
474 /* For var_integer, don't let the user set the value
475 to INT_MAX directly, as that exposes an
476 implementation detail to the user interface. */
477 || (c
->var_type
== var_integer
&& val
>= INT_MAX
)
478 || (c
->var_type
== var_zinteger
&& val
> INT_MAX
))
479 error (_("integer %s out of range"), plongest (val
));
481 if (*(int *) c
->var
!= val
)
483 *(int *) c
->var
= val
;
491 const char *end_arg
= arg
;
492 const char *match
= parse_cli_var_enum (&end_arg
, c
->enums
);
494 int len
= end_arg
- arg
;
495 const char *after
= skip_spaces (end_arg
);
497 error (_("Junk after item \"%.*s\": %s"), len
, arg
, after
);
499 if (*(const char **) c
->var
!= match
)
501 *(const char **) c
->var
= match
;
507 case var_zuinteger_unlimited
:
509 int val
= parse_cli_var_zuinteger_unlimited (&arg
, true);
511 if (*(int *) c
->var
!= val
)
513 *(int *) c
->var
= val
;
519 error (_("gdb internal error: bad var_type in do_setshow_command"));
521 c
->func (c
, NULL
, from_tty
);
523 if (notify_command_param_changed_p (option_changed
, c
))
526 struct cmd_list_element
**cmds
;
527 struct cmd_list_element
*p
;
531 /* Compute the whole multi-word command options. If user types command
532 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
533 command option change notification, because it is confusing. We can
534 trace back through field 'prefix' to compute the whole options,
535 and pass "foo bar baz" to notification. */
537 for (i
= 0, p
= c
; p
!= NULL
; i
++)
539 length
+= strlen (p
->name
);
544 cp
= name
= (char *) xmalloc (length
);
545 cmds
= XNEWVEC (struct cmd_list_element
*, i
);
547 /* Track back through filed 'prefix' and cache them in CMDS. */
548 for (i
= 0, p
= c
; p
!= NULL
; i
++)
554 /* Don't trigger any observer notification if prefixlist is not
557 if (cmds
[i
]->prefixlist
!= &setlist
)
564 /* Traverse them in the reversed order, and copy their names into
566 for (i
--; i
>= 0; i
--)
568 memcpy (cp
, cmds
[i
]->name
, strlen (cmds
[i
]->name
));
569 cp
+= strlen (cmds
[i
]->name
);
584 case var_string_noescape
:
586 case var_optional_filename
:
588 gdb::observers::command_param_changed
.notify (name
, *(char **) c
->var
);
592 const char *opt
= *(int *) c
->var
? "on" : "off";
594 gdb::observers::command_param_changed
.notify (name
, opt
);
597 case var_auto_boolean
:
599 const char *s
= auto_boolean_enums
[*(enum auto_boolean
*) c
->var
];
601 gdb::observers::command_param_changed
.notify (name
, s
);
609 xsnprintf (s
, sizeof s
, "%u", *(unsigned int *) c
->var
);
610 gdb::observers::command_param_changed
.notify (name
, s
);
615 case var_zuinteger_unlimited
:
619 xsnprintf (s
, sizeof s
, "%d", *(int *) c
->var
);
620 gdb::observers::command_param_changed
.notify (name
, s
);
628 /* Do a "show" command. ARG is NULL if no argument, or the
629 text of the argument, and FROM_TTY is nonzero if this command is
630 being entered directly by the user (i.e. these are just like any
631 other command). C is the command list element for the command. */
634 do_show_command (const char *arg
, int from_tty
, struct cmd_list_element
*c
)
636 struct ui_out
*uiout
= current_uiout
;
638 gdb_assert (c
->type
== show_cmd
);
642 /* Possibly call the pre hook. */
643 if (c
->pre_show_hook
)
644 (c
->pre_show_hook
) (c
);
649 if (*(char **) c
->var
)
650 stb
.putstr (*(char **) c
->var
, '"');
652 case var_string_noescape
:
653 case var_optional_filename
:
656 if (*(char **) c
->var
)
657 stb
.puts (*(char **) c
->var
);
660 stb
.puts (*(int *) c
->var
? "on" : "off");
662 case var_auto_boolean
:
663 switch (*(enum auto_boolean
*) c
->var
)
665 case AUTO_BOOLEAN_TRUE
:
668 case AUTO_BOOLEAN_FALSE
:
671 case AUTO_BOOLEAN_AUTO
:
675 internal_error (__FILE__
, __LINE__
,
676 _("do_show_command: "
677 "invalid var_auto_boolean"));
683 if (c
->var_type
== var_uinteger
684 && *(unsigned int *) c
->var
== UINT_MAX
)
685 stb
.puts ("unlimited");
687 stb
.printf ("%u", *(unsigned int *) c
->var
);
691 if (c
->var_type
== var_integer
692 && *(int *) c
->var
== INT_MAX
)
693 stb
.puts ("unlimited");
695 stb
.printf ("%d", *(int *) c
->var
);
697 case var_zuinteger_unlimited
:
699 if (*(int *) c
->var
== -1)
700 stb
.puts ("unlimited");
702 stb
.printf ("%d", *(int *) c
->var
);
706 error (_("gdb internal error: bad var_type in do_show_command"));
710 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
711 convert the value into a string (esentially the above); and
712 code to print the value out. For the latter there should be
713 MI and CLI specific versions. */
715 if (uiout
->is_mi_like_p ())
716 uiout
->field_stream ("value", stb
);
719 if (c
->show_value_func
!= NULL
)
720 c
->show_value_func (gdb_stdout
, from_tty
, c
, stb
.c_str ());
722 deprecated_show_value_hack (gdb_stdout
, from_tty
, c
, stb
.c_str ());
725 c
->func (c
, NULL
, from_tty
);
728 /* Show all the settings in a list of show commands. */
731 cmd_show_list (struct cmd_list_element
*list
, int from_tty
, const char *prefix
)
733 struct ui_out
*uiout
= current_uiout
;
735 ui_out_emit_tuple
tuple_emitter (uiout
, "showlist");
736 for (; list
!= NULL
; list
= list
->next
)
738 /* If we find a prefix, run its list, prefixing our output by its
739 prefix (with "show " skipped). */
740 if (list
->prefixlist
&& !list
->abbrev_flag
)
742 ui_out_emit_tuple
optionlist_emitter (uiout
, "optionlist");
743 const char *new_prefix
= strstr (list
->prefixname
, "show ") + 5;
745 if (uiout
->is_mi_like_p ())
746 uiout
->field_string ("prefix", new_prefix
);
747 cmd_show_list (*list
->prefixlist
, from_tty
, new_prefix
);
751 if (list
->theclass
!= no_set_class
)
753 ui_out_emit_tuple
option_emitter (uiout
, "option");
755 uiout
->text (prefix
);
756 uiout
->field_string ("name", list
->name
);
758 if (list
->type
== show_cmd
)
759 do_show_command ((char *) NULL
, from_tty
, list
);
761 cmd_func (list
, NULL
, from_tty
);
This page took 0.046226 seconds and 5 git commands to generate.