96bb5f576ff617b49940baab72668d61f961f9b7
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 /* 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. */
55 compute_python_string (struct command_line
*l
)
57 struct command_line
*iter
;
62 for (iter
= l
; iter
; iter
= iter
->next
)
63 size
+= strlen (iter
->line
) + 1;
65 script
= xmalloc (size
+ 1);
67 for (iter
= l
; iter
; iter
= iter
->next
)
69 int len
= strlen (iter
->line
);
70 strcpy (&script
[here
], iter
->line
);
72 script
[here
++] = '\n';
78 /* Take a command line structure representing a 'python' command, and
79 evaluate its body using the Python interpreter. */
82 eval_python_from_control_command (struct command_line
*cmd
)
86 struct cleanup
*cleanup
;
87 PyGILState_STATE state
;
89 if (cmd
->body_count
!= 1)
90 error (_("Invalid \"python\" block structure."));
92 state
= PyGILState_Ensure ();
93 cleanup
= make_cleanup_py_restore_gil (&state
);
95 script
= compute_python_string (cmd
->body_list
[0]);
96 ret
= PyRun_SimpleString (script
);
100 gdbpy_print_stack ();
101 error (_("Error while executing Python code."));
104 do_cleanups (cleanup
);
107 /* Implementation of the gdb "python" command. */
110 python_command (char *arg
, int from_tty
)
112 struct cleanup
*cleanup
;
113 PyGILState_STATE state
;
115 state
= PyGILState_Ensure ();
116 cleanup
= make_cleanup_py_restore_gil (&state
);
118 while (arg
&& *arg
&& isspace (*arg
))
122 if (PyRun_SimpleString (arg
))
124 gdbpy_print_stack ();
125 error (_("Error while executing Python code."));
130 struct command_line
*l
= get_command_line (python_control
, "");
131 make_cleanup_free_command_lines (&l
);
132 execute_control_command_untraced (l
);
135 do_cleanups (cleanup
);
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
145 parameter_to_python (struct cmd_list_element
*cmd
)
147 switch (cmd
->var_type
)
150 case var_string_noescape
:
151 case var_optional_filename
:
155 char *str
= * (char **) cmd
->var
;
158 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
163 if (* (int *) cmd
->var
)
169 case var_auto_boolean
:
171 enum auto_boolean ab
= * (enum auto_boolean
*) cmd
->var
;
172 if (ab
== AUTO_BOOLEAN_TRUE
)
174 else if (ab
== AUTO_BOOLEAN_FALSE
)
181 if ((* (int *) cmd
->var
) == INT_MAX
)
185 return PyLong_FromLong (* (int *) cmd
->var
);
189 unsigned int val
= * (unsigned int *) cmd
->var
;
192 return PyLong_FromUnsignedLong (val
);
196 return PyErr_Format (PyExc_RuntimeError
, "programmer error: unhandled type");
199 /* A Python function which returns a gdb parameter's value as a Python
203 get_parameter (PyObject
*self
, PyObject
*args
)
205 struct cmd_list_element
*alias
, *prefix
, *cmd
;
207 volatile struct gdb_exception except
;
209 if (! PyArg_ParseTuple (args
, "s", &arg
))
212 newarg
= concat ("show ", arg
, (char *) NULL
);
214 TRY_CATCH (except
, RETURN_MASK_ALL
)
216 if (! lookup_cmd_composition (newarg
, &alias
, &prefix
, &cmd
))
219 return PyErr_Format (PyExc_RuntimeError
,
220 "could not find variable `%s'", arg
);
224 GDB_PY_HANDLE_EXCEPTION (except
);
227 return PyErr_Format (PyExc_RuntimeError
, "`%s' is not a variable", arg
);
228 return parameter_to_python (cmd
);
231 /* A Python function which evaluates a string using the gdb CLI. */
234 execute_gdb_command (PyObject
*self
, PyObject
*args
)
236 struct cmd_list_element
*alias
, *prefix
, *cmd
;
238 PyObject
*from_tty_obj
= NULL
;
241 volatile struct gdb_exception except
;
243 if (! PyArg_ParseTuple (args
, "s|O!", &arg
, &PyBool_Type
, &from_tty_obj
))
249 cmp
= PyObject_IsTrue (from_tty_obj
);
255 TRY_CATCH (except
, RETURN_MASK_ALL
)
257 execute_command (arg
, from_tty
);
259 GDB_PY_HANDLE_EXCEPTION (except
);
261 /* Do any commands attached to breakpoint we stopped at. */
262 bpstat_do_actions ();
271 /* A python function to write a single string using gdb's filtered
274 gdbpy_write (PyObject
*self
, PyObject
*args
)
277 if (! PyArg_ParseTuple (args
, "s", &arg
))
279 printf_filtered ("%s", arg
);
283 /* A python function to flush gdb's filtered output stream. */
285 gdbpy_flush (PyObject
*self
, PyObject
*args
)
287 gdb_flush (gdb_stdout
);
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. */
295 gdbpy_print_stack (void)
297 if (gdbpy_should_print_stack
)
303 #else /* HAVE_PYTHON */
305 /* Dummy implementation of the gdb "python" command. */
308 python_command (char *arg
, int from_tty
)
310 while (arg
&& *arg
&& isspace (*arg
))
313 error (_("Python scripting is not supported in this copy of GDB."));
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
);
324 eval_python_from_control_command (struct command_line
*cmd
)
326 error (_("Python scripting is not supported in this copy of GDB."));
329 #endif /* HAVE_PYTHON */
333 /* Lists for 'maint set python' commands. */
335 static struct cmd_list_element
*set_python_list
;
336 static struct cmd_list_element
*show_python_list
;
338 /* Function for use by 'maint set python' prefix command. */
341 set_python (char *args
, int from_tty
)
343 help_list (set_python_list
, "maintenance set python ", -1, gdb_stdout
);
346 /* Function for use by 'maint show python' prefix command. */
349 show_python (char *args
, int from_tty
)
351 cmd_show_list (show_python_list
, from_tty
, "");
354 /* Initialize the Python code. */
357 _initialize_python (void)
359 add_com ("python", class_obscure
, python_command
,
362 Evaluate a Python command.\n\
364 The command can be given as an argument, for instance:\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 */
373 Evaluate a Python command.\n\
375 Python scripting is not supported in this copy of GDB.\n\
376 This command is only a placeholder.")
377 #endif /* HAVE_PYTHON */
380 add_prefix_cmd ("python", no_class
, show_python
,
381 _("Prefix command for python maintenance settings."),
382 &show_python_list
, "maintenance show python ", 0,
383 &maintenance_show_cmdlist
);
384 add_prefix_cmd ("python", no_class
, set_python
,
385 _("Prefix command for python maintenance settings."),
386 &set_python_list
, "maintenance set python ", 0,
387 &maintenance_set_cmdlist
);
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."),
400 PyEval_InitThreads ();
402 gdb_module
= Py_InitModule ("gdb", GdbMethods
);
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
);
409 gdbpy_initialize_values ();
411 PyRun_SimpleString ("import gdb");
413 /* Create a couple objects which are used for Python's stdout and
415 PyRun_SimpleString ("\
417 class GdbOutputFile:\n\
425 def write(self, s):\n\
428 def writelines(self, iterable):\n\
429 for line in iterable:\n\
435 sys.stderr = GdbOutputFile()\n\
436 sys.stdout = GdbOutputFile()\n\
439 /* Release the GIL while gdb runs. */
440 PyThreadState_Swap (NULL
);
441 PyEval_ReleaseLock ();
443 #endif /* HAVE_PYTHON */
450 static PyMethodDef GdbMethods
[] =
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" },
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." },
464 {NULL
, NULL
, 0, NULL
}
467 #endif /* HAVE_PYTHON */
This page took 0.06921 seconds and 3 git commands to generate.