Commit | Line | Data |
---|---|---|
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 |
209b934f | 19 | #include <assert.h> |
7e0cc23b | 20 | #include <arpa/inet.h> |
209b934f | 21 | #include <fcntl.h> |
7e0cc23b CB |
22 | #include <stdarg.h> |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <sys/mman.h> | |
27 | #include <sys/stat.h> | |
28 | #include <sys/types.h> | |
29 | #include <unistd.h> | |
0fc2834c | 30 | #include <stdbool.h> |
5fcaccbc MD |
31 | #include <signal.h> |
32 | #include <poll.h> | |
33 | #include <errno.h> | |
ae941114 | 34 | #include "utils.h" |
901cb87c | 35 | #include "signal-helper.h" |
7e0cc23b CB |
36 | |
37 | #define TRACEPOINT_DEFINE | |
38 | #include "tp.h" | |
39 | ||
209b934f DG |
40 | void create_file(const char *path) |
41 | { | |
5fcaccbc | 42 | static bool file_created = false; |
209b934f DG |
43 | int ret; |
44 | ||
5fcaccbc MD |
45 | if (!path || file_created) { |
46 | return; | |
47 | } | |
209b934f DG |
48 | |
49 | ret = creat(path, S_IRWXU); | |
50 | if (ret < 0) { | |
51 | fprintf(stderr, "Failed to create file %s\n", path); | |
52 | return; | |
53 | } | |
54 | ||
55 | (void) close(ret); | |
5fcaccbc MD |
56 | file_created = true; |
57 | } | |
58 | ||
59 | static | |
60 | void wait_on_file(const char *path) | |
61 | { | |
62 | if (!path) { | |
63 | return; | |
64 | } | |
65 | for (;;) { | |
66 | int ret; | |
67 | struct stat buf; | |
68 | ||
69 | ret = stat(path, &buf); | |
70 | if (ret == -1 && errno == ENOENT) { | |
71 | (void) poll(NULL, 0, 10); /* 10 ms delay */ | |
72 | continue; /* retry */ | |
73 | } | |
74 | if (ret) { | |
75 | perror("stat"); | |
76 | exit(EXIT_FAILURE); | |
77 | } | |
78 | break; /* found */ | |
79 | } | |
209b934f DG |
80 | } |
81 | ||
7e0cc23b CB |
82 | int main(int argc, char **argv) |
83 | { | |
0fc2834c | 84 | unsigned int i, netint; |
7e0cc23b CB |
85 | long values[] = { 1, 2, 3 }; |
86 | char text[10] = "test"; | |
87 | double dbl = 2.0; | |
88 | float flt = 2222.0; | |
ae941114 | 89 | int nr_iter = 100, ret = 0; |
7e0cc23b | 90 | useconds_t nr_usec = 0; |
5fcaccbc MD |
91 | char *after_first_event_file_path = NULL; |
92 | char *before_last_event_file_path = NULL; | |
7e0cc23b | 93 | |
901cb87c JG |
94 | if (set_signal_handler()) { |
95 | ret = -1; | |
96 | goto end; | |
97 | } | |
98 | ||
7e0cc23b | 99 | if (argc >= 2) { |
0fc2834c MD |
100 | /* |
101 | * If nr_iter is negative, do an infinite tracing loop. | |
102 | */ | |
7e0cc23b CB |
103 | nr_iter = atoi(argv[1]); |
104 | } | |
105 | ||
2c34b645 | 106 | if (argc >= 3) { |
7e0cc23b CB |
107 | /* By default, don't wait unless user specifies. */ |
108 | nr_usec = atoi(argv[2]); | |
109 | } | |
110 | ||
2c34b645 | 111 | if (argc >= 4) { |
5fcaccbc MD |
112 | after_first_event_file_path = argv[3]; |
113 | } | |
114 | ||
115 | if (argc >= 5) { | |
116 | before_last_event_file_path = argv[4]; | |
209b934f DG |
117 | } |
118 | ||
0fc2834c | 119 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
5fcaccbc MD |
120 | if (nr_iter >= 0 && i == nr_iter - 1) { |
121 | /* | |
122 | * Wait on synchronization before writing last | |
123 | * event. | |
124 | */ | |
125 | wait_on_file(before_last_event_file_path); | |
126 | } | |
7e0cc23b | 127 | netint = htonl(i); |
5fcaccbc MD |
128 | tracepoint(tp, tptest, i, netint, values, text, |
129 | strlen(text), dbl, flt); | |
209b934f DG |
130 | |
131 | /* | |
5fcaccbc MD |
132 | * First loop we create the file if asked to indicate |
133 | * that at least one tracepoint has been hit. | |
209b934f | 134 | */ |
5fcaccbc | 135 | create_file(after_first_event_file_path); |
b734f5f7 | 136 | if (nr_usec) { |
ae941114 JG |
137 | if (usleep_safe(nr_usec)) { |
138 | ret = -1; | |
139 | goto end; | |
140 | } | |
b734f5f7 | 141 | } |
901cb87c JG |
142 | if (should_quit) { |
143 | break; | |
144 | } | |
7e0cc23b CB |
145 | } |
146 | ||
ae941114 JG |
147 | end: |
148 | exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE); | |
7e0cc23b | 149 | } |