Gen-ust-events: use options instead of arguments
[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 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by the
6 * Free Software Foundation; version 2.1 of the License.
7 *
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #define _LGPL_SOURCE
19 #include <getopt.h>
20 #include <assert.h>
21 #include <arpa/inet.h>
22 #include <fcntl.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <stdbool.h>
32 #include <signal.h>
33 #include <poll.h>
34 #include <errno.h>
35 #include "utils.h"
36 #include "signal-helper.h"
37
38 #define TRACEPOINT_DEFINE
39 #include "tp.h"
40
41 static struct option long_options[] =
42 {
43 /* These options set a flag. */
44 {"iter", required_argument, 0, 'i'},
45 {"wait", required_argument, 0, 'w'},
46 {"sync-after-first-event", required_argument, 0, 'a'},
47 {"sync-before-last-event", required_argument, 0, 'b'},
48 {0, 0, 0, 0}
49 };
50
51 int main(int argc, char **argv)
52 {
53 unsigned int i, netint;
54 int option_index;
55 char option_char;
56 long values[] = { 1, 2, 3 };
57 char text[10] = "test";
58 double dbl = 2.0;
59 float flt = 2222.0;
60 int nr_iter = 100, ret = 0, first_event_file_created = 0;
61 useconds_t nr_usec = 0;
62 char *after_first_event_file_path = NULL;
63 char *before_last_event_file_path = NULL;
64
65 while((option_char = getopt_long(argc, argv, "i:w:a:b:c:d:", long_options, &option_index)) != -1) {
66 switch (option_char) {
67 case 'a':
68 after_first_event_file_path = strdup(optarg);
69 break;
70 case 'b':
71 before_last_event_file_path = strdup(optarg);
72 break;
73 case 'i':
74 nr_iter = atoi(optarg);
75 break;
76 case 'w':
77 nr_usec = atoi(optarg);
78 break;
79 case '?':
80 /* getopt_long already printed an error message. */
81 break;
82 default:
83 ret = -1;
84 goto end;
85 }
86 }
87
88 if (optind != argc) {
89 fprintf(stderr, "Error: takes long options only.");
90 ret = -1;
91 goto end;
92 }
93
94
95 if (set_signal_handler()) {
96 ret = -1;
97 goto end;
98 }
99
100 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
101 if (nr_iter >= 0 && i == nr_iter - 1) {
102 /*
103 * Wait on synchronization before writing last
104 * event.
105 */
106 if (before_last_event_file_path) {
107 ret = wait_on_file(before_last_event_file_path);
108 if (ret != 0) {
109 goto end;
110 }
111 }
112 }
113 netint = htonl(i);
114 tracepoint(tp, tptest, i, netint, values, text,
115 strlen(text), dbl, flt);
116
117 /*
118 * First loop we create the file if asked to indicate
119 * that at least one tracepoint has been hit.
120 */
121 if (after_first_event_file_path && first_event_file_created == 0) {
122 ret = create_file(after_first_event_file_path);
123
124 if (ret != 0) {
125 goto end;
126 } else {
127 first_event_file_created = 1;
128 }
129 }
130
131 if (nr_usec) {
132 if (usleep_safe(nr_usec)) {
133 ret = -1;
134 goto end;
135 }
136 }
137 if (should_quit) {
138 break;
139 }
140 }
141
142 end:
143 free(after_first_event_file_path);
144 free(before_last_event_file_path);
145 exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE);
146 }
This page took 0.047463 seconds and 5 git commands to generate.