blob: a5059dc3921abf2920ec3839f08bb38c601a8ac2 [file] [log] [blame]
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001/*
2 * probe-file.c : operate ftrace k/uprobe events files
3 *
4 * Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
Masami Hiramatsudd975492016-06-15 12:28:30 +090017#include <sys/uio.h>
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090018#include "util.h"
19#include "event.h"
20#include "strlist.h"
21#include "debug.h"
22#include "cache.h"
23#include "color.h"
24#include "symbol.h"
25#include "thread.h"
Jiri Olsafbf99622015-09-02 09:56:45 +020026#include <api/fs/tracing_path.h>
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090027#include "probe-event.h"
28#include "probe-file.h"
29#include "session.h"
30
31#define MAX_CMDLEN 256
32
33static void print_open_warning(int err, bool uprobe)
34{
35 char sbuf[STRERR_BUFSIZE];
36
37 if (err == -ENOENT) {
38 const char *config;
39
40 if (uprobe)
41 config = "CONFIG_UPROBE_EVENTS";
42 else
43 config = "CONFIG_KPROBE_EVENTS";
44
45 pr_warning("%cprobe_events file does not exist"
46 " - please rebuild kernel with %s.\n",
47 uprobe ? 'u' : 'k', config);
48 } else if (err == -ENOTSUP)
49 pr_warning("Tracefs or debugfs is not mounted.\n");
50 else
51 pr_warning("Failed to open %cprobe_events: %s\n",
52 uprobe ? 'u' : 'k',
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030053 str_error_r(-err, sbuf, sizeof(sbuf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090054}
55
56static void print_both_open_warning(int kerr, int uerr)
57{
58 /* Both kprobes and uprobes are disabled, warn it. */
59 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
60 pr_warning("Tracefs or debugfs is not mounted.\n");
61 else if (kerr == -ENOENT && uerr == -ENOENT)
62 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
63 "or/and CONFIG_UPROBE_EVENTS.\n");
64 else {
65 char sbuf[STRERR_BUFSIZE];
66 pr_warning("Failed to open kprobe events: %s.\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030067 str_error_r(-kerr, sbuf, sizeof(sbuf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090068 pr_warning("Failed to open uprobe events: %s.\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030069 str_error_r(-uerr, sbuf, sizeof(sbuf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090070 }
71}
72
73static int open_probe_events(const char *trace_file, bool readwrite)
74{
75 char buf[PATH_MAX];
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090076 const char *tracing_dir = "";
77 int ret;
78
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090079 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
Jiri Olsafbf99622015-09-02 09:56:45 +020080 tracing_path, tracing_dir, trace_file);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090081 if (ret >= 0) {
82 pr_debug("Opening %s write=%d\n", buf, readwrite);
83 if (readwrite && !probe_event_dry_run)
84 ret = open(buf, O_RDWR | O_APPEND, 0);
85 else
86 ret = open(buf, O_RDONLY, 0);
87
88 if (ret < 0)
89 ret = -errno;
90 }
91 return ret;
92}
93
94static int open_kprobe_events(bool readwrite)
95{
96 return open_probe_events("kprobe_events", readwrite);
97}
98
99static int open_uprobe_events(bool readwrite)
100{
101 return open_probe_events("uprobe_events", readwrite);
102}
103
104int probe_file__open(int flag)
105{
106 int fd;
107
108 if (flag & PF_FL_UPROBE)
109 fd = open_uprobe_events(flag & PF_FL_RW);
110 else
111 fd = open_kprobe_events(flag & PF_FL_RW);
112 if (fd < 0)
113 print_open_warning(fd, flag & PF_FL_UPROBE);
114
115 return fd;
116}
117
118int probe_file__open_both(int *kfd, int *ufd, int flag)
119{
120 if (!kfd || !ufd)
121 return -EINVAL;
122
123 *kfd = open_kprobe_events(flag & PF_FL_RW);
124 *ufd = open_uprobe_events(flag & PF_FL_RW);
125 if (*kfd < 0 && *ufd < 0) {
126 print_both_open_warning(*kfd, *ufd);
127 return *kfd;
128 }
129
130 return 0;
131}
132
133/* Get raw string list of current kprobe_events or uprobe_events */
134struct strlist *probe_file__get_rawlist(int fd)
135{
136 int ret, idx;
137 FILE *fp;
138 char buf[MAX_CMDLEN];
139 char *p;
140 struct strlist *sl;
141
Wang Nan421fd082015-11-06 09:50:15 +0000142 if (fd < 0)
143 return NULL;
144
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900145 sl = strlist__new(NULL, NULL);
146
147 fp = fdopen(dup(fd), "r");
148 while (!feof(fp)) {
149 p = fgets(buf, MAX_CMDLEN, fp);
150 if (!p)
151 break;
152
153 idx = strlen(p) - 1;
154 if (p[idx] == '\n')
155 p[idx] = '\0';
156 ret = strlist__add(sl, buf);
157 if (ret < 0) {
158 pr_debug("strlist__add failed (%d)\n", ret);
159 strlist__delete(sl);
160 return NULL;
161 }
162 }
163 fclose(fp);
164
165 return sl;
166}
167
168static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
169{
170 char buf[128];
171 struct strlist *sl, *rawlist;
172 struct str_node *ent;
173 struct probe_trace_event tev;
174 int ret = 0;
175
176 memset(&tev, 0, sizeof(tev));
177 rawlist = probe_file__get_rawlist(fd);
178 if (!rawlist)
179 return NULL;
180 sl = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -0300181 strlist__for_each_entry(ent, rawlist) {
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900182 ret = parse_probe_trace_command(ent->s, &tev);
183 if (ret < 0)
184 break;
185 if (include_group) {
186 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
187 tev.event);
188 if (ret >= 0)
189 ret = strlist__add(sl, buf);
190 } else
191 ret = strlist__add(sl, tev.event);
192 clear_probe_trace_event(&tev);
193 if (ret < 0)
194 break;
195 }
196 strlist__delete(rawlist);
197
198 if (ret < 0) {
199 strlist__delete(sl);
200 return NULL;
201 }
202 return sl;
203}
204
205/* Get current perf-probe event names */
206struct strlist *probe_file__get_namelist(int fd)
207{
208 return __probe_file__get_namelist(fd, false);
209}
210
211int probe_file__add_event(int fd, struct probe_trace_event *tev)
212{
213 int ret = 0;
214 char *buf = synthesize_probe_trace_command(tev);
215 char sbuf[STRERR_BUFSIZE];
216
217 if (!buf) {
218 pr_debug("Failed to synthesize probe trace event.\n");
219 return -EINVAL;
220 }
221
222 pr_debug("Writing event: %s\n", buf);
223 if (!probe_event_dry_run) {
Masami Hiramatsu6ed07202016-04-26 18:03:04 +0900224 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900225 ret = -errno;
226 pr_warning("Failed to write event: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300227 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900228 }
229 }
230 free(buf);
231
232 return ret;
233}
234
235static int __del_trace_probe_event(int fd, struct str_node *ent)
236{
237 char *p;
238 char buf[128];
239 int ret;
240
241 /* Convert from perf-probe event to trace-probe event */
242 ret = e_snprintf(buf, 128, "-:%s", ent->s);
243 if (ret < 0)
244 goto error;
245
246 p = strchr(buf + 2, ':');
247 if (!p) {
248 pr_debug("Internal error: %s should have ':' but not.\n",
249 ent->s);
250 ret = -ENOTSUP;
251 goto error;
252 }
253 *p = '/';
254
255 pr_debug("Writing event: %s\n", buf);
256 ret = write(fd, buf, strlen(buf));
257 if (ret < 0) {
258 ret = -errno;
259 goto error;
260 }
261
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900262 return 0;
263error:
264 pr_warning("Failed to delete event: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300265 str_error_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900266 return ret;
267}
268
Namhyung Kime607f142015-09-04 21:16:03 +0900269int probe_file__get_events(int fd, struct strfilter *filter,
270 struct strlist *plist)
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900271{
272 struct strlist *namelist;
273 struct str_node *ent;
274 const char *p;
275 int ret = -ENOENT;
276
Wang Nan421fd082015-11-06 09:50:15 +0000277 if (!plist)
278 return -EINVAL;
279
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900280 namelist = __probe_file__get_namelist(fd, true);
281 if (!namelist)
282 return -ENOENT;
283
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -0300284 strlist__for_each_entry(ent, namelist) {
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900285 p = strchr(ent->s, ':');
286 if ((p && strfilter__compare(filter, p + 1)) ||
287 strfilter__compare(filter, ent->s)) {
Namhyung Kime7895e42015-09-04 21:16:02 +0900288 strlist__add(plist, ent->s);
289 ret = 0;
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900290 }
291 }
292 strlist__delete(namelist);
293
294 return ret;
295}
Namhyung Kime7895e42015-09-04 21:16:02 +0900296
Namhyung Kime607f142015-09-04 21:16:03 +0900297int probe_file__del_strlist(int fd, struct strlist *namelist)
Namhyung Kime7895e42015-09-04 21:16:02 +0900298{
299 int ret = 0;
300 struct str_node *ent;
301
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -0300302 strlist__for_each_entry(ent, namelist) {
Namhyung Kime7895e42015-09-04 21:16:02 +0900303 ret = __del_trace_probe_event(fd, ent);
304 if (ret < 0)
305 break;
306 }
307 return ret;
308}
309
310int probe_file__del_events(int fd, struct strfilter *filter)
311{
312 struct strlist *namelist;
313 int ret;
314
315 namelist = strlist__new(NULL, NULL);
316 if (!namelist)
317 return -ENOMEM;
318
319 ret = probe_file__get_events(fd, filter, namelist);
320 if (ret < 0)
321 return ret;
322
323 ret = probe_file__del_strlist(fd, namelist);
324 strlist__delete(namelist);
325
326 return ret;
327}
Masami Hiramatsudd975492016-06-15 12:28:30 +0900328
329/* Caller must ensure to remove this entry from list */
330static void probe_cache_entry__delete(struct probe_cache_entry *entry)
331{
332 if (entry) {
333 BUG_ON(!list_empty(&entry->node));
334
335 strlist__delete(entry->tevlist);
336 clear_perf_probe_event(&entry->pev);
337 zfree(&entry->spev);
338 free(entry);
339 }
340}
341
342static struct probe_cache_entry *
343probe_cache_entry__new(struct perf_probe_event *pev)
344{
345 struct probe_cache_entry *entry = zalloc(sizeof(*entry));
346
347 if (entry) {
348 INIT_LIST_HEAD(&entry->node);
349 entry->tevlist = strlist__new(NULL, NULL);
350 if (!entry->tevlist)
351 zfree(&entry);
352 else if (pev) {
353 entry->spev = synthesize_perf_probe_command(pev);
354 if (!entry->spev ||
355 perf_probe_event__copy(&entry->pev, pev) < 0) {
356 probe_cache_entry__delete(entry);
357 return NULL;
358 }
359 }
360 }
361
362 return entry;
363}
364
365/* For the kernel probe caches, pass target = NULL */
366static int probe_cache__open(struct probe_cache *pcache, const char *target)
367{
368 char cpath[PATH_MAX];
369 char sbuildid[SBUILD_ID_SIZE];
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900370 char *dir_name = NULL;
Masami Hiramatsudd975492016-06-15 12:28:30 +0900371 bool is_kallsyms = !target;
372 int ret, fd;
373
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900374 if (target && build_id_cache__cached(target)) {
375 /* This is a cached buildid */
376 strncpy(sbuildid, target, SBUILD_ID_SIZE);
377 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
378 goto found;
379 }
380
Masami Hiramatsudd975492016-06-15 12:28:30 +0900381 if (target)
382 ret = filename__sprintf_build_id(target, sbuildid);
383 else {
384 target = DSO__NAME_KALLSYMS;
385 ret = sysfs__sprintf_build_id("/", sbuildid);
386 }
387 if (ret < 0) {
388 pr_debug("Failed to get build-id from %s.\n", target);
389 return ret;
390 }
391
392 /* If we have no buildid cache, make it */
393 if (!build_id_cache__cached(sbuildid)) {
394 ret = build_id_cache__add_s(sbuildid, target,
395 is_kallsyms, NULL);
396 if (ret < 0) {
397 pr_debug("Failed to add build-id cache: %s\n", target);
398 return ret;
399 }
400 }
401
402 dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
403 false);
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900404found:
405 if (!dir_name) {
406 pr_debug("Failed to get cache from %s\n", target);
Masami Hiramatsudd975492016-06-15 12:28:30 +0900407 return -ENOMEM;
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900408 }
Masami Hiramatsudd975492016-06-15 12:28:30 +0900409
410 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
411 fd = open(cpath, O_CREAT | O_RDWR, 0644);
412 if (fd < 0)
413 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
414 free(dir_name);
415 pcache->fd = fd;
416
417 return fd;
418}
419
420static int probe_cache__load(struct probe_cache *pcache)
421{
422 struct probe_cache_entry *entry = NULL;
423 char buf[MAX_CMDLEN], *p;
424 int ret = 0;
425 FILE *fp;
426
427 fp = fdopen(dup(pcache->fd), "r");
428 if (!fp)
429 return -EINVAL;
430
431 while (!feof(fp)) {
432 if (!fgets(buf, MAX_CMDLEN, fp))
433 break;
434 p = strchr(buf, '\n');
435 if (p)
436 *p = '\0';
Masami Hiramatsu6430a94e2016-07-01 17:04:10 +0900437 /* #perf_probe_event or %sdt_event */
438 if (buf[0] == '#' || buf[0] == '%') {
Masami Hiramatsudd975492016-06-15 12:28:30 +0900439 entry = probe_cache_entry__new(NULL);
440 if (!entry) {
441 ret = -ENOMEM;
442 goto out;
443 }
Masami Hiramatsu6430a94e2016-07-01 17:04:10 +0900444 if (buf[0] == '%')
445 entry->sdt = true;
Masami Hiramatsudd975492016-06-15 12:28:30 +0900446 entry->spev = strdup(buf + 1);
447 if (entry->spev)
448 ret = parse_perf_probe_command(buf + 1,
449 &entry->pev);
450 else
451 ret = -ENOMEM;
452 if (ret < 0) {
453 probe_cache_entry__delete(entry);
454 goto out;
455 }
456 list_add_tail(&entry->node, &pcache->entries);
457 } else { /* trace_probe_event */
458 if (!entry) {
459 ret = -EINVAL;
460 goto out;
461 }
462 strlist__add(entry->tevlist, buf);
463 }
464 }
465out:
466 fclose(fp);
467 return ret;
468}
469
470static struct probe_cache *probe_cache__alloc(void)
471{
472 struct probe_cache *pcache = zalloc(sizeof(*pcache));
473
474 if (pcache) {
475 INIT_LIST_HEAD(&pcache->entries);
476 pcache->fd = -EINVAL;
477 }
478 return pcache;
479}
480
481void probe_cache__purge(struct probe_cache *pcache)
482{
483 struct probe_cache_entry *entry, *n;
484
485 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
486 list_del_init(&entry->node);
487 probe_cache_entry__delete(entry);
488 }
489}
490
491void probe_cache__delete(struct probe_cache *pcache)
492{
493 if (!pcache)
494 return;
495
496 probe_cache__purge(pcache);
497 if (pcache->fd > 0)
498 close(pcache->fd);
499 free(pcache);
500}
501
502struct probe_cache *probe_cache__new(const char *target)
503{
504 struct probe_cache *pcache = probe_cache__alloc();
505 int ret;
506
507 if (!pcache)
508 return NULL;
509
510 ret = probe_cache__open(pcache, target);
511 if (ret < 0) {
512 pr_debug("Cache open error: %d\n", ret);
513 goto out_err;
514 }
515
516 ret = probe_cache__load(pcache);
517 if (ret < 0) {
518 pr_debug("Cache read error: %d\n", ret);
519 goto out_err;
520 }
521
522 return pcache;
523
524out_err:
525 probe_cache__delete(pcache);
526 return NULL;
527}
528
529static bool streql(const char *a, const char *b)
530{
531 if (a == b)
532 return true;
533
534 if (!a || !b)
535 return false;
536
537 return !strcmp(a, b);
538}
539
Masami Hiramatsubc062232016-07-01 17:03:12 +0900540struct probe_cache_entry *
Masami Hiramatsudd975492016-06-15 12:28:30 +0900541probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
542{
543 struct probe_cache_entry *entry = NULL;
544 char *cmd = synthesize_perf_probe_command(pev);
545
546 if (!cmd)
547 return NULL;
548
549 list_for_each_entry(entry, &pcache->entries, node) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +0900550 if (pev->sdt) {
551 if (entry->pev.event &&
552 streql(entry->pev.event, pev->event) &&
553 (!pev->group ||
554 streql(entry->pev.group, pev->group)))
555 goto found;
556
557 continue;
558 }
Masami Hiramatsudd975492016-06-15 12:28:30 +0900559 /* Hit if same event name or same command-string */
560 if ((pev->event &&
561 (streql(entry->pev.group, pev->group) &&
562 streql(entry->pev.event, pev->event))) ||
563 (!strcmp(entry->spev, cmd)))
564 goto found;
565 }
566 entry = NULL;
567
568found:
569 free(cmd);
570 return entry;
571}
572
Masami Hiramatsubc062232016-07-01 17:03:12 +0900573struct probe_cache_entry *
574probe_cache__find_by_name(struct probe_cache *pcache,
575 const char *group, const char *event)
576{
577 struct probe_cache_entry *entry = NULL;
578
579 list_for_each_entry(entry, &pcache->entries, node) {
580 /* Hit if same event name or same command-string */
581 if (streql(entry->pev.group, group) &&
582 streql(entry->pev.event, event))
583 goto found;
584 }
585 entry = NULL;
586
587found:
588 return entry;
589}
590
Masami Hiramatsudd975492016-06-15 12:28:30 +0900591int probe_cache__add_entry(struct probe_cache *pcache,
592 struct perf_probe_event *pev,
593 struct probe_trace_event *tevs, int ntevs)
594{
595 struct probe_cache_entry *entry = NULL;
596 char *command;
597 int i, ret = 0;
598
599 if (!pcache || !pev || !tevs || ntevs <= 0) {
600 ret = -EINVAL;
601 goto out_err;
602 }
603
604 /* Remove old cache entry */
605 entry = probe_cache__find(pcache, pev);
606 if (entry) {
607 list_del_init(&entry->node);
608 probe_cache_entry__delete(entry);
609 }
610
611 ret = -ENOMEM;
612 entry = probe_cache_entry__new(pev);
613 if (!entry)
614 goto out_err;
615
616 for (i = 0; i < ntevs; i++) {
617 if (!tevs[i].point.symbol)
618 continue;
619
620 command = synthesize_probe_trace_command(&tevs[i]);
621 if (!command)
622 goto out_err;
623 strlist__add(entry->tevlist, command);
624 free(command);
625 }
626 list_add_tail(&entry->node, &pcache->entries);
627 pr_debug("Added probe cache: %d\n", ntevs);
628 return 0;
629
630out_err:
631 pr_debug("Failed to add probe caches\n");
632 probe_cache_entry__delete(entry);
633 return ret;
634}
635
Arnaldo Carvalho de Melo1c1a3a42016-07-12 12:19:09 -0300636#ifdef HAVE_GELF_GETNOTE_SUPPORT
Masami Hiramatsu6430a94e2016-07-01 17:04:10 +0900637static unsigned long long sdt_note__get_addr(struct sdt_note *note)
638{
639 return note->bit32 ? (unsigned long long)note->addr.a32[0]
640 : (unsigned long long)note->addr.a64[0];
641}
642
643int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
644{
645 struct probe_cache_entry *entry = NULL;
646 struct list_head sdtlist;
647 struct sdt_note *note;
648 char *buf;
649 char sdtgrp[64];
650 int ret;
651
652 INIT_LIST_HEAD(&sdtlist);
653 ret = get_sdt_note_list(&sdtlist, pathname);
654 if (ret < 0) {
655 pr_debug("Failed to get sdt note: %d\n", ret);
656 return ret;
657 }
658 list_for_each_entry(note, &sdtlist, note_list) {
659 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
660 if (ret < 0)
661 break;
662 /* Try to find same-name entry */
663 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
664 if (!entry) {
665 entry = probe_cache_entry__new(NULL);
666 if (!entry) {
667 ret = -ENOMEM;
668 break;
669 }
670 entry->sdt = true;
671 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
672 note->name, note->name);
673 if (ret < 0)
674 break;
675 entry->pev.event = strdup(note->name);
676 entry->pev.group = strdup(sdtgrp);
677 list_add_tail(&entry->node, &pcache->entries);
678 }
679 ret = asprintf(&buf, "p:%s/%s %s:0x%llx",
680 sdtgrp, note->name, pathname,
681 sdt_note__get_addr(note));
682 if (ret < 0)
683 break;
684 strlist__add(entry->tevlist, buf);
685 free(buf);
686 entry = NULL;
687 }
688 if (entry) {
689 list_del_init(&entry->node);
690 probe_cache_entry__delete(entry);
691 }
692 cleanup_sdt_note_list(&sdtlist);
693 return ret;
694}
Arnaldo Carvalho de Melo1c1a3a42016-07-12 12:19:09 -0300695#endif
Masami Hiramatsu6430a94e2016-07-01 17:04:10 +0900696
Masami Hiramatsudd975492016-06-15 12:28:30 +0900697static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
698{
699 struct str_node *snode;
700 struct stat st;
701 struct iovec iov[3];
Masami Hiramatsu6430a94e2016-07-01 17:04:10 +0900702 const char *prefix = entry->sdt ? "%" : "#";
Masami Hiramatsudd975492016-06-15 12:28:30 +0900703 int ret;
704 /* Save stat for rollback */
705 ret = fstat(fd, &st);
706 if (ret < 0)
707 return ret;
708
Masami Hiramatsu6430a94e2016-07-01 17:04:10 +0900709 pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
710 iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
Masami Hiramatsudd975492016-06-15 12:28:30 +0900711 iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
712 iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
713 ret = writev(fd, iov, 3);
714 if (ret < (int)iov[1].iov_len + 2)
715 goto rollback;
716
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -0300717 strlist__for_each_entry(snode, entry->tevlist) {
Masami Hiramatsudd975492016-06-15 12:28:30 +0900718 iov[0].iov_base = (void *)snode->s;
719 iov[0].iov_len = strlen(snode->s);
720 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
721 ret = writev(fd, iov, 2);
722 if (ret < (int)iov[0].iov_len + 1)
723 goto rollback;
724 }
725 return 0;
726
727rollback:
728 /* Rollback to avoid cache file corruption */
729 if (ret > 0)
730 ret = -1;
731 if (ftruncate(fd, st.st_size) < 0)
732 ret = -2;
733
734 return ret;
735}
736
737int probe_cache__commit(struct probe_cache *pcache)
738{
739 struct probe_cache_entry *entry;
740 int ret = 0;
741
742 /* TBD: if we do not update existing entries, skip it */
743 ret = lseek(pcache->fd, 0, SEEK_SET);
744 if (ret < 0)
745 goto out;
746
747 ret = ftruncate(pcache->fd, 0);
748 if (ret < 0)
749 goto out;
750
751 list_for_each_entry(entry, &pcache->entries, node) {
752 ret = probe_cache_entry__write(entry, pcache->fd);
753 pr_debug("Cache committed: %d\n", ret);
754 if (ret < 0)
755 break;
756 }
757out:
758 return ret;
759}
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900760
Masami Hiramatsu4a0f65c2016-07-01 17:03:36 +0900761static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
762 struct strfilter *filter)
763{
764 char buf[128], *ptr = entry->spev;
765
766 if (entry->pev.event) {
767 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
768 ptr = buf;
769 }
770 return strfilter__compare(filter, ptr);
771}
772
773int probe_cache__filter_purge(struct probe_cache *pcache,
774 struct strfilter *filter)
775{
776 struct probe_cache_entry *entry, *tmp;
777
778 list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
779 if (probe_cache_entry__compare(entry, filter)) {
780 pr_info("Removed cached event: %s\n", entry->spev);
781 list_del_init(&entry->node);
782 probe_cache_entry__delete(entry);
783 }
784 }
785 return 0;
786}
787
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900788static int probe_cache__show_entries(struct probe_cache *pcache,
789 struct strfilter *filter)
790{
791 struct probe_cache_entry *entry;
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900792
793 list_for_each_entry(entry, &pcache->entries, node) {
Masami Hiramatsu4a0f65c2016-07-01 17:03:36 +0900794 if (probe_cache_entry__compare(entry, filter))
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900795 printf("%s\n", entry->spev);
796 }
797 return 0;
798}
799
800/* Show all cached probes */
801int probe_cache__show_all_caches(struct strfilter *filter)
802{
803 struct probe_cache *pcache;
804 struct strlist *bidlist;
805 struct str_node *nd;
806 char *buf = strfilter__string(filter);
807
808 pr_debug("list cache with filter: %s\n", buf);
809 free(buf);
810
Masami Hiramatsuc3492a32016-07-12 19:04:54 +0900811 bidlist = build_id_cache__list_all(true);
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900812 if (!bidlist) {
813 pr_debug("Failed to get buildids: %d\n", errno);
814 return -EINVAL;
815 }
816 strlist__for_each_entry(nd, bidlist) {
817 pcache = probe_cache__new(nd->s);
818 if (!pcache)
819 continue;
820 if (!list_empty(&pcache->entries)) {
821 buf = build_id_cache__origname(nd->s);
822 printf("%s (%s):\n", buf, nd->s);
823 free(buf);
824 probe_cache__show_entries(pcache, filter);
825 }
826 probe_cache__delete(pcache);
827 }
828 strlist__delete(bidlist);
829
830 return 0;
831}