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