3
3
#include <argp.h>
4
4
#include <signal.h>
5
5
#include <stdio.h>
6
+ #include <stdlib.h>
6
7
#include <sys/resource.h>
7
8
#include <bpf/bpf.h>
8
9
#include <bpf/libbpf.h>
@@ -53,6 +54,22 @@ int main(int argc, char **argv)
53
54
int iter_fd ;
54
55
ssize_t ret ;
55
56
int err ;
57
+ LIBBPF_OPTS (bpf_iter_attach_opts , opts );
58
+ union bpf_iter_link_info linfo ;
59
+ pid_t pid_filter = 0 ;
60
+
61
+ /* The user can provide a process id as first argument to the executable. This can be used
62
+ * to filter out all tasks not belonging to a particular process.
63
+ */
64
+ if (argc > 1 ) {
65
+ errno = 0 ;
66
+ pid_filter = (pid_t )strtol (argv [1 ], NULL , 10 );
67
+ err = - errno ;
68
+ if (err != 0 || pid_filter < 0 ) {
69
+ fprintf (stderr , "Failed to parse pid '%s'\n" , argv [1 ]);
70
+ return err ;
71
+ }
72
+ }
56
73
57
74
/* Set up libbpf errors and debug info callback */
58
75
libbpf_set_print (libbpf_print_fn );
@@ -68,12 +85,25 @@ int main(int argc, char **argv)
68
85
goto cleanup ;
69
86
}
70
87
71
- /* Attach tracepoints */
72
- err = task_iter_bpf__attach (skel );
73
- if (err ) {
88
+ /* Attach BPF iterator program */
89
+ memset (& linfo , 0 , sizeof (linfo ));
90
+ linfo .task .pid = pid_filter ; /* If the pid is set to zero, no filtering logic is applied */
91
+ opts .link_info = & linfo ;
92
+ opts .link_info_len = sizeof (linfo );
93
+ skel -> links .get_tasks = bpf_program__attach_iter (skel -> progs .get_tasks , & opts );
94
+ if (!skel -> links .get_tasks ) {
95
+ err = - errno ;
74
96
fprintf (stderr , "Failed to attach BPF skeleton\n" );
75
97
goto cleanup ;
76
98
}
99
+ /* Alternatively, if the user doesn't want to provide any option, the following simplified
100
+ * version can be used:
101
+ * err = task_iter_bpf__attach(skel);
102
+ * if (err) {
103
+ * fprintf(stderr, "Failed to attach BPF skeleton\n");
104
+ * goto cleanup;
105
+ * }
106
+ */
77
107
78
108
iter_fd = bpf_iter_create (bpf_link__fd (skel -> links .get_tasks ));
79
109
if (iter_fd < 0 ) {
0 commit comments