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
)
88 length
= strlen (arg
);
90 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
93 /* Note that "o" is ambiguous. */
95 if ((length
== 2 && strncmp (arg
, "on", length
) == 0)
96 || strncmp (arg
, "1", length
) == 0
97 || strncmp (arg
, "yes", length
) == 0
98 || strncmp (arg
, "enable", length
) == 0)
100 else if ((length
>= 2 && strncmp (arg
, "off", length
) == 0)
101 || strncmp (arg
, "0", length
) == 0
102 || strncmp (arg
, "no", length
) == 0
103 || strncmp (arg
, "disable", length
) == 0)
110 deprecated_show_value_hack (struct ui_file
*ignore_file
,
112 struct cmd_list_element
*c
,
115 /* If there's no command or value, don't try to print it out. */
116 if (c
== NULL
|| value
== NULL
)
118 /* Print doc minus "show" at start. */
119 print_doc_line (gdb_stdout
, c
->doc
+ 5);
123 case var_string_noescape
:
124 case var_optional_filename
:
127 printf_filtered ((" is \"%s\".\n"), value
);
130 printf_filtered ((" is %s.\n"), value
);
135 /* Returns true if ARG is "unlimited". */
138 is_unlimited_literal (const char *arg
)
140 arg
= skip_spaces (arg
);
142 const char *p
= skip_to_space (arg
);
144 size_t len
= p
- arg
;
146 if (len
> 0 && strncmp ("unlimited", arg
, len
) == 0)
153 /* Do a "set" command. ARG is NULL if no argument, or the
154 text of the argument, and FROM_TTY is nonzero if this command is
155 being entered directly by the user (i.e. these are just like any
156 other command). C is the command list element for the command. */
159 do_set_command (const char *arg
, int from_tty
, struct cmd_list_element
*c
)
161 /* A flag to indicate the option is changed or not. */
162 int option_changed
= 0;
164 gdb_assert (c
->type
== set_cmd
);
177 newobj
= (char *) xmalloc (strlen (arg
) + 2);
180 while ((ch
= *p
++) != '\000')
184 /* \ at end of argument is used after spaces
185 so they won't be lost. */
186 /* This is obsolete now that we no longer strip
187 trailing whitespace and actually, the backslash
188 didn't get here in my test, readline or
189 something did something funky with a backslash
190 right before a newline. */
193 ch
= parse_escape (get_current_arch (), &p
);
203 if (*(p
- 1) != '\\')
207 newobj
= (char *) xrealloc (newobj
, q
- newobj
);
209 if (*(char **) c
->var
== NULL
210 || strcmp (*(char **) c
->var
, newobj
) != 0)
212 xfree (*(char **) c
->var
);
213 *(char **) c
->var
= newobj
;
221 case var_string_noescape
:
225 if (*(char **) c
->var
== NULL
|| strcmp (*(char **) c
->var
, arg
) != 0)
227 xfree (*(char **) c
->var
);
228 *(char **) c
->var
= xstrdup (arg
);
235 error_no_arg (_("filename to set it to."));
237 case var_optional_filename
:
243 /* Clear trailing whitespace of filename. */
244 const char *ptr
= arg
+ strlen (arg
) - 1;
247 while (ptr
>= arg
&& (*ptr
== ' ' || *ptr
== '\t'))
249 copy
= xstrndup (arg
, ptr
+ 1 - arg
);
251 val
= tilde_expand (copy
);
257 if (*(char **) c
->var
== NULL
258 || strcmp (*(char **) c
->var
, val
) != 0)
260 xfree (*(char **) c
->var
);
261 *(char **) c
->var
= val
;
271 int val
= parse_cli_boolean_value (arg
);
274 error (_("\"on\" or \"off\" expected."));
275 if (val
!= *(int *) c
->var
)
277 *(int *) c
->var
= val
;
283 case var_auto_boolean
:
285 enum auto_boolean val
= parse_auto_binary_operation (arg
);
287 if (*(enum auto_boolean
*) c
->var
!= val
)
289 *(enum auto_boolean
*) c
->var
= val
;
302 if (c
->var_type
== var_uinteger
)
303 error_no_arg (_("integer to set it to, or \"unlimited\"."));
305 error_no_arg (_("integer to set it to."));
308 if (c
->var_type
== var_uinteger
&& is_unlimited_literal (arg
))
311 val
= parse_and_eval_long (arg
);
313 if (c
->var_type
== var_uinteger
&& val
== 0)
316 /* For var_uinteger, don't let the user set the value
317 to UINT_MAX directly, as that exposes an
318 implementation detail to the user interface. */
319 || (c
->var_type
== var_uinteger
&& val
>= UINT_MAX
)
320 || (c
->var_type
== var_zuinteger
&& val
> UINT_MAX
))
321 error (_("integer %s out of range"), plongest (val
));
323 if (*(unsigned int *) c
->var
!= val
)
325 *(unsigned int *) c
->var
= val
;
338 if (c
->var_type
== var_integer
)
339 error_no_arg (_("integer to set it to, or \"unlimited\"."));
341 error_no_arg (_("integer to set it to."));
344 if (c
->var_type
== var_integer
&& is_unlimited_literal (arg
))
347 val
= parse_and_eval_long (arg
);
349 if (val
== 0 && c
->var_type
== var_integer
)
351 else if (val
< INT_MIN
352 /* For var_integer, don't let the user set the value
353 to INT_MAX directly, as that exposes an
354 implementation detail to the user interface. */
355 || (c
->var_type
== var_integer
&& val
>= INT_MAX
)
356 || (c
->var_type
== var_zinteger
&& val
> INT_MAX
))
357 error (_("integer %s out of range"), plongest (val
));
359 if (*(int *) c
->var
!= val
)
361 *(int *) c
->var
= val
;
372 const char *match
= NULL
;
375 /* If no argument was supplied, print an informative error
381 for (i
= 0; c
->enums
[i
]; i
++)
387 error (_("Requires an argument. Valid arguments are %s."),
391 p
= strchr (arg
, ' ');
399 for (i
= 0; c
->enums
[i
]; i
++)
400 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
402 if (c
->enums
[i
][len
] == '\0')
406 break; /* Exact match. */
416 error (_("Undefined item: \"%s\"."), arg
);
419 error (_("Ambiguous item \"%s\"."), arg
);
421 const char *after
= skip_spaces (arg
+ len
);
423 error (_("Junk after item \"%.*s\": %s"), len
, arg
, after
);
425 if (*(const char **) c
->var
!= match
)
427 *(const char **) c
->var
= match
;
433 case var_zuinteger_unlimited
:
438 error_no_arg (_("integer to set it to, or \"unlimited\"."));
440 if (is_unlimited_literal (arg
))
443 val
= parse_and_eval_long (arg
);
446 error (_("integer %s out of range"), plongest (val
));
448 error (_("only -1 is allowed to set as unlimited"));
450 if (*(int *) c
->var
!= val
)
452 *(int *) c
->var
= val
;
458 error (_("gdb internal error: bad var_type in do_setshow_command"));
460 c
->func (c
, NULL
, from_tty
);
462 if (notify_command_param_changed_p (option_changed
, c
))
465 struct cmd_list_element
**cmds
;
466 struct cmd_list_element
*p
;
470 /* Compute the whole multi-word command options. If user types command
471 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
472 command option change notification, because it is confusing. We can
473 trace back through field 'prefix' to compute the whole options,
474 and pass "foo bar baz" to notification. */
476 for (i
= 0, p
= c
; p
!= NULL
; i
++)
478 length
+= strlen (p
->name
);
483 cp
= name
= (char *) xmalloc (length
);
484 cmds
= XNEWVEC (struct cmd_list_element
*, i
);
486 /* Track back through filed 'prefix' and cache them in CMDS. */
487 for (i
= 0, p
= c
; p
!= NULL
; i
++)
493 /* Don't trigger any observer notification if prefixlist is not
496 if (cmds
[i
]->prefixlist
!= &setlist
)
503 /* Traverse them in the reversed order, and copy their names into
505 for (i
--; i
>= 0; i
--)
507 memcpy (cp
, cmds
[i
]->name
, strlen (cmds
[i
]->name
));
508 cp
+= strlen (cmds
[i
]->name
);
523 case var_string_noescape
:
525 case var_optional_filename
:
527 gdb::observers::command_param_changed
.notify (name
, *(char **) c
->var
);
531 const char *opt
= *(int *) c
->var
? "on" : "off";
533 gdb::observers::command_param_changed
.notify (name
, opt
);
536 case var_auto_boolean
:
538 const char *s
= auto_boolean_enums
[*(enum auto_boolean
*) c
->var
];
540 gdb::observers::command_param_changed
.notify (name
, s
);
548 xsnprintf (s
, sizeof s
, "%u", *(unsigned int *) c
->var
);
549 gdb::observers::command_param_changed
.notify (name
, s
);
554 case var_zuinteger_unlimited
:
558 xsnprintf (s
, sizeof s
, "%d", *(int *) c
->var
);
559 gdb::observers::command_param_changed
.notify (name
, s
);
567 /* Do a "show" command. ARG is NULL if no argument, or the
568 text of the argument, and FROM_TTY is nonzero if this command is
569 being entered directly by the user (i.e. these are just like any
570 other command). C is the command list element for the command. */
573 do_show_command (const char *arg
, int from_tty
, struct cmd_list_element
*c
)
575 struct ui_out
*uiout
= current_uiout
;
577 gdb_assert (c
->type
== show_cmd
);
581 /* Possibly call the pre hook. */
582 if (c
->pre_show_hook
)
583 (c
->pre_show_hook
) (c
);
588 if (*(char **) c
->var
)
589 stb
.putstr (*(char **) c
->var
, '"');
591 case var_string_noescape
:
592 case var_optional_filename
:
595 if (*(char **) c
->var
)
596 stb
.puts (*(char **) c
->var
);
599 stb
.puts (*(int *) c
->var
? "on" : "off");
601 case var_auto_boolean
:
602 switch (*(enum auto_boolean
*) c
->var
)
604 case AUTO_BOOLEAN_TRUE
:
607 case AUTO_BOOLEAN_FALSE
:
610 case AUTO_BOOLEAN_AUTO
:
614 internal_error (__FILE__
, __LINE__
,
615 _("do_show_command: "
616 "invalid var_auto_boolean"));
622 if (c
->var_type
== var_uinteger
623 && *(unsigned int *) c
->var
== UINT_MAX
)
624 stb
.puts ("unlimited");
626 stb
.printf ("%u", *(unsigned int *) c
->var
);
630 if (c
->var_type
== var_integer
631 && *(int *) c
->var
== INT_MAX
)
632 stb
.puts ("unlimited");
634 stb
.printf ("%d", *(int *) c
->var
);
636 case var_zuinteger_unlimited
:
638 if (*(int *) c
->var
== -1)
639 stb
.puts ("unlimited");
641 stb
.printf ("%d", *(int *) c
->var
);
645 error (_("gdb internal error: bad var_type in do_show_command"));
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. */
654 if (uiout
->is_mi_like_p ())
655 uiout
->field_stream ("value", stb
);
658 if (c
->show_value_func
!= NULL
)
659 c
->show_value_func (gdb_stdout
, from_tty
, c
, stb
.c_str ());
661 deprecated_show_value_hack (gdb_stdout
, from_tty
, c
, stb
.c_str ());
664 c
->func (c
, NULL
, from_tty
);
667 /* Show all the settings in a list of show commands. */
670 cmd_show_list (struct cmd_list_element
*list
, int from_tty
, const char *prefix
)
672 struct ui_out
*uiout
= current_uiout
;
674 ui_out_emit_tuple
tuple_emitter (uiout
, "showlist");
675 for (; list
!= NULL
; list
= list
->next
)
677 /* If we find a prefix, run its list, prefixing our output by its
678 prefix (with "show " skipped). */
679 if (list
->prefixlist
&& !list
->abbrev_flag
)
681 ui_out_emit_tuple
optionlist_emitter (uiout
, "optionlist");
682 const char *new_prefix
= strstr (list
->prefixname
, "show ") + 5;
684 if (uiout
->is_mi_like_p ())
685 uiout
->field_string ("prefix", new_prefix
);
686 cmd_show_list (*list
->prefixlist
, from_tty
, new_prefix
);
690 if (list
->theclass
!= no_set_class
)
692 ui_out_emit_tuple
option_emitter (uiout
, "option");
694 uiout
->text (prefix
);
695 uiout
->field_string ("name", list
->name
);
697 if (list
->type
== show_cmd
)
698 do_show_command ((char *) NULL
, from_tty
, list
);
700 cmd_func (list
, NULL
, from_tty
);
This page took 0.044355 seconds and 4 git commands to generate.