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