github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/cmd/examples/uprobe_bash_stats/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "time" 7 8 "github.com/dylandreimerink/gobpfld" 9 "github.com/dylandreimerink/gobpfld/bpftypes" 10 ) 11 12 func main() { 13 elf, err := os.Open("bpf/bash_stats") 14 if err != nil { 15 panic(err) 16 } 17 18 loadedElf, err := gobpfld.LoadProgramFromELF(elf, gobpfld.ELFParseSettings{ 19 TruncateNames: true, 20 }) 21 if err != nil { 22 panic(err) 23 } 24 25 stats := loadedElf.Maps["bash_stats"].(*gobpfld.ArrayMap) 26 err = stats.Load() 27 if err != nil { 28 panic(err) 29 } 30 31 program := loadedElf.Programs["bpf_prog1"].(*gobpfld.ProgramKProbe) 32 33 log, err := program.Load(gobpfld.ProgKPLoadOpts{ 34 VerifierLogLevel: bpftypes.BPFLogLevelVerbose, 35 VerifierLogSize: 1 << 20, 36 }) 37 if err != nil { 38 fmt.Println(log) 39 panic(err) 40 } 41 42 fmt.Println("attaching") 43 44 err = program.Attach(gobpfld.ProgKPAttachOpts{ 45 Event: "bash_trace", 46 }) 47 if err != nil { 48 panic(err) 49 } 50 51 fmt.Println("attached") 52 53 defer func() { 54 err = program.Detach() 55 if err != nil { 56 panic(err) 57 } 58 }() 59 60 for i := 0; i < 10; i++ { 61 time.Sleep(1 * time.Second) 62 var val uint64 63 err = stats.Get(0, &val) 64 if err != nil { 65 fmt.Println(err.Error()) 66 } 67 fmt.Println(val) 68 } 69 }