Python pretty-printing [1/6]

Tom Tromey tromey@redhat.com
Thu Apr 2 20:54:00 GMT 2009


This is the first in a series of patches which implement the
long-discussed Python pretty-printing feature.

This patch imports --with-gdb-data from Sérgio's syscall patch.
It also adds --with-pythondir and arranges for some Python variables
to be set appropriately.

This code is needed as the basis of the auto-loading patch.

Tom

2009-01-25  Sergio Durigan Junior <sergiodj@linux.vnet.ibm.com>
	    Tom Tromey  <tromey@redhat.com>

	* configure, config.in: Regenerate.
	* configure.ac: Support for relocatable GDB datadir.  Add
	--with-pythondir.
	* python/python.c (_initialize_python): Set pythondir or datadir
	as needed.  Update sys.path.
	* maint.c (_initialize_maint_cmds): Add "maint set gdb_datadir".
	* defs.h (gdb_datadir): Declare.
	* main.c (gdb_datadir): New global.
	(captured_main): Initialize gdb_datadir.

 gdb/ChangeLog       |   13 ++++++++
 gdb/config.in       |    9 ++++++
 gdb/configure       |   77 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 gdb/configure.ac    |   45 +++++++++++++++++++++++++++++
 gdb/defs.h          |    3 ++
 gdb/main.c          |   37 ++++++++++++++++++++++++
 gdb/maint.c         |    8 +++++
 gdb/python/python.c |   15 ++++++++++
 8 files changed, 206 insertions(+), 1 deletions(-)

diff --git a/gdb/configure.ac b/gdb/configure.ac
index 821dffe..9bca6c6 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -118,6 +118,51 @@ case ${debugdir} in
   ;;
 esac
 
+# GDB's datadir relocation
+
+gdbdatadir=${datadir}/gdb
+
+AC_ARG_WITH([gdb-datadir],
+  [AS_HELP_STRING([--with-gdb-datadir],
+                  [look for global separate data files in this path [DATADIR/gdb]])], [gdbdatadir="${withval}"])
+
+AC_DEFINE_DIR(GDB_DATADIR, gdbdatadir,
+              [Global directory for GDB data files. ])
+
+if test "x$exec_prefix" = xNONE || test "x$exec_prefix" = 'x${prefix}'; then
+  if test "x$prefix" = xNONE; then
+    test_prefix=/usr/local
+  else
+    test_prefix=$prefix
+  fi
+else
+  test_prefix=$exec_prefix
+fi
+
+case ${gdbdatadir} in
+  "${test_prefix}"|"${test_prefix}/"*|\
+  '${exec_prefix}'|'${exec_prefix}/'*)
+    AC_DEFINE(GDB_DATADIR_RELOCATABLE, 1, [Define if GDB datadir should be relocated when GDB is moved.])
+  ;;
+esac
+GDB_DATADIR_PATH=${gdbdatadir}
+AC_SUBST(GDB_DATADIR_PATH)
+
+AC_ARG_WITH([pythondir],
+  [AS_HELP_STRING([--with-pythondir],
+                  [install Python data files in this path [DATADIR/gdb/python]])], [pythondir="${withval}"], [pythondir=no])
+
+# If the user passed in a path, define it.  Otherwise, compute it at
+# runtime based on the possibly-relocatable datadir.
+if test "$pythondir" = "no"; then
+  pythondir='$(GDB_DATADIR_PATH)/python'
+else
+  AC_DEFINE_UNQUOTED(PYTHONDIR, "$pythondir",
+      [Define to install path for Python sources])
+fi
+AC_SUBST(pythondir)
+
+
 AC_CONFIG_SUBDIRS(doc testsuite)
 
 # Check whether to support alternative target configurations
diff --git a/gdb/defs.h b/gdb/defs.h
index 882a844..ee80659 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -153,6 +153,9 @@ extern int dbx_commands;
 /* System root path, used to find libraries etc.  */
 extern char *gdb_sysroot;
 
+/* GDB datadir, used to store data files.  */
+extern char *gdb_datadir;
+
 /* Search path for separate debug files.  */
 extern char *debug_file_directory;
 
diff --git a/gdb/main.c b/gdb/main.c
index 5d4640b..fe65dc5 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -62,6 +62,9 @@ int dbx_commands = 0;
 /* System root path, used to find libraries etc.  */
 char *gdb_sysroot = 0;
 
+/* GDB datadir, used to store data files.  */
+char *gdb_datadir = 0;
+
 struct ui_file *gdb_stdout;
 struct ui_file *gdb_stderr;
 struct ui_file *gdb_stdlog;
@@ -357,6 +360,40 @@ captured_main (void *data)
 	}
     }
 
+#ifdef GDB_DATADIR_RELOCATABLE
+  gdb_datadir = make_relative_prefix (argv[0], BINDIR, GDB_DATADIR);
+  if (gdb_datadir)
+    {
+      struct stat s;
+      int res = 0;
+
+      if (stat (gdb_datadir, &s) == 0)
+	if (S_ISDIR (s.st_mode))
+	  res = 1;
+
+      if (res == 0)
+	{
+	  xfree (gdb_datadir);
+	  gdb_datadir = xstrdup (GDB_DATADIR);
+	}
+    }
+  else
+    gdb_datadir = xstrdup (GDB_DATADIR);
+#else
+  gdb_datadir = xstrdup (GDB_DATADIR);
+#endif /* GDB_DATADIR_RELOCATABLE */
+
+  /* Canonicalize the GDB's datadir path.  */
+  if (*gdb_datadir)
+    {
+      char *canon_debug = lrealpath (gdb_datadir);
+      if (canon_debug)
+	{
+	  xfree (gdb_datadir);
+	  gdb_datadir = canon_debug;
+	}
+    }
+
   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
 
   /* There will always be an interpreter.  Either the one passed into
diff --git a/gdb/maint.c b/gdb/maint.c
index 56cafe9..1b57ff5 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -906,4 +906,12 @@ When enabled GDB is profiled."),
 			   show_maintenance_profile_p,
 			   &maintenance_set_cmdlist,
 			   &maintenance_show_cmdlist);
+  add_setshow_filename_cmd ("gdb_datadir", class_maintenance,
+                           &gdb_datadir, _("Set GDB's datadir path."),
+                           _("Show GDB's datadir path."),
+                           _("\
+When set, GDB uses the specified path to search for data files."),
+                           NULL, NULL,
+                           &maintenance_set_cmdlist,
+                           &maintenance_show_cmdlist);
 }
diff --git a/gdb/python/python.c b/gdb/python/python.c
index b48cf05..1762c46 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -409,6 +409,12 @@ Enables or disables printing of Python stack traces."),
   PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version);
   PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name);
   PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name);
+#ifdef PYTHONDIR
+  PyModule_AddStringConstant (gdb_module, "pythondir", PYTHONDIR);
+#else
+  if (gdb_datadir)
+    PyModule_AddStringConstant (gdb_module, "datadir", gdb_datadir);
+#endif
 
   gdbpy_initialize_values ();
   gdbpy_initialize_commands ();
@@ -442,6 +448,15 @@ class GdbOutputFile:\n\
 \n\
 sys.stderr = GdbOutputFile()\n\
 sys.stdout = GdbOutputFile()\n\
+if hasattr (gdb, 'datadir'):\n\
+  gdb.pythondir = gdb.datadir + '/python'\n\
+if hasattr (gdb, 'pythondir'):\n\
+  sys.path.insert(0, gdb.pythondir)\n\
+  gdb.__path__ = [gdb.pythondir + '/gdb']\n\
+  from os.path import exists\n\
+  ipy = gdb.pythondir + '/gdb/__init__.py'\n\
+  if exists (ipy):\n\
+    execfile (ipy)\n\
 ");
 
   /* Release the GIL while gdb runs.  */
-- 
1.6.0.6



More information about the Gdb-patches mailing list