1#!/usr/bin/env python 2 3import lldb 4import struct 5 6 7class OperatingSystemPlugIn(object): 8 """Class that provides data for an instance of a LLDB 'OperatingSystemPython' plug-in class""" 9 10 def __init__(self, process): 11 '''Initialization needs a valid.SBProcess object. 12 13 This plug-in will get created after a live process is valid and has stopped for the 14 first time.''' 15 self.process = None 16 self.registers = None 17 self.threads = None 18 if isinstance(process, lldb.SBProcess) and process.IsValid(): 19 self.process = process 20 self.threads = None # Will be an dictionary containing info for each thread 21 22 def get_target(self): 23 # NOTE: Don't use "lldb.target" when trying to get your target as the "lldb.target" 24 # tracks the current target in the LLDB command interpreter which isn't the 25 # correct thing to use for this plug-in. 26 return self.process.target 27 28 def create_thread(self, tid, context): 29 if tid == 0x444444444: 30 thread_info = { 31 'tid': tid, 32 'name': 'four', 33 'queue': 'queue4', 34 'state': 'stopped', 35 'stop_reason': 'none'} 36 self.threads.append(thread_info) 37 return thread_info 38 return None 39 40 def get_thread_info(self): 41 if not self.threads: 42 # The sample dictionary below shows the values that can be returned for a thread 43 # tid => thread ID (mandatory) 44 # name => thread name (optional key/value pair) 45 # queue => thread dispatch queue name (optional key/value pair) 46 # state => thred state (mandatory, set to 'stopped' for now) 47 # stop_reason => thread stop reason. (mandatory, usually set to 'none') 48 # Possible values include: 49 # 'breakpoint' if the thread is stopped at a breakpoint 50 # 'none' thread is just stopped because the process is stopped 51 # 'trace' the thread just single stepped 52 # The usual value for this while threads are in memory is 'none' 53 # register_data_addr => the address of the register data in memory (optional key/value pair) 54 # Specifying this key/value pair for a thread will avoid a call to get_register_data() 55 # and can be used when your registers are in a thread context structure that is contiguous 56 # in memory. Don't specify this if your register layout in memory doesn't match the layout 57 # described by the dictionary returned from a call to the 58 # get_register_info() method. 59 self.threads = [ 60 {'tid': 0x111111111, 'core': 0} 61 ] 62 return self.threads 63 64 def get_register_info(self): 65 if self.registers is None: 66 self.registers = dict() 67 self.registers['sets'] = ['GPR'] 68 self.registers['registers'] = [ 69 {'name': 'rax', 'bitsize': 64, 'offset': 0, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 0, 'dwarf': 0}, 70 {'name': 'rbx', 'bitsize': 64, 'offset': 8, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 3, 'dwarf': 3}, 71 {'name': 'rcx', 'bitsize': 64, 'offset': 16, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 2, 'dwarf': 2, 'generic': 'arg4', 'alt-name': 'arg4', }, 72 {'name': 'rdx', 'bitsize': 64, 'offset': 24, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 1, 'dwarf': 1, 'generic': 'arg3', 'alt-name': 'arg3', }, 73 {'name': 'rdi', 'bitsize': 64, 'offset': 32, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 5, 'dwarf': 5, 'generic': 'arg1', 'alt-name': 'arg1', }, 74 {'name': 'rsi', 'bitsize': 64, 'offset': 40, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 4, 'dwarf': 4, 'generic': 'arg2', 'alt-name': 'arg2', }, 75 {'name': 'rbp', 'bitsize': 64, 'offset': 48, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 6, 'dwarf': 6, 'generic': 'fp', 'alt-name': 'fp', }, 76 {'name': 'rsp', 'bitsize': 64, 'offset': 56, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 7, 'dwarf': 7, 'generic': 'sp', 'alt-name': 'sp', }, 77 {'name': 'r8', 'bitsize': 64, 'offset': 64, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 8, 'dwarf': 8, 'generic': 'arg5', 'alt-name': 'arg5', }, 78 {'name': 'r9', 'bitsize': 64, 'offset': 72, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 9, 'dwarf': 9, 'generic': 'arg6', 'alt-name': 'arg6', }, 79 {'name': 'r10', 'bitsize': 64, 'offset': 80, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 10, 'dwarf': 10}, 80 {'name': 'r11', 'bitsize': 64, 'offset': 88, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 11, 'dwarf': 11}, 81 {'name': 'r12', 'bitsize': 64, 'offset': 96, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 12, 'dwarf': 12}, 82 {'name': 'r13', 'bitsize': 64, 'offset': 104, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 13, 'dwarf': 13}, 83 {'name': 'r14', 'bitsize': 64, 'offset': 112, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 14, 'dwarf': 14}, 84 {'name': 'r15', 'bitsize': 64, 'offset': 120, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 15, 'dwarf': 15}, 85 {'name': 'rip', 'bitsize': 64, 'offset': 128, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'gcc': 16, 'dwarf': 16, 'generic': 'pc', 'alt-name': 'pc'}, 86 {'name': 'rflags', 'bitsize': 64, 'offset': 136, 'encoding': 'uint', 'format': 'hex', 'set': 0, 'generic': 'flags', 'alt-name': 'flags'}, 87 {'name': 'cs', 'bitsize': 64, 'offset': 144, 'encoding': 'uint', 'format': 'hex', 'set': 0}, 88 {'name': 'fs', 'bitsize': 64, 'offset': 152, 'encoding': 'uint', 'format': 'hex', 'set': 0}, 89 {'name': 'gs', 'bitsize': 64, 'offset': 160, 'encoding': 'uint', 'format': 'hex', 'set': 0}, 90 ] 91 return self.registers 92 93 def get_register_data(self, tid): 94 return struct.pack( 95 '21Q', 96 tid + 1, 97 tid + 2, 98 tid + 3, 99 tid + 4, 100 tid + 5, 101 tid + 6, 102 tid + 7, 103 tid + 8, 104 tid + 9, 105 tid + 10, 106 tid + 11, 107 tid + 12, 108 tid + 13, 109 tid + 14, 110 tid + 15, 111 tid + 16, 112 tid + 17, 113 tid + 18, 114 tid + 19, 115 tid + 20, 116 tid + 21) 117