Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scripts/gdb: add a Radix Tree Parser

Linux makes use of the Radix Tree data structure to store pointers indexed
by integer values. This structure is utilised across many structures in
the kernel including the IRQ descriptor tables, and several filesystems.

This module provides a method to lookup values from a structure given its
head node.

Usage:

The function lx_radix_tree_lookup, must be given a symbol of type struct
radix_tree_root, and an index into that tree.

The object returned is a generic integer value, and must be cast correctly
to the type based on the storage in the data structure.

For example, to print the irq descriptor in the sparse irq_desc_tree at
index 18, try the following:

(gdb) print (struct irq_desc)$lx_radix_tree_lookup(irq_desc_tree, 18)

This script previously existed under commit
e127a73d41ac471d7e3ba950cf128f42d6ee3448 ("scripts/gdb: add a Radix Tree
Parser") and was later reverted with
b447e02548a3304c47b78b5e2d75a4312a8f17e1i (Revert "scripts/gdb: add a
Radix Tree Parser").

This version expects the XArray based radix tree implementation and has
been verified using QEMU/x86 on Linux 6.3-rc5.

[f.fainelli@gmail.com: revive and update for xarray implementation]
[f.fainelli@gmail.com: guard against a NULL node in the while loop]
Link: https://lkml.kernel.org/r/20230405222743.1191674-1-f.fainelli@gmail.com
Link: https://lkml.kernel.org/r/20230404214049.1016811-1-f.fainelli@gmail.com
Signed-off-by: Kieran Bingham <kieran.bingham@linaro.org>
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
Cc: Jan Kiszka <jan.kiszka@siemens.com>
Cc: Kieran Bingham <kbingham@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Kieran Bingham and committed by
Andrew Morton
b7235d6b b0687c11

+99
+8
scripts/gdb/linux/constants.py.in
··· 17 17 #include <linux/hrtimer.h> 18 18 #include <linux/mount.h> 19 19 #include <linux/of_fdt.h> 20 + #include <linux/radix-tree.h> 20 21 #include <linux/threads.h> 21 22 22 23 /* We need to stringify expanded macros so that they can be parsed */ ··· 68 67 69 68 /* linux/of_fdt.h> */ 70 69 LX_VALUE(OF_DT_HEADER) 70 + 71 + /* linux/radix-tree.h */ 72 + LX_GDBPARSED(RADIX_TREE_ENTRY_MASK) 73 + LX_GDBPARSED(RADIX_TREE_INTERNAL_NODE) 74 + LX_GDBPARSED(RADIX_TREE_MAP_SIZE) 75 + LX_GDBPARSED(RADIX_TREE_MAP_SHIFT) 76 + LX_GDBPARSED(RADIX_TREE_MAP_MASK) 71 77 72 78 /* Kernel Configs */ 73 79 LX_CONFIG(CONFIG_GENERIC_CLOCKEVENTS)
+90
scripts/gdb/linux/radixtree.py
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + # 3 + # Radix Tree Parser 4 + # 5 + # Copyright (c) 2016 Linaro Ltd 6 + # Copyright (c) 2023 Broadcom 7 + # 8 + # Authors: 9 + # Kieran Bingham <kieran.bingham@linaro.org> 10 + # Florian Fainelli <f.fainelli@gmail.com> 11 + 12 + import gdb 13 + 14 + from linux import utils 15 + from linux import constants 16 + 17 + radix_tree_root_type = utils.CachedType("struct xarray") 18 + radix_tree_node_type = utils.CachedType("struct xa_node") 19 + 20 + def is_internal_node(node): 21 + long_type = utils.get_long_type() 22 + return ((node.cast(long_type) & constants.LX_RADIX_TREE_ENTRY_MASK) == constants.LX_RADIX_TREE_INTERNAL_NODE) 23 + 24 + def entry_to_node(node): 25 + long_type = utils.get_long_type() 26 + node_type = node.type 27 + indirect_ptr = node.cast(long_type) & ~constants.LX_RADIX_TREE_INTERNAL_NODE 28 + return indirect_ptr.cast(radix_tree_node_type.get_type().pointer()) 29 + 30 + def node_maxindex(node): 31 + return (constants.LX_RADIX_TREE_MAP_SIZE << node['shift']) - 1 32 + 33 + def lookup(root, index): 34 + if root.type == radix_tree_root_type.get_type().pointer(): 35 + node = root.dereference() 36 + elif root.type != radix_tree_root_type.get_type(): 37 + raise gdb.GdbError("must be {} not {}" 38 + .format(radix_tree_root_type.get_type(), root.type)) 39 + 40 + node = root['xa_head'] 41 + if node == 0: 42 + return None 43 + 44 + if not (is_internal_node(node)): 45 + if (index > 0): 46 + return None 47 + return node 48 + 49 + node = entry_to_node(node) 50 + maxindex = node_maxindex(node) 51 + 52 + if (index > maxindex): 53 + return None 54 + 55 + shift = node['shift'] + constants.LX_RADIX_TREE_MAP_SHIFT 56 + 57 + while True: 58 + offset = (index >> node['shift']) & constants.LX_RADIX_TREE_MAP_MASK 59 + slot = node['slots'][offset] 60 + 61 + if slot == 0: 62 + return None 63 + 64 + node = slot.cast(node.type.pointer()).dereference() 65 + if node == 0: 66 + return None 67 + 68 + shift -= constants.LX_RADIX_TREE_MAP_SHIFT 69 + if (shift <= 0): 70 + break 71 + 72 + return node 73 + 74 + class LxRadixTree(gdb.Function): 75 + """ Lookup and return a node from a RadixTree. 76 + 77 + $lx_radix_tree_lookup(root_node [, index]): Return the node at the given index. 78 + If index is omitted, the root node is dereference and returned.""" 79 + 80 + def __init__(self): 81 + super(LxRadixTree, self).__init__("lx_radix_tree_lookup") 82 + 83 + def invoke(self, root, index=0): 84 + result = lookup(root, index) 85 + if result is None: 86 + raise gdb.GdbError("No entry in tree at index {}".format(index)) 87 + 88 + return result 89 + 90 + LxRadixTree()
+1
scripts/gdb/vmlinux-gdb.py
··· 38 38 import linux.genpd 39 39 import linux.device 40 40 import linux.mm 41 + import linux.radixtree