Skip to content

Commit b8923c0

Browse files
committed
[lldb/API] Expose Module::IsLoadedInTarget() to SB API (NFC)
This patch adds an `SBTarget::IsLoaded(const SBModule&) const` endpoint to lldb's Scripting Bridge API. As the name suggests, it will allow the user to know if the module is loaded in a specific target. rdar://37957625 Differential Review: https://reviews.llvm.org/D95686 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 94fac81 commit b8923c0

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

lldb/bindings/interface/SBTarget.i

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,16 @@ public:
938938
lldb::addr_t
939939
GetStackRedZoneSize();
940940

941+
%feature("docstring", "
942+
Returns true if the module has been loaded in this `SBTarget`.
943+
A module can be loaded either by the dynamic loader or by being manually
944+
added to the target (see `SBTarget.AddModule` and the `target module add` command).
945+
946+
:rtype: bool
947+
") IsLoaded;
948+
bool
949+
IsLoaded (const lldb::SBModule &module) const;
950+
941951
lldb::SBLaunchInfo
942952
GetLaunchInfo () const;
943953

lldb/include/lldb/API/SBTarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,8 @@ class LLDB_API SBTarget {
834834

835835
lldb::addr_t GetStackRedZoneSize();
836836

837+
bool IsLoaded(const lldb::SBModule &module) const;
838+
837839
lldb::SBLaunchInfo GetLaunchInfo() const;
838840

839841
void SetLaunchInfo(const lldb::SBLaunchInfo &launch_info);

lldb/source/API/SBTarget.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,6 +2410,21 @@ lldb::addr_t SBTarget::GetStackRedZoneSize() {
24102410
return 0;
24112411
}
24122412

2413+
bool SBTarget::IsLoaded(const SBModule &module) const {
2414+
LLDB_RECORD_METHOD_CONST(bool, SBTarget, IsLoaded, (const lldb::SBModule &),
2415+
module);
2416+
2417+
TargetSP target_sp(GetSP());
2418+
if (!target_sp)
2419+
return LLDB_RECORD_RESULT(false);
2420+
2421+
ModuleSP module_sp(module.GetSP());
2422+
if (!module_sp)
2423+
return LLDB_RECORD_RESULT(false);
2424+
2425+
return LLDB_RECORD_RESULT(module_sp->IsLoadedInTarget(target_sp.get()));
2426+
}
2427+
24132428
lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
24142429
LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBLaunchInfo, SBTarget, GetLaunchInfo);
24152430

@@ -2682,6 +2697,8 @@ void RegisterMethods<SBTarget>(Registry &R) {
26822697
LLDB_REGISTER_METHOD(lldb::SBValue, SBTarget, EvaluateExpression,
26832698
(const char *, const lldb::SBExpressionOptions &));
26842699
LLDB_REGISTER_METHOD(lldb::addr_t, SBTarget, GetStackRedZoneSize, ());
2700+
LLDB_REGISTER_METHOD_CONST(bool, SBTarget, IsLoaded,
2701+
(const lldb::SBModule &));
26852702
LLDB_REGISTER_METHOD_CONST(lldb::SBLaunchInfo, SBTarget, GetLaunchInfo, ());
26862703
LLDB_REGISTER_METHOD(void, SBTarget, SetLaunchInfo,
26872704
(const lldb::SBLaunchInfo &));

lldb/test/API/python_api/target/TestTargetAPI.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,27 @@ def test_default_arch(self):
489489
target3 = self.dbg.CreateTargetWithFileAndTargetTriple(exe, target.GetTriple())
490490
self.assertTrue(target3.IsValid())
491491

492+
493+
@add_test_categories(['pyapi'])
494+
def test_is_loaded(self):
495+
"""Exercise SBTarget.IsLoaded(SBModule&) API."""
496+
d = {'EXE': 'b.out'}
497+
self.build(dictionary=d)
498+
self.setTearDownCleanup(dictionary=d)
499+
target = self.create_simple_target('b.out')
500+
501+
self.assertFalse(target.IsLoaded(lldb.SBModule()))
502+
503+
num_modules = target.GetNumModules()
504+
for i in range(num_modules):
505+
module = target.GetModuleAtIndex(i)
506+
self.assertFalse(target.IsLoaded(module), "Target that isn't "
507+
"running shouldn't have any module loaded.")
508+
509+
process = target.LaunchSimple(None, None,
510+
self.get_process_working_directory())
511+
512+
for i in range(num_modules):
513+
module = target.GetModuleAtIndex(i)
514+
self.assertTrue(target.IsLoaded(module), "Running the target should "
515+
"have loaded its modules.")

0 commit comments

Comments
 (0)