https://github.com/medismailben updated https://github.com/llvm/llvm-project/pull/237517
>From 8d4c1f2b0b46bdfeef865e313175e2ecdb0e09c5 Mon Sep 17 00:00:00 2001 From: Med Ismail Bennani <[email protected]> Date: Mon, 5 Oct 2026 17:45:49 -0700 Subject: [PATCH] [lldb] Show a process picker in process attach without a pid This patch makes `process attach` show an `IOHandlerPicker` listing the processes on the selected platform, with their pid, name, and architecture, when it is run without a pid or process name and the target has no executable to fall back on. `platform process attach` does the same whenever no pid or name is given. Scripted process attaches (`-C`) are left unchanged. When the input is not an interactive terminal, both commands skip the picker and fail the same way they did before, and canceling the picker fails with "no process selected". Assisted-by: Claude Signed-off-by: Med Ismail Bennani <[email protected]> --- .../source/Commands/CommandObjectPlatform.cpp | 13 ++++++ lldb/source/Commands/CommandObjectProcess.cpp | 14 +++++++ .../Commands/CommandOptionsProcessAttach.cpp | 42 +++++++++++++++++++ .../Commands/CommandOptionsProcessAttach.h | 9 ++++ .../iohandler/picker/TestIOHandlerPicker.py | 10 ++++- 5 files changed, 87 insertions(+), 1 deletion(-) diff --git a/lldb/source/Commands/CommandObjectPlatform.cpp b/lldb/source/Commands/CommandObjectPlatform.cpp index b040df1868961..326df3f3efac2 100644 --- a/lldb/source/Commands/CommandObjectPlatform.cpp +++ b/lldb/source/Commands/CommandObjectPlatform.cpp @@ -1548,6 +1548,19 @@ class CommandObjectPlatformProcessAttach : public CommandObjectParsed { m_options.attach_info.SetScriptedMetadata(metadata_sp); } + // Without a pid or name, let the user pick a process interactively. + if (!m_options.attach_info.ProcessInfoSpecified() && + m_class_options.GetName().empty() && + GetDebugger().GetInputFile().GetIsRealTerminal()) { + llvm::Expected<lldb::pid_t> pid = + PickProcessToAttach(GetDebugger(), *platform_sp, result); + if (!pid) { + result.SetError(pid.takeError()); + return; + } + m_options.attach_info.SetProcessID(*pid); + } + Status err; ProcessSP remote_process_sp = platform_sp->Attach( m_options.attach_info, GetDebugger(), nullptr, err); diff --git a/lldb/source/Commands/CommandObjectProcess.cpp b/lldb/source/Commands/CommandObjectProcess.cpp index 3add186a2bd2d..ccd48e1eeaad9 100644 --- a/lldb/source/Commands/CommandObjectProcess.cpp +++ b/lldb/source/Commands/CommandObjectProcess.cpp @@ -349,6 +349,20 @@ class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach { m_options.attach_info.SetScriptedMetadata(metadata_sp); } + // Without a pid, name, or target executable to fall back on, let the user + // pick a process interactively. + if (!m_options.attach_info.ProcessInfoSpecified() && + !target->GetExecutableModule() && m_class_options.GetName().empty() && + GetDebugger().GetInputFile().GetIsRealTerminal()) { + llvm::Expected<lldb::pid_t> pid = + PickProcessToAttach(GetDebugger(), *platform_sp, result); + if (!pid) { + result.SetError(pid.takeError()); + return; + } + m_options.attach_info.SetProcessID(*pid); + } + // Record the old executable module, we want to issue a warning if the // process of attaching changed the current executable (like somebody said // "file foo" then attached to a PID whose executable was bar.) diff --git a/lldb/source/Commands/CommandOptionsProcessAttach.cpp b/lldb/source/Commands/CommandOptionsProcessAttach.cpp index 2a198627e23a4..5cf51c18d4720 100644 --- a/lldb/source/Commands/CommandOptionsProcessAttach.cpp +++ b/lldb/source/Commands/CommandOptionsProcessAttach.cpp @@ -8,12 +8,15 @@ #include "CommandOptionsProcessAttach.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Core/IOHandlerPicker.h" #include "lldb/Host/FileSystem.h" #include "lldb/Host/HostInfo.h" #include "lldb/Host/OptionParser.h" #include "lldb/Interpreter/CommandCompletions.h" #include "lldb/Interpreter/CommandObject.h" #include "lldb/Interpreter/CommandOptionArgumentTable.h" +#include "lldb/Interpreter/CommandReturnObject.h" #include "lldb/Interpreter/OptionArgParser.h" #include "lldb/Target/ExecutionContext.h" #include "lldb/Target/Platform.h" @@ -74,3 +77,42 @@ Status CommandOptionsProcessAttach::SetOptionValue( llvm::ArrayRef<OptionDefinition> CommandOptionsProcessAttach::GetDefinitions() { return llvm::ArrayRef(g_process_attach_options); } + +llvm::Expected<lldb::pid_t> +lldb_private::PickProcessToAttach(Debugger &debugger, Platform &platform, + CommandReturnObject &result) { + ProcessInstanceInfoMatch match_info; + ProcessInstanceInfoList proc_infos; + platform.FindProcesses(match_info, proc_infos); + if (proc_infos.empty()) + return llvm::createStringError( + "no processes found on the current platform"); + + llvm::SmallVector<PickerColumnName> columns = {"PID", "NAME", "ARCH"}; + llvm::SmallVector<PickerItem, 0> items; + items.reserve(proc_infos.size()); + for (const ProcessInstanceInfo &proc : proc_infos) { + const ArchSpec &arch = proc.GetArchitecture(); + PickerItem item; + item.values = {std::to_string(proc.GetProcessID()), proc.GetName().str(), + arch.IsValid() ? arch.GetTriple().getArchName().str() : ""}; + items.push_back(std::move(item)); + } + + auto picker_sp = std::make_shared<IOHandlerPicker>( + debugger, "Select a process to attach to:", std::move(columns), + std::move(items), PickerMode::SingleSelect); + debugger.RunIOHandlerSync(picker_sp); + + const PickerResult &picked = picker_sp->GetResult(); + if (picked.was_canceled || picked.selected_indices.empty()) + return llvm::createStringError("no process selected"); + + const ProcessInstanceInfo &proc = proc_infos[picked.selected_indices.front()]; + lldb::pid_t pid = proc.GetProcessID(); + if (llvm::StringRef name = proc.GetName(); !name.empty()) + result.AppendMessageWithFormatv("Attaching to \"{0}\" ({1})...", name, pid); + else + result.AppendMessageWithFormatv("Attaching to process {0}...", pid); + return pid; +} diff --git a/lldb/source/Commands/CommandOptionsProcessAttach.h b/lldb/source/Commands/CommandOptionsProcessAttach.h index 02daf1aa769a5..a5abb35771a73 100644 --- a/lldb/source/Commands/CommandOptionsProcessAttach.h +++ b/lldb/source/Commands/CommandOptionsProcessAttach.h @@ -14,6 +14,8 @@ namespace lldb_private { +class CommandReturnObject; + // CommandOptionsProcessAttach class CommandOptionsProcessAttach : public lldb_private::OptionGroup { @@ -42,6 +44,13 @@ class CommandOptionsProcessAttach : public lldb_private::OptionGroup { lldb_private::ProcessAttachInfo attach_info; }; // CommandOptionsProcessAttach +/// Let the user pick one of \a platform's processes and return its pid, or +/// an error if there are none or the picker is canceled. On success, a +/// message naming the process is appended to \a result. +llvm::Expected<lldb::pid_t> PickProcessToAttach(Debugger &debugger, + Platform &platform, + CommandReturnObject &result); + } // namespace lldb_private #endif // LLDB_SOURCE_COMMANDS_COMMANDOPTIONSPROCESSATTACH_H diff --git a/lldb/test/API/iohandler/picker/TestIOHandlerPicker.py b/lldb/test/API/iohandler/picker/TestIOHandlerPicker.py index 8502bf4196579..c0301d1810528 100644 --- a/lldb/test/API/iohandler/picker/TestIOHandlerPicker.py +++ b/lldb/test/API/iohandler/picker/TestIOHandlerPicker.py @@ -19,7 +19,9 @@ class IOHandlerPickerTest(PExpectTest): escape_key = "\x1b" def start_picker(self, command, header, dimensions=(24, 120), setup=()): - if not self.dbg.GetCommandInterpreter().CommandExists("test-picker"): + if command.startswith("test-picker") and not ( + self.dbg.GetCommandInterpreter().CommandExists("test-picker") + ): self.skipTest("test-picker requires a build with assertions") self.launch(dimensions=dimensions) # launch() runs "settings clear --all", which resets the terminal size. @@ -220,3 +222,9 @@ def test_search_multi_keeps_hidden_selection(self): # stay pre-selected. self.child.send("/ch\r \r") self.finish("selected: 5", "selected: 4", "selected: 1") + + def test_process_attach_picker(self): + """`process attach` without a pid, name, or target opens the picker.""" + self.start_picker("process attach", "Select a process to attach to:") + self.child.send(self.escape_key) + self.finish("no process selected") _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
