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