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