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