1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a43783aeSArnaldo Carvalho de Melo #include <errno.h>
37ae811b1SArnaldo Carvalho de Melo #include <limits.h>
48e5dc848SMasami Hiramatsu #include <stdio.h>
57ae811b1SArnaldo Carvalho de Melo #include <stdlib.h>
626049111SArnaldo Carvalho de Melo #include <unistd.h>
78e5dc848SMasami Hiramatsu #include <sys/epoll.h>
82f2ae234SArnaldo Carvalho de Melo #include <util/symbol.h>
98e5dc848SMasami Hiramatsu #include <linux/filter.h>
108e5dc848SMasami Hiramatsu #include "tests.h"
118e5dc848SMasami Hiramatsu #include "debug.h"
128e5dc848SMasami Hiramatsu #include "probe-file.h"
138e5dc848SMasami Hiramatsu #include "build-id.h"
142da39f1cSArnaldo Carvalho de Melo #include "util.h"
158e5dc848SMasami Hiramatsu
168e5dc848SMasami Hiramatsu /* To test SDT event, we need libelf support to scan elf binary */
178e5dc848SMasami Hiramatsu #if defined(HAVE_SDT_EVENT) && defined(HAVE_LIBELF_SUPPORT)
188e5dc848SMasami Hiramatsu
198e5dc848SMasami Hiramatsu #include <sys/sdt.h>
208e5dc848SMasami Hiramatsu
target_function(void)218e5dc848SMasami Hiramatsu static int target_function(void)
228e5dc848SMasami Hiramatsu {
238e5dc848SMasami Hiramatsu DTRACE_PROBE(perf, test_target);
248e5dc848SMasami Hiramatsu return TEST_OK;
258e5dc848SMasami Hiramatsu }
268e5dc848SMasami Hiramatsu
278e5dc848SMasami Hiramatsu /* Copied from builtin-buildid-cache.c */
build_id_cache__add_file(const char * filename)288e5dc848SMasami Hiramatsu static int build_id_cache__add_file(const char *filename)
298e5dc848SMasami Hiramatsu {
308e5dc848SMasami Hiramatsu char sbuild_id[SBUILD_ID_SIZE];
31f766819cSJiri Olsa struct build_id bid;
328e5dc848SMasami Hiramatsu int err;
338e5dc848SMasami Hiramatsu
34f766819cSJiri Olsa err = filename__read_build_id(filename, &bid);
358e5dc848SMasami Hiramatsu if (err < 0) {
368e5dc848SMasami Hiramatsu pr_debug("Failed to read build id of %s\n", filename);
378e5dc848SMasami Hiramatsu return err;
388e5dc848SMasami Hiramatsu }
398e5dc848SMasami Hiramatsu
40bf541169SJiri Olsa build_id__sprintf(&bid, sbuild_id);
41f045b8c4SKrister Johansen err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false);
428e5dc848SMasami Hiramatsu if (err < 0)
438e5dc848SMasami Hiramatsu pr_debug("Failed to add build id cache of %s\n", filename);
448e5dc848SMasami Hiramatsu return err;
458e5dc848SMasami Hiramatsu }
468e5dc848SMasami Hiramatsu
get_self_path(void)478e5dc848SMasami Hiramatsu static char *get_self_path(void)
488e5dc848SMasami Hiramatsu {
498e5dc848SMasami Hiramatsu char *buf = calloc(PATH_MAX, sizeof(char));
508e5dc848SMasami Hiramatsu
510e6ba115STommi Rantala if (buf && readlink("/proc/self/exe", buf, PATH_MAX - 1) < 0) {
528e5dc848SMasami Hiramatsu pr_debug("Failed to get correct path of perf\n");
538e5dc848SMasami Hiramatsu free(buf);
548e5dc848SMasami Hiramatsu return NULL;
558e5dc848SMasami Hiramatsu }
568e5dc848SMasami Hiramatsu return buf;
578e5dc848SMasami Hiramatsu }
588e5dc848SMasami Hiramatsu
search_cached_probe(const char * target,const char * group,const char * event)598e5dc848SMasami Hiramatsu static int search_cached_probe(const char *target,
608e5dc848SMasami Hiramatsu const char *group, const char *event)
618e5dc848SMasami Hiramatsu {
62f045b8c4SKrister Johansen struct probe_cache *cache = probe_cache__new(target, NULL);
638e5dc848SMasami Hiramatsu int ret = 0;
648e5dc848SMasami Hiramatsu
658e5dc848SMasami Hiramatsu if (!cache) {
668e5dc848SMasami Hiramatsu pr_debug("Failed to open probe cache of %s\n", target);
678e5dc848SMasami Hiramatsu return -EINVAL;
688e5dc848SMasami Hiramatsu }
698e5dc848SMasami Hiramatsu
708e5dc848SMasami Hiramatsu if (!probe_cache__find_by_name(cache, group, event)) {
718e5dc848SMasami Hiramatsu pr_debug("Failed to find %s:%s in the cache\n", group, event);
728e5dc848SMasami Hiramatsu ret = -ENOENT;
738e5dc848SMasami Hiramatsu }
748e5dc848SMasami Hiramatsu probe_cache__delete(cache);
758e5dc848SMasami Hiramatsu
768e5dc848SMasami Hiramatsu return ret;
778e5dc848SMasami Hiramatsu }
788e5dc848SMasami Hiramatsu
test__sdt_event(struct test_suite * test __maybe_unused,int subtests __maybe_unused)79*33f44bfdSIan Rogers static int test__sdt_event(struct test_suite *test __maybe_unused, int subtests __maybe_unused)
808e5dc848SMasami Hiramatsu {
818e5dc848SMasami Hiramatsu int ret = TEST_FAIL;
828e5dc848SMasami Hiramatsu char __tempdir[] = "./test-buildid-XXXXXX";
838e5dc848SMasami Hiramatsu char *tempdir = NULL, *myself = get_self_path();
848e5dc848SMasami Hiramatsu
858e5dc848SMasami Hiramatsu if (myself == NULL || mkdtemp(__tempdir) == NULL) {
868e5dc848SMasami Hiramatsu pr_debug("Failed to make a tempdir for build-id cache\n");
878e5dc848SMasami Hiramatsu goto error;
888e5dc848SMasami Hiramatsu }
898e5dc848SMasami Hiramatsu /* Note that buildid_dir must be an absolute path */
908e5dc848SMasami Hiramatsu tempdir = realpath(__tempdir, NULL);
918526bafcSArnaldo Carvalho de Melo if (tempdir == NULL)
928526bafcSArnaldo Carvalho de Melo goto error_rmdir;
938e5dc848SMasami Hiramatsu
948e5dc848SMasami Hiramatsu /* At first, scan itself */
958e5dc848SMasami Hiramatsu set_buildid_dir(tempdir);
968e5dc848SMasami Hiramatsu if (build_id_cache__add_file(myself) < 0)
978e5dc848SMasami Hiramatsu goto error_rmdir;
988e5dc848SMasami Hiramatsu
998e5dc848SMasami Hiramatsu /* Open a cache and make sure the SDT is stored */
1008e5dc848SMasami Hiramatsu if (search_cached_probe(myself, "sdt_perf", "test_target") < 0)
1018e5dc848SMasami Hiramatsu goto error_rmdir;
1028e5dc848SMasami Hiramatsu
1038e5dc848SMasami Hiramatsu /* TBD: probing on the SDT event and collect logs */
1048e5dc848SMasami Hiramatsu
1058e5dc848SMasami Hiramatsu /* Call the target and get an event */
1068e5dc848SMasami Hiramatsu ret = target_function();
1078e5dc848SMasami Hiramatsu
1088e5dc848SMasami Hiramatsu error_rmdir:
1098e5dc848SMasami Hiramatsu /* Cleanup temporary buildid dir */
1108526bafcSArnaldo Carvalho de Melo rm_rf(__tempdir);
1118e5dc848SMasami Hiramatsu error:
1128e5dc848SMasami Hiramatsu free(tempdir);
1138e5dc848SMasami Hiramatsu free(myself);
1148e5dc848SMasami Hiramatsu return ret;
1158e5dc848SMasami Hiramatsu }
1168e5dc848SMasami Hiramatsu #else
test__sdt_event(struct test_suite * test __maybe_unused,int subtests __maybe_unused)117*33f44bfdSIan Rogers static int test__sdt_event(struct test_suite *test __maybe_unused, int subtests __maybe_unused)
1188e5dc848SMasami Hiramatsu {
1198e5dc848SMasami Hiramatsu pr_debug("Skip SDT event test because SDT support is not compiled\n");
1208e5dc848SMasami Hiramatsu return TEST_SKIP;
1218e5dc848SMasami Hiramatsu }
1228e5dc848SMasami Hiramatsu #endif
123d68f0365SIan Rogers
124d68f0365SIan Rogers DEFINE_SUITE("Probe SDT events", sdt_event);
125