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.
26 #include <api/fs/tracing_path.h>
27 #include "probe-event.h"
28 #include "probe-file.h"
30 #include "perf_regs.h"
33 /* 4096 - 2 ('\n' + '\0') */
34 #define MAX_CMDLEN 4094
36 static void print_open_warning(int err, bool uprobe)
38 char sbuf[STRERR_BUFSIZE];
44 config = "CONFIG_UPROBE_EVENTS";
46 config = "CONFIG_KPROBE_EVENTS";
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");
54 pr_warning("Failed to open %cprobe_events: %s\n",
56 str_error_r(-err, sbuf, sizeof(sbuf)));
59 static void print_both_open_warning(int kerr, int uerr)
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");
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)));
76 int open_trace_file(const char *trace_file, bool readwrite)
81 ret = e_snprintf(buf, PATH_MAX, "%s/%s",
82 tracing_path, trace_file);
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);
88 ret = open(buf, O_RDONLY, 0);
96 static int open_kprobe_events(bool readwrite)
98 return open_trace_file("kprobe_events", readwrite);
101 static int open_uprobe_events(bool readwrite)
103 return open_trace_file("uprobe_events", readwrite);
106 int probe_file__open(int flag)
110 if (flag & PF_FL_UPROBE)
111 fd = open_uprobe_events(flag & PF_FL_RW);
113 fd = open_kprobe_events(flag & PF_FL_RW);
115 print_open_warning(fd, flag & PF_FL_UPROBE);
120 int probe_file__open_both(int *kfd, int *ufd, int flag)
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);
135 /* Get raw string list of current kprobe_events or uprobe_events */
136 struct strlist *probe_file__get_rawlist(int fd)
140 char buf[MAX_CMDLEN];
147 sl = strlist__new(NULL, NULL);
155 fp = fdopen(fddup, "r");
157 goto out_close_fddup;
160 p = fgets(buf, MAX_CMDLEN, fp);
167 ret = strlist__add(sl, buf);
169 pr_debug("strlist__add failed (%d)\n", ret);
187 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
190 struct strlist *sl, *rawlist;
191 struct str_node *ent;
192 struct probe_trace_event tev;
195 memset(&tev, 0, sizeof(tev));
196 rawlist = probe_file__get_rawlist(fd);
199 sl = strlist__new(NULL, NULL);
200 strlist__for_each_entry(ent, rawlist) {
201 ret = parse_probe_trace_command(ent->s, &tev);
205 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
208 ret = strlist__add(sl, buf);
210 ret = strlist__add(sl, tev.event);
211 clear_probe_trace_event(&tev);
215 strlist__delete(rawlist);
224 /* Get current perf-probe event names */
225 struct strlist *probe_file__get_namelist(int fd)
227 return __probe_file__get_namelist(fd, false);
230 int probe_file__add_event(int fd, struct probe_trace_event *tev)
233 char *buf = synthesize_probe_trace_command(tev);
234 char sbuf[STRERR_BUFSIZE];
237 pr_debug("Failed to synthesize probe trace event.\n");
241 pr_debug("Writing event: %s\n", buf);
242 if (!probe_event_dry_run) {
243 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
245 pr_warning("Failed to write event: %s\n",
246 str_error_r(errno, sbuf, sizeof(sbuf)));
254 static int __del_trace_probe_event(int fd, struct str_node *ent)
260 /* Convert from perf-probe event to trace-probe event */
261 ret = e_snprintf(buf, 128, "-:%s", ent->s);
265 p = strchr(buf + 2, ':');
267 pr_debug("Internal error: %s should have ':' but not.\n",
274 pr_debug("Writing event: %s\n", buf);
275 ret = write(fd, buf, strlen(buf));
283 pr_warning("Failed to delete event: %s\n",
284 str_error_r(-ret, buf, sizeof(buf)));
288 int probe_file__get_events(int fd, struct strfilter *filter,
289 struct strlist *plist)
291 struct strlist *namelist;
292 struct str_node *ent;
299 namelist = __probe_file__get_namelist(fd, true);
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);
311 strlist__delete(namelist);
316 int probe_file__del_strlist(int fd, struct strlist *namelist)
319 struct str_node *ent;
321 strlist__for_each_entry(ent, namelist) {
322 ret = __del_trace_probe_event(fd, ent);
329 int probe_file__del_events(int fd, struct strfilter *filter)
331 struct strlist *namelist;
334 namelist = strlist__new(NULL, NULL);
338 ret = probe_file__get_events(fd, filter, namelist);
342 ret = probe_file__del_strlist(fd, namelist);
343 strlist__delete(namelist);
348 /* Caller must ensure to remove this entry from list */
349 static void probe_cache_entry__delete(struct probe_cache_entry *entry)
352 BUG_ON(!list_empty(&entry->node));
354 strlist__delete(entry->tevlist);
355 clear_perf_probe_event(&entry->pev);
361 static struct probe_cache_entry *
362 probe_cache_entry__new(struct perf_probe_event *pev)
364 struct probe_cache_entry *entry = zalloc(sizeof(*entry));
367 INIT_LIST_HEAD(&entry->node);
368 entry->tevlist = strlist__new(NULL, NULL);
372 entry->spev = synthesize_perf_probe_command(pev);
374 perf_probe_event__copy(&entry->pev, pev) < 0) {
375 probe_cache_entry__delete(entry);
384 int probe_cache_entry__get_event(struct probe_cache_entry *entry,
385 struct probe_trace_event **tevs)
387 struct probe_trace_event *tev;
388 struct str_node *node;
391 ret = strlist__nr_entries(entry->tevlist);
392 if (ret > probe_conf.max_probes)
395 *tevs = zalloc(ret * sizeof(*tev));
400 strlist__for_each_entry(node, entry->tevlist) {
402 ret = parse_probe_trace_command(node->s, tev);
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)
412 char cpath[PATH_MAX];
413 char sbuildid[SBUILD_ID_SIZE];
414 char *dir_name = NULL;
415 bool is_kallsyms = false;
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);
425 if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
426 target = DSO__NAME_KALLSYMS;
428 ret = sysfs__sprintf_build_id("/", sbuildid);
430 ret = filename__sprintf_build_id(target, sbuildid);
433 pr_debug("Failed to get build-id from %s.\n", target);
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,
442 pr_debug("Failed to add build-id cache: %s\n", target);
447 dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
451 pr_debug("Failed to get cache from %s\n", target);
455 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
456 fd = open(cpath, O_CREAT | O_RDWR, 0644);
458 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
465 static int probe_cache__load(struct probe_cache *pcache)
467 struct probe_cache_entry *entry = NULL;
468 char buf[MAX_CMDLEN], *p;
472 fddup = dup(pcache->fd);
475 fp = fdopen(fddup, "r");
482 if (!fgets(buf, MAX_CMDLEN, fp))
484 p = strchr(buf, '\n');
487 /* #perf_probe_event or %sdt_event */
488 if (buf[0] == '#' || buf[0] == '%') {
489 entry = probe_cache_entry__new(NULL);
496 entry->spev = strdup(buf + 1);
498 ret = parse_perf_probe_command(buf + 1,
503 probe_cache_entry__delete(entry);
506 list_add_tail(&entry->node, &pcache->entries);
507 } else { /* trace_probe_event */
512 strlist__add(entry->tevlist, buf);
520 static struct probe_cache *probe_cache__alloc(void)
522 struct probe_cache *pcache = zalloc(sizeof(*pcache));
525 INIT_LIST_HEAD(&pcache->entries);
526 pcache->fd = -EINVAL;
531 void probe_cache__purge(struct probe_cache *pcache)
533 struct probe_cache_entry *entry, *n;
535 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
536 list_del_init(&entry->node);
537 probe_cache_entry__delete(entry);
541 void probe_cache__delete(struct probe_cache *pcache)
546 probe_cache__purge(pcache);
552 struct probe_cache *probe_cache__new(const char *target)
554 struct probe_cache *pcache = probe_cache__alloc();
560 ret = probe_cache__open(pcache, target);
562 pr_debug("Cache open error: %d\n", ret);
566 ret = probe_cache__load(pcache);
568 pr_debug("Cache read error: %d\n", ret);
575 probe_cache__delete(pcache);
579 static bool streql(const char *a, const char *b)
587 return !strcmp(a, b);
590 struct probe_cache_entry *
591 probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
593 struct probe_cache_entry *entry = NULL;
594 char *cmd = synthesize_perf_probe_command(pev);
599 for_each_probe_cache_entry(entry, pcache) {
601 if (entry->pev.event &&
602 streql(entry->pev.event, pev->event) &&
604 streql(entry->pev.group, pev->group)))
609 /* Hit if same event name or same command-string */
611 (streql(entry->pev.group, pev->group) &&
612 streql(entry->pev.event, pev->event))) ||
613 (!strcmp(entry->spev, cmd)))
623 struct probe_cache_entry *
624 probe_cache__find_by_name(struct probe_cache *pcache,
625 const char *group, const char *event)
627 struct probe_cache_entry *entry = NULL;
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))
641 int probe_cache__add_entry(struct probe_cache *pcache,
642 struct perf_probe_event *pev,
643 struct probe_trace_event *tevs, int ntevs)
645 struct probe_cache_entry *entry = NULL;
649 if (!pcache || !pev || !tevs || ntevs <= 0) {
654 /* Remove old cache entry */
655 entry = probe_cache__find(pcache, pev);
657 list_del_init(&entry->node);
658 probe_cache_entry__delete(entry);
662 entry = probe_cache_entry__new(pev);
666 for (i = 0; i < ntevs; i++) {
667 if (!tevs[i].point.symbol)
670 command = synthesize_probe_trace_command(&tevs[i]);
673 strlist__add(entry->tevlist, command);
676 list_add_tail(&entry->node, &pcache->entries);
677 pr_debug("Added probe cache: %d\n", ntevs);
681 pr_debug("Failed to add probe caches\n");
682 probe_cache_entry__delete(entry);
686 #ifdef HAVE_GELF_GETNOTE_SUPPORT
687 static unsigned long long sdt_note__get_addr(struct sdt_note *note)
689 return note->bit32 ? (unsigned long long)note->addr.a32[0]
690 : (unsigned long long)note->addr.a64[0];
693 static const char * const type_to_suffix[] = {
694 ":s64", "", "", "", ":s32", "", ":s16", ":s8",
695 "", ":u8", ":u16", "", ":u32", "", "", "", ":u64"
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
703 static int sdt_arg_parse_size(char *n_ptr, const char **suffix)
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");
713 *suffix = type_to_suffix[type_idx + 8];
717 static int synthesize_sdt_probe_arg(struct strbuf *buf, int i, const char *arg)
719 char *op, *desc = strdup(arg), *new_op = NULL;
720 const char *suffix = "";
724 pr_debug4("Allocation error\n");
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 @).
733 op = strchr(desc, '@');
738 if (sdt_arg_parse_size(desc, &suffix))
744 ret = arch_sdt_arg_parse_op(op, &new_op);
749 if (ret == SDT_ARG_VALID) {
750 ret = strbuf_addf(buf, " arg%d=%s%s", i + 1, new_op, suffix);
762 static char *synthesize_sdt_probe_command(struct sdt_note *note,
763 const char *pathname,
767 char *ret = NULL, **args;
770 if (strbuf_init(&buf, 32) < 0)
773 if (strbuf_addf(&buf, "p:%s/%s %s:0x%llx",
774 sdtgrp, note->name, pathname,
775 sdt_note__get_addr(note)) < 0)
782 args = argv_split(note->args, &args_count);
784 for (i = 0; i < args_count; ++i) {
785 if (synthesize_sdt_probe_arg(&buf, i, args[i]) < 0)
791 ret = strbuf_detach(&buf, NULL);
793 strbuf_release(&buf);
797 int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
799 struct probe_cache_entry *entry = NULL;
800 struct list_head sdtlist;
801 struct sdt_note *note;
806 INIT_LIST_HEAD(&sdtlist);
807 ret = get_sdt_note_list(&sdtlist, pathname);
809 pr_debug4("Failed to get sdt note: %d\n", ret);
812 list_for_each_entry(note, &sdtlist, note_list) {
813 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
816 /* Try to find same-name entry */
817 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
819 entry = probe_cache_entry__new(NULL);
825 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
826 note->name, note->name);
829 entry->pev.event = strdup(note->name);
830 entry->pev.group = strdup(sdtgrp);
831 list_add_tail(&entry->node, &pcache->entries);
833 buf = synthesize_sdt_probe_command(note, pathname, sdtgrp);
839 strlist__add(entry->tevlist, buf);
844 list_del_init(&entry->node);
845 probe_cache_entry__delete(entry);
847 cleanup_sdt_note_list(&sdtlist);
852 static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
854 struct str_node *snode;
857 const char *prefix = entry->sdt ? "%" : "#";
859 /* Save stat for rollback */
860 ret = fstat(fd, &st);
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)
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)
883 /* Rollback to avoid cache file corruption */
886 if (ftruncate(fd, st.st_size) < 0)
892 int probe_cache__commit(struct probe_cache *pcache)
894 struct probe_cache_entry *entry;
897 /* TBD: if we do not update existing entries, skip it */
898 ret = lseek(pcache->fd, 0, SEEK_SET);
902 ret = ftruncate(pcache->fd, 0);
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);
916 static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
917 struct strfilter *filter)
919 char buf[128], *ptr = entry->spev;
921 if (entry->pev.event) {
922 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
925 return strfilter__compare(filter, ptr);
928 int probe_cache__filter_purge(struct probe_cache *pcache,
929 struct strfilter *filter)
931 struct probe_cache_entry *entry, *tmp;
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);
943 static int probe_cache__show_entries(struct probe_cache *pcache,
944 struct strfilter *filter)
946 struct probe_cache_entry *entry;
948 for_each_probe_cache_entry(entry, pcache) {
949 if (probe_cache_entry__compare(entry, filter))
950 printf("%s\n", entry->spev);
955 /* Show all cached probes */
956 int probe_cache__show_all_caches(struct strfilter *filter)
958 struct probe_cache *pcache;
959 struct strlist *bidlist;
961 char *buf = strfilter__string(filter);
963 pr_debug("list cache with filter: %s\n", buf);
966 bidlist = build_id_cache__list_all(true);
968 pr_debug("Failed to get buildids: %d\n", errno);
971 strlist__for_each_entry(nd, bidlist) {
972 pcache = probe_cache__new(nd->s);
975 if (!list_empty(&pcache->entries)) {
976 buf = build_id_cache__origname(nd->s);
977 printf("%s (%s):\n", buf, nd->s);
979 probe_cache__show_entries(pcache, filter);
981 probe_cache__delete(pcache);
983 strlist__delete(bidlist);
989 FTRACE_README_PROBE_TYPE_X = 0,
990 FTRACE_README_KRETPROBE_OFFSET,
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): *"),
1004 static bool scan_ftrace_readme(enum ftrace_readme type)
1011 static bool scanned = false;
1016 fd = open_trace_file("README", false);
1020 fp = fdopen(fd, "r");
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);
1037 if (type >= FTRACE_README_END)
1040 return ftrace_readme_table[type].avail;
1043 bool probe_type_is_available(enum probe_type type)
1045 if (type >= PROBE_TYPE_END)
1047 else if (type == PROBE_TYPE_X)
1048 return scan_ftrace_readme(FTRACE_README_PROBE_TYPE_X);
1053 bool kretprobe_offset_is_supported(void)
1055 return scan_ftrace_readme(FTRACE_README_KRETPROBE_OFFSET);