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