Skip to content

Commit 70627af

Browse files
authored
[lldb] Synchronize access to m_statusline in the Debugger (#134759)
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 438f984 commit 70627af

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
@@ -752,6 +752,8 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
752752
IOHandlerStack m_io_handler_stack;
753753
std::recursive_mutex m_io_handler_synchronous_mutex;
754754

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

757759
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
}
@@ -1148,20 +1157,27 @@ void Debugger::SetErrorFile(FileSP file_sp) {
11481157
}
11491158

11501159
void Debugger::SaveInputTerminalState() {
1151-
if (m_statusline)
1152-
m_statusline->Disable();
1160+
{
1161+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
1162+
if (m_statusline)
1163+
m_statusline->Disable();
1164+
}
11531165
int fd = GetInputFile().GetDescriptor();
11541166
if (fd != File::kInvalidDescriptor)
11551167
m_terminal_state.Save(fd, true);
11561168
}
11571169

11581170
void Debugger::RestoreInputTerminalState() {
11591171
m_terminal_state.Restore();
1160-
if (m_statusline)
1161-
m_statusline->Enable();
1172+
{
1173+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
1174+
if (m_statusline)
1175+
m_statusline->Enable();
1176+
}
11621177
}
11631178

11641179
void Debugger::RedrawStatusline(bool update) {
1180+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
11651181
if (m_statusline)
11661182
m_statusline->Redraw(update);
11671183
}
@@ -2039,8 +2055,11 @@ lldb::thread_result_t Debugger::DefaultEventHandler() {
20392055
// are now listening to all required events so no events get missed
20402056
m_sync_broadcaster.BroadcastEvent(eBroadcastBitEventThreadIsListening);
20412057

2042-
if (!m_statusline && StatuslineSupported())
2043-
m_statusline.emplace(*this);
2058+
if (StatuslineSupported()) {
2059+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
2060+
if (!m_statusline)
2061+
m_statusline.emplace(*this);
2062+
}
20442063

20452064
bool done = false;
20462065
while (!done) {
@@ -2101,8 +2120,11 @@ lldb::thread_result_t Debugger::DefaultEventHandler() {
21012120
}
21022121
}
21032122

2104-
if (m_statusline)
2105-
m_statusline.reset();
2123+
{
2124+
std::lock_guard<std::mutex> guard(m_statusline_mutex);
2125+
if (m_statusline)
2126+
m_statusline.reset();
2127+
}
21062128

21072129
return {};
21082130
}

0 commit comments

Comments
 (0)