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