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 | |
d6c9f207 | 25 | #include "ust/ustcmd.h" |
2028e7fd | 26 | #include "usterr.h" |
ae937656 PP |
27 | |
28 | enum command { | |
b0a5a08b PMF |
29 | CREATE_TRACE=1000, |
30 | ALLOC_TRACE, | |
ae937656 PP |
31 | START_TRACE, |
32 | STOP_TRACE, | |
b0a5a08b | 33 | DESTROY_TRACE, |
ae937656 | 34 | LIST_MARKERS, |
a3adfb05 | 35 | LIST_TRACE_EVENTS, |
ae937656 PP |
36 | ENABLE_MARKER, |
37 | DISABLE_MARKER, | |
38 | GET_ONLINE_PIDS, | |
763f41e5 DS |
39 | SET_SUBBUF_SIZE, |
40 | SET_SUBBUF_NUM, | |
e77b8e8e DS |
41 | GET_SUBBUF_SIZE, |
42 | GET_SUBBUF_NUM, | |
b2fb2f91 AH |
43 | GET_SOCK_PATH, |
44 | SET_SOCK_PATH, | |
b9318b35 | 45 | FORCE_SWITCH, |
ae937656 | 46 | UNKNOWN |
ef290fca | 47 | }; |
fbd8191b | 48 | |
52c51a47 | 49 | struct ust_opts { |
ae937656 | 50 | enum command cmd; |
52c51a47 | 51 | pid_t *pids; |
08230db7 | 52 | char *regex; |
52c51a47 PMF |
53 | }; |
54 | ||
fd2fb4f9 PMF |
55 | char *progname = NULL; |
56 | ||
57 | void usage(void) | |
58 | { | |
ae937656 | 59 | fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname); |
fd2fb4f9 PMF |
60 | fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\ |
61 | \n\ | |
62 | Commands:\n\ | |
62ec620f | 63 | --create-trace\t\t\tCreate trace\n\ |
4ed9f99d | 64 | --alloc-trace\t\t\tAlloc trace\n\ |
fd2fb4f9 PMF |
65 | --start-trace\t\t\tStart tracing\n\ |
66 | --stop-trace\t\t\tStop tracing\n\ | |
67 | --destroy-trace\t\t\tDestroy the trace\n\ | |
763f41e5 DS |
68 | --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\ |
69 | --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\ | |
b2fb2f91 | 70 | --set-sock-path\t\t\tSet the path of the daemon socket\n\ |
e77b8e8e DS |
71 | --get-subbuf-size \"CHANNEL\"\t\tGet the size of subbuffers per channel\n\ |
72 | --get-subbuf-num \"CHANNEL\"\t\tGet the number of subbuffers per channel\n\ | |
b2fb2f91 | 73 | --get-sock-path\t\t\tGet the path of the daemon socket\n\ |
ae937656 PP |
74 | --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\ |
75 | --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\ | |
76 | --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\ | |
a3adfb05 | 77 | --list-trace-events\t\t\tList the trace-events of the process\n\ |
b9318b35 | 78 | --force-switch\t\t\tForce a subbuffer switch\n\ |
ae937656 | 79 | \ |
fd2fb4f9 PMF |
80 | "); |
81 | } | |
82 | ||
52c51a47 | 83 | int parse_opts_long(int argc, char **argv, struct ust_opts *opts) |
fbd8191b | 84 | { |
52c51a47 | 85 | int c; |
52c51a47 | 86 | |
52c51a47 | 87 | opts->pids = NULL; |
ef290fca | 88 | opts->regex = NULL; |
52c51a47 PMF |
89 | |
90 | while (1) { | |
52c51a47 PMF |
91 | int option_index = 0; |
92 | static struct option long_options[] = { | |
b0a5a08b PMF |
93 | { "create-trace", 0, 0, CREATE_TRACE }, |
94 | { "alloc-trace", 0, 0, ALLOC_TRACE }, | |
95 | { "start-trace", 0, 0, START_TRACE }, | |
96 | { "stop-trace", 0, 0, STOP_TRACE }, | |
97 | { "destroy-trace", 0, 0, DESTROY_TRACE }, | |
98 | { "list-markers", 0, 0, LIST_MARKERS }, | |
a3adfb05 | 99 | { "list-trace-events", 0, 0, LIST_TRACE_EVENTS}, |
b0a5a08b PMF |
100 | { "enable-marker", 1, 0, ENABLE_MARKER }, |
101 | { "disable-marker", 1, 0, DISABLE_MARKER }, | |
102 | { "help", 0, 0, 'h' }, | |
103 | { "online-pids", 0, 0, GET_ONLINE_PIDS }, | |
104 | { "set-subbuf-size", 1, 0, SET_SUBBUF_SIZE }, | |
105 | { "set-subbuf-num", 1, 0, SET_SUBBUF_NUM }, | |
e77b8e8e DS |
106 | { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE }, |
107 | { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM }, | |
b2fb2f91 AH |
108 | { "get-sock-path", 0, 0, GET_SOCK_PATH }, |
109 | { "set-sock-path", 1, 0, SET_SOCK_PATH }, | |
b9318b35 | 110 | { "force-switch", 0, 0, FORCE_SWITCH }, |
b0a5a08b | 111 | { 0, 0, 0, 0 } |
52c51a47 PMF |
112 | }; |
113 | ||
d373b5cd | 114 | c = getopt_long(argc, argv, "h", long_options, &option_index); |
52c51a47 PMF |
115 | if (c == -1) |
116 | break; | |
117 | ||
b0a5a08b PMF |
118 | if(c >= 1000) |
119 | opts->cmd = c; | |
120 | ||
52c51a47 PMF |
121 | switch (c) { |
122 | case 0: | |
123 | printf("option %s", long_options[option_index].name); | |
124 | if (optarg) | |
125 | printf(" with arg %s", optarg); | |
126 | printf("\n"); | |
127 | break; | |
128 | ||
b0a5a08b PMF |
129 | case ENABLE_MARKER: |
130 | case DISABLE_MARKER: | |
131 | case SET_SUBBUF_SIZE: | |
132 | case SET_SUBBUF_NUM: | |
e77b8e8e DS |
133 | case GET_SUBBUF_SIZE: |
134 | case GET_SUBBUF_NUM: | |
b2fb2f91 | 135 | case SET_SOCK_PATH: |
ef290fca | 136 | opts->regex = strdup(optarg); |
ae937656 | 137 | break; |
b0a5a08b | 138 | |
d373b5cd PMF |
139 | case 'h': |
140 | usage(); | |
141 | exit(0); | |
b0a5a08b PMF |
142 | |
143 | case '?': | |
144 | fprintf(stderr, "Invalid argument\n\n"); | |
145 | usage(); | |
146 | exit(1); | |
fbd8191b PMF |
147 | } |
148 | } | |
149 | ||
772030fe | 150 | if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
151 | int i; |
152 | int pididx=0; | |
7032c7d3 | 153 | opts->pids = zmalloc((argc-optind+1) * sizeof(pid_t)); |
fbd8191b | 154 | |
52c51a47 | 155 | for(i=optind; i<argc; i++) { |
08230db7 PMF |
156 | /* don't take any chances, use a long long */ |
157 | long long tmp; | |
158 | char *endptr; | |
159 | tmp = strtoull(argv[i], &endptr, 10); | |
160 | if(*endptr != '\0') { | |
161 | ERR("The pid \"%s\" is invalid.", argv[i]); | |
162 | return 1; | |
163 | } | |
164 | opts->pids[pididx++] = (pid_t) tmp; | |
52c51a47 PMF |
165 | } |
166 | opts->pids[pididx] = -1; | |
fbd8191b PMF |
167 | } |
168 | ||
52c51a47 PMF |
169 | return 0; |
170 | } | |
fbd8191b | 171 | |
72098143 NC |
172 | static int scan_ch_marker(const char *channel_marker, char **channel, |
173 | char **marker) | |
174 | { | |
175 | int result; | |
176 | ||
177 | *channel = NULL; | |
178 | *marker = NULL; | |
179 | ||
180 | result = sscanf(channel_marker, "%a[^/]/%as", channel, marker); | |
181 | if (result != 2) { | |
182 | if (errno) { | |
183 | PERROR("Failed to read channel and marker names"); | |
184 | } else { | |
185 | ERR("Failed to parse marker and channel names"); | |
186 | } | |
187 | if (*channel) { | |
188 | free(*channel); | |
189 | } | |
190 | if (*marker) { | |
191 | free(*marker); | |
192 | } | |
193 | return -1; | |
194 | } else { | |
195 | return 0; | |
196 | } | |
197 | } | |
198 | ||
199 | static int scan_ch_and_num(const char *ch_num, char **channel, unsigned int *num) | |
200 | { | |
201 | int result; | |
202 | ||
203 | *channel = NULL; | |
204 | ||
205 | result = sscanf(ch_num, "%a[^/]/%u", channel, num); | |
206 | if (result != 2) { | |
207 | if (errno) { | |
208 | PERROR("Failed to parse channel and number"); | |
209 | } else { | |
210 | ERR("Failed to parse channel and number"); | |
211 | } | |
212 | if (*channel) { | |
213 | free(*channel); | |
214 | } | |
215 | return -1; | |
216 | } | |
217 | } | |
218 | ||
fbd8191b PMF |
219 | int main(int argc, char *argv[]) |
220 | { | |
52c51a47 | 221 | pid_t *pidit; |
52c51a47 | 222 | int result; |
1e620c53 | 223 | int retval = EXIT_SUCCESS; |
b2fb2f91 | 224 | char *tmp; |
52c51a47 | 225 | struct ust_opts opts; |
fbd8191b | 226 | |
52c51a47 | 227 | progname = argv[0]; |
fbd8191b | 228 | |
52c51a47 PMF |
229 | if(argc <= 1) { |
230 | fprintf(stderr, "No operation specified.\n"); | |
231 | usage(); | |
232 | exit(EXIT_FAILURE); | |
233 | } | |
234 | ||
235 | result = parse_opts_long(argc, argv, &opts); | |
236 | if(result) { | |
08230db7 | 237 | fprintf(stderr, "\n"); |
52c51a47 PMF |
238 | usage(); |
239 | exit(EXIT_FAILURE); | |
240 | } | |
241 | ||
ae937656 | 242 | if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
243 | fprintf(stderr, "No pid specified.\n"); |
244 | usage(); | |
245 | exit(EXIT_FAILURE); | |
246 | } | |
ae937656 | 247 | if(opts.cmd == UNKNOWN) { |
52c51a47 PMF |
248 | fprintf(stderr, "No command specified.\n"); |
249 | usage(); | |
250 | exit(EXIT_FAILURE); | |
251 | } | |
ae937656 | 252 | if (opts.cmd == GET_ONLINE_PIDS) { |
08230db7 | 253 | pid_t *pp = ustcmd_get_online_pids(); |
ae937656 | 254 | unsigned int i = 0; |
52c51a47 | 255 | |
ae937656 PP |
256 | if (pp) { |
257 | while (pp[i] != 0) { | |
258 | printf("%u\n", (unsigned int) pp[i]); | |
259 | ++i; | |
260 | } | |
261 | free(pp); | |
52c51a47 | 262 | } |
ef290fca | 263 | |
ae937656 PP |
264 | exit(EXIT_SUCCESS); |
265 | } | |
52c51a47 | 266 | |
ae937656 | 267 | pidit = opts.pids; |
08230db7 | 268 | struct marker_status *cmsf = NULL; |
a3adfb05 NC |
269 | struct trace_event_status *tes = NULL; |
270 | unsigned int i = 0; | |
ef290fca | 271 | |
ae937656 PP |
272 | while(*pidit != -1) { |
273 | switch (opts.cmd) { | |
62ec620f PMF |
274 | case CREATE_TRACE: |
275 | result = ustcmd_create_trace(*pidit); | |
276 | if (result) { | |
277 | ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 278 | retval = EXIT_FAILURE; |
62ec620f PMF |
279 | break; |
280 | } | |
281 | break; | |
282 | ||
ae937656 | 283 | case START_TRACE: |
08230db7 PMF |
284 | result = ustcmd_start_trace(*pidit); |
285 | if (result) { | |
286 | ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 287 | retval = EXIT_FAILURE; |
08230db7 PMF |
288 | break; |
289 | } | |
ae937656 | 290 | break; |
ef290fca | 291 | |
ae937656 | 292 | case STOP_TRACE: |
08230db7 PMF |
293 | result = ustcmd_stop_trace(*pidit); |
294 | if (result) { | |
295 | ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 296 | retval = EXIT_FAILURE; |
08230db7 PMF |
297 | break; |
298 | } | |
ae937656 | 299 | break; |
ef290fca | 300 | |
b0a5a08b | 301 | case DESTROY_TRACE: |
08230db7 PMF |
302 | result = ustcmd_destroy_trace(*pidit); |
303 | if (result) { | |
304 | ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 305 | retval = EXIT_FAILURE; |
08230db7 PMF |
306 | break; |
307 | } | |
ae937656 | 308 | break; |
ef290fca | 309 | |
ae937656 | 310 | case LIST_MARKERS: |
08230db7 PMF |
311 | cmsf = NULL; |
312 | if (ustcmd_get_cmsf(&cmsf, *pidit)) { | |
1e620c53 DG |
313 | ERR("error while trying to list markers for PID %u\n", (unsigned int) *pidit); |
314 | retval = EXIT_FAILURE; | |
08230db7 PMF |
315 | break; |
316 | } | |
a3adfb05 | 317 | i = 0; |
08230db7 PMF |
318 | while (cmsf[i].channel != NULL) { |
319 | printf("{PID: %u, channel/marker: %s/%s, " | |
264f6231 | 320 | "state: %u, fmt: %s}\n", |
08230db7 PMF |
321 | (unsigned int) *pidit, |
322 | cmsf[i].channel, | |
323 | cmsf[i].marker, | |
324 | cmsf[i].state, | |
325 | cmsf[i].fs); | |
326 | ++i; | |
327 | } | |
328 | ustcmd_free_cmsf(cmsf); | |
ae937656 | 329 | break; |
ef290fca | 330 | |
a3adfb05 NC |
331 | case LIST_TRACE_EVENTS: |
332 | tes = NULL; | |
333 | if (ustcmd_get_tes(&tes, *pidit)) { | |
334 | ERR("error while trying to list " | |
335 | "trace_events for PID %u\n", | |
336 | (unsigned int) *pidit); | |
337 | break; | |
338 | } | |
339 | i = 0; | |
340 | while (tes[i].name != NULL) { | |
341 | printf("{PID: %u, trace_event: %s}\n", | |
342 | (unsigned int) *pidit, | |
343 | tes[i].name); | |
344 | ++i; | |
345 | } | |
346 | ustcmd_free_tes(tes); | |
347 | ||
348 | break; | |
ae937656 | 349 | case ENABLE_MARKER: |
1e620c53 | 350 | if (opts.regex) { |
72098143 NC |
351 | char *channel, *marker; |
352 | ||
353 | if (scan_ch_marker(opts.regex, | |
354 | &channel, &marker)) { | |
355 | retval = EXIT_FAILURE; | |
356 | break; | |
357 | } | |
358 | if (ustcmd_set_marker_state(channel, marker, 1, *pidit)) { | |
359 | PERROR("error while trying to enable marker %s with PID %u", | |
360 | opts.regex, (unsigned int) *pidit); | |
1e620c53 DG |
361 | retval = EXIT_FAILURE; |
362 | } | |
363 | } | |
72098143 | 364 | |
ee648b8f | 365 | break; |
ae937656 | 366 | case DISABLE_MARKER: |
1e620c53 | 367 | if (opts.regex) { |
72098143 NC |
368 | char *channel, *marker; |
369 | ||
370 | if (scan_ch_marker(opts.regex, | |
371 | &channel, &marker)) { | |
372 | retval = EXIT_FAILURE; | |
373 | break; | |
374 | } | |
375 | if (ustcmd_set_marker_state(channel, marker, 0, *pidit)) { | |
1e620c53 DG |
376 | ERR("error while trying to disable marker %s with PID %u\n", |
377 | opts.regex, (unsigned int) *pidit); | |
378 | retval = EXIT_FAILURE; | |
379 | } | |
380 | } | |
ee648b8f | 381 | break; |
ef290fca | 382 | |
763f41e5 | 383 | case SET_SUBBUF_SIZE: |
1e620c53 | 384 | if (opts.regex) { |
72098143 NC |
385 | char *channel; |
386 | unsigned int size; | |
387 | if (scan_ch_and_num(opts.regex, &channel, &size)) { | |
388 | retval = EXIT_FAILURE; | |
389 | break; | |
390 | } | |
391 | ||
392 | if (ustcmd_set_subbuf_size(channel, size, *pidit)) { | |
1e620c53 DG |
393 | ERR("error while trying to set the size of subbuffers with PID %u\n", |
394 | (unsigned int) *pidit); | |
395 | retval = EXIT_FAILURE; | |
396 | } | |
397 | } | |
763f41e5 DS |
398 | break; |
399 | ||
400 | case SET_SUBBUF_NUM: | |
1e620c53 | 401 | if (opts.regex) { |
72098143 NC |
402 | char *channel; |
403 | unsigned int num; | |
404 | if (scan_ch_and_num(opts.regex, &channel, &num)) { | |
405 | retval = EXIT_FAILURE; | |
406 | break; | |
407 | } | |
408 | ||
409 | if (num < 2) { | |
410 | ERR("Subbuffer count should be greater or equal to 2"); | |
411 | retval = EXIT_FAILURE; | |
412 | break; | |
413 | } | |
414 | if (ustcmd_set_subbuf_num(channel, num, *pidit)) { | |
1e620c53 DG |
415 | ERR("error while trying to set the number of subbuffers with PID %u\n", |
416 | (unsigned int) *pidit); | |
417 | retval = EXIT_FAILURE; | |
418 | } | |
419 | } | |
763f41e5 DS |
420 | break; |
421 | ||
e77b8e8e DS |
422 | case GET_SUBBUF_SIZE: |
423 | result = ustcmd_get_subbuf_size(opts.regex, *pidit); | |
424 | if (result == -1) { | |
425 | ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 426 | retval = EXIT_FAILURE; |
e77b8e8e DS |
427 | break; |
428 | } | |
429 | ||
430 | printf("the size of subbufers is %d\n", result); | |
431 | break; | |
432 | ||
433 | case GET_SUBBUF_NUM: | |
434 | result = ustcmd_get_subbuf_num(opts.regex, *pidit); | |
435 | if (result == -1) { | |
436 | ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 437 | retval = EXIT_FAILURE; |
e77b8e8e DS |
438 | break; |
439 | } | |
440 | ||
441 | printf("the number of subbufers is %d\n", result); | |
442 | break; | |
443 | ||
763f41e5 DS |
444 | case ALLOC_TRACE: |
445 | result = ustcmd_alloc_trace(*pidit); | |
446 | if (result) { | |
447 | ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 448 | retval = EXIT_FAILURE; |
763f41e5 DS |
449 | } |
450 | break; | |
451 | ||
b2fb2f91 AH |
452 | case GET_SOCK_PATH: |
453 | result = ustcmd_get_sock_path(&tmp, *pidit); | |
454 | if (result) { | |
455 | ERR("error while trying to get sock path for PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 456 | retval = EXIT_FAILURE; |
b2fb2f91 AH |
457 | break; |
458 | } | |
459 | printf("the socket path is %s\n", tmp); | |
460 | free(tmp); | |
461 | break; | |
462 | ||
463 | case SET_SOCK_PATH: | |
464 | result = ustcmd_set_sock_path(opts.regex, *pidit); | |
465 | if (result) { | |
466 | ERR("error while trying to set sock path for PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 467 | retval = EXIT_FAILURE; |
b2fb2f91 AH |
468 | } |
469 | break; | |
470 | ||
b9318b35 AH |
471 | case FORCE_SWITCH: |
472 | result = ustcmd_force_switch(*pidit); | |
473 | if (result) { | |
474 | ERR("error while trying to force switch for PID %u\n", (unsigned int) *pidit); | |
1e620c53 | 475 | retval = EXIT_FAILURE; |
b9318b35 AH |
476 | } |
477 | break; | |
478 | ||
ae937656 | 479 | default: |
08230db7 | 480 | ERR("unknown command\n"); |
1e620c53 DG |
481 | retval = EXIT_FAILURE; |
482 | break; | |
ae937656 | 483 | } |
ef290fca | 484 | |
52c51a47 PMF |
485 | pidit++; |
486 | } | |
82b1a169 | 487 | |
ef290fca PMF |
488 | if (opts.pids != NULL) { |
489 | free(opts.pids); | |
490 | } | |
491 | if (opts.regex != NULL) { | |
492 | free(opts.regex); | |
ae937656 | 493 | } |
fbd8191b | 494 | |
1e620c53 | 495 | return retval; |
fbd8191b | 496 | } |
ae937656 | 497 |