1 # 1 # 2 # gdb helper commands and functions for Linux 2 # gdb helper commands and functions for Linux kernel debugging 3 # 3 # 4 # common utilities 4 # common utilities 5 # 5 # 6 # Copyright (c) Siemens AG, 2011-2013 6 # Copyright (c) Siemens AG, 2011-2013 7 # 7 # 8 # Authors: 8 # Authors: 9 # Jan Kiszka <jan.kiszka@siemens.com> 9 # Jan Kiszka <jan.kiszka@siemens.com> 10 # 10 # 11 # This work is licensed under the terms of the 11 # This work is licensed under the terms of the GNU GPL version 2. 12 # 12 # 13 13 14 import gdb 14 import gdb 15 15 16 16 17 class CachedType: 17 class CachedType: 18 def __init__(self, name): 18 def __init__(self, name): 19 self._type = None 19 self._type = None 20 self._name = name 20 self._name = name 21 21 22 def _new_objfile_handler(self, event): 22 def _new_objfile_handler(self, event): 23 self._type = None 23 self._type = None 24 gdb.events.new_objfile.disconnect(self 24 gdb.events.new_objfile.disconnect(self._new_objfile_handler) 25 25 26 def get_type(self): 26 def get_type(self): 27 if self._type is None: 27 if self._type is None: 28 self._type = gdb.lookup_type(self. 28 self._type = gdb.lookup_type(self._name) 29 if self._type is None: 29 if self._type is None: 30 raise gdb.GdbError( 30 raise gdb.GdbError( 31 "cannot resolve type '{0}' 31 "cannot resolve type '{0}'".format(self._name)) 32 if hasattr(gdb, 'events') and hasa 32 if hasattr(gdb, 'events') and hasattr(gdb.events, 'new_objfile'): 33 gdb.events.new_objfile.connect 33 gdb.events.new_objfile.connect(self._new_objfile_handler) 34 return self._type 34 return self._type 35 35 36 36 37 long_type = CachedType("long") 37 long_type = CachedType("long") 38 ulong_type = CachedType("unsigned long") << 39 uint_type = CachedType("unsigned int") << 40 atomic_long_type = CachedType("atomic_long_t") 38 atomic_long_type = CachedType("atomic_long_t") 41 size_t_type = CachedType("size_t") << 42 struct_page_type = CachedType("struct page") << 43 << 44 def get_uint_type(): << 45 global uint_type << 46 return uint_type.get_type() << 47 << 48 def get_page_type(): << 49 global struct_page_type << 50 return struct_page_type.get_type() << 51 39 52 def get_long_type(): 40 def get_long_type(): 53 global long_type 41 global long_type 54 return long_type.get_type() 42 return long_type.get_type() 55 43 56 def get_ulong_type(): << 57 global ulong_type << 58 return ulong_type.get_type() << 59 << 60 def get_size_t_type(): << 61 global size_t_type << 62 return size_t_type.get_type() << 63 << 64 def offset_of(typeobj, field): 44 def offset_of(typeobj, field): 65 element = gdb.Value(0).cast(typeobj) 45 element = gdb.Value(0).cast(typeobj) 66 return int(str(element[field].address).spl 46 return int(str(element[field].address).split()[0], 16) 67 47 68 48 69 def container_of(ptr, typeobj, member): 49 def container_of(ptr, typeobj, member): 70 return (ptr.cast(get_long_type()) - 50 return (ptr.cast(get_long_type()) - 71 offset_of(typeobj, member)).cast(t 51 offset_of(typeobj, member)).cast(typeobj) 72 52 73 53 74 class ContainerOf(gdb.Function): 54 class ContainerOf(gdb.Function): 75 """Return pointer to containing data struc 55 """Return pointer to containing data structure. 76 56 77 $container_of(PTR, "TYPE", "ELEMENT"): Given P 57 $container_of(PTR, "TYPE", "ELEMENT"): Given PTR, return a pointer to the 78 data structure of the type TYPE in which PTR i 58 data structure of the type TYPE in which PTR is the address of ELEMENT. 79 Note that TYPE and ELEMENT have to be quoted a 59 Note that TYPE and ELEMENT have to be quoted as strings.""" 80 60 81 def __init__(self): 61 def __init__(self): 82 super(ContainerOf, self).__init__("con 62 super(ContainerOf, self).__init__("container_of") 83 63 84 def invoke(self, ptr, typename, elementnam 64 def invoke(self, ptr, typename, elementname): 85 return container_of(ptr, gdb.lookup_ty 65 return container_of(ptr, gdb.lookup_type(typename.string()).pointer(), 86 elementname.string 66 elementname.string()) 87 67 88 68 89 ContainerOf() 69 ContainerOf() 90 70 91 71 92 BIG_ENDIAN = 0 72 BIG_ENDIAN = 0 93 LITTLE_ENDIAN = 1 73 LITTLE_ENDIAN = 1 94 target_endianness = None 74 target_endianness = None 95 75 96 76 97 def get_target_endianness(): 77 def get_target_endianness(): 98 global target_endianness 78 global target_endianness 99 if target_endianness is None: 79 if target_endianness is None: 100 endian = gdb.execute("show endian", to 80 endian = gdb.execute("show endian", to_string=True) 101 if "little endian" in endian: 81 if "little endian" in endian: 102 target_endianness = LITTLE_ENDIAN 82 target_endianness = LITTLE_ENDIAN 103 elif "big endian" in endian: 83 elif "big endian" in endian: 104 target_endianness = BIG_ENDIAN 84 target_endianness = BIG_ENDIAN 105 else: 85 else: 106 raise gdb.GdbError("unknown endian 86 raise gdb.GdbError("unknown endianness '{0}'".format(str(endian))) 107 return target_endianness 87 return target_endianness 108 88 109 89 110 def read_memoryview(inf, start, length): 90 def read_memoryview(inf, start, length): 111 m = inf.read_memory(start, length) !! 91 return memoryview(inf.read_memory(start, length)) 112 if type(m) is memoryview: << 113 return m << 114 return memoryview(m) << 115 92 116 93 117 def read_u16(buffer, offset): 94 def read_u16(buffer, offset): 118 buffer_val = buffer[offset:offset + 2] 95 buffer_val = buffer[offset:offset + 2] 119 value = [0, 0] 96 value = [0, 0] 120 97 121 if type(buffer_val[0]) is str: 98 if type(buffer_val[0]) is str: 122 value[0] = ord(buffer_val[0]) 99 value[0] = ord(buffer_val[0]) 123 value[1] = ord(buffer_val[1]) 100 value[1] = ord(buffer_val[1]) 124 else: 101 else: 125 value[0] = buffer_val[0] 102 value[0] = buffer_val[0] 126 value[1] = buffer_val[1] 103 value[1] = buffer_val[1] 127 104 128 if get_target_endianness() == LITTLE_ENDIA 105 if get_target_endianness() == LITTLE_ENDIAN: 129 return value[0] + (value[1] << 8) 106 return value[0] + (value[1] << 8) 130 else: 107 else: 131 return value[1] + (value[0] << 8) 108 return value[1] + (value[0] << 8) 132 109 133 110 134 def read_u32(buffer, offset): 111 def read_u32(buffer, offset): 135 if get_target_endianness() == LITTLE_ENDIA 112 if get_target_endianness() == LITTLE_ENDIAN: 136 return read_u16(buffer, offset) + (rea 113 return read_u16(buffer, offset) + (read_u16(buffer, offset + 2) << 16) 137 else: 114 else: 138 return read_u16(buffer, offset + 2) + 115 return read_u16(buffer, offset + 2) + (read_u16(buffer, offset) << 16) 139 116 140 117 141 def read_u64(buffer, offset): 118 def read_u64(buffer, offset): 142 if get_target_endianness() == LITTLE_ENDIA 119 if get_target_endianness() == LITTLE_ENDIAN: 143 return read_u32(buffer, offset) + (rea 120 return read_u32(buffer, offset) + (read_u32(buffer, offset + 4) << 32) 144 else: 121 else: 145 return read_u32(buffer, offset + 4) + 122 return read_u32(buffer, offset + 4) + (read_u32(buffer, offset) << 32) 146 123 147 124 148 def read_ulong(buffer, offset): 125 def read_ulong(buffer, offset): 149 if get_long_type().sizeof == 8: 126 if get_long_type().sizeof == 8: 150 return read_u64(buffer, offset) 127 return read_u64(buffer, offset) 151 else: 128 else: 152 return read_u32(buffer, offset) 129 return read_u32(buffer, offset) 153 130 154 atomic_long_counter_offset = atomic_long_type. 131 atomic_long_counter_offset = atomic_long_type.get_type()['counter'].bitpos 155 atomic_long_counter_sizeof = atomic_long_type. 132 atomic_long_counter_sizeof = atomic_long_type.get_type()['counter'].type.sizeof 156 133 157 def read_atomic_long(buffer, offset): 134 def read_atomic_long(buffer, offset): 158 global atomic_long_counter_offset 135 global atomic_long_counter_offset 159 global atomic_long_counter_sizeof 136 global atomic_long_counter_sizeof 160 137 161 if atomic_long_counter_sizeof == 8: 138 if atomic_long_counter_sizeof == 8: 162 return read_u64(buffer, offset + atomi 139 return read_u64(buffer, offset + atomic_long_counter_offset) 163 else: 140 else: 164 return read_u32(buffer, offset + atomi 141 return read_u32(buffer, offset + atomic_long_counter_offset) 165 142 166 target_arch = None 143 target_arch = None 167 144 168 145 169 def is_target_arch(arch): 146 def is_target_arch(arch): 170 if hasattr(gdb.Frame, 'architecture'): 147 if hasattr(gdb.Frame, 'architecture'): 171 return arch in gdb.newest_frame().arch 148 return arch in gdb.newest_frame().architecture().name() 172 else: 149 else: 173 global target_arch 150 global target_arch 174 if target_arch is None: 151 if target_arch is None: 175 target_arch = gdb.execute("show ar 152 target_arch = gdb.execute("show architecture", to_string=True) 176 return arch in target_arch 153 return arch in target_arch 177 154 178 155 179 GDBSERVER_QEMU = 0 156 GDBSERVER_QEMU = 0 180 GDBSERVER_KGDB = 1 157 GDBSERVER_KGDB = 1 181 gdbserver_type = None 158 gdbserver_type = None 182 159 183 160 184 def get_gdbserver_type(): 161 def get_gdbserver_type(): 185 def exit_handler(event): 162 def exit_handler(event): 186 global gdbserver_type 163 global gdbserver_type 187 gdbserver_type = None 164 gdbserver_type = None 188 gdb.events.exited.disconnect(exit_hand 165 gdb.events.exited.disconnect(exit_handler) 189 166 190 def probe_qemu(): 167 def probe_qemu(): 191 try: 168 try: 192 return gdb.execute("monitor info v 169 return gdb.execute("monitor info version", to_string=True) != "" 193 except gdb.error: 170 except gdb.error: 194 return False 171 return False 195 172 196 def probe_kgdb(): 173 def probe_kgdb(): 197 try: 174 try: 198 thread_info = gdb.execute("info th 175 thread_info = gdb.execute("info thread 2", to_string=True) 199 return "shadowCPU" in thread_info !! 176 return "shadowCPU0" in thread_info 200 except gdb.error: 177 except gdb.error: 201 return False 178 return False 202 179 203 global gdbserver_type 180 global gdbserver_type 204 if gdbserver_type is None: 181 if gdbserver_type is None: 205 if probe_qemu(): 182 if probe_qemu(): 206 gdbserver_type = GDBSERVER_QEMU 183 gdbserver_type = GDBSERVER_QEMU 207 elif probe_kgdb(): 184 elif probe_kgdb(): 208 gdbserver_type = GDBSERVER_KGDB 185 gdbserver_type = GDBSERVER_KGDB 209 if gdbserver_type is not None and hasa 186 if gdbserver_type is not None and hasattr(gdb, 'events'): 210 gdb.events.exited.connect(exit_han 187 gdb.events.exited.connect(exit_handler) 211 return gdbserver_type 188 return gdbserver_type 212 189 213 190 214 def gdb_eval_or_none(expresssion): 191 def gdb_eval_or_none(expresssion): 215 try: 192 try: 216 return gdb.parse_and_eval(expresssion) 193 return gdb.parse_and_eval(expresssion) 217 except gdb.error: 194 except gdb.error: 218 return None 195 return None >> 196 >> 197 >> 198 def dentry_name(d): >> 199 parent = d['d_parent'] >> 200 if parent == d or parent == 0: >> 201 return "" >> 202 p = dentry_name(d['d_parent']) + "/" >> 203 return p + d['d_iname'].string()
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.