perf probe: Check for dup and fdopen failures
[linux-2.6-block.git] / tools / perf / util / probe-file.c
1 /*
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  */
17 #include <sys/uio.h>
18 #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"
26 #include <api/fs/tracing_path.h>
27 #include "probe-event.h"
28 #include "probe-file.h"
29 #include "session.h"
30
31 #define MAX_CMDLEN 256
32
33 static 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',
53                            str_error_r(-err, sbuf, sizeof(sbuf)));
54 }
55
56 static 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",
67                            str_error_r(-kerr, sbuf, sizeof(sbuf)));
68                 pr_warning("Failed to open uprobe events: %s.\n",
69                            str_error_r(-uerr, sbuf, sizeof(sbuf)));
70         }
71 }
72
73 static int open_probe_events(const char *trace_file, bool readwrite)
74 {
75         char buf[PATH_MAX];
76         const char *tracing_dir = "";
77         int ret;
78
79         ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
80                          tracing_path, tracing_dir, trace_file);
81         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
94 static int open_kprobe_events(bool readwrite)
95 {
96         return open_probe_events("kprobe_events", readwrite);
97 }
98
99 static int open_uprobe_events(bool readwrite)
100 {
101         return open_probe_events("uprobe_events", readwrite);
102 }
103
104 int 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
118 int 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 */
134 struct strlist *probe_file__get_rawlist(int fd)
135 {
136         int ret, idx, fddup;
137         FILE *fp;
138         char buf[MAX_CMDLEN];
139         char *p;
140         struct strlist *sl;
141
142         if (fd < 0)
143                 return NULL;
144
145         sl = strlist__new(NULL, NULL);
146
147         fddup = dup(fd);
148         if (fddup < 0)
149                 goto out_free_sl;
150
151         fp = fdopen(fddup, "r");
152         if (!fp)
153                 goto out_close_fddup;
154
155         while (!feof(fp)) {
156                 p = fgets(buf, MAX_CMDLEN, fp);
157                 if (!p)
158                         break;
159
160                 idx = strlen(p) - 1;
161                 if (p[idx] == '\n')
162                         p[idx] = '\0';
163                 ret = strlist__add(sl, buf);
164                 if (ret < 0) {
165                         pr_debug("strlist__add failed (%d)\n", ret);
166                         strlist__delete(sl);
167                         return NULL;
168                 }
169         }
170         fclose(fp);
171
172         return sl;
173
174 out_close_fddup:
175         close(fddup);
176 out_free_sl:
177         strlist__delete(sl);
178         return NULL;
179 }
180
181 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
182 {
183         char buf[128];
184         struct strlist *sl, *rawlist;
185         struct str_node *ent;
186         struct probe_trace_event tev;
187         int ret = 0;
188
189         memset(&tev, 0, sizeof(tev));
190         rawlist = probe_file__get_rawlist(fd);
191         if (!rawlist)
192                 return NULL;
193         sl = strlist__new(NULL, NULL);
194         strlist__for_each_entry(ent, rawlist) {
195                 ret = parse_probe_trace_command(ent->s, &tev);
196                 if (ret < 0)
197                         break;
198                 if (include_group) {
199                         ret = e_snprintf(buf, 128, "%s:%s", tev.group,
200                                         tev.event);
201                         if (ret >= 0)
202                                 ret = strlist__add(sl, buf);
203                 } else
204                         ret = strlist__add(sl, tev.event);
205                 clear_probe_trace_event(&tev);
206                 if (ret < 0)
207                         break;
208         }
209         strlist__delete(rawlist);
210
211         if (ret < 0) {
212                 strlist__delete(sl);
213                 return NULL;
214         }
215         return sl;
216 }
217
218 /* Get current perf-probe event names */
219 struct strlist *probe_file__get_namelist(int fd)
220 {
221         return __probe_file__get_namelist(fd, false);
222 }
223
224 int probe_file__add_event(int fd, struct probe_trace_event *tev)
225 {
226         int ret = 0;
227         char *buf = synthesize_probe_trace_command(tev);
228         char sbuf[STRERR_BUFSIZE];
229
230         if (!buf) {
231                 pr_debug("Failed to synthesize probe trace event.\n");
232                 return -EINVAL;
233         }
234
235         pr_debug("Writing event: %s\n", buf);
236         if (!probe_event_dry_run) {
237                 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
238                         ret = -errno;
239                         pr_warning("Failed to write event: %s\n",
240                                    str_error_r(errno, sbuf, sizeof(sbuf)));
241                 }
242         }
243         free(buf);
244
245         return ret;
246 }
247
248 static int __del_trace_probe_event(int fd, struct str_node *ent)
249 {
250         char *p;
251         char buf[128];
252         int ret;
253
254         /* Convert from perf-probe event to trace-probe event */
255         ret = e_snprintf(buf, 128, "-:%s", ent->s);
256         if (ret < 0)
257                 goto error;
258
259         p = strchr(buf + 2, ':');
260         if (!p) {
261                 pr_debug("Internal error: %s should have ':' but not.\n",
262                          ent->s);
263                 ret = -ENOTSUP;
264                 goto error;
265         }
266         *p = '/';
267
268         pr_debug("Writing event: %s\n", buf);
269         ret = write(fd, buf, strlen(buf));
270         if (ret < 0) {
271                 ret = -errno;
272                 goto error;
273         }
274
275         return 0;
276 error:
277         pr_warning("Failed to delete event: %s\n",
278                    str_error_r(-ret, buf, sizeof(buf)));
279         return ret;
280 }
281
282 int probe_file__get_events(int fd, struct strfilter *filter,
283                            struct strlist *plist)
284 {
285         struct strlist *namelist;
286         struct str_node *ent;
287         const char *p;
288         int ret = -ENOENT;
289
290         if (!plist)
291                 return -EINVAL;
292
293         namelist = __probe_file__get_namelist(fd, true);
294         if (!namelist)
295                 return -ENOENT;
296
297         strlist__for_each_entry(ent, namelist) {
298                 p = strchr(ent->s, ':');
299                 if ((p && strfilter__compare(filter, p + 1)) ||
300                     strfilter__compare(filter, ent->s)) {
301                         strlist__add(plist, ent->s);
302                         ret = 0;
303                 }
304         }
305         strlist__delete(namelist);
306
307         return ret;
308 }
309
310 int probe_file__del_strlist(int fd, struct strlist *namelist)
311 {
312         int ret = 0;
313         struct str_node *ent;
314
315         strlist__for_each_entry(ent, namelist) {
316                 ret = __del_trace_probe_event(fd, ent);
317                 if (ret < 0)
318                         break;
319         }
320         return ret;
321 }
322
323 int probe_file__del_events(int fd, struct strfilter *filter)
324 {
325         struct strlist *namelist;
326         int ret;
327
328         namelist = strlist__new(NULL, NULL);
329         if (!namelist)
330                 return -ENOMEM;
331
332         ret = probe_file__get_events(fd, filter, namelist);
333         if (ret < 0)
334                 return ret;
335
336         ret = probe_file__del_strlist(fd, namelist);
337         strlist__delete(namelist);
338
339         return ret;
340 }
341
342 /* Caller must ensure to remove this entry from list */
343 static void probe_cache_entry__delete(struct probe_cache_entry *entry)
344 {
345         if (entry) {
346                 BUG_ON(!list_empty(&entry->node));
347
348                 strlist__delete(entry->tevlist);
349                 clear_perf_probe_event(&entry->pev);
350                 zfree(&entry->spev);
351                 free(entry);
352         }
353 }
354
355 static struct probe_cache_entry *
356 probe_cache_entry__new(struct perf_probe_event *pev)
357 {
358         struct probe_cache_entry *entry = zalloc(sizeof(*entry));
359
360         if (entry) {
361                 INIT_LIST_HEAD(&entry->node);
362                 entry->tevlist = strlist__new(NULL, NULL);
363                 if (!entry->tevlist)
364                         zfree(&entry);
365                 else if (pev) {
366                         entry->spev = synthesize_perf_probe_command(pev);
367                         if (!entry->spev ||
368                             perf_probe_event__copy(&entry->pev, pev) < 0) {
369                                 probe_cache_entry__delete(entry);
370                                 return NULL;
371                         }
372                 }
373         }
374
375         return entry;
376 }
377
378 int probe_cache_entry__get_event(struct probe_cache_entry *entry,
379                                  struct probe_trace_event **tevs)
380 {
381         struct probe_trace_event *tev;
382         struct str_node *node;
383         int ret, i;
384
385         ret = strlist__nr_entries(entry->tevlist);
386         if (ret > probe_conf.max_probes)
387                 return -E2BIG;
388
389         *tevs = zalloc(ret * sizeof(*tev));
390         if (!*tevs)
391                 return -ENOMEM;
392
393         i = 0;
394         strlist__for_each_entry(node, entry->tevlist) {
395                 tev = &(*tevs)[i++];
396                 ret = parse_probe_trace_command(node->s, tev);
397                 if (ret < 0)
398                         break;
399         }
400         return i;
401 }
402
403 /* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
404 static int probe_cache__open(struct probe_cache *pcache, const char *target)
405 {
406         char cpath[PATH_MAX];
407         char sbuildid[SBUILD_ID_SIZE];
408         char *dir_name = NULL;
409         bool is_kallsyms = false;
410         int ret, fd;
411
412         if (target && build_id_cache__cached(target)) {
413                 /* This is a cached buildid */
414                 strncpy(sbuildid, target, SBUILD_ID_SIZE);
415                 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
416                 goto found;
417         }
418
419         if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
420                 target = DSO__NAME_KALLSYMS;
421                 is_kallsyms = true;
422                 ret = sysfs__sprintf_build_id("/", sbuildid);
423         } else
424                 ret = filename__sprintf_build_id(target, sbuildid);
425
426         if (ret < 0) {
427                 pr_debug("Failed to get build-id from %s.\n", target);
428                 return ret;
429         }
430
431         /* If we have no buildid cache, make it */
432         if (!build_id_cache__cached(sbuildid)) {
433                 ret = build_id_cache__add_s(sbuildid, target,
434                                             is_kallsyms, NULL);
435                 if (ret < 0) {
436                         pr_debug("Failed to add build-id cache: %s\n", target);
437                         return ret;
438                 }
439         }
440
441         dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
442                                             false);
443 found:
444         if (!dir_name) {
445                 pr_debug("Failed to get cache from %s\n", target);
446                 return -ENOMEM;
447         }
448
449         snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
450         fd = open(cpath, O_CREAT | O_RDWR, 0644);
451         if (fd < 0)
452                 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
453         free(dir_name);
454         pcache->fd = fd;
455
456         return fd;
457 }
458
459 static int probe_cache__load(struct probe_cache *pcache)
460 {
461         struct probe_cache_entry *entry = NULL;
462         char buf[MAX_CMDLEN], *p;
463         int ret = 0, fddup;
464         FILE *fp;
465
466         fddup = dup(pcache->fd);
467         if (fddup < 0)
468                 return -errno;
469         fp = fdopen(fddup, "r");
470         if (!fp)
471                 return -EINVAL;
472
473         while (!feof(fp)) {
474                 if (!fgets(buf, MAX_CMDLEN, fp))
475                         break;
476                 p = strchr(buf, '\n');
477                 if (p)
478                         *p = '\0';
479                 /* #perf_probe_event or %sdt_event */
480                 if (buf[0] == '#' || buf[0] == '%') {
481                         entry = probe_cache_entry__new(NULL);
482                         if (!entry) {
483                                 ret = -ENOMEM;
484                                 goto out;
485                         }
486                         if (buf[0] == '%')
487                                 entry->sdt = true;
488                         entry->spev = strdup(buf + 1);
489                         if (entry->spev)
490                                 ret = parse_perf_probe_command(buf + 1,
491                                                                 &entry->pev);
492                         else
493                                 ret = -ENOMEM;
494                         if (ret < 0) {
495                                 probe_cache_entry__delete(entry);
496                                 goto out;
497                         }
498                         list_add_tail(&entry->node, &pcache->entries);
499                 } else {        /* trace_probe_event */
500                         if (!entry) {
501                                 ret = -EINVAL;
502                                 goto out;
503                         }
504                         strlist__add(entry->tevlist, buf);
505                 }
506         }
507 out:
508         fclose(fp);
509         return ret;
510 }
511
512 static struct probe_cache *probe_cache__alloc(void)
513 {
514         struct probe_cache *pcache = zalloc(sizeof(*pcache));
515
516         if (pcache) {
517                 INIT_LIST_HEAD(&pcache->entries);
518                 pcache->fd = -EINVAL;
519         }
520         return pcache;
521 }
522
523 void probe_cache__purge(struct probe_cache *pcache)
524 {
525         struct probe_cache_entry *entry, *n;
526
527         list_for_each_entry_safe(entry, n, &pcache->entries, node) {
528                 list_del_init(&entry->node);
529                 probe_cache_entry__delete(entry);
530         }
531 }
532
533 void probe_cache__delete(struct probe_cache *pcache)
534 {
535         if (!pcache)
536                 return;
537
538         probe_cache__purge(pcache);
539         if (pcache->fd > 0)
540                 close(pcache->fd);
541         free(pcache);
542 }
543
544 struct probe_cache *probe_cache__new(const char *target)
545 {
546         struct probe_cache *pcache = probe_cache__alloc();
547         int ret;
548
549         if (!pcache)
550                 return NULL;
551
552         ret = probe_cache__open(pcache, target);
553         if (ret < 0) {
554                 pr_debug("Cache open error: %d\n", ret);
555                 goto out_err;
556         }
557
558         ret = probe_cache__load(pcache);
559         if (ret < 0) {
560                 pr_debug("Cache read error: %d\n", ret);
561                 goto out_err;
562         }
563
564         return pcache;
565
566 out_err:
567         probe_cache__delete(pcache);
568         return NULL;
569 }
570
571 static bool streql(const char *a, const char *b)
572 {
573         if (a == b)
574                 return true;
575
576         if (!a || !b)
577                 return false;
578
579         return !strcmp(a, b);
580 }
581
582 struct probe_cache_entry *
583 probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
584 {
585         struct probe_cache_entry *entry = NULL;
586         char *cmd = synthesize_perf_probe_command(pev);
587
588         if (!cmd)
589                 return NULL;
590
591         for_each_probe_cache_entry(entry, pcache) {
592                 if (pev->sdt) {
593                         if (entry->pev.event &&
594                             streql(entry->pev.event, pev->event) &&
595                             (!pev->group ||
596                              streql(entry->pev.group, pev->group)))
597                                 goto found;
598
599                         continue;
600                 }
601                 /* Hit if same event name or same command-string */
602                 if ((pev->event &&
603                      (streql(entry->pev.group, pev->group) &&
604                       streql(entry->pev.event, pev->event))) ||
605                     (!strcmp(entry->spev, cmd)))
606                         goto found;
607         }
608         entry = NULL;
609
610 found:
611         free(cmd);
612         return entry;
613 }
614
615 struct probe_cache_entry *
616 probe_cache__find_by_name(struct probe_cache *pcache,
617                           const char *group, const char *event)
618 {
619         struct probe_cache_entry *entry = NULL;
620
621         for_each_probe_cache_entry(entry, pcache) {
622                 /* Hit if same event name or same command-string */
623                 if (streql(entry->pev.group, group) &&
624                     streql(entry->pev.event, event))
625                         goto found;
626         }
627         entry = NULL;
628
629 found:
630         return entry;
631 }
632
633 int probe_cache__add_entry(struct probe_cache *pcache,
634                            struct perf_probe_event *pev,
635                            struct probe_trace_event *tevs, int ntevs)
636 {
637         struct probe_cache_entry *entry = NULL;
638         char *command;
639         int i, ret = 0;
640
641         if (!pcache || !pev || !tevs || ntevs <= 0) {
642                 ret = -EINVAL;
643                 goto out_err;
644         }
645
646         /* Remove old cache entry */
647         entry = probe_cache__find(pcache, pev);
648         if (entry) {
649                 list_del_init(&entry->node);
650                 probe_cache_entry__delete(entry);
651         }
652
653         ret = -ENOMEM;
654         entry = probe_cache_entry__new(pev);
655         if (!entry)
656                 goto out_err;
657
658         for (i = 0; i < ntevs; i++) {
659                 if (!tevs[i].point.symbol)
660                         continue;
661
662                 command = synthesize_probe_trace_command(&tevs[i]);
663                 if (!command)
664                         goto out_err;
665                 strlist__add(entry->tevlist, command);
666                 free(command);
667         }
668         list_add_tail(&entry->node, &pcache->entries);
669         pr_debug("Added probe cache: %d\n", ntevs);
670         return 0;
671
672 out_err:
673         pr_debug("Failed to add probe caches\n");
674         probe_cache_entry__delete(entry);
675         return ret;
676 }
677
678 #ifdef HAVE_GELF_GETNOTE_SUPPORT
679 static unsigned long long sdt_note__get_addr(struct sdt_note *note)
680 {
681         return note->bit32 ? (unsigned long long)note->addr.a32[0]
682                  : (unsigned long long)note->addr.a64[0];
683 }
684
685 int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
686 {
687         struct probe_cache_entry *entry = NULL;
688         struct list_head sdtlist;
689         struct sdt_note *note;
690         char *buf;
691         char sdtgrp[64];
692         int ret;
693
694         INIT_LIST_HEAD(&sdtlist);
695         ret = get_sdt_note_list(&sdtlist, pathname);
696         if (ret < 0) {
697                 pr_debug("Failed to get sdt note: %d\n", ret);
698                 return ret;
699         }
700         list_for_each_entry(note, &sdtlist, note_list) {
701                 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
702                 if (ret < 0)
703                         break;
704                 /* Try to find same-name entry */
705                 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
706                 if (!entry) {
707                         entry = probe_cache_entry__new(NULL);
708                         if (!entry) {
709                                 ret = -ENOMEM;
710                                 break;
711                         }
712                         entry->sdt = true;
713                         ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
714                                         note->name, note->name);
715                         if (ret < 0)
716                                 break;
717                         entry->pev.event = strdup(note->name);
718                         entry->pev.group = strdup(sdtgrp);
719                         list_add_tail(&entry->node, &pcache->entries);
720                 }
721                 ret = asprintf(&buf, "p:%s/%s %s:0x%llx",
722                                 sdtgrp, note->name, pathname,
723                                 sdt_note__get_addr(note));
724                 if (ret < 0)
725                         break;
726                 strlist__add(entry->tevlist, buf);
727                 free(buf);
728                 entry = NULL;
729         }
730         if (entry) {
731                 list_del_init(&entry->node);
732                 probe_cache_entry__delete(entry);
733         }
734         cleanup_sdt_note_list(&sdtlist);
735         return ret;
736 }
737 #endif
738
739 static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
740 {
741         struct str_node *snode;
742         struct stat st;
743         struct iovec iov[3];
744         const char *prefix = entry->sdt ? "%" : "#";
745         int ret;
746         /* Save stat for rollback */
747         ret = fstat(fd, &st);
748         if (ret < 0)
749                 return ret;
750
751         pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
752         iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
753         iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
754         iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
755         ret = writev(fd, iov, 3);
756         if (ret < (int)iov[1].iov_len + 2)
757                 goto rollback;
758
759         strlist__for_each_entry(snode, entry->tevlist) {
760                 iov[0].iov_base = (void *)snode->s;
761                 iov[0].iov_len = strlen(snode->s);
762                 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
763                 ret = writev(fd, iov, 2);
764                 if (ret < (int)iov[0].iov_len + 1)
765                         goto rollback;
766         }
767         return 0;
768
769 rollback:
770         /* Rollback to avoid cache file corruption */
771         if (ret > 0)
772                 ret = -1;
773         if (ftruncate(fd, st.st_size) < 0)
774                 ret = -2;
775
776         return ret;
777 }
778
779 int probe_cache__commit(struct probe_cache *pcache)
780 {
781         struct probe_cache_entry *entry;
782         int ret = 0;
783
784         /* TBD: if we do not update existing entries, skip it */
785         ret = lseek(pcache->fd, 0, SEEK_SET);
786         if (ret < 0)
787                 goto out;
788
789         ret = ftruncate(pcache->fd, 0);
790         if (ret < 0)
791                 goto out;
792
793         for_each_probe_cache_entry(entry, pcache) {
794                 ret = probe_cache_entry__write(entry, pcache->fd);
795                 pr_debug("Cache committed: %d\n", ret);
796                 if (ret < 0)
797                         break;
798         }
799 out:
800         return ret;
801 }
802
803 static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
804                                        struct strfilter *filter)
805 {
806         char buf[128], *ptr = entry->spev;
807
808         if (entry->pev.event) {
809                 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
810                 ptr = buf;
811         }
812         return strfilter__compare(filter, ptr);
813 }
814
815 int probe_cache__filter_purge(struct probe_cache *pcache,
816                               struct strfilter *filter)
817 {
818         struct probe_cache_entry *entry, *tmp;
819
820         list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
821                 if (probe_cache_entry__compare(entry, filter)) {
822                         pr_info("Removed cached event: %s\n", entry->spev);
823                         list_del_init(&entry->node);
824                         probe_cache_entry__delete(entry);
825                 }
826         }
827         return 0;
828 }
829
830 static int probe_cache__show_entries(struct probe_cache *pcache,
831                                      struct strfilter *filter)
832 {
833         struct probe_cache_entry *entry;
834
835         for_each_probe_cache_entry(entry, pcache) {
836                 if (probe_cache_entry__compare(entry, filter))
837                         printf("%s\n", entry->spev);
838         }
839         return 0;
840 }
841
842 /* Show all cached probes */
843 int probe_cache__show_all_caches(struct strfilter *filter)
844 {
845         struct probe_cache *pcache;
846         struct strlist *bidlist;
847         struct str_node *nd;
848         char *buf = strfilter__string(filter);
849
850         pr_debug("list cache with filter: %s\n", buf);
851         free(buf);
852
853         bidlist = build_id_cache__list_all(true);
854         if (!bidlist) {
855                 pr_debug("Failed to get buildids: %d\n", errno);
856                 return -EINVAL;
857         }
858         strlist__for_each_entry(nd, bidlist) {
859                 pcache = probe_cache__new(nd->s);
860                 if (!pcache)
861                         continue;
862                 if (!list_empty(&pcache->entries)) {
863                         buf = build_id_cache__origname(nd->s);
864                         printf("%s (%s):\n", buf, nd->s);
865                         free(buf);
866                         probe_cache__show_entries(pcache, filter);
867                 }
868                 probe_cache__delete(pcache);
869         }
870         strlist__delete(bidlist);
871
872         return 0;
873 }