39ca4025ac347941d2aee84f1abd99533509b113
[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 "ustcomm.h"
26 #include "ustcmd.h"
27 #include "usterr.h"
28
29 enum command {
30 CREATE_TRACE,
31 START_TRACE,
32 STOP_TRACE,
33 START,
34 DESTROY,
35 LIST_MARKERS,
36 ENABLE_MARKER,
37 DISABLE_MARKER,
38 GET_ONLINE_PIDS,
39 SET_SUBBUF_SIZE,
40 SET_SUBBUF_NUM,
41 ALLOC_TRACE,
42 UNKNOWN
43 };
44
45 struct ust_opts {
46 enum command cmd;
47 pid_t *pids;
48 char *regex;
49 };
50
51 char *progname = NULL;
52
53 void usage(void)
54 {
55 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
56 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
57 \n\
58 Commands:\n\
59 --create-trace\t\t\tCreate trace\n\
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\
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\
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 \
69 ");
70 }
71
72 int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
73 {
74 int c;
75
76 opts->pids = NULL;
77 opts->regex = NULL;
78
79 while (1) {
80 int option_index = 0;
81 static struct option long_options[] = {
82 {"create-trace", 0, 0, 1012},
83 {"alloc-trace", 0, 0, 1015},
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},
93 {"help", 0, 0, 'h'},
94 {"version", 0, 0, 1010},
95 {"online-pids", 0, 0, 1011},
96 {"set-subbuf-size", 1, 0, 1013},
97 {"set-subbuf-num", 1, 0, 1014},
98 {0, 0, 0, 0}
99 };
100
101 c = getopt_long(argc, argv, "h", long_options, &option_index);
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:
114 opts->cmd = START_TRACE;
115 break;
116 case 1001:
117 opts->cmd = STOP_TRACE;
118 break;
119 case 1009:
120 opts->cmd = START;
121 break;
122 case 1002:
123 opts->cmd = DESTROY;
124 break;
125 case 1004:
126 opts->cmd = LIST_MARKERS;
127 break;
128 case 1007:
129 opts->cmd = ENABLE_MARKER;
130 opts->regex = strdup(optarg);
131 break;
132 case 1008:
133 opts->cmd = DISABLE_MARKER;
134 opts->regex = strdup(optarg);
135 break;
136 case 1011:
137 opts->cmd = GET_ONLINE_PIDS;
138 break;
139 case 'h':
140 usage();
141 exit(0);
142 case 1010:
143 printf("Version 0.1\n");
144 break;
145 case 1012:
146 opts->cmd = CREATE_TRACE;
147 break;
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;
159 default:
160 /* unknown option or other error; error is
161 printed by getopt, just return */
162 opts->cmd = UNKNOWN;
163 return 1;
164 }
165 }
166
167 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
168 int i;
169 int pididx=0;
170 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
171
172 for(i=optind; i<argc; i++) {
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;
182 }
183 opts->pids[pididx] = -1;
184 }
185
186 return 0;
187 }
188
189 int main(int argc, char *argv[])
190 {
191 pid_t *pidit;
192 int result;
193 struct ust_opts opts;
194
195 progname = argv[0];
196
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) {
205 fprintf(stderr, "\n");
206 usage();
207 exit(EXIT_FAILURE);
208 }
209
210 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
211 fprintf(stderr, "No pid specified.\n");
212 usage();
213 exit(EXIT_FAILURE);
214 }
215 if(opts.cmd == UNKNOWN) {
216 fprintf(stderr, "No command specified.\n");
217 usage();
218 exit(EXIT_FAILURE);
219 }
220 if (opts.cmd == GET_ONLINE_PIDS) {
221 pid_t *pp = ustcmd_get_online_pids();
222 unsigned int i = 0;
223
224 if (pp) {
225 while (pp[i] != 0) {
226 printf("%u\n", (unsigned int) pp[i]);
227 ++i;
228 }
229 free(pp);
230 }
231
232 exit(EXIT_SUCCESS);
233 }
234
235 pidit = opts.pids;
236 struct marker_status *cmsf = NULL;
237
238 while(*pidit != -1) {
239 switch (opts.cmd) {
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
248 case START_TRACE:
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);
255 break;
256
257 case STOP_TRACE:
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);
264 break;
265
266 case START:
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);
273 break;
274
275 case DESTROY:
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);
282 break;
283
284 case LIST_MARKERS:
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, "
295 "state: %u, fmt: %s}\n",
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);
304 break;
305
306 case ENABLE_MARKER:
307 if(opts.regex)
308 ustcmd_set_marker_state(opts.regex, 1, *pidit);
309 break;
310 case DISABLE_MARKER:
311 if(opts.regex)
312 ustcmd_set_marker_state(opts.regex, 0, *pidit);
313 break;
314
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
331 default:
332 ERR("unknown command\n");
333 break;
334 }
335
336 pidit++;
337 }
338
339 if (opts.pids != NULL) {
340 free(opts.pids);
341 }
342 if (opts.regex != NULL) {
343 free(opts.regex);
344 }
345
346 return 0;
347 }
348
This page took 0.035565 seconds and 3 git commands to generate.