mib updated this revision to Diff 323484.
mib marked 3 inline comments as done.
mib edited the summary of this revision.
mib added a comment.
Herald added a subscriber: mgorny.

Address @JDevlieghere comments:

- Instead of having the ScriptedProcess python base class in a SWIG file, I 
moved it to a python file in the example folder. There is a post-build event 
that copies it to the python lldb module under to `lldb.plugins` package.
- Added pydoc documentation to the ScriptedProcess base class.
- Removed all the typing hints that prevented the feature to be compatible with 
Python 2. I, however, kept them in the `MyScriptProcess` example.

I also added a first test that checks that the base class exists in the lldb 
python module.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D95712/new/

https://reviews.llvm.org/D95712

Files:
  lldb/bindings/python/CMakeLists.txt
  lldb/examples/python/scripted_process/my_scripted_process.py
  lldb/examples/python/scripted_process/scripted_process.py
  lldb/test/API/functionalities/scripted_process/Makefile
  lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py
  lldb/test/API/functionalities/scripted_process/main.c

Index: lldb/test/API/functionalities/scripted_process/main.c
===================================================================
--- /dev/null
+++ lldb/test/API/functionalities/scripted_process/main.c
@@ -0,0 +1,5 @@
+#include <stdlib.h>
+
+int main() {
+  return 0; // break here
+}
Index: lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py
===================================================================
--- /dev/null
+++ lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py
@@ -0,0 +1,45 @@
+"""
+Test python scripted process in lldb
+"""
+
+import os
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+from lldbsuite.test import lldbtest
+
+
+class PlatformProcessCrashInfoTestCase(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+
+    def setUp(self):
+        TestBase.setUp(self)
+        self.source = "main.c"
+
+    def tearDown(self):
+        TestBase.tearDown(self)
+
+    @skipUnlessDarwin
+    def test_python_plugin_package(self):
+        """Test that the lldb python module has a `plugins.scripted_process`
+        package."""
+        self.expect('script import lldb.plugins',
+                    patterns=["^((?!ModuleNotFoundError: No module named \'lldb\.plugins\').)*$"])
+
+        self.expect('script dir(lldb.plugins)',
+                    patterns=["scripted_process"])
+
+        self.expect('script import lldb.plugins.scripted_process',
+                    patterns=["^((?!ModuleNotFoundError: No module named \'lldb\.plugins\.scripted_process\').)*$"])
+
+        self.expect('script dir(lldb.plugins.scripted_process)',
+                    patterns=["ScriptedProcess"])
+
+        self.expect('script from lldb.plugins.scripted_process import ScriptedProcess',
+                    patterns=["^((?!ImportError: cannot import name \'ScriptedProcess\' from \'lldb.plugins\.scripted_process\').)*$"])
+
+        self.expect('script dir(ScriptedProcess)',
+                    patterns=["launch"])
Index: lldb/test/API/functionalities/scripted_process/Makefile
===================================================================
--- /dev/null
+++ lldb/test/API/functionalities/scripted_process/Makefile
@@ -0,0 +1,4 @@
+C_SOURCES := main.c
+
+include Makefile.rules
+
Index: lldb/examples/python/scripted_process/scripted_process.py
===================================================================
--- /dev/null
+++ lldb/examples/python/scripted_process/scripted_process.py
@@ -0,0 +1,206 @@
+from abc import ABCMeta, abstractmethod
+import six
+
+import lldb
+
+@six.add_metaclass(ABCMeta)
+class ScriptedProcess:
+
+    """
+    The base class for a scripted process.
+
+    Most of the base class methods are `@abstractmethod` that need to be
+    overwritten by the inheriting class.
+
+    Attributes:
+        args (lldb.SBStructuredData): Dictionary holding arbitrary values
+        loaded_images (list): List of the scripted process loaded images.
+        memory_regions (list): List of the current memory regions.
+        process_id (int): Scripted process identifier.
+        stops (list): List of public stops.
+        target (lldb.SBTarget): Target launching the scripted process.
+        threads (list): List of the scripted process threads.
+
+    Methods:
+        get_num_memory_regions() -> int:
+            Get the number of memory regions for the scripted process.
+
+        get_memory_region_at_index(idx: int) -> lldb.SBMemoryRegionInfo:
+            Get the memory regions for the scripted process, at index idx.
+
+        get_num_threads() -> int:
+            Get the number of threads for the scripted process.
+
+        get_thread_at_index(idx: int) -> Dict:
+            Get the scripted process thread at index idx.
+
+        get_register_for_thread(tid:int) -> Dict:
+            Get the register context dictionary for a certain thread.
+
+        read_memory_at_address(addr:int) -> lldb.SBData:
+            Get a memory buffer from the scripted process at a certain address.
+
+        get_loaded_images() -> List:
+            Get the list of loaded images for the scripted process.
+
+        get_process_id() -> int:
+            Get the scripted process identifier.
+
+        launch() -> lldb.SBError:
+            Simulate the scripted process launch.
+
+        can_debug() -> bool:
+            Check if the scripted process can be debugged.
+
+        is_alive() -> bool:
+            Check if the scripted process is alive.
+    """
+
+    @abstractmethod
+    def __init__(self, target, args):
+        """ Construct a scripted process.
+
+        Args:
+            target (lldb.SBTarget): The target launching the scripted process.
+            args (lldb.SBStructuredData): A Dictionary holding arbitrary
+                key/value pairs used by the scripted process.
+        """
+        self.process_id = 0
+        self.memory_regions = []
+        self.threads = []
+        self.loaded_images = []
+        self.stops = []
+        self.target = None
+        self.args = None
+        if isinstance(target, lldb.SBTarget) and target.IsValid():
+            self.target = target
+        if isinstance(args, lldb.SBStructuredData) and args.IsValid():
+            self.args = args
+
+    @abstractmethod
+    def get_num_memory_regions(self):
+        """ Get the number of memory regions for the scripted process.
+
+        Returns:
+            int: The number of memory regions in the scripted process.
+        """
+        pass
+
+    @abstractmethod
+    def get_memory_region_at_index(idx):
+        """ Get the memory regions for the scripted process, at index idx.
+
+        Args:
+            idx (int): Index of the memory region.
+
+        Returns:
+            lldb.SBMemoryRegionInfo: The memory region at index idx.
+                                    None if out of bounds.
+        """
+        pass
+
+    @abstractmethod
+    def get_num_threads(self):
+        """ Get the number of threads for the scripted process.
+
+        Returns:
+            int: The number of threads in the scripted process.
+        """
+        pass
+
+    @abstractmethod
+    def get_thread_at_index(idx):
+        """ Get the scripted process thread at index idx.
+
+        Args:
+            idx (int): Index of the thread.
+
+        Returns:
+            Dict: The thread at index idx, represented as a dictionary.
+                None if out of bounds.
+        """
+        pass
+
+    @abstractmethod
+    def get_register_for_thread(tid):
+        """ Get the register context dictionary for a certain thread of
+            the scripted process.
+
+        Args:
+            tid (int): Thread identifier.
+
+        Returns:
+            Dict: The register context represented as a dictionary, for the
+                tid thread. None if tid doesn't match any of the scripted
+                process threads.
+        """
+        pass
+
+    @abstractmethod
+    def read_memory_at_address(addr):
+        """ Get a memory buffer from the scripted process at a certain address.
+
+        Args:
+            addr (int): Address from which we should start reading.
+
+        Returns:
+            lldb.SBData: An `lldb.SBData` buffer with the target byte size and
+                byte order storing the memory read.
+        """
+        pass
+
+    @abstractmethod
+    def get_loaded_images(self):
+        """ Get the list of loaded images for the scripted process.
+
+        ```
+        class ScriptedProcessImage:
+            def __init__(name, file_spec, uuid, load_address):
+              self.name = name
+              self.file_spec = file_spec
+              self.uuid = uuid
+              self.load_address = load_address
+        ```
+
+        Returns:
+            List[ScriptedProcessImage]: A list of `ScriptedProcessImage`
+                containing for each entry, the name of the library, a UUID,
+                an `lldb.SBFileSpec` and a load address.
+                None if the list is empty.
+        """
+        pass
+
+    def get_process_id(self):
+        """ Get the scripted process identifier.
+
+        Returns:
+            int: The scripted process identifier.
+        """
+        return self.process_id
+
+
+    def launch(self) -> lldb.SBError:
+        """ Simulate the scripted process launch.
+
+        Returns:
+            lldb.SBError: An `lldb.SBError` with error code 0.
+        """
+        return lldb.SBError()
+
+    @abstractmethod
+    def can_debug(self) -> bool:
+        """ Check if the scripted process can be debugged.
+
+        Returns:
+            bool: True if scripted process can be debugged. False otherwise.
+        """
+        pass
+
+    @abstractmethod
+    def is_alive(self) -> bool:
+        """ Check if the scripted process is alive.
+
+        Returns:
+            bool: True if scripted process is alive. False otherwise.
+        """
+        pass
Index: lldb/examples/python/scripted_process/my_scripted_process.py
===================================================================
--- /dev/null
+++ lldb/examples/python/scripted_process/my_scripted_process.py
@@ -0,0 +1,45 @@
+import lldb
+from lldb.plugins.scripted_process import ScriptedProcess
+
+class MyScriptedProcess(ScriptedProcess):
+    def __init__(self, target: lldb.SBTarget, args : lldb.SBStructuredData):
+        super().__init__(target, args)
+
+    def get_num_memory_regions(self) -> int:
+        return len(self.memory_regions)
+
+    def get_memory_region_at_index(self, idx: int) -> lldb.SBMemoryRegionInfo:
+        return self.memory_regions[idx]
+
+    def get_num_threads(self) -> int:
+        return len(self.threads)
+
+    def get_thread_at_index(self, idx: int) -> lldb.SBThread:
+        return self.threads[idx]
+
+    def get_register_for_thread(self, tid: int):
+        return tid
+
+    def read_memory_at_address(self, addr: int) -> lldb.SBData:
+        data = lldb.SBData().CreateDataFromCString(
+                                    self.target.GetByteOrder(),
+                                    self.target.GetCodeByteSize(),
+                                    "Hello, world!")
+        return data
+
+    def get_loaded_images(self):
+        return self.loaded_images
+
+    def get_process_id(self) -> int:
+        return 42
+
+    def can_debug(self) -> bool:
+        return True
+
+    def is_alive(self) -> bool:
+        return True
+
+def __lldb_init_module(debugger, dict):
+    debugger.HandleCommand(
+        "process launch -C %s.%s" % (__name__,
+                                     MyScriptedProcess.__name__))
\ No newline at end of file
Index: lldb/bindings/python/CMakeLists.txt
===================================================================
--- lldb/bindings/python/CMakeLists.txt
+++ lldb/bindings/python/CMakeLists.txt
@@ -104,6 +104,13 @@
     FILES "${LLDB_SOURCE_DIR}/examples/python/in_call_stack.py"
           "${LLDB_SOURCE_DIR}/examples/python/symbolication.py")
 
+  create_python_package(
+    ${swig_target}
+    ${lldb_python_target_dir}
+    "plugins"
+    FILES
+    "${LLDB_SOURCE_DIR}/examples/python/scripted_process/scripted_process.py")
+
   if(APPLE)
     create_python_package(
       ${swig_target}
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to