From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19599 invoked by alias); 26 Sep 2013 01:29:21 -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 19588 invoked by uid 89); 26 Sep 2013 01:29:21 -0000 Received: from mail-yh0-f73.google.com (HELO mail-yh0-f73.google.com) (209.85.213.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Thu, 26 Sep 2013 01:29:21 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.2 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD,SPAM_SUBJECT,SPF_SOFTFAIL autolearn=no version=3.3.2 X-HELO: mail-yh0-f73.google.com Received: by mail-yh0-f73.google.com with SMTP id z20so62468yhz.4 for ; Wed, 25 Sep 2013 18:29:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-type; bh=eVyqKEjTv6WgreiGbPfY+KG2Eh4yU878waezV7gd4U0=; b=APZ5J6PUjHq+7mWCBV0VCfLY3BsdYv+05nH/67HdZaoQ/3pqFbLsKbmDn0YwemnyoD RowGoVk510tbmptK09ldH5q2u/00baP4r8BmW0FBESSj/6Dm00XqWzGn1Er5AaoX6l/a Z0y3fih9unQVStVncI9U/XEKeTb6NJBcdZM9xyR2LhTer8e7Ta3E+Wd9R7nIM629oPUC /GoRlho5DER03KVVWdx6s13ZNc1eeQgh/gLKjKTYd5CRlneJMuVZlD6zK9C/WqRz/1TV 08akttaTN4mdlWGePJjSMz2HamvtKekeXaXq0VD6PgMvNSpfp3zJW/vCSGnjdkG1HhXd STvA== X-Gm-Message-State: ALoCoQlJGlYhCvHotPxuj3T9qGTUH3Nr+9nnO+WGcOym2FL5r8Lf0qsGE/URyLj3HfNoq2wELTmUYVUfuzqL7FCUFK01eePocz5gHqmHSZJQF48eE3vn1XKUhl4/e1S+h8YTxmDxLdtasi3pW1aNJ+ZI3+Wf+gwiL2nQWH2GVUsp6jxmfb6CaagYm9FhJJ321hXzA0qPBDAuGBIXpN2gwe8MalMrM3qFmw== X-Received: by 10.236.14.100 with SMTP id c64mr10257374yhc.38.1380158957358; Wed, 25 Sep 2013 18:29:17 -0700 (PDT) Received: from corp2gmr1-2.hot.corp.google.com (corp2gmr1-2.hot.corp.google.com [172.24.189.93]) by gmr-mx.google.com with ESMTPS id y62si6327755yha.0.1969.12.31.16.00.00 (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Wed, 25 Sep 2013 18:29:17 -0700 (PDT) Received: from ruffy.mtv.corp.google.com (ruffy.mtv.corp.google.com [172.17.128.44]) by corp2gmr1-2.hot.corp.google.com (Postfix) with ESMTP id C6A025A40D9; Wed, 25 Sep 2013 18:29:16 -0700 (PDT) From: Doug Evans To: gdb-patches@sourceware.org, jan.kratochvil@redhat.com Subject: [RFC] Record objfile->original_name as an absolute path Date: Thu, 26 Sep 2013 01:29:00 -0000 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-IsSubscribed: yes X-SW-Source: 2013-09/txt/msg00915.txt.bz2 Hi. The comment for objfile->original_name says the path is stored as an absolute path, but that's not the case, and that got me thinking: What if the user cd's before the debug info is accessed? This patch is only RFC, and not RFA, because I'm not sure how to do something better in the test. I can certainly clean up what's there, if people think it's not *too* cheezy. [Later, if we ever choose to display objfile->original_name in some context, and want to clean up the name a bit (/tmp/./foo -> /tmp/foo), I can imagine making a function out of the canonicalizer in cli-cmds.c:cd_command and using that. [We wouldn't want to remove "../" for this case though unless we go to the extra trouble of not expanding symlinks.]] 2013-09-25 Doug Evans * objfiles.c (allocate_objfile): Save original_name as an absolute path. * objfiles.c (struct objfile): Expand comment on original_name. * utils.c (gdb_abspath): New function. * utils.h (gdb_abspath): Declare. testsuite/ * gdb.dwarf/dwp-symlink.c: Fake out gdb to not load debug info at start. * gdb.dwarf/dwp-symlink.exp: Test trying to load dwp when the binary has been specified with a relative path and we have chdir'd before accessing the debug info. Index: objfiles.c =================================================================== RCS file: /cvs/src/src/gdb/objfiles.c,v retrieving revision 1.168 diff -u -p -r1.168 objfiles.c --- objfiles.c 26 Sep 2013 01:08:35 -0000 1.168 +++ objfiles.c 26 Sep 2013 01:11:55 -0000 @@ -270,6 +270,7 @@ struct objfile * allocate_objfile (bfd *abfd, const char *name, int flags) { struct objfile *objfile; + char *expanded_name; objfile = (struct objfile *) xzalloc (sizeof (struct objfile)); objfile->psymbol_cache = psymbol_bcache_init (); @@ -283,10 +284,18 @@ allocate_objfile (bfd *abfd, const char if (name == NULL) { gdb_assert (abfd == NULL); - name = "<>"; + expanded_name = xstrdup ("<>"); } - objfile->original_name = obstack_copy0 (&objfile->objfile_obstack, name, - strlen (name)); + else + expanded_name = gdb_abspath (name); + objfile->original_name = obstack_copy0 (&objfile->objfile_obstack, + expanded_name, + strlen (expanded_name)); + xfree (expanded_name); + + /* Update the per-objfile information that comes from the bfd, ensuring + that any data that is reference is saved in the per-objfile data + region. */ /* Update the per-objfile information that comes from the bfd, ensuring that any data that is reference is saved in the per-objfile data Index: objfiles.h =================================================================== RCS file: /cvs/src/src/gdb/objfiles.h,v retrieving revision 1.112 diff -u -p -r1.112 objfiles.h --- objfiles.h 24 Sep 2013 14:00:06 -0000 1.112 +++ objfiles.h 26 Sep 2013 01:11:55 -0000 @@ -205,8 +205,10 @@ struct objfile struct objfile *next; - /* The object file's name, tilde-expanded and absolute. This - pointer is never NULL. This does not have to be freed; it is + /* The object file's original name as specified by the user, + made absolute, and tilde-expanded. However, it is not canonicalized + (i.e., it has not been passed through gdb_realpath). + This pointer is never NULL. This does not have to be freed; it is guaranteed to have a lifetime at least as long as the objfile. */ char *original_name; Index: utils.c =================================================================== RCS file: /cvs/src/src/gdb/utils.c,v retrieving revision 1.306 diff -u -p -r1.306 utils.c --- utils.c 13 Aug 2013 08:31:20 -0000 1.306 +++ utils.c 26 Sep 2013 01:11:55 -0000 @@ -3204,6 +3204,38 @@ gdb_realpath (const char *filename) return xstrdup (filename); } +/* Return PATH in absolute form, performing tilde-expansion if necessary. + PATH cannot be NULL or the empty string. + This does not resolve symlinks however, use gdb_realpath for that. + Space for the result is allocated with malloc. + If the path is already absolute, it is strdup'd. + If there is a problem computing the absolute path, the path is returned + unchanged (still strdup'd). */ + +char * +gdb_abspath (const char *path) +{ +#if defined (PATH_MAX) + char cwd_buf[PATH_MAX]; +#elif defined (_WIN32) + char cwd_buf[MAX_PATH]; +#else + char cwd_buf[1024]; +#endif + + gdb_assert (path != NULL && path[0] != '\0'); + + if (path[0] == '~') + return tilde_expand (path); + + if (IS_ABSOLUTE_PATH (path)) + return xstrdup (path); + + if (getcwd (cwd_buf, sizeof (cwd_buf)) == NULL) + return xstrdup (path); + return concat (cwd_buf, SLASH_STRING, path, NULL); +} + ULONGEST align_up (ULONGEST v, int n) { Index: utils.h =================================================================== RCS file: /cvs/src/src/gdb/utils.h,v retrieving revision 1.11 diff -u -p -r1.11 utils.h --- utils.h 1 Aug 2013 09:09:58 -0000 1.11 +++ utils.h 26 Sep 2013 01:11:55 -0000 @@ -128,6 +128,8 @@ extern struct cleanup *make_bpstat_clear extern char *gdb_realpath (const char *); +extern char *gdb_abspath (const char *); + extern int gdb_filename_fnmatch (const char *pattern, const char *string, int flags); Index: testsuite/gdb.dwarf2/dwp-symlink.c =================================================================== RCS file: /cvs/src/src/gdb/testsuite/gdb.dwarf2/dwp-symlink.c,v retrieving revision 1.1 diff -u -p -r1.1 dwp-symlink.c --- testsuite/gdb.dwarf2/dwp-symlink.c 24 Sep 2013 14:03:43 -0000 1.1 +++ testsuite/gdb.dwarf2/dwp-symlink.c 26 Sep 2013 01:11:55 -0000 @@ -15,6 +15,13 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +/* Cheezy hack to prevent set_initial_language from trying to look up main. + We do this so that gdb won't try to open the dwp file when the file is + first selected. This gives us a chance to do a chdir before attempting + to access the debug info. */ +asm (".globl main.main"); +asm ("main.main: .byte 0"); + int main (int argc, char **argv) { Index: testsuite/gdb.dwarf2/dwp-symlink.exp =================================================================== RCS file: /cvs/src/src/gdb/testsuite/gdb.dwarf2/dwp-symlink.exp,v retrieving revision 1.1 diff -u -p -r1.1 dwp-symlink.exp --- testsuite/gdb.dwarf2/dwp-symlink.exp 24 Sep 2013 14:03:43 -0000 1.1 +++ testsuite/gdb.dwarf2/dwp-symlink.exp 26 Sep 2013 01:11:55 -0000 @@ -75,3 +75,23 @@ gdb_test "ptype main" {type = int \(\)} clean_restart "$thelink" gdb_test "ptype main" {type = int \(int, char \*\*\)} "binary symlink, dwp at symlink" + +# Verify we can still find the dwp if we change directories and we specified +# a relative path for the program. + +set saved_pwd [pwd] + +# This is clean_restart, but specifying a relative path to the binary. +gdb_exit +gdb_start +gdb_reinitialize_dir $srcdir/$subdir +gdb_test "cd [file dirname [standard_output_file ${thelink}]]" \ + "Working directory .*" +gdb_load "./${thelink}" + +gdb_test "cd .." "Working directory .*" + +gdb_test "ptype main" {type = int \(int, char \*\*\)} \ + "relative path, binary symlink, dwp at symlink" + +cd $saved_pwd