1#
2# gdb helper commands and functions for Linux kernel debugging
3#
4#  VFS tools
5#
6# Copyright (c) 2023 Glenn Washburn
7# Copyright (c) 2016 Linaro Ltd
8#
9# Authors:
10#  Glenn Washburn <development@efficientek.com>
11#  Kieran Bingham <kieran.bingham@linaro.org>
12#
13# This work is licensed under the terms of the GNU GPL version 2.
14#
15
16import gdb
17from linux import utils
18
19
20def dentry_name(d):
21    parent = d['d_parent']
22    if parent == d or parent == 0:
23        return ""
24    p = dentry_name(d['d_parent']) + "/"
25    return p + d['d_iname'].string()
26
27class DentryName(gdb.Function):
28    """Return string of the full path of a dentry.
29
30$lx_dentry_name(PTR): Given PTR to a dentry struct, return a string
31of the full path of the dentry."""
32
33    def __init__(self):
34        super(DentryName, self).__init__("lx_dentry_name")
35
36    def invoke(self, dentry_ptr):
37        return dentry_name(dentry_ptr)
38
39DentryName()
40
41
42dentry_type = utils.CachedType("struct dentry")
43
44class InodeDentry(gdb.Function):
45    """Return dentry pointer for inode.
46
47$lx_i_dentry(PTR): Given PTR to an inode struct, return a pointer to
48the associated dentry struct, if there is one."""
49
50    def __init__(self):
51        super(InodeDentry, self).__init__("lx_i_dentry")
52
53    def invoke(self, inode_ptr):
54        d_u = inode_ptr["i_dentry"]["first"]
55        if d_u == 0:
56            return ""
57        return utils.container_of(d_u, dentry_type.get_type().pointer(), "d_u")
58
59InodeDentry()
60