Skip to content

Commit

Permalink
Add SB API's for writing breakpoints to & creating the from a file.
Browse files Browse the repository at this point in the history
Moved the guts of the code from CommandObjectBreakpoint to Target (should
have done it that way in the first place.)  Added an SBBreakpointList class
so there's a way to specify which breakpoints to serialize and to report the
deserialized breakpoints.

<rdar://problem/12611863> 

llvm-svn: 281520
  • Loading branch information
jimingham committed Sep 14, 2016
1 parent db30877 commit 01f1666
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 115 deletions.
30 changes: 30 additions & 0 deletions lldb/include/lldb/API/SBBreakpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class LLDB_API SBBreakpoint {
GetNumBreakpointLocationsFromEvent(const lldb::SBEvent &event_sp);

private:
friend class SBBreakpointList;
friend class SBBreakpointLocation;
friend class SBTarget;

Expand All @@ -139,6 +140,35 @@ class LLDB_API SBBreakpoint {
lldb::BreakpointSP m_opaque_sp;
};

class SBBreakpointListImpl;

class LLDB_API SBBreakpointList {
public:
SBBreakpointList(SBTarget &target);

~SBBreakpointList();

size_t GetSize() const;

SBBreakpoint GetBreakpointAtIndex(size_t idx);

void Append(const SBBreakpoint &sb_file);

bool AppendIfUnique(const SBBreakpoint &sb_file);

void AppendByID(lldb::break_id_t id);

void Clear();

protected:
friend class SBTarget;

void CopyToBreakpointIDList(lldb_private::BreakpointIDList &bp_id_list);

private:
std::shared_ptr<SBBreakpointListImpl> m_opaque_sp;
};

} // namespace lldb

#endif // LLDB_SBBreakpoint_h_
14 changes: 14 additions & 0 deletions lldb/include/lldb/API/SBTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// Project includes
#include "lldb/API/SBAddress.h"
#include "lldb/API/SBAttachInfo.h"
#include "lldb/API/SBBreakpoint.h"
#include "lldb/API/SBBroadcaster.h"
#include "lldb/API/SBDefines.h"
#include "lldb/API/SBFileSpec.h"
Expand Down Expand Up @@ -641,6 +642,18 @@ class LLDB_API SBTarget {

lldb::SBBreakpoint BreakpointCreateBySBAddress(SBAddress &address);

// Reads in breakpoints from source_file, returning the newly created
// breakpoints in new_bps.
lldb::SBError BreakpointsCreateFromFile(SBFileSpec &source_file,
SBBreakpointList &new_bps);

// Writes all breakpoints to dest_file.
lldb::SBError BreakpointsWriteToFile(SBFileSpec &dest_file);

// Writes the breakpoints in bkpt_list to dest_file
lldb::SBError BreakpointsWriteToFile(SBFileSpec &dest_file,
SBBreakpointList &bkpt_list);

uint32_t GetNumBreakpoints() const;

lldb::SBBreakpoint GetBreakpointAtIndex(uint32_t idx) const;
Expand Down Expand Up @@ -741,6 +754,7 @@ class LLDB_API SBTarget {
protected:
friend class SBAddress;
friend class SBBlock;
friend class SBBreakpointListImpl;
friend class SBDebugger;
friend class SBExecutionContext;
friend class SBFunction;
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Core/StructuredData.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ class StructuredData {

static ObjectSP ParseJSON(std::string json_text);

static ObjectSP ParseJSONFromFile(FileSpec &file, Error &error);
static ObjectSP ParseJSONFromFile(const FileSpec &file, Error &error);
};

} // namespace lldb_private
Expand Down
10 changes: 8 additions & 2 deletions lldb/include/lldb/Target/Target.h
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,12 @@ class Target : public std::enable_shared_from_this<Target>,

bool IgnoreWatchpointByID(lldb::watch_id_t watch_id, uint32_t ignore_count);

Error SerializeBreakpointsToFile(const FileSpec &file,
const BreakpointIDList &bp_ids);

Error CreateBreakpointsFromFile(const FileSpec &file,
BreakpointIDList &new_bps);

//------------------------------------------------------------------
/// Get \a load_addr as a callable code load address for this target
///
Expand Down Expand Up @@ -1191,10 +1197,10 @@ class Target : public std::enable_shared_from_this<Target>,
Debugger &m_debugger;
lldb::PlatformSP m_platform_sp; ///< The platform for this target.
std::recursive_mutex m_mutex; ///< An API mutex that is used by the lldb::SB*
///classes make the SB interface thread safe
/// classes make the SB interface thread safe
ArchSpec m_arch;
ModuleList m_images; ///< The list of images for this process (shared
///libraries and anything dynamically loaded).
/// libraries and anything dynamically loaded).
SectionLoadHistory m_section_load_history;
BreakpointList m_breakpoint_list;
BreakpointList m_internal_breakpoint_list;
Expand Down
25 changes: 25 additions & 0 deletions lldb/scripts/interface/SBBreakpoint.i
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,29 @@ public:

};

class SBBreakpointListImpl;

class LLDB_API SBBreakpointList
{
public:
SBBreakpointList(SBTarget &target);

~SBBreakpointList();

size_t GetSize() const;

SBBreakpoint
GetBreakpointAtIndex(size_t idx);

void Append(const SBBreakpoint &sb_bkpt);

bool AppendIfUnique(const SBBreakpoint &sb_bkpt);

void AppendByID (lldb::break_id_t id);

void Clear();
private:
std::shared_ptr<SBBreakpointListImpl> m_opaque_sp;
};

} // namespace lldb
10 changes: 10 additions & 0 deletions lldb/scripts/interface/SBTarget.i
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,16 @@ public:
bool
DeleteAllBreakpoints ();

lldb::SBError
BreakpointsCreateFromFile(SBFileSpec &source_file,
SBBreakpointList &bkpt_list);

lldb::SBError
BreakpointsWriteToFile(SBFileSpec &dest_file);

lldb::SBError
BreakpointsWriteToFile(SBFileSpec &dest_file, SBBreakpointList &bkpt_list);

uint32_t
GetNumWatchpoints () const;

Expand Down
126 changes: 126 additions & 0 deletions lldb/source/API/SBBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "lldb/API/SBThread.h"

#include "lldb/Breakpoint/Breakpoint.h"
#include "lldb/Breakpoint/BreakpointIDList.h"
#include "lldb/Breakpoint/BreakpointLocation.h"
#include "lldb/Breakpoint/StoppointCallbackContext.h"
#include "lldb/Core/Address.h"
Expand Down Expand Up @@ -678,3 +679,128 @@ SBBreakpoint::GetNumBreakpointLocationsFromEvent(const lldb::SBEvent &event) {
event.GetSP()));
return num_locations;
}

// This is simple collection of breakpoint id's and their target.
class lldb::SBBreakpointListImpl {
public:
SBBreakpointListImpl(SBTarget &target) : m_target_wp() {
if (target.IsValid())
m_target_wp = target.GetSP();
}

~SBBreakpointListImpl() = default;

size_t GetSize() { return m_break_ids.size(); }

BreakpointSP GetBreakpointAtIndex(size_t idx) {
if (idx >= m_break_ids.size())
return BreakpointSP();
TargetSP target_sp = m_target_wp.lock();
if (!target_sp)
return BreakpointSP();
lldb::break_id_t bp_id = m_break_ids[idx];
return target_sp->GetBreakpointList().FindBreakpointByID(bp_id);
}

bool Append(Breakpoint &bkpt) {
TargetSP target_sp = m_target_wp.lock();
if (!target_sp)
return false;
if (bkpt.GetTargetSP() != target_sp)
return false;
m_break_ids.push_back(bkpt.GetID());
return true;
}

bool AppendIfUnique(Breakpoint &bkpt) {
TargetSP target_sp = m_target_wp.lock();
if (!target_sp)
return false;
if (bkpt.GetTargetSP() != target_sp)
return false;
lldb::break_id_t bp_id = bkpt.GetID();
if (find(m_break_ids.begin(), m_break_ids.end(), bp_id) ==
m_break_ids.end())
return false;

m_break_ids.push_back(bkpt.GetID());
return true;
}

bool AppendByID(lldb::break_id_t id) {
TargetSP target_sp = m_target_wp.lock();
if (!target_sp)
return false;
if (id == LLDB_INVALID_BREAK_ID)
return false;
m_break_ids.push_back(id);
return true;
}

void Clear() { m_break_ids.clear(); }

void CopyToBreakpointIDList(lldb_private::BreakpointIDList &bp_list) {
for (lldb::break_id_t id : m_break_ids) {
bp_list.AddBreakpointID(BreakpointID(id));
}
}

TargetSP GetTarget() { return m_target_wp.lock(); }

private:
std::vector<lldb::break_id_t> m_break_ids;
TargetWP m_target_wp;
};

SBBreakpointList::SBBreakpointList(SBTarget &target)
: m_opaque_sp(new lldb::SBBreakpointListImpl(target)) {}

SBBreakpointList::~SBBreakpointList() {}

size_t SBBreakpointList::GetSize() const {
if (!m_opaque_sp)
return 0;
else
return m_opaque_sp->GetSize();
}

SBBreakpoint SBBreakpointList::GetBreakpointAtIndex(size_t idx) {
if (!m_opaque_sp)
return SBBreakpoint();

BreakpointSP bkpt_sp = m_opaque_sp->GetBreakpointAtIndex(idx);
return SBBreakpoint(bkpt_sp);
}

void SBBreakpointList::Append(const SBBreakpoint &sb_bkpt) {
if (!sb_bkpt.IsValid())
return;
if (!m_opaque_sp)
return;
m_opaque_sp->Append(*sb_bkpt.get());
}

void SBBreakpointList::AppendByID(lldb::break_id_t id) {
if (!m_opaque_sp)
return;
m_opaque_sp->AppendByID(id);
}

bool SBBreakpointList::AppendIfUnique(const SBBreakpoint &sb_bkpt) {
if (!sb_bkpt.IsValid())
return false;
if (!m_opaque_sp)
return false;
return m_opaque_sp->AppendIfUnique(*sb_bkpt.get());
}

void SBBreakpointList::Clear() {
if (m_opaque_sp)
m_opaque_sp->Clear();
}

void SBBreakpointList::CopyToBreakpointIDList(
lldb_private::BreakpointIDList &bp_id_list) {
if (m_opaque_sp)
m_opaque_sp->CopyToBreakpointIDList(bp_id_list);
}
52 changes: 52 additions & 0 deletions lldb/source/API/SBTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,58 @@ bool SBTarget::DeleteAllBreakpoints() {
return false;
}

lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
SBBreakpointList &new_bps) {
SBError sberr;
TargetSP target_sp(GetSP());
if (!target_sp) {
sberr.SetErrorString(
"BreakpointCreateFromFile called with invalid target.");
return sberr;
}
std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());

BreakpointIDList bp_ids;
sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(), bp_ids);
if (sberr.Fail())
return sberr;

size_t num_bkpts = bp_ids.GetSize();
for (size_t i = 0; i < num_bkpts; i++) {
BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
new_bps.AppendByID(bp_id.GetBreakpointID());
}
return sberr;
}

lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
SBError sberr;
TargetSP target_sp(GetSP());
if (!target_sp) {
sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
return sberr;
}
SBBreakpointList bkpt_list(*this);
return BreakpointsWriteToFile(dest_file, bkpt_list);
}

lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
SBBreakpointList &bkpt_list) {
SBError sberr;
TargetSP target_sp(GetSP());
if (!target_sp) {
sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
return sberr;
}

std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
BreakpointIDList bp_id_list;
bkpt_list.CopyToBreakpointIDList(bp_id_list);
sberr.ref() =
target_sp->SerializeBreakpointsToFile(dest_file.ref(), bp_id_list);
return sberr;
}

uint32_t SBTarget::GetNumWatchpoints() const {
TargetSP target_sp(GetSP());
if (target_sp) {
Expand Down
Loading

0 comments on commit 01f1666

Please sign in to comment.