Make relocate_{path,gdb_directory} return std::string
authorChristian Biesinger <cbiesinger@google.com>
Mon, 9 Sep 2019 17:55:39 +0000 (12:55 -0500)
committerChristian Biesinger <cbiesinger@google.com>
Wed, 11 Sep 2019 21:31:36 +0000 (16:31 -0500)
This simplifies memory management. I've also changed some global variables
to std::string accordingly (which store the result of these functions),
but not all because some are used with add_setshow_optional_filename_cmd
which requires a char*.

gdb/ChangeLog:

2019-09-11  Christian Biesinger  <cbiesinger@google.com>

* auto-load.c (auto_load_expand_dir_vars): Update.
* defs.h (gdb_datadir): Change to std::string.
(python_libdir): Likewise.
(relocate_gdb_directory): Change return type to std::string.
* guile/guile.c (gdbscm_data_directory): Update.
(initialize_scheme_side): Update.
* jit.c (jit_reader_dir): Change to std::string.
(jit_reader_load_command): Update.
* main.c (gdb_datadir): Change to std::string.
(python_libdir): Likewise.
(set_gdb_data_directory): Update.
(relocate_path): Change to return std::string.
(relocate_gdb_directory): Change to return std::string.
(relocate_gdbinit_path_maybe_in_datadir): Update.
(captured_main_1): Update.
* python/python.c (do_start_initialization): Update.
* top.c (show_gdb_datadir): Update.
* xml-syscall.c (xml_init_syscalls_info): Update.
(init_syscalls_info): Update.

gdb/ChangeLog
gdb/auto-load.c
gdb/defs.h
gdb/guile/guile.c
gdb/jit.c
gdb/main.c
gdb/python/python.c
gdb/top.c
gdb/xml-syscall.c

index 91b860c78a3ffb38544faaf3d2a116f23e54736c..4c354700c4525c49becf1863522ca9c100495a33 100644 (file)
@@ -1,3 +1,25 @@
+2019-09-11  Christian Biesinger  <cbiesinger@google.com>
+
+       * auto-load.c (auto_load_expand_dir_vars): Update.
+       * defs.h (gdb_datadir): Change to std::string.
+       (python_libdir): Likewise.
+       (relocate_gdb_directory): Change return type to std::string.
+       * guile/guile.c (gdbscm_data_directory): Update.
+       (initialize_scheme_side): Update.
+       * jit.c (jit_reader_dir): Change to std::string.
+       (jit_reader_load_command): Update.
+       * main.c (gdb_datadir): Change to std::string.
+       (python_libdir): Likewise.
+       (set_gdb_data_directory): Update.
+       (relocate_path): Change to return std::string.
+       (relocate_gdb_directory): Change to return std::string.
+       (relocate_gdbinit_path_maybe_in_datadir): Update.
+       (captured_main_1): Update.
+       * python/python.c (do_start_initialization): Update.
+       * top.c (show_gdb_datadir): Update.
+       * xml-syscall.c (xml_init_syscalls_info): Update.
+       (init_syscalls_info): Update.
+
 2019-09-11  Christian Biesinger  <cbiesinger@google.com>
 
        * main.c (relocate_gdbinit_path_maybe_in_datadir): Factor this code
index 616aeb6fc96a5988e26d05a20a74d844010fe248..115d5c10e821eb6b33521b7b365c9eb4cf03a2af 100644 (file)
@@ -178,7 +178,7 @@ static std::vector<gdb::unique_xmalloc_ptr<char>>
 auto_load_expand_dir_vars (const char *string)
 {
   char *s = xstrdup (string);
-  substitute_path_component (&s, "$datadir", gdb_datadir);
+  substitute_path_component (&s, "$datadir", gdb_datadir.c_str ());
   substitute_path_component (&s, "$debugdir", debug_file_directory);
 
   if (debug_auto_load && strcmp (s, string) != 0)
index 14e0a3e1d1df3d52c2bef6640bdf55a82d5f097c..c9a38b60a6d0525de2c41e415e1631052dbfee58 100644 (file)
@@ -115,11 +115,11 @@ extern int dbx_commands;
 extern char *gdb_sysroot;
 
 /* * GDB datadir, used to store data files.  */
-extern char *gdb_datadir;
+extern std::string gdb_datadir;
 
-/* * If non-NULL, the possibly relocated path to python's "lib" directory
+/* * If not empty, the possibly relocated path to python's "lib" directory
    specified with --with-python.  */
-extern char *python_libdir;
+extern std::string python_libdir;
 
 /* * Search path for separate debug files.  */
 extern char *debug_file_directory;
@@ -282,7 +282,7 @@ struct value;
 
 /* This really belong in utils.c (path-utils.c?), but it references some
    globals that are currently only available to main.c.  */
-extern char *relocate_gdb_directory (const char *initial, bool relocatable);
+extern std::string relocate_gdb_directory (const char *initial, bool relocatable);
 
 \f
 /* Annotation stuff.  */
index 39bec8724f46d5fa1d49878275d3f988fa7c4cab..defe554f7675a98dd3e8f1f780ea3d191f7e7f08 100644 (file)
@@ -325,7 +325,7 @@ gdbscm_execute_gdb_command (SCM command_scm, SCM rest)
 static SCM
 gdbscm_data_directory (void)
 {
-  return gdbscm_scm_from_c_string (gdb_datadir);
+  return gdbscm_scm_from_c_string (gdb_datadir.c_str ());
 }
 
 /* (guile-data-directory) -> string */
@@ -582,7 +582,8 @@ initialize_scheme_side (void)
 {
   char *boot_scm_path;
 
-  guile_datadir = concat (gdb_datadir, SLASH_STRING, "guile", (char *) NULL);
+  guile_datadir = concat (gdb_datadir.c_str (), SLASH_STRING, "guile",
+                         (char *) NULL);
   boot_scm_path = concat (guile_datadir, SLASH_STRING, "gdb",
                          SLASH_STRING, boot_scm_filename, (char *) NULL);
 
index 5fef03700c1fbe689c98091e1f4e16d2035e9bab..4722d6c6ce4a9903061be07191064ed3ea96657d 100644 (file)
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -42,7 +42,7 @@
 #include "readline/tilde.h"
 #include "completer.h"
 
-static const char *jit_reader_dir = NULL;
+static std::string jit_reader_dir;
 
 static const struct objfile_data *jit_objfile_data;
 
@@ -216,7 +216,7 @@ jit_reader_load_command (const char *args, int from_tty)
     error (_("JIT reader already loaded.  Run jit-reader-unload first."));
 
   if (!IS_ABSOLUTE_PATH (file.get ()))
-    file.reset (xstrprintf ("%s%s%s", jit_reader_dir, SLASH_STRING,
+    file.reset (xstrprintf ("%s%s%s", jit_reader_dir.c_str (), SLASH_STRING,
                            file.get ()));
 
   loaded_jit_reader = jit_reader_load (file.get ());
index 9e2288959016930b71321630cf6ab66533003cf2..6b75fced23626e3c2b0c0361d78b183d88f086be 100644 (file)
@@ -61,7 +61,7 @@ int dbx_commands = 0;
 char *gdb_sysroot = 0;
 
 /* GDB datadir, used to store data files.  */
-char *gdb_datadir = 0;
+std::string gdb_datadir;
 
 /* Non-zero if GDB_DATADIR was provided on the command line.
    This doesn't track whether data-directory is set later from the
@@ -70,7 +70,7 @@ static int gdb_datadir_provided = 0;
 
 /* If gdb was configured with --with-python=/path,
    the possibly relocated path to python's lib directory.  */
-char *python_libdir = 0;
+std::string python_libdir;
 
 /* Target IO streams.  */
 struct ui_file *gdb_stdtargin;
@@ -121,71 +121,70 @@ set_gdb_data_directory (const char *new_datadir)
   else if (!S_ISDIR (st.st_mode))
     warning (_("%s is not a directory."), new_datadir);
 
-  xfree (gdb_datadir);
-  gdb_datadir = gdb_realpath (new_datadir).release ();
+  gdb_datadir = gdb_realpath (new_datadir).get ();
 
   /* gdb_realpath won't return an absolute path if the path doesn't exist,
      but we still want to record an absolute path here.  If the user entered
      "../foo" and "../foo" doesn't exist then we'll record $(pwd)/../foo which
      isn't canonical, but that's ok.  */
-  if (!IS_ABSOLUTE_PATH (gdb_datadir))
+  if (!IS_ABSOLUTE_PATH (gdb_datadir.c_str ()))
     {
-      gdb::unique_xmalloc_ptr<char> abs_datadir = gdb_abspath (gdb_datadir);
+      gdb::unique_xmalloc_ptr<char> abs_datadir
+        = gdb_abspath (gdb_datadir.c_str ());
 
-      xfree (gdb_datadir);
-      gdb_datadir = abs_datadir.release ();
+      gdb_datadir = abs_datadir.get ();
     }
 }
 
 /* Relocate a file or directory.  PROGNAME is the name by which gdb
    was invoked (i.e., argv[0]).  INITIAL is the default value for the
    file or directory.  RELOCATABLE is true if the value is relocatable,
-   false otherwise.  Returns a newly allocated string; this may return
-   NULL under the same conditions as make_relative_prefix.  */
+   false otherwise.  This may return an empty string under the same
+   conditions as make_relative_prefix returning NULL.  */
 
-static char *
+static std::string
 relocate_path (const char *progname, const char *initial, bool relocatable)
 {
   if (relocatable)
-    return make_relative_prefix (progname, BINDIR, initial);
-  return xstrdup (initial);
+    {
+      gdb::unique_xmalloc_ptr<char> str (make_relative_prefix (progname,
+                                                              BINDIR,
+                                                              initial));
+      if (str != nullptr)
+       return str.get ();
+      return std::string ();
+    }
+  return initial;
 }
 
 /* Like relocate_path, but specifically checks for a directory.
    INITIAL is relocated according to the rules of relocate_path.  If
    the result is a directory, it is used; otherwise, INITIAL is used.
-   The chosen directory is then canonicalized using lrealpath.  This
-   function always returns a newly-allocated string.  */
+   The chosen directory is then canonicalized using lrealpath.  */
 
-char *
+std::string
 relocate_gdb_directory (const char *initial, bool relocatable)
 {
-  char *dir;
-
-  dir = relocate_path (gdb_program_name, initial, relocatable);
-  if (dir)
+  std::string dir = relocate_path (gdb_program_name, initial, relocatable);
+  if (!dir.empty ())
     {
       struct stat s;
 
-      if (*dir == '\0' || stat (dir, &s) != 0 || !S_ISDIR (s.st_mode))
+      if (stat (dir.c_str (), &s) != 0 || !S_ISDIR (s.st_mode))
        {
-         xfree (dir);
-         dir = NULL;
+         dir.clear ();
        }
     }
-  if (!dir)
-    dir = xstrdup (initial);
+  if (dir.empty ())
+    dir = initial;
 
   /* Canonicalize the directory.  */
-  if (*dir)
+  if (!dir.empty ())
     {
-      char *canon_sysroot = lrealpath (dir);
+      gdb::unique_xmalloc_ptr<char> canon_sysroot (lrealpath (dir.c_str ()));
 
       if (canon_sysroot)
-       {
-         xfree (dir);
-         dir = canon_sysroot;
-       }
+       dir = canon_sysroot.get ();
     }
 
   return dir;
@@ -220,14 +219,9 @@ relocate_gdbinit_path_maybe_in_datadir (const std::string& file)
     }
   else
     {
-      char *relocated = relocate_path (gdb_program_name,
-                                      file.c_str (),
-                                      SYSTEM_GDBINIT_RELOCATABLE);
-      if (relocated != nullptr)
-       {
-         relocated_path = relocated;
-         xfree (relocated);
-       }
+      relocated_path = relocate_path (gdb_program_name,
+                                     file.c_str (),
+                                     SYSTEM_GDBINIT_RELOCATABLE);
     }
     return relocated_path;
 }
@@ -541,20 +535,23 @@ captured_main_1 (struct captured_main_args *context)
     perror_warning_with_name (_("error finding working directory"));
 
   /* Set the sysroot path.  */
-  gdb_sysroot = relocate_gdb_directory (TARGET_SYSTEM_ROOT,
-                                       TARGET_SYSTEM_ROOT_RELOCATABLE);
+  gdb_sysroot
+    = xstrdup (relocate_gdb_directory (TARGET_SYSTEM_ROOT,
+                                    TARGET_SYSTEM_ROOT_RELOCATABLE).c_str ());
 
-  if (gdb_sysroot == NULL || *gdb_sysroot == '\0')
+  if (*gdb_sysroot == '\0')
     {
       xfree (gdb_sysroot);
       gdb_sysroot = xstrdup (TARGET_SYSROOT_PREFIX);
     }
 
-  debug_file_directory = relocate_gdb_directory (DEBUGDIR,
-                                                DEBUGDIR_RELOCATABLE);
+  debug_file_directory
+    = xstrdup (relocate_gdb_directory (DEBUGDIR,
+                                    DEBUGDIR_RELOCATABLE).c_str ());
 
-  gdb_datadir = relocate_gdb_directory (GDB_DATADIR,
-                                       GDB_DATADIR_RELOCATABLE);
+  gdb_datadir
+    = xstrdup (relocate_gdb_directory (GDB_DATADIR,
+                                    GDB_DATADIR_RELOCATABLE).c_str ());
 
 #ifdef WITH_PYTHON_PATH
   {
index 9c49151db38655b54e56ec387faffc2d04e178dd..9c8c635f72b60a6c632d9d18a482aba7926cfd72 100644 (file)
@@ -1608,7 +1608,7 @@ do_start_initialization ()
      /foo/lib/pythonX.Y/...
      This must be done before calling Py_Initialize.  */
   gdb::unique_xmalloc_ptr<char> progname
-    (concat (ldirname (python_libdir).c_str (), SLASH_STRING, "bin",
+    (concat (ldirname (python_libdir.c_str ()).c_str (), SLASH_STRING, "bin",
              SLASH_STRING, "python", (char *) NULL));
 #ifdef IS_PY3K
   std::string oldloc = setlocale (LC_ALL, NULL);
index 9d4ce1fa3bdc2dd59fb76ace3f6e932b87dd7805..2b53640af061e7f0bb1085433627150fbb4af7db 100644 (file)
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -2038,7 +2038,7 @@ show_gdb_datadir (struct ui_file *file, int from_tty,
                  struct cmd_list_element *c, const char *value)
 {
   fprintf_filtered (file, _("GDB's data directory is \"%s\".\n"),
-                   gdb_datadir);
+                   gdb_datadir.c_str ());
 }
 
 static void
index d144f82fbf23165f7ba2fd2334aef9627cdf9b31..dc988dfae8c131e2cf85c1d27c48ad4aa5bff388 100644 (file)
@@ -316,7 +316,8 @@ static struct syscalls_info *
 xml_init_syscalls_info (const char *filename)
 {
   gdb::optional<gdb::char_vector> full_file
-    = xml_fetch_content_from_file (filename, gdb_datadir);
+    = xml_fetch_content_from_file (filename,
+                                  const_cast<char *>(gdb_datadir.c_str ()));
   if (!full_file)
     return NULL;
 
@@ -336,7 +337,7 @@ init_syscalls_info (struct gdbarch *gdbarch)
   /* Should we re-read the XML info for this target?  */
   if (syscalls_info != NULL && !syscalls_info->my_gdb_datadir.empty ()
       && filename_cmp (syscalls_info->my_gdb_datadir.c_str (),
-                      gdb_datadir) != 0)
+                      gdb_datadir.c_str ()) != 0)
     {
       /* The data-directory changed from the last time we used it.
         It means that we have to re-read the XML info.  */
@@ -361,7 +362,7 @@ init_syscalls_info (struct gdbarch *gdbarch)
     {
       if (xml_syscall_file != NULL)
        warning (_("Could not load the syscall XML file `%s/%s'."),
-                gdb_datadir, xml_syscall_file);
+                gdb_datadir.c_str (), xml_syscall_file);
       else
        warning (_("There is no XML file to open."));
 
This page took 0.045523 seconds and 4 git commands to generate.