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