1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * probe-file.c : operate ftrace k/uprobe events files
5 * Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
10 #include <sys/types.h>
13 #include <linux/zalloc.h>
14 #include "namespaces.h"
17 #include "strfilter.h"
24 #include <api/fs/tracing_path.h>
25 #include "probe-event.h"
26 #include "probe-file.h"
28 #include "perf_regs.h"
31 /* 4096 - 2 ('\n' + '\0') */
32 #define MAX_CMDLEN 4094
34 static void print_open_warning(int err, bool uprobe)
36 char sbuf[STRERR_BUFSIZE];
42 config = "CONFIG_UPROBE_EVENTS";
44 config = "CONFIG_KPROBE_EVENTS";
46 pr_warning("%cprobe_events file does not exist"
47 " - please rebuild kernel with %s.\n",
48 uprobe ? 'u' : 'k', config);
49 } else if (err == -ENOTSUP)
50 pr_warning("Tracefs or debugfs is not mounted.\n");
52 pr_warning("Failed to open %cprobe_events: %s\n",
54 str_error_r(-err, sbuf, sizeof(sbuf)));
57 static void print_both_open_warning(int kerr, int uerr)
59 /* Both kprobes and uprobes are disabled, warn it. */
60 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
61 pr_warning("Tracefs or debugfs is not mounted.\n");
62 else if (kerr == -ENOENT && uerr == -ENOENT)
63 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
64 "or/and CONFIG_UPROBE_EVENTS.\n");
66 char sbuf[STRERR_BUFSIZE];
67 pr_warning("Failed to open kprobe events: %s.\n",
68 str_error_r(-kerr, sbuf, sizeof(sbuf)));
69 pr_warning("Failed to open uprobe events: %s.\n",
70 str_error_r(-uerr, sbuf, sizeof(sbuf)));
74 int open_trace_file(const char *trace_file, bool readwrite)
79 ret = e_snprintf(buf, PATH_MAX, "%s/%s", tracing_path_mount(), trace_file);
81 pr_debug("Opening %s write=%d\n", buf, readwrite);
82 if (readwrite && !probe_event_dry_run)
83 ret = open(buf, O_RDWR | O_APPEND, 0);
85 ret = open(buf, O_RDONLY, 0);
93 static int open_kprobe_events(bool readwrite)
95 return open_trace_file("kprobe_events", readwrite);
98 static int open_uprobe_events(bool readwrite)
100 return open_trace_file("uprobe_events", readwrite);
103 int probe_file__open(int flag)
107 if (flag & PF_FL_UPROBE)
108 fd = open_uprobe_events(flag & PF_FL_RW);
110 fd = open_kprobe_events(flag & PF_FL_RW);
112 print_open_warning(fd, flag & PF_FL_UPROBE);
117 int probe_file__open_both(int *kfd, int *ufd, int flag)
122 *kfd = open_kprobe_events(flag & PF_FL_RW);
123 *ufd = open_uprobe_events(flag & PF_FL_RW);
124 if (*kfd < 0 && *ufd < 0) {
125 print_both_open_warning(*kfd, *ufd);
132 /* Get raw string list of current kprobe_events or uprobe_events */
133 struct strlist *probe_file__get_rawlist(int fd)
137 char buf[MAX_CMDLEN];
144 sl = strlist__new(NULL, NULL);
152 fp = fdopen(fddup, "r");
154 goto out_close_fddup;
157 p = fgets(buf, MAX_CMDLEN, fp);
164 ret = strlist__add(sl, buf);
166 pr_debug("strlist__add failed (%d)\n", ret);
184 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
187 struct strlist *sl, *rawlist;
188 struct str_node *ent;
189 struct probe_trace_event tev;
192 memset(&tev, 0, sizeof(tev));
193 rawlist = probe_file__get_rawlist(fd);
196 sl = strlist__new(NULL, NULL);
197 strlist__for_each_entry(ent, rawlist) {
198 ret = parse_probe_trace_command(ent->s, &tev);
202 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
205 ret = strlist__add(sl, buf);
207 ret = strlist__add(sl, tev.event);
208 clear_probe_trace_event(&tev);
209 /* Skip if there is same name multi-probe event in the list */
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 ret = strlist__add(plist, ent->s);
308 if (ret == -ENOMEM) {
309 pr_err("strlist__add failed with -ENOMEM\n");
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,
418 char cpath[PATH_MAX];
419 char sbuildid[SBUILD_ID_SIZE];
420 char *dir_name = NULL;
421 bool is_kallsyms = false;
425 if (target && build_id_cache__cached(target)) {
426 /* This is a cached buildid */
427 strlcpy(sbuildid, target, SBUILD_ID_SIZE);
428 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
432 if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
433 target = DSO__NAME_KALLSYMS;
435 ret = sysfs__sprintf_build_id("/", sbuildid);
437 nsinfo__mountns_enter(nsi, &nsc);
438 ret = filename__sprintf_build_id(target, sbuildid);
439 nsinfo__mountns_exit(&nsc);
443 pr_debug("Failed to get build-id from %s.\n", target);
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,
452 pr_debug("Failed to add build-id cache: %s\n", target);
457 dir_name = build_id_cache__cachedir(sbuildid, target, nsi, is_kallsyms,
461 pr_debug("Failed to get cache from %s\n", target);
465 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
466 fd = open(cpath, O_CREAT | O_RDWR, 0644);
468 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
475 static int probe_cache__load(struct probe_cache *pcache)
477 struct probe_cache_entry *entry = NULL;
478 char buf[MAX_CMDLEN], *p;
482 fddup = dup(pcache->fd);
485 fp = fdopen(fddup, "r");
492 if (!fgets(buf, MAX_CMDLEN, fp))
494 p = strchr(buf, '\n');
497 /* #perf_probe_event or %sdt_event */
498 if (buf[0] == '#' || buf[0] == '%') {
499 entry = probe_cache_entry__new(NULL);
506 entry->spev = strdup(buf + 1);
508 ret = parse_perf_probe_command(buf + 1,
513 probe_cache_entry__delete(entry);
516 list_add_tail(&entry->node, &pcache->entries);
517 } else { /* trace_probe_event */
522 ret = strlist__add(entry->tevlist, buf);
523 if (ret == -ENOMEM) {
524 pr_err("strlist__add failed with -ENOMEM\n");
534 static struct probe_cache *probe_cache__alloc(void)
536 struct probe_cache *pcache = zalloc(sizeof(*pcache));
539 INIT_LIST_HEAD(&pcache->entries);
540 pcache->fd = -EINVAL;
545 void probe_cache__purge(struct probe_cache *pcache)
547 struct probe_cache_entry *entry, *n;
549 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
550 list_del_init(&entry->node);
551 probe_cache_entry__delete(entry);
555 void probe_cache__delete(struct probe_cache *pcache)
560 probe_cache__purge(pcache);
566 struct probe_cache *probe_cache__new(const char *target, struct nsinfo *nsi)
568 struct probe_cache *pcache = probe_cache__alloc();
574 ret = probe_cache__open(pcache, target, nsi);
576 pr_debug("Cache open error: %d\n", ret);
580 ret = probe_cache__load(pcache);
582 pr_debug("Cache read error: %d\n", ret);
589 probe_cache__delete(pcache);
593 static bool streql(const char *a, const char *b)
601 return !strcmp(a, b);
604 struct probe_cache_entry *
605 probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
607 struct probe_cache_entry *entry = NULL;
608 char *cmd = synthesize_perf_probe_command(pev);
613 for_each_probe_cache_entry(entry, pcache) {
615 if (entry->pev.event &&
616 streql(entry->pev.event, pev->event) &&
618 streql(entry->pev.group, pev->group)))
623 /* Hit if same event name or same command-string */
625 (streql(entry->pev.group, pev->group) &&
626 streql(entry->pev.event, pev->event))) ||
627 (!strcmp(entry->spev, cmd)))
637 struct probe_cache_entry *
638 probe_cache__find_by_name(struct probe_cache *pcache,
639 const char *group, const char *event)
641 struct probe_cache_entry *entry = NULL;
643 for_each_probe_cache_entry(entry, pcache) {
644 /* Hit if same event name or same command-string */
645 if (streql(entry->pev.group, group) &&
646 streql(entry->pev.event, event))
655 int probe_cache__add_entry(struct probe_cache *pcache,
656 struct perf_probe_event *pev,
657 struct probe_trace_event *tevs, int ntevs)
659 struct probe_cache_entry *entry = NULL;
663 if (!pcache || !pev || !tevs || ntevs <= 0) {
668 /* Remove old cache entry */
669 entry = probe_cache__find(pcache, pev);
671 list_del_init(&entry->node);
672 probe_cache_entry__delete(entry);
676 entry = probe_cache_entry__new(pev);
680 for (i = 0; i < ntevs; i++) {
681 if (!tevs[i].point.symbol)
684 command = synthesize_probe_trace_command(&tevs[i]);
687 ret = strlist__add(entry->tevlist, command);
688 if (ret == -ENOMEM) {
689 pr_err("strlist__add failed with -ENOMEM\n");
695 list_add_tail(&entry->node, &pcache->entries);
696 pr_debug("Added probe cache: %d\n", ntevs);
700 pr_debug("Failed to add probe caches\n");
701 probe_cache_entry__delete(entry);
705 #ifdef HAVE_GELF_GETNOTE_SUPPORT
706 static unsigned long long sdt_note__get_addr(struct sdt_note *note)
709 (unsigned long long)note->addr.a32[SDT_NOTE_IDX_LOC] :
710 (unsigned long long)note->addr.a64[SDT_NOTE_IDX_LOC];
713 static unsigned long long sdt_note__get_ref_ctr_offset(struct sdt_note *note)
716 (unsigned long long)note->addr.a32[SDT_NOTE_IDX_REFCTR] :
717 (unsigned long long)note->addr.a64[SDT_NOTE_IDX_REFCTR];
720 static const char * const type_to_suffix[] = {
721 ":s64", "", "", "", ":s32", "", ":s16", ":s8",
722 "", ":u8", ":u16", "", ":u32", "", "", "", ":u64"
726 * Isolate the string number and convert it into a decimal value;
727 * this will be an index to get suffix of the uprobe name (defining
730 static int sdt_arg_parse_size(char *n_ptr, const char **suffix)
734 type_idx = strtol(n_ptr, NULL, 10);
735 if (type_idx < -8 || type_idx > 8) {
736 pr_debug4("Failed to get a valid sdt type\n");
740 *suffix = type_to_suffix[type_idx + 8];
744 static int synthesize_sdt_probe_arg(struct strbuf *buf, int i, const char *arg)
746 char *op, *desc = strdup(arg), *new_op = NULL;
747 const char *suffix = "";
751 pr_debug4("Allocation error\n");
756 * Argument is in N@OP format. N is size of the argument and OP is
757 * the actual assembly operand. N can be omitted; in that case
758 * argument is just OP(without @).
760 op = strchr(desc, '@');
765 if (sdt_arg_parse_size(desc, &suffix))
771 ret = arch_sdt_arg_parse_op(op, &new_op);
776 if (ret == SDT_ARG_VALID) {
777 ret = strbuf_addf(buf, " arg%d=%s%s", i + 1, new_op, suffix);
789 static char *synthesize_sdt_probe_command(struct sdt_note *note,
790 const char *pathname,
795 int i, args_count, err;
796 unsigned long long ref_ctr_offset;
798 if (strbuf_init(&buf, 32) < 0)
801 err = strbuf_addf(&buf, "p:%s/%s %s:0x%llx",
802 sdtgrp, note->name, pathname,
803 sdt_note__get_addr(note));
805 ref_ctr_offset = sdt_note__get_ref_ctr_offset(note);
806 if (ref_ctr_offset && err >= 0)
807 err = strbuf_addf(&buf, "(0x%llx)", ref_ctr_offset);
816 char **args = argv_split(note->args, &args_count);
821 for (i = 0; i < args_count; ++i) {
822 if (synthesize_sdt_probe_arg(&buf, i, args[i]) < 0) {
832 ret = strbuf_detach(&buf, NULL);
834 strbuf_release(&buf);
838 int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
840 struct probe_cache_entry *entry = NULL;
841 struct list_head sdtlist;
842 struct sdt_note *note;
847 INIT_LIST_HEAD(&sdtlist);
848 ret = get_sdt_note_list(&sdtlist, pathname);
850 pr_debug4("Failed to get sdt note: %d\n", ret);
853 list_for_each_entry(note, &sdtlist, note_list) {
854 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
857 /* Try to find same-name entry */
858 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
860 entry = probe_cache_entry__new(NULL);
866 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
867 note->name, note->name);
870 entry->pev.event = strdup(note->name);
871 entry->pev.group = strdup(sdtgrp);
872 list_add_tail(&entry->node, &pcache->entries);
874 buf = synthesize_sdt_probe_command(note, pathname, sdtgrp);
880 ret = strlist__add(entry->tevlist, buf);
885 if (ret == -ENOMEM) {
886 pr_err("strlist__add failed with -ENOMEM\n");
891 list_del_init(&entry->node);
892 probe_cache_entry__delete(entry);
894 cleanup_sdt_note_list(&sdtlist);
899 static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
901 struct str_node *snode;
904 const char *prefix = entry->sdt ? "%" : "#";
906 /* Save stat for rollback */
907 ret = fstat(fd, &st);
911 pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
912 iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
913 iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
914 iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
915 ret = writev(fd, iov, 3);
916 if (ret < (int)iov[1].iov_len + 2)
919 strlist__for_each_entry(snode, entry->tevlist) {
920 iov[0].iov_base = (void *)snode->s;
921 iov[0].iov_len = strlen(snode->s);
922 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
923 ret = writev(fd, iov, 2);
924 if (ret < (int)iov[0].iov_len + 1)
930 /* Rollback to avoid cache file corruption */
933 if (ftruncate(fd, st.st_size) < 0)
939 int probe_cache__commit(struct probe_cache *pcache)
941 struct probe_cache_entry *entry;
944 /* TBD: if we do not update existing entries, skip it */
945 ret = lseek(pcache->fd, 0, SEEK_SET);
949 ret = ftruncate(pcache->fd, 0);
953 for_each_probe_cache_entry(entry, pcache) {
954 ret = probe_cache_entry__write(entry, pcache->fd);
955 pr_debug("Cache committed: %d\n", ret);
963 static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
964 struct strfilter *filter)
966 char buf[128], *ptr = entry->spev;
968 if (entry->pev.event) {
969 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
972 return strfilter__compare(filter, ptr);
975 int probe_cache__filter_purge(struct probe_cache *pcache,
976 struct strfilter *filter)
978 struct probe_cache_entry *entry, *tmp;
980 list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
981 if (probe_cache_entry__compare(entry, filter)) {
982 pr_info("Removed cached event: %s\n", entry->spev);
983 list_del_init(&entry->node);
984 probe_cache_entry__delete(entry);
990 static int probe_cache__show_entries(struct probe_cache *pcache,
991 struct strfilter *filter)
993 struct probe_cache_entry *entry;
995 for_each_probe_cache_entry(entry, pcache) {
996 if (probe_cache_entry__compare(entry, filter))
997 printf("%s\n", entry->spev);
1002 /* Show all cached probes */
1003 int probe_cache__show_all_caches(struct strfilter *filter)
1005 struct probe_cache *pcache;
1006 struct strlist *bidlist;
1007 struct str_node *nd;
1008 char *buf = strfilter__string(filter);
1010 pr_debug("list cache with filter: %s\n", buf);
1013 bidlist = build_id_cache__list_all(true);
1015 pr_debug("Failed to get buildids: %d\n", errno);
1018 strlist__for_each_entry(nd, bidlist) {
1019 pcache = probe_cache__new(nd->s, NULL);
1022 if (!list_empty(&pcache->entries)) {
1023 buf = build_id_cache__origname(nd->s);
1024 printf("%s (%s):\n", buf, nd->s);
1026 probe_cache__show_entries(pcache, filter);
1028 probe_cache__delete(pcache);
1030 strlist__delete(bidlist);
1035 enum ftrace_readme {
1036 FTRACE_README_PROBE_TYPE_X = 0,
1037 FTRACE_README_KRETPROBE_OFFSET,
1038 FTRACE_README_UPROBE_REF_CTR,
1039 FTRACE_README_USER_ACCESS,
1040 FTRACE_README_MULTIPROBE_EVENT,
1041 FTRACE_README_IMMEDIATE_VALUE,
1046 const char *pattern;
1048 } ftrace_readme_table[] = {
1049 #define DEFINE_TYPE(idx, pat) \
1050 [idx] = {.pattern = pat, .avail = false}
1051 DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"),
1052 DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"),
1053 DEFINE_TYPE(FTRACE_README_UPROBE_REF_CTR, "*ref_ctr_offset*"),
1054 DEFINE_TYPE(FTRACE_README_USER_ACCESS, "*u]<offset>*"),
1055 DEFINE_TYPE(FTRACE_README_MULTIPROBE_EVENT, "*Create/append/*"),
1056 DEFINE_TYPE(FTRACE_README_IMMEDIATE_VALUE, "*\\imm-value,*"),
1059 static bool scan_ftrace_readme(enum ftrace_readme type)
1066 static bool scanned = false;
1071 fd = open_trace_file("README", false);
1075 fp = fdopen(fd, "r");
1081 while (getline(&buf, &len, fp) > 0)
1082 for (enum ftrace_readme i = 0; i < FTRACE_README_END; i++)
1083 if (!ftrace_readme_table[i].avail)
1084 ftrace_readme_table[i].avail =
1085 strglobmatch(buf, ftrace_readme_table[i].pattern);
1092 if (type >= FTRACE_README_END)
1095 return ftrace_readme_table[type].avail;
1098 bool probe_type_is_available(enum probe_type type)
1100 if (type >= PROBE_TYPE_END)
1102 else if (type == PROBE_TYPE_X)
1103 return scan_ftrace_readme(FTRACE_README_PROBE_TYPE_X);
1108 bool kretprobe_offset_is_supported(void)
1110 return scan_ftrace_readme(FTRACE_README_KRETPROBE_OFFSET);
1113 bool uprobe_ref_ctr_is_supported(void)
1115 return scan_ftrace_readme(FTRACE_README_UPROBE_REF_CTR);
1118 bool user_access_is_supported(void)
1120 return scan_ftrace_readme(FTRACE_README_USER_ACCESS);
1123 bool multiprobe_event_is_supported(void)
1125 return scan_ftrace_readme(FTRACE_README_MULTIPROBE_EVENT);
1128 bool immediate_value_is_supported(void)
1130 return scan_ftrace_readme(FTRACE_README_IMMEDIATE_VALUE);