summaryrefslogtreecommitdiff
path: root/gdbsupport
diff options
context:
space:
mode:
authorAndrew Burgess <andrew.burgess@embecosm.com>2020-09-25 14:50:56 +0100
committerAndrew Burgess <andrew.burgess@embecosm.com>2020-11-02 17:42:11 +0000
commit64aaad6349d2b2c45063a5383f877ce9a3a0c354 (patch)
tree80261e6099c2421820a8e8a8f025a13cd9995c27 /gdbsupport
parent8768c3e3629657d9728487c680b173868baeee7f (diff)
downloadbinutils-gdb-64aaad6349d2b2c45063a5383f877ce9a3a0c354.tar.gz
gdb: use get_standard_config_dir when looking for .gdbinit
This commit effectively changes the default location of the .gdbinit file, while maintaining backward compatibility. For non Apple hosts the .gdbinit file will now be looked for in the following locations: $XDG_CONFIG_HOME/gdb/gdbinit $HOME/.config/gdb/gdbinit $HOME/.gdbinit On Apple hosts the search order is instead: $HOME/Library/Preferences/gdb/gdbinit $HOME/.gdbinit I've performed an extensive rewrite of the documentation, moving all information about initialization files and where to find them into a new @node, text from other areas has been moved into this one location, and other areas cross-reference to this new @node as much as possible. gdb/ChangeLog: * NEWS: Mention changes to config file search path. * main.c gdb/doc/ChangeLog: * gdb.texinfo (Mode Options): Descriptions of initialization files has been moved to 'Initialization Files'. (Startup): Likewise. (Initialization Files): New node. (gdb man): Update to mention alternative file paths. (gdbinit man): Likewise.
Diffstat (limited to 'gdbsupport')
-rw-r--r--gdbsupport/pathstuff.cc49
-rw-r--r--gdbsupport/pathstuff.h23
2 files changed, 72 insertions, 0 deletions
diff --git a/gdbsupport/pathstuff.cc b/gdbsupport/pathstuff.cc
index 9fb5e5cf614..a52e53b8671 100644
--- a/gdbsupport/pathstuff.cc
+++ b/gdbsupport/pathstuff.cc
@@ -23,6 +23,10 @@
#include "filenames.h"
#include "gdb_tilde_expand.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
#ifdef USE_WIN32API
#include <windows.h>
#endif
@@ -298,6 +302,51 @@ get_standard_config_dir ()
return {};
}
+/* See pathstuff.h. */
+
+std::string
+get_standard_config_filename (const char *filename)
+{
+ std::string config_dir = get_standard_config_dir ();
+ if (config_dir != "")
+ {
+ const char *tmp = (*filename == '.') ? (filename + 1) : filename;
+ std::string path = config_dir + SLASH_STRING + std::string (tmp);
+ return path;
+ }
+
+ return {};
+}
+
+/* See pathstuff.h. */
+
+std::string
+find_gdb_home_config_file (const char *name, struct stat *buf)
+{
+ gdb_assert (name != nullptr);
+ gdb_assert (*name != '\0');
+
+ std::string config_dir_file = get_standard_config_filename (name);
+ if (!config_dir_file.empty ())
+ {
+ if (stat (config_dir_file.c_str (), buf) == 0)
+ return config_dir_file;
+ }
+
+ const char *homedir = getenv ("HOME");
+ if (homedir != nullptr)
+ {
+ /* Make sure the path is absolute and tilde-expanded. */
+ gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (homedir));
+ std::string path = (std::string (abs.get ()) + SLASH_STRING
+ + std::string (name));
+ if (stat (path.c_str (), buf) == 0)
+ return path;
+ }
+
+ return {};
+}
+
/* See gdbsupport/pathstuff.h. */
const char *
diff --git a/gdbsupport/pathstuff.h b/gdbsupport/pathstuff.h
index 85241bc8c7c..996c8f2bbf6 100644
--- a/gdbsupport/pathstuff.h
+++ b/gdbsupport/pathstuff.h
@@ -99,6 +99,29 @@ extern std::string get_standard_temp_dir ();
extern std::string get_standard_config_dir ();
+/* Look for FILENAME in the standard configuration directory as returned by
+ GET_STANDARD_CONFIG_DIR and return the path to the file. No check is
+ performed that the file actually exists or not.
+
+ If FILENAME begins with a '.' then the path returned will remove the
+ leading '.' character, for example passing '.gdbinit' could return the
+ path '/home/username/.config/gdb/gdbinit'. */
+
+extern std::string get_standard_config_filename (const char *filename);
+
+/* Look for a file called NAME in either the standard config directory or
+ in the users home directory. If a suitable file is found then *BUF will
+ be filled with the contents of a call to 'stat' on the found file,
+ otherwise *BUF is undefined after this call.
+
+ If NAME starts with a '.' character then, when looking in the standard
+ config directory the file searched for has the '.' removed. For
+ example, if NAME is '.gdbinit' then on a Linux target GDB might look for
+ '~/.config/gdb/gdbinit' and then '~/.gdbinit'. */
+
+extern std::string find_gdb_home_config_file (const char *name,
+ struct stat *buf);
+
/* Return the file name of the user's shell. Normally this comes from
the SHELL environment variable. */