Skip to content

Commit

Permalink
Merge pull request iovisor#163 from iovisor/bblanco_dev
Browse files Browse the repository at this point in the history
Add debug flag to enable prints from kernel verifier
  • Loading branch information
4ast committed Aug 25, 2015
2 parents 88fda69 + b71f9fa commit 7eb074a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
13 changes: 8 additions & 5 deletions src/cc/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,26 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
const char *license, unsigned kern_version,
char *log_buf, unsigned log_buf_size)
{
log_buf = log_buf ? log_buf : bpf_log_buf;
log_buf_size = log_buf_size ? log_buf_size : LOG_BUF_SIZE;
union bpf_attr attr = {
.prog_type = prog_type,
.insns = ptr_to_u64((void *) insns),
.insn_cnt = prog_len / sizeof(struct bpf_insn),
.license = ptr_to_u64((void *) license),
.log_buf = ptr_to_u64(log_buf),
.log_size = log_buf_size,
.log_level = 1,
.log_level = log_buf ? 1 : 0,
};

attr.kern_version = kern_version;
log_buf[0] = 0;
if (log_buf)
log_buf[0] = 0;

int ret = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
if (ret < 0 && log_buf == bpf_log_buf) {
if (ret < 0 && !log_buf) {
// caller did not specify log_buf but failure should be printed,
// so call recursively and print the result to stderr
bpf_prog_load(prog_type, insns, prog_len, license, kern_version,
bpf_log_buf, LOG_BUF_SIZE);
fprintf(stderr, "bpf: %s\n%s\n", strerror(errno), bpf_log_buf);
}
return ret;
Expand Down
24 changes: 21 additions & 3 deletions src/python/bpf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ def _find_file(filename):
return filename

def __init__(self, src_file="", hdr_file="", text=None, debug=0):
"""Create a a new BPF module with the given source code.
Note:
All fields are marked as optional, but either `src_file` or `text`
must be supplied, and not both.
Args:
src_file (Optional[str]): Path to a source file for the module
hdr_file (Optional[str]): Path to a helper header file for the `src_file`
text (Optional[str]): Contents of a source file for the module
debug (Optional[int]): Flags used for debug prints, can be |'d together
0x1: print LLVM IR to stderr
0x2: print BPF bytecode to stderr
"""

self.debug = debug
self.funcs = {}
self.tables = {}
Expand All @@ -284,16 +299,19 @@ def load_func(self, func_name, prog_type):
if lib.bpf_function_start(self.module, func_name.encode("ascii")) == None:
raise Exception("Unknown program %s" % func_name)

log_buf = ct.create_string_buffer(65536) if self.debug else None

fd = lib.bpf_prog_load(prog_type,
lib.bpf_function_start(self.module, func_name.encode("ascii")),
lib.bpf_function_size(self.module, func_name.encode("ascii")),
lib.bpf_module_license(self.module),
lib.bpf_module_kern_version(self.module),
None, 0)
log_buf, ct.sizeof(log_buf) if log_buf else 0)

if self.debug & 0x2:
print(log_buf.value.decode(), file=sys.stderr)

if fd < 0:
print((ct.c_char * 65536).in_dll(lib, "bpf_log_buf").value)
#print(ct.c_char_p.in_dll(lib, "bpf_log_buf").value)
raise Exception("Failed to load BPF program %s" % func_name)

fn = BPF.Function(self, func_name, fd)
Expand Down
12 changes: 3 additions & 9 deletions tests/cc/test_trace4.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from bpf import BPF
import os
from socket import socket, AF_INET, SOCK_DGRAM
import sys
from unittest import main, TestCase

Expand All @@ -23,18 +22,13 @@ def setUp(self):
return 0;
}
""")
self.b.attach_kprobe(event_re="^SyS_send.*", fn_name="hello",
pid=0, cpu=-1)
self.b.attach_kretprobe(event_re="^SyS_send.*", fn_name="goodbye",
pid=1, cpu=-1)
self.b.attach_kprobe(event_re="^SyS_bp.*", fn_name="hello")
self.b.attach_kretprobe(event_re="^SyS_bp.*", fn_name="goodbye")

def test_send1(self):
udp = socket(AF_INET, SOCK_DGRAM)
udp.sendto(b"a" * 10, ("127.0.0.1", 5000))
udp.close()
k1 = self.b["stats"].Key(1)
k2 = self.b["stats"].Key(2)
self.assertEqual(self.b["stats"][k1].val, self.b["stats"][k2].val)
self.assertEqual(self.b["stats"][k1].val, self.b["stats"][k2].val + 1)

if __name__ == "__main__":
main()

0 comments on commit 7eb074a

Please sign in to comment.