Set the exit status of ustctl main function v3
[ust.git] / ustctl / ustctl.c
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
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24
25 #include "ust/ustcmd.h"
26 #include "usterr.h"
27
28 enum command {
29 CREATE_TRACE=1000,
30 ALLOC_TRACE,
31 START_TRACE,
32 STOP_TRACE,
33 DESTROY_TRACE,
34 LIST_MARKERS,
35 ENABLE_MARKER,
36 DISABLE_MARKER,
37 GET_ONLINE_PIDS,
38 SET_SUBBUF_SIZE,
39 SET_SUBBUF_NUM,
40 GET_SUBBUF_SIZE,
41 GET_SUBBUF_NUM,
42 GET_SOCK_PATH,
43 SET_SOCK_PATH,
44 FORCE_SWITCH,
45 UNKNOWN
46 };
47
48 struct ust_opts {
49 enum command cmd;
50 pid_t *pids;
51 char *regex;
52 };
53
54 char *progname = NULL;
55
56 void usage(void)
57 {
58 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
59 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
60 \n\
61 Commands:\n\
62 --create-trace\t\t\tCreate trace\n\
63 --alloc-trace\t\t\tAlloc trace\n\
64 --start-trace\t\t\tStart tracing\n\
65 --stop-trace\t\t\tStop tracing\n\
66 --destroy-trace\t\t\tDestroy the trace\n\
67 --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\
68 --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\
69 --set-sock-path\t\t\tSet the path of the daemon socket\n\
70 --get-subbuf-size \"CHANNEL\"\t\tGet the size of subbuffers per channel\n\
71 --get-subbuf-num \"CHANNEL\"\t\tGet the number of subbuffers per channel\n\
72 --get-sock-path\t\t\tGet the path of the daemon socket\n\
73 --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\
74 --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\
75 --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\
76 --force-switch\t\t\tForce a subbuffer switch\n\
77 \
78 ");
79 }
80
81 int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
82 {
83 int c;
84
85 opts->pids = NULL;
86 opts->regex = NULL;
87
88 while (1) {
89 int option_index = 0;
90 static struct option long_options[] = {
91 { "create-trace", 0, 0, CREATE_TRACE },
92 { "alloc-trace", 0, 0, ALLOC_TRACE },
93 { "start-trace", 0, 0, START_TRACE },
94 { "stop-trace", 0, 0, STOP_TRACE },
95 { "destroy-trace", 0, 0, DESTROY_TRACE },
96 { "list-markers", 0, 0, LIST_MARKERS },
97 { "enable-marker", 1, 0, ENABLE_MARKER },
98 { "disable-marker", 1, 0, DISABLE_MARKER },
99 { "help", 0, 0, 'h' },
100 { "online-pids", 0, 0, GET_ONLINE_PIDS },
101 { "set-subbuf-size", 1, 0, SET_SUBBUF_SIZE },
102 { "set-subbuf-num", 1, 0, SET_SUBBUF_NUM },
103 { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE },
104 { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM },
105 { "get-sock-path", 0, 0, GET_SOCK_PATH },
106 { "set-sock-path", 1, 0, SET_SOCK_PATH },
107 { "force-switch", 0, 0, FORCE_SWITCH },
108 { 0, 0, 0, 0 }
109 };
110
111 c = getopt_long(argc, argv, "h", long_options, &option_index);
112 if (c == -1)
113 break;
114
115 if(c >= 1000)
116 opts->cmd = c;
117
118 switch (c) {
119 case 0:
120 printf("option %s", long_options[option_index].name);
121 if (optarg)
122 printf(" with arg %s", optarg);
123 printf("\n");
124 break;
125
126 case ENABLE_MARKER:
127 case DISABLE_MARKER:
128 case SET_SUBBUF_SIZE:
129 case SET_SUBBUF_NUM:
130 case GET_SUBBUF_SIZE:
131 case GET_SUBBUF_NUM:
132 case SET_SOCK_PATH:
133 opts->regex = strdup(optarg);
134 break;
135
136 case 'h':
137 usage();
138 exit(0);
139
140 case '?':
141 fprintf(stderr, "Invalid argument\n\n");
142 usage();
143 exit(1);
144 }
145 }
146
147 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
148 int i;
149 int pididx=0;
150 opts->pids = zmalloc((argc-optind+1) * sizeof(pid_t));
151
152 for(i=optind; i<argc; i++) {
153 /* don't take any chances, use a long long */
154 long long tmp;
155 char *endptr;
156 tmp = strtoull(argv[i], &endptr, 10);
157 if(*endptr != '\0') {
158 ERR("The pid \"%s\" is invalid.", argv[i]);
159 return 1;
160 }
161 opts->pids[pididx++] = (pid_t) tmp;
162 }
163 opts->pids[pididx] = -1;
164 }
165
166 return 0;
167 }
168
169 int main(int argc, char *argv[])
170 {
171 pid_t *pidit;
172 int result;
173 int retval = EXIT_SUCCESS;
174 char *tmp;
175 struct ust_opts opts;
176
177 progname = argv[0];
178
179 if(argc <= 1) {
180 fprintf(stderr, "No operation specified.\n");
181 usage();
182 exit(EXIT_FAILURE);
183 }
184
185 result = parse_opts_long(argc, argv, &opts);
186 if(result) {
187 fprintf(stderr, "\n");
188 usage();
189 exit(EXIT_FAILURE);
190 }
191
192 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
193 fprintf(stderr, "No pid specified.\n");
194 usage();
195 exit(EXIT_FAILURE);
196 }
197 if(opts.cmd == UNKNOWN) {
198 fprintf(stderr, "No command specified.\n");
199 usage();
200 exit(EXIT_FAILURE);
201 }
202 if (opts.cmd == GET_ONLINE_PIDS) {
203 pid_t *pp = ustcmd_get_online_pids();
204 unsigned int i = 0;
205
206 if (pp) {
207 while (pp[i] != 0) {
208 printf("%u\n", (unsigned int) pp[i]);
209 ++i;
210 }
211 free(pp);
212 }
213
214 exit(EXIT_SUCCESS);
215 }
216
217 pidit = opts.pids;
218 struct marker_status *cmsf = NULL;
219
220 while(*pidit != -1) {
221 switch (opts.cmd) {
222 case CREATE_TRACE:
223 result = ustcmd_create_trace(*pidit);
224 if (result) {
225 ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit);
226 retval = EXIT_FAILURE;
227 break;
228 }
229 break;
230
231 case START_TRACE:
232 result = ustcmd_start_trace(*pidit);
233 if (result) {
234 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
235 retval = EXIT_FAILURE;
236 break;
237 }
238 break;
239
240 case STOP_TRACE:
241 result = ustcmd_stop_trace(*pidit);
242 if (result) {
243 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
244 retval = EXIT_FAILURE;
245 break;
246 }
247 break;
248
249 case DESTROY_TRACE:
250 result = ustcmd_destroy_trace(*pidit);
251 if (result) {
252 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
253 retval = EXIT_FAILURE;
254 break;
255 }
256 break;
257
258 case LIST_MARKERS:
259 cmsf = NULL;
260 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
261 ERR("error while trying to list markers for PID %u\n", (unsigned int) *pidit);
262 retval = EXIT_FAILURE;
263 break;
264 }
265 unsigned int i = 0;
266 while (cmsf[i].channel != NULL) {
267 printf("{PID: %u, channel/marker: %s/%s, "
268 "state: %u, fmt: %s}\n",
269 (unsigned int) *pidit,
270 cmsf[i].channel,
271 cmsf[i].marker,
272 cmsf[i].state,
273 cmsf[i].fs);
274 ++i;
275 }
276 ustcmd_free_cmsf(cmsf);
277 break;
278
279 case ENABLE_MARKER:
280 if (opts.regex) {
281 if (ustcmd_set_marker_state(opts.regex, 1, *pidit)) {
282 ERR("error while trying to enable marker %s with PID %u\n",
283 opts.regex, (unsigned int) *pidit);
284 retval = EXIT_FAILURE;
285 }
286 }
287 break;
288 case DISABLE_MARKER:
289 if (opts.regex) {
290 if (ustcmd_set_marker_state(opts.regex, 0, *pidit)) {
291 ERR("error while trying to disable marker %s with PID %u\n",
292 opts.regex, (unsigned int) *pidit);
293 retval = EXIT_FAILURE;
294 }
295 }
296 break;
297
298 case SET_SUBBUF_SIZE:
299 if (opts.regex) {
300 if (ustcmd_set_subbuf_size(opts.regex, *pidit)) {
301 ERR("error while trying to set the size of subbuffers with PID %u\n",
302 (unsigned int) *pidit);
303 retval = EXIT_FAILURE;
304 }
305 }
306 break;
307
308 case SET_SUBBUF_NUM:
309 if (opts.regex) {
310 if (ustcmd_set_subbuf_num(opts.regex, *pidit)) {
311 ERR("error while trying to set the number of subbuffers with PID %u\n",
312 (unsigned int) *pidit);
313 retval = EXIT_FAILURE;
314 }
315 }
316 break;
317
318 case GET_SUBBUF_SIZE:
319 result = ustcmd_get_subbuf_size(opts.regex, *pidit);
320 if (result == -1) {
321 ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit);
322 retval = EXIT_FAILURE;
323 break;
324 }
325
326 printf("the size of subbufers is %d\n", result);
327 break;
328
329 case GET_SUBBUF_NUM:
330 result = ustcmd_get_subbuf_num(opts.regex, *pidit);
331 if (result == -1) {
332 ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit);
333 retval = EXIT_FAILURE;
334 break;
335 }
336
337 printf("the number of subbufers is %d\n", result);
338 break;
339
340 case ALLOC_TRACE:
341 result = ustcmd_alloc_trace(*pidit);
342 if (result) {
343 ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit);
344 retval = EXIT_FAILURE;
345 }
346 break;
347
348 case GET_SOCK_PATH:
349 result = ustcmd_get_sock_path(&tmp, *pidit);
350 if (result) {
351 ERR("error while trying to get sock path for PID %u\n", (unsigned int) *pidit);
352 retval = EXIT_FAILURE;
353 break;
354 }
355 printf("the socket path is %s\n", tmp);
356 free(tmp);
357 break;
358
359 case SET_SOCK_PATH:
360 result = ustcmd_set_sock_path(opts.regex, *pidit);
361 if (result) {
362 ERR("error while trying to set sock path for PID %u\n", (unsigned int) *pidit);
363 retval = EXIT_FAILURE;
364 }
365 break;
366
367 case FORCE_SWITCH:
368 result = ustcmd_force_switch(*pidit);
369 if (result) {
370 ERR("error while trying to force switch for PID %u\n", (unsigned int) *pidit);
371 retval = EXIT_FAILURE;
372 }
373 break;
374
375 default:
376 ERR("unknown command\n");
377 retval = EXIT_FAILURE;
378 break;
379 }
380
381 pidit++;
382 }
383
384 if (opts.pids != NULL) {
385 free(opts.pids);
386 }
387 if (opts.regex != NULL) {
388 free(opts.regex);
389 }
390
391 return retval;
392 }
393
This page took 0.03626 seconds and 4 git commands to generate.