1 // SPDX-License-Identifier: GPL-2.0
5 #include <util/evlist.h>
6 #include <util/symbol.h>
7 #include <linux/filter.h>
10 #include "probe-file.h"
13 /* To test SDT event, we need libelf support to scan elf binary */
14 #if defined(HAVE_SDT_EVENT) && defined(HAVE_LIBELF_SUPPORT)
18 static int target_function(void)
20 DTRACE_PROBE(perf
, test_target
);
24 /* Copied from builtin-buildid-cache.c */
25 static int build_id_cache__add_file(const char *filename
)
27 char sbuild_id
[SBUILD_ID_SIZE
];
28 u8 build_id
[BUILD_ID_SIZE
];
31 err
= filename__read_build_id(filename
, &build_id
, sizeof(build_id
));
33 pr_debug("Failed to read build id of %s\n", filename
);
37 build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
38 err
= build_id_cache__add_s(sbuild_id
, filename
, NULL
, false, false);
40 pr_debug("Failed to add build id cache of %s\n", filename
);
44 static char *get_self_path(void)
46 char *buf
= calloc(PATH_MAX
, sizeof(char));
48 if (buf
&& readlink("/proc/self/exe", buf
, PATH_MAX
- 1) < 0) {
49 pr_debug("Failed to get correct path of perf\n");
56 static int search_cached_probe(const char *target
,
57 const char *group
, const char *event
)
59 struct probe_cache
*cache
= probe_cache__new(target
, NULL
);
63 pr_debug("Failed to open probe cache of %s\n", target
);
67 if (!probe_cache__find_by_name(cache
, group
, event
)) {
68 pr_debug("Failed to find %s:%s in the cache\n", group
, event
);
71 probe_cache__delete(cache
);
76 int test__sdt_event(struct test
*test __maybe_unused
, int subtests __maybe_unused
)
79 char __tempdir
[] = "./test-buildid-XXXXXX";
80 char *tempdir
= NULL
, *myself
= get_self_path();
82 if (myself
== NULL
|| mkdtemp(__tempdir
) == NULL
) {
83 pr_debug("Failed to make a tempdir for build-id cache\n");
86 /* Note that buildid_dir must be an absolute path */
87 tempdir
= realpath(__tempdir
, NULL
);
91 /* At first, scan itself */
92 set_buildid_dir(tempdir
);
93 if (build_id_cache__add_file(myself
) < 0)
96 /* Open a cache and make sure the SDT is stored */
97 if (search_cached_probe(myself
, "sdt_perf", "test_target") < 0)
100 /* TBD: probing on the SDT event and collect logs */
102 /* Call the target and get an event */
103 ret
= target_function();
106 /* Cleanup temporary buildid dir */
114 int test__sdt_event(struct test
*test __maybe_unused
, int subtests __maybe_unused
)
116 pr_debug("Skip SDT event test because SDT support is not compiled\n");