py-cmd.c revision 1.1.1.2 1 /* gdb commands implemented in Python
2
3 Copyright (C) 2008-2015 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 static 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. It also takes HANDLE_BRKCHARS_P, an argument to
216 identify whether it is being called from the brkchar handler or
217 from the "pure" completer. In the first case, it effectively calls
218 the Python method for completion, and records the PyObject in a
219 static variable (used as a "cache"). In the second case, it just
220 returns that variable, without actually calling the Python method
221 again. This saves us one Python method call.
222
223 The reason for this two step dance is that we need to know the set
224 of "brkchars" to use early on, before we actually try to perform
225 the completion. But if a Python command supplies a "complete"
226 method then we have to call that method first: it may return as its
227 result the kind of completion to perform and that will in turn
228 specify which brkchars to use. IOW, we need the result of the
229 "complete" method before we actually perform the completion.
230
231 It is important to mention that this function is built on the
232 assumption that the calls to cmdpy_completer_handle_brkchars and
233 cmdpy_completer will be subsequent with nothing intervening. This
234 is true for our completer mechanism.
235
236 This function returns the PyObject representing the Python method
237 call. */
238
239 static PyObject *
240 cmdpy_completer_helper (struct cmd_list_element *command,
241 const char *text, const char *word,
242 int handle_brkchars_p)
243 {
244 cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
245 PyObject *textobj, *wordobj;
246 /* This static variable will server as a "cache" for us, in order to
247 store the PyObject that results from calling the Python
248 function. */
249 static PyObject *resultobj = NULL;
250
251 if (handle_brkchars_p)
252 {
253 /* If we were called to handle brkchars, it means this is the
254 first function call of two that will happen in a row.
255 Therefore, we need to call the completer ourselves, and cache
256 the return value in the static variable RESULTOBJ. Then, in
257 the second call, we can just use the value of RESULTOBJ to do
258 our job. */
259 if (resultobj != NULL)
260 Py_DECREF (resultobj);
261
262 resultobj = NULL;
263 if (obj == NULL)
264 error (_("Invalid invocation of Python command object."));
265 if (!PyObject_HasAttr ((PyObject *) obj, complete_cst))
266 {
267 /* If there is no complete method, don't error. */
268 return NULL;
269 }
270
271 textobj = PyUnicode_Decode (text, strlen (text), host_charset (), NULL);
272 if (textobj == NULL)
273 error (_("Could not convert argument to Python string."));
274 wordobj = PyUnicode_Decode (word, sizeof (word), host_charset (), NULL);
275 if (wordobj == NULL)
276 {
277 Py_DECREF (textobj);
278 error (_("Could not convert argument to Python string."));
279 }
280
281 resultobj = PyObject_CallMethodObjArgs ((PyObject *) obj, complete_cst,
282 textobj, wordobj, NULL);
283 Py_DECREF (textobj);
284 Py_DECREF (wordobj);
285 if (!resultobj)
286 {
287 /* Just swallow errors here. */
288 PyErr_Clear ();
289 }
290
291 Py_XINCREF (resultobj);
292 }
293
294 return resultobj;
295 }
296
297 /* Python function called to determine the break characters of a
298 certain completer. We are only interested in knowing if the
299 completer registered by the user will return one of the integer
300 codes (see COMPLETER_* symbols). */
301
302 static void
303 cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
304 const char *text, const char *word)
305 {
306 PyObject *resultobj = NULL;
307 struct cleanup *cleanup;
308
309 cleanup = ensure_python_env (get_current_arch (), current_language);
310
311 /* Calling our helper to obtain the PyObject of the Python
312 function. */
313 resultobj = cmdpy_completer_helper (command, text, word, 1);
314
315 /* Check if there was an error. */
316 if (resultobj == NULL)
317 goto done;
318
319 if (PyInt_Check (resultobj))
320 {
321 /* User code may also return one of the completion constants,
322 thus requesting that sort of completion. We are only
323 interested in this kind of return. */
324 long value;
325
326 if (!gdb_py_int_as_long (resultobj, &value))
327 {
328 /* Ignore. */
329 PyErr_Clear ();
330 }
331 else if (value >= 0 && value < (long) N_COMPLETERS)
332 {
333 /* This is the core of this function. Depending on which
334 completer type the Python function returns, we have to
335 adjust the break characters accordingly. */
336 set_gdb_completion_word_break_characters
337 (completers[value].completer);
338 }
339 }
340
341 done:
342
343 /* We do not call Py_XDECREF here because RESULTOBJ will be used in
344 the subsequent call to cmdpy_completer function. */
345 do_cleanups (cleanup);
346 }
347
348 /* Called by gdb for command completion. */
349
350 static VEC (char_ptr) *
351 cmdpy_completer (struct cmd_list_element *command,
352 const char *text, const char *word)
353 {
354 PyObject *resultobj = NULL;
355 VEC (char_ptr) *result = NULL;
356 struct cleanup *cleanup;
357
358 cleanup = ensure_python_env (get_current_arch (), current_language);
359
360 /* Calling our helper to obtain the PyObject of the Python
361 function. */
362 resultobj = cmdpy_completer_helper (command, text, word, 0);
363
364 /* If the result object of calling the Python function is NULL, it
365 means that there was an error. In this case, just give up and
366 return NULL. */
367 if (resultobj == NULL)
368 goto done;
369
370 result = NULL;
371 if (PyInt_Check (resultobj))
372 {
373 /* User code may also return one of the completion constants,
374 thus requesting that sort of completion. */
375 long value;
376
377 if (! gdb_py_int_as_long (resultobj, &value))
378 {
379 /* Ignore. */
380 PyErr_Clear ();
381 }
382 else if (value >= 0 && value < (long) N_COMPLETERS)
383 result = completers[value].completer (command, text, word);
384 }
385 else
386 {
387 PyObject *iter = PyObject_GetIter (resultobj);
388 PyObject *elt;
389
390 if (iter == NULL)
391 goto done;
392
393 while ((elt = PyIter_Next (iter)) != NULL)
394 {
395 char *item;
396
397 if (! gdbpy_is_string (elt))
398 {
399 /* Skip problem elements. */
400 Py_DECREF (elt);
401 continue;
402 }
403 item = python_string_to_host_string (elt);
404 Py_DECREF (elt);
405 if (item == NULL)
406 {
407 /* Skip problem elements. */
408 PyErr_Clear ();
409 continue;
410 }
411 VEC_safe_push (char_ptr, result, item);
412 }
413
414 Py_DECREF (iter);
415
416 /* If we got some results, ignore problems. Otherwise, report
417 the problem. */
418 if (result != NULL && PyErr_Occurred ())
419 PyErr_Clear ();
420 }
421
422 done:
423
424 do_cleanups (cleanup);
425
426 return result;
427 }
428
429 /* Helper for cmdpy_init which locates the command list to use and
430 pulls out the command name.
431
432 NAME is the command name list. The final word in the list is the
433 name of the new command. All earlier words must be existing prefix
434 commands.
435
436 *BASE_LIST is set to the final prefix command's list of
437 *sub-commands.
438
439 START_LIST is the list in which the search starts.
440
441 This function returns the xmalloc()d name of the new command. On
442 error sets the Python error and returns NULL. */
443
444 char *
445 gdbpy_parse_command_name (const char *name,
446 struct cmd_list_element ***base_list,
447 struct cmd_list_element **start_list)
448 {
449 struct cmd_list_element *elt;
450 int len = strlen (name);
451 int i, lastchar;
452 char *prefix_text;
453 const char *prefix_text2;
454 char *result;
455
456 /* Skip trailing whitespace. */
457 for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
458 ;
459 if (i < 0)
460 {
461 PyErr_SetString (PyExc_RuntimeError, _("No command name found."));
462 return NULL;
463 }
464 lastchar = i;
465
466 /* Find first character of the final word. */
467 for (; i > 0 && (isalnum (name[i - 1])
468 || name[i - 1] == '-'
469 || name[i - 1] == '_');
470 --i)
471 ;
472 result = xmalloc (lastchar - i + 2);
473 memcpy (result, &name[i], lastchar - i + 1);
474 result[lastchar - i + 1] = '\0';
475
476 /* Skip whitespace again. */
477 for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
478 ;
479 if (i < 0)
480 {
481 *base_list = start_list;
482 return result;
483 }
484
485 prefix_text = xmalloc (i + 2);
486 memcpy (prefix_text, name, i + 1);
487 prefix_text[i + 1] = '\0';
488
489 prefix_text2 = prefix_text;
490 elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, 1);
491 if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
492 {
493 PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."),
494 prefix_text);
495 xfree (prefix_text);
496 xfree (result);
497 return NULL;
498 }
499
500 if (elt->prefixlist)
501 {
502 xfree (prefix_text);
503 *base_list = elt->prefixlist;
504 return result;
505 }
506
507 PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."),
508 prefix_text);
509 xfree (prefix_text);
510 xfree (result);
511 return NULL;
512 }
513
514 /* Object initializer; sets up gdb-side structures for command.
515
516 Use: __init__(NAME, COMMAND_CLASS [, COMPLETER_CLASS][, PREFIX]]).
517
518 NAME is the name of the command. It may consist of multiple words,
519 in which case the final word is the name of the new command, and
520 earlier words must be prefix commands.
521
522 COMMAND_CLASS is the kind of command. It should be one of the COMMAND_*
523 constants defined in the gdb module.
524
525 COMPLETER_CLASS is the kind of completer. If not given, the
526 "complete" method will be used. Otherwise, it should be one of the
527 COMPLETE_* constants defined in the gdb module.
528
529 If PREFIX is True, then this command is a prefix command.
530
531 The documentation for the command is taken from the doc string for
532 the python class. */
533
534 static int
535 cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
536 {
537 cmdpy_object *obj = (cmdpy_object *) self;
538 const char *name;
539 int cmdtype;
540 int completetype = -1;
541 char *docstring = NULL;
542 volatile struct gdb_exception except;
543 struct cmd_list_element **cmd_list;
544 char *cmd_name, *pfx_name;
545 static char *keywords[] = { "name", "command_class", "completer_class",
546 "prefix", NULL };
547 PyObject *is_prefix = NULL;
548 int cmp;
549
550 if (obj->command)
551 {
552 /* Note: this is apparently not documented in Python. We return
553 0 for success, -1 for failure. */
554 PyErr_Format (PyExc_RuntimeError,
555 _("Command object already initialized."));
556 return -1;
557 }
558
559 if (! PyArg_ParseTupleAndKeywords (args, kw, "si|iO",
560 keywords, &name, &cmdtype,
561 &completetype, &is_prefix))
562 return -1;
563
564 if (cmdtype != no_class && cmdtype != class_run
565 && cmdtype != class_vars && cmdtype != class_stack
566 && cmdtype != class_files && cmdtype != class_support
567 && cmdtype != class_info && cmdtype != class_breakpoint
568 && cmdtype != class_trace && cmdtype != class_obscure
569 && cmdtype != class_maintenance && cmdtype != class_user)
570 {
571 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
572 return -1;
573 }
574
575 if (completetype < -1 || completetype >= (int) N_COMPLETERS)
576 {
577 PyErr_Format (PyExc_RuntimeError,
578 _("Invalid completion type argument."));
579 return -1;
580 }
581
582 cmd_name = gdbpy_parse_command_name (name, &cmd_list, &cmdlist);
583 if (! cmd_name)
584 return -1;
585
586 pfx_name = NULL;
587 if (is_prefix != NULL)
588 {
589 cmp = PyObject_IsTrue (is_prefix);
590 if (cmp == 1)
591 {
592 int i, out;
593
594 /* Make a normalized form of the command name. */
595 pfx_name = xmalloc (strlen (name) + 2);
596
597 i = 0;
598 out = 0;
599 while (name[i])
600 {
601 /* Skip whitespace. */
602 while (name[i] == ' ' || name[i] == '\t')
603 ++i;
604 /* Copy non-whitespace characters. */
605 while (name[i] && name[i] != ' ' && name[i] != '\t')
606 pfx_name[out++] = name[i++];
607 /* Add a single space after each word -- including the final
608 word. */
609 pfx_name[out++] = ' ';
610 }
611 pfx_name[out] = '\0';
612 }
613 else if (cmp < 0)
614 {
615 xfree (cmd_name);
616 return -1;
617 }
618 }
619 if (PyObject_HasAttr (self, gdbpy_doc_cst))
620 {
621 PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst);
622
623 if (ds_obj && gdbpy_is_string (ds_obj))
624 {
625 docstring = python_string_to_host_string (ds_obj);
626 if (docstring == NULL)
627 {
628 xfree (cmd_name);
629 xfree (pfx_name);
630 Py_DECREF (ds_obj);
631 return -1;
632 }
633 }
634
635 Py_XDECREF (ds_obj);
636 }
637 if (! docstring)
638 docstring = xstrdup (_("This command is not documented."));
639
640 Py_INCREF (self);
641
642 TRY_CATCH (except, RETURN_MASK_ALL)
643 {
644 struct cmd_list_element *cmd;
645
646 if (pfx_name)
647 {
648 int allow_unknown;
649
650 /* If we have our own "invoke" method, then allow unknown
651 sub-commands. */
652 allow_unknown = PyObject_HasAttr (self, invoke_cst);
653 cmd = add_prefix_cmd (cmd_name, (enum command_class) cmdtype,
654 NULL, docstring, &obj->sub_list,
655 pfx_name, allow_unknown, cmd_list);
656 }
657 else
658 cmd = add_cmd (cmd_name, (enum command_class) cmdtype, NULL,
659 docstring, cmd_list);
660
661 /* There appears to be no API to set this. */
662 cmd->func = cmdpy_function;
663 cmd->destroyer = cmdpy_destroyer;
664
665 obj->command = cmd;
666 set_cmd_context (cmd, self);
667 set_cmd_completer (cmd, ((completetype == -1) ? cmdpy_completer
668 : completers[completetype].completer));
669 if (completetype == -1)
670 set_cmd_completer_handle_brkchars (cmd,
671 cmdpy_completer_handle_brkchars);
672 }
673 if (except.reason < 0)
674 {
675 xfree (cmd_name);
676 xfree (docstring);
677 xfree (pfx_name);
678 Py_DECREF (self);
679 PyErr_Format (except.reason == RETURN_QUIT
680 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
681 "%s", except.message);
682 return -1;
683 }
684 return 0;
685 }
686
687
688
690 /* Initialize the 'commands' code. */
691
692 int
693 gdbpy_initialize_commands (void)
694 {
695 int i;
696
697 cmdpy_object_type.tp_new = PyType_GenericNew;
698 if (PyType_Ready (&cmdpy_object_type) < 0)
699 return -1;
700
701 /* Note: alias and user are special; pseudo appears to be unused,
702 and there is no reason to expose tui or xdb, I think. */
703 if (PyModule_AddIntConstant (gdb_module, "COMMAND_NONE", no_class) < 0
704 || PyModule_AddIntConstant (gdb_module, "COMMAND_RUNNING", class_run) < 0
705 || PyModule_AddIntConstant (gdb_module, "COMMAND_DATA", class_vars) < 0
706 || PyModule_AddIntConstant (gdb_module, "COMMAND_STACK", class_stack) < 0
707 || PyModule_AddIntConstant (gdb_module, "COMMAND_FILES", class_files) < 0
708 || PyModule_AddIntConstant (gdb_module, "COMMAND_SUPPORT",
709 class_support) < 0
710 || PyModule_AddIntConstant (gdb_module, "COMMAND_STATUS", class_info) < 0
711 || PyModule_AddIntConstant (gdb_module, "COMMAND_BREAKPOINTS",
712 class_breakpoint) < 0
713 || PyModule_AddIntConstant (gdb_module, "COMMAND_TRACEPOINTS",
714 class_trace) < 0
715 || PyModule_AddIntConstant (gdb_module, "COMMAND_OBSCURE",
716 class_obscure) < 0
717 || PyModule_AddIntConstant (gdb_module, "COMMAND_MAINTENANCE",
718 class_maintenance) < 0
719 || PyModule_AddIntConstant (gdb_module, "COMMAND_USER", class_user) < 0)
720 return -1;
721
722 for (i = 0; i < N_COMPLETERS; ++i)
723 {
724 if (PyModule_AddIntConstant (gdb_module, completers[i].name, i) < 0)
725 return -1;
726 }
727
728 if (gdb_pymodule_addobject (gdb_module, "Command",
729 (PyObject *) &cmdpy_object_type) < 0)
730 return -1;
731
732 invoke_cst = PyString_FromString ("invoke");
733 if (invoke_cst == NULL)
734 return -1;
735 complete_cst = PyString_FromString ("complete");
736 if (complete_cst == NULL)
737 return -1;
738
739 return 0;
740 }
741
742
743
745 static PyMethodDef cmdpy_object_methods[] =
746 {
747 { "dont_repeat", cmdpy_dont_repeat, METH_NOARGS,
748 "Prevent command repetition when user enters empty line." },
749
750 { 0 }
751 };
752
753 static PyTypeObject cmdpy_object_type =
754 {
755 PyVarObject_HEAD_INIT (NULL, 0)
756 "gdb.Command", /*tp_name*/
757 sizeof (cmdpy_object), /*tp_basicsize*/
758 0, /*tp_itemsize*/
759 0, /*tp_dealloc*/
760 0, /*tp_print*/
761 0, /*tp_getattr*/
762 0, /*tp_setattr*/
763 0, /*tp_compare*/
764 0, /*tp_repr*/
765 0, /*tp_as_number*/
766 0, /*tp_as_sequence*/
767 0, /*tp_as_mapping*/
768 0, /*tp_hash */
769 0, /*tp_call*/
770 0, /*tp_str*/
771 0, /*tp_getattro*/
772 0, /*tp_setattro*/
773 0, /*tp_as_buffer*/
774 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
775 "GDB command object", /* tp_doc */
776 0, /* tp_traverse */
777 0, /* tp_clear */
778 0, /* tp_richcompare */
779 0, /* tp_weaklistoffset */
780 0, /* tp_iter */
781 0, /* tp_iternext */
782 cmdpy_object_methods, /* tp_methods */
783 0, /* tp_members */
784 0, /* tp_getset */
785 0, /* tp_base */
786 0, /* tp_dict */
787 0, /* tp_descr_get */
788 0, /* tp_descr_set */
789 0, /* tp_dictoffset */
790 cmdpy_init, /* tp_init */
791 0, /* tp_alloc */
792 };
793
794
795
797 /* Utility to build a buildargv-like result from ARGS.
798 This intentionally parses arguments the way libiberty/argv.c:buildargv
799 does. It splits up arguments in a reasonable way, and we want a standard
800 way of parsing arguments. Several gdb commands use buildargv to parse their
801 arguments. Plus we want to be able to write compatible python
802 implementations of gdb commands. */
803
804 PyObject *
805 gdbpy_string_to_argv (PyObject *self, PyObject *args)
806 {
807 PyObject *py_argv;
808 const char *input;
809
810 if (!PyArg_ParseTuple (args, "s", &input))
811 return NULL;
812
813 py_argv = PyList_New (0);
814 if (py_argv == NULL)
815 return NULL;
816
817 /* buildargv uses NULL to represent an empty argument list, but we can't use
818 that in Python. Instead, if ARGS is "" then return an empty list.
819 This undoes the NULL -> "" conversion that cmdpy_function does. */
820
821 if (*input != '\0')
822 {
823 char **c_argv = gdb_buildargv (input);
824 int i;
825
826 for (i = 0; c_argv[i] != NULL; ++i)
827 {
828 PyObject *argp = PyString_FromString (c_argv[i]);
829
830 if (argp == NULL
831 || PyList_Append (py_argv, argp) < 0)
832 {
833 Py_XDECREF (argp);
834 Py_DECREF (py_argv);
835 freeargv (c_argv);
836 return NULL;
837 }
838 Py_DECREF (argp);
839 }
840
841 freeargv (c_argv);
842 }
843
844 return py_argv;
845 }
846