1from abc import ABCMeta, abstractmethod
2import six
3
4import lldb
5
6@six.add_metaclass(ABCMeta)
7class ScriptedProcess:
8
9    """
10    The base class for a scripted process.
11
12    Most of the base class methods are `@abstractmethod` that need to be
13    overwritten by the inheriting class.
14
15    DISCLAIMER: THIS INTERFACE IS STILL UNDER DEVELOPMENT AND NOT STABLE.
16                THE METHODS EXPOSED MIGHT CHANGE IN THE FUTURE.
17    """
18
19    memory_regions = None
20    stack_memory_dump = None
21    loaded_images = None
22    threads = {}
23
24    @abstractmethod
25    def __init__(self, target, args):
26        """ Construct a scripted process.
27
28        Args:
29            target (lldb.SBTarget): The target launching the scripted process.
30            args (lldb.SBStructuredData): A Dictionary holding arbitrary
31                key/value pairs used by the scripted process.
32        """
33        self.target = None
34        self.args = None
35        self.arch = None
36        if isinstance(target, lldb.SBTarget) and target.IsValid():
37            self.target = target
38            triple = self.target.triple
39            if triple:
40                self.arch = triple.split('-')[0]
41        if isinstance(args, lldb.SBStructuredData) and args.IsValid():
42            self.args = args
43
44    @abstractmethod
45    def get_memory_region_containing_address(self, addr):
46        """ Get the memory region for the scripted process, containing a
47            specific address.
48
49        Args:
50            addr (int): Address to look for in the scripted process memory
51                regions.
52
53        Returns:
54            lldb.SBMemoryRegionInfo: The memory region containing the address.
55                None if out of bounds.
56        """
57        pass
58
59    def get_threads_info(self):
60        """ Get the dictionary describing the process' Scripted Threads.
61
62        Returns:
63            Dict: The dictionary of threads, with the thread ID as the key and
64            a Scripted Thread instance as the value.
65            The dictionary can be empty.
66        """
67        return self.threads
68
69    @abstractmethod
70    def get_thread_with_id(self, tid):
71        """ Get the scripted process thread with a specific ID.
72
73        Args:
74            tid (int): Thread ID to look for in the scripted process.
75
76        Returns:
77            Dict: The thread represented as a dictionary, with the
78                tid thread ID. None if tid doesn't match any of the scripted
79                process threads.
80        """
81        pass
82
83    @abstractmethod
84    def get_registers_for_thread(self, tid):
85        """ Get the register context dictionary for a certain thread of
86            the scripted process.
87
88        Args:
89            tid (int): Thread ID for the thread's register context.
90
91        Returns:
92            Dict: The register context represented as a dictionary, for the
93                tid thread. None if tid doesn't match any of the scripted
94                process threads.
95        """
96        pass
97
98    @abstractmethod
99    def read_memory_at_address(self, addr, size):
100        """ Get a memory buffer from the scripted process at a certain address,
101            of a certain size.
102
103        Args:
104            addr (int): Address from which we should start reading.
105            size (int): Size of the memory to read.
106
107        Returns:
108            lldb.SBData: An `lldb.SBData` buffer with the target byte size and
109                byte order storing the memory read.
110        """
111        pass
112
113    def get_loaded_images(self):
114        """ Get the list of loaded images for the scripted process.
115
116        ```
117        class ScriptedProcessImage:
118            def __init__(name, file_spec, uuid, load_address):
119              self.name = name
120              self.file_spec = file_spec
121              self.uuid = uuid
122              self.load_address = load_address
123        ```
124
125        Returns:
126            List[ScriptedProcessImage]: A list of `ScriptedProcessImage`
127                containing for each entry, the name of the library, a UUID,
128                an `lldb.SBFileSpec` and a load address.
129                None if the list is empty.
130        """
131        return self.loaded_images
132
133    def get_process_id(self):
134        """ Get the scripted process identifier.
135
136        Returns:
137            int: The scripted process identifier.
138        """
139        return 0
140
141
142    def launch(self):
143        """ Simulate the scripted process launch.
144
145        Returns:
146            lldb.SBError: An `lldb.SBError` with error code 0.
147        """
148        return lldb.SBError()
149
150    def resume(self):
151        """ Simulate the scripted process resume.
152
153        Returns:
154            lldb.SBError: An `lldb.SBError` with error code 0.
155        """
156        return lldb.SBError()
157
158    @abstractmethod
159    def should_stop(self):
160        """ Check if the scripted process plugin should produce the stop event.
161
162        Returns:
163            bool: True if scripted process should broadcast a stop event.
164                  False otherwise.
165        """
166        pass
167
168    def stop(self):
169        """ Trigger the scripted process stop.
170
171        Returns:
172            lldb.SBError: An `lldb.SBError` with error code 0.
173        """
174        return lldb.SBError()
175
176    @abstractmethod
177    def is_alive(self):
178        """ Check if the scripted process is alive.
179
180        Returns:
181            bool: True if scripted process is alive. False otherwise.
182        """
183        pass
184
185    @abstractmethod
186    def get_scripted_thread_plugin(self):
187        """ Get scripted thread plugin name.
188
189        Returns:
190            str: Name of the scripted thread plugin.
191        """
192        return None
193
194@six.add_metaclass(ABCMeta)
195class ScriptedThread:
196
197    """
198    The base class for a scripted thread.
199
200    Most of the base class methods are `@abstractmethod` that need to be
201    overwritten by the inheriting class.
202
203    DISCLAIMER: THIS INTERFACE IS STILL UNDER DEVELOPMENT AND NOT STABLE.
204                THE METHODS EXPOSED MIGHT CHANGE IN THE FUTURE.
205    """
206
207    @abstractmethod
208    def __init__(self, scripted_process, args):
209        """ Construct a scripted thread.
210
211        Args:
212            process (ScriptedProcess): The scripted process owning this thread.
213            args (lldb.SBStructuredData): A Dictionary holding arbitrary
214                key/value pairs used by the scripted thread.
215        """
216        self.target = None
217        self.scripted_process = None
218        self.process = None
219        self.args = None
220        if isinstance(scripted_process, ScriptedProcess):
221            self.target = scripted_process.target
222            self.scripted_process = scripted_process
223            self.process = self.target.GetProcess()
224
225        self.id = None
226        self.idx = None
227        self.name = None
228        self.queue = None
229        self.state = None
230        self.stop_reason = None
231        self.register_info = None
232        self.register_ctx = {}
233        self.frames = []
234
235    @abstractmethod
236    def get_thread_id(self):
237        """ Get the scripted thread identifier.
238
239        Returns:
240            int: The identifier of the scripted thread.
241        """
242        pass
243
244    @abstractmethod
245    def get_name(self):
246        """ Get the scripted thread name.
247
248        Returns:
249            str: The name of the scripted thread.
250        """
251        pass
252
253    def get_state(self):
254        """ Get the scripted thread state type.
255
256            eStateStopped,   ///< Process or thread is stopped and can be examined.
257            eStateRunning,   ///< Process or thread is running and can't be examined.
258            eStateStepping,  ///< Process or thread is in the process of stepping and can
259                             /// not be examined.
260
261        Returns:
262            int: The state type of the scripted thread.
263                 Returns lldb.eStateStopped by default.
264        """
265        return lldb.eStateStopped
266
267    def get_queue(self):
268        """ Get the scripted thread associated queue name.
269            This method is optional.
270
271        Returns:
272            str: The queue name associated with the scripted thread.
273        """
274        pass
275
276    @abstractmethod
277    def get_stop_reason(self):
278        """ Get the dictionary describing the stop reason type with some data.
279            This method is optional.
280
281        Returns:
282            Dict: The dictionary holding the stop reason type and the possibly
283            the stop reason data.
284        """
285        pass
286
287    def get_stackframes(self):
288        """ Get the list of stack frames for the scripted thread.
289
290        ```
291        class ScriptedStackFrame:
292            def __init__(idx, cfa, pc, symbol_ctx):
293                self.idx = idx
294                self.cfa = cfa
295                self.pc = pc
296                self.symbol_ctx = symbol_ctx
297        ```
298
299        Returns:
300            List[ScriptedFrame]: A list of `ScriptedStackFrame`
301                containing for each entry, the frame index, the canonical
302                frame address, the program counter value for that frame
303                and a symbol context.
304                None if the list is empty.
305        """
306        return 0
307
308    def get_register_info(self):
309        if self.register_info is None:
310            self.register_info = dict()
311            if self.scripted_process.arch == 'x86_64':
312                self.register_info['sets'] = ['General Purpose Registers']
313                self.register_info['registers'] = INTEL64_GPR
314            elif 'arm64' in self.scripted_process.arch:
315                self.register_info['sets'] = ['General Purpose Registers']
316                self.register_info['registers'] = ARM64_GPR
317            else: raise ValueError('Unknown architecture', self.scripted_process.arch)
318        return self.register_info
319
320    @abstractmethod
321    def get_register_context(self):
322        """ Get the scripted thread register context
323
324        Returns:
325            str: A byte representing all register's value.
326        """
327        pass
328
329ARM64_GPR = [ {'name': 'x0',   'bitsize': 64, 'offset': 0,   'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 0,  'dwarf': 0,  'generic': 'arg0', 'alt-name': 'arg0'},
330              {'name': 'x1',   'bitsize': 64, 'offset': 8,   'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 1,  'dwarf': 1,  'generic': 'arg1', 'alt-name': 'arg1'},
331              {'name': 'x2',   'bitsize': 64, 'offset': 16,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 2,  'dwarf': 2,  'generic': 'arg2', 'alt-name': 'arg2'},
332              {'name': 'x3',   'bitsize': 64, 'offset': 24,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 3,  'dwarf': 3,  'generic': 'arg3', 'alt-name': 'arg3'},
333              {'name': 'x4',   'bitsize': 64, 'offset': 32,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 4,  'dwarf': 4,  'generic': 'arg4', 'alt-name': 'arg4'},
334              {'name': 'x5',   'bitsize': 64, 'offset': 40,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 5,  'dwarf': 5,  'generic': 'arg5', 'alt-name': 'arg5'},
335              {'name': 'x6',   'bitsize': 64, 'offset': 48,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 6,  'dwarf': 6,  'generic': 'arg6', 'alt-name': 'arg6'},
336              {'name': 'x7',   'bitsize': 64, 'offset': 56,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 7,  'dwarf': 7,  'generic': 'arg7', 'alt-name': 'arg7'},
337              {'name': 'x8',   'bitsize': 64, 'offset': 64,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 8,  'dwarf': 8 },
338              {'name': 'x9',   'bitsize': 64, 'offset': 72,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 9,  'dwarf': 9 },
339              {'name': 'x10',  'bitsize': 64, 'offset': 80,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 10, 'dwarf': 10},
340              {'name': 'x11',  'bitsize': 64, 'offset': 88,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 11, 'dwarf': 11},
341              {'name': 'x12',  'bitsize': 64, 'offset': 96,  'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 12, 'dwarf': 12},
342              {'name': 'x13',  'bitsize': 64, 'offset': 104, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 13, 'dwarf': 13},
343              {'name': 'x14',  'bitsize': 64, 'offset': 112, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 14, 'dwarf': 14},
344              {'name': 'x15',  'bitsize': 64, 'offset': 120, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 15, 'dwarf': 15},
345              {'name': 'x16',  'bitsize': 64, 'offset': 128, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 16, 'dwarf': 16},
346              {'name': 'x17',  'bitsize': 64, 'offset': 136, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 17, 'dwarf': 17},
347              {'name': 'x18',  'bitsize': 64, 'offset': 144, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 18, 'dwarf': 18},
348              {'name': 'x19',  'bitsize': 64, 'offset': 152, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 19, 'dwarf': 19},
349              {'name': 'x20',  'bitsize': 64, 'offset': 160, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 20, 'dwarf': 20},
350              {'name': 'x21',  'bitsize': 64, 'offset': 168, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 21, 'dwarf': 21},
351              {'name': 'x22',  'bitsize': 64, 'offset': 176, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 22, 'dwarf': 22},
352              {'name': 'x23',  'bitsize': 64, 'offset': 184, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 23, 'dwarf': 23},
353              {'name': 'x24',  'bitsize': 64, 'offset': 192, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 24, 'dwarf': 24},
354              {'name': 'x25',  'bitsize': 64, 'offset': 200, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 25, 'dwarf': 25},
355              {'name': 'x26',  'bitsize': 64, 'offset': 208, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 26, 'dwarf': 26},
356              {'name': 'x27',  'bitsize': 64, 'offset': 216, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 27, 'dwarf': 27},
357              {'name': 'x28',  'bitsize': 64, 'offset': 224, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 28, 'dwarf': 28},
358              {'name': 'x29',  'bitsize': 64, 'offset': 232, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 29, 'dwarf': 29, 'generic': 'fp', 'alt-name': 'fp'},
359              {'name': 'x30',  'bitsize': 64, 'offset': 240, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 30, 'dwarf': 30, 'generic': 'lr', 'alt-name': 'lr'},
360              {'name': 'sp',   'bitsize': 64, 'offset': 248, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 31, 'dwarf': 31, 'generic': 'sp', 'alt-name': 'sp'},
361              {'name': 'pc',   'bitsize': 64, 'offset': 256, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 32, 'dwarf': 32, 'generic': 'pc', 'alt-name': 'pc'},
362              {'name': 'cpsr', 'bitsize': 32, 'offset': 264, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 33, 'dwarf': 33}
363            ]
364
365INTEL64_GPR = [ {'name': 'rax', 'bitsize': 64, 'offset': 0, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 0, 'dwarf': 0},
366                {'name': 'rbx', 'bitsize': 64, 'offset': 8, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 3, 'dwarf': 3},
367                {'name': 'rcx', 'bitsize': 64, 'offset': 16, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 2, 'dwarf': 2, 'generic': 'arg4', 'alt-name': 'arg4'},
368                {'name': 'rdx', 'bitsize': 64, 'offset': 24, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 1, 'dwarf': 1, 'generic': 'arg3', 'alt-name': 'arg3'},
369                {'name': 'rdi', 'bitsize': 64, 'offset': 32, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 5, 'dwarf': 5, 'generic': 'arg1', 'alt-name': 'arg1'},
370                {'name': 'rsi', 'bitsize': 64, 'offset': 40, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 4, 'dwarf': 4, 'generic': 'arg2', 'alt-name': 'arg2'},
371                {'name': 'rbp', 'bitsize': 64, 'offset': 48, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 6, 'dwarf': 6, 'generic': 'fp', 'alt-name': 'fp'},
372                {'name': 'rsp', 'bitsize': 64, 'offset': 56, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 7, 'dwarf': 7, 'generic': 'sp', 'alt-name': 'sp'},
373                {'name': 'r8', 'bitsize': 64, 'offset': 64, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 8, 'dwarf': 8, 'generic': 'arg5', 'alt-name': 'arg5'},
374                {'name': 'r9', 'bitsize': 64, 'offset': 72, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 9, 'dwarf': 9, 'generic': 'arg6', 'alt-name': 'arg6'},
375                {'name': 'r10', 'bitsize': 64, 'offset': 80, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 10, 'dwarf': 10},
376                {'name': 'r11', 'bitsize': 64, 'offset': 88, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 11, 'dwarf': 11},
377                {'name': 'r12', 'bitsize': 64, 'offset': 96, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 12, 'dwarf': 12},
378                {'name': 'r13', 'bitsize': 64, 'offset': 104, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 13, 'dwarf': 13},
379                {'name': 'r14', 'bitsize': 64, 'offset': 112, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 14, 'dwarf': 14},
380                {'name': 'r15', 'bitsize': 64, 'offset': 120, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 15, 'dwarf': 15},
381                {'name': 'rip', 'bitsize': 64, 'offset': 128, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 16, 'dwarf': 16, 'generic': 'pc', 'alt-name': 'pc'},
382                {'name': 'rflags', 'bitsize': 64, 'offset': 136, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'generic': 'flags', 'alt-name': 'flags'},
383                {'name': 'cs', 'bitsize': 64, 'offset': 144, 'encoding': 'uint', 'format': 'hex', 'set': 0},
384                {'name': 'fs', 'bitsize': 64, 'offset': 152, 'encoding': 'uint', 'format': 'hex', 'set': 0},
385                {'name': 'gs', 'bitsize': 64, 'offset': 160, 'encoding': 'uint', 'format': 'hex', 'set': 0}
386              ]
387
388
389