cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

sdt.c (2995B)


      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"
     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	struct build_id bid;
     32	int err;
     33
     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;
     38	}
     39
     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;
     45}
     46
     47static char *get_self_path(void)
     48{
     49	char *buf = calloc(PATH_MAX, sizeof(char));
     50
     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;
     55	}
     56	return buf;
     57}
     58
     59static int search_cached_probe(const char *target,
     60			       const char *group, const char *event)
     61{
     62	struct probe_cache *cache = probe_cache__new(target, NULL);
     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
     79static int test__sdt_event(struct test_suite *test __maybe_unused, int subtests __maybe_unused)
     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);
     91	if (tempdir == NULL)
     92		goto error_rmdir;
     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 */
    110	rm_rf(__tempdir);
    111error:
    112	free(tempdir);
    113	free(myself);
    114	return ret;
    115}
    116#else
    117static int test__sdt_event(struct test_suite *test __maybe_unused, int subtests __maybe_unused)
    118{
    119	pr_debug("Skip SDT event test because SDT support is not compiled\n");
    120	return TEST_SKIP;
    121}
    122#endif
    123
    124DEFINE_SUITE("Probe SDT events", sdt_event);