Skip to content

[lldb-dap] Correctly report breakpoints as resolved on macOS (#129589) #10459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 1 addition & 79 deletions lldb/bindings/headers.swig
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,5 @@
/* The liblldb header files to be included. */
%{
#include "lldb/lldb-public.h"
#include "lldb/API/SBAddress.h"
#include "lldb/API/SBAddressRange.h"
#include "lldb/API/SBAddressRangeList.h"
#include "lldb/API/SBAttachInfo.h"
#include "lldb/API/SBBlock.h"
#include "lldb/API/SBBreakpoint.h"
#include "lldb/API/SBBreakpointLocation.h"
#include "lldb/API/SBBreakpointName.h"
#include "lldb/API/SBBroadcaster.h"
#include "lldb/API/SBCommandInterpreter.h"
#include "lldb/API/SBCommandInterpreterRunOptions.h"
#include "lldb/API/SBCommandReturnObject.h"
#include "lldb/API/SBCommunication.h"
#include "lldb/API/SBCompileUnit.h"
#include "lldb/API/SBSaveCoreOptions.h"
#include "lldb/API/SBData.h"
#include "lldb/API/SBDebugger.h"
#include "lldb/API/SBDeclaration.h"
#include "lldb/API/SBEnvironment.h"
#include "lldb/API/SBError.h"
#include "lldb/API/SBEvent.h"
#include "lldb/API/SBExecutionContext.h"
#include "lldb/API/SBExpressionOptions.h"
#include "lldb/API/SBFile.h"
#include "lldb/API/SBFileSpec.h"
#include "lldb/API/SBFileSpecList.h"
#include "lldb/API/SBFormat.h"
#include "lldb/API/SBFrame.h"
#include "lldb/API/SBFunction.h"
#include "lldb/API/SBHostOS.h"
#include "lldb/API/SBInstruction.h"
#include "lldb/API/SBInstructionList.h"
#include "lldb/API/SBLanguages.h"
#include "lldb/API/SBLanguageRuntime.h"
#include "lldb/API/SBLaunchInfo.h"
#include "lldb/API/SBLineEntry.h"
#include "lldb/API/SBListener.h"
#include "lldb/API/SBMemoryRegionInfo.h"
#include "lldb/API/SBMemoryRegionInfoList.h"
#include "lldb/API/SBModule.h"
#include "lldb/API/SBModuleSpec.h"
#include "lldb/API/SBPlatform.h"
#include "lldb/API/SBProcess.h"
#include "lldb/API/SBProcessInfo.h"
#include "lldb/API/SBProcessInfoList.h"
#include "lldb/API/SBProgress.h"
#include "lldb/API/SBQueue.h"
#include "lldb/API/SBQueueItem.h"
#include "lldb/API/SBReproducer.h"
#include "lldb/API/SBScriptObject.h"
#include "lldb/API/SBSection.h"
#include "lldb/API/SBSourceManager.h"
#include "lldb/API/SBStatisticsOptions.h"
#include "lldb/API/SBStream.h"
#include "lldb/API/SBStringList.h"
#include "lldb/API/SBStructuredData.h"
#include "lldb/API/SBSymbol.h"
#include "lldb/API/SBSymbolContext.h"
#include "lldb/API/SBSymbolContextList.h"
#include "lldb/API/SBTarget.h"
#include "lldb/API/SBThread.h"
#include "lldb/API/SBThreadCollection.h"
#include "lldb/API/SBThreadPlan.h"
#include "lldb/API/SBTrace.h"
#include "lldb/API/SBTraceCursor.h"
#include "lldb/API/SBType.h"
#include "lldb/API/SBTypeCategory.h"
#include "lldb/API/SBTypeEnumMember.h"
#include "lldb/API/SBTypeFilter.h"
#include "lldb/API/SBTypeFormat.h"
#include "lldb/API/SBTypeNameSpecifier.h"
#include "lldb/API/SBTypeSummary.h"
#include "lldb/API/SBTypeSynthetic.h"
#include "lldb/API/SBUnixSignals.h"
#include "lldb/API/SBValue.h"
#include "lldb/API/SBValueList.h"
#include "lldb/API/SBVariablesOptions.h"
#include "lldb/API/SBWatchpoint.h"
#include "lldb/API/SBWatchpointOptions.h"
#include "lldb/API/LLDB.h"
%}
12 changes: 12 additions & 0 deletions lldb/bindings/interface/SBMutexExtensions.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%extend lldb::SBMutex {
#ifdef SWIGPYTHON
%pythoncode %{
def __enter__(self):
self.lock()
return self

def __exit__(self, exc_type, exc_value, traceback):
self.unlock()
%}
#endif
}
4 changes: 3 additions & 1 deletion lldb/bindings/interfaces.swig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
%include "./interface/SBMemoryRegionInfoListDocstrings.i"
%include "./interface/SBModuleDocstrings.i"
%include "./interface/SBModuleSpecDocstrings.i"
%include "./interface/SBMutexExtensions.i"
%include "./interface/SBPlatformDocstrings.i"
%include "./interface/SBProcessDocstrings.i"
%include "./interface/SBProcessInfoDocstrings.i"
Expand Down Expand Up @@ -121,15 +122,16 @@
%include "lldb/API/SBHostOS.h"
%include "lldb/API/SBInstruction.h"
%include "lldb/API/SBInstructionList.h"
%include "lldb/API/SBLanguages.h"
%include "lldb/API/SBLanguageRuntime.h"
%include "lldb/API/SBLanguages.h"
%include "lldb/API/SBLaunchInfo.h"
%include "lldb/API/SBLineEntry.h"
%include "lldb/API/SBListener.h"
%include "lldb/API/SBMemoryRegionInfo.h"
%include "lldb/API/SBMemoryRegionInfoList.h"
%include "lldb/API/SBModule.h"
%include "lldb/API/SBModuleSpec.h"
%include "lldb/API/SBMutex.h"
%include "lldb/API/SBPlatform.h"
%include "lldb/API/SBProcess.h"
%include "lldb/API/SBProcessInfo.h"
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/LLDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "lldb/API/SBMemoryRegionInfoList.h"
#include "lldb/API/SBModule.h"
#include "lldb/API/SBModuleSpec.h"
#include "lldb/API/SBMutex.h"
#include "lldb/API/SBPlatform.h"
#include "lldb/API/SBProcess.h"
#include "lldb/API/SBProcessInfo.h"
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class LLDB_API SBMemoryRegionInfoList;
class LLDB_API SBModule;
class LLDB_API SBModuleSpec;
class LLDB_API SBModuleSpecList;
class LLDB_API SBMutex;
class LLDB_API SBPlatform;
class LLDB_API SBPlatformConnectOptions;
class LLDB_API SBPlatformShellCommand;
Expand Down
45 changes: 45 additions & 0 deletions lldb/include/lldb/API/SBMutex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===-- SBMutex.h ---------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_API_SBMUTEX_H
#define LLDB_API_SBMUTEX_H

#include "lldb/API/SBDefines.h"
#include "lldb/lldb-forward.h"
#include <mutex>

namespace lldb {

class LLDB_API SBMutex {
public:
SBMutex();
SBMutex(const SBMutex &rhs);
const SBMutex &operator=(const SBMutex &rhs);
~SBMutex();

/// Returns true if this lock has ownership of the underlying mutex.
bool IsValid() const;

/// Blocking operation that takes ownership of this lock.
void lock() const;

/// Releases ownership of this lock.
void unlock() const;

private:
// Private constructor used by SBTarget to create the Target API mutex.
// Requires a friend declaration.
SBMutex(lldb::TargetSP target_sp);
friend class SBTarget;

std::shared_ptr<std::recursive_mutex> m_opaque_sp;
};

} // namespace lldb

#endif
4 changes: 3 additions & 1 deletion lldb/include/lldb/API/SBTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class LLDB_API SBTarget {
uint32_t GetAddressByteSize();

const char *GetTriple();

const char *GetABIName();

const char *GetLabel() const;
Expand Down Expand Up @@ -957,6 +957,8 @@ class LLDB_API SBTarget {
/// An error if a Trace already exists or the trace couldn't be created.
lldb::SBTrace CreateTrace(SBError &error);

lldb::SBMutex GetAPIMutex() const;

protected:
friend class SBAddress;
friend class SBAddressRange;
Expand Down
1 change: 1 addition & 0 deletions lldb/source/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ add_lldb_library(liblldb SHARED ${option_framework}
SBMemoryRegionInfoList.cpp
SBModule.cpp
SBModuleSpec.cpp
SBMutex.cpp
SBPlatform.cpp
SBProgress.cpp
SBProcess.cpp
Expand Down
60 changes: 60 additions & 0 deletions lldb/source/API/SBMutex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//===-- SBMutex.cpp -------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/API/SBMutex.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/Instrumentation.h"
#include "lldb/lldb-forward.h"
#include <memory>
#include <mutex>

using namespace lldb;
using namespace lldb_private;

SBMutex::SBMutex() : m_opaque_sp(std::make_shared<std::recursive_mutex>()) {
LLDB_INSTRUMENT_VA(this);
}

SBMutex::SBMutex(const SBMutex &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
LLDB_INSTRUMENT_VA(this);
}

const SBMutex &SBMutex::operator=(const SBMutex &rhs) {
LLDB_INSTRUMENT_VA(this);

m_opaque_sp = rhs.m_opaque_sp;
return *this;
}

SBMutex::SBMutex(lldb::TargetSP target_sp)
: m_opaque_sp(std::shared_ptr<std::recursive_mutex>(
target_sp, &target_sp->GetAPIMutex())) {
LLDB_INSTRUMENT_VA(this, target_sp);
}

SBMutex::~SBMutex() { LLDB_INSTRUMENT_VA(this); }

bool SBMutex::IsValid() const {
LLDB_INSTRUMENT_VA(this);

return static_cast<bool>(m_opaque_sp);
}

void SBMutex::lock() const {
LLDB_INSTRUMENT_VA(this);

if (m_opaque_sp)
m_opaque_sp->lock();
}

void SBMutex::unlock() const {
LLDB_INSTRUMENT_VA(this);

if (m_opaque_sp)
m_opaque_sp->unlock();
}
16 changes: 12 additions & 4 deletions lldb/source/API/SBTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
//===----------------------------------------------------------------------===//

#include "lldb/API/SBTarget.h"
#include "lldb/Utility/Instrumentation.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/lldb-public.h"

#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "lldb/API/SBBreakpoint.h"
#include "lldb/API/SBDebugger.h"
Expand All @@ -21,6 +17,7 @@
#include "lldb/API/SBListener.h"
#include "lldb/API/SBModule.h"
#include "lldb/API/SBModuleSpec.h"
#include "lldb/API/SBMutex.h"
#include "lldb/API/SBProcess.h"
#include "lldb/API/SBSourceManager.h"
#include "lldb/API/SBStream.h"
Expand Down Expand Up @@ -59,11 +56,14 @@
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/Args.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/Instrumentation.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/ProcessInfo.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/ValueObject/ValueObjectConstResult.h"
#include "lldb/ValueObject/ValueObjectList.h"
#include "lldb/ValueObject/ValueObjectVariable.h"
#include "lldb/lldb-public.h"

#include "Commands/CommandObjectBreakpoint.h"
#include "lldb/Interpreter/CommandReturnObject.h"
Expand Down Expand Up @@ -2497,3 +2497,11 @@ lldb::SBTrace SBTarget::CreateTrace(lldb::SBError &error) {
}
return SBTrace();
}

lldb::SBMutex SBTarget::GetAPIMutex() const {
LLDB_INSTRUMENT_VA(this);

if (TargetSP target_sp = GetSP())
return lldb::SBMutex(target_sp);
return lldb::SBMutex();
}
24 changes: 24 additions & 0 deletions lldb/test/API/python_api/target/TestTargetAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,27 @@ def test_setting_selected_target_with_invalid_target(self):
"""Make sure we don't crash when trying to select invalid target."""
target = lldb.SBTarget()
self.dbg.SetSelectedTarget(target)

@no_debug_info_test
def test_get_api_mutex(self):
"""Make sure we can lock and unlock the API mutex from Python."""
target = self.dbg.GetDummyTarget()

mutex = target.GetAPIMutex()
self.assertTrue(mutex.IsValid())
mutex.lock()
# The API call below doesn't actually matter, it's just there to
# confirm we don't block on the API lock.
target.BreakpointCreateByName("foo", "bar")
mutex.unlock()

@no_debug_info_test
def test_get_api_mutex_with_statement(self):
"""Make sure we can lock and unlock the API mutex using a with-statement from Python."""
target = self.dbg.GetDummyTarget()

with target.GetAPIMutex() as mutex:
self.assertTrue(mutex.IsValid())
# The API call below doesn't actually matter, it's just there to
# confirm we don't block on the API lock.
target.BreakpointCreateByName("foo", "bar")
6 changes: 6 additions & 0 deletions lldb/tools/lldb-dap/Breakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
//===----------------------------------------------------------------------===//

#include "Breakpoint.h"
#include "DAP.h"
#include "JSONUtils.h"
#include "lldb/API/SBAddress.h"
#include "lldb/API/SBBreakpointLocation.h"
#include "lldb/API/SBLineEntry.h"
#include "lldb/API/SBMutex.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/JSON.h"
#include <cstddef>
#include <cstdint>
#include <mutex>
#include <string>

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

void Breakpoint::SetBreakpoint() {
lldb::SBMutex lock = dap.GetAPIMutex();
std::lock_guard<lldb::SBMutex> guard(lock);

// See comments in BreakpointBase::GetBreakpointLabel() for details of why
// we add a label to our breakpoints.
bp.AddName(GetBreakpointLabel());
Expand Down
3 changes: 3 additions & 0 deletions lldb/tools/lldb-dap/DAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "lldb/API/SBFile.h"
#include "lldb/API/SBFormat.h"
#include "lldb/API/SBFrame.h"
#include "lldb/API/SBMutex.h"
#include "lldb/API/SBTarget.h"
#include "lldb/API/SBThread.h"
#include "lldb/API/SBValue.h"
Expand Down Expand Up @@ -362,6 +363,8 @@ struct DAP {

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

lldb::SBMutex GetAPIMutex() const { return target.GetAPIMutex(); }

private:
// Send the JSON in "json_str" to the "out" stream. Correctly send the
// "Content-Length:" field followed by the length, followed by the raw
Expand Down
Loading