[Lldb-commits] [lldb] 82093e8 - [lldb/Driver] Fix handling on positional arguments

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Mon May 18 18:52:24 PDT 2020


Author: Jonas Devlieghere
Date: 2020-05-18T18:52:18-07:00
New Revision: 82093e8fb7d65486ff450d33bf386aabd0d194f7

URL: https://github.com/llvm/llvm-project/commit/82093e8fb7d65486ff450d33bf386aabd0d194f7
DIFF: https://github.com/llvm/llvm-project/commit/82093e8fb7d65486ff450d33bf386aabd0d194f7.diff

LOG: [lldb/Driver] Fix handling on positional arguments

Before the transition to libOption it was possible to specify arguments
for the inferior without -- as long as they didn't start with a dash.

For example, the following invocations should all behave the same:

  $ lldb inferior inferior-arg
  $ lldb inferior -- inferior-arg
  $ lldb -- inferior inferior-arg

This patch fixes that behavior, documents it and adds a test to cover
the different combinations.

Differential revision: https://reviews.llvm.org/D80165

Added: 
    lldb/test/Shell/Driver/TestPositionalArgs.test

Modified: 
    lldb/docs/man/lldb.rst
    lldb/test/Shell/Driver/TestNoUseColor.test
    lldb/tools/driver/Driver.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/docs/man/lldb.rst b/lldb/docs/man/lldb.rst
index b4972df1b601..a3a0736680ad 100644
--- a/lldb/docs/man/lldb.rst
+++ b/lldb/docs/man/lldb.rst
@@ -251,11 +251,16 @@ EXAMPLES
 
 The debugger can be started in several modes.
 
-Passing an executable as a positional argument prepares :program:`lldb` to
-debug the given executable. Arguments passed after -- are considered arguments
-to the debugged executable.
+Passing an executable as a positional argument prepares lldb to debug the given
+executable. To disambiguate between arguments passed to lldb and arguments
+passed to the debugged executable, arguments starting with a - must be passed
+after --.
 
-  lldb --arch x86_64 /path/to/program -- --arch arvm7
+  lldb --arch x86_64 /path/to/program program argument -- --arch arvm7
+
+For convenience, passing the executable after -- is also supported.
+
+  lldb --arch x86_64 -- /path/to/program program argument --arch arvm7
 
 Passing one of the attach options causes :program:`lldb` to immediately attach
 to the given process.

diff  --git a/lldb/test/Shell/Driver/TestNoUseColor.test b/lldb/test/Shell/Driver/TestNoUseColor.test
index 62735301fca8..61d03b308f78 100644
--- a/lldb/test/Shell/Driver/TestNoUseColor.test
+++ b/lldb/test/Shell/Driver/TestNoUseColor.test
@@ -1,4 +1,4 @@
-# RUN: %lldb --no-use-color -s %s | FileCheck %s
+# RUN: %lldb --no-use-colors -s %s | FileCheck %s
 settings show use-color
 # CHECK: use-color (boolean) = false
 q

diff  --git a/lldb/test/Shell/Driver/TestPositionalArgs.test b/lldb/test/Shell/Driver/TestPositionalArgs.test
new file mode 100644
index 000000000000..327d006d814e
--- /dev/null
+++ b/lldb/test/Shell/Driver/TestPositionalArgs.test
@@ -0,0 +1,30 @@
+RUN: echo "int main() { return 0; }" | %clang_host -x c - -o %t.foo
+
+RUN: %lldb -x -b %t.foo bar baz quux | FileCheck %s
+RUN: %lldb -x -b -- %t.foo bar baz quux | FileCheck %s
+RUN: %lldb -x -b %t.foo -- bar baz quux | FileCheck %s
+RUN: %lldb -x -b %t.foo bar -- baz quux | FileCheck %s
+RUN: %lldb -x -b %t.foo bar baz -- quux | FileCheck %s
+RUN: %lldb %t.foo -x bar -b baz -- quux | FileCheck %s
+RUN: %lldb -x -b -f %t.foo bar baz quux | FileCheck %s
+RUN: %lldb -x -b -f %t.foo -- bar baz quux | FileCheck %s
+RUN: %lldb -x -b -f %t.foo bar -- baz quux | FileCheck %s
+RUN: %lldb -x -b -f %t.foo bar baz -- quux | FileCheck %s
+
+CHECK: Current executable set to {{.*}}foo
+CHECK: target.run-args "bar" "baz" "quux"
+
+RUN: %lldb -x -b %t.foo -- bar -baz --quux | FileCheck %s --check-prefix DASH
+RUN: %lldb -x -b -- %t.foo bar -baz --quux | FileCheck %s --check-prefix DASH
+RUN: %lldb -x -b -f %t.foo -- bar -baz --quux | FileCheck %s --check-prefix DASH
+
+DASH: Current executable set to {{.*}}foo
+DASH: target.run-args "bar" "-baz" "--quux"
+
+RUN: %lldb -x -b %t.foo bar -baz --quux 2>&1 | FileCheck %s --check-prefix UNKNOWN
+RUN: %lldb -x -b -f %t.foo bar -baz --quux 2>&1 | FileCheck %s --check-prefix UNKNOWN
+
+UNKNOWN: warning: ignoring unknown option: -baz
+UNKNOWN: warning: ignoring unknown option: --quux
+UNKNOWN: Current executable set to {{.*}}foo
+UNKNOWN: target.run-args "bar"

diff  --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index b38423b28559..7b783ad9c3f4 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -361,13 +361,8 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
   if (m_option_data.m_process_name.empty() &&
       m_option_data.m_process_pid == LLDB_INVALID_PROCESS_ID) {
 
-    // If the option data args array is empty that means the file was not
-    // specified with -f and we need to get it from the input args.
-    if (m_option_data.m_args.empty()) {
-      if (auto *arg = args.getLastArgNoClaim(OPT_INPUT)) {
-        m_option_data.m_args.push_back(arg->getAsString((args)));
-      }
-    }
+    for (auto *arg : args.filtered(OPT_INPUT))
+      m_option_data.m_args.push_back(arg->getAsString((args)));
 
     // Any argument following -- is an argument for the inferior.
     if (auto *arg = args.getLastArgNoClaim(OPT_REM)) {
@@ -765,10 +760,15 @@ static void printHelp(LLDBOptTable &table, llvm::StringRef tool_name) {
   The debugger can be started in several modes.
 
   Passing an executable as a positional argument prepares lldb to debug the
-  given executable. Arguments passed after -- are considered arguments to the
-  debugged executable.
+  given executable. To disambiguate between arguments passed to lldb and
+  arguments passed to the debugged executable, arguments starting with a - must
+  be passed after --.
+
+    lldb --arch x86_64 /path/to/program program argument -- --arch arvm7
+
+  For convenience, passing the executable after -- is also supported.
 
-    lldb --arch x86_64 /path/to/program -- --arch arvm7
+    lldb --arch x86_64 -- /path/to/program program argument --arch arvm7
 
   Passing one of the attach options causes lldb to immediately attach to the
   given process.


        


More information about the lldb-commits mailing list