Commit | Line | Data |
---|---|---|
d57a3c85 TJB |
1 | /* General python/gdb code |
2 | ||
0fb0cc75 | 3 | Copyright (C) 2008, 2009 Free Software Foundation, Inc. |
d57a3c85 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 | #include "defs.h" | |
21 | #include "command.h" | |
22 | #include "ui-out.h" | |
23 | #include "cli/cli-script.h" | |
24 | #include "gdbcmd.h" | |
25 | ||
26 | #include <ctype.h> | |
27 | ||
28 | /* True if we should print the stack when catching a Python error, | |
29 | false otherwise. */ | |
30 | static int gdbpy_should_print_stack = 1; | |
31 | ||
32 | #ifdef HAVE_PYTHON | |
33 | ||
34 | #include "python.h" | |
35 | #include "libiberty.h" | |
36 | #include "cli/cli-decode.h" | |
37 | #include "charset.h" | |
38 | #include "top.h" | |
39 | #include "exceptions.h" | |
40 | #include "python-internal.h" | |
41 | #include "version.h" | |
42 | #include "target.h" | |
43 | #include "gdbthread.h" | |
44 | ||
12453b93 | 45 | static PyMethodDef GdbMethods[]; |
d57a3c85 TJB |
46 | |
47 | PyObject *gdb_module; | |
48 | ||
d57a3c85 TJB |
49 | /* Given a command_line, return a command string suitable for passing |
50 | to Python. Lines in the string are separated by newlines. The | |
51 | return value is allocated using xmalloc and the caller is | |
52 | responsible for freeing it. */ | |
53 | ||
54 | static char * | |
55 | compute_python_string (struct command_line *l) | |
56 | { | |
57 | struct command_line *iter; | |
58 | char *script = NULL; | |
59 | int size = 0; | |
60 | int here; | |
61 | ||
62 | for (iter = l; iter; iter = iter->next) | |
63 | size += strlen (iter->line) + 1; | |
64 | ||
65 | script = xmalloc (size + 1); | |
66 | here = 0; | |
67 | for (iter = l; iter; iter = iter->next) | |
68 | { | |
69 | int len = strlen (iter->line); | |
70 | strcpy (&script[here], iter->line); | |
71 | here += len; | |
72 | script[here++] = '\n'; | |
73 | } | |
74 | script[here] = '\0'; | |
75 | return script; | |
76 | } | |
77 | ||
78 | /* Take a command line structure representing a 'python' command, and | |
79 | evaluate its body using the Python interpreter. */ | |
80 | ||
81 | void | |
82 | eval_python_from_control_command (struct command_line *cmd) | |
83 | { | |
12453b93 | 84 | int ret; |
d57a3c85 | 85 | char *script; |
ca30a762 TT |
86 | struct cleanup *cleanup; |
87 | PyGILState_STATE state; | |
d57a3c85 TJB |
88 | |
89 | if (cmd->body_count != 1) | |
90 | error (_("Invalid \"python\" block structure.")); | |
91 | ||
ca30a762 TT |
92 | state = PyGILState_Ensure (); |
93 | cleanup = make_cleanup_py_restore_gil (&state); | |
94 | ||
d57a3c85 | 95 | script = compute_python_string (cmd->body_list[0]); |
12453b93 | 96 | ret = PyRun_SimpleString (script); |
d57a3c85 | 97 | xfree (script); |
12453b93 | 98 | if (ret) |
d57a3c85 TJB |
99 | { |
100 | gdbpy_print_stack (); | |
12453b93 | 101 | error (_("Error while executing Python code.")); |
d57a3c85 | 102 | } |
ca30a762 TT |
103 | |
104 | do_cleanups (cleanup); | |
d57a3c85 TJB |
105 | } |
106 | ||
107 | /* Implementation of the gdb "python" command. */ | |
108 | ||
109 | static void | |
110 | python_command (char *arg, int from_tty) | |
111 | { | |
ca30a762 TT |
112 | struct cleanup *cleanup; |
113 | PyGILState_STATE state; | |
114 | ||
115 | state = PyGILState_Ensure (); | |
116 | cleanup = make_cleanup_py_restore_gil (&state); | |
117 | ||
d57a3c85 TJB |
118 | while (arg && *arg && isspace (*arg)) |
119 | ++arg; | |
120 | if (arg && *arg) | |
121 | { | |
12453b93 | 122 | if (PyRun_SimpleString (arg)) |
d57a3c85 TJB |
123 | { |
124 | gdbpy_print_stack (); | |
12453b93 | 125 | error (_("Error while executing Python code.")); |
d57a3c85 TJB |
126 | } |
127 | } | |
128 | else | |
129 | { | |
130 | struct command_line *l = get_command_line (python_control, ""); | |
ca30a762 | 131 | make_cleanup_free_command_lines (&l); |
d57a3c85 | 132 | execute_control_command_untraced (l); |
d57a3c85 | 133 | } |
ca30a762 TT |
134 | |
135 | do_cleanups (cleanup); | |
d57a3c85 TJB |
136 | } |
137 | ||
138 | \f | |
139 | ||
140 | /* Transform a gdb parameters's value into a Python value. May return | |
141 | NULL (and set a Python exception) on error. Helper function for | |
142 | get_parameter. */ | |
143 | ||
144 | static PyObject * | |
145 | parameter_to_python (struct cmd_list_element *cmd) | |
146 | { | |
147 | switch (cmd->var_type) | |
148 | { | |
149 | case var_string: | |
150 | case var_string_noescape: | |
151 | case var_optional_filename: | |
152 | case var_filename: | |
153 | case var_enum: | |
154 | { | |
155 | char *str = * (char **) cmd->var; | |
156 | if (! str) | |
157 | str = ""; | |
158 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
159 | } | |
160 | ||
161 | case var_boolean: | |
162 | { | |
163 | if (* (int *) cmd->var) | |
164 | Py_RETURN_TRUE; | |
165 | else | |
166 | Py_RETURN_FALSE; | |
167 | } | |
168 | ||
169 | case var_auto_boolean: | |
170 | { | |
171 | enum auto_boolean ab = * (enum auto_boolean *) cmd->var; | |
172 | if (ab == AUTO_BOOLEAN_TRUE) | |
173 | Py_RETURN_TRUE; | |
174 | else if (ab == AUTO_BOOLEAN_FALSE) | |
175 | Py_RETURN_FALSE; | |
176 | else | |
177 | Py_RETURN_NONE; | |
178 | } | |
179 | ||
180 | case var_integer: | |
181 | if ((* (int *) cmd->var) == INT_MAX) | |
182 | Py_RETURN_NONE; | |
183 | /* Fall through. */ | |
184 | case var_zinteger: | |
185 | return PyLong_FromLong (* (int *) cmd->var); | |
186 | ||
187 | case var_uinteger: | |
188 | { | |
189 | unsigned int val = * (unsigned int *) cmd->var; | |
190 | if (val == UINT_MAX) | |
191 | Py_RETURN_NONE; | |
192 | return PyLong_FromUnsignedLong (val); | |
193 | } | |
194 | } | |
195 | ||
196 | return PyErr_Format (PyExc_RuntimeError, "programmer error: unhandled type"); | |
197 | } | |
198 | ||
199 | /* A Python function which returns a gdb parameter's value as a Python | |
200 | value. */ | |
201 | ||
202 | static PyObject * | |
203 | get_parameter (PyObject *self, PyObject *args) | |
204 | { | |
205 | struct cmd_list_element *alias, *prefix, *cmd; | |
206 | char *arg, *newarg; | |
207 | volatile struct gdb_exception except; | |
208 | ||
209 | if (! PyArg_ParseTuple (args, "s", &arg)) | |
210 | return NULL; | |
211 | ||
212 | newarg = concat ("show ", arg, (char *) NULL); | |
213 | ||
214 | TRY_CATCH (except, RETURN_MASK_ALL) | |
215 | { | |
216 | if (! lookup_cmd_composition (newarg, &alias, &prefix, &cmd)) | |
217 | { | |
218 | xfree (newarg); | |
219 | return PyErr_Format (PyExc_RuntimeError, | |
220 | "could not find variable `%s'", arg); | |
221 | } | |
222 | } | |
223 | xfree (newarg); | |
224 | GDB_PY_HANDLE_EXCEPTION (except); | |
225 | ||
226 | if (! cmd->var) | |
227 | return PyErr_Format (PyExc_RuntimeError, "`%s' is not a variable", arg); | |
228 | return parameter_to_python (cmd); | |
229 | } | |
230 | ||
231 | /* A Python function which evaluates a string using the gdb CLI. */ | |
232 | ||
233 | static PyObject * | |
234 | execute_gdb_command (PyObject *self, PyObject *args) | |
235 | { | |
236 | struct cmd_list_element *alias, *prefix, *cmd; | |
237 | char *arg, *newarg; | |
12453b93 TJB |
238 | PyObject *from_tty_obj = NULL; |
239 | int from_tty; | |
240 | int cmp; | |
d57a3c85 | 241 | volatile struct gdb_exception except; |
d57a3c85 | 242 | |
12453b93 | 243 | if (! PyArg_ParseTuple (args, "s|O!", &arg, &PyBool_Type, &from_tty_obj)) |
d57a3c85 TJB |
244 | return NULL; |
245 | ||
12453b93 TJB |
246 | from_tty = 0; |
247 | if (from_tty_obj) | |
248 | { | |
249 | cmp = PyObject_IsTrue (from_tty_obj); | |
250 | if (cmp < 0) | |
251 | return NULL; | |
252 | from_tty = cmp; | |
253 | } | |
254 | ||
d57a3c85 TJB |
255 | TRY_CATCH (except, RETURN_MASK_ALL) |
256 | { | |
12453b93 | 257 | execute_command (arg, from_tty); |
d57a3c85 TJB |
258 | } |
259 | GDB_PY_HANDLE_EXCEPTION (except); | |
260 | ||
347bddb7 PA |
261 | /* Do any commands attached to breakpoint we stopped at. */ |
262 | bpstat_do_actions (); | |
d57a3c85 TJB |
263 | |
264 | Py_RETURN_NONE; | |
265 | } | |
266 | ||
267 | \f | |
268 | ||
269 | /* Printing. */ | |
270 | ||
271 | /* A python function to write a single string using gdb's filtered | |
272 | output stream. */ | |
273 | static PyObject * | |
274 | gdbpy_write (PyObject *self, PyObject *args) | |
275 | { | |
276 | char *arg; | |
277 | if (! PyArg_ParseTuple (args, "s", &arg)) | |
278 | return NULL; | |
279 | printf_filtered ("%s", arg); | |
280 | Py_RETURN_NONE; | |
281 | } | |
282 | ||
283 | /* A python function to flush gdb's filtered output stream. */ | |
284 | static PyObject * | |
285 | gdbpy_flush (PyObject *self, PyObject *args) | |
286 | { | |
287 | gdb_flush (gdb_stdout); | |
288 | Py_RETURN_NONE; | |
289 | } | |
290 | ||
291 | /* Print a python exception trace, or print nothing and clear the | |
292 | python exception, depending on gdbpy_should_print_stack. Only call | |
293 | this if a python exception is set. */ | |
294 | void | |
295 | gdbpy_print_stack (void) | |
296 | { | |
297 | if (gdbpy_should_print_stack) | |
298 | PyErr_Print (); | |
299 | else | |
300 | PyErr_Clear (); | |
301 | } | |
302 | ||
303 | #else /* HAVE_PYTHON */ | |
304 | ||
305 | /* Dummy implementation of the gdb "python" command. */ | |
306 | ||
307 | static void | |
308 | python_command (char *arg, int from_tty) | |
309 | { | |
310 | while (arg && *arg && isspace (*arg)) | |
311 | ++arg; | |
312 | if (arg && *arg) | |
313 | error (_("Python scripting is not supported in this copy of GDB.")); | |
314 | else | |
315 | { | |
316 | struct command_line *l = get_command_line (python_control, ""); | |
317 | struct cleanup *cleanups = make_cleanup_free_command_lines (&l); | |
318 | execute_control_command_untraced (l); | |
319 | do_cleanups (cleanups); | |
320 | } | |
321 | } | |
322 | ||
323 | void | |
324 | eval_python_from_control_command (struct command_line *cmd) | |
325 | { | |
326 | error (_("Python scripting is not supported in this copy of GDB.")); | |
327 | } | |
328 | ||
329 | #endif /* HAVE_PYTHON */ | |
330 | ||
331 | \f | |
332 | ||
333 | /* Lists for 'maint set python' commands. */ | |
334 | ||
335 | static struct cmd_list_element *set_python_list; | |
336 | static struct cmd_list_element *show_python_list; | |
337 | ||
338 | /* Function for use by 'maint set python' prefix command. */ | |
339 | ||
340 | static void | |
341 | set_python (char *args, int from_tty) | |
342 | { | |
343 | help_list (set_python_list, "maintenance set python ", -1, gdb_stdout); | |
344 | } | |
345 | ||
346 | /* Function for use by 'maint show python' prefix command. */ | |
347 | ||
348 | static void | |
349 | show_python (char *args, int from_tty) | |
350 | { | |
351 | cmd_show_list (show_python_list, from_tty, ""); | |
352 | } | |
353 | ||
354 | /* Initialize the Python code. */ | |
355 | ||
356 | void | |
357 | _initialize_python (void) | |
358 | { | |
359 | add_com ("python", class_obscure, python_command, | |
360 | #ifdef HAVE_PYTHON | |
361 | _("\ | |
362 | Evaluate a Python command.\n\ | |
363 | \n\ | |
364 | The command can be given as an argument, for instance:\n\ | |
365 | \n\ | |
366 | python print 23\n\ | |
367 | \n\ | |
368 | If no argument is given, the following lines are read and used\n\ | |
369 | as the Python commands. Type a line containing \"end\" to indicate\n\ | |
370 | the end of the command.") | |
371 | #else /* HAVE_PYTHON */ | |
372 | _("\ | |
373 | Evaluate a Python command.\n\ | |
374 | \n\ | |
375 | Python scripting is not supported in this copy of GDB.\n\ | |
376 | This command is only a placeholder.") | |
377 | #endif /* HAVE_PYTHON */ | |
378 | ); | |
379 | ||
380 | add_prefix_cmd ("python", no_class, show_python, | |
381 | _("Prefix command for python maintenance settings."), | |
509238d6 | 382 | &show_python_list, "maintenance show python ", 0, |
d57a3c85 TJB |
383 | &maintenance_show_cmdlist); |
384 | add_prefix_cmd ("python", no_class, set_python, | |
385 | _("Prefix command for python maintenance settings."), | |
509238d6 | 386 | &set_python_list, "maintenance set python ", 0, |
d57a3c85 TJB |
387 | &maintenance_set_cmdlist); |
388 | ||
389 | add_setshow_boolean_cmd ("print-stack", class_maintenance, | |
390 | &gdbpy_should_print_stack, _("\ | |
391 | Enable or disable printing of Python stack dump on error."), _("\ | |
392 | Show whether Python stack will be printed on error."), _("\ | |
393 | Enables or disables printing of Python stack traces."), | |
394 | NULL, NULL, | |
395 | &set_python_list, | |
396 | &show_python_list); | |
397 | ||
398 | #ifdef HAVE_PYTHON | |
399 | Py_Initialize (); | |
ca30a762 | 400 | PyEval_InitThreads (); |
d57a3c85 TJB |
401 | |
402 | gdb_module = Py_InitModule ("gdb", GdbMethods); | |
403 | ||
404 | /* The casts to (char*) are for python 2.4. */ | |
405 | PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version); | |
406 | PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name); | |
407 | PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name); | |
408 | ||
a08702d6 TJB |
409 | gdbpy_initialize_values (); |
410 | ||
d57a3c85 TJB |
411 | PyRun_SimpleString ("import gdb"); |
412 | ||
413 | /* Create a couple objects which are used for Python's stdout and | |
414 | stderr. */ | |
415 | PyRun_SimpleString ("\ | |
416 | import sys\n\ | |
417 | class GdbOutputFile:\n\ | |
418 | def close(self):\n\ | |
419 | # Do nothing.\n\ | |
420 | return None\n\ | |
421 | \n\ | |
422 | def isatty(self):\n\ | |
423 | return False\n\ | |
424 | \n\ | |
425 | def write(self, s):\n\ | |
426 | gdb.write(s)\n\ | |
427 | \n\ | |
428 | def writelines(self, iterable):\n\ | |
429 | for line in iterable:\n\ | |
430 | self.write(line)\n\ | |
431 | \n\ | |
432 | def flush(self):\n\ | |
433 | gdb.flush()\n\ | |
434 | \n\ | |
435 | sys.stderr = GdbOutputFile()\n\ | |
436 | sys.stdout = GdbOutputFile()\n\ | |
437 | "); | |
ca30a762 TT |
438 | |
439 | /* Release the GIL while gdb runs. */ | |
440 | PyThreadState_Swap (NULL); | |
441 | PyEval_ReleaseLock (); | |
442 | ||
d57a3c85 TJB |
443 | #endif /* HAVE_PYTHON */ |
444 | } | |
12453b93 TJB |
445 | |
446 | \f | |
447 | ||
448 | #if HAVE_PYTHON | |
449 | ||
450 | static PyMethodDef GdbMethods[] = | |
451 | { | |
452 | { "history", gdbpy_history, METH_VARARGS, | |
453 | "Get a value from history" }, | |
454 | { "execute", execute_gdb_command, METH_VARARGS, | |
455 | "Execute a gdb command" }, | |
456 | { "get_parameter", get_parameter, METH_VARARGS, | |
457 | "Return a gdb parameter's value" }, | |
458 | ||
459 | { "write", gdbpy_write, METH_VARARGS, | |
460 | "Write a string using gdb's filtered stream." }, | |
461 | { "flush", gdbpy_flush, METH_NOARGS, | |
462 | "Flush gdb's filtered stdout stream." }, | |
463 | ||
464 | {NULL, NULL, 0, NULL} | |
465 | }; | |
466 | ||
467 | #endif /* HAVE_PYTHON */ |