maint-test-options.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* Maintenance commands for testing the options framework.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbcmd.h"
  16. #include "cli/cli-option.h"
  17. /* This file defines three "maintenance test-options" subcommands to
  18. exercise TAB-completion and option processing:
  19. (gdb) maint test-options require-delimiter
  20. (gdb) maint test-options unknown-is-error
  21. (gdb) maint test-options unknown-is-operand
  22. And a fourth one to help with TAB-completion testing.
  23. (gdb) maint show test-options-completion-result
  24. Each of the test-options subcommands exercise
  25. gdb::option::process_options with a different enum
  26. process_options_mode value. Examples for commands they model:
  27. - "print" and "compile print", are like "require-delimiter",
  28. because they accept random expressions as argument.
  29. - "backtrace" and "frame/thread apply" are like
  30. "unknown-is-operand", because "-" is a valid command.
  31. - "compile file" and "compile code" are like "unknown-is-error".
  32. These commands allow exercising all aspects of option processing
  33. without having to pick some existing command. That should be more
  34. stable going forward than relying on an existing user command,
  35. since if we picked say "print", that command or its options could
  36. change in future, and then we'd be left with having to pick some
  37. other command or option to exercise some non-command-specific
  38. option processing detail. Also, actual user commands have side
  39. effects that we're not interested in when we're focusing on unit
  40. testing the options machinery. BTW, a maintenance command is used
  41. as a sort of unit test driver instead of actual "maint selftest"
  42. unit tests, since we need to go all the way via gdb including
  43. readline, for proper testing of TAB completion.
  44. These maintenance commands support options of all the different
  45. available kinds of commands (boolean, enum, flag, string, uinteger):
  46. (gdb) maint test-options require-delimiter -[TAB]
  47. -bool -enum -flag -string -uinteger -xx1 -xx2
  48. (gdb) maint test-options require-delimiter -bool o[TAB]
  49. off on
  50. (gdb) maint test-options require-delimiter -enum [TAB]
  51. xxx yyy zzz
  52. (gdb) maint test-options require-delimiter -uinteger [TAB]
  53. NUMBER unlimited
  54. '-xx1' and '-xx2' are flag options too. They exist in order to
  55. test ambiguous option names, like '-xx'.
  56. Invoking the commands makes them print out the options parsed:
  57. (gdb) maint test-options unknown-is-error -flag -enum yyy cmdarg
  58. -flag 1 -xx1 0 -xx2 0 -bool 0 -enum yyy -uint 0 -zuint-unl 0 -- cmdarg
  59. (gdb) maint test-options require-delimiter -flag -enum yyy cmdarg
  60. -flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- -flag -enum yyy cmdarg
  61. (gdb) maint test-options require-delimiter -flag -enum yyy cmdarg --
  62. Unrecognized option at: cmdarg --
  63. (gdb) maint test-options require-delimiter -flag -enum yyy -- cmdarg
  64. -flag 1 -xx1 0 -xx2 0 -bool 0 -enum yyy -uint 0 -zuint-unl 0 -- cmdarg
  65. The "maint show test-options-completion-result" command exists in
  66. order to do something similar for completion:
  67. (gdb) maint test-options unknown-is-error -flag -b 0 -enum yyy OPERAND[TAB]
  68. (gdb) maint show test-options-completion-result
  69. 0 OPERAND
  70. (gdb) maint test-options unknown-is-error -flag -b 0 -enum yyy[TAB]
  71. (gdb) maint show test-options-completion-result
  72. 1
  73. (gdb) maint test-options require-dash -unknown[TAB]
  74. (gdb) maint show test-options-completion-result
  75. 1
  76. Here, "1" means the completion function processed the whole input
  77. line, and that the command shouldn't do anything with the arguments,
  78. since there are no operands. While "0" indicates that there are
  79. operands after options. The text after "0" is the operands.
  80. This level of detail is particularly important because getting the
  81. completion function's entry point to return back to the caller the
  82. right pointer into the operand is quite tricky in several
  83. scenarios. */
  84. /* Enum values for the "maintenance test-options" commands. */
  85. const char test_options_enum_values_xxx[] = "xxx";
  86. const char test_options_enum_values_yyy[] = "yyy";
  87. const char test_options_enum_values_zzz[] = "zzz";
  88. static const char *const test_options_enum_values_choices[] =
  89. {
  90. test_options_enum_values_xxx,
  91. test_options_enum_values_yyy,
  92. test_options_enum_values_zzz,
  93. NULL
  94. };
  95. /* Option data for the "maintenance test-options" commands. */
  96. struct test_options_opts
  97. {
  98. bool flag_opt = false;
  99. bool xx1_opt = false;
  100. bool xx2_opt = false;
  101. bool boolean_opt = false;
  102. const char *enum_opt = test_options_enum_values_xxx;
  103. unsigned int uint_opt = 0;
  104. int zuint_unl_opt = 0;
  105. std::string string_opt;
  106. test_options_opts () = default;
  107. DISABLE_COPY_AND_ASSIGN (test_options_opts);
  108. /* Dump the options to FILE. ARGS is the remainder unprocessed
  109. arguments. */
  110. void dump (ui_file *file, const char *args) const
  111. {
  112. gdb_printf (file,
  113. _("-flag %d -xx1 %d -xx2 %d -bool %d "
  114. "-enum %s -uint %s -zuint-unl %s -string '%s' -- %s\n"),
  115. flag_opt,
  116. xx1_opt,
  117. xx2_opt,
  118. boolean_opt,
  119. enum_opt,
  120. (uint_opt == UINT_MAX
  121. ? "unlimited"
  122. : pulongest (uint_opt)),
  123. (zuint_unl_opt == -1
  124. ? "unlimited"
  125. : plongest (zuint_unl_opt)),
  126. string_opt.c_str (),
  127. args);
  128. }
  129. };
  130. /* Option definitions for the "maintenance test-options" commands. */
  131. static const gdb::option::option_def test_options_option_defs[] = {
  132. /* A flag option. */
  133. gdb::option::flag_option_def<test_options_opts> {
  134. "flag",
  135. [] (test_options_opts *opts) { return &opts->flag_opt; },
  136. N_("A flag option."),
  137. },
  138. /* A couple flags with similar names, for "ambiguous option names"
  139. testing. */
  140. gdb::option::flag_option_def<test_options_opts> {
  141. "xx1",
  142. [] (test_options_opts *opts) { return &opts->xx1_opt; },
  143. N_("A flag option."),
  144. },
  145. gdb::option::flag_option_def<test_options_opts> {
  146. "xx2",
  147. [] (test_options_opts *opts) { return &opts->xx2_opt; },
  148. N_("A flag option."),
  149. },
  150. /* A boolean option. */
  151. gdb::option::boolean_option_def<test_options_opts> {
  152. "bool",
  153. [] (test_options_opts *opts) { return &opts->boolean_opt; },
  154. nullptr, /* show_cmd_cb */
  155. N_("A boolean option."),
  156. },
  157. /* An enum option. */
  158. gdb::option::enum_option_def<test_options_opts> {
  159. "enum",
  160. test_options_enum_values_choices,
  161. [] (test_options_opts *opts) { return &opts->enum_opt; },
  162. nullptr, /* show_cmd_cb */
  163. N_("An enum option."),
  164. },
  165. /* A uinteger option. */
  166. gdb::option::uinteger_option_def<test_options_opts> {
  167. "uinteger",
  168. [] (test_options_opts *opts) { return &opts->uint_opt; },
  169. nullptr, /* show_cmd_cb */
  170. N_("A uinteger option."),
  171. nullptr, /* show_doc */
  172. N_("A help doc that spawns\nmultiple lines."),
  173. },
  174. /* A zuinteger_unlimited option. */
  175. gdb::option::zuinteger_unlimited_option_def<test_options_opts> {
  176. "zuinteger-unlimited",
  177. [] (test_options_opts *opts) { return &opts->zuint_unl_opt; },
  178. nullptr, /* show_cmd_cb */
  179. N_("A zuinteger-unlimited option."),
  180. nullptr, /* show_doc */
  181. nullptr, /* help_doc */
  182. },
  183. /* A string option. */
  184. gdb::option::string_option_def<test_options_opts> {
  185. "string",
  186. [] (test_options_opts *opts) { return &opts->string_opt; },
  187. nullptr, /* show_cmd_cb */
  188. N_("A string option."),
  189. },
  190. };
  191. /* Create an option_def_group for the test_options_opts options, with
  192. OPTS as context. */
  193. static inline gdb::option::option_def_group
  194. make_test_options_options_def_group (test_options_opts *opts)
  195. {
  196. return {{test_options_option_defs}, opts};
  197. }
  198. /* Implementation of the "maintenance test-options
  199. require-delimiter/unknown-is-error/unknown-is-operand" commands.
  200. Each of the commands maps to a different enum process_options_mode
  201. enumerator. The test strategy is simply processing the options in
  202. a number of scenarios, and printing back the parsed result. */
  203. static void
  204. maintenance_test_options_command_mode (const char *args,
  205. gdb::option::process_options_mode mode)
  206. {
  207. test_options_opts opts;
  208. gdb::option::process_options (&args, mode,
  209. make_test_options_options_def_group (&opts));
  210. if (args == nullptr)
  211. args = "";
  212. else
  213. args = skip_spaces (args);
  214. opts.dump (gdb_stdout, args);
  215. }
  216. /* Variable used by the "maintenance show
  217. test-options-completion-result" command. This variable is stored
  218. by the completer of the "maint test-options" subcommands.
  219. If the completer returned false, this includes the text at the word
  220. point after gdb::option::complete_options returns. If true, then
  221. this includes a dump of the processed options. */
  222. static std::string maintenance_test_options_command_completion_text;
  223. /* The "maintenance show test-options-completion-result" command. */
  224. static void
  225. maintenance_show_test_options_completion_result (const char *args,
  226. int from_tty)
  227. {
  228. gdb_puts (maintenance_test_options_command_completion_text.c_str ());
  229. }
  230. /* Save the completion result in the global variables read by the
  231. "maintenance test-options require-delimiter" command. */
  232. static void
  233. save_completion_result (const test_options_opts &opts, bool res,
  234. const char *text)
  235. {
  236. if (res)
  237. {
  238. string_file stream;
  239. stream.puts ("1 ");
  240. opts.dump (&stream, text);
  241. maintenance_test_options_command_completion_text = stream.release ();
  242. }
  243. else
  244. {
  245. maintenance_test_options_command_completion_text
  246. = string_printf ("0 %s\n", text);
  247. }
  248. }
  249. /* Implementation of completer for the "maintenance test-options
  250. require-delimiter/unknown-is-error/unknown-is-operand" commands.
  251. Each of the commands maps to a different enum process_options_mode
  252. enumerator. */
  253. static void
  254. maintenance_test_options_completer_mode (completion_tracker &tracker,
  255. const char *text,
  256. gdb::option::process_options_mode mode)
  257. {
  258. test_options_opts opts;
  259. try
  260. {
  261. bool res = (gdb::option::complete_options
  262. (tracker, &text, mode,
  263. make_test_options_options_def_group (&opts)));
  264. save_completion_result (opts, res, text);
  265. }
  266. catch (const gdb_exception_error &ex)
  267. {
  268. save_completion_result (opts, true, text);
  269. throw;
  270. }
  271. }
  272. /* Implementation of the "maintenance test-options require-delimiter"
  273. command. */
  274. static void
  275. maintenance_test_options_require_delimiter_command (const char *args,
  276. int from_tty)
  277. {
  278. maintenance_test_options_command_mode
  279. (args, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER);
  280. }
  281. /* Implementation of the "maintenance test-options
  282. unknown-is-error" command. */
  283. static void
  284. maintenance_test_options_unknown_is_error_command (const char *args,
  285. int from_tty)
  286. {
  287. maintenance_test_options_command_mode
  288. (args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR);
  289. }
  290. /* Implementation of the "maintenance test-options
  291. unknown-is-operand" command. */
  292. static void
  293. maintenance_test_options_unknown_is_operand_command (const char *args,
  294. int from_tty)
  295. {
  296. maintenance_test_options_command_mode
  297. (args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND);
  298. }
  299. /* Completer for the "maintenance test-options require-delimiter"
  300. command. */
  301. static void
  302. maintenance_test_options_require_delimiter_command_completer
  303. (cmd_list_element *ignore, completion_tracker &tracker,
  304. const char *text, const char *word)
  305. {
  306. maintenance_test_options_completer_mode
  307. (tracker, text, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER);
  308. }
  309. /* Completer for the "maintenance test-options unknown-is-error"
  310. command. */
  311. static void
  312. maintenance_test_options_unknown_is_error_command_completer
  313. (cmd_list_element *ignore, completion_tracker &tracker,
  314. const char *text, const char *word)
  315. {
  316. maintenance_test_options_completer_mode
  317. (tracker, text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR);
  318. }
  319. /* Completer for the "maintenance test-options unknown-is-operand"
  320. command. */
  321. static void
  322. maintenance_test_options_unknown_is_operand_command_completer
  323. (cmd_list_element *ignore, completion_tracker &tracker,
  324. const char *text, const char *word)
  325. {
  326. maintenance_test_options_completer_mode
  327. (tracker, text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND);
  328. }
  329. /* Command list for maint test-options. */
  330. static cmd_list_element *maintenance_test_options_list;
  331. void _initialize_maint_test_options ();
  332. void
  333. _initialize_maint_test_options ()
  334. {
  335. cmd_list_element *cmd;
  336. add_basic_prefix_cmd ("test-options", no_class,
  337. _("\
  338. Generic command for testing the options infrastructure."),
  339. &maintenance_test_options_list,
  340. 0, &maintenancelist);
  341. const auto def_group = make_test_options_options_def_group (nullptr);
  342. static const std::string help_require_delim_str
  343. = gdb::option::build_help (_("\
  344. Command used for testing options processing.\n\
  345. Usage: maint test-options require-delimiter [[OPTION]... --] [OPERAND]...\n\
  346. \n\
  347. Options:\n\
  348. %OPTIONS%\n\
  349. \n\
  350. If you specify any command option, you must use a double dash (\"--\")\n\
  351. to mark the end of option processing."),
  352. def_group);
  353. static const std::string help_unknown_is_error_str
  354. = gdb::option::build_help (_("\
  355. Command used for testing options processing.\n\
  356. Usage: maint test-options unknown-is-error [OPTION]... [OPERAND]...\n\
  357. \n\
  358. Options:\n\
  359. %OPTIONS%"),
  360. def_group);
  361. static const std::string help_unknown_is_operand_str
  362. = gdb::option::build_help (_("\
  363. Command used for testing options processing.\n\
  364. Usage: maint test-options unknown-is-operand [OPTION]... [OPERAND]...\n\
  365. \n\
  366. Options:\n\
  367. %OPTIONS%"),
  368. def_group);
  369. cmd = add_cmd ("require-delimiter", class_maintenance,
  370. maintenance_test_options_require_delimiter_command,
  371. help_require_delim_str.c_str (),
  372. &maintenance_test_options_list);
  373. set_cmd_completer_handle_brkchars
  374. (cmd, maintenance_test_options_require_delimiter_command_completer);
  375. cmd = add_cmd ("unknown-is-error", class_maintenance,
  376. maintenance_test_options_unknown_is_error_command,
  377. help_unknown_is_error_str.c_str (),
  378. &maintenance_test_options_list);
  379. set_cmd_completer_handle_brkchars
  380. (cmd, maintenance_test_options_unknown_is_error_command_completer);
  381. cmd = add_cmd ("unknown-is-operand", class_maintenance,
  382. maintenance_test_options_unknown_is_operand_command,
  383. help_unknown_is_operand_str.c_str (),
  384. &maintenance_test_options_list);
  385. set_cmd_completer_handle_brkchars
  386. (cmd, maintenance_test_options_unknown_is_operand_command_completer);
  387. add_cmd ("test-options-completion-result", class_maintenance,
  388. maintenance_show_test_options_completion_result,
  389. _("\
  390. Show maintenance test-options completion result.\n\
  391. Shows the results of completing\n\
  392. \"maint test-options require-delimiter\",\n\
  393. \"maint test-options unknown-is-error\", or\n\
  394. \"maint test-options unknown-is-operand\"."),
  395. &maintenance_show_cmdlist);
  396. }