2 # gdb helper commands and functions for Linux kernel debugging
6 # Copyright (c) Siemens AG, 2011-2013
11 # This work is licensed under the terms of the GNU GPL version 2.
18 def __init__(self, name):
22 def _new_objfile_handler(self, event):
24 gdb.events.new_objfile.disconnect(self._new_objfile_handler)
27 if self._type is None:
28 self._type = gdb.lookup_type(self._name)
29 if self._type is None:
31 "cannot resolve type '{0}'".format(self._name))
32 if hasattr(gdb, 'events') and hasattr(gdb.events, 'new_objfile'):
33 gdb.events.new_objfile.connect(self._new_objfile_handler)
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")
41 size_t_type = CachedType("size_t")
42 struct_page_type = CachedType("struct page")
46 return uint_type.get_type()
49 global struct_page_type
50 return struct_page_type.get_type()
54 return long_type.get_type()
58 return ulong_type.get_type()
60 def get_size_t_type():
62 return size_t_type.get_type()
64 def offset_of(typeobj, field):
65 element = gdb.Value(0).cast(typeobj)
66 return int(str(element[field].address).split()[0], 16)
69 def container_of(ptr, typeobj, member):
70 return (ptr.cast(get_long_type()) -
71 offset_of(typeobj, member)).cast(typeobj)
74 class ContainerOf(gdb.Function):
75 """Return pointer to containing data structure.
77 $container_of(PTR, "TYPE", "ELEMENT"): Given PTR, return a pointer to the
78 data structure of the type TYPE in which PTR is the address of ELEMENT.
79 Note that TYPE and ELEMENT have to be quoted as strings."""
82 super(ContainerOf, self).__init__("container_of")
84 def invoke(self, ptr, typename, elementname):
85 return container_of(ptr, gdb.lookup_type(typename.string()).pointer(),
94 target_endianness = None
97 def get_target_endianness():
98 global target_endianness
99 if target_endianness is None:
100 endian = gdb.execute("show endian", to_string=True)
101 if "little endian" in endian:
102 target_endianness = LITTLE_ENDIAN
103 elif "big endian" in endian:
104 target_endianness = BIG_ENDIAN
106 raise gdb.GdbError("unknown endianness '{0}'".format(str(endian)))
107 return target_endianness
110 def read_memoryview(inf, start, length):
111 m = inf.read_memory(start, length)
112 if type(m) is memoryview:
117 def read_u16(buffer, offset):
118 buffer_val = buffer[offset:offset + 2]
121 if type(buffer_val[0]) is str:
122 value[0] = ord(buffer_val[0])
123 value[1] = ord(buffer_val[1])
125 value[0] = buffer_val[0]
126 value[1] = buffer_val[1]
128 if get_target_endianness() == LITTLE_ENDIAN:
129 return value[0] + (value[1] << 8)
131 return value[1] + (value[0] << 8)
134 def read_u32(buffer, offset):
135 if get_target_endianness() == LITTLE_ENDIAN:
136 return read_u16(buffer, offset) + (read_u16(buffer, offset + 2) << 16)
138 return read_u16(buffer, offset + 2) + (read_u16(buffer, offset) << 16)
141 def read_u64(buffer, offset):
142 if get_target_endianness() == LITTLE_ENDIAN:
143 return read_u32(buffer, offset) + (read_u32(buffer, offset + 4) << 32)
145 return read_u32(buffer, offset + 4) + (read_u32(buffer, offset) << 32)
148 def read_ulong(buffer, offset):
149 if get_long_type().sizeof == 8:
150 return read_u64(buffer, offset)
152 return read_u32(buffer, offset)
154 atomic_long_counter_offset = atomic_long_type.get_type()['counter'].bitpos
155 atomic_long_counter_sizeof = atomic_long_type.get_type()['counter'].type.sizeof
157 def read_atomic_long(buffer, offset):
158 global atomic_long_counter_offset
159 global atomic_long_counter_sizeof
161 if atomic_long_counter_sizeof == 8:
162 return read_u64(buffer, offset + atomic_long_counter_offset)
164 return read_u32(buffer, offset + atomic_long_counter_offset)
169 def is_target_arch(arch):
170 if hasattr(gdb.Frame, 'architecture'):
171 return arch in gdb.newest_frame().architecture().name()
174 if target_arch is None:
175 target_arch = gdb.execute("show architecture", to_string=True)
176 return arch in target_arch
181 gdbserver_type = None
184 def get_gdbserver_type():
185 def exit_handler(event):
186 global gdbserver_type
187 gdbserver_type = None
188 gdb.events.exited.disconnect(exit_handler)
192 return gdb.execute("monitor info version", to_string=True) != ""
198 thread_info = gdb.execute("info thread 2", to_string=True)
199 return "shadowCPU0" in thread_info
203 global gdbserver_type
204 if gdbserver_type is None:
206 gdbserver_type = GDBSERVER_QEMU
208 gdbserver_type = GDBSERVER_KGDB
209 if gdbserver_type is not None and hasattr(gdb, 'events'):
210 gdb.events.exited.connect(exit_handler)
211 return gdbserver_type
214 def gdb_eval_or_none(expresssion):
216 return gdb.parse_and_eval(expresssion)