iovisor / iovisor/bcc

BPF pre-processor for command line options

Open
#2,129 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
22.7k
Forks
4.1k
Avg merge
10d 4h
Merged PRs (30d)
3

Description

Tools currently use string substitution to implement different BPF code for different command line options. We're pushing it a bit too far in some tools, eg, opensnoop:

```Python
PID_TID_FILTER
UID_FILTER
FLAGS_FILTER
[...]
if args.tid: # TID trumps PID
bpf_text = bpf_text.replace('PID_TID_FILTER',
'if (tid != %s) { return 0; }' % args.tid)
elif args.pid:
bpf_text = bpf_text.replace('PID_TID_FILTER',
'if (pid != %s) { return 0; }' % args.pid)
else:
bpf_text = bpf_text.replace('PID_TID_FILTER', '')
if args.uid:
bpf_text = bpf_text.replace('UID_FILTER',
'if (uid != %s) { return 0; }' % args.uid)
else:
bpf_text = bpf_text.replace('UID_FILTER', '')
if args.flag_filter:
bpf_text = bpf_text.replace('FLAGS_FILTER',
'if (!(flags & %d)) { return 0; }' % flag_filter_mask)
else:
bpf_text = bpf_text.replace('FLAGS_FILTER', '')
if not (args.extended_fields or args.flag_filter):
bpf_text = '\n'.join(x for x in bpf_text.split('\n')
if 'EXTENDED_STRUCT_MEMBER' not in x)
```

This wasn't too bad for simple PID filters, but is getting messier, especially if we start having more -e extended field modes, where we want to have a perf output struct that also varies based on command line options. This ticket is to discuss a better way.

@vincentbernat once suggested (3 years ago) that I look at jinja2. I don't know it, but it looks interesting: https://github.com/vincentbernat/systemtap-cookbook/blob/116500e65c42fe2f9b5cea4f42961db9e26685b9/io#L239-L315

Resulting code has things like:

```
{%- if options.milliseconds %}
delta /= 1000;
{%- endif %}
```

Any other such libraries?

I think someone needs to pick a tool (say, opensnoop), and convert it to use these libraries so we can see if it's an improvement or not.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.