[Lldb-commits] [lldb] e1cd7ca - [lldb] Tab completion for process load/unload

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Fri Aug 21 01:37:04 PDT 2020


Author: Gongyu Deng
Date: 2020-08-21T10:36:39+02:00
New Revision: e1cd7cac8a36608616d515b64d12f2e86643970d

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

LOG: [lldb] Tab completion for process load/unload

1. Complete `process load` with the common disk file completion, so there is not test provided for it;
2. Complete `process unload` with the tokens of valid loaded images.

Thanks for Raphael's help on the test for `process unload`.

Reviewed By: teemperor

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

Added: 
    lldb/test/API/functionalities/completion/shared.cpp

Modified: 
    lldb/include/lldb/Target/Process.h
    lldb/source/Commands/CommandObjectProcess.cpp
    lldb/test/API/functionalities/completion/Makefile
    lldb/test/API/functionalities/completion/TestCompletion.py

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h
index 42f10082b981..90172f39412d 100644
--- a/lldb/include/lldb/Target/Process.h
+++ b/lldb/include/lldb/Target/Process.h
@@ -739,6 +739,12 @@ class Process : public std::enable_shared_from_this<Process>,
 
   void SetShouldDetach(bool b) { m_should_detach = b; }
 
+  /// Get the image vector for the current process.
+  ///
+  /// \return
+  ///     The constant reference to the member m_image_tokens.
+  const std::vector<lldb::addr_t>& GetImageTokens() { return m_image_tokens; }
+
   /// Get the image information address for the current process.
   ///
   /// Some runtimes have system functions that can help dynamic loaders locate

diff  --git a/lldb/source/Commands/CommandObjectProcess.cpp b/lldb/source/Commands/CommandObjectProcess.cpp
index a5df62f23345..25fe2e4b8b1a 100644
--- a/lldb/source/Commands/CommandObjectProcess.cpp
+++ b/lldb/source/Commands/CommandObjectProcess.cpp
@@ -923,6 +923,17 @@ class CommandObjectProcessLoad : public CommandObjectParsed {
 
   ~CommandObjectProcessLoad() override = default;
 
+  void
+  HandleArgumentCompletion(CompletionRequest &request,
+                           OptionElementVector &opt_element_vector) override {
+    if (!m_exe_ctx.HasProcessScope())
+      return;
+
+    CommandCompletions::InvokeCommonCompletionCallbacks(
+        GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
+        request, nullptr);
+  }
+
   Options *GetOptions() override { return &m_options; }
 
 protected:
@@ -988,6 +999,24 @@ class CommandObjectProcessUnload : public CommandObjectParsed {
 
   ~CommandObjectProcessUnload() override = default;
 
+  void
+  HandleArgumentCompletion(CompletionRequest &request,
+                           OptionElementVector &opt_element_vector) override {
+
+    if (request.GetCursorIndex() || !m_exe_ctx.HasProcessScope())
+      return;
+
+    Process *process = m_exe_ctx.GetProcessPtr();
+
+    const std::vector<lldb::addr_t> &tokens = process->GetImageTokens();
+    const size_t token_num = tokens.size();
+    for (size_t i = 0; i < token_num; ++i) {
+      if (tokens[i] == LLDB_INVALID_IMAGE_TOKEN)
+        continue;
+      request.TryCompleteCurrentArg(std::to_string(i));
+    }
+  }
+
 protected:
   bool DoExecute(Args &command, CommandReturnObject &result) override {
     Process *process = m_exe_ctx.GetProcessPtr();

diff  --git a/lldb/test/API/functionalities/completion/Makefile b/lldb/test/API/functionalities/completion/Makefile
index 99998b20bcb0..f46742243bd9 100644
--- a/lldb/test/API/functionalities/completion/Makefile
+++ b/lldb/test/API/functionalities/completion/Makefile
@@ -1,3 +1,10 @@
 CXX_SOURCES := main.cpp
+USE_LIBDL := 1
+
+a.out: lib_shared
+
+lib_shared:
+	$(MAKE) -f $(MAKEFILE_RULES) \
+		DYLIB_ONLY=YES DYLIB_CXX_SOURCES=shared.cpp DYLIB_NAME=shared
 
 include Makefile.rules

diff  --git a/lldb/test/API/functionalities/completion/TestCompletion.py b/lldb/test/API/functionalities/completion/TestCompletion.py
index 4922cca7b722..4c81288b3836 100644
--- a/lldb/test/API/functionalities/completion/TestCompletion.py
+++ b/lldb/test/API/functionalities/completion/TestCompletion.py
@@ -85,6 +85,31 @@ def test_process_launch_arch(self):
                               ['mips',
                                'arm64'])
 
+    def test_process_load(self):
+        self.build()
+        lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
+        self.complete_from_to('process load Makef', 'process load Makefile')
+
+    @skipUnlessPlatform(["linux"])
+    def test_process_unload(self):
+        """Test the completion for "process unload <index>" """
+        # This tab completion should not work without a running process.
+        self.complete_from_to('process unload ',
+                              'process unload ')
+
+        self.build()
+        lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
+        err = lldb.SBError()
+        self.process().LoadImage(lldb.SBFileSpec(self.getBuildArtifact("libshared.so")), err)
+        self.assertSuccess(err)
+
+        self.complete_from_to('process unload ',
+                              'process unload 0')
+
+        self.process().UnloadImage(0)
+        self.complete_from_to('process unload ',
+                              'process unload ')
+
     def test_process_plugin_completion(self):
         subcommands = ['attach -P', 'connect -p', 'launch -p']
 

diff  --git a/lldb/test/API/functionalities/completion/shared.cpp b/lldb/test/API/functionalities/completion/shared.cpp
new file mode 100644
index 000000000000..89c187559e7f
--- /dev/null
+++ b/lldb/test/API/functionalities/completion/shared.cpp
@@ -0,0 +1,3 @@
+int foo() {
+  return 123;
+}


        


More information about the lldb-commits mailing list