1 #include <stdbool.h>
2 #include "tests.h"
3 #include "dso.h"
4 #include "debug.h"
5
test(const char * path,bool alloc_name,bool alloc_ext,bool kmod,bool comp,const char * name,const char * ext)6 static int test(const char *path, bool alloc_name, bool alloc_ext,
7 bool kmod, bool comp, const char *name, const char *ext)
8 {
9 struct kmod_path m;
10
11 memset(&m, 0x0, sizeof(m));
12
13 TEST_ASSERT_VAL("kmod_path__parse",
14 !__kmod_path__parse(&m, path, alloc_name, alloc_ext));
15
16 pr_debug("%s - alloc name %d, alloc ext %d, kmod %d, comp %d, name '%s', ext '%s'\n",
17 path, alloc_name, alloc_ext, m.kmod, m.comp, m.name, m.ext);
18
19 TEST_ASSERT_VAL("wrong kmod", m.kmod == kmod);
20 TEST_ASSERT_VAL("wrong comp", m.comp == comp);
21
22 if (ext)
23 TEST_ASSERT_VAL("wrong ext", m.ext && !strcmp(ext, m.ext));
24 else
25 TEST_ASSERT_VAL("wrong ext", !m.ext);
26
27 if (name)
28 TEST_ASSERT_VAL("wrong name", m.name && !strcmp(name, m.name));
29 else
30 TEST_ASSERT_VAL("wrong name", !m.name);
31
32 free(m.name);
33 free(m.ext);
34 return 0;
35 }
36
test_is_kernel_module(const char * path,int cpumode,bool expect)37 static int test_is_kernel_module(const char *path, int cpumode, bool expect)
38 {
39 TEST_ASSERT_VAL("is_kernel_module",
40 (!!is_kernel_module(path, cpumode)) == (!!expect));
41 pr_debug("%s (cpumode: %d) - is_kernel_module: %s\n",
42 path, cpumode, expect ? "true" : "false");
43 return 0;
44 }
45
46 #define T(path, an, ae, k, c, n, e) \
47 TEST_ASSERT_VAL("failed", !test(path, an, ae, k, c, n, e))
48
49 #define M(path, c, e) \
50 TEST_ASSERT_VAL("failed", !test_is_kernel_module(path, c, e))
51
test__kmod_path__parse(void)52 int test__kmod_path__parse(void)
53 {
54 /* path alloc_name alloc_ext kmod comp name ext */
55 T("/xxxx/xxxx/x-x.ko", true , true , true, false, "[x_x]", NULL);
56 T("/xxxx/xxxx/x-x.ko", false , true , true, false, NULL , NULL);
57 T("/xxxx/xxxx/x-x.ko", true , false , true, false, "[x_x]", NULL);
58 T("/xxxx/xxxx/x-x.ko", false , false , true, false, NULL , NULL);
59 M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
60 M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_KERNEL, true);
61 M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_USER, false);
62
63 #ifdef HAVE_ZLIB_SUPPORT
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 #endif
100
101 /* path alloc_name alloc_ext kmod comp name ext */
102 T("[test_module]", true , true , true, false, "[test_module]", NULL);
103 T("[test_module]", false , true , true, false, NULL , NULL);
104 T("[test_module]", true , false , true, false, "[test_module]", NULL);
105 T("[test_module]", false , false , true, false, NULL , NULL);
106 M("[test_module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
107 M("[test_module]", PERF_RECORD_MISC_KERNEL, true);
108 M("[test_module]", PERF_RECORD_MISC_USER, false);
109
110 /* path alloc_name alloc_ext kmod comp name ext */
111 T("[test.module]", true , true , true, false, "[test.module]", NULL);
112 T("[test.module]", false , true , true, false, NULL , NULL);
113 T("[test.module]", true , false , true, false, "[test.module]", NULL);
114 T("[test.module]", false , false , true, false, NULL , NULL);
115 M("[test.module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
116 M("[test.module]", PERF_RECORD_MISC_KERNEL, true);
117 M("[test.module]", PERF_RECORD_MISC_USER, false);
118
119 /* path alloc_name alloc_ext kmod comp name ext */
120 T("[vdso]", true , true , false, false, "[vdso]", NULL);
121 T("[vdso]", false , true , false, false, NULL , NULL);
122 T("[vdso]", true , false , false, false, "[vdso]", NULL);
123 T("[vdso]", false , false , false, false, NULL , NULL);
124 M("[vdso]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
125 M("[vdso]", PERF_RECORD_MISC_KERNEL, false);
126 M("[vdso]", PERF_RECORD_MISC_USER, false);
127
128 /* path alloc_name alloc_ext kmod comp name ext */
129 T("[vsyscall]", true , true , false, false, "[vsyscall]", NULL);
130 T("[vsyscall]", false , true , false, false, NULL , NULL);
131 T("[vsyscall]", true , false , false, false, "[vsyscall]", NULL);
132 T("[vsyscall]", false , false , false, false, NULL , NULL);
133 M("[vsyscall]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
134 M("[vsyscall]", PERF_RECORD_MISC_KERNEL, false);
135 M("[vsyscall]", PERF_RECORD_MISC_USER, false);
136
137 /* path alloc_name alloc_ext kmod comp name ext */
138 T("[kernel.kallsyms]", true , true , false, false, "[kernel.kallsyms]", NULL);
139 T("[kernel.kallsyms]", false , true , false, false, NULL , NULL);
140 T("[kernel.kallsyms]", true , false , false, false, "[kernel.kallsyms]", NULL);
141 T("[kernel.kallsyms]", false , false , false, false, NULL , NULL);
142 M("[kernel.kallsyms]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
143 M("[kernel.kallsyms]", PERF_RECORD_MISC_KERNEL, false);
144 M("[kernel.kallsyms]", PERF_RECORD_MISC_USER, false);
145
146 return 0;
147 }
148