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