Commit | Line | Data |
---|---|---|
c39c72ee PMF |
1 | /* Copyright (C) 2009 Pierre-Marc Fournier |
2 | * | |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
872037bb PMF |
18 | #define _GNU_SOURCE |
19 | #include <stdio.h> | |
fbd8191b PMF |
20 | #include <unistd.h> |
21 | #include <getopt.h> | |
22 | #include <stdlib.h> | |
fbd8191b | 23 | #include <fcntl.h> |
fbd8191b | 24 | |
f9e5ce61 | 25 | #include "ustcomm.h" |
ae937656 PP |
26 | #include "ustcmd.h" |
27 | ||
28 | enum command { | |
29 | START_TRACE, | |
30 | STOP_TRACE, | |
31 | START, | |
32 | DESTROY, | |
33 | LIST_MARKERS, | |
34 | ENABLE_MARKER, | |
35 | DISABLE_MARKER, | |
36 | GET_ONLINE_PIDS, | |
37 | UNKNOWN | |
38 | }; | |
fbd8191b | 39 | |
52c51a47 | 40 | struct ust_opts { |
ae937656 | 41 | enum command cmd; |
52c51a47 | 42 | pid_t *pids; |
ae937656 | 43 | char* m_name; |
52c51a47 PMF |
44 | }; |
45 | ||
fd2fb4f9 PMF |
46 | char *progname = NULL; |
47 | ||
48 | void usage(void) | |
49 | { | |
ae937656 | 50 | fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname); |
fd2fb4f9 PMF |
51 | fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\ |
52 | \n\ | |
53 | Commands:\n\ | |
54 | --start-trace\t\t\tStart tracing\n\ | |
55 | --stop-trace\t\t\tStop tracing\n\ | |
56 | --destroy-trace\t\t\tDestroy the trace\n\ | |
ae937656 PP |
57 | --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\ |
58 | --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\ | |
59 | --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\ | |
60 | \ | |
fd2fb4f9 PMF |
61 | "); |
62 | } | |
63 | ||
52c51a47 | 64 | int parse_opts_long(int argc, char **argv, struct ust_opts *opts) |
fbd8191b | 65 | { |
52c51a47 | 66 | int c; |
52c51a47 | 67 | |
52c51a47 | 68 | opts->pids = NULL; |
ae937656 | 69 | opts->m_name = NULL; |
52c51a47 PMF |
70 | |
71 | while (1) { | |
52c51a47 PMF |
72 | int option_index = 0; |
73 | static struct option long_options[] = { | |
74 | {"start-trace", 0, 0, 1000}, | |
75 | {"stop-trace", 0, 0, 1001}, | |
76 | {"destroy-trace", 0, 0, 1002}, | |
77 | {"list-markers", 0, 0, 1004}, | |
78 | {"print-markers", 0, 0, 1005}, | |
79 | {"pid", 1, 0, 1006}, | |
80 | {"enable-marker", 1, 0, 1007}, | |
81 | {"disable-marker", 1, 0, 1008}, | |
82 | {"start", 0, 0, 1009}, | |
d373b5cd PMF |
83 | {"help", 0, 0, 'h'}, |
84 | {"version", 0, 0, 1010}, | |
ae937656 | 85 | {"online-pids", 0, 0, 1011}, |
52c51a47 PMF |
86 | {0, 0, 0, 0} |
87 | }; | |
88 | ||
d373b5cd | 89 | c = getopt_long(argc, argv, "h", long_options, &option_index); |
52c51a47 PMF |
90 | if (c == -1) |
91 | break; | |
92 | ||
93 | switch (c) { | |
94 | case 0: | |
95 | printf("option %s", long_options[option_index].name); | |
96 | if (optarg) | |
97 | printf(" with arg %s", optarg); | |
98 | printf("\n"); | |
99 | break; | |
100 | ||
101 | case 1000: | |
ae937656 | 102 | opts->cmd = START_TRACE; |
52c51a47 PMF |
103 | break; |
104 | case 1001: | |
ae937656 | 105 | opts->cmd = STOP_TRACE; |
52c51a47 PMF |
106 | break; |
107 | case 1009: | |
ae937656 | 108 | opts->cmd = START; |
52c51a47 PMF |
109 | break; |
110 | case 1002: | |
ae937656 | 111 | opts->cmd = DESTROY; |
52c51a47 PMF |
112 | break; |
113 | case 1004: | |
ae937656 | 114 | opts->cmd = LIST_MARKERS; |
52c51a47 PMF |
115 | break; |
116 | case 1007: | |
ae937656 PP |
117 | opts->cmd = ENABLE_MARKER; |
118 | opts->m_name = strdup(optarg); | |
52c51a47 PMF |
119 | break; |
120 | case 1008: | |
ae937656 PP |
121 | opts->cmd = DISABLE_MARKER; |
122 | opts->m_name = strdup(optarg); | |
123 | break; | |
124 | case 1011: | |
125 | opts->cmd = GET_ONLINE_PIDS; | |
52c51a47 | 126 | break; |
d373b5cd PMF |
127 | case 'h': |
128 | usage(); | |
129 | exit(0); | |
130 | case 1010: | |
ae937656 | 131 | printf("Version 0.1\n"); |
52c51a47 PMF |
132 | |
133 | default: | |
ae937656 PP |
134 | /* unknown option or other error; error is |
135 | printed by getopt, just return */ | |
136 | opts->cmd = UNKNOWN; | |
52c51a47 | 137 | return 1; |
fbd8191b PMF |
138 | } |
139 | } | |
140 | ||
ae937656 | 141 | if(argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
142 | int i; |
143 | int pididx=0; | |
144 | opts->pids = malloc((argc-optind+1) * sizeof(pid_t)); | |
fbd8191b | 145 | |
52c51a47 PMF |
146 | for(i=optind; i<argc; i++) { |
147 | opts->pids[pididx++] = atoi(argv[i]); | |
148 | } | |
149 | opts->pids[pididx] = -1; | |
fbd8191b PMF |
150 | } |
151 | ||
52c51a47 PMF |
152 | return 0; |
153 | } | |
fbd8191b | 154 | |
fbd8191b PMF |
155 | int main(int argc, char *argv[]) |
156 | { | |
52c51a47 PMF |
157 | pid_t *pidit; |
158 | //char *msg = argv[2]; | |
159 | struct ustcomm_connection conn; | |
160 | int result; | |
161 | struct ust_opts opts; | |
fbd8191b | 162 | |
52c51a47 | 163 | progname = argv[0]; |
fbd8191b | 164 | |
52c51a47 PMF |
165 | if(argc <= 1) { |
166 | fprintf(stderr, "No operation specified.\n"); | |
167 | usage(); | |
168 | exit(EXIT_FAILURE); | |
169 | } | |
170 | ||
171 | result = parse_opts_long(argc, argv, &opts); | |
172 | if(result) { | |
173 | usage(); | |
174 | exit(EXIT_FAILURE); | |
175 | } | |
176 | ||
ae937656 | 177 | if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
178 | fprintf(stderr, "No pid specified.\n"); |
179 | usage(); | |
180 | exit(EXIT_FAILURE); | |
181 | } | |
ae937656 | 182 | if(opts.cmd == UNKNOWN) { |
52c51a47 PMF |
183 | fprintf(stderr, "No command specified.\n"); |
184 | usage(); | |
185 | exit(EXIT_FAILURE); | |
186 | } | |
187 | ||
ae937656 PP |
188 | if (opts.cmd == GET_ONLINE_PIDS) { |
189 | pid_t* pp = ustcmd_get_online_pids(); | |
190 | unsigned int i = 0; | |
52c51a47 | 191 | |
ae937656 PP |
192 | if (pp) { |
193 | while (pp[i] != 0) { | |
194 | printf("%u\n", (unsigned int) pp[i]); | |
195 | ++i; | |
196 | } | |
197 | free(pp); | |
52c51a47 | 198 | } |
ae937656 PP |
199 | |
200 | exit(EXIT_SUCCESS); | |
201 | } | |
52c51a47 | 202 | |
ae937656 PP |
203 | pidit = opts.pids; |
204 | struct USTcmd_cmsf* cmsf = NULL; | |
205 | ||
206 | while(*pidit != -1) { | |
207 | switch (opts.cmd) { | |
208 | case START_TRACE: | |
209 | if (ustcmd_start_trace(*pidit)) { | |
210 | fprintf(stderr, | |
211 | "error while trying to for trace " | |
212 | "with PID %u\n", (unsigned int) *pidit); | |
213 | break; | |
214 | } | |
215 | printf("sucessfully started trace for PID %u\n", | |
216 | (unsigned int) *pidit); | |
217 | break; | |
218 | ||
219 | case STOP_TRACE: | |
220 | if (ustcmd_stop_trace(*pidit)) { | |
221 | fprintf(stderr, | |
222 | "error while trying to stop trace " | |
223 | "for PID %u\n", (unsigned int) *pidit); | |
224 | break; | |
225 | } | |
226 | printf("sucessfully stopped trace for PID %u\n", | |
227 | (unsigned int) *pidit); | |
228 | break; | |
229 | ||
230 | case START: | |
231 | if (ustcmd_setup_and_start(*pidit)) { | |
232 | fprintf(stderr, | |
233 | "error while trying to setup/start " | |
234 | "trace for PID %u\n", | |
235 | (unsigned int) *pidit); | |
236 | break; | |
237 | } | |
238 | printf("sucessfully setup/started trace for PID %u\n", | |
239 | (unsigned int) *pidit); | |
240 | break; | |
241 | ||
242 | case DESTROY: | |
243 | if (ustcmd_destroy_trace(*pidit)) { | |
244 | fprintf(stderr, | |
245 | "error while trying to destroy " | |
246 | "trace with PID %u\n", | |
247 | (unsigned int) *pidit); | |
248 | break; | |
249 | } | |
250 | printf("sucessfully destroyed trace for PID %u\n", | |
251 | (unsigned int) *pidit); | |
252 | break; | |
253 | ||
254 | case LIST_MARKERS: | |
255 | cmsf = NULL; | |
256 | if (ustcmd_get_cmsf(&cmsf, *pidit)) { | |
257 | fprintf(stderr, | |
258 | "error while trying to list markers for" | |
259 | " PID %u\n", (unsigned int) *pidit); | |
260 | break; | |
261 | } | |
262 | unsigned int i = 0; | |
263 | while (cmsf[i].channel != NULL) { | |
264 | printf("{PID: %u, channel/marker: %s/%s, " | |
265 | "state: %u, fs: %s}\n", | |
266 | (unsigned int) *pidit, | |
267 | cmsf[i].channel, | |
268 | cmsf[i].marker, | |
269 | cmsf[i].state, | |
270 | cmsf[i].fs); | |
271 | ++i; | |
272 | } | |
273 | ustcmd_free_cmsf(cmsf); | |
274 | break; | |
275 | ||
276 | case ENABLE_MARKER: | |
277 | if (ustcmd_set_marker_state(opts.m_name, USTCMD_MS_ON, | |
278 | *pidit)) { | |
279 | ||
280 | fprintf(stderr, | |
281 | "error while trying to enable marker" | |
282 | "\"%s\" for PID %u\n", | |
283 | opts.m_name, | |
284 | (unsigned int) *pidit); | |
285 | break; | |
286 | } | |
287 | printf("sucessfully enabled marker \"%s\" for PID %u\n", | |
288 | opts.m_name, (unsigned int) *pidit); | |
289 | break; | |
290 | ||
291 | case DISABLE_MARKER: | |
292 | if (ustcmd_set_marker_state(opts.m_name, USTCMD_MS_OFF, | |
293 | *pidit)) { | |
294 | fprintf(stderr, | |
295 | "error while trying to disable marker" | |
296 | "\"%s\" for PID %u\n", | |
297 | opts.m_name, | |
298 | (unsigned int) *pidit); | |
299 | break; | |
300 | } | |
301 | printf("sucessfully disabled marker \"%s\" for PID %u\n", | |
302 | opts.m_name, (unsigned int) *pidit); | |
303 | break; | |
304 | ||
305 | default: | |
306 | fprintf(stderr, "error: unknown command...\n"); | |
307 | break; | |
308 | } | |
309 | ||
52c51a47 PMF |
310 | pidit++; |
311 | } | |
82b1a169 | 312 | |
52c51a47 | 313 | free(opts.pids); |
ae937656 PP |
314 | if (opts.m_name != NULL) { |
315 | free(opts.m_name); | |
316 | } | |
fbd8191b PMF |
317 | |
318 | return 0; | |
319 | } | |
ae937656 | 320 |