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