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