blob: 0579a70bbbff139cfa6a4f2fffd20ac5cafd7dd0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsa3c8a67f2015-02-05 15:40:25 +01002#include <stdbool.h>
Arnaldo Carvalho de Meloe5e63122016-07-22 12:12:49 -03003#include <stdlib.h>
Jiri Olsa3c8a67f2015-02-05 15:40:25 +01004#include "tests.h"
5#include "dso.h"
6#include "debug.h"
7
Jiri Olsab946cd32018-08-17 11:48:13 +02008static int test(const char *path, bool alloc_name, bool kmod,
9 int comp, const char *name)
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010010{
11 struct kmod_path m;
12
13 memset(&m, 0x0, sizeof(m));
14
15 TEST_ASSERT_VAL("kmod_path__parse",
Jiri Olsab946cd32018-08-17 11:48:13 +020016 !__kmod_path__parse(&m, path, alloc_name));
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010017
Jiri Olsab946cd32018-08-17 11:48:13 +020018 pr_debug("%s - alloc name %d, kmod %d, comp %d, name '%s'\n",
19 path, alloc_name, m.kmod, m.comp, m.name);
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010020
21 TEST_ASSERT_VAL("wrong kmod", m.kmod == kmod);
22 TEST_ASSERT_VAL("wrong comp", m.comp == comp);
23
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010024 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);
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010030 return 0;
31}
32
Wang Nan1f121b02015-06-03 08:52:21 +000033static 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
Jiri Olsab946cd32018-08-17 11:48:13 +020042#define T(path, an, k, c, n) \
43 TEST_ASSERT_VAL("failed", !test(path, an, k, c, n))
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010044
Wang Nan1f121b02015-06-03 08:52:21 +000045#define M(path, c, e) \
46 TEST_ASSERT_VAL("failed", !test_is_kernel_module(path, c, e))
47
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030048int test__kmod_path__parse(struct test *t __maybe_unused, int subtest __maybe_unused)
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010049{
Jiri Olsab946cd32018-08-17 11:48:13 +020050 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +000055 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);
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010058
Kim Phillips805b1512017-05-03 13:14:02 +010059#ifdef HAVE_ZLIB_SUPPORT
Jiri Olsab946cd32018-08-17 11:48:13 +020060 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +000065 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);
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010068
Jiri Olsab946cd32018-08-17 11:48:13 +020069 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +000074 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);
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010077
Jiri Olsab946cd32018-08-17 11:48:13 +020078 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +000083 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);
Jiri Olsa3c8a67f2015-02-05 15:40:25 +010086
Jiri Olsab946cd32018-08-17 11:48:13 +020087 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +000092 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);
Kim Phillips805b1512017-05-03 13:14:02 +010095#endif
Wang Nan1f121b02015-06-03 08:52:21 +000096
Jiri Olsab946cd32018-08-17 11:48:13 +020097 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +0000102 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
Jiri Olsab946cd32018-08-17 11:48:13 +0200106 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +0000111 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
Jiri Olsab946cd32018-08-17 11:48:13 +0200115 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +0000120 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
Jiri Olsab946cd32018-08-17 11:48:13 +0200124 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 );
Adrian Hunterdcaeae42018-06-04 15:56:53 +0300128 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
Jiri Olsab946cd32018-08-17 11:48:13 +0200132 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 );
Adrian Hunterdcaeae42018-06-04 15:56:53 +0300136 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
Jiri Olsab946cd32018-08-17 11:48:13 +0200140 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +0000145 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
Jiri Olsab946cd32018-08-17 11:48:13 +0200149 /* 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 );
Wang Nan1f121b02015-06-03 08:52:21 +0000154 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);
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100157
158 return 0;
159}