1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
12 #include "skeleton/pid_iter.h"
14 #ifdef BPFTOOL_WITHOUT_SKELETONS
16 int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type)
20 void delete_obj_refs_table(struct obj_refs_table *table) {}
21 void emit_obj_refs_plain(struct obj_refs_table *table, __u32 id, const char *prefix) {}
22 void emit_obj_refs_json(struct obj_refs_table *table, __u32 id, json_writer_t *json_writer) {}
24 #else /* BPFTOOL_WITHOUT_SKELETONS */
26 #include "pid_iter.skel.h"
28 static void add_ref(struct obj_refs_table *table, struct pid_iter_entry *e)
30 struct obj_refs *refs;
35 hash_for_each_possible(table->table, refs, node, e->id) {
36 if (refs->id != e->id)
39 for (i = 0; i < refs->ref_cnt; i++) {
40 if (refs->refs[i].pid == e->pid)
44 tmp = realloc(refs->refs, (refs->ref_cnt + 1) * sizeof(*ref));
46 p_err("failed to re-alloc memory for ID %u, PID %d, COMM %s...",
47 e->id, e->pid, e->comm);
51 ref = &refs->refs[refs->ref_cnt];
53 memcpy(ref->comm, e->comm, sizeof(ref->comm));
60 refs = calloc(1, sizeof(*refs));
62 p_err("failed to alloc memory for ID %u, PID %d, COMM %s...",
63 e->id, e->pid, e->comm);
68 refs->refs = malloc(sizeof(*refs->refs));
71 p_err("failed to alloc memory for ID %u, PID %d, COMM %s...",
72 e->id, e->pid, e->comm);
77 memcpy(ref->comm, e->comm, sizeof(ref->comm));
79 hash_add(table->table, &refs->node, e->id);
82 static int __printf(2, 0)
83 libbpf_print_none(__maybe_unused enum libbpf_print_level level,
84 __maybe_unused const char *format,
85 __maybe_unused va_list args)
90 int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type)
92 struct pid_iter_entry *e;
93 char buf[4096 / sizeof(*e) * sizeof(*e)];
94 struct pid_iter_bpf *skel;
95 int err, ret, fd = -1, i;
96 libbpf_print_fn_t default_print;
98 hash_init(table->table);
101 skel = pid_iter_bpf__open();
103 p_err("failed to open PID iterator skeleton");
107 skel->rodata->obj_type = type;
109 /* we don't want output polluted with libbpf errors if bpf_iter is not
112 default_print = libbpf_set_print(libbpf_print_none);
113 err = pid_iter_bpf__load(skel);
114 libbpf_set_print(default_print);
116 /* too bad, kernel doesn't support BPF iterators yet */
120 err = pid_iter_bpf__attach(skel);
122 /* if we loaded above successfully, attach has to succeed */
123 p_err("failed to attach PID iterator: %d", err);
127 fd = bpf_iter_create(bpf_link__fd(skel->links.iter));
130 p_err("failed to create PID iterator session: %d", err);
135 ret = read(fd, buf, sizeof(buf));
140 p_err("failed to read PID iterator output: %d", err);
145 if (ret % sizeof(*e)) {
147 p_err("invalid PID iterator output format");
153 for (i = 0; i < ret; i++, e++) {
161 pid_iter_bpf__destroy(skel);
165 void delete_obj_refs_table(struct obj_refs_table *table)
167 struct obj_refs *refs;
168 struct hlist_node *tmp;
171 hash_for_each_safe(table->table, bkt, tmp, refs, node) {
172 hash_del(&refs->node);
178 void emit_obj_refs_json(struct obj_refs_table *table, __u32 id,
179 json_writer_t *json_writer)
181 struct obj_refs *refs;
185 if (hash_empty(table->table))
188 hash_for_each_possible(table->table, refs, node, id) {
191 if (refs->ref_cnt == 0)
194 jsonw_name(json_writer, "pids");
195 jsonw_start_array(json_writer);
196 for (i = 0; i < refs->ref_cnt; i++) {
197 ref = &refs->refs[i];
198 jsonw_start_object(json_writer);
199 jsonw_int_field(json_writer, "pid", ref->pid);
200 jsonw_string_field(json_writer, "comm", ref->comm);
201 jsonw_end_object(json_writer);
203 jsonw_end_array(json_writer);
208 void emit_obj_refs_plain(struct obj_refs_table *table, __u32 id, const char *prefix)
210 struct obj_refs *refs;
214 if (hash_empty(table->table))
217 hash_for_each_possible(table->table, refs, node, id) {
220 if (refs->ref_cnt == 0)
223 printf("%s", prefix);
224 for (i = 0; i < refs->ref_cnt; i++) {
225 ref = &refs->refs[i];
226 printf("%s%s(%d)", i == 0 ? "" : ", ", ref->comm, ref->pid);