+/* Submit an event to the gdb thread. */
+static PyObject *
+gdbpy_post_event (PyObject *self, PyObject *args)
+{
+ struct gdbpy_event *event;
+ PyObject *func;
+ int wakeup;
+
+ if (!PyArg_ParseTuple (args, "O", &func))
+ return NULL;
+
+ if (!PyCallable_Check (func))
+ {
+ PyErr_SetString (PyExc_RuntimeError,
+ _("Posted event is not callable"));
+ return NULL;
+ }
+
+ Py_INCREF (func);
+
+ /* From here until the end of the function, we have the GIL, so we
+ can operate on our global data structures without worrying. */
+ wakeup = gdbpy_event_list == NULL;
+
+ event = XNEW (struct gdbpy_event);
+ event->event = func;
+ event->next = NULL;
+ *gdbpy_event_list_end = event;
+ gdbpy_event_list_end = &event->next;
+
+ /* Wake up gdb when needed. */
+ if (wakeup)
+ {
+ char c = 'q'; /* Anything. */
+
+ if (serial_write (gdbpy_event_fds[1], &c, 1))
+ return PyErr_SetFromErrno (PyExc_IOError);
+ }
+
+ Py_RETURN_NONE;
+}
+
+/* Initialize the Python event handler. */
+static void
+gdbpy_initialize_events (void)
+{
+ if (serial_pipe (gdbpy_event_fds) == 0)
+ {
+ gdbpy_event_list_end = &gdbpy_event_list;
+ serial_async (gdbpy_event_fds[0], gdbpy_run_events, NULL);
+ }
+}
+
+\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;
+}
+