add options to set subbuf size and cnt
[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
PMF
93 {"help", 0, 0, 'h'},
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);
142 case 1010:
ae937656 143 printf("Version 0.1\n");
62ec620f
PMF
144 break;
145 case 1012:
146 opts->cmd = CREATE_TRACE;
147 break;
763f41e5
DS
148 case 1013:
149 opts->cmd = SET_SUBBUF_SIZE;
150 opts->regex = strdup(optarg);
151 break;
152 case 1014:
153 opts->cmd = SET_SUBBUF_NUM;
154 opts->regex = strdup(optarg);
155 break;
156 case 1015:
157 opts->cmd = ALLOC_TRACE;
158 break;
52c51a47 159 default:
ae937656
PP
160 /* unknown option or other error; error is
161 printed by getopt, just return */
162 opts->cmd = UNKNOWN;
52c51a47 163 return 1;
fbd8191b
PMF
164 }
165 }
166
772030fe 167 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
52c51a47
PMF
168 int i;
169 int pididx=0;
170 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
fbd8191b 171
52c51a47 172 for(i=optind; i<argc; i++) {
08230db7
PMF
173 /* don't take any chances, use a long long */
174 long long tmp;
175 char *endptr;
176 tmp = strtoull(argv[i], &endptr, 10);
177 if(*endptr != '\0') {
178 ERR("The pid \"%s\" is invalid.", argv[i]);
179 return 1;
180 }
181 opts->pids[pididx++] = (pid_t) tmp;
52c51a47
PMF
182 }
183 opts->pids[pididx] = -1;
fbd8191b
PMF
184 }
185
52c51a47
PMF
186 return 0;
187}
fbd8191b 188
fbd8191b
PMF
189int main(int argc, char *argv[])
190{
52c51a47 191 pid_t *pidit;
52c51a47
PMF
192 int result;
193 struct ust_opts opts;
fbd8191b 194
52c51a47 195 progname = argv[0];
fbd8191b 196
52c51a47
PMF
197 if(argc <= 1) {
198 fprintf(stderr, "No operation specified.\n");
199 usage();
200 exit(EXIT_FAILURE);
201 }
202
203 result = parse_opts_long(argc, argv, &opts);
204 if(result) {
08230db7 205 fprintf(stderr, "\n");
52c51a47
PMF
206 usage();
207 exit(EXIT_FAILURE);
208 }
209
ae937656 210 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
52c51a47
PMF
211 fprintf(stderr, "No pid specified.\n");
212 usage();
213 exit(EXIT_FAILURE);
214 }
ae937656 215 if(opts.cmd == UNKNOWN) {
52c51a47
PMF
216 fprintf(stderr, "No command specified.\n");
217 usage();
218 exit(EXIT_FAILURE);
219 }
ae937656 220 if (opts.cmd == GET_ONLINE_PIDS) {
08230db7 221 pid_t *pp = ustcmd_get_online_pids();
ae937656 222 unsigned int i = 0;
52c51a47 223
ae937656
PP
224 if (pp) {
225 while (pp[i] != 0) {
226 printf("%u\n", (unsigned int) pp[i]);
227 ++i;
228 }
229 free(pp);
52c51a47 230 }
ef290fca 231
ae937656
PP
232 exit(EXIT_SUCCESS);
233 }
52c51a47 234
ae937656 235 pidit = opts.pids;
08230db7 236 struct marker_status *cmsf = NULL;
ef290fca 237
ae937656
PP
238 while(*pidit != -1) {
239 switch (opts.cmd) {
62ec620f
PMF
240 case CREATE_TRACE:
241 result = ustcmd_create_trace(*pidit);
242 if (result) {
243 ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit);
244 break;
245 }
246 break;
247
ae937656 248 case START_TRACE:
08230db7
PMF
249 result = ustcmd_start_trace(*pidit);
250 if (result) {
251 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
252 break;
253 }
254 //printf("sucessfully started trace for PID %u\n", (unsigned int) *pidit);
ae937656 255 break;
ef290fca 256
ae937656 257 case STOP_TRACE:
08230db7
PMF
258 result = ustcmd_stop_trace(*pidit);
259 if (result) {
260 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
261 break;
262 }
263 //printf("sucessfully stopped trace for PID %u\n", (unsigned int) *pidit);
ae937656 264 break;
ef290fca 265
ae937656 266 case START:
08230db7
PMF
267 result = ustcmd_setup_and_start(*pidit);
268 if (result) {
269 ERR("error while trying to setup/start trace for PID %u\n", (unsigned int) *pidit);
270 break;
271 }
272 //printf("sucessfully setup/started trace for PID %u\n", (unsigned int) *pidit);
ae937656 273 break;
ef290fca 274
ae937656 275 case DESTROY:
08230db7
PMF
276 result = ustcmd_destroy_trace(*pidit);
277 if (result) {
278 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
279 break;
280 }
281 //printf("sucessfully destroyed trace for PID %u\n", (unsigned int) *pidit);
ae937656 282 break;
ef290fca 283
ae937656 284 case LIST_MARKERS:
08230db7
PMF
285 cmsf = NULL;
286 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
287 fprintf(stderr,
288 "error while trying to list markers for"
289 " PID %u\n", (unsigned int) *pidit);
290 break;
291 }
292 unsigned int i = 0;
293 while (cmsf[i].channel != NULL) {
294 printf("{PID: %u, channel/marker: %s/%s, "
264f6231 295 "state: %u, fmt: %s}\n",
08230db7
PMF
296 (unsigned int) *pidit,
297 cmsf[i].channel,
298 cmsf[i].marker,
299 cmsf[i].state,
300 cmsf[i].fs);
301 ++i;
302 }
303 ustcmd_free_cmsf(cmsf);
ae937656 304 break;
ef290fca 305
ae937656 306 case ENABLE_MARKER:
ee648b8f
PMF
307 if(opts.regex)
308 ustcmd_set_marker_state(opts.regex, 1, *pidit);
309 break;
ae937656 310 case DISABLE_MARKER:
ee648b8f
PMF
311 if(opts.regex)
312 ustcmd_set_marker_state(opts.regex, 0, *pidit);
313 break;
ef290fca 314
763f41e5
DS
315 case SET_SUBBUF_SIZE:
316 ustcmd_set_subbuf_size(opts.regex, *pidit);
317 break;
318
319 case SET_SUBBUF_NUM:
320 ustcmd_set_subbuf_num(opts.regex, *pidit);
321 break;
322
323 case ALLOC_TRACE:
324 result = ustcmd_alloc_trace(*pidit);
325 if (result) {
326 ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit);
327 break;
328 }
329 break;
330
ae937656 331 default:
08230db7 332 ERR("unknown command\n");
ae937656
PP
333 break;
334 }
ef290fca 335
52c51a47
PMF
336 pidit++;
337 }
82b1a169 338
ef290fca
PMF
339 if (opts.pids != NULL) {
340 free(opts.pids);
341 }
342 if (opts.regex != NULL) {
343 free(opts.regex);
ae937656 344 }
fbd8191b
PMF
345
346 return 0;
347}
ae937656 348
This page took 0.041993 seconds and 4 git commands to generate.