Move kernelcompat.h to include/ust/ and share.h, usterr.h to include/
[ust.git] / ustctl / ustctl.c
CommitLineData
c39c72ee
PMF
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
872037bb
PMF
18#define _GNU_SOURCE
19#include <stdio.h>
fbd8191b
PMF
20#include <unistd.h>
21#include <getopt.h>
22#include <stdlib.h>
fbd8191b 23#include <fcntl.h>
fbd8191b 24
f9e5ce61 25#include "ustcomm.h"
ae937656 26#include "ustcmd.h"
2028e7fd 27#include "usterr.h"
ae937656
PP
28
29enum 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
ef290fca 39};
fbd8191b 40
52c51a47 41struct ust_opts {
ae937656 42 enum command cmd;
52c51a47 43 pid_t *pids;
08230db7 44 char *regex;
52c51a47
PMF
45};
46
fd2fb4f9
PMF
47char *progname = NULL;
48
49void usage(void)
50{
ae937656 51 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
fd2fb4f9
PMF
52 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
53\n\
54Commands:\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\
ae937656
PP
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\
fd2fb4f9
PMF
62");
63}
64
52c51a47 65int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
fbd8191b 66{
52c51a47 67 int c;
52c51a47 68
52c51a47 69 opts->pids = NULL;
ef290fca 70 opts->regex = NULL;
52c51a47
PMF
71
72 while (1) {
52c51a47
PMF
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},
d373b5cd
PMF
84 {"help", 0, 0, 'h'},
85 {"version", 0, 0, 1010},
ae937656 86 {"online-pids", 0, 0, 1011},
52c51a47
PMF
87 {0, 0, 0, 0}
88 };
89
d373b5cd 90 c = getopt_long(argc, argv, "h", long_options, &option_index);
52c51a47
PMF
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:
ae937656 103 opts->cmd = START_TRACE;
52c51a47
PMF
104 break;
105 case 1001:
ae937656 106 opts->cmd = STOP_TRACE;
52c51a47
PMF
107 break;
108 case 1009:
ae937656 109 opts->cmd = START;
52c51a47
PMF
110 break;
111 case 1002:
ae937656 112 opts->cmd = DESTROY;
52c51a47
PMF
113 break;
114 case 1004:
ae937656 115 opts->cmd = LIST_MARKERS;
52c51a47
PMF
116 break;
117 case 1007:
ae937656 118 opts->cmd = ENABLE_MARKER;
ef290fca 119 opts->regex = strdup(optarg);
52c51a47
PMF
120 break;
121 case 1008:
ae937656 122 opts->cmd = DISABLE_MARKER;
ef290fca 123 opts->regex = strdup(optarg);
ae937656
PP
124 break;
125 case 1011:
126 opts->cmd = GET_ONLINE_PIDS;
52c51a47 127 break;
d373b5cd
PMF
128 case 'h':
129 usage();
130 exit(0);
131 case 1010:
ae937656 132 printf("Version 0.1\n");
52c51a47
PMF
133
134 default:
ae937656
PP
135 /* unknown option or other error; error is
136 printed by getopt, just return */
137 opts->cmd = UNKNOWN;
52c51a47 138 return 1;
fbd8191b
PMF
139 }
140 }
141
772030fe 142 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
52c51a47
PMF
143 int i;
144 int pididx=0;
145 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
fbd8191b 146
52c51a47 147 for(i=optind; i<argc; i++) {
08230db7
PMF
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;
52c51a47
PMF
157 }
158 opts->pids[pididx] = -1;
fbd8191b
PMF
159 }
160
52c51a47
PMF
161 return 0;
162}
fbd8191b 163
fbd8191b
PMF
164int main(int argc, char *argv[])
165{
52c51a47 166 pid_t *pidit;
52c51a47
PMF
167 int result;
168 struct ust_opts opts;
fbd8191b 169
52c51a47 170 progname = argv[0];
fbd8191b 171
52c51a47
PMF
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) {
08230db7 180 fprintf(stderr, "\n");
52c51a47
PMF
181 usage();
182 exit(EXIT_FAILURE);
183 }
184
ae937656 185 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
52c51a47
PMF
186 fprintf(stderr, "No pid specified.\n");
187 usage();
188 exit(EXIT_FAILURE);
189 }
ae937656 190 if(opts.cmd == UNKNOWN) {
52c51a47
PMF
191 fprintf(stderr, "No command specified.\n");
192 usage();
193 exit(EXIT_FAILURE);
194 }
ae937656 195 if (opts.cmd == GET_ONLINE_PIDS) {
08230db7 196 pid_t *pp = ustcmd_get_online_pids();
ae937656 197 unsigned int i = 0;
52c51a47 198
ae937656
PP
199 if (pp) {
200 while (pp[i] != 0) {
201 printf("%u\n", (unsigned int) pp[i]);
202 ++i;
203 }
204 free(pp);
52c51a47 205 }
ef290fca 206
ae937656
PP
207 exit(EXIT_SUCCESS);
208 }
52c51a47 209
ae937656 210 pidit = opts.pids;
08230db7 211 struct marker_status *cmsf = NULL;
ef290fca 212
ae937656
PP
213 while(*pidit != -1) {
214 switch (opts.cmd) {
215 case START_TRACE:
08230db7
PMF
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);
ae937656 222 break;
ef290fca 223
ae937656 224 case STOP_TRACE:
08230db7
PMF
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);
ae937656 231 break;
ef290fca 232
ae937656 233 case START:
08230db7
PMF
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);
ae937656 240 break;
ef290fca 241
ae937656 242 case DESTROY:
08230db7
PMF
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);
ae937656 249 break;
ef290fca 250
ae937656 251 case LIST_MARKERS:
08230db7
PMF
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, "
264f6231 262 "state: %u, fmt: %s}\n",
08230db7
PMF
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);
ae937656 271 break;
ef290fca 272
ae937656 273 case ENABLE_MARKER:
ee648b8f
PMF
274 if(opts.regex)
275 ustcmd_set_marker_state(opts.regex, 1, *pidit);
276 break;
ae937656 277 case DISABLE_MARKER:
ee648b8f
PMF
278 if(opts.regex)
279 ustcmd_set_marker_state(opts.regex, 0, *pidit);
280 break;
ef290fca 281
ae937656 282 default:
08230db7 283 ERR("unknown command\n");
ae937656
PP
284 break;
285 }
ef290fca 286
52c51a47
PMF
287 pidit++;
288 }
82b1a169 289
ef290fca
PMF
290 if (opts.pids != NULL) {
291 free(opts.pids);
292 }
293 if (opts.regex != NULL) {
294 free(opts.regex);
ae937656 295 }
fbd8191b
PMF
296
297 return 0;
298}
ae937656 299
This page took 0.035607 seconds and 4 git commands to generate.