System notice: In light of the Debian OpenSSL security issue we've regenerated the server keys. See this thread for instructions and the new key fingerprints.

Commit c316b574d1e355d1c5ad9b98edb2c193fdd88b5f

Add mechanism to define commands in Python.

Commit diff

gdb/Makefile.in

 
263263SUBDIR_PYTHON_OBS = \
264264 python.o \
265265 python-breakpoint.o \
266 python-cmd.o \
266267 python-hooks.o \
267268 python-value.o
268269SUBDIR_PYTHON_SRCS = \
269270 python/python.c \
270271 python/breakpoint.c \
272 python/cmd.c \
271273 python/hooks.c \
272274 python/value.c
273275SUBDIR_PYTHON_DEPS =
34243424 $(breakpoint_h) $(gdbcmd_h)
34253425 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
34263426 $(srcdir)/python/breakpoint.c -o python-breakpoint.o
3427python-cmd.o: $(srcdir)/python/cmd.c $(defs_h) $(python_h) $(value_h) \
3428 $(exceptions_h) $(python_internal_h) $(charset_h) \
3429 $(gdbcmd_h) $(cli_decode_h)
3430 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
3431 $(srcdir)/python/cmd.c -o python-cmd.o
34273432python-hooks.o: $(srcdir)/python/hooks.c $(defs_h) $(cli_decode_h) \
34283433 $(charset_h) $(gdb_events_h) $(python_h) $(python_internal_h)
34293434 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
toggle raw diff

gdb/cli/cli-cmds.c

 
615615 struct symtab_and_line sal;
616616 struct symbol *sym;
617617 char *arg1;
618 int cmdlen, log10;
619 unsigned m;
618 int cmdlen;
620619 char *editor;
621620 char *p, *fn;
622621
691691 if ((editor = (char *) getenv ("EDITOR")) == NULL)
692692 editor = "/bin/ex";
693693
694 /* Approximate base-10 log of line to 1 unit for digit count */
695 for(log10=32, m=0x80000000; !(sal.line & m) && log10>0; log10--, m=m>>1);
696 log10 = 1 + (int)((log10 + (0 == ((m-1) & sal.line)))/3.32192809);
697
698694 /* If we don't already know the full absolute file name of the
699695 source file, find it now. */
700696 if (!sal.symtab->fullname)
toggle raw diff

gdb/cli/cli-decode.c

 
190190 c->allow_unknown = 0;
191191 c->abbrev_flag = 0;
192192 set_cmd_completer (c, make_symbol_completion_list);
193 c->destroyer = NULL;
193194 c->type = not_set_cmd;
194195 c->var = NULL;
195196 c->var_type = var_boolean;
615615void
616616delete_cmd (char *name, struct cmd_list_element **list)
617617{
618 struct cmd_list_element *c;
619 struct cmd_list_element *p;
618 struct cmd_list_element *iter;
619 struct cmd_list_element **previous_chain_ptr;
620
621 previous_chain_ptr = list;
620622
621 while (*list && strcmp ((*list)->name, name) == 0)
623 for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
622624 {
623 if ((*list)->hookee_pre)
624 (*list)->hookee_pre->hook_pre = 0; /* Hook slips out of its mouth */
625 if ((*list)->hookee_post)
626 (*list)->hookee_post->hook_post = 0; /* Hook slips out of its bottom */
627 p = (*list)->next;
628 xfree (* list);
629 *list = p;
625 if (strcmp (iter->name, name) == 0)
626 {
627 if (iter->destroyer)
628 iter->destroyer (iter, iter->context);
629 if (iter->hookee_pre)
630 iter->hookee_pre->hook_pre = 0;
631 if (iter->hookee_post)
632 iter->hookee_post->hook_post = 0;
633
634 /* Update the link. Note that we don't change
635 previous_chain_ptr; next time through the loop this must
636 stay the same. */
637 *previous_chain_ptr = iter->next;
638
639 xfree (iter);
640 }
641 else
642 previous_chain_ptr = &iter->next;
630643 }
631
632 if (*list)
633 for (c = *list; c->next;)
634 {
635 if (strcmp (c->next->name, name) == 0)
636 {
637 if (c->next->hookee_pre)
638 c->next->hookee_pre->hook_pre = 0; /* hooked cmd gets away. */
639 if (c->next->hookee_post)
640 c->next->hookee_post->hook_post = 0; /* remove post hook */
641 /* :( no fishing metaphore */
642 p = c->next->next;
643 xfree (c->next);
644 c->next = p;
645 }
646 else
647 c = c->next;
648 }
649644}
650645
651646/* Shorthands to the commands above. */
toggle raw diff

gdb/cli/cli-decode.h

 
168168 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
169169 char **(*completer) (char *text, char *word);
170170
171 /* Destruction routine for this command. If non-NULL, this is
172 called when this command instance is destroyed. This may be
173 used to finalize the CONTEXT field, if needed. */
174 void (*destroyer) (struct cmd_list_element *self, void *context);
175
171176 /* Type of "set" or "show" command (or SET_NOT_SET if not "set"
172177 or "show"). */
173178 cmd_types type;
toggle raw diff

gdb/python/cmd.c

 
1/* gdb commands implemented in Python
2
3 Copyright (C) 2008 Free Software Foundation, Inc.
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"
22#include "value.h"
23#include "exceptions.h"
24#include "python-internal.h"
25#include "charset.h"
26#include "gdbcmd.h"
27#include "cli/cli-decode.h"
28
29/* A gdb command. For the time being only ordinary commands (not
30 set/show commands) are allowed. */
31struct cmdpy_object
32{
33 PyObject_HEAD
34
35 /* The corresponding gdb command object, or NULL if the command is
36 no longer installed. */
37 /* It isn't clear if we will ever care about this. */
38 struct cmd_list_element *command;
39};
40
41typedef struct cmdpy_object cmdpy_object;
42
43static PyObject *cmdpy_dont_repeat (PyObject *self, PyObject *args);
44
45static PyMethodDef cmdpy_object_methods[] =
46{
47 { "dont_repeat", cmdpy_dont_repeat, METH_NOARGS,
48 "Prevent command repetition when user enters empty line." },
49
50 { 0 }
51};
52
53static PyTypeObject cmdpy_object_type =
54{
55 PyObject_HEAD_INIT (NULL)
56 0, /*ob_size*/
57 "gdb.Command", /*tp_name*/
58 sizeof (cmdpy_object), /*tp_basicsize*/
59 0, /*tp_itemsize*/
60 0, /*tp_dealloc*/
61 0, /*tp_print*/
62 0, /*tp_getattr*/
63 0, /*tp_setattr*/
64 0, /*tp_compare*/
65 0, /*tp_repr*/
66 0, /*tp_as_number*/
67 0, /*tp_as_sequence*/
68 0, /*tp_as_mapping*/
69 0, /*tp_hash */
70 0, /*tp_call*/
71 0, /*tp_str*/
72 0, /*tp_getattro*/
73 0, /*tp_setattro*/
74 0, /*tp_as_buffer*/
75 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
76 "GDB command object", /* tp_doc */
77 0, /* tp_traverse */
78 0, /* tp_clear */
79 0, /* tp_richcompare */
80 0, /* tp_weaklistoffset */
81 0, /* tp_iter */
82 0, /* tp_iternext */
83 cmdpy_object_methods /* tp_methods */
84};
85
86
87
88static PyObject *
89cmdpy_dont_repeat (PyObject *self, PyObject *args)
90{
91 dont_repeat ();
92 Py_RETURN_NONE;
93}
94
95
96
97
98/* Called if the gdb cmd_list_element is destroyed. */
99static void
100cmdpy_destroyer (struct cmd_list_element *self, void *context)
101{
102 /* Release our hold on the command object. */
103 cmdpy_object *cmd = (cmdpy_object *) context;
104 cmd->command = NULL;
105 Py_DECREF (cmd);
106
107 /* We allocated the name and doc string. */
108 xfree (self->name);
109 xfree (self->doc);
110}
111
112/* Called by gdb to invoke the command. */
113static void
114cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
115{
116 cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
117 PyObject *method, *argobj, *ttyobj, *result;
118
119 if (! obj)
120 error ("Invalid invocation of Python command object");
121 if (! PyObject_HasAttrString ((PyObject *) obj, "invoke"))
122 error ("Python command object missing 'invoke' method");
123
124 method = PyString_FromString ("invoke");
125 if (! args)
126 {
127 argobj = Py_None;
128 Py_INCREF (argobj);
129 }
130 else
131 {
132 argobj = PyString_FromString (args);
133 if (! argobj)
134 error ("Couldn't convert arguments to Python string");
135 }
136 ttyobj = from_tty ? Py_True : Py_False;
137 Py_INCREF (ttyobj);
138 result = PyObject_CallMethodObjArgs ((PyObject *) obj, method, argobj,
139 ttyobj, NULL);
140 Py_DECREF (method);
141 Py_DECREF (argobj);
142 Py_DECREF (ttyobj);
143 if (! result)
144 {
145 /* FIXME: pretty lame. */
146 PyErr_Print ();
147 error ("Error occurred in Python command");
148 }
149 Py_DECREF (result);
150}
151
152/* Object initializer; sets up gdb-side structures for command.
153
154 Use: __init__(NAME, CMDCLASS).
155
156 NAME is the name of the command; spaces in the name mean to look up
157 initial segments as prefixes.
158
159 CMDCLASS is the kind of command. It should be one of the COMMAND_*
160 constants defined in the gdb module.
161
162 The documentation for the command is taken from the doc string for
163 the python class.
164
165*/
166static int
167cmdpy_init (PyObject *self, PyObject *args, PyObject *kwds)
168{
169 cmdpy_object *obj = (cmdpy_object *) self;
170 char *name;
171 int cmdtype;
172 volatile struct gdb_exception except;
173
174 if (obj->command)
175 {
176 /* Note: this is apparently not documented in Python. We return
177 0 for success, -1 for failure. */
178 PyErr_Format (PyExc_RuntimeError, "Command object already initialized");
179 return -1;
180 }
181
182 if (! PyArg_ParseTuple (args, "si", &name, &cmdtype))
183 return -1;
184
185 if (cmdtype != no_class && cmdtype != class_run
186 && cmdtype != class_vars && cmdtype != class_stack
187 && cmdtype != class_files && cmdtype != class_support
188 && cmdtype != class_info && cmdtype != class_breakpoint
189 && cmdtype != class_trace && cmdtype != class_obscure
190 && cmdtype != class_maintenance)
191 {
192 PyErr_Format (PyExc_RuntimeError, "invalid command class argument");
193 return -1;
194 }
195
196 TRY_CATCH (except, RETURN_MASK_ALL)
197 {
198 /* FIXME: decoding NAME if it has spaces. Also, how to add new
199 prefix commands? */
200 struct cmd_list_element *cmd = add_cmd (xstrdup (name),
201 (enum command_class) cmdtype,
202 NULL,
203 xstrdup ("FIXME: docstring"),
204 &cmdlist /* FIXME */);
205 /* There appears to be no API to set this. */
206 cmd->func = cmdpy_function;
207 /* FIXME: route completion requests through user object. */
208
209 obj->command = cmd;
210 Py_INCREF (self);
211 set_cmd_context (cmd, self);
212 }
213 if (except.reason < 0)
214 {
215 PyErr_Format (except.reason == RETURN_QUIT
216 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
217 "%s", except.message);
218 return -1;
219 }
220 return 0;
221}
222
223
224
225void
226gdbpy_initialize_commands (void)
227{
228 cmdpy_object_type.tp_new = PyType_GenericNew;
229 cmdpy_object_type.tp_init = cmdpy_init;
230 if (PyType_Ready (&cmdpy_object_type) < 0)
231 return;
232
233 /* Note: alias and user seem to be special; pseudo appears to be
234 unused, and there is no reason to expose tui or xdb, I think. */
235 if (PyModule_AddIntConstant (gdb_module, "COMMAND_NONE", no_class) < 0
236 || PyModule_AddIntConstant (gdb_module, "COMMAND_RUN", class_run) < 0
237 || PyModule_AddIntConstant (gdb_module, "COMMAND_VARS", class_vars) < 0
238 || PyModule_AddIntConstant (gdb_module, "COMMAND_STACK", class_stack) < 0
239 || PyModule_AddIntConstant (gdb_module, "COMMAND_FILES", class_files) < 0
240 || PyModule_AddIntConstant (gdb_module, "COMMAND_SUPPORT",
241 class_support) < 0
242 || PyModule_AddIntConstant (gdb_module, "COMMAND_INFO", class_info) < 0
243 || PyModule_AddIntConstant (gdb_module, "COMMAND_BREAKPOINT",
244 class_breakpoint) < 0
245 || PyModule_AddIntConstant (gdb_module, "COMMAND_TRACE", class_trace) < 0
246 || PyModule_AddIntConstant (gdb_module, "COMMAND_OBSCURE",
247 class_obscure) < 0
248 || PyModule_AddIntConstant (gdb_module, "COMMAND_MAINTENANCE",
249 class_maintenance) < 0)
250 return;
251
252 Py_INCREF (&cmdpy_object_type);
253 PyModule_AddObject (gdb_module, "Command",
254 (PyObject *) &cmdpy_object_type);
255}
toggle raw diff

gdb/python/python-internal.h

 
4444void gdbpy_initialize_values (void);
4545void gdbpy_initialize_hooks (void);
4646void gdbpy_initialize_breakpoints (void);
47void gdbpy_initialize_commands (void);
4748
4849/* Use this after a TRY_EXCEPT to throw the appropriate Python
4950 exception. FIXME: throw different errors depending on
toggle raw diff

gdb/python/python.c

 
7070 gdbpy_initialize_hooks ();
7171 gdbpy_initialize_values ();
7272 gdbpy_initialize_breakpoints ();
73 gdbpy_initialize_commands ();
7374
7475 PyRun_SimpleString ("import gdb");
7576
toggle raw diff