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