Re: [PATCH 2/2] Try to initialize data-directory by first searching for "data-directory" in the same directory as the gdb binary

Jan Kratochvil
2012-09-21T18:31:41Z

On Wed, 19 Sep 2012 21:55:28 +0200, Khoo Yit Phang wrote:
> + /* Then try to find GDB_DATADIR relocated relative to gdb. */
> + if (!dir)
> + dir = relocate_path (gdb_program_name, GDB_DATADIR, 1,
> + GDB_DATADIR_RELOCATABLE);
> +
> + /* Otherwise use GDB_DATADIR as is. */
> + if (!dir)
> + dir = xstrdup (GDB_DATADIR);
> +
> + /* Canonicalize the directory. */
> + if (*dir)
> + {
> + char *canon_sysroot = lrealpath (dir);
> +
> + if (canon_sysroot)
> + {
> + xfree (dir);
> + dir = canon_sysroot;
> + }
> + }

I am not happy from the code duplication with existing function:

char *
relocate_gdb_directory (const char *initial, int flag)
{
char *dir;

dir = relocate_path (gdb_program_name, initial, 1, flag);
if (!dir)
dir = xstrdup (initial);

/* Canonicalize the directory. */
if (*dir)
{
char *canon_sysroot = lrealpath (dir);

if (canon_sysroot)
{
xfree (dir);
dir = canon_sysroot;
}
}

return dir;
}

Also it could check for /usr/gdb/data-directory which is less likely to be
present than /usr/bin/data-directory .

Proposing the patch below instead, although I do not like it much either.

Thanks,
Jan

gdb/
2012-09-21 Jan Kratochvil <jan.kratochvil@redhat.com>

* main.c: New variables dir and datadir. Change GDB_DATADIR
initialization to attempt to find gdb/data-directory/ first.

diff --git a/gdb/main.c b/gdb/main.c
index bde37f9..635c86b 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -314,6 +314,7 @@ captured_main (void *data)
int i;
int save_auto_load;
struct objfile *objfile;
+ char *dir, *datadir;

struct cleanup *pre_stat_chain;

@@ -370,8 +371,23 @@ captured_main (void *data)
debug_file_directory = relocate_gdb_directory (DEBUGDIR,
DEBUGDIR_RELOCATABLE);

- gdb_datadir = relocate_gdb_directory (GDB_DATADIR,
- GDB_DATADIR_RELOCATABLE);
+ /* Attempt first to find gdb/data-directory/ if we are run still from the
+ build directory. */
+ datadir = ldirname (BINDIR);
+ datadir = reconcat (datadir, datadir,
+ SLASH_STRING "gdb" SLASH_STRING "data-directory", NULL);
+ dir = relocate_path (gdb_program_name, datadir, 1, 1);
+ xfree (datadir);
+ if (dir)
+ {
+ /* DATADIR could never have less components than BINDIR, therefore
+ we do not need to check it DIR is not "" like
+ relocate_gdb_directory has to. */
+ gdb_datadir = lrealpath (dir);
+ xfree (dir);
+ }
+ if (gdb_datadir == NULL)
+ gdb_datadir = relocate_gdb_directory (GDB_DATADIR, GDB_DATADIR_RELOCATABLE);

#ifdef WITH_PYTHON_PATH
{