Skip to content

Commit

Permalink
Add simple single file example
Browse files Browse the repository at this point in the history
Signed-off-by: Brenden Blanco <[email protected]>
  • Loading branch information
Brenden Blanco committed Jun 5, 2015
1 parent 6c0311e commit 41d10e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
# Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")

# run in project directory with:
# sudo bash -c "PYTHONPATH=$PWD/src LD_LIBRARY_PATH=$PWD/build/src/cc examples/hello_world.py"

from bpf import BPF
from subprocess import call

prog = """
#include "src/cc/bpf_helpers.h"
BPF_EXPORT(hello)
int hello(void *ctx) {
char fmt[] = "Hello, World!\\n";
bpf_trace_printk(fmt, sizeof(fmt));
return 0;
};
"""
b = BPF(text=prog)
fn = b.load_func("hello", BPF.KPROBE)
BPF.attach_kprobe(fn, "sys_clone")
call(["cat", "/sys/kernel/debug/tracing/trace_pipe"])
2 changes: 1 addition & 1 deletion src/bpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def attach_raw_socket(fn, dev):
fn.sock = sock

@staticmethod
def attach_kprobe(fn, event, pid=-1, cpu=0, group_fd=-1):
def attach_kprobe(fn, event, pid=0, cpu=-1, group_fd=-1):
if not isinstance(fn, BPF.Function):
raise Exception("arg 1 must be of type BPF.Function")
ev_name = "p_" + event.replace("+", "_")
Expand Down

0 comments on commit 41d10e2

Please sign in to comment.