GDB (API)
/home/stan/gdb/src/gdb/python/py-cmd.c
Go to the documentation of this file.
00001 /* gdb commands implemented in Python
00002 
00003    Copyright (C) 2008-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 
00021 #include "defs.h"
00022 #include "arch-utils.h"
00023 #include "value.h"
00024 #include "exceptions.h"
00025 #include "python-internal.h"
00026 #include "charset.h"
00027 #include "gdbcmd.h"
00028 #include "cli/cli-decode.h"
00029 #include "completer.h"
00030 #include "language.h"
00031 
00032 /* Struct representing built-in completion types.  */
00033 struct cmdpy_completer
00034 {
00035   /* Python symbol name.  */
00036   char *name;
00037   /* Completion function.  */
00038   completer_ftype *completer;
00039 };
00040 
00041 static struct cmdpy_completer completers[] =
00042 {
00043   { "COMPLETE_NONE", noop_completer },
00044   { "COMPLETE_FILENAME", filename_completer },
00045   { "COMPLETE_LOCATION", location_completer },
00046   { "COMPLETE_COMMAND", command_completer },
00047   { "COMPLETE_SYMBOL", make_symbol_completion_list_fn },
00048 };
00049 
00050 #define N_COMPLETERS (sizeof (completers) / sizeof (completers[0]))
00051 
00052 /* A gdb command.  For the time being only ordinary commands (not
00053    set/show commands) are allowed.  */
00054 struct cmdpy_object
00055 {
00056   PyObject_HEAD
00057 
00058   /* The corresponding gdb command object, or NULL if the command is
00059      no longer installed.  */
00060   struct cmd_list_element *command;
00061 
00062   /* A prefix command requires storage for a list of its sub-commands.
00063      A pointer to this is passed to add_prefix_command, and to add_cmd
00064      for sub-commands of that prefix.  If this Command is not a prefix
00065      command, then this field is unused.  */
00066   struct cmd_list_element *sub_list;
00067 };
00068 
00069 typedef struct cmdpy_object cmdpy_object;
00070 
00071 static PyTypeObject cmdpy_object_type
00072     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("cmdpy_object");
00073 
00074 /* Constants used by this module.  */
00075 static PyObject *invoke_cst;
00076 static PyObject *complete_cst;
00077 
00078 
00079 
00080 /* Python function which wraps dont_repeat.  */
00081 static PyObject *
00082 cmdpy_dont_repeat (PyObject *self, PyObject *args)
00083 {
00084   dont_repeat ();
00085   Py_RETURN_NONE;
00086 }
00087 
00088 
00089 
00090 /* Called if the gdb cmd_list_element is destroyed.  */
00091 
00092 static void
00093 cmdpy_destroyer (struct cmd_list_element *self, void *context)
00094 {
00095   cmdpy_object *cmd;
00096   struct cleanup *cleanup;
00097 
00098   cleanup = ensure_python_env (get_current_arch (), current_language);
00099 
00100   /* Release our hold on the command object.  */
00101   cmd = (cmdpy_object *) context;
00102   cmd->command = NULL;
00103   Py_DECREF (cmd);
00104 
00105   /* We allocated the name, doc string, and perhaps the prefix
00106      name.  */
00107   xfree ((char *) self->name);
00108   xfree (self->doc);
00109   xfree (self->prefixname);
00110 
00111   do_cleanups (cleanup);
00112 }
00113 
00114 /* Called by gdb to invoke the command.  */
00115 
00116 static void
00117 cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
00118 {
00119   cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
00120   PyObject *argobj, *ttyobj, *result;
00121   struct cleanup *cleanup;
00122 
00123   cleanup = ensure_python_env (get_current_arch (), current_language);
00124 
00125   if (! obj)
00126     error (_("Invalid invocation of Python command object."));
00127   if (! PyObject_HasAttr ((PyObject *) obj, invoke_cst))
00128     {
00129       if (obj->command->prefixname)
00130         {
00131           /* A prefix command does not need an invoke method.  */
00132           do_cleanups (cleanup);
00133           return;
00134         }
00135       error (_("Python command object missing 'invoke' method."));
00136     }
00137 
00138   if (! args)
00139     args = "";
00140   argobj = PyUnicode_Decode (args, strlen (args), host_charset (), NULL);
00141   if (! argobj)
00142     {
00143       gdbpy_print_stack ();
00144       error (_("Could not convert arguments to Python string."));
00145     }
00146 
00147   ttyobj = from_tty ? Py_True : Py_False;
00148   Py_INCREF (ttyobj);
00149   result = PyObject_CallMethodObjArgs ((PyObject *) obj, invoke_cst, argobj,
00150                                        ttyobj, NULL);
00151   Py_DECREF (argobj);
00152   Py_DECREF (ttyobj);
00153 
00154   if (! result)
00155     {
00156       PyObject *ptype, *pvalue, *ptraceback;
00157       char *msg;
00158 
00159       PyErr_Fetch (&ptype, &pvalue, &ptraceback);
00160 
00161       /* Try to fetch an error message contained within ptype, pvalue.
00162          When fetching the error message we need to make our own copy,
00163          we no longer own ptype, pvalue after the call to PyErr_Restore.  */
00164 
00165       msg = gdbpy_exception_to_string (ptype, pvalue);
00166       make_cleanup (xfree, msg);
00167 
00168       if (msg == NULL)
00169         {
00170           /* An error occurred computing the string representation of the
00171              error message.  This is rare, but we should inform the user.  */
00172           printf_filtered (_("An error occurred in a Python command\n"
00173                              "and then another occurred computing the "
00174                              "error message.\n"));
00175           gdbpy_print_stack ();
00176         }
00177 
00178       /* Don't print the stack for gdb.GdbError exceptions.
00179          It is generally used to flag user errors.
00180 
00181          We also don't want to print "Error occurred in Python command"
00182          for user errors.  However, a missing message for gdb.GdbError
00183          exceptions is arguably a bug, so we flag it as such.  */
00184 
00185       if (! PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc)
00186           || msg == NULL || *msg == '\0')
00187         {
00188           PyErr_Restore (ptype, pvalue, ptraceback);
00189           gdbpy_print_stack ();
00190           if (msg != NULL && *msg != '\0')
00191             error (_("Error occurred in Python command: %s"), msg);
00192           else
00193             error (_("Error occurred in Python command."));
00194         }
00195       else
00196         {
00197           Py_XDECREF (ptype);
00198           Py_XDECREF (pvalue);
00199           Py_XDECREF (ptraceback);
00200           error ("%s", msg);
00201         }
00202     }
00203 
00204   Py_DECREF (result);
00205   do_cleanups (cleanup);
00206 }
00207 
00208 /* Called by gdb for command completion.  */
00209 
00210 static VEC (char_ptr) *
00211 cmdpy_completer (struct cmd_list_element *command,
00212                  const char *text, const char *word)
00213 {
00214   cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
00215   PyObject *textobj, *wordobj, *resultobj = NULL;
00216   VEC (char_ptr) *result = NULL;
00217   struct cleanup *cleanup;
00218 
00219   cleanup = ensure_python_env (get_current_arch (), current_language);
00220 
00221   if (! obj)
00222     error (_("Invalid invocation of Python command object."));
00223   if (! PyObject_HasAttr ((PyObject *) obj, complete_cst))
00224     {
00225       /* If there is no complete method, don't error -- instead, just
00226          say that there are no completions.  */
00227       goto done;
00228     }
00229 
00230   textobj = PyUnicode_Decode (text, strlen (text), host_charset (), NULL);
00231   if (! textobj)
00232     error (_("Could not convert argument to Python string."));
00233   wordobj = PyUnicode_Decode (word, strlen (word), host_charset (), NULL);
00234   if (! wordobj)
00235     error (_("Could not convert argument to Python string."));
00236 
00237   resultobj = PyObject_CallMethodObjArgs ((PyObject *) obj, complete_cst,
00238                                           textobj, wordobj, NULL);
00239   Py_DECREF (textobj);
00240   Py_DECREF (wordobj);
00241   if (! resultobj)
00242     {
00243       /* Just swallow errors here.  */
00244       PyErr_Clear ();
00245       goto done;
00246     }
00247 
00248   result = NULL;
00249   if (PyInt_Check (resultobj))
00250     {
00251       /* User code may also return one of the completion constants,
00252          thus requesting that sort of completion.  */
00253       long value;
00254 
00255       if (! gdb_py_int_as_long (resultobj, &value))
00256         {
00257           /* Ignore.  */
00258           PyErr_Clear ();
00259         }
00260       else if (value >= 0 && value < (long) N_COMPLETERS)
00261         result = completers[value].completer (command, text, word);
00262     }
00263   else
00264     {
00265       PyObject *iter = PyObject_GetIter (resultobj);
00266       PyObject *elt;
00267 
00268       if (iter == NULL)
00269         goto done;
00270 
00271       while ((elt = PyIter_Next (iter)) != NULL)
00272         {
00273           char *item;
00274 
00275           if (! gdbpy_is_string (elt))
00276             {
00277               /* Skip problem elements.  */
00278               Py_DECREF (elt);
00279               continue;
00280             }
00281           item = python_string_to_host_string (elt);
00282           Py_DECREF (elt);
00283           if (item == NULL)
00284             {
00285               /* Skip problem elements.  */
00286               PyErr_Clear ();
00287               continue;
00288             }
00289           VEC_safe_push (char_ptr, result, item);
00290         }
00291 
00292       Py_DECREF (iter);
00293 
00294       /* If we got some results, ignore problems.  Otherwise, report
00295          the problem.  */
00296       if (result != NULL && PyErr_Occurred ())
00297         PyErr_Clear ();
00298     }
00299 
00300  done:
00301 
00302   Py_XDECREF (resultobj);
00303   do_cleanups (cleanup);
00304 
00305   return result;
00306 }
00307 
00308 /* Helper for cmdpy_init which locates the command list to use and
00309    pulls out the command name.
00310    
00311    NAME is the command name list.  The final word in the list is the
00312    name of the new command.  All earlier words must be existing prefix
00313    commands.
00314 
00315    *BASE_LIST is set to the final prefix command's list of
00316    *sub-commands.
00317    
00318    START_LIST is the list in which the search starts.
00319 
00320    This function returns the xmalloc()d name of the new command.  On
00321    error sets the Python error and returns NULL.  */
00322 
00323 char *
00324 gdbpy_parse_command_name (const char *name,
00325                           struct cmd_list_element ***base_list,
00326                           struct cmd_list_element **start_list)
00327 {
00328   struct cmd_list_element *elt;
00329   int len = strlen (name);
00330   int i, lastchar;
00331   char *prefix_text;
00332   const char *prefix_text2;
00333   char *result;
00334 
00335   /* Skip trailing whitespace.  */
00336   for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
00337     ;
00338   if (i < 0)
00339     {
00340       PyErr_SetString (PyExc_RuntimeError, _("No command name found."));
00341       return NULL;
00342     }
00343   lastchar = i;
00344 
00345   /* Find first character of the final word.  */
00346   for (; i > 0 && (isalnum (name[i - 1])
00347                    || name[i - 1] == '-'
00348                    || name[i - 1] == '_');
00349        --i)
00350     ;
00351   result = xmalloc (lastchar - i + 2);
00352   memcpy (result, &name[i], lastchar - i + 1);
00353   result[lastchar - i + 1] = '\0';
00354 
00355   /* Skip whitespace again.  */
00356   for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
00357     ;
00358   if (i < 0)
00359     {
00360       *base_list = start_list;
00361       return result;
00362     }
00363 
00364   prefix_text = xmalloc (i + 2);
00365   memcpy (prefix_text, name, i + 1);
00366   prefix_text[i + 1] = '\0';
00367 
00368   prefix_text2 = prefix_text;
00369   elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, 1);
00370   if (!elt || elt == (struct cmd_list_element *) -1)
00371     {
00372       PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."),
00373                     prefix_text);
00374       xfree (prefix_text);
00375       xfree (result);
00376       return NULL;
00377     }
00378 
00379   if (elt->prefixlist)
00380     {
00381       xfree (prefix_text);
00382       *base_list = elt->prefixlist;
00383       return result;
00384     }
00385 
00386   PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."),
00387                 prefix_text);
00388   xfree (prefix_text);
00389   xfree (result);
00390   return NULL;
00391 }
00392 
00393 /* Object initializer; sets up gdb-side structures for command.
00394 
00395    Use: __init__(NAME, COMMAND_CLASS [, COMPLETER_CLASS][, PREFIX]]).
00396 
00397    NAME is the name of the command.  It may consist of multiple words,
00398    in which case the final word is the name of the new command, and
00399    earlier words must be prefix commands.
00400 
00401    COMMAND_CLASS is the kind of command.  It should be one of the COMMAND_*
00402    constants defined in the gdb module.
00403 
00404    COMPLETER_CLASS is the kind of completer.  If not given, the
00405    "complete" method will be used.  Otherwise, it should be one of the
00406    COMPLETE_* constants defined in the gdb module.
00407 
00408    If PREFIX is True, then this command is a prefix command.
00409 
00410    The documentation for the command is taken from the doc string for
00411    the python class.  */
00412 
00413 static int
00414 cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
00415 {
00416   cmdpy_object *obj = (cmdpy_object *) self;
00417   const char *name;
00418   int cmdtype;
00419   int completetype = -1;
00420   char *docstring = NULL;
00421   volatile struct gdb_exception except;
00422   struct cmd_list_element **cmd_list;
00423   char *cmd_name, *pfx_name;
00424   static char *keywords[] = { "name", "command_class", "completer_class",
00425                               "prefix", NULL };
00426   PyObject *is_prefix = NULL;
00427   int cmp;
00428 
00429   if (obj->command)
00430     {
00431       /* Note: this is apparently not documented in Python.  We return
00432          0 for success, -1 for failure.  */
00433       PyErr_Format (PyExc_RuntimeError,
00434                     _("Command object already initialized."));
00435       return -1;
00436     }
00437 
00438   if (! PyArg_ParseTupleAndKeywords (args, kw, "si|iO",
00439                                      keywords, &name, &cmdtype,
00440                           &completetype, &is_prefix))
00441     return -1;
00442 
00443   if (cmdtype != no_class && cmdtype != class_run
00444       && cmdtype != class_vars && cmdtype != class_stack
00445       && cmdtype != class_files && cmdtype != class_support
00446       && cmdtype != class_info && cmdtype != class_breakpoint
00447       && cmdtype != class_trace && cmdtype != class_obscure
00448       && cmdtype != class_maintenance && cmdtype != class_user)
00449     {
00450       PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
00451       return -1;
00452     }
00453 
00454   if (completetype < -1 || completetype >= (int) N_COMPLETERS)
00455     {
00456       PyErr_Format (PyExc_RuntimeError,
00457                     _("Invalid completion type argument."));
00458       return -1;
00459     }
00460 
00461   cmd_name = gdbpy_parse_command_name (name, &cmd_list, &cmdlist);
00462   if (! cmd_name)
00463     return -1;
00464 
00465   pfx_name = NULL;
00466   if (is_prefix != NULL) 
00467     {
00468       cmp = PyObject_IsTrue (is_prefix);
00469       if (cmp == 1)
00470         {
00471           int i, out;
00472           
00473           /* Make a normalized form of the command name.  */
00474           pfx_name = xmalloc (strlen (name) + 2);
00475           
00476           i = 0;
00477           out = 0;
00478           while (name[i])
00479             {
00480               /* Skip whitespace.  */
00481               while (name[i] == ' ' || name[i] == '\t')
00482                 ++i;
00483               /* Copy non-whitespace characters.  */
00484               while (name[i] && name[i] != ' ' && name[i] != '\t')
00485                 pfx_name[out++] = name[i++];
00486               /* Add a single space after each word -- including the final
00487                  word.  */
00488               pfx_name[out++] = ' ';
00489             }
00490           pfx_name[out] = '\0';
00491         }
00492       else if (cmp < 0)
00493         {
00494           xfree (cmd_name);
00495           return -1;
00496         }
00497     }
00498   if (PyObject_HasAttr (self, gdbpy_doc_cst))
00499     {
00500       PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst);
00501 
00502       if (ds_obj && gdbpy_is_string (ds_obj))
00503         {
00504           docstring = python_string_to_host_string (ds_obj);
00505           if (docstring == NULL)
00506             {
00507               xfree (cmd_name);
00508               xfree (pfx_name);
00509               Py_DECREF (ds_obj);
00510               return -1;
00511             }
00512         }
00513 
00514       Py_XDECREF (ds_obj);
00515     }
00516   if (! docstring)
00517     docstring = xstrdup (_("This command is not documented."));
00518 
00519   Py_INCREF (self);
00520 
00521   TRY_CATCH (except, RETURN_MASK_ALL)
00522     {
00523       struct cmd_list_element *cmd;
00524 
00525       if (pfx_name)
00526         {
00527           int allow_unknown;
00528 
00529           /* If we have our own "invoke" method, then allow unknown
00530              sub-commands.  */
00531           allow_unknown = PyObject_HasAttr (self, invoke_cst);
00532           cmd = add_prefix_cmd (cmd_name, (enum command_class) cmdtype,
00533                                 NULL, docstring, &obj->sub_list,
00534                                 pfx_name, allow_unknown, cmd_list);
00535         }
00536       else
00537         cmd = add_cmd (cmd_name, (enum command_class) cmdtype, NULL,
00538                        docstring, cmd_list);
00539 
00540       /* There appears to be no API to set this.  */
00541       cmd->func = cmdpy_function;
00542       cmd->destroyer = cmdpy_destroyer;
00543 
00544       obj->command = cmd;
00545       set_cmd_context (cmd, self);
00546       set_cmd_completer (cmd, ((completetype == -1) ? cmdpy_completer
00547                                : completers[completetype].completer));
00548     }
00549   if (except.reason < 0)
00550     {
00551       xfree (cmd_name);
00552       xfree (docstring);
00553       xfree (pfx_name);
00554       Py_DECREF (self);
00555       PyErr_Format (except.reason == RETURN_QUIT
00556                     ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
00557                     "%s", except.message);
00558       return -1;
00559     }
00560   return 0;
00561 }
00562 
00563 
00564 
00565 /* Initialize the 'commands' code.  */
00566 
00567 int
00568 gdbpy_initialize_commands (void)
00569 {
00570   int i;
00571 
00572   cmdpy_object_type.tp_new = PyType_GenericNew;
00573   if (PyType_Ready (&cmdpy_object_type) < 0)
00574     return -1;
00575 
00576   /* Note: alias and user are special; pseudo appears to be unused,
00577      and there is no reason to expose tui or xdb, I think.  */
00578   if (PyModule_AddIntConstant (gdb_module, "COMMAND_NONE", no_class) < 0
00579       || PyModule_AddIntConstant (gdb_module, "COMMAND_RUNNING", class_run) < 0
00580       || PyModule_AddIntConstant (gdb_module, "COMMAND_DATA", class_vars) < 0
00581       || PyModule_AddIntConstant (gdb_module, "COMMAND_STACK", class_stack) < 0
00582       || PyModule_AddIntConstant (gdb_module, "COMMAND_FILES", class_files) < 0
00583       || PyModule_AddIntConstant (gdb_module, "COMMAND_SUPPORT",
00584                                   class_support) < 0
00585       || PyModule_AddIntConstant (gdb_module, "COMMAND_STATUS", class_info) < 0
00586       || PyModule_AddIntConstant (gdb_module, "COMMAND_BREAKPOINTS",
00587                                   class_breakpoint) < 0
00588       || PyModule_AddIntConstant (gdb_module, "COMMAND_TRACEPOINTS",
00589                                   class_trace) < 0
00590       || PyModule_AddIntConstant (gdb_module, "COMMAND_OBSCURE",
00591                                   class_obscure) < 0
00592       || PyModule_AddIntConstant (gdb_module, "COMMAND_MAINTENANCE",
00593                                   class_maintenance) < 0
00594       || PyModule_AddIntConstant (gdb_module, "COMMAND_USER", class_user) < 0)
00595     return -1;
00596 
00597   for (i = 0; i < N_COMPLETERS; ++i)
00598     {
00599       if (PyModule_AddIntConstant (gdb_module, completers[i].name, i) < 0)
00600         return -1;
00601     }
00602 
00603   if (gdb_pymodule_addobject (gdb_module, "Command",
00604                               (PyObject *) &cmdpy_object_type) < 0)
00605     return -1;
00606 
00607   invoke_cst = PyString_FromString ("invoke");
00608   if (invoke_cst == NULL)
00609     return -1;
00610   complete_cst = PyString_FromString ("complete");
00611   if (complete_cst == NULL)
00612     return -1;
00613 
00614   return 0;
00615 }
00616 
00617 
00618 
00619 static PyMethodDef cmdpy_object_methods[] =
00620 {
00621   { "dont_repeat", cmdpy_dont_repeat, METH_NOARGS,
00622     "Prevent command repetition when user enters empty line." },
00623 
00624   { 0 }
00625 };
00626 
00627 static PyTypeObject cmdpy_object_type =
00628 {
00629   PyVarObject_HEAD_INIT (NULL, 0)
00630   "gdb.Command",                  /*tp_name*/
00631   sizeof (cmdpy_object),          /*tp_basicsize*/
00632   0,                              /*tp_itemsize*/
00633   0,                              /*tp_dealloc*/
00634   0,                              /*tp_print*/
00635   0,                              /*tp_getattr*/
00636   0,                              /*tp_setattr*/
00637   0,                              /*tp_compare*/
00638   0,                              /*tp_repr*/
00639   0,                              /*tp_as_number*/
00640   0,                              /*tp_as_sequence*/
00641   0,                              /*tp_as_mapping*/
00642   0,                              /*tp_hash */
00643   0,                              /*tp_call*/
00644   0,                              /*tp_str*/
00645   0,                              /*tp_getattro*/
00646   0,                              /*tp_setattro*/
00647   0,                              /*tp_as_buffer*/
00648   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
00649   "GDB command object",           /* tp_doc */
00650   0,                              /* tp_traverse */
00651   0,                              /* tp_clear */
00652   0,                              /* tp_richcompare */
00653   0,                              /* tp_weaklistoffset */
00654   0,                              /* tp_iter */
00655   0,                              /* tp_iternext */
00656   cmdpy_object_methods,           /* tp_methods */
00657   0,                              /* tp_members */
00658   0,                              /* tp_getset */
00659   0,                              /* tp_base */
00660   0,                              /* tp_dict */
00661   0,                              /* tp_descr_get */
00662   0,                              /* tp_descr_set */
00663   0,                              /* tp_dictoffset */
00664   cmdpy_init,                     /* tp_init */
00665   0,                              /* tp_alloc */
00666 };
00667 
00668 
00669 
00670 /* Utility to build a buildargv-like result from ARGS.
00671    This intentionally parses arguments the way libiberty/argv.c:buildargv
00672    does.  It splits up arguments in a reasonable way, and we want a standard
00673    way of parsing arguments.  Several gdb commands use buildargv to parse their
00674    arguments.  Plus we want to be able to write compatible python
00675    implementations of gdb commands.  */
00676 
00677 PyObject *
00678 gdbpy_string_to_argv (PyObject *self, PyObject *args)
00679 {
00680   PyObject *py_argv;
00681   const char *input;
00682 
00683   if (!PyArg_ParseTuple (args, "s", &input))
00684     return NULL;
00685 
00686   py_argv = PyList_New (0);
00687   if (py_argv == NULL)
00688     return NULL;
00689 
00690   /* buildargv uses NULL to represent an empty argument list, but we can't use
00691      that in Python.  Instead, if ARGS is "" then return an empty list.
00692      This undoes the NULL -> "" conversion that cmdpy_function does.  */
00693 
00694   if (*input != '\0')
00695     {
00696       char **c_argv = gdb_buildargv (input);
00697       int i;
00698 
00699       for (i = 0; c_argv[i] != NULL; ++i)
00700         {
00701           PyObject *argp = PyString_FromString (c_argv[i]);
00702 
00703           if (argp == NULL
00704               || PyList_Append (py_argv, argp) < 0)
00705             {
00706               Py_XDECREF (argp);
00707               Py_DECREF (py_argv);
00708               freeargv (c_argv);
00709               return NULL;
00710             }
00711           Py_DECREF (argp);
00712         }
00713 
00714       freeargv (c_argv);
00715     }
00716 
00717   return py_argv;
00718 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines