Commit | Line | Data |
---|---|---|
d8906c6f TJB |
1 | /* gdb commands implemented in Python |
2 | ||
4c38e0a4 | 3 | Copyright (C) 2008, 2009, 2010 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. */ | |
33 | struct cmdpy_completer | |
34 | { | |
35 | /* Python symbol name. */ | |
36 | char *name; | |
37 | /* Completion function. */ | |
38 | char **(*completer) (struct cmd_list_element *, char *, char *); | |
39 | }; | |
40 | ||
41 | static 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. */ | |
54 | struct 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 | ||
69 | typedef struct cmdpy_object cmdpy_object; | |
70 | ||
71 | static PyTypeObject cmdpy_object_type; | |
72 | ||
73 | ||
74 | /* Constants used by this module. */ | |
75 | static PyObject *invoke_cst; | |
76 | static PyObject *complete_cst; | |
77 | ||
78 | \f | |
79 | ||
80 | /* Python function which wraps dont_repeat. */ | |
81 | static PyObject * | |
82 | cmdpy_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 |
92 | static void |
93 | cmdpy_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. */ | |
107 | xfree (self->name); | |
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 |
116 | static void |
117 | cmdpy_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" | |
173 | "and then another occurred computing the error message.\n")); | |
d8906c6f | 174 | gdbpy_print_stack (); |
d8906c6f | 175 | } |
07ca107c DE |
176 | |
177 | /* Don't print the stack for gdb.GdbError exceptions. | |
178 | It is generally used to flag user errors. | |
179 | ||
180 | We also don't want to print "Error occurred in Python command" | |
181 | for user errors. However, a missing message for gdb.GdbError | |
182 | exceptions is arguably a bug, so we flag it as such. */ | |
183 | ||
184 | if (! PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc) | |
185 | || msg == NULL || *msg == '\0') | |
d8906c6f TJB |
186 | { |
187 | PyErr_Restore (ptype, pvalue, ptraceback); | |
188 | gdbpy_print_stack (); | |
07ca107c DE |
189 | if (msg != NULL && *msg != '\0') |
190 | error (_("Error occurred in Python command: %s"), msg); | |
191 | else | |
192 | error (_("Error occurred in Python command.")); | |
d8906c6f | 193 | } |
07ca107c DE |
194 | else |
195 | error ("%s", msg); | |
d8906c6f | 196 | } |
07ca107c | 197 | |
d8906c6f TJB |
198 | Py_DECREF (result); |
199 | do_cleanups (cleanup); | |
200 | } | |
201 | ||
202 | /* Called by gdb for command completion. */ | |
203 | static char ** | |
204 | cmdpy_completer (struct cmd_list_element *command, char *text, char *word) | |
205 | { | |
206 | cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command); | |
207 | PyObject *textobj, *wordobj, *resultobj = NULL; | |
208 | char **result = NULL; | |
209 | struct cleanup *cleanup; | |
d8906c6f | 210 | |
d452c4bc | 211 | cleanup = ensure_python_env (get_current_arch (), current_language); |
d8906c6f TJB |
212 | |
213 | if (! obj) | |
214 | error (_("Invalid invocation of Python command object.")); | |
215 | if (! PyObject_HasAttr ((PyObject *) obj, complete_cst)) | |
216 | { | |
217 | /* If there is no complete method, don't error -- instead, just | |
218 | say that there are no completions. */ | |
219 | goto done; | |
220 | } | |
221 | ||
222 | textobj = PyUnicode_Decode (text, strlen (text), host_charset (), NULL); | |
223 | if (! textobj) | |
224 | error (_("Could not convert argument to Python string.")); | |
225 | wordobj = PyUnicode_Decode (word, strlen (word), host_charset (), NULL); | |
226 | if (! wordobj) | |
227 | error (_("Could not convert argument to Python string.")); | |
228 | ||
229 | resultobj = PyObject_CallMethodObjArgs ((PyObject *) obj, complete_cst, | |
230 | textobj, wordobj, NULL); | |
231 | Py_DECREF (textobj); | |
232 | Py_DECREF (wordobj); | |
233 | if (! resultobj) | |
234 | { | |
235 | /* Just swallow errors here. */ | |
236 | PyErr_Clear (); | |
237 | goto done; | |
238 | } | |
239 | make_cleanup_py_decref (resultobj); | |
240 | ||
241 | result = NULL; | |
242 | if (PySequence_Check (resultobj)) | |
243 | { | |
244 | Py_ssize_t i, len = PySequence_Size (resultobj); | |
245 | Py_ssize_t out; | |
d59b6f6c | 246 | |
d8906c6f TJB |
247 | if (len < 0) |
248 | goto done; | |
249 | ||
250 | result = (char **) xmalloc ((len + 1) * sizeof (char *)); | |
251 | for (i = out = 0; i < len; ++i) | |
252 | { | |
d8906c6f | 253 | PyObject *elt = PySequence_GetItem (resultobj, i); |
d59b6f6c | 254 | |
d8906c6f TJB |
255 | if (elt == NULL || ! gdbpy_is_string (elt)) |
256 | { | |
257 | /* Skip problem elements. */ | |
258 | PyErr_Clear (); | |
259 | continue; | |
260 | } | |
261 | result[out] = python_string_to_host_string (elt); | |
8dc78533 JK |
262 | if (result[out] == NULL) |
263 | { | |
264 | /* Skip problem elements. */ | |
265 | PyErr_Clear (); | |
266 | continue; | |
267 | } | |
d8906c6f TJB |
268 | ++out; |
269 | } | |
270 | result[out] = NULL; | |
271 | } | |
272 | else if (PyInt_Check (resultobj)) | |
273 | { | |
274 | /* User code may also return one of the completion constants, | |
275 | thus requesting that sort of completion. */ | |
276 | long value = PyInt_AsLong (resultobj); | |
d59b6f6c | 277 | |
d8906c6f TJB |
278 | if (value >= 0 && value < (long) N_COMPLETERS) |
279 | result = completers[value].completer (command, text, word); | |
280 | } | |
281 | ||
282 | done: | |
283 | ||
284 | do_cleanups (cleanup); | |
285 | ||
286 | return result; | |
287 | } | |
288 | ||
289 | /* Helper for cmdpy_init which locates the command list to use and | |
290 | pulls out the command name. | |
291 | ||
292 | TEXT is the command name list. The final word in the list is the | |
293 | name of the new command. All earlier words must be existing prefix | |
294 | commands. | |
295 | ||
296 | *BASE_LIST is set to the final prefix command's list of | |
297 | *sub-commands. | |
298 | ||
d7b32ed3 PM |
299 | START_LIST is the list in which the search starts. |
300 | ||
d8906c6f TJB |
301 | This function returns the xmalloc()d name of the new command. On |
302 | error sets the Python error and returns NULL. */ | |
d7b32ed3 PM |
303 | char * |
304 | gdbpy_parse_command_name (char *text, | |
305 | struct cmd_list_element ***base_list, | |
306 | struct cmd_list_element **start_list) | |
d8906c6f TJB |
307 | { |
308 | struct cmd_list_element *elt; | |
309 | int len = strlen (text); | |
310 | int i, lastchar; | |
311 | char *prefix_text; | |
312 | char *result; | |
313 | ||
314 | /* Skip trailing whitespace. */ | |
315 | for (i = len - 1; i >= 0 && (text[i] == ' ' || text[i] == '\t'); --i) | |
316 | ; | |
317 | if (i < 0) | |
318 | { | |
044c0f87 | 319 | PyErr_SetString (PyExc_RuntimeError, _("No command name found.")); |
d8906c6f TJB |
320 | return NULL; |
321 | } | |
322 | lastchar = i; | |
323 | ||
324 | /* Find first character of the final word. */ | |
325 | for (; i > 0 && (isalnum (text[i - 1]) | |
326 | || text[i - 1] == '-' | |
327 | || text[i - 1] == '_'); | |
328 | --i) | |
329 | ; | |
330 | result = xmalloc (lastchar - i + 2); | |
331 | memcpy (result, &text[i], lastchar - i + 1); | |
332 | result[lastchar - i + 1] = '\0'; | |
333 | ||
334 | /* Skip whitespace again. */ | |
335 | for (--i; i >= 0 && (text[i] == ' ' || text[i] == '\t'); --i) | |
336 | ; | |
337 | if (i < 0) | |
338 | { | |
d7b32ed3 | 339 | *base_list = start_list; |
d8906c6f TJB |
340 | return result; |
341 | } | |
342 | ||
343 | prefix_text = xmalloc (i + 2); | |
344 | memcpy (prefix_text, text, i + 1); | |
345 | prefix_text[i + 1] = '\0'; | |
346 | ||
347 | text = prefix_text; | |
d7b32ed3 | 348 | elt = lookup_cmd_1 (&text, *start_list, NULL, 1); |
d8906c6f TJB |
349 | if (!elt || elt == (struct cmd_list_element *) -1) |
350 | { | |
044c0f87 | 351 | PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."), |
d8906c6f TJB |
352 | prefix_text); |
353 | xfree (prefix_text); | |
354 | xfree (result); | |
355 | return NULL; | |
356 | } | |
357 | ||
358 | if (elt->prefixlist) | |
359 | { | |
360 | xfree (prefix_text); | |
361 | *base_list = elt->prefixlist; | |
362 | return result; | |
363 | } | |
364 | ||
044c0f87 | 365 | PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."), |
d8906c6f TJB |
366 | prefix_text); |
367 | xfree (prefix_text); | |
368 | xfree (result); | |
369 | return NULL; | |
370 | } | |
371 | ||
372 | /* Object initializer; sets up gdb-side structures for command. | |
373 | ||
cc924cad | 374 | Use: __init__(NAME, COMMAND_CLASS [, COMPLETER_CLASS][, PREFIX]]). |
d8906c6f TJB |
375 | |
376 | NAME is the name of the command. It may consist of multiple words, | |
377 | in which case the final word is the name of the new command, and | |
378 | earlier words must be prefix commands. | |
379 | ||
cc924cad | 380 | COMMAND_CLASS is the kind of command. It should be one of the COMMAND_* |
d8906c6f TJB |
381 | constants defined in the gdb module. |
382 | ||
cc924cad | 383 | COMPLETER_CLASS is the kind of completer. If not given, the |
d8906c6f TJB |
384 | "complete" method will be used. Otherwise, it should be one of the |
385 | COMPLETE_* constants defined in the gdb module. | |
386 | ||
387 | If PREFIX is True, then this command is a prefix command. | |
388 | ||
389 | The documentation for the command is taken from the doc string for | |
390 | the python class. | |
391 | ||
392 | */ | |
393 | static int | |
cc924cad | 394 | cmdpy_init (PyObject *self, PyObject *args, PyObject *kw) |
d8906c6f TJB |
395 | { |
396 | cmdpy_object *obj = (cmdpy_object *) self; | |
397 | char *name; | |
398 | int cmdtype; | |
399 | int completetype = -1; | |
400 | char *docstring = NULL; | |
401 | volatile struct gdb_exception except; | |
402 | struct cmd_list_element **cmd_list; | |
403 | char *cmd_name, *pfx_name; | |
cc924cad TJB |
404 | static char *keywords[] = { "name", "command_class", "completer_class", |
405 | "prefix", NULL }; | |
d8906c6f TJB |
406 | PyObject *is_prefix = NULL; |
407 | int cmp; | |
408 | ||
409 | if (obj->command) | |
410 | { | |
411 | /* Note: this is apparently not documented in Python. We return | |
412 | 0 for success, -1 for failure. */ | |
413 | PyErr_Format (PyExc_RuntimeError, | |
044c0f87 | 414 | _("Command object already initialized.")); |
d8906c6f TJB |
415 | return -1; |
416 | } | |
417 | ||
cc924cad | 418 | if (! PyArg_ParseTupleAndKeywords (args, kw, "si|iO", keywords, &name, &cmdtype, |
d8906c6f TJB |
419 | &completetype, &is_prefix)) |
420 | return -1; | |
421 | ||
422 | if (cmdtype != no_class && cmdtype != class_run | |
423 | && cmdtype != class_vars && cmdtype != class_stack | |
424 | && cmdtype != class_files && cmdtype != class_support | |
425 | && cmdtype != class_info && cmdtype != class_breakpoint | |
426 | && cmdtype != class_trace && cmdtype != class_obscure | |
427 | && cmdtype != class_maintenance) | |
428 | { | |
044c0f87 | 429 | PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument.")); |
d8906c6f TJB |
430 | return -1; |
431 | } | |
432 | ||
433 | if (completetype < -1 || completetype >= (int) N_COMPLETERS) | |
434 | { | |
044c0f87 | 435 | PyErr_Format (PyExc_RuntimeError, _("Invalid completion type argument.")); |
d8906c6f TJB |
436 | return -1; |
437 | } | |
438 | ||
d7b32ed3 | 439 | cmd_name = gdbpy_parse_command_name (name, &cmd_list, &cmdlist); |
d8906c6f TJB |
440 | if (! cmd_name) |
441 | return -1; | |
442 | ||
443 | pfx_name = NULL; | |
444 | if (is_prefix != NULL) | |
445 | { | |
446 | cmp = PyObject_IsTrue (is_prefix); | |
447 | if (cmp == 1) | |
448 | { | |
449 | int i, out; | |
450 | ||
451 | /* Make a normalized form of the command name. */ | |
452 | pfx_name = xmalloc (strlen (name) + 2); | |
453 | ||
454 | i = 0; | |
455 | out = 0; | |
456 | while (name[i]) | |
457 | { | |
458 | /* Skip whitespace. */ | |
459 | while (name[i] == ' ' || name[i] == '\t') | |
460 | ++i; | |
461 | /* Copy non-whitespace characters. */ | |
462 | while (name[i] && name[i] != ' ' && name[i] != '\t') | |
463 | pfx_name[out++] = name[i++]; | |
464 | /* Add a single space after each word -- including the final | |
465 | word. */ | |
466 | pfx_name[out++] = ' '; | |
467 | } | |
468 | pfx_name[out] = '\0'; | |
469 | } | |
470 | else if (cmp < 0) | |
471 | return -1; | |
472 | } | |
473 | if (PyObject_HasAttr (self, gdbpy_doc_cst)) | |
474 | { | |
475 | PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst); | |
d59b6f6c | 476 | |
d8906c6f | 477 | if (ds_obj && gdbpy_is_string (ds_obj)) |
8dc78533 JK |
478 | { |
479 | docstring = python_string_to_host_string (ds_obj); | |
480 | if (docstring == NULL) | |
481 | { | |
482 | xfree (cmd_name); | |
483 | xfree (pfx_name); | |
484 | return -1; | |
485 | } | |
486 | } | |
d8906c6f TJB |
487 | } |
488 | if (! docstring) | |
489 | docstring = xstrdup (_("This command is not documented.")); | |
490 | ||
491 | Py_INCREF (self); | |
492 | ||
493 | TRY_CATCH (except, RETURN_MASK_ALL) | |
494 | { | |
495 | struct cmd_list_element *cmd; | |
496 | ||
497 | if (pfx_name) | |
498 | { | |
499 | int allow_unknown; | |
500 | ||
501 | /* If we have our own "invoke" method, then allow unknown | |
502 | sub-commands. */ | |
503 | allow_unknown = PyObject_HasAttr (self, invoke_cst); | |
504 | cmd = add_prefix_cmd (cmd_name, (enum command_class) cmdtype, | |
505 | NULL, docstring, &obj->sub_list, | |
506 | pfx_name, allow_unknown, cmd_list); | |
507 | } | |
508 | else | |
509 | cmd = add_cmd (cmd_name, (enum command_class) cmdtype, NULL, | |
510 | docstring, cmd_list); | |
511 | ||
512 | /* There appears to be no API to set this. */ | |
513 | cmd->func = cmdpy_function; | |
514 | cmd->destroyer = cmdpy_destroyer; | |
515 | ||
516 | obj->command = cmd; | |
517 | set_cmd_context (cmd, self); | |
518 | set_cmd_completer (cmd, ((completetype == -1) ? cmdpy_completer | |
519 | : completers[completetype].completer)); | |
520 | } | |
521 | if (except.reason < 0) | |
522 | { | |
523 | xfree (cmd_name); | |
524 | xfree (docstring); | |
525 | xfree (pfx_name); | |
526 | Py_DECREF (self); | |
527 | PyErr_Format (except.reason == RETURN_QUIT | |
528 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, | |
529 | "%s", except.message); | |
530 | return -1; | |
531 | } | |
532 | return 0; | |
533 | } | |
534 | ||
535 | \f | |
536 | ||
537 | /* Initialize the 'commands' code. */ | |
538 | void | |
539 | gdbpy_initialize_commands (void) | |
540 | { | |
541 | int i; | |
542 | ||
543 | if (PyType_Ready (&cmdpy_object_type) < 0) | |
544 | return; | |
545 | ||
546 | /* Note: alias and user are special; pseudo appears to be unused, | |
547 | and there is no reason to expose tui or xdb, I think. */ | |
548 | if (PyModule_AddIntConstant (gdb_module, "COMMAND_NONE", no_class) < 0 | |
549 | || PyModule_AddIntConstant (gdb_module, "COMMAND_RUNNING", class_run) < 0 | |
550 | || PyModule_AddIntConstant (gdb_module, "COMMAND_DATA", class_vars) < 0 | |
551 | || PyModule_AddIntConstant (gdb_module, "COMMAND_STACK", class_stack) < 0 | |
552 | || PyModule_AddIntConstant (gdb_module, "COMMAND_FILES", class_files) < 0 | |
553 | || PyModule_AddIntConstant (gdb_module, "COMMAND_SUPPORT", | |
554 | class_support) < 0 | |
555 | || PyModule_AddIntConstant (gdb_module, "COMMAND_STATUS", class_info) < 0 | |
556 | || PyModule_AddIntConstant (gdb_module, "COMMAND_BREAKPOINTS", | |
557 | class_breakpoint) < 0 | |
558 | || PyModule_AddIntConstant (gdb_module, "COMMAND_TRACEPOINTS", | |
559 | class_trace) < 0 | |
560 | || PyModule_AddIntConstant (gdb_module, "COMMAND_OBSCURE", | |
561 | class_obscure) < 0 | |
562 | || PyModule_AddIntConstant (gdb_module, "COMMAND_MAINTENANCE", | |
563 | class_maintenance) < 0) | |
564 | return; | |
565 | ||
566 | for (i = 0; i < N_COMPLETERS; ++i) | |
567 | { | |
568 | if (PyModule_AddIntConstant (gdb_module, completers[i].name, i) < 0) | |
569 | return; | |
570 | } | |
571 | ||
572 | Py_INCREF (&cmdpy_object_type); | |
573 | PyModule_AddObject (gdb_module, "Command", | |
574 | (PyObject *) &cmdpy_object_type); | |
575 | ||
576 | invoke_cst = PyString_FromString ("invoke"); | |
577 | complete_cst = PyString_FromString ("complete"); | |
578 | } | |
579 | ||
580 | \f | |
581 | ||
582 | static PyMethodDef cmdpy_object_methods[] = | |
583 | { | |
584 | { "dont_repeat", cmdpy_dont_repeat, METH_NOARGS, | |
585 | "Prevent command repetition when user enters empty line." }, | |
586 | ||
587 | { 0 } | |
588 | }; | |
589 | ||
590 | static PyTypeObject cmdpy_object_type = | |
591 | { | |
592 | PyObject_HEAD_INIT (NULL) | |
593 | 0, /*ob_size*/ | |
594 | "gdb.Command", /*tp_name*/ | |
595 | sizeof (cmdpy_object), /*tp_basicsize*/ | |
596 | 0, /*tp_itemsize*/ | |
597 | 0, /*tp_dealloc*/ | |
598 | 0, /*tp_print*/ | |
599 | 0, /*tp_getattr*/ | |
600 | 0, /*tp_setattr*/ | |
601 | 0, /*tp_compare*/ | |
602 | 0, /*tp_repr*/ | |
603 | 0, /*tp_as_number*/ | |
604 | 0, /*tp_as_sequence*/ | |
605 | 0, /*tp_as_mapping*/ | |
606 | 0, /*tp_hash */ | |
607 | 0, /*tp_call*/ | |
608 | 0, /*tp_str*/ | |
609 | 0, /*tp_getattro*/ | |
610 | 0, /*tp_setattro*/ | |
611 | 0, /*tp_as_buffer*/ | |
612 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
613 | "GDB command object", /* tp_doc */ | |
614 | 0, /* tp_traverse */ | |
615 | 0, /* tp_clear */ | |
616 | 0, /* tp_richcompare */ | |
617 | 0, /* tp_weaklistoffset */ | |
618 | 0, /* tp_iter */ | |
619 | 0, /* tp_iternext */ | |
620 | cmdpy_object_methods, /* tp_methods */ | |
621 | 0, /* tp_members */ | |
622 | 0, /* tp_getset */ | |
623 | 0, /* tp_base */ | |
624 | 0, /* tp_dict */ | |
625 | 0, /* tp_descr_get */ | |
626 | 0, /* tp_descr_set */ | |
627 | 0, /* tp_dictoffset */ | |
628 | cmdpy_init, /* tp_init */ | |
629 | 0, /* tp_alloc */ | |
630 | PyType_GenericNew /* tp_new */ | |
631 | }; | |
07ca107c DE |
632 | |
633 | \f | |
634 | ||
635 | /* Utility to build a buildargv-like result from ARGS. | |
636 | This intentionally parses arguments the way libiberty/argv.c:buildargv | |
637 | does. It splits up arguments in a reasonable way, and we want a standard | |
638 | way of parsing arguments. Several gdb commands use buildargv to parse their | |
639 | arguments. Plus we want to be able to write compatible python | |
640 | implementations of gdb commands. */ | |
641 | ||
642 | PyObject * | |
643 | gdbpy_string_to_argv (PyObject *self, PyObject *args) | |
644 | { | |
645 | PyObject *py_argv; | |
646 | char *input; | |
647 | ||
648 | if (!PyArg_ParseTuple (args, "s", &input)) | |
649 | return NULL; | |
650 | ||
651 | py_argv = PyList_New (0); | |
652 | ||
653 | /* buildargv uses NULL to represent an empty argument list, but we can't use | |
654 | that in Python. Instead, if ARGS is "" then return an empty list. | |
655 | This undoes the NULL -> "" conversion that cmdpy_function does. */ | |
656 | ||
657 | if (*input != '\0') | |
658 | { | |
659 | char **c_argv = gdb_buildargv (input); | |
660 | int i; | |
661 | ||
662 | for (i = 0; c_argv[i] != NULL; ++i) | |
663 | { | |
664 | PyObject *argp = PyString_FromString (c_argv[i]); | |
665 | ||
666 | if (argp == NULL | |
667 | || PyList_Append (py_argv, argp) < 0) | |
668 | { | |
669 | if (argp != NULL) | |
670 | { | |
671 | Py_DECREF (argp); | |
672 | } | |
673 | Py_DECREF (py_argv); | |
674 | freeargv (c_argv); | |
675 | return NULL; | |
676 | } | |
677 | } | |
678 | ||
679 | freeargv (c_argv); | |
680 | } | |
681 | ||
682 | return py_argv; | |
683 | } |