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);
153 fp = fdopen(fddup, "r");
155 goto out_close_fddup;
158 p = fgets(buf, MAX_CMDLEN, fp);
165 ret = strlist__add(sl, buf);
167 pr_debug("strlist__add failed (%d)\n", ret);
185 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
188 struct strlist *sl, *rawlist;
189 struct str_node *ent;
190 struct probe_trace_event tev;
193 memset(&tev, 0, sizeof(tev));
194 rawlist = probe_file__get_rawlist(fd);
197 sl = strlist__new(NULL, NULL);
198 strlist__for_each_entry(ent, rawlist) {
199 ret = parse_probe_trace_command(ent->s, &tev);
203 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
206 ret = strlist__add(sl, buf);
208 ret = strlist__add(sl, tev.event);
209 clear_probe_trace_event(&tev);
213 strlist__delete(rawlist);
222 /* Get current perf-probe event names */
223 struct strlist *probe_file__get_namelist(int fd)
225 return __probe_file__get_namelist(fd, false);
228 int probe_file__add_event(int fd, struct probe_trace_event *tev)
231 char *buf = synthesize_probe_trace_command(tev);
232 char sbuf[STRERR_BUFSIZE];
235 pr_debug("Failed to synthesize probe trace event.\n");
239 pr_debug("Writing event: %s\n", buf);
240 if (!probe_event_dry_run) {
241 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
243 pr_warning("Failed to write event: %s\n",
244 str_error_r(errno, sbuf, sizeof(sbuf)));
252 static int __del_trace_probe_event(int fd, struct str_node *ent)
258 /* Convert from perf-probe event to trace-probe event */
259 ret = e_snprintf(buf, 128, "-:%s", ent->s);
263 p = strchr(buf + 2, ':');
265 pr_debug("Internal error: %s should have ':' but not.\n",
272 pr_debug("Writing event: %s\n", buf);
273 ret = write(fd, buf, strlen(buf));
281 pr_warning("Failed to delete event: %s\n",
282 str_error_r(-ret, buf, sizeof(buf)));
286 int probe_file__get_events(int fd, struct strfilter *filter,
287 struct strlist *plist)
289 struct strlist *namelist;
290 struct str_node *ent;
297 namelist = __probe_file__get_namelist(fd, true);
301 strlist__for_each_entry(ent, namelist) {
302 p = strchr(ent->s, ':');
303 if ((p && strfilter__compare(filter, p + 1)) ||
304 strfilter__compare(filter, ent->s)) {
305 strlist__add(plist, ent->s);
309 strlist__delete(namelist);
314 int probe_file__del_strlist(int fd, struct strlist *namelist)
317 struct str_node *ent;
319 strlist__for_each_entry(ent, namelist) {
320 ret = __del_trace_probe_event(fd, ent);
327 int probe_file__del_events(int fd, struct strfilter *filter)
329 struct strlist *namelist;
332 namelist = strlist__new(NULL, NULL);
336 ret = probe_file__get_events(fd, filter, namelist);
340 ret = probe_file__del_strlist(fd, namelist);
341 strlist__delete(namelist);
346 /* Caller must ensure to remove this entry from list */
347 static void probe_cache_entry__delete(struct probe_cache_entry *entry)
350 BUG_ON(!list_empty(&entry->node));
352 strlist__delete(entry->tevlist);
353 clear_perf_probe_event(&entry->pev);
359 static struct probe_cache_entry *
360 probe_cache_entry__new(struct perf_probe_event *pev)
362 struct probe_cache_entry *entry = zalloc(sizeof(*entry));
365 INIT_LIST_HEAD(&entry->node);
366 entry->tevlist = strlist__new(NULL, NULL);
370 entry->spev = synthesize_perf_probe_command(pev);
372 perf_probe_event__copy(&entry->pev, pev) < 0) {
373 probe_cache_entry__delete(entry);
382 int probe_cache_entry__get_event(struct probe_cache_entry *entry,
383 struct probe_trace_event **tevs)
385 struct probe_trace_event *tev;
386 struct str_node *node;
389 ret = strlist__nr_entries(entry->tevlist);
390 if (ret > probe_conf.max_probes)
393 *tevs = zalloc(ret * sizeof(*tev));
398 strlist__for_each_entry(node, entry->tevlist) {
400 ret = parse_probe_trace_command(node->s, tev);
407 /* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
408 static int probe_cache__open(struct probe_cache *pcache, const char *target)
410 char cpath[PATH_MAX];
411 char sbuildid[SBUILD_ID_SIZE];
412 char *dir_name = NULL;
413 bool is_kallsyms = false;
416 if (target && build_id_cache__cached(target)) {
417 /* This is a cached buildid */
418 strncpy(sbuildid, target, SBUILD_ID_SIZE);
419 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
423 if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
424 target = DSO__NAME_KALLSYMS;
426 ret = sysfs__sprintf_build_id("/", sbuildid);
428 ret = filename__sprintf_build_id(target, sbuildid);
431 pr_debug("Failed to get build-id from %s.\n", target);
435 /* If we have no buildid cache, make it */
436 if (!build_id_cache__cached(sbuildid)) {
437 ret = build_id_cache__add_s(sbuildid, target,
440 pr_debug("Failed to add build-id cache: %s\n", target);
445 dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
449 pr_debug("Failed to get cache from %s\n", target);
453 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
454 fd = open(cpath, O_CREAT | O_RDWR, 0644);
456 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
463 static int probe_cache__load(struct probe_cache *pcache)
465 struct probe_cache_entry *entry = NULL;
466 char buf[MAX_CMDLEN], *p;
470 fddup = dup(pcache->fd);
473 fp = fdopen(fddup, "r");
480 if (!fgets(buf, MAX_CMDLEN, fp))
482 p = strchr(buf, '\n');
485 /* #perf_probe_event or %sdt_event */
486 if (buf[0] == '#' || buf[0] == '%') {
487 entry = probe_cache_entry__new(NULL);
494 entry->spev = strdup(buf + 1);
496 ret = parse_perf_probe_command(buf + 1,
501 probe_cache_entry__delete(entry);
504 list_add_tail(&entry->node, &pcache->entries);
505 } else { /* trace_probe_event */
510 strlist__add(entry->tevlist, buf);
518 static struct probe_cache *probe_cache__alloc(void)
520 struct probe_cache *pcache = zalloc(sizeof(*pcache));
523 INIT_LIST_HEAD(&pcache->entries);
524 pcache->fd = -EINVAL;
529 void probe_cache__purge(struct probe_cache *pcache)
531 struct probe_cache_entry *entry, *n;
533 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
534 list_del_init(&entry->node);
535 probe_cache_entry__delete(entry);
539 void probe_cache__delete(struct probe_cache *pcache)
544 probe_cache__purge(pcache);
550 struct probe_cache *probe_cache__new(const char *target)
552 struct probe_cache *pcache = probe_cache__alloc();
558 ret = probe_cache__open(pcache, target);
560 pr_debug("Cache open error: %d\n", ret);
564 ret = probe_cache__load(pcache);
566 pr_debug("Cache read error: %d\n", ret);
573 probe_cache__delete(pcache);
577 static bool streql(const char *a, const char *b)
585 return !strcmp(a, b);
588 struct probe_cache_entry *
589 probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
591 struct probe_cache_entry *entry = NULL;
592 char *cmd = synthesize_perf_probe_command(pev);
597 for_each_probe_cache_entry(entry, pcache) {
599 if (entry->pev.event &&
600 streql(entry->pev.event, pev->event) &&
602 streql(entry->pev.group, pev->group)))
607 /* Hit if same event name or same command-string */
609 (streql(entry->pev.group, pev->group) &&
610 streql(entry->pev.event, pev->event))) ||
611 (!strcmp(entry->spev, cmd)))
621 struct probe_cache_entry *
622 probe_cache__find_by_name(struct probe_cache *pcache,
623 const char *group, const char *event)
625 struct probe_cache_entry *entry = NULL;
627 for_each_probe_cache_entry(entry, pcache) {
628 /* Hit if same event name or same command-string */
629 if (streql(entry->pev.group, group) &&
630 streql(entry->pev.event, event))
639 int probe_cache__add_entry(struct probe_cache *pcache,
640 struct perf_probe_event *pev,
641 struct probe_trace_event *tevs, int ntevs)
643 struct probe_cache_entry *entry = NULL;
647 if (!pcache || !pev || !tevs || ntevs <= 0) {
652 /* Remove old cache entry */
653 entry = probe_cache__find(pcache, pev);
655 list_del_init(&entry->node);
656 probe_cache_entry__delete(entry);
660 entry = probe_cache_entry__new(pev);
664 for (i = 0; i < ntevs; i++) {
665 if (!tevs[i].point.symbol)
668 command = synthesize_probe_trace_command(&tevs[i]);
671 strlist__add(entry->tevlist, command);
674 list_add_tail(&entry->node, &pcache->entries);
675 pr_debug("Added probe cache: %d\n", ntevs);
679 pr_debug("Failed to add probe caches\n");
680 probe_cache_entry__delete(entry);
684 #ifdef HAVE_GELF_GETNOTE_SUPPORT
685 static unsigned long long sdt_note__get_addr(struct sdt_note *note)
687 return note->bit32 ? (unsigned long long)note->addr.a32[0]
688 : (unsigned long long)note->addr.a64[0];
691 int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
693 struct probe_cache_entry *entry = NULL;
694 struct list_head sdtlist;
695 struct sdt_note *note;
700 INIT_LIST_HEAD(&sdtlist);
701 ret = get_sdt_note_list(&sdtlist, pathname);
703 pr_debug("Failed to get sdt note: %d\n", ret);
706 list_for_each_entry(note, &sdtlist, note_list) {
707 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
710 /* Try to find same-name entry */
711 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
713 entry = probe_cache_entry__new(NULL);
719 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
720 note->name, note->name);
723 entry->pev.event = strdup(note->name);
724 entry->pev.group = strdup(sdtgrp);
725 list_add_tail(&entry->node, &pcache->entries);
727 ret = asprintf(&buf, "p:%s/%s %s:0x%llx",
728 sdtgrp, note->name, pathname,
729 sdt_note__get_addr(note));
732 strlist__add(entry->tevlist, buf);
737 list_del_init(&entry->node);
738 probe_cache_entry__delete(entry);
740 cleanup_sdt_note_list(&sdtlist);
745 static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
747 struct str_node *snode;
750 const char *prefix = entry->sdt ? "%" : "#";
752 /* Save stat for rollback */
753 ret = fstat(fd, &st);
757 pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
758 iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
759 iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
760 iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
761 ret = writev(fd, iov, 3);
762 if (ret < (int)iov[1].iov_len + 2)
765 strlist__for_each_entry(snode, entry->tevlist) {
766 iov[0].iov_base = (void *)snode->s;
767 iov[0].iov_len = strlen(snode->s);
768 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
769 ret = writev(fd, iov, 2);
770 if (ret < (int)iov[0].iov_len + 1)
776 /* Rollback to avoid cache file corruption */
779 if (ftruncate(fd, st.st_size) < 0)
785 int probe_cache__commit(struct probe_cache *pcache)
787 struct probe_cache_entry *entry;
790 /* TBD: if we do not update existing entries, skip it */
791 ret = lseek(pcache->fd, 0, SEEK_SET);
795 ret = ftruncate(pcache->fd, 0);
799 for_each_probe_cache_entry(entry, pcache) {
800 ret = probe_cache_entry__write(entry, pcache->fd);
801 pr_debug("Cache committed: %d\n", ret);
809 static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
810 struct strfilter *filter)
812 char buf[128], *ptr = entry->spev;
814 if (entry->pev.event) {
815 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
818 return strfilter__compare(filter, ptr);
821 int probe_cache__filter_purge(struct probe_cache *pcache,
822 struct strfilter *filter)
824 struct probe_cache_entry *entry, *tmp;
826 list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
827 if (probe_cache_entry__compare(entry, filter)) {
828 pr_info("Removed cached event: %s\n", entry->spev);
829 list_del_init(&entry->node);
830 probe_cache_entry__delete(entry);
836 static int probe_cache__show_entries(struct probe_cache *pcache,
837 struct strfilter *filter)
839 struct probe_cache_entry *entry;
841 for_each_probe_cache_entry(entry, pcache) {
842 if (probe_cache_entry__compare(entry, filter))
843 printf("%s\n", entry->spev);
848 /* Show all cached probes */
849 int probe_cache__show_all_caches(struct strfilter *filter)
851 struct probe_cache *pcache;
852 struct strlist *bidlist;
854 char *buf = strfilter__string(filter);
856 pr_debug("list cache with filter: %s\n", buf);
859 bidlist = build_id_cache__list_all(true);
861 pr_debug("Failed to get buildids: %d\n", errno);
864 strlist__for_each_entry(nd, bidlist) {
865 pcache = probe_cache__new(nd->s);
868 if (!list_empty(&pcache->entries)) {
869 buf = build_id_cache__origname(nd->s);
870 printf("%s (%s):\n", buf, nd->s);
872 probe_cache__show_entries(pcache, filter);
874 probe_cache__delete(pcache);
876 strlist__delete(bidlist);