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