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