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