Skip to content

Commit e1cd7ca

Browse files
MrHateTeemperor
authored andcommitted
[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
1 parent 80e9dd0 commit e1cd7ca

5 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎lldb/include/lldb/Target/Process.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,12 @@ class Process : public std::enable_shared_from_this<Process>,
739739

740740
void SetShouldDetach(bool b) { m_should_detach = b; }
741741

742+
/// Get the image vector for the current process.
743+
///
744+
/// \return
745+
/// The constant reference to the member m_image_tokens.
746+
const std::vector<lldb::addr_t>& GetImageTokens() { return m_image_tokens; }
747+
742748
/// Get the image information address for the current process.
743749
///
744750
/// Some runtimes have system functions that can help dynamic loaders locate

‎lldb/source/Commands/CommandObjectProcess.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,17 @@ class CommandObjectProcessLoad : public CommandObjectParsed {
923923

924924
~CommandObjectProcessLoad() override = default;
925925

926+
void
927+
HandleArgumentCompletion(CompletionRequest &request,
928+
OptionElementVector &opt_element_vector) override {
929+
if (!m_exe_ctx.HasProcessScope())
930+
return;
931+
932+
CommandCompletions::InvokeCommonCompletionCallbacks(
933+
GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
934+
request, nullptr);
935+
}
936+
926937
Options *GetOptions() override { return &m_options; }
927938

928939
protected:
@@ -988,6 +999,24 @@ class CommandObjectProcessUnload : public CommandObjectParsed {
988999

9891000
~CommandObjectProcessUnload() override = default;
9901001

1002+
void
1003+
HandleArgumentCompletion(CompletionRequest &request,
1004+
OptionElementVector &opt_element_vector) override {
1005+
1006+
if (request.GetCursorIndex() || !m_exe_ctx.HasProcessScope())
1007+
return;
1008+
1009+
Process *process = m_exe_ctx.GetProcessPtr();
1010+
1011+
const std::vector<lldb::addr_t> &tokens = process->GetImageTokens();
1012+
const size_t token_num = tokens.size();
1013+
for (size_t i = 0; i < token_num; ++i) {
1014+
if (tokens[i] == LLDB_INVALID_IMAGE_TOKEN)
1015+
continue;
1016+
request.TryCompleteCurrentArg(std::to_string(i));
1017+
}
1018+
}
1019+
9911020
protected:
9921021
bool DoExecute(Args &command, CommandReturnObject &result) override {
9931022
Process *process = m_exe_ctx.GetProcessPtr();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
CXX_SOURCES := main.cpp
2+
USE_LIBDL := 1
3+
4+
a.out: lib_shared
5+
6+
lib_shared:
7+
$(MAKE) -f $(MAKEFILE_RULES) \
8+
DYLIB_ONLY=YES DYLIB_CXX_SOURCES=shared.cpp DYLIB_NAME=shared
29

310
include Makefile.rules

‎lldb/test/API/functionalities/completion/TestCompletion.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,31 @@ def test_process_launch_arch(self):
8585
['mips',
8686
'arm64'])
8787

88+
def test_process_load(self):
89+
self.build()
90+
lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
91+
self.complete_from_to('process load Makef', 'process load Makefile')
92+
93+
@skipUnlessPlatform(["linux"])
94+
def test_process_unload(self):
95+
"""Test the completion for "process unload <index>" """
96+
# This tab completion should not work without a running process.
97+
self.complete_from_to('process unload ',
98+
'process unload ')
99+
100+
self.build()
101+
lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
102+
err = lldb.SBError()
103+
self.process().LoadImage(lldb.SBFileSpec(self.getBuildArtifact("libshared.so")), err)
104+
self.assertSuccess(err)
105+
106+
self.complete_from_to('process unload ',
107+
'process unload 0')
108+
109+
self.process().UnloadImage(0)
110+
self.complete_from_to('process unload ',
111+
'process unload ')
112+
88113
def test_process_plugin_completion(self):
89114
subcommands = ['attach -P', 'connect -p', 'launch -p']
90115

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int foo() {
2+
return 123;
3+
}

0 commit comments

Comments
 (0)