1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 * Copyright (C) 2020, VMware, Tzvetomir Stoyanov <[email protected]>
4 *
5 * Modified from libtracefs to libtraceevent:
6 * Copyright (C) 2021, VMware, Steven Rostedt <[email protected]>
7 *
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <time.h>
15 #include <dirent.h>
16 #include <ftw.h>
17
18 #include <CUnit/CUnit.h>
19 #include <CUnit/Basic.h>
20
21 #include "event-parse.h"
22 #include "trace-seq.h"
23
24 #define TRACEEVENT_SUITE "traceevent library"
25
26 #define DYN_STR_EVENT_SYSTEM "irq"
27 #define DYN_STR_FIELD "name"
28 #define DYN_STRING "hello"
29 #define DYN_STRING_FMT "irq=0 handler=hello"
30 static const char dyn_str_event[] =
31 "name: irq_handler_entry\n"
32 "ID: 1\n"
33 "format:\n"
34 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
35 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
36 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\tsigned:0;\n"
37 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
38 "\n"
39 "\tfield:int irq;\toffset:8;\tsize:4;\tsigned:1;\n"
40 "\tfield:__data_loc char[] name;\toffset:12;\tsize:4;\tsigned:1;\n"
41 "\n"
42 "print fmt: \"irq=%d handler=%s\", REC->irq, __get_str(name)\n";
43
44 static char dyn_str_data[] = {
45 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
46 /* common type */ 1, 0x00,
47 #else
48 /* common type */ 0x00, 1,
49 #endif
50 /* common flags */ 0x00,
51 /* common_preempt_count */ 0x00,
52 /* common_pid */ 0x00, 0x00, 0x00, 0x00,
53 /* irq */ 0x00, 0x00, 0x00, 0x00,
54
55 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
56 /* name : offset */ 16, 0x00,
57 /* name : length */ 6, 0x00,
58 #else
59 /* name : length */ 0x00, 6,
60 /* name : offset */ 0x00, 16,
61 #endif
62 /* name */ 'h', 'e', 'l', 'l', 'o', '\0',
63 /* padding */ 0x00, 0x00
64 };
65 static void *dyn_str_event_data = (void *)dyn_str_data;
66
67 static const char dyn_str_old_event[] =
68 "name: irq_handler_entry\n"
69 "ID: 2\n"
70 "format:\n"
71 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\n"
72 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\n"
73 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\n"
74 "\tfield:int common_pid;\toffset:4;\tsize:4;\n"
75 "\n"
76 "\tfield:int irq;\toffset:8;\tsize:4;\n"
77 "\tfield:__data_loc name;\toffset:12;\tsize:2;\n"
78 "\n"
79 "print fmt: \"irq=%d handler=%s\", REC->irq, __get_str(name)\n";
80
81 static char dyn_str_old_data[] = {
82 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
83 /* common type */ 2, 0x00,
84 #else
85 /* common type */ 0x00, 2,
86 #endif
87 /* common flags */ 0x00,
88 /* common_preempt_count */ 0x00,
89 /* common_pid */ 0x00, 0x00, 0x00, 0x00,
90 /* irq */ 0x00, 0x00, 0x00, 0x00,
91
92 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
93 /* name : offset */ 16, 0x00,
94 #else
95 /* name : offset */ 0x00, 16,
96 #endif
97 /* padding */ 0x00, 0x00,
98 /* name */ 'h', 'e', 'l', 'l', 'o', '\0',
99 /* padding */ 0x00, 0x00, 0x00
100 };
101 static void *dyn_str_old_event_data = (void *)dyn_str_old_data;
102
103 #define CPUMASK_EVENT_SYSTEM "ipi"
104 #define CPUMASK_EVENT_FIELD "cpumask"
105 static const char cpumask_event_format[] =
106 "name: ipi_send_cpumask\n"
107 "ID: 3\n"
108 "format:\n"
109 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\n"
110 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\n"
111 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\n"
112 "\tfield:int common_pid;\toffset:4;\tsize:4;\n"
113 "\n"
114 "\tfield:__data_loc cpumask_t *[] cpumask;\toffset:8;\tsize:4;\tsigned:0;\n"
115 "\n"
116 "print fmt: \"cpumask=%s\", __get_cpumask(cpumask)\n";
117
118 /* Mind the endianness! */
119 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
120 #define DECL_CPUMASK_EVENT_DATA(name, args...) \
121 static char cpumask_##name##_event_data[] = { \
122 /* common type */ 3, 0x00, \
123 /* common flags */ 0x00, \
124 /* common_preempt_count */ 0x00, \
125 /* common_pid */ 0x00, 0x00, 0x00, 0x00, \
126 /* [offset, size] */ 16, 0x00, 8, 0x00, \
127 /* padding */ 0x00, 0x00, 0x00, 0x00, \
128 /* cpumask */ args, \
129 }
130 #else
131 #define DECL_CPUMASK_EVENT_DATA(name, args...) \
132 static char cpumask_##name##_event_data[] = { \
133 /* common type */ 0x00, 3, \
134 /* common flags */ 0x00, \
135 /* common_preempt_count */ 0x00, \
136 /* common_pid */ 0x00, 0x00, 0x00, 0x00, \
137 /* [offset, size] */ 0x00, 8, 0x00, 16, \
138 /* padding */ 0x00, 0x00, 0x00, 0x00, \
139 /* cpumask */ args, \
140 }
141 #endif
142
143 #define SIZEOF_LONG0_FMT "[FAILED TO PARSE] s4=0 u4=0 s8=0 u8=0x0"
144 #define SIZEOF_LONG4_FMT "int=4 unsigned=4 unsigned int=4 long=4 unsigned long=4 long long=8 unsigned long long=8 s4=4 u4=4 s8=8 u8=8"
145 #define SIZEOF_LONG8_FMT "int=4 unsigned=4 unsigned int=4 long=8 unsigned long=8 long long=8 unsigned long long=8 s4=4 u4=4 s8=8 u8=8"
146
147 static const char size_of_event[] =
148 "name: sizeof_event\n"
149 "ID: 23\n"
150 "format:\n"
151 "\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
152 "\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
153 "\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\tsigned:0;\n"
154 "\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
155 "\n"
156 "\tfield:int s4;\toffset:8;\tsize:4;\tsigned:1;\n"
157 "\tfield:unsigned int u4;\toffset:12;\tsize:4;\tsigned:0;\n"
158 "\tfield:long long s8;\toffset:16;\tsize:8;\tsigned:1;\n"
159 "\tfield:unsigned long long u8;\toffset:24;\tsize:8;\tsigned:0;\n"
160 "\n"
161 "print fmt: \"int=%d unsigned=%d unsigned int=%d long=%d unsigned long=%d long long=%d unsigned long long=%d s4=%d u4=%d s8=%d u8=%d\", "
162 "sizeof(int), sizeof(unsigned), sizeof(unsigned int), sizeof(long), sizeof(unsigned long), "
163 "sizeof(long long), sizeof(unsigned long long), sizeof(REC->s4), "
164 "sizeof(REC->u4), sizeof(REC->s8), sizeof(REC->u8))\n";
165 static char sizeof_data[] = {
166 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
167 /* common type */ 23, 0x00,
168 #else
169 /* common type */ 0x00, 23,
170 #endif
171 /* common flags */ 0x00,
172 /* common_preempt_count */ 0x00,
173 /* common_pid */ 0x00, 0x00, 0x00, 0x00,
174 /* irq */ 0x00, 0x00, 0x00, 0x00,
175
176 /* s4 */ 0x00, 0x00, 0x00, 0x00,
177 /* u4 */ 0x00, 0x00, 0x00, 0x00,
178 /* s8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179 /* u8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180 };
181 static void *sizeof_event_data = (void *)sizeof_data;
182
183 DECL_CPUMASK_EVENT_DATA(full, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
184 #define CPUMASK_FULL "ARRAY[ff, ff, ff, ff, ff, ff, ff, ff]"
185 #define CPUMASK_FULL_FMT "cpumask=0-63"
186
187 DECL_CPUMASK_EVENT_DATA(empty, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
188 #define CPUMASK_EMPTY "ARRAY[00, 00, 00, 00, 00, 00, 00, 00]"
189 #define CPUMASK_EMPTY_FMT "cpumask="
190
191 DECL_CPUMASK_EVENT_DATA(half, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55);
192 #define CPUMASK_HALF "ARRAY[55, 55, 55, 55, 55, 55, 55, 55]"
193 #define CPUMASK_HALF_FMT "cpumask=0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62"
194
195 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
196 DECL_CPUMASK_EVENT_DATA(bytep1, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
197 #define CPUMASK_BYTEP1 "ARRAY[01, 80, 00, 00, 00, 00, 00, 00]"
198 #define CPUMASK_BYTEP1_FMT "cpumask=0,15"
199
200 DECL_CPUMASK_EVENT_DATA(bytep2, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00);
201 #define CPUMASK_BYTEP2 "ARRAY[01, 00, 80, 00, 00, 00, 00, 00]"
202 #define CPUMASK_BYTEP2_FMT "cpumask=0,23"
203
204 DECL_CPUMASK_EVENT_DATA(bytepn, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80);
205 #define CPUMASK_BYTEPN "ARRAY[01, 00, 00, 00, 00, 00, 00, 80]"
206 #define CPUMASK_BYTEPN_FMT "cpumask=0,63"
207
208 #else
209
210 DECL_CPUMASK_EVENT_DATA(bytep1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01);
211 #define CPUMASK_BYTEP1 "ARRAY[00, 00, 00, 00, 00, 00, 80, 01]"
212 #define CPUMASK_BYTEP1_FMT "cpumask=0,15"
213
214 DECL_CPUMASK_EVENT_DATA(bytep2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x01);
215 #define CPUMASK_BYTEP2 "ARRAY[00, 00, 00, 00, 00, 80, 00, 01]"
216 #define CPUMASK_BYTEP2_FMT "cpumask=0,23"
217
218 DECL_CPUMASK_EVENT_DATA(bytepn, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01);
219 #define CPUMASK_BYTEPN "ARRAY[80, 00, 00, 00, 00, 00, 00, 01]"
220 #define CPUMASK_BYTEPN_FMT "cpumask=0,63"
221 #endif
222
223 static struct tep_handle *test_tep;
224 static struct trace_seq *test_seq;
225 static struct trace_seq seq_storage;
226
parse_dyn_str(const char * dyn_str,void * data,int size)227 static void parse_dyn_str(const char *dyn_str, void *data, int size)
228 {
229 struct tep_format_field *field;
230 struct tep_event *event;
231 struct tep_record record;
232
233 record.data = data;
234 record.size = size;
235
236 CU_TEST(tep_parse_format(test_tep, &event,
237 dyn_str, strlen(dyn_str),
238 DYN_STR_EVENT_SYSTEM) == TEP_ERRNO__SUCCESS);
239
240 field = tep_find_any_field(event, DYN_STR_FIELD);
241 CU_TEST(field != NULL);
242 trace_seq_reset(test_seq);
243 tep_print_field_content(test_seq, data, size, field);
244 CU_TEST(strcmp(test_seq->buffer, DYN_STRING) == 0);
245
246 trace_seq_reset(test_seq);
247 tep_print_event(test_tep, test_seq, &record, "%s", TEP_PRINT_INFO);
248 trace_seq_do_printf(test_seq);
249 CU_TEST(strcmp(test_seq->buffer, DYN_STRING_FMT) == 0);
250 }
251
test_parse_dyn_str_event(void)252 static void test_parse_dyn_str_event(void)
253 {
254 parse_dyn_str(dyn_str_event, dyn_str_event_data, sizeof(dyn_str_data));
255 }
256
test_parse_dyn_str_old_event(void)257 static void test_parse_dyn_str_old_event(void)
258 {
259 parse_dyn_str(dyn_str_old_event, dyn_str_old_event_data, sizeof(dyn_str_old_data));
260 }
261
parse_cpumask(const char * format,void * data,int size,const char * expected_raw,const char * expected)262 static void parse_cpumask(const char *format, void *data, int size,
263 const char* expected_raw, const char* expected)
264 {
265 struct tep_format_field *field;
266 struct tep_event *event;
267 struct tep_record record;
268
269 record.data = data;
270 record.size = size;
271
272 CU_TEST(tep_parse_format(test_tep, &event,
273 format, strlen(format),
274 CPUMASK_EVENT_SYSTEM) == TEP_ERRNO__SUCCESS);
275
276 field = tep_find_any_field(event, CPUMASK_EVENT_FIELD);
277 CU_TEST(field != NULL);
278
279 trace_seq_reset(test_seq);
280 tep_print_field_content(test_seq, data, size, field);
281 CU_TEST(strcmp(test_seq->buffer, expected_raw) == 0);
282
283 trace_seq_reset(test_seq);
284 tep_print_event(test_tep, test_seq, &record, "%s", TEP_PRINT_INFO);
285 trace_seq_do_printf(test_seq);
286 CU_TEST(strcmp(test_seq->buffer, expected) == 0);
287 }
288
test_parse_cpumask_full(void)289 static void test_parse_cpumask_full(void)
290 {
291 parse_cpumask(cpumask_event_format,
292 cpumask_full_event_data, sizeof(cpumask_full_event_data),
293 CPUMASK_FULL, CPUMASK_FULL_FMT);
294 }
295
test_parse_cpumask_empty(void)296 static void test_parse_cpumask_empty(void)
297 {
298 parse_cpumask(cpumask_event_format,
299 cpumask_empty_event_data, sizeof(cpumask_empty_event_data),
300 CPUMASK_EMPTY, CPUMASK_EMPTY_FMT);
301 }
302
test_parse_cpumask_half(void)303 static void test_parse_cpumask_half(void)
304 {
305 parse_cpumask(cpumask_event_format,
306 cpumask_half_event_data, sizeof(cpumask_half_event_data),
307 CPUMASK_HALF, CPUMASK_HALF_FMT);
308 }
309
test_parse_cpumask_bytep1(void)310 static void test_parse_cpumask_bytep1(void)
311 {
312 parse_cpumask(cpumask_event_format,
313 cpumask_bytep1_event_data, sizeof(cpumask_bytep1_event_data),
314 CPUMASK_BYTEP1, CPUMASK_BYTEP1_FMT);
315 }
316
test_parse_cpumask_bytep2(void)317 static void test_parse_cpumask_bytep2(void)
318 {
319 parse_cpumask(cpumask_event_format,
320 cpumask_bytep2_event_data, sizeof(cpumask_bytep2_event_data),
321 CPUMASK_BYTEP2, CPUMASK_BYTEP2_FMT);
322 }
323
test_parse_cpumask_bytepn(void)324 static void test_parse_cpumask_bytepn(void)
325 {
326 parse_cpumask(cpumask_event_format,
327 cpumask_bytepn_event_data, sizeof(cpumask_bytepn_event_data),
328 CPUMASK_BYTEPN, CPUMASK_BYTEPN_FMT);
329 }
330
test_parse_sizeof(int long_size,int value,const char * system,const char * test_str)331 static void test_parse_sizeof(int long_size, int value, const char *system,
332 const char *test_str)
333 {
334 struct tep_event *event;
335 struct tep_record record;
336 char *sizeof_event;
337 char *p;
338
339 tep_set_long_size(test_tep, long_size);
340
341 record.data = sizeof_event_data;
342 record.size = sizeof(sizeof_data);
343
344 sizeof_event = strdup(size_of_event);
345 CU_TEST(sizeof_event != NULL);
346
347 /* Set a new id */
348 p = strstr(sizeof_event, "ID: 2");
349 p[5] = '0' + value;
350
351 /* Handles endianess */
352 *(unsigned short *)sizeof_data = 20 + value;
353
354 CU_TEST(tep_parse_format(test_tep, &event, sizeof_event,
355 strlen(sizeof_event),
356 system) == TEP_ERRNO__SUCCESS);
357
358 trace_seq_reset(test_seq);
359 tep_print_event(test_tep, test_seq, &record, "%s", TEP_PRINT_INFO);
360 trace_seq_do_printf(test_seq);
361 CU_TEST(strcmp(test_seq->buffer, test_str) == 0);
362
363 free(sizeof_event);
364 }
365
test_parse_sizeof8(void)366 static void test_parse_sizeof8(void)
367 {
368 test_parse_sizeof(8, 3, "sizeof8", SIZEOF_LONG8_FMT);
369 }
370
test_parse_sizeof4(void)371 static void test_parse_sizeof4(void)
372 {
373 test_parse_sizeof(4, 4, "sizeof4", SIZEOF_LONG4_FMT);
374 }
375
test_parse_sizeof_undef(void)376 static void test_parse_sizeof_undef(void)
377 {
378 test_parse_sizeof(0, 5, "sizeof_undef", SIZEOF_LONG0_FMT);
379 }
380
test_suite_destroy(void)381 static int test_suite_destroy(void)
382 {
383 tep_free(test_tep);
384 trace_seq_destroy(test_seq);
385 return 0;
386 }
387
test_suite_init(void)388 static int test_suite_init(void)
389 {
390 test_seq = &seq_storage;
391 trace_seq_init(test_seq);
392 test_tep = tep_alloc();
393 if (!test_tep)
394 return 1;
395 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
396 tep_set_file_bigendian(test_tep, TEP_BIG_ENDIAN);
397 #endif
398 return 0;
399 }
400
test_traceevent_lib(void)401 void test_traceevent_lib(void)
402 {
403 CU_pSuite suite = NULL;
404
405 suite = CU_add_suite(TRACEEVENT_SUITE, test_suite_init, test_suite_destroy);
406 if (suite == NULL) {
407 fprintf(stderr, "Suite \"%s\" cannot be ceated\n", TRACEEVENT_SUITE);
408 return;
409 }
410 CU_add_test(suite, "parse dynamic string event",
411 test_parse_dyn_str_event);
412 CU_add_test(suite, "parse old dynamic string event",
413 test_parse_dyn_str_old_event);
414 CU_add_test(suite, "parse full cpumask",
415 test_parse_cpumask_full);
416 CU_add_test(suite, "parse empty cpumask",
417 test_parse_cpumask_empty);
418 CU_add_test(suite, "parse half-filled cpumask",
419 test_parse_cpumask_half);
420 CU_add_test(suite, "parse cpumask spanning 2 bytes",
421 test_parse_cpumask_bytep1);
422 CU_add_test(suite, "parse cpumask spanning 3 bytes",
423 test_parse_cpumask_bytep2);
424 CU_add_test(suite, "parse cpumask spanning all bytes",
425 test_parse_cpumask_bytepn);
426 CU_add_test(suite, "parse sizeof() 8byte values",
427 test_parse_sizeof8);
428 CU_add_test(suite, "parse sizeof() 4byte values",
429 test_parse_sizeof4);
430 CU_add_test(suite, "parse sizeof() no long size defined",
431 test_parse_sizeof_undef);
432 }
433