gdb/interps.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Manages interpreters for GDB, the GNU debugger.

  2.    Copyright (C) 2000-2015 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 "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 "continuations.h"

  32. /* True if the current interpreter in is async mode.  See interps.h
  33.    for more details.  This starts out disabled, until all the explicit
  34.    command line arguments (e.g., `gdb -ex "start" -ex "next"') are
  35.    processed.  */
  36. int interpreter_async = 0;

  37. struct interp
  38. {
  39.   /* This is the name in "-i=" and set interpreter.  */
  40.   const char *name;

  41.   /* Interpreters are stored in a linked list, this is the next
  42.      one...  */
  43.   struct interp *next;

  44.   /* This is a cookie that an instance of the interpreter can use.
  45.      This is a bit confused right now as the exact initialization
  46.      sequence for it, and how it relates to the interpreter's uiout
  47.      object is a bit confused.  */
  48.   void *data;

  49.   /* Has the init_proc been run?  */
  50.   int inited;

  51.   const struct interp_procs *procs;
  52.   int quiet_p;
  53. };

  54. /* The magic initialization routine for this module.  */

  55. void _initialize_interpreter (void);

  56. /* Variables local to this file: */

  57. static struct interp *interp_list = NULL;
  58. static struct interp *current_interpreter = NULL;
  59. static struct interp *top_level_interpreter_ptr = NULL;

  60. /* interp_new - This allocates space for a new interpreter,
  61.    fills the fields from the inputs, and returns a pointer to the
  62.    interpreter.  */
  63. struct interp *
  64. interp_new (const char *name, const struct interp_procs *procs)
  65. {
  66.   struct interp *new_interp;

  67.   new_interp = XNEW (struct interp);

  68.   new_interp->name = xstrdup (name);
  69.   new_interp->data = NULL;
  70.   new_interp->quiet_p = 0;
  71.   new_interp->procs = procs;
  72.   new_interp->inited = 0;

  73.   /* Check for required procs.  */
  74.   gdb_assert (procs->command_loop_proc != NULL);

  75.   return new_interp;
  76. }

  77. /* Add interpreter INTERP to the gdb interpreter list.  The
  78.    interpreter must not have previously been added.  */
  79. void
  80. interp_add (struct interp *interp)
  81. {
  82.   gdb_assert (interp_lookup (interp->name) == NULL);

  83.   interp->next = interp_list;
  84.   interp_list = interp;
  85. }

  86. /* This sets the current interpreter to be INTERP.  If INTERP has not
  87.    been initialized, then this will also run the init proc.  If the
  88.    init proc is successful, return 1, if it fails, set the old
  89.    interpreter back in place and return 0.  If we can't restore the
  90.    old interpreter, then raise an internal error, since we are in
  91.    pretty bad shape at this point.

  92.    The TOP_LEVEL parameter tells if this new interpreter is
  93.    the top-level one.  The top-level is what is requested
  94.    on the command line, and is responsible for reporting general
  95.    notification about target state changes.  For example, if
  96.    MI is the top-level interpreter, then it will always report
  97.    events such as target stops and new thread creation, even if they
  98.    are caused by CLI commands.  */
  99. int
  100. interp_set (struct interp *interp, int top_level)
  101. {
  102.   struct interp *old_interp = current_interpreter;
  103.   int first_time = 0;
  104.   char buffer[64];

  105.   /* If we already have an interpreter, then trying to
  106.      set top level interpreter is kinda pointless.  */
  107.   gdb_assert (!top_level || !current_interpreter);
  108.   gdb_assert (!top_level || !top_level_interpreter_ptr);

  109.   if (current_interpreter != NULL)
  110.     {
  111.       ui_out_flush (current_uiout);
  112.       if (current_interpreter->procs->suspend_proc
  113.           && !current_interpreter->procs->suspend_proc (current_interpreter->
  114.                                                         data))
  115.         {
  116.           error (_("Could not suspend interpreter \"%s\"."),
  117.                  current_interpreter->name);
  118.         }
  119.     }
  120.   else
  121.     {
  122.       first_time = 1;
  123.     }

  124.   current_interpreter = interp;
  125.   if (top_level)
  126.     top_level_interpreter_ptr = interp;

  127.   /* We use interpreter_p for the "set interpreter" variable, so we need
  128.      to make sure we have a malloc'ed copy for the set command to free.  */
  129.   if (interpreter_p != NULL
  130.       && strcmp (current_interpreter->name, interpreter_p) != 0)
  131.     {
  132.       xfree (interpreter_p);

  133.       interpreter_p = xstrdup (current_interpreter->name);
  134.     }

  135.   /* Run the init proc.  If it fails, try to restore the old interp.  */

  136.   if (!interp->inited)
  137.     {
  138.       if (interp->procs->init_proc != NULL)
  139.         {
  140.           interp->data = interp->procs->init_proc (interp, top_level);
  141.         }
  142.       interp->inited = 1;
  143.     }

  144.   /* Do this only after the interpreter is initialized.  */
  145.   current_uiout = interp->procs->ui_out_proc (interp);

  146.   /* Clear out any installed interpreter hooks/event handlers.  */
  147.   clear_interpreter_hooks ();

  148.   if (interp->procs->resume_proc != NULL
  149.       && (!interp->procs->resume_proc (interp->data)))
  150.     {
  151.       if (old_interp == NULL || !interp_set (old_interp, 0))
  152.         internal_error (__FILE__, __LINE__,
  153.                         _("Failed to initialize new interp \"%s\" %s"),
  154.                         interp->name, "and could not restore old interp!\n");
  155.       return 0;
  156.     }

  157.   if (!first_time && !interp_quiet_p (interp))
  158.     {
  159.       xsnprintf (buffer, sizeof (buffer),
  160.                  "Switching to interpreter \"%.24s\".\n", interp->name);
  161.       ui_out_text (current_uiout, buffer);
  162.     }

  163.   return 1;
  164. }

  165. /* interp_lookup - Looks up the interpreter for NAME.  If no such
  166.    interpreter exists, return NULL, otherwise return a pointer to the
  167.    interpreter.  */
  168. struct interp *
  169. interp_lookup (const char *name)
  170. {
  171.   struct interp *interp;

  172.   if (name == NULL || strlen (name) == 0)
  173.     return NULL;

  174.   for (interp = interp_list; interp != NULL; interp = interp->next)
  175.     {
  176.       if (strcmp (interp->name, name) == 0)
  177.         return interp;
  178.     }

  179.   return NULL;
  180. }

  181. /* Returns the current interpreter.  */

  182. struct ui_out *
  183. interp_ui_out (struct interp *interp)
  184. {
  185.   if (interp != NULL)
  186.     return interp->procs->ui_out_proc (interp);

  187.   return current_interpreter->procs->ui_out_proc (current_interpreter);
  188. }

  189. int
  190. current_interp_set_logging (int start_log, struct ui_file *out,
  191.                             struct ui_file *logfile)
  192. {
  193.   if (current_interpreter == NULL
  194.       || current_interpreter->procs->set_logging_proc == NULL)
  195.     return 0;

  196.   return current_interpreter->procs->set_logging_proc (current_interpreter,
  197.                                                        start_log, out,
  198.                                                        logfile);
  199. }

  200. /* Temporarily overrides the current interpreter.  */
  201. struct interp *
  202. interp_set_temp (const char *name)
  203. {
  204.   struct interp *interp = interp_lookup (name);
  205.   struct interp *old_interp = current_interpreter;

  206.   if (interp)
  207.     current_interpreter = interp;
  208.   return old_interp;
  209. }

  210. /* Returns the interpreter's cookie.  */

  211. void *
  212. interp_data (struct interp *interp)
  213. {
  214.   return interp->data;
  215. }

  216. /* Returns the interpreter's name.  */

  217. const char *
  218. interp_name (struct interp *interp)
  219. {
  220.   return interp->name;
  221. }

  222. /* Returns true if the current interp is the passed in name.  */
  223. int
  224. current_interp_named_p (const char *interp_name)
  225. {
  226.   if (current_interpreter)
  227.     return (strcmp (current_interpreter->name, interp_name) == 0);

  228.   return 0;
  229. }

  230. /* The interpreter that is active while `interp_exec' is active, NULL
  231.    at all other times.  */
  232. static struct interp *command_interpreter;

  233. /* The interpreter that was active when a command was executed.
  234.    Normally that'd always be CURRENT_INTERPRETER, except that MI's
  235.    -interpreter-exec command doesn't actually flip the current
  236.    interpreter when running its sub-command.  The
  237.    `command_interpreter' global tracks when interp_exec is called
  238.    (IOW, when -interpreter-exec is called).  If that is set, it is
  239.    INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
  240.    INTERP "CMD".  Otherwise, interp_exec isn't active, and so the
  241.    interpreter running the command is the current interpreter.  */

  242. struct interp *
  243. command_interp (void)
  244. {
  245.   if (command_interpreter != NULL)
  246.     return command_interpreter;
  247.   else
  248.     return current_interpreter;
  249. }

  250. /* Run the current command interpreter's main loop.  */
  251. void
  252. current_interp_command_loop (void)
  253. {
  254.   gdb_assert (current_interpreter != NULL);

  255.   current_interpreter->procs->command_loop_proc (current_interpreter->data);
  256. }

  257. int
  258. interp_quiet_p (struct interp *interp)
  259. {
  260.   if (interp != NULL)
  261.     return interp->quiet_p;
  262.   else
  263.     return current_interpreter->quiet_p;
  264. }

  265. static int
  266. interp_set_quiet (struct interp *interp, int quiet)
  267. {
  268.   int old_val = interp->quiet_p;

  269.   interp->quiet_p = quiet;
  270.   return old_val;
  271. }

  272. /* interp_exec - This executes COMMAND_STR in the current
  273.    interpreter.  */

  274. struct gdb_exception
  275. interp_exec (struct interp *interp, const char *command_str)
  276. {
  277.   struct gdb_exception ex;
  278.   struct interp *save_command_interp;

  279.   gdb_assert (interp->procs->exec_proc != NULL);

  280.   /* See `command_interp' for why we do this.  */
  281.   save_command_interp = command_interpreter;
  282.   command_interpreter = interp;

  283.   ex = interp->procs->exec_proc (interp->data, command_str);

  284.   command_interpreter = save_command_interp;

  285.   return ex;
  286. }

  287. /* A convenience routine that nulls out all the common command hooks.
  288.    Use it when removing your interpreter in its suspend proc.  */
  289. void
  290. clear_interpreter_hooks (void)
  291. {
  292.   deprecated_init_ui_hook = 0;
  293.   deprecated_print_frame_info_listing_hook = 0;
  294.   /*print_frame_more_info_hook = 0; */
  295.   deprecated_query_hook = 0;
  296.   deprecated_warning_hook = 0;
  297.   deprecated_interactive_hook = 0;
  298.   deprecated_readline_begin_hook = 0;
  299.   deprecated_readline_hook = 0;
  300.   deprecated_readline_end_hook = 0;
  301.   deprecated_register_changed_hook = 0;
  302.   deprecated_context_hook = 0;
  303.   deprecated_target_wait_hook = 0;
  304.   deprecated_call_command_hook = 0;
  305.   deprecated_error_begin_hook = 0;
  306. }

  307. static void
  308. interpreter_exec_cmd (char *args, int from_tty)
  309. {
  310.   struct interp *old_interp, *interp_to_use;
  311.   char **prules = NULL;
  312.   char **trule = NULL;
  313.   unsigned int nrules;
  314.   unsigned int i;
  315.   int old_quiet, use_quiet;
  316.   struct cleanup *cleanup;

  317.   if (args == NULL)
  318.     error_no_arg (_("interpreter-exec command"));

  319.   prules = gdb_buildargv (args);
  320.   cleanup = make_cleanup_freeargv (prules);

  321.   nrules = 0;
  322.   for (trule = prules; *trule != NULL; trule++)
  323.     nrules++;

  324.   if (nrules < 2)
  325.     error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));

  326.   old_interp = current_interpreter;

  327.   interp_to_use = interp_lookup (prules[0]);
  328.   if (interp_to_use == NULL)
  329.     error (_("Could not find interpreter \"%s\"."), prules[0]);

  330.   /* Temporarily set interpreters quiet.  */
  331.   old_quiet = interp_set_quiet (old_interp, 1);
  332.   use_quiet = interp_set_quiet (interp_to_use, 1);

  333.   if (!interp_set (interp_to_use, 0))
  334.     error (_("Could not switch to interpreter \"%s\"."), prules[0]);

  335.   for (i = 1; i < nrules; i++)
  336.     {
  337.       struct gdb_exception e = interp_exec (interp_to_use, prules[i]);

  338.       if (e.reason < 0)
  339.         {
  340.           interp_set (old_interp, 0);
  341.           interp_set_quiet (interp_to_use, use_quiet);
  342.           interp_set_quiet (old_interp, old_quiet);
  343.           error (_("error in command: \"%s\"."), prules[i]);
  344.         }
  345.     }

  346.   interp_set (old_interp, 0);
  347.   interp_set_quiet (interp_to_use, use_quiet);
  348.   interp_set_quiet (old_interp, old_quiet);

  349.   do_cleanups (cleanup);
  350. }

  351. /* List the possible interpreters which could complete the given text.  */
  352. static VEC (char_ptr) *
  353. interpreter_completer (struct cmd_list_element *ignore,
  354.                        const char *text, const char *word)
  355. {
  356.   int textlen;
  357.   VEC (char_ptr) *matches = NULL;
  358.   struct interp *interp;

  359.   textlen = strlen (text);
  360.   for (interp = interp_list; interp != NULL; interp = interp->next)
  361.     {
  362.       if (strncmp (interp->name, text, textlen) == 0)
  363.         {
  364.           char *match;

  365.           match = (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
  366.           if (word == text)
  367.             strcpy (match, interp->name);
  368.           else if (word > text)
  369.             {
  370.               /* Return some portion of interp->name.  */
  371.               strcpy (match, interp->name + (word - text));
  372.             }
  373.           else
  374.             {
  375.               /* Return some of text plus interp->name.  */
  376.               strncpy (match, word, text - word);
  377.               match[text - word] = '\0';
  378.               strcat (match, interp->name);
  379.             }
  380.           VEC_safe_push (char_ptr, matches, match);
  381.         }
  382.     }

  383.   return matches;
  384. }

  385. struct interp *
  386. top_level_interpreter (void)
  387. {
  388.   return top_level_interpreter_ptr;
  389. }

  390. void *
  391. top_level_interpreter_data (void)
  392. {
  393.   gdb_assert (top_level_interpreter_ptr);
  394.   return top_level_interpreter_ptr->data;
  395. }

  396. /* This just adds the "interpreter-exec" command.  */
  397. void
  398. _initialize_interpreter (void)
  399. {
  400.   struct cmd_list_element *c;

  401.   c = add_cmd ("interpreter-exec", class_support,
  402.                interpreter_exec_cmd, _("\
  403. Execute a command in an interpreter.  It takes two arguments:\n\
  404. The first argument is the name of the interpreter to use.\n\
  405. The second argument is the command to execute.\n"), &cmdlist);
  406.   set_cmd_completer (c, interpreter_completer);
  407. }