disable ustctl --version until it is saner
[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
f9e5ce61 25#include "ustcomm.h"
ae937656 26#include "ustcmd.h"
2028e7fd 27#include "usterr.h"
ae937656
PP
28
29enum command {
62ec620f 30 CREATE_TRACE,
ae937656
PP
31 START_TRACE,
32 STOP_TRACE,
33 START,
34 DESTROY,
35 LIST_MARKERS,
36 ENABLE_MARKER,
37 DISABLE_MARKER,
38 GET_ONLINE_PIDS,
763f41e5
DS
39 SET_SUBBUF_SIZE,
40 SET_SUBBUF_NUM,
41 ALLOC_TRACE,
ae937656 42 UNKNOWN
ef290fca 43};
fbd8191b 44
52c51a47 45struct ust_opts {
ae937656 46 enum command cmd;
52c51a47 47 pid_t *pids;
08230db7 48 char *regex;
52c51a47
PMF
49};
50
fd2fb4f9
PMF
51char *progname = NULL;
52
53void usage(void)
54{
ae937656 55 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
fd2fb4f9
PMF
56 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
57\n\
58Commands:\n\
62ec620f 59 --create-trace\t\t\tCreate trace\n\
fd2fb4f9
PMF
60 --start-trace\t\t\tStart tracing\n\
61 --stop-trace\t\t\tStop tracing\n\
62 --destroy-trace\t\t\tDestroy the trace\n\
763f41e5
DS
63 --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\
64 --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\
ae937656
PP
65 --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\
66 --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\
67 --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\
68\
fd2fb4f9
PMF
69");
70}
71
52c51a47 72int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
fbd8191b 73{
52c51a47 74 int c;
52c51a47 75
52c51a47 76 opts->pids = NULL;
ef290fca 77 opts->regex = NULL;
52c51a47
PMF
78
79 while (1) {
52c51a47
PMF
80 int option_index = 0;
81 static struct option long_options[] = {
62ec620f 82 {"create-trace", 0, 0, 1012},
763f41e5 83 {"alloc-trace", 0, 0, 1015},
52c51a47
PMF
84 {"start-trace", 0, 0, 1000},
85 {"stop-trace", 0, 0, 1001},
86 {"destroy-trace", 0, 0, 1002},
87 {"list-markers", 0, 0, 1004},
88 {"print-markers", 0, 0, 1005},
89 {"pid", 1, 0, 1006},
90 {"enable-marker", 1, 0, 1007},
91 {"disable-marker", 1, 0, 1008},
92 {"start", 0, 0, 1009},
d373b5cd 93 {"help", 0, 0, 'h'},
ce3296b6 94 /*{"version", 0, 0, 1010},*/
ae937656 95 {"online-pids", 0, 0, 1011},
763f41e5
DS
96 {"set-subbuf-size", 1, 0, 1013},
97 {"set-subbuf-num", 1, 0, 1014},
52c51a47
PMF
98 {0, 0, 0, 0}
99 };
100
d373b5cd 101 c = getopt_long(argc, argv, "h", long_options, &option_index);
52c51a47
PMF
102 if (c == -1)
103 break;
104
105 switch (c) {
106 case 0:
107 printf("option %s", long_options[option_index].name);
108 if (optarg)
109 printf(" with arg %s", optarg);
110 printf("\n");
111 break;
112
113 case 1000:
ae937656 114 opts->cmd = START_TRACE;
52c51a47
PMF
115 break;
116 case 1001:
ae937656 117 opts->cmd = STOP_TRACE;
52c51a47
PMF
118 break;
119 case 1009:
ae937656 120 opts->cmd = START;
52c51a47
PMF
121 break;
122 case 1002:
ae937656 123 opts->cmd = DESTROY;
52c51a47
PMF
124 break;
125 case 1004:
ae937656 126 opts->cmd = LIST_MARKERS;
52c51a47
PMF
127 break;
128 case 1007:
ae937656 129 opts->cmd = ENABLE_MARKER;
ef290fca 130 opts->regex = strdup(optarg);
52c51a47
PMF
131 break;
132 case 1008:
ae937656 133 opts->cmd = DISABLE_MARKER;
ef290fca 134 opts->regex = strdup(optarg);
ae937656
PP
135 break;
136 case 1011:
137 opts->cmd = GET_ONLINE_PIDS;
52c51a47 138 break;
d373b5cd
PMF
139 case 'h':
140 usage();
141 exit(0);
ce3296b6 142/*
d373b5cd 143 case 1010:
ae937656 144 printf("Version 0.1\n");
62ec620f 145 break;
ce3296b6 146*/
62ec620f
PMF
147 case 1012:
148 opts->cmd = CREATE_TRACE;
149 break;
763f41e5
DS
150 case 1013:
151 opts->cmd = SET_SUBBUF_SIZE;
152 opts->regex = strdup(optarg);
153 break;
154 case 1014:
155 opts->cmd = SET_SUBBUF_NUM;
156 opts->regex = strdup(optarg);
157 break;
158 case 1015:
159 opts->cmd = ALLOC_TRACE;
160 break;
52c51a47 161 default:
ae937656
PP
162 /* unknown option or other error; error is
163 printed by getopt, just return */
164 opts->cmd = UNKNOWN;
52c51a47 165 return 1;
fbd8191b
PMF
166 }
167 }
168
772030fe 169 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
52c51a47
PMF
170 int i;
171 int pididx=0;
172 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
fbd8191b 173
52c51a47 174 for(i=optind; i<argc; i++) {
08230db7
PMF
175 /* don't take any chances, use a long long */
176 long long tmp;
177 char *endptr;
178 tmp = strtoull(argv[i], &endptr, 10);
179 if(*endptr != '\0') {
180 ERR("The pid \"%s\" is invalid.", argv[i]);
181 return 1;
182 }
183 opts->pids[pididx++] = (pid_t) tmp;
52c51a47
PMF
184 }
185 opts->pids[pididx] = -1;
fbd8191b
PMF
186 }
187
52c51a47
PMF
188 return 0;
189}
fbd8191b 190
fbd8191b
PMF
191int main(int argc, char *argv[])
192{
52c51a47 193 pid_t *pidit;
52c51a47
PMF
194 int result;
195 struct ust_opts opts;
fbd8191b 196
52c51a47 197 progname = argv[0];
fbd8191b 198
52c51a47
PMF
199 if(argc <= 1) {
200 fprintf(stderr, "No operation specified.\n");
201 usage();
202 exit(EXIT_FAILURE);
203 }
204
205 result = parse_opts_long(argc, argv, &opts);
206 if(result) {
08230db7 207 fprintf(stderr, "\n");
52c51a47
PMF
208 usage();
209 exit(EXIT_FAILURE);
210 }
211
ae937656 212 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
52c51a47
PMF
213 fprintf(stderr, "No pid specified.\n");
214 usage();
215 exit(EXIT_FAILURE);
216 }
ae937656 217 if(opts.cmd == UNKNOWN) {
52c51a47
PMF
218 fprintf(stderr, "No command specified.\n");
219 usage();
220 exit(EXIT_FAILURE);
221 }
ae937656 222 if (opts.cmd == GET_ONLINE_PIDS) {
08230db7 223 pid_t *pp = ustcmd_get_online_pids();
ae937656 224 unsigned int i = 0;
52c51a47 225
ae937656
PP
226 if (pp) {
227 while (pp[i] != 0) {
228 printf("%u\n", (unsigned int) pp[i]);
229 ++i;
230 }
231 free(pp);
52c51a47 232 }
ef290fca 233
ae937656
PP
234 exit(EXIT_SUCCESS);
235 }
52c51a47 236
ae937656 237 pidit = opts.pids;
08230db7 238 struct marker_status *cmsf = NULL;
ef290fca 239
ae937656
PP
240 while(*pidit != -1) {
241 switch (opts.cmd) {
62ec620f
PMF
242 case CREATE_TRACE:
243 result = ustcmd_create_trace(*pidit);
244 if (result) {
245 ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit);
246 break;
247 }
248 break;
249
ae937656 250 case START_TRACE:
08230db7
PMF
251 result = ustcmd_start_trace(*pidit);
252 if (result) {
253 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
254 break;
255 }
256 //printf("sucessfully started trace for PID %u\n", (unsigned int) *pidit);
ae937656 257 break;
ef290fca 258
ae937656 259 case STOP_TRACE:
08230db7
PMF
260 result = ustcmd_stop_trace(*pidit);
261 if (result) {
262 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
263 break;
264 }
265 //printf("sucessfully stopped trace for PID %u\n", (unsigned int) *pidit);
ae937656 266 break;
ef290fca 267
ae937656 268 case START:
08230db7
PMF
269 result = ustcmd_setup_and_start(*pidit);
270 if (result) {
271 ERR("error while trying to setup/start trace for PID %u\n", (unsigned int) *pidit);
272 break;
273 }
274 //printf("sucessfully setup/started trace for PID %u\n", (unsigned int) *pidit);
ae937656 275 break;
ef290fca 276
ae937656 277 case DESTROY:
08230db7
PMF
278 result = ustcmd_destroy_trace(*pidit);
279 if (result) {
280 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
281 break;
282 }
283 //printf("sucessfully destroyed trace for PID %u\n", (unsigned int) *pidit);
ae937656 284 break;
ef290fca 285
ae937656 286 case LIST_MARKERS:
08230db7
PMF
287 cmsf = NULL;
288 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
289 fprintf(stderr,
290 "error while trying to list markers for"
291 " PID %u\n", (unsigned int) *pidit);
292 break;
293 }
294 unsigned int i = 0;
295 while (cmsf[i].channel != NULL) {
296 printf("{PID: %u, channel/marker: %s/%s, "
264f6231 297 "state: %u, fmt: %s}\n",
08230db7
PMF
298 (unsigned int) *pidit,
299 cmsf[i].channel,
300 cmsf[i].marker,
301 cmsf[i].state,
302 cmsf[i].fs);
303 ++i;
304 }
305 ustcmd_free_cmsf(cmsf);
ae937656 306 break;
ef290fca 307
ae937656 308 case ENABLE_MARKER:
ee648b8f
PMF
309 if(opts.regex)
310 ustcmd_set_marker_state(opts.regex, 1, *pidit);
311 break;
ae937656 312 case DISABLE_MARKER:
ee648b8f
PMF
313 if(opts.regex)
314 ustcmd_set_marker_state(opts.regex, 0, *pidit);
315 break;
ef290fca 316
763f41e5
DS
317 case SET_SUBBUF_SIZE:
318 ustcmd_set_subbuf_size(opts.regex, *pidit);
319 break;
320
321 case SET_SUBBUF_NUM:
322 ustcmd_set_subbuf_num(opts.regex, *pidit);
323 break;
324
325 case ALLOC_TRACE:
326 result = ustcmd_alloc_trace(*pidit);
327 if (result) {
328 ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit);
329 break;
330 }
331 break;
332
ae937656 333 default:
08230db7 334 ERR("unknown command\n");
ae937656
PP
335 break;
336 }
ef290fca 337
52c51a47
PMF
338 pidit++;
339 }
82b1a169 340
ef290fca
PMF
341 if (opts.pids != NULL) {
342 free(opts.pids);
343 }
344 if (opts.regex != NULL) {
345 free(opts.regex);
ae937656 346 }
fbd8191b
PMF
347
348 return 0;
349}
ae937656 350
This page took 0.041685 seconds and 4 git commands to generate.