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.
19 #include <sys/types.h>
25 #include "strfilter.h"
31 #include <api/fs/tracing_path.h>
32 #include "probe-event.h"
33 #include "probe-file.h"
35 #include "perf_regs.h"
38 /* 4096 - 2 ('\n' + '\0') */
39 #define MAX_CMDLEN 4094
41 static void print_open_warning(int err, bool uprobe)
43 char sbuf[STRERR_BUFSIZE];
49 config = "CONFIG_UPROBE_EVENTS";
51 config = "CONFIG_KPROBE_EVENTS";
53 pr_warning("%cprobe_events file does not exist"
54 " - please rebuild kernel with %s.\n",
55 uprobe ? 'u' : 'k', config);
56 } else if (err == -ENOTSUP)
57 pr_warning("Tracefs or debugfs is not mounted.\n");
59 pr_warning("Failed to open %cprobe_events: %s\n",
61 str_error_r(-err, sbuf, sizeof(sbuf)));
64 static void print_both_open_warning(int kerr, int uerr)
66 /* Both kprobes and uprobes are disabled, warn it. */
67 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
68 pr_warning("Tracefs or debugfs is not mounted.\n");
69 else if (kerr == -ENOENT && uerr == -ENOENT)
70 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
71 "or/and CONFIG_UPROBE_EVENTS.\n");
73 char sbuf[STRERR_BUFSIZE];
74 pr_warning("Failed to open kprobe events: %s.\n",
75 str_error_r(-kerr, sbuf, sizeof(sbuf)));
76 pr_warning("Failed to open uprobe events: %s.\n",
77 str_error_r(-uerr, sbuf, sizeof(sbuf)));
81 int open_trace_file(const char *trace_file, bool readwrite)
86 ret = e_snprintf(buf, PATH_MAX, "%s/%s",
87 tracing_path, trace_file);
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);
93 ret = open(buf, O_RDONLY, 0);
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)
115 if (flag & PF_FL_UPROBE)
116 fd = open_uprobe_events(flag & PF_FL_RW);
118 fd = open_kprobe_events(flag & PF_FL_RW);
120 print_open_warning(fd, flag & PF_FL_UPROBE);
125 int probe_file__open_both(int *kfd, int *ufd, int flag)
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);
140 /* Get raw string list of current kprobe_events or uprobe_events */
141 struct strlist *probe_file__get_rawlist(int fd)
145 char buf[MAX_CMDLEN];
152 sl = strlist__new(NULL, NULL);
160 fp = fdopen(fddup, "r");
162 goto out_close_fddup;
165 p = fgets(buf, MAX_CMDLEN, fp);
172 ret = strlist__add(sl, buf);
174 pr_debug("strlist__add failed (%d)\n", ret);
192 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
195 struct strlist *sl, *rawlist;
196 struct str_node *ent;
197 struct probe_trace_event tev;
200 memset(&tev, 0, sizeof(tev));
201 rawlist = probe_file__get_rawlist(fd);
204 sl = strlist__new(NULL, NULL);
205 strlist__for_each_entry(ent, rawlist) {
206 ret = parse_probe_trace_command(ent->s, &tev);
210 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
213 ret = strlist__add(sl, buf);
215 ret = strlist__add(sl, tev.event);
216 clear_probe_trace_event(&tev);
220 strlist__delete(rawlist);
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)
238 char *buf = synthesize_probe_trace_command(tev);
239 char sbuf[STRERR_BUFSIZE];
242 pr_debug("Failed to synthesize probe trace event.\n");
246 pr_debug("Writing event: %s\n", buf);
247 if (!probe_event_dry_run) {
248 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
250 pr_warning("Failed to write event: %s\n",
251 str_error_r(errno, sbuf, sizeof(sbuf)));
259 static int __del_trace_probe_event(int fd, struct str_node *ent)
265 /* Convert from perf-probe event to trace-probe event */
266 ret = e_snprintf(buf, 128, "-:%s", ent->s);
270 p = strchr(buf + 2, ':');
272 pr_debug("Internal error: %s should have ':' but not.\n",
279 pr_debug("Writing event: %s\n", buf);
280 ret = write(fd, buf, strlen(buf));
288 pr_warning("Failed to delete event: %s\n",
289 str_error_r(-ret, buf, sizeof(buf)));
293 int probe_file__get_events(int fd, struct strfilter *filter,
294 struct strlist *plist)
296 struct strlist *namelist;
297 struct str_node *ent;
304 namelist = __probe_file__get_namelist(fd, true);
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);
316 strlist__delete(namelist);
321 int probe_file__del_strlist(int fd, struct strlist *namelist)
324 struct str_node *ent;
326 strlist__for_each_entry(ent, namelist) {
327 ret = __del_trace_probe_event(fd, ent);
334 int probe_file__del_events(int fd, struct strfilter *filter)
336 struct strlist *namelist;
339 namelist = strlist__new(NULL, NULL);
343 ret = probe_file__get_events(fd, filter, namelist);
347 ret = probe_file__del_strlist(fd, namelist);
348 strlist__delete(namelist);
353 /* Caller must ensure to remove this entry from list */
354 static void probe_cache_entry__delete(struct probe_cache_entry *entry)
357 BUG_ON(!list_empty(&entry->node));
359 strlist__delete(entry->tevlist);
360 clear_perf_probe_event(&entry->pev);
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));
372 INIT_LIST_HEAD(&entry->node);
373 entry->tevlist = strlist__new(NULL, NULL);
377 entry->spev = synthesize_perf_probe_command(pev);
379 perf_probe_event__copy(&entry->pev, pev) < 0) {
380 probe_cache_entry__delete(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;
396 ret = strlist__nr_entries(entry->tevlist);
397 if (ret > probe_conf.max_probes)
400 *tevs = zalloc(ret * sizeof(*tev));
405 strlist__for_each_entry(node, entry->tevlist) {
407 ret = parse_probe_trace_command(node->s, tev);
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)
417 char cpath[PATH_MAX];
418 char sbuildid[SBUILD_ID_SIZE];
419 char *dir_name = NULL;
420 bool is_kallsyms = false;
423 if (target && build_id_cache__cached(target)) {
424 /* This is a cached buildid */
425 strncpy(sbuildid, target, SBUILD_ID_SIZE);
426 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
430 if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
431 target = DSO__NAME_KALLSYMS;
433 ret = sysfs__sprintf_build_id("/", sbuildid);
435 ret = filename__sprintf_build_id(target, sbuildid);
438 pr_debug("Failed to get build-id from %s.\n", target);
442 /* If we have no buildid cache, make it */
443 if (!build_id_cache__cached(sbuildid)) {
444 ret = build_id_cache__add_s(sbuildid, target,
447 pr_debug("Failed to add build-id cache: %s\n", target);
452 dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
456 pr_debug("Failed to get cache from %s\n", target);
460 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
461 fd = open(cpath, O_CREAT | O_RDWR, 0644);
463 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
470 static int probe_cache__load(struct probe_cache *pcache)
472 struct probe_cache_entry *entry = NULL;
473 char buf[MAX_CMDLEN], *p;
477 fddup = dup(pcache->fd);
480 fp = fdopen(fddup, "r");
487 if (!fgets(buf, MAX_CMDLEN, fp))
489 p = strchr(buf, '\n');
492 /* #perf_probe_event or %sdt_event */
493 if (buf[0] == '#' || buf[0] == '%') {
494 entry = probe_cache_entry__new(NULL);
501 entry->spev = strdup(buf + 1);
503 ret = parse_perf_probe_command(buf + 1,
508 probe_cache_entry__delete(entry);
511 list_add_tail(&entry->node, &pcache->entries);
512 } else { /* trace_probe_event */
517 strlist__add(entry->tevlist, buf);
525 static struct probe_cache *probe_cache__alloc(void)
527 struct probe_cache *pcache = zalloc(sizeof(*pcache));
530 INIT_LIST_HEAD(&pcache->entries);
531 pcache->fd = -EINVAL;
536 void probe_cache__purge(struct probe_cache *pcache)
538 struct probe_cache_entry *entry, *n;
540 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
541 list_del_init(&entry->node);
542 probe_cache_entry__delete(entry);
546 void probe_cache__delete(struct probe_cache *pcache)
551 probe_cache__purge(pcache);
557 struct probe_cache *probe_cache__new(const char *target)
559 struct probe_cache *pcache = probe_cache__alloc();
565 ret = probe_cache__open(pcache, target);
567 pr_debug("Cache open error: %d\n", ret);
571 ret = probe_cache__load(pcache);
573 pr_debug("Cache read error: %d\n", ret);
580 probe_cache__delete(pcache);
584 static bool streql(const char *a, const char *b)
592 return !strcmp(a, b);
595 struct probe_cache_entry *
596 probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
598 struct probe_cache_entry *entry = NULL;
599 char *cmd = synthesize_perf_probe_command(pev);
604 for_each_probe_cache_entry(entry, pcache) {
606 if (entry->pev.event &&
607 streql(entry->pev.event, pev->event) &&
609 streql(entry->pev.group, pev->group)))
614 /* Hit if same event name or same command-string */
616 (streql(entry->pev.group, pev->group) &&
617 streql(entry->pev.event, pev->event))) ||
618 (!strcmp(entry->spev, cmd)))
628 struct probe_cache_entry *
629 probe_cache__find_by_name(struct probe_cache *pcache,
630 const char *group, const char *event)
632 struct probe_cache_entry *entry = NULL;
634 for_each_probe_cache_entry(entry, pcache) {
635 /* Hit if same event name or same command-string */
636 if (streql(entry->pev.group, group) &&
637 streql(entry->pev.event, event))
646 int probe_cache__add_entry(struct probe_cache *pcache,
647 struct perf_probe_event *pev,
648 struct probe_trace_event *tevs, int ntevs)
650 struct probe_cache_entry *entry = NULL;
654 if (!pcache || !pev || !tevs || ntevs <= 0) {
659 /* Remove old cache entry */
660 entry = probe_cache__find(pcache, pev);
662 list_del_init(&entry->node);
663 probe_cache_entry__delete(entry);
667 entry = probe_cache_entry__new(pev);
671 for (i = 0; i < ntevs; i++) {
672 if (!tevs[i].point.symbol)
675 command = synthesize_probe_trace_command(&tevs[i]);
678 strlist__add(entry->tevlist, command);
681 list_add_tail(&entry->node, &pcache->entries);
682 pr_debug("Added probe cache: %d\n", ntevs);
686 pr_debug("Failed to add probe caches\n");
687 probe_cache_entry__delete(entry);
691 #ifdef HAVE_GELF_GETNOTE_SUPPORT
692 static unsigned long long sdt_note__get_addr(struct sdt_note *note)
694 return note->bit32 ? (unsigned long long)note->addr.a32[0]
695 : (unsigned long long)note->addr.a64[0];
698 static const char * const type_to_suffix[] = {
699 ":s64", "", "", "", ":s32", "", ":s16", ":s8",
700 "", ":u8", ":u16", "", ":u32", "", "", "", ":u64"
704 * Isolate the string number and convert it into a decimal value;
705 * this will be an index to get suffix of the uprobe name (defining
708 static int sdt_arg_parse_size(char *n_ptr, const char **suffix)
712 type_idx = strtol(n_ptr, NULL, 10);
713 if (type_idx < -8 || type_idx > 8) {
714 pr_debug4("Failed to get a valid sdt type\n");
718 *suffix = type_to_suffix[type_idx + 8];
722 static int synthesize_sdt_probe_arg(struct strbuf *buf, int i, const char *arg)
724 char *op, *desc = strdup(arg), *new_op = NULL;
725 const char *suffix = "";
729 pr_debug4("Allocation error\n");
734 * Argument is in N@OP format. N is size of the argument and OP is
735 * the actual assembly operand. N can be omitted; in that case
736 * argument is just OP(without @).
738 op = strchr(desc, '@');
743 if (sdt_arg_parse_size(desc, &suffix))
749 ret = arch_sdt_arg_parse_op(op, &new_op);
754 if (ret == SDT_ARG_VALID) {
755 ret = strbuf_addf(buf, " arg%d=%s%s", i + 1, new_op, suffix);
767 static char *synthesize_sdt_probe_command(struct sdt_note *note,
768 const char *pathname,
772 char *ret = NULL, **args;
775 if (strbuf_init(&buf, 32) < 0)
778 if (strbuf_addf(&buf, "p:%s/%s %s:0x%llx",
779 sdtgrp, note->name, pathname,
780 sdt_note__get_addr(note)) < 0)
787 args = argv_split(note->args, &args_count);
789 for (i = 0; i < args_count; ++i) {
790 if (synthesize_sdt_probe_arg(&buf, i, args[i]) < 0)
796 ret = strbuf_detach(&buf, NULL);
798 strbuf_release(&buf);
802 int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
804 struct probe_cache_entry *entry = NULL;
805 struct list_head sdtlist;
806 struct sdt_note *note;
811 INIT_LIST_HEAD(&sdtlist);
812 ret = get_sdt_note_list(&sdtlist, pathname);
814 pr_debug4("Failed to get sdt note: %d\n", ret);
817 list_for_each_entry(note, &sdtlist, note_list) {
818 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
821 /* Try to find same-name entry */
822 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
824 entry = probe_cache_entry__new(NULL);
830 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
831 note->name, note->name);
834 entry->pev.event = strdup(note->name);
835 entry->pev.group = strdup(sdtgrp);
836 list_add_tail(&entry->node, &pcache->entries);
838 buf = synthesize_sdt_probe_command(note, pathname, sdtgrp);
844 strlist__add(entry->tevlist, buf);
849 list_del_init(&entry->node);
850 probe_cache_entry__delete(entry);
852 cleanup_sdt_note_list(&sdtlist);
857 static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
859 struct str_node *snode;
862 const char *prefix = entry->sdt ? "%" : "#";
864 /* Save stat for rollback */
865 ret = fstat(fd, &st);
869 pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
870 iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
871 iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
872 iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
873 ret = writev(fd, iov, 3);
874 if (ret < (int)iov[1].iov_len + 2)
877 strlist__for_each_entry(snode, entry->tevlist) {
878 iov[0].iov_base = (void *)snode->s;
879 iov[0].iov_len = strlen(snode->s);
880 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
881 ret = writev(fd, iov, 2);
882 if (ret < (int)iov[0].iov_len + 1)
888 /* Rollback to avoid cache file corruption */
891 if (ftruncate(fd, st.st_size) < 0)
897 int probe_cache__commit(struct probe_cache *pcache)
899 struct probe_cache_entry *entry;
902 /* TBD: if we do not update existing entries, skip it */
903 ret = lseek(pcache->fd, 0, SEEK_SET);
907 ret = ftruncate(pcache->fd, 0);
911 for_each_probe_cache_entry(entry, pcache) {
912 ret = probe_cache_entry__write(entry, pcache->fd);
913 pr_debug("Cache committed: %d\n", ret);
921 static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
922 struct strfilter *filter)
924 char buf[128], *ptr = entry->spev;
926 if (entry->pev.event) {
927 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
930 return strfilter__compare(filter, ptr);
933 int probe_cache__filter_purge(struct probe_cache *pcache,
934 struct strfilter *filter)
936 struct probe_cache_entry *entry, *tmp;
938 list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
939 if (probe_cache_entry__compare(entry, filter)) {
940 pr_info("Removed cached event: %s\n", entry->spev);
941 list_del_init(&entry->node);
942 probe_cache_entry__delete(entry);
948 static int probe_cache__show_entries(struct probe_cache *pcache,
949 struct strfilter *filter)
951 struct probe_cache_entry *entry;
953 for_each_probe_cache_entry(entry, pcache) {
954 if (probe_cache_entry__compare(entry, filter))
955 printf("%s\n", entry->spev);
960 /* Show all cached probes */
961 int probe_cache__show_all_caches(struct strfilter *filter)
963 struct probe_cache *pcache;
964 struct strlist *bidlist;
966 char *buf = strfilter__string(filter);
968 pr_debug("list cache with filter: %s\n", buf);
971 bidlist = build_id_cache__list_all(true);
973 pr_debug("Failed to get buildids: %d\n", errno);
976 strlist__for_each_entry(nd, bidlist) {
977 pcache = probe_cache__new(nd->s);
980 if (!list_empty(&pcache->entries)) {
981 buf = build_id_cache__origname(nd->s);
982 printf("%s (%s):\n", buf, nd->s);
984 probe_cache__show_entries(pcache, filter);
986 probe_cache__delete(pcache);
988 strlist__delete(bidlist);
994 FTRACE_README_PROBE_TYPE_X = 0,
995 FTRACE_README_KRETPROBE_OFFSET,
1000 const char *pattern;
1002 } ftrace_readme_table[] = {
1003 #define DEFINE_TYPE(idx, pat) \
1004 [idx] = {.pattern = pat, .avail = false}
1005 DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"),
1006 DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"),
1009 static bool scan_ftrace_readme(enum ftrace_readme type)
1016 static bool scanned = false;
1021 fd = open_trace_file("README", false);
1025 fp = fdopen(fd, "r");
1031 while (getline(&buf, &len, fp) > 0)
1032 for (enum ftrace_readme i = 0; i < FTRACE_README_END; i++)
1033 if (!ftrace_readme_table[i].avail)
1034 ftrace_readme_table[i].avail =
1035 strglobmatch(buf, ftrace_readme_table[i].pattern);
1042 if (type >= FTRACE_README_END)
1045 return ftrace_readme_table[type].avail;
1048 bool probe_type_is_available(enum probe_type type)
1050 if (type >= PROBE_TYPE_END)
1052 else if (type == PROBE_TYPE_X)
1053 return scan_ftrace_readme(FTRACE_README_PROBE_TYPE_X);
1058 bool kretprobe_offset_is_supported(void)
1060 return scan_ftrace_readme(FTRACE_README_KRETPROBE_OFFSET);