Skip to content

Commit 79e804b

Browse files
[lldb] Improve isolation between Process plugins and OS plugins (#125302)
Generally speaking, process plugins (e.g. ProcessGDBRemote) should not be aware of OS plugin threads. However, ProcessGDBRemote attempts to check for the existence of OS threads when calculating stop info. When OS threads are present, it sets the stop info directly on the OS plugin thread and leaves the ThreadGDBRemote without a StopInfo. This is problematic for a few reasons: 1. No other process plugins do this, as they shouldn't. They should set the stop info for their own process threads, and let the abstractions built on top propagate StopInfos. 2. This conflicts with the expectations of ThreadMemory, which checks for the backing threads's info, and then attempts to propagate it (in the future, it should probably ask the plugin itself too...). We see this happening in the code below. The `if` condition will not trigger, because `backing_stop_info_sp` will be null (remember, ProcessGDB remote is ignoring its own threads), and then this method returns false. ``` bool ThreadMemory::CalculateStopInfo() { ... lldb::StopInfoSP backing_stop_info_sp( m_backing_thread_sp->GetPrivateStopInfo()); if (backing_stop_info_sp && backing_stop_info_sp->IsValidForOperatingSystemThread(*this)) { backing_stop_info_sp->SetThread(shared_from_this()); ``` ``` Thread::GetPrivateStopInfo ... if (!CalculateStopInfo()) SetStopInfo(StopInfoSP()); ``` To solve this, we change ProcessGDB remote so that it does the principled thing: it now only sets the stop info of its own threads. This change by itself breaks the tests TestPythonOSPlugin.py and TestOSPluginStepping.py and probably explains why ProcessGDB had originally "violated" this isolation of layers. To make this work, BreakpointSites must be aware of BackingThreads when answering the question: "Is this breakpoint valid for this thread?". Why? Breakpoints are created on top of the OS threads (that's what the user sees), but breakpoints are hit by process threads. In the presence of OS threads, a TID-specific breakpoint is valid for a process thread if it is backing an OS thread with that TID.
1 parent 1abe7e8 commit 79e804b

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

lldb/source/Breakpoint/BreakpointSite.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "lldb/Breakpoint/Breakpoint.h"
1414
#include "lldb/Breakpoint/BreakpointLocation.h"
15+
#include "lldb/Target/Thread.h"
1516
#include "lldb/Utility/Stream.h"
1617

1718
using namespace lldb;
@@ -161,6 +162,8 @@ BreakpointLocationSP BreakpointSite::GetConstituentAtIndex(size_t index) {
161162

162163
bool BreakpointSite::ValidForThisThread(Thread &thread) {
163164
std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
165+
if (ThreadSP backed_thread = thread.GetBackedThread())
166+
return m_constituents.ValidForThisThread(*backed_thread);
164167
return m_constituents.ValidForThisThread(thread);
165168
}
166169

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -1726,10 +1726,6 @@ ThreadSP ProcessGDBRemote::SetThreadStopInfo(
17261726

17271727
if (!thread_sp->StopInfoIsUpToDate()) {
17281728
thread_sp->SetStopInfo(StopInfoSP());
1729-
// If there's a memory thread backed by this thread, we need to use it to
1730-
// calculate StopInfo.
1731-
if (ThreadSP memory_thread_sp = thread_sp->GetBackedThread())
1732-
thread_sp = memory_thread_sp;
17331729

17341730
if (exc_type != 0) {
17351731
// For thread plan async interrupt, creating stop info on the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
add_lldb_unittest(ProcessGdbRemoteTests
2+
GDBRemoteClientBaseTest.cpp
3+
GDBRemoteCommunicationClientTest.cpp
4+
GDBRemoteCommunicationServerLLGSTest.cpp
5+
GDBRemoteCommunicationServerTest.cpp
6+
GDBRemoteCommunicationTest.cpp
7+
GDBRemoteTestUtils.cpp
8+
9+
LINK_LIBS
10+
LLVMTestingSupport
11+
lldbCore
12+
lldbHost
13+
lldbInterpreter
14+
lldbPluginProcessUtility
15+
lldbSymbol
16+
lldbTarget
17+
lldbValueObject
18+
19+
LINK_COMPONENTS
20+
Support
21+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===-- OperatingSystemPlugin.h ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lldb/Core/PluginManager.h"
10+
#include "lldb/Target/OperatingSystem.h"
11+
#include "lldb/Target/Thread.h"
12+
#include "lldb/Target/ThreadList.h"
13+
14+
/// An operating system plugin that does nothing: simply keeps the thread lists
15+
/// as they are.
16+
class OperatingSystemIdentityMap : public lldb_private::OperatingSystem {
17+
public:
18+
OperatingSystemIdentityMap(lldb_private::Process *process)
19+
: OperatingSystem(process) {}
20+
21+
static OperatingSystem *CreateInstance(lldb_private::Process *process,
22+
bool force) {
23+
return new OperatingSystemIdentityMap(process);
24+
}
25+
static llvm::StringRef GetPluginNameStatic() { return "identity map"; }
26+
static llvm::StringRef GetPluginDescriptionStatic() { return ""; }
27+
28+
static void Initialize() {
29+
lldb_private::PluginManager::RegisterPlugin(GetPluginNameStatic(),
30+
GetPluginDescriptionStatic(),
31+
CreateInstance, nullptr);
32+
}
33+
static void Terminate() {
34+
lldb_private::PluginManager::UnregisterPlugin(CreateInstance);
35+
}
36+
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
37+
38+
// Simply adds the threads from real_thread_list into new_thread_list.
39+
bool UpdateThreadList(lldb_private::ThreadList &old_thread_list,
40+
lldb_private::ThreadList &real_thread_list,
41+
lldb_private::ThreadList &new_thread_list) override {
42+
for (const auto &real_thread : real_thread_list.Threads())
43+
new_thread_list.AddThread(real_thread);
44+
return true;
45+
}
46+
47+
void ThreadWasSelected(lldb_private::Thread *thread) override {}
48+
49+
lldb::RegisterContextSP
50+
CreateRegisterContextForThread(lldb_private::Thread *thread,
51+
lldb::addr_t reg_data_addr) override {
52+
return thread->GetRegisterContext();
53+
}
54+
55+
lldb::StopInfoSP
56+
CreateThreadStopReason(lldb_private::Thread *thread) override {
57+
return thread->GetStopInfo();
58+
}
59+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//===-- OperatingSystemPlugin.h ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "OperatingSystemPlugin.h"
10+
LLDB_PLUGIN_DEFINE(OperatingSystemIdentityMap)

0 commit comments

Comments
 (0)