Merge tag 'ntb-5.11' of git://github.com/jonmason/ntb
[linux/fpc-iii.git] / tools / perf / tests / sdt.c
blobed76c693f65ecff5f8ff656828a03ab484cf96c2
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 <unistd.h>
7 #include <sys/epoll.h>
8 #include <util/symbol.h>
9 #include <linux/filter.h>
10 #include "tests.h"
11 #include "debug.h"
12 #include "probe-file.h"
13 #include "build-id.h"
14 #include "util.h"
16 /* To test SDT event, we need libelf support to scan elf binary */
17 #if defined(HAVE_SDT_EVENT) && defined(HAVE_LIBELF_SUPPORT)
19 #include <sys/sdt.h>
21 static int target_function(void)
23 DTRACE_PROBE(perf, test_target);
24 return TEST_OK;
27 /* Copied from builtin-buildid-cache.c */
28 static int build_id_cache__add_file(const char *filename)
30 char sbuild_id[SBUILD_ID_SIZE];
31 struct build_id bid;
32 int err;
34 err = filename__read_build_id(filename, &bid);
35 if (err < 0) {
36 pr_debug("Failed to read build id of %s\n", filename);
37 return err;
40 build_id__sprintf(&bid, sbuild_id);
41 err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false);
42 if (err < 0)
43 pr_debug("Failed to add build id cache of %s\n", filename);
44 return err;
47 static char *get_self_path(void)
49 char *buf = calloc(PATH_MAX, sizeof(char));
51 if (buf && readlink("/proc/self/exe", buf, PATH_MAX - 1) < 0) {
52 pr_debug("Failed to get correct path of perf\n");
53 free(buf);
54 return NULL;
56 return buf;
59 static int search_cached_probe(const char *target,
60 const char *group, const char *event)
62 struct probe_cache *cache = probe_cache__new(target, NULL);
63 int ret = 0;
65 if (!cache) {
66 pr_debug("Failed to open probe cache of %s\n", target);
67 return -EINVAL;
70 if (!probe_cache__find_by_name(cache, group, event)) {
71 pr_debug("Failed to find %s:%s in the cache\n", group, event);
72 ret = -ENOENT;
74 probe_cache__delete(cache);
76 return ret;
79 int test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused)
81 int ret = TEST_FAIL;
82 char __tempdir[] = "./test-buildid-XXXXXX";
83 char *tempdir = NULL, *myself = get_self_path();
85 if (myself == NULL || mkdtemp(__tempdir) == NULL) {
86 pr_debug("Failed to make a tempdir for build-id cache\n");
87 goto error;
89 /* Note that buildid_dir must be an absolute path */
90 tempdir = realpath(__tempdir, NULL);
91 if (tempdir == NULL)
92 goto error_rmdir;
94 /* At first, scan itself */
95 set_buildid_dir(tempdir);
96 if (build_id_cache__add_file(myself) < 0)
97 goto error_rmdir;
99 /* Open a cache and make sure the SDT is stored */
100 if (search_cached_probe(myself, "sdt_perf", "test_target") < 0)
101 goto error_rmdir;
103 /* TBD: probing on the SDT event and collect logs */
105 /* Call the target and get an event */
106 ret = target_function();
108 error_rmdir:
109 /* Cleanup temporary buildid dir */
110 rm_rf(__tempdir);
111 error:
112 free(tempdir);
113 free(myself);
114 return ret;
116 #else
117 int test__sdt_event(struct test *test __maybe_unused, int subtests __maybe_unused)
119 pr_debug("Skip SDT event test because SDT support is not compiled\n");
120 return TEST_SKIP;
122 #endif