d572973acd540dbb1c1b00db3bdb7addfe25f3be
[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
29 enum 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
39 };
40
41 struct ust_opts {
42 enum command cmd;
43 pid_t *pids;
44 char* regex;
45 regex_t preg;
46 int regex_state;
47 };
48
49 char *progname = NULL;
50
51 void usage(void)
52 {
53 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
54 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
55 \n\
56 Commands:\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 opts->regex_state = -1;
74
75 while (1) {
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},
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
137 default:
138 /* unknown option or other error; error is
139 printed by getopt, just return */
140 opts->cmd = UNKNOWN;
141 return 1;
142 }
143 }
144
145 if(argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
146 int i;
147 int pididx=0;
148 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
149
150 for(i=optind; i<argc; i++) {
151 opts->pids[pididx++] = atoi(argv[i]);
152 }
153 opts->pids[pididx] = -1;
154 }
155
156 return 0;
157 }
158
159 int main(int argc, char *argv[])
160 {
161 pid_t *pidit;
162 //char *msg = argv[2];
163 struct ustcomm_connection conn;
164 int result;
165 struct ust_opts opts;
166
167 progname = argv[0];
168
169 if(argc <= 1) {
170 fprintf(stderr, "No operation specified.\n");
171 usage();
172 exit(EXIT_FAILURE);
173 }
174
175 result = parse_opts_long(argc, argv, &opts);
176 if(result) {
177 usage();
178 exit(EXIT_FAILURE);
179 }
180
181 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
182 fprintf(stderr, "No pid specified.\n");
183 usage();
184 exit(EXIT_FAILURE);
185 }
186 if(opts.cmd == UNKNOWN) {
187 fprintf(stderr, "No command specified.\n");
188 usage();
189 exit(EXIT_FAILURE);
190 }
191 if (opts.cmd == GET_ONLINE_PIDS) {
192 pid_t* pp = ustcmd_get_online_pids();
193 unsigned int i = 0;
194
195 if (pp) {
196 while (pp[i] != 0) {
197 printf("%u\n", (unsigned int) pp[i]);
198 ++i;
199 }
200 free(pp);
201 }
202
203 exit(EXIT_SUCCESS);
204 }
205
206 pidit = opts.pids;
207 struct USTcmd_cmsf* cmsf = NULL;
208
209 while(*pidit != -1) {
210 switch (opts.cmd) {
211 case START_TRACE:
212 if (ustcmd_start_trace(*pidit)) {
213 fprintf(stderr,
214 "error while trying to for trace "
215 "with PID %u\n", (unsigned int) *pidit);
216 break;
217 }
218 printf("sucessfully started trace for PID %u\n",
219 (unsigned int) *pidit);
220 break;
221
222 case STOP_TRACE:
223 if (ustcmd_stop_trace(*pidit)) {
224 fprintf(stderr,
225 "error while trying to stop trace "
226 "for PID %u\n", (unsigned int) *pidit);
227 break;
228 }
229 printf("sucessfully stopped trace for PID %u\n",
230 (unsigned int) *pidit);
231 break;
232
233 case START:
234 if (ustcmd_setup_and_start(*pidit)) {
235 fprintf(stderr,
236 "error while trying to setup/start "
237 "trace for PID %u\n",
238 (unsigned int) *pidit);
239 break;
240 }
241 printf("sucessfully setup/started trace for PID %u\n",
242 (unsigned int) *pidit);
243 break;
244
245 case DESTROY:
246 if (ustcmd_destroy_trace(*pidit)) {
247 fprintf(stderr,
248 "error while trying to destroy "
249 "trace with PID %u\n",
250 (unsigned int) *pidit);
251 break;
252 }
253 printf("sucessfully destroyed trace for PID %u\n",
254 (unsigned int) *pidit);
255 break;
256
257 case LIST_MARKERS:
258 cmsf = NULL;
259 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
260 fprintf(stderr,
261 "error while trying to list markers for"
262 " PID %u\n", (unsigned int) *pidit);
263 break;
264 }
265 unsigned int i = 0;
266 while (cmsf[i].channel != NULL) {
267 printf("{PID: %u, channel/marker: %s/%s, "
268 "state: %u, fs: %s}\n",
269 (unsigned int) *pidit,
270 cmsf[i].channel,
271 cmsf[i].marker,
272 cmsf[i].state,
273 cmsf[i].fs);
274 ++i;
275 }
276 ustcmd_free_cmsf(cmsf);
277 break;
278
279 case ENABLE_MARKER:
280 case DISABLE_MARKER:
281 regex_change_m_state(&opts, *pidit);
282 break;
283
284 default:
285 fprintf(stderr, "error: unknown command...\n");
286 break;
287 }
288
289 pidit++;
290 }
291
292 exit_free:
293 if (opts.pids != NULL) {
294 free(opts.pids);
295 }
296 if (opts.regex != NULL) {
297 free(opts.regex);
298 }
299
300 return 0;
301 }
302
This page took 0.0339 seconds and 3 git commands to generate.