Skip to content

Commit

Permalink
bpflbr: Show process info
Browse files Browse the repository at this point in the history
Signed-off-by: Leon Hwang <[email protected]>
  • Loading branch information
Asphaltt committed Dec 5, 2024
1 parent f997662 commit 4355c98
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions bpf/lbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct event {
__s64 nr_bytes;
__s64 func_ret;
__u64 func_ip;
__u32 cpu;
__u32 pid;
__u8 comm[16];
} __attribute__((packed));

struct event lbr_events[1] SEC(".data.lbrs");
Expand All @@ -34,6 +37,9 @@ emit_lbr_event(void *ctx)
bpf_get_func_ret(ctx, (void *) &retval); /* required 5.17 kernel. */
event->func_ret = retval;
event->func_ip = bpf_get_func_ip(ctx); /* required 5.17 kernel. */
event->cpu = cpu;
event->pid = bpf_get_current_pid_tgid() >> 32;
bpf_get_current_comm(event->comm, sizeof(event->comm));

bpf_ringbuf_output(&events, event, sizeof(*event), 0);

Expand Down
6 changes: 5 additions & 1 deletion internal/bpflbr/lbr.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type Event struct {
NrBytes int64
Retval int64
FuncIP uintptr
CPU uint32
Pid uint32
Comm [16]byte
}

func Run(reader *ringbuf.Reader, progs *bpfProgs, addr2line *Addr2Line, ksyms *Kallsyms, w io.Writer) error {
Expand Down Expand Up @@ -113,7 +116,8 @@ func Run(reader *ringbuf.Reader, progs *bpfProgs, addr2line *Addr2Line, ksyms *K
}
}

fmt.Fprintf(&sb, "Recv a record for %s with retval=%d :\n", targetName, event.Retval)
fmt.Fprintf(&sb, "Recv a record for %s with retval=%d/%#x cpu=%d process=(%d:%s) :\n",
targetName, event.Retval, uint64(event.Retval), event.CPU, event.Pid, nullTerminated(event.Comm[:]))
stack.output(&sb)
fmt.Fprintln(w, sb.String())

Expand Down
21 changes: 21 additions & 0 deletions internal/bpflbr/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 Leon Hwang.
// SPDX-License-Identifier: Apache-2.0

package bpflbr

import "unsafe"

func nullTerminated(b []byte) string {
for i, c := range b {
if c == 0 {
b = b[:i]
break
}
}

if len(b) == 0 {
return ""
}

return unsafe.String(&b[0], len(b))
}

0 comments on commit 4355c98

Please sign in to comment.