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