Set the exit status of ustctl main function v3
[ust.git] / ustctl / ustctl.c
CommitLineData
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
d6c9f207 25#include "ust/ustcmd.h"
2028e7fd 26#include "usterr.h"
ae937656
PP
27
28enum command {
b0a5a08b
PMF
29 CREATE_TRACE=1000,
30 ALLOC_TRACE,
ae937656
PP
31 START_TRACE,
32 STOP_TRACE,
b0a5a08b 33 DESTROY_TRACE,
ae937656
PP
34 LIST_MARKERS,
35 ENABLE_MARKER,
36 DISABLE_MARKER,
37 GET_ONLINE_PIDS,
763f41e5
DS
38 SET_SUBBUF_SIZE,
39 SET_SUBBUF_NUM,
e77b8e8e
DS
40 GET_SUBBUF_SIZE,
41 GET_SUBBUF_NUM,
b2fb2f91
AH
42 GET_SOCK_PATH,
43 SET_SOCK_PATH,
b9318b35 44 FORCE_SWITCH,
ae937656 45 UNKNOWN
ef290fca 46};
fbd8191b 47
52c51a47 48struct ust_opts {
ae937656 49 enum command cmd;
52c51a47 50 pid_t *pids;
08230db7 51 char *regex;
52c51a47
PMF
52};
53
fd2fb4f9
PMF
54char *progname = NULL;
55
56void usage(void)
57{
ae937656 58 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
fd2fb4f9
PMF
59 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
60\n\
61Commands:\n\
62ec620f 62 --create-trace\t\t\tCreate trace\n\
4ed9f99d 63 --alloc-trace\t\t\tAlloc trace\n\
fd2fb4f9
PMF
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\
763f41e5
DS
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\
b2fb2f91 69 --set-sock-path\t\t\tSet the path of the daemon socket\n\
e77b8e8e
DS
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\
b2fb2f91 72 --get-sock-path\t\t\tGet the path of the daemon socket\n\
ae937656
PP
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\
b9318b35 76 --force-switch\t\t\tForce a subbuffer switch\n\
ae937656 77\
fd2fb4f9
PMF
78");
79}
80
52c51a47 81int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
fbd8191b 82{
52c51a47 83 int c;
52c51a47 84
52c51a47 85 opts->pids = NULL;
ef290fca 86 opts->regex = NULL;
52c51a47
PMF
87
88 while (1) {
52c51a47
PMF
89 int option_index = 0;
90 static struct option long_options[] = {
b0a5a08b
PMF
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 },
e77b8e8e
DS
103 { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE },
104 { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM },
b2fb2f91
AH
105 { "get-sock-path", 0, 0, GET_SOCK_PATH },
106 { "set-sock-path", 1, 0, SET_SOCK_PATH },
b9318b35 107 { "force-switch", 0, 0, FORCE_SWITCH },
b0a5a08b 108 { 0, 0, 0, 0 }
52c51a47
PMF
109 };
110
d373b5cd 111 c = getopt_long(argc, argv, "h", long_options, &option_index);
52c51a47
PMF
112 if (c == -1)
113 break;
114
b0a5a08b
PMF
115 if(c >= 1000)
116 opts->cmd = c;
117
52c51a47
PMF
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
b0a5a08b
PMF
126 case ENABLE_MARKER:
127 case DISABLE_MARKER:
128 case SET_SUBBUF_SIZE:
129 case SET_SUBBUF_NUM:
e77b8e8e
DS
130 case GET_SUBBUF_SIZE:
131 case GET_SUBBUF_NUM:
b2fb2f91 132 case SET_SOCK_PATH:
ef290fca 133 opts->regex = strdup(optarg);
ae937656 134 break;
b0a5a08b 135
d373b5cd
PMF
136 case 'h':
137 usage();
138 exit(0);
b0a5a08b
PMF
139
140 case '?':
141 fprintf(stderr, "Invalid argument\n\n");
142 usage();
143 exit(1);
fbd8191b
PMF
144 }
145 }
146
772030fe 147 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
52c51a47
PMF
148 int i;
149 int pididx=0;
7032c7d3 150 opts->pids = zmalloc((argc-optind+1) * sizeof(pid_t));
fbd8191b 151
52c51a47 152 for(i=optind; i<argc; i++) {
08230db7
PMF
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;
52c51a47
PMF
162 }
163 opts->pids[pididx] = -1;
fbd8191b
PMF
164 }
165
52c51a47
PMF
166 return 0;
167}
fbd8191b 168
fbd8191b
PMF
169int main(int argc, char *argv[])
170{
52c51a47 171 pid_t *pidit;
52c51a47 172 int result;
1e620c53 173 int retval = EXIT_SUCCESS;
b2fb2f91 174 char *tmp;
52c51a47 175 struct ust_opts opts;
fbd8191b 176
52c51a47 177 progname = argv[0];
fbd8191b 178
52c51a47
PMF
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) {
08230db7 187 fprintf(stderr, "\n");
52c51a47
PMF
188 usage();
189 exit(EXIT_FAILURE);
190 }
191
ae937656 192 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
52c51a47
PMF
193 fprintf(stderr, "No pid specified.\n");
194 usage();
195 exit(EXIT_FAILURE);
196 }
ae937656 197 if(opts.cmd == UNKNOWN) {
52c51a47
PMF
198 fprintf(stderr, "No command specified.\n");
199 usage();
200 exit(EXIT_FAILURE);
201 }
ae937656 202 if (opts.cmd == GET_ONLINE_PIDS) {
08230db7 203 pid_t *pp = ustcmd_get_online_pids();
ae937656 204 unsigned int i = 0;
52c51a47 205
ae937656
PP
206 if (pp) {
207 while (pp[i] != 0) {
208 printf("%u\n", (unsigned int) pp[i]);
209 ++i;
210 }
211 free(pp);
52c51a47 212 }
ef290fca 213
ae937656
PP
214 exit(EXIT_SUCCESS);
215 }
52c51a47 216
ae937656 217 pidit = opts.pids;
08230db7 218 struct marker_status *cmsf = NULL;
ef290fca 219
ae937656
PP
220 while(*pidit != -1) {
221 switch (opts.cmd) {
62ec620f
PMF
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);
1e620c53 226 retval = EXIT_FAILURE;
62ec620f
PMF
227 break;
228 }
229 break;
230
ae937656 231 case START_TRACE:
08230db7
PMF
232 result = ustcmd_start_trace(*pidit);
233 if (result) {
234 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
1e620c53 235 retval = EXIT_FAILURE;
08230db7
PMF
236 break;
237 }
ae937656 238 break;
ef290fca 239
ae937656 240 case STOP_TRACE:
08230db7
PMF
241 result = ustcmd_stop_trace(*pidit);
242 if (result) {
243 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
1e620c53 244 retval = EXIT_FAILURE;
08230db7
PMF
245 break;
246 }
ae937656 247 break;
ef290fca 248
b0a5a08b 249 case DESTROY_TRACE:
08230db7
PMF
250 result = ustcmd_destroy_trace(*pidit);
251 if (result) {
252 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
1e620c53 253 retval = EXIT_FAILURE;
08230db7
PMF
254 break;
255 }
ae937656 256 break;
ef290fca 257
ae937656 258 case LIST_MARKERS:
08230db7
PMF
259 cmsf = NULL;
260 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
1e620c53
DG
261 ERR("error while trying to list markers for PID %u\n", (unsigned int) *pidit);
262 retval = EXIT_FAILURE;
08230db7
PMF
263 break;
264 }
265 unsigned int i = 0;
266 while (cmsf[i].channel != NULL) {
267 printf("{PID: %u, channel/marker: %s/%s, "
264f6231 268 "state: %u, fmt: %s}\n",
08230db7
PMF
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);
ae937656 277 break;
ef290fca 278
ae937656 279 case ENABLE_MARKER:
1e620c53
DG
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 }
ee648b8f 287 break;
ae937656 288 case DISABLE_MARKER:
1e620c53
DG
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 }
ee648b8f 296 break;
ef290fca 297
763f41e5 298 case SET_SUBBUF_SIZE:
1e620c53
DG
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 }
763f41e5
DS
306 break;
307
308 case SET_SUBBUF_NUM:
1e620c53
DG
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 }
763f41e5
DS
316 break;
317
e77b8e8e
DS
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);
1e620c53 322 retval = EXIT_FAILURE;
e77b8e8e
DS
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);
1e620c53 333 retval = EXIT_FAILURE;
e77b8e8e
DS
334 break;
335 }
336
337 printf("the number of subbufers is %d\n", result);
338 break;
339
763f41e5
DS
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);
1e620c53 344 retval = EXIT_FAILURE;
763f41e5
DS
345 }
346 break;
347
b2fb2f91
AH
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);
1e620c53 352 retval = EXIT_FAILURE;
b2fb2f91
AH
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);
1e620c53 363 retval = EXIT_FAILURE;
b2fb2f91
AH
364 }
365 break;
366
b9318b35
AH
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);
1e620c53 371 retval = EXIT_FAILURE;
b9318b35
AH
372 }
373 break;
374
ae937656 375 default:
08230db7 376 ERR("unknown command\n");
1e620c53
DG
377 retval = EXIT_FAILURE;
378 break;
ae937656 379 }
ef290fca 380
52c51a47
PMF
381 pidit++;
382 }
82b1a169 383
ef290fca
PMF
384 if (opts.pids != NULL) {
385 free(opts.pids);
386 }
387 if (opts.regex != NULL) {
388 free(opts.regex);
ae937656 389 }
fbd8191b 390
1e620c53 391 return retval;
fbd8191b 392}
ae937656 393
This page took 0.048249 seconds and 4 git commands to generate.