1
2""" Module to abstract lazy evaluation of lldb.SBTarget
3    for kernel
4"""
5
6import lldb
7
8class LazyTarget(object):
9    """ A common object that lazy-evaluates and caches the lldb.SBTarget
10        and lldb.SBProcess for the current interactive debugging session.
11    """
12    _debugger = None # This holds an lldb.SBDebugger object for debugger state
13    _target   = None # This holds an lldb.SBTarget object for symbol lookup
14    _process  = None # This holds an lldb.SBProcess object for reading memory
15
16    @staticmethod
17    def Initialize(debugger):
18        """ Initialize the LazyTarget with an SBDebugger.
19        """
20        LazyTarget._debugger = debugger
21        LazyTarget._target = None
22        LazyTarget._process = None
23
24    @staticmethod
25    def GetTarget():
26        """ Get an SBTarget for the most recently selected
27            target, or throw an exception.
28        """
29        if not LazyTarget._target is None:
30            return LazyTarget._target
31
32        target = LazyTarget._debugger.GetSelectedTarget()
33        if target is None:
34            raise AttributeError('No target selected')
35
36        if not target.IsValid():
37            raise AttributeError('Target is not valid')
38
39        LazyTarget._target = target
40        return target
41
42    @staticmethod
43    def GetProcess():
44        """ Get an SBProcess for the most recently selected
45            target, or throw an exception.
46        """
47
48        target = LazyTarget.GetTarget()
49        process = target.process
50
51        if process is None:
52            raise AttributeError('Target does not have a process')
53
54        if not process.IsValid():
55            raise AttributeError('Process is not valid')
56
57        return process
58