Skip to content

Commit ec8415b

Browse files
Rtoaxekyooo
authored andcommitted
libbpf-tools: filelife: Fix problem when using perf-buffer
When ring buffers are not supported, will fallback to perf-buffer. The map type is set for the perf buffer in `bpf_buffer__new()`. This setting must be done before the BPF program is loaded. Otherwise, `BPF_MAP_TYPE_RINGBUF` will be used in `bpf_perf_event_output()`, resulting in the following error message: ; return bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, buf, size); @ compat.bpf.h:46 136: (79) r1 = *(u64 *)(r10 -40) ; R1_w=ctx() R10=fp0 fp-40=ctx() 137: (18) r2 = 0xffff8bd101623c00 ; R2_w=map_ptr(map=events,ks=0,vs=0) 139: (18) r3 = 0xffffffff ; R3_w=0xffffffff 141: (bf) r4 = r7 ; R4_w=map_value(map=heap,ks=4,vs=10240) R7=map_value(map=heap,ks=4,vs=10240) 142: (b7) r5 = 8200 ; R5_w=8200 143: (85) call bpf_perf_event_output#25 cannot pass map_type 27 into func bpf_perf_event_output#25 map_type 27 is BPF_MAP_TYPE_RINGBUF. At the same time, I also moved the notification message (Tracing...) to after all steps were successful. Signed-off-by: Rong Tao <rongtao@cestc.cn>
1 parent 7da5916 commit ec8415b

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

libbpf-tools/filelife.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ int main(int argc, char **argv)
150150
return 1;
151151
}
152152

153+
buf = bpf_buffer__new(obj->maps.events, obj->maps.heap);
154+
if (!buf) {
155+
err = -errno;
156+
fprintf(stderr, "failed to create ring/perf buffer: %d", err);
157+
goto cleanup;
158+
}
159+
153160
/* initialize global data (filtering options) */
154161
obj->rodata->targ_tgid = env.pid;
155162
obj->rodata->full_path = env.full_path;
@@ -169,16 +176,6 @@ int main(int argc, char **argv)
169176
goto cleanup;
170177
}
171178

172-
printf("Tracing the lifespan of short-lived files ... Hit Ctrl-C to end.\n");
173-
printf("%-8s %-6s %-16s %-7s %s\n", "TIME", "PID", "COMM", "AGE(s)", "FILE");
174-
175-
buf = bpf_buffer__new(obj->maps.events, obj->maps.heap);
176-
if (!buf) {
177-
err = -errno;
178-
fprintf(stderr, "failed to create ring/perf buffer: %d", err);
179-
goto cleanup;
180-
}
181-
182179
err = bpf_buffer__open(buf, handle_event, handle_lost_events, NULL);
183180
if (err) {
184181
err = -errno;
@@ -192,6 +189,9 @@ int main(int argc, char **argv)
192189
goto cleanup;
193190
}
194191

192+
printf("Tracing the lifespan of short-lived files ... Hit Ctrl-C to end.\n");
193+
printf("%-8s %-6s %-16s %-7s %s\n", "TIME", "PID", "COMM", "AGE(s)", "FILE");
194+
195195
while (!exiting) {
196196
err = bpf_buffer__poll(buf, POLL_TIMEOUT_MS);
197197
if (err < 0 && err != -EINTR) {

0 commit comments

Comments
 (0)