GDB (API)
|
00001 /* Handle set and show GDB commands. 00002 00003 Copyright (C) 2000-2013 Free Software Foundation, Inc. 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00017 00018 #include "defs.h" 00019 #include "readline/tilde.h" 00020 #include "value.h" 00021 #include <ctype.h> 00022 #include "gdb_string.h" 00023 #include "arch-utils.h" 00024 #include "observer.h" 00025 00026 #include "ui-out.h" 00027 00028 #include "cli/cli-decode.h" 00029 #include "cli/cli-cmds.h" 00030 #include "cli/cli-setshow.h" 00031 #include "cli/cli-utils.h" 00032 00033 /* Return true if the change of command parameter should be notified. */ 00034 00035 static int 00036 notify_command_param_changed_p (int param_changed, struct cmd_list_element *c) 00037 { 00038 if (param_changed == 0) 00039 return 0; 00040 00041 if (c->class == class_maintenance || c->class == class_deprecated 00042 || c->class == class_obscure) 00043 return 0; 00044 00045 return 1; 00046 } 00047 00048 00049 static enum auto_boolean 00050 parse_auto_binary_operation (const char *arg) 00051 { 00052 if (arg != NULL && *arg != '\0') 00053 { 00054 int length = strlen (arg); 00055 00056 while (isspace (arg[length - 1]) && length > 0) 00057 length--; 00058 if (strncmp (arg, "on", length) == 0 00059 || strncmp (arg, "1", length) == 0 00060 || strncmp (arg, "yes", length) == 0 00061 || strncmp (arg, "enable", length) == 0) 00062 return AUTO_BOOLEAN_TRUE; 00063 else if (strncmp (arg, "off", length) == 0 00064 || strncmp (arg, "0", length) == 0 00065 || strncmp (arg, "no", length) == 0 00066 || strncmp (arg, "disable", length) == 0) 00067 return AUTO_BOOLEAN_FALSE; 00068 else if (strncmp (arg, "auto", length) == 0 00069 || (strncmp (arg, "-1", length) == 0 && length > 1)) 00070 return AUTO_BOOLEAN_AUTO; 00071 } 00072 error (_("\"on\", \"off\" or \"auto\" expected.")); 00073 return AUTO_BOOLEAN_AUTO; /* Pacify GCC. */ 00074 } 00075 00076 /* See cli-setshow.h. */ 00077 00078 int 00079 parse_cli_boolean_value (char *arg) 00080 { 00081 int length; 00082 00083 if (!arg || !*arg) 00084 return 1; 00085 00086 length = strlen (arg); 00087 00088 while (arg[length - 1] == ' ' || arg[length - 1] == '\t') 00089 length--; 00090 00091 if (strncmp (arg, "on", length) == 0 00092 || strncmp (arg, "1", length) == 0 00093 || strncmp (arg, "yes", length) == 0 00094 || strncmp (arg, "enable", length) == 0) 00095 return 1; 00096 else if (strncmp (arg, "off", length) == 0 00097 || strncmp (arg, "0", length) == 0 00098 || strncmp (arg, "no", length) == 0 00099 || strncmp (arg, "disable", length) == 0) 00100 return 0; 00101 else 00102 return -1; 00103 } 00104 00105 void 00106 deprecated_show_value_hack (struct ui_file *ignore_file, 00107 int ignore_from_tty, 00108 struct cmd_list_element *c, 00109 const char *value) 00110 { 00111 /* If there's no command or value, don't try to print it out. */ 00112 if (c == NULL || value == NULL) 00113 return; 00114 /* Print doc minus "show" at start. */ 00115 print_doc_line (gdb_stdout, c->doc + 5); 00116 switch (c->var_type) 00117 { 00118 case var_string: 00119 case var_string_noescape: 00120 case var_optional_filename: 00121 case var_filename: 00122 case var_enum: 00123 printf_filtered ((" is \"%s\".\n"), value); 00124 break; 00125 default: 00126 printf_filtered ((" is %s.\n"), value); 00127 break; 00128 } 00129 } 00130 00131 /* Returns true if ARG is "unlimited". */ 00132 00133 static int 00134 is_unlimited_literal (const char *arg) 00135 { 00136 size_t len = sizeof ("unlimited") - 1; 00137 00138 arg = skip_spaces_const (arg); 00139 00140 return (strncmp (arg, "unlimited", len) == 0 00141 && (isspace (arg[len]) || arg[len] == '\0')); 00142 } 00143 00144 00145 /* Do a "set" command. ARG is NULL if no argument, or the 00146 text of the argument, and FROM_TTY is nonzero if this command is 00147 being entered directly by the user (i.e. these are just like any 00148 other command). C is the command list element for the command. */ 00149 00150 void 00151 do_set_command (char *arg, int from_tty, struct cmd_list_element *c) 00152 { 00153 /* A flag to indicate the option is changed or not. */ 00154 int option_changed = 0; 00155 00156 gdb_assert (c->type == set_cmd); 00157 00158 switch (c->var_type) 00159 { 00160 case var_string: 00161 { 00162 char *new; 00163 const char *p; 00164 char *q; 00165 int ch; 00166 00167 if (arg == NULL) 00168 arg = ""; 00169 new = (char *) xmalloc (strlen (arg) + 2); 00170 p = arg; 00171 q = new; 00172 while ((ch = *p++) != '\000') 00173 { 00174 if (ch == '\\') 00175 { 00176 /* \ at end of argument is used after spaces 00177 so they won't be lost. */ 00178 /* This is obsolete now that we no longer strip 00179 trailing whitespace and actually, the backslash 00180 didn't get here in my test, readline or 00181 something did something funky with a backslash 00182 right before a newline. */ 00183 if (*p == 0) 00184 break; 00185 ch = parse_escape (get_current_arch (), &p); 00186 if (ch == 0) 00187 break; /* C loses */ 00188 else if (ch > 0) 00189 *q++ = ch; 00190 } 00191 else 00192 *q++ = ch; 00193 } 00194 #if 0 00195 if (*(p - 1) != '\\') 00196 *q++ = ' '; 00197 #endif 00198 *q++ = '\0'; 00199 new = (char *) xrealloc (new, q - new); 00200 00201 if (*(char **) c->var == NULL 00202 || strcmp (*(char **) c->var, new) != 0) 00203 { 00204 xfree (*(char **) c->var); 00205 *(char **) c->var = new; 00206 00207 option_changed = 1; 00208 } 00209 else 00210 xfree (new); 00211 } 00212 break; 00213 case var_string_noescape: 00214 if (arg == NULL) 00215 arg = ""; 00216 00217 if (*(char **) c->var == NULL || strcmp (*(char **) c->var, arg) != 0) 00218 { 00219 xfree (*(char **) c->var); 00220 *(char **) c->var = xstrdup (arg); 00221 00222 option_changed = 1; 00223 } 00224 break; 00225 case var_filename: 00226 if (arg == NULL) 00227 error_no_arg (_("filename to set it to.")); 00228 /* FALLTHROUGH */ 00229 case var_optional_filename: 00230 { 00231 char *val = NULL; 00232 00233 if (arg != NULL) 00234 { 00235 /* Clear trailing whitespace of filename. */ 00236 char *ptr = arg + strlen (arg) - 1; 00237 00238 while (ptr >= arg && (*ptr == ' ' || *ptr == '\t')) 00239 ptr--; 00240 *(ptr + 1) = '\0'; 00241 00242 val = tilde_expand (arg); 00243 } 00244 else 00245 val = xstrdup (""); 00246 00247 if (*(char **) c->var == NULL 00248 || strcmp (*(char **) c->var, val) != 0) 00249 { 00250 xfree (*(char **) c->var); 00251 *(char **) c->var = val; 00252 00253 option_changed = 1; 00254 } 00255 else 00256 xfree (val); 00257 } 00258 break; 00259 case var_boolean: 00260 { 00261 int val = parse_cli_boolean_value (arg); 00262 00263 if (val < 0) 00264 error (_("\"on\" or \"off\" expected.")); 00265 if (val != *(int *) c->var) 00266 { 00267 *(int *) c->var = val; 00268 00269 option_changed = 1; 00270 } 00271 } 00272 break; 00273 case var_auto_boolean: 00274 { 00275 enum auto_boolean val = parse_auto_binary_operation (arg); 00276 00277 if (*(enum auto_boolean *) c->var != val) 00278 { 00279 *(enum auto_boolean *) c->var = val; 00280 00281 option_changed = 1; 00282 } 00283 } 00284 break; 00285 case var_uinteger: 00286 case var_zuinteger: 00287 { 00288 LONGEST val; 00289 00290 if (arg == NULL) 00291 { 00292 if (c->var_type == var_uinteger) 00293 error_no_arg (_("integer to set it to, or \"unlimited\".")); 00294 else 00295 error_no_arg (_("integer to set it to.")); 00296 } 00297 00298 if (c->var_type == var_uinteger && is_unlimited_literal (arg)) 00299 val = 0; 00300 else 00301 val = parse_and_eval_long (arg); 00302 00303 if (c->var_type == var_uinteger && val == 0) 00304 val = UINT_MAX; 00305 else if (val < 0 00306 /* For var_uinteger, don't let the user set the value 00307 to UINT_MAX directly, as that exposes an 00308 implementation detail to the user interface. */ 00309 || (c->var_type == var_uinteger && val >= UINT_MAX) 00310 || (c->var_type == var_zuinteger && val > UINT_MAX)) 00311 error (_("integer %s out of range"), plongest (val)); 00312 00313 if (*(unsigned int *) c->var != val) 00314 { 00315 *(unsigned int *) c->var = val; 00316 00317 option_changed = 1; 00318 } 00319 } 00320 break; 00321 case var_integer: 00322 case var_zinteger: 00323 { 00324 LONGEST val; 00325 00326 if (arg == NULL) 00327 { 00328 if (c->var_type == var_integer) 00329 error_no_arg (_("integer to set it to, or \"unlimited\".")); 00330 else 00331 error_no_arg (_("integer to set it to.")); 00332 } 00333 00334 if (c->var_type == var_integer && is_unlimited_literal (arg)) 00335 val = 0; 00336 else 00337 val = parse_and_eval_long (arg); 00338 00339 if (val == 0 && c->var_type == var_integer) 00340 val = INT_MAX; 00341 else if (val < INT_MIN 00342 /* For var_integer, don't let the user set the value 00343 to INT_MAX directly, as that exposes an 00344 implementation detail to the user interface. */ 00345 || (c->var_type == var_integer && val >= INT_MAX) 00346 || (c->var_type == var_zinteger && val > INT_MAX)) 00347 error (_("integer %s out of range"), plongest (val)); 00348 00349 if (*(int *) c->var != val) 00350 { 00351 *(int *) c->var = val; 00352 00353 option_changed = 1; 00354 } 00355 break; 00356 } 00357 case var_enum: 00358 { 00359 int i; 00360 int len; 00361 int nmatches; 00362 const char *match = NULL; 00363 char *p; 00364 00365 /* If no argument was supplied, print an informative error 00366 message. */ 00367 if (arg == NULL) 00368 { 00369 char *msg; 00370 int msg_len = 0; 00371 00372 for (i = 0; c->enums[i]; i++) 00373 msg_len += strlen (c->enums[i]) + 2; 00374 00375 msg = xmalloc (msg_len); 00376 *msg = '\0'; 00377 make_cleanup (xfree, msg); 00378 00379 for (i = 0; c->enums[i]; i++) 00380 { 00381 if (i != 0) 00382 strcat (msg, ", "); 00383 strcat (msg, c->enums[i]); 00384 } 00385 error (_("Requires an argument. Valid arguments are %s."), 00386 msg); 00387 } 00388 00389 p = strchr (arg, ' '); 00390 00391 if (p) 00392 len = p - arg; 00393 else 00394 len = strlen (arg); 00395 00396 nmatches = 0; 00397 for (i = 0; c->enums[i]; i++) 00398 if (strncmp (arg, c->enums[i], len) == 0) 00399 { 00400 if (c->enums[i][len] == '\0') 00401 { 00402 match = c->enums[i]; 00403 nmatches = 1; 00404 break; /* Exact match. */ 00405 } 00406 else 00407 { 00408 match = c->enums[i]; 00409 nmatches++; 00410 } 00411 } 00412 00413 if (nmatches <= 0) 00414 error (_("Undefined item: \"%s\"."), arg); 00415 00416 if (nmatches > 1) 00417 error (_("Ambiguous item \"%s\"."), arg); 00418 00419 if (*(const char **) c->var != match) 00420 { 00421 *(const char **) c->var = match; 00422 00423 option_changed = 1; 00424 } 00425 } 00426 break; 00427 case var_zuinteger_unlimited: 00428 { 00429 LONGEST val; 00430 00431 if (arg == NULL) 00432 error_no_arg (_("integer to set it to, or \"unlimited\".")); 00433 00434 if (is_unlimited_literal (arg)) 00435 val = -1; 00436 else 00437 val = parse_and_eval_long (arg); 00438 00439 if (val > INT_MAX) 00440 error (_("integer %s out of range"), plongest (val)); 00441 else if (val < -1) 00442 error (_("only -1 is allowed to set as unlimited")); 00443 00444 if (*(int *) c->var != val) 00445 { 00446 *(int *) c->var = val; 00447 option_changed = 1; 00448 } 00449 } 00450 break; 00451 default: 00452 error (_("gdb internal error: bad var_type in do_setshow_command")); 00453 } 00454 c->func (c, NULL, from_tty); 00455 if (deprecated_set_hook) 00456 deprecated_set_hook (c); 00457 00458 if (notify_command_param_changed_p (option_changed, c)) 00459 { 00460 char *name, *cp; 00461 struct cmd_list_element **cmds; 00462 struct cmd_list_element *p; 00463 int i; 00464 int length = 0; 00465 00466 /* Compute the whole multi-word command options. If user types command 00467 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to 00468 command option change notification, because it is confusing. We can 00469 trace back through field 'prefix' to compute the whole options, 00470 and pass "foo bar baz" to notification. */ 00471 00472 for (i = 0, p = c; p != NULL; i++) 00473 { 00474 length += strlen (p->name); 00475 length++; 00476 00477 p = p->prefix; 00478 } 00479 cp = name = xmalloc (length); 00480 cmds = xmalloc (sizeof (struct cmd_list_element *) * i); 00481 00482 /* Track back through filed 'prefix' and cache them in CMDS. */ 00483 for (i = 0, p = c; p != NULL; i++) 00484 { 00485 cmds[i] = p; 00486 p = p->prefix; 00487 } 00488 00489 /* Don't trigger any observer notification if prefixlist is not 00490 setlist. */ 00491 i--; 00492 if (cmds[i]->prefixlist != &setlist) 00493 { 00494 xfree (cmds); 00495 xfree (name); 00496 00497 return; 00498 } 00499 /* Traverse them in the reversed order, and copy their names into 00500 NAME. */ 00501 for (i--; i >= 0; i--) 00502 { 00503 memcpy (cp, cmds[i]->name, strlen (cmds[i]->name)); 00504 cp += strlen (cmds[i]->name); 00505 00506 if (i != 0) 00507 { 00508 cp[0] = ' '; 00509 cp++; 00510 } 00511 } 00512 cp[0] = 0; 00513 00514 xfree (cmds); 00515 00516 switch (c->var_type) 00517 { 00518 case var_string: 00519 case var_string_noescape: 00520 case var_filename: 00521 case var_optional_filename: 00522 case var_enum: 00523 observer_notify_command_param_changed (name, *(char **) c->var); 00524 break; 00525 case var_boolean: 00526 { 00527 char *opt = *(int *) c->var ? "on" : "off"; 00528 00529 observer_notify_command_param_changed (name, opt); 00530 } 00531 break; 00532 case var_auto_boolean: 00533 { 00534 const char *s = auto_boolean_enums[*(enum auto_boolean *) c->var]; 00535 00536 observer_notify_command_param_changed (name, s); 00537 } 00538 break; 00539 case var_uinteger: 00540 case var_zuinteger: 00541 { 00542 char s[64]; 00543 00544 xsnprintf (s, sizeof s, "%u", *(unsigned int *) c->var); 00545 observer_notify_command_param_changed (name, s); 00546 } 00547 break; 00548 case var_integer: 00549 case var_zinteger: 00550 case var_zuinteger_unlimited: 00551 { 00552 char s[64]; 00553 00554 xsnprintf (s, sizeof s, "%d", *(int *) c->var); 00555 observer_notify_command_param_changed (name, s); 00556 } 00557 break; 00558 } 00559 xfree (name); 00560 } 00561 } 00562 00563 /* Do a "show" command. ARG is NULL if no argument, or the 00564 text of the argument, and FROM_TTY is nonzero if this command is 00565 being entered directly by the user (i.e. these are just like any 00566 other command). C is the command list element for the command. */ 00567 00568 void 00569 do_show_command (char *arg, int from_tty, struct cmd_list_element *c) 00570 { 00571 struct ui_out *uiout = current_uiout; 00572 struct cleanup *old_chain; 00573 struct ui_file *stb; 00574 00575 gdb_assert (c->type == show_cmd); 00576 00577 stb = mem_fileopen (); 00578 old_chain = make_cleanup_ui_file_delete (stb); 00579 00580 /* Possibly call the pre hook. */ 00581 if (c->pre_show_hook) 00582 (c->pre_show_hook) (c); 00583 00584 switch (c->var_type) 00585 { 00586 case var_string: 00587 if (*(char **) c->var) 00588 fputstr_filtered (*(char **) c->var, '"', stb); 00589 break; 00590 case var_string_noescape: 00591 case var_optional_filename: 00592 case var_filename: 00593 case var_enum: 00594 if (*(char **) c->var) 00595 fputs_filtered (*(char **) c->var, stb); 00596 break; 00597 case var_boolean: 00598 fputs_filtered (*(int *) c->var ? "on" : "off", stb); 00599 break; 00600 case var_auto_boolean: 00601 switch (*(enum auto_boolean*) c->var) 00602 { 00603 case AUTO_BOOLEAN_TRUE: 00604 fputs_filtered ("on", stb); 00605 break; 00606 case AUTO_BOOLEAN_FALSE: 00607 fputs_filtered ("off", stb); 00608 break; 00609 case AUTO_BOOLEAN_AUTO: 00610 fputs_filtered ("auto", stb); 00611 break; 00612 default: 00613 internal_error (__FILE__, __LINE__, 00614 _("do_show_command: " 00615 "invalid var_auto_boolean")); 00616 break; 00617 } 00618 break; 00619 case var_uinteger: 00620 case var_zuinteger: 00621 if (c->var_type == var_uinteger 00622 && *(unsigned int *) c->var == UINT_MAX) 00623 fputs_filtered ("unlimited", stb); 00624 else 00625 fprintf_filtered (stb, "%u", *(unsigned int *) c->var); 00626 break; 00627 case var_integer: 00628 case var_zinteger: 00629 if (c->var_type == var_integer 00630 && *(int *) c->var == INT_MAX) 00631 fputs_filtered ("unlimited", stb); 00632 else 00633 fprintf_filtered (stb, "%d", *(int *) c->var); 00634 break; 00635 case var_zuinteger_unlimited: 00636 { 00637 if (*(int *) c->var == -1) 00638 fputs_filtered ("unlimited", stb); 00639 else 00640 fprintf_filtered (stb, "%d", *(int *) c->var); 00641 } 00642 break; 00643 default: 00644 error (_("gdb internal error: bad var_type in do_show_command")); 00645 } 00646 00647 00648 /* FIXME: cagney/2005-02-10: Need to split this in half: code to 00649 convert the value into a string (esentially the above); and 00650 code to print the value out. For the latter there should be 00651 MI and CLI specific versions. */ 00652 00653 if (ui_out_is_mi_like_p (uiout)) 00654 ui_out_field_stream (uiout, "value", stb); 00655 else 00656 { 00657 char *value = ui_file_xstrdup (stb, NULL); 00658 00659 make_cleanup (xfree, value); 00660 if (c->show_value_func != NULL) 00661 c->show_value_func (gdb_stdout, from_tty, c, value); 00662 else 00663 deprecated_show_value_hack (gdb_stdout, from_tty, c, value); 00664 } 00665 do_cleanups (old_chain); 00666 00667 c->func (c, NULL, from_tty); 00668 } 00669 00670 /* Show all the settings in a list of show commands. */ 00671 00672 void 00673 cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix) 00674 { 00675 struct cleanup *showlist_chain; 00676 struct ui_out *uiout = current_uiout; 00677 00678 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist"); 00679 for (; list != NULL; list = list->next) 00680 { 00681 /* If we find a prefix, run its list, prefixing our output by its 00682 prefix (with "show " skipped). */ 00683 if (list->prefixlist && !list->abbrev_flag) 00684 { 00685 struct cleanup *optionlist_chain 00686 = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist"); 00687 char *new_prefix = strstr (list->prefixname, "show ") + 5; 00688 00689 if (ui_out_is_mi_like_p (uiout)) 00690 ui_out_field_string (uiout, "prefix", new_prefix); 00691 cmd_show_list (*list->prefixlist, from_tty, new_prefix); 00692 /* Close the tuple. */ 00693 do_cleanups (optionlist_chain); 00694 } 00695 else 00696 { 00697 if (list->class != no_set_class) 00698 { 00699 struct cleanup *option_chain 00700 = make_cleanup_ui_out_tuple_begin_end (uiout, "option"); 00701 00702 ui_out_text (uiout, prefix); 00703 ui_out_field_string (uiout, "name", list->name); 00704 ui_out_text (uiout, ": "); 00705 if (list->type == show_cmd) 00706 do_show_command ((char *) NULL, from_tty, list); 00707 else 00708 cmd_func (list, NULL, from_tty); 00709 /* Close the tuple. */ 00710 do_cleanups (option_chain); 00711 } 00712 } 00713 } 00714 /* Close the tuple. */ 00715 do_cleanups (showlist_chain); 00716 } 00717