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