1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
11 #include <bpf/hashmap.h>
14 #include "skeleton/pid_iter.h"
16 #ifdef BPFTOOL_WITHOUT_SKELETONS
18 int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type)
22 void delete_obj_refs_table(struct hashmap *map) {}
23 void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix) {}
24 void emit_obj_refs_json(struct hashmap *map, __u32 id, json_writer_t *json_writer) {}
26 #else /* BPFTOOL_WITHOUT_SKELETONS */
28 #include "pid_iter.skel.h"
30 static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
32 struct hashmap_entry *entry;
33 struct obj_refs *refs;
38 hashmap__for_each_key_entry(map, entry, u32_as_hash_field(e->id)) {
41 for (i = 0; i < refs->ref_cnt; i++) {
42 if (refs->refs[i].pid == e->pid)
46 tmp = realloc(refs->refs, (refs->ref_cnt + 1) * sizeof(*ref));
48 p_err("failed to re-alloc memory for ID %u, PID %d, COMM %s...",
49 e->id, e->pid, e->comm);
53 ref = &refs->refs[refs->ref_cnt];
55 memcpy(ref->comm, e->comm, sizeof(ref->comm));
62 refs = calloc(1, sizeof(*refs));
64 p_err("failed to alloc memory for ID %u, PID %d, COMM %s...",
65 e->id, e->pid, e->comm);
69 refs->refs = malloc(sizeof(*refs->refs));
72 p_err("failed to alloc memory for ID %u, PID %d, COMM %s...",
73 e->id, e->pid, e->comm);
78 memcpy(ref->comm, e->comm, sizeof(ref->comm));
81 err = hashmap__append(map, u32_as_hash_field(e->id), refs);
83 p_err("failed to append entry to hashmap for ID %u: %s",
84 e->id, strerror(errno));
87 static int __printf(2, 0)
88 libbpf_print_none(__maybe_unused enum libbpf_print_level level,
89 __maybe_unused const char *format,
90 __maybe_unused va_list args)
95 int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type)
97 struct pid_iter_entry *e;
98 char buf[4096 / sizeof(*e) * sizeof(*e)];
99 struct pid_iter_bpf *skel;
100 int err, ret, fd = -1, i;
101 libbpf_print_fn_t default_print;
103 *map = hashmap__new(hash_fn_for_key_as_id, equal_fn_for_key_as_id, NULL);
105 p_err("failed to create hashmap for PID references");
110 skel = pid_iter_bpf__open();
112 p_err("failed to open PID iterator skeleton");
116 skel->rodata->obj_type = type;
118 /* we don't want output polluted with libbpf errors if bpf_iter is not
121 default_print = libbpf_set_print(libbpf_print_none);
122 err = pid_iter_bpf__load(skel);
123 libbpf_set_print(default_print);
125 /* too bad, kernel doesn't support BPF iterators yet */
129 err = pid_iter_bpf__attach(skel);
131 /* if we loaded above successfully, attach has to succeed */
132 p_err("failed to attach PID iterator: %d", err);
136 fd = bpf_iter_create(bpf_link__fd(skel->links.iter));
139 p_err("failed to create PID iterator session: %d", err);
144 ret = read(fd, buf, sizeof(buf));
149 p_err("failed to read PID iterator output: %d", err);
154 if (ret % sizeof(*e)) {
156 p_err("invalid PID iterator output format");
162 for (i = 0; i < ret; i++, e++) {
170 pid_iter_bpf__destroy(skel);
174 void delete_obj_refs_table(struct hashmap *map)
176 struct hashmap_entry *entry;
182 hashmap__for_each_entry(map, entry, bkt) {
183 struct obj_refs *refs = entry->value;
192 void emit_obj_refs_json(struct hashmap *map, __u32 id,
193 json_writer_t *json_writer)
195 struct hashmap_entry *entry;
197 if (hashmap__empty(map))
200 hashmap__for_each_key_entry(map, entry, u32_as_hash_field(id)) {
201 struct obj_refs *refs = entry->value;
204 if (refs->ref_cnt == 0)
207 jsonw_name(json_writer, "pids");
208 jsonw_start_array(json_writer);
209 for (i = 0; i < refs->ref_cnt; i++) {
210 struct obj_ref *ref = &refs->refs[i];
212 jsonw_start_object(json_writer);
213 jsonw_int_field(json_writer, "pid", ref->pid);
214 jsonw_string_field(json_writer, "comm", ref->comm);
215 jsonw_end_object(json_writer);
217 jsonw_end_array(json_writer);
222 void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
224 struct hashmap_entry *entry;
226 if (hashmap__empty(map))
229 hashmap__for_each_key_entry(map, entry, u32_as_hash_field(id)) {
230 struct obj_refs *refs = entry->value;
233 if (refs->ref_cnt == 0)
236 printf("%s", prefix);
237 for (i = 0; i < refs->ref_cnt; i++) {
238 struct obj_ref *ref = &refs->refs[i];
240 printf("%s%s(%d)", i == 0 ? "" : ", ", ref->comm, ref->pid);