summaryrefslogtreecommitdiffstats
path: root/scripts/gdb/linux/rbtree.py
blob: c4b99160791785e5b346b0a86e8cb4e28f182b2d (plain)
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
174
175
176
177
# SPDX-License-Identifier: GPL-2.0
#
# Copyright 2019 Google LLC.

import gdb

from linux import utils

rb_root_type = utils.CachedType("struct rb_root")
rb_node_type = utils.CachedType("struct rb_node")


def rb_first(root):
    if root.type == rb_root_type.get_type():
        node = root.address.cast(rb_root_type.get_type().pointer())
    elif root.type != rb_root_type.get_type().pointer():
        raise gdb.GdbError("Must be struct rb_root not {}".format(root.type))

    node = root['rb_node']
    if node is 0:
        return None

    while node['rb_left']:
        node = node['rb_left']

    return node


def rb_last(root):
    if root.type == rb_root_type.get_type():
        node = root.address.cast(rb_root_type.get_type().pointer())
    elif root.type != rb_root_type.get_type().pointer():
        raise gdb.GdbError("Must be struct rb_root not {}".format(root.type))

    node = root['rb_node']
    if node is 0:
        return None

    while node['rb_right']:
        node = node['rb_right']

    return node


def rb_parent(node):
    parent = gdb.Value(node['__rb_parent_color'] & ~3)
    return parent.cast(rb_node_type.get_type().pointer())


def rb_empty_node(node):
    return node['__rb_parent_color'] == node.address


def rb_next(node):
    if node.type == rb_node_type.get_type():
        node = node.address.cast(rb_node_type.get_type().pointer())
    elif node.type != rb_node_type.get_type().pointer():
        raise gdb.GdbError("Must be struct rb_node not {}".format(node.type))

    if rb_empty_node(node):
        return None

    if node['rb_right']:
        node = node['rb_right']
        while node['rb_left']:
            node = node['rb_left']
        return node

    parent = rb_parent(node)
    while parent and node == parent['rb_right']:
            node = parent
            parent = rb_parent(node)

    return parent


def rb_prev(node):
    if node.type == rb_node_type.get_type():
        node = node.address.cast(rb_node_type.get_type().pointer())
    elif node.type != rb_node_type.get_type().pointer():
        raise gdb.GdbError("Must be struct rb_node not {}".format(node.type))

    if rb_empty_node(node):
        return None

    if node['rb_left']:
        node = node['rb_left']
        while node['rb_right']:
            node = node['rb_right']
        return node.dereference()

    parent = rb_parent(node)
    while parent and node == parent['rb_left'].dereference():
            node = parent
            parent = rb_parent(node)

    return parent


class LxRbFirst(gdb.Function):
    """Lookup and return a node from an RBTree

$lx_rb_first(root): Return the node at the given index.
If index is omitted, the root node is dereferenced and returned."""

    def __init__(self):
        super(LxRbFirst, self).__init__("lx_rb_first")

    def invoke(self, root):
        result = rb_first(root)
        if result is None:
            raise gdb.GdbError("No entry in tree")

        return result


LxRbFirst()


class LxRbLast(gdb.Function):
    """Lookup and return a node from an RBTree.

$lx_rb_last(root): Return the node at the given index.
If index is omitted, the root node is dereferenced and returned."""

    def __init__(self):
        super(LxRbLast, self).__init__("lx_rb_last")

    def invoke(self, root):
        result = rb_last(root)
        if result is None:
            raise gdb.GdbError("No entry in tree")

        return result


LxRbLast()


class LxRbNext(gdb.Function):
    """Lookup and return a node from an RBTree.

$lx_rb_next(node): Return the node at the given index.
If index is omitted, the root node is dereferenced and returned."""

    def __init__(self):
        super(LxRbNext, self).__init__("lx_rb_next")

    def invoke(self, node):
        result = rb_next(node)
        if result is None:
            raise gdb.GdbError("No entry in tree")

        return result


LxRbNext()


class LxRbPrev(gdb.Function):
    """Lookup and return a node from an RBTree.

$lx_rb_prev(node): Return the node at the given index.
If index is omitted, the root node is dereferenced and returned."""

    def __init__(self):
        super(LxRbPrev, self).__init__("lx_rb_prev")

    def invoke(self, node):
        result = rb_prev(node)
        if result is None:
            raise gdb.GdbError("No entry in tree")

        return result


LxRbPrev()