]>
git.saurik.com Git - apple/xnu.git/blob - tools/lldbmacros/core/caching.py
2 A basic caching module for xnu debug macros to use.
3 It is recommended to use [Get|Save][Static|Dynamic]CacheData() apis for
4 your caching needs. These APIs will handle the case of clearing caches when
5 a debugger continues and stops or hit a breakpoint.
7 Use Static caches for data that will not change if the program is run and stopped again. e.g. typedata, version numbers etc.
8 An example invocation could be like
9 def getDSYMPathForUUID(uuid):
10 # Get the data from cache
11 cached_data = caching.GetStaticCacheData('dsym.for.uuid', {})
13 if uuid in cached_data:
14 return cached_data[uuid]
16 path = #get info for uuid
17 cached_data[uuid] = path
19 # save the cached_data object to cache.
20 caching.SaveStaticCacheData('dsym.for.uuid', cached_data)
22 return cached_data[uuid]
24 And use Dynamic caches for things like thread data, zones information etc.
25 These will automatically be dropped when debugger continues the target
26 An example use of Dynamic cache could be as follows
28 def GetExecutablePathForPid(pid):
29 # Get the data from cache
30 cached_data = caching.GetDynamicCacheData('exec_for_path', {})
32 if pid in cached_data:
33 return cached_data[pid]
35 exec_path = "/path/to/exec" #get exec path for pid
36 cached_data[pid] = path
38 # save the cached_data object to cache.
39 caching.SaveDynamicCacheData('exec_for_path', cached_data)
41 return cached_data[pid]
45 #Private Routines and objects
47 from configuration
import *
52 The format for the saved data dictionaries is
54 'key' : (valueobj, versno),
58 The versno is an int defining the version of obj. In case of version mismatch it will set valueobj to default upon access.
66 def _GetDebuggerSessionID():
67 """ A default callable function that _GetCurrentSessionID uses to
68 identify a stopped session.
72 def _GetCurrentSessionID():
73 """ Get the current session id. This will update whenever
74 system is continued or if there is new information that would
75 cause the dynamic cache to be deleted.
77 returns: int - session id number.
79 session_id
= _GetDebuggerSessionID()
86 """ Returns number of bytes held in cache.
88 int - size of cache including static and dynamic
90 global _static_data
, _dynamic_data
91 return sys
.getsizeof(_static_data
) + sys
.getsizeof(_dynamic_data
)
94 def GetStaticCacheData(key
, default_value
= None):
95 """ Get cached object based on key from the cache of static information.
97 key: str - a unique string identifying your data.
98 default_value : obj - an object that should be returned if key is not found.
100 default_value - if the static cache does not have your data.
101 obj - The data obj saved with SaveStaticCacheData()
105 if key
in _static_data
:
106 return _static_data
[key
][0]
109 def SaveStaticCacheData(key
, value
):
110 """ Save data into the cache identified by key.
111 It will overwrite any data that was previously associated with key
113 key : str - a unique string identifying your data
114 value: obj - any object that is to be cached.
120 if not config
['CacheStaticData']:
124 _static_data
[key
] = (value
, _GetCurrentSessionID())
128 def GetDynamicCacheData(key
, default_value
=None):
129 """ Get cached object based on key from the cache of dynamic information.
131 key: str - a unique string identifying cached object
132 default_value : obj - an object that should be returned if key is not found.
134 default_value - if dynamic cache does not have data or if the saved version mismatches with current session id.
135 obj - The data obj saved with SaveDynamicCacheData()
139 if key
in _dynamic_data
:
140 if _GetCurrentSessionID() == _dynamic_data
[key
][1]:
141 return _dynamic_data
[key
][0]
143 del _dynamic_data
[key
]
148 def SaveDynamicCacheData(key
, value
):
149 """ Save data into the cache identified by key.
150 It will overwrite any data that was previously associated with key
152 key : str - a unique string identifying your data
153 value: obj - any object that is to be cached.
159 if not config
['CacheDynamicData']:
163 _dynamic_data
[key
] = (value
, _GetCurrentSessionID())