Skip to content

Commit

Permalink
Add AddressRange to SB API
Browse files Browse the repository at this point in the history
Summary:
This adds new SB API calls and classes to allow a user of the SB API to
obtain an address range from SBFunction and SBBlock.

Test Plan:

Reviewers: clayborg

Subscribers: lldb-commits

Tasks:

Tags:
  • Loading branch information
mbucko committed May 13, 2024
1 parent a037d88 commit 309050c
Show file tree
Hide file tree
Showing 23 changed files with 533 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lldb/bindings/headers.swig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
%{
#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"
Expand Down
3 changes: 3 additions & 0 deletions lldb/bindings/interface/SBAddressRangeDocstrings.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%feature("docstring",
"API clients can get address range information."
) lldb::SBAddressRange;
3 changes: 3 additions & 0 deletions lldb/bindings/interface/SBAddressRangeListDocstrings.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%feature("docstring",
"Represents a list of :py:class:`SBAddressRange`."
) lldb::SBAddressRangeList;
13 changes: 13 additions & 0 deletions lldb/bindings/interface/SBAddressRangeListExtensions.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
%extend lldb::SBAddressRangeList {
#ifdef SWIGPYTHON
%pythoncode%{
def __len__(self):
'''Return the number of address ranges in a lldb.SBAddressRangeList object.'''
return self.GetSize()

def __iter__(self):
'''Iterate over all the address ranges in a lldb.SBAddressRangeList object.'''
return lldb_iter(self, 'GetSize', 'GetAddressRangeAtIndex')
%}
#endif
}
2 changes: 2 additions & 0 deletions lldb/bindings/interfaces.swig
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@

/* API headers */
%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"
Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/API/LLDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define LLDB_API_LLDB_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"
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class LLDB_API SBAddress {
lldb::SBLineEntry GetLineEntry();

protected:
friend class SBAddressRange;
friend class SBBlock;
friend class SBBreakpoint;
friend class SBBreakpointLocation;
Expand Down
63 changes: 63 additions & 0 deletions lldb/include/lldb/API/SBAddressRange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===-- SBAddressRange.h ----------------------------------------*- C++ -*-===//
//
// 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_SBADDRESSRANGE_H
#define LLDB_API_SBADDRESSRANGE_H

#include "lldb/API/SBDefines.h"

namespace lldb {

class LLDB_API SBAddressRange {
public:
SBAddressRange();

SBAddressRange(const lldb::SBAddressRange &rhs);

SBAddressRange(lldb::addr_t file_addr, lldb::addr_t byte_size);

~SBAddressRange();

const lldb::SBAddressRange &operator=(const lldb::SBAddressRange &rhs);

void Clear();

bool IsValid() const;

/// Get the base address of the range.
///
/// \return
/// Base address object.
lldb::SBAddress GetBaseAddress() const;

/// Get the byte size of this range.
///
/// \return
/// The size in bytes of this address range.
lldb::addr_t GetByteSize() const;

protected:
friend class SBAddressRangeList;
friend class SBBlock;
friend class SBFunction;

lldb_private::AddressRange &ref();

const lldb_private::AddressRange &ref() const;

private:
AddressRangeUP m_opaque_up;
};

#ifndef SWIG
bool LLDB_API operator==(const SBAddressRange &lhs, const SBAddressRange &rhs);
#endif

} // namespace lldb

#endif // LLDB_API_SBADDRESSRANGE_H
57 changes: 57 additions & 0 deletions lldb/include/lldb/API/SBAddressRangeList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===-- SBAddressRangeList.h ------------------------------------*- C++ -*-===//
//
// 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_SBADDRESSRANGELIST_H
#define LLDB_API_SBADDRESSRANGELIST_H

#include <memory>

#include "lldb/API/SBDefines.h"

class AddressRangeListImpl;

namespace lldb {

class LLDB_API SBAddressRangeList {
public:
SBAddressRangeList();

SBAddressRangeList(const lldb::SBAddressRangeList &rhs);

~SBAddressRangeList();

const lldb::SBAddressRangeList &operator=(const lldb::SBAddressRangeList &rhs);

uint32_t GetSize() const;

void Clear();

bool GetAddressRangeAtIndex(uint64_t idx, SBAddressRange &addr_range);

void Append(const lldb::SBAddressRange &addr_range);

void Append(const lldb::SBAddressRangeList &addr_range_list);

protected:
const AddressRangeListImpl *operator->() const;

const AddressRangeListImpl &operator*() const;

private:
friend class SBProcess;

lldb_private::AddressRanges &ref();

const lldb_private::AddressRanges &ref() const;

std::unique_ptr<AddressRangeListImpl> m_opaque_up;
};

} // namespace lldb

#endif // LLDB_API_SBADDRESSRANGELIST_H
3 changes: 3 additions & 0 deletions lldb/include/lldb/API/SBBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLDB_API_SBBLOCK_H
#define LLDB_API_SBBLOCK_H

#include "lldb/API/SBAddressRange.h"
#include "lldb/API/SBDefines.h"
#include "lldb/API/SBFrame.h"
#include "lldb/API/SBTarget.h"
Expand Down Expand Up @@ -52,6 +53,8 @@ class LLDB_API SBBlock {

lldb::SBAddress GetRangeEndAddress(uint32_t idx);

lldb::SBAddressRange GetRangeAtIndex(uint32_t idx);

uint32_t GetRangeIndexForBlockAddress(lldb::SBAddress block_addr);

lldb::SBValueList GetVariables(lldb::SBFrame &frame, bool arguments,
Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/API/SBDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
namespace lldb {

class LLDB_API SBAddress;
class LLDB_API SBAddressRange;
class LLDB_API SBAddressRangeList;
class LLDB_API SBAttachInfo;
class LLDB_API SBBlock;
class LLDB_API SBBreakpoint;
Expand Down
3 changes: 3 additions & 0 deletions lldb/include/lldb/API/SBFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLDB_API_SBFUNCTION_H

#include "lldb/API/SBAddress.h"
#include "lldb/API/SBAddressRange.h"
#include "lldb/API/SBDefines.h"
#include "lldb/API/SBInstructionList.h"

Expand Down Expand Up @@ -44,6 +45,8 @@ class LLDB_API SBFunction {

lldb::SBAddress GetEndAddress();

lldb::SBAddressRange GetRange();

const char *GetArgumentName(uint32_t arg_idx);

uint32_t GetPrologueByteSize();
Expand Down
8 changes: 8 additions & 0 deletions lldb/include/lldb/Core/AddressRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class AddressRange {
/// (LLDB_INVALID_ADDRESS) and a zero byte size.
void Clear();

bool IsValid() const;

/// Check if a section offset address is contained in this range.
///
/// \param[in] so_addr
Expand Down Expand Up @@ -242,6 +244,12 @@ class AddressRange {
lldb::addr_t m_byte_size = 0; ///< The size in bytes of this address range.
};

// Forward-declarable wrapper.
class AddressRanges : public std::vector<lldb_private::AddressRange> {
public:
using std::vector<lldb_private::AddressRange>::vector;
};

} // namespace lldb_private

#endif // LLDB_CORE_ADDRESSRANGE_H
3 changes: 3 additions & 0 deletions lldb/include/lldb/lldb-forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ASTResultSynthesizer;
class ASTStructExtractor;
class Address;
class AddressRange;
class AddressRanges;
class AddressRangeList;
class AddressResolver;
class ArchSpec;
class Architecture;
Expand Down Expand Up @@ -308,6 +310,7 @@ template <unsigned N> class StreamBuffer;
namespace lldb {

typedef std::shared_ptr<lldb_private::ABI> ABISP;
typedef std::unique_ptr<lldb_private::AddressRange> AddressRangeUP;
typedef std::shared_ptr<lldb_private::Baton> BatonSP;
typedef std::shared_ptr<lldb_private::Block> BlockSP;
typedef std::shared_ptr<lldb_private::Breakpoint> BreakpointSP;
Expand Down
2 changes: 2 additions & 0 deletions lldb/source/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ add_custom_target(lldb-sbapi-dwarf-enums

add_lldb_library(liblldb SHARED ${option_framework}
SBAddress.cpp
SBAddressRange.cpp
SBAddressRangeList.cpp
SBAttachInfo.cpp
SBBlock.cpp
SBBreakpoint.cpp
Expand Down
78 changes: 78 additions & 0 deletions lldb/source/API/SBAddressRange.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//===-- SBAddressRange.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/SBAddressRange.h"
#include "lldb/API/SBAddress.h"
#include "lldb/Core/AddressRange.h"
#include "lldb/Utility/Instrumentation.h"
#include "Utils.h"
#include <cstddef>
#include <memory>

using namespace lldb;
using namespace lldb_private;

SBAddressRange::SBAddressRange()
: m_opaque_up(std::make_unique<AddressRange>()) {
LLDB_INSTRUMENT_VA(this);
}

SBAddressRange::SBAddressRange(const SBAddressRange &rhs) {
LLDB_INSTRUMENT_VA(this, rhs);

m_opaque_up = clone(rhs.m_opaque_up);
}

SBAddressRange::SBAddressRange(lldb::addr_t file_addr, lldb::addr_t byte_size)
: m_opaque_up(std::make_unique<AddressRange>(file_addr, byte_size)) {
LLDB_INSTRUMENT_VA(this, file_addr, byte_size);
}

SBAddressRange::~SBAddressRange() = default;

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

if (this != &rhs)
m_opaque_up = clone(rhs.m_opaque_up);
return *this;
}

void SBAddressRange::Clear() {
LLDB_INSTRUMENT_VA(this);

m_opaque_up.reset();
}

bool SBAddressRange::IsValid() const {
return m_opaque_up && m_opaque_up->IsValid();
}

lldb::SBAddress SBAddressRange::GetBaseAddress() const {
LLDB_INSTRUMENT_VA(this);

assert(m_opaque_up.get() && "AddressRange is NULL");
return lldb::SBAddress(m_opaque_up->GetBaseAddress());
}

lldb::addr_t SBAddressRange::GetByteSize() const {
LLDB_INSTRUMENT_VA(this);

assert(m_opaque_up.get() && "AddressRange is NULL");
return m_opaque_up->GetByteSize();
}

AddressRange &SBAddressRange::ref() {
assert(m_opaque_up.get() && "AddressRange is NULL");
return *m_opaque_up;
}

const AddressRange &SBAddressRange::ref() const {
assert(m_opaque_up.get() && "AddressRange is NULL");
return *m_opaque_up;
}
Loading

0 comments on commit 309050c

Please sign in to comment.