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