1 /* Copyright (C) 2009 Pierre-Marc Fournier
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.
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.
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
25 #include "ust/ustcmd.h"
55 char *progname
= NULL
;
59 fprintf(stderr
, "usage: %s COMMAND PIDs...\n", progname
);
60 fprintf(stderr
, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
63 --create-trace\t\t\tCreate trace\n\
64 --alloc-trace\t\t\tAlloc trace\n\
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\
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\
70 --set-sock-path\t\t\tSet the path of the daemon socket\n\
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\
73 --get-sock-path\t\t\tGet the path of the daemon socket\n\
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\
77 --list-trace-events\t\t\tList the trace-events of the process\n\
78 --force-switch\t\t\tForce a subbuffer switch\n\
83 int parse_opts_long(int argc
, char **argv
, struct ust_opts
*opts
)
92 static struct option long_options
[] = {
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
},
99 { "list-trace-events", 0, 0, LIST_TRACE_EVENTS
},
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
},
106 { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE
},
107 { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM
},
108 { "get-sock-path", 0, 0, GET_SOCK_PATH
},
109 { "set-sock-path", 1, 0, SET_SOCK_PATH
},
110 { "force-switch", 0, 0, FORCE_SWITCH
},
114 c
= getopt_long(argc
, argv
, "h", long_options
, &option_index
);
123 printf("option %s", long_options
[option_index
].name
);
125 printf(" with arg %s", optarg
);
131 case SET_SUBBUF_SIZE
:
133 case GET_SUBBUF_SIZE
:
136 opts
->regex
= strdup(optarg
);
144 fprintf(stderr
, "Invalid argument\n\n");
150 if (argc
- optind
> 0 && opts
->cmd
!= GET_ONLINE_PIDS
) {
153 opts
->pids
= zmalloc((argc
-optind
+1) * sizeof(pid_t
));
155 for(i
=optind
; i
<argc
; i
++) {
156 /* don't take any chances, use a long long */
159 tmp
= strtoull(argv
[i
], &endptr
, 10);
160 if(*endptr
!= '\0') {
161 ERR("The pid \"%s\" is invalid.", argv
[i
]);
164 opts
->pids
[pididx
++] = (pid_t
) tmp
;
166 opts
->pids
[pididx
] = -1;
172 static int scan_ch_marker(const char *channel_marker
, char **channel
,
180 result
= sscanf(channel_marker
, "%a[^/]/%as", channel
, marker
);
183 PERROR("Failed to read channel and marker names");
185 ERR("Failed to parse marker and channel names");
199 static int scan_ch_and_num(const char *ch_num
, char **channel
, unsigned int *num
)
205 result
= sscanf(ch_num
, "%a[^/]/%u", channel
, num
);
208 PERROR("Failed to parse channel and number");
210 ERR("Failed to parse channel and number");
219 int main(int argc
, char *argv
[])
223 int retval
= EXIT_SUCCESS
;
225 struct ust_opts opts
;
230 fprintf(stderr
, "No operation specified.\n");
235 result
= parse_opts_long(argc
, argv
, &opts
);
237 fprintf(stderr
, "\n");
242 if(opts
.pids
== NULL
&& opts
.cmd
!= GET_ONLINE_PIDS
) {
243 fprintf(stderr
, "No pid specified.\n");
247 if(opts
.cmd
== UNKNOWN
) {
248 fprintf(stderr
, "No command specified.\n");
252 if (opts
.cmd
== GET_ONLINE_PIDS
) {
253 pid_t
*pp
= ustcmd_get_online_pids();
258 printf("%u\n", (unsigned int) pp
[i
]);
268 struct marker_status
*cmsf
= NULL
;
269 struct trace_event_status
*tes
= NULL
;
272 while(*pidit
!= -1) {
275 result
= ustcmd_create_trace(*pidit
);
277 ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit
);
278 retval
= EXIT_FAILURE
;
284 result
= ustcmd_start_trace(*pidit
);
286 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit
);
287 retval
= EXIT_FAILURE
;
293 result
= ustcmd_stop_trace(*pidit
);
295 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit
);
296 retval
= EXIT_FAILURE
;
302 result
= ustcmd_destroy_trace(*pidit
);
304 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit
);
305 retval
= EXIT_FAILURE
;
312 if (ustcmd_get_cmsf(&cmsf
, *pidit
)) {
313 ERR("error while trying to list markers for PID %u\n", (unsigned int) *pidit
);
314 retval
= EXIT_FAILURE
;
318 while (cmsf
[i
].channel
!= NULL
) {
319 printf("{PID: %u, channel/marker: %s/%s, "
320 "state: %u, fmt: %s}\n",
321 (unsigned int) *pidit
,
328 ustcmd_free_cmsf(cmsf
);
331 case LIST_TRACE_EVENTS
:
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
);
340 while (tes
[i
].name
!= NULL
) {
341 printf("{PID: %u, trace_event: %s}\n",
342 (unsigned int) *pidit
,
346 ustcmd_free_tes(tes
);
351 char *channel
, *marker
;
353 if (scan_ch_marker(opts
.regex
,
354 &channel
, &marker
)) {
355 retval
= EXIT_FAILURE
;
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
);
361 retval
= EXIT_FAILURE
;
368 char *channel
, *marker
;
370 if (scan_ch_marker(opts
.regex
,
371 &channel
, &marker
)) {
372 retval
= EXIT_FAILURE
;
375 if (ustcmd_set_marker_state(channel
, marker
, 0, *pidit
)) {
376 ERR("error while trying to disable marker %s with PID %u\n",
377 opts
.regex
, (unsigned int) *pidit
);
378 retval
= EXIT_FAILURE
;
383 case SET_SUBBUF_SIZE
:
387 if (scan_ch_and_num(opts
.regex
, &channel
, &size
)) {
388 retval
= EXIT_FAILURE
;
392 if (ustcmd_set_subbuf_size(channel
, size
, *pidit
)) {
393 ERR("error while trying to set the size of subbuffers with PID %u\n",
394 (unsigned int) *pidit
);
395 retval
= EXIT_FAILURE
;
404 if (scan_ch_and_num(opts
.regex
, &channel
, &num
)) {
405 retval
= EXIT_FAILURE
;
410 ERR("Subbuffer count should be greater or equal to 2");
411 retval
= EXIT_FAILURE
;
414 if (ustcmd_set_subbuf_num(channel
, num
, *pidit
)) {
415 ERR("error while trying to set the number of subbuffers with PID %u\n",
416 (unsigned int) *pidit
);
417 retval
= EXIT_FAILURE
;
422 case GET_SUBBUF_SIZE
:
423 result
= ustcmd_get_subbuf_size(opts
.regex
, *pidit
);
425 ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit
);
426 retval
= EXIT_FAILURE
;
430 printf("the size of subbufers is %d\n", result
);
434 result
= ustcmd_get_subbuf_num(opts
.regex
, *pidit
);
436 ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit
);
437 retval
= EXIT_FAILURE
;
441 printf("the number of subbufers is %d\n", result
);
445 result
= ustcmd_alloc_trace(*pidit
);
447 ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit
);
448 retval
= EXIT_FAILURE
;
453 result
= ustcmd_get_sock_path(&tmp
, *pidit
);
455 ERR("error while trying to get sock path for PID %u\n", (unsigned int) *pidit
);
456 retval
= EXIT_FAILURE
;
459 printf("the socket path is %s\n", tmp
);
464 result
= ustcmd_set_sock_path(opts
.regex
, *pidit
);
466 ERR("error while trying to set sock path for PID %u\n", (unsigned int) *pidit
);
467 retval
= EXIT_FAILURE
;
472 result
= ustcmd_force_switch(*pidit
);
474 ERR("error while trying to force switch for PID %u\n", (unsigned int) *pidit
);
475 retval
= EXIT_FAILURE
;
480 ERR("unknown command\n");
481 retval
= EXIT_FAILURE
;
488 if (opts
.pids
!= NULL
) {
491 if (opts
.regex
!= NULL
) {
This page took 0.039663 seconds and 4 git commands to generate.