1 /* General python/gdb code
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "cli/cli-script.h"
28 /* True if we should print the stack when catching a Python error,
30 static int gdbpy_should_print_stack
= 1;
35 #include "libiberty.h"
36 #include "cli/cli-decode.h"
39 #include "exceptions.h"
40 #include "python-internal.h"
43 #include "gdbthread.h"
45 static PyMethodDef GdbMethods
[];
49 PyObject
*gdbpy_doc_cst
;
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. */
57 compute_python_string (struct command_line
*l
)
59 struct command_line
*iter
;
64 for (iter
= l
; iter
; iter
= iter
->next
)
65 size
+= strlen (iter
->line
) + 1;
67 script
= xmalloc (size
+ 1);
69 for (iter
= l
; iter
; iter
= iter
->next
)
71 int len
= strlen (iter
->line
);
72 strcpy (&script
[here
], iter
->line
);
74 script
[here
++] = '\n';
80 /* Take a command line structure representing a 'python' command, and
81 evaluate its body using the Python interpreter. */
84 eval_python_from_control_command (struct command_line
*cmd
)
88 struct cleanup
*cleanup
;
89 PyGILState_STATE state
;
91 if (cmd
->body_count
!= 1)
92 error (_("Invalid \"python\" block structure."));
94 state
= PyGILState_Ensure ();
95 cleanup
= make_cleanup_py_restore_gil (&state
);
97 script
= compute_python_string (cmd
->body_list
[0]);
98 ret
= PyRun_SimpleString (script
);
102 gdbpy_print_stack ();
103 error (_("Error while executing Python code."));
106 do_cleanups (cleanup
);
109 /* Implementation of the gdb "python" command. */
112 python_command (char *arg
, int from_tty
)
114 struct cleanup
*cleanup
;
115 PyGILState_STATE state
;
117 state
= PyGILState_Ensure ();
118 cleanup
= make_cleanup_py_restore_gil (&state
);
120 while (arg
&& *arg
&& isspace (*arg
))
124 if (PyRun_SimpleString (arg
))
126 gdbpy_print_stack ();
127 error (_("Error while executing Python code."));
132 struct command_line
*l
= get_command_line (python_control
, "");
133 make_cleanup_free_command_lines (&l
);
134 execute_control_command_untraced (l
);
137 do_cleanups (cleanup
);
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
147 parameter_to_python (struct cmd_list_element
*cmd
)
149 switch (cmd
->var_type
)
152 case var_string_noescape
:
153 case var_optional_filename
:
157 char *str
= * (char **) cmd
->var
;
160 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
165 if (* (int *) cmd
->var
)
171 case var_auto_boolean
:
173 enum auto_boolean ab
= * (enum auto_boolean
*) cmd
->var
;
174 if (ab
== AUTO_BOOLEAN_TRUE
)
176 else if (ab
== AUTO_BOOLEAN_FALSE
)
183 if ((* (int *) cmd
->var
) == INT_MAX
)
187 return PyLong_FromLong (* (int *) cmd
->var
);
191 unsigned int val
= * (unsigned int *) cmd
->var
;
194 return PyLong_FromUnsignedLong (val
);
198 return PyErr_Format (PyExc_RuntimeError
, "programmer error: unhandled type");
201 /* A Python function which returns a gdb parameter's value as a Python
205 get_parameter (PyObject
*self
, PyObject
*args
)
207 struct cmd_list_element
*alias
, *prefix
, *cmd
;
209 volatile struct gdb_exception except
;
211 if (! PyArg_ParseTuple (args
, "s", &arg
))
214 newarg
= concat ("show ", arg
, (char *) NULL
);
216 TRY_CATCH (except
, RETURN_MASK_ALL
)
218 if (! lookup_cmd_composition (newarg
, &alias
, &prefix
, &cmd
))
221 return PyErr_Format (PyExc_RuntimeError
,
222 "could not find variable `%s'", arg
);
226 GDB_PY_HANDLE_EXCEPTION (except
);
229 return PyErr_Format (PyExc_RuntimeError
, "`%s' is not a variable", arg
);
230 return parameter_to_python (cmd
);
233 /* A Python function which evaluates a string using the gdb CLI. */
236 execute_gdb_command (PyObject
*self
, PyObject
*args
)
238 struct cmd_list_element
*alias
, *prefix
, *cmd
;
240 PyObject
*from_tty_obj
= NULL
;
243 volatile struct gdb_exception except
;
245 if (! PyArg_ParseTuple (args
, "s|O!", &arg
, &PyBool_Type
, &from_tty_obj
))
251 cmp
= PyObject_IsTrue (from_tty_obj
);
257 TRY_CATCH (except
, RETURN_MASK_ALL
)
259 execute_command (arg
, from_tty
);
261 GDB_PY_HANDLE_EXCEPTION (except
);
263 /* Do any commands attached to breakpoint we stopped at. */
264 bpstat_do_actions ();
273 /* A python function to write a single string using gdb's filtered
276 gdbpy_write (PyObject
*self
, PyObject
*args
)
279 if (! PyArg_ParseTuple (args
, "s", &arg
))
281 printf_filtered ("%s", arg
);
285 /* A python function to flush gdb's filtered output stream. */
287 gdbpy_flush (PyObject
*self
, PyObject
*args
)
289 gdb_flush (gdb_stdout
);
293 /* Print a python exception trace, or print nothing and clear the
294 python exception, depending on gdbpy_should_print_stack. Only call
295 this if a python exception is set. */
297 gdbpy_print_stack (void)
299 if (gdbpy_should_print_stack
)
305 #else /* HAVE_PYTHON */
307 /* Dummy implementation of the gdb "python" command. */
310 python_command (char *arg
, int from_tty
)
312 while (arg
&& *arg
&& isspace (*arg
))
315 error (_("Python scripting is not supported in this copy of GDB."));
318 struct command_line
*l
= get_command_line (python_control
, "");
319 struct cleanup
*cleanups
= make_cleanup_free_command_lines (&l
);
320 execute_control_command_untraced (l
);
321 do_cleanups (cleanups
);
326 eval_python_from_control_command (struct command_line
*cmd
)
328 error (_("Python scripting is not supported in this copy of GDB."));
331 #endif /* HAVE_PYTHON */
335 /* Lists for 'maint set python' commands. */
337 static struct cmd_list_element
*set_python_list
;
338 static struct cmd_list_element
*show_python_list
;
340 /* Function for use by 'maint set python' prefix command. */
343 set_python (char *args
, int from_tty
)
345 help_list (set_python_list
, "maintenance set python ", -1, gdb_stdout
);
348 /* Function for use by 'maint show python' prefix command. */
351 show_python (char *args
, int from_tty
)
353 cmd_show_list (show_python_list
, from_tty
, "");
356 /* Initialize the Python code. */
359 _initialize_python (void)
361 add_com ("python", class_obscure
, python_command
,
364 Evaluate a Python command.\n\
366 The command can be given as an argument, for instance:\n\
370 If no argument is given, the following lines are read and used\n\
371 as the Python commands. Type a line containing \"end\" to indicate\n\
372 the end of the command.")
373 #else /* HAVE_PYTHON */
375 Evaluate a Python command.\n\
377 Python scripting is not supported in this copy of GDB.\n\
378 This command is only a placeholder.")
379 #endif /* HAVE_PYTHON */
382 add_prefix_cmd ("python", no_class
, show_python
,
383 _("Prefix command for python maintenance settings."),
384 &show_python_list
, "maintenance show python ", 0,
385 &maintenance_show_cmdlist
);
386 add_prefix_cmd ("python", no_class
, set_python
,
387 _("Prefix command for python maintenance settings."),
388 &set_python_list
, "maintenance set python ", 0,
389 &maintenance_set_cmdlist
);
391 add_setshow_boolean_cmd ("print-stack", class_maintenance
,
392 &gdbpy_should_print_stack
, _("\
393 Enable or disable printing of Python stack dump on error."), _("\
394 Show whether Python stack will be printed on error."), _("\
395 Enables or disables printing of Python stack traces."),
402 PyEval_InitThreads ();
404 gdb_module
= Py_InitModule ("gdb", GdbMethods
);
406 /* The casts to (char*) are for python 2.4. */
407 PyModule_AddStringConstant (gdb_module
, "VERSION", (char*) version
);
408 PyModule_AddStringConstant (gdb_module
, "HOST_CONFIG", (char*) host_name
);
409 PyModule_AddStringConstant (gdb_module
, "TARGET_CONFIG", (char*) target_name
);
411 gdbpy_initialize_values ();
412 gdbpy_initialize_commands ();
414 PyRun_SimpleString ("import gdb");
416 gdbpy_doc_cst
= PyString_FromString ("__doc__");
418 /* Create a couple objects which are used for Python's stdout and
420 PyRun_SimpleString ("\
422 class GdbOutputFile:\n\
430 def write(self, s):\n\
433 def writelines(self, iterable):\n\
434 for line in iterable:\n\
440 sys.stderr = GdbOutputFile()\n\
441 sys.stdout = GdbOutputFile()\n\
444 /* Release the GIL while gdb runs. */
445 PyThreadState_Swap (NULL
);
446 PyEval_ReleaseLock ();
448 #endif /* HAVE_PYTHON */
455 static PyMethodDef GdbMethods
[] =
457 { "history", gdbpy_history
, METH_VARARGS
,
458 "Get a value from history" },
459 { "execute", execute_gdb_command
, METH_VARARGS
,
460 "Execute a gdb command" },
461 { "get_parameter", get_parameter
, METH_VARARGS
,
462 "Return a gdb parameter's value" },
464 { "write", gdbpy_write
, METH_VARARGS
,
465 "Write a string using gdb's filtered stream." },
466 { "flush", gdbpy_flush
, METH_NOARGS
,
467 "Flush gdb's filtered stdout stream." },
469 {NULL
, NULL
, 0, NULL
}
472 #endif /* HAVE_PYTHON */
This page took 0.03811 seconds and 4 git commands to generate.