[2/3] Add cwd paramter to openp

Message ID 20240929-pr-31716-capture-openp-v1-2-ee6bd616d3bf@tromey.com
State New
Headers
Series Capture some globals for background DWARF reading |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Tom Tromey Sept. 29, 2024, 6:52 p.m. UTC
  This patch adds a cwd paramter to openp, so that the current directory
can be passed in by the caller.  This is useful when background
threads call this function -- they can then avoid using the global and
thus avoid races with the user using "cd".

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31716
---
 gdb/source.c | 9 +++++----
 gdb/source.h | 4 +++-
 2 files changed, 8 insertions(+), 5 deletions(-)
  

Patch

diff --git a/gdb/source.c b/gdb/source.c
index b9122c421a023ce010f5117f0e75347fa751116d..b7d8ed6cbc05e6fdb692bc31a8a3f56e51196249 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -770,7 +770,8 @@  prepare_path_for_appending (const char *path)
     >>>>  eg executable, non-directory.  */
 int
 openp (const char *path, openp_flags opts, const char *string,
-       int mode, gdb::unique_xmalloc_ptr<char> *filename_opened)
+       int mode, gdb::unique_xmalloc_ptr<char> *filename_opened,
+       const char *cwd)
 {
   int fd;
   char *filename;
@@ -851,14 +852,14 @@  openp (const char *path, openp_flags opts, const char *string,
 	  int newlen;
 
 	  /* First, realloc the filename buffer if too short.  */
-	  len = strlen (current_directory);
+	  len = strlen (cwd);
 	  newlen = len + strlen (string) + 2;
 	  if (newlen > alloclen)
 	    {
 	      alloclen = newlen;
 	      filename = (char *) alloca (alloclen);
 	    }
-	  strcpy (filename, current_directory);
+	  strcpy (filename, cwd);
 	}
       else if (strchr(dir, '~'))
 	{
@@ -921,7 +922,7 @@  openp (const char *path, openp_flags opts, const char *string,
 	*filename_opened = gdb_realpath (filename);
       else
 	*filename_opened
-	  = make_unique_xstrdup (gdb_abspath (filename).c_str ());
+	  = make_unique_xstrdup (gdb_abspath (filename, cwd).c_str ());
     }
 
   errno = last_errno;
diff --git a/gdb/source.h b/gdb/source.h
index 541ee3b7a1248bbfed4eddd52e5258407b644c0c..33ccda727c67be7e918fac7be2a73a95a26ee07d 100644
--- a/gdb/source.h
+++ b/gdb/source.h
@@ -19,6 +19,7 @@ 
 #ifndef SOURCE_H
 #define SOURCE_H
 
+#include "gdbsupport/pathstuff.h"
 #include "gdbsupport/scoped_fd.h"
 
 struct program_space;
@@ -37,7 +38,8 @@  enum openp_flag
 DEF_ENUM_FLAGS_TYPE(openp_flag, openp_flags);
 
 extern int openp (const char *, openp_flags, const char *, int,
-		  gdb::unique_xmalloc_ptr<char> *);
+		  gdb::unique_xmalloc_ptr<char> *,
+		  const char *cwd = current_directory);
 
 extern int source_full_path_of (const char *, gdb::unique_xmalloc_ptr<char> *);