Add --create-trace option to ustctl
[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 UNKNOWN
40 };
41
42 struct ust_opts {
43 enum command cmd;
44 pid_t *pids;
45 char *regex;
46 };
47
48 char *progname = NULL;
49
50 void usage(void)
51 {
52 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
53 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
54 \n\
55 Commands:\n\
56 --create-trace\t\t\tCreate trace\n\
57 --start-trace\t\t\tStart tracing\n\
58 --stop-trace\t\t\tStop tracing\n\
59 --destroy-trace\t\t\tDestroy the trace\n\
60 --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\
61 --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\
62 --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\
63 \
64 ");
65 }
66
67 int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
68 {
69 int c;
70
71 opts->pids = NULL;
72 opts->regex = NULL;
73
74 while (1) {
75 int option_index = 0;
76 static struct option long_options[] = {
77 {"create-trace", 0, 0, 1012},
78 {"start-trace", 0, 0, 1000},
79 {"stop-trace", 0, 0, 1001},
80 {"destroy-trace", 0, 0, 1002},
81 {"list-markers", 0, 0, 1004},
82 {"print-markers", 0, 0, 1005},
83 {"pid", 1, 0, 1006},
84 {"enable-marker", 1, 0, 1007},
85 {"disable-marker", 1, 0, 1008},
86 {"start", 0, 0, 1009},
87 {"help", 0, 0, 'h'},
88 {"version", 0, 0, 1010},
89 {"online-pids", 0, 0, 1011},
90 {0, 0, 0, 0}
91 };
92
93 c = getopt_long(argc, argv, "h", long_options, &option_index);
94 if (c == -1)
95 break;
96
97 switch (c) {
98 case 0:
99 printf("option %s", long_options[option_index].name);
100 if (optarg)
101 printf(" with arg %s", optarg);
102 printf("\n");
103 break;
104
105 case 1000:
106 opts->cmd = START_TRACE;
107 break;
108 case 1001:
109 opts->cmd = STOP_TRACE;
110 break;
111 case 1009:
112 opts->cmd = START;
113 break;
114 case 1002:
115 opts->cmd = DESTROY;
116 break;
117 case 1004:
118 opts->cmd = LIST_MARKERS;
119 break;
120 case 1007:
121 opts->cmd = ENABLE_MARKER;
122 opts->regex = strdup(optarg);
123 break;
124 case 1008:
125 opts->cmd = DISABLE_MARKER;
126 opts->regex = strdup(optarg);
127 break;
128 case 1011:
129 opts->cmd = GET_ONLINE_PIDS;
130 break;
131 case 'h':
132 usage();
133 exit(0);
134 case 1010:
135 printf("Version 0.1\n");
136 break;
137 case 1012:
138 opts->cmd = CREATE_TRACE;
139 break;
140 default:
141 /* unknown option or other error; error is
142 printed by getopt, just return */
143 opts->cmd = UNKNOWN;
144 return 1;
145 }
146 }
147
148 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
149 int i;
150 int pididx=0;
151 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
152
153 for(i=optind; i<argc; i++) {
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;
163 }
164 opts->pids[pididx] = -1;
165 }
166
167 return 0;
168 }
169
170 int main(int argc, char *argv[])
171 {
172 pid_t *pidit;
173 int result;
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 //printf("sucessfully started trace for PID %u\n", (unsigned int) *pidit);
236 break;
237
238 case STOP_TRACE:
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 }
244 //printf("sucessfully stopped trace for PID %u\n", (unsigned int) *pidit);
245 break;
246
247 case START:
248 result = ustcmd_setup_and_start(*pidit);
249 if (result) {
250 ERR("error while trying to setup/start trace for PID %u\n", (unsigned int) *pidit);
251 break;
252 }
253 //printf("sucessfully setup/started trace for PID %u\n", (unsigned int) *pidit);
254 break;
255
256 case DESTROY:
257 result = ustcmd_destroy_trace(*pidit);
258 if (result) {
259 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
260 break;
261 }
262 //printf("sucessfully destroyed trace for PID %u\n", (unsigned int) *pidit);
263 break;
264
265 case LIST_MARKERS:
266 cmsf = NULL;
267 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
268 fprintf(stderr,
269 "error while trying to list markers for"
270 " PID %u\n", (unsigned int) *pidit);
271 break;
272 }
273 unsigned int i = 0;
274 while (cmsf[i].channel != NULL) {
275 printf("{PID: %u, channel/marker: %s/%s, "
276 "state: %u, fmt: %s}\n",
277 (unsigned int) *pidit,
278 cmsf[i].channel,
279 cmsf[i].marker,
280 cmsf[i].state,
281 cmsf[i].fs);
282 ++i;
283 }
284 ustcmd_free_cmsf(cmsf);
285 break;
286
287 case ENABLE_MARKER:
288 if(opts.regex)
289 ustcmd_set_marker_state(opts.regex, 1, *pidit);
290 break;
291 case DISABLE_MARKER:
292 if(opts.regex)
293 ustcmd_set_marker_state(opts.regex, 0, *pidit);
294 break;
295
296 default:
297 ERR("unknown command\n");
298 break;
299 }
300
301 pidit++;
302 }
303
304 if (opts.pids != NULL) {
305 free(opts.pids);
306 }
307 if (opts.regex != NULL) {
308 free(opts.regex);
309 }
310
311 return 0;
312 }
313
This page took 0.052206 seconds and 5 git commands to generate.