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);
147 fp = fdopen(dup(fd), "r");
149 p = fgets(buf, MAX_CMDLEN, fp);
156 ret = strlist__add(sl, buf);
158 pr_debug("strlist__add failed (%d)\n", ret);
168 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
171 struct strlist *sl, *rawlist;
172 struct str_node *ent;
173 struct probe_trace_event tev;
176 memset(&tev, 0, sizeof(tev));
177 rawlist = probe_file__get_rawlist(fd);
180 sl = strlist__new(NULL, NULL);
181 strlist__for_each_entry(ent, rawlist) {
182 ret = parse_probe_trace_command(ent->s, &tev);
186 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
189 ret = strlist__add(sl, buf);
191 ret = strlist__add(sl, tev.event);
192 clear_probe_trace_event(&tev);
196 strlist__delete(rawlist);
205 /* Get current perf-probe event names */
206 struct strlist *probe_file__get_namelist(int fd)
208 return __probe_file__get_namelist(fd, false);
211 int probe_file__add_event(int fd, struct probe_trace_event *tev)
214 char *buf = synthesize_probe_trace_command(tev);
215 char sbuf[STRERR_BUFSIZE];
218 pr_debug("Failed to synthesize probe trace event.\n");
222 pr_debug("Writing event: %s\n", buf);
223 if (!probe_event_dry_run) {
224 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
226 pr_warning("Failed to write event: %s\n",
227 str_error_r(errno, sbuf, sizeof(sbuf)));
235 static int __del_trace_probe_event(int fd, struct str_node *ent)
241 /* Convert from perf-probe event to trace-probe event */
242 ret = e_snprintf(buf, 128, "-:%s", ent->s);
246 p = strchr(buf + 2, ':');
248 pr_debug("Internal error: %s should have ':' but not.\n",
255 pr_debug("Writing event: %s\n", buf);
256 ret = write(fd, buf, strlen(buf));
264 pr_warning("Failed to delete event: %s\n",
265 str_error_r(-ret, buf, sizeof(buf)));
269 int probe_file__get_events(int fd, struct strfilter *filter,
270 struct strlist *plist)
272 struct strlist *namelist;
273 struct str_node *ent;
280 namelist = __probe_file__get_namelist(fd, true);
284 strlist__for_each_entry(ent, namelist) {
285 p = strchr(ent->s, ':');
286 if ((p && strfilter__compare(filter, p + 1)) ||
287 strfilter__compare(filter, ent->s)) {
288 strlist__add(plist, ent->s);
292 strlist__delete(namelist);
297 int probe_file__del_strlist(int fd, struct strlist *namelist)
300 struct str_node *ent;
302 strlist__for_each_entry(ent, namelist) {
303 ret = __del_trace_probe_event(fd, ent);
310 int probe_file__del_events(int fd, struct strfilter *filter)
312 struct strlist *namelist;
315 namelist = strlist__new(NULL, NULL);
319 ret = probe_file__get_events(fd, filter, namelist);
323 ret = probe_file__del_strlist(fd, namelist);
324 strlist__delete(namelist);
329 /* Caller must ensure to remove this entry from list */
330 static void probe_cache_entry__delete(struct probe_cache_entry *entry)
333 BUG_ON(!list_empty(&entry->node));
335 strlist__delete(entry->tevlist);
336 clear_perf_probe_event(&entry->pev);
342 static struct probe_cache_entry *
343 probe_cache_entry__new(struct perf_probe_event *pev)
345 struct probe_cache_entry *entry = zalloc(sizeof(*entry));
348 INIT_LIST_HEAD(&entry->node);
349 entry->tevlist = strlist__new(NULL, NULL);
353 entry->spev = synthesize_perf_probe_command(pev);
355 perf_probe_event__copy(&entry->pev, pev) < 0) {
356 probe_cache_entry__delete(entry);
365 int probe_cache_entry__get_event(struct probe_cache_entry *entry,
366 struct probe_trace_event **tevs)
368 struct probe_trace_event *tev;
369 struct str_node *node;
372 ret = strlist__nr_entries(entry->tevlist);
373 if (ret > probe_conf.max_probes)
376 *tevs = zalloc(ret * sizeof(*tev));
381 strlist__for_each_entry(node, entry->tevlist) {
383 ret = parse_probe_trace_command(node->s, tev);
390 /* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
391 static int probe_cache__open(struct probe_cache *pcache, const char *target)
393 char cpath[PATH_MAX];
394 char sbuildid[SBUILD_ID_SIZE];
395 char *dir_name = NULL;
396 bool is_kallsyms = false;
399 if (target && build_id_cache__cached(target)) {
400 /* This is a cached buildid */
401 strncpy(sbuildid, target, SBUILD_ID_SIZE);
402 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
406 if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
407 target = DSO__NAME_KALLSYMS;
409 ret = sysfs__sprintf_build_id("/", sbuildid);
411 ret = filename__sprintf_build_id(target, sbuildid);
414 pr_debug("Failed to get build-id from %s.\n", target);
418 /* If we have no buildid cache, make it */
419 if (!build_id_cache__cached(sbuildid)) {
420 ret = build_id_cache__add_s(sbuildid, target,
423 pr_debug("Failed to add build-id cache: %s\n", target);
428 dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
432 pr_debug("Failed to get cache from %s\n", target);
436 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
437 fd = open(cpath, O_CREAT | O_RDWR, 0644);
439 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
446 static int probe_cache__load(struct probe_cache *pcache)
448 struct probe_cache_entry *entry = NULL;
449 char buf[MAX_CMDLEN], *p;
453 fp = fdopen(dup(pcache->fd), "r");
458 if (!fgets(buf, MAX_CMDLEN, fp))
460 p = strchr(buf, '\n');
463 /* #perf_probe_event or %sdt_event */
464 if (buf[0] == '#' || buf[0] == '%') {
465 entry = probe_cache_entry__new(NULL);
472 entry->spev = strdup(buf + 1);
474 ret = parse_perf_probe_command(buf + 1,
479 probe_cache_entry__delete(entry);
482 list_add_tail(&entry->node, &pcache->entries);
483 } else { /* trace_probe_event */
488 strlist__add(entry->tevlist, buf);
496 static struct probe_cache *probe_cache__alloc(void)
498 struct probe_cache *pcache = zalloc(sizeof(*pcache));
501 INIT_LIST_HEAD(&pcache->entries);
502 pcache->fd = -EINVAL;
507 void probe_cache__purge(struct probe_cache *pcache)
509 struct probe_cache_entry *entry, *n;
511 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
512 list_del_init(&entry->node);
513 probe_cache_entry__delete(entry);
517 void probe_cache__delete(struct probe_cache *pcache)
522 probe_cache__purge(pcache);
528 struct probe_cache *probe_cache__new(const char *target)
530 struct probe_cache *pcache = probe_cache__alloc();
536 ret = probe_cache__open(pcache, target);
538 pr_debug("Cache open error: %d\n", ret);
542 ret = probe_cache__load(pcache);
544 pr_debug("Cache read error: %d\n", ret);
551 probe_cache__delete(pcache);
555 static bool streql(const char *a, const char *b)
563 return !strcmp(a, b);
566 struct probe_cache_entry *
567 probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
569 struct probe_cache_entry *entry = NULL;
570 char *cmd = synthesize_perf_probe_command(pev);
575 for_each_probe_cache_entry(entry, pcache) {
577 if (entry->pev.event &&
578 streql(entry->pev.event, pev->event) &&
580 streql(entry->pev.group, pev->group)))
585 /* Hit if same event name or same command-string */
587 (streql(entry->pev.group, pev->group) &&
588 streql(entry->pev.event, pev->event))) ||
589 (!strcmp(entry->spev, cmd)))
599 struct probe_cache_entry *
600 probe_cache__find_by_name(struct probe_cache *pcache,
601 const char *group, const char *event)
603 struct probe_cache_entry *entry = NULL;
605 for_each_probe_cache_entry(entry, pcache) {
606 /* Hit if same event name or same command-string */
607 if (streql(entry->pev.group, group) &&
608 streql(entry->pev.event, event))
617 int probe_cache__add_entry(struct probe_cache *pcache,
618 struct perf_probe_event *pev,
619 struct probe_trace_event *tevs, int ntevs)
621 struct probe_cache_entry *entry = NULL;
625 if (!pcache || !pev || !tevs || ntevs <= 0) {
630 /* Remove old cache entry */
631 entry = probe_cache__find(pcache, pev);
633 list_del_init(&entry->node);
634 probe_cache_entry__delete(entry);
638 entry = probe_cache_entry__new(pev);
642 for (i = 0; i < ntevs; i++) {
643 if (!tevs[i].point.symbol)
646 command = synthesize_probe_trace_command(&tevs[i]);
649 strlist__add(entry->tevlist, command);
652 list_add_tail(&entry->node, &pcache->entries);
653 pr_debug("Added probe cache: %d\n", ntevs);
657 pr_debug("Failed to add probe caches\n");
658 probe_cache_entry__delete(entry);
662 #ifdef HAVE_GELF_GETNOTE_SUPPORT
663 static unsigned long long sdt_note__get_addr(struct sdt_note *note)
665 return note->bit32 ? (unsigned long long)note->addr.a32[0]
666 : (unsigned long long)note->addr.a64[0];
669 int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
671 struct probe_cache_entry *entry = NULL;
672 struct list_head sdtlist;
673 struct sdt_note *note;
678 INIT_LIST_HEAD(&sdtlist);
679 ret = get_sdt_note_list(&sdtlist, pathname);
681 pr_debug("Failed to get sdt note: %d\n", ret);
684 list_for_each_entry(note, &sdtlist, note_list) {
685 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
688 /* Try to find same-name entry */
689 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
691 entry = probe_cache_entry__new(NULL);
697 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
698 note->name, note->name);
701 entry->pev.event = strdup(note->name);
702 entry->pev.group = strdup(sdtgrp);
703 list_add_tail(&entry->node, &pcache->entries);
705 ret = asprintf(&buf, "p:%s/%s %s:0x%llx",
706 sdtgrp, note->name, pathname,
707 sdt_note__get_addr(note));
710 strlist__add(entry->tevlist, buf);
715 list_del_init(&entry->node);
716 probe_cache_entry__delete(entry);
718 cleanup_sdt_note_list(&sdtlist);
723 static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
725 struct str_node *snode;
728 const char *prefix = entry->sdt ? "%" : "#";
730 /* Save stat for rollback */
731 ret = fstat(fd, &st);
735 pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
736 iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
737 iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
738 iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
739 ret = writev(fd, iov, 3);
740 if (ret < (int)iov[1].iov_len + 2)
743 strlist__for_each_entry(snode, entry->tevlist) {
744 iov[0].iov_base = (void *)snode->s;
745 iov[0].iov_len = strlen(snode->s);
746 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
747 ret = writev(fd, iov, 2);
748 if (ret < (int)iov[0].iov_len + 1)
754 /* Rollback to avoid cache file corruption */
757 if (ftruncate(fd, st.st_size) < 0)
763 int probe_cache__commit(struct probe_cache *pcache)
765 struct probe_cache_entry *entry;
768 /* TBD: if we do not update existing entries, skip it */
769 ret = lseek(pcache->fd, 0, SEEK_SET);
773 ret = ftruncate(pcache->fd, 0);
777 for_each_probe_cache_entry(entry, pcache) {
778 ret = probe_cache_entry__write(entry, pcache->fd);
779 pr_debug("Cache committed: %d\n", ret);
787 static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
788 struct strfilter *filter)
790 char buf[128], *ptr = entry->spev;
792 if (entry->pev.event) {
793 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
796 return strfilter__compare(filter, ptr);
799 int probe_cache__filter_purge(struct probe_cache *pcache,
800 struct strfilter *filter)
802 struct probe_cache_entry *entry, *tmp;
804 list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
805 if (probe_cache_entry__compare(entry, filter)) {
806 pr_info("Removed cached event: %s\n", entry->spev);
807 list_del_init(&entry->node);
808 probe_cache_entry__delete(entry);
814 static int probe_cache__show_entries(struct probe_cache *pcache,
815 struct strfilter *filter)
817 struct probe_cache_entry *entry;
819 for_each_probe_cache_entry(entry, pcache) {
820 if (probe_cache_entry__compare(entry, filter))
821 printf("%s\n", entry->spev);
826 /* Show all cached probes */
827 int probe_cache__show_all_caches(struct strfilter *filter)
829 struct probe_cache *pcache;
830 struct strlist *bidlist;
832 char *buf = strfilter__string(filter);
834 pr_debug("list cache with filter: %s\n", buf);
837 bidlist = build_id_cache__list_all(true);
839 pr_debug("Failed to get buildids: %d\n", errno);
842 strlist__for_each_entry(nd, bidlist) {
843 pcache = probe_cache__new(nd->s);
846 if (!list_empty(&pcache->entries)) {
847 buf = build_id_cache__origname(nd->s);
848 printf("%s (%s):\n", buf, nd->s);
850 probe_cache__show_entries(pcache, filter);
852 probe_cache__delete(pcache);
854 strlist__delete(bidlist);