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