2 # gdb helper commands and functions for Linux kernel debugging
6 # Copyright (c) Thiebaud Weksteen, 2015
11 # This work is licensed under the terms of the GNU GPL version 2.
16 from linux import utils
18 list_head = utils.CachedType("struct list_head")
19 hlist_head = utils.CachedType("struct hlist_head")
20 hlist_node = utils.CachedType("struct hlist_node")
23 def list_for_each(head):
24 if head.type == list_head.get_type().pointer():
25 head = head.dereference()
26 elif head.type != list_head.get_type():
27 raise TypeError("Must be struct list_head not {}"
30 node = head['next'].dereference()
31 while node.address != head.address:
33 node = node['next'].dereference()
36 def list_for_each_entry(head, gdbtype, member):
37 for node in list_for_each(head):
38 yield utils.container_of(node, gdbtype, member)
41 def hlist_for_each(head):
42 if head.type == hlist_head.get_type().pointer():
43 head = head.dereference()
44 elif head.type != hlist_head.get_type():
45 raise TypeError("Must be struct hlist_head not {}"
48 node = head['first'].dereference()
51 node = node['next'].dereference()
54 def hlist_for_each_entry(head, gdbtype, member):
55 for node in hlist_for_each(head):
56 yield utils.container_of(node, gdbtype, member)
61 if (head.type == list_head.get_type().pointer()):
62 head = head.dereference()
63 elif (head.type != list_head.get_type()):
64 raise gdb.GdbError('argument must be of type (struct list_head [*])')
67 gdb.write("Starting with: {}\n".format(c))
68 except gdb.MemoryError:
69 gdb.write('head is not accessible\n')
72 p = c['prev'].dereference()
73 n = c['next'].dereference()
75 if p['next'] != c.address:
76 gdb.write('prev.next != current: '
77 'current@{current_addr}={current} '
78 'prev@{p_addr}={p}\n'.format(
79 current_addr=c.address,
85 except gdb.MemoryError:
86 gdb.write('prev is not accessible: '
87 'current@{current_addr}={current}\n'.format(
88 current_addr=c.address,
93 if n['prev'] != c.address:
94 gdb.write('next.prev != current: '
95 'current@{current_addr}={current} '
96 'next@{n_addr}={n}\n'.format(
97 current_addr=c.address,
103 except gdb.MemoryError:
104 gdb.write('next is not accessible: '
105 'current@{current_addr}={current}\n'.format(
106 current_addr=c.address,
113 gdb.write("list is consistent: {} node(s)\n".format(nb))
117 class LxListChk(gdb.Command):
118 """Verify a list consistency"""
121 super(LxListChk, self).__init__("lx-list-check", gdb.COMMAND_DATA,
122 gdb.COMPLETE_EXPRESSION)
124 def invoke(self, arg, from_tty):
125 argv = gdb.string_to_argv(arg)
127 raise gdb.GdbError("lx-list-check takes one argument")
128 list_check(gdb.parse_and_eval(argv[0]))