Commit b950d6f8 authored by Sasha Goldshtein's avatar Sasha Goldshtein

Moved auto-includes helper to __init__.py

parent c08c431a
......@@ -73,6 +73,30 @@ class BPF(object):
_lib_load_address_cache = {}
_lib_symbol_cache = {}
_auto_includes = {
"linux/time.h" : ["time"],
"linux/fs.h" : ["fs", "file"],
"linux/blkdev.h" : ["bio", "request"],
"linux/slab.h" : ["alloc"],
"linux/netdevice.h" : ["sk_buff", "net_device"]
}
@classmethod
def generate_auto_includes(cls, program_words):
"""
Generates #include statements automatically based on a set of
recognized types such as sk_buff and bio. The input is all the words
that appear in the BPF program, and the output is a (possibly empty)
string of #include statements, such as "#include <linux/fs.h>".
"""
headers = ""
for header, keywords in cls._auto_includes.items():
for keyword in keywords:
for word in program_words:
if keyword in word and header not in headers:
headers += "#include <%s>\n" % header
return headers
# defined for compatibility reasons, to be removed
Table = Table
......
......@@ -36,24 +36,6 @@ int PROBENAME(struct pt_regs *ctx SIGNATURE)
"""
next_probe_index = 0
aliases = { "$PID": "bpf_get_current_pid_tgid()" }
auto_includes = {
"linux/time.h" : ["time"],
"linux/fs.h" : ["fs", "file"],
"linux/blkdev.h" : ["bio", "request"],
"linux/slab.h" : ["alloc"],
"linux/netdevice.h" : ["sk_buff", "net_device"]
}
@staticmethod
def generate_auto_includes(specifiers):
headers = ""
for header, keywords in Specifier.auto_includes.items():
for keyword in keywords:
for specifier in specifiers:
if keyword in specifier:
headers += "#include <%s>\n" \
% header
return headers
def _substitute_aliases(self, expr):
if expr is None:
......@@ -590,7 +572,7 @@ struct __string_t { char s[%d]; };
""" % self.args.string_size
for include in (self.args.include or []):
bpf_source += "#include <%s>\n" % include
bpf_source += Specifier.generate_auto_includes(
bpf_source += BPF.generate_auto_includes(
map(lambda s: s.raw_spec, self.specifiers))
bpf_source += Tracepoint.generate_decl()
bpf_source += Tracepoint.generate_entry_probe()
......
......@@ -328,25 +328,6 @@ int %s(struct pt_regs *ctx)
def _time_off_str(cls, timestamp_ns):
return "%.6f" % (1e-9 * (timestamp_ns - cls.first_ts))
auto_includes = {
"linux/time.h" : ["time"],
"linux/fs.h" : ["fs", "file"],
"linux/blkdev.h" : ["bio", "request"],
"linux/slab.h" : ["alloc"],
"linux/netdevice.h" : ["sk_buff"]
}
@classmethod
def generate_auto_includes(cls, probes):
headers = ""
for header, keywords in cls.auto_includes.items():
for keyword in keywords:
for probe in probes:
if keyword in probe:
headers += "#include <%s>\n" \
% header
return headers
def _display_function(self):
if self.probe_type != 't':
return self.function
......@@ -468,7 +449,7 @@ trace 't:block:block_rq_complete "sectors=%d", tp.nr_sector'
#include <linux/sched.h> /* For TASK_COMM_LEN */
"""
self.program += Probe.generate_auto_includes(
self.program += BPF.generate_auto_includes(
map(lambda p: p.raw_probe, self.probes))
self.program += Tracepoint.generate_decl()
self.program += Tracepoint.generate_entry_probe()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment