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