1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <errno.h>
3*4882a593Smuzhiyun #include <limits.h>
4*4882a593Smuzhiyun #include <stdio.h>
5*4882a593Smuzhiyun #include <stdlib.h>
6*4882a593Smuzhiyun #include <unistd.h>
7*4882a593Smuzhiyun #include <sys/epoll.h>
8*4882a593Smuzhiyun #include <util/symbol.h>
9*4882a593Smuzhiyun #include <linux/filter.h>
10*4882a593Smuzhiyun #include "tests.h"
11*4882a593Smuzhiyun #include "debug.h"
12*4882a593Smuzhiyun #include "probe-file.h"
13*4882a593Smuzhiyun #include "build-id.h"
14*4882a593Smuzhiyun #include "util.h"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /* To test SDT event, we need libelf support to scan elf binary */
17*4882a593Smuzhiyun #if defined(HAVE_SDT_EVENT) && defined(HAVE_LIBELF_SUPPORT)
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <sys/sdt.h>
20*4882a593Smuzhiyun
target_function(void)21*4882a593Smuzhiyun static int target_function(void)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun DTRACE_PROBE(perf, test_target);
24*4882a593Smuzhiyun return TEST_OK;
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /* Copied from builtin-buildid-cache.c */
build_id_cache__add_file(const char * filename)28*4882a593Smuzhiyun static int build_id_cache__add_file(const char *filename)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun char sbuild_id[SBUILD_ID_SIZE];
31*4882a593Smuzhiyun struct build_id bid;
32*4882a593Smuzhiyun int err;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun err = filename__read_build_id(filename, &bid);
35*4882a593Smuzhiyun if (err < 0) {
36*4882a593Smuzhiyun pr_debug("Failed to read build id of %s\n", filename);
37*4882a593Smuzhiyun return err;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun build_id__sprintf(&bid, sbuild_id);
41*4882a593Smuzhiyun err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false);
42*4882a593Smuzhiyun if (err < 0)
43*4882a593Smuzhiyun pr_debug("Failed to add build id cache of %s\n", filename);
44*4882a593Smuzhiyun return err;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
get_self_path(void)47*4882a593Smuzhiyun static char *get_self_path(void)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun char *buf = calloc(PATH_MAX, sizeof(char));
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun if (buf && readlink("/proc/self/exe", buf, PATH_MAX - 1) < 0) {
52*4882a593Smuzhiyun pr_debug("Failed to get correct path of perf\n");
53*4882a593Smuzhiyun free(buf);
54*4882a593Smuzhiyun return NULL;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun return buf;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
search_cached_probe(const char * target,const char * group,const char * event)59*4882a593Smuzhiyun static int search_cached_probe(const char *target,
60*4882a593Smuzhiyun const char *group, const char *event)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun struct probe_cache *cache = probe_cache__new(target, NULL);
63*4882a593Smuzhiyun int ret = 0;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun if (!cache) {
66*4882a593Smuzhiyun pr_debug("Failed to open probe cache of %s\n", target);
67*4882a593Smuzhiyun return -EINVAL;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun if (!probe_cache__find_by_name(cache, group, event)) {
71*4882a593Smuzhiyun pr_debug("Failed to find %s:%s in the cache\n", group, event);
72*4882a593Smuzhiyun ret = -ENOENT;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun probe_cache__delete(cache);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun return ret;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
test__sdt_event(struct test * test __maybe_unused,int subtests __maybe_unused)79*4882a593Smuzhiyun int test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun int ret = TEST_FAIL;
82*4882a593Smuzhiyun char __tempdir[] = "./test-buildid-XXXXXX";
83*4882a593Smuzhiyun char *tempdir = NULL, *myself = get_self_path();
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (myself == NULL || mkdtemp(__tempdir) == NULL) {
86*4882a593Smuzhiyun pr_debug("Failed to make a tempdir for build-id cache\n");
87*4882a593Smuzhiyun goto error;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun /* Note that buildid_dir must be an absolute path */
90*4882a593Smuzhiyun tempdir = realpath(__tempdir, NULL);
91*4882a593Smuzhiyun if (tempdir == NULL)
92*4882a593Smuzhiyun goto error_rmdir;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* At first, scan itself */
95*4882a593Smuzhiyun set_buildid_dir(tempdir);
96*4882a593Smuzhiyun if (build_id_cache__add_file(myself) < 0)
97*4882a593Smuzhiyun goto error_rmdir;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* Open a cache and make sure the SDT is stored */
100*4882a593Smuzhiyun if (search_cached_probe(myself, "sdt_perf", "test_target") < 0)
101*4882a593Smuzhiyun goto error_rmdir;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /* TBD: probing on the SDT event and collect logs */
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /* Call the target and get an event */
106*4882a593Smuzhiyun ret = target_function();
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun error_rmdir:
109*4882a593Smuzhiyun /* Cleanup temporary buildid dir */
110*4882a593Smuzhiyun rm_rf(__tempdir);
111*4882a593Smuzhiyun error:
112*4882a593Smuzhiyun free(tempdir);
113*4882a593Smuzhiyun free(myself);
114*4882a593Smuzhiyun return ret;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun #else
test__sdt_event(struct test * test __maybe_unused,int subtests __maybe_unused)117*4882a593Smuzhiyun int test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun pr_debug("Skip SDT event test because SDT support is not compiled\n");
120*4882a593Smuzhiyun return TEST_SKIP;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun #endif
123