Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions lldb/bindings/interface/SBFrameListExtensions.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
%extend lldb::SBFrameList {

#ifdef SWIGPYTHON
%nothreadallow;
#endif
std::string lldb::SBFrameList::__str__ (){
lldb::SBStream description;
if (!$self->GetDescription(description))
return std::string("<empty> lldb.SBFrameList()");
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return std::string(desc, desc_len);
}
#ifdef SWIGPYTHON
%clearnothreadallow;
#endif

#ifdef SWIGPYTHON
%pythoncode %{
def __iter__(self):
'''Iterate over all frames in a lldb.SBFrameList object.'''
return lldb_iter(self, 'GetSize', 'GetFrameAtIndex')

def __len__(self):
return int(self.GetSize())

def __getitem__(self, key):
if type(key) is not int:
return None
if key < 0:
count = len(self)
if -count <= key < count:
key %= count

frame = self.GetFrameAtIndex(key)
return frame if frame.IsValid() else None
%}
#endif
}
3 changes: 2 additions & 1 deletion lldb/bindings/interface/SBThreadExtensions.i
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ STRING_EXTENSION_OUTSIDE(SBThread)
def get_thread_frames(self):
'''An accessor function that returns a list() that contains all frames in a lldb.SBThread object.'''
frames = []
for frame in self:
frame_list = self.GetFrames()
for frame in frame_list:
frames.append(frame)
return frames

Expand Down
2 changes: 2 additions & 0 deletions lldb/bindings/interfaces.swig
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
%include "lldb/API/SBFileSpecList.h"
%include "lldb/API/SBFormat.h"
%include "lldb/API/SBFrame.h"
%include "lldb/API/SBFrameList.h"
%include "lldb/API/SBFunction.h"
%include "lldb/API/SBHostOS.h"
%include "lldb/API/SBInstruction.h"
Expand Down Expand Up @@ -193,6 +194,7 @@
%include "./interface/SBFileSpecExtensions.i"
%include "./interface/SBFileSpecListExtensions.i"
%include "./interface/SBFrameExtensions.i"
%include "./interface/SBFrameListExtensions.i"
%include "./interface/SBFunctionExtensions.i"
%include "./interface/SBInstructionExtensions.i"
%include "./interface/SBInstructionListExtensions.i"
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 @@ -37,6 +37,7 @@
#include "lldb/API/SBFileSpecList.h"
#include "lldb/API/SBFormat.h"
#include "lldb/API/SBFrame.h"
#include "lldb/API/SBFrameList.h"
#include "lldb/API/SBFunction.h"
#include "lldb/API/SBHostOS.h"
#include "lldb/API/SBInstruction.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 @@ -76,6 +76,7 @@ class LLDB_API SBFileSpec;
class LLDB_API SBFileSpecList;
class LLDB_API SBFormat;
class LLDB_API SBFrame;
class LLDB_API SBFrameList;
class LLDB_API SBFunction;
class LLDB_API SBHostOS;
class LLDB_API SBInstruction;
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class LLDB_API SBFrame {
protected:
friend class SBBlock;
friend class SBExecutionContext;
friend class SBFrameList;
friend class SBInstruction;
friend class SBThread;
friend class SBValue;
Expand Down
82 changes: 82 additions & 0 deletions lldb/include/lldb/API/SBFrameList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//===----------------------------------------------------------------------===//
//
// 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_SBFRAMELIST_H
#define LLDB_API_SBFRAMELIST_H

#include "lldb/API/SBDefines.h"

namespace lldb {

/// Represents a list of SBFrame objects.
///
/// SBFrameList provides a way to iterate over stack frames lazily,
/// materializing frames on-demand as they are accessed. This is more
/// efficient than eagerly creating all frames upfront.
class LLDB_API SBFrameList {
public:
SBFrameList();

SBFrameList(const lldb::SBFrameList &rhs);

~SBFrameList();

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

explicit operator bool() const;

bool IsValid() const;

/// Returns the number of frames in the list.
uint32_t GetSize() const;

/// Returns the frame at the given index.
///
/// \param[in] idx
/// The index of the frame to retrieve (0-based).
///
/// \return
/// An SBFrame object for the frame at the specified index.
/// Returns an invalid SBFrame if idx is out of range.
lldb::SBFrame GetFrameAtIndex(uint32_t idx) const;

/// Get the thread associated with this frame list.
///
/// \return
/// An SBThread object representing the thread.
lldb::SBThread GetThread() const;

/// Clear all frames from this list.
void Clear();

/// Get a description of this frame list.
///
/// \param[in] description
/// The stream to write the description to.
///
/// \return
/// True if the description was successfully written.
bool GetDescription(lldb::SBStream &description) const;

protected:
friend class SBThread;

private:
SBFrameList(const lldb::StackFrameListSP &frame_list_sp);

void SetFrameList(const lldb::StackFrameListSP &frame_list_sp);

// This needs to be a shared_ptr since an SBFrameList can be passed to
// scripting affordances like ScriptedFrameProviders but also out of
// convenience because Thread::GetStackFrameList returns a StackFrameListSP.
lldb::StackFrameListSP m_opaque_sp;
};

} // namespace lldb

#endif // LLDB_API_SBFRAMELIST_H
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class LLDB_API SBStream {
friend class SBFileSpec;
friend class SBFileSpecList;
friend class SBFrame;
friend class SBFrameList;
friend class SBFunction;
friend class SBInstruction;
friend class SBInstructionList;
Expand Down
3 changes: 3 additions & 0 deletions lldb/include/lldb/API/SBThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class LLDB_API SBThread {

lldb::SBFrame GetFrameAtIndex(uint32_t idx);

lldb::SBFrameList GetFrames();

lldb::SBFrame GetSelectedFrame();

lldb::SBFrame SetSelectedFrame(uint32_t frame_idx);
Expand Down Expand Up @@ -244,6 +246,7 @@ class LLDB_API SBThread {
friend class SBSaveCoreOptions;
friend class SBExecutionContext;
friend class SBFrame;
friend class SBFrameList;
friend class SBProcess;
friend class SBDebugger;
friend class SBValue;
Expand Down
3 changes: 3 additions & 0 deletions lldb/include/lldb/Target/StackFrameList.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ class StackFrameList {
/// Returns whether we have currently fetched all the frames of a stack.
bool WereAllFramesFetched() const;

/// Get the thread associated with this frame list.
Thread &GetThread() const { return m_thread; }

protected:
friend class Thread;
friend class ScriptedThread;
Expand Down
4 changes: 2 additions & 2 deletions lldb/include/lldb/Target/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,8 @@ class Thread : public std::enable_shared_from_this<Thread>,
/// an empty std::optional is returned in that case.
std::optional<lldb::addr_t> GetPreviousFrameZeroPC();

lldb::StackFrameListSP GetStackFrameList();

protected:
friend class ThreadPlan;
friend class ThreadList;
Expand Down Expand Up @@ -1336,8 +1338,6 @@ class Thread : public std::enable_shared_from_this<Thread>,
return StructuredData::ObjectSP();
}

lldb::StackFrameListSP GetStackFrameList();

void SetTemporaryResumeState(lldb::StateType new_state) {
m_temporary_resume_state = new_state;
}
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 @@ -69,6 +69,7 @@ add_lldb_library(liblldb SHARED ${option_framework}
SBFileSpecList.cpp
SBFormat.cpp
SBFrame.cpp
SBFrameList.cpp
SBFunction.cpp
SBHostOS.cpp
SBInstruction.cpp
Expand Down
97 changes: 97 additions & 0 deletions lldb/source/API/SBFrameList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//===----------------------------------------------------------------------===//
//
// 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/SBFrameList.h"
#include "lldb/API/SBFrame.h"
#include "lldb/API/SBStream.h"
#include "lldb/API/SBThread.h"
#include "lldb/Target/StackFrameList.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/Instrumentation.h"

using namespace lldb;
using namespace lldb_private;

SBFrameList::SBFrameList() : m_opaque_sp() { LLDB_INSTRUMENT_VA(this); }

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

SBFrameList::~SBFrameList() = default;

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

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

SBFrameList::SBFrameList(const lldb::StackFrameListSP &frame_list_sp)
: m_opaque_sp(frame_list_sp) {}

void SBFrameList::SetFrameList(const lldb::StackFrameListSP &frame_list_sp) {
m_opaque_sp = frame_list_sp;
}

SBFrameList::operator bool() const {
LLDB_INSTRUMENT_VA(this);

return m_opaque_sp.get() != nullptr;
}

bool SBFrameList::IsValid() const {
LLDB_INSTRUMENT_VA(this);
return this->operator bool();
}

uint32_t SBFrameList::GetSize() const {
LLDB_INSTRUMENT_VA(this);

if (m_opaque_sp)
return m_opaque_sp->GetNumFrames();
return 0;
}

SBFrame SBFrameList::GetFrameAtIndex(uint32_t idx) const {
LLDB_INSTRUMENT_VA(this, idx);

SBFrame sb_frame;
if (m_opaque_sp)
sb_frame.SetFrameSP(m_opaque_sp->GetFrameAtIndex(idx));
return sb_frame;
}

SBThread SBFrameList::GetThread() const {
LLDB_INSTRUMENT_VA(this);

SBThread sb_thread;
if (m_opaque_sp)
sb_thread.SetThread(m_opaque_sp->GetThread().shared_from_this());
return sb_thread;
}

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

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

bool SBFrameList::GetDescription(SBStream &description) const {
LLDB_INSTRUMENT_VA(this, description);

if (!m_opaque_sp)
return false;

Stream &strm = description.ref();
m_opaque_sp->Dump(&strm);
return true;
}
21 changes: 21 additions & 0 deletions lldb/source/API/SBThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "lldb/API/SBFileSpec.h"
#include "lldb/API/SBFormat.h"
#include "lldb/API/SBFrame.h"
#include "lldb/API/SBFrameList.h"
#include "lldb/API/SBProcess.h"
#include "lldb/API/SBStream.h"
#include "lldb/API/SBStructuredData.h"
Expand Down Expand Up @@ -1102,6 +1103,26 @@ SBFrame SBThread::GetFrameAtIndex(uint32_t idx) {
return sb_frame;
}

lldb::SBFrameList SBThread::GetFrames() {
LLDB_INSTRUMENT_VA(this);

SBFrameList sb_frame_list;
llvm::Expected<StoppedExecutionContext> exe_ctx =
GetStoppedExecutionContext(m_opaque_sp);
if (!exe_ctx) {
LLDB_LOG_ERROR(GetLog(LLDBLog::API), exe_ctx.takeError(), "{0}");
return SBFrameList();
}

if (exe_ctx->HasThreadScope()) {
StackFrameListSP frame_list_sp =
exe_ctx->GetThreadPtr()->GetStackFrameList();
sb_frame_list.SetFrameList(frame_list_sp);
}

return sb_frame_list;
}

lldb::SBFrame SBThread::GetSelectedFrame() {
LLDB_INSTRUMENT_VA(this);

Expand Down
3 changes: 3 additions & 0 deletions lldb/test/API/python_api/frame_list/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CXX_SOURCES := main.cpp

include Makefile.rules
Loading
Loading