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