tests: trigger action firing policy
[lttng-tools.git] / tests / utils / testapp / gen-ust-events / gen-ust-events.c
1 /*
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <getopt.h>
10 #include <assert.h>
11 #include <arpa/inet.h>
12 #include <fcntl.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/mman.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <stdbool.h>
22 #include <signal.h>
23 #include <poll.h>
24 #include <errno.h>
25 #include "utils.h"
26 #include "signal-helper.h"
27
28 #define TRACEPOINT_DEFINE
29 #include "tp.h"
30
31 static struct option long_options[] =
32 {
33 /* These options set a flag. */
34 {"iter", required_argument, 0, 'i'},
35 {"wait", required_argument, 0, 'w'},
36 {"sync-after-first-event", required_argument, 0, 'a'},
37 {"sync-before-last-event", required_argument, 0, 'b'},
38 {"sync-before-last-event-touch", required_argument, 0, 'c'},
39 {"sync-before-exit", required_argument, 0, 'd'},
40 {"sync-before-exit-touch", required_argument, 0, 'e'},
41 {"emit-end-event", no_argument, 0, 'f'},
42 {0, 0, 0, 0}
43 };
44
45 int main(int argc, char **argv)
46 {
47 unsigned int i, netint;
48 int option_index;
49 int option;
50 long values[] = { 1, 2, 3 };
51 char text[10] = "test";
52 char escape[10] = "\\*";
53 double dbl = 2.0;
54 float flt = 2222.0;
55 uint32_t net_values[] = { 1, 2, 3 };
56 int nr_iter = 100, ret = 0, first_event_file_created = 0;
57 useconds_t nr_usec = 0;
58 char *after_first_event_file_path = NULL;
59 char *before_last_event_file_path = NULL;
60 /*
61 * Touch a file to indicate that all events except one were
62 * generated.
63 */
64 char *before_last_event_file_path_touch = NULL;
65 /* Touch file when we are exiting */
66 char *before_exit_file_path_touch = NULL;
67 /* Wait on file before exiting */
68 char *before_exit_file_path = NULL;
69 /* Emit an end event */
70 bool emit_end_event = false;
71
72 for (i = 0; i < 3; i++) {
73 net_values[i] = htonl(net_values[i]);
74 }
75
76 while ((option = getopt_long(argc, argv, "i:w:a:b:c:d:e:f",
77 long_options, &option_index)) != -1) {
78 switch (option) {
79 case 'a':
80 after_first_event_file_path = strdup(optarg);
81 break;
82 case 'b':
83 before_last_event_file_path = strdup(optarg);
84 break;
85 case 'c':
86 before_last_event_file_path_touch = strdup(optarg);
87 break;
88 case 'd':
89 before_exit_file_path = strdup(optarg);
90 break;
91 case 'e':
92 before_exit_file_path_touch = strdup(optarg);
93 break;
94 case 'f':
95 emit_end_event = true;
96 break;
97 case 'i':
98 nr_iter = atoi(optarg);
99 break;
100 case 'w':
101 nr_usec = atoi(optarg);
102 break;
103 case '?':
104 /* getopt_long already printed an error message. */
105 default:
106 ret = -1;
107 goto end;
108 }
109 }
110
111 if (optind != argc) {
112 fprintf(stderr, "Error: takes long options only.\n");
113
114 /*
115 * Aborting the test program for now because callers typically don't check
116 * the test program return value, and the transition from positional
117 * arguments to getopt causes hangs when caller scripts are not updated.
118 * An abort is easier to diagnose and fix. This is a temporary solution:
119 * we should eventually ensure that all scripts test and report the test
120 * app return values.
121 */
122 abort();
123
124 ret = -1;
125 goto end;
126 }
127
128
129 if (set_signal_handler()) {
130 ret = -1;
131 goto end;
132 }
133
134 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
135 if (nr_iter >= 0 && i == nr_iter - 1) {
136 if (before_last_event_file_path_touch) {
137 ret = create_file(before_last_event_file_path_touch);
138 if (ret != 0) {
139 goto end;
140 }
141 }
142
143 /*
144 * Wait on synchronization before writing last
145 * event.
146 */
147 if (before_last_event_file_path) {
148 ret = wait_on_file(before_last_event_file_path);
149 if (ret != 0) {
150 goto end;
151 }
152 }
153 }
154 netint = htonl(i);
155 tracepoint(tp, tptest, i, netint, values, text,
156 strlen(text), escape, net_values, dbl, flt);
157
158 /*
159 * First loop we create the file if asked to indicate
160 * that at least one tracepoint has been hit.
161 */
162 if (after_first_event_file_path && first_event_file_created == 0) {
163 ret = create_file(after_first_event_file_path);
164
165 if (ret != 0) {
166 goto end;
167 } else {
168 first_event_file_created = 1;
169 }
170 }
171
172 if (nr_usec) {
173 if (usleep_safe(nr_usec)) {
174 ret = -1;
175 goto end;
176 }
177 }
178 if (should_quit) {
179 break;
180 }
181 }
182
183 if (emit_end_event) {
184 tracepoint(tp, end);
185 }
186
187 if (before_exit_file_path_touch) {
188 ret = create_file(before_exit_file_path_touch);
189 if (ret != 0) {
190 goto end;
191 }
192 }
193 if (before_exit_file_path) {
194 ret = wait_on_file(before_exit_file_path);
195 if (ret != 0) {
196 goto end;
197 }
198 }
199 end:
200 free(after_first_event_file_path);
201 free(before_last_event_file_path);
202 free(before_last_event_file_path_touch);
203 free(before_exit_file_path);
204 free(before_exit_file_path_touch);
205 exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE);
206 }
This page took 0.032797 seconds and 4 git commands to generate.