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