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