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