1 // SPDX-License-Identifier: GPL-2.0
8 #include <util/symbol.h>
9 #include <linux/filter.h>
12 #include "probe-file.h"
16 /* To test SDT event, we need libelf support to scan elf binary */
17 #if defined(HAVE_SDT_EVENT) && defined(HAVE_LIBELF_SUPPORT)
21 static int target_function(void)
23 DTRACE_PROBE(perf, test_target);
27 /* Copied from builtin-buildid-cache.c */
28 static int build_id_cache__add_file(const char *filename)
30 char sbuild_id[SBUILD_ID_SIZE];
31 u8 build_id[BUILD_ID_SIZE];
34 err = filename__read_build_id(filename, &build_id, sizeof(build_id));
36 pr_debug("Failed to read build id of %s\n", filename);
40 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
41 err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false);
43 pr_debug("Failed to add build id cache of %s\n", filename);
47 static char *get_self_path(void)
49 char *buf = calloc(PATH_MAX, sizeof(char));
51 if (buf && readlink("/proc/self/exe", buf, PATH_MAX - 1) < 0) {
52 pr_debug("Failed to get correct path of perf\n");
59 static int search_cached_probe(const char *target,
60 const char *group, const char *event)
62 struct probe_cache *cache = probe_cache__new(target, NULL);
66 pr_debug("Failed to open probe cache of %s\n", target);
70 if (!probe_cache__find_by_name(cache, group, event)) {
71 pr_debug("Failed to find %s:%s in the cache\n", group, event);
74 probe_cache__delete(cache);
79 int test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused)
82 char __tempdir[] = "./test-buildid-XXXXXX";
83 char *tempdir = NULL, *myself = get_self_path();
85 if (myself == NULL || mkdtemp(__tempdir) == NULL) {
86 pr_debug("Failed to make a tempdir for build-id cache\n");
89 /* Note that buildid_dir must be an absolute path */
90 tempdir = realpath(__tempdir, NULL);
94 /* At first, scan itself */
95 set_buildid_dir(tempdir);
96 if (build_id_cache__add_file(myself) < 0)
99 /* Open a cache and make sure the SDT is stored */
100 if (search_cached_probe(myself, "sdt_perf", "test_target") < 0)
103 /* TBD: probing on the SDT event and collect logs */
105 /* Call the target and get an event */
106 ret = target_function();
109 /* Cleanup temporary buildid dir */
117 int test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused)
119 pr_debug("Skip SDT event test because SDT support is not compiled\n");