mi-cmds.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* MI Command Set for GDB, the GNU debugger.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions (a Red Hat company).
  4. This file is part of GDB.
  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. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "top.h"
  17. #include "mi-cmds.h"
  18. #include "mi-main.h"
  19. #include "mi-parse.h"
  20. #include <map>
  21. #include <string>
  22. /* MI command table (built at run time). */
  23. static std::map<std::string, mi_command_up> mi_cmd_table;
  24. /* MI command with a pure MI implementation. */
  25. struct mi_command_mi : public mi_command
  26. {
  27. /* Constructor. For NAME and SUPPRESS_NOTIFICATION see mi_command
  28. constructor, FUNC is the function called from do_invoke, which
  29. implements this MI command. */
  30. mi_command_mi (const char *name, mi_cmd_argv_ftype func,
  31. int *suppress_notification)
  32. : mi_command (name, suppress_notification),
  33. m_argv_function (func)
  34. {
  35. gdb_assert (func != nullptr);
  36. }
  37. /* Called when this MI command has been invoked, calls m_argv_function
  38. with arguments contained within PARSE. */
  39. void invoke (struct mi_parse *parse) const override
  40. {
  41. mi_parse_argv (parse->args, parse);
  42. if (parse->argv == nullptr)
  43. error (_("Problem parsing arguments: %s %s"), parse->command,
  44. parse->args);
  45. this->m_argv_function (parse->command, parse->argv, parse->argc);
  46. }
  47. private:
  48. /* The function that implements this MI command. */
  49. mi_cmd_argv_ftype *m_argv_function;
  50. };
  51. /* MI command implemented on top of a CLI command. */
  52. struct mi_command_cli : public mi_command
  53. {
  54. /* Constructor. For NAME and SUPPRESS_NOTIFICATION see mi_command
  55. constructor, CLI_NAME is the name of a CLI command that should be
  56. invoked to implement this MI command. If ARGS_P is true then any
  57. arguments from entered by the user as part of the MI command line are
  58. forwarded to CLI_NAME as its argument string, otherwise, if ARGS_P is
  59. false, nullptr is send to CLI_NAME as its argument string. */
  60. mi_command_cli (const char *name, const char *cli_name, bool args_p,
  61. int *suppress_notification)
  62. : mi_command (name, suppress_notification),
  63. m_cli_name (cli_name),
  64. m_args_p (args_p)
  65. { /* Nothing. */ }
  66. /* Called when this MI command has been invoked, calls the m_cli_name
  67. CLI function. In m_args_p is true then the argument string from
  68. within PARSE is passed through to the CLI function, otherwise nullptr
  69. is passed through to the CLI function as its argument string. */
  70. void invoke (struct mi_parse *parse) const override
  71. {
  72. const char *args = m_args_p ? parse->args : nullptr;
  73. mi_execute_cli_command (m_cli_name, m_args_p, args);
  74. }
  75. private:
  76. /* The name of the CLI command to execute. */
  77. const char *m_cli_name;
  78. /* Should we be passing an argument string to the m_cli_name function? */
  79. bool m_args_p;
  80. };
  81. /* See mi-cmds.h. */
  82. bool
  83. insert_mi_cmd_entry (mi_command_up command)
  84. {
  85. gdb_assert (command != nullptr);
  86. const std::string &name = command->name ();
  87. if (mi_cmd_table.find (name) != mi_cmd_table.end ())
  88. return false;
  89. mi_cmd_table[name] = std::move (command);
  90. return true;
  91. }
  92. /* See mi-cmds.h. */
  93. bool
  94. remove_mi_cmd_entry (const std::string &name)
  95. {
  96. if (mi_cmd_table.find (name) == mi_cmd_table.end ())
  97. return false;
  98. mi_cmd_table.erase (name);
  99. return true;
  100. }
  101. /* See mi-cmds.h. */
  102. void
  103. remove_mi_cmd_entries (remove_mi_cmd_entries_ftype callback)
  104. {
  105. for (auto it = mi_cmd_table.cbegin (); it != mi_cmd_table.cend (); )
  106. {
  107. if (callback (it->second.get ()))
  108. it = mi_cmd_table.erase (it);
  109. else
  110. ++it;
  111. }
  112. }
  113. /* Create and register a new MI command with an MI specific implementation.
  114. NAME must name an MI command that does not already exist, otherwise an
  115. assertion will trigger. */
  116. static void
  117. add_mi_cmd_mi (const char *name, mi_cmd_argv_ftype function,
  118. int *suppress_notification = nullptr)
  119. {
  120. mi_command_up command (new mi_command_mi (name, function,
  121. suppress_notification));
  122. bool success = insert_mi_cmd_entry (std::move (command));
  123. gdb_assert (success);
  124. }
  125. /* Create and register a new MI command implemented on top of a CLI
  126. command. NAME must name an MI command that does not already exist,
  127. otherwise an assertion will trigger. */
  128. static void
  129. add_mi_cmd_cli (const char *name, const char *cli_name, int args_p,
  130. int *suppress_notification = nullptr)
  131. {
  132. mi_command_up command (new mi_command_cli (name, cli_name, args_p != 0,
  133. suppress_notification));
  134. bool success = insert_mi_cmd_entry (std::move (command));
  135. gdb_assert (success);
  136. }
  137. /* See mi-cmds.h. */
  138. mi_command::mi_command (const char *name, int *suppress_notification)
  139. : m_name (name),
  140. m_suppress_notification (suppress_notification)
  141. {
  142. gdb_assert (m_name != nullptr && m_name[0] != '\0');
  143. }
  144. /* See mi-cmds.h. */
  145. gdb::optional<scoped_restore_tmpl<int>>
  146. mi_command::do_suppress_notification () const
  147. {
  148. if (m_suppress_notification != nullptr)
  149. return scoped_restore_tmpl<int> (m_suppress_notification, 1);
  150. else
  151. return {};
  152. }
  153. /* Initialize the available MI commands. */
  154. static void
  155. add_builtin_mi_commands ()
  156. {
  157. add_mi_cmd_mi ("ada-task-info", mi_cmd_ada_task_info);
  158. add_mi_cmd_mi ("add-inferior", mi_cmd_add_inferior);
  159. add_mi_cmd_cli ("break-after", "ignore", 1,
  160. &mi_suppress_notification.breakpoint);
  161. add_mi_cmd_mi ("break-condition",mi_cmd_break_condition,
  162. &mi_suppress_notification.breakpoint);
  163. add_mi_cmd_mi ("break-commands", mi_cmd_break_commands,
  164. &mi_suppress_notification.breakpoint);
  165. add_mi_cmd_cli ("break-delete", "delete breakpoint", 1,
  166. &mi_suppress_notification.breakpoint);
  167. add_mi_cmd_cli ("break-disable", "disable breakpoint", 1,
  168. &mi_suppress_notification.breakpoint);
  169. add_mi_cmd_cli ("break-enable", "enable breakpoint", 1,
  170. &mi_suppress_notification.breakpoint);
  171. add_mi_cmd_cli ("break-info", "info break", 1);
  172. add_mi_cmd_mi ("break-insert", mi_cmd_break_insert,
  173. &mi_suppress_notification.breakpoint);
  174. add_mi_cmd_mi ("dprintf-insert", mi_cmd_dprintf_insert,
  175. &mi_suppress_notification.breakpoint);
  176. add_mi_cmd_cli ("break-list", "info break", 0);
  177. add_mi_cmd_mi ("break-passcount", mi_cmd_break_passcount,
  178. &mi_suppress_notification.breakpoint);
  179. add_mi_cmd_mi ("break-watch", mi_cmd_break_watch,
  180. &mi_suppress_notification.breakpoint);
  181. add_mi_cmd_mi ("catch-assert", mi_cmd_catch_assert,
  182. &mi_suppress_notification.breakpoint);
  183. add_mi_cmd_mi ("catch-exception", mi_cmd_catch_exception,
  184. &mi_suppress_notification.breakpoint);
  185. add_mi_cmd_mi ("catch-handlers", mi_cmd_catch_handlers,
  186. &mi_suppress_notification.breakpoint);
  187. add_mi_cmd_mi ("catch-load", mi_cmd_catch_load,
  188. &mi_suppress_notification.breakpoint);
  189. add_mi_cmd_mi ("catch-unload", mi_cmd_catch_unload,
  190. &mi_suppress_notification.breakpoint);
  191. add_mi_cmd_mi ("catch-throw", mi_cmd_catch_throw,
  192. &mi_suppress_notification.breakpoint),
  193. add_mi_cmd_mi ("catch-rethrow", mi_cmd_catch_rethrow,
  194. &mi_suppress_notification.breakpoint),
  195. add_mi_cmd_mi ("catch-catch", mi_cmd_catch_catch,
  196. &mi_suppress_notification.breakpoint),
  197. add_mi_cmd_mi ("complete", mi_cmd_complete);
  198. add_mi_cmd_mi ("data-disassemble", mi_cmd_disassemble);
  199. add_mi_cmd_mi ("data-evaluate-expression", mi_cmd_data_evaluate_expression);
  200. add_mi_cmd_mi ("data-list-changed-registers",
  201. mi_cmd_data_list_changed_registers);
  202. add_mi_cmd_mi ("data-list-register-names", mi_cmd_data_list_register_names);
  203. add_mi_cmd_mi ("data-list-register-values",
  204. mi_cmd_data_list_register_values);
  205. add_mi_cmd_mi ("data-read-memory", mi_cmd_data_read_memory);
  206. add_mi_cmd_mi ("data-read-memory-bytes", mi_cmd_data_read_memory_bytes);
  207. add_mi_cmd_mi ("data-write-memory", mi_cmd_data_write_memory,
  208. &mi_suppress_notification.memory);
  209. add_mi_cmd_mi ("data-write-memory-bytes", mi_cmd_data_write_memory_bytes,
  210. &mi_suppress_notification.memory);
  211. add_mi_cmd_mi ("data-write-register-values",
  212. mi_cmd_data_write_register_values);
  213. add_mi_cmd_mi ("enable-timings", mi_cmd_enable_timings);
  214. add_mi_cmd_mi ("enable-pretty-printing", mi_cmd_enable_pretty_printing);
  215. add_mi_cmd_mi ("enable-frame-filters", mi_cmd_enable_frame_filters);
  216. add_mi_cmd_mi ("environment-cd", mi_cmd_env_cd);
  217. add_mi_cmd_mi ("environment-directory", mi_cmd_env_dir);
  218. add_mi_cmd_mi ("environment-path", mi_cmd_env_path);
  219. add_mi_cmd_mi ("environment-pwd", mi_cmd_env_pwd);
  220. add_mi_cmd_cli ("exec-arguments", "set args", 1,
  221. &mi_suppress_notification.cmd_param_changed);
  222. add_mi_cmd_mi ("exec-continue", mi_cmd_exec_continue);
  223. add_mi_cmd_mi ("exec-finish", mi_cmd_exec_finish);
  224. add_mi_cmd_mi ("exec-jump", mi_cmd_exec_jump);
  225. add_mi_cmd_mi ("exec-interrupt", mi_cmd_exec_interrupt);
  226. add_mi_cmd_mi ("exec-next", mi_cmd_exec_next);
  227. add_mi_cmd_mi ("exec-next-instruction", mi_cmd_exec_next_instruction);
  228. add_mi_cmd_mi ("exec-return", mi_cmd_exec_return);
  229. add_mi_cmd_mi ("exec-run", mi_cmd_exec_run);
  230. add_mi_cmd_mi ("exec-step", mi_cmd_exec_step);
  231. add_mi_cmd_mi ("exec-step-instruction", mi_cmd_exec_step_instruction);
  232. add_mi_cmd_cli ("exec-until", "until", 1);
  233. add_mi_cmd_cli ("file-exec-and-symbols", "file", 1);
  234. add_mi_cmd_cli ("file-exec-file", "exec-file", 1);
  235. add_mi_cmd_mi ("file-list-exec-source-file",
  236. mi_cmd_file_list_exec_source_file);
  237. add_mi_cmd_mi ("file-list-exec-source-files",
  238. mi_cmd_file_list_exec_source_files);
  239. add_mi_cmd_mi ("file-list-shared-libraries",
  240. mi_cmd_file_list_shared_libraries),
  241. add_mi_cmd_cli ("file-symbol-file", "symbol-file", 1);
  242. add_mi_cmd_mi ("fix-multi-location-breakpoint-output",
  243. mi_cmd_fix_multi_location_breakpoint_output),
  244. add_mi_cmd_mi ("gdb-exit", mi_cmd_gdb_exit);
  245. add_mi_cmd_cli ("gdb-set", "set", 1,
  246. &mi_suppress_notification.cmd_param_changed);
  247. add_mi_cmd_cli ("gdb-show", "show", 1);
  248. add_mi_cmd_cli ("gdb-version", "show version", 0);
  249. add_mi_cmd_mi ("inferior-tty-set", mi_cmd_inferior_tty_set);
  250. add_mi_cmd_mi ("inferior-tty-show", mi_cmd_inferior_tty_show);
  251. add_mi_cmd_mi ("info-ada-exceptions", mi_cmd_info_ada_exceptions);
  252. add_mi_cmd_mi ("info-gdb-mi-command", mi_cmd_info_gdb_mi_command);
  253. add_mi_cmd_mi ("info-os", mi_cmd_info_os);
  254. add_mi_cmd_mi ("interpreter-exec", mi_cmd_interpreter_exec);
  255. add_mi_cmd_mi ("list-features", mi_cmd_list_features);
  256. add_mi_cmd_mi ("list-target-features", mi_cmd_list_target_features);
  257. add_mi_cmd_mi ("list-thread-groups", mi_cmd_list_thread_groups);
  258. add_mi_cmd_mi ("remove-inferior", mi_cmd_remove_inferior);
  259. add_mi_cmd_mi ("stack-info-depth", mi_cmd_stack_info_depth);
  260. add_mi_cmd_mi ("stack-info-frame", mi_cmd_stack_info_frame);
  261. add_mi_cmd_mi ("stack-list-arguments", mi_cmd_stack_list_args);
  262. add_mi_cmd_mi ("stack-list-frames", mi_cmd_stack_list_frames);
  263. add_mi_cmd_mi ("stack-list-locals", mi_cmd_stack_list_locals);
  264. add_mi_cmd_mi ("stack-list-variables", mi_cmd_stack_list_variables);
  265. add_mi_cmd_mi ("stack-select-frame", mi_cmd_stack_select_frame,
  266. &mi_suppress_notification.user_selected_context);
  267. add_mi_cmd_mi ("symbol-list-lines", mi_cmd_symbol_list_lines);
  268. add_mi_cmd_mi ("symbol-info-functions", mi_cmd_symbol_info_functions);
  269. add_mi_cmd_mi ("symbol-info-variables", mi_cmd_symbol_info_variables);
  270. add_mi_cmd_mi ("symbol-info-types", mi_cmd_symbol_info_types);
  271. add_mi_cmd_mi ("symbol-info-modules", mi_cmd_symbol_info_modules);
  272. add_mi_cmd_mi ("symbol-info-module-functions",
  273. mi_cmd_symbol_info_module_functions);
  274. add_mi_cmd_mi ("symbol-info-module-variables",
  275. mi_cmd_symbol_info_module_variables);
  276. add_mi_cmd_cli ("target-attach", "attach", 1);
  277. add_mi_cmd_mi ("target-detach", mi_cmd_target_detach);
  278. add_mi_cmd_cli ("target-disconnect", "disconnect", 0);
  279. add_mi_cmd_cli ("target-download", "load", 1);
  280. add_mi_cmd_mi ("target-file-delete", mi_cmd_target_file_delete);
  281. add_mi_cmd_mi ("target-file-get", mi_cmd_target_file_get);
  282. add_mi_cmd_mi ("target-file-put", mi_cmd_target_file_put);
  283. add_mi_cmd_mi ("target-flash-erase", mi_cmd_target_flash_erase);
  284. add_mi_cmd_cli ("target-select", "target", 1);
  285. add_mi_cmd_mi ("thread-info", mi_cmd_thread_info);
  286. add_mi_cmd_mi ("thread-list-ids", mi_cmd_thread_list_ids);
  287. add_mi_cmd_mi ("thread-select", mi_cmd_thread_select,
  288. &mi_suppress_notification.user_selected_context);
  289. add_mi_cmd_mi ("trace-define-variable", mi_cmd_trace_define_variable);
  290. add_mi_cmd_mi ("trace-find", mi_cmd_trace_find,
  291. &mi_suppress_notification.traceframe);
  292. add_mi_cmd_mi ("trace-frame-collected", mi_cmd_trace_frame_collected);
  293. add_mi_cmd_mi ("trace-list-variables", mi_cmd_trace_list_variables);
  294. add_mi_cmd_mi ("trace-save", mi_cmd_trace_save);
  295. add_mi_cmd_mi ("trace-start", mi_cmd_trace_start);
  296. add_mi_cmd_mi ("trace-status", mi_cmd_trace_status);
  297. add_mi_cmd_mi ("trace-stop", mi_cmd_trace_stop);
  298. add_mi_cmd_mi ("var-assign", mi_cmd_var_assign);
  299. add_mi_cmd_mi ("var-create", mi_cmd_var_create);
  300. add_mi_cmd_mi ("var-delete", mi_cmd_var_delete);
  301. add_mi_cmd_mi ("var-evaluate-expression", mi_cmd_var_evaluate_expression);
  302. add_mi_cmd_mi ("var-info-path-expression", mi_cmd_var_info_path_expression);
  303. add_mi_cmd_mi ("var-info-expression", mi_cmd_var_info_expression);
  304. add_mi_cmd_mi ("var-info-num-children", mi_cmd_var_info_num_children);
  305. add_mi_cmd_mi ("var-info-type", mi_cmd_var_info_type);
  306. add_mi_cmd_mi ("var-list-children", mi_cmd_var_list_children);
  307. add_mi_cmd_mi ("var-set-format", mi_cmd_var_set_format);
  308. add_mi_cmd_mi ("var-set-frozen", mi_cmd_var_set_frozen);
  309. add_mi_cmd_mi ("var-set-update-range", mi_cmd_var_set_update_range);
  310. add_mi_cmd_mi ("var-set-visualizer", mi_cmd_var_set_visualizer);
  311. add_mi_cmd_mi ("var-show-attributes", mi_cmd_var_show_attributes);
  312. add_mi_cmd_mi ("var-show-format", mi_cmd_var_show_format);
  313. add_mi_cmd_mi ("var-update", mi_cmd_var_update);
  314. }
  315. /* See mi-cmds.h. */
  316. mi_command *
  317. mi_cmd_lookup (const char *command)
  318. {
  319. gdb_assert (command != nullptr);
  320. auto it = mi_cmd_table.find (command);
  321. if (it == mi_cmd_table.end ())
  322. return nullptr;
  323. return it->second.get ();
  324. }
  325. void _initialize_mi_cmds ();
  326. void
  327. _initialize_mi_cmds ()
  328. {
  329. add_builtin_mi_commands ();
  330. }