[Lldb-commits] [lldb] 763bc23 - [lldb] Tab completion for process plugin name

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Wed May 27 05:11:38 PDT 2020


Author: Gongyu Deng
Date: 2020-05-27T14:11:16+02:00
New Revision: 763bc2305797c980a4f4fa2f6314ed78a010678d

URL: https://github.com/llvm/llvm-project/commit/763bc2305797c980a4f4fa2f6314ed78a010678d
DIFF: https://github.com/llvm/llvm-project/commit/763bc2305797c980a4f4fa2f6314ed78a010678d.diff

LOG: [lldb] Tab completion for process plugin name

Summary:

1. Added tab completion to `process launch -p`, `process attach -P`, `process
connect -p`;

2. Bound the plugin name common completion as the default completion for
`eArgTypePlugin` arguments.

Reviewers: teemperor, JDevlieghere

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D79929

Added: 
    

Modified: 
    lldb/include/lldb/Core/PluginManager.h
    lldb/include/lldb/Interpreter/CommandCompletions.h
    lldb/source/Commands/CommandCompletions.cpp
    lldb/source/Commands/CommandObjectProcess.cpp
    lldb/source/Core/PluginManager.cpp
    lldb/source/Interpreter/CommandObject.cpp
    lldb/test/API/functionalities/completion/TestCompletion.py

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/PluginManager.h b/lldb/include/lldb/Core/PluginManager.h
index 4cae597d3732..5e0c9395dae0 100644
--- a/lldb/include/lldb/Core/PluginManager.h
+++ b/lldb/include/lldb/Core/PluginManager.h
@@ -243,6 +243,9 @@ class PluginManager {
 
   static const char *GetProcessPluginDescriptionAtIndex(uint32_t idx);
 
+  static void AutoCompleteProcessName(llvm::StringRef partial_name,
+                                      CompletionRequest &request);
+
   // ScriptInterpreter
   static bool RegisterPlugin(ConstString name, const char *description,
                              lldb::ScriptLanguage script_lang,

diff  --git a/lldb/include/lldb/Interpreter/CommandCompletions.h b/lldb/include/lldb/Interpreter/CommandCompletions.h
index dc2bf841620d..39d1c98eaa39 100644
--- a/lldb/include/lldb/Interpreter/CommandCompletions.h
+++ b/lldb/include/lldb/Interpreter/CommandCompletions.h
@@ -36,10 +36,11 @@ class CommandCompletions {
     eVariablePathCompletion = (1u << 8),
     eRegisterCompletion = (1u << 9),
     eBreakpointCompletion = (1u << 10),
+    eProcessPluginCompletion = (1u << 11),
     // This item serves two purposes.  It is the last element in the enum, so
     // you can add custom enums starting from here in your Option class. Also
     // if you & in this bit the base code will not process the option.
-    eCustomCompletion = (1u << 11)
+    eCustomCompletion = (1u << 12)
   };
 
   static bool InvokeCommonCompletionCallbacks(
@@ -89,6 +90,10 @@ class CommandCompletions {
 
   static void Breakpoints(CommandInterpreter &interpreter,
                           CompletionRequest &request, SearchFilter *searcher);
+
+  static void ProcessPluginNames(CommandInterpreter &interpreter,
+                                 CompletionRequest &request,
+                                 SearchFilter *searcher);
 };
 
 } // namespace lldb_private

diff  --git a/lldb/source/Commands/CommandCompletions.cpp b/lldb/source/Commands/CommandCompletions.cpp
index d4e4f6a5ebb5..11198f68490d 100644
--- a/lldb/source/Commands/CommandCompletions.cpp
+++ b/lldb/source/Commands/CommandCompletions.cpp
@@ -58,6 +58,7 @@ bool CommandCompletions::InvokeCommonCompletionCallbacks(
       {eVariablePathCompletion, CommandCompletions::VariablePath},
       {eRegisterCompletion, CommandCompletions::Registers},
       {eBreakpointCompletion, CommandCompletions::Breakpoints},
+      {eProcessPluginCompletion, CommandCompletions::ProcessPluginNames},
       {eNoCompletion, nullptr} // This one has to be last in the list.
   };
 
@@ -582,3 +583,10 @@ void CommandCompletions::Breakpoints(CommandInterpreter &interpreter,
     request.TryCompleteCurrentArg(std::to_string(bp->GetID()), bp_info);
   }
 }
+
+void CommandCompletions::ProcessPluginNames(CommandInterpreter &interpreter,
+                                            CompletionRequest &request,
+                                            SearchFilter *searcher) {
+  PluginManager::AutoCompleteProcessName(request.GetCursorArgumentPrefix(),
+                                         request);
+}
\ No newline at end of file

diff  --git a/lldb/source/Commands/CommandObjectProcess.cpp b/lldb/source/Commands/CommandObjectProcess.cpp
index 043765a0c09c..4f591b53aaa6 100644
--- a/lldb/source/Commands/CommandObjectProcess.cpp
+++ b/lldb/source/Commands/CommandObjectProcess.cpp
@@ -325,34 +325,38 @@ class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
       int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
       int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
 
-      // We are only completing the name option for now...
-
-      // Are we in the name?
-      if (GetDefinitions()[opt_defs_index].short_option != 'n')
-        return;
-
-      // Look to see if there is a -P argument provided, and if so use that
-      // plugin, otherwise use the default plugin.
-
-      const char *partial_name = nullptr;
-      partial_name = request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos);
-
-      PlatformSP platform_sp(interpreter.GetPlatform(true));
-      if (!platform_sp)
-        return;
-      ProcessInstanceInfoList process_infos;
-      ProcessInstanceInfoMatch match_info;
-      if (partial_name) {
-        match_info.GetProcessInfo().GetExecutableFile().SetFile(
-            partial_name, FileSpec::Style::native);
-        match_info.SetNameMatchType(NameMatch::StartsWith);
-      }
-      platform_sp->FindProcesses(match_info, process_infos);
-      const size_t num_matches = process_infos.size();
-      if (num_matches == 0)
-        return;
-      for (size_t i = 0; i < num_matches; ++i) {
-        request.AddCompletion(process_infos[i].GetNameAsStringRef());
+      switch (GetDefinitions()[opt_defs_index].short_option) {
+      case 'n': {
+        // Look to see if there is a -P argument provided, and if so use that
+        // plugin, otherwise use the default plugin.
+
+        const char *partial_name = nullptr;
+        partial_name = request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos);
+
+        PlatformSP platform_sp(interpreter.GetPlatform(true));
+        if (!platform_sp)
+          return;
+        ProcessInstanceInfoList process_infos;
+        ProcessInstanceInfoMatch match_info;
+        if (partial_name) {
+          match_info.GetProcessInfo().GetExecutableFile().SetFile(
+              partial_name, FileSpec::Style::native);
+          match_info.SetNameMatchType(NameMatch::StartsWith);
+        }
+        platform_sp->FindProcesses(match_info, process_infos);
+        const size_t num_matches = process_infos.size();
+        if (num_matches == 0)
+          return;
+        for (size_t i = 0; i < num_matches; ++i) {
+          request.AddCompletion(process_infos[i].GetNameAsStringRef());
+        }
+      } break;
+
+      case 'P':
+        CommandCompletions::InvokeCommonCompletionCallbacks(
+            interpreter, CommandCompletions::eProcessPluginCompletion, request,
+            nullptr);
+        break;
       }
     }
 

diff  --git a/lldb/source/Core/PluginManager.cpp b/lldb/source/Core/PluginManager.cpp
index 2a1f094534d9..3545ef66cc38 100644
--- a/lldb/source/Core/PluginManager.cpp
+++ b/lldb/source/Core/PluginManager.cpp
@@ -830,6 +830,14 @@ PluginManager::GetProcessCreateCallbackForPluginName(ConstString name) {
   return GetProcessInstances().GetCallbackForName(name);
 }
 
+void PluginManager::AutoCompleteProcessName(llvm::StringRef name,
+                                            CompletionRequest &request) {
+  for (const auto &instance : GetProcessInstances().GetInstances()) {
+    if (instance.name.GetStringRef().startswith(name))
+      request.AddCompletion(instance.name.GetCString(), instance.description);
+  }
+}
+
 #pragma mark ScriptInterpreter
 
 struct ScriptInterpreterInstance

diff  --git a/lldb/source/Interpreter/CommandObject.cpp b/lldb/source/Interpreter/CommandObject.cpp
index ddf1f5511ecd..4cadaa373d14 100644
--- a/lldb/source/Interpreter/CommandObject.cpp
+++ b/lldb/source/Interpreter/CommandObject.cpp
@@ -1080,7 +1080,7 @@ CommandObject::ArgumentTableEntry CommandObject::g_arguments_data[] = {
     { eArgTypePermissionsNumber, "perms-numeric", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as an octal number (e.g. 755)." },
     { eArgTypePermissionsString, "perms=string", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as a string value (e.g. rw-r-xr--)." },
     { eArgTypePid, "pid", CommandCompletions::eNoCompletion, { nullptr, false }, "The process ID number." },
-    { eArgTypePlugin, "plugin", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
+    { eArgTypePlugin, "plugin", CommandCompletions::eProcessPluginCompletion, { nullptr, false }, "Help text goes here." },
     { eArgTypeProcessName, "process-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of the process." },
     { eArgTypePythonClass, "python-class", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python class." },
     { eArgTypePythonFunction, "python-function", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python function." },

diff  --git a/lldb/test/API/functionalities/completion/TestCompletion.py b/lldb/test/API/functionalities/completion/TestCompletion.py
index a53ade542f46..7c674dc872ed 100644
--- a/lldb/test/API/functionalities/completion/TestCompletion.py
+++ b/lldb/test/API/functionalities/completion/TestCompletion.py
@@ -85,6 +85,13 @@ def test_process_launch_arch(self):
                               ['mips',
                                'arm64'])
 
+    def test_process_plugin_completion(self):
+        subcommands = ['attach -P', 'connect -p', 'launch -p']
+
+        for subcommand in subcommands:
+            self.complete_from_to('process ' + subcommand + ' mac',
+                                  'process ' + subcommand + ' mach-o-core')
+
     def test_process_signal(self):
         # The tab completion for "process signal"  won't work without a running process.
         self.complete_from_to('process signal ',


        


More information about the lldb-commits mailing list