Created
April 17, 2017 03:45
-
-
Save unakatsuo/d2a0669856a1dec68989ac45ce8cb731 to your computer and use it in GitHub Desktop.
iovisor/bcc Go binding test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build: linux,cgo | |
package main | |
/* | |
#cgo pkg-config: libbcc | |
#include <bcc/bpf_common.h> | |
#include <bcc/libbpf.h> | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"unsafe" | |
) | |
var CNULL = unsafe.Pointer(uintptr(0)) | |
const BPF_PROGRAM = ` | |
int on_sys_clone(void *ctx) { | |
bpf_trace_printk("Hello, World! Here I did a sys_clone call!\n"); | |
return 0; | |
} | |
` | |
func main() { | |
bpf_prog := C.CString(BPF_PROGRAM) | |
defer C.free(unsafe.Pointer(bpf_prog)) | |
var cflags **C.char | |
defer C.free(unsafe.Pointer(cflags)) | |
//var bpf_mod unsafe.Pointer | |
// void * bpf_module_create_c_from_string(const char *text, unsigned flags, const char *cflags[], int ncflags); | |
bpf_mod := C.bpf_module_create_c_from_string(bpf_prog, C.uint(0), cflags, C.int(0)) | |
if bpf_mod == CNULL { | |
panic("Failed bpf_module_create_c_from_string") | |
} | |
defer C.bpf_module_destroy(bpf_mod) | |
kver := C.bpf_module_kern_version(bpf_mod) | |
fmt.Printf("%x\n", kver) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment