This is going to break the windows build, as getpwuid doesnt exist. Can you wrap this?<br><br>As an aside, why not submit a patch to the llvm function instead, it makes more sense there<br><div class="gmail_quote"><div dir="ltr">On Tue, Apr 19, 2016 at 4:10 PM Greg Clayton via lldb-commits <<a href="mailto:lldb-commits@lists.llvm.org">lldb-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: gclayton<br>
Date: Tue Apr 19 18:04:35 2016<br>
New Revision: 266832<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=266832&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=266832&view=rev</a><br>
Log:<br>
llvm::sys::path::home_directory() relies on having "HOME" set in the environment and that might not always be set. Our FileSpec class uses this function to resolve any paths that start with "~/" on systems that support home directories as '~'. I have modified FileSpec::ResolveUsername (llvm::SmallVectorImpl<char> &path) to deal with the cases where llvm::sys::path::home_directory() returns false by digging a little further on unix systems and setting "HOME" in the environment so that subsequent calls to llvm::sys::path::home_directory() will succeed.<br>
<br>
I also added a test to ensure we don't regress.<br>
<br>
<rdar://problem/25342377><br>
<br>
<br>
Added:<br>
lldb/trunk/packages/Python/lldbsuite/test/functionalities/llvm/<br>
lldb/trunk/packages/Python/lldbsuite/test/functionalities/llvm/TestLLVM.py<br>
Modified:<br>
lldb/trunk/source/Host/common/FileSpec.cpp<br>
<br>
Added: lldb/trunk/packages/Python/lldbsuite/test/functionalities/llvm/TestLLVM.py<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/llvm/TestLLVM.py?rev=266832&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/llvm/TestLLVM.py?rev=266832&view=auto</a><br>
==============================================================================<br>
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/llvm/TestLLVM.py (added)<br>
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/llvm/TestLLVM.py Tue Apr 19 18:04:35 2016<br>
@@ -0,0 +1,60 @@<br>
+"""<br>
+Test lldb 'commands regex' command which allows the user to create a regular expression command.<br>
+"""<br>
+<br>
+from __future__ import print_function<br>
+<br>
+<br>
+<br>
+import os<br>
+import lldb<br>
+from lldbsuite.test.decorators import *<br>
+from lldbsuite.test.lldbtest import *<br>
+from lldbsuite.test import lldbutil<br>
+<br>
+class TestHomeDirectory(TestBase):<br>
+<br>
+ mydir = TestBase.compute_mydir(__file__)<br>
+<br>
+ @expectedFailureAll(oslist=["windows"])<br>
+ @no_debug_info_test<br>
+ def test_tilde_home_directory(self):<br>
+ """Test that we can resolve "~/" in paths correctly.<br>
+ When a path starts with "~/", we use llvm::sys::path::home_directory() to<br>
+ resolve the home directory. This currently relies on "HOME" being set in the<br>
+ environment. While this is usually set, we can't rely upon that. We might<br>
+ eventually get a fix into llvm::sys::path::home_directory() so it doesn't rely<br>
+ on having to have an environment variable set, but until then we have work around<br>
+ code in FileSpec::ResolveUsername (llvm::SmallVectorImpl<char> &path) to ensure<br>
+ this always works. This test tests that we get the correct answer for with and<br>
+ without "HOME" being set in the environment."""<br>
+ import pexpect<br>
+ prompt = "(lldb) "<br>
+<br>
+ child = pexpect.spawn('%s --no-use-colors %s' % (lldbtest_config.lldbExec, self.lldbOption))<br>
+ # Turn on logging for what the child sends back.<br>
+ if self.TraceOn():<br>
+ child.logfile_read = sys.stdout<br>
+ # So that the spawned lldb session gets shutdown durng teardown.<br>
+ self.child = child<br>
+<br>
+ # Resolve "~/." to the full path of our home directory + "/."<br>
+ if 'HOME' in os.environ:<br>
+ home_dir = os.environ['HOME']<br>
+ if self.TraceOn():<br>
+ print("home directory is: '%s'" % (home_dir))<br>
+ if os.path.exists(home_dir):<br>
+ home_dir_slash_dot = home_dir + '/.'<br>
+ child.expect_exact(prompt)<br>
+ child.sendline('''script str(lldb.SBFileSpec("~/.", True))''')<br>
+ child.expect_exact(home_dir)<br>
+ child.expect_exact(prompt)<br>
+ child.sendline('''script import os; os.unsetenv('HOME'); str(lldb.SBFileSpec("~/", True))''');<br>
+ child.expect_exact(home_dir)<br>
+ child.expect_exact(prompt)<br>
+ elif self.TraceOn():<br>
+ print('''home directory "%s" doesn't exist, skipping home directory test''' % (home_dir))<br>
+ elif self.TraceOn():<br>
+ print('"HOME" not in environment, skipping home directory test')<br>
+<br>
+ child.sendline('quit')<br>
<br>
Modified: lldb/trunk/source/Host/common/FileSpec.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/FileSpec.cpp?rev=266832&r1=266831&r2=266832&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/FileSpec.cpp?rev=266832&r1=266831&r2=266832&view=diff</a><br>
==============================================================================<br>
--- lldb/trunk/source/Host/common/FileSpec.cpp (original)<br>
+++ lldb/trunk/source/Host/common/FileSpec.cpp Tue Apr 19 18:04:35 2016<br>
@@ -185,8 +185,22 @@ FileSpec::ResolveUsername (llvm::SmallVe<br>
{<br>
// A path of ~/ resolves to the current user's home dir<br>
llvm::SmallString<64> home_dir;<br>
+ // llvm::sys::path::home_directory() only checks if "HOME" is set in the<br>
+ // environment and does nothing else to locate the user home directory<br>
if (!llvm::sys::path::home_directory(home_dir))<br>
- return;<br>
+ {<br>
+ struct passwd *pw = getpwuid(getuid());<br>
+ if (pw && pw->pw_dir && pw->pw_dir[0])<br>
+ {<br>
+ // Update our environemnt so llvm::sys::path::home_directory() works next time<br>
+ setenv("HOME", pw->pw_dir, 0);<br>
+ home_dir.assign(llvm::StringRef(pw->pw_dir));<br>
+ }<br>
+ else<br>
+ {<br>
+ return;<br>
+ }<br>
+ }<br>
<br>
// Overwrite the ~ with the first character of the homedir, and insert<br>
// the rest. This way we only trigger one move, whereas an insert<br>
<br>
<br>
_______________________________________________<br>
lldb-commits mailing list<br>
<a href="mailto:lldb-commits@lists.llvm.org" target="_blank">lldb-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits</a><br>
</blockquote></div>