#include "exceptions.h"
#include "event-loop.h"
#include "serial.h"
+#include "readline/tilde.h"
#include "python.h"
#include <ctype.h>
-/* True if we should print the stack when catching a Python error,
- false otherwise. */
-static int gdbpy_should_print_stack = 0;
+/* Declared constants and enum for python stack printing. */
+static const char python_excp_none[] = "none";
+static const char python_excp_full[] = "full";
+static const char python_excp_message[] = "message";
+
+/* "set python print-stack" choices. */
+static const char *python_excp_enums[] =
+ {
+ python_excp_none,
+ python_excp_full,
+ python_excp_message,
+ NULL
+ };
+
+/* The exception printing variable. 'full' if we want to print the
+ error message and stack, 'none' if we want to print nothing, and
+ 'message' if we only want to print the error message. 'message' is
+ the default. */
+static const char *gdbpy_should_print_stack = python_excp_message;
#ifdef HAVE_PYTHON
#include "version.h"
#include "target.h"
#include "gdbthread.h"
+#include "observer.h"
+#include "interps.h"
static PyMethodDef GdbMethods[];
return make_cleanup (restore_python_env, env);
}
+/* A wrapper around PyRun_SimpleFile. FILENAME is the name of
+ the Python script to run.
+
+ One of the parameters of PyRun_SimpleFile is a FILE *.
+ The problem is that type FILE is extremely system and compiler
+ dependent. So, unless the Python library has been compiled using
+ the same build environment as GDB, we run the risk of getting
+ a crash due to inconsistencies between the definition used by GDB,
+ and the definition used by Python. A mismatch can very likely
+ lead to a crash.
+
+ There is also the situation where the Python library and GDB
+ are using two different versions of the C runtime library.
+ This is particularly visible on Windows, where few users would
+ build Python themselves (this is no trivial task on this platform),
+ and thus use binaries built by someone else instead. Python,
+ being built with VC, would use one version of the msvcr DLL
+ (Eg. msvcr100.dll), while MinGW uses msvcrt.dll. A FILE *
+ from one runtime does not necessarily operate correctly in
+ the other runtime.
+
+ To work around this potential issue, we create the FILE object
+ using Python routines, thus making sure that it is compatible
+ with the Python library. */
+
+static void
+python_run_simple_file (const char *filename)
+{
+ char *full_path;
+ PyObject *python_file;
+ struct cleanup *cleanup;
+
+ /* Because we have a string for a filename, and are using Python to
+ open the file, we need to expand any tilde in the path first. */
+ full_path = tilde_expand (filename);
+ cleanup = make_cleanup (xfree, full_path);
+ python_file = PyFile_FromString (full_path, "r");
+ if (! python_file)
+ {
+ do_cleanups (cleanup);
+ gdbpy_print_stack ();
+ error (_("Error while opening file: %s"), full_path);
+ }
+
+ make_cleanup_py_decref (python_file);
+ PyRun_SimpleFile (PyFile_AsFile (python_file), filename);
+ do_cleanups (cleanup);
+}
/* Given a command_line, return a command string suitable for passing
to Python. Lines in the string are separated by newlines. The
ret = PyRun_SimpleString (script);
xfree (script);
if (ret)
- {
- gdbpy_print_stack ();
- error (_("Error while executing Python code."));
- }
+ error (_("Error while executing Python code."));
do_cleanups (cleanup);
}
struct cleanup *cleanup;
cleanup = ensure_python_env (get_current_arch (), current_language);
+
+ make_cleanup_restore_integer (&interpreter_async);
+ interpreter_async = 0;
+
while (arg && *arg && isspace (*arg))
++arg;
if (arg && *arg)
{
if (PyRun_SimpleString (arg))
- {
- gdbpy_print_stack ();
- error (_("Error while executing Python code."));
- }
+ error (_("Error while executing Python code."));
}
else
{
char *copy = xstrdup (arg);
struct cleanup *cleanup = make_cleanup (xfree, copy);
+ make_cleanup_restore_integer (&interpreter_async);
+ interpreter_async = 0;
+
prevent_dont_repeat ();
if (to_string)
result = execute_command_to_string (copy, from_tty);
if (! PyArg_ParseTuple (args, "|s", &arg))
return NULL;
- cleanups = ensure_python_env (get_current_arch (), current_language);
+ cleanups = make_cleanup (null_cleanup, NULL);
TRY_CATCH (except, RETURN_MASK_ALL)
{
{
copy = xstrdup (arg);
make_cleanup (xfree, copy);
- sals = decode_line_1 (©, 0, 0, 0, 0);
+ sals = decode_line_1 (©, 0, 0, 0);
make_cleanup (xfree, sals.sals);
}
else
return value_to_value_object (result);
}
-/* Read a file as Python code. STREAM is the input file; FILE is the
- name of the file.
- STREAM is not closed, that is the caller's responsibility. */
+/* Read a file as Python code.
+ FILE is the name of the file.
+ This does not throw any errors. If an exception occurs python will print
+ the traceback and clear the error indicator. */
void
-source_python_script (FILE *stream, const char *file)
+source_python_script (const char *file)
{
struct cleanup *cleanup;
cleanup = ensure_python_env (get_current_arch (), current_language);
-
- /* Note: If an exception occurs python will print the traceback and
- clear the error indicator. */
- PyRun_SimpleFile (stream, file);
-
+ python_run_simple_file (file);
do_cleanups (cleanup);
}
}
}
+\f
+
+static void
+before_prompt_hook (const char *current_gdb_prompt)
+{
+ struct cleanup *cleanup;
+ char *prompt = NULL;
+
+ cleanup = ensure_python_env (get_current_arch (), current_language);
+
+ if (PyObject_HasAttrString (gdb_module, "prompt_hook"))
+ {
+ PyObject *hook;
+
+ hook = PyObject_GetAttrString (gdb_module, "prompt_hook");
+ if (hook == NULL)
+ goto fail;
+
+ if (PyCallable_Check (hook))
+ {
+ PyObject *result;
+ PyObject *current_prompt;
+
+ current_prompt = PyString_FromString (current_gdb_prompt);
+ if (current_prompt == NULL)
+ goto fail;
+
+ result = PyObject_CallFunctionObjArgs (hook, current_prompt, NULL);
+
+ Py_DECREF (current_prompt);
+
+ if (result == NULL)
+ goto fail;
+
+ make_cleanup_py_decref (result);
+
+ /* Return type should be None, or a String. If it is None,
+ fall through, we will not set a prompt. If it is a
+ string, set PROMPT. Anything else, set an exception. */
+ if (result != Py_None && ! PyString_Check (result))
+ {
+ PyErr_Format (PyExc_RuntimeError,
+ _("Return from prompt_hook must " \
+ "be either a Python string, or None"));
+ goto fail;
+ }
+
+ if (result != Py_None)
+ {
+ prompt = python_string_to_host_string (result);
+
+ if (prompt == NULL)
+ goto fail;
+ else
+ make_cleanup (xfree, prompt);
+ }
+ }
+ }
+
+ /* If a prompt has been set, PROMPT will not be NULL. If it is
+ NULL, do not set the prompt. */
+ if (prompt != NULL)
+ set_prompt (prompt);
+
+ do_cleanups (cleanup);
+ return;
+
+ fail:
+ gdbpy_print_stack ();
+ do_cleanups (cleanup);
+ return;
+}
+
+\f
+
/* Printing. */
/* A python function to write a single string using gdb's filtered
Py_RETURN_NONE;
}
-/* Print a python exception trace, or print nothing and clear the
- python exception, depending on gdbpy_should_print_stack. Only call
- this if a python exception is set. */
+/* Print a python exception trace, print just a message, or print
+ nothing and clear the python exception, depending on
+ gdbpy_should_print_stack. Only call this if a python exception is
+ set. */
void
gdbpy_print_stack (void)
{
- if (gdbpy_should_print_stack)
+ /* Print "none", just clear exception. */
+ if (gdbpy_should_print_stack == python_excp_none)
+ {
+ PyErr_Clear ();
+ }
+ /* Print "full" message and backtrace. */
+ else if (gdbpy_should_print_stack == python_excp_full)
{
PyErr_Print ();
/* PyErr_Print doesn't necessarily end output with a newline.
printf_filtered. */
begin_line ();
}
+ /* Print "message", just error print message. */
else
- PyErr_Clear ();
+ {
+ PyObject *ptype, *pvalue, *ptraceback;
+ char *msg = NULL, *type = NULL;
+
+ PyErr_Fetch (&ptype, &pvalue, &ptraceback);
+
+ /* Fetch the error message contained within ptype, pvalue. */
+ msg = gdbpy_exception_to_string (ptype, pvalue);
+ type = gdbpy_obj_to_string (ptype);
+ if (msg == NULL)
+ {
+ /* An error occurred computing the string representation of the
+ error message. */
+ fprintf_filtered (gdb_stderr,
+ _("Error occurred computing Python error" \
+ "message.\n"));
+ }
+ else
+ fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
+ type, msg);
+
+ Py_XDECREF (ptype);
+ Py_XDECREF (pvalue);
+ Py_XDECREF (ptraceback);
+ xfree (msg);
+ }
}
\f
source_python_script_for_objfile; it is NULL at other times. */
static struct objfile *gdbpy_current_objfile;
-/* Set the current objfile to OBJFILE and then read STREAM,FILE as
- Python code. */
+/* Set the current objfile to OBJFILE and then read FILE as Python code.
+ This does not throw any errors. If an exception occurs python will print
+ the traceback and clear the error indicator. */
void
-source_python_script_for_objfile (struct objfile *objfile,
- FILE *stream, const char *file)
+source_python_script_for_objfile (struct objfile *objfile, const char *file)
{
struct cleanup *cleanups;
cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
gdbpy_current_objfile = objfile;
- /* Note: If an exception occurs python will print the traceback and
- clear the error indicator. */
- PyRun_SimpleFile (stream, file);
+ python_run_simple_file (file);
do_cleanups (cleanups);
gdbpy_current_objfile = NULL;
}
void
-source_python_script (FILE *stream, const char *file)
+source_python_script (const char *file)
{
throw_error (UNSUPPORTED_ERROR,
_("Python scripting is not supported in this copy of GDB."));
\f
-/* Lists for 'maint set python' commands. */
-
-static struct cmd_list_element *maint_set_python_list;
-static struct cmd_list_element *maint_show_python_list;
-
/* Lists for 'set python' commands. */
static struct cmd_list_element *user_set_python_list;
static struct cmd_list_element *user_show_python_list;
-/* Function for use by 'maint set python' prefix command. */
-
-static void
-maint_set_python (char *args, int from_tty)
-{
- help_list (maint_set_python_list, "maintenance set python ",
- class_deprecated, gdb_stdout);
-}
-
-/* Function for use by 'maint show python' prefix command. */
-
-static void
-maint_show_python (char *args, int from_tty)
-{
- cmd_show_list (maint_show_python_list, from_tty, "");
-}
-
/* Function for use by 'set python' prefix command. */
static void
#endif /* HAVE_PYTHON */
);
- add_prefix_cmd ("python", no_class, maint_show_python,
- _("Prefix command for python maintenance settings."),
- &maint_show_python_list, "maintenance show python ", 0,
- &maintenance_show_cmdlist);
- add_prefix_cmd ("python", no_class, maint_set_python,
- _("Prefix command for python maintenance settings."),
- &maint_set_python_list, "maintenance set python ", 0,
- &maintenance_set_cmdlist);
-
- add_setshow_boolean_cmd ("print-stack", class_maintenance,
- &gdbpy_should_print_stack, _("\
-Enable or disable printing of Python stack dump on error."), _("\
-Show whether Python stack will be printed on error."), _("\
-Enables or disables printing of Python stack traces."),
- NULL, NULL,
- &maint_set_python_list,
- &maint_show_python_list);
-
- /* Deprecate maint set/show python print-stack in favour of
- non-maintenance alternatives. */
- cmd_name = "print-stack";
- cmd = lookup_cmd (&cmd_name, maint_set_python_list, "", -1, 0);
- deprecate_cmd (cmd, "set python print-stack");
- cmd_name = "print-stack"; /* Reset name. */
- cmd = lookup_cmd (&cmd_name, maint_show_python_list, "", -1, 0);
- deprecate_cmd (cmd, "show python print-stack");
-
/* Add set/show python print-stack. */
add_prefix_cmd ("python", no_class, user_show_python,
_("Prefix command for python preference settings."),
&user_set_python_list, "set python ", 0,
&setlist);
- add_setshow_boolean_cmd ("print-stack", no_class,
- &gdbpy_should_print_stack, _("\
-Enable or disable printing of Python stack dump on error."), _("\
-Show whether Python stack will be printed on error."), _("\
-Enables or disables printing of Python stack traces."),
- NULL, NULL,
- &user_set_python_list,
- &user_show_python_list);
+ add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums,
+ &gdbpy_should_print_stack, _("\
+Set mode for Python stack dump on error."), _("\
+Show the mode of Python stack printing on error."), _("\
+none == no stack or message will be printed.\n\
+full == a message and a stack will be printed.\n\
+message == an error message without a stack will be printed."),
+ NULL, NULL,
+ &user_set_python_list,
+ &user_show_python_list);
#ifdef HAVE_PYTHON
#ifdef WITH_PYTHON_PATH
gdbpy_initialize_pspace ();
gdbpy_initialize_objfile ();
gdbpy_initialize_breakpoints ();
+ gdbpy_initialize_finishbreakpoints ();
gdbpy_initialize_lazy_string ();
gdbpy_initialize_thread ();
gdbpy_initialize_inferior ();
gdbpy_initialize_continue_event ();
gdbpy_initialize_exited_event ();
gdbpy_initialize_thread_event ();
+ gdbpy_initialize_new_objfile_event () ;
+
+ observer_attach_before_prompt (before_prompt_hook);
PyRun_SimpleString ("import gdb");
PyRun_SimpleString ("gdb.pretty_printers = []");
sys.path.insert (0, gdb.PYTHONDIR)\n\
\n\
# Tell python where to find submodules of gdb.\n\
- gdb.__path__ = [gdb.PYTHONDIR + '/gdb']\n\
+ gdb.__path__ = [os.path.join (gdb.PYTHONDIR, 'gdb')]\n\
\n\
# The gdb module is implemented in C rather than in Python. As a result,\n\
# the associated __init.py__ script is not not executed by default when\n\
# the gdb module gets imported. Execute that script manually if it\n\
# exists.\n\
- ipy = gdb.PYTHONDIR + '/gdb/__init__.py'\n\
+ ipy = os.path.join (gdb.PYTHONDIR, 'gdb', '__init__.py')\n\
if os.path.exists (ipy):\n\
execfile (ipy)\n\
\n\
# Install the default gdb.PYTHONDIR.\n\
GdbSetPythonDirectory (gdb.PYTHONDIR)\n\
+# Default prompt hook does nothing.\n\
+prompt_hook = None\n\
");
do_cleanups (cleanup);
{ "selected_thread", gdbpy_selected_thread, METH_NOARGS,
"selected_thread () -> gdb.InferiorThread.\n\
Return the selected thread object." },
+ { "selected_inferior", gdbpy_selected_inferior, METH_NOARGS,
+ "selected_inferior () -> gdb.Inferior.\n\
+Return the selected inferior object." },
{ "inferiors", gdbpy_inferiors, METH_NOARGS,
"inferiors () -> (gdb.Inferior, ...).\n\
Return a tuple containing all inferiors." },