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