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