tests: gen-ust-events: use options instead of arguments
[lttng-tools.git] / tests / utils / testapp / gen-ust-events / gen-ust-events.c
CommitLineData
7e0cc23b
CB
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
70dac0d7 18#define _LGPL_SOURCE
6c4a91d6 19#include <getopt.h>
209b934f 20#include <assert.h>
7e0cc23b 21#include <arpa/inet.h>
209b934f 22#include <fcntl.h>
7e0cc23b
CB
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>
0fc2834c 31#include <stdbool.h>
5fcaccbc
MD
32#include <signal.h>
33#include <poll.h>
34#include <errno.h>
ae941114 35#include "utils.h"
95983a02 36#include "signal-helper.h"
7e0cc23b
CB
37
38#define TRACEPOINT_DEFINE
39#include "tp.h"
40
6c4a91d6
MD
41static 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
7e0cc23b
CB
51int main(int argc, char **argv)
52{
0fc2834c 53 unsigned int i, netint;
6c4a91d6
MD
54 int option_index;
55 int option;
7e0cc23b
CB
56 long values[] = { 1, 2, 3 };
57 char text[10] = "test";
58 double dbl = 2.0;
59 float flt = 2222.0;
b2047add 60 int nr_iter = 100, ret = 0, first_event_file_created = 0;
7e0cc23b 61 useconds_t nr_usec = 0;
5fcaccbc
MD
62 char *after_first_event_file_path = NULL;
63 char *before_last_event_file_path = NULL;
7e0cc23b 64
6c4a91d6
MD
65 while ((option = getopt_long(argc, argv, "i:w:a:b:c:d:",
66 long_options, &option_index)) != -1) {
67 switch (option) {
68 case 'a':
69 after_first_event_file_path = strdup(optarg);
70 break;
71 case 'b':
72 before_last_event_file_path = strdup(optarg);
73 break;
74 case 'i':
75 nr_iter = atoi(optarg);
76 break;
77 case 'w':
78 nr_usec = atoi(optarg);
79 break;
80 case '?':
81 /* getopt_long already printed an error message. */
82 break;
83 default:
84 ret = -1;
85 goto end;
86 }
7e0cc23b
CB
87 }
88
6c4a91d6
MD
89 if (optind != argc) {
90 fprintf(stderr, "Error: takes long options only.\n");
91 ret = -1;
92 goto end;
7e0cc23b
CB
93 }
94
5fcaccbc 95
6c4a91d6
MD
96 if (set_signal_handler()) {
97 ret = -1;
98 goto end;
209b934f
DG
99 }
100
0fc2834c 101 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
5fcaccbc
MD
102 if (nr_iter >= 0 && i == nr_iter - 1) {
103 /*
104 * Wait on synchronization before writing last
105 * event.
106 */
b2047add
FD
107 if (before_last_event_file_path) {
108 ret = wait_on_file(before_last_event_file_path);
109 if (ret != 0) {
110 goto end;
111 }
112 }
5fcaccbc 113 }
7e0cc23b 114 netint = htonl(i);
5fcaccbc
MD
115 tracepoint(tp, tptest, i, netint, values, text,
116 strlen(text), dbl, flt);
209b934f
DG
117
118 /*
5fcaccbc
MD
119 * First loop we create the file if asked to indicate
120 * that at least one tracepoint has been hit.
209b934f 121 */
b2047add
FD
122 if (after_first_event_file_path && first_event_file_created == 0) {
123 ret = create_file(after_first_event_file_path);
124
125 if (ret != 0) {
126 goto end;
127 } else {
128 first_event_file_created = 1;
129 }
130 }
131
b734f5f7 132 if (nr_usec) {
ae941114
JG
133 if (usleep_safe(nr_usec)) {
134 ret = -1;
135 goto end;
136 }
b734f5f7 137 }
95983a02
JG
138 if (should_quit) {
139 break;
140 }
7e0cc23b
CB
141 }
142
ae941114 143end:
6c4a91d6
MD
144 free(after_first_event_file_path);
145 free(before_last_event_file_path);
ae941114 146 exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE);
7e0cc23b 147}
This page took 0.05736 seconds and 5 git commands to generate.