822ef4c9f505b3be009856510bf4972b52457c96
[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 UNKNOWN
44 };
45
46 struct ust_opts {
47 enum command cmd;
48 pid_t *pids;
49 char *regex;
50 };
51
52 char *progname = NULL;
53
54 void usage(void)
55 {
56 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
57 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
58 \n\
59 Commands:\n\
60 --create-trace\t\t\tCreate trace\n\
61 --alloc-trace\t\t\tAlloc trace\n\
62 --start-trace\t\t\tStart tracing\n\
63 --stop-trace\t\t\tStop tracing\n\
64 --destroy-trace\t\t\tDestroy the trace\n\
65 --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\
66 --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\
67 --get-subbuf-size \"CHANNEL\"\t\tGet the size of subbuffers per channel\n\
68 --get-subbuf-num \"CHANNEL\"\t\tGet the number of subbuffers per channel\n\
69 --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\
70 --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\
71 --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\
72 \
73 ");
74 }
75
76 int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
77 {
78 int c;
79
80 opts->pids = NULL;
81 opts->regex = NULL;
82
83 while (1) {
84 int option_index = 0;
85 static struct option long_options[] = {
86 { "create-trace", 0, 0, CREATE_TRACE },
87 { "alloc-trace", 0, 0, ALLOC_TRACE },
88 { "start-trace", 0, 0, START_TRACE },
89 { "stop-trace", 0, 0, STOP_TRACE },
90 { "destroy-trace", 0, 0, DESTROY_TRACE },
91 { "list-markers", 0, 0, LIST_MARKERS },
92 { "enable-marker", 1, 0, ENABLE_MARKER },
93 { "disable-marker", 1, 0, DISABLE_MARKER },
94 { "help", 0, 0, 'h' },
95 { "online-pids", 0, 0, GET_ONLINE_PIDS },
96 { "set-subbuf-size", 1, 0, SET_SUBBUF_SIZE },
97 { "set-subbuf-num", 1, 0, SET_SUBBUF_NUM },
98 { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE },
99 { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM },
100 { 0, 0, 0, 0 }
101 };
102
103 c = getopt_long(argc, argv, "h", long_options, &option_index);
104 if (c == -1)
105 break;
106
107 if(c >= 1000)
108 opts->cmd = c;
109
110 switch (c) {
111 case 0:
112 printf("option %s", long_options[option_index].name);
113 if (optarg)
114 printf(" with arg %s", optarg);
115 printf("\n");
116 break;
117
118 case ENABLE_MARKER:
119 case DISABLE_MARKER:
120 case SET_SUBBUF_SIZE:
121 case SET_SUBBUF_NUM:
122 case GET_SUBBUF_SIZE:
123 case GET_SUBBUF_NUM:
124 opts->regex = strdup(optarg);
125 break;
126
127 case 'h':
128 usage();
129 exit(0);
130
131 case '?':
132 fprintf(stderr, "Invalid argument\n\n");
133 usage();
134 exit(1);
135 }
136 }
137
138 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
139 int i;
140 int pididx=0;
141 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
142
143 for(i=optind; i<argc; i++) {
144 /* don't take any chances, use a long long */
145 long long tmp;
146 char *endptr;
147 tmp = strtoull(argv[i], &endptr, 10);
148 if(*endptr != '\0') {
149 ERR("The pid \"%s\" is invalid.", argv[i]);
150 return 1;
151 }
152 opts->pids[pididx++] = (pid_t) tmp;
153 }
154 opts->pids[pididx] = -1;
155 }
156
157 return 0;
158 }
159
160 int main(int argc, char *argv[])
161 {
162 pid_t *pidit;
163 int result;
164 struct ust_opts opts;
165
166 progname = argv[0];
167
168 if(argc <= 1) {
169 fprintf(stderr, "No operation specified.\n");
170 usage();
171 exit(EXIT_FAILURE);
172 }
173
174 result = parse_opts_long(argc, argv, &opts);
175 if(result) {
176 fprintf(stderr, "\n");
177 usage();
178 exit(EXIT_FAILURE);
179 }
180
181 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
182 fprintf(stderr, "No pid specified.\n");
183 usage();
184 exit(EXIT_FAILURE);
185 }
186 if(opts.cmd == UNKNOWN) {
187 fprintf(stderr, "No command specified.\n");
188 usage();
189 exit(EXIT_FAILURE);
190 }
191 if (opts.cmd == GET_ONLINE_PIDS) {
192 pid_t *pp = ustcmd_get_online_pids();
193 unsigned int i = 0;
194
195 if (pp) {
196 while (pp[i] != 0) {
197 printf("%u\n", (unsigned int) pp[i]);
198 ++i;
199 }
200 free(pp);
201 }
202
203 exit(EXIT_SUCCESS);
204 }
205
206 pidit = opts.pids;
207 struct marker_status *cmsf = NULL;
208
209 while(*pidit != -1) {
210 switch (opts.cmd) {
211 case CREATE_TRACE:
212 result = ustcmd_create_trace(*pidit);
213 if (result) {
214 ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit);
215 break;
216 }
217 break;
218
219 case START_TRACE:
220 result = ustcmd_start_trace(*pidit);
221 if (result) {
222 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
223 break;
224 }
225 break;
226
227 case STOP_TRACE:
228 result = ustcmd_stop_trace(*pidit);
229 if (result) {
230 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
231 break;
232 }
233 break;
234
235 case DESTROY_TRACE:
236 result = ustcmd_destroy_trace(*pidit);
237 if (result) {
238 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
239 break;
240 }
241 break;
242
243 case LIST_MARKERS:
244 cmsf = NULL;
245 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
246 fprintf(stderr,
247 "error while trying to list markers for"
248 " PID %u\n", (unsigned int) *pidit);
249 break;
250 }
251 unsigned int i = 0;
252 while (cmsf[i].channel != NULL) {
253 printf("{PID: %u, channel/marker: %s/%s, "
254 "state: %u, fmt: %s}\n",
255 (unsigned int) *pidit,
256 cmsf[i].channel,
257 cmsf[i].marker,
258 cmsf[i].state,
259 cmsf[i].fs);
260 ++i;
261 }
262 ustcmd_free_cmsf(cmsf);
263 break;
264
265 case ENABLE_MARKER:
266 if(opts.regex)
267 ustcmd_set_marker_state(opts.regex, 1, *pidit);
268 break;
269 case DISABLE_MARKER:
270 if(opts.regex)
271 ustcmd_set_marker_state(opts.regex, 0, *pidit);
272 break;
273
274 case SET_SUBBUF_SIZE:
275 ustcmd_set_subbuf_size(opts.regex, *pidit);
276 break;
277
278 case SET_SUBBUF_NUM:
279 ustcmd_set_subbuf_num(opts.regex, *pidit);
280 break;
281
282 case GET_SUBBUF_SIZE:
283 result = ustcmd_get_subbuf_size(opts.regex, *pidit);
284 if (result == -1) {
285 ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit);
286 break;
287 }
288
289 printf("the size of subbufers is %d\n", result);
290 break;
291
292 case GET_SUBBUF_NUM:
293 result = ustcmd_get_subbuf_num(opts.regex, *pidit);
294 if (result == -1) {
295 ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit);
296 break;
297 }
298
299 printf("the number of subbufers is %d\n", result);
300 break;
301
302 case ALLOC_TRACE:
303 result = ustcmd_alloc_trace(*pidit);
304 if (result) {
305 ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit);
306 break;
307 }
308 break;
309
310 default:
311 ERR("unknown command\n");
312 break;
313 }
314
315 pidit++;
316 }
317
318 if (opts.pids != NULL) {
319 free(opts.pids);
320 }
321 if (opts.regex != NULL) {
322 free(opts.regex);
323 }
324
325 return 0;
326 }
327
This page took 0.034421 seconds and 3 git commands to generate.