aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/python.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@redhat.com>2009-05-28 00:40:24 +0000
committerTom Tromey <tromey@redhat.com>2009-05-28 00:40:24 +0000
commit89c73adef9f9fac7e1efeb6961a867f46e54e24b (patch)
treea1023e41bebc2f1ce3cf04909ee2cce0a3a6f706 /gdb/python/python.c
parent58683880f52b1ae4b0233ea1565e7ee39a36b9b0 (diff)
downloadgdb-89c73adef9f9fac7e1efeb6961a867f46e54e24b.zip
gdb-89c73adef9f9fac7e1efeb6961a867f46e54e24b.tar.gz
gdb-89c73adef9f9fac7e1efeb6961a867f46e54e24b.tar.bz2
gdb
2009-04-01 Tom Tromey <tromey@redhat.com> Thiago Jung Bauermann <bauerman@br.ibm.com> Phil Muldoon <pmuldoon@redhat.com> * python/python.c: Include objfiles.h, observer.h. (gdbpy_auto_load): New global. (gdbpy_current_objfile): Likewise. (GDBPY_AUTO_FILENAME): New define. (gdbpy_new_objfile): New function. (gdbpy_get_current_objfile): Likewise. (gdbpy_objfiles): Likewise. (_initialize_python): Add "maint set auto-load". Call gdbpy_initialize_objfile. Attach objfile observer. (GdbMethods): New methods current_objfile, objfiles. * python/python-objfile.c: New file. * python/python-internal.h (objfile_to_objfile_object): Declare. (objfpy_get_printers): Likewise. (gdbpy_initialize_objfile): Likewise. * Makefile.in (SUBDIR_PYTHON_OBS): Add python-objfile.o. (SUBDIR_PYTHON_SRCS): Add python-objfile.c. (python-objfile.o): New target. gdb/doc 2009-04-01 Tom Tromey <tromey@redhat.com> Thiago Jung Bauermann <bauerman@br.ibm.com> * gdb.texinfo (Python API): Update. (Auto-loading): New node. (Objfiles In Python): New node. gdb/testsuite 2009-04-06 Tom Tromey <tromey@redhat.com> * gdb.python/python.exp (gdb_py_test_multiple): Add two objfile tests. * gdb.python/python-value.exp (py_objfile_tests): New proc. Call it.
Diffstat (limited to 'gdb/python/python.c')
-rw-r--r--gdb/python/python.c146
1 files changed, 146 insertions, 0 deletions
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 52fc780..78a23c7 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -22,6 +22,8 @@
#include "ui-out.h"
#include "cli/cli-script.h"
#include "gdbcmd.h"
+#include "objfiles.h"
+#include "observer.h"
#include <ctype.h>
@@ -29,6 +31,10 @@
false otherwise. */
static int gdbpy_should_print_stack = 1;
+/* This is true if we should auto-load python code when an objfile is
+ opened, false otherwise. */
+static int gdbpy_auto_load = 1;
+
#ifdef HAVE_PYTHON
#include "python.h"
@@ -301,6 +307,129 @@ gdbpy_print_stack (void)
PyErr_Clear ();
}
+
+
+/* The "current" objfile. This is set when gdb detects that a new
+ objfile has been loaded. It is only set for the duration of a call
+ to gdbpy_new_objfile; it is NULL at other times. */
+static struct objfile *gdbpy_current_objfile;
+
+/* The file name we attempt to read. */
+#define GDBPY_AUTO_FILENAME "-gdb.py"
+
+/* This is a new_objfile observer callback which loads python code
+ based on the path to the objfile. */
+static void
+gdbpy_new_objfile (struct objfile *objfile)
+{
+ char *realname;
+ char *filename, *debugfile;
+ int len;
+ FILE *input;
+ PyGILState_STATE state;
+ struct cleanup *cleanups;
+
+ if (!gdbpy_auto_load || !objfile || !objfile->name)
+ return;
+
+ state = PyGILState_Ensure ();
+
+ gdbpy_current_objfile = objfile;
+
+ realname = gdb_realpath (objfile->name);
+ len = strlen (realname);
+ filename = xmalloc (len + sizeof (GDBPY_AUTO_FILENAME));
+ memcpy (filename, realname, len);
+ strcpy (filename + len, GDBPY_AUTO_FILENAME);
+
+ input = fopen (filename, "r");
+ debugfile = filename;
+
+ cleanups = make_cleanup (xfree, filename);
+ make_cleanup (xfree, realname);
+
+ if (!input && debug_file_directory)
+ {
+ /* Also try the same file in the separate debug info directory. */
+ debugfile = xmalloc (strlen (filename)
+ + strlen (debug_file_directory) + 1);
+ strcpy (debugfile, debug_file_directory);
+ /* FILENAME is absolute, so we don't need a "/" here. */
+ strcat (debugfile, filename);
+
+ make_cleanup (xfree, debugfile);
+ input = fopen (debugfile, "r");
+ }
+
+ if (!input && gdb_datadir)
+ {
+ /* Also try the same file in a subdirectory of gdb's data
+ directory. */
+ debugfile = xmalloc (strlen (gdb_datadir) + strlen (filename)
+ + strlen ("/auto-load") + 1);
+ strcpy (debugfile, gdb_datadir);
+ strcat (debugfile, "/auto-load");
+ /* FILENAME is absolute, so we don't need a "/" here. */
+ strcat (debugfile, filename);
+
+ make_cleanup (xfree, debugfile);
+ input = fopen (debugfile, "r");
+ }
+
+ if (input)
+ {
+ /* We don't want to throw an exception here -- but the user
+ would like to know that something went wrong. */
+ if (PyRun_SimpleFile (input, debugfile))
+ gdbpy_print_stack ();
+ fclose (input);
+ }
+
+ do_cleanups (cleanups);
+ gdbpy_current_objfile = NULL;
+
+ PyGILState_Release (state);
+}
+
+/* Return the current Objfile, or None if there isn't one. */
+static PyObject *
+gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
+{
+ PyObject *result;
+
+ if (! gdbpy_current_objfile)
+ Py_RETURN_NONE;
+
+ result = objfile_to_objfile_object (gdbpy_current_objfile);
+ if (result)
+ Py_INCREF (result);
+ return result;
+}
+
+/* Return a sequence holding all the Objfiles. */
+static PyObject *
+gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
+{
+ struct objfile *objf;
+ PyObject *list;
+
+ list = PyList_New (0);
+ if (!list)
+ return NULL;
+
+ ALL_OBJFILES (objf)
+ {
+ PyObject *item = objfile_to_objfile_object (objf);
+ if (!item || PyList_Append (list, item) == -1)
+ {
+ Py_DECREF (list);
+ return NULL;
+ }
+ }
+
+ return list;
+}
+
#else /* HAVE_PYTHON */
/* Dummy implementation of the gdb "python" command. */
@@ -399,6 +528,15 @@ Enables or disables printing of Python stack traces."),
&set_python_list,
&show_python_list);
+ add_setshow_boolean_cmd ("auto-load", class_maintenance,
+ &gdbpy_auto_load, _("\
+Enable or disable auto-loading of Python code when an object is opened."), _("\
+Show whether Python code will be auto-loaded when an object is opened."), _("\
+Enables or disables auto-loading of Python code when an object is opened."),
+ NULL, NULL,
+ &set_python_list,
+ &show_python_list);
+
#ifdef HAVE_PYTHON
Py_Initialize ();
PyEval_InitThreads ();
@@ -414,9 +552,12 @@ Enables or disables printing of Python stack traces."),
gdbpy_initialize_frames ();
gdbpy_initialize_commands ();
gdbpy_initialize_functions ();
+ gdbpy_initialize_objfile ();
PyRun_SimpleString ("import gdb");
+ observer_attach_new_objfile (gdbpy_new_objfile);
+
gdbpy_doc_cst = PyString_FromString ("__doc__");
/* Create a couple objects which are used for Python's stdout and
@@ -465,6 +606,11 @@ static PyMethodDef GdbMethods[] =
{ "get_parameter", get_parameter, METH_VARARGS,
"Return a gdb parameter's value" },
+ { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS,
+ "Return the current Objfile being loaded, or None." },
+ { "objfiles", gdbpy_objfiles, METH_NOARGS,
+ "Return a sequence of all loaded objfiles." },
+
{ "selected_frame", gdbpy_selected_frame, METH_NOARGS,
"selected_frame () -> gdb.Frame.\n\
Return the selected frame object." },