Home | History | Annotate | Line # | Download | only in python
py-cmd.c revision 1.3
      1  1.1  christos /* gdb commands implemented in Python
      2  1.1  christos 
      3  1.3  christos    Copyright (C) 2008-2015 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos 
     21  1.1  christos #include "defs.h"
     22  1.1  christos #include "arch-utils.h"
     23  1.1  christos #include "value.h"
     24  1.1  christos #include "python-internal.h"
     25  1.1  christos #include "charset.h"
     26  1.1  christos #include "gdbcmd.h"
     27  1.1  christos #include "cli/cli-decode.h"
     28  1.1  christos #include "completer.h"
     29  1.1  christos #include "language.h"
     30  1.1  christos 
     31  1.1  christos /* Struct representing built-in completion types.  */
     32  1.1  christos struct cmdpy_completer
     33  1.1  christos {
     34  1.3  christos   /* Python symbol name.
     35  1.3  christos      This isn't a const char * for Python 2.4's sake.
     36  1.3  christos      PyModule_AddIntConstant only takes a char *, sigh.  */
     37  1.1  christos   char *name;
     38  1.1  christos   /* Completion function.  */
     39  1.1  christos   completer_ftype *completer;
     40  1.1  christos };
     41  1.1  christos 
     42  1.3  christos static const struct cmdpy_completer completers[] =
     43  1.1  christos {
     44  1.1  christos   { "COMPLETE_NONE", noop_completer },
     45  1.1  christos   { "COMPLETE_FILENAME", filename_completer },
     46  1.1  christos   { "COMPLETE_LOCATION", location_completer },
     47  1.1  christos   { "COMPLETE_COMMAND", command_completer },
     48  1.1  christos   { "COMPLETE_SYMBOL", make_symbol_completion_list_fn },
     49  1.1  christos   { "COMPLETE_EXPRESSION", expression_completer },
     50  1.1  christos };
     51  1.1  christos 
     52  1.1  christos #define N_COMPLETERS (sizeof (completers) / sizeof (completers[0]))
     53  1.1  christos 
     54  1.1  christos /* A gdb command.  For the time being only ordinary commands (not
     55  1.1  christos    set/show commands) are allowed.  */
     56  1.1  christos struct cmdpy_object
     57  1.1  christos {
     58  1.1  christos   PyObject_HEAD
     59  1.1  christos 
     60  1.1  christos   /* The corresponding gdb command object, or NULL if the command is
     61  1.1  christos      no longer installed.  */
     62  1.1  christos   struct cmd_list_element *command;
     63  1.1  christos 
     64  1.1  christos   /* A prefix command requires storage for a list of its sub-commands.
     65  1.1  christos      A pointer to this is passed to add_prefix_command, and to add_cmd
     66  1.1  christos      for sub-commands of that prefix.  If this Command is not a prefix
     67  1.1  christos      command, then this field is unused.  */
     68  1.1  christos   struct cmd_list_element *sub_list;
     69  1.1  christos };
     70  1.1  christos 
     71  1.1  christos typedef struct cmdpy_object cmdpy_object;
     72  1.1  christos 
     73  1.1  christos static PyTypeObject cmdpy_object_type
     74  1.1  christos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("cmdpy_object");
     75  1.1  christos 
     76  1.1  christos /* Constants used by this module.  */
     77  1.1  christos static PyObject *invoke_cst;
     78  1.1  christos static PyObject *complete_cst;
     79  1.1  christos 
     80  1.1  christos 
     81  1.1  christos 
     83  1.1  christos /* Python function which wraps dont_repeat.  */
     84  1.1  christos static PyObject *
     85  1.1  christos cmdpy_dont_repeat (PyObject *self, PyObject *args)
     86  1.1  christos {
     87  1.1  christos   dont_repeat ();
     88  1.1  christos   Py_RETURN_NONE;
     89  1.1  christos }
     90  1.1  christos 
     91  1.1  christos 
     92  1.1  christos 
     94  1.1  christos /* Called if the gdb cmd_list_element is destroyed.  */
     95  1.1  christos 
     96  1.1  christos static void
     97  1.1  christos cmdpy_destroyer (struct cmd_list_element *self, void *context)
     98  1.1  christos {
     99  1.1  christos   cmdpy_object *cmd;
    100  1.1  christos   struct cleanup *cleanup;
    101  1.1  christos 
    102  1.1  christos   cleanup = ensure_python_env (get_current_arch (), current_language);
    103  1.1  christos 
    104  1.1  christos   /* Release our hold on the command object.  */
    105  1.1  christos   cmd = (cmdpy_object *) context;
    106  1.1  christos   cmd->command = NULL;
    107  1.1  christos   Py_DECREF (cmd);
    108  1.1  christos 
    109  1.1  christos   /* We allocated the name, doc string, and perhaps the prefix
    110  1.3  christos      name.  */
    111  1.3  christos   xfree ((char *) self->name);
    112  1.1  christos   xfree ((char *) self->doc);
    113  1.1  christos   xfree ((char *) self->prefixname);
    114  1.1  christos 
    115  1.1  christos   do_cleanups (cleanup);
    116  1.1  christos }
    117  1.1  christos 
    118  1.1  christos /* Called by gdb to invoke the command.  */
    119  1.1  christos 
    120  1.1  christos static void
    121  1.1  christos cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
    122  1.1  christos {
    123  1.1  christos   cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
    124  1.1  christos   PyObject *argobj, *ttyobj, *result;
    125  1.1  christos   struct cleanup *cleanup;
    126  1.1  christos 
    127  1.1  christos   cleanup = ensure_python_env (get_current_arch (), current_language);
    128  1.1  christos 
    129  1.1  christos   if (! obj)
    130  1.1  christos     error (_("Invalid invocation of Python command object."));
    131  1.1  christos   if (! PyObject_HasAttr ((PyObject *) obj, invoke_cst))
    132  1.1  christos     {
    133  1.1  christos       if (obj->command->prefixname)
    134  1.1  christos 	{
    135  1.1  christos 	  /* A prefix command does not need an invoke method.  */
    136  1.1  christos 	  do_cleanups (cleanup);
    137  1.1  christos 	  return;
    138  1.1  christos 	}
    139  1.1  christos       error (_("Python command object missing 'invoke' method."));
    140  1.1  christos     }
    141  1.1  christos 
    142  1.1  christos   if (! args)
    143  1.1  christos     args = "";
    144  1.1  christos   argobj = PyUnicode_Decode (args, strlen (args), host_charset (), NULL);
    145  1.1  christos   if (! argobj)
    146  1.1  christos     {
    147  1.1  christos       gdbpy_print_stack ();
    148  1.1  christos       error (_("Could not convert arguments to Python string."));
    149  1.1  christos     }
    150  1.1  christos 
    151  1.1  christos   ttyobj = from_tty ? Py_True : Py_False;
    152  1.1  christos   Py_INCREF (ttyobj);
    153  1.1  christos   result = PyObject_CallMethodObjArgs ((PyObject *) obj, invoke_cst, argobj,
    154  1.1  christos 				       ttyobj, NULL);
    155  1.1  christos   Py_DECREF (argobj);
    156  1.1  christos   Py_DECREF (ttyobj);
    157  1.1  christos 
    158  1.1  christos   if (! result)
    159  1.1  christos     {
    160  1.1  christos       PyObject *ptype, *pvalue, *ptraceback;
    161  1.1  christos       char *msg;
    162  1.1  christos 
    163  1.1  christos       PyErr_Fetch (&ptype, &pvalue, &ptraceback);
    164  1.1  christos 
    165  1.1  christos       /* Try to fetch an error message contained within ptype, pvalue.
    166  1.1  christos 	 When fetching the error message we need to make our own copy,
    167  1.1  christos 	 we no longer own ptype, pvalue after the call to PyErr_Restore.  */
    168  1.1  christos 
    169  1.1  christos       msg = gdbpy_exception_to_string (ptype, pvalue);
    170  1.1  christos       make_cleanup (xfree, msg);
    171  1.1  christos 
    172  1.1  christos       if (msg == NULL)
    173  1.1  christos 	{
    174  1.1  christos 	  /* An error occurred computing the string representation of the
    175  1.1  christos 	     error message.  This is rare, but we should inform the user.  */
    176  1.1  christos 	  printf_filtered (_("An error occurred in a Python command\n"
    177  1.1  christos 			     "and then another occurred computing the "
    178  1.1  christos 			     "error message.\n"));
    179  1.1  christos 	  gdbpy_print_stack ();
    180  1.1  christos 	}
    181  1.1  christos 
    182  1.1  christos       /* Don't print the stack for gdb.GdbError exceptions.
    183  1.1  christos 	 It is generally used to flag user errors.
    184  1.1  christos 
    185  1.1  christos 	 We also don't want to print "Error occurred in Python command"
    186  1.1  christos 	 for user errors.  However, a missing message for gdb.GdbError
    187  1.1  christos 	 exceptions is arguably a bug, so we flag it as such.  */
    188  1.1  christos 
    189  1.1  christos       if (! PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc)
    190  1.1  christos 	  || msg == NULL || *msg == '\0')
    191  1.1  christos 	{
    192  1.1  christos 	  PyErr_Restore (ptype, pvalue, ptraceback);
    193  1.1  christos 	  gdbpy_print_stack ();
    194  1.1  christos 	  if (msg != NULL && *msg != '\0')
    195  1.1  christos 	    error (_("Error occurred in Python command: %s"), msg);
    196  1.1  christos 	  else
    197  1.1  christos 	    error (_("Error occurred in Python command."));
    198  1.1  christos 	}
    199  1.1  christos       else
    200  1.1  christos 	{
    201  1.1  christos 	  Py_XDECREF (ptype);
    202  1.1  christos 	  Py_XDECREF (pvalue);
    203  1.1  christos 	  Py_XDECREF (ptraceback);
    204  1.1  christos 	  error ("%s", msg);
    205  1.1  christos 	}
    206  1.1  christos     }
    207  1.1  christos 
    208  1.1  christos   Py_DECREF (result);
    209  1.1  christos   do_cleanups (cleanup);
    210  1.3  christos }
    211  1.3  christos 
    212  1.3  christos /* Helper function for the Python command completers (both "pure"
    213  1.3  christos    completer and brkchar handler).  This function takes COMMAND, TEXT
    214  1.3  christos    and WORD and tries to call the Python method for completion with
    215  1.3  christos    these arguments.  It also takes HANDLE_BRKCHARS_P, an argument to
    216  1.3  christos    identify whether it is being called from the brkchar handler or
    217  1.3  christos    from the "pure" completer.  In the first case, it effectively calls
    218  1.3  christos    the Python method for completion, and records the PyObject in a
    219  1.3  christos    static variable (used as a "cache").  In the second case, it just
    220  1.3  christos    returns that variable, without actually calling the Python method
    221  1.3  christos    again.  This saves us one Python method call.
    222  1.3  christos 
    223  1.3  christos    The reason for this two step dance is that we need to know the set
    224  1.3  christos    of "brkchars" to use early on, before we actually try to perform
    225  1.3  christos    the completion.  But if a Python command supplies a "complete"
    226  1.3  christos    method then we have to call that method first: it may return as its
    227  1.3  christos    result the kind of completion to perform and that will in turn
    228  1.3  christos    specify which brkchars to use.  IOW, we need the result of the
    229  1.3  christos    "complete" method before we actually perform the completion.
    230  1.3  christos 
    231  1.3  christos    It is important to mention that this function is built on the
    232  1.3  christos    assumption that the calls to cmdpy_completer_handle_brkchars and
    233  1.3  christos    cmdpy_completer will be subsequent with nothing intervening.  This
    234  1.3  christos    is true for our completer mechanism.
    235  1.3  christos 
    236  1.3  christos    This function returns the PyObject representing the Python method
    237  1.3  christos    call.  */
    238  1.3  christos 
    239  1.3  christos static PyObject *
    240  1.3  christos cmdpy_completer_helper (struct cmd_list_element *command,
    241  1.3  christos 			const char *text, const char *word,
    242  1.3  christos 			int handle_brkchars_p)
    243  1.3  christos {
    244  1.3  christos   cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
    245  1.3  christos   PyObject *textobj, *wordobj;
    246  1.3  christos   /* This static variable will server as a "cache" for us, in order to
    247  1.3  christos      store the PyObject that results from calling the Python
    248  1.3  christos      function.  */
    249  1.3  christos   static PyObject *resultobj = NULL;
    250  1.3  christos 
    251  1.3  christos   if (handle_brkchars_p)
    252  1.3  christos     {
    253  1.3  christos       /* If we were called to handle brkchars, it means this is the
    254  1.3  christos 	 first function call of two that will happen in a row.
    255  1.3  christos 	 Therefore, we need to call the completer ourselves, and cache
    256  1.3  christos 	 the return value in the static variable RESULTOBJ.  Then, in
    257  1.3  christos 	 the second call, we can just use the value of RESULTOBJ to do
    258  1.3  christos 	 our job.  */
    259  1.3  christos       if (resultobj != NULL)
    260  1.3  christos 	Py_DECREF (resultobj);
    261  1.3  christos 
    262  1.3  christos       resultobj = NULL;
    263  1.3  christos       if (obj == NULL)
    264  1.3  christos 	error (_("Invalid invocation of Python command object."));
    265  1.3  christos       if (!PyObject_HasAttr ((PyObject *) obj, complete_cst))
    266  1.3  christos 	{
    267  1.3  christos 	  /* If there is no complete method, don't error.  */
    268  1.3  christos 	  return NULL;
    269  1.3  christos 	}
    270  1.3  christos 
    271  1.3  christos       textobj = PyUnicode_Decode (text, strlen (text), host_charset (), NULL);
    272  1.3  christos       if (textobj == NULL)
    273  1.3  christos 	error (_("Could not convert argument to Python string."));
    274  1.3  christos       wordobj = PyUnicode_Decode (word, sizeof (word), host_charset (), NULL);
    275  1.3  christos       if (wordobj == NULL)
    276  1.3  christos 	{
    277  1.3  christos 	  Py_DECREF (textobj);
    278  1.3  christos 	  error (_("Could not convert argument to Python string."));
    279  1.3  christos 	}
    280  1.3  christos 
    281  1.3  christos       resultobj = PyObject_CallMethodObjArgs ((PyObject *) obj, complete_cst,
    282  1.3  christos 					      textobj, wordobj, NULL);
    283  1.3  christos       Py_DECREF (textobj);
    284  1.3  christos       Py_DECREF (wordobj);
    285  1.3  christos       if (!resultobj)
    286  1.3  christos 	{
    287  1.3  christos 	  /* Just swallow errors here.  */
    288  1.3  christos 	  PyErr_Clear ();
    289  1.3  christos 	}
    290  1.3  christos 
    291  1.3  christos       Py_XINCREF (resultobj);
    292  1.3  christos     }
    293  1.3  christos 
    294  1.3  christos   return resultobj;
    295  1.3  christos }
    296  1.3  christos 
    297  1.3  christos /* Python function called to determine the break characters of a
    298  1.3  christos    certain completer.  We are only interested in knowing if the
    299  1.3  christos    completer registered by the user will return one of the integer
    300  1.3  christos    codes (see COMPLETER_* symbols).  */
    301  1.3  christos 
    302  1.3  christos static void
    303  1.3  christos cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
    304  1.3  christos 				 const char *text, const char *word)
    305  1.3  christos {
    306  1.3  christos   PyObject *resultobj = NULL;
    307  1.3  christos   struct cleanup *cleanup;
    308  1.3  christos 
    309  1.3  christos   cleanup = ensure_python_env (get_current_arch (), current_language);
    310  1.3  christos 
    311  1.3  christos   /* Calling our helper to obtain the PyObject of the Python
    312  1.3  christos      function.  */
    313  1.3  christos   resultobj = cmdpy_completer_helper (command, text, word, 1);
    314  1.3  christos 
    315  1.3  christos   /* Check if there was an error.  */
    316  1.3  christos   if (resultobj == NULL)
    317  1.3  christos     goto done;
    318  1.3  christos 
    319  1.3  christos   if (PyInt_Check (resultobj))
    320  1.3  christos     {
    321  1.3  christos       /* User code may also return one of the completion constants,
    322  1.3  christos 	 thus requesting that sort of completion.  We are only
    323  1.3  christos 	 interested in this kind of return.  */
    324  1.3  christos       long value;
    325  1.3  christos 
    326  1.3  christos       if (!gdb_py_int_as_long (resultobj, &value))
    327  1.3  christos 	{
    328  1.3  christos 	  /* Ignore.  */
    329  1.3  christos 	  PyErr_Clear ();
    330  1.3  christos 	}
    331  1.3  christos       else if (value >= 0 && value < (long) N_COMPLETERS)
    332  1.3  christos 	{
    333  1.3  christos 	  /* This is the core of this function.  Depending on which
    334  1.3  christos 	     completer type the Python function returns, we have to
    335  1.3  christos 	     adjust the break characters accordingly.  */
    336  1.3  christos 	  set_gdb_completion_word_break_characters
    337  1.3  christos 	    (completers[value].completer);
    338  1.3  christos 	}
    339  1.3  christos     }
    340  1.3  christos 
    341  1.3  christos  done:
    342  1.3  christos 
    343  1.3  christos   /* We do not call Py_XDECREF here because RESULTOBJ will be used in
    344  1.3  christos      the subsequent call to cmdpy_completer function.  */
    345  1.3  christos   do_cleanups (cleanup);
    346  1.1  christos }
    347  1.1  christos 
    348  1.1  christos /* Called by gdb for command completion.  */
    349  1.1  christos 
    350  1.1  christos static VEC (char_ptr) *
    351  1.1  christos cmdpy_completer (struct cmd_list_element *command,
    352  1.3  christos 		 const char *text, const char *word)
    353  1.1  christos {
    354  1.1  christos   PyObject *resultobj = NULL;
    355  1.1  christos   VEC (char_ptr) *result = NULL;
    356  1.1  christos   struct cleanup *cleanup;
    357  1.1  christos 
    358  1.3  christos   cleanup = ensure_python_env (get_current_arch (), current_language);
    359  1.3  christos 
    360  1.3  christos   /* Calling our helper to obtain the PyObject of the Python
    361  1.3  christos      function.  */
    362  1.3  christos   resultobj = cmdpy_completer_helper (command, text, word, 0);
    363  1.3  christos 
    364  1.3  christos   /* If the result object of calling the Python function is NULL, it
    365  1.3  christos      means that there was an error.  In this case, just give up and
    366  1.3  christos      return NULL.  */
    367  1.1  christos   if (resultobj == NULL)
    368  1.1  christos     goto done;
    369  1.1  christos 
    370  1.1  christos   result = NULL;
    371  1.1  christos   if (PyInt_Check (resultobj))
    372  1.1  christos     {
    373  1.1  christos       /* User code may also return one of the completion constants,
    374  1.1  christos 	 thus requesting that sort of completion.  */
    375  1.1  christos       long value;
    376  1.1  christos 
    377  1.1  christos       if (! gdb_py_int_as_long (resultobj, &value))
    378  1.1  christos 	{
    379  1.1  christos 	  /* Ignore.  */
    380  1.1  christos 	  PyErr_Clear ();
    381  1.1  christos 	}
    382  1.1  christos       else if (value >= 0 && value < (long) N_COMPLETERS)
    383  1.1  christos 	result = completers[value].completer (command, text, word);
    384  1.1  christos     }
    385  1.1  christos   else
    386  1.1  christos     {
    387  1.1  christos       PyObject *iter = PyObject_GetIter (resultobj);
    388  1.1  christos       PyObject *elt;
    389  1.1  christos 
    390  1.1  christos       if (iter == NULL)
    391  1.1  christos 	goto done;
    392  1.1  christos 
    393  1.1  christos       while ((elt = PyIter_Next (iter)) != NULL)
    394  1.1  christos 	{
    395  1.1  christos 	  char *item;
    396  1.1  christos 
    397  1.1  christos 	  if (! gdbpy_is_string (elt))
    398  1.1  christos 	    {
    399  1.1  christos 	      /* Skip problem elements.  */
    400  1.1  christos 	      Py_DECREF (elt);
    401  1.1  christos 	      continue;
    402  1.1  christos 	    }
    403  1.1  christos 	  item = python_string_to_host_string (elt);
    404  1.1  christos 	  Py_DECREF (elt);
    405  1.1  christos 	  if (item == NULL)
    406  1.1  christos 	    {
    407  1.1  christos 	      /* Skip problem elements.  */
    408  1.1  christos 	      PyErr_Clear ();
    409  1.1  christos 	      continue;
    410  1.1  christos 	    }
    411  1.1  christos 	  VEC_safe_push (char_ptr, result, item);
    412  1.1  christos 	}
    413  1.1  christos 
    414  1.1  christos       Py_DECREF (iter);
    415  1.1  christos 
    416  1.1  christos       /* If we got some results, ignore problems.  Otherwise, report
    417  1.1  christos 	 the problem.  */
    418  1.1  christos       if (result != NULL && PyErr_Occurred ())
    419  1.1  christos 	PyErr_Clear ();
    420  1.1  christos     }
    421  1.1  christos 
    422  1.1  christos  done:
    423  1.1  christos 
    424  1.1  christos   do_cleanups (cleanup);
    425  1.1  christos 
    426  1.1  christos   return result;
    427  1.1  christos }
    428  1.1  christos 
    429  1.1  christos /* Helper for cmdpy_init which locates the command list to use and
    430  1.1  christos    pulls out the command name.
    431  1.1  christos 
    432  1.1  christos    NAME is the command name list.  The final word in the list is the
    433  1.1  christos    name of the new command.  All earlier words must be existing prefix
    434  1.1  christos    commands.
    435  1.1  christos 
    436  1.1  christos    *BASE_LIST is set to the final prefix command's list of
    437  1.1  christos    *sub-commands.
    438  1.1  christos 
    439  1.1  christos    START_LIST is the list in which the search starts.
    440  1.1  christos 
    441  1.1  christos    This function returns the xmalloc()d name of the new command.  On
    442  1.1  christos    error sets the Python error and returns NULL.  */
    443  1.1  christos 
    444  1.1  christos char *
    445  1.1  christos gdbpy_parse_command_name (const char *name,
    446  1.1  christos 			  struct cmd_list_element ***base_list,
    447  1.1  christos 			  struct cmd_list_element **start_list)
    448  1.1  christos {
    449  1.1  christos   struct cmd_list_element *elt;
    450  1.1  christos   int len = strlen (name);
    451  1.1  christos   int i, lastchar;
    452  1.1  christos   char *prefix_text;
    453  1.1  christos   const char *prefix_text2;
    454  1.1  christos   char *result;
    455  1.1  christos 
    456  1.1  christos   /* Skip trailing whitespace.  */
    457  1.1  christos   for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
    458  1.1  christos     ;
    459  1.1  christos   if (i < 0)
    460  1.1  christos     {
    461  1.1  christos       PyErr_SetString (PyExc_RuntimeError, _("No command name found."));
    462  1.1  christos       return NULL;
    463  1.1  christos     }
    464  1.1  christos   lastchar = i;
    465  1.1  christos 
    466  1.1  christos   /* Find first character of the final word.  */
    467  1.1  christos   for (; i > 0 && (isalnum (name[i - 1])
    468  1.1  christos 		   || name[i - 1] == '-'
    469  1.1  christos 		   || name[i - 1] == '_');
    470  1.1  christos        --i)
    471  1.1  christos     ;
    472  1.1  christos   result = xmalloc (lastchar - i + 2);
    473  1.1  christos   memcpy (result, &name[i], lastchar - i + 1);
    474  1.1  christos   result[lastchar - i + 1] = '\0';
    475  1.1  christos 
    476  1.1  christos   /* Skip whitespace again.  */
    477  1.1  christos   for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
    478  1.1  christos     ;
    479  1.1  christos   if (i < 0)
    480  1.1  christos     {
    481  1.1  christos       *base_list = start_list;
    482  1.1  christos       return result;
    483  1.1  christos     }
    484  1.1  christos 
    485  1.1  christos   prefix_text = xmalloc (i + 2);
    486  1.1  christos   memcpy (prefix_text, name, i + 1);
    487  1.1  christos   prefix_text[i + 1] = '\0';
    488  1.1  christos 
    489  1.3  christos   prefix_text2 = prefix_text;
    490  1.1  christos   elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, 1);
    491  1.1  christos   if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
    492  1.1  christos     {
    493  1.1  christos       PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."),
    494  1.1  christos 		    prefix_text);
    495  1.1  christos       xfree (prefix_text);
    496  1.1  christos       xfree (result);
    497  1.1  christos       return NULL;
    498  1.1  christos     }
    499  1.1  christos 
    500  1.1  christos   if (elt->prefixlist)
    501  1.1  christos     {
    502  1.1  christos       xfree (prefix_text);
    503  1.1  christos       *base_list = elt->prefixlist;
    504  1.1  christos       return result;
    505  1.1  christos     }
    506  1.1  christos 
    507  1.1  christos   PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."),
    508  1.1  christos 		prefix_text);
    509  1.1  christos   xfree (prefix_text);
    510  1.1  christos   xfree (result);
    511  1.1  christos   return NULL;
    512  1.1  christos }
    513  1.1  christos 
    514  1.1  christos /* Object initializer; sets up gdb-side structures for command.
    515  1.1  christos 
    516  1.1  christos    Use: __init__(NAME, COMMAND_CLASS [, COMPLETER_CLASS][, PREFIX]]).
    517  1.1  christos 
    518  1.1  christos    NAME is the name of the command.  It may consist of multiple words,
    519  1.1  christos    in which case the final word is the name of the new command, and
    520  1.1  christos    earlier words must be prefix commands.
    521  1.1  christos 
    522  1.1  christos    COMMAND_CLASS is the kind of command.  It should be one of the COMMAND_*
    523  1.1  christos    constants defined in the gdb module.
    524  1.1  christos 
    525  1.1  christos    COMPLETER_CLASS is the kind of completer.  If not given, the
    526  1.1  christos    "complete" method will be used.  Otherwise, it should be one of the
    527  1.1  christos    COMPLETE_* constants defined in the gdb module.
    528  1.1  christos 
    529  1.1  christos    If PREFIX is True, then this command is a prefix command.
    530  1.1  christos 
    531  1.1  christos    The documentation for the command is taken from the doc string for
    532  1.1  christos    the python class.  */
    533  1.1  christos 
    534  1.1  christos static int
    535  1.1  christos cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
    536  1.1  christos {
    537  1.1  christos   cmdpy_object *obj = (cmdpy_object *) self;
    538  1.1  christos   const char *name;
    539  1.1  christos   int cmdtype;
    540  1.1  christos   int completetype = -1;
    541  1.1  christos   char *docstring = NULL;
    542  1.1  christos   volatile struct gdb_exception except;
    543  1.1  christos   struct cmd_list_element **cmd_list;
    544  1.1  christos   char *cmd_name, *pfx_name;
    545  1.1  christos   static char *keywords[] = { "name", "command_class", "completer_class",
    546  1.1  christos 			      "prefix", NULL };
    547  1.1  christos   PyObject *is_prefix = NULL;
    548  1.1  christos   int cmp;
    549  1.1  christos 
    550  1.1  christos   if (obj->command)
    551  1.1  christos     {
    552  1.1  christos       /* Note: this is apparently not documented in Python.  We return
    553  1.1  christos 	 0 for success, -1 for failure.  */
    554  1.1  christos       PyErr_Format (PyExc_RuntimeError,
    555  1.1  christos 		    _("Command object already initialized."));
    556  1.1  christos       return -1;
    557  1.1  christos     }
    558  1.1  christos 
    559  1.1  christos   if (! PyArg_ParseTupleAndKeywords (args, kw, "si|iO",
    560  1.1  christos 				     keywords, &name, &cmdtype,
    561  1.1  christos 			  &completetype, &is_prefix))
    562  1.1  christos     return -1;
    563  1.1  christos 
    564  1.1  christos   if (cmdtype != no_class && cmdtype != class_run
    565  1.1  christos       && cmdtype != class_vars && cmdtype != class_stack
    566  1.1  christos       && cmdtype != class_files && cmdtype != class_support
    567  1.1  christos       && cmdtype != class_info && cmdtype != class_breakpoint
    568  1.1  christos       && cmdtype != class_trace && cmdtype != class_obscure
    569  1.1  christos       && cmdtype != class_maintenance && cmdtype != class_user)
    570  1.1  christos     {
    571  1.1  christos       PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
    572  1.1  christos       return -1;
    573  1.1  christos     }
    574  1.1  christos 
    575  1.1  christos   if (completetype < -1 || completetype >= (int) N_COMPLETERS)
    576  1.1  christos     {
    577  1.1  christos       PyErr_Format (PyExc_RuntimeError,
    578  1.1  christos 		    _("Invalid completion type argument."));
    579  1.1  christos       return -1;
    580  1.1  christos     }
    581  1.1  christos 
    582  1.1  christos   cmd_name = gdbpy_parse_command_name (name, &cmd_list, &cmdlist);
    583  1.1  christos   if (! cmd_name)
    584  1.1  christos     return -1;
    585  1.1  christos 
    586  1.1  christos   pfx_name = NULL;
    587  1.1  christos   if (is_prefix != NULL)
    588  1.1  christos     {
    589  1.1  christos       cmp = PyObject_IsTrue (is_prefix);
    590  1.1  christos       if (cmp == 1)
    591  1.1  christos 	{
    592  1.1  christos 	  int i, out;
    593  1.1  christos 
    594  1.1  christos 	  /* Make a normalized form of the command name.  */
    595  1.1  christos 	  pfx_name = xmalloc (strlen (name) + 2);
    596  1.1  christos 
    597  1.1  christos 	  i = 0;
    598  1.1  christos 	  out = 0;
    599  1.1  christos 	  while (name[i])
    600  1.1  christos 	    {
    601  1.1  christos 	      /* Skip whitespace.  */
    602  1.1  christos 	      while (name[i] == ' ' || name[i] == '\t')
    603  1.1  christos 		++i;
    604  1.1  christos 	      /* Copy non-whitespace characters.  */
    605  1.1  christos 	      while (name[i] && name[i] != ' ' && name[i] != '\t')
    606  1.1  christos 		pfx_name[out++] = name[i++];
    607  1.1  christos 	      /* Add a single space after each word -- including the final
    608  1.1  christos 		 word.  */
    609  1.1  christos 	      pfx_name[out++] = ' ';
    610  1.1  christos 	    }
    611  1.1  christos 	  pfx_name[out] = '\0';
    612  1.1  christos 	}
    613  1.1  christos       else if (cmp < 0)
    614  1.1  christos 	{
    615  1.1  christos 	  xfree (cmd_name);
    616  1.1  christos 	  return -1;
    617  1.1  christos 	}
    618  1.1  christos     }
    619  1.1  christos   if (PyObject_HasAttr (self, gdbpy_doc_cst))
    620  1.1  christos     {
    621  1.1  christos       PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst);
    622  1.1  christos 
    623  1.1  christos       if (ds_obj && gdbpy_is_string (ds_obj))
    624  1.1  christos 	{
    625  1.1  christos 	  docstring = python_string_to_host_string (ds_obj);
    626  1.1  christos 	  if (docstring == NULL)
    627  1.1  christos 	    {
    628  1.1  christos 	      xfree (cmd_name);
    629  1.1  christos 	      xfree (pfx_name);
    630  1.1  christos 	      Py_DECREF (ds_obj);
    631  1.1  christos 	      return -1;
    632  1.1  christos 	    }
    633  1.1  christos 	}
    634  1.1  christos 
    635  1.1  christos       Py_XDECREF (ds_obj);
    636  1.1  christos     }
    637  1.1  christos   if (! docstring)
    638  1.1  christos     docstring = xstrdup (_("This command is not documented."));
    639  1.1  christos 
    640  1.1  christos   Py_INCREF (self);
    641  1.1  christos 
    642  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    643  1.1  christos     {
    644  1.1  christos       struct cmd_list_element *cmd;
    645  1.1  christos 
    646  1.1  christos       if (pfx_name)
    647  1.1  christos 	{
    648  1.1  christos 	  int allow_unknown;
    649  1.1  christos 
    650  1.1  christos 	  /* If we have our own "invoke" method, then allow unknown
    651  1.1  christos 	     sub-commands.  */
    652  1.1  christos 	  allow_unknown = PyObject_HasAttr (self, invoke_cst);
    653  1.1  christos 	  cmd = add_prefix_cmd (cmd_name, (enum command_class) cmdtype,
    654  1.1  christos 				NULL, docstring, &obj->sub_list,
    655  1.1  christos 				pfx_name, allow_unknown, cmd_list);
    656  1.1  christos 	}
    657  1.1  christos       else
    658  1.1  christos 	cmd = add_cmd (cmd_name, (enum command_class) cmdtype, NULL,
    659  1.1  christos 		       docstring, cmd_list);
    660  1.1  christos 
    661  1.1  christos       /* There appears to be no API to set this.  */
    662  1.1  christos       cmd->func = cmdpy_function;
    663  1.1  christos       cmd->destroyer = cmdpy_destroyer;
    664  1.1  christos 
    665  1.1  christos       obj->command = cmd;
    666  1.1  christos       set_cmd_context (cmd, self);
    667  1.3  christos       set_cmd_completer (cmd, ((completetype == -1) ? cmdpy_completer
    668  1.3  christos 			       : completers[completetype].completer));
    669  1.3  christos       if (completetype == -1)
    670  1.1  christos 	set_cmd_completer_handle_brkchars (cmd,
    671  1.1  christos 					   cmdpy_completer_handle_brkchars);
    672  1.1  christos     }
    673  1.1  christos   if (except.reason < 0)
    674  1.1  christos     {
    675  1.1  christos       xfree (cmd_name);
    676  1.1  christos       xfree (docstring);
    677  1.1  christos       xfree (pfx_name);
    678  1.1  christos       Py_DECREF (self);
    679  1.1  christos       PyErr_Format (except.reason == RETURN_QUIT
    680  1.1  christos 		    ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
    681  1.1  christos 		    "%s", except.message);
    682  1.1  christos       return -1;
    683  1.1  christos     }
    684  1.1  christos   return 0;
    685  1.1  christos }
    686  1.1  christos 
    687  1.1  christos 
    688  1.1  christos 
    690  1.1  christos /* Initialize the 'commands' code.  */
    691  1.1  christos 
    692  1.1  christos int
    693  1.1  christos gdbpy_initialize_commands (void)
    694  1.1  christos {
    695  1.1  christos   int i;
    696  1.1  christos 
    697  1.1  christos   cmdpy_object_type.tp_new = PyType_GenericNew;
    698  1.1  christos   if (PyType_Ready (&cmdpy_object_type) < 0)
    699  1.1  christos     return -1;
    700  1.1  christos 
    701  1.1  christos   /* Note: alias and user are special; pseudo appears to be unused,
    702  1.1  christos      and there is no reason to expose tui or xdb, I think.  */
    703  1.1  christos   if (PyModule_AddIntConstant (gdb_module, "COMMAND_NONE", no_class) < 0
    704  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_RUNNING", class_run) < 0
    705  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_DATA", class_vars) < 0
    706  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_STACK", class_stack) < 0
    707  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_FILES", class_files) < 0
    708  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_SUPPORT",
    709  1.1  christos 				  class_support) < 0
    710  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_STATUS", class_info) < 0
    711  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_BREAKPOINTS",
    712  1.1  christos 				  class_breakpoint) < 0
    713  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_TRACEPOINTS",
    714  1.1  christos 				  class_trace) < 0
    715  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_OBSCURE",
    716  1.1  christos 				  class_obscure) < 0
    717  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_MAINTENANCE",
    718  1.1  christos 				  class_maintenance) < 0
    719  1.1  christos       || PyModule_AddIntConstant (gdb_module, "COMMAND_USER", class_user) < 0)
    720  1.1  christos     return -1;
    721  1.1  christos 
    722  1.1  christos   for (i = 0; i < N_COMPLETERS; ++i)
    723  1.1  christos     {
    724  1.1  christos       if (PyModule_AddIntConstant (gdb_module, completers[i].name, i) < 0)
    725  1.1  christos 	return -1;
    726  1.1  christos     }
    727  1.1  christos 
    728  1.1  christos   if (gdb_pymodule_addobject (gdb_module, "Command",
    729  1.1  christos 			      (PyObject *) &cmdpy_object_type) < 0)
    730  1.1  christos     return -1;
    731  1.1  christos 
    732  1.1  christos   invoke_cst = PyString_FromString ("invoke");
    733  1.1  christos   if (invoke_cst == NULL)
    734  1.1  christos     return -1;
    735  1.1  christos   complete_cst = PyString_FromString ("complete");
    736  1.1  christos   if (complete_cst == NULL)
    737  1.1  christos     return -1;
    738  1.1  christos 
    739  1.1  christos   return 0;
    740  1.1  christos }
    741  1.1  christos 
    742  1.1  christos 
    743  1.1  christos 
    745  1.1  christos static PyMethodDef cmdpy_object_methods[] =
    746  1.1  christos {
    747  1.1  christos   { "dont_repeat", cmdpy_dont_repeat, METH_NOARGS,
    748  1.1  christos     "Prevent command repetition when user enters empty line." },
    749  1.1  christos 
    750  1.1  christos   { 0 }
    751  1.1  christos };
    752  1.1  christos 
    753  1.1  christos static PyTypeObject cmdpy_object_type =
    754  1.1  christos {
    755  1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
    756  1.1  christos   "gdb.Command",		  /*tp_name*/
    757  1.1  christos   sizeof (cmdpy_object),	  /*tp_basicsize*/
    758  1.1  christos   0,				  /*tp_itemsize*/
    759  1.1  christos   0,				  /*tp_dealloc*/
    760  1.1  christos   0,				  /*tp_print*/
    761  1.1  christos   0,				  /*tp_getattr*/
    762  1.1  christos   0,				  /*tp_setattr*/
    763  1.1  christos   0,				  /*tp_compare*/
    764  1.1  christos   0,				  /*tp_repr*/
    765  1.1  christos   0,				  /*tp_as_number*/
    766  1.1  christos   0,				  /*tp_as_sequence*/
    767  1.1  christos   0,				  /*tp_as_mapping*/
    768  1.1  christos   0,				  /*tp_hash */
    769  1.1  christos   0,				  /*tp_call*/
    770  1.1  christos   0,				  /*tp_str*/
    771  1.1  christos   0,				  /*tp_getattro*/
    772  1.1  christos   0,				  /*tp_setattro*/
    773  1.1  christos   0,				  /*tp_as_buffer*/
    774  1.1  christos   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    775  1.1  christos   "GDB command object",		  /* tp_doc */
    776  1.1  christos   0,				  /* tp_traverse */
    777  1.1  christos   0,				  /* tp_clear */
    778  1.1  christos   0,				  /* tp_richcompare */
    779  1.1  christos   0,				  /* tp_weaklistoffset */
    780  1.1  christos   0,				  /* tp_iter */
    781  1.1  christos   0,				  /* tp_iternext */
    782  1.1  christos   cmdpy_object_methods,		  /* tp_methods */
    783  1.1  christos   0,				  /* tp_members */
    784  1.1  christos   0,				  /* tp_getset */
    785  1.1  christos   0,				  /* tp_base */
    786  1.1  christos   0,				  /* tp_dict */
    787  1.1  christos   0,				  /* tp_descr_get */
    788  1.1  christos   0,				  /* tp_descr_set */
    789  1.1  christos   0,				  /* tp_dictoffset */
    790  1.1  christos   cmdpy_init,			  /* tp_init */
    791  1.1  christos   0,				  /* tp_alloc */
    792  1.1  christos };
    793  1.1  christos 
    794  1.1  christos 
    795  1.1  christos 
    797  1.1  christos /* Utility to build a buildargv-like result from ARGS.
    798  1.1  christos    This intentionally parses arguments the way libiberty/argv.c:buildargv
    799  1.1  christos    does.  It splits up arguments in a reasonable way, and we want a standard
    800  1.1  christos    way of parsing arguments.  Several gdb commands use buildargv to parse their
    801  1.1  christos    arguments.  Plus we want to be able to write compatible python
    802  1.1  christos    implementations of gdb commands.  */
    803  1.1  christos 
    804  1.1  christos PyObject *
    805  1.1  christos gdbpy_string_to_argv (PyObject *self, PyObject *args)
    806  1.1  christos {
    807  1.1  christos   PyObject *py_argv;
    808  1.1  christos   const char *input;
    809  1.1  christos 
    810  1.1  christos   if (!PyArg_ParseTuple (args, "s", &input))
    811  1.1  christos     return NULL;
    812  1.1  christos 
    813  1.1  christos   py_argv = PyList_New (0);
    814  1.1  christos   if (py_argv == NULL)
    815  1.1  christos     return NULL;
    816  1.1  christos 
    817  1.1  christos   /* buildargv uses NULL to represent an empty argument list, but we can't use
    818  1.1  christos      that in Python.  Instead, if ARGS is "" then return an empty list.
    819  1.1  christos      This undoes the NULL -> "" conversion that cmdpy_function does.  */
    820  1.1  christos 
    821  1.1  christos   if (*input != '\0')
    822  1.1  christos     {
    823  1.1  christos       char **c_argv = gdb_buildargv (input);
    824  1.1  christos       int i;
    825  1.1  christos 
    826  1.1  christos       for (i = 0; c_argv[i] != NULL; ++i)
    827  1.1  christos 	{
    828  1.1  christos 	  PyObject *argp = PyString_FromString (c_argv[i]);
    829  1.1  christos 
    830  1.1  christos 	  if (argp == NULL
    831  1.1  christos 	      || PyList_Append (py_argv, argp) < 0)
    832  1.1  christos 	    {
    833  1.1  christos 	      Py_XDECREF (argp);
    834  1.1  christos 	      Py_DECREF (py_argv);
    835  1.1  christos 	      freeargv (c_argv);
    836  1.1  christos 	      return NULL;
    837  1.1  christos 	    }
    838  1.1  christos 	  Py_DECREF (argp);
    839  1.1  christos 	}
    840  1.1  christos 
    841                      freeargv (c_argv);
    842                    }
    843                
    844                  return py_argv;
    845                }
    846