interps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* Manages interpreters for GDB, the GNU debugger.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc.
  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. /* This is just a first cut at separating out the "interpreter"
  16. functions of gdb into self-contained modules. There are a couple
  17. of open areas that need to be sorted out:
  18. 1) The interpreter explicitly contains a UI_OUT, and can insert itself
  19. into the event loop, but it doesn't explicitly contain hooks for readline.
  20. I did this because it seems to me many interpreters won't want to use
  21. the readline command interface, and it is probably simpler to just let
  22. them take over the input in their resume proc. */
  23. #include "defs.h"
  24. #include "gdbcmd.h"
  25. #include "ui-out.h"
  26. #include "gdbsupport/event-loop.h"
  27. #include "event-top.h"
  28. #include "interps.h"
  29. #include "completer.h"
  30. #include "top.h" /* For command_loop. */
  31. #include "main.h"
  32. #include "gdbsupport/buildargv.h"
  33. /* Each UI has its own independent set of interpreters. */
  34. struct ui_interp_info
  35. {
  36. /* Each top level has its own independent set of interpreters. */
  37. struct interp *interp_list;
  38. struct interp *current_interpreter;
  39. struct interp *top_level_interpreter;
  40. /* The interpreter that is active while `interp_exec' is active, NULL
  41. at all other times. */
  42. struct interp *command_interpreter;
  43. };
  44. /* Get UI's ui_interp_info object. Never returns NULL. */
  45. static struct ui_interp_info *
  46. get_interp_info (struct ui *ui)
  47. {
  48. if (ui->interp_info == NULL)
  49. ui->interp_info = XCNEW (struct ui_interp_info);
  50. return ui->interp_info;
  51. }
  52. /* Get the current UI's ui_interp_info object. Never returns
  53. NULL. */
  54. static struct ui_interp_info *
  55. get_current_interp_info (void)
  56. {
  57. return get_interp_info (current_ui);
  58. }
  59. /* The magic initialization routine for this module. */
  60. static struct interp *interp_lookup_existing (struct ui *ui,
  61. const char *name);
  62. interp::interp (const char *name)
  63. : m_name (xstrdup (name))
  64. {
  65. this->inited = false;
  66. }
  67. interp::~interp ()
  68. {
  69. xfree (m_name);
  70. }
  71. /* An interpreter factory. Maps an interpreter name to the factory
  72. function that instantiates an interpreter by that name. */
  73. struct interp_factory
  74. {
  75. interp_factory (const char *name_, interp_factory_func func_)
  76. : name (name_), func (func_)
  77. {}
  78. /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
  79. const char *name;
  80. /* The function that creates the interpreter. */
  81. interp_factory_func func;
  82. };
  83. /* The registered interpreter factories. */
  84. static std::vector<interp_factory> interpreter_factories;
  85. /* See interps.h. */
  86. void
  87. interp_factory_register (const char *name, interp_factory_func func)
  88. {
  89. /* Assert that no factory for NAME is already registered. */
  90. for (const interp_factory &f : interpreter_factories)
  91. if (strcmp (f.name, name) == 0)
  92. {
  93. internal_error (__FILE__, __LINE__,
  94. _("interpreter factory already registered: \"%s\"\n"),
  95. name);
  96. }
  97. interpreter_factories.emplace_back (name, func);
  98. }
  99. /* Add interpreter INTERP to the gdb interpreter list. The
  100. interpreter must not have previously been added. */
  101. static void
  102. interp_add (struct ui *ui, struct interp *interp)
  103. {
  104. struct ui_interp_info *ui_interp = get_interp_info (ui);
  105. gdb_assert (interp_lookup_existing (ui, interp->name ()) == NULL);
  106. interp->next = ui_interp->interp_list;
  107. ui_interp->interp_list = interp;
  108. }
  109. /* This sets the current interpreter to be INTERP. If INTERP has not
  110. been initialized, then this will also run the init method.
  111. The TOP_LEVEL parameter tells if this new interpreter is
  112. the top-level one. The top-level is what is requested
  113. on the command line, and is responsible for reporting general
  114. notification about target state changes. For example, if
  115. MI is the top-level interpreter, then it will always report
  116. events such as target stops and new thread creation, even if they
  117. are caused by CLI commands. */
  118. static void
  119. interp_set (struct interp *interp, bool top_level)
  120. {
  121. struct ui_interp_info *ui_interp = get_current_interp_info ();
  122. struct interp *old_interp = ui_interp->current_interpreter;
  123. /* If we already have an interpreter, then trying to
  124. set top level interpreter is kinda pointless. */
  125. gdb_assert (!top_level || !ui_interp->current_interpreter);
  126. gdb_assert (!top_level || !ui_interp->top_level_interpreter);
  127. if (old_interp != NULL)
  128. {
  129. current_uiout->flush ();
  130. old_interp->suspend ();
  131. }
  132. ui_interp->current_interpreter = interp;
  133. if (top_level)
  134. ui_interp->top_level_interpreter = interp;
  135. /* We use interpreter_p for the "set interpreter" variable, so we need
  136. to make sure we have a malloc'ed copy for the set command to free. */
  137. if (interpreter_p != NULL
  138. && strcmp (interp->name (), interpreter_p) != 0)
  139. {
  140. xfree (interpreter_p);
  141. interpreter_p = xstrdup (interp->name ());
  142. }
  143. /* Run the init proc. */
  144. if (!interp->inited)
  145. {
  146. interp->init (top_level);
  147. interp->inited = true;
  148. }
  149. /* Do this only after the interpreter is initialized. */
  150. current_uiout = interp->interp_ui_out ();
  151. /* Clear out any installed interpreter hooks/event handlers. */
  152. clear_interpreter_hooks ();
  153. interp->resume ();
  154. }
  155. /* Look up the interpreter for NAME. If no such interpreter exists,
  156. return NULL, otherwise return a pointer to the interpreter. */
  157. static struct interp *
  158. interp_lookup_existing (struct ui *ui, const char *name)
  159. {
  160. struct ui_interp_info *ui_interp = get_interp_info (ui);
  161. struct interp *interp;
  162. for (interp = ui_interp->interp_list;
  163. interp != NULL;
  164. interp = interp->next)
  165. {
  166. if (strcmp (interp->name (), name) == 0)
  167. return interp;
  168. }
  169. return NULL;
  170. }
  171. /* See interps.h. */
  172. struct interp *
  173. interp_lookup (struct ui *ui, const char *name)
  174. {
  175. if (name == NULL || strlen (name) == 0)
  176. return NULL;
  177. /* Only create each interpreter once per top level. */
  178. struct interp *interp = interp_lookup_existing (ui, name);
  179. if (interp != NULL)
  180. return interp;
  181. for (const interp_factory &factory : interpreter_factories)
  182. if (strcmp (factory.name, name) == 0)
  183. {
  184. interp = factory.func (name);
  185. interp_add (ui, interp);
  186. return interp;
  187. }
  188. return NULL;
  189. }
  190. /* See interps.h. */
  191. void
  192. set_top_level_interpreter (const char *name)
  193. {
  194. /* Find it. */
  195. struct interp *interp = interp_lookup (current_ui, name);
  196. if (interp == NULL)
  197. error (_("Interpreter `%s' unrecognized"), name);
  198. /* Install it. */
  199. interp_set (interp, true);
  200. }
  201. void
  202. current_interp_set_logging (ui_file_up logfile, bool logging_redirect,
  203. bool debug_redirect)
  204. {
  205. struct ui_interp_info *ui_interp = get_current_interp_info ();
  206. struct interp *interp = ui_interp->current_interpreter;
  207. interp->set_logging (std::move (logfile), logging_redirect, debug_redirect);
  208. }
  209. /* Temporarily overrides the current interpreter. */
  210. struct interp *
  211. scoped_restore_interp::set_interp (const char *name)
  212. {
  213. struct ui_interp_info *ui_interp = get_current_interp_info ();
  214. struct interp *interp = interp_lookup (current_ui, name);
  215. struct interp *old_interp = ui_interp->current_interpreter;
  216. if (interp)
  217. ui_interp->current_interpreter = interp;
  218. return old_interp;
  219. }
  220. /* Returns true if the current interp is the passed in name. */
  221. int
  222. current_interp_named_p (const char *interp_name)
  223. {
  224. struct ui_interp_info *ui_interp = get_current_interp_info ();
  225. struct interp *interp = ui_interp->current_interpreter;
  226. if (interp != NULL)
  227. return (strcmp (interp->name (), interp_name) == 0);
  228. return 0;
  229. }
  230. /* The interpreter that was active when a command was executed.
  231. Normally that'd always be CURRENT_INTERPRETER, except that MI's
  232. -interpreter-exec command doesn't actually flip the current
  233. interpreter when running its sub-command. The
  234. `command_interpreter' global tracks when interp_exec is called
  235. (IOW, when -interpreter-exec is called). If that is set, it is
  236. INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
  237. INTERP "CMD". Otherwise, interp_exec isn't active, and so the
  238. interpreter running the command is the current interpreter. */
  239. struct interp *
  240. command_interp (void)
  241. {
  242. struct ui_interp_info *ui_interp = get_current_interp_info ();
  243. if (ui_interp->command_interpreter != NULL)
  244. return ui_interp->command_interpreter;
  245. else
  246. return ui_interp->current_interpreter;
  247. }
  248. /* See interps.h. */
  249. void
  250. interp_pre_command_loop (struct interp *interp)
  251. {
  252. gdb_assert (interp != NULL);
  253. interp->pre_command_loop ();
  254. }
  255. /* See interp.h */
  256. int
  257. interp_supports_command_editing (struct interp *interp)
  258. {
  259. return interp->supports_command_editing ();
  260. }
  261. /* interp_exec - This executes COMMAND_STR in the current
  262. interpreter. */
  263. struct gdb_exception
  264. interp_exec (struct interp *interp, const char *command_str)
  265. {
  266. struct ui_interp_info *ui_interp = get_current_interp_info ();
  267. /* See `command_interp' for why we do this. */
  268. scoped_restore save_command_interp
  269. = make_scoped_restore (&ui_interp->command_interpreter, interp);
  270. return interp->exec (command_str);
  271. }
  272. /* A convenience routine that nulls out all the common command hooks.
  273. Use it when removing your interpreter in its suspend proc. */
  274. void
  275. clear_interpreter_hooks (void)
  276. {
  277. deprecated_print_frame_info_listing_hook = 0;
  278. /*print_frame_more_info_hook = 0; */
  279. deprecated_query_hook = 0;
  280. deprecated_warning_hook = 0;
  281. deprecated_readline_begin_hook = 0;
  282. deprecated_readline_hook = 0;
  283. deprecated_readline_end_hook = 0;
  284. deprecated_context_hook = 0;
  285. deprecated_call_command_hook = 0;
  286. deprecated_error_begin_hook = 0;
  287. }
  288. static void
  289. interpreter_exec_cmd (const char *args, int from_tty)
  290. {
  291. struct ui_interp_info *ui_interp = get_current_interp_info ();
  292. struct interp *old_interp, *interp_to_use;
  293. unsigned int nrules;
  294. unsigned int i;
  295. /* Interpreters may clobber stdout/stderr (e.g. in mi_interp::resume at time
  296. of writing), preserve their state here. */
  297. scoped_restore save_stdout = make_scoped_restore (&gdb_stdout);
  298. scoped_restore save_stderr = make_scoped_restore (&gdb_stderr);
  299. scoped_restore save_stdlog = make_scoped_restore (&gdb_stdlog);
  300. scoped_restore save_stdtarg = make_scoped_restore (&gdb_stdtarg);
  301. scoped_restore save_stdtargerr = make_scoped_restore (&gdb_stdtargerr);
  302. if (args == NULL)
  303. error_no_arg (_("interpreter-exec command"));
  304. gdb_argv prules (args);
  305. nrules = prules.count ();
  306. if (nrules < 2)
  307. error (_("Usage: interpreter-exec INTERPRETER COMMAND..."));
  308. old_interp = ui_interp->current_interpreter;
  309. interp_to_use = interp_lookup (current_ui, prules[0]);
  310. if (interp_to_use == NULL)
  311. error (_("Could not find interpreter \"%s\"."), prules[0]);
  312. interp_set (interp_to_use, false);
  313. for (i = 1; i < nrules; i++)
  314. {
  315. struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
  316. if (e.reason < 0)
  317. {
  318. interp_set (old_interp, 0);
  319. error (_("error in command: \"%s\"."), prules[i]);
  320. }
  321. }
  322. interp_set (old_interp, 0);
  323. }
  324. /* See interps.h. */
  325. void
  326. interpreter_completer (struct cmd_list_element *ignore,
  327. completion_tracker &tracker,
  328. const char *text, const char *word)
  329. {
  330. int textlen = strlen (text);
  331. for (const interp_factory &interp : interpreter_factories)
  332. {
  333. if (strncmp (interp.name, text, textlen) == 0)
  334. {
  335. tracker.add_completion
  336. (make_completion_match_str (interp.name, text, word));
  337. }
  338. }
  339. }
  340. struct interp *
  341. top_level_interpreter (void)
  342. {
  343. struct ui_interp_info *ui_interp = get_current_interp_info ();
  344. return ui_interp->top_level_interpreter;
  345. }
  346. /* See interps.h. */
  347. struct interp *
  348. current_interpreter (void)
  349. {
  350. struct ui_interp_info *ui_interp = get_interp_info (current_ui);
  351. return ui_interp->current_interpreter;
  352. }
  353. /* This just adds the "interpreter-exec" command. */
  354. void _initialize_interpreter ();
  355. void
  356. _initialize_interpreter ()
  357. {
  358. struct cmd_list_element *c;
  359. c = add_cmd ("interpreter-exec", class_support,
  360. interpreter_exec_cmd, _("\
  361. Execute a command in an interpreter.\n\
  362. Usage: interpreter-exec INTERPRETER COMMAND...\n\
  363. The first argument is the name of the interpreter to use.\n\
  364. The following arguments are the commands to execute.\n\
  365. A command can have arguments, separated by spaces.\n\
  366. These spaces must be escaped using \\ or the command\n\
  367. and its arguments must be enclosed in double quotes."), &cmdlist);
  368. set_cmd_completer (c, interpreter_completer);
  369. }