Skip to content

Commit e64cc75

Browse files
committed
[lldb-server/linux] Use waitpid(-1) to collect inferior events
This is a follow-up to D116372, which had a rather unfortunate side effect of making the processing of a single SIGCHLD quadratic in the number of threads -- which does not matter for simple applications, but can get really bad for applications with thousands of threads. This patch fixes the problem by implementing the other possibility mentioned in the first patch -- doing waitpid(-1) centrally and then routing the events to the correct process instance. The "uncollected" threads are held in the process factory class -- which I've renamed to Manager for this purpose, as it now does more than creating processes. Differential Revision: https://reviews.llvm.org/D146977
1 parent 44f0c91 commit e64cc75

13 files changed

+217
-184
lines changed

lldb/include/lldb/Host/common/NativeProcessProtocol.h

+14-7
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ class NativeProcessProtocol {
256256
virtual Status GetFileLoadAddress(const llvm::StringRef &file_name,
257257
lldb::addr_t &load_addr) = 0;
258258

259-
/// Extension flag constants, returned by Factory::GetSupportedExtensions()
259+
/// Extension flag constants, returned by Manager::GetSupportedExtensions()
260260
/// and passed to SetEnabledExtension()
261261
enum class Extension {
262262
multiprocess = (1u << 0),
@@ -272,9 +272,14 @@ class NativeProcessProtocol {
272272
LLVM_MARK_AS_BITMASK_ENUM(siginfo_read)
273273
};
274274

275-
class Factory {
275+
class Manager {
276276
public:
277-
virtual ~Factory();
277+
Manager(MainLoop &mainloop) : m_mainloop(mainloop) {}
278+
Manager(const Manager &) = delete;
279+
Manager &operator=(const Manager &) = delete;
280+
281+
virtual ~Manager();
282+
278283
/// Launch a process for debugging.
279284
///
280285
/// \param[in] launch_info
@@ -294,8 +299,8 @@ class NativeProcessProtocol {
294299
/// A NativeProcessProtocol shared pointer if the operation succeeded or
295300
/// an error object if it failed.
296301
virtual llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
297-
Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
298-
MainLoop &mainloop) const = 0;
302+
Launch(ProcessLaunchInfo &launch_info,
303+
NativeDelegate &native_delegate) = 0;
299304

300305
/// Attach to an existing process.
301306
///
@@ -316,14 +321,16 @@ class NativeProcessProtocol {
316321
/// A NativeProcessProtocol shared pointer if the operation succeeded or
317322
/// an error object if it failed.
318323
virtual llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
319-
Attach(lldb::pid_t pid, NativeDelegate &native_delegate,
320-
MainLoop &mainloop) const = 0;
324+
Attach(lldb::pid_t pid, NativeDelegate &native_delegate) = 0;
321325

322326
/// Get the bitmask of extensions supported by this process plugin.
323327
///
324328
/// \return
325329
/// A NativeProcessProtocol::Extension bitmask.
326330
virtual Extension GetSupportedExtensions() const { return {}; }
331+
332+
protected:
333+
MainLoop &m_mainloop;
327334
};
328335

329336
/// Notify tracers that the target process will resume

lldb/source/Host/common/NativeProcessProtocol.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -759,4 +759,4 @@ void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
759759
// Default implementation does nothing.
760760
}
761761

762-
NativeProcessProtocol::Factory::~Factory() = default;
762+
NativeProcessProtocol::Manager::~Manager() = default;

lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ static Status EnsureFDFlags(int fd, int flags) {
5151
// Public Static Methods
5252

5353
llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
54-
NativeProcessFreeBSD::Factory::Launch(ProcessLaunchInfo &launch_info,
55-
NativeDelegate &native_delegate,
56-
MainLoop &mainloop) const {
54+
NativeProcessFreeBSD::Manager::Launch(ProcessLaunchInfo &launch_info,
55+
NativeDelegate &native_delegate) {
5756
Log *log = GetLog(POSIXLog::Process);
5857

5958
Status status;
@@ -91,7 +90,7 @@ NativeProcessFreeBSD::Factory::Launch(ProcessLaunchInfo &launch_info,
9190

9291
std::unique_ptr<NativeProcessFreeBSD> process_up(new NativeProcessFreeBSD(
9392
pid, launch_info.GetPTY().ReleasePrimaryFileDescriptor(), native_delegate,
94-
Info.GetArchitecture(), mainloop));
93+
Info.GetArchitecture(), m_mainloop));
9594

9695
status = process_up->SetupTrace();
9796
if (status.Fail())
@@ -105,9 +104,8 @@ NativeProcessFreeBSD::Factory::Launch(ProcessLaunchInfo &launch_info,
105104
}
106105

107106
llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
108-
NativeProcessFreeBSD::Factory::Attach(
109-
lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
110-
MainLoop &mainloop) const {
107+
NativeProcessFreeBSD::Manager::Attach(
108+
lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate) {
111109
Log *log = GetLog(POSIXLog::Process);
112110
LLDB_LOG(log, "pid = {0:x}", pid);
113111

@@ -119,7 +117,7 @@ NativeProcessFreeBSD::Factory::Attach(
119117
}
120118

121119
std::unique_ptr<NativeProcessFreeBSD> process_up(new NativeProcessFreeBSD(
122-
pid, -1, native_delegate, Info.GetArchitecture(), mainloop));
120+
pid, -1, native_delegate, Info.GetArchitecture(), m_mainloop));
123121

124122
Status status = process_up->Attach();
125123
if (!status.Success())
@@ -129,7 +127,7 @@ NativeProcessFreeBSD::Factory::Attach(
129127
}
130128

131129
NativeProcessFreeBSD::Extension
132-
NativeProcessFreeBSD::Factory::GetSupportedExtensions() const {
130+
NativeProcessFreeBSD::Manager::GetSupportedExtensions() const {
133131
return
134132
#if defined(PT_COREDUMP)
135133
Extension::savecore |

lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ namespace process_freebsd {
3030
class NativeProcessFreeBSD : public NativeProcessELF,
3131
private NativeProcessSoftwareSingleStep {
3232
public:
33-
class Factory : public NativeProcessProtocol::Factory {
33+
class Manager : public NativeProcessProtocol::Manager {
3434
public:
35+
using NativeProcessProtocol::Manager::Manager;
36+
3537
llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
36-
Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
37-
MainLoop &mainloop) const override;
38+
Launch(ProcessLaunchInfo &launch_info,
39+
NativeDelegate &native_delegate) override;
3840

3941
llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
40-
Attach(lldb::pid_t pid, NativeDelegate &native_delegate,
41-
MainLoop &mainloop) const override;
42+
Attach(lldb::pid_t pid, NativeDelegate &native_delegate) override;
4243

4344
Extension GetSupportedExtensions() const override;
4445
};

0 commit comments

Comments
 (0)