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 "namespaces.h"
17 #include "strfilter.h"
23 #include <api/fs/tracing_path.h>
24 #include "probe-event.h"
25 #include "probe-file.h"
27 #include "perf_regs.h"
30 /* 4096 - 2 ('\n' + '\0') */
31 #define MAX_CMDLEN 4094
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 int open_trace_file(const char *trace_file
, bool readwrite
)
78 ret
= e_snprintf(buf
, PATH_MAX
, "%s/%s", tracing_path_mount(), trace_file
);
80 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
81 if (readwrite
&& !probe_event_dry_run
)
82 ret
= open(buf
, O_RDWR
| O_APPEND
, 0);
84 ret
= open(buf
, O_RDONLY
, 0);
92 static int open_kprobe_events(bool readwrite
)
94 return open_trace_file("kprobe_events", readwrite
);
97 static int open_uprobe_events(bool readwrite
)
99 return open_trace_file("uprobe_events", readwrite
);
102 int probe_file__open(int flag
)
106 if (flag
& PF_FL_UPROBE
)
107 fd
= open_uprobe_events(flag
& PF_FL_RW
);
109 fd
= open_kprobe_events(flag
& PF_FL_RW
);
111 print_open_warning(fd
, flag
& PF_FL_UPROBE
);
116 int probe_file__open_both(int *kfd
, int *ufd
, int flag
)
121 *kfd
= open_kprobe_events(flag
& PF_FL_RW
);
122 *ufd
= open_uprobe_events(flag
& PF_FL_RW
);
123 if (*kfd
< 0 && *ufd
< 0) {
124 print_both_open_warning(*kfd
, *ufd
);
131 /* Get raw string list of current kprobe_events or uprobe_events */
132 struct strlist
*probe_file__get_rawlist(int fd
)
136 char buf
[MAX_CMDLEN
];
143 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
);
183 static struct strlist
*__probe_file__get_namelist(int fd
, bool include_group
)
186 struct strlist
*sl
, *rawlist
;
187 struct str_node
*ent
;
188 struct probe_trace_event tev
;
191 memset(&tev
, 0, sizeof(tev
));
192 rawlist
= probe_file__get_rawlist(fd
);
195 sl
= strlist__new(NULL
, NULL
);
196 strlist__for_each_entry(ent
, rawlist
) {
197 ret
= parse_probe_trace_command(ent
->s
, &tev
);
201 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
204 ret
= strlist__add(sl
, buf
);
206 ret
= strlist__add(sl
, tev
.event
);
207 clear_probe_trace_event(&tev
);
211 strlist__delete(rawlist
);
220 /* Get current perf-probe event names */
221 struct strlist
*probe_file__get_namelist(int fd
)
223 return __probe_file__get_namelist(fd
, false);
226 int probe_file__add_event(int fd
, struct probe_trace_event
*tev
)
229 char *buf
= synthesize_probe_trace_command(tev
);
230 char sbuf
[STRERR_BUFSIZE
];
233 pr_debug("Failed to synthesize probe trace event.\n");
237 pr_debug("Writing event: %s\n", buf
);
238 if (!probe_event_dry_run
) {
239 if (write(fd
, buf
, strlen(buf
)) < (int)strlen(buf
)) {
241 pr_warning("Failed to write event: %s\n",
242 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
250 static int __del_trace_probe_event(int fd
, struct str_node
*ent
)
256 /* Convert from perf-probe event to trace-probe event */
257 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
261 p
= strchr(buf
+ 2, ':');
263 pr_debug("Internal error: %s should have ':' but not.\n",
270 pr_debug("Writing event: %s\n", buf
);
271 ret
= write(fd
, buf
, strlen(buf
));
279 pr_warning("Failed to delete event: %s\n",
280 str_error_r(-ret
, buf
, sizeof(buf
)));
284 int probe_file__get_events(int fd
, struct strfilter
*filter
,
285 struct strlist
*plist
)
287 struct strlist
*namelist
;
288 struct str_node
*ent
;
295 namelist
= __probe_file__get_namelist(fd
, true);
299 strlist__for_each_entry(ent
, namelist
) {
300 p
= strchr(ent
->s
, ':');
301 if ((p
&& strfilter__compare(filter
, p
+ 1)) ||
302 strfilter__compare(filter
, ent
->s
)) {
303 strlist__add(plist
, ent
->s
);
307 strlist__delete(namelist
);
312 int probe_file__del_strlist(int fd
, struct strlist
*namelist
)
315 struct str_node
*ent
;
317 strlist__for_each_entry(ent
, namelist
) {
318 ret
= __del_trace_probe_event(fd
, ent
);
325 int probe_file__del_events(int fd
, struct strfilter
*filter
)
327 struct strlist
*namelist
;
330 namelist
= strlist__new(NULL
, NULL
);
334 ret
= probe_file__get_events(fd
, filter
, namelist
);
338 ret
= probe_file__del_strlist(fd
, namelist
);
339 strlist__delete(namelist
);
344 /* Caller must ensure to remove this entry from list */
345 static void probe_cache_entry__delete(struct probe_cache_entry
*entry
)
348 BUG_ON(!list_empty(&entry
->node
));
350 strlist__delete(entry
->tevlist
);
351 clear_perf_probe_event(&entry
->pev
);
357 static struct probe_cache_entry
*
358 probe_cache_entry__new(struct perf_probe_event
*pev
)
360 struct probe_cache_entry
*entry
= zalloc(sizeof(*entry
));
363 INIT_LIST_HEAD(&entry
->node
);
364 entry
->tevlist
= strlist__new(NULL
, NULL
);
368 entry
->spev
= synthesize_perf_probe_command(pev
);
370 perf_probe_event__copy(&entry
->pev
, pev
) < 0) {
371 probe_cache_entry__delete(entry
);
380 int probe_cache_entry__get_event(struct probe_cache_entry
*entry
,
381 struct probe_trace_event
**tevs
)
383 struct probe_trace_event
*tev
;
384 struct str_node
*node
;
387 ret
= strlist__nr_entries(entry
->tevlist
);
388 if (ret
> probe_conf
.max_probes
)
391 *tevs
= zalloc(ret
* sizeof(*tev
));
396 strlist__for_each_entry(node
, entry
->tevlist
) {
398 ret
= parse_probe_trace_command(node
->s
, tev
);
405 /* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
406 static int probe_cache__open(struct probe_cache
*pcache
, const char *target
,
409 char cpath
[PATH_MAX
];
410 char sbuildid
[SBUILD_ID_SIZE
];
411 char *dir_name
= NULL
;
412 bool is_kallsyms
= false;
416 if (target
&& build_id_cache__cached(target
)) {
417 /* This is a cached buildid */
418 strlcpy(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 nsinfo__mountns_enter(nsi
, &nsc
);
429 ret
= filename__sprintf_build_id(target
, sbuildid
);
430 nsinfo__mountns_exit(&nsc
);
434 pr_debug("Failed to get build-id from %s.\n", target
);
438 /* If we have no buildid cache, make it */
439 if (!build_id_cache__cached(sbuildid
)) {
440 ret
= build_id_cache__add_s(sbuildid
, target
, nsi
,
443 pr_debug("Failed to add build-id cache: %s\n", target
);
448 dir_name
= build_id_cache__cachedir(sbuildid
, target
, nsi
, is_kallsyms
,
452 pr_debug("Failed to get cache from %s\n", target
);
456 snprintf(cpath
, PATH_MAX
, "%s/probes", dir_name
);
457 fd
= open(cpath
, O_CREAT
| O_RDWR
, 0644);
459 pr_debug("Failed to open cache(%d): %s\n", fd
, cpath
);
466 static int probe_cache__load(struct probe_cache
*pcache
)
468 struct probe_cache_entry
*entry
= NULL
;
469 char buf
[MAX_CMDLEN
], *p
;
473 fddup
= dup(pcache
->fd
);
476 fp
= fdopen(fddup
, "r");
483 if (!fgets(buf
, MAX_CMDLEN
, fp
))
485 p
= strchr(buf
, '\n');
488 /* #perf_probe_event or %sdt_event */
489 if (buf
[0] == '#' || buf
[0] == '%') {
490 entry
= probe_cache_entry__new(NULL
);
497 entry
->spev
= strdup(buf
+ 1);
499 ret
= parse_perf_probe_command(buf
+ 1,
504 probe_cache_entry__delete(entry
);
507 list_add_tail(&entry
->node
, &pcache
->entries
);
508 } else { /* trace_probe_event */
513 strlist__add(entry
->tevlist
, buf
);
521 static struct probe_cache
*probe_cache__alloc(void)
523 struct probe_cache
*pcache
= zalloc(sizeof(*pcache
));
526 INIT_LIST_HEAD(&pcache
->entries
);
527 pcache
->fd
= -EINVAL
;
532 void probe_cache__purge(struct probe_cache
*pcache
)
534 struct probe_cache_entry
*entry
, *n
;
536 list_for_each_entry_safe(entry
, n
, &pcache
->entries
, node
) {
537 list_del_init(&entry
->node
);
538 probe_cache_entry__delete(entry
);
542 void probe_cache__delete(struct probe_cache
*pcache
)
547 probe_cache__purge(pcache
);
553 struct probe_cache
*probe_cache__new(const char *target
, struct nsinfo
*nsi
)
555 struct probe_cache
*pcache
= probe_cache__alloc();
561 ret
= probe_cache__open(pcache
, target
, nsi
);
563 pr_debug("Cache open error: %d\n", ret
);
567 ret
= probe_cache__load(pcache
);
569 pr_debug("Cache read error: %d\n", ret
);
576 probe_cache__delete(pcache
);
580 static bool streql(const char *a
, const char *b
)
588 return !strcmp(a
, b
);
591 struct probe_cache_entry
*
592 probe_cache__find(struct probe_cache
*pcache
, struct perf_probe_event
*pev
)
594 struct probe_cache_entry
*entry
= NULL
;
595 char *cmd
= synthesize_perf_probe_command(pev
);
600 for_each_probe_cache_entry(entry
, pcache
) {
602 if (entry
->pev
.event
&&
603 streql(entry
->pev
.event
, pev
->event
) &&
605 streql(entry
->pev
.group
, pev
->group
)))
610 /* Hit if same event name or same command-string */
612 (streql(entry
->pev
.group
, pev
->group
) &&
613 streql(entry
->pev
.event
, pev
->event
))) ||
614 (!strcmp(entry
->spev
, cmd
)))
624 struct probe_cache_entry
*
625 probe_cache__find_by_name(struct probe_cache
*pcache
,
626 const char *group
, const char *event
)
628 struct probe_cache_entry
*entry
= NULL
;
630 for_each_probe_cache_entry(entry
, pcache
) {
631 /* Hit if same event name or same command-string */
632 if (streql(entry
->pev
.group
, group
) &&
633 streql(entry
->pev
.event
, event
))
642 int probe_cache__add_entry(struct probe_cache
*pcache
,
643 struct perf_probe_event
*pev
,
644 struct probe_trace_event
*tevs
, int ntevs
)
646 struct probe_cache_entry
*entry
= NULL
;
650 if (!pcache
|| !pev
|| !tevs
|| ntevs
<= 0) {
655 /* Remove old cache entry */
656 entry
= probe_cache__find(pcache
, pev
);
658 list_del_init(&entry
->node
);
659 probe_cache_entry__delete(entry
);
663 entry
= probe_cache_entry__new(pev
);
667 for (i
= 0; i
< ntevs
; i
++) {
668 if (!tevs
[i
].point
.symbol
)
671 command
= synthesize_probe_trace_command(&tevs
[i
]);
674 strlist__add(entry
->tevlist
, command
);
677 list_add_tail(&entry
->node
, &pcache
->entries
);
678 pr_debug("Added probe cache: %d\n", ntevs
);
682 pr_debug("Failed to add probe caches\n");
683 probe_cache_entry__delete(entry
);
687 #ifdef HAVE_GELF_GETNOTE_SUPPORT
688 static unsigned long long sdt_note__get_addr(struct sdt_note
*note
)
691 (unsigned long long)note
->addr
.a32
[SDT_NOTE_IDX_LOC
] :
692 (unsigned long long)note
->addr
.a64
[SDT_NOTE_IDX_LOC
];
695 static unsigned long long sdt_note__get_ref_ctr_offset(struct sdt_note
*note
)
698 (unsigned long long)note
->addr
.a32
[SDT_NOTE_IDX_REFCTR
] :
699 (unsigned long long)note
->addr
.a64
[SDT_NOTE_IDX_REFCTR
];
702 static const char * const type_to_suffix
[] = {
703 ":s64", "", "", "", ":s32", "", ":s16", ":s8",
704 "", ":u8", ":u16", "", ":u32", "", "", "", ":u64"
708 * Isolate the string number and convert it into a decimal value;
709 * this will be an index to get suffix of the uprobe name (defining
712 static int sdt_arg_parse_size(char *n_ptr
, const char **suffix
)
716 type_idx
= strtol(n_ptr
, NULL
, 10);
717 if (type_idx
< -8 || type_idx
> 8) {
718 pr_debug4("Failed to get a valid sdt type\n");
722 *suffix
= type_to_suffix
[type_idx
+ 8];
726 static int synthesize_sdt_probe_arg(struct strbuf
*buf
, int i
, const char *arg
)
728 char *op
, *desc
= strdup(arg
), *new_op
= NULL
;
729 const char *suffix
= "";
733 pr_debug4("Allocation error\n");
738 * Argument is in N@OP format. N is size of the argument and OP is
739 * the actual assembly operand. N can be omitted; in that case
740 * argument is just OP(without @).
742 op
= strchr(desc
, '@');
747 if (sdt_arg_parse_size(desc
, &suffix
))
753 ret
= arch_sdt_arg_parse_op(op
, &new_op
);
758 if (ret
== SDT_ARG_VALID
) {
759 ret
= strbuf_addf(buf
, " arg%d=%s%s", i
+ 1, new_op
, suffix
);
771 static char *synthesize_sdt_probe_command(struct sdt_note
*note
,
772 const char *pathname
,
776 char *ret
= NULL
, **args
;
777 int i
, args_count
, err
;
778 unsigned long long ref_ctr_offset
;
780 if (strbuf_init(&buf
, 32) < 0)
783 err
= strbuf_addf(&buf
, "p:%s/%s %s:0x%llx",
784 sdtgrp
, note
->name
, pathname
,
785 sdt_note__get_addr(note
));
787 ref_ctr_offset
= sdt_note__get_ref_ctr_offset(note
);
788 if (ref_ctr_offset
&& err
>= 0)
789 err
= strbuf_addf(&buf
, "(0x%llx)", ref_ctr_offset
);
798 args
= argv_split(note
->args
, &args_count
);
800 for (i
= 0; i
< args_count
; ++i
) {
801 if (synthesize_sdt_probe_arg(&buf
, i
, args
[i
]) < 0)
807 ret
= strbuf_detach(&buf
, NULL
);
809 strbuf_release(&buf
);
813 int probe_cache__scan_sdt(struct probe_cache
*pcache
, const char *pathname
)
815 struct probe_cache_entry
*entry
= NULL
;
816 struct list_head sdtlist
;
817 struct sdt_note
*note
;
822 INIT_LIST_HEAD(&sdtlist
);
823 ret
= get_sdt_note_list(&sdtlist
, pathname
);
825 pr_debug4("Failed to get sdt note: %d\n", ret
);
828 list_for_each_entry(note
, &sdtlist
, note_list
) {
829 ret
= snprintf(sdtgrp
, 64, "sdt_%s", note
->provider
);
832 /* Try to find same-name entry */
833 entry
= probe_cache__find_by_name(pcache
, sdtgrp
, note
->name
);
835 entry
= probe_cache_entry__new(NULL
);
841 ret
= asprintf(&entry
->spev
, "%s:%s=%s", sdtgrp
,
842 note
->name
, note
->name
);
845 entry
->pev
.event
= strdup(note
->name
);
846 entry
->pev
.group
= strdup(sdtgrp
);
847 list_add_tail(&entry
->node
, &pcache
->entries
);
849 buf
= synthesize_sdt_probe_command(note
, pathname
, sdtgrp
);
855 strlist__add(entry
->tevlist
, buf
);
860 list_del_init(&entry
->node
);
861 probe_cache_entry__delete(entry
);
863 cleanup_sdt_note_list(&sdtlist
);
868 static int probe_cache_entry__write(struct probe_cache_entry
*entry
, int fd
)
870 struct str_node
*snode
;
873 const char *prefix
= entry
->sdt
? "%" : "#";
875 /* Save stat for rollback */
876 ret
= fstat(fd
, &st
);
880 pr_debug("Writing cache: %s%s\n", prefix
, entry
->spev
);
881 iov
[0].iov_base
= (void *)prefix
; iov
[0].iov_len
= 1;
882 iov
[1].iov_base
= entry
->spev
; iov
[1].iov_len
= strlen(entry
->spev
);
883 iov
[2].iov_base
= (void *)"\n"; iov
[2].iov_len
= 1;
884 ret
= writev(fd
, iov
, 3);
885 if (ret
< (int)iov
[1].iov_len
+ 2)
888 strlist__for_each_entry(snode
, entry
->tevlist
) {
889 iov
[0].iov_base
= (void *)snode
->s
;
890 iov
[0].iov_len
= strlen(snode
->s
);
891 iov
[1].iov_base
= (void *)"\n"; iov
[1].iov_len
= 1;
892 ret
= writev(fd
, iov
, 2);
893 if (ret
< (int)iov
[0].iov_len
+ 1)
899 /* Rollback to avoid cache file corruption */
902 if (ftruncate(fd
, st
.st_size
) < 0)
908 int probe_cache__commit(struct probe_cache
*pcache
)
910 struct probe_cache_entry
*entry
;
913 /* TBD: if we do not update existing entries, skip it */
914 ret
= lseek(pcache
->fd
, 0, SEEK_SET
);
918 ret
= ftruncate(pcache
->fd
, 0);
922 for_each_probe_cache_entry(entry
, pcache
) {
923 ret
= probe_cache_entry__write(entry
, pcache
->fd
);
924 pr_debug("Cache committed: %d\n", ret
);
932 static bool probe_cache_entry__compare(struct probe_cache_entry
*entry
,
933 struct strfilter
*filter
)
935 char buf
[128], *ptr
= entry
->spev
;
937 if (entry
->pev
.event
) {
938 snprintf(buf
, 128, "%s:%s", entry
->pev
.group
, entry
->pev
.event
);
941 return strfilter__compare(filter
, ptr
);
944 int probe_cache__filter_purge(struct probe_cache
*pcache
,
945 struct strfilter
*filter
)
947 struct probe_cache_entry
*entry
, *tmp
;
949 list_for_each_entry_safe(entry
, tmp
, &pcache
->entries
, node
) {
950 if (probe_cache_entry__compare(entry
, filter
)) {
951 pr_info("Removed cached event: %s\n", entry
->spev
);
952 list_del_init(&entry
->node
);
953 probe_cache_entry__delete(entry
);
959 static int probe_cache__show_entries(struct probe_cache
*pcache
,
960 struct strfilter
*filter
)
962 struct probe_cache_entry
*entry
;
964 for_each_probe_cache_entry(entry
, pcache
) {
965 if (probe_cache_entry__compare(entry
, filter
))
966 printf("%s\n", entry
->spev
);
971 /* Show all cached probes */
972 int probe_cache__show_all_caches(struct strfilter
*filter
)
974 struct probe_cache
*pcache
;
975 struct strlist
*bidlist
;
977 char *buf
= strfilter__string(filter
);
979 pr_debug("list cache with filter: %s\n", buf
);
982 bidlist
= build_id_cache__list_all(true);
984 pr_debug("Failed to get buildids: %d\n", errno
);
987 strlist__for_each_entry(nd
, bidlist
) {
988 pcache
= probe_cache__new(nd
->s
, NULL
);
991 if (!list_empty(&pcache
->entries
)) {
992 buf
= build_id_cache__origname(nd
->s
);
993 printf("%s (%s):\n", buf
, nd
->s
);
995 probe_cache__show_entries(pcache
, filter
);
997 probe_cache__delete(pcache
);
999 strlist__delete(bidlist
);
1004 enum ftrace_readme
{
1005 FTRACE_README_PROBE_TYPE_X
= 0,
1006 FTRACE_README_KRETPROBE_OFFSET
,
1007 FTRACE_README_UPROBE_REF_CTR
,
1012 const char *pattern
;
1014 } ftrace_readme_table
[] = {
1015 #define DEFINE_TYPE(idx, pat) \
1016 [idx] = {.pattern = pat, .avail = false}
1017 DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X
, "*type: * x8/16/32/64,*"),
1018 DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET
, "*place (kretprobe): *"),
1019 DEFINE_TYPE(FTRACE_README_UPROBE_REF_CTR
, "*ref_ctr_offset*"),
1022 static bool scan_ftrace_readme(enum ftrace_readme type
)
1029 static bool scanned
= false;
1034 fd
= open_trace_file("README", false);
1038 fp
= fdopen(fd
, "r");
1044 while (getline(&buf
, &len
, fp
) > 0)
1045 for (enum ftrace_readme i
= 0; i
< FTRACE_README_END
; i
++)
1046 if (!ftrace_readme_table
[i
].avail
)
1047 ftrace_readme_table
[i
].avail
=
1048 strglobmatch(buf
, ftrace_readme_table
[i
].pattern
);
1055 if (type
>= FTRACE_README_END
)
1058 return ftrace_readme_table
[type
].avail
;
1061 bool probe_type_is_available(enum probe_type type
)
1063 if (type
>= PROBE_TYPE_END
)
1065 else if (type
== PROBE_TYPE_X
)
1066 return scan_ftrace_readme(FTRACE_README_PROBE_TYPE_X
);
1071 bool kretprobe_offset_is_supported(void)
1073 return scan_ftrace_readme(FTRACE_README_KRETPROBE_OFFSET
);
1076 bool uprobe_ref_ctr_is_supported(void)
1078 return scan_ftrace_readme(FTRACE_README_UPROBE_REF_CTR
);