Skip to content

Commit f03e3e0

Browse files
committed
[lldb-dap] Protect SetBreakpoint with the API mutex (llvm#134030) (backport)
Protect the various SetBreakpoint functions with the API mutex. This fixes a race condition between the breakpoint being created and the DAP label getting added. This was causing `TestDAP_breakpointEvents.py` to be flaky. Fixes llvm#131242.
1 parent 5250a07 commit f03e3e0

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

lldb/tools/lldb-dap/Breakpoint.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Breakpoint.h"
10+
#include "DAP.h"
1011
#include "JSONUtils.h"
1112
#include "lldb/API/SBAddress.h"
1213
#include "lldb/API/SBBreakpointLocation.h"
1314
#include "lldb/API/SBLineEntry.h"
15+
#include "lldb/API/SBMutex.h"
1416
#include "llvm/ADT/StringExtras.h"
1517
#include "llvm/Support/JSON.h"
1618
#include <cstddef>
1719
#include <cstdint>
20+
#include <mutex>
1821
#include <string>
1922

2023
using namespace lldb_dap;
@@ -72,6 +75,9 @@ void Breakpoint::CreateJsonObject(llvm::json::Object &object) {
7275
bool Breakpoint::MatchesName(const char *name) { return bp.MatchesName(name); }
7376

7477
void Breakpoint::SetBreakpoint() {
78+
lldb::SBMutex lock = dap.GetAPIMutex();
79+
std::lock_guard<lldb::SBMutex> guard(lock);
80+
7581
// See comments in BreakpointBase::GetBreakpointLabel() for details of why
7682
// we add a label to our breakpoints.
7783
bp.AddName(GetBreakpointLabel());

lldb/tools/lldb-dap/DAP.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "lldb/API/SBFile.h"
2525
#include "lldb/API/SBFormat.h"
2626
#include "lldb/API/SBFrame.h"
27+
#include "lldb/API/SBMutex.h"
2728
#include "lldb/API/SBTarget.h"
2829
#include "lldb/API/SBThread.h"
2930
#include "lldb/API/SBValue.h"
@@ -362,6 +363,8 @@ struct DAP {
362363

363364
InstructionBreakpoint *GetInstructionBPFromStopReason(lldb::SBThread &thread);
364365

366+
lldb::SBMutex GetAPIMutex() const { return target.GetAPIMutex(); }
367+
365368
private:
366369
// Send the JSON in "json_str" to the "out" stream. Correctly send the
367370
// "Content-Length:" field followed by the length, followed by the raw

lldb/tools/lldb-dap/ExceptionBreakpoint.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "ExceptionBreakpoint.h"
1010
#include "BreakpointBase.h"
1111
#include "DAP.h"
12+
#include "lldb/API/SBMutex.h"
1213
#include "lldb/API/SBTarget.h"
14+
#include <mutex>
1315

1416
namespace lldb_dap {
1517

@@ -26,6 +28,9 @@ void ExceptionBreakpoint::SetBreakpoint() {
2628
}
2729

2830
void ExceptionBreakpoint::ClearBreakpoint() {
31+
lldb::SBMutex lock = dap.GetAPIMutex();
32+
std::lock_guard<lldb::SBMutex> guard(lock);
33+
2934
if (!bp.IsValid())
3035
return;
3136
dap.target.BreakpointDelete(bp.GetID());

lldb/tools/lldb-dap/FunctionBreakpoint.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
#include "FunctionBreakpoint.h"
1010
#include "DAP.h"
1111
#include "JSONUtils.h"
12+
#include "lldb/API/SBMutex.h"
13+
#include <mutex>
1214

1315
namespace lldb_dap {
1416

1517
FunctionBreakpoint::FunctionBreakpoint(DAP &d, const llvm::json::Object &obj)
1618
: Breakpoint(d, obj), functionName(std::string(GetString(obj, "name"))) {}
1719

1820
void FunctionBreakpoint::SetBreakpoint() {
21+
lldb::SBMutex lock = dap.GetAPIMutex();
22+
std::lock_guard<lldb::SBMutex> guard(lock);
23+
1924
if (functionName.empty())
2025
return;
2126
bp = dap.target.BreakpointCreateByName(functionName.c_str());

lldb/tools/lldb-dap/InstructionBreakpoint.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
#include "DAP.h"
1212
#include "JSONUtils.h"
1313
#include "lldb/API/SBBreakpoint.h"
14+
#include "lldb/API/SBMutex.h"
1415
#include "lldb/API/SBTarget.h"
1516
#include "llvm/ADT/StringRef.h"
17+
#include <mutex>
1618

1719
namespace lldb_dap {
1820

@@ -27,6 +29,9 @@ InstructionBreakpoint::InstructionBreakpoint(DAP &d,
2729
}
2830

2931
void InstructionBreakpoint::SetBreakpoint() {
32+
lldb::SBMutex lock = dap.GetAPIMutex();
33+
std::lock_guard<lldb::SBMutex> guard(lock);
34+
3035
bp = dap.target.BreakpointCreateByAddress(instructionAddressReference);
3136
Breakpoint::SetBreakpoint();
3237
}

lldb/tools/lldb-dap/SourceBreakpoint.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
#include "lldb/API/SBBreakpoint.h"
1414
#include "lldb/API/SBFileSpecList.h"
1515
#include "lldb/API/SBFrame.h"
16+
#include "lldb/API/SBMutex.h"
1617
#include "lldb/API/SBTarget.h"
1718
#include "lldb/API/SBThread.h"
1819
#include "lldb/API/SBValue.h"
1920
#include "lldb/lldb-enumerations.h"
2021
#include <cassert>
2122
#include <cctype>
2223
#include <cstdlib>
24+
#include <mutex>
2325
#include <utility>
2426

2527
namespace lldb_dap {
@@ -31,6 +33,9 @@ SourceBreakpoint::SourceBreakpoint(DAP &dap, const llvm::json::Object &obj)
3133
}
3234

3335
void SourceBreakpoint::SetBreakpoint(const llvm::StringRef source_path) {
36+
lldb::SBMutex lock = dap.GetAPIMutex();
37+
std::lock_guard<lldb::SBMutex> guard(lock);
38+
3439
lldb::SBFileSpecList module_list;
3540
bp = dap.target.BreakpointCreateByLocation(source_path.str().c_str(), line,
3641
column, 0, module_list);

0 commit comments

Comments
 (0)