Skip to content

Commit

Permalink
BTF support for tracepoints defined in modules
Browse files Browse the repository at this point in the history
For tracepoints defined in vmlinux, we support usage of BTF for type
parsing which can automatically resolve types from tracepoint defs.
Thanks to this feature, users do not have to include headers with
necessary types manually.

This commit enables the same for tracepoints defined in kernel modules.
Since parsing BTF for modules is already supported for kfuncs, it is
sufficient to add the tracepoint subsystem name to the list of modules.
If the name of the subsystem does not correspond to a loaded module, it
will be ignored.

Note that this is only supported for single-subsystem tracepoints at the
moment. The reason is that tracepoints require dumping of C definitions
from BTF which is not possible for multiple modules at once (as it could
introduce type redefinitions).
  • Loading branch information
viktormalik committed Jan 20, 2023
1 parent 54d4cca commit 65ae766
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ jobs:
run: >
docker load --input /tmp/docker-save/i.tar
- name: Load kernel modules
# nf_tables is necessary for testing kernel modules BTF support
run: sudo modprobe nf_tables
# nf_tables and xfs are necessary for testing kernel modules BTF support
run: |
sudo modprobe nf_tables
sudo modprobe xfs
- name: Build and test
env: ${{matrix.env}}
run: >
Expand Down
12 changes: 10 additions & 2 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ void Driver::error(const std::string &m)
failed_ = true;
}

// Retrieves the list of kernel modules for all attached-to functions.
// Currently modules are only important for k(ret)func probes.
// Retrieves the list of kernel modules for all attachpoints. Will be used to
// identify modules whose BTF we need to parse.
// Currently, this is useful for k(ret)func and tracepoint probes.
std::set<std::string> Driver::list_modules() const
{
std::set<std::string> modules;
Expand All @@ -102,6 +103,13 @@ std::set<std::string> Driver::list_modules() const
else
modules.insert(ap->target);
}
else if (probe_type == ProbeType::tracepoint)
{
// For now, we support this for a single target only since tracepoints
// need dumping of C definitions BTF and that is not available for
// multiple modules at once.
modules.insert(ap->target);
}
}
}
return modules;
Expand Down
7 changes: 7 additions & 0 deletions tests/runtime/btf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ REQUIRES lsmod | grep '^nf_tables'
REQUIRES /usr/sbin/nft --help
AFTER nft delete table bpftrace

NAME kernel_module_tracepoint
PROG tracepoint:xfs:xfs_setfilesize { print(args->offset) } i:ms:1 { exit(); }
EXPECT Attaching 2 probes...
TIMEOUT 5
REQUIRES_FEATURE btf
REQUIRES lsmod | grep "^xfs"

# args->ctxt has type 'struct x86_emulate_ctxt' which is forward-defined in
# 'vmlinux' BTF and fully defined in 'kvm' BTF.
# This tests checks that the correct BTF definition is pulled from 'kvm'.
Expand Down

0 comments on commit 65ae766

Please sign in to comment.