Skip to content

Commit 0030d34

Browse files
committed
more strlen examples
1 parent 03c2bc5 commit 0030d34

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

examples/tracing/strlen_count.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/python
2+
#
3+
# strlen_count Trace strlen() and print a frequency count of strings.
4+
# For Linux, uses BCC, eBPF. Embedded C.
5+
#
6+
# Written as a basic example of BCC and uprobes.
7+
#
8+
# Also see strlensnoop.
9+
#
10+
# Copyright 2016 Netflix, Inc.
11+
# Licensed under the Apache License, Version 2.0 (the "License")
12+
13+
from __future__ import print_function
14+
from bcc import BPF
15+
from time import sleep
16+
17+
# load BPF program
18+
b = BPF(text="""
19+
#include <uapi/linux/ptrace.h>
20+
21+
struct key_t {
22+
char c[80];
23+
};
24+
BPF_HASH(counts, struct key_t);
25+
26+
int count(struct pt_regs *ctx) {
27+
if (!ctx->si)
28+
return 0;
29+
30+
struct key_t key = {};
31+
u64 zero = 0, *val;
32+
33+
bpf_probe_read(&key.c, sizeof(key.c), (void *)ctx->si);
34+
val = counts.lookup_or_init(&key, &zero);
35+
(*val)++;
36+
return 0;
37+
};
38+
""")
39+
b.attach_uprobe(name="c", sym="strlen", fn_name="count")
40+
41+
# header
42+
print("Tracing strlen()... Hit Ctrl-C to end.")
43+
44+
# sleep until Ctrl-C
45+
try:
46+
sleep(99999999)
47+
except KeyboardInterrupt:
48+
pass
49+
50+
# print output
51+
print("%10s %s" % ("COUNT", "STRING"))
52+
counts = b.get_table("counts")
53+
for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
54+
print("%10d \"%s\"" % (v.value, k.c.encode('string-escape')))

examples/tracing/strlen_snoop.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/python
2+
#
3+
# strlen_snoop Trace strlen() library function for a given PID.
4+
# For Linux, uses BCC, eBPF. Embedded C.
5+
#
6+
# USAGE: strlensnoop PID
7+
#
8+
# Try running this on a separate bash shell.
9+
#
10+
# Written as a basic example of BCC and uprobes.
11+
#
12+
# Copyright 2016 Netflix, Inc.
13+
# Licensed under the Apache License, Version 2.0 (the "License")
14+
15+
from __future__ import print_function
16+
from bcc import BPF
17+
from os import getpid
18+
import sys
19+
20+
if len(sys.argv) < 2:
21+
print("USAGE: strlensnoop PID")
22+
exit()
23+
pid = sys.argv[1]
24+
25+
# load BPF program
26+
bpf_text = """
27+
#include <uapi/linux/ptrace.h>
28+
int printarg(struct pt_regs *ctx) {
29+
if (!ctx->si)
30+
return 0;
31+
32+
u32 pid = bpf_get_current_pid_tgid();
33+
if (pid != PID)
34+
return 0;
35+
36+
char str[80] = {};
37+
bpf_probe_read(&str, sizeof(str), (void *)ctx->si);
38+
bpf_trace_printk("%s\\n", &str);
39+
40+
return 0;
41+
};
42+
"""
43+
bpf_text = bpf_text.replace('PID', pid)
44+
b = BPF(text=bpf_text)
45+
b.attach_uprobe(name="c", sym="strlen", fn_name="printarg")
46+
47+
# header
48+
print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "STRLEN"))
49+
50+
# format output
51+
me = getpid()
52+
while 1:
53+
try:
54+
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
55+
except ValueError:
56+
continue
57+
if pid == me or msg == "":
58+
continue
59+
print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))

0 commit comments

Comments
 (0)