]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/perf/util/probe-file.c
perf tools: Move extra string util functions to util/string2.h
[karo-tx-linux.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 #include "perf_regs.h"
31 #include "string2.h"
32
33 /* 4096 - 2 ('\n' + '\0') */
34 #define MAX_CMDLEN 4094
35
36 static void print_open_warning(int err, bool uprobe)
37 {
38         char sbuf[STRERR_BUFSIZE];
39
40         if (err == -ENOENT) {
41                 const char *config;
42
43                 if (uprobe)
44                         config = "CONFIG_UPROBE_EVENTS";
45                 else
46                         config = "CONFIG_KPROBE_EVENTS";
47
48                 pr_warning("%cprobe_events file does not exist"
49                            " - please rebuild kernel with %s.\n",
50                            uprobe ? 'u' : 'k', config);
51         } else if (err == -ENOTSUP)
52                 pr_warning("Tracefs or debugfs is not mounted.\n");
53         else
54                 pr_warning("Failed to open %cprobe_events: %s\n",
55                            uprobe ? 'u' : 'k',
56                            str_error_r(-err, sbuf, sizeof(sbuf)));
57 }
58
59 static void print_both_open_warning(int kerr, int uerr)
60 {
61         /* Both kprobes and uprobes are disabled, warn it. */
62         if (kerr == -ENOTSUP && uerr == -ENOTSUP)
63                 pr_warning("Tracefs or debugfs is not mounted.\n");
64         else if (kerr == -ENOENT && uerr == -ENOENT)
65                 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
66                            "or/and CONFIG_UPROBE_EVENTS.\n");
67         else {
68                 char sbuf[STRERR_BUFSIZE];
69                 pr_warning("Failed to open kprobe events: %s.\n",
70                            str_error_r(-kerr, sbuf, sizeof(sbuf)));
71                 pr_warning("Failed to open uprobe events: %s.\n",
72                            str_error_r(-uerr, sbuf, sizeof(sbuf)));
73         }
74 }
75
76 int open_trace_file(const char *trace_file, bool readwrite)
77 {
78         char buf[PATH_MAX];
79         int ret;
80
81         ret = e_snprintf(buf, PATH_MAX, "%s/%s",
82                          tracing_path, trace_file);
83         if (ret >= 0) {
84                 pr_debug("Opening %s write=%d\n", buf, readwrite);
85                 if (readwrite && !probe_event_dry_run)
86                         ret = open(buf, O_RDWR | O_APPEND, 0);
87                 else
88                         ret = open(buf, O_RDONLY, 0);
89
90                 if (ret < 0)
91                         ret = -errno;
92         }
93         return ret;
94 }
95
96 static int open_kprobe_events(bool readwrite)
97 {
98         return open_trace_file("kprobe_events", readwrite);
99 }
100
101 static int open_uprobe_events(bool readwrite)
102 {
103         return open_trace_file("uprobe_events", readwrite);
104 }
105
106 int probe_file__open(int flag)
107 {
108         int fd;
109
110         if (flag & PF_FL_UPROBE)
111                 fd = open_uprobe_events(flag & PF_FL_RW);
112         else
113                 fd = open_kprobe_events(flag & PF_FL_RW);
114         if (fd < 0)
115                 print_open_warning(fd, flag & PF_FL_UPROBE);
116
117         return fd;
118 }
119
120 int probe_file__open_both(int *kfd, int *ufd, int flag)
121 {
122         if (!kfd || !ufd)
123                 return -EINVAL;
124
125         *kfd = open_kprobe_events(flag & PF_FL_RW);
126         *ufd = open_uprobe_events(flag & PF_FL_RW);
127         if (*kfd < 0 && *ufd < 0) {
128                 print_both_open_warning(*kfd, *ufd);
129                 return *kfd;
130         }
131
132         return 0;
133 }
134
135 /* Get raw string list of current kprobe_events  or uprobe_events */
136 struct strlist *probe_file__get_rawlist(int fd)
137 {
138         int ret, idx, fddup;
139         FILE *fp;
140         char buf[MAX_CMDLEN];
141         char *p;
142         struct strlist *sl;
143
144         if (fd < 0)
145                 return NULL;
146
147         sl = strlist__new(NULL, NULL);
148         if (sl == NULL)
149                 return NULL;
150
151         fddup = dup(fd);
152         if (fddup < 0)
153                 goto out_free_sl;
154
155         fp = fdopen(fddup, "r");
156         if (!fp)
157                 goto out_close_fddup;
158
159         while (!feof(fp)) {
160                 p = fgets(buf, MAX_CMDLEN, fp);
161                 if (!p)
162                         break;
163
164                 idx = strlen(p) - 1;
165                 if (p[idx] == '\n')
166                         p[idx] = '\0';
167                 ret = strlist__add(sl, buf);
168                 if (ret < 0) {
169                         pr_debug("strlist__add failed (%d)\n", ret);
170                         goto out_close_fp;
171                 }
172         }
173         fclose(fp);
174
175         return sl;
176
177 out_close_fp:
178         fclose(fp);
179         goto out_free_sl;
180 out_close_fddup:
181         close(fddup);
182 out_free_sl:
183         strlist__delete(sl);
184         return NULL;
185 }
186
187 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
188 {
189         char buf[128];
190         struct strlist *sl, *rawlist;
191         struct str_node *ent;
192         struct probe_trace_event tev;
193         int ret = 0;
194
195         memset(&tev, 0, sizeof(tev));
196         rawlist = probe_file__get_rawlist(fd);
197         if (!rawlist)
198                 return NULL;
199         sl = strlist__new(NULL, NULL);
200         strlist__for_each_entry(ent, rawlist) {
201                 ret = parse_probe_trace_command(ent->s, &tev);
202                 if (ret < 0)
203                         break;
204                 if (include_group) {
205                         ret = e_snprintf(buf, 128, "%s:%s", tev.group,
206                                         tev.event);
207                         if (ret >= 0)
208                                 ret = strlist__add(sl, buf);
209                 } else
210                         ret = strlist__add(sl, tev.event);
211                 clear_probe_trace_event(&tev);
212                 if (ret < 0)
213                         break;
214         }
215         strlist__delete(rawlist);
216
217         if (ret < 0) {
218                 strlist__delete(sl);
219                 return NULL;
220         }
221         return sl;
222 }
223
224 /* Get current perf-probe event names */
225 struct strlist *probe_file__get_namelist(int fd)
226 {
227         return __probe_file__get_namelist(fd, false);
228 }
229
230 int probe_file__add_event(int fd, struct probe_trace_event *tev)
231 {
232         int ret = 0;
233         char *buf = synthesize_probe_trace_command(tev);
234         char sbuf[STRERR_BUFSIZE];
235
236         if (!buf) {
237                 pr_debug("Failed to synthesize probe trace event.\n");
238                 return -EINVAL;
239         }
240
241         pr_debug("Writing event: %s\n", buf);
242         if (!probe_event_dry_run) {
243                 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
244                         ret = -errno;
245                         pr_warning("Failed to write event: %s\n",
246                                    str_error_r(errno, sbuf, sizeof(sbuf)));
247                 }
248         }
249         free(buf);
250
251         return ret;
252 }
253
254 static int __del_trace_probe_event(int fd, struct str_node *ent)
255 {
256         char *p;
257         char buf[128];
258         int ret;
259
260         /* Convert from perf-probe event to trace-probe event */
261         ret = e_snprintf(buf, 128, "-:%s", ent->s);
262         if (ret < 0)
263                 goto error;
264
265         p = strchr(buf + 2, ':');
266         if (!p) {
267                 pr_debug("Internal error: %s should have ':' but not.\n",
268                          ent->s);
269                 ret = -ENOTSUP;
270                 goto error;
271         }
272         *p = '/';
273
274         pr_debug("Writing event: %s\n", buf);
275         ret = write(fd, buf, strlen(buf));
276         if (ret < 0) {
277                 ret = -errno;
278                 goto error;
279         }
280
281         return 0;
282 error:
283         pr_warning("Failed to delete event: %s\n",
284                    str_error_r(-ret, buf, sizeof(buf)));
285         return ret;
286 }
287
288 int probe_file__get_events(int fd, struct strfilter *filter,
289                            struct strlist *plist)
290 {
291         struct strlist *namelist;
292         struct str_node *ent;
293         const char *p;
294         int ret = -ENOENT;
295
296         if (!plist)
297                 return -EINVAL;
298
299         namelist = __probe_file__get_namelist(fd, true);
300         if (!namelist)
301                 return -ENOENT;
302
303         strlist__for_each_entry(ent, namelist) {
304                 p = strchr(ent->s, ':');
305                 if ((p && strfilter__compare(filter, p + 1)) ||
306                     strfilter__compare(filter, ent->s)) {
307                         strlist__add(plist, ent->s);
308                         ret = 0;
309                 }
310         }
311         strlist__delete(namelist);
312
313         return ret;
314 }
315
316 int probe_file__del_strlist(int fd, struct strlist *namelist)
317 {
318         int ret = 0;
319         struct str_node *ent;
320
321         strlist__for_each_entry(ent, namelist) {
322                 ret = __del_trace_probe_event(fd, ent);
323                 if (ret < 0)
324                         break;
325         }
326         return ret;
327 }
328
329 int probe_file__del_events(int fd, struct strfilter *filter)
330 {
331         struct strlist *namelist;
332         int ret;
333
334         namelist = strlist__new(NULL, NULL);
335         if (!namelist)
336                 return -ENOMEM;
337
338         ret = probe_file__get_events(fd, filter, namelist);
339         if (ret < 0)
340                 return ret;
341
342         ret = probe_file__del_strlist(fd, namelist);
343         strlist__delete(namelist);
344
345         return ret;
346 }
347
348 /* Caller must ensure to remove this entry from list */
349 static void probe_cache_entry__delete(struct probe_cache_entry *entry)
350 {
351         if (entry) {
352                 BUG_ON(!list_empty(&entry->node));
353
354                 strlist__delete(entry->tevlist);
355                 clear_perf_probe_event(&entry->pev);
356                 zfree(&entry->spev);
357                 free(entry);
358         }
359 }
360
361 static struct probe_cache_entry *
362 probe_cache_entry__new(struct perf_probe_event *pev)
363 {
364         struct probe_cache_entry *entry = zalloc(sizeof(*entry));
365
366         if (entry) {
367                 INIT_LIST_HEAD(&entry->node);
368                 entry->tevlist = strlist__new(NULL, NULL);
369                 if (!entry->tevlist)
370                         zfree(&entry);
371                 else if (pev) {
372                         entry->spev = synthesize_perf_probe_command(pev);
373                         if (!entry->spev ||
374                             perf_probe_event__copy(&entry->pev, pev) < 0) {
375                                 probe_cache_entry__delete(entry);
376                                 return NULL;
377                         }
378                 }
379         }
380
381         return entry;
382 }
383
384 int probe_cache_entry__get_event(struct probe_cache_entry *entry,
385                                  struct probe_trace_event **tevs)
386 {
387         struct probe_trace_event *tev;
388         struct str_node *node;
389         int ret, i;
390
391         ret = strlist__nr_entries(entry->tevlist);
392         if (ret > probe_conf.max_probes)
393                 return -E2BIG;
394
395         *tevs = zalloc(ret * sizeof(*tev));
396         if (!*tevs)
397                 return -ENOMEM;
398
399         i = 0;
400         strlist__for_each_entry(node, entry->tevlist) {
401                 tev = &(*tevs)[i++];
402                 ret = parse_probe_trace_command(node->s, tev);
403                 if (ret < 0)
404                         break;
405         }
406         return i;
407 }
408
409 /* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
410 static int probe_cache__open(struct probe_cache *pcache, const char *target)
411 {
412         char cpath[PATH_MAX];
413         char sbuildid[SBUILD_ID_SIZE];
414         char *dir_name = NULL;
415         bool is_kallsyms = false;
416         int ret, fd;
417
418         if (target && build_id_cache__cached(target)) {
419                 /* This is a cached buildid */
420                 strncpy(sbuildid, target, SBUILD_ID_SIZE);
421                 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
422                 goto found;
423         }
424
425         if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
426                 target = DSO__NAME_KALLSYMS;
427                 is_kallsyms = true;
428                 ret = sysfs__sprintf_build_id("/", sbuildid);
429         } else
430                 ret = filename__sprintf_build_id(target, sbuildid);
431
432         if (ret < 0) {
433                 pr_debug("Failed to get build-id from %s.\n", target);
434                 return ret;
435         }
436
437         /* If we have no buildid cache, make it */
438         if (!build_id_cache__cached(sbuildid)) {
439                 ret = build_id_cache__add_s(sbuildid, target,
440                                             is_kallsyms, NULL);
441                 if (ret < 0) {
442                         pr_debug("Failed to add build-id cache: %s\n", target);
443                         return ret;
444                 }
445         }
446
447         dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
448                                             false);
449 found:
450         if (!dir_name) {
451                 pr_debug("Failed to get cache from %s\n", target);
452                 return -ENOMEM;
453         }
454
455         snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
456         fd = open(cpath, O_CREAT | O_RDWR, 0644);
457         if (fd < 0)
458                 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
459         free(dir_name);
460         pcache->fd = fd;
461
462         return fd;
463 }
464
465 static int probe_cache__load(struct probe_cache *pcache)
466 {
467         struct probe_cache_entry *entry = NULL;
468         char buf[MAX_CMDLEN], *p;
469         int ret = 0, fddup;
470         FILE *fp;
471
472         fddup = dup(pcache->fd);
473         if (fddup < 0)
474                 return -errno;
475         fp = fdopen(fddup, "r");
476         if (!fp) {
477                 close(fddup);
478                 return -EINVAL;
479         }
480
481         while (!feof(fp)) {
482                 if (!fgets(buf, MAX_CMDLEN, fp))
483                         break;
484                 p = strchr(buf, '\n');
485                 if (p)
486                         *p = '\0';
487                 /* #perf_probe_event or %sdt_event */
488                 if (buf[0] == '#' || buf[0] == '%') {
489                         entry = probe_cache_entry__new(NULL);
490                         if (!entry) {
491                                 ret = -ENOMEM;
492                                 goto out;
493                         }
494                         if (buf[0] == '%')
495                                 entry->sdt = true;
496                         entry->spev = strdup(buf + 1);
497                         if (entry->spev)
498                                 ret = parse_perf_probe_command(buf + 1,
499                                                                 &entry->pev);
500                         else
501                                 ret = -ENOMEM;
502                         if (ret < 0) {
503                                 probe_cache_entry__delete(entry);
504                                 goto out;
505                         }
506                         list_add_tail(&entry->node, &pcache->entries);
507                 } else {        /* trace_probe_event */
508                         if (!entry) {
509                                 ret = -EINVAL;
510                                 goto out;
511                         }
512                         strlist__add(entry->tevlist, buf);
513                 }
514         }
515 out:
516         fclose(fp);
517         return ret;
518 }
519
520 static struct probe_cache *probe_cache__alloc(void)
521 {
522         struct probe_cache *pcache = zalloc(sizeof(*pcache));
523
524         if (pcache) {
525                 INIT_LIST_HEAD(&pcache->entries);
526                 pcache->fd = -EINVAL;
527         }
528         return pcache;
529 }
530
531 void probe_cache__purge(struct probe_cache *pcache)
532 {
533         struct probe_cache_entry *entry, *n;
534
535         list_for_each_entry_safe(entry, n, &pcache->entries, node) {
536                 list_del_init(&entry->node);
537                 probe_cache_entry__delete(entry);
538         }
539 }
540
541 void probe_cache__delete(struct probe_cache *pcache)
542 {
543         if (!pcache)
544                 return;
545
546         probe_cache__purge(pcache);
547         if (pcache->fd > 0)
548                 close(pcache->fd);
549         free(pcache);
550 }
551
552 struct probe_cache *probe_cache__new(const char *target)
553 {
554         struct probe_cache *pcache = probe_cache__alloc();
555         int ret;
556
557         if (!pcache)
558                 return NULL;
559
560         ret = probe_cache__open(pcache, target);
561         if (ret < 0) {
562                 pr_debug("Cache open error: %d\n", ret);
563                 goto out_err;
564         }
565
566         ret = probe_cache__load(pcache);
567         if (ret < 0) {
568                 pr_debug("Cache read error: %d\n", ret);
569                 goto out_err;
570         }
571
572         return pcache;
573
574 out_err:
575         probe_cache__delete(pcache);
576         return NULL;
577 }
578
579 static bool streql(const char *a, const char *b)
580 {
581         if (a == b)
582                 return true;
583
584         if (!a || !b)
585                 return false;
586
587         return !strcmp(a, b);
588 }
589
590 struct probe_cache_entry *
591 probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
592 {
593         struct probe_cache_entry *entry = NULL;
594         char *cmd = synthesize_perf_probe_command(pev);
595
596         if (!cmd)
597                 return NULL;
598
599         for_each_probe_cache_entry(entry, pcache) {
600                 if (pev->sdt) {
601                         if (entry->pev.event &&
602                             streql(entry->pev.event, pev->event) &&
603                             (!pev->group ||
604                              streql(entry->pev.group, pev->group)))
605                                 goto found;
606
607                         continue;
608                 }
609                 /* Hit if same event name or same command-string */
610                 if ((pev->event &&
611                      (streql(entry->pev.group, pev->group) &&
612                       streql(entry->pev.event, pev->event))) ||
613                     (!strcmp(entry->spev, cmd)))
614                         goto found;
615         }
616         entry = NULL;
617
618 found:
619         free(cmd);
620         return entry;
621 }
622
623 struct probe_cache_entry *
624 probe_cache__find_by_name(struct probe_cache *pcache,
625                           const char *group, const char *event)
626 {
627         struct probe_cache_entry *entry = NULL;
628
629         for_each_probe_cache_entry(entry, pcache) {
630                 /* Hit if same event name or same command-string */
631                 if (streql(entry->pev.group, group) &&
632                     streql(entry->pev.event, event))
633                         goto found;
634         }
635         entry = NULL;
636
637 found:
638         return entry;
639 }
640
641 int probe_cache__add_entry(struct probe_cache *pcache,
642                            struct perf_probe_event *pev,
643                            struct probe_trace_event *tevs, int ntevs)
644 {
645         struct probe_cache_entry *entry = NULL;
646         char *command;
647         int i, ret = 0;
648
649         if (!pcache || !pev || !tevs || ntevs <= 0) {
650                 ret = -EINVAL;
651                 goto out_err;
652         }
653
654         /* Remove old cache entry */
655         entry = probe_cache__find(pcache, pev);
656         if (entry) {
657                 list_del_init(&entry->node);
658                 probe_cache_entry__delete(entry);
659         }
660
661         ret = -ENOMEM;
662         entry = probe_cache_entry__new(pev);
663         if (!entry)
664                 goto out_err;
665
666         for (i = 0; i < ntevs; i++) {
667                 if (!tevs[i].point.symbol)
668                         continue;
669
670                 command = synthesize_probe_trace_command(&tevs[i]);
671                 if (!command)
672                         goto out_err;
673                 strlist__add(entry->tevlist, command);
674                 free(command);
675         }
676         list_add_tail(&entry->node, &pcache->entries);
677         pr_debug("Added probe cache: %d\n", ntevs);
678         return 0;
679
680 out_err:
681         pr_debug("Failed to add probe caches\n");
682         probe_cache_entry__delete(entry);
683         return ret;
684 }
685
686 #ifdef HAVE_GELF_GETNOTE_SUPPORT
687 static unsigned long long sdt_note__get_addr(struct sdt_note *note)
688 {
689         return note->bit32 ? (unsigned long long)note->addr.a32[0]
690                  : (unsigned long long)note->addr.a64[0];
691 }
692
693 static const char * const type_to_suffix[] = {
694         ":s64", "", "", "", ":s32", "", ":s16", ":s8",
695         "", ":u8", ":u16", "", ":u32", "", "", "", ":u64"
696 };
697
698 /*
699  * Isolate the string number and convert it into a decimal value;
700  * this will be an index to get suffix of the uprobe name (defining
701  * the type)
702  */
703 static int sdt_arg_parse_size(char *n_ptr, const char **suffix)
704 {
705         long type_idx;
706
707         type_idx = strtol(n_ptr, NULL, 10);
708         if (type_idx < -8 || type_idx > 8) {
709                 pr_debug4("Failed to get a valid sdt type\n");
710                 return -1;
711         }
712
713         *suffix = type_to_suffix[type_idx + 8];
714         return 0;
715 }
716
717 static int synthesize_sdt_probe_arg(struct strbuf *buf, int i, const char *arg)
718 {
719         char *op, *desc = strdup(arg), *new_op = NULL;
720         const char *suffix = "";
721         int ret = -1;
722
723         if (desc == NULL) {
724                 pr_debug4("Allocation error\n");
725                 return ret;
726         }
727
728         /*
729          * Argument is in N@OP format. N is size of the argument and OP is
730          * the actual assembly operand. N can be omitted; in that case
731          * argument is just OP(without @).
732          */
733         op = strchr(desc, '@');
734         if (op) {
735                 op[0] = '\0';
736                 op++;
737
738                 if (sdt_arg_parse_size(desc, &suffix))
739                         goto error;
740         } else {
741                 op = desc;
742         }
743
744         ret = arch_sdt_arg_parse_op(op, &new_op);
745
746         if (ret < 0)
747                 goto error;
748
749         if (ret == SDT_ARG_VALID) {
750                 ret = strbuf_addf(buf, " arg%d=%s%s", i + 1, new_op, suffix);
751                 if (ret < 0)
752                         goto error;
753         }
754
755         ret = 0;
756 error:
757         free(desc);
758         free(new_op);
759         return ret;
760 }
761
762 static char *synthesize_sdt_probe_command(struct sdt_note *note,
763                                         const char *pathname,
764                                         const char *sdtgrp)
765 {
766         struct strbuf buf;
767         char *ret = NULL, **args;
768         int i, args_count;
769
770         if (strbuf_init(&buf, 32) < 0)
771                 return NULL;
772
773         if (strbuf_addf(&buf, "p:%s/%s %s:0x%llx",
774                                 sdtgrp, note->name, pathname,
775                                 sdt_note__get_addr(note)) < 0)
776                 goto error;
777
778         if (!note->args)
779                 goto out;
780
781         if (note->args) {
782                 args = argv_split(note->args, &args_count);
783
784                 for (i = 0; i < args_count; ++i) {
785                         if (synthesize_sdt_probe_arg(&buf, i, args[i]) < 0)
786                                 goto error;
787                 }
788         }
789
790 out:
791         ret = strbuf_detach(&buf, NULL);
792 error:
793         strbuf_release(&buf);
794         return ret;
795 }
796
797 int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
798 {
799         struct probe_cache_entry *entry = NULL;
800         struct list_head sdtlist;
801         struct sdt_note *note;
802         char *buf;
803         char sdtgrp[64];
804         int ret;
805
806         INIT_LIST_HEAD(&sdtlist);
807         ret = get_sdt_note_list(&sdtlist, pathname);
808         if (ret < 0) {
809                 pr_debug4("Failed to get sdt note: %d\n", ret);
810                 return ret;
811         }
812         list_for_each_entry(note, &sdtlist, note_list) {
813                 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
814                 if (ret < 0)
815                         break;
816                 /* Try to find same-name entry */
817                 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
818                 if (!entry) {
819                         entry = probe_cache_entry__new(NULL);
820                         if (!entry) {
821                                 ret = -ENOMEM;
822                                 break;
823                         }
824                         entry->sdt = true;
825                         ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
826                                         note->name, note->name);
827                         if (ret < 0)
828                                 break;
829                         entry->pev.event = strdup(note->name);
830                         entry->pev.group = strdup(sdtgrp);
831                         list_add_tail(&entry->node, &pcache->entries);
832                 }
833                 buf = synthesize_sdt_probe_command(note, pathname, sdtgrp);
834                 if (!buf) {
835                         ret = -ENOMEM;
836                         break;
837                 }
838
839                 strlist__add(entry->tevlist, buf);
840                 free(buf);
841                 entry = NULL;
842         }
843         if (entry) {
844                 list_del_init(&entry->node);
845                 probe_cache_entry__delete(entry);
846         }
847         cleanup_sdt_note_list(&sdtlist);
848         return ret;
849 }
850 #endif
851
852 static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
853 {
854         struct str_node *snode;
855         struct stat st;
856         struct iovec iov[3];
857         const char *prefix = entry->sdt ? "%" : "#";
858         int ret;
859         /* Save stat for rollback */
860         ret = fstat(fd, &st);
861         if (ret < 0)
862                 return ret;
863
864         pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
865         iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
866         iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
867         iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
868         ret = writev(fd, iov, 3);
869         if (ret < (int)iov[1].iov_len + 2)
870                 goto rollback;
871
872         strlist__for_each_entry(snode, entry->tevlist) {
873                 iov[0].iov_base = (void *)snode->s;
874                 iov[0].iov_len = strlen(snode->s);
875                 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
876                 ret = writev(fd, iov, 2);
877                 if (ret < (int)iov[0].iov_len + 1)
878                         goto rollback;
879         }
880         return 0;
881
882 rollback:
883         /* Rollback to avoid cache file corruption */
884         if (ret > 0)
885                 ret = -1;
886         if (ftruncate(fd, st.st_size) < 0)
887                 ret = -2;
888
889         return ret;
890 }
891
892 int probe_cache__commit(struct probe_cache *pcache)
893 {
894         struct probe_cache_entry *entry;
895         int ret = 0;
896
897         /* TBD: if we do not update existing entries, skip it */
898         ret = lseek(pcache->fd, 0, SEEK_SET);
899         if (ret < 0)
900                 goto out;
901
902         ret = ftruncate(pcache->fd, 0);
903         if (ret < 0)
904                 goto out;
905
906         for_each_probe_cache_entry(entry, pcache) {
907                 ret = probe_cache_entry__write(entry, pcache->fd);
908                 pr_debug("Cache committed: %d\n", ret);
909                 if (ret < 0)
910                         break;
911         }
912 out:
913         return ret;
914 }
915
916 static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
917                                        struct strfilter *filter)
918 {
919         char buf[128], *ptr = entry->spev;
920
921         if (entry->pev.event) {
922                 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
923                 ptr = buf;
924         }
925         return strfilter__compare(filter, ptr);
926 }
927
928 int probe_cache__filter_purge(struct probe_cache *pcache,
929                               struct strfilter *filter)
930 {
931         struct probe_cache_entry *entry, *tmp;
932
933         list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
934                 if (probe_cache_entry__compare(entry, filter)) {
935                         pr_info("Removed cached event: %s\n", entry->spev);
936                         list_del_init(&entry->node);
937                         probe_cache_entry__delete(entry);
938                 }
939         }
940         return 0;
941 }
942
943 static int probe_cache__show_entries(struct probe_cache *pcache,
944                                      struct strfilter *filter)
945 {
946         struct probe_cache_entry *entry;
947
948         for_each_probe_cache_entry(entry, pcache) {
949                 if (probe_cache_entry__compare(entry, filter))
950                         printf("%s\n", entry->spev);
951         }
952         return 0;
953 }
954
955 /* Show all cached probes */
956 int probe_cache__show_all_caches(struct strfilter *filter)
957 {
958         struct probe_cache *pcache;
959         struct strlist *bidlist;
960         struct str_node *nd;
961         char *buf = strfilter__string(filter);
962
963         pr_debug("list cache with filter: %s\n", buf);
964         free(buf);
965
966         bidlist = build_id_cache__list_all(true);
967         if (!bidlist) {
968                 pr_debug("Failed to get buildids: %d\n", errno);
969                 return -EINVAL;
970         }
971         strlist__for_each_entry(nd, bidlist) {
972                 pcache = probe_cache__new(nd->s);
973                 if (!pcache)
974                         continue;
975                 if (!list_empty(&pcache->entries)) {
976                         buf = build_id_cache__origname(nd->s);
977                         printf("%s (%s):\n", buf, nd->s);
978                         free(buf);
979                         probe_cache__show_entries(pcache, filter);
980                 }
981                 probe_cache__delete(pcache);
982         }
983         strlist__delete(bidlist);
984
985         return 0;
986 }
987
988 enum ftrace_readme {
989         FTRACE_README_PROBE_TYPE_X = 0,
990         FTRACE_README_KRETPROBE_OFFSET,
991         FTRACE_README_END,
992 };
993
994 static struct {
995         const char *pattern;
996         bool avail;
997 } ftrace_readme_table[] = {
998 #define DEFINE_TYPE(idx, pat)                   \
999         [idx] = {.pattern = pat, .avail = false}
1000         DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"),
1001         DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"),
1002 };
1003
1004 static bool scan_ftrace_readme(enum ftrace_readme type)
1005 {
1006         int fd;
1007         FILE *fp;
1008         char *buf = NULL;
1009         size_t len = 0;
1010         bool ret = false;
1011         static bool scanned = false;
1012
1013         if (scanned)
1014                 goto result;
1015
1016         fd = open_trace_file("README", false);
1017         if (fd < 0)
1018                 return ret;
1019
1020         fp = fdopen(fd, "r");
1021         if (!fp) {
1022                 close(fd);
1023                 return ret;
1024         }
1025
1026         while (getline(&buf, &len, fp) > 0)
1027                 for (enum ftrace_readme i = 0; i < FTRACE_README_END; i++)
1028                         if (!ftrace_readme_table[i].avail)
1029                                 ftrace_readme_table[i].avail =
1030                                         strglobmatch(buf, ftrace_readme_table[i].pattern);
1031         scanned = true;
1032
1033         fclose(fp);
1034         free(buf);
1035
1036 result:
1037         if (type >= FTRACE_README_END)
1038                 return false;
1039
1040         return ftrace_readme_table[type].avail;
1041 }
1042
1043 bool probe_type_is_available(enum probe_type type)
1044 {
1045         if (type >= PROBE_TYPE_END)
1046                 return false;
1047         else if (type == PROBE_TYPE_X)
1048                 return scan_ftrace_readme(FTRACE_README_PROBE_TYPE_X);
1049
1050         return true;
1051 }
1052
1053 bool kretprobe_offset_is_supported(void)
1054 {
1055         return scan_ftrace_readme(FTRACE_README_KRETPROBE_OFFSET);
1056 }