From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id D930D3857C74 for ; Fri, 7 May 2021 10:29:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D930D3857C74 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=andrew.burgess@embecosm.com Received: by mail-wr1-x42a.google.com with SMTP id l2so8656837wrm.9 for ; Fri, 07 May 2021 03:29:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=RHnCVyxpHY+AMNJ0GPSVATUVaJLEsz575kQMOYgq+Os=; b=ECAwP+zIFp32m4ysIGMiOyPAP6H2bPkrrVuKrNSXA4AIdrbIJt99ierKGtcQoLIzRy 4qxA28enCba0W3/WEvUhgV+ffLbM9swTMvu/6gx1RzzNwWuoGG3Ge9t+2pQSwPubL7iX i7eI0Ij1+NGkYPlanKtT6hhgCSddIkSHWrTor2d+DT/lCR1NRhsYwp0QK1T45eoBCgxm X+gKCKH0tOFEyB1GK3/tYkzNfY0FAKC5I7I0IJqKASEiSy8NwH2IvQK90EHYl+BkIEr2 zzD61Tz6TCE9DmvvG3ylWZp1pXTHevjw8AqJLHtT0n/Lgg7dJoXGT8JZY2SanAQfjGkk 54lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=RHnCVyxpHY+AMNJ0GPSVATUVaJLEsz575kQMOYgq+Os=; b=dZxUUenY9wMBvozM1k9+wwV2Q7T7GQvZIkuLv8E9hqB8cpf+VqsEwF7inxHGCti1Vp orWpo2pbj/jvzhNopC+BI+nobo4eSUXPSgwJsSFyriev9kQf9Rgod4XdH4JdQTHEny2J DP13SQ1pO3+WdVpBxm6ut+ufFknx+aA3h8FEhNmYfJ5RN61g+84t7NoodlwmXckIGJFX Rb06nMWi3pxqqi9ezeygTvHvUTq05FRCo8wHHtYNAEsfRdP8MPjKHaYvtnayJ7mtQeFU ZKpOm3dKDK2Riuh4WJaNzefnTbScZkypGsMX+seT0QsmeizBbCdXDaxY1T2krS9vZzds IpbA== X-Gm-Message-State: AOAM530VrniccuoFRYbmOKkvQUvgQIlCYz7hHdU2P/TJ9n94sx3nxIGY VCKPo6EvZ7sNA0OBt4KkIB+LfeiYmZx60w== X-Google-Smtp-Source: ABdhPJwXRlWm/j7JJYxPTdEgwO3hWHQCZ+BfcXy/VAU5DQ6tFs+zYIsZwUnfkmWa9CPl9C56FIZlCQ== X-Received: by 2002:adf:f751:: with SMTP id z17mr11143965wrp.175.1620383351923; Fri, 07 May 2021 03:29:11 -0700 (PDT) Received: from localhost (host109-151-46-70.range109-151.btcentralplus.com. [109.151.46.70]) by smtp.gmail.com with ESMTPSA id y14sm8078399wrs.64.2021.05.07.03.29.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 May 2021 03:29:11 -0700 (PDT) Date: Fri, 7 May 2021 11:29:10 +0100 From: Andrew Burgess To: Simon Marchi Cc: gdb-patches@sourceware.org Subject: Re: [PATCHv2 1/2] gdb/guile: perform tilde expansion when sourcing guile scripts Message-ID: <20210507102910.GL6612@embecosm.com> References: <87bl9pq9li.fsf@tromey.com> <6e22f6c142ac81db0ec98d3c9485be5efcf58eca.1620248278.git.andrew.burgess@embecosm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: Linux/5.8.18-100.fc31.x86_64 (x86_64) X-Uptime: 11:28:25 up 3 days, 23:22, X-Editor: GNU Emacs [ http://www.gnu.org/software/emacs ] X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 May 2021 10:29:14 -0000 * Simon Marchi [2021-05-05 22:23:11 -0400]: > > > On 2021-05-05 5:01 p.m., Andrew Burgess wrote: > > Before this patch: > > > > (gdb) source ~/script.scm > > ERROR: In procedure apply-smob/1: > > ERROR: In procedure primitive-load-path: Unable to find file "~/script.scm" in load path > > Error while executing Scheme code. > > (gdb) > > > > This is because the path is not tilde expanded. In contrast, when > > sourcing a .py or .gdb script the path is tilde expanded. > > > > This commit fixes this oversight, and allows the above source command > > to work as expected. > > > > The tilde expansion is done in guile/scm-safe-call.c rather than at a > > higher level, for example, it might be tempting to add the tilde > > expansion in cli/cli-cmds.c:source_script_from_stream, however, not > > every extension language wants to see a tilde expanded file path. For > > example, consider Python. Currently we pass in an unexpanded path and > > we see this behaviour: > > > > (gdb) source ~/tmp/xxx.py > > Traceback (most recent call last): > > File "~/tmp/xxx.py", line 1, in > > NameError: name 'undefined' is not defined > > > > If we performed tilde expansion prior to calling the Python sourcer > > function (gdbpy_source_script), we would instead see: > > > > (gdb) source ~/tmp/xxx.py > > Traceback (most recent call last): > > File "/home/andrew/tmp/xxx.py", line 1, in > > undefined > > NameError: name 'undefined' is not defined > > > > Notice the path expansion in the second line of the error message, I > > believe this is a change for the worse. > > I really don't have a strong opinion, but I don't think this would be a > problematic change. The tilde-expansion thing is really a shell thing > (and we replicate it in GDB). But once the paths are processed by > programs such as Python, the tilde usually doesn't have a special > meaning. > > If I do: > > $ python3 ~/foo.py > > I get: > > Traceback (most recent call last): > File "/home/simark/foo.py", line 1, in > blah > NameError: name 'blah' is not defined > > Because the shell did the tilde-expansion before passing the file name > to Python. If I do: > > $ python3 '~/foo.py' > > then I get: > > python3: can't open file '/home/simark/build/binutils-gdb-one-target/gdb/~/foo.py': [Errno 2] No such file or directory > > Python shows the tilde version of the path in our error messages just > becore we tell it that's the name of the file, Python doesn't use that > name otherwise (I think). We could tell it the filename is "potato", > and it would say that the error is in file "potato". > > So in a sense, passing the tilde-expanded version would make it look > more like the "regular" Python interpreter. > > But like I said, I don't have a strong opinion: in the end this is shown > to the user, they will understand it either way. > > Otherwise, the patch LGTM. I think you convinced me. How's the patch below? Thanks, Andrew --- commit 25d175146367939dca8b45a155fd85fb0cc28092 Author: Andrew Burgess Date: Wed May 5 16:50:17 2021 +0100 gdb/guile: perform tilde expansion when sourcing guile scripts Before this patch: (gdb) source ~/script.scm ERROR: In procedure apply-smob/1: ERROR: In procedure primitive-load-path: Unable to find file "~/script.scm" in load path Error while executing Scheme code. (gdb) This is because the path is not tilde expanded. In contrast, when sourcing a .py or .gdb script the path is tilde expanded. This commit fixes this oversight, and allows the above source command to work as expected. The tilde expansion is done in the generic GDB code before we call the sourcer function for any particular extension language. gdb/ChangeLog: * cli/cli-cmds.c: Add 'gdbsupport/gdb_tilde_expand.h' include. (source_script_with_search): Perform tilde expansion. gdb/testsuite/ChangeLog: * gdb.guile/guile.exp: Add an extra test. diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index 68ef92eca4c..c641b9db13d 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -54,6 +54,7 @@ #include "extension.h" #include "gdbsupport/pathstuff.h" +#include "gdbsupport/gdb_tilde_expand.h" #ifdef TUI #include "tui/tui.h" /* For tui_active et.al. */ @@ -737,8 +738,10 @@ source_script_with_search (const char *file, int from_tty, int search_path) anyway so that error messages show the actual file used. But only do this if we (may have) used search_path, as printing the full path in errors for the non-search case can be more noise than signal. */ + gdb::unique_xmalloc_ptr file_to_open = gdb_tilde_expand_up (file); source_script_from_stream (opened->stream.get (), file, - search_path ? opened->full_path.get () : file); + search_path ? opened->full_path.get () + : file_to_open.get ()); } /* Wrapper around source_script_with_search to export it to main.c diff --git a/gdb/testsuite/gdb.guile/guile.exp b/gdb/testsuite/gdb.guile/guile.exp index 6e464cc0e77..0fb82284f46 100644 --- a/gdb/testsuite/gdb.guile/guile.exp +++ b/gdb/testsuite/gdb.guile/guile.exp @@ -82,3 +82,10 @@ gdb_test_no_output "guile (define a (execute \"help\" #:to-string #t))" \ gdb_test "guile (print a)" "= .*aliases -- User-defined aliases of other commands.*" \ "verify help to uiout" + +# Verify that we can source a guile script using ~ for the HOME directory. +save_vars { env(HOME) } { + set env(HOME) $srcdir/$subdir + clean_restart + gdb_test "source ~/source2.scm" "yes" +}