aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/python.c
diff options
context:
space:
mode:
authorPhil Muldoon <pmuldoon@redhat.com>2011-07-21 11:03:48 +0000
committerPhil Muldoon <pmuldoon@redhat.com>2011-07-21 11:03:48 +0000
commitd17b6f81012b6844de08934193fe7cb7db8cbd5f (patch)
tree38de35dc7c7f44f1f76fbe6af3bed4037b600e00 /gdb/python/python.c
parent3779080d04bb9504542076e02969f77dff46ae63 (diff)
downloadgdb-d17b6f81012b6844de08934193fe7cb7db8cbd5f.zip
gdb-d17b6f81012b6844de08934193fe7cb7db8cbd5f.tar.gz
gdb-d17b6f81012b6844de08934193fe7cb7db8cbd5f.tar.bz2
2011-07-21 Phil Muldoon <pmuldoon@redhat.com>
Tom Tromey <tromey@redhat.com> * top.c (set_prompt): Rewrite to free previous prompt, free asynch_new_prompt and set both on new prompts. * event-top.c (display_gdb_prompt): Add prompt substitution logic. * python/python.c (before_prompt_hook): New function. 2011-07-21 Phil Muldoon <pmuldoon@redhat.com> * gdb.python/python.exp: Add prompt substitution tests. 2011-07-21 Phil Muldoon <pmuldoon@redhat.com> * observer.texi (GDB Observers): Add before_prompt observer. * gdb.texinfo (Basic Python): Add documentation for prompt substitution.
Diffstat (limited to 'gdb/python/python.c')
-rw-r--r--gdb/python/python.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/gdb/python/python.c b/gdb/python/python.c
index f15936d..e3a8d19 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -51,6 +51,7 @@ static int gdbpy_should_print_stack = 0;
#include "version.h"
#include "target.h"
#include "gdbthread.h"
+#include "observer.h"
static PyMethodDef GdbMethods[];
@@ -682,6 +683,81 @@ gdbpy_initialize_events (void)
}
}
+
+
+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;
+}
+
+
+
/* Printing. */
/* A python function to write a single string using gdb's filtered
@@ -1134,6 +1210,8 @@ Enables or disables printing of Python stack traces."),
gdbpy_initialize_exited_event ();
gdbpy_initialize_thread_event ();
+ observer_attach_before_prompt (before_prompt_hook);
+
PyRun_SimpleString ("import gdb");
PyRun_SimpleString ("gdb.pretty_printers = []");
@@ -1236,6 +1314,8 @@ def GdbSetPythonDirectory (dir):\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);