1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
12 #include <bpf/hashmap.h>
15 #include "skeleton/pid_iter.h"
17 #ifdef BPFTOOL_WITHOUT_SKELETONS
19 int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type)
23 void delete_obj_refs_table(struct hashmap *map) {}
24 void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix) {}
25 void emit_obj_refs_json(struct hashmap *map, __u32 id, json_writer_t *json_writer) {}
27 #else /* BPFTOOL_WITHOUT_SKELETONS */
29 #include "pid_iter.skel.h"
31 static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
33 struct hashmap_entry *entry;
34 struct obj_refs *refs;
39 hashmap__for_each_key_entry(map, entry, e->id) {
42 for (i = 0; i < refs->ref_cnt; i++) {
43 if (refs->refs[i].pid == e->pid)
47 tmp = realloc(refs->refs, (refs->ref_cnt + 1) * sizeof(*ref));
49 p_err("failed to re-alloc memory for ID %u, PID %d, COMM %s...",
50 e->id, e->pid, e->comm);
54 ref = &refs->refs[refs->ref_cnt];
56 memcpy(ref->comm, e->comm, sizeof(ref->comm));
63 refs = calloc(1, sizeof(*refs));
65 p_err("failed to alloc memory for ID %u, PID %d, COMM %s...",
66 e->id, e->pid, e->comm);
70 refs->refs = malloc(sizeof(*refs->refs));
73 p_err("failed to alloc memory for ID %u, PID %d, COMM %s...",
74 e->id, e->pid, e->comm);
79 memcpy(ref->comm, e->comm, sizeof(ref->comm));
81 refs->has_bpf_cookie = e->has_bpf_cookie;
82 refs->bpf_cookie = e->bpf_cookie;
84 err = hashmap__append(map, e->id, refs);
86 p_err("failed to append entry to hashmap for ID %u: %s",
87 e->id, strerror(errno));
90 static int __printf(2, 0)
91 libbpf_print_none(__maybe_unused enum libbpf_print_level level,
92 __maybe_unused const char *format,
93 __maybe_unused va_list args)
98 int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type)
100 struct pid_iter_entry *e;
101 char buf[4096 / sizeof(*e) * sizeof(*e)];
102 struct pid_iter_bpf *skel;
103 int err, ret, fd = -1, i;
104 libbpf_print_fn_t default_print;
106 *map = hashmap__new(hash_fn_for_key_as_id, equal_fn_for_key_as_id, NULL);
108 p_err("failed to create hashmap for PID references");
113 skel = pid_iter_bpf__open();
115 p_err("failed to open PID iterator skeleton");
119 skel->rodata->obj_type = type;
121 /* we don't want output polluted with libbpf errors if bpf_iter is not
124 default_print = libbpf_set_print(libbpf_print_none);
125 err = pid_iter_bpf__load(skel);
126 libbpf_set_print(default_print);
128 /* too bad, kernel doesn't support BPF iterators yet */
132 err = pid_iter_bpf__attach(skel);
134 /* if we loaded above successfully, attach has to succeed */
135 p_err("failed to attach PID iterator: %d", err);
139 fd = bpf_iter_create(bpf_link__fd(skel->links.iter));
142 p_err("failed to create PID iterator session: %d", err);
147 ret = read(fd, buf, sizeof(buf));
152 p_err("failed to read PID iterator output: %d", err);
157 if (ret % sizeof(*e)) {
159 p_err("invalid PID iterator output format");
165 for (i = 0; i < ret; i++, e++) {
173 pid_iter_bpf__destroy(skel);
177 void delete_obj_refs_table(struct hashmap *map)
179 struct hashmap_entry *entry;
185 hashmap__for_each_entry(map, entry, bkt) {
186 struct obj_refs *refs = entry->pvalue;
195 void emit_obj_refs_json(struct hashmap *map, __u32 id,
196 json_writer_t *json_writer)
198 struct hashmap_entry *entry;
200 if (hashmap__empty(map))
203 hashmap__for_each_key_entry(map, entry, id) {
204 struct obj_refs *refs = entry->pvalue;
207 if (refs->ref_cnt == 0)
210 if (refs->has_bpf_cookie)
211 jsonw_lluint_field(json_writer, "bpf_cookie", refs->bpf_cookie);
213 jsonw_name(json_writer, "pids");
214 jsonw_start_array(json_writer);
215 for (i = 0; i < refs->ref_cnt; i++) {
216 struct obj_ref *ref = &refs->refs[i];
218 jsonw_start_object(json_writer);
219 jsonw_int_field(json_writer, "pid", ref->pid);
220 jsonw_string_field(json_writer, "comm", ref->comm);
221 jsonw_end_object(json_writer);
223 jsonw_end_array(json_writer);
228 void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
230 struct hashmap_entry *entry;
232 if (hashmap__empty(map))
235 hashmap__for_each_key_entry(map, entry, id) {
236 struct obj_refs *refs = entry->pvalue;
239 if (refs->ref_cnt == 0)
242 if (refs->has_bpf_cookie)
243 printf("\n\tbpf_cookie %llu", (unsigned long long) refs->bpf_cookie);
245 printf("%s", prefix);
246 for (i = 0; i < refs->ref_cnt; i++) {
247 struct obj_ref *ref = &refs->refs[i];
249 printf("%s%s(%d)", i == 0 ? "" : ", ", ref->comm, ref->pid);