daily update
[deliverable/binutils-gdb.git] / gdb / python / py-cmd.c
CommitLineData
d8906c6f
TJB
1/* gdb commands implemented in Python
2
28e7fd62 3 Copyright (C) 2008-2013 Free Software Foundation, Inc.
d8906c6f
TJB
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"
d452c4bc 22#include "arch-utils.h"
d8906c6f
TJB
23#include "value.h"
24#include "exceptions.h"
25#include "python-internal.h"
26#include "charset.h"
27#include "gdbcmd.h"
28#include "cli/cli-decode.h"
29#include "completer.h"
d452c4bc 30#include "language.h"
d8906c6f
TJB
31
32/* Struct representing built-in completion types. */
33struct cmdpy_completer
34{
35 /* Python symbol name. */
36 char *name;
37 /* Completion function. */
625e8578 38 completer_ftype *completer;
d8906c6f
TJB
39};
40
41static struct cmdpy_completer completers[] =
42{
43 { "COMPLETE_NONE", noop_completer },
44 { "COMPLETE_FILENAME", filename_completer },
45 { "COMPLETE_LOCATION", location_completer },
46 { "COMPLETE_COMMAND", command_completer },
47 { "COMPLETE_SYMBOL", make_symbol_completion_list_fn },
48};
49
50#define N_COMPLETERS (sizeof (completers) / sizeof (completers[0]))
51
52/* A gdb command. For the time being only ordinary commands (not
53 set/show commands) are allowed. */
54struct cmdpy_object
55{
56 PyObject_HEAD
57
58 /* The corresponding gdb command object, or NULL if the command is
59 no longer installed. */
60 struct cmd_list_element *command;
61
62 /* A prefix command requires storage for a list of its sub-commands.
63 A pointer to this is passed to add_prefix_command, and to add_cmd
64 for sub-commands of that prefix. If this Command is not a prefix
65 command, then this field is unused. */
66 struct cmd_list_element *sub_list;
67};
68
69typedef struct cmdpy_object cmdpy_object;
70
62eec1a5
TT
71static PyTypeObject cmdpy_object_type
72 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("cmdpy_object");
d8906c6f 73
d8906c6f
TJB
74/* Constants used by this module. */
75static PyObject *invoke_cst;
76static PyObject *complete_cst;
77
78\f
79
80/* Python function which wraps dont_repeat. */
81static PyObject *
82cmdpy_dont_repeat (PyObject *self, PyObject *args)
83{
84 dont_repeat ();
85 Py_RETURN_NONE;
86}
87
88\f
89
90/* Called if the gdb cmd_list_element is destroyed. */
07ca107c 91
d8906c6f
TJB
92static void
93cmdpy_destroyer (struct cmd_list_element *self, void *context)
94{
95 cmdpy_object *cmd;
d452c4bc 96 struct cleanup *cleanup;
d8906c6f 97
d452c4bc 98 cleanup = ensure_python_env (get_current_arch (), current_language);
d8906c6f
TJB
99
100 /* Release our hold on the command object. */
101 cmd = (cmdpy_object *) context;
102 cmd->command = NULL;
103 Py_DECREF (cmd);
104
105 /* We allocated the name, doc string, and perhaps the prefix
106 name. */
6f937416 107 xfree ((char *) self->name);
d8906c6f
TJB
108 xfree (self->doc);
109 xfree (self->prefixname);
110
d452c4bc 111 do_cleanups (cleanup);
d8906c6f
TJB
112}
113
114/* Called by gdb to invoke the command. */
07ca107c 115
d8906c6f
TJB
116static void
117cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
118{
119 cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
120 PyObject *argobj, *ttyobj, *result;
121 struct cleanup *cleanup;
d8906c6f 122
d452c4bc 123 cleanup = ensure_python_env (get_current_arch (), current_language);
d8906c6f
TJB
124
125 if (! obj)
126 error (_("Invalid invocation of Python command object."));
127 if (! PyObject_HasAttr ((PyObject *) obj, invoke_cst))
128 {
129 if (obj->command->prefixname)
130 {
131 /* A prefix command does not need an invoke method. */
132 do_cleanups (cleanup);
133 return;
134 }
135 error (_("Python command object missing 'invoke' method."));
136 }
137
138 if (! args)
139 args = "";
140 argobj = PyUnicode_Decode (args, strlen (args), host_charset (), NULL);
141 if (! argobj)
8dc78533
JK
142 {
143 gdbpy_print_stack ();
144 error (_("Could not convert arguments to Python string."));
145 }
d8906c6f
TJB
146
147 ttyobj = from_tty ? Py_True : Py_False;
148 Py_INCREF (ttyobj);
149 result = PyObject_CallMethodObjArgs ((PyObject *) obj, invoke_cst, argobj,
150 ttyobj, NULL);
151 Py_DECREF (argobj);
152 Py_DECREF (ttyobj);
07ca107c 153
d8906c6f
TJB
154 if (! result)
155 {
156 PyObject *ptype, *pvalue, *ptraceback;
07ca107c 157 char *msg;
d8906c6f
TJB
158
159 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
160
07ca107c
DE
161 /* Try to fetch an error message contained within ptype, pvalue.
162 When fetching the error message we need to make our own copy,
163 we no longer own ptype, pvalue after the call to PyErr_Restore. */
d8906c6f 164
07ca107c
DE
165 msg = gdbpy_exception_to_string (ptype, pvalue);
166 make_cleanup (xfree, msg);
167
168 if (msg == NULL)
169 {
170 /* An error occurred computing the string representation of the
171 error message. This is rare, but we should inform the user. */
172 printf_filtered (_("An error occurred in a Python command\n"
9a2b4c1b
MS
173 "and then another occurred computing the "
174 "error message.\n"));
d8906c6f 175 gdbpy_print_stack ();
d8906c6f 176 }
07ca107c
DE
177
178 /* Don't print the stack for gdb.GdbError exceptions.
179 It is generally used to flag user errors.
180
181 We also don't want to print "Error occurred in Python command"
182 for user errors. However, a missing message for gdb.GdbError
183 exceptions is arguably a bug, so we flag it as such. */
184
185 if (! PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc)
186 || msg == NULL || *msg == '\0')
d8906c6f
TJB
187 {
188 PyErr_Restore (ptype, pvalue, ptraceback);
189 gdbpy_print_stack ();
07ca107c
DE
190 if (msg != NULL && *msg != '\0')
191 error (_("Error occurred in Python command: %s"), msg);
192 else
193 error (_("Error occurred in Python command."));
d8906c6f 194 }
07ca107c 195 else
cca56ac7
TT
196 {
197 Py_XDECREF (ptype);
198 Py_XDECREF (pvalue);
199 Py_XDECREF (ptraceback);
200 error ("%s", msg);
201 }
d8906c6f 202 }
07ca107c 203
d8906c6f
TJB
204 Py_DECREF (result);
205 do_cleanups (cleanup);
206}
207
208/* Called by gdb for command completion. */
63d97a20 209
49c4e619 210static VEC (char_ptr) *
6f937416
PA
211cmdpy_completer (struct cmd_list_element *command,
212 const char *text, const char *word)
d8906c6f
TJB
213{
214 cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
215 PyObject *textobj, *wordobj, *resultobj = NULL;
49c4e619 216 VEC (char_ptr) *result = NULL;
d8906c6f 217 struct cleanup *cleanup;
d8906c6f 218
d452c4bc 219 cleanup = ensure_python_env (get_current_arch (), current_language);
d8906c6f
TJB
220
221 if (! obj)
222 error (_("Invalid invocation of Python command object."));
223 if (! PyObject_HasAttr ((PyObject *) obj, complete_cst))
224 {
225 /* If there is no complete method, don't error -- instead, just
226 say that there are no completions. */
227 goto done;
228 }
229
230 textobj = PyUnicode_Decode (text, strlen (text), host_charset (), NULL);
231 if (! textobj)
232 error (_("Could not convert argument to Python string."));
233 wordobj = PyUnicode_Decode (word, strlen (word), host_charset (), NULL);
234 if (! wordobj)
235 error (_("Could not convert argument to Python string."));
236
237 resultobj = PyObject_CallMethodObjArgs ((PyObject *) obj, complete_cst,
238 textobj, wordobj, NULL);
239 Py_DECREF (textobj);
240 Py_DECREF (wordobj);
241 if (! resultobj)
242 {
243 /* Just swallow errors here. */
244 PyErr_Clear ();
245 goto done;
246 }
d8906c6f
TJB
247
248 result = NULL;
ba327838 249 if (PyInt_Check (resultobj))
d8906c6f 250 {
ba327838
TT
251 /* User code may also return one of the completion constants,
252 thus requesting that sort of completion. */
253 long value;
254
255 if (! gdb_py_int_as_long (resultobj, &value))
256 {
257 /* Ignore. */
258 PyErr_Clear ();
259 }
260 else if (value >= 0 && value < (long) N_COMPLETERS)
261 result = completers[value].completer (command, text, word);
262 }
263 else
264 {
265 PyObject *iter = PyObject_GetIter (resultobj);
266 PyObject *elt;
d59b6f6c 267
ba327838 268 if (iter == NULL)
d8906c6f
TJB
269 goto done;
270
ba327838 271 while ((elt = PyIter_Next (iter)) != NULL)
d8906c6f 272 {
49c4e619 273 char *item;
d59b6f6c 274
ba327838 275 if (! gdbpy_is_string (elt))
d8906c6f
TJB
276 {
277 /* Skip problem elements. */
ba327838 278 Py_DECREF (elt);
d8906c6f
TJB
279 continue;
280 }
49c4e619 281 item = python_string_to_host_string (elt);
ba327838 282 Py_DECREF (elt);
49c4e619 283 if (item == NULL)
8dc78533
JK
284 {
285 /* Skip problem elements. */
286 PyErr_Clear ();
287 continue;
288 }
49c4e619 289 VEC_safe_push (char_ptr, result, item);
d8906c6f 290 }
d59b6f6c 291
ba327838
TT
292 Py_DECREF (iter);
293
294 /* If we got some results, ignore problems. Otherwise, report
295 the problem. */
296 if (result != NULL && PyErr_Occurred ())
297 PyErr_Clear ();
d8906c6f
TJB
298 }
299
300 done:
301
3d4a3c3e 302 Py_XDECREF (resultobj);
d8906c6f
TJB
303 do_cleanups (cleanup);
304
305 return result;
306}
307
308/* Helper for cmdpy_init which locates the command list to use and
309 pulls out the command name.
310
63d97a20 311 NAME is the command name list. The final word in the list is the
d8906c6f
TJB
312 name of the new command. All earlier words must be existing prefix
313 commands.
314
315 *BASE_LIST is set to the final prefix command's list of
316 *sub-commands.
317
d7b32ed3
PM
318 START_LIST is the list in which the search starts.
319
d8906c6f
TJB
320 This function returns the xmalloc()d name of the new command. On
321 error sets the Python error and returns NULL. */
63d97a20 322
d7b32ed3 323char *
63d97a20 324gdbpy_parse_command_name (const char *name,
d7b32ed3
PM
325 struct cmd_list_element ***base_list,
326 struct cmd_list_element **start_list)
d8906c6f
TJB
327{
328 struct cmd_list_element *elt;
63d97a20 329 int len = strlen (name);
d8906c6f 330 int i, lastchar;
6f937416
PA
331 char *prefix_text;
332 const char *prefix_text2;
d8906c6f
TJB
333 char *result;
334
335 /* Skip trailing whitespace. */
63d97a20 336 for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
d8906c6f
TJB
337 ;
338 if (i < 0)
339 {
044c0f87 340 PyErr_SetString (PyExc_RuntimeError, _("No command name found."));
d8906c6f
TJB
341 return NULL;
342 }
343 lastchar = i;
344
345 /* Find first character of the final word. */
63d97a20
DE
346 for (; i > 0 && (isalnum (name[i - 1])
347 || name[i - 1] == '-'
348 || name[i - 1] == '_');
d8906c6f
TJB
349 --i)
350 ;
351 result = xmalloc (lastchar - i + 2);
63d97a20 352 memcpy (result, &name[i], lastchar - i + 1);
d8906c6f
TJB
353 result[lastchar - i + 1] = '\0';
354
355 /* Skip whitespace again. */
63d97a20 356 for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
d8906c6f
TJB
357 ;
358 if (i < 0)
359 {
d7b32ed3 360 *base_list = start_list;
d8906c6f
TJB
361 return result;
362 }
363
364 prefix_text = xmalloc (i + 2);
63d97a20 365 memcpy (prefix_text, name, i + 1);
d8906c6f
TJB
366 prefix_text[i + 1] = '\0';
367
63d97a20
DE
368 prefix_text2 = prefix_text;
369 elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, 1);
d8906c6f
TJB
370 if (!elt || elt == (struct cmd_list_element *) -1)
371 {
044c0f87 372 PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."),
d8906c6f
TJB
373 prefix_text);
374 xfree (prefix_text);
375 xfree (result);
376 return NULL;
377 }
378
379 if (elt->prefixlist)
380 {
381 xfree (prefix_text);
382 *base_list = elt->prefixlist;
383 return result;
384 }
385
044c0f87 386 PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."),
d8906c6f
TJB
387 prefix_text);
388 xfree (prefix_text);
389 xfree (result);
390 return NULL;
391}
392
393/* Object initializer; sets up gdb-side structures for command.
394
cc924cad 395 Use: __init__(NAME, COMMAND_CLASS [, COMPLETER_CLASS][, PREFIX]]).
d8906c6f
TJB
396
397 NAME is the name of the command. It may consist of multiple words,
398 in which case the final word is the name of the new command, and
399 earlier words must be prefix commands.
400
cc924cad 401 COMMAND_CLASS is the kind of command. It should be one of the COMMAND_*
d8906c6f
TJB
402 constants defined in the gdb module.
403
cc924cad 404 COMPLETER_CLASS is the kind of completer. If not given, the
d8906c6f
TJB
405 "complete" method will be used. Otherwise, it should be one of the
406 COMPLETE_* constants defined in the gdb module.
407
408 If PREFIX is True, then this command is a prefix command.
409
410 The documentation for the command is taken from the doc string for
63d97a20
DE
411 the python class. */
412
d8906c6f 413static int
cc924cad 414cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
d8906c6f
TJB
415{
416 cmdpy_object *obj = (cmdpy_object *) self;
ddd49eee 417 const char *name;
d8906c6f
TJB
418 int cmdtype;
419 int completetype = -1;
420 char *docstring = NULL;
421 volatile struct gdb_exception except;
422 struct cmd_list_element **cmd_list;
423 char *cmd_name, *pfx_name;
cc924cad
TJB
424 static char *keywords[] = { "name", "command_class", "completer_class",
425 "prefix", NULL };
d8906c6f
TJB
426 PyObject *is_prefix = NULL;
427 int cmp;
428
429 if (obj->command)
430 {
431 /* Note: this is apparently not documented in Python. We return
432 0 for success, -1 for failure. */
433 PyErr_Format (PyExc_RuntimeError,
044c0f87 434 _("Command object already initialized."));
d8906c6f
TJB
435 return -1;
436 }
437
9a2b4c1b
MS
438 if (! PyArg_ParseTupleAndKeywords (args, kw, "si|iO",
439 keywords, &name, &cmdtype,
d8906c6f
TJB
440 &completetype, &is_prefix))
441 return -1;
442
443 if (cmdtype != no_class && cmdtype != class_run
444 && cmdtype != class_vars && cmdtype != class_stack
445 && cmdtype != class_files && cmdtype != class_support
446 && cmdtype != class_info && cmdtype != class_breakpoint
447 && cmdtype != class_trace && cmdtype != class_obscure
7d74f244 448 && cmdtype != class_maintenance && cmdtype != class_user)
d8906c6f 449 {
044c0f87 450 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
d8906c6f
TJB
451 return -1;
452 }
453
454 if (completetype < -1 || completetype >= (int) N_COMPLETERS)
455 {
9a2b4c1b
MS
456 PyErr_Format (PyExc_RuntimeError,
457 _("Invalid completion type argument."));
d8906c6f
TJB
458 return -1;
459 }
460
63d97a20 461 cmd_name = gdbpy_parse_command_name (name, &cmd_list, &cmdlist);
d8906c6f
TJB
462 if (! cmd_name)
463 return -1;
464
465 pfx_name = NULL;
466 if (is_prefix != NULL)
467 {
468 cmp = PyObject_IsTrue (is_prefix);
469 if (cmp == 1)
470 {
471 int i, out;
472
473 /* Make a normalized form of the command name. */
474 pfx_name = xmalloc (strlen (name) + 2);
475
476 i = 0;
477 out = 0;
478 while (name[i])
479 {
480 /* Skip whitespace. */
481 while (name[i] == ' ' || name[i] == '\t')
482 ++i;
483 /* Copy non-whitespace characters. */
484 while (name[i] && name[i] != ' ' && name[i] != '\t')
485 pfx_name[out++] = name[i++];
486 /* Add a single space after each word -- including the final
487 word. */
488 pfx_name[out++] = ' ';
489 }
490 pfx_name[out] = '\0';
491 }
492 else if (cmp < 0)
e463f587
MS
493 {
494 xfree (cmd_name);
d8906c6f 495 return -1;
e463f587 496 }
d8906c6f
TJB
497 }
498 if (PyObject_HasAttr (self, gdbpy_doc_cst))
499 {
500 PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst);
d59b6f6c 501
d8906c6f 502 if (ds_obj && gdbpy_is_string (ds_obj))
8dc78533
JK
503 {
504 docstring = python_string_to_host_string (ds_obj);
505 if (docstring == NULL)
506 {
507 xfree (cmd_name);
508 xfree (pfx_name);
d8191432 509 Py_DECREF (ds_obj);
8dc78533
JK
510 return -1;
511 }
512 }
d8191432
TT
513
514 Py_XDECREF (ds_obj);
d8906c6f
TJB
515 }
516 if (! docstring)
517 docstring = xstrdup (_("This command is not documented."));
518
519 Py_INCREF (self);
520
521 TRY_CATCH (except, RETURN_MASK_ALL)
522 {
523 struct cmd_list_element *cmd;
524
525 if (pfx_name)
526 {
527 int allow_unknown;
528
529 /* If we have our own "invoke" method, then allow unknown
530 sub-commands. */
531 allow_unknown = PyObject_HasAttr (self, invoke_cst);
532 cmd = add_prefix_cmd (cmd_name, (enum command_class) cmdtype,
533 NULL, docstring, &obj->sub_list,
534 pfx_name, allow_unknown, cmd_list);
535 }
536 else
537 cmd = add_cmd (cmd_name, (enum command_class) cmdtype, NULL,
538 docstring, cmd_list);
539
540 /* There appears to be no API to set this. */
541 cmd->func = cmdpy_function;
542 cmd->destroyer = cmdpy_destroyer;
543
544 obj->command = cmd;
545 set_cmd_context (cmd, self);
546 set_cmd_completer (cmd, ((completetype == -1) ? cmdpy_completer
547 : completers[completetype].completer));
548 }
549 if (except.reason < 0)
550 {
551 xfree (cmd_name);
552 xfree (docstring);
553 xfree (pfx_name);
554 Py_DECREF (self);
555 PyErr_Format (except.reason == RETURN_QUIT
556 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
557 "%s", except.message);
558 return -1;
559 }
560 return 0;
561}
562
563\f
564
565/* Initialize the 'commands' code. */
63d97a20 566
999633ed 567int
d8906c6f
TJB
568gdbpy_initialize_commands (void)
569{
570 int i;
571
6a1b1664 572 cmdpy_object_type.tp_new = PyType_GenericNew;
d8906c6f 573 if (PyType_Ready (&cmdpy_object_type) < 0)
999633ed 574 return -1;
d8906c6f
TJB
575
576 /* Note: alias and user are special; pseudo appears to be unused,
577 and there is no reason to expose tui or xdb, I think. */
578 if (PyModule_AddIntConstant (gdb_module, "COMMAND_NONE", no_class) < 0
579 || PyModule_AddIntConstant (gdb_module, "COMMAND_RUNNING", class_run) < 0
580 || PyModule_AddIntConstant (gdb_module, "COMMAND_DATA", class_vars) < 0
581 || PyModule_AddIntConstant (gdb_module, "COMMAND_STACK", class_stack) < 0
582 || PyModule_AddIntConstant (gdb_module, "COMMAND_FILES", class_files) < 0
583 || PyModule_AddIntConstant (gdb_module, "COMMAND_SUPPORT",
584 class_support) < 0
585 || PyModule_AddIntConstant (gdb_module, "COMMAND_STATUS", class_info) < 0
586 || PyModule_AddIntConstant (gdb_module, "COMMAND_BREAKPOINTS",
587 class_breakpoint) < 0
588 || PyModule_AddIntConstant (gdb_module, "COMMAND_TRACEPOINTS",
589 class_trace) < 0
590 || PyModule_AddIntConstant (gdb_module, "COMMAND_OBSCURE",
591 class_obscure) < 0
592 || PyModule_AddIntConstant (gdb_module, "COMMAND_MAINTENANCE",
7d74f244
DE
593 class_maintenance) < 0
594 || PyModule_AddIntConstant (gdb_module, "COMMAND_USER", class_user) < 0)
999633ed 595 return -1;
d8906c6f
TJB
596
597 for (i = 0; i < N_COMPLETERS; ++i)
598 {
599 if (PyModule_AddIntConstant (gdb_module, completers[i].name, i) < 0)
999633ed 600 return -1;
d8906c6f
TJB
601 }
602
aa36459a
TT
603 if (gdb_pymodule_addobject (gdb_module, "Command",
604 (PyObject *) &cmdpy_object_type) < 0)
999633ed 605 return -1;
d8906c6f
TJB
606
607 invoke_cst = PyString_FromString ("invoke");
999633ed
TT
608 if (invoke_cst == NULL)
609 return -1;
d8906c6f 610 complete_cst = PyString_FromString ("complete");
999633ed
TT
611 if (complete_cst == NULL)
612 return -1;
613
614 return 0;
d8906c6f
TJB
615}
616
617\f
618
619static PyMethodDef cmdpy_object_methods[] =
620{
621 { "dont_repeat", cmdpy_dont_repeat, METH_NOARGS,
622 "Prevent command repetition when user enters empty line." },
623
624 { 0 }
625};
626
627static PyTypeObject cmdpy_object_type =
628{
9a27f2c6 629 PyVarObject_HEAD_INIT (NULL, 0)
d8906c6f
TJB
630 "gdb.Command", /*tp_name*/
631 sizeof (cmdpy_object), /*tp_basicsize*/
632 0, /*tp_itemsize*/
633 0, /*tp_dealloc*/
634 0, /*tp_print*/
635 0, /*tp_getattr*/
636 0, /*tp_setattr*/
637 0, /*tp_compare*/
638 0, /*tp_repr*/
639 0, /*tp_as_number*/
640 0, /*tp_as_sequence*/
641 0, /*tp_as_mapping*/
642 0, /*tp_hash */
643 0, /*tp_call*/
644 0, /*tp_str*/
645 0, /*tp_getattro*/
646 0, /*tp_setattro*/
647 0, /*tp_as_buffer*/
648 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
649 "GDB command object", /* tp_doc */
650 0, /* tp_traverse */
651 0, /* tp_clear */
652 0, /* tp_richcompare */
653 0, /* tp_weaklistoffset */
654 0, /* tp_iter */
655 0, /* tp_iternext */
656 cmdpy_object_methods, /* tp_methods */
657 0, /* tp_members */
658 0, /* tp_getset */
659 0, /* tp_base */
660 0, /* tp_dict */
661 0, /* tp_descr_get */
662 0, /* tp_descr_set */
663 0, /* tp_dictoffset */
664 cmdpy_init, /* tp_init */
665 0, /* tp_alloc */
d8906c6f 666};
07ca107c
DE
667
668\f
669
670/* Utility to build a buildargv-like result from ARGS.
671 This intentionally parses arguments the way libiberty/argv.c:buildargv
672 does. It splits up arguments in a reasonable way, and we want a standard
673 way of parsing arguments. Several gdb commands use buildargv to parse their
674 arguments. Plus we want to be able to write compatible python
675 implementations of gdb commands. */
676
677PyObject *
678gdbpy_string_to_argv (PyObject *self, PyObject *args)
679{
680 PyObject *py_argv;
ddd49eee 681 const char *input;
07ca107c
DE
682
683 if (!PyArg_ParseTuple (args, "s", &input))
684 return NULL;
685
686 py_argv = PyList_New (0);
3919fd96
TT
687 if (py_argv == NULL)
688 return NULL;
07ca107c
DE
689
690 /* buildargv uses NULL to represent an empty argument list, but we can't use
691 that in Python. Instead, if ARGS is "" then return an empty list.
692 This undoes the NULL -> "" conversion that cmdpy_function does. */
693
694 if (*input != '\0')
695 {
696 char **c_argv = gdb_buildargv (input);
697 int i;
698
699 for (i = 0; c_argv[i] != NULL; ++i)
700 {
701 PyObject *argp = PyString_FromString (c_argv[i]);
702
703 if (argp == NULL
704 || PyList_Append (py_argv, argp) < 0)
705 {
5af65ec0 706 Py_XDECREF (argp);
07ca107c
DE
707 Py_DECREF (py_argv);
708 freeargv (c_argv);
709 return NULL;
710 }
5af65ec0 711 Py_DECREF (argp);
07ca107c
DE
712 }
713
714 freeargv (c_argv);
715 }
716
717 return py_argv;
718}
This page took 0.496295 seconds and 4 git commands to generate.