+ if (result)
+ {
+ PyObject *r = PyString_FromString (result);
+ xfree (result);
+ return r;
+ }
+ Py_RETURN_NONE;
+}
+
+/* Implementation of gdb.solib_name (Long) -> String.
+ Returns the name of the shared library holding a given address, or None. */
+
+static PyObject *
+gdbpy_solib_name (PyObject *self, PyObject *args)
+{
+ char *soname;
+ PyObject *str_obj;
+ gdb_py_longest pc;
+
+ if (!PyArg_ParseTuple (args, GDB_PY_LL_ARG, &pc))
+ return NULL;
+
+ soname = solib_name_from_address (current_program_space, pc);
+ if (soname)
+ str_obj = PyString_Decode (soname, strlen (soname), host_charset (), NULL);
+ else
+ {
+ str_obj = Py_None;
+ Py_INCREF (Py_None);
+ }
+
+ return str_obj;
+}
+
+/* A Python function which is a wrapper for decode_line_1. */
+
+static PyObject *
+gdbpy_decode_line (PyObject *self, PyObject *args)
+{
+ struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to
+ appease gcc. */
+ struct symtab_and_line sal;
+ const char *arg = NULL;
+ char *copy = NULL;
+ struct cleanup *cleanups;
+ PyObject *result = NULL;
+ PyObject *return_result = NULL;
+ PyObject *unparsed = NULL;
+ volatile struct gdb_exception except;
+
+ if (! PyArg_ParseTuple (args, "|s", &arg))
+ return NULL;
+
+ cleanups = ensure_python_env (get_current_arch (), current_language);
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ if (arg)
+ {
+ copy = xstrdup (arg);
+ make_cleanup (xfree, copy);
+ sals = decode_line_1 (©, 0, 0, 0, 0);
+ make_cleanup (xfree, sals.sals);
+ }
+ else
+ {
+ set_default_source_symtab_and_line ();
+ sal = get_current_source_symtab_and_line ();
+ sals.sals = &sal;
+ sals.nelts = 1;
+ }
+ }
+ if (except.reason < 0)
+ {
+ do_cleanups (cleanups);
+ /* We know this will always throw. */
+ GDB_PY_HANDLE_EXCEPTION (except);
+ }
+
+ if (sals.nelts)
+ {
+ int i;
+
+ result = PyTuple_New (sals.nelts);
+ if (! result)
+ goto error;
+ for (i = 0; i < sals.nelts; ++i)
+ {
+ PyObject *obj;
+ char *str;
+
+ obj = symtab_and_line_to_sal_object (sals.sals[i]);
+ if (! obj)
+ {
+ Py_DECREF (result);
+ goto error;
+ }
+
+ PyTuple_SetItem (result, i, obj);
+ }
+ }
+ else
+ {
+ result = Py_None;
+ Py_INCREF (Py_None);
+ }
+
+ return_result = PyTuple_New (2);
+ if (! return_result)
+ {
+ Py_DECREF (result);
+ goto error;
+ }
+
+ if (copy && strlen (copy) > 0)
+ unparsed = PyString_FromString (copy);
+ else
+ {
+ unparsed = Py_None;
+ Py_INCREF (Py_None);
+ }
+
+ PyTuple_SetItem (return_result, 0, unparsed);
+ PyTuple_SetItem (return_result, 1, result);
+
+ do_cleanups (cleanups);
+
+ return return_result;
+
+ error:
+ do_cleanups (cleanups);
+ return NULL;
+}
+
+/* Parse a string and evaluate it as an expression. */
+static PyObject *
+gdbpy_parse_and_eval (PyObject *self, PyObject *args)
+{
+ const char *expr_str;
+ struct value *result = NULL;
+ volatile struct gdb_exception except;
+
+ if (!PyArg_ParseTuple (args, "s", &expr_str))
+ return NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ char *copy = xstrdup (expr_str);
+ struct cleanup *cleanup = make_cleanup (xfree, copy);
+
+ result = parse_and_eval (copy);
+ do_cleanups (cleanup);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ return value_to_value_object (result);
+}
+
+/* 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 (const char *file)
+{
+ struct cleanup *cleanup;
+
+ cleanup = ensure_python_env (get_current_arch (), current_language);
+ python_run_simple_file (file);
+ do_cleanups (cleanup);
+}
+
+\f
+
+/* Posting and handling events. */
+
+/* A single event. */
+struct gdbpy_event
+{
+ /* The Python event. This is just a callable object. */
+ PyObject *event;
+ /* The next event. */
+ struct gdbpy_event *next;
+};
+
+/* All pending events. */
+static struct gdbpy_event *gdbpy_event_list;
+/* The final link of the event list. */
+static struct gdbpy_event **gdbpy_event_list_end;
+
+/* We use a file handler, and not an async handler, so that we can
+ wake up the main thread even when it is blocked in poll(). */
+static struct serial *gdbpy_event_fds[2];
+
+/* The file handler callback. This reads from the internal pipe, and
+ then processes the Python event queue. This will always be run in
+ the main gdb thread. */
+
+static void
+gdbpy_run_events (struct serial *scb, void *context)
+{
+ struct cleanup *cleanup;
+ int r;
+
+ cleanup = ensure_python_env (get_current_arch (), current_language);
+
+ /* Flush the fd. Do this before flushing the events list, so that
+ any new event post afterwards is sure to re-awake the event
+ loop. */
+ while (serial_readchar (gdbpy_event_fds[0], 0) >= 0)
+ ;
+
+ while (gdbpy_event_list)
+ {
+ /* Dispatching the event might push a new element onto the event
+ loop, so we update here "atomically enough". */
+ struct gdbpy_event *item = gdbpy_event_list;
+ gdbpy_event_list = gdbpy_event_list->next;
+ if (gdbpy_event_list == NULL)
+ gdbpy_event_list_end = &gdbpy_event_list;
+
+ /* Ignore errors. */
+ if (PyObject_CallObject (item->event, NULL) == NULL)
+ PyErr_Clear ();
+
+ Py_DECREF (item->event);
+ xfree (item);
+ }
+
+ do_cleanups (cleanup);
+}
+
+/* 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);
+ }
+