Revert "perf augmented_syscalls: Drop 'write', 'poll' for testing without self pid...
[linux/fpc-iii.git] / tools / perf / util / probe-file.c
blobaac7817d9e14dda07978f46b32a6e20857822bce
1 /*
2 * probe-file.c : operate ftrace k/uprobe events files
4 * Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
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.
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.
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <sys/uio.h>
22 #include <unistd.h>
23 #include "util.h"
24 #include "event.h"
25 #include "strlist.h"
26 #include "strfilter.h"
27 #include "debug.h"
28 #include "cache.h"
29 #include "color.h"
30 #include "symbol.h"
31 #include "thread.h"
32 #include <api/fs/tracing_path.h>
33 #include "probe-event.h"
34 #include "probe-file.h"
35 #include "session.h"
36 #include "perf_regs.h"
37 #include "string2.h"
39 /* 4096 - 2 ('\n' + '\0') */
40 #define MAX_CMDLEN 4094
42 static void print_open_warning(int err, bool uprobe)
44 char sbuf[STRERR_BUFSIZE];
46 if (err == -ENOENT) {
47 const char *config;
49 if (uprobe)
50 config = "CONFIG_UPROBE_EVENTS";
51 else
52 config = "CONFIG_KPROBE_EVENTS";
54 pr_warning("%cprobe_events file does not exist"
55 " - please rebuild kernel with %s.\n",
56 uprobe ? 'u' : 'k', config);
57 } else if (err == -ENOTSUP)
58 pr_warning("Tracefs or debugfs is not mounted.\n");
59 else
60 pr_warning("Failed to open %cprobe_events: %s\n",
61 uprobe ? 'u' : 'k',
62 str_error_r(-err, sbuf, sizeof(sbuf)));
65 static void print_both_open_warning(int kerr, int uerr)
67 /* Both kprobes and uprobes are disabled, warn it. */
68 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
69 pr_warning("Tracefs or debugfs is not mounted.\n");
70 else if (kerr == -ENOENT && uerr == -ENOENT)
71 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
72 "or/and CONFIG_UPROBE_EVENTS.\n");
73 else {
74 char sbuf[STRERR_BUFSIZE];
75 pr_warning("Failed to open kprobe events: %s.\n",
76 str_error_r(-kerr, sbuf, sizeof(sbuf)));
77 pr_warning("Failed to open uprobe events: %s.\n",
78 str_error_r(-uerr, sbuf, sizeof(sbuf)));
82 int open_trace_file(const char *trace_file, bool readwrite)
84 char buf[PATH_MAX];
85 int ret;
87 ret = e_snprintf(buf, PATH_MAX, "%s/%s", tracing_path_mount(), trace_file);
88 if (ret >= 0) {
89 pr_debug("Opening %s write=%d\n", buf, readwrite);
90 if (readwrite && !probe_event_dry_run)
91 ret = open(buf, O_RDWR | O_APPEND, 0);
92 else
93 ret = open(buf, O_RDONLY, 0);
95 if (ret < 0)
96 ret = -errno;
98 return ret;
101 static int open_kprobe_events(bool readwrite)
103 return open_trace_file("kprobe_events", readwrite);
106 static int open_uprobe_events(bool readwrite)
108 return open_trace_file("uprobe_events", readwrite);
111 int probe_file__open(int flag)
113 int fd;
115 if (flag & PF_FL_UPROBE)
116 fd = open_uprobe_events(flag & PF_FL_RW);
117 else
118 fd = open_kprobe_events(flag & PF_FL_RW);
119 if (fd < 0)
120 print_open_warning(fd, flag & PF_FL_UPROBE);
122 return fd;
125 int probe_file__open_both(int *kfd, int *ufd, int flag)
127 if (!kfd || !ufd)
128 return -EINVAL;
130 *kfd = open_kprobe_events(flag & PF_FL_RW);
131 *ufd = open_uprobe_events(flag & PF_FL_RW);
132 if (*kfd < 0 && *ufd < 0) {
133 print_both_open_warning(*kfd, *ufd);
134 return *kfd;
137 return 0;
140 /* Get raw string list of current kprobe_events or uprobe_events */
141 struct strlist *probe_file__get_rawlist(int fd)
143 int ret, idx, fddup;
144 FILE *fp;
145 char buf[MAX_CMDLEN];
146 char *p;
147 struct strlist *sl;
149 if (fd < 0)
150 return NULL;
152 sl = strlist__new(NULL, NULL);
153 if (sl == NULL)
154 return NULL;
156 fddup = dup(fd);
157 if (fddup < 0)
158 goto out_free_sl;
160 fp = fdopen(fddup, "r");
161 if (!fp)
162 goto out_close_fddup;
164 while (!feof(fp)) {
165 p = fgets(buf, MAX_CMDLEN, fp);
166 if (!p)
167 break;
169 idx = strlen(p) - 1;
170 if (p[idx] == '\n')
171 p[idx] = '\0';
172 ret = strlist__add(sl, buf);
173 if (ret < 0) {
174 pr_debug("strlist__add failed (%d)\n", ret);
175 goto out_close_fp;
178 fclose(fp);
180 return sl;
182 out_close_fp:
183 fclose(fp);
184 goto out_free_sl;
185 out_close_fddup:
186 close(fddup);
187 out_free_sl:
188 strlist__delete(sl);
189 return NULL;
192 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
194 char buf[128];
195 struct strlist *sl, *rawlist;
196 struct str_node *ent;
197 struct probe_trace_event tev;
198 int ret = 0;
200 memset(&tev, 0, sizeof(tev));
201 rawlist = probe_file__get_rawlist(fd);
202 if (!rawlist)
203 return NULL;
204 sl = strlist__new(NULL, NULL);
205 strlist__for_each_entry(ent, rawlist) {
206 ret = parse_probe_trace_command(ent->s, &tev);
207 if (ret < 0)
208 break;
209 if (include_group) {
210 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
211 tev.event);
212 if (ret >= 0)
213 ret = strlist__add(sl, buf);
214 } else
215 ret = strlist__add(sl, tev.event);
216 clear_probe_trace_event(&tev);
217 if (ret < 0)
218 break;
220 strlist__delete(rawlist);
222 if (ret < 0) {
223 strlist__delete(sl);
224 return NULL;
226 return sl;
229 /* Get current perf-probe event names */
230 struct strlist *probe_file__get_namelist(int fd)
232 return __probe_file__get_namelist(fd, false);
235 int probe_file__add_event(int fd, struct probe_trace_event *tev)
237 int ret = 0;
238 char *buf = synthesize_probe_trace_command(tev);
239 char sbuf[STRERR_BUFSIZE];
241 if (!buf) {
242 pr_debug("Failed to synthesize probe trace event.\n");
243 return -EINVAL;
246 pr_debug("Writing event: %s\n", buf);
247 if (!probe_event_dry_run) {
248 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
249 ret = -errno;
250 pr_warning("Failed to write event: %s\n",
251 str_error_r(errno, sbuf, sizeof(sbuf)));
254 free(buf);
256 return ret;
259 static int __del_trace_probe_event(int fd, struct str_node *ent)
261 char *p;
262 char buf[128];
263 int ret;
265 /* Convert from perf-probe event to trace-probe event */
266 ret = e_snprintf(buf, 128, "-:%s", ent->s);
267 if (ret < 0)
268 goto error;
270 p = strchr(buf + 2, ':');
271 if (!p) {
272 pr_debug("Internal error: %s should have ':' but not.\n",
273 ent->s);
274 ret = -ENOTSUP;
275 goto error;
277 *p = '/';
279 pr_debug("Writing event: %s\n", buf);
280 ret = write(fd, buf, strlen(buf));
281 if (ret < 0) {
282 ret = -errno;
283 goto error;
286 return 0;
287 error:
288 pr_warning("Failed to delete event: %s\n",
289 str_error_r(-ret, buf, sizeof(buf)));
290 return ret;
293 int probe_file__get_events(int fd, struct strfilter *filter,
294 struct strlist *plist)
296 struct strlist *namelist;
297 struct str_node *ent;
298 const char *p;
299 int ret = -ENOENT;
301 if (!plist)
302 return -EINVAL;
304 namelist = __probe_file__get_namelist(fd, true);
305 if (!namelist)
306 return -ENOENT;
308 strlist__for_each_entry(ent, namelist) {
309 p = strchr(ent->s, ':');
310 if ((p && strfilter__compare(filter, p + 1)) ||
311 strfilter__compare(filter, ent->s)) {
312 strlist__add(plist, ent->s);
313 ret = 0;
316 strlist__delete(namelist);
318 return ret;
321 int probe_file__del_strlist(int fd, struct strlist *namelist)
323 int ret = 0;
324 struct str_node *ent;
326 strlist__for_each_entry(ent, namelist) {
327 ret = __del_trace_probe_event(fd, ent);
328 if (ret < 0)
329 break;
331 return ret;
334 int probe_file__del_events(int fd, struct strfilter *filter)
336 struct strlist *namelist;
337 int ret;
339 namelist = strlist__new(NULL, NULL);
340 if (!namelist)
341 return -ENOMEM;
343 ret = probe_file__get_events(fd, filter, namelist);
344 if (ret < 0)
345 return ret;
347 ret = probe_file__del_strlist(fd, namelist);
348 strlist__delete(namelist);
350 return ret;
353 /* Caller must ensure to remove this entry from list */
354 static void probe_cache_entry__delete(struct probe_cache_entry *entry)
356 if (entry) {
357 BUG_ON(!list_empty(&entry->node));
359 strlist__delete(entry->tevlist);
360 clear_perf_probe_event(&entry->pev);
361 zfree(&entry->spev);
362 free(entry);
366 static struct probe_cache_entry *
367 probe_cache_entry__new(struct perf_probe_event *pev)
369 struct probe_cache_entry *entry = zalloc(sizeof(*entry));
371 if (entry) {
372 INIT_LIST_HEAD(&entry->node);
373 entry->tevlist = strlist__new(NULL, NULL);
374 if (!entry->tevlist)
375 zfree(&entry);
376 else if (pev) {
377 entry->spev = synthesize_perf_probe_command(pev);
378 if (!entry->spev ||
379 perf_probe_event__copy(&entry->pev, pev) < 0) {
380 probe_cache_entry__delete(entry);
381 return NULL;
386 return entry;
389 int probe_cache_entry__get_event(struct probe_cache_entry *entry,
390 struct probe_trace_event **tevs)
392 struct probe_trace_event *tev;
393 struct str_node *node;
394 int ret, i;
396 ret = strlist__nr_entries(entry->tevlist);
397 if (ret > probe_conf.max_probes)
398 return -E2BIG;
400 *tevs = zalloc(ret * sizeof(*tev));
401 if (!*tevs)
402 return -ENOMEM;
404 i = 0;
405 strlist__for_each_entry(node, entry->tevlist) {
406 tev = &(*tevs)[i++];
407 ret = parse_probe_trace_command(node->s, tev);
408 if (ret < 0)
409 break;
411 return i;
414 /* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
415 static int probe_cache__open(struct probe_cache *pcache, const char *target,
416 struct nsinfo *nsi)
418 char cpath[PATH_MAX];
419 char sbuildid[SBUILD_ID_SIZE];
420 char *dir_name = NULL;
421 bool is_kallsyms = false;
422 int ret, fd;
423 struct nscookie nsc;
425 if (target && build_id_cache__cached(target)) {
426 /* This is a cached buildid */
427 strncpy(sbuildid, target, SBUILD_ID_SIZE);
428 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
429 goto found;
432 if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
433 target = DSO__NAME_KALLSYMS;
434 is_kallsyms = true;
435 ret = sysfs__sprintf_build_id("/", sbuildid);
436 } else {
437 nsinfo__mountns_enter(nsi, &nsc);
438 ret = filename__sprintf_build_id(target, sbuildid);
439 nsinfo__mountns_exit(&nsc);
442 if (ret < 0) {
443 pr_debug("Failed to get build-id from %s.\n", target);
444 return ret;
447 /* If we have no buildid cache, make it */
448 if (!build_id_cache__cached(sbuildid)) {
449 ret = build_id_cache__add_s(sbuildid, target, nsi,
450 is_kallsyms, NULL);
451 if (ret < 0) {
452 pr_debug("Failed to add build-id cache: %s\n", target);
453 return ret;
457 dir_name = build_id_cache__cachedir(sbuildid, target, nsi, is_kallsyms,
458 false);
459 found:
460 if (!dir_name) {
461 pr_debug("Failed to get cache from %s\n", target);
462 return -ENOMEM;
465 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
466 fd = open(cpath, O_CREAT | O_RDWR, 0644);
467 if (fd < 0)
468 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
469 free(dir_name);
470 pcache->fd = fd;
472 return fd;
475 static int probe_cache__load(struct probe_cache *pcache)
477 struct probe_cache_entry *entry = NULL;
478 char buf[MAX_CMDLEN], *p;
479 int ret = 0, fddup;
480 FILE *fp;
482 fddup = dup(pcache->fd);
483 if (fddup < 0)
484 return -errno;
485 fp = fdopen(fddup, "r");
486 if (!fp) {
487 close(fddup);
488 return -EINVAL;
491 while (!feof(fp)) {
492 if (!fgets(buf, MAX_CMDLEN, fp))
493 break;
494 p = strchr(buf, '\n');
495 if (p)
496 *p = '\0';
497 /* #perf_probe_event or %sdt_event */
498 if (buf[0] == '#' || buf[0] == '%') {
499 entry = probe_cache_entry__new(NULL);
500 if (!entry) {
501 ret = -ENOMEM;
502 goto out;
504 if (buf[0] == '%')
505 entry->sdt = true;
506 entry->spev = strdup(buf + 1);
507 if (entry->spev)
508 ret = parse_perf_probe_command(buf + 1,
509 &entry->pev);
510 else
511 ret = -ENOMEM;
512 if (ret < 0) {
513 probe_cache_entry__delete(entry);
514 goto out;
516 list_add_tail(&entry->node, &pcache->entries);
517 } else { /* trace_probe_event */
518 if (!entry) {
519 ret = -EINVAL;
520 goto out;
522 strlist__add(entry->tevlist, buf);
525 out:
526 fclose(fp);
527 return ret;
530 static struct probe_cache *probe_cache__alloc(void)
532 struct probe_cache *pcache = zalloc(sizeof(*pcache));
534 if (pcache) {
535 INIT_LIST_HEAD(&pcache->entries);
536 pcache->fd = -EINVAL;
538 return pcache;
541 void probe_cache__purge(struct probe_cache *pcache)
543 struct probe_cache_entry *entry, *n;
545 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
546 list_del_init(&entry->node);
547 probe_cache_entry__delete(entry);
551 void probe_cache__delete(struct probe_cache *pcache)
553 if (!pcache)
554 return;
556 probe_cache__purge(pcache);
557 if (pcache->fd > 0)
558 close(pcache->fd);
559 free(pcache);
562 struct probe_cache *probe_cache__new(const char *target, struct nsinfo *nsi)
564 struct probe_cache *pcache = probe_cache__alloc();
565 int ret;
567 if (!pcache)
568 return NULL;
570 ret = probe_cache__open(pcache, target, nsi);
571 if (ret < 0) {
572 pr_debug("Cache open error: %d\n", ret);
573 goto out_err;
576 ret = probe_cache__load(pcache);
577 if (ret < 0) {
578 pr_debug("Cache read error: %d\n", ret);
579 goto out_err;
582 return pcache;
584 out_err:
585 probe_cache__delete(pcache);
586 return NULL;
589 static bool streql(const char *a, const char *b)
591 if (a == b)
592 return true;
594 if (!a || !b)
595 return false;
597 return !strcmp(a, b);
600 struct probe_cache_entry *
601 probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
603 struct probe_cache_entry *entry = NULL;
604 char *cmd = synthesize_perf_probe_command(pev);
606 if (!cmd)
607 return NULL;
609 for_each_probe_cache_entry(entry, pcache) {
610 if (pev->sdt) {
611 if (entry->pev.event &&
612 streql(entry->pev.event, pev->event) &&
613 (!pev->group ||
614 streql(entry->pev.group, pev->group)))
615 goto found;
617 continue;
619 /* Hit if same event name or same command-string */
620 if ((pev->event &&
621 (streql(entry->pev.group, pev->group) &&
622 streql(entry->pev.event, pev->event))) ||
623 (!strcmp(entry->spev, cmd)))
624 goto found;
626 entry = NULL;
628 found:
629 free(cmd);
630 return entry;
633 struct probe_cache_entry *
634 probe_cache__find_by_name(struct probe_cache *pcache,
635 const char *group, const char *event)
637 struct probe_cache_entry *entry = NULL;
639 for_each_probe_cache_entry(entry, pcache) {
640 /* Hit if same event name or same command-string */
641 if (streql(entry->pev.group, group) &&
642 streql(entry->pev.event, event))
643 goto found;
645 entry = NULL;
647 found:
648 return entry;
651 int probe_cache__add_entry(struct probe_cache *pcache,
652 struct perf_probe_event *pev,
653 struct probe_trace_event *tevs, int ntevs)
655 struct probe_cache_entry *entry = NULL;
656 char *command;
657 int i, ret = 0;
659 if (!pcache || !pev || !tevs || ntevs <= 0) {
660 ret = -EINVAL;
661 goto out_err;
664 /* Remove old cache entry */
665 entry = probe_cache__find(pcache, pev);
666 if (entry) {
667 list_del_init(&entry->node);
668 probe_cache_entry__delete(entry);
671 ret = -ENOMEM;
672 entry = probe_cache_entry__new(pev);
673 if (!entry)
674 goto out_err;
676 for (i = 0; i < ntevs; i++) {
677 if (!tevs[i].point.symbol)
678 continue;
680 command = synthesize_probe_trace_command(&tevs[i]);
681 if (!command)
682 goto out_err;
683 strlist__add(entry->tevlist, command);
684 free(command);
686 list_add_tail(&entry->node, &pcache->entries);
687 pr_debug("Added probe cache: %d\n", ntevs);
688 return 0;
690 out_err:
691 pr_debug("Failed to add probe caches\n");
692 probe_cache_entry__delete(entry);
693 return ret;
696 #ifdef HAVE_GELF_GETNOTE_SUPPORT
697 static unsigned long long sdt_note__get_addr(struct sdt_note *note)
699 return note->bit32 ?
700 (unsigned long long)note->addr.a32[SDT_NOTE_IDX_LOC] :
701 (unsigned long long)note->addr.a64[SDT_NOTE_IDX_LOC];
704 static unsigned long long sdt_note__get_ref_ctr_offset(struct sdt_note *note)
706 return note->bit32 ?
707 (unsigned long long)note->addr.a32[SDT_NOTE_IDX_REFCTR] :
708 (unsigned long long)note->addr.a64[SDT_NOTE_IDX_REFCTR];
711 static const char * const type_to_suffix[] = {
712 ":s64", "", "", "", ":s32", "", ":s16", ":s8",
713 "", ":u8", ":u16", "", ":u32", "", "", "", ":u64"
717 * Isolate the string number and convert it into a decimal value;
718 * this will be an index to get suffix of the uprobe name (defining
719 * the type)
721 static int sdt_arg_parse_size(char *n_ptr, const char **suffix)
723 long type_idx;
725 type_idx = strtol(n_ptr, NULL, 10);
726 if (type_idx < -8 || type_idx > 8) {
727 pr_debug4("Failed to get a valid sdt type\n");
728 return -1;
731 *suffix = type_to_suffix[type_idx + 8];
732 return 0;
735 static int synthesize_sdt_probe_arg(struct strbuf *buf, int i, const char *arg)
737 char *op, *desc = strdup(arg), *new_op = NULL;
738 const char *suffix = "";
739 int ret = -1;
741 if (desc == NULL) {
742 pr_debug4("Allocation error\n");
743 return ret;
747 * Argument is in N@OP format. N is size of the argument and OP is
748 * the actual assembly operand. N can be omitted; in that case
749 * argument is just OP(without @).
751 op = strchr(desc, '@');
752 if (op) {
753 op[0] = '\0';
754 op++;
756 if (sdt_arg_parse_size(desc, &suffix))
757 goto error;
758 } else {
759 op = desc;
762 ret = arch_sdt_arg_parse_op(op, &new_op);
764 if (ret < 0)
765 goto error;
767 if (ret == SDT_ARG_VALID) {
768 ret = strbuf_addf(buf, " arg%d=%s%s", i + 1, new_op, suffix);
769 if (ret < 0)
770 goto error;
773 ret = 0;
774 error:
775 free(desc);
776 free(new_op);
777 return ret;
780 static char *synthesize_sdt_probe_command(struct sdt_note *note,
781 const char *pathname,
782 const char *sdtgrp)
784 struct strbuf buf;
785 char *ret = NULL, **args;
786 int i, args_count, err;
787 unsigned long long ref_ctr_offset;
789 if (strbuf_init(&buf, 32) < 0)
790 return NULL;
792 err = strbuf_addf(&buf, "p:%s/%s %s:0x%llx",
793 sdtgrp, note->name, pathname,
794 sdt_note__get_addr(note));
796 ref_ctr_offset = sdt_note__get_ref_ctr_offset(note);
797 if (ref_ctr_offset && err >= 0)
798 err = strbuf_addf(&buf, "(0x%llx)", ref_ctr_offset);
800 if (err < 0)
801 goto error;
803 if (!note->args)
804 goto out;
806 if (note->args) {
807 args = argv_split(note->args, &args_count);
809 for (i = 0; i < args_count; ++i) {
810 if (synthesize_sdt_probe_arg(&buf, i, args[i]) < 0)
811 goto error;
815 out:
816 ret = strbuf_detach(&buf, NULL);
817 error:
818 strbuf_release(&buf);
819 return ret;
822 int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
824 struct probe_cache_entry *entry = NULL;
825 struct list_head sdtlist;
826 struct sdt_note *note;
827 char *buf;
828 char sdtgrp[64];
829 int ret;
831 INIT_LIST_HEAD(&sdtlist);
832 ret = get_sdt_note_list(&sdtlist, pathname);
833 if (ret < 0) {
834 pr_debug4("Failed to get sdt note: %d\n", ret);
835 return ret;
837 list_for_each_entry(note, &sdtlist, note_list) {
838 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
839 if (ret < 0)
840 break;
841 /* Try to find same-name entry */
842 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
843 if (!entry) {
844 entry = probe_cache_entry__new(NULL);
845 if (!entry) {
846 ret = -ENOMEM;
847 break;
849 entry->sdt = true;
850 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
851 note->name, note->name);
852 if (ret < 0)
853 break;
854 entry->pev.event = strdup(note->name);
855 entry->pev.group = strdup(sdtgrp);
856 list_add_tail(&entry->node, &pcache->entries);
858 buf = synthesize_sdt_probe_command(note, pathname, sdtgrp);
859 if (!buf) {
860 ret = -ENOMEM;
861 break;
864 strlist__add(entry->tevlist, buf);
865 free(buf);
866 entry = NULL;
868 if (entry) {
869 list_del_init(&entry->node);
870 probe_cache_entry__delete(entry);
872 cleanup_sdt_note_list(&sdtlist);
873 return ret;
875 #endif
877 static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
879 struct str_node *snode;
880 struct stat st;
881 struct iovec iov[3];
882 const char *prefix = entry->sdt ? "%" : "#";
883 int ret;
884 /* Save stat for rollback */
885 ret = fstat(fd, &st);
886 if (ret < 0)
887 return ret;
889 pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
890 iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
891 iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
892 iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
893 ret = writev(fd, iov, 3);
894 if (ret < (int)iov[1].iov_len + 2)
895 goto rollback;
897 strlist__for_each_entry(snode, entry->tevlist) {
898 iov[0].iov_base = (void *)snode->s;
899 iov[0].iov_len = strlen(snode->s);
900 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
901 ret = writev(fd, iov, 2);
902 if (ret < (int)iov[0].iov_len + 1)
903 goto rollback;
905 return 0;
907 rollback:
908 /* Rollback to avoid cache file corruption */
909 if (ret > 0)
910 ret = -1;
911 if (ftruncate(fd, st.st_size) < 0)
912 ret = -2;
914 return ret;
917 int probe_cache__commit(struct probe_cache *pcache)
919 struct probe_cache_entry *entry;
920 int ret = 0;
922 /* TBD: if we do not update existing entries, skip it */
923 ret = lseek(pcache->fd, 0, SEEK_SET);
924 if (ret < 0)
925 goto out;
927 ret = ftruncate(pcache->fd, 0);
928 if (ret < 0)
929 goto out;
931 for_each_probe_cache_entry(entry, pcache) {
932 ret = probe_cache_entry__write(entry, pcache->fd);
933 pr_debug("Cache committed: %d\n", ret);
934 if (ret < 0)
935 break;
937 out:
938 return ret;
941 static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
942 struct strfilter *filter)
944 char buf[128], *ptr = entry->spev;
946 if (entry->pev.event) {
947 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
948 ptr = buf;
950 return strfilter__compare(filter, ptr);
953 int probe_cache__filter_purge(struct probe_cache *pcache,
954 struct strfilter *filter)
956 struct probe_cache_entry *entry, *tmp;
958 list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
959 if (probe_cache_entry__compare(entry, filter)) {
960 pr_info("Removed cached event: %s\n", entry->spev);
961 list_del_init(&entry->node);
962 probe_cache_entry__delete(entry);
965 return 0;
968 static int probe_cache__show_entries(struct probe_cache *pcache,
969 struct strfilter *filter)
971 struct probe_cache_entry *entry;
973 for_each_probe_cache_entry(entry, pcache) {
974 if (probe_cache_entry__compare(entry, filter))
975 printf("%s\n", entry->spev);
977 return 0;
980 /* Show all cached probes */
981 int probe_cache__show_all_caches(struct strfilter *filter)
983 struct probe_cache *pcache;
984 struct strlist *bidlist;
985 struct str_node *nd;
986 char *buf = strfilter__string(filter);
988 pr_debug("list cache with filter: %s\n", buf);
989 free(buf);
991 bidlist = build_id_cache__list_all(true);
992 if (!bidlist) {
993 pr_debug("Failed to get buildids: %d\n", errno);
994 return -EINVAL;
996 strlist__for_each_entry(nd, bidlist) {
997 pcache = probe_cache__new(nd->s, NULL);
998 if (!pcache)
999 continue;
1000 if (!list_empty(&pcache->entries)) {
1001 buf = build_id_cache__origname(nd->s);
1002 printf("%s (%s):\n", buf, nd->s);
1003 free(buf);
1004 probe_cache__show_entries(pcache, filter);
1006 probe_cache__delete(pcache);
1008 strlist__delete(bidlist);
1010 return 0;
1013 enum ftrace_readme {
1014 FTRACE_README_PROBE_TYPE_X = 0,
1015 FTRACE_README_KRETPROBE_OFFSET,
1016 FTRACE_README_UPROBE_REF_CTR,
1017 FTRACE_README_END,
1020 static struct {
1021 const char *pattern;
1022 bool avail;
1023 } ftrace_readme_table[] = {
1024 #define DEFINE_TYPE(idx, pat) \
1025 [idx] = {.pattern = pat, .avail = false}
1026 DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"),
1027 DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"),
1028 DEFINE_TYPE(FTRACE_README_UPROBE_REF_CTR, "*ref_ctr_offset*"),
1031 static bool scan_ftrace_readme(enum ftrace_readme type)
1033 int fd;
1034 FILE *fp;
1035 char *buf = NULL;
1036 size_t len = 0;
1037 bool ret = false;
1038 static bool scanned = false;
1040 if (scanned)
1041 goto result;
1043 fd = open_trace_file("README", false);
1044 if (fd < 0)
1045 return ret;
1047 fp = fdopen(fd, "r");
1048 if (!fp) {
1049 close(fd);
1050 return ret;
1053 while (getline(&buf, &len, fp) > 0)
1054 for (enum ftrace_readme i = 0; i < FTRACE_README_END; i++)
1055 if (!ftrace_readme_table[i].avail)
1056 ftrace_readme_table[i].avail =
1057 strglobmatch(buf, ftrace_readme_table[i].pattern);
1058 scanned = true;
1060 fclose(fp);
1061 free(buf);
1063 result:
1064 if (type >= FTRACE_README_END)
1065 return false;
1067 return ftrace_readme_table[type].avail;
1070 bool probe_type_is_available(enum probe_type type)
1072 if (type >= PROBE_TYPE_END)
1073 return false;
1074 else if (type == PROBE_TYPE_X)
1075 return scan_ftrace_readme(FTRACE_README_PROBE_TYPE_X);
1077 return true;
1080 bool kretprobe_offset_is_supported(void)
1082 return scan_ftrace_readme(FTRACE_README_KRETPROBE_OFFSET);
1085 bool uprobe_ref_ctr_is_supported(void)
1087 return scan_ftrace_readme(FTRACE_README_UPROBE_REF_CTR);