Fix: syscall event rule: emission sites not compared in is_equal
[lttng-tools.git] / tests / utils / testapp / gen-syscall-events / gen-syscall-events.cpp
1 /*
2 * Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include "utils.h"
9
10 #include <common/align.hpp>
11 #include <common/error.hpp>
12
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <sys/syscall.h>
16 #include <unistd.h>
17
18 #define MAX_LEN 16
19
20 /*
21 * The LTTng system call tracing facilities can't handle page faults at the
22 * moment. If a fault would occur while reading a syscall argument, the
23 * tracer will report an empty string (""). Since the proper execution of the
24 * tests which use this generator depends on some syscall string arguments being
25 * present, this util allows us to mitigate the page-fault risk.
26 *
27 * This isn't a proper fix; it is simply the best we can do for now.
28 * See bug #1261 for more context.
29 */
30 static void prefault_string(const char *p)
31 {
32 const char *const end = p + strlen(p) + 1;
33
34 while (p < end) {
35 /*
36 * Trigger a read attempt on *p, faulting-in the pages
37 * for reading.
38 */
39 asm volatile("" : : "m"(*p));
40 p += sysconf(_SC_PAGE_SIZE);
41 }
42 }
43
44 static int open_read_close(const char *path)
45 {
46 int fd, ret;
47 char buf[MAX_LEN];
48
49 /*
50 * Start generating syscalls. We use syscall(2) to prevent libc from
51 * changing the underlying syscall (e.g. calling openat(2) instead of
52 * open(2)).
53 */
54 prefault_string(path);
55 fd = syscall(SYS_openat, AT_FDCWD, path, O_RDONLY);
56 if (fd < 0) {
57 PERROR_NO_LOGGER("Failed to open file with openat(): path = '%s'", path);
58 ret = -1;
59 goto error;
60 }
61
62 ret = syscall(SYS_read, fd, buf, MAX_LEN);
63 if (ret < 0) {
64 PERROR_NO_LOGGER("Failed to read file: path = '%s', fd = %d, length = %d",
65 path,
66 fd,
67 MAX_LEN);
68 ret = -1;
69 goto error;
70 }
71
72 ret = syscall(SYS_close, fd);
73 if (ret == -1) {
74 PERROR_NO_LOGGER("Failed to close file: path = '%s', fd = %d", path, fd);
75 ret = -1;
76 goto error;
77 }
78
79 error:
80 return ret;
81 }
82
83 /*
84 * The process waits for the creation of a file passed as argument from an
85 * external processes to execute a syscall and exiting. This is useful for tests
86 * in combinaison with LTTng's PID tracker feature where we can trace the kernel
87 * events generated by our test process only.
88 */
89 int main(int argc, char **argv)
90 {
91 int ret;
92 const char *start_file, *path1, *path2;
93
94 if (argc != 4) {
95 fprintf(stderr, "Error: Missing argument\n");
96 fprintf(stderr, "USAGE: %s PATH_WAIT_FILE PATH1_TO_OPEN PATH2_TO_OPEN\n", argv[0]);
97 ret = -1;
98 goto error;
99 }
100
101 start_file = argv[1];
102 path1 = argv[2];
103 path2 = argv[3];
104
105 /*
106 * Wait for the start_file to be created by an external process
107 * (typically the test script) before executing the syscalls.
108 */
109 ret = wait_on_file(start_file);
110 if (ret != 0) {
111 goto error;
112 }
113
114 /*
115 * Start generating syscalls. We use syscall(2) to prevent libc to change
116 * the underlying syscall. e.g. calling openat(2) instead of open(2).
117 */
118 ret = open_read_close(path1);
119 if (ret == -1) {
120 ret = -1;
121 goto error;
122 }
123
124 ret = open_read_close(path2);
125 if (ret == -1) {
126 ret = -1;
127 goto error;
128 }
129
130 error:
131 return ret;
132 }
This page took 0.030508 seconds and 4 git commands to generate.