1 // SPDX-License-Identifier: GPL-2.0
5 #include <util/evlist.h>
6 #include <linux/filter.h>
9 #include "probe-file.h"
12 /* To test SDT event, we need libelf support to scan elf binary */
13 #if defined(HAVE_SDT_EVENT) && defined(HAVE_LIBELF_SUPPORT)
17 static int target_function(void)
19 DTRACE_PROBE(perf, test_target);
23 /* Copied from builtin-buildid-cache.c */
24 static int build_id_cache__add_file(const char *filename)
26 char sbuild_id[SBUILD_ID_SIZE];
27 u8 build_id[BUILD_ID_SIZE];
30 err = filename__read_build_id(filename, &build_id, sizeof(build_id));
32 pr_debug("Failed to read build id of %s\n", filename);
36 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
37 err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false);
39 pr_debug("Failed to add build id cache of %s\n", filename);
43 static char *get_self_path(void)
45 char *buf = calloc(PATH_MAX, sizeof(char));
47 if (buf && readlink("/proc/self/exe", buf, PATH_MAX - 1) < 0) {
48 pr_debug("Failed to get correct path of perf\n");
55 static int search_cached_probe(const char *target,
56 const char *group, const char *event)
58 struct probe_cache *cache = probe_cache__new(target, NULL);
62 pr_debug("Failed to open probe cache of %s\n", target);
66 if (!probe_cache__find_by_name(cache, group, event)) {
67 pr_debug("Failed to find %s:%s in the cache\n", group, event);
70 probe_cache__delete(cache);
75 int test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused)
78 char __tempdir[] = "./test-buildid-XXXXXX";
79 char *tempdir = NULL, *myself = get_self_path();
81 if (myself == NULL || mkdtemp(__tempdir) == NULL) {
82 pr_debug("Failed to make a tempdir for build-id cache\n");
85 /* Note that buildid_dir must be an absolute path */
86 tempdir = realpath(__tempdir, NULL);
90 /* At first, scan itself */
91 set_buildid_dir(tempdir);
92 if (build_id_cache__add_file(myself) < 0)
95 /* Open a cache and make sure the SDT is stored */
96 if (search_cached_probe(myself, "sdt_perf", "test_target") < 0)
99 /* TBD: probing on the SDT event and collect logs */
101 /* Call the target and get an event */
102 ret = target_function();
105 /* Cleanup temporary buildid dir */
113 int test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused)
115 pr_debug("Skip SDT event test because SDT support is not compiled\n");