py-cmd.c revision 1.8 1 1.1 christos /* gdb commands implemented in Python
2 1.1 christos
3 1.8 christos Copyright (C) 2008-2019 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos
21 1.1 christos #include "defs.h"
22 1.1 christos #include "arch-utils.h"
23 1.1 christos #include "value.h"
24 1.1 christos #include "python-internal.h"
25 1.1 christos #include "charset.h"
26 1.1 christos #include "gdbcmd.h"
27 1.1 christos #include "cli/cli-decode.h"
28 1.1 christos #include "completer.h"
29 1.1 christos #include "language.h"
30 1.1 christos
31 1.1 christos /* Struct representing built-in completion types. */
32 1.1 christos struct cmdpy_completer
33 1.1 christos {
34 1.7 christos /* Python symbol name. */
35 1.7 christos const char *name;
36 1.1 christos /* Completion function. */
37 1.1 christos completer_ftype *completer;
38 1.1 christos };
39 1.1 christos
40 1.3 christos static const struct cmdpy_completer completers[] =
41 1.1 christos {
42 1.1 christos { "COMPLETE_NONE", noop_completer },
43 1.1 christos { "COMPLETE_FILENAME", filename_completer },
44 1.1 christos { "COMPLETE_LOCATION", location_completer },
45 1.1 christos { "COMPLETE_COMMAND", command_completer },
46 1.8 christos { "COMPLETE_SYMBOL", symbol_completer },
47 1.1 christos { "COMPLETE_EXPRESSION", expression_completer },
48 1.1 christos };
49 1.1 christos
50 1.1 christos #define N_COMPLETERS (sizeof (completers) / sizeof (completers[0]))
51 1.1 christos
52 1.1 christos /* A gdb command. For the time being only ordinary commands (not
53 1.1 christos set/show commands) are allowed. */
54 1.1 christos struct cmdpy_object
55 1.1 christos {
56 1.1 christos PyObject_HEAD
57 1.1 christos
58 1.1 christos /* The corresponding gdb command object, or NULL if the command is
59 1.1 christos no longer installed. */
60 1.1 christos struct cmd_list_element *command;
61 1.1 christos
62 1.1 christos /* A prefix command requires storage for a list of its sub-commands.
63 1.1 christos A pointer to this is passed to add_prefix_command, and to add_cmd
64 1.1 christos for sub-commands of that prefix. If this Command is not a prefix
65 1.1 christos command, then this field is unused. */
66 1.1 christos struct cmd_list_element *sub_list;
67 1.1 christos };
68 1.1 christos
69 1.1 christos typedef struct cmdpy_object cmdpy_object;
70 1.1 christos
71 1.5 christos extern PyTypeObject cmdpy_object_type
72 1.1 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("cmdpy_object");
73 1.1 christos
74 1.1 christos /* Constants used by this module. */
75 1.1 christos static PyObject *invoke_cst;
76 1.1 christos static PyObject *complete_cst;
77 1.1 christos
78 1.1 christos
79 1.1 christos
81 1.1 christos /* Python function which wraps dont_repeat. */
82 1.1 christos static PyObject *
83 1.1 christos cmdpy_dont_repeat (PyObject *self, PyObject *args)
84 1.1 christos {
85 1.1 christos dont_repeat ();
86 1.1 christos Py_RETURN_NONE;
87 1.1 christos }
88 1.1 christos
89 1.1 christos
90 1.1 christos
92 1.1 christos /* Called if the gdb cmd_list_element is destroyed. */
93 1.1 christos
94 1.1 christos static void
95 1.7 christos cmdpy_destroyer (struct cmd_list_element *self, void *context)
96 1.1 christos {
97 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
98 1.7 christos
99 1.1 christos /* Release our hold on the command object. */
100 1.1 christos gdbpy_ref<cmdpy_object> cmd ((cmdpy_object *) context);
101 1.1 christos cmd->command = NULL;
102 1.1 christos
103 1.1 christos /* We allocated the name, doc string, and perhaps the prefix
104 1.3 christos name. */
105 1.3 christos xfree ((char *) self->name);
106 1.1 christos xfree ((char *) self->doc);
107 1.1 christos xfree ((char *) self->prefixname);
108 1.1 christos }
109 1.1 christos
110 1.1 christos /* Called by gdb to invoke the command. */
111 1.7 christos
112 1.8 christos static void
113 1.1 christos cmdpy_function (struct cmd_list_element *command,
114 1.1 christos const char *args, int from_tty)
115 1.1 christos {
116 1.7 christos cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
117 1.1 christos
118 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
119 1.1 christos
120 1.1 christos if (! obj)
121 1.1 christos error (_("Invalid invocation of Python command object."));
122 1.1 christos if (! PyObject_HasAttr ((PyObject *) obj, invoke_cst))
123 1.1 christos {
124 1.1 christos if (obj->command->prefixname)
125 1.1 christos {
126 1.1 christos /* A prefix command does not need an invoke method. */
127 1.1 christos return;
128 1.1 christos }
129 1.1 christos error (_("Python command object missing 'invoke' method."));
130 1.1 christos }
131 1.1 christos
132 1.7 christos if (! args)
133 1.7 christos args = "";
134 1.7 christos gdbpy_ref<> argobj (PyUnicode_Decode (args, strlen (args), host_charset (),
135 1.1 christos NULL));
136 1.1 christos if (argobj == NULL)
137 1.1 christos {
138 1.1 christos gdbpy_print_stack ();
139 1.1 christos error (_("Could not convert arguments to Python string."));
140 1.8 christos }
141 1.8 christos
142 1.7 christos gdbpy_ref<> ttyobj
143 1.7 christos = gdbpy_ref<>::new_reference (from_tty ? Py_True : Py_False);
144 1.7 christos gdbpy_ref<> result (PyObject_CallMethodObjArgs ((PyObject *) obj, invoke_cst,
145 1.1 christos argobj.get (), ttyobj.get (),
146 1.7 christos NULL));
147 1.8 christos
148 1.1 christos if (result == NULL)
149 1.1 christos gdbpy_handle_exception ();
150 1.3 christos }
151 1.3 christos
152 1.3 christos /* Helper function for the Python command completers (both "pure"
153 1.5 christos completer and brkchar handler). This function takes COMMAND, TEXT
154 1.5 christos and WORD and tries to call the Python method for completion with
155 1.5 christos these arguments.
156 1.5 christos
157 1.5 christos This function is usually called twice: once when we are figuring out
158 1.5 christos the break characters to be used, and another to perform the real
159 1.5 christos completion itself. The reason for this two step dance is that we
160 1.5 christos need to know the set of "brkchars" to use early on, before we
161 1.5 christos actually try to perform the completion. But if a Python command
162 1.5 christos supplies a "complete" method then we have to call that method
163 1.5 christos first: it may return as its result the kind of completion to
164 1.5 christos perform and that will in turn specify which brkchars to use. IOW,
165 1.5 christos we need the result of the "complete" method before we actually
166 1.5 christos perform the completion. The only situation when this function is
167 1.5 christos not called twice is when the user uses the "complete" command: in
168 1.5 christos this scenario, there is no call to determine the "brkchars".
169 1.5 christos
170 1.5 christos Ideally, it would be nice to cache the result of the first call (to
171 1.5 christos determine the "brkchars") and return this value directly in the
172 1.5 christos second call (to perform the actual completion). However, due to
173 1.5 christos the peculiarity of the "complete" command mentioned above, it is
174 1.5 christos possible to put GDB in a bad state if you perform a TAB-completion
175 1.3 christos and then a "complete"-completion sequentially. Therefore, we just
176 1.8 christos recalculate everything twice for TAB-completions.
177 1.8 christos
178 1.3 christos This function returns a reference to the PyObject representing the
179 1.8 christos Python method call. */
180 1.3 christos
181 1.5 christos static gdbpy_ref<>
182 1.3 christos cmdpy_completer_helper (struct cmd_list_element *command,
183 1.3 christos const char *text, const char *word)
184 1.3 christos {
185 1.5 christos cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
186 1.5 christos
187 1.5 christos if (obj == NULL)
188 1.3 christos error (_("Invalid invocation of Python command object."));
189 1.5 christos if (!PyObject_HasAttr ((PyObject *) obj, complete_cst))
190 1.5 christos {
191 1.5 christos /* If there is no complete method, don't error. */
192 1.3 christos return NULL;
193 1.7 christos }
194 1.7 christos
195 1.5 christos gdbpy_ref<> textobj (PyUnicode_Decode (text, strlen (text), host_charset (),
196 1.5 christos NULL));
197 1.8 christos if (textobj == NULL)
198 1.8 christos error (_("Could not convert argument to Python string."));
199 1.8 christos
200 1.8 christos gdbpy_ref<> wordobj;
201 1.8 christos if (word == NULL)
202 1.8 christos {
203 1.8 christos /* "brkchars" phase. */
204 1.8 christos wordobj = gdbpy_ref<>::new_reference (Py_None);
205 1.8 christos }
206 1.8 christos else
207 1.8 christos {
208 1.8 christos wordobj.reset (PyUnicode_Decode (word, strlen (word), host_charset (),
209 1.8 christos NULL));
210 1.8 christos if (wordobj == NULL)
211 1.3 christos error (_("Could not convert argument to Python string."));
212 1.7 christos }
213 1.7 christos
214 1.7 christos gdbpy_ref<> resultobj (PyObject_CallMethodObjArgs ((PyObject *) obj,
215 1.7 christos complete_cst,
216 1.7 christos textobj.get (),
217 1.5 christos wordobj.get (), NULL));
218 1.5 christos if (resultobj == NULL)
219 1.5 christos {
220 1.3 christos /* Just swallow errors here. */
221 1.3 christos PyErr_Clear ();
222 1.8 christos }
223 1.3 christos
224 1.3 christos return resultobj;
225 1.3 christos }
226 1.3 christos
227 1.3 christos /* Python function called to determine the break characters of a
228 1.3 christos certain completer. We are only interested in knowing if the
229 1.3 christos completer registered by the user will return one of the integer
230 1.3 christos codes (see COMPLETER_* symbols). */
231 1.3 christos
232 1.8 christos static void
233 1.3 christos cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
234 1.3 christos completion_tracker &tracker,
235 1.7 christos const char *text, const char *word)
236 1.3 christos {
237 1.8 christos gdbpy_enter enter_py (get_current_arch (), current_language);
238 1.3 christos
239 1.8 christos /* Calling our helper to obtain a reference to the PyObject of the Python
240 1.3 christos function. */
241 1.3 christos gdbpy_ref<> resultobj = cmdpy_completer_helper (command, text, word);
242 1.3 christos
243 1.7 christos /* Check if there was an error. */
244 1.3 christos if (resultobj == NULL)
245 1.7 christos return;
246 1.3 christos
247 1.3 christos if (PyInt_Check (resultobj.get ()))
248 1.3 christos {
249 1.3 christos /* User code may also return one of the completion constants,
250 1.3 christos thus requesting that sort of completion. We are only
251 1.3 christos interested in this kind of return. */
252 1.7 christos long value;
253 1.3 christos
254 1.3 christos if (!gdb_py_int_as_long (resultobj.get (), &value))
255 1.3 christos {
256 1.3 christos /* Ignore. */
257 1.3 christos PyErr_Clear ();
258 1.3 christos }
259 1.8 christos else if (value >= 0 && value < (long) N_COMPLETERS)
260 1.8 christos {
261 1.3 christos completer_handle_brkchars_ftype *brkchars_fn;
262 1.3 christos
263 1.3 christos /* This is the core of this function. Depending on which
264 1.8 christos completer type the Python function returns, we have to
265 1.8 christos adjust the break characters accordingly. */
266 1.8 christos brkchars_fn = (completer_handle_brkchars_func_for_completer
267 1.3 christos (completers[value].completer));
268 1.3 christos brkchars_fn (command, tracker, text, word);
269 1.3 christos }
270 1.3 christos }
271 1.1 christos }
272 1.1 christos
273 1.8 christos /* Called by gdb for command completion. */
274 1.1 christos
275 1.8 christos static void
276 1.1 christos cmdpy_completer (struct cmd_list_element *command,
277 1.1 christos completion_tracker &tracker,
278 1.7 christos const char *text, const char *word)
279 1.1 christos {
280 1.8 christos gdbpy_enter enter_py (get_current_arch (), current_language);
281 1.3 christos
282 1.8 christos /* Calling our helper to obtain a reference to the PyObject of the Python
283 1.3 christos function. */
284 1.3 christos gdbpy_ref<> resultobj = cmdpy_completer_helper (command, text, word);
285 1.8 christos
286 1.3 christos /* If the result object of calling the Python function is NULL, it
287 1.8 christos means that there was an error. In this case, just give up. */
288 1.1 christos if (resultobj == NULL)
289 1.7 christos return;
290 1.1 christos
291 1.1 christos if (PyInt_Check (resultobj.get ()))
292 1.1 christos {
293 1.1 christos /* User code may also return one of the completion constants,
294 1.1 christos thus requesting that sort of completion. */
295 1.7 christos long value;
296 1.1 christos
297 1.1 christos if (! gdb_py_int_as_long (resultobj.get (), &value))
298 1.1 christos {
299 1.1 christos /* Ignore. */
300 1.1 christos PyErr_Clear ();
301 1.8 christos }
302 1.1 christos else if (value >= 0 && value < (long) N_COMPLETERS)
303 1.1 christos completers[value].completer (command, tracker, text, word);
304 1.1 christos }
305 1.7 christos else
306 1.1 christos {
307 1.1 christos gdbpy_ref<> iter (PyObject_GetIter (resultobj.get ()));
308 1.8 christos
309 1.1 christos if (iter == NULL)
310 1.8 christos return;
311 1.7 christos
312 1.1 christos bool got_matches = false;
313 1.7 christos while (true)
314 1.7 christos {
315 1.7 christos gdbpy_ref<> elt (PyIter_Next (iter.get ()));
316 1.1 christos if (elt == NULL)
317 1.7 christos break;
318 1.1 christos
319 1.1 christos if (! gdbpy_is_string (elt.get ()))
320 1.1 christos {
321 1.1 christos /* Skip problem elements. */
322 1.7 christos continue;
323 1.7 christos }
324 1.1 christos gdb::unique_xmalloc_ptr<char>
325 1.1 christos item (python_string_to_host_string (elt.get ()));
326 1.1 christos if (item == NULL)
327 1.1 christos {
328 1.1 christos /* Skip problem elements. */
329 1.1 christos PyErr_Clear ();
330 1.8 christos continue;
331 1.8 christos }
332 1.1 christos tracker.add_completion (std::move (item));
333 1.1 christos got_matches = true;
334 1.1 christos }
335 1.1 christos
336 1.8 christos /* If we got some results, ignore problems. Otherwise, report
337 1.1 christos the problem. */
338 1.1 christos if (got_matches && PyErr_Occurred ())
339 1.1 christos PyErr_Clear ();
340 1.1 christos }
341 1.1 christos }
342 1.1 christos
343 1.1 christos /* Helper for cmdpy_init which locates the command list to use and
344 1.1 christos pulls out the command name.
345 1.1 christos
346 1.1 christos NAME is the command name list. The final word in the list is the
347 1.1 christos name of the new command. All earlier words must be existing prefix
348 1.1 christos commands.
349 1.1 christos
350 1.1 christos *BASE_LIST is set to the final prefix command's list of
351 1.1 christos *sub-commands.
352 1.1 christos
353 1.1 christos START_LIST is the list in which the search starts.
354 1.1 christos
355 1.1 christos This function returns the xmalloc()d name of the new command. On
356 1.1 christos error sets the Python error and returns NULL. */
357 1.1 christos
358 1.1 christos char *
359 1.1 christos gdbpy_parse_command_name (const char *name,
360 1.1 christos struct cmd_list_element ***base_list,
361 1.1 christos struct cmd_list_element **start_list)
362 1.1 christos {
363 1.1 christos struct cmd_list_element *elt;
364 1.1 christos int len = strlen (name);
365 1.1 christos int i, lastchar;
366 1.1 christos const char *prefix_text2;
367 1.1 christos char *result;
368 1.1 christos
369 1.1 christos /* Skip trailing whitespace. */
370 1.1 christos for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
371 1.1 christos ;
372 1.1 christos if (i < 0)
373 1.1 christos {
374 1.1 christos PyErr_SetString (PyExc_RuntimeError, _("No command name found."));
375 1.1 christos return NULL;
376 1.1 christos }
377 1.1 christos lastchar = i;
378 1.1 christos
379 1.1 christos /* Find first character of the final word. */
380 1.1 christos for (; i > 0 && (isalnum (name[i - 1])
381 1.1 christos || name[i - 1] == '-'
382 1.1 christos || name[i - 1] == '_');
383 1.6 christos --i)
384 1.1 christos ;
385 1.1 christos result = (char *) xmalloc (lastchar - i + 2);
386 1.1 christos memcpy (result, &name[i], lastchar - i + 1);
387 1.1 christos result[lastchar - i + 1] = '\0';
388 1.1 christos
389 1.1 christos /* Skip whitespace again. */
390 1.1 christos for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
391 1.1 christos ;
392 1.1 christos if (i < 0)
393 1.1 christos {
394 1.1 christos *base_list = start_list;
395 1.1 christos return result;
396 1.8 christos }
397 1.1 christos
398 1.8 christos std::string prefix_text (name, i + 1);
399 1.1 christos
400 1.3 christos prefix_text2 = prefix_text.c_str ();
401 1.1 christos elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, 1);
402 1.1 christos if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
403 1.8 christos {
404 1.1 christos PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."),
405 1.1 christos prefix_text.c_str ());
406 1.1 christos xfree (result);
407 1.1 christos return NULL;
408 1.1 christos }
409 1.1 christos
410 1.1 christos if (elt->prefixlist)
411 1.1 christos {
412 1.1 christos *base_list = elt->prefixlist;
413 1.1 christos return result;
414 1.1 christos }
415 1.8 christos
416 1.1 christos PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."),
417 1.1 christos prefix_text.c_str ());
418 1.1 christos xfree (result);
419 1.1 christos return NULL;
420 1.1 christos }
421 1.1 christos
422 1.1 christos /* Object initializer; sets up gdb-side structures for command.
423 1.1 christos
424 1.1 christos Use: __init__(NAME, COMMAND_CLASS [, COMPLETER_CLASS][, PREFIX]]).
425 1.1 christos
426 1.1 christos NAME is the name of the command. It may consist of multiple words,
427 1.1 christos in which case the final word is the name of the new command, and
428 1.1 christos earlier words must be prefix commands.
429 1.1 christos
430 1.1 christos COMMAND_CLASS is the kind of command. It should be one of the COMMAND_*
431 1.1 christos constants defined in the gdb module.
432 1.1 christos
433 1.1 christos COMPLETER_CLASS is the kind of completer. If not given, the
434 1.1 christos "complete" method will be used. Otherwise, it should be one of the
435 1.1 christos COMPLETE_* constants defined in the gdb module.
436 1.1 christos
437 1.1 christos If PREFIX is True, then this command is a prefix command.
438 1.1 christos
439 1.1 christos The documentation for the command is taken from the doc string for
440 1.1 christos the python class. */
441 1.1 christos
442 1.1 christos static int
443 1.1 christos cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
444 1.1 christos {
445 1.1 christos cmdpy_object *obj = (cmdpy_object *) self;
446 1.1 christos const char *name;
447 1.1 christos int cmdtype;
448 1.1 christos int completetype = -1;
449 1.1 christos char *docstring = NULL;
450 1.7 christos struct cmd_list_element **cmd_list;
451 1.7 christos char *cmd_name, *pfx_name;
452 1.1 christos static const char *keywords[] = { "name", "command_class", "completer_class",
453 1.1 christos "prefix", NULL };
454 1.1 christos PyObject *is_prefix = NULL;
455 1.1 christos int cmp;
456 1.1 christos
457 1.1 christos if (obj->command)
458 1.1 christos {
459 1.1 christos /* Note: this is apparently not documented in Python. We return
460 1.1 christos 0 for success, -1 for failure. */
461 1.1 christos PyErr_Format (PyExc_RuntimeError,
462 1.1 christos _("Command object already initialized."));
463 1.1 christos return -1;
464 1.7 christos }
465 1.7 christos
466 1.7 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "si|iO",
467 1.1 christos keywords, &name, &cmdtype,
468 1.1 christos &completetype, &is_prefix))
469 1.1 christos return -1;
470 1.1 christos
471 1.1 christos if (cmdtype != no_class && cmdtype != class_run
472 1.1 christos && cmdtype != class_vars && cmdtype != class_stack
473 1.1 christos && cmdtype != class_files && cmdtype != class_support
474 1.1 christos && cmdtype != class_info && cmdtype != class_breakpoint
475 1.1 christos && cmdtype != class_trace && cmdtype != class_obscure
476 1.1 christos && cmdtype != class_maintenance && cmdtype != class_user)
477 1.1 christos {
478 1.1 christos PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
479 1.1 christos return -1;
480 1.1 christos }
481 1.1 christos
482 1.1 christos if (completetype < -1 || completetype >= (int) N_COMPLETERS)
483 1.1 christos {
484 1.1 christos PyErr_Format (PyExc_RuntimeError,
485 1.1 christos _("Invalid completion type argument."));
486 1.1 christos return -1;
487 1.1 christos }
488 1.1 christos
489 1.1 christos cmd_name = gdbpy_parse_command_name (name, &cmd_list, &cmdlist);
490 1.1 christos if (! cmd_name)
491 1.1 christos return -1;
492 1.1 christos
493 1.1 christos pfx_name = NULL;
494 1.1 christos if (is_prefix != NULL)
495 1.1 christos {
496 1.1 christos cmp = PyObject_IsTrue (is_prefix);
497 1.1 christos if (cmp == 1)
498 1.1 christos {
499 1.1 christos int i, out;
500 1.6 christos
501 1.1 christos /* Make a normalized form of the command name. */
502 1.1 christos pfx_name = (char *) xmalloc (strlen (name) + 2);
503 1.1 christos
504 1.1 christos i = 0;
505 1.1 christos out = 0;
506 1.1 christos while (name[i])
507 1.1 christos {
508 1.1 christos /* Skip whitespace. */
509 1.1 christos while (name[i] == ' ' || name[i] == '\t')
510 1.1 christos ++i;
511 1.1 christos /* Copy non-whitespace characters. */
512 1.1 christos while (name[i] && name[i] != ' ' && name[i] != '\t')
513 1.1 christos pfx_name[out++] = name[i++];
514 1.1 christos /* Add a single space after each word -- including the final
515 1.1 christos word. */
516 1.1 christos pfx_name[out++] = ' ';
517 1.1 christos }
518 1.1 christos pfx_name[out] = '\0';
519 1.1 christos }
520 1.1 christos else if (cmp < 0)
521 1.1 christos {
522 1.1 christos xfree (cmd_name);
523 1.1 christos return -1;
524 1.1 christos }
525 1.1 christos }
526 1.7 christos if (PyObject_HasAttr (self, gdbpy_doc_cst))
527 1.1 christos {
528 1.7 christos gdbpy_ref<> ds_obj (PyObject_GetAttr (self, gdbpy_doc_cst));
529 1.1 christos
530 1.7 christos if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
531 1.1 christos {
532 1.1 christos docstring = python_string_to_host_string (ds_obj.get ()).release ();
533 1.1 christos if (docstring == NULL)
534 1.1 christos {
535 1.1 christos xfree (cmd_name);
536 1.1 christos xfree (pfx_name);
537 1.1 christos return -1;
538 1.1 christos }
539 1.1 christos }
540 1.1 christos }
541 1.1 christos if (! docstring)
542 1.8 christos docstring = xstrdup (_("This command is not documented."));
543 1.1 christos
544 1.5 christos gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self);
545 1.1 christos
546 1.1 christos TRY
547 1.1 christos {
548 1.1 christos struct cmd_list_element *cmd;
549 1.1 christos
550 1.1 christos if (pfx_name)
551 1.1 christos {
552 1.1 christos int allow_unknown;
553 1.1 christos
554 1.1 christos /* If we have our own "invoke" method, then allow unknown
555 1.1 christos sub-commands. */
556 1.1 christos allow_unknown = PyObject_HasAttr (self, invoke_cst);
557 1.1 christos cmd = add_prefix_cmd (cmd_name, (enum command_class) cmdtype,
558 1.1 christos NULL, docstring, &obj->sub_list,
559 1.1 christos pfx_name, allow_unknown, cmd_list);
560 1.8 christos }
561 1.1 christos else
562 1.1 christos cmd = add_cmd (cmd_name, (enum command_class) cmdtype,
563 1.1 christos docstring, cmd_list);
564 1.1 christos
565 1.1 christos /* There appears to be no API to set this. */
566 1.1 christos cmd->func = cmdpy_function;
567 1.1 christos cmd->destroyer = cmdpy_destroyer;
568 1.8 christos
569 1.1 christos obj->command = cmd;
570 1.1 christos set_cmd_context (cmd, self_ref.release ());
571 1.3 christos set_cmd_completer (cmd, ((completetype == -1) ? cmdpy_completer
572 1.3 christos : completers[completetype].completer));
573 1.3 christos if (completetype == -1)
574 1.1 christos set_cmd_completer_handle_brkchars (cmd,
575 1.5 christos cmdpy_completer_handle_brkchars);
576 1.1 christos }
577 1.1 christos CATCH (except, RETURN_MASK_ALL)
578 1.1 christos {
579 1.1 christos xfree (cmd_name);
580 1.8 christos xfree (docstring);
581 1.1 christos xfree (pfx_name);
582 1.1 christos gdbpy_convert_exception (except);
583 1.5 christos return -1;
584 1.5 christos }
585 1.1 christos END_CATCH
586 1.1 christos
587 1.1 christos return 0;
588 1.1 christos }
589 1.1 christos
590 1.1 christos
591 1.1 christos
593 1.1 christos /* Initialize the 'commands' code. */
594 1.1 christos
595 1.1 christos int
596 1.1 christos gdbpy_initialize_commands (void)
597 1.1 christos {
598 1.1 christos int i;
599 1.1 christos
600 1.1 christos cmdpy_object_type.tp_new = PyType_GenericNew;
601 1.1 christos if (PyType_Ready (&cmdpy_object_type) < 0)
602 1.5 christos return -1;
603 1.1 christos
604 1.1 christos /* Note: alias and user are special; pseudo appears to be unused,
605 1.1 christos and there is no reason to expose tui, I think. */
606 1.1 christos if (PyModule_AddIntConstant (gdb_module, "COMMAND_NONE", no_class) < 0
607 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_RUNNING", class_run) < 0
608 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_DATA", class_vars) < 0
609 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_STACK", class_stack) < 0
610 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_FILES", class_files) < 0
611 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_SUPPORT",
612 1.1 christos class_support) < 0
613 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_STATUS", class_info) < 0
614 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_BREAKPOINTS",
615 1.1 christos class_breakpoint) < 0
616 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_TRACEPOINTS",
617 1.1 christos class_trace) < 0
618 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_OBSCURE",
619 1.1 christos class_obscure) < 0
620 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_MAINTENANCE",
621 1.1 christos class_maintenance) < 0
622 1.1 christos || PyModule_AddIntConstant (gdb_module, "COMMAND_USER", class_user) < 0)
623 1.1 christos return -1;
624 1.1 christos
625 1.1 christos for (i = 0; i < N_COMPLETERS; ++i)
626 1.1 christos {
627 1.1 christos if (PyModule_AddIntConstant (gdb_module, completers[i].name, i) < 0)
628 1.1 christos return -1;
629 1.1 christos }
630 1.1 christos
631 1.1 christos if (gdb_pymodule_addobject (gdb_module, "Command",
632 1.1 christos (PyObject *) &cmdpy_object_type) < 0)
633 1.1 christos return -1;
634 1.1 christos
635 1.1 christos invoke_cst = PyString_FromString ("invoke");
636 1.1 christos if (invoke_cst == NULL)
637 1.1 christos return -1;
638 1.1 christos complete_cst = PyString_FromString ("complete");
639 1.1 christos if (complete_cst == NULL)
640 1.1 christos return -1;
641 1.1 christos
642 1.1 christos return 0;
643 1.1 christos }
644 1.1 christos
645 1.1 christos
646 1.1 christos
648 1.1 christos static PyMethodDef cmdpy_object_methods[] =
649 1.1 christos {
650 1.1 christos { "dont_repeat", cmdpy_dont_repeat, METH_NOARGS,
651 1.1 christos "Prevent command repetition when user enters empty line." },
652 1.5 christos
653 1.1 christos { 0 }
654 1.1 christos };
655 1.1 christos
656 1.1 christos PyTypeObject cmdpy_object_type =
657 1.1 christos {
658 1.1 christos PyVarObject_HEAD_INIT (NULL, 0)
659 1.1 christos "gdb.Command", /*tp_name*/
660 1.1 christos sizeof (cmdpy_object), /*tp_basicsize*/
661 1.1 christos 0, /*tp_itemsize*/
662 1.1 christos 0, /*tp_dealloc*/
663 1.1 christos 0, /*tp_print*/
664 1.1 christos 0, /*tp_getattr*/
665 1.1 christos 0, /*tp_setattr*/
666 1.1 christos 0, /*tp_compare*/
667 1.1 christos 0, /*tp_repr*/
668 1.1 christos 0, /*tp_as_number*/
669 1.1 christos 0, /*tp_as_sequence*/
670 1.1 christos 0, /*tp_as_mapping*/
671 1.1 christos 0, /*tp_hash */
672 1.1 christos 0, /*tp_call*/
673 1.1 christos 0, /*tp_str*/
674 1.1 christos 0, /*tp_getattro*/
675 1.1 christos 0, /*tp_setattro*/
676 1.1 christos 0, /*tp_as_buffer*/
677 1.1 christos Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
678 1.1 christos "GDB command object", /* tp_doc */
679 1.1 christos 0, /* tp_traverse */
680 1.1 christos 0, /* tp_clear */
681 1.1 christos 0, /* tp_richcompare */
682 1.1 christos 0, /* tp_weaklistoffset */
683 1.1 christos 0, /* tp_iter */
684 1.1 christos 0, /* tp_iternext */
685 1.1 christos cmdpy_object_methods, /* tp_methods */
686 1.1 christos 0, /* tp_members */
687 1.1 christos 0, /* tp_getset */
688 1.1 christos 0, /* tp_base */
689 1.1 christos 0, /* tp_dict */
690 1.1 christos 0, /* tp_descr_get */
691 1.1 christos 0, /* tp_descr_set */
692 1.1 christos 0, /* tp_dictoffset */
693 1.1 christos cmdpy_init, /* tp_init */
694 1.1 christos 0, /* tp_alloc */
695 1.1 christos };
696 1.1 christos
697 1.1 christos
698 1.1 christos
700 1.1 christos /* Utility to build a buildargv-like result from ARGS.
701 1.1 christos This intentionally parses arguments the way libiberty/argv.c:buildargv
702 1.1 christos does. It splits up arguments in a reasonable way, and we want a standard
703 1.1 christos way of parsing arguments. Several gdb commands use buildargv to parse their
704 1.1 christos arguments. Plus we want to be able to write compatible python
705 1.1 christos implementations of gdb commands. */
706 1.1 christos
707 1.1 christos PyObject *
708 1.1 christos gdbpy_string_to_argv (PyObject *self, PyObject *args)
709 1.1 christos {
710 1.7 christos const char *input;
711 1.1 christos
712 1.1 christos if (!PyArg_ParseTuple (args, "s", &input))
713 1.1 christos return NULL;
714 1.1 christos
715 1.1 christos gdbpy_ref<> py_argv (PyList_New (0));
716 1.1 christos if (py_argv == NULL)
717 1.1 christos return NULL;
718 1.1 christos
719 1.1 christos /* buildargv uses NULL to represent an empty argument list, but we can't use
720 1.8 christos that in Python. Instead, if ARGS is "" then return an empty list.
721 1.1 christos This undoes the NULL -> "" conversion that cmdpy_function does. */
722 1.8 christos
723 1.1 christos if (*input != '\0')
724 1.8 christos {
725 1.1 christos gdb_argv c_argv (input);
726 1.1 christos
727 1.7 christos for (char *arg : c_argv)
728 1.8 christos {
729 1.1 christos gdbpy_ref<> argp (PyString_FromString (arg));
730 1.1 christos
731 1.1 christos if (argp == NULL
732 1.7 christos || PyList_Append (py_argv.get (), argp.get ()) < 0)
733 1.1 christos return NULL;
734 }
735 }
736
737 return py_argv.release ();
738 }
739