libbpf-tools: update gethostlatency for libbpf 1.0
[platform/upstream/bcc.git] / libbpf-tools / gethostlatency.c
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2
3 /*
4  * gethostlatency  Show latency for getaddrinfo/gethostbyname[2] calls.
5  *
6  * Copyright (c) 2021 Hengqi Chen
7  *
8  * Based on gethostlatency(8) from BCC by Brendan Gregg.
9  * 24-Mar-2021   Hengqi Chen   Created this.
10  */
11 #include <argp.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <time.h>
15
16 #include <bpf/libbpf.h>
17 #include <bpf/bpf.h>
18 #include "gethostlatency.h"
19 #include "gethostlatency.skel.h"
20 #include "trace_helpers.h"
21 #include "uprobe_helpers.h"
22
23 #define PERF_BUFFER_PAGES       16
24 #define PERF_POLL_TIMEOUT_MS    100
25 #define warn(...) fprintf(stderr, __VA_ARGS__)
26
27 static volatile sig_atomic_t exiting = 0;
28
29 static pid_t target_pid = 0;
30 static const char *libc_path = NULL;
31
32 const char *argp_program_version = "gethostlatency 0.1";
33 const char *argp_program_bug_address =
34         "https://github.com/iovisor/bcc/tree/master/libbpf-tools";
35 const char argp_program_doc[] =
36 "Show latency for getaddrinfo/gethostbyname[2] calls.\n"
37 "\n"
38 "USAGE: gethostlatency [-h] [-p PID] [-l LIBC]\n"
39 "\n"
40 "EXAMPLES:\n"
41 "    gethostlatency             # time getaddrinfo/gethostbyname[2] calls\n"
42 "    gethostlatency -p 1216     # only trace PID 1216\n";
43
44 static const struct argp_option opts[] = {
45         { "pid", 'p', "PID", 0, "Process ID to trace" },
46         { "libc", 'l', "LIBC", 0, "Specify which libc.so to use" },
47         { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
48         {},
49 };
50
51 static error_t parse_arg(int key, char *arg, struct argp_state *state)
52 {
53         long pid;
54
55         switch (key) {
56         case 'p':
57                 errno = 0;
58                 pid = strtol(arg, NULL, 10);
59                 if (errno || pid <= 0) {
60                         warn("Invalid PID: %s\n", arg);
61                         argp_usage(state);
62                 }
63                 target_pid = pid;
64                 break;
65         case 'l':
66                 libc_path = strdup(arg);
67                 if (access(libc_path, F_OK)) {
68                         warn("Invalid libc: %s\n", arg);
69                         argp_usage(state);
70                 }
71                 break;
72         case 'h':
73                 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
74                 break;
75         default:
76                 return ARGP_ERR_UNKNOWN;
77         }
78         return 0;
79 }
80
81 static void sig_int(int signo)
82 {
83         exiting = 1;
84 }
85
86 static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
87 {
88         const struct event *e = data;
89         struct tm *tm;
90         char ts[16];
91         time_t t;
92
93         time(&t);
94         tm = localtime(&t);
95         strftime(ts, sizeof(ts), "%H:%M:%S", tm);
96         printf("%-8s %-7d %-16s %-10.3f %-s\n",
97                ts, e->pid, e->comm, (double)e->time/1000000, e->host);
98 }
99
100 static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
101 {
102         warn("lost %llu events on CPU #%d\n", lost_cnt, cpu);
103 }
104
105 static int get_libc_path(char *path)
106 {
107         FILE *f;
108         char buf[PATH_MAX] = {};
109         char *filename;
110         float version;
111
112         if (libc_path) {
113                 memcpy(path, libc_path, strlen(libc_path));
114                 return 0;
115         }
116
117         f = fopen("/proc/self/maps", "r");
118         if (!f)
119                 return -errno;
120
121         while (fscanf(f, "%*x-%*x %*s %*s %*s %*s %[^\n]\n", buf) != EOF) {
122                 if (strchr(buf, '/') != buf)
123                         continue;
124                 filename = strrchr(buf, '/') + 1;
125                 if (sscanf(filename, "libc-%f.so", &version) == 1) {
126                         memcpy(path, buf, strlen(buf));
127                         fclose(f);
128                         return 0;
129                 }
130         }
131
132         fclose(f);
133         return -1;
134 }
135
136 static int attach_uprobes(struct gethostlatency_bpf *obj, struct bpf_link *links[])
137 {
138         int err;
139         char libc_path[PATH_MAX] = {};
140         off_t func_off;
141
142         err = get_libc_path(libc_path);
143         if (err) {
144                 warn("could not find libc.so\n");
145                 return -1;
146         }
147
148         func_off = get_elf_func_offset(libc_path, "getaddrinfo");
149         if (func_off < 0) {
150                 warn("could not find getaddrinfo in %s\n", libc_path);
151                 return -1;
152         }
153         links[0] = bpf_program__attach_uprobe(obj->progs.handle_entry, false,
154                                               target_pid ?: -1, libc_path, func_off);
155         if (!links[0]) {
156                 warn("failed to attach getaddrinfo: %d\n", -errno);
157                 return -1;
158         }
159         links[1] = bpf_program__attach_uprobe(obj->progs.handle_return, true,
160                                               target_pid ?: -1, libc_path, func_off);
161         if (!links[1]) {
162                 warn("failed to attach getaddrinfo: %d\n", -errno);
163                 return -1;
164         }
165
166         func_off = get_elf_func_offset(libc_path, "gethostbyname");
167         if (func_off < 0) {
168                 warn("could not find gethostbyname in %s\n", libc_path);
169                 return -1;
170         }
171         links[2] = bpf_program__attach_uprobe(obj->progs.handle_entry, false,
172                                               target_pid ?: -1, libc_path, func_off);
173         if (!links[2]) {
174                 warn("failed to attach gethostbyname: %d\n", -errno);
175                 return -1;
176         }
177         links[3] = bpf_program__attach_uprobe(obj->progs.handle_return, true,
178                                               target_pid ?: -1, libc_path, func_off);
179         if (!links[3]) {
180                 warn("failed to attach gethostbyname: %d\n", -errno);
181                 return -1;
182         }
183
184         func_off = get_elf_func_offset(libc_path, "gethostbyname2");
185         if (func_off < 0) {
186                 warn("could not find gethostbyname2 in %s\n", libc_path);
187                 return -1;
188         }
189         links[4] = bpf_program__attach_uprobe(obj->progs.handle_entry, false,
190                                               target_pid ?: -1, libc_path, func_off);
191         if (!links[4]) {
192                 warn("failed to attach gethostbyname2: %d\n", -errno);
193                 return -1;
194         }
195         links[5] = bpf_program__attach_uprobe(obj->progs.handle_return, true,
196                                               target_pid ?: -1, libc_path, func_off);
197         if (!links[5]) {
198                 warn("failed to attach gethostbyname2: %d\n", -errno);
199                 return -1;
200         }
201
202         return 0;
203 }
204
205 int main(int argc, char **argv)
206 {
207         static const struct argp argp = {
208                 .options = opts,
209                 .parser = parse_arg,
210                 .doc = argp_program_doc,
211         };
212         struct perf_buffer *pb = NULL;
213         struct bpf_link *links[6] = {};
214         struct gethostlatency_bpf *obj;
215         int i, err;
216
217         err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
218         if (err)
219                 return err;
220
221         libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
222
223         obj = gethostlatency_bpf__open();
224         if (!obj) {
225                 warn("failed to open BPF object\n");
226                 return 1;
227         }
228
229         obj->rodata->target_pid = target_pid;
230
231         err = gethostlatency_bpf__load(obj);
232         if (err) {
233                 warn("failed to load BPF object: %d\n", err);
234                 goto cleanup;
235         }
236
237         err = attach_uprobes(obj, links);
238         if (err)
239                 goto cleanup;
240
241         pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES,
242                               handle_event, handle_lost_events, NULL, NULL);
243         if (!pb) {
244                 err = -errno;
245                 warn("failed to open perf buffer: %d\n", err);
246                 goto cleanup;
247         }
248
249         if (signal(SIGINT, sig_int) == SIG_ERR) {
250                 warn("can't set signal handler: %s\n", strerror(errno));
251                 err = 1;
252                 goto cleanup;
253         }
254
255         printf("%-8s %-7s %-16s %-10s %-s\n",
256                "TIME", "PID", "COMM", "LATms", "HOST");
257
258         while (!exiting) {
259                 err = perf_buffer__poll(pb, PERF_POLL_TIMEOUT_MS);
260                 if (err < 0 && err != -EINTR) {
261                         warn("error polling perf buffer: %s\n", strerror(-err));
262                         goto cleanup;
263                 }
264                 /* reset err to return 0 if exiting */
265                 err = 0;
266         }
267
268 cleanup:
269         perf_buffer__free(pb);
270         for (i = 0; i < 6; i++)
271                 bpf_link__destroy(links[i]);
272         gethostlatency_bpf__destroy(obj);
273
274         return err != 0;
275 }