+/* 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;
+#ifdef PY_LONG_LONG
+ unsigned PY_LONG_LONG pc;
+ /* To be compatible with Python 2.4 the format strings are not const. */
+ char *format = "K";
+#else
+ unsigned long pc;
+ char *format = "k";
+#endif
+
+ if (!PyArg_ParseTuple (args, format, &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;
+ 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)
+ {
+ arg = xstrdup (arg);
+ make_cleanup (xfree, arg);
+ copy = arg;
+ sals = decode_line_1 (©, 0, 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)
+{
+ 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)
+ {
+ result = parse_and_eval (expr_str);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ 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. */
+
+void
+source_python_script (FILE *stream, 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);
+
+ do_cleanups (cleanup);
+}
+