PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/gdb/gdb/interps.c

https://github.com/okuoku/freebsd-head
C | 486 lines | 323 code | 70 blank | 93 comment | 77 complexity | 4a40492bb34cbfc2a38d8d1cf0866259 MD5 | raw file
  1. /* Manages interpreters for GDB, the GNU debugger.
  2. Copyright 2000, 2002, 2003 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 2 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, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. /* This is just a first cut at separating out the "interpreter"
  18. functions of gdb into self-contained modules. There are a couple
  19. of open areas that need to be sorted out:
  20. 1) The interpreter explicitly contains a UI_OUT, and can insert itself
  21. into the event loop, but it doesn't explicitly contain hooks for readline.
  22. I did this because it seems to me many interpreters won't want to use
  23. the readline command interface, and it is probably simpler to just let
  24. them take over the input in their resume proc. */
  25. #include "defs.h"
  26. #include "gdbcmd.h"
  27. #include "ui-out.h"
  28. #include "event-loop.h"
  29. #include "event-top.h"
  30. #include "interps.h"
  31. #include "completer.h"
  32. #include "gdb_string.h"
  33. #include "gdb-events.h"
  34. #include "gdb_assert.h"
  35. #include "top.h" /* For command_loop. */
  36. struct interp
  37. {
  38. /* This is the name in "-i=" and set interpreter. */
  39. const char *name;
  40. /* Interpreters are stored in a linked list, this is the next
  41. one... */
  42. struct interp *next;
  43. /* This is a cookie that an instance of the interpreter can use.
  44. This is a bit confused right now as the exact initialization
  45. sequence for it, and how it relates to the interpreter's uiout
  46. object is a bit confused. */
  47. void *data;
  48. /* Has the init_proc been run? */
  49. int inited;
  50. /* This is the ui_out used to collect results for this interpreter.
  51. It can be a formatter for stdout, as is the case for the console
  52. & mi outputs, or it might be a result formatter. */
  53. struct ui_out *interpreter_out;
  54. const struct interp_procs *procs;
  55. int quiet_p;
  56. };
  57. /* Functions local to this file. */
  58. static void initialize_interps (void);
  59. static char **interpreter_completer (char *text, char *word);
  60. /* The magic initialization routine for this module. */
  61. void _initialize_interpreter (void);
  62. /* Variables local to this file: */
  63. static struct interp *interp_list = NULL;
  64. static struct interp *current_interpreter = NULL;
  65. static int interpreter_initialized = 0;
  66. /* interp_new - This allocates space for a new interpreter,
  67. fills the fields from the inputs, and returns a pointer to the
  68. interpreter. */
  69. struct interp *
  70. interp_new (const char *name, void *data, struct ui_out *uiout,
  71. const struct interp_procs *procs)
  72. {
  73. struct interp *new_interp;
  74. new_interp = XMALLOC (struct interp);
  75. new_interp->name = xstrdup (name);
  76. new_interp->data = data;
  77. new_interp->interpreter_out = uiout;
  78. new_interp->quiet_p = 0;
  79. new_interp->procs = procs;
  80. new_interp->inited = 0;
  81. return new_interp;
  82. }
  83. /* Add interpreter INTERP to the gdb interpreter list. The
  84. interpreter must not have previously been added. */
  85. void
  86. interp_add (struct interp *interp)
  87. {
  88. if (!interpreter_initialized)
  89. initialize_interps ();
  90. gdb_assert (interp_lookup (interp->name) == NULL);
  91. interp->next = interp_list;
  92. interp_list = interp;
  93. }
  94. /* This sets the current interpreter to be INTERP. If INTERP has not
  95. been initialized, then this will also run the init proc. If the
  96. init proc is successful, return 1, if it fails, set the old
  97. interpreter back in place and return 0. If we can't restore the
  98. old interpreter, then raise an internal error, since we are in
  99. pretty bad shape at this point. */
  100. int
  101. interp_set (struct interp *interp)
  102. {
  103. struct interp *old_interp = current_interpreter;
  104. int first_time = 0;
  105. char buffer[64];
  106. if (current_interpreter != NULL)
  107. {
  108. do_all_continuations ();
  109. ui_out_flush (uiout);
  110. if (current_interpreter->procs->suspend_proc
  111. && !current_interpreter->procs->suspend_proc (current_interpreter->
  112. data))
  113. {
  114. error ("Could not suspend interpreter \"%s\"\n",
  115. current_interpreter->name);
  116. }
  117. }
  118. else
  119. {
  120. first_time = 1;
  121. }
  122. current_interpreter = interp;
  123. /* We use interpreter_p for the "set interpreter" variable, so we need
  124. to make sure we have a malloc'ed copy for the set command to free. */
  125. if (interpreter_p != NULL
  126. && strcmp (current_interpreter->name, interpreter_p) != 0)
  127. {
  128. xfree (interpreter_p);
  129. interpreter_p = xstrdup (current_interpreter->name);
  130. }
  131. uiout = interp->interpreter_out;
  132. /* Run the init proc. If it fails, try to restore the old interp. */
  133. if (!interp->inited)
  134. {
  135. if (interp->procs->init_proc != NULL)
  136. {
  137. interp->data = interp->procs->init_proc ();
  138. }
  139. interp->inited = 1;
  140. }
  141. /* Clear out any installed interpreter hooks/event handlers. */
  142. clear_interpreter_hooks ();
  143. if (interp->procs->resume_proc != NULL
  144. && (!interp->procs->resume_proc (interp->data)))
  145. {
  146. if (old_interp == NULL || !interp_set (old_interp))
  147. internal_error (__FILE__, __LINE__,
  148. "Failed to initialize new interp \"%s\" %s",
  149. interp->name, "and could not restore old interp!\n");
  150. return 0;
  151. }
  152. /* Finally, put up the new prompt to show that we are indeed here.
  153. Also, display_gdb_prompt for the console does some readline magic
  154. which is needed for the console interpreter, at least... */
  155. if (!first_time)
  156. {
  157. if (!interp_quiet_p (interp))
  158. {
  159. sprintf (buffer, "Switching to interpreter \"%.24s\".\n",
  160. interp->name);
  161. ui_out_text (uiout, buffer);
  162. }
  163. display_gdb_prompt (NULL);
  164. }
  165. return 1;
  166. }
  167. /* interp_lookup - Looks up the interpreter for NAME. If no such
  168. interpreter exists, return NULL, otherwise return a pointer to the
  169. interpreter. */
  170. struct interp *
  171. interp_lookup (const char *name)
  172. {
  173. struct interp *interp;
  174. if (name == NULL || strlen (name) == 0)
  175. return NULL;
  176. for (interp = interp_list; interp != NULL; interp = interp->next)
  177. {
  178. if (strcmp (interp->name, name) == 0)
  179. return interp;
  180. }
  181. return NULL;
  182. }
  183. /* Returns the current interpreter. */
  184. struct ui_out *
  185. interp_ui_out (struct interp *interp)
  186. {
  187. if (interp != NULL)
  188. return interp->interpreter_out;
  189. return current_interpreter->interpreter_out;
  190. }
  191. /* Returns true if the current interp is the passed in name. */
  192. int
  193. current_interp_named_p (const char *interp_name)
  194. {
  195. if (current_interpreter)
  196. return (strcmp (current_interpreter->name, interp_name) == 0);
  197. return 0;
  198. }
  199. /* This is called in display_gdb_prompt. If the proc returns a zero
  200. value, display_gdb_prompt will return without displaying the
  201. prompt. */
  202. int
  203. current_interp_display_prompt_p (void)
  204. {
  205. if (current_interpreter == NULL
  206. || current_interpreter->procs->prompt_proc_p == NULL)
  207. return 0;
  208. else
  209. return current_interpreter->procs->prompt_proc_p (current_interpreter->
  210. data);
  211. }
  212. /* Run the current command interpreter's main loop. */
  213. void
  214. current_interp_command_loop (void)
  215. {
  216. /* Somewhat messy. For the moment prop up all the old ways of
  217. selecting the command loop. `command_loop_hook' should be
  218. deprecated. */
  219. if (command_loop_hook != NULL)
  220. command_loop_hook ();
  221. else if (current_interpreter != NULL
  222. && current_interpreter->procs->command_loop_proc != NULL)
  223. current_interpreter->procs->command_loop_proc (current_interpreter->data);
  224. else if (event_loop_p)
  225. cli_command_loop ();
  226. else
  227. command_loop ();
  228. }
  229. int
  230. interp_quiet_p (struct interp *interp)
  231. {
  232. if (interp != NULL)
  233. return interp->quiet_p;
  234. else
  235. return current_interpreter->quiet_p;
  236. }
  237. static int
  238. interp_set_quiet (struct interp *interp, int quiet)
  239. {
  240. int old_val = interp->quiet_p;
  241. interp->quiet_p = quiet;
  242. return old_val;
  243. }
  244. /* interp_exec - This executes COMMAND_STR in the current
  245. interpreter. */
  246. int
  247. interp_exec_p (struct interp *interp)
  248. {
  249. return interp->procs->exec_proc != NULL;
  250. }
  251. int
  252. interp_exec (struct interp *interp, const char *command_str)
  253. {
  254. if (interp->procs->exec_proc != NULL)
  255. {
  256. return interp->procs->exec_proc (interp->data, command_str);
  257. }
  258. return 0;
  259. }
  260. /* A convenience routine that nulls out all the
  261. common command hooks. Use it when removing your interpreter in its
  262. suspend proc. */
  263. void
  264. clear_interpreter_hooks (void)
  265. {
  266. init_ui_hook = 0;
  267. print_frame_info_listing_hook = 0;
  268. /*print_frame_more_info_hook = 0; */
  269. query_hook = 0;
  270. warning_hook = 0;
  271. create_breakpoint_hook = 0;
  272. delete_breakpoint_hook = 0;
  273. modify_breakpoint_hook = 0;
  274. interactive_hook = 0;
  275. registers_changed_hook = 0;
  276. readline_begin_hook = 0;
  277. readline_hook = 0;
  278. readline_end_hook = 0;
  279. register_changed_hook = 0;
  280. memory_changed_hook = 0;
  281. context_hook = 0;
  282. target_wait_hook = 0;
  283. call_command_hook = 0;
  284. error_hook = 0;
  285. error_begin_hook = 0;
  286. command_loop_hook = 0;
  287. clear_gdb_event_hooks ();
  288. }
  289. /* This is a lazy init routine, called the first time
  290. the interpreter module is used. I put it here just in case, but I haven't
  291. thought of a use for it yet. I will probably bag it soon, since I don't
  292. think it will be necessary. */
  293. static void
  294. initialize_interps (void)
  295. {
  296. interpreter_initialized = 1;
  297. /* Don't know if anything needs to be done here... */
  298. }
  299. static void
  300. interpreter_exec_cmd (char *args, int from_tty)
  301. {
  302. struct interp *old_interp, *interp_to_use;
  303. char **prules = NULL;
  304. char **trule = NULL;
  305. unsigned int nrules;
  306. unsigned int i;
  307. int old_quiet, use_quiet;
  308. prules = buildargv (args);
  309. if (prules == NULL)
  310. {
  311. error ("unable to parse arguments");
  312. }
  313. nrules = 0;
  314. if (prules != NULL)
  315. {
  316. for (trule = prules; *trule != NULL; trule++)
  317. {
  318. nrules++;
  319. }
  320. }
  321. if (nrules < 2)
  322. error ("usage: interpreter-exec <interpreter> [ <command> ... ]");
  323. old_interp = current_interpreter;
  324. interp_to_use = interp_lookup (prules[0]);
  325. if (interp_to_use == NULL)
  326. error ("Could not find interpreter \"%s\".", prules[0]);
  327. /* Temporarily set interpreters quiet */
  328. old_quiet = interp_set_quiet (old_interp, 1);
  329. use_quiet = interp_set_quiet (interp_to_use, 1);
  330. if (!interp_set (interp_to_use))
  331. error ("Could not switch to interpreter \"%s\".", prules[0]);
  332. for (i = 1; i < nrules; i++)
  333. {
  334. if (!interp_exec (interp_to_use, prules[i]))
  335. {
  336. interp_set (old_interp);
  337. interp_set_quiet (interp_to_use, old_quiet);
  338. error ("error in command: \"%s\".", prules[i]);
  339. break;
  340. }
  341. }
  342. interp_set (old_interp);
  343. interp_set_quiet (interp_to_use, use_quiet);
  344. interp_set_quiet (old_interp, old_quiet);
  345. }
  346. /* List the possible interpreters which could complete the given text. */
  347. static char **
  348. interpreter_completer (char *text, char *word)
  349. {
  350. int alloced = 0;
  351. int textlen;
  352. int num_matches;
  353. char **matches;
  354. struct interp *interp;
  355. /* We expect only a very limited number of interpreters, so just
  356. allocate room for all of them. */
  357. for (interp = interp_list; interp != NULL; interp = interp->next)
  358. ++alloced;
  359. matches = (char **) xmalloc (alloced * sizeof (char *));
  360. num_matches = 0;
  361. textlen = strlen (text);
  362. for (interp = interp_list; interp != NULL; interp = interp->next)
  363. {
  364. if (strncmp (interp->name, text, textlen) == 0)
  365. {
  366. matches[num_matches] =
  367. (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
  368. if (word == text)
  369. strcpy (matches[num_matches], interp->name);
  370. else if (word > text)
  371. {
  372. /* Return some portion of interp->name */
  373. strcpy (matches[num_matches], interp->name + (word - text));
  374. }
  375. else
  376. {
  377. /* Return some of text plus interp->name */
  378. strncpy (matches[num_matches], word, text - word);
  379. matches[num_matches][text - word] = '\0';
  380. strcat (matches[num_matches], interp->name);
  381. }
  382. ++num_matches;
  383. }
  384. }
  385. if (num_matches == 0)
  386. {
  387. xfree (matches);
  388. matches = NULL;
  389. }
  390. else if (num_matches < alloced)
  391. {
  392. matches = (char **) xrealloc ((char *) matches, ((num_matches + 1)
  393. * sizeof (char *)));
  394. matches[num_matches] = NULL;
  395. }
  396. return matches;
  397. }
  398. /* This just adds the "interpreter-exec" command. */
  399. void
  400. _initialize_interpreter (void)
  401. {
  402. struct cmd_list_element *c;
  403. c = add_cmd ("interpreter-exec", class_support,
  404. interpreter_exec_cmd,
  405. "Execute a command in an interpreter. It takes two arguments:\n\
  406. The first argument is the name of the interpreter to use.\n\
  407. The second argument is the command to execute.\n", &cmdlist);
  408. set_cmd_completer (c, interpreter_completer);
  409. }