This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Resolve exe location for `target create`
ClosedPublic

Authored by alvinhochun on Jun 9 2022, 12:32 PM.

Details

Summary

This fixes an issue that, when you start lldb or use target create
with a program name which is on $PATH, or not specify the .exe suffix of
a program in the working directory on Windows, you get a confusing
error, for example:

(lldb) target create notepad
error: 'C:\WINDOWS\SYSTEM32\notepad.exe' doesn't contain any 'host'
platform architectures: i686, x86_64, i386, i386

Fixes https://github.com/mstorsjo/llvm-mingw/issues/265

Diff Detail

Event Timeline

alvinhochun created this revision.Jun 9 2022, 12:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 9 2022, 12:32 PM
Herald added subscribers: jsji, pengfei. · View Herald Transcript
alvinhochun requested review of this revision.Jun 9 2022, 12:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 9 2022, 12:32 PM

Is it easy to test this?

Ideally this behaviour works with a path to a file as well as PATH itself e.g. C:\foo becomes C:\foo.exe and you could write a test that only runs on Windows quite easily. If not I guess you could do a shell test that compiles a program then runs lldb with PATH set to the current dir? (though it sounds like you can use the current dir by default anyway)

I suppose it is possible to have a Windows-only test for this. (Someone will need to verify them for me though as I don't have a build that runs tests on Windows.)

This doesn't currently work with a path without the suffix though (like C:\Windows\System32\notepad would not work). ResolveExecutableLocation calls llvm::sys::findProgramByName (from llvm/lib/Support/Windows/Program.inc), which doesn't do anything if the argument is a path. I can change this behaviour, but it may affect more than just target create... I don't feel comfortable making this change here for now.

alvinhochun added a comment.EditedJun 10 2022, 12:03 PM

Does this test look good:

diff --git a/lldb/test/Shell/Commands/command-target-create-resolve-exe.test b/lldb/test/Shell/Commands/command-target-create-resolve-exe.test
new file mode 100644
index 000000000000..3dfa7f0d9853
--- /dev/null
+++ b/lldb/test/Shell/Commands/command-target-create-resolve-exe.test
@@ -0,0 +1,22 @@
+# REQUIRES: system-windows
+# RUN: mkdir "%t.dir"
+# RUN: %clang_host -g0 -O0 %S/Inputs/main.c -o %t.dir\testmain.exe
+
+## Test with full path to exe
+# RUN: %lldb %t.dir\testmain.exe -b | FileCheck %s
+
+## Test with exe on path, with .exe suffix
+# RUN: set "PATH=%t.dir;%%PATH%%" && %lldb testmain.exe -b | FileCheck %s
+
+## Test with exe on path, without .exe suffix
+# RUN: set "PATH=%t.dir;%%PATH%%" && %lldb testmain -b | FileCheck %s
+
+## Test in cwd, with .exe suffix
+# RUN: cd "%t.dir" && %lldb testmain.exe -b | FileCheck %s
+
+## Test in cwd, without .exe suffix
+# RUN: cd "%t.dir" && %lldb testmain -b | FileCheck %s
+
+# LABEL: target create
+# CHECK-NEXT: Current executable set to '{{.*[/\\]}}testmain.exe'
+# CHECK-NOT: Error

Does this test look good:

With a bunch of minor fixups, this testcase does work - I uploaded a working copy of it at https://martin.st/temp/command-target-create-resolve-exe.test.

Added Windows-specific test

This revision is now accepted and ready to land.Jun 22 2022, 1:51 AM
This revision was automatically updated to reflect the committed changes.

Yep, noted. It worked for me in my MSVC build configuration - I'll investigate a bit more and either push another attempt at fixing it, or revert, in a little while.

The second fix attempt, in https://github.com/llvm/llvm-project/commit/a1ee0b947d46c9be1cc2ea8db21603bac84efb18, seems to have fixed this test now - however the latest test run seems to have some other failures: https://lab.llvm.org/buildbot/#/builders/83/builds/20304 (Not sure if these are spurious failures or if they are other regressions that happened meanwhile)