This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
RFC: user auto-load directory
- From: "Jim Blandy" <jimb at red-bean dot com>
- To: archer at sourceware dot org
- Date: Tue, 30 Dec 2008 17:15:18 -0800
- Subject: RFC: user auto-load directory
This patch adds a new parameter, 'maintenance set/show python
auto-load-user-directory', naming a directory that GDB searches for
Python scripts for loaded objfiles. It just uses the basename of the
objfile.
I want to be able to define my own per-object pretty-printers, etc.,
but I don't want to have to copy or symlink .py files into object
directories that get blown away regularly, so the same-dir-as-object
rule doesn't work for me. I don't want to mess with my
debug-file-directory settings.
Perhaps we should load the file we find in auto-load-user-directory
after also loading files we find by the existing rules?
If folks think the idea is good, I'll write docs.
gdb
* python/python.c: #include "filenames.h" and "readline/readline.h".
(gdbpy_auto_load_user_dir): New variable.
(gdbpy_new_objfile): Check the auto-load user directory, too.
(_initialize_python): Define 'maintenance set/show python
auto-load-user-directory' command. Add explanation of search
process to help for 'maint set/show python auto-load'.
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 9f1c8a4..c1793e8 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -39,6 +39,10 @@ static int gdbpy_should_print_stack = 1;
opened, false otherwise. */
static int gdbpy_auto_load = 1;
+/* If not the empty string, this is the directory we should check
+ for the user's per-object Python code. */
+static char *gdbpy_auto_load_user_dir;
+
#ifdef HAVE_PYTHON
#include "python.h"
@@ -58,6 +62,8 @@ static int gdbpy_auto_load = 1;
#include "target.h"
#include "gdbthread.h"
#include "event-top.h"
+#include "filenames.h"
+#include "readline/readline.h"
static PyMethodDef GdbMethods[];
@@ -740,6 +746,36 @@ gdbpy_new_objfile (struct objfile *objfile)
input = fopen (filename, "r");
+ if (!input && gdbpy_auto_load_user_dir && gdbpy_auto_load_user_dir[0])
+ {
+ /* Check for a file in the user's auto-load directory. */
+ char *user_dir = tilde_expand (gdbpy_auto_load_user_dir);
+ int user_dir_len = strlen (user_dir);
+ const char *basename, *p;
+ char *userfile;
+
+ /* Find last component of object file's path. */
+ basename = NULL;
+ for (p = filename; *p; p++)
+ if (IS_DIR_SEPARATOR (*p))
+ basename = p + 1;
+ if (! basename)
+ basename = filename;
+
+ /* Compute "AUTO-LOAD_USER_DIR/BASE-gdb.py". Use the
+ GDBPY_AUTO_FILENAME appended above. */
+ userfile = xmalloc (user_dir_len + 1 + (p - basename) + 1);
+ memcpy (userfile, user_dir, user_dir_len);
+ userfile[user_dir_len] = '/';
+ strcpy (userfile + user_dir_len + 1, basename);
+
+ xfree (user_dir);
+ xfree (filename);
+ filename = userfile;
+
+ input = fopen (filename, "r");
+ }
+
if (!input && debug_file_directory)
{
/* Also try the same file in the separate debug info directory. */
@@ -1433,11 +1469,32 @@ Enables or disables printing of Python stack traces."),
&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."),
+When GDB reads debugging information for an executable or shared library,\n\
+it looks in various places for Python code to load associated with that\n\
+object file. For an object file named OBJFILE in directory DIR, GDB\n\
+checks for a script under the following names:\n\
+ DIR/BASE-gdb.py\n\
+ (that is, in the same directory as the object file)\n\
+ PYTHON-AUTO-LOAD-USER-DIRECTORY/BASE-gdb.py\n\
+ (see 'help maintenance set python auto-load-user-directory')\n\
+ DEBUG-FILE-DIRECTORY/DIR/BASE-gdb.py\n\
+ (see 'help set debug-file-directory')\n\
+GDB loads the first one of these it finds."),
NULL, NULL,
&set_python_list,
&show_python_list);
+ add_setshow_optional_filename_cmd ("auto-load-user-directory", class_maintenance,
+ &gdbpy_auto_load_user_dir, _("\
+Set directory holding user Python code to load when an object is opened."),
+ _("\
+Show directory holding user Python code to load when an object is opened."),
+ _("\
+See 'help maintenance set python auto-load' for details."),
+ NULL, NULL,
+ &set_python_list,
+ &show_python_list);
+
#ifdef HAVE_PYTHON
Py_Initialize ();
PyEval_InitThreads ();