Skip to content

[lldb] Add an API to derive language-specific runtime information #116904

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 1 commit into from
Nov 20, 2024
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
5 changes: 5 additions & 0 deletions lldb/include/lldb/API/SBFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class LLDB_API SBFrame {
lldb::SBValue EvaluateExpression(const char *expr,
const SBExpressionOptions &options);

/// Language plugins can use this API to report language-specific
/// runtime information about this compile unit, such as additional
/// language version details or feature flags.
SBStructuredData GetLanguageInfo();

/// Gets the lexical block that defines the stack frame. Another way to think
/// of this is it will return the block that contains all of the variables
/// for a stack frame. Inlined functions are represented as SBBlock objects
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBStructuredData.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class SBStructuredData {
friend class SBCommandReturnObject;
friend class SBLaunchInfo;
friend class SBDebugger;
friend class SBFrame;
friend class SBTarget;
friend class SBProcess;
friend class SBThread;
Expand Down
5 changes: 5 additions & 0 deletions lldb/include/lldb/Target/LanguageRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ class LanguageRuntime : public Runtime, public PluginInterface {
lldb_private::RegisterContext *regctx,
bool &behaves_like_zeroth_frame);

/// Language runtime plugins can use this API to report
/// language-specific runtime information about this compile unit,
/// such as additional language version details or feature flags.
virtual StructuredData::ObjectSP GetLanguageInfo(SymbolContext sc);

protected:
// The static GetRuntimeUnwindPlan method above is only implemented in the
// base class; subclasses may override this protected member if they can
Expand Down
6 changes: 6 additions & 0 deletions lldb/include/lldb/Target/StackFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "lldb/Utility/Scalar.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/Utility/UserID.h"
#include "lldb/ValueObject/ValueObjectList.h"

Expand Down Expand Up @@ -408,6 +409,11 @@ class StackFrame : public ExecutionContextScope,
/// system implementation details this way.
bool IsHidden();

/// Language plugins can use this API to report language-specific
/// runtime information about this compile unit, such as additional
/// language version details or feature flags.
StructuredData::ObjectSP GetLanguageInfo();

/// Get the frame's demangled name.
///
/// /// \return
Expand Down
16 changes: 16 additions & 0 deletions lldb/source/API/SBFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "lldb/API/SBExpressionOptions.h"
#include "lldb/API/SBFormat.h"
#include "lldb/API/SBStream.h"
#include "lldb/API/SBStructuredData.h"
#include "lldb/API/SBSymbolContext.h"
#include "lldb/API/SBThread.h"
#include "lldb/API/SBValue.h"
Expand Down Expand Up @@ -1154,6 +1155,21 @@ lldb::SBValue SBFrame::EvaluateExpression(const char *expr,
return expr_result;
}

SBStructuredData SBFrame::GetLanguageInfo() {
LLDB_INSTRUMENT_VA(this);

SBStructuredData sb_data;
std::unique_lock<std::recursive_mutex> lock;
ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
StackFrame *frame = exe_ctx.GetFramePtr();
if (!frame)
return sb_data;

StructuredData::ObjectSP data(frame->GetLanguageInfo());
sb_data.m_impl_up->SetObjectSP(data);
return sb_data;
}

bool SBFrame::IsInlined() {
LLDB_INSTRUMENT_VA(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3398,6 +3398,13 @@ std::optional<uint64_t> AppleObjCRuntimeV2::GetSharedCacheImageHeaderVersion() {
return std::nullopt;
}

StructuredData::ObjectSP AppleObjCRuntimeV2::GetLanguageInfo(SymbolContext sc) {
auto dict_up = std::make_unique<StructuredData::Dictionary>();
dict_up->AddItem("Objective-C runtime version",
std::make_unique<StructuredData::UnsignedInteger>(2));
return dict_up;
}

#pragma mark Frame recognizers

class ObjCExceptionRecognizedStackFrame : public RecognizedStackFrame {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class AppleObjCRuntimeV2 : public AppleObjCRuntime {

std::optional<uint64_t> GetSharedCacheImageHeaderVersion();

StructuredData::ObjectSP GetLanguageInfo(SymbolContext sc) override;

protected:
lldb::BreakpointResolverSP
CreateExceptionResolver(const lldb::BreakpointSP &bkpt, bool catch_bp,
Expand Down
4 changes: 4 additions & 0 deletions lldb/source/Target/LanguageRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ LanguageRuntime::GetRuntimeUnwindPlan(Thread &thread, RegisterContext *regctx,
return UnwindPlanSP();
}

StructuredData::ObjectSP LanguageRuntime::GetLanguageInfo(SymbolContext sc) {
return {};
}

void LanguageRuntime::InitializeCommands(CommandObject *parent) {
if (!parent)
return;
Expand Down
13 changes: 13 additions & 0 deletions lldb/source/Target/StackFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/ABI.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/LanguageRuntime.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/StackFrameRecognizer.h"
Expand Down Expand Up @@ -1230,6 +1231,18 @@ bool StackFrame::IsHidden() {
return false;
}

StructuredData::ObjectSP StackFrame::GetLanguageInfo() {
auto process_sp = CalculateProcess();
SourceLanguage language = GetLanguage();
if (!language)
return {};
if (auto runtime_sp =
process_sp->GetLanguageRuntime(language.AsLanguageType()))
return runtime_sp->GetLanguageInfo(
GetSymbolContext(eSymbolContextFunction));
return {};
}

const char *StackFrame::GetFunctionName() {
const char *name = nullptr;
SymbolContext sc = GetSymbolContext(
Expand Down
4 changes: 4 additions & 0 deletions lldb/test/API/lang/objc/languageinfo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OBJC_SOURCES := main.m
LD_EXTRAS := -lobjc

include Makefile.rules
16 changes: 16 additions & 0 deletions lldb/test/API/lang/objc/languageinfo/TestObjCLanguageInfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class ObjCiVarIMPTestCase(TestBase):
@skipUnlessDarwin
@no_debug_info_test
def test_imp_ivar_type(self):
self.build()
target, process, thread, bkpt = lldbutil.run_to_name_breakpoint(self, "main")
frame = thread.GetFrameAtIndex(0)
lang_info = frame.GetLanguageInfo()
version = lang_info.GetValueForKey("Objective-C runtime version")
self.assertEqual(version.GetIntegerValue(), 2)
1 change: 1 addition & 0 deletions lldb/test/API/lang/objc/languageinfo/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int main(int argc, char const *argv[]) { return 0; }
Loading