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