50e0d012abff05ff861e0c1f9a9fe4546804975d
[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=1000,
31 ALLOC_TRACE,
32 START_TRACE,
33 STOP_TRACE,
34 DESTROY_TRACE,
35 LIST_MARKERS,
36 ENABLE_MARKER,
37 DISABLE_MARKER,
38 GET_ONLINE_PIDS,
39 SET_SUBBUF_SIZE,
40 SET_SUBBUF_NUM,
41 GET_SUBBUF_SIZE,
42 GET_SUBBUF_NUM,
43 GET_SOCK_PATH,
44 SET_SOCK_PATH,
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 \
77 ");
78 }
79
80 int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
81 {
82 int c;
83
84 opts->pids = NULL;
85 opts->regex = NULL;
86
87 while (1) {
88 int option_index = 0;
89 static struct option long_options[] = {
90 { "create-trace", 0, 0, CREATE_TRACE },
91 { "alloc-trace", 0, 0, ALLOC_TRACE },
92 { "start-trace", 0, 0, START_TRACE },
93 { "stop-trace", 0, 0, STOP_TRACE },
94 { "destroy-trace", 0, 0, DESTROY_TRACE },
95 { "list-markers", 0, 0, LIST_MARKERS },
96 { "enable-marker", 1, 0, ENABLE_MARKER },
97 { "disable-marker", 1, 0, DISABLE_MARKER },
98 { "help", 0, 0, 'h' },
99 { "online-pids", 0, 0, GET_ONLINE_PIDS },
100 { "set-subbuf-size", 1, 0, SET_SUBBUF_SIZE },
101 { "set-subbuf-num", 1, 0, SET_SUBBUF_NUM },
102 { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE },
103 { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM },
104 { "get-sock-path", 0, 0, GET_SOCK_PATH },
105 { "set-sock-path", 1, 0, SET_SOCK_PATH },
106 { 0, 0, 0, 0 }
107 };
108
109 c = getopt_long(argc, argv, "h", long_options, &option_index);
110 if (c == -1)
111 break;
112
113 if(c >= 1000)
114 opts->cmd = c;
115
116 switch (c) {
117 case 0:
118 printf("option %s", long_options[option_index].name);
119 if (optarg)
120 printf(" with arg %s", optarg);
121 printf("\n");
122 break;
123
124 case ENABLE_MARKER:
125 case DISABLE_MARKER:
126 case SET_SUBBUF_SIZE:
127 case SET_SUBBUF_NUM:
128 case GET_SUBBUF_SIZE:
129 case GET_SUBBUF_NUM:
130 case SET_SOCK_PATH:
131 opts->regex = strdup(optarg);
132 break;
133
134 case 'h':
135 usage();
136 exit(0);
137
138 case '?':
139 fprintf(stderr, "Invalid argument\n\n");
140 usage();
141 exit(1);
142 }
143 }
144
145 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
146 int i;
147 int pididx=0;
148 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
149
150 for(i=optind; i<argc; i++) {
151 /* don't take any chances, use a long long */
152 long long tmp;
153 char *endptr;
154 tmp = strtoull(argv[i], &endptr, 10);
155 if(*endptr != '\0') {
156 ERR("The pid \"%s\" is invalid.", argv[i]);
157 return 1;
158 }
159 opts->pids[pididx++] = (pid_t) tmp;
160 }
161 opts->pids[pididx] = -1;
162 }
163
164 return 0;
165 }
166
167 int main(int argc, char *argv[])
168 {
169 pid_t *pidit;
170 int result;
171 char *tmp;
172 struct ust_opts opts;
173
174 progname = argv[0];
175
176 if(argc <= 1) {
177 fprintf(stderr, "No operation specified.\n");
178 usage();
179 exit(EXIT_FAILURE);
180 }
181
182 result = parse_opts_long(argc, argv, &opts);
183 if(result) {
184 fprintf(stderr, "\n");
185 usage();
186 exit(EXIT_FAILURE);
187 }
188
189 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
190 fprintf(stderr, "No pid specified.\n");
191 usage();
192 exit(EXIT_FAILURE);
193 }
194 if(opts.cmd == UNKNOWN) {
195 fprintf(stderr, "No command specified.\n");
196 usage();
197 exit(EXIT_FAILURE);
198 }
199 if (opts.cmd == GET_ONLINE_PIDS) {
200 pid_t *pp = ustcmd_get_online_pids();
201 unsigned int i = 0;
202
203 if (pp) {
204 while (pp[i] != 0) {
205 printf("%u\n", (unsigned int) pp[i]);
206 ++i;
207 }
208 free(pp);
209 }
210
211 exit(EXIT_SUCCESS);
212 }
213
214 pidit = opts.pids;
215 struct marker_status *cmsf = NULL;
216
217 while(*pidit != -1) {
218 switch (opts.cmd) {
219 case CREATE_TRACE:
220 result = ustcmd_create_trace(*pidit);
221 if (result) {
222 ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit);
223 break;
224 }
225 break;
226
227 case START_TRACE:
228 result = ustcmd_start_trace(*pidit);
229 if (result) {
230 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
231 break;
232 }
233 break;
234
235 case STOP_TRACE:
236 result = ustcmd_stop_trace(*pidit);
237 if (result) {
238 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
239 break;
240 }
241 break;
242
243 case DESTROY_TRACE:
244 result = ustcmd_destroy_trace(*pidit);
245 if (result) {
246 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
247 break;
248 }
249 break;
250
251 case LIST_MARKERS:
252 cmsf = NULL;
253 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
254 fprintf(stderr,
255 "error while trying to list markers for"
256 " PID %u\n", (unsigned int) *pidit);
257 break;
258 }
259 unsigned int i = 0;
260 while (cmsf[i].channel != NULL) {
261 printf("{PID: %u, channel/marker: %s/%s, "
262 "state: %u, fmt: %s}\n",
263 (unsigned int) *pidit,
264 cmsf[i].channel,
265 cmsf[i].marker,
266 cmsf[i].state,
267 cmsf[i].fs);
268 ++i;
269 }
270 ustcmd_free_cmsf(cmsf);
271 break;
272
273 case ENABLE_MARKER:
274 if(opts.regex)
275 ustcmd_set_marker_state(opts.regex, 1, *pidit);
276 break;
277 case DISABLE_MARKER:
278 if(opts.regex)
279 ustcmd_set_marker_state(opts.regex, 0, *pidit);
280 break;
281
282 case SET_SUBBUF_SIZE:
283 ustcmd_set_subbuf_size(opts.regex, *pidit);
284 break;
285
286 case SET_SUBBUF_NUM:
287 ustcmd_set_subbuf_num(opts.regex, *pidit);
288 break;
289
290 case GET_SUBBUF_SIZE:
291 result = ustcmd_get_subbuf_size(opts.regex, *pidit);
292 if (result == -1) {
293 ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit);
294 break;
295 }
296
297 printf("the size of subbufers is %d\n", result);
298 break;
299
300 case GET_SUBBUF_NUM:
301 result = ustcmd_get_subbuf_num(opts.regex, *pidit);
302 if (result == -1) {
303 ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit);
304 break;
305 }
306
307 printf("the number of subbufers is %d\n", result);
308 break;
309
310 case ALLOC_TRACE:
311 result = ustcmd_alloc_trace(*pidit);
312 if (result) {
313 ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit);
314 break;
315 }
316 break;
317
318 case GET_SOCK_PATH:
319 result = ustcmd_get_sock_path(&tmp, *pidit);
320 if (result) {
321 ERR("error while trying to get sock path for PID %u\n", (unsigned int) *pidit);
322 break;
323 }
324 printf("the socket path is %s\n", tmp);
325 free(tmp);
326 break;
327
328 case SET_SOCK_PATH:
329 result = ustcmd_set_sock_path(opts.regex, *pidit);
330 if (result) {
331 ERR("error while trying to set sock path for PID %u\n", (unsigned int) *pidit);
332 }
333 break;
334
335 default:
336 ERR("unknown command\n");
337 break;
338 }
339
340 pidit++;
341 }
342
343 if (opts.pids != NULL) {
344 free(opts.pids);
345 }
346 if (opts.regex != NULL) {
347 free(opts.regex);
348 }
349
350 return 0;
351 }
352
This page took 0.037158 seconds and 3 git commands to generate.