testapp: gen-ust-events: augment captured fields
[lttng-tools.git] / tests / utils / testapp / gen-ust-events / gen-ust-events.c
CommitLineData
7e0cc23b 1/*
9d16b343 2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
7e0cc23b 3 *
9d16b343 4 * SPDX-License-Identifier: LGPL-2.1-only
7e0cc23b 5 *
7e0cc23b
CB
6 */
7
70dac0d7 8#define _LGPL_SOURCE
6c4a91d6 9#include <getopt.h>
209b934f 10#include <assert.h>
7e0cc23b 11#include <arpa/inet.h>
209b934f 12#include <fcntl.h>
7e0cc23b
CB
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>
0fc2834c 21#include <stdbool.h>
5fcaccbc
MD
22#include <signal.h>
23#include <poll.h>
24#include <errno.h>
ae941114 25#include "utils.h"
95983a02 26#include "signal-helper.h"
7e0cc23b
CB
27
28#define TRACEPOINT_DEFINE
29#include "tp.h"
30
6c4a91d6
MD
31static 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'},
4e88fe0a
JR
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'},
6c4a91d6
MD
41 {0, 0, 0, 0}
42};
43
7e0cc23b
CB
44int main(int argc, char **argv)
45{
0fc2834c 46 unsigned int i, netint;
6c4a91d6
MD
47 int option_index;
48 int option;
7e0cc23b
CB
49 long values[] = { 1, 2, 3 };
50 char text[10] = "test";
6f9f5cd0 51 char escape[10] = "\\*";
7e0cc23b
CB
52 double dbl = 2.0;
53 float flt = 2222.0;
6f9f5cd0 54 uint32_t net_values[] = { 1, 2, 3 };
b2047add 55 int nr_iter = 100, ret = 0, first_event_file_created = 0;
7e0cc23b 56 useconds_t nr_usec = 0;
5fcaccbc
MD
57 char *after_first_event_file_path = NULL;
58 char *before_last_event_file_path = NULL;
4e88fe0a
JR
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;
7e0cc23b 68
6f9f5cd0
JR
69 for (i = 0; i < 3; i++) {
70 net_values[i] = htonl(net_values[i]);
71 }
72
6c4a91d6
MD
73 while ((option = getopt_long(argc, argv, "i:w:a:b:c:d:",
74 long_options, &option_index)) != -1) {
75 switch (option) {
76 case 'a':
77 after_first_event_file_path = strdup(optarg);
78 break;
79 case 'b':
80 before_last_event_file_path = strdup(optarg);
81 break;
4e88fe0a
JR
82 case 'c':
83 before_last_event_file_path_touch = strdup(optarg);
84 break;
85 case 'd':
86 before_exit_file_path = strdup(optarg);
87 break;
88 case 'e':
89 before_exit_file_path_touch = strdup(optarg);
90 break;
6c4a91d6
MD
91 case 'i':
92 nr_iter = atoi(optarg);
93 break;
94 case 'w':
95 nr_usec = atoi(optarg);
96 break;
97 case '?':
98 /* getopt_long already printed an error message. */
6c4a91d6
MD
99 default:
100 ret = -1;
101 goto end;
102 }
7e0cc23b
CB
103 }
104
6c4a91d6
MD
105 if (optind != argc) {
106 fprintf(stderr, "Error: takes long options only.\n");
56d48389
MD
107
108 /*
109 * Aborting the test program for now because callers typically don't check
110 * the test program return value, and the transition from positional
111 * arguments to getopt causes hangs when caller scripts are not updated.
112 * An abort is easier to diagnose and fix. This is a temporary solution:
113 * we should eventually ensure that all scripts test and report the test
114 * app return values.
115 */
116 abort();
117
6c4a91d6
MD
118 ret = -1;
119 goto end;
7e0cc23b
CB
120 }
121
5fcaccbc 122
6c4a91d6
MD
123 if (set_signal_handler()) {
124 ret = -1;
125 goto end;
209b934f
DG
126 }
127
0fc2834c 128 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
5fcaccbc 129 if (nr_iter >= 0 && i == nr_iter - 1) {
4e88fe0a
JR
130 if (before_last_event_file_path_touch) {
131 ret = create_file(before_last_event_file_path_touch);
132 if (ret != 0) {
133 goto end;
134 }
135 }
136
5fcaccbc
MD
137 /*
138 * Wait on synchronization before writing last
139 * event.
140 */
b2047add
FD
141 if (before_last_event_file_path) {
142 ret = wait_on_file(before_last_event_file_path);
143 if (ret != 0) {
144 goto end;
145 }
146 }
5fcaccbc 147 }
7e0cc23b 148 netint = htonl(i);
5fcaccbc 149 tracepoint(tp, tptest, i, netint, values, text,
6f9f5cd0 150 strlen(text), escape, net_values, dbl, flt);
209b934f
DG
151
152 /*
5fcaccbc
MD
153 * First loop we create the file if asked to indicate
154 * that at least one tracepoint has been hit.
209b934f 155 */
b2047add
FD
156 if (after_first_event_file_path && first_event_file_created == 0) {
157 ret = create_file(after_first_event_file_path);
158
159 if (ret != 0) {
160 goto end;
161 } else {
162 first_event_file_created = 1;
163 }
164 }
165
b734f5f7 166 if (nr_usec) {
ae941114
JG
167 if (usleep_safe(nr_usec)) {
168 ret = -1;
169 goto end;
170 }
b734f5f7 171 }
95983a02
JG
172 if (should_quit) {
173 break;
174 }
7e0cc23b
CB
175 }
176
4e88fe0a
JR
177 if (before_exit_file_path_touch) {
178 ret = create_file(before_exit_file_path_touch);
179 if (ret != 0) {
180 goto end;
181 }
182 }
183 if (before_exit_file_path) {
184 ret = wait_on_file(before_exit_file_path);
185 if (ret != 0) {
186 goto end;
187 }
188 }
ae941114 189end:
6c4a91d6
MD
190 free(after_first_event_file_path);
191 free(before_last_event_file_path);
4e88fe0a
JR
192 free(before_last_event_file_path_touch);
193 free(before_exit_file_path);
194 free(before_exit_file_path_touch);
ae941114 195 exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE);
7e0cc23b 196}
This page took 0.07156 seconds and 4 git commands to generate.