1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23c8a67f5SJiri Olsa #include <stdbool.h>
3e5e6312bSArnaldo Carvalho de Melo #include <stdlib.h>
48520a98dSArnaldo Carvalho de Melo #include <string.h>
53c8a67f5SJiri Olsa #include "tests.h"
63c8a67f5SJiri Olsa #include "dso.h"
73c8a67f5SJiri Olsa #include "debug.h"
88520a98dSArnaldo Carvalho de Melo #include "event.h"
93c8a67f5SJiri Olsa
test(const char * path,bool alloc_name,bool kmod,int comp,const char * name)10b946cd37SJiri Olsa static int test(const char *path, bool alloc_name, bool kmod,
11b946cd37SJiri Olsa int comp, const char *name)
123c8a67f5SJiri Olsa {
133c8a67f5SJiri Olsa struct kmod_path m;
143c8a67f5SJiri Olsa
153c8a67f5SJiri Olsa memset(&m, 0x0, sizeof(m));
163c8a67f5SJiri Olsa
173c8a67f5SJiri Olsa TEST_ASSERT_VAL("kmod_path__parse",
18b946cd37SJiri Olsa !__kmod_path__parse(&m, path, alloc_name));
193c8a67f5SJiri Olsa
20b946cd37SJiri Olsa pr_debug("%s - alloc name %d, kmod %d, comp %d, name '%s'\n",
21b946cd37SJiri Olsa path, alloc_name, m.kmod, m.comp, m.name);
223c8a67f5SJiri Olsa
233c8a67f5SJiri Olsa TEST_ASSERT_VAL("wrong kmod", m.kmod == kmod);
243c8a67f5SJiri Olsa TEST_ASSERT_VAL("wrong comp", m.comp == comp);
253c8a67f5SJiri Olsa
263c8a67f5SJiri Olsa if (name)
273c8a67f5SJiri Olsa TEST_ASSERT_VAL("wrong name", m.name && !strcmp(name, m.name));
283c8a67f5SJiri Olsa else
293c8a67f5SJiri Olsa TEST_ASSERT_VAL("wrong name", !m.name);
303c8a67f5SJiri Olsa
313c8a67f5SJiri Olsa free(m.name);
323c8a67f5SJiri Olsa return 0;
333c8a67f5SJiri Olsa }
343c8a67f5SJiri Olsa
test_is_kernel_module(const char * path,int cpumode,bool expect)351f121b03SWang Nan static int test_is_kernel_module(const char *path, int cpumode, bool expect)
361f121b03SWang Nan {
371f121b03SWang Nan TEST_ASSERT_VAL("is_kernel_module",
381f121b03SWang Nan (!!is_kernel_module(path, cpumode)) == (!!expect));
391f121b03SWang Nan pr_debug("%s (cpumode: %d) - is_kernel_module: %s\n",
401f121b03SWang Nan path, cpumode, expect ? "true" : "false");
411f121b03SWang Nan return 0;
421f121b03SWang Nan }
431f121b03SWang Nan
44b946cd37SJiri Olsa #define T(path, an, k, c, n) \
45b946cd37SJiri Olsa TEST_ASSERT_VAL("failed", !test(path, an, k, c, n))
463c8a67f5SJiri Olsa
471f121b03SWang Nan #define M(path, c, e) \
481f121b03SWang Nan TEST_ASSERT_VAL("failed", !test_is_kernel_module(path, c, e))
491f121b03SWang Nan
test__kmod_path__parse(struct test_suite * t __maybe_unused,int subtest __maybe_unused)50*33f44bfdSIan Rogers static int test__kmod_path__parse(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
513c8a67f5SJiri Olsa {
52b946cd37SJiri Olsa /* path alloc_name kmod comp name */
53b946cd37SJiri Olsa T("/xxxx/xxxx/x-x.ko", true , true, 0 , "[x_x]");
54b946cd37SJiri Olsa T("/xxxx/xxxx/x-x.ko", false , true, 0 , NULL );
55b946cd37SJiri Olsa T("/xxxx/xxxx/x-x.ko", true , true, 0 , "[x_x]");
56b946cd37SJiri Olsa T("/xxxx/xxxx/x-x.ko", false , true, 0 , NULL );
571f121b03SWang Nan M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
581f121b03SWang Nan M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_KERNEL, true);
591f121b03SWang Nan M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_USER, false);
603c8a67f5SJiri Olsa
61805b151aSKim Phillips #ifdef HAVE_ZLIB_SUPPORT
62b946cd37SJiri Olsa /* path alloc_name kmod comp name */
63b946cd37SJiri Olsa T("/xxxx/xxxx/x.ko.gz", true , true, 1 , "[x]");
64b946cd37SJiri Olsa T("/xxxx/xxxx/x.ko.gz", false , true, 1 , NULL );
65b946cd37SJiri Olsa T("/xxxx/xxxx/x.ko.gz", true , true, 1 , "[x]");
66b946cd37SJiri Olsa T("/xxxx/xxxx/x.ko.gz", false , true, 1 , NULL );
671f121b03SWang Nan M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
681f121b03SWang Nan M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_KERNEL, true);
691f121b03SWang Nan M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_USER, false);
703c8a67f5SJiri Olsa
71b946cd37SJiri Olsa /* path alloc_name kmod comp name */
72b946cd37SJiri Olsa T("/xxxx/xxxx/x.gz", true , false, 1 , "x.gz");
73b946cd37SJiri Olsa T("/xxxx/xxxx/x.gz", false , false, 1 , NULL );
74b946cd37SJiri Olsa T("/xxxx/xxxx/x.gz", true , false, 1 , "x.gz");
75b946cd37SJiri Olsa T("/xxxx/xxxx/x.gz", false , false, 1 , NULL );
761f121b03SWang Nan M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
771f121b03SWang Nan M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_KERNEL, false);
781f121b03SWang Nan M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_USER, false);
793c8a67f5SJiri Olsa
80b946cd37SJiri Olsa /* path alloc_name kmod comp name */
81b946cd37SJiri Olsa T("x.gz", true , false, 1 , "x.gz");
82b946cd37SJiri Olsa T("x.gz", false , false, 1 , NULL );
83b946cd37SJiri Olsa T("x.gz", true , false, 1 , "x.gz");
84b946cd37SJiri Olsa T("x.gz", false , false, 1 , NULL );
851f121b03SWang Nan M("x.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
861f121b03SWang Nan M("x.gz", PERF_RECORD_MISC_KERNEL, false);
871f121b03SWang Nan M("x.gz", PERF_RECORD_MISC_USER, false);
883c8a67f5SJiri Olsa
89b946cd37SJiri Olsa /* path alloc_name kmod comp name */
90b946cd37SJiri Olsa T("x.ko.gz", true , true, 1 , "[x]");
91b946cd37SJiri Olsa T("x.ko.gz", false , true, 1 , NULL );
92b946cd37SJiri Olsa T("x.ko.gz", true , true, 1 , "[x]");
93b946cd37SJiri Olsa T("x.ko.gz", false , true, 1 , NULL );
941f121b03SWang Nan M("x.ko.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
951f121b03SWang Nan M("x.ko.gz", PERF_RECORD_MISC_KERNEL, true);
961f121b03SWang Nan M("x.ko.gz", PERF_RECORD_MISC_USER, false);
97805b151aSKim Phillips #endif
981f121b03SWang Nan
99b946cd37SJiri Olsa /* path alloc_name kmod comp name */
100b946cd37SJiri Olsa T("[test_module]", true , true, false, "[test_module]");
101b946cd37SJiri Olsa T("[test_module]", false , true, false, NULL );
102b946cd37SJiri Olsa T("[test_module]", true , true, false, "[test_module]");
103b946cd37SJiri Olsa T("[test_module]", false , true, false, NULL );
1041f121b03SWang Nan M("[test_module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
1051f121b03SWang Nan M("[test_module]", PERF_RECORD_MISC_KERNEL, true);
1061f121b03SWang Nan M("[test_module]", PERF_RECORD_MISC_USER, false);
1071f121b03SWang Nan
108b946cd37SJiri Olsa /* path alloc_name kmod comp name */
109b946cd37SJiri Olsa T("[test.module]", true , true, false, "[test.module]");
110b946cd37SJiri Olsa T("[test.module]", false , true, false, NULL );
111b946cd37SJiri Olsa T("[test.module]", true , true, false, "[test.module]");
112b946cd37SJiri Olsa T("[test.module]", false , true, false, NULL );
1131f121b03SWang Nan M("[test.module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
1141f121b03SWang Nan M("[test.module]", PERF_RECORD_MISC_KERNEL, true);
1151f121b03SWang Nan M("[test.module]", PERF_RECORD_MISC_USER, false);
1161f121b03SWang Nan
117b946cd37SJiri Olsa /* path alloc_name kmod comp name */
118b946cd37SJiri Olsa T("[vdso]", true , false, false, "[vdso]");
119b946cd37SJiri Olsa T("[vdso]", false , false, false, NULL );
120b946cd37SJiri Olsa T("[vdso]", true , false, false, "[vdso]");
121b946cd37SJiri Olsa T("[vdso]", false , false, false, NULL );
1221f121b03SWang Nan M("[vdso]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
1231f121b03SWang Nan M("[vdso]", PERF_RECORD_MISC_KERNEL, false);
1241f121b03SWang Nan M("[vdso]", PERF_RECORD_MISC_USER, false);
1251f121b03SWang Nan
126b946cd37SJiri Olsa T("[vdso32]", true , false, false, "[vdso32]");
127b946cd37SJiri Olsa T("[vdso32]", false , false, false, NULL );
128b946cd37SJiri Olsa T("[vdso32]", true , false, false, "[vdso32]");
129b946cd37SJiri Olsa T("[vdso32]", false , false, false, NULL );
130dcaeae4eSAdrian Hunter M("[vdso32]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
131dcaeae4eSAdrian Hunter M("[vdso32]", PERF_RECORD_MISC_KERNEL, false);
132dcaeae4eSAdrian Hunter M("[vdso32]", PERF_RECORD_MISC_USER, false);
133dcaeae4eSAdrian Hunter
134b946cd37SJiri Olsa T("[vdsox32]", true , false, false, "[vdsox32]");
135b946cd37SJiri Olsa T("[vdsox32]", false , false, false, NULL );
136b946cd37SJiri Olsa T("[vdsox32]", true , false, false, "[vdsox32]");
137b946cd37SJiri Olsa T("[vdsox32]", false , false, false, NULL );
138dcaeae4eSAdrian Hunter M("[vdsox32]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
139dcaeae4eSAdrian Hunter M("[vdsox32]", PERF_RECORD_MISC_KERNEL, false);
140dcaeae4eSAdrian Hunter M("[vdsox32]", PERF_RECORD_MISC_USER, false);
141dcaeae4eSAdrian Hunter
142b946cd37SJiri Olsa /* path alloc_name kmod comp name */
143b946cd37SJiri Olsa T("[vsyscall]", true , false, false, "[vsyscall]");
144b946cd37SJiri Olsa T("[vsyscall]", false , false, false, NULL );
145b946cd37SJiri Olsa T("[vsyscall]", true , false, false, "[vsyscall]");
146b946cd37SJiri Olsa T("[vsyscall]", false , false, false, NULL );
1471f121b03SWang Nan M("[vsyscall]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
1481f121b03SWang Nan M("[vsyscall]", PERF_RECORD_MISC_KERNEL, false);
1491f121b03SWang Nan M("[vsyscall]", PERF_RECORD_MISC_USER, false);
1501f121b03SWang Nan
151b946cd37SJiri Olsa /* path alloc_name kmod comp name */
152b946cd37SJiri Olsa T("[kernel.kallsyms]", true , false, false, "[kernel.kallsyms]");
153b946cd37SJiri Olsa T("[kernel.kallsyms]", false , false, false, NULL );
154b946cd37SJiri Olsa T("[kernel.kallsyms]", true , false, false, "[kernel.kallsyms]");
155b946cd37SJiri Olsa T("[kernel.kallsyms]", false , false, false, NULL );
1561f121b03SWang Nan M("[kernel.kallsyms]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
1571f121b03SWang Nan M("[kernel.kallsyms]", PERF_RECORD_MISC_KERNEL, false);
1581f121b03SWang Nan M("[kernel.kallsyms]", PERF_RECORD_MISC_USER, false);
1593c8a67f5SJiri Olsa
1603c8a67f5SJiri Olsa return 0;
1613c8a67f5SJiri Olsa }
162d68f0365SIan Rogers
163d68f0365SIan Rogers DEFINE_SUITE("kmod_path__parse", kmod_path__parse);
164