Skip to content

Commit 9fcc1c8

Browse files
committed
[lldb] Synchronize access to m_statusline in the Debugger
Eliminate the potential for a race between the main thread, the default event handler thread and the signal handling thread, when accessing the m_statusline member.
1 parent 6493345 commit 9fcc1c8

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

lldb/include/lldb/Core/Debugger.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,8 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
751751
IOHandlerStack m_io_handler_stack;
752752
std::recursive_mutex m_io_handler_synchronous_mutex;
753753

754+
/// Mutex protecting the m_statusline member.
755+
std::mutex m_statusline_mutex;
754756
std::optional<Statusline> m_statusline;
755757

756758
llvm::StringMap<std::weak_ptr<LogHandler>> m_stream_handlers;

lldb/source/Core/Debugger.cpp

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
251251
g_debugger_properties[ePropertyShowStatusline].name) {
252252
// Statusline setting changed. If we have a statusline instance, update it
253253
// now. Otherwise it will get created in the default event handler.
254+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
254255
if (StatuslineSupported())
255256
m_statusline.emplace(*this);
256257
else
@@ -391,8 +392,12 @@ bool Debugger::SetTerminalWidth(uint64_t term_width) {
391392

392393
if (auto handler_sp = m_io_handler_stack.Top())
393394
handler_sp->TerminalSizeChanged();
394-
if (m_statusline)
395-
m_statusline->TerminalSizeChanged();
395+
396+
{
397+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
398+
if (m_statusline)
399+
m_statusline->TerminalSizeChanged();
400+
}
396401

397402
return success;
398403
}
@@ -409,8 +414,12 @@ bool Debugger::SetTerminalHeight(uint64_t term_height) {
409414

410415
if (auto handler_sp = m_io_handler_stack.Top())
411416
handler_sp->TerminalSizeChanged();
412-
if (m_statusline)
413-
m_statusline->TerminalSizeChanged();
417+
418+
{
419+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
420+
if (m_statusline)
421+
m_statusline->TerminalSizeChanged();
422+
}
414423

415424
return success;
416425
}
@@ -1141,20 +1150,27 @@ void Debugger::SetErrorFile(FileSP file_sp) {
11411150
}
11421151

11431152
void Debugger::SaveInputTerminalState() {
1144-
if (m_statusline)
1145-
m_statusline->Disable();
1153+
{
1154+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
1155+
if (m_statusline)
1156+
m_statusline->Disable();
1157+
}
11461158
int fd = GetInputFile().GetDescriptor();
11471159
if (fd != File::kInvalidDescriptor)
11481160
m_terminal_state.Save(fd, true);
11491161
}
11501162

11511163
void Debugger::RestoreInputTerminalState() {
11521164
m_terminal_state.Restore();
1153-
if (m_statusline)
1154-
m_statusline->Enable();
1165+
{
1166+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
1167+
if (m_statusline)
1168+
m_statusline->Enable();
1169+
}
11551170
}
11561171

11571172
void Debugger::RedrawStatusline(bool update) {
1173+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
11581174
if (m_statusline)
11591175
m_statusline->Redraw(update);
11601176
}
@@ -2032,8 +2048,11 @@ lldb::thread_result_t Debugger::DefaultEventHandler() {
20322048
// are now listening to all required events so no events get missed
20332049
m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening);
20342050

2035-
if (!m_statusline && StatuslineSupported())
2036-
m_statusline.emplace(*this);
2051+
if (StatuslineSupported()) {
2052+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
2053+
if (!m_statusline)
2054+
m_statusline.emplace(*this);
2055+
}
20372056

20382057
bool done = false;
20392058
while (!done) {
@@ -2094,8 +2113,11 @@ lldb::thread_result_t Debugger::DefaultEventHandler() {
20942113
}
20952114
}
20962115

2097-
if (m_statusline)
2098-
m_statusline.reset();
2116+
{
2117+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
2118+
if (m_statusline)
2119+
m_statusline.reset();
2120+
}
20992121

21002122
return {};
21012123
}

0 commit comments

Comments
 (0)