cleanups
[ust.git] / ust / ust.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 #include <regex.h>
25
26 #include "ustcomm.h"
27 #include "ustcmd.h"
28 #include "usterr.h"
29
30 enum command {
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 regex_t preg;
47 int regex_state;
48 };
49
50 char *progname = NULL;
51
52 void usage(void)
53 {
54 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
55 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
56 \n\
57 Commands:\n\
58 --start-trace\t\t\tStart tracing\n\
59 --stop-trace\t\t\tStop tracing\n\
60 --destroy-trace\t\t\tDestroy the trace\n\
61 --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\
62 --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\
63 --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\
64 \
65 ");
66 }
67
68 int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
69 {
70 int c;
71
72 opts->pids = NULL;
73 opts->regex = NULL;
74 opts->regex_state = -1;
75
76 while (1) {
77 int option_index = 0;
78 static struct option long_options[] = {
79 {"start-trace", 0, 0, 1000},
80 {"stop-trace", 0, 0, 1001},
81 {"destroy-trace", 0, 0, 1002},
82 {"list-markers", 0, 0, 1004},
83 {"print-markers", 0, 0, 1005},
84 {"pid", 1, 0, 1006},
85 {"enable-marker", 1, 0, 1007},
86 {"disable-marker", 1, 0, 1008},
87 {"start", 0, 0, 1009},
88 {"help", 0, 0, 'h'},
89 {"version", 0, 0, 1010},
90 {"online-pids", 0, 0, 1011},
91 {0, 0, 0, 0}
92 };
93
94 c = getopt_long(argc, argv, "h", long_options, &option_index);
95 if (c == -1)
96 break;
97
98 switch (c) {
99 case 0:
100 printf("option %s", long_options[option_index].name);
101 if (optarg)
102 printf(" with arg %s", optarg);
103 printf("\n");
104 break;
105
106 case 1000:
107 opts->cmd = START_TRACE;
108 break;
109 case 1001:
110 opts->cmd = STOP_TRACE;
111 break;
112 case 1009:
113 opts->cmd = START;
114 break;
115 case 1002:
116 opts->cmd = DESTROY;
117 break;
118 case 1004:
119 opts->cmd = LIST_MARKERS;
120 break;
121 case 1007:
122 opts->cmd = ENABLE_MARKER;
123 opts->regex = strdup(optarg);
124 break;
125 case 1008:
126 opts->cmd = DISABLE_MARKER;
127 opts->regex = strdup(optarg);
128 break;
129 case 1011:
130 opts->cmd = GET_ONLINE_PIDS;
131 break;
132 case 'h':
133 usage();
134 exit(0);
135 case 1010:
136 printf("Version 0.1\n");
137
138 default:
139 /* unknown option or other error; error is
140 printed by getopt, just return */
141 opts->cmd = UNKNOWN;
142 return 1;
143 }
144 }
145
146 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
147 int i;
148 int pididx=0;
149 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
150
151 for(i=optind; i<argc; i++) {
152 /* don't take any chances, use a long long */
153 long long tmp;
154 char *endptr;
155 tmp = strtoull(argv[i], &endptr, 10);
156 if(*endptr != '\0') {
157 ERR("The pid \"%s\" is invalid.", argv[i]);
158 return 1;
159 }
160 opts->pids[pididx++] = (pid_t) tmp;
161 }
162 opts->pids[pididx] = -1;
163 }
164
165 if (opts->cmd == ENABLE_MARKER || opts->cmd == DISABLE_MARKER) {
166 opts->regex_state = regcomp(&opts->preg, opts->regex, 0);
167 if (opts->regex_state) {
168 ERR("invalid regular expression\n");
169 }
170 }
171
172 return 0;
173 }
174
175 static void regex_change_m_state(struct ust_opts *opts, pid_t pid)
176 {
177 struct marker_status *cmsf = NULL;
178 unsigned int i = 0;
179 int e = (opts->cmd == ENABLE_MARKER);
180
181 if (opts->regex_state != 0) {
182 return;
183 }
184
185 if (ustcmd_get_cmsf(&cmsf, pid)) {
186 fprintf(stderr, "error while trying to get markers for PID "
187 "%u\n", (unsigned int) pid);
188 return;
189 }
190 while (cmsf[i].channel != NULL) {
191 char *mc;
192 asprintf(&mc, "%s/%s", cmsf[i].channel, cmsf[i].marker);
193 if (regexec(&opts->preg, mc, 0, NULL, 0) == 0) {
194 /* We got a match! */
195 if (ustcmd_set_marker_state(mc,
196 e ? USTCMD_MS_ON : USTCMD_MS_OFF, pid)) {
197 fprintf(stderr,
198 "error while trying to %sable marker"
199 "\"%s\" for PID %u\n",
200 e ? "en" : "dis", mc,
201 (unsigned int) pid);
202 } else {
203 printf("sucessfully %sabled marker "
204 "\"%s\" for PID %u\n",
205 e ? "en" : "dis", mc,
206 (unsigned int) pid);
207 }
208 }
209 free(mc);
210 ++i;
211 }
212 ustcmd_free_cmsf(cmsf);
213 }
214
215 int main(int argc, char *argv[])
216 {
217 pid_t *pidit;
218 int result;
219 struct ust_opts opts;
220
221 progname = argv[0];
222
223 if(argc <= 1) {
224 fprintf(stderr, "No operation specified.\n");
225 usage();
226 exit(EXIT_FAILURE);
227 }
228
229 result = parse_opts_long(argc, argv, &opts);
230 if(result) {
231 fprintf(stderr, "\n");
232 usage();
233 exit(EXIT_FAILURE);
234 }
235
236 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
237 fprintf(stderr, "No pid specified.\n");
238 usage();
239 exit(EXIT_FAILURE);
240 }
241 if(opts.cmd == UNKNOWN) {
242 fprintf(stderr, "No command specified.\n");
243 usage();
244 exit(EXIT_FAILURE);
245 }
246 if (opts.cmd == GET_ONLINE_PIDS) {
247 pid_t *pp = ustcmd_get_online_pids();
248 unsigned int i = 0;
249
250 if (pp) {
251 while (pp[i] != 0) {
252 printf("%u\n", (unsigned int) pp[i]);
253 ++i;
254 }
255 free(pp);
256 }
257
258 exit(EXIT_SUCCESS);
259 }
260
261 pidit = opts.pids;
262 struct marker_status *cmsf = NULL;
263
264 while(*pidit != -1) {
265 switch (opts.cmd) {
266 case START_TRACE:
267 result = ustcmd_start_trace(*pidit);
268 if (result) {
269 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
270 break;
271 }
272 //printf("sucessfully started trace for PID %u\n", (unsigned int) *pidit);
273 break;
274
275 case STOP_TRACE:
276 result = ustcmd_stop_trace(*pidit);
277 if (result) {
278 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
279 break;
280 }
281 //printf("sucessfully stopped trace for PID %u\n", (unsigned int) *pidit);
282 break;
283
284 case START:
285 result = ustcmd_setup_and_start(*pidit);
286 if (result) {
287 ERR("error while trying to setup/start trace for PID %u\n", (unsigned int) *pidit);
288 break;
289 }
290 //printf("sucessfully setup/started trace for PID %u\n", (unsigned int) *pidit);
291 break;
292
293 case DESTROY:
294 result = ustcmd_destroy_trace(*pidit);
295 if (result) {
296 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
297 break;
298 }
299 //printf("sucessfully destroyed trace for PID %u\n", (unsigned int) *pidit);
300 break;
301
302 case LIST_MARKERS:
303 cmsf = NULL;
304 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
305 fprintf(stderr,
306 "error while trying to list markers for"
307 " PID %u\n", (unsigned int) *pidit);
308 break;
309 }
310 unsigned int i = 0;
311 while (cmsf[i].channel != NULL) {
312 printf("{PID: %u, channel/marker: %s/%s, "
313 "state: %u, fs: %s}\n",
314 (unsigned int) *pidit,
315 cmsf[i].channel,
316 cmsf[i].marker,
317 cmsf[i].state,
318 cmsf[i].fs);
319 ++i;
320 }
321 ustcmd_free_cmsf(cmsf);
322 break;
323
324 case ENABLE_MARKER:
325 case DISABLE_MARKER:
326 regex_change_m_state(&opts, *pidit);
327 break;
328
329 default:
330 ERR("unknown command\n");
331 break;
332 }
333
334 pidit++;
335 }
336
337 if (opts.pids != NULL) {
338 free(opts.pids);
339 }
340 if (opts.regex != NULL) {
341 free(opts.regex);
342 }
343
344 return 0;
345 }
346
This page took 0.036644 seconds and 4 git commands to generate.