-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.py
173 lines (135 loc) · 5.38 KB
/
queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from collections import namedtuple
import functools
class Iterator:
def __init__(self, head, field, first, next):
self._next = next
self._first = head[first]
self._head = head
self._field = field
self._current = None
def _is_empty(self, entry):
if entry == self._head.address:
return True
if int(str(entry), 16) == 0:
return True
return False
def _deref(self, ptr, field):
entry = functools.reduce(lambda e, f: e[f], self._field.split('.'),
ptr.dereference())[field]
return None if self._is_empty(entry) else entry
def __iter__(self):
self._current = None if self._is_empty(self._first) else self._first
return self
def __next__(self):
try:
if self._current is None:
raise StopIteration
current = self._current
self._current = self._deref(current, self._next)
return current
except gdb.MemoryError:
raise StopIteration
class TreeIterator(Iterator):
def __init__(self, head, field, root, left, right):
super().__init__(head, field, root, None)
self._left = left
self._right = right
def _left_node(self, node):
return self._deref(node, self._left)
def _right_node(self, node):
return self._deref(node, self._right)
def _next_nodes(self, current):
if current is None or self._is_empty(current):
return None
yield from self._next_nodes(self._left_node(current))
yield current
yield from self._next_nodes(self._right_node(current))
def __iter__(self):
return self.__next__()
def __next__(self):
yield from self._next_nodes(self._first)
def make_iter(head, field):
Container = namedtuple('Container', ['type', 'head', 'params'])
containers = [
Container(Iterator, 'lh_first', ['le_next']),
Container(Iterator, 'slh_first', ['sle_next']),
Container(Iterator, 'stqh_first', ['stqe_next']),
Container(Iterator, 'sqh_first', ['sqe_next']),
Container(Iterator, 'tqh_first', ['tqe_next']),
Container(Iterator, 'cqh_first', ['cqe_next']),
Container(TreeIterator, 'sph_root', ['spe_left', 'spe_right']),
Container(TreeIterator, 'rbh_root', ['rbe_left', 'rbe_right'])]
headobj = gdb.parse_and_eval(head)
for c in containers:
try:
if headobj[c.head] is not None:
return c.type(headobj, field, c.head, *c.params)
except gdb.error as err:
pass
raise ValueError('Unknown container type')
def print_result(value, expr=None):
if type(value) is gdb.Value:
dereference = '*' if value.type.code == gdb.TYPE_CODE_PTR else ''
expr = f'.{expr}' if expr is not None else ''
cmd = 'print ({}({}){}){}'.format(dereference, value.type, value, expr)
else:
if expr is not None:
raise ValueError(f'Invalid expression "{expr}" on object '
f'of type {type(value)}')
cmd = 'print {}'.format(value)
gdb.execute(cmd)
class SizeCommand(gdb.Command):
def __init__(self):
gdb.Command.__init__(self, 'queue-size', gdb.COMMAND_DATA,
gdb.COMPLETE_SYMBOL, True)
def invoke(self, arg, from_tty):
arg_list = gdb.string_to_argv(arg)
if len(arg_list) != 2:
print('usage: queue-size HEAD FIELD')
return
head, field = arg_list
iter = make_iter(head, field)
print_result(functools.reduce(lambda v, _: v + 1, iter, 0))
class AtCommand(gdb.Command):
def __init__(self):
gdb.Command.__init__(self, 'queue-at', gdb.COMMAND_DATA,
gdb.COMPLETE_SYMBOL, True)
def invoke(self, arg, from_tty):
arg_list = gdb.string_to_argv(arg)
if len(arg_list) != 3:
print('usage: queue-at HEAD FIELD INDEX')
return
head, field, index = arg_list
index = int(str(gdb.parse_and_eval(index)), 10)
for i, entry in enumerate(make_iter(head, field)):
if i == index:
print_result(entry)
class ForeachCommand(gdb.Command):
def __init__(self):
gdb.Command.__init__(self, 'queue-foreach', gdb.COMMAND_DATA,
gdb.COMPLETE_SYMBOL, True)
def invoke(self, arg, from_tty):
arg_list = gdb.string_to_argv(arg)
if len(arg_list) not in (2, 3):
print('usage: queue-foreach HEAD FIELD [EXPR]')
return
head, field, expr, *_ = [*arg_list, None]
for entry in make_iter(head, field):
print_result(entry, expr)
SizeCommand()
AtCommand()
ForeachCommand()