2 # gdb helper commands and functions for Linux kernel debugging
6 # Copyright (c) Siemens AG, 2011-2013
9 # Jan Kiszka <jan.kiszka@siemens.com>
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")
42 return long_type.get_type()
45 def offset_of(typeobj, field):
46 element = gdb.Value(0).cast(typeobj)
47 return int(str(element[field].address).split()[0], 16)
50 def container_of(ptr, typeobj, member):
51 return (ptr.cast(get_long_type()) -
52 offset_of(typeobj, member)).cast(typeobj)
55 class ContainerOf(gdb.Function):
56 """Return pointer to containing data structure.
58 $container_of(PTR, "TYPE", "ELEMENT"): Given PTR, return a pointer to the
59 data structure of the type TYPE in which PTR is the address of ELEMENT.
60 Note that TYPE and ELEMENT have to be quoted as strings."""
63 super(ContainerOf, self).__init__("container_of")
65 def invoke(self, ptr, typename, elementname):
66 return container_of(ptr, gdb.lookup_type(typename.string()).pointer(),
75 target_endianness = None
78 def get_target_endianness():
79 global target_endianness
80 if target_endianness is None:
81 endian = gdb.execute("show endian", to_string=True)
82 if "little endian" in endian:
83 target_endianness = LITTLE_ENDIAN
84 elif "big endian" in endian:
85 target_endianness = BIG_ENDIAN
87 raise gdb.GdbError("unknown endianness '{0}'".format(str(endian)))
88 return target_endianness
91 def read_memoryview(inf, start, length):
92 m = inf.read_memory(start, length)
93 if type(m) is memoryview:
98 def read_u16(buffer, offset):
99 buffer_val = buffer[offset:offset + 2]
102 if type(buffer_val[0]) is str:
103 value[0] = ord(buffer_val[0])
104 value[1] = ord(buffer_val[1])
106 value[0] = buffer_val[0]
107 value[1] = buffer_val[1]
109 if get_target_endianness() == LITTLE_ENDIAN:
110 return value[0] + (value[1] << 8)
112 return value[1] + (value[0] << 8)
115 def read_u32(buffer, offset):
116 if get_target_endianness() == LITTLE_ENDIAN:
117 return read_u16(buffer, offset) + (read_u16(buffer, offset + 2) << 16)
119 return read_u16(buffer, offset + 2) + (read_u16(buffer, offset) << 16)
122 def read_u64(buffer, offset):
123 if get_target_endianness() == LITTLE_ENDIAN:
124 return read_u32(buffer, offset) + (read_u32(buffer, offset + 4) << 32)
126 return read_u32(buffer, offset + 4) + (read_u32(buffer, offset) << 32)
129 def read_ulong(buffer, offset):
130 if get_long_type().sizeof == 8:
131 return read_u64(buffer, offset)
133 return read_u32(buffer, offset)
139 def is_target_arch(arch):
140 if hasattr(gdb.Frame, 'architecture'):
141 return arch in gdb.newest_frame().architecture().name()
144 if target_arch is None:
145 target_arch = gdb.execute("show architecture", to_string=True)
146 return arch in target_arch
151 gdbserver_type = None
154 def get_gdbserver_type():
155 def exit_handler(event):
156 global gdbserver_type
157 gdbserver_type = None
158 gdb.events.exited.disconnect(exit_handler)
162 return gdb.execute("monitor info version", to_string=True) != ""
168 thread_info = gdb.execute("info thread 2", to_string=True)
169 return "shadowCPU0" in thread_info
173 global gdbserver_type
174 if gdbserver_type is None:
176 gdbserver_type = GDBSERVER_QEMU
178 gdbserver_type = GDBSERVER_KGDB
179 if gdbserver_type is not None and hasattr(gdb, 'events'):
180 gdb.events.exited.connect(exit_handler)
181 return gdbserver_type
184 def gdb_eval_or_none(expresssion):
186 return gdb.parse_and_eval(expresssion)
192 parent = d['d_parent']
193 if parent == d or parent == 0:
195 p = dentry_name(d['d_parent']) + "/"
196 return p + d['d_iname'].string()