18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <errno.h> 38c2ecf20Sopenharmony_ci#include <limits.h> 48c2ecf20Sopenharmony_ci#include <stdio.h> 58c2ecf20Sopenharmony_ci#include <stdlib.h> 68c2ecf20Sopenharmony_ci#include <unistd.h> 78c2ecf20Sopenharmony_ci#include <sys/epoll.h> 88c2ecf20Sopenharmony_ci#include <util/symbol.h> 98c2ecf20Sopenharmony_ci#include <linux/filter.h> 108c2ecf20Sopenharmony_ci#include "tests.h" 118c2ecf20Sopenharmony_ci#include "debug.h" 128c2ecf20Sopenharmony_ci#include "probe-file.h" 138c2ecf20Sopenharmony_ci#include "build-id.h" 148c2ecf20Sopenharmony_ci#include "util.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci/* To test SDT event, we need libelf support to scan elf binary */ 178c2ecf20Sopenharmony_ci#if defined(HAVE_SDT_EVENT) && defined(HAVE_LIBELF_SUPPORT) 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <sys/sdt.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic int target_function(void) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci DTRACE_PROBE(perf, test_target); 248c2ecf20Sopenharmony_ci return TEST_OK; 258c2ecf20Sopenharmony_ci} 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/* Copied from builtin-buildid-cache.c */ 288c2ecf20Sopenharmony_cistatic int build_id_cache__add_file(const char *filename) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci char sbuild_id[SBUILD_ID_SIZE]; 318c2ecf20Sopenharmony_ci struct build_id bid; 328c2ecf20Sopenharmony_ci int err; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci err = filename__read_build_id(filename, &bid); 358c2ecf20Sopenharmony_ci if (err < 0) { 368c2ecf20Sopenharmony_ci pr_debug("Failed to read build id of %s\n", filename); 378c2ecf20Sopenharmony_ci return err; 388c2ecf20Sopenharmony_ci } 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci build_id__sprintf(&bid, sbuild_id); 418c2ecf20Sopenharmony_ci err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false); 428c2ecf20Sopenharmony_ci if (err < 0) 438c2ecf20Sopenharmony_ci pr_debug("Failed to add build id cache of %s\n", filename); 448c2ecf20Sopenharmony_ci return err; 458c2ecf20Sopenharmony_ci} 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cistatic char *get_self_path(void) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci char *buf = calloc(PATH_MAX, sizeof(char)); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci if (buf && readlink("/proc/self/exe", buf, PATH_MAX - 1) < 0) { 528c2ecf20Sopenharmony_ci pr_debug("Failed to get correct path of perf\n"); 538c2ecf20Sopenharmony_ci free(buf); 548c2ecf20Sopenharmony_ci return NULL; 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci return buf; 578c2ecf20Sopenharmony_ci} 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistatic int search_cached_probe(const char *target, 608c2ecf20Sopenharmony_ci const char *group, const char *event) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci struct probe_cache *cache = probe_cache__new(target, NULL); 638c2ecf20Sopenharmony_ci int ret = 0; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci if (!cache) { 668c2ecf20Sopenharmony_ci pr_debug("Failed to open probe cache of %s\n", target); 678c2ecf20Sopenharmony_ci return -EINVAL; 688c2ecf20Sopenharmony_ci } 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci if (!probe_cache__find_by_name(cache, group, event)) { 718c2ecf20Sopenharmony_ci pr_debug("Failed to find %s:%s in the cache\n", group, event); 728c2ecf20Sopenharmony_ci ret = -ENOENT; 738c2ecf20Sopenharmony_ci } 748c2ecf20Sopenharmony_ci probe_cache__delete(cache); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci return ret; 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ciint test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci int ret = TEST_FAIL; 828c2ecf20Sopenharmony_ci char __tempdir[] = "./test-buildid-XXXXXX"; 838c2ecf20Sopenharmony_ci char *tempdir = NULL, *myself = get_self_path(); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (myself == NULL || mkdtemp(__tempdir) == NULL) { 868c2ecf20Sopenharmony_ci pr_debug("Failed to make a tempdir for build-id cache\n"); 878c2ecf20Sopenharmony_ci goto error; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci /* Note that buildid_dir must be an absolute path */ 908c2ecf20Sopenharmony_ci tempdir = realpath(__tempdir, NULL); 918c2ecf20Sopenharmony_ci if (tempdir == NULL) 928c2ecf20Sopenharmony_ci goto error_rmdir; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci /* At first, scan itself */ 958c2ecf20Sopenharmony_ci set_buildid_dir(tempdir); 968c2ecf20Sopenharmony_ci if (build_id_cache__add_file(myself) < 0) 978c2ecf20Sopenharmony_ci goto error_rmdir; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci /* Open a cache and make sure the SDT is stored */ 1008c2ecf20Sopenharmony_ci if (search_cached_probe(myself, "sdt_perf", "test_target") < 0) 1018c2ecf20Sopenharmony_ci goto error_rmdir; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci /* TBD: probing on the SDT event and collect logs */ 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci /* Call the target and get an event */ 1068c2ecf20Sopenharmony_ci ret = target_function(); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cierror_rmdir: 1098c2ecf20Sopenharmony_ci /* Cleanup temporary buildid dir */ 1108c2ecf20Sopenharmony_ci rm_rf(__tempdir); 1118c2ecf20Sopenharmony_cierror: 1128c2ecf20Sopenharmony_ci free(tempdir); 1138c2ecf20Sopenharmony_ci free(myself); 1148c2ecf20Sopenharmony_ci return ret; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci#else 1178c2ecf20Sopenharmony_ciint test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci pr_debug("Skip SDT event test because SDT support is not compiled\n"); 1208c2ecf20Sopenharmony_ci return TEST_SKIP; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci#endif 123