xref: /linux/tools/perf/tests/kmod-path.c (revision 63307d015b91e626c97bb82e88054af3d0b74643)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdbool.h>
3 #include <stdlib.h>
4 #include "tests.h"
5 #include "dso.h"
6 #include "debug.h"
7 
8 static int test(const char *path, bool alloc_name, bool kmod,
9 		int comp, const char *name)
10 {
11 	struct kmod_path m;
12 
13 	memset(&m, 0x0, sizeof(m));
14 
15 	TEST_ASSERT_VAL("kmod_path__parse",
16 			!__kmod_path__parse(&m, path, alloc_name));
17 
18 	pr_debug("%s - alloc name %d, kmod %d, comp %d, name '%s'\n",
19 		 path, alloc_name, m.kmod, m.comp, m.name);
20 
21 	TEST_ASSERT_VAL("wrong kmod", m.kmod == kmod);
22 	TEST_ASSERT_VAL("wrong comp", m.comp == comp);
23 
24 	if (name)
25 		TEST_ASSERT_VAL("wrong name", m.name && !strcmp(name, m.name));
26 	else
27 		TEST_ASSERT_VAL("wrong name", !m.name);
28 
29 	free(m.name);
30 	return 0;
31 }
32 
33 static int test_is_kernel_module(const char *path, int cpumode, bool expect)
34 {
35 	TEST_ASSERT_VAL("is_kernel_module",
36 			(!!is_kernel_module(path, cpumode)) == (!!expect));
37 	pr_debug("%s (cpumode: %d) - is_kernel_module: %s\n",
38 			path, cpumode, expect ? "true" : "false");
39 	return 0;
40 }
41 
42 #define T(path, an, k, c, n) \
43 	TEST_ASSERT_VAL("failed", !test(path, an, k, c, n))
44 
45 #define M(path, c, e) \
46 	TEST_ASSERT_VAL("failed", !test_is_kernel_module(path, c, e))
47 
48 int test__kmod_path__parse(struct test *t __maybe_unused, int subtest __maybe_unused)
49 {
50 	/* path                alloc_name  kmod  comp   name   */
51 	T("/xxxx/xxxx/x-x.ko", true      , true, 0    , "[x_x]");
52 	T("/xxxx/xxxx/x-x.ko", false     , true, 0    , NULL   );
53 	T("/xxxx/xxxx/x-x.ko", true      , true, 0    , "[x_x]");
54 	T("/xxxx/xxxx/x-x.ko", false     , true, 0    , NULL   );
55 	M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
56 	M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_KERNEL, true);
57 	M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_USER, false);
58 
59 #ifdef HAVE_ZLIB_SUPPORT
60 	/* path                alloc_name   kmod  comp  name  */
61 	T("/xxxx/xxxx/x.ko.gz", true     , true, 1   , "[x]");
62 	T("/xxxx/xxxx/x.ko.gz", false    , true, 1   , NULL );
63 	T("/xxxx/xxxx/x.ko.gz", true     , true, 1   , "[x]");
64 	T("/xxxx/xxxx/x.ko.gz", false    , true, 1   , NULL );
65 	M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
66 	M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_KERNEL, true);
67 	M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_USER, false);
68 
69 	/* path              alloc_name  kmod   comp  name  */
70 	T("/xxxx/xxxx/x.gz", true      , false, 1   , "x.gz");
71 	T("/xxxx/xxxx/x.gz", false     , false, 1   , NULL  );
72 	T("/xxxx/xxxx/x.gz", true      , false, 1   , "x.gz");
73 	T("/xxxx/xxxx/x.gz", false     , false, 1   , NULL  );
74 	M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
75 	M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_KERNEL, false);
76 	M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_USER, false);
77 
78 	/* path   alloc_name  kmod   comp  name   */
79 	T("x.gz", true      , false, 1   , "x.gz");
80 	T("x.gz", false     , false, 1   , NULL  );
81 	T("x.gz", true      , false, 1   , "x.gz");
82 	T("x.gz", false     , false, 1   , NULL  );
83 	M("x.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
84 	M("x.gz", PERF_RECORD_MISC_KERNEL, false);
85 	M("x.gz", PERF_RECORD_MISC_USER, false);
86 
87 	/* path      alloc_name  kmod  comp  name  */
88 	T("x.ko.gz", true      , true, 1   , "[x]");
89 	T("x.ko.gz", false     , true, 1   , NULL );
90 	T("x.ko.gz", true      , true, 1   , "[x]");
91 	T("x.ko.gz", false     , true, 1   , NULL );
92 	M("x.ko.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
93 	M("x.ko.gz", PERF_RECORD_MISC_KERNEL, true);
94 	M("x.ko.gz", PERF_RECORD_MISC_USER, false);
95 #endif
96 
97 	/* path            alloc_name  kmod  comp   name           */
98 	T("[test_module]", true      , true, false, "[test_module]");
99 	T("[test_module]", false     , true, false, NULL           );
100 	T("[test_module]", true      , true, false, "[test_module]");
101 	T("[test_module]", false     , true, false, NULL           );
102 	M("[test_module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
103 	M("[test_module]", PERF_RECORD_MISC_KERNEL, true);
104 	M("[test_module]", PERF_RECORD_MISC_USER, false);
105 
106 	/* path            alloc_name  kmod  comp   name           */
107 	T("[test.module]", true      , true, false, "[test.module]");
108 	T("[test.module]", false     , true, false, NULL           );
109 	T("[test.module]", true      , true, false, "[test.module]");
110 	T("[test.module]", false     , true, false, NULL           );
111 	M("[test.module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
112 	M("[test.module]", PERF_RECORD_MISC_KERNEL, true);
113 	M("[test.module]", PERF_RECORD_MISC_USER, false);
114 
115 	/* path     alloc_name  kmod   comp   name    */
116 	T("[vdso]", true      , false, false, "[vdso]");
117 	T("[vdso]", false     , false, false, NULL    );
118 	T("[vdso]", true      , false, false, "[vdso]");
119 	T("[vdso]", false     , false, false, NULL    );
120 	M("[vdso]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
121 	M("[vdso]", PERF_RECORD_MISC_KERNEL, false);
122 	M("[vdso]", PERF_RECORD_MISC_USER, false);
123 
124 	T("[vdso32]", true      , false, false, "[vdso32]");
125 	T("[vdso32]", false     , false, false, NULL      );
126 	T("[vdso32]", true      , false, false, "[vdso32]");
127 	T("[vdso32]", false     , false, false, NULL      );
128 	M("[vdso32]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
129 	M("[vdso32]", PERF_RECORD_MISC_KERNEL, false);
130 	M("[vdso32]", PERF_RECORD_MISC_USER, false);
131 
132 	T("[vdsox32]", true      , false, false, "[vdsox32]");
133 	T("[vdsox32]", false     , false, false, NULL       );
134 	T("[vdsox32]", true      , false, false, "[vdsox32]");
135 	T("[vdsox32]", false     , false, false, NULL       );
136 	M("[vdsox32]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
137 	M("[vdsox32]", PERF_RECORD_MISC_KERNEL, false);
138 	M("[vdsox32]", PERF_RECORD_MISC_USER, false);
139 
140 	/* path         alloc_name  kmod   comp   name        */
141 	T("[vsyscall]", true      , false, false, "[vsyscall]");
142 	T("[vsyscall]", false     , false, false, NULL        );
143 	T("[vsyscall]", true      , false, false, "[vsyscall]");
144 	T("[vsyscall]", false     , false, false, NULL        );
145 	M("[vsyscall]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
146 	M("[vsyscall]", PERF_RECORD_MISC_KERNEL, false);
147 	M("[vsyscall]", PERF_RECORD_MISC_USER, false);
148 
149 	/* path                alloc_name  kmod   comp   name      */
150 	T("[kernel.kallsyms]", true      , false, false, "[kernel.kallsyms]");
151 	T("[kernel.kallsyms]", false     , false, false, NULL               );
152 	T("[kernel.kallsyms]", true      , false, false, "[kernel.kallsyms]");
153 	T("[kernel.kallsyms]", false     , false, false, NULL               );
154 	M("[kernel.kallsyms]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
155 	M("[kernel.kallsyms]", PERF_RECORD_MISC_KERNEL, false);
156 	M("[kernel.kallsyms]", PERF_RECORD_MISC_USER, false);
157 
158 	return 0;
159 }
160