Skip to content

Commit de4ee37

Browse files
Merge pull request #9945 from felipepiovezan/felipe/cherrypick_stop_reason_fixes
[cherry-pick] Fixes for StopReason propagation in the presence of OS plugins
2 parents 6447c07 + 4913072 commit de4ee37

File tree

9 files changed

+122
-21
lines changed

9 files changed

+122
-21
lines changed

lldb/include/lldb/Target/Thread.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,26 @@ class Thread : public std::enable_shared_from_this<Thread>,
465465

466466
virtual void ClearStackFrames();
467467

468+
/// Sets the thread that is backed by this thread.
469+
/// If backed_thread.GetBackedThread() is null, this method also calls
470+
/// backed_thread.SetBackingThread(this).
471+
/// If backed_thread.GetBackedThread() is non-null, asserts that it is equal
472+
/// to `this`.
473+
void SetBackedThread(Thread &backed_thread) {
474+
m_backed_thread = backed_thread.shared_from_this();
475+
476+
// Ensure the bidrectional relationship is preserved.
477+
Thread *backing_thread = backed_thread.GetBackingThread().get();
478+
assert(backing_thread == nullptr || backing_thread == this);
479+
if (backing_thread == nullptr)
480+
backed_thread.SetBackingThread(shared_from_this());
481+
}
482+
483+
void ClearBackedThread() { m_backed_thread.reset(); }
484+
485+
/// Returns the thread that is backed by this thread, if any.
486+
lldb::ThreadSP GetBackedThread() const { return m_backed_thread.lock(); }
487+
468488
virtual bool SetBackingThread(const lldb::ThreadSP &thread_sp) {
469489
return false;
470490
}
@@ -1376,6 +1396,9 @@ class Thread : public std::enable_shared_from_this<Thread>,
13761396
LazyBool m_override_should_notify;
13771397
mutable std::unique_ptr<ThreadPlanStack> m_null_plan_stack_up;
13781398

1399+
/// The Thread backed by this thread, if any.
1400+
lldb::ThreadWP m_backed_thread;
1401+
13791402
private:
13801403
bool m_extended_info_fetched; // Have we tried to retrieve the m_extended_info
13811404
// for this thread?

lldb/include/lldb/Target/ThreadList.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ class ThreadList : public ThreadCollection {
101101

102102
lldb::ThreadSP GetThreadSPForThreadPtr(Thread *thread_ptr);
103103

104-
lldb::ThreadSP GetBackingThread(const lldb::ThreadSP &real_thread);
105-
106104
bool ShouldStop(Event *event_ptr);
107105

108106
Vote ShouldReportStop(Event *event_ptr);

lldb/source/Breakpoint/BreakpointSite.cpp

Lines changed: 3 additions & 0 deletions
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/Utility/ThreadMemory.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,17 @@ class ThreadMemory : public lldb_private::Thread {
7272

7373
void ClearStackFrames() override;
7474

75-
void ClearBackingThread() override { m_backing_thread_sp.reset(); }
75+
void ClearBackingThread() override {
76+
if (m_backing_thread_sp)
77+
m_backing_thread_sp->ClearBackedThread();
78+
m_backing_thread_sp.reset();
79+
}
7680

7781
bool SetBackingThread(const lldb::ThreadSP &thread_sp) override {
7882
// printf ("Thread 0x%llx is being backed by thread 0x%llx\n", GetID(),
7983
// thread_sp->GetID());
8084
m_backing_thread_sp = thread_sp;
85+
thread_sp->SetBackedThread(*this);
8186
return (bool)thread_sp;
8287
}
8388

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

Lines changed: 0 additions & 4 deletions
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 = m_thread_list.GetBackingThread(thread_sp))
1732-
thread_sp = memory_thread_sp;
17331729

17341730
if (exc_type != 0) {
17351731
const size_t exc_data_size = exc_data.size();

lldb/source/Target/ThreadList.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,6 @@ ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) {
191191
return thread_sp;
192192
}
193193

194-
ThreadSP ThreadList::GetBackingThread(const ThreadSP &real_thread) {
195-
std::lock_guard<std::recursive_mutex> guard(GetMutex());
196-
197-
ThreadSP thread_sp;
198-
const uint32_t num_threads = m_threads.size();
199-
for (uint32_t idx = 0; idx < num_threads; ++idx) {
200-
if (m_threads[idx]->GetBackingThread() == real_thread) {
201-
thread_sp = m_threads[idx];
202-
break;
203-
}
204-
}
205-
return thread_sp;
206-
}
207-
208194
ThreadSP ThreadList::FindThreadByIndexID(uint32_t index_id, bool can_update) {
209195
std::lock_guard<std::recursive_mutex> guard(GetMutex());
210196

Lines changed: 21 additions & 0 deletions
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+
)
Lines changed: 59 additions & 0 deletions
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+
};
Lines changed: 10 additions & 0 deletions
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)