File Index
Files and directories
Sign in
FILE

kallsyms-split.c

File preview with syntax highlighting.

linux-6.19.14/tools/perf/tests/kallsyms-split.c C
  1// SPDX-License-Identifier: GPL-2.0
  2#include <linux/compiler.h>
  3#include <fcntl.h>
  4#include <signal.h>
  5#include <unistd.h>
  6#include <sys/stat.h>
  7#include "util/dso.h"
  8#include "util/map.h"
  9#include "util/symbol.h"
 10#include "util/debug.h"
 11#include "util/machine.h"
 12#include "tests.h"
 13
 14/*
 15 * This test is to check whether a bad symbol in a module won't split kallsyms maps.
 16 * The main_symbol[1-3] should belong to the main [kernel.kallsyms] map even if the
 17 * bad_symbol from the module is found in the middle.
 18 */
 19static char root_template[] = "/tmp/perf-test.XXXXXX";
 20static char *root_dir;
 21
 22static const char proc_version[] = "Linux version X.Y.Z (just for perf test)\n";
 23static const char proc_modules[] = "module 4096 1 - Live 0xffffffffcd000000\n";
 24static const char proc_kallsyms[] =
 25	"ffffffffab200000 T _stext\n"
 26	"ffffffffab200010 T good_symbol\n"
 27	"ffffffffab200020 t bad_symbol\n"
 28	"ffffffffab200030 t main_symbol1\n"
 29	"ffffffffab200040 t main_symbol2\n"
 30	"ffffffffab200050 t main_symbol3\n"
 31	"ffffffffab200060 T _etext\n"
 32	"ffffffffcd000000 T start_module\t[module]\n"
 33	"ffffffffab200020 u bad_symbol\t[module]\n"
 34	"ffffffffcd000040 T end_module\t[module]\n";
 35
 36static struct {
 37	const char *name;
 38	const char *contents;
 39	long len;
 40} proc_files[] = {
 41	{ "version", proc_version, sizeof(proc_version) - 1 },
 42	{ "modules", proc_modules, sizeof(proc_modules) - 1 },
 43	{ "kallsyms", proc_kallsyms, sizeof(proc_kallsyms) - 1 },
 44};
 45
 46static void remove_proc_dir(int sig __maybe_unused)
 47{
 48	char buf[128];
 49
 50	if (root_dir == NULL)
 51		return;
 52
 53	for (unsigned i = 0; i < ARRAY_SIZE(proc_files); i++) {
 54		scnprintf(buf, sizeof(buf), "%s/proc/%s", root_dir, proc_files[i].name);
 55		remove(buf);
 56	}
 57
 58	scnprintf(buf, sizeof(buf), "%s/proc", root_dir);
 59	rmdir(buf);
 60
 61	rmdir(root_dir);
 62	root_dir = NULL;
 63}
 64
 65static int create_proc_dir(void)
 66{
 67	char buf[128];
 68
 69	root_dir = mkdtemp(root_template);
 70	if (root_dir == NULL)
 71		return -1;
 72
 73	scnprintf(buf, sizeof(buf), "%s/proc", root_dir);
 74	if (mkdir(buf, 0700) < 0)
 75		goto err;
 76
 77	for (unsigned i = 0; i < ARRAY_SIZE(proc_files); i++) {
 78		int fd, len;
 79
 80		scnprintf(buf, sizeof(buf), "%s/proc/%s", root_dir, proc_files[i].name);
 81		fd = open(buf, O_RDWR | O_CREAT, 0600);
 82		if (fd < 0)
 83			goto err;
 84
 85		len = write(fd, proc_files[i].contents, proc_files[i].len);
 86		close(fd);
 87		if (len != proc_files[i].len)
 88			goto err;
 89	}
 90	return 0;
 91
 92err:
 93	remove_proc_dir(0);
 94	return -1;
 95}
 96
 97static int test__kallsyms_split(struct test_suite *test __maybe_unused,
 98				int subtest __maybe_unused)
 99{
100	struct machine m;
101	struct map *map = NULL;
102	int ret = TEST_FAIL;
103
104	pr_debug("try to create fake root directory\n");
105	if (create_proc_dir() < 0) {
106		pr_debug("SKIP: cannot create a fake root directory\n");
107		return TEST_SKIP;
108	}
109
110	signal(SIGINT, remove_proc_dir);
111	signal(SIGPIPE, remove_proc_dir);
112	signal(SIGSEGV, remove_proc_dir);
113	signal(SIGTERM, remove_proc_dir);
114
115	pr_debug("create kernel maps from the fake root directory\n");
116	machine__init(&m, root_dir, HOST_KERNEL_ID);
117	if (machine__create_kernel_maps(&m) < 0) {
118		pr_debug("FAIL: failed to create kernel maps\n");
119		goto out;
120	}
121
122	/* force to use /proc/kallsyms */
123	symbol_conf.ignore_vmlinux = true;
124	symbol_conf.ignore_vmlinux_buildid = true;
125	symbol_conf.allow_aliases = true;
126
127	if (map__load(machine__kernel_map(&m)) < 0) {
128		pr_debug("FAIL: failed to load kallsyms\n");
129		goto out;
130	}
131
132	pr_debug("kernel map loaded - check symbol and map\n");
133	if (maps__nr_maps(machine__kernel_maps(&m)) != 2) {
134		pr_debug("FAIL: it should have the kernel and a module, but has %u maps\n",
135			 maps__nr_maps(machine__kernel_maps(&m)));
136		goto out;
137	}
138
139	if (machine__find_kernel_symbol_by_name(&m, "main_symbol3", &map) == NULL) {
140		pr_debug("FAIL: failed to find a symbol\n");
141		goto out;
142	}
143
144	if (!RC_CHK_EQUAL(map, machine__kernel_map(&m))) {
145		pr_debug("FAIL: the symbol is not in the kernel map\n");
146		goto out;
147	}
148	ret = TEST_OK;
149
150out:
151	map__put(map);
152	remove_proc_dir(0);
153	machine__exit(&m);
154	return ret;
155}
156
157DEFINE_SUITE("split kallsyms", kallsyms_split);
~/ Read-only file preview.