Skip to content

Commit

Permalink
bcc: Make regex helpers publicly accessible
Browse files Browse the repository at this point in the history
Make the `get_user_functions`, `get_kprobe_functions`, and
`get_tracepoints` methods publicly accessible from the BPF class.
These can then be used by tools that need to do their own work
before attaching programs to a set of functions or tracepoints.
  • Loading branch information
goldshtn committed Oct 19, 2016
1 parent b778ccd commit e84febd
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ def attach_raw_socket(fn, dev):
% (dev, errstr))
fn.sock = sock

def _get_kprobe_functions(self, event_re):
@staticmethod
def get_kprobe_functions(event_re):
with open("%s/../kprobes/blacklist" % TRACEFS) as blacklist_file:
blacklist = set([line.rstrip().split()[1] for line in
blacklist_file])
Expand All @@ -386,7 +387,6 @@ def _get_kprobe_functions(self, event_re):
fn = line.rstrip().split()[0]
if re.match(event_re, fn) and fn not in blacklist:
fns.append(fn)
self._check_probe_quota(len(fns))
return fns

def _check_probe_quota(self, num_new_probes):
Expand All @@ -409,7 +409,7 @@ def attach_kprobe(self, event="", fn_name="", event_re="",

# allow the caller to glob multiple functions together
if event_re:
for line in self._get_kprobe_functions(event_re):
for line in BPF.get_kprobe_functions(event_re):
try:
self.attach_kprobe(event=line, fn_name=fn_name, pid=pid,
cpu=cpu, group_fd=group_fd)
Expand Down Expand Up @@ -448,7 +448,7 @@ def attach_kretprobe(self, event="", fn_name="", event_re="",

# allow the caller to glob multiple functions together
if event_re:
for line in self._get_kprobe_functions(event_re):
for line in BPF.get_kprobe_functions(event_re):
try:
self.attach_kretprobe(event=line, fn_name=fn_name, pid=pid,
cpu=cpu, group_fd=group_fd)
Expand Down Expand Up @@ -531,7 +531,8 @@ def find_library(libname):
res = lib.bcc_procutils_which_so(libname.encode("ascii"))
return res if res is None else res.decode()

def _get_tracepoints(self, tp_re):
@staticmethod
def get_tracepoints(tp_re):
results = []
events_dir = os.path.join(TRACEFS, "events")
for category in os.listdir(events_dir):
Expand Down Expand Up @@ -570,7 +571,7 @@ def attach_tracepoint(self, tp="", tp_re="", fn_name="", pid=-1,
"""

if tp_re:
for tp in self._get_tracepoints(tp_re):
for tp in BPF.get_tracepoints(tp_re):
self.attach_tracepoint(tp=tp, fn_name=fn_name, pid=pid,
cpu=cpu, group_fd=group_fd)
return
Expand Down Expand Up @@ -615,7 +616,13 @@ def _del_uprobe(self, name):
del self.open_uprobes[name]
_num_open_probes -= 1

def _get_user_functions(self, name, sym_re):
@staticmethod
def get_user_functions(name, sym_re):
return set([name for (name, _) in
BPF.get_user_functions_and_addresses(name, sym_re)])

@staticmethod
def get_user_addresses(name, sym_re):
"""
We are returning addresses here instead of symbol names because it
turns out that the same name may appear multiple times with different
Expand All @@ -624,10 +631,15 @@ def _get_user_functions(self, name, sym_re):
it makes sense to return the unique set of addresses that are mapped to
a symbol that matches the provided regular expression.
"""
return set([address for (_, address) in
BPF.get_user_functions_and_addresses(name, sym_re)])

@staticmethod
def get_user_functions_and_addresses(name, sym_re):
addresses = []
def sym_cb(sym_name, addr):
if re.match(sym_re, sym_name) and addr not in addresses:
addresses.append(addr)
if re.match(sym_re, sym_name):
addresses.append((sym_name, addr))
return 0

res = lib.bcc_foreach_symbol(name, _SYM_CB_TYPE(sym_cb))
Expand Down Expand Up @@ -660,7 +672,7 @@ def attach_uprobe(self, name="", sym="", sym_re="", addr=None,
name = str(name)

if sym_re:
for sym_addr in self._get_user_functions(name, sym_re):
for sym_addr in BPF.get_user_adddresses(name, sym_re):
self.attach_uprobe(name=name, addr=sym_addr,
fn_name=fn_name, pid=pid, cpu=cpu,
group_fd=group_fd)
Expand Down Expand Up @@ -711,7 +723,7 @@ def attach_uretprobe(self, name="", sym="", sym_re="", addr=None,
"""

if sym_re:
for sym_addr in self._get_user_functions(name, sym_re):
for sym_addr in BPF.get_user_addresses(name, sym_re):
self.attach_uretprobe(name=name, addr=sym_addr,
fn_name=fn_name, pid=pid, cpu=cpu,
group_fd=group_fd)
Expand Down

0 comments on commit e84febd

Please sign in to comment.