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"
31 #define MAX_CMDLEN 256
33 static void print_open_warning(int err, bool uprobe)
35 char sbuf[STRERR_BUFSIZE];
41 config = "CONFIG_UPROBE_EVENTS";
43 config = "CONFIG_KPROBE_EVENTS";
45 pr_warning("%cprobe_events file does not exist"
46 " - please rebuild kernel with %s.\n",
47 uprobe ? 'u' : 'k', config);
48 } else if (err == -ENOTSUP)
49 pr_warning("Tracefs or debugfs is not mounted.\n");
51 pr_warning("Failed to open %cprobe_events: %s\n",
53 str_error_r(-err, sbuf, sizeof(sbuf)));
56 static void print_both_open_warning(int kerr, int uerr)
58 /* Both kprobes and uprobes are disabled, warn it. */
59 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
60 pr_warning("Tracefs or debugfs is not mounted.\n");
61 else if (kerr == -ENOENT && uerr == -ENOENT)
62 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
63 "or/and CONFIG_UPROBE_EVENTS.\n");
65 char sbuf[STRERR_BUFSIZE];
66 pr_warning("Failed to open kprobe events: %s.\n",
67 str_error_r(-kerr, sbuf, sizeof(sbuf)));
68 pr_warning("Failed to open uprobe events: %s.\n",
69 str_error_r(-uerr, sbuf, sizeof(sbuf)));
73 static int open_probe_events(const char *trace_file, bool readwrite)
76 const char *tracing_dir = "";
79 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
80 tracing_path, tracing_dir, trace_file);
82 pr_debug("Opening %s write=%d\n", buf, readwrite);
83 if (readwrite && !probe_event_dry_run)
84 ret = open(buf, O_RDWR | O_APPEND, 0);
86 ret = open(buf, O_RDONLY, 0);
94 static int open_kprobe_events(bool readwrite)
96 return open_probe_events("kprobe_events", readwrite);
99 static int open_uprobe_events(bool readwrite)
101 return open_probe_events("uprobe_events", readwrite);
104 int probe_file__open(int flag)
108 if (flag & PF_FL_UPROBE)
109 fd = open_uprobe_events(flag & PF_FL_RW);
111 fd = open_kprobe_events(flag & PF_FL_RW);
113 print_open_warning(fd, flag & PF_FL_UPROBE);
118 int probe_file__open_both(int *kfd, int *ufd, int flag)
123 *kfd = open_kprobe_events(flag & PF_FL_RW);
124 *ufd = open_uprobe_events(flag & PF_FL_RW);
125 if (*kfd < 0 && *ufd < 0) {
126 print_both_open_warning(*kfd, *ufd);
133 /* Get raw string list of current kprobe_events or uprobe_events */
134 struct strlist *probe_file__get_rawlist(int fd)
138 char buf[MAX_CMDLEN];
145 sl = strlist__new(NULL, NULL);
151 fp = fdopen(fddup, "r");
153 goto out_close_fddup;
156 p = fgets(buf, MAX_CMDLEN, fp);
163 ret = strlist__add(sl, buf);
165 pr_debug("strlist__add failed (%d)\n", ret);
181 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
184 struct strlist *sl, *rawlist;
185 struct str_node *ent;
186 struct probe_trace_event tev;
189 memset(&tev, 0, sizeof(tev));
190 rawlist = probe_file__get_rawlist(fd);
193 sl = strlist__new(NULL, NULL);
194 strlist__for_each_entry(ent, rawlist) {
195 ret = parse_probe_trace_command(ent->s, &tev);
199 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
202 ret = strlist__add(sl, buf);
204 ret = strlist__add(sl, tev.event);
205 clear_probe_trace_event(&tev);
209 strlist__delete(rawlist);
218 /* Get current perf-probe event names */
219 struct strlist *probe_file__get_namelist(int fd)
221 return __probe_file__get_namelist(fd, false);
224 int probe_file__add_event(int fd, struct probe_trace_event *tev)
227 char *buf = synthesize_probe_trace_command(tev);
228 char sbuf[STRERR_BUFSIZE];
231 pr_debug("Failed to synthesize probe trace event.\n");
235 pr_debug("Writing event: %s\n", buf);
236 if (!probe_event_dry_run) {
237 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
239 pr_warning("Failed to write event: %s\n",
240 str_error_r(errno, sbuf, sizeof(sbuf)));
248 static int __del_trace_probe_event(int fd, struct str_node *ent)
254 /* Convert from perf-probe event to trace-probe event */
255 ret = e_snprintf(buf, 128, "-:%s", ent->s);
259 p = strchr(buf + 2, ':');
261 pr_debug("Internal error: %s should have ':' but not.\n",
268 pr_debug("Writing event: %s\n", buf);
269 ret = write(fd, buf, strlen(buf));
277 pr_warning("Failed to delete event: %s\n",
278 str_error_r(-ret, buf, sizeof(buf)));
282 int probe_file__get_events(int fd, struct strfilter *filter,
283 struct strlist *plist)
285 struct strlist *namelist;
286 struct str_node *ent;
293 namelist = __probe_file__get_namelist(fd, true);
297 strlist__for_each_entry(ent, namelist) {
298 p = strchr(ent->s, ':');
299 if ((p && strfilter__compare(filter, p + 1)) ||
300 strfilter__compare(filter, ent->s)) {
301 strlist__add(plist, ent->s);
305 strlist__delete(namelist);
310 int probe_file__del_strlist(int fd, struct strlist *namelist)
313 struct str_node *ent;
315 strlist__for_each_entry(ent, namelist) {
316 ret = __del_trace_probe_event(fd, ent);
323 int probe_file__del_events(int fd, struct strfilter *filter)
325 struct strlist *namelist;
328 namelist = strlist__new(NULL, NULL);
332 ret = probe_file__get_events(fd, filter, namelist);
336 ret = probe_file__del_strlist(fd, namelist);
337 strlist__delete(namelist);
342 /* Caller must ensure to remove this entry from list */
343 static void probe_cache_entry__delete(struct probe_cache_entry *entry)
346 BUG_ON(!list_empty(&entry->node));
348 strlist__delete(entry->tevlist);
349 clear_perf_probe_event(&entry->pev);
355 static struct probe_cache_entry *
356 probe_cache_entry__new(struct perf_probe_event *pev)
358 struct probe_cache_entry *entry = zalloc(sizeof(*entry));
361 INIT_LIST_HEAD(&entry->node);
362 entry->tevlist = strlist__new(NULL, NULL);
366 entry->spev = synthesize_perf_probe_command(pev);
368 perf_probe_event__copy(&entry->pev, pev) < 0) {
369 probe_cache_entry__delete(entry);
378 int probe_cache_entry__get_event(struct probe_cache_entry *entry,
379 struct probe_trace_event **tevs)
381 struct probe_trace_event *tev;
382 struct str_node *node;
385 ret = strlist__nr_entries(entry->tevlist);
386 if (ret > probe_conf.max_probes)
389 *tevs = zalloc(ret * sizeof(*tev));
394 strlist__for_each_entry(node, entry->tevlist) {
396 ret = parse_probe_trace_command(node->s, tev);
403 /* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
404 static int probe_cache__open(struct probe_cache *pcache, const char *target)
406 char cpath[PATH_MAX];
407 char sbuildid[SBUILD_ID_SIZE];
408 char *dir_name = NULL;
409 bool is_kallsyms = false;
412 if (target && build_id_cache__cached(target)) {
413 /* This is a cached buildid */
414 strncpy(sbuildid, target, SBUILD_ID_SIZE);
415 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
419 if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
420 target = DSO__NAME_KALLSYMS;
422 ret = sysfs__sprintf_build_id("/", sbuildid);
424 ret = filename__sprintf_build_id(target, sbuildid);
427 pr_debug("Failed to get build-id from %s.\n", target);
431 /* If we have no buildid cache, make it */
432 if (!build_id_cache__cached(sbuildid)) {
433 ret = build_id_cache__add_s(sbuildid, target,
436 pr_debug("Failed to add build-id cache: %s\n", target);
441 dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
445 pr_debug("Failed to get cache from %s\n", target);
449 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
450 fd = open(cpath, O_CREAT | O_RDWR, 0644);
452 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
459 static int probe_cache__load(struct probe_cache *pcache)
461 struct probe_cache_entry *entry = NULL;
462 char buf[MAX_CMDLEN], *p;
466 fddup = dup(pcache->fd);
469 fp = fdopen(fddup, "r");
474 if (!fgets(buf, MAX_CMDLEN, fp))
476 p = strchr(buf, '\n');
479 /* #perf_probe_event or %sdt_event */
480 if (buf[0] == '#' || buf[0] == '%') {
481 entry = probe_cache_entry__new(NULL);
488 entry->spev = strdup(buf + 1);
490 ret = parse_perf_probe_command(buf + 1,
495 probe_cache_entry__delete(entry);
498 list_add_tail(&entry->node, &pcache->entries);
499 } else { /* trace_probe_event */
504 strlist__add(entry->tevlist, buf);
512 static struct probe_cache *probe_cache__alloc(void)
514 struct probe_cache *pcache = zalloc(sizeof(*pcache));
517 INIT_LIST_HEAD(&pcache->entries);
518 pcache->fd = -EINVAL;
523 void probe_cache__purge(struct probe_cache *pcache)
525 struct probe_cache_entry *entry, *n;
527 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
528 list_del_init(&entry->node);
529 probe_cache_entry__delete(entry);
533 void probe_cache__delete(struct probe_cache *pcache)
538 probe_cache__purge(pcache);
544 struct probe_cache *probe_cache__new(const char *target)
546 struct probe_cache *pcache = probe_cache__alloc();
552 ret = probe_cache__open(pcache, target);
554 pr_debug("Cache open error: %d\n", ret);
558 ret = probe_cache__load(pcache);
560 pr_debug("Cache read error: %d\n", ret);
567 probe_cache__delete(pcache);
571 static bool streql(const char *a, const char *b)
579 return !strcmp(a, b);
582 struct probe_cache_entry *
583 probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
585 struct probe_cache_entry *entry = NULL;
586 char *cmd = synthesize_perf_probe_command(pev);
591 for_each_probe_cache_entry(entry, pcache) {
593 if (entry->pev.event &&
594 streql(entry->pev.event, pev->event) &&
596 streql(entry->pev.group, pev->group)))
601 /* Hit if same event name or same command-string */
603 (streql(entry->pev.group, pev->group) &&
604 streql(entry->pev.event, pev->event))) ||
605 (!strcmp(entry->spev, cmd)))
615 struct probe_cache_entry *
616 probe_cache__find_by_name(struct probe_cache *pcache,
617 const char *group, const char *event)
619 struct probe_cache_entry *entry = NULL;
621 for_each_probe_cache_entry(entry, pcache) {
622 /* Hit if same event name or same command-string */
623 if (streql(entry->pev.group, group) &&
624 streql(entry->pev.event, event))
633 int probe_cache__add_entry(struct probe_cache *pcache,
634 struct perf_probe_event *pev,
635 struct probe_trace_event *tevs, int ntevs)
637 struct probe_cache_entry *entry = NULL;
641 if (!pcache || !pev || !tevs || ntevs <= 0) {
646 /* Remove old cache entry */
647 entry = probe_cache__find(pcache, pev);
649 list_del_init(&entry->node);
650 probe_cache_entry__delete(entry);
654 entry = probe_cache_entry__new(pev);
658 for (i = 0; i < ntevs; i++) {
659 if (!tevs[i].point.symbol)
662 command = synthesize_probe_trace_command(&tevs[i]);
665 strlist__add(entry->tevlist, command);
668 list_add_tail(&entry->node, &pcache->entries);
669 pr_debug("Added probe cache: %d\n", ntevs);
673 pr_debug("Failed to add probe caches\n");
674 probe_cache_entry__delete(entry);
678 #ifdef HAVE_GELF_GETNOTE_SUPPORT
679 static unsigned long long sdt_note__get_addr(struct sdt_note *note)
681 return note->bit32 ? (unsigned long long)note->addr.a32[0]
682 : (unsigned long long)note->addr.a64[0];
685 int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
687 struct probe_cache_entry *entry = NULL;
688 struct list_head sdtlist;
689 struct sdt_note *note;
694 INIT_LIST_HEAD(&sdtlist);
695 ret = get_sdt_note_list(&sdtlist, pathname);
697 pr_debug("Failed to get sdt note: %d\n", ret);
700 list_for_each_entry(note, &sdtlist, note_list) {
701 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
704 /* Try to find same-name entry */
705 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
707 entry = probe_cache_entry__new(NULL);
713 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
714 note->name, note->name);
717 entry->pev.event = strdup(note->name);
718 entry->pev.group = strdup(sdtgrp);
719 list_add_tail(&entry->node, &pcache->entries);
721 ret = asprintf(&buf, "p:%s/%s %s:0x%llx",
722 sdtgrp, note->name, pathname,
723 sdt_note__get_addr(note));
726 strlist__add(entry->tevlist, buf);
731 list_del_init(&entry->node);
732 probe_cache_entry__delete(entry);
734 cleanup_sdt_note_list(&sdtlist);
739 static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
741 struct str_node *snode;
744 const char *prefix = entry->sdt ? "%" : "#";
746 /* Save stat for rollback */
747 ret = fstat(fd, &st);
751 pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
752 iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
753 iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
754 iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
755 ret = writev(fd, iov, 3);
756 if (ret < (int)iov[1].iov_len + 2)
759 strlist__for_each_entry(snode, entry->tevlist) {
760 iov[0].iov_base = (void *)snode->s;
761 iov[0].iov_len = strlen(snode->s);
762 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
763 ret = writev(fd, iov, 2);
764 if (ret < (int)iov[0].iov_len + 1)
770 /* Rollback to avoid cache file corruption */
773 if (ftruncate(fd, st.st_size) < 0)
779 int probe_cache__commit(struct probe_cache *pcache)
781 struct probe_cache_entry *entry;
784 /* TBD: if we do not update existing entries, skip it */
785 ret = lseek(pcache->fd, 0, SEEK_SET);
789 ret = ftruncate(pcache->fd, 0);
793 for_each_probe_cache_entry(entry, pcache) {
794 ret = probe_cache_entry__write(entry, pcache->fd);
795 pr_debug("Cache committed: %d\n", ret);
803 static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
804 struct strfilter *filter)
806 char buf[128], *ptr = entry->spev;
808 if (entry->pev.event) {
809 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
812 return strfilter__compare(filter, ptr);
815 int probe_cache__filter_purge(struct probe_cache *pcache,
816 struct strfilter *filter)
818 struct probe_cache_entry *entry, *tmp;
820 list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
821 if (probe_cache_entry__compare(entry, filter)) {
822 pr_info("Removed cached event: %s\n", entry->spev);
823 list_del_init(&entry->node);
824 probe_cache_entry__delete(entry);
830 static int probe_cache__show_entries(struct probe_cache *pcache,
831 struct strfilter *filter)
833 struct probe_cache_entry *entry;
835 for_each_probe_cache_entry(entry, pcache) {
836 if (probe_cache_entry__compare(entry, filter))
837 printf("%s\n", entry->spev);
842 /* Show all cached probes */
843 int probe_cache__show_all_caches(struct strfilter *filter)
845 struct probe_cache *pcache;
846 struct strlist *bidlist;
848 char *buf = strfilter__string(filter);
850 pr_debug("list cache with filter: %s\n", buf);
853 bidlist = build_id_cache__list_all(true);
855 pr_debug("Failed to get buildids: %d\n", errno);
858 strlist__for_each_entry(nd, bidlist) {
859 pcache = probe_cache__new(nd->s);
862 if (!list_empty(&pcache->entries)) {
863 buf = build_id_cache__origname(nd->s);
864 printf("%s (%s):\n", buf, nd->s);
866 probe_cache__show_entries(pcache, filter);
868 probe_cache__delete(pcache);
870 strlist__delete(bidlist);