[Lldb-commits] [lldb] ff52ef3 - [lldb/API] Add ability to check if module is backed by a file on disk

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Wed Jan 26 11:42:03 PST 2022


Author: Med Ismail Bennani
Date: 2022-01-26T20:40:24+01:00
New Revision: ff52ef334beb20a90bdb438419000dce4aacba9d

URL: https://github.com/llvm/llvm-project/commit/ff52ef334beb20a90bdb438419000dce4aacba9d
DIFF: https://github.com/llvm/llvm-project/commit/ff52ef334beb20a90bdb438419000dce4aacba9d.diff

LOG: [lldb/API] Add ability to check if module is backed by a file on disk

This patch introduces a new SBAPI method: `SBModule::IsFileBacked`

As the name suggests, it tells the user if the module's object file is
on disk or in memory.

rdar://68538278

Differential Revision: https://reviews.llvm.org/D118261

Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>

Added: 
    lldb/test/API/python_api/sbmodule/Makefile
    lldb/test/API/python_api/sbmodule/TestSBModule.py
    lldb/test/API/python_api/sbmodule/main.c

Modified: 
    lldb/bindings/interface/SBModule.i
    lldb/include/lldb/API/SBModule.h
    lldb/source/API/SBModule.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBModule.i b/lldb/bindings/interface/SBModule.i
index 606c9a5bbd0cf..bda602d15690e 100644
--- a/lldb/bindings/interface/SBModule.i
+++ b/lldb/bindings/interface/SBModule.i
@@ -137,6 +137,13 @@ public:
     void
     Clear();
 
+    %feature("docstring", "Check if the module is file backed.
+    @return
+        True, if the module is backed by an object file on disk.
+        False, if the module is backed by an object file in memory.") IsFileBacked;
+    bool
+    IsFileBacked() const;
+
     %feature("docstring", "
     Get const accessor for the module file specification.
 

diff  --git a/lldb/include/lldb/API/SBModule.h b/lldb/include/lldb/API/SBModule.h
index dd783fe4107db..7200a1ef53fd8 100644
--- a/lldb/include/lldb/API/SBModule.h
+++ b/lldb/include/lldb/API/SBModule.h
@@ -37,6 +37,8 @@ class LLDB_API SBModule {
 
   void Clear();
 
+  bool IsFileBacked() const;
+
   /// Get const accessor for the module file specification.
   ///
   /// This function returns the file for the module on the host system

diff  --git a/lldb/source/API/SBModule.cpp b/lldb/source/API/SBModule.cpp
index 2483495b97db0..1454012d3eb9a 100644
--- a/lldb/source/API/SBModule.cpp
+++ b/lldb/source/API/SBModule.cpp
@@ -88,6 +88,20 @@ void SBModule::Clear() {
   m_opaque_sp.reset();
 }
 
+bool SBModule::IsFileBacked() const {
+  LLDB_INSTRUMENT_VA(this);
+
+  ModuleSP module_sp(GetSP());
+  if (!module_sp)
+    return false;
+
+  ObjectFile *obj_file = module_sp->GetObjectFile();
+  if (!obj_file)
+    return false;
+
+  return !obj_file->IsInMemory();
+}
+
 SBFileSpec SBModule::GetFileSpec() const {
   LLDB_INSTRUMENT_VA(this);
 

diff  --git a/lldb/test/API/python_api/sbmodule/Makefile b/lldb/test/API/python_api/sbmodule/Makefile
new file mode 100644
index 0000000000000..10495940055b6
--- /dev/null
+++ b/lldb/test/API/python_api/sbmodule/Makefile
@@ -0,0 +1,3 @@
+C_SOURCES := main.c
+
+include Makefile.rules

diff  --git a/lldb/test/API/python_api/sbmodule/TestSBModule.py b/lldb/test/API/python_api/sbmodule/TestSBModule.py
new file mode 100644
index 0000000000000..ab6a9a20884a3
--- /dev/null
+++ b/lldb/test/API/python_api/sbmodule/TestSBModule.py
@@ -0,0 +1,58 @@
+"""Test the SBDModule APIs."""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+import os, signal, subprocess
+
+class SBModuleAPICase(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+
+    def setUp(self):
+        TestBase.setUp(self)
+        self.background_pid = None
+
+    def tearDown(self):
+        TestBase.tearDown(self)
+        if self.background_pid:
+            os.kill(self.background_pid, signal.SIGKILL)
+
+
+    def test_module_is_file_backed(self):
+        """Test the SBModule::IsFileBacked() method"""
+        self.build()
+        target, _, _, _  = lldbutil.run_to_source_breakpoint(self, "// break here",
+                                                    lldb.SBFileSpec("main.c"))
+
+        self.assertGreater(target.GetNumModules(), 0)
+        main_module = target.GetModuleAtIndex(0)
+        self.assertEqual(main_module.GetFileSpec().GetFilename(), "a.out")
+        self.assertTrue(main_module.IsFileBacked(),
+                         "The module should be backed by a file on disk")
+
+        self.dbg.DeleteTarget(target)
+        self.assertEqual(self.dbg.GetNumTargets(), 0)
+
+        exe = self.getBuildArtifact("a.out")
+        background_process = subprocess.Popen([exe])
+        self.assertTrue(background_process, "process is not valid")
+        self.background_pid = background_process.pid
+        os.unlink(exe)
+
+        target = self.dbg.CreateTarget('')
+        self.assertEqual(self.dbg.GetNumTargets(), 1)
+        error = lldb.SBError()
+        process = target.AttachToProcessWithID(self.dbg.GetListener(),
+                                               self.background_pid, error)
+        self.assertTrue(error.Success() and process,  PROCESS_IS_VALID)
+        main_module = target.GetModuleAtIndex(0)
+        self.assertEqual(main_module.GetFileSpec().GetFilename(), "a.out")
+        self.assertFalse(main_module.IsFileBacked(),
+                         "The module should not be backed by a file on disk.")
+
+        error = process.Destroy()
+        self.assertTrue(error.Success(), "couldn't destroy process %s" % background_process.pid)
+

diff  --git a/lldb/test/API/python_api/sbmodule/main.c b/lldb/test/API/python_api/sbmodule/main.c
new file mode 100644
index 0000000000000..101d495698f45
--- /dev/null
+++ b/lldb/test/API/python_api/sbmodule/main.c
@@ -0,0 +1,5 @@
+int main() {
+  while (1) // break here
+    ;
+  return 42;
+}


        


More information about the lldb-commits mailing list