aboutsummaryrefslogtreecommitdiff
path: root/utest/traceevent-utest.c
blob: ebd5eb93efc248aa0e4db47fee9c230d42086f49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// SPDX-License-Identifier: LGPL-2.1
/*
 * Copyright (C) 2020, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
 *
 * Modified from libtracefs to libtraceevent:
 *   Copyright (C) 2021, VMware, Steven Rostedt <rostedt@goodmis.org>
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#include <ftw.h>

#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

#include "event-parse.h"
#include "trace-seq.h"

#define TRACEEVENT_SUITE	"traceevent library"

#define DYN_STR_EVENT_SYSTEM		"irq"
#define DYN_STR_FIELD			"name"
#define DYN_STRING			"hello"
#define DYN_STRING_FMT			"irq=0 handler=hello"
static const char dyn_str_event[] =
	"name: irq_handler_entry\n"
	"ID: 1\n"
	"format:\n"
	"\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
	"\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
	"\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\tsigned:0;\n"
	"\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
	"\n"
        "\tfield:int irq;\toffset:8;\tsize:4;\tsigned:1;\n"
        "\tfield:__data_loc char[] name;\toffset:12;\tsize:4;\tsigned:1;\n"
	"\n"
	"print fmt: \"irq=%d handler=%s\", REC->irq, __get_str(name)\n";

static char dyn_str_data[] = {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	/* common type */		1, 0x00,
#else
	/* common type */		0x00, 1
#endif
	/* common flags */		0x00,
	/* common_preempt_count */	0x00,
	/* common_pid */		0x00, 0x00, 0x00, 0x00,
	/* irq */			0x00, 0x00, 0x00, 0x00,

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	/* name : offset */		16, 0x00,
	/* name : length */		6, 0x00,
#else
	/* name : length */		0x00, 6,
	/* name : offset */		0x00, 16,
#endif
	/* name */			'h', 'e', 'l', 'l', 'o', '\0',
	/* padding */			0x00, 0x00
};
static void *dyn_str_event_data = (void *)dyn_str_data;

static const char dyn_str_old_event[] =
	"name: irq_handler_entry\n"
	"ID: 2\n"
	"format:\n"
	"\tfield:unsigned short common_type;\toffset:0;\tsize:2;\n"
	"\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\n"
	"\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\n"
	"\tfield:int common_pid;\toffset:4;\tsize:4;\n"
	"\n"
        "\tfield:int irq;\toffset:8;\tsize:4;\n"
        "\tfield:__data_loc name;\toffset:12;\tsize:2;\n"
	"\n"
	"print fmt: \"irq=%d handler=%s\", REC->irq, __get_str(name)\n";

static char dyn_str_old_data[] = {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	/* common type */		2, 0x00,
#else
	/* common type */		0x00, 2
#endif
	/* common flags */		0x00,
	/* common_preempt_count */	0x00,
	/* common_pid */		0x00, 0x00, 0x00, 0x00,
	/* irq */			0x00, 0x00, 0x00, 0x00,

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	/* name : offset */		16, 0x00,
#else
	/* name : offset */		0x00, 16,
#endif
	/* padding */			0x00, 0x00,
	/* name */			'h', 'e', 'l', 'l', 'o', '\0',
	/* padding */			0x00, 0x00, 0x00
};
static void *dyn_str_old_event_data = (void *)dyn_str_old_data;

#define CPUMASK_EVENT_SYSTEM "ipi"
#define CPUMASK_EVENT_FIELD  "cpumask"
static const char cpumask_event_format[] =
	"name: ipi_send_cpumask\n"
	"ID: 3\n"
	"format:\n"
	"\tfield:unsigned short common_type;\toffset:0;\tsize:2;\n"
	"\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\n"
	"\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\n"
	"\tfield:int common_pid;\toffset:4;\tsize:4;\n"
	"\n"
	"\tfield:__data_loc cpumask_t *[] cpumask;\toffset:8;\tsize:4;\tsigned:0;\n"
	"\n"
	"print fmt: \"cpumask=%s\", __get_cpumask(cpumask)\n";

/* Mind the endianness! */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define DECL_CPUMASK_EVENT_DATA(name, args...)			\
	static char cpumask_##name##_event_data[] = {		\
	/* common type */		3, 0x00,                \
	/* common flags */		0x00,                   \
	/* common_preempt_count */	0x00,                   \
	/* common_pid */		0x00, 0x00, 0x00, 0x00, \
	/* [offset, size] */            16, 0x00, 8, 0x00,      \
	/* padding */			0x00, 0x00, 0x00, 0x00, \
	/* cpumask */                   args,                   \
}
#else
#define DECL_CPUMASK_EVENT_DATA(name, args...)			\
static char cpumask_##name##_event_data[] = {                       \
	/* common type */		0x00, 3,                \
	/* common flags */		0x00,                   \
	/* common_preempt_count */	0x00,                   \
	/* common_pid */		0x00, 0x00, 0x00, 0x00, \
	/* [offset, size] */            0x00, 8, 0x00, 16,      \
	/* padding */			0x00, 0x00, 0x00, 0x00, \
	/* cpumask */                   args,                   \
}
#endif

#define SIZEOF_LONG0_FMT "[FAILED TO PARSE] s4=0 u4=0 s8=0 u8=0x0"
#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"
#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"

static const char size_of_event[] =
	"name: sizeof_event\n"
	"ID: 23\n"
	"format:\n"
	"\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
	"\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
	"\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\tsigned:0;\n"
	"\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
	"\n"
        "\tfield:int s4;\toffset:8;\tsize:4;\tsigned:1;\n"
        "\tfield:unsigned int u4;\toffset:12;\tsize:4;\tsigned:0;\n"
        "\tfield:long long s8;\toffset:16;\tsize:8;\tsigned:1;\n"
        "\tfield:unsigned long long u8;\toffset:24;\tsize:8;\tsigned:0;\n"
	"\n"
	"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\", "
	"sizeof(int), sizeof(unsigned), sizeof(unsigned int), sizeof(long), sizeof(unsigned long), "
	"sizeof(long long), sizeof(unsigned long long), sizeof(REC->s4), "
	"sizeof(REC->u4), sizeof(REC->s8), sizeof(REC->u8))\n";
static char sizeof_data[] = {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	/* common type */		23, 0x00,
#else
	/* common type */		0x00, 23
#endif
	/* common flags */		0x00,
	/* common_preempt_count */	0x00,
	/* common_pid */		0x00, 0x00, 0x00, 0x00,
	/* irq */			0x00, 0x00, 0x00, 0x00,

	/* s4 */			0x00, 0x00, 0x00, 0x00,
	/* u4 */			0x00, 0x00, 0x00, 0x00,
	/* s8 */			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	/* u8 */			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
static void *sizeof_event_data = (void *)sizeof_data;

DECL_CPUMASK_EVENT_DATA(full, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
#define CPUMASK_FULL     "ARRAY[ff, ff, ff, ff, ff, ff, ff, ff]"
#define CPUMASK_FULL_FMT "cpumask=0-63"

DECL_CPUMASK_EVENT_DATA(empty, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
#define CPUMASK_EMPTY     "ARRAY[00, 00, 00, 00, 00, 00, 00, 00]"
#define CPUMASK_EMPTY_FMT "cpumask="

DECL_CPUMASK_EVENT_DATA(half, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55);
#define CPUMASK_HALF     "ARRAY[55, 55, 55, 55, 55, 55, 55, 55]"
#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"

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
DECL_CPUMASK_EVENT_DATA(bytep1, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
#define CPUMASK_BYTEP1     "ARRAY[01, 80, 00, 00, 00, 00, 00, 00]"
#define CPUMASK_BYTEP1_FMT "cpumask=0,15"

DECL_CPUMASK_EVENT_DATA(bytep2, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00);
#define CPUMASK_BYTEP2     "ARRAY[01, 00, 80, 00, 00, 00, 00, 00]"
#define CPUMASK_BYTEP2_FMT "cpumask=0,23"

DECL_CPUMASK_EVENT_DATA(bytepn, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80);
#define CPUMASK_BYTEPN     "ARRAY[01, 00, 00, 00, 00, 00, 00, 80]"
#define CPUMASK_BYTEPN_FMT "cpumask=0,63"

#else

DECL_CPUMASK_EVENT_DATA(bytep1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01);
#define CPUMASK_BYTEP1     "ARRAY[00, 00, 00, 00, 00, 00, 80, 01]"
#define CPUMASK_BYTEP1_FMT "cpumask=0,15"

DECL_CPUMASK_EVENT_DATA(bytep2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x01);
#define CPUMASK_BYTEP2     "ARRAY[00, 00, 00, 00, 00, 80, 00, 01]"
#define CPUMASK_BYTEP2_FMT "cpumask=0,23"

DECL_CPUMASK_EVENT_DATA(bytepn, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01);
#define CPUMASK_BYTEPN     "ARRAY[80, 00, 00, 00, 00, 00, 80, 01]"
#define CPUMASK_BYTEPN_FMT "cpumask=0,63"
#endif

static struct tep_handle *test_tep;
static struct trace_seq *test_seq;
static struct trace_seq seq_storage;

static void parse_dyn_str(const char *dyn_str, void *data, int size)
{
	struct tep_format_field *field;
	struct tep_event *event;
	struct tep_record record;

	record.data = data;
	record.size = size;

	CU_TEST(tep_parse_format(test_tep, &event,
				 dyn_str, strlen(dyn_str),
				DYN_STR_EVENT_SYSTEM) == TEP_ERRNO__SUCCESS);

	field = tep_find_any_field(event, DYN_STR_FIELD);
	CU_TEST(field != NULL);
	trace_seq_reset(test_seq);
	tep_print_field_content(test_seq, data, size, field);
	CU_TEST(strcmp(test_seq->buffer, DYN_STRING) == 0);

	trace_seq_reset(test_seq);
	tep_print_event(test_tep, test_seq, &record, "%s", TEP_PRINT_INFO);
	trace_seq_do_printf(test_seq);
	CU_TEST(strcmp(test_seq->buffer, DYN_STRING_FMT) == 0);
}

static void test_parse_dyn_str_event(void)
{
	parse_dyn_str(dyn_str_event, dyn_str_event_data, sizeof(dyn_str_data));
}

static void test_parse_dyn_str_old_event(void)
{
	parse_dyn_str(dyn_str_old_event, dyn_str_old_event_data, sizeof(dyn_str_old_data));
}

static void parse_cpumask(const char *format, void *data, int size,
			  const char* expected_raw, const char* expected)
{
	struct tep_format_field *field;
	struct tep_event *event;
	struct tep_record record;

	record.data = data;
	record.size = size;

	CU_TEST(tep_parse_format(test_tep, &event,
				 format, strlen(format),
				 CPUMASK_EVENT_SYSTEM) == TEP_ERRNO__SUCCESS);

	field = tep_find_any_field(event, CPUMASK_EVENT_FIELD);
	CU_TEST(field != NULL);

	trace_seq_reset(test_seq);
	tep_print_field_content(test_seq, data, size, field);
	CU_TEST(strcmp(test_seq->buffer, expected_raw) == 0);

	trace_seq_reset(test_seq);
	tep_print_event(test_tep, test_seq, &record, "%s", TEP_PRINT_INFO);
	trace_seq_do_printf(test_seq);
	CU_TEST(strcmp(test_seq->buffer, expected) == 0);
}

static void test_parse_cpumask_full(void)
{
	parse_cpumask(cpumask_event_format,
		      cpumask_full_event_data, sizeof(cpumask_full_event_data),
		      CPUMASK_FULL, CPUMASK_FULL_FMT);
}

static void test_parse_cpumask_empty(void)
{
	parse_cpumask(cpumask_event_format,
		      cpumask_empty_event_data, sizeof(cpumask_empty_event_data),
		      CPUMASK_EMPTY, CPUMASK_EMPTY_FMT);
}

static void test_parse_cpumask_half(void)
{
	parse_cpumask(cpumask_event_format,
		      cpumask_half_event_data, sizeof(cpumask_half_event_data),
		      CPUMASK_HALF, CPUMASK_HALF_FMT);
}

static void test_parse_cpumask_bytep1(void)
{
	parse_cpumask(cpumask_event_format,
		      cpumask_bytep1_event_data, sizeof(cpumask_bytep1_event_data),
		      CPUMASK_BYTEP1, CPUMASK_BYTEP1_FMT);
}

static void test_parse_cpumask_bytep2(void)
{
	parse_cpumask(cpumask_event_format,
		      cpumask_bytep2_event_data, sizeof(cpumask_bytep2_event_data),
		      CPUMASK_BYTEP2, CPUMASK_BYTEP2_FMT);
}

static void test_parse_cpumask_bytepn(void)
{
	parse_cpumask(cpumask_event_format,
		      cpumask_bytepn_event_data, sizeof(cpumask_bytepn_event_data),
		      CPUMASK_BYTEPN, CPUMASK_BYTEPN_FMT);
}

static void test_parse_sizeof(int long_size, int value, const char *system,
			      const char *test_str)
{
	struct tep_event *event;
	struct tep_record record;
	char *sizeof_event;
	char *p;

	tep_set_long_size(test_tep, long_size);

	record.data = sizeof_event_data;
	record.size = sizeof(sizeof_data);

	sizeof_event = strdup(size_of_event);
	CU_TEST(sizeof_event != NULL);

	/* Set a new id */
	p = strstr(sizeof_event, "ID: 2");
	p[5] = '0' + value;

	/* Handles endianess */
	*(unsigned short *)sizeof_data = 20 + value;

	CU_TEST(tep_parse_format(test_tep, &event, sizeof_event,
				 strlen(sizeof_event),
				 system) == TEP_ERRNO__SUCCESS);

	trace_seq_reset(test_seq);
	tep_print_event(test_tep, test_seq, &record, "%s", TEP_PRINT_INFO);
	trace_seq_do_printf(test_seq);
	CU_TEST(strcmp(test_seq->buffer, test_str) == 0);

	free(sizeof_event);
}

static void test_parse_sizeof8(void)
{
	test_parse_sizeof(8, 3, "sizeof8", SIZEOF_LONG8_FMT);
}

static void test_parse_sizeof4(void)
{
	test_parse_sizeof(4, 4, "sizeof4", SIZEOF_LONG4_FMT);
}

static void test_parse_sizeof_undef(void)
{
	test_parse_sizeof(0, 5, "sizeof_undef", SIZEOF_LONG0_FMT);
}

static int test_suite_destroy(void)
{
	tep_free(test_tep);
	trace_seq_destroy(test_seq);
	return 0;
}

static int test_suite_init(void)
{
	test_seq = &seq_storage;
	trace_seq_init(test_seq);
	test_tep = tep_alloc();
	if (!test_tep)
		return 1;
	return 0;
}

void test_traceevent_lib(void)
{
	CU_pSuite suite = NULL;

	suite = CU_add_suite(TRACEEVENT_SUITE, test_suite_init, test_suite_destroy);
	if (suite == NULL) {
		fprintf(stderr, "Suite \"%s\" cannot be ceated\n", TRACEEVENT_SUITE);
		return;
	}
	CU_add_test(suite, "parse dynamic string event",
		    test_parse_dyn_str_event);
	CU_add_test(suite, "parse old dynamic string event",
		    test_parse_dyn_str_old_event);
	CU_add_test(suite, "parse full cpumask",
		    test_parse_cpumask_full);
	CU_add_test(suite, "parse empty cpumask",
		    test_parse_cpumask_empty);
	CU_add_test(suite, "parse half-filled cpumask",
		    test_parse_cpumask_half);
	CU_add_test(suite, "parse cpumask spanning 2 bytes",
		    test_parse_cpumask_bytep1);
	CU_add_test(suite, "parse cpumask spanning 3 bytes",
		    test_parse_cpumask_bytep2);
	CU_add_test(suite, "parse cpumask spanning all bytes",
		    test_parse_cpumask_bytepn);
	CU_add_test(suite, "parse sizeof() 8byte values",
		    test_parse_sizeof8);
	CU_add_test(suite, "parse sizeof() 4byte values",
		    test_parse_sizeof4);
	CU_add_test(suite, "parse sizeof() no long size defined",
		    test_parse_sizeof_undef);
}