GDB (API)
/home/stan/gdb/src/gdb/interps.c
Go to the documentation of this file.
00001 /* Manages interpreters for GDB, the GNU debugger.
00002 
00003    Copyright (C) 2000-2013 Free Software Foundation, Inc.
00004 
00005    Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 /* This is just a first cut at separating out the "interpreter"
00023    functions of gdb into self-contained modules.  There are a couple
00024    of open areas that need to be sorted out:
00025 
00026    1) The interpreter explicitly contains a UI_OUT, and can insert itself
00027    into the event loop, but it doesn't explicitly contain hooks for readline.
00028    I did this because it seems to me many interpreters won't want to use
00029    the readline command interface, and it is probably simpler to just let
00030    them take over the input in their resume proc.  */
00031 
00032 #include "defs.h"
00033 #include "gdbcmd.h"
00034 #include "ui-out.h"
00035 #include "event-loop.h"
00036 #include "event-top.h"
00037 #include "interps.h"
00038 #include "completer.h"
00039 #include "gdb_string.h"
00040 #include "gdb_assert.h"
00041 #include "top.h"                /* For command_loop.  */
00042 #include "exceptions.h"
00043 #include "continuations.h"
00044 
00045 /* True if the current interpreter in is async mode.  See interps.h
00046    for more details.  This starts out disabled, until all the explicit
00047    command line arguments (e.g., `gdb -ex "start" -ex "next"') are
00048    processed.  */
00049 int interpreter_async = 0;
00050 
00051 struct interp
00052 {
00053   /* This is the name in "-i=" and set interpreter.  */
00054   const char *name;
00055 
00056   /* Interpreters are stored in a linked list, this is the next
00057      one...  */
00058   struct interp *next;
00059 
00060   /* This is a cookie that an instance of the interpreter can use.
00061      This is a bit confused right now as the exact initialization
00062      sequence for it, and how it relates to the interpreter's uiout
00063      object is a bit confused.  */
00064   void *data;
00065 
00066   /* Has the init_proc been run?  */
00067   int inited;
00068 
00069   const struct interp_procs *procs;
00070   int quiet_p;
00071 };
00072 
00073 /* Functions local to this file.  */
00074 static void initialize_interps (void);
00075 
00076 /* The magic initialization routine for this module.  */
00077 
00078 void _initialize_interpreter (void);
00079 
00080 /* Variables local to this file: */
00081 
00082 static struct interp *interp_list = NULL;
00083 static struct interp *current_interpreter = NULL;
00084 static struct interp *top_level_interpreter_ptr = NULL;
00085 
00086 static int interpreter_initialized = 0;
00087 
00088 /* interp_new - This allocates space for a new interpreter,
00089    fills the fields from the inputs, and returns a pointer to the
00090    interpreter.  */
00091 struct interp *
00092 interp_new (const char *name, const struct interp_procs *procs)
00093 {
00094   struct interp *new_interp;
00095 
00096   new_interp = XMALLOC (struct interp);
00097 
00098   new_interp->name = xstrdup (name);
00099   new_interp->data = NULL;
00100   new_interp->quiet_p = 0;
00101   new_interp->procs = procs;
00102   new_interp->inited = 0;
00103 
00104   /* Check for required procs.  */
00105   gdb_assert (procs->command_loop_proc != NULL);
00106 
00107   return new_interp;
00108 }
00109 
00110 /* Add interpreter INTERP to the gdb interpreter list.  The
00111    interpreter must not have previously been added.  */
00112 void
00113 interp_add (struct interp *interp)
00114 {
00115   if (!interpreter_initialized)
00116     initialize_interps ();
00117 
00118   gdb_assert (interp_lookup (interp->name) == NULL);
00119 
00120   interp->next = interp_list;
00121   interp_list = interp;
00122 }
00123 
00124 /* This sets the current interpreter to be INTERP.  If INTERP has not
00125    been initialized, then this will also run the init proc.  If the
00126    init proc is successful, return 1, if it fails, set the old
00127    interpreter back in place and return 0.  If we can't restore the
00128    old interpreter, then raise an internal error, since we are in
00129    pretty bad shape at this point.
00130 
00131    The TOP_LEVEL parameter tells if this new interpreter is
00132    the top-level one.  The top-level is what is requested
00133    on the command line, and is responsible for reporting general
00134    notification about target state changes.  For example, if
00135    MI is the top-level interpreter, then it will always report
00136    events such as target stops and new thread creation, even if they
00137    are caused by CLI commands.  */
00138 int
00139 interp_set (struct interp *interp, int top_level)
00140 {
00141   struct interp *old_interp = current_interpreter;
00142   int first_time = 0;
00143   char buffer[64];
00144 
00145   /* If we already have an interpreter, then trying to
00146      set top level interpreter is kinda pointless.  */
00147   gdb_assert (!top_level || !current_interpreter);
00148   gdb_assert (!top_level || !top_level_interpreter_ptr);
00149 
00150   if (current_interpreter != NULL)
00151     {
00152       ui_out_flush (current_uiout);
00153       if (current_interpreter->procs->suspend_proc
00154           && !current_interpreter->procs->suspend_proc (current_interpreter->
00155                                                         data))
00156         {
00157           error (_("Could not suspend interpreter \"%s\"."),
00158                  current_interpreter->name);
00159         }
00160     }
00161   else
00162     {
00163       first_time = 1;
00164     }
00165 
00166   current_interpreter = interp;
00167   if (top_level)
00168     top_level_interpreter_ptr = interp;
00169 
00170   /* We use interpreter_p for the "set interpreter" variable, so we need
00171      to make sure we have a malloc'ed copy for the set command to free.  */
00172   if (interpreter_p != NULL
00173       && strcmp (current_interpreter->name, interpreter_p) != 0)
00174     {
00175       xfree (interpreter_p);
00176 
00177       interpreter_p = xstrdup (current_interpreter->name);
00178     }
00179 
00180   /* Run the init proc.  If it fails, try to restore the old interp.  */
00181 
00182   if (!interp->inited)
00183     {
00184       if (interp->procs->init_proc != NULL)
00185         {
00186           interp->data = interp->procs->init_proc (interp, top_level);
00187         }
00188       interp->inited = 1;
00189     }
00190 
00191   /* Do this only after the interpreter is initialized.  */
00192   current_uiout = interp->procs->ui_out_proc (interp);
00193 
00194   /* Clear out any installed interpreter hooks/event handlers.  */
00195   clear_interpreter_hooks ();
00196 
00197   if (interp->procs->resume_proc != NULL
00198       && (!interp->procs->resume_proc (interp->data)))
00199     {
00200       if (old_interp == NULL || !interp_set (old_interp, 0))
00201         internal_error (__FILE__, __LINE__,
00202                         _("Failed to initialize new interp \"%s\" %s"),
00203                         interp->name, "and could not restore old interp!\n");
00204       return 0;
00205     }
00206 
00207   /* Finally, put up the new prompt to show that we are indeed here. 
00208      Also, display_gdb_prompt for the console does some readline magic
00209      which is needed for the console interpreter, at least...  */
00210 
00211   if (!first_time)
00212     {
00213       if (!interp_quiet_p (interp))
00214         {
00215           xsnprintf (buffer, sizeof (buffer),
00216                      "Switching to interpreter \"%.24s\".\n", interp->name);
00217           ui_out_text (current_uiout, buffer);
00218         }
00219       display_gdb_prompt (NULL);
00220     }
00221 
00222   return 1;
00223 }
00224 
00225 /* interp_lookup - Looks up the interpreter for NAME.  If no such
00226    interpreter exists, return NULL, otherwise return a pointer to the
00227    interpreter.  */
00228 struct interp *
00229 interp_lookup (const char *name)
00230 {
00231   struct interp *interp;
00232 
00233   if (name == NULL || strlen (name) == 0)
00234     return NULL;
00235 
00236   for (interp = interp_list; interp != NULL; interp = interp->next)
00237     {
00238       if (strcmp (interp->name, name) == 0)
00239         return interp;
00240     }
00241 
00242   return NULL;
00243 }
00244 
00245 /* Returns the current interpreter.  */
00246 
00247 struct ui_out *
00248 interp_ui_out (struct interp *interp)
00249 {
00250   if (interp != NULL)
00251     return interp->procs->ui_out_proc (interp);
00252 
00253   return current_interpreter->procs->ui_out_proc (current_interpreter);
00254 }
00255 
00256 int
00257 current_interp_set_logging (int start_log, struct ui_file *out,
00258                             struct ui_file *logfile)
00259 {
00260   if (current_interpreter == NULL
00261       || current_interpreter->procs->set_logging_proc == NULL)
00262     return 0;
00263 
00264   return current_interpreter->procs->set_logging_proc (current_interpreter,
00265                                                        start_log, out,
00266                                                        logfile);
00267 }
00268 
00269 /* Temporarily overrides the current interpreter.  */
00270 struct interp *
00271 interp_set_temp (const char *name)
00272 {
00273   struct interp *interp = interp_lookup (name);
00274   struct interp *old_interp = current_interpreter;
00275 
00276   if (interp)
00277     current_interpreter = interp;
00278   return old_interp;
00279 }
00280 
00281 /* Returns the interpreter's cookie.  */
00282 
00283 void *
00284 interp_data (struct interp *interp)
00285 {
00286   return interp->data;
00287 }
00288 
00289 /* Returns the interpreter's name.  */
00290 
00291 const char *
00292 interp_name (struct interp *interp)
00293 {
00294   return interp->name;
00295 }
00296 
00297 /* Returns true if the current interp is the passed in name.  */
00298 int
00299 current_interp_named_p (const char *interp_name)
00300 {
00301   if (current_interpreter)
00302     return (strcmp (current_interpreter->name, interp_name) == 0);
00303 
00304   return 0;
00305 }
00306 
00307 /* This is called in display_gdb_prompt.  If the proc returns a zero
00308    value, display_gdb_prompt will return without displaying the
00309    prompt.  */
00310 int
00311 current_interp_display_prompt_p (void)
00312 {
00313   if (current_interpreter == NULL
00314       || current_interpreter->procs->prompt_proc_p == NULL)
00315     return 0;
00316   else
00317     return current_interpreter->procs->prompt_proc_p (current_interpreter->
00318                                                       data);
00319 }
00320 
00321 /* Run the current command interpreter's main loop.  */
00322 void
00323 current_interp_command_loop (void)
00324 {
00325   gdb_assert (current_interpreter != NULL);
00326 
00327   current_interpreter->procs->command_loop_proc (current_interpreter->data);
00328 }
00329 
00330 int
00331 interp_quiet_p (struct interp *interp)
00332 {
00333   if (interp != NULL)
00334     return interp->quiet_p;
00335   else
00336     return current_interpreter->quiet_p;
00337 }
00338 
00339 static int
00340 interp_set_quiet (struct interp *interp, int quiet)
00341 {
00342   int old_val = interp->quiet_p;
00343 
00344   interp->quiet_p = quiet;
00345   return old_val;
00346 }
00347 
00348 /* interp_exec - This executes COMMAND_STR in the current 
00349    interpreter.  */
00350 int
00351 interp_exec_p (struct interp *interp)
00352 {
00353   return interp->procs->exec_proc != NULL;
00354 }
00355 
00356 struct gdb_exception
00357 interp_exec (struct interp *interp, const char *command_str)
00358 {
00359   if (interp->procs->exec_proc != NULL)
00360     {
00361       return interp->procs->exec_proc (interp->data, command_str);
00362     }
00363   return exception_none;
00364 }
00365 
00366 /* A convenience routine that nulls out all the common command hooks.
00367    Use it when removing your interpreter in its suspend proc.  */
00368 void
00369 clear_interpreter_hooks (void)
00370 {
00371   deprecated_init_ui_hook = 0;
00372   deprecated_print_frame_info_listing_hook = 0;
00373   /*print_frame_more_info_hook = 0; */
00374   deprecated_query_hook = 0;
00375   deprecated_warning_hook = 0;
00376   deprecated_interactive_hook = 0;
00377   deprecated_readline_begin_hook = 0;
00378   deprecated_readline_hook = 0;
00379   deprecated_readline_end_hook = 0;
00380   deprecated_register_changed_hook = 0;
00381   deprecated_context_hook = 0;
00382   deprecated_target_wait_hook = 0;
00383   deprecated_call_command_hook = 0;
00384   deprecated_error_begin_hook = 0;
00385 }
00386 
00387 /* This is a lazy init routine, called the first time the interpreter
00388    module is used.  I put it here just in case, but I haven't thought
00389    of a use for it yet.  I will probably bag it soon, since I don't
00390    think it will be necessary.  */
00391 static void
00392 initialize_interps (void)
00393 {
00394   interpreter_initialized = 1;
00395   /* Don't know if anything needs to be done here...  */
00396 }
00397 
00398 static void
00399 interpreter_exec_cmd (char *args, int from_tty)
00400 {
00401   struct interp *old_interp, *interp_to_use;
00402   char **prules = NULL;
00403   char **trule = NULL;
00404   unsigned int nrules;
00405   unsigned int i;
00406   int old_quiet, use_quiet;
00407   struct cleanup *cleanup;
00408 
00409   if (args == NULL)
00410     error_no_arg (_("interpreter-exec command"));
00411 
00412   prules = gdb_buildargv (args);
00413   cleanup = make_cleanup_freeargv (prules);
00414 
00415   nrules = 0;
00416   for (trule = prules; *trule != NULL; trule++)
00417     nrules++;
00418 
00419   if (nrules < 2)
00420     error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));
00421 
00422   old_interp = current_interpreter;
00423 
00424   interp_to_use = interp_lookup (prules[0]);
00425   if (interp_to_use == NULL)
00426     error (_("Could not find interpreter \"%s\"."), prules[0]);
00427 
00428   /* Temporarily set interpreters quiet.  */
00429   old_quiet = interp_set_quiet (old_interp, 1);
00430   use_quiet = interp_set_quiet (interp_to_use, 1);
00431 
00432   if (!interp_set (interp_to_use, 0))
00433     error (_("Could not switch to interpreter \"%s\"."), prules[0]);
00434 
00435   for (i = 1; i < nrules; i++)
00436     {
00437       struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
00438 
00439       if (e.reason < 0)
00440         {
00441           interp_set (old_interp, 0);
00442           interp_set_quiet (interp_to_use, use_quiet);
00443           interp_set_quiet (old_interp, old_quiet);
00444           error (_("error in command: \"%s\"."), prules[i]);
00445         }
00446     }
00447 
00448   interp_set (old_interp, 0);
00449   interp_set_quiet (interp_to_use, use_quiet);
00450   interp_set_quiet (old_interp, old_quiet);
00451 
00452   do_cleanups (cleanup);
00453 }
00454 
00455 /* List the possible interpreters which could complete the given text.  */
00456 static VEC (char_ptr) *
00457 interpreter_completer (struct cmd_list_element *ignore,
00458                        const char *text, const char *word)
00459 {
00460   int textlen;
00461   VEC (char_ptr) *matches = NULL;
00462   struct interp *interp;
00463 
00464   textlen = strlen (text);
00465   for (interp = interp_list; interp != NULL; interp = interp->next)
00466     {
00467       if (strncmp (interp->name, text, textlen) == 0)
00468         {
00469           char *match;
00470 
00471           match = (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
00472           if (word == text)
00473             strcpy (match, interp->name);
00474           else if (word > text)
00475             {
00476               /* Return some portion of interp->name.  */
00477               strcpy (match, interp->name + (word - text));
00478             }
00479           else
00480             {
00481               /* Return some of text plus interp->name.  */
00482               strncpy (match, word, text - word);
00483               match[text - word] = '\0';
00484               strcat (match, interp->name);
00485             }
00486           VEC_safe_push (char_ptr, matches, match);
00487         }
00488     }
00489 
00490   return matches;
00491 }
00492 
00493 struct interp *
00494 top_level_interpreter (void)
00495 {
00496   return top_level_interpreter_ptr;  
00497 }
00498 
00499 void *
00500 top_level_interpreter_data (void)
00501 {
00502   gdb_assert (top_level_interpreter_ptr);
00503   return top_level_interpreter_ptr->data;  
00504 }
00505 
00506 /* This just adds the "interpreter-exec" command.  */
00507 void
00508 _initialize_interpreter (void)
00509 {
00510   struct cmd_list_element *c;
00511 
00512   c = add_cmd ("interpreter-exec", class_support,
00513                interpreter_exec_cmd, _("\
00514 Execute a command in an interpreter.  It takes two arguments:\n\
00515 The first argument is the name of the interpreter to use.\n\
00516 The second argument is the command to execute.\n"), &cmdlist);
00517   set_cmd_completer (c, interpreter_completer);
00518 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines