From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 71814 invoked by alias); 9 Sep 2019 18:08:47 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 71748 invoked by uid 89); 9 Sep 2019 18:08:46 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.8 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy=chosen, Search X-HELO: mail-qk1-f202.google.com Received: from mail-qk1-f202.google.com (HELO mail-qk1-f202.google.com) (209.85.222.202) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 09 Sep 2019 18:08:43 +0000 Received: by mail-qk1-f202.google.com with SMTP id v143so17250438qka.21 for ; Mon, 09 Sep 2019 11:08:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=AZBaur/ImYUSdel+fWfg0P8QZAV0FMylO9v4XFBXcL4=; b=oIo03ncIzGBK87el/k1+ZQOrQV8Oj4wkl3eJTM7YyXeASxMykF8hvgDp0wLS12yeez ZIZRqn5Wet+M4EX5ZwAKDkR8pf+7uFiWNcvkDukR152EjUaadYYAGNo/0nh3jafZad3O ArvqWezvaRLwuqFOnLbjH0wJorrhz/xjoPZGc/edGi7/tZr6An8xKYpWaGpF5j32zNO/ L9BZEc1KwmzkOEDlKiosn6TYKttd44w4KXWp1B/YfdRenAf1PQkML1HLdzfhXV6snGnr 6L8McIep5wLWYDUA824cTodf7RLtZZts5PC7Ybjg6SsJltbmDy2PhgMaVk9u1d+50dwK ye8w== Date: Mon, 09 Sep 2019 18:08:00 -0000 In-Reply-To: <20190909180830.215313-1-cbiesinger@google.com> Message-Id: <20190909180830.215313-4-cbiesinger@google.com> Mime-Version: 1.0 References: <20190909180830.215313-1-cbiesinger@google.com> Subject: [PATCH 3/3] Make relocate_{path,gdb_directory} return std::string From: "Christian Biesinger via gdb-patches" Reply-To: Christian Biesinger To: gdb-patches@sourceware.org Cc: Christian Biesinger Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2019-09/txt/msg00138.txt.bz2 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-09 Christian Biesinger * 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/auto-load.c | 2 +- gdb/defs.h | 8 ++--- gdb/guile/guile.c | 5 +-- gdb/jit.c | 4 +-- gdb/main.c | 84 ++++++++++++++++++++++----------------------- gdb/python/python.c | 2 +- gdb/top.c | 2 +- gdb/xml-syscall.c | 7 ++-- 8 files changed, 57 insertions(+), 57 deletions(-) diff --git a/gdb/auto-load.c b/gdb/auto-load.c index 616aeb6fc9..115d5c10e8 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -178,7 +178,7 @@ static std::vector> auto_load_expand_dir_vars (const char *string) { char *s =3D 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); =20 if (debug_auto_load && strcmp (s, string) !=3D 0) diff --git a/gdb/defs.h b/gdb/defs.h index 14e0a3e1d1..c9a38b60a6 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -115,11 +115,11 @@ extern int dbx_commands; extern char *gdb_sysroot; =20 /* * GDB datadir, used to store data files. */ -extern char *gdb_datadir; +extern std::string gdb_datadir; =20 -/* * 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; =20 /* * Search path for separate debug files. */ extern char *debug_file_directory; @@ -282,7 +282,7 @@ struct value; =20 /* 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 reloc= atable); =20 =0C /* Annotation stuff. */ diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c index 39bec8724f..defe554f76 100644 --- a/gdb/guile/guile.c +++ b/gdb/guile/guile.c @@ -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 ()); } =20 /* (guile-data-directory) -> string */ @@ -582,7 +582,8 @@ initialize_scheme_side (void) { char *boot_scm_path; =20 - guile_datadir =3D concat (gdb_datadir, SLASH_STRING, "guile", (char *) N= ULL); + guile_datadir =3D concat (gdb_datadir.c_str (), SLASH_STRING, "guile", + (char *) NULL); boot_scm_path =3D concat (guile_datadir, SLASH_STRING, "gdb", SLASH_STRING, boot_scm_filename, (char *) NULL); =20 diff --git a/gdb/jit.c b/gdb/jit.c index 5fef03700c..4722d6c6ce 100644 --- a/gdb/jit.c +++ b/gdb/jit.c @@ -42,7 +42,7 @@ #include "readline/tilde.h" #include "completer.h" =20 -static const char *jit_reader_dir =3D NULL; +static std::string jit_reader_dir; =20 static const struct objfile_data *jit_objfile_data; =20 @@ -216,7 +216,7 @@ jit_reader_load_command (const char *args, int from_tty) error (_("JIT reader already loaded. Run jit-reader-unload first.")); =20 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_STRIN= G, file.get ())); =20 loaded_jit_reader =3D jit_reader_load (file.get ()); diff --git a/gdb/main.c b/gdb/main.c index 24aad0ca5a..f9126c7299 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -61,7 +61,7 @@ int dbx_commands =3D 0; char *gdb_sysroot =3D 0; =20 /* GDB datadir, used to store data files. */ -char *gdb_datadir =3D 0; +std::string gdb_datadir; =20 /* 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 =3D 0; =20 /* If gdb was configured with --with-python=3D/path, the possibly relocated path to python's lib directory. */ -char *python_libdir =3D 0; +std::string python_libdir; =20 /* Target IO streams. */ struct ui_file *gdb_stdtargin; @@ -121,70 +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); =20 - xfree (gdb_datadir); - gdb_datadir =3D gdb_realpath (new_datadir).release (); + gdb_datadir =3D gdb_realpath (new_datadir).get (); =20 /* 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 enter= ed "../foo" and "../foo" doesn't exist then we'll record $(pwd)/../foo w= hich 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 abs_datadir =3D gdb_abspath (gdb_datad= ir); + gdb::unique_xmalloc_ptr abs_datadir =3D + gdb_abspath (gdb_datadir.c_str ()); =20 - xfree (gdb_datadir); - gdb_datadir =3D abs_datadir.release (); + gdb_datadir =3D abs_datadir.get (); } } =20 /* 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. */ =20 -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); + { + char *str =3D make_relative_prefix (progname, BINDIR, initial); + if (str !=3D nullptr) + return str; + return std::string (); + } + return initial; } =20 /* 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. */ =20 -char * +std::string relocate_gdb_directory (const char *initial, bool relocatable) { - char *dir; - - dir =3D relocate_path (gdb_program_name, initial, relocatable); - if (dir) + std::string dir =3D relocate_path (gdb_program_name, initial, relocatabl= e); + if (!dir.empty ()) { struct stat s; =20 - if (*dir =3D=3D '\0' || stat (dir, &s) !=3D 0 || !S_ISDIR (s.st_mode= )) + if (stat (dir.c_str (), &s) !=3D 0 || !S_ISDIR (s.st_mode)) { - xfree (dir); - dir =3D NULL; + dir.clear (); } } - if (!dir) - dir =3D xstrdup (initial); + if (dir.empty ()) + dir =3D initial; =20 /* Canonicalize the directory. */ - if (*dir) + if (!dir.empty ()) { - char *canon_sysroot =3D lrealpath (dir); + char *canon_sysroot =3D lrealpath (dir.c_str ()); =20 if (canon_sysroot) { - xfree (dir); dir =3D canon_sysroot; + xfree (canon_sysroot); } } =20 @@ -216,14 +216,9 @@ relocate_gdbinit_path_maybe_in_datadir (const std::str= ing& file) } else { - char *relocated =3D relocate_path (gdb_program_name, - file.c_str (), - SYSTEM_GDBINIT_RELOCATABLE); - if (relocated !=3D nullptr) - { - relocated_path =3D relocated; - xfree (relocated); - } + relocated_path =3D relocate_path (gdb_program_name, + file.c_str (), + SYSTEM_GDBINIT_RELOCATABLE); } return relocated_path; } @@ -537,20 +532,23 @@ captured_main_1 (struct captured_main_args *context) perror_warning_with_name (_("error finding working directory")); =20 /* Set the sysroot path. */ - gdb_sysroot =3D relocate_gdb_directory (TARGET_SYSTEM_ROOT, - TARGET_SYSTEM_ROOT_RELOCATABLE); + gdb_sysroot =3D + xstrdup (relocate_gdb_directory (TARGET_SYSTEM_ROOT, + TARGET_SYSTEM_ROOT_RELOCATABLE).c_str ()); =20 - if (gdb_sysroot =3D=3D NULL || *gdb_sysroot =3D=3D '\0') + if (*gdb_sysroot =3D=3D '\0') { xfree (gdb_sysroot); gdb_sysroot =3D xstrdup (TARGET_SYSROOT_PREFIX); } =20 - debug_file_directory =3D relocate_gdb_directory (DEBUGDIR, - DEBUGDIR_RELOCATABLE); + debug_file_directory =3D + xstrdup (relocate_gdb_directory (DEBUGDIR, + DEBUGDIR_RELOCATABLE).c_str ()); =20 - gdb_datadir =3D relocate_gdb_directory (GDB_DATADIR, - GDB_DATADIR_RELOCATABLE); + gdb_datadir =3D + xstrdup (relocate_gdb_directory (GDB_DATADIR, + GDB_DATADIR_RELOCATABLE).c_str ()); =20 #ifdef WITH_PYTHON_PATH { diff --git a/gdb/python/python.c b/gdb/python/python.c index b309ae91ba..ed7d51b6ef 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1601,7 +1601,7 @@ do_start_initialization () /foo/lib/pythonX.Y/... This must be done before calling Py_Initialize. */ gdb::unique_xmalloc_ptr progname - (concat (ldirname (python_libdir).c_str (), SLASH_STRING, "bin", + (concat (ldirname (python_libdir.c_str ()).c_str (), SLASH_STRING, "bi= n", SLASH_STRING, "python", (char *) NULL)); #ifdef IS_PY3K std::string oldloc =3D setlocale (LC_ALL, NULL); diff --git a/gdb/top.c b/gdb/top.c index 9d4ce1fa3b..2b53640af0 100644 --- 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 ()); } =20 static void diff --git a/gdb/xml-syscall.c b/gdb/xml-syscall.c index d144f82fbf..dc988dfae8 100644 --- a/gdb/xml-syscall.c +++ b/gdb/xml-syscall.c @@ -316,7 +316,8 @@ static struct syscalls_info * xml_init_syscalls_info (const char *filename) { gdb::optional full_file - =3D xml_fetch_content_from_file (filename, gdb_datadir); + =3D xml_fetch_content_from_file (filename, + const_cast(gdb_datadir.c_str ())); if (!full_file) return NULL; =20 @@ -336,7 +337,7 @@ init_syscalls_info (struct gdbarch *gdbarch) /* Should we re-read the XML info for this target? */ if (syscalls_info !=3D NULL && !syscalls_info->my_gdb_datadir.empty () && filename_cmp (syscalls_info->my_gdb_datadir.c_str (), - gdb_datadir) !=3D 0) + gdb_datadir.c_str ()) !=3D 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 !=3D 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.")); =20 --=20 2.23.0.187.g17f5b7556c-goog