File: tracecmd-utest.c

package info (click to toggle)
trace-cmd 3.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,056 kB
  • sloc: ansic: 33,750; makefile: 966; sh: 762; python: 604
file content (818 lines) | stat: -rw-r--r-- 18,095 bytes parent folder | download
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
// SPDX-License-Identifier: LGPL-2.1
/*
 * Copyright (C) 2020, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>

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

#include <trace-cmd.h>

#include "trace-utest.h"

#ifndef PATH_MAX
#define PATH_MAX 1024
#endif

static char tracecmd_exec[PATH_MAX];

#define TRACECMD_SUITE		"trace-cmd"
#define TRACECMD_FILE		"__trace_test__.dat"
#define TRACECMD_FILE2		"__trace_test__2.dat"
#define TRACECMD_OUT		"-o", TRACECMD_FILE
#define TRACECMD_OUT2		"-o", TRACECMD_FILE2
#define TRACECMD_IN		"-i", TRACECMD_FILE
#define TRACECMD_IN2		"-i", TRACECMD_FILE2

#define TRACECMD_SQL_HIST	"SELECT irq FROM irq_handler_entry"
#define TRACECMD_SQL_READ_HIST	"show", "--hist", "irq_handler_entry"

#define SYNTH_EVENT		"wakeup"
#define TRACECMD_SQL_SYNTH	"-e", "-n", SYNTH_EVENT, "SELECT start.pid AS this_pid, (end.TIMESTAMP_USECS - start.TIMESTAMP_USECS) AS delta FROM sched_waking as start JOIN sched_switch AS end ON start.pid = end.next_pid"
#define TRACECMD_SQL_START_SYNTH "start", "-e", SYNTH_EVENT

static char **get_args(const char *cmd, va_list ap)
{
	const char *param;
	char **argv;
	char **tmp;

	argv = tracefs_list_add(NULL, tracecmd_exec);
	if (!argv)
		return NULL;

	tmp = tracefs_list_add(argv, cmd);
	if (!tmp)
		goto fail;
	argv = tmp;

	for (param = va_arg(ap, const char *);
	     param; param = va_arg(ap, const char *)) {
		tmp = tracefs_list_add(argv, param);
		if (!tmp)
			goto fail;
		argv = tmp;
	}

	return argv;
 fail:
	tracefs_list_free(argv);
	return NULL;
}

static void silent_output(void)
{
	close(STDOUT_FILENO);
	open("/dev/null", O_WRONLY);
	close(STDERR_FILENO);
	open("/dev/null", O_WRONLY);
}

static int wait_for_exec(int pid)
{
	int status;
	int ret;

	ret = waitpid(pid, &status, 0);
	if (ret != pid)
		return -1;

	return WEXITSTATUS(status) ? -1 : 0;
}

static int run_trace(const char *cmd, ...)
{
	char **argv;
	va_list ap;
	int ret = -1;
	pid_t pid;

	va_start(ap, cmd);
	argv = get_args(cmd, ap);
	va_end(ap);

	if (!argv)
		return -1;

	pid = fork();
	if (pid < 0)
		goto out;

	if (!pid) {
		if (!show_output)
			silent_output();
		ret = execvp(tracecmd_exec, argv);
		exit (ret);
	}

	ret = wait_for_exec(pid);
 out:
	tracefs_list_free(argv);
	return ret;
}

static int pipe_it(int *ofd, int *efd, int (*func)(void *),
		   void *data)
{
	int obrass[2];
	int ebrass[2];
	pid_t pid;
	int ret;

	if (pipe(obrass) < 0)
		return -1;

	if (pipe(ebrass) < 0)
		goto fail_out;

	pid = fork();
	if (pid < 0)
		goto fail;

	if (!pid) {
		char shret[32];

		close(obrass[0]);
		close(STDOUT_FILENO);
		if (dup2(obrass[1], STDOUT_FILENO) < 0)
			exit(-1);

		close(ebrass[0]);
		close(STDERR_FILENO);
		if (dup2(obrass[1], STDERR_FILENO) < 0)
			exit(-1);

		ret = func(data);

		/*
		 * valgrind triggers its reports when the application
		 * exits. If the application does a fork() and the child
		 * exits, it will still trigger the valgrind report for
		 * all the allocations that were not freed by the parent.
		 *
		 * To prevent valgrind from triggering, do an execl() on
		 * a basic shell that will simply exit with the return value.
		 * This will quiet valgrind from reporting memory that has
		 * been allocated by the parent up to here.
		 */
		snprintf(shret, 32, "exit %d", ret);
		execl("/usr/bin/sh", "/usr/bin/sh", "-c", shret, NULL);
		execl("/bin/sh", "/bin/sh", "-c", shret, NULL);

		/* If the above execl() fails, simply do an exit */
		exit(ret);
	}

	close(obrass[1]);
	close(ebrass[1]);

	*ofd = obrass[0];
	*efd = ebrass[0];

	return pid;

 fail:
	close(ebrass[0]);
	close(ebrass[1]);
 fail_out:
	close(obrass[0]);
	close(obrass[1]);
	return -1;
}

struct do_grep {
	const char		*cmd;
	va_list			*ap;
};

static int do_grep(void *data)
{
	struct do_grep *gdata = data;
	char **argv;
	int ret;

	argv = get_args(gdata->cmd, *gdata->ap);
	if (!argv)
		exit(-1);

	ret = execvp(tracecmd_exec, argv);
	tracefs_list_free(argv);
	return ret;
}

struct do_grep_it {
	const char		*match;
	const char		*cmd;
	va_list			*ap;
};

static int do_grep_it(void *data)
{
	struct do_grep_it *dgdata = data;
	struct do_grep gdata;
	FILE *fp;
	regex_t reg;
	char *buf = NULL;
	ssize_t n;
	size_t l = 0;
	int ofd;
	int efd;
	int pid;
	int ret;

	if (regcomp(&reg, dgdata->match, REG_ICASE|REG_NOSUB))
		return -1;

	gdata.cmd = dgdata->cmd;
	gdata.ap = dgdata->ap;
	pid = pipe_it(&ofd, &efd, do_grep, &gdata);

	if (pid < 0) {
		regfree(&reg);
		return -1;
	}

	fp = fdopen(ofd, "r");
	if (!fp)
		goto out;

	do {
		n = getline(&buf, &l, fp);
		if (n > 0 && regexec(&reg, buf, 0, NULL, 0) == 0)
			printf("%s", buf);
	} while (n >= 0);

	free(buf);
 out:
	ret = wait_for_exec(pid);
	if (fp)
		fclose(fp);
	else
		perror("fp");
	close(ofd);
	close(efd);
	regfree(&reg);

	return ret > 0 ? 0 : ret;
}

struct do_grep_match {
	const char		*match;
	const char		*cmd;
	va_list			*ap;
};

static int grep_match(const char *match, const char *cmd, ...)
{
	struct do_grep_it gdata;
	FILE *fp;
	va_list ap;
	char *buf = NULL;
	ssize_t n;
	size_t l = 0;
	bool found = false;
	int ofd;
	int efd;
	int pid;
	int ret;

	va_start(ap, cmd);
	gdata.match = match;
	gdata.cmd = cmd;
	gdata.ap = &ap;
	pid = pipe_it(&ofd, &efd, do_grep_it, &gdata);
	va_end(ap);

	if (pid < 0)
		return -1;

	fp = fdopen(ofd, "r");
	if (!fp)
		goto out;

	do {
		n = getline(&buf, &l, fp);
		if (n > 0) {
			if (show_output)
				printf("%s", buf);
			found = true;
		}
	} while (n >= 0);

	free(buf);
 out:
	ret = wait_for_exec(pid);
	if (ret)
		n = 1;
	if (fp)
		fclose(fp);
	else {
		perror("fp");
		close(ofd);
	}
	close(efd);

	return found ? 0 : 1;
}

static void test_trace_record_report(void)
{
	int ret;

	ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
	CU_TEST(ret == 0);
	ret = run_trace("convert", "--file-version", "6", TRACECMD_IN, TRACECMD_OUT2, NULL);
	CU_TEST(ret == 0);
}

static void test_trace_sqlhist_hist(void)
{
	int ret;

	ret = run_trace("sqlhist", "-e", TRACECMD_SQL_HIST, NULL);
	CU_TEST(ret == 0);
	ret = grep_match(" *Hits: [0-9][0-9]*", TRACECMD_SQL_READ_HIST, NULL);
	CU_TEST(ret == 0);
	ret = run_trace("sqlhist", TRACECMD_SQL_SYNTH, NULL);
	CU_TEST(ret == 0);
	ret = run_trace(TRACECMD_SQL_START_SYNTH, NULL);
	CU_TEST(ret == 0);
	sleep(1);
	ret = grep_match(SYNTH_EVENT ":", "show", NULL);
	CU_TEST(ret == 0);
	/* Ensure synthetic events remain untouched after "trace-cmd reset -k synth". */
	ret = run_trace("reset", "-k", "synth", NULL);
	CU_TEST(ret == 0);
	ret = grep_match(SYNTH_EVENT, "stat", NULL);
	CU_TEST(ret == 0);

	tracefs_instance_reset(NULL);
}

static int read_stats(const char *out, const char *match, const char *cmd, ...)
{
	struct do_grep_it gdata;
	FILE *fp;
	va_list ap;
	bool found = false;
	char *buf = NULL;
	char *p;
	ssize_t n;
	size_t l = 0;
	int ofd;
	int efd;
	int pid;
	int ret;
	int val;

	va_start(ap, cmd);
	gdata.match = match;
	gdata.cmd = cmd;
	gdata.ap = &ap;
	pid = pipe_it(&ofd, &efd, do_grep_it, &gdata);
	va_end(ap);

	if (pid < 0)
		return -1;

	fp = fdopen(ofd, "r");
	if (!fp)
		goto out;

	do {
		n = getline(&buf, &l, fp);
		if (n > 0) {
			for (p = buf; isspace(*p); p++)
				;
			val = atoi(p);
			found = true;
			if (show_output)
				printf("%s", buf);
			CU_TEST(val < 10000000);
		}
	} while (n >= 0);

	free(buf);
 out:
	ret = wait_for_exec(pid);
	if (fp)
		fclose(fp);
	else {
		perror("fp");
	}
	if (!found)
		ret = -1;
	close(ofd);
	close(efd);
	return ret > 0 ? 0 : ret;
}

static void test_trace_record_max(void)
{
	int ret;

	ret = run_trace("record", TRACECMD_OUT, "-p", "function", "-m", "5000",
			"sleep", "10", NULL);
	CU_TEST(ret == 0);

	ret = read_stats(TRACECMD_FILE, ".*bytes in size.*", "report", TRACECMD_IN, "--stat", NULL);
	CU_TEST(ret == 0);
}

static void test_trace_convert6(void)
{
	struct stat st;
	int ret;

	/* If the trace data is already created, just use it, otherwise make it again */
	if (stat(TRACECMD_FILE, &st) < 0) {
		ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
		CU_TEST(ret == 0);
	}
	ret = grep_match("[ \t]6[ \t]*\\[Version\\]", "dump", TRACECMD_IN2, NULL);
	CU_TEST(ret == 0);
}

struct callback_data {
	long			counter;
	struct trace_seq	seq;
};

static int read_events(struct tracecmd_input *handle, struct tep_record *record,
		       int cpu, void *data)
{
	struct tep_handle *tep = tracecmd_get_tep(handle);
	struct callback_data *cd = data;
	struct trace_seq *seq = &cd->seq;

	cd->counter++;

	trace_seq_reset(seq);
	tep_print_event(tep, seq, record, "%6.1000d", TEP_PRINT_TIME);
	trace_seq_printf(seq, " [%03d] ", cpu);
	tep_print_event(tep, seq, record, "%s-%d %s %s\n",
			TEP_PRINT_COMM, TEP_PRINT_PID,
			TEP_PRINT_NAME, TEP_PRINT_INFO);
	if (show_output)
		trace_seq_do_printf(seq);
	return 0;
}

static int read_events_10(struct tracecmd_input *handle, struct tep_record *record,
			  int cpu, void *data)
{
	struct callback_data *cd = data;

	read_events(handle, record, cpu, data);
	return  cd->counter < 10 ? 0 : 1;
}

static void test_trace_library_read(void)
{
	struct tracecmd_input *handle;
	struct callback_data data;
	struct stat st;
	int ret;

	data.counter = 0;
	trace_seq_init(&data.seq);

	/* If the trace data is already created, just use it, otherwise make it again */
	if (stat(TRACECMD_FILE, &st) < 0) {
		ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
		CU_TEST(ret == 0);
	}

	handle = tracecmd_open(TRACECMD_FILE, 0);
	CU_TEST(handle != NULL);
	ret = tracecmd_iterate_events(handle, NULL, 0, read_events, &data);
	CU_TEST(ret == 0);

	tracecmd_close(handle);

	CU_TEST(data.counter > 0);
	trace_seq_destroy(&data.seq);
}

static void test_trace_library_read_inc(void)
{
	struct tracecmd_input *handle;
	struct callback_data data;
	struct stat st;
	long save_count;
	long total = 0;
	int ret;

	data.counter = 0;
	trace_seq_init(&data.seq);

	/* If the trace data is already created, just use it, otherwise make it again */
	if (stat(TRACECMD_FILE, &st) < 0) {
		ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
		CU_TEST(ret == 0);
	}

	/* First read all again */
	handle = tracecmd_open(TRACECMD_FILE, 0);
	CU_TEST(handle != NULL);
	ret = tracecmd_iterate_events(handle, NULL, 0, read_events, &data);
	CU_TEST(ret == 0);
	CU_TEST(data.counter > 0);

	/* Save the counter */
	save_count = data.counter;

	tracecmd_iterate_reset(handle);

	/* Read 10 at a time */
	do {
		data.counter = 0;
		ret = tracecmd_iterate_events(handle, NULL, 0, read_events_10, &data);
		CU_TEST(ret >= 0);
		CU_TEST(data.counter <= 10);
		total += data.counter;
	} while (data.counter);
	CU_TEST(ret == 0);

	CU_TEST(total == save_count);

	trace_seq_destroy(&data.seq);
	tracecmd_close(handle);
}

static void test_trace_library_read_back(void)
{
	struct tracecmd_input *handle;
	struct callback_data data;
	struct stat st;
	long save_count;
	int ret;

	data.counter = 0;
	trace_seq_init(&data.seq);

	/* If the trace data is already created, just use it, otherwise make it again */
	if (stat(TRACECMD_FILE, &st) < 0) {
		ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
		CU_TEST(ret == 0);
	}

	/* First read all again */
	handle = tracecmd_open(TRACECMD_FILE, 0);
	CU_TEST(handle != NULL);
	ret = tracecmd_iterate_events(handle, NULL, 0, read_events, &data);
	CU_TEST(ret == 0);
	CU_TEST(data.counter > 0);

	/* Save the counter */
	save_count = data.counter;

	tracecmd_iterate_reset(handle);

	/* Read backwards */
	data.counter = 0;
	ret = tracecmd_iterate_events_reverse(handle, NULL, 0, read_events, &data, false);
	CU_TEST(ret == 0);
	CU_TEST(data.counter == save_count);

	/* Read forward again */
	data.counter = 0;
	ret = tracecmd_iterate_events(handle, NULL, 0, read_events, &data);
	CU_TEST(ret == 0);
	CU_TEST(data.counter == save_count);

	/* Read backwards from where we left off */
	data.counter = 0;
	ret = tracecmd_iterate_events_reverse(handle, NULL, 0, read_events, &data, true);
	CU_TEST(ret == 0);
	CU_TEST(data.counter == save_count);

	trace_seq_destroy(&data.seq);
	tracecmd_close(handle);
}

static void test_trace_reset_kprobe(void)
{
	int ret;

	/* Create a simple kprobe for do_sys_open */
	ret = tracefs_instance_file_write(NULL, "kprobe_events", "p do_sys_open");
	CU_TEST(ret > 0);

	/* Ensure the kprobe is listed in "trace-cmd stat" output. */
	ret = grep_match("p:kprobes/p_do_sys_open_0 do_sys_open", "stat", NULL);
	CU_TEST(ret == 0);

	/* Issue "trace-cmd reset", but keep kprobes. */
	ret = run_trace("reset", "-k", "kprobe", NULL);
	CU_TEST(ret == 0);

	/* Verify the kprobe's existence after reset. */
	ret = grep_match("p:kprobes/p_do_sys_open_0 do_sys_open", "stat", NULL);
	CU_TEST(ret == 0);
}

static void test_trace_reset_kretprobe(void)
{
	int ret;

	/* Create a simple kretprobe for do_sys_open */
	ret = tracefs_instance_file_write(NULL, "kprobe_events", "r do_sys_open");
	CU_TEST(ret > 0);

	/* Ensure the kretprobe is listed in "trace-cmd stat" output. */
	ret = grep_match("r[0-9]*:kprobes/r_do_sys_open_0 do_sys_open", "stat", NULL);
	CU_TEST(ret == 0);

	/* Issue "trace-cmd reset", but keep kretprobes. */
	ret = run_trace("reset", "-k", "kretprobe", NULL);
	CU_TEST(ret == 0);

	/* Verify the kretprobe's existence after reset. */
	ret = grep_match("r[0-9]*:kprobes/r_do_sys_open_0 do_sys_open", "stat", NULL);
	CU_TEST(ret == 0);
}

static void test_trace_reset_uprobe(void)
{
	int ret;

	/* Create a simple uprobe for do_sys_open */
	ret = tracefs_instance_file_write(NULL, "uprobe_events", "p /bin/bash:0x4245c0");
	CU_TEST(ret > 0);

	/* Ensure the uprobe is listed in "trace-cmd stat" output. */
	ret = grep_match("p:uprobes/p_bash_0x4245c0 /bin/bash:0x00000000004245c0", "stat", NULL);
	CU_TEST(ret == 0);

	/* Issue "trace-cmd reset", but keep uprobes. */
	ret = run_trace("reset", "-k", "uprobe", NULL);
	CU_TEST(ret == 0);

	/* Verify the uprobe's existence after reset. */
	ret = grep_match("p:uprobes/p_bash_0x4245c0 /bin/bash:0x00000000004245c0", "stat", NULL);
	CU_TEST(ret == 0);
}

static void test_trace_reset_uretprobe(void)
{
	int ret;

	/* Create a simple uretprobe for do_sys_open */
	ret = tracefs_instance_file_write(NULL, "uprobe_events", "r /bin/bash:0x4245c0");
	CU_TEST(ret > 0);

	/* Ensure the uretprobe is listed in "trace-cmd stat" output. */
	ret = grep_match("r:uprobes/p_bash_0x4245c0 /bin/bash:0x00000000004245c0", "stat", NULL);
	CU_TEST(ret == 0);

	/* Issue "trace-cmd reset", but keep uretprobes. */
	ret = run_trace("reset", "-k", "uretprobe", NULL);
	CU_TEST(ret == 0);

	/* Verify the uretprobe's existence after reset. */
	ret = grep_match("r:uprobes/p_bash_0x4245c0 /bin/bash:0x00000000004245c0", "stat", NULL);
	CU_TEST(ret == 0);
}

static void test_trace_reset_eprobe(void)
{
	int fd;
	bool matched = false;
	size_t l = 0;
	ssize_t n;
	char *buf = NULL;
	struct tracefs_dynevent *deprobe;
	FILE *fp;

	deprobe = tracefs_eprobe_alloc(NULL, "sopen_in", "syscalls", "sys_enter_openat", NULL);
	CU_TEST(deprobe != NULL);

	CU_TEST(tracefs_dynevent_create(deprobe) == 0);

	/* Issue "trace-cmd reset", but keep eprobes. */
	CU_TEST(run_trace("reset", "-k", "eprobe", NULL) == 0);

	/* Verify the eprobe's existence after reset. */
	fd = tracefs_instance_file_open(NULL, "dynamic_events", O_RDONLY);
	CU_TEST(fd != -1);
	fp = fdopen(fd, "r");
	CU_TEST(fp != NULL);

	while ((n = getline(&buf, &l, fp)) != -1) {
		if (!strcmp(buf, "e:eprobes/sopen_in syscalls.sys_enter_openat\n")) {
			matched = true;
			break;
		}
	}
	free(buf);

	fclose(fp);

	CU_TEST(matched == true);

	CU_TEST(tracefs_dynevent_destroy(deprobe, false) == 0);

	tracefs_dynevent_free(deprobe);
}

static void test_trace_reset(void)
{
	char *str;

	test_trace_reset_kprobe();
	test_trace_reset_kretprobe();
	test_trace_reset_uprobe();
	test_trace_reset_uretprobe();
	test_trace_reset_eprobe();

	/* Destroy all dynamic events. */
	CU_TEST(run_trace("reset", NULL) == 0);

	/* Paranoia check since "trace-cmd reset" may tell porkies. */
	str = tracefs_instance_file_read(NULL, "dynamic_events", NULL);
	CU_TEST(str == NULL);
	if (str)
		free(str);
}

static int test_suite_destroy(void)
{
	unlink(TRACECMD_FILE);
	unlink(TRACECMD_FILE2);
	return 0;
}

static int find_in_tree_tracecmd_exec(void)
{
	struct stat st;
	const char *p;

	/* The test must be in the utest directory */
	for (p = argv0 + strlen(argv0) - 1; p > argv0 && *p != '/'; p--)
		;

	if (*p == '/')
		snprintf(tracecmd_exec, PATH_MAX, "%.*s/../tracecmd/trace-cmd",
			 (int)(p - argv0), argv0);
	else
		strncpy(tracecmd_exec, "../tracecmd/trace-cmd", PATH_MAX);

	if (stat(tracecmd_exec, &st) < 0) {
		fprintf(stderr, "In tree trace-cmd executable not found\n");
		return 1;
	}

	if (!(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
		fprintf(stderr, "In tree trace-cmd executable not executable\n");
		return 1;
	}

	return 0;
}

static int test_suite_init(void)
{
        if (find_in_tree_tracecmd_exec() != 0)
                strncpy(tracecmd_exec, "/usr/bin/trace-cmd", sizeof(tracecmd_exec));

        return 0;
}

void test_tracecmd_lib(void)
{
	CU_pSuite suite = NULL;

	suite = CU_add_suite(TRACECMD_SUITE, test_suite_init, test_suite_destroy);
	if (suite == NULL) {
		fprintf(stderr, "Suite \"%s\" cannot be created\n", TRACECMD_SUITE);
		return;
	}
	CU_add_test(suite, "Simple record and report",
		    test_trace_record_report);
	CU_add_test(suite, "Create a histogram",
		    test_trace_sqlhist_hist);
	CU_add_test(suite, "Test convert from v7 to v6",
		    test_trace_convert6);
	CU_add_test(suite, "Use libraries to read file",
		    test_trace_library_read);
	CU_add_test(suite, "Use libraries to read file incremental",
		    test_trace_library_read_inc);
	CU_add_test(suite, "Use libraries to read file backwards",
		    test_trace_library_read_back);
	CU_add_test(suite, "Test max length",
		    test_trace_record_max);
	CU_add_test(suite, "Simple reset", test_trace_reset);
}