osandov / drgn

Programmable debugger
Other
1.78k stars 165 forks source link

Add sysfs helpers #443

Open osandov opened 1 month ago

osandov commented 1 month ago

Background

When debugging anything related to a device driver, the first step is often finding the struct device that represents the device in the kernel. One way to find a device is by its sysfs path. However, drgn doesn't currently have an easy way to do that. You can kind of do it with something like:

>>> path = path_lookup("/sys/devices/pci0000:00/0000:00:06.0/0000:04:00.0/nvme/nvme0/nvme0n1")
>>> kn = cast("struct kernfs_node *", path.dentry.d_inode.i_private)
>>> kobject = cast("struct kobject *", kn.priv)
>>> device = container_of(kobject, "struct device", "kobj")

But this has a few issues:

Instead, we should improve our kernfs helpers and provide sysfs-specific helpers on top of those. This can be broken down into multiple smaller features.

kernfs

sysfs is actually an instance of kernfs, which is generic infrastructure for pseudo file systems. A kernfs filesystem comprises a tree of struct kernfs_node, each of which represents a file or directory.

We already have a few basic helpers for inspecting kernfs: https://drgn.readthedocs.io/en/latest/helpers.html#kernfs. I would like two extensions to those:

sysfs

Once we have the kernfs changes, we can add sysfs-specific helpers that use the new kernfs features.