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