From patchwork Sat Mar 3 05:10:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 26170 Received: (qmail 22399 invoked by alias); 3 Mar 2018 05:10:25 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 22328 invoked by uid 89); 3 Mar 2018 05:10:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_SOFTFAIL autolearn=ham version=3.3.2 spammy= X-HELO: barracuda.ebox.ca Received: from barracuda.ebox.ca (HELO barracuda.ebox.ca) (96.127.255.19) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 03 Mar 2018 05:10:22 +0000 X-ASG-Debug-ID: 1520053820-0c856e61891f1ad0001-fS2M51 Received: from smtp.ebox.ca (smtp.electronicbox.net [96.127.255.82]) by barracuda.ebox.ca with ESMTP id 1xEBfQp2Qu4nJZ9o (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 03 Mar 2018 00:10:20 -0500 (EST) X-Barracuda-Envelope-From: simon.marchi@polymtl.ca X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from simark.lan (192-222-251-162.qc.cable.ebox.net [192.222.251.162]) by smtp.ebox.ca (Postfix) with ESMTP id 33DA3441B21; Sat, 3 Mar 2018 00:10:20 -0500 (EST) From: Simon Marchi X-Barracuda-Effective-Source-IP: 192-222-251-162.qc.cable.ebox.net[192.222.251.162] X-Barracuda-Apparent-Source-IP: 192.222.251.162 X-Barracuda-RBL-IP: 192.222.251.162 To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 1/2] Make find_separate_debug_file* return std::string Date: Sat, 3 Mar 2018 00:10:15 -0500 X-ASG-Orig-Subj: [PATCH 1/2] Make find_separate_debug_file* return std::string Message-Id: <20180303051016.28157-1-simon.marchi@polymtl.ca> X-Barracuda-Connect: smtp.electronicbox.net[96.127.255.82] X-Barracuda-Start-Time: 1520053820 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Barracuda-Scan-Msg-Size: 11323 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.50 X-Barracuda-Spam-Status: No, SCORE=0.50 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests=BSF_RULE7568M X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.48517 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_RULE7568M Custom Rule 7568M X-IsSubscribed: yes This patch makes the find_separate_debug_file* functions return std::string, which allows to get rid of some manual memory management and one cleanup. gdb/ChangeLog: * build-id.c (find_separate_debug_file_by_buildid): Return std::string. * build-id.h (find_separate_debug_file_by_buildid): Return std::string. * coffread.c (coff_symfile_read): Adjust to std::string. * elfread.c (elf_symfile_read): Adjust to std::string. * symfile.c (separate_debug_file_exists): Change parameter to std::string. (find_separate_debug_file): Return std::string. (find_separate_debug_file_by_debuglink): Return std::string. * symfile.h (find_separate_debug_file_by_debuglink): Return std::string. --- gdb/build-id.c | 7 +++--- gdb/build-id.h | 8 +++---- gdb/coffread.c | 15 +++++------- gdb/elfread.c | 13 +++++----- gdb/symfile.c | 76 +++++++++++++++++++++++----------------------------------- gdb/symfile.h | 2 +- 6 files changed, 51 insertions(+), 70 deletions(-) diff --git a/gdb/build-id.c b/gdb/build-id.c index 57d98c9618..a5d4e67971 100644 --- a/gdb/build-id.c +++ b/gdb/build-id.c @@ -136,7 +136,7 @@ build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id) /* See build-id.h. */ -char * +std::string find_separate_debug_file_by_buildid (struct objfile *objfile) { const struct bfd_build_id *build_id; @@ -157,7 +157,8 @@ find_separate_debug_file_by_buildid (struct objfile *objfile) warning (_("\"%s\": separate debug info file has no debug info"), bfd_get_filename (abfd.get ())); else if (abfd != NULL) - return xstrdup (bfd_get_filename (abfd.get ())); + return std::string (bfd_get_filename (abfd.get ())); } - return NULL; + + return std::string (); } diff --git a/gdb/build-id.h b/gdb/build-id.h index 0f13c7d4cf..15fb609409 100644 --- a/gdb/build-id.h +++ b/gdb/build-id.h @@ -41,10 +41,10 @@ extern gdb_bfd_ref_ptr build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id); /* Find the separate debug file for OBJFILE, by using the build-id - associated with OBJFILE's BFD. If successful, returns a malloc'd - file name for the separate debug file. The caller must free this. - Otherwise, returns NULL. */ + associated with OBJFILE's BFD. If successful, returns the file name for the + separate debug file, otherwise, return an empty string. */ -extern char *find_separate_debug_file_by_buildid (struct objfile *objfile); +extern std::string find_separate_debug_file_by_buildid + (struct objfile *objfile); #endif /* BUILD_ID_H */ diff --git a/gdb/coffread.c b/gdb/coffread.c index fbbbb68f71..cad3e7e2f1 100644 --- a/gdb/coffread.c +++ b/gdb/coffread.c @@ -733,20 +733,17 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags) /* Try to add separate debug file if no symbols table found. */ if (!objfile_has_partial_symbols (objfile)) { - char *debugfile; + std::string debugfile = find_separate_debug_file_by_buildid (objfile); - debugfile = find_separate_debug_file_by_buildid (objfile); - - if (debugfile == NULL) + if (debugfile.empty ()) debugfile = find_separate_debug_file_by_debuglink (objfile); - make_cleanup (xfree, debugfile); - if (debugfile) + if (!debugfile.empty ()) { - gdb_bfd_ref_ptr abfd (symfile_bfd_open (debugfile)); + gdb_bfd_ref_ptr abfd (symfile_bfd_open (debugfile.c_str ())); - symbol_file_add_separate (abfd.get (), debugfile, symfile_flags, - objfile); + symbol_file_add_separate (abfd.get (), debugfile.c_str (), + symfile_flags, objfile); } } diff --git a/gdb/elfread.c b/gdb/elfread.c index 103b2144c3..260789062d 100644 --- a/gdb/elfread.c +++ b/gdb/elfread.c @@ -1259,17 +1259,16 @@ elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags) && objfile->separate_debug_objfile == NULL && objfile->separate_debug_objfile_backlink == NULL) { - gdb::unique_xmalloc_ptr debugfile - (find_separate_debug_file_by_buildid (objfile)); + std::string debugfile = find_separate_debug_file_by_buildid (objfile); - if (debugfile == NULL) - debugfile.reset (find_separate_debug_file_by_debuglink (objfile)); + if (debugfile.empty ()) + debugfile = find_separate_debug_file_by_debuglink (objfile); - if (debugfile != NULL) + if (!debugfile.empty ()) { - gdb_bfd_ref_ptr abfd (symfile_bfd_open (debugfile.get ())); + gdb_bfd_ref_ptr abfd (symfile_bfd_open (debugfile.c_str ())); - symbol_file_add_separate (abfd.get (), debugfile.get (), + symbol_file_add_separate (abfd.get (), debugfile.c_str (), symfile_flags, objfile); } } diff --git a/gdb/symfile.c b/gdb/symfile.c index 72bf0d8451..58747d545b 100644 --- a/gdb/symfile.c +++ b/gdb/symfile.c @@ -1333,7 +1333,7 @@ symbol_file_clear (int from_tty) int separate_debug_file_debug = 0; static int -separate_debug_file_exists (const char *name, unsigned long crc, +separate_debug_file_exists (const std::string &name, unsigned long crc, struct objfile *parent_objfile) { unsigned long file_crc; @@ -1347,13 +1347,13 @@ separate_debug_file_exists (const char *name, unsigned long crc, ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where the separate debug infos with the same basename can exist. */ - if (filename_cmp (name, objfile_name (parent_objfile)) == 0) + if (filename_cmp (name.c_str (), objfile_name (parent_objfile)) == 0) return 0; if (separate_debug_file_debug) - printf_unfiltered (_(" Trying %s\n"), name); + printf_unfiltered (_(" Trying %s\n"), name.c_str ()); - gdb_bfd_ref_ptr abfd (gdb_bfd_open (name, gnutarget, -1)); + gdb_bfd_ref_ptr abfd (gdb_bfd_open (name.c_str (), gnutarget, -1)); if (abfd == NULL) return 0; @@ -1403,7 +1403,7 @@ separate_debug_file_exists (const char *name, unsigned long crc, if (verified_as_different || parent_crc != file_crc) warning (_("the debug information found in \"%s\"" " does not match \"%s\" (CRC mismatch).\n"), - name, objfile_name (parent_objfile)); + name.c_str (), objfile_name (parent_objfile)); return 0; } @@ -1431,46 +1431,31 @@ show_debug_file_directory (struct ui_file *file, int from_tty, dirname(objfile->name) due to symlinks), and DEBUGLINK as the file we are looking for. CANON_DIR is the "realpath" form of DIR. DIR must contain a trailing '/'. - Returns the path of the file with separate debug info, of NULL. */ + Returns the path of the file with separate debug info, or an empty + string. */ -static char * +static std::string find_separate_debug_file (const char *dir, const char *canon_dir, const char *debuglink, unsigned long crc32, struct objfile *objfile) { - char *debugfile; - int i; - if (separate_debug_file_debug) printf_unfiltered (_("\nLooking for separate debug info (debug link) for " "%s\n"), objfile_name (objfile)); - /* Set I to std::max (strlen (canon_dir), strlen (dir)). */ - i = strlen (dir); - if (canon_dir != NULL && strlen (canon_dir) > i) - i = strlen (canon_dir); - - debugfile - = (char *) xmalloc (strlen (debug_file_directory) + 1 - + i - + strlen (DEBUG_SUBDIRECTORY) - + strlen ("/") - + strlen (debuglink) - + 1); - /* First try in the same directory as the original file. */ - strcpy (debugfile, dir); - strcat (debugfile, debuglink); + std::string debugfile = dir; + debugfile += debuglink; if (separate_debug_file_exists (debugfile, crc32, objfile)) return debugfile; /* Then try in the subdirectory named DEBUG_SUBDIRECTORY. */ - strcpy (debugfile, dir); - strcat (debugfile, DEBUG_SUBDIRECTORY); - strcat (debugfile, "/"); - strcat (debugfile, debuglink); + debugfile = dir; + debugfile += DEBUG_SUBDIRECTORY; + debugfile += "/"; + debugfile += debuglink; if (separate_debug_file_exists (debugfile, crc32, objfile)) return debugfile; @@ -1485,10 +1470,10 @@ find_separate_debug_file (const char *dir, for (const gdb::unique_xmalloc_ptr &debugdir : debugdir_vec) { - strcpy (debugfile, debugdir.get ()); - strcat (debugfile, "/"); - strcat (debugfile, dir); - strcat (debugfile, debuglink); + debugfile = debugdir.get (); + debugfile += "/"; + debugfile += dir; + debugfile += debuglink; if (separate_debug_file_exists (debugfile, crc32, objfile)) return debugfile; @@ -1500,18 +1485,17 @@ find_separate_debug_file (const char *dir, strlen (gdb_sysroot)) == 0 && IS_DIR_SEPARATOR (canon_dir[strlen (gdb_sysroot)])) { - strcpy (debugfile, debugdir.get ()); - strcat (debugfile, canon_dir + strlen (gdb_sysroot)); - strcat (debugfile, "/"); - strcat (debugfile, debuglink); + debugfile = debugdir.get (); + debugfile += (canon_dir + strlen (gdb_sysroot)); + debugfile += "/"; + debugfile += debuglink; if (separate_debug_file_exists (debugfile, crc32, objfile)) return debugfile; } } - xfree (debugfile); - return NULL; + return std::string (); } /* Modify PATH to contain only "[/]directory/" part of PATH. @@ -1534,12 +1518,11 @@ terminate_after_last_dir_separator (char *path) } /* Find separate debuginfo for OBJFILE (using .gnu_debuglink section). - Returns pathname, or NULL. */ + Returns pathname, or an empty string. */ -char * +std::string find_separate_debug_file_by_debuglink (struct objfile *objfile) { - char *debugfile; unsigned long crc32; gdb::unique_xmalloc_ptr debuglink @@ -1549,17 +1532,18 @@ find_separate_debug_file_by_debuglink (struct objfile *objfile) { /* There's no separate debug info, hence there's no way we could load it => no warning. */ - return NULL; + return std::string (); } std::string dir = objfile_name (objfile); terminate_after_last_dir_separator (&dir[0]); gdb::unique_xmalloc_ptr canon_dir (lrealpath (dir.c_str ())); - debugfile = find_separate_debug_file (dir.c_str (), canon_dir.get (), - debuglink.get (), crc32, objfile); + std::string debugfile + = find_separate_debug_file (dir.c_str (), canon_dir.get (), + debuglink.get (), crc32, objfile); - if (debugfile == NULL) + if (debugfile.empty ()) { /* For PR gdb/9538, try again with realpath (if different from the original). */ diff --git a/gdb/symfile.h b/gdb/symfile.h index 7c3fd8240a..8cd47d8811 100644 --- a/gdb/symfile.h +++ b/gdb/symfile.h @@ -426,7 +426,7 @@ extern struct objfile *symbol_file_add_from_bfd (bfd *, const char *, symfile_ad extern void symbol_file_add_separate (bfd *, const char *, symfile_add_flags, struct objfile *); -extern char *find_separate_debug_file_by_debuglink (struct objfile *); +extern std::string find_separate_debug_file_by_debuglink (struct objfile *); /* Create a new section_addr_info, with room for NUM_SECTIONS. */