blob: 55356b66f8ea59c90ef2af654df490c910cb7cc7 [file] [log] [blame]
Thiébaud Weksteen084f6b12015-06-30 14:58:07 -07001#
2# gdb helper commands and functions for Linux kernel debugging
3#
4# list tools
5#
6# Copyright (c) Thiebaud Weksteen, 2015
7#
8# Authors:
9# Thiebaud Weksteen <thiebaud@weksteen.fr>
10#
11# This work is licensed under the terms of the GNU GPL version 2.
12#
13
14import gdb
15
16from linux import utils
17
18list_head = utils.CachedType("struct list_head")
Leonard Crestez47d0d122019-05-14 15:46:05 -070019hlist_head = utils.CachedType("struct hlist_head")
20hlist_node = utils.CachedType("struct hlist_node")
Thiébaud Weksteen084f6b12015-06-30 14:58:07 -070021
22
Kieran Binghama84be612016-05-23 16:24:42 -070023def 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 gdb.GdbError("Must be struct list_head not {}"
28 .format(head.type))
29
30 node = head['next'].dereference()
31 while node.address != head.address:
32 yield node.address
33 node = node['next'].dereference()
34
35
36def list_for_each_entry(head, gdbtype, member):
37 for node in list_for_each(head):
38 if node.type != list_head.get_type().pointer():
39 raise TypeError("Type {} found. Expected struct list_head *."
40 .format(node.type))
41 yield utils.container_of(node, gdbtype, member)
42
43
Leonard Crestez47d0d122019-05-14 15:46:05 -070044def hlist_for_each(head):
45 if head.type == hlist_head.get_type().pointer():
46 head = head.dereference()
47 elif head.type != hlist_head.get_type():
48 raise gdb.GdbError("Must be struct hlist_head not {}"
49 .format(head.type))
50
51 node = head['first'].dereference()
52 while node.address:
53 yield node.address
54 node = node['next'].dereference()
55
56
57def hlist_for_each_entry(head, gdbtype, member):
58 for node in hlist_for_each(head):
59 if node.type != hlist_node.get_type().pointer():
60 raise TypeError("Type {} found. Expected struct hlist_head *."
61 .format(node.type))
62 yield utils.container_of(node, gdbtype, member)
63
64
Thiébaud Weksteen084f6b12015-06-30 14:58:07 -070065def list_check(head):
66 nb = 0
Jan Kiszka433296b2015-06-30 14:58:10 -070067 if (head.type == list_head.get_type().pointer()):
68 head = head.dereference()
69 elif (head.type != list_head.get_type()):
70 raise gdb.GdbError('argument must be of type (struct list_head [*])')
Thiébaud Weksteen084f6b12015-06-30 14:58:07 -070071 c = head
Thiébaud Weksteen084f6b12015-06-30 14:58:07 -070072 try:
73 gdb.write("Starting with: {}\n".format(c))
74 except gdb.MemoryError:
75 gdb.write('head is not accessible\n')
76 return
77 while True:
78 p = c['prev'].dereference()
79 n = c['next'].dereference()
80 try:
81 if p['next'] != c.address:
82 gdb.write('prev.next != current: '
83 'current@{current_addr}={current} '
84 'prev@{p_addr}={p}\n'.format(
85 current_addr=c.address,
86 current=c,
87 p_addr=p.address,
88 p=p,
89 ))
90 return
91 except gdb.MemoryError:
92 gdb.write('prev is not accessible: '
93 'current@{current_addr}={current}\n'.format(
94 current_addr=c.address,
95 current=c
96 ))
97 return
98 try:
99 if n['prev'] != c.address:
100 gdb.write('next.prev != current: '
101 'current@{current_addr}={current} '
102 'next@{n_addr}={n}\n'.format(
103 current_addr=c.address,
104 current=c,
105 n_addr=n.address,
106 n=n,
107 ))
108 return
109 except gdb.MemoryError:
110 gdb.write('next is not accessible: '
111 'current@{current_addr}={current}\n'.format(
112 current_addr=c.address,
113 current=c
114 ))
115 return
116 c = n
117 nb += 1
118 if c == head:
119 gdb.write("list is consistent: {} node(s)\n".format(nb))
120 return
121
122
123class LxListChk(gdb.Command):
124 """Verify a list consistency"""
125
126 def __init__(self):
Jan Kiszka3328bc92015-06-30 14:58:13 -0700127 super(LxListChk, self).__init__("lx-list-check", gdb.COMMAND_DATA,
128 gdb.COMPLETE_EXPRESSION)
Thiébaud Weksteen084f6b12015-06-30 14:58:07 -0700129
130 def invoke(self, arg, from_tty):
131 argv = gdb.string_to_argv(arg)
132 if len(argv) != 1:
133 raise gdb.GdbError("lx-list-check takes one argument")
134 list_check(gdb.parse_and_eval(argv[0]))
135
Stephen Boyd494dbe02019-05-14 15:46:02 -0700136
Thiébaud Weksteen084f6b12015-06-30 14:58:07 -0700137LxListChk()