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