Skip to content

Commit

Permalink
bpflbr: Trace only for specified pid by --filter-pid
Browse files Browse the repository at this point in the history
It should be helpful to trace only for specified process.

Signed-off-by: Leon Hwang <[email protected]>
  • Loading branch information
Asphaltt committed Dec 5, 2024
1 parent cea0c2e commit 95c8b2d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
15 changes: 11 additions & 4 deletions bpf/lbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ struct lbr_config {
__u32 suppress_lbr:1;
__u32 output_stack:1;
__u32 pad:30;
__u32 pid;
};

volatile const struct lbr_config lbr_config = {
.suppress_lbr = 0,
};
volatile const struct lbr_config lbr_config = {};
#define cfg (&lbr_config)

#define MAX_STACK_DEPTH 50
Expand Down Expand Up @@ -55,11 +54,19 @@ emit_lbr_event(void *ctx)

if (!cfg->suppress_lbr)
event->nr_bytes = bpf_get_branch_snapshot(event->lbr, sizeof(event->lbr), 0); /* required 5.16 kernel. */

/* Other filters must be after bpf_get_branch_snapshot() to avoid polluting
* LBR entries.
*/

event->pid = bpf_get_current_pid_tgid() >> 32;
if (cfg->pid && event->pid != cfg->pid)
return BPF_OK;

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));
event->func_stack_id = -1;
if (cfg->output_stack)
Expand Down
1 change: 1 addition & 0 deletions internal/bpflbr/bpf_tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewBPFTracing(spec *ebpf.CollectionSpec, reusedMaps map[string]*ebpf.Map, i
var cfg LbrConfig
cfg.SetSuppressLbr(suppressLbr)
cfg.SetOutputStack(outputFuncStack)
cfg.FilterPid = uint32(filterPid)

if err := spec.Variables["lbr_config"].Set(cfg); err != nil {
return nil, fmt.Errorf("failed to set lbr config: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions internal/bpflbr/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (

suppressLbr bool
outputFuncStack bool
filterPid uint32
)

type ProgFlag struct {
Expand Down Expand Up @@ -133,6 +134,7 @@ func ParseFlags() (*Flags, error) {
f.StringVarP(&mode, "mode", "m", TracingModeExit, "mode of lbr tracing, exit or entry")
f.BoolVar(&suppressLbr, "suppress-lbr", false, "suppress LBR perf event")
f.BoolVar(&outputFuncStack, "output-stack", false, "output function call stack")
f.Uint32Var(&filterPid, "filter-pid", 0, "filter pid for tracing")

return &flags, f.Parse(os.Args)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/bpflbr/lbr_bpf_prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const (
)

type LbrConfig struct {
Flags uint32
Flags uint32
FilterPid uint32
}

func (cfg *LbrConfig) SetSuppressLbr(v bool) {
Expand Down

0 comments on commit 95c8b2d

Please sign in to comment.