e5631928053c4930b57f5ae35b9754a7070b5a85
3 * Linux Trace Toolkit Control
5 * Small program that controls LTTng through liblttctl.
7 * Copyright 2008 FUJITSU
8 * Zhao Lei <zhaolei@cn.fujitsu.com>
9 * Gui Jianfeng <guijianfeng@cn.fujitsu.com>
10 * Copyright 2009-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <liblttctl/lttctl.h>
42 #define OPT_MAX (1024)
43 #define OPT_NAMELEN (256)
44 #define OPT_VALSTRINGLEN (256)
50 struct channel_option
{
51 char chan_name
[OPT_NAMELEN
];
59 struct lttctl_option
{
61 struct channel_option chan_opt
;
64 struct lttctl_option
*next
;
67 struct lttctl_option
*opt_head
, *last_opt
;
69 static int opt_create
;
70 static int opt_destroy
;
74 static const char *opt_transport
;
75 static const char *opt_write
;
76 static int opt_append
;
77 static unsigned int opt_dump_threads
;
78 static char channel_root_default
[PATH_MAX
];
79 static const char *opt_channel_root
;
80 static const char *opt_tracename
;
85 static void show_arguments(void)
87 printf("Linux Trace Toolkit Trace Control " VERSION
"\n");
89 printf("Usage: lttctl [OPTION]... [TRACENAME]\n");
91 printf("Examples:\n");
92 printf(" lttctl -c trace1 "
93 "# Create a trace named trace1.\n");
94 printf(" lttctl -s trace1 "
95 "# start a trace named trace1.\n");
96 printf(" lttctl -p trace1 "
97 "# pause a trace named trace1.\n");
98 printf(" lttctl -d trace1 "
99 "# Destroy a trace named trace1.\n");
100 printf(" lttctl -C -w /tmp/trace1 trace1 "
101 "# Create a trace named trace1, start it and\n"
103 "# write non-overwrite channels' data to\n"
105 "# /tmp/trace1, debugfs must be mounted for\n"
108 printf(" lttctl -D -w /tmp/trace1 trace1 "
109 "# Pause and destroy a trace named trace1 and\n"
111 "# write overwrite channels' data to\n"
113 "# /tmp/trace1, debugfs must be mounted for\n"
117 printf(" Basic options:\n");
118 printf(" -c, --create\n");
119 printf(" Create a trace.\n");
120 printf(" -d, --destroy\n");
121 printf(" Destroy a trace.\n");
122 printf(" -s, --start\n");
123 printf(" Start a trace.\n");
124 printf(" -p, --pause\n");
125 printf(" Pause a trace.\n");
126 printf(" -h, --help\n");
127 printf(" Show this help.\n");
129 printf(" Advanced options:\n");
130 printf(" --transport TRANSPORT\n");
131 printf(" Set trace's transport. (ex. relay-locked or relay)\n");
132 printf(" -o, --option OPTION\n");
133 printf(" Set options, following operations are supported:\n");
134 printf(" channel.<channelname>.enable=\n");
135 printf(" channel.<channelname>.overwrite=\n");
136 printf(" channel.<channelname>.bufnum=\n");
137 printf(" channel.<channelname>.bufsize= (in bytes, rounded to "
138 "next power of 2)\n");
139 printf(" <channelname> can be set to all for all channels\n");
140 printf(" channel.<channelname>.switch_timer= (timer interval in "
143 printf(" Integration options:\n");
144 printf(" -C, --create_start\n");
145 printf(" Create and start a trace.\n");
146 printf(" -D, --pause_destroy\n");
147 printf(" Pause and destroy a trace.\n");
148 printf(" -w, --write PATH\n");
149 printf(" Path for write trace datas.\n");
150 printf(" For -c, -C, -d, -D options\n");
151 printf(" -a, --append\n");
152 printf(" Append to trace, For -w option\n");
153 printf(" -n, --dump_threads NUMBER\n");
154 printf(" Number of lttd threads, For -w option\n");
155 printf(" --channel_root PATH\n");
156 printf(" Set channels root path, For -w option."
157 " (ex. /mnt/debugfs/ltt)\n");
162 * Separate option name to 3 fields
164 * Input: name = channel.cpu.bufsize
165 * Output: name1 = channel
172 * Make sure that name1~3 longer than OPT_NAMELEN.
173 * name1~3 can be NULL to discard value
176 static int separate_opt(const char *name
, char *name1
, char *name2
, char *name3
)
184 p
= strchr(name
, '.');
187 if (p
- name
>= OPT_NAMELEN
)
190 memcpy(name1
, name
, p
- name
);
196 p
= strchr(name
, '.');
199 if (p
- name
>= OPT_NAMELEN
)
202 memcpy(name2
, name
, p
- name
);
208 if (strlen(name
) >= OPT_NAMELEN
)
216 static void init_channel_opt(struct channel_option
*opt
, char *opt_name
)
218 if (opt
&& opt_name
) {
223 opt
->switch_timer
= -1;
224 strcpy(opt
->chan_name
, opt_name
);
228 static struct lttctl_option
*find_insert_channel_opt(char *opt_name
)
230 struct lttctl_option
*iter
, *new_opt
;
233 opt_head
= (struct lttctl_option
*)malloc(sizeof(struct lttctl_option
));
234 init_channel_opt(&opt_head
->opt_mode
.chan_opt
, opt_name
);
235 opt_head
->type
= CHANNEL
;
236 opt_head
->next
= NULL
;
241 for (iter
= opt_head
; iter
; iter
= iter
->next
) {
242 if (iter
->type
!= CHANNEL
)
244 if (!strcmp(iter
->opt_mode
.chan_opt
.chan_name
, opt_name
))
248 new_opt
= (struct lttctl_option
*)malloc(sizeof(struct lttctl_option
));
249 init_channel_opt(&new_opt
->opt_mode
.chan_opt
, opt_name
);
250 new_opt
->type
= CHANNEL
;
251 new_opt
->next
= NULL
;
252 last_opt
->next
= new_opt
;
257 int set_channel_opt(struct channel_option
*opt
, char *opt_name
, char *opt_valstr
)
261 if (!strcmp("enable", opt_name
)) {
262 if (opt_valstr
[1] != 0) {
265 if (opt_valstr
[0] == 'Y' || opt_valstr
[0] == 'y'
266 || opt_valstr
[0] == '1')
268 else if (opt_valstr
[0] == 'N' || opt_valstr
[0] == 'n'
269 || opt_valstr
[0] == '0')
275 opt
->enable
= opt_val
;
277 } else if (!strcmp("overwrite", opt_name
)) {
278 if (opt_valstr
[1] != 0) {
281 if (opt_valstr
[0] == 'Y' || opt_valstr
[0] == 'y'
282 || opt_valstr
[0] == '1')
284 else if (opt_valstr
[0] == 'N' || opt_valstr
[0] == 'n'
285 || opt_valstr
[0] == '0')
291 opt
->overwrite
= opt_val
;
294 } else if (!strcmp("bufnum", opt_name
)) {
295 ret
= sscanf(opt_valstr
, "%d", &opt_val
);
296 if (ret
!= 1 || opt_val
< 0) {
300 opt
->bufnum
= opt_val
;
302 } else if (!strcmp("bufsize", opt_name
)) {
303 ret
= sscanf(opt_valstr
, "%d", &opt_val
);
304 if (ret
!= 1 || opt_val
< 0) {
308 opt
->bufsize
= opt_val
;
310 } else if (!strcmp("switch_timer", opt_name
)) {
311 ret
= sscanf(opt_valstr
, "%d", &opt_val
);
312 if (ret
!= 1 || opt_val
< 0) {
316 opt
->switch_timer
= opt_val
;
324 static int parst_opt(const char *optarg
)
327 char opt_name
[OPT_NAMELEN
* 3];
328 char opt_valstr
[OPT_VALSTRINGLEN
];
331 char name1
[OPT_NAMELEN
];
332 char name2
[OPT_NAMELEN
];
333 char name3
[OPT_NAMELEN
];
337 unsigned int opt_uintval
;
338 struct lttctl_option
*opt
;
341 fprintf(stderr
, "Option empty\n");
345 /* Get option name and val_str */
346 p
= strchr(optarg
, '=');
348 fprintf(stderr
, "Option format error: %s\n", optarg
);
352 if (p
- optarg
>= sizeof(opt_name
)/sizeof(opt_name
[0])) {
353 fprintf(stderr
, "Option name too long: %s\n", optarg
);
357 if (strlen(p
+1) >= OPT_VALSTRINGLEN
) {
358 fprintf(stderr
, "Option value too long: %s\n", optarg
);
362 memcpy(opt_name
, optarg
, p
- optarg
);
363 opt_name
[p
- optarg
] = 0;
364 strcpy(opt_valstr
, p
+1);
366 /* separate option name into 3 fields */
367 ret
= separate_opt(opt_name
, name1
, name2
, name3
);
369 fprintf(stderr
, "Option name error1: %s\n", optarg
);
373 if (!strcmp("channel", name1
)) {
374 opt
= find_insert_channel_opt(name2
);
375 if ((ret
= set_channel_opt(&opt
->opt_mode
.chan_opt
,
376 name3
, opt_valstr
) != 0)) {
377 fprintf(stderr
, "Option name error2: %s\n", optarg
);
381 fprintf(stderr
, "Option name error3: %s\n", optarg
);
390 * Parses the command line arguments.
392 * Returns -1 if the arguments were correct, but doesn't ask for program
393 * continuation. Returns EINVAL if the arguments are incorrect, or 0 if OK.
395 static int parse_arguments(int argc
, char **argv
)
399 static struct option longopts
[] = {
400 {"create", no_argument
, NULL
, 'c'},
401 {"destroy", no_argument
, NULL
, 'd'},
402 {"start", no_argument
, NULL
, 's'},
403 {"pause", no_argument
, NULL
, 'p'},
404 {"help", no_argument
, NULL
, 'h'},
405 {"transport", required_argument
, NULL
, 2},
406 {"option", required_argument
, NULL
, 'o'},
407 {"create_start", no_argument
, NULL
, 'C'},
408 {"pause_destroy", no_argument
, NULL
, 'D'},
409 {"write", required_argument
, NULL
, 'w'},
410 {"append", no_argument
, NULL
, 'a'},
411 {"dump_threads", required_argument
, NULL
, 'n'},
412 {"channel_root", required_argument
, NULL
, 3},
413 { NULL
, 0, NULL
, 0 },
417 * Enable all channels in default
418 * To make novice users happy
420 parst_opt("channel.all.enable=1");
422 opterr
= 1; /* Print error message on getopt_long */
425 c
= getopt_long(argc
, argv
, "cdspho:CDw:an:", longopts
, NULL
);
447 if (!opt_transport
) {
448 opt_transport
= optarg
;
451 "Please specify only 1 transport\n");
456 ret
= parst_opt(optarg
);
473 "Please specify only 1 write dir\n");
481 if (opt_dump_threads
) {
483 "Please specify only 1 dump threads\n");
487 ret
= sscanf(optarg
, "%u", &opt_dump_threads
);
490 "Dump threads not positive number\n");
495 if (!opt_channel_root
) {
496 opt_channel_root
= optarg
;
499 "Please specify only 1 channel root\n");
510 /* Don't check args when user needs help */
515 if (optind
< argc
- 1) {
516 fprintf(stderr
, "Please specify only 1 trace name\n");
519 if (optind
> argc
- 1) {
520 fprintf(stderr
, "Please specify trace name\n");
523 opt_tracename
= argv
[optind
];
528 if (!opt_create
&& !opt_start
&& !opt_destroy
&& !opt_pause
) {
530 "Please specify a option of "
531 "create, destroy, start, or pause\n");
535 if ((opt_create
|| opt_start
) && (opt_destroy
|| opt_pause
)) {
537 "Create and start conflict with destroy and pause\n");
543 opt_transport
= "relay";
549 "Transport option must be combine with create"
556 if (!opt_create
&& !opt_destroy
) {
558 "Write option must be combine with create or"
559 " destroy option\n");
563 if (!opt_channel_root
)
564 if (getdebugfsmntdir(channel_root_default
) == 0) {
565 strcat(channel_root_default
, "/ltt");
566 opt_channel_root
= channel_root_default
;
569 "Channel_root is necessary for -w"
570 " option, but neither --channel_root"
572 "specified, nor debugfs's mount dir"
573 " found, mount debugfs also failed\n");
577 if (opt_dump_threads
== 0)
578 opt_dump_threads
= 1;
584 "Append option must be combine with write"
590 if (opt_dump_threads
) {
593 "Dump_threads option must be combine with write"
599 if (opt_channel_root
) {
602 "Channel_root option must be combine with write"
611 static void show_info(void)
613 printf("Linux Trace Toolkit Trace Control " VERSION
"\n");
615 if (opt_tracename
!= NULL
) {
616 printf("Controlling trace : %s\n", opt_tracename
);
621 static int lttctl_channel_setup(struct channel_option
*opt
)
625 if (opt
->enable
!= -1) {
626 if ((ret
= lttctl_set_channel_enable(opt_tracename
,
631 if (opt
->overwrite
!= -1) {
632 if ((ret
= lttctl_set_channel_overwrite(opt_tracename
,
634 opt
->overwrite
)) != 0)
637 if (opt
->bufnum
!= -1) {
638 if ((ret
= lttctl_set_channel_subbuf_num(opt_tracename
,
643 if (opt
->bufsize
!= -1) {
644 if ((ret
= lttctl_set_channel_subbuf_size(opt_tracename
,
649 if (opt
->switch_timer
!= -1) {
650 if ((ret
= lttctl_set_channel_switch_timer(opt_tracename
,
651 opt
->chan_name
, opt
->switch_timer
)) != 0)
658 static int lttctl_create_trace(void)
662 struct lttctl_option
*opt
;
664 ret
= lttctl_setup_trace(opt_tracename
);
666 goto setup_trace_fail
;
668 for (opt
= opt_head
; opt
; opt
= opt
->next
) {
669 if (opt
->type
!= CHANNEL
)
671 ret
= lttctl_channel_setup(&opt
->opt_mode
.chan_opt
);
673 goto set_option_fail
;;
676 ret
= lttctl_set_trans(opt_tracename
, opt_transport
);
678 goto set_option_fail
;
680 ret
= lttctl_alloc_trace(opt_tracename
);
682 goto alloc_trace_fail
;
688 lttctl_destroy_trace(opt_tracename
);
694 * Start a lttd daemon to write trace datas
695 * Dump overwrite channels on overwrite!=0
696 * Dump normal(non-overwrite) channels on overwrite=0
701 static int lttctl_daemon(int overwrite
)
708 perror("Error in forking for lttd daemon");
716 char channel_path
[PATH_MAX
];
720 argv
[argc
] = getenv("LTT_DAEMON");
721 if (argv
[argc
] == NULL
)
722 argv
[argc
] = PACKAGE_BIN_DIR
"/lttd";
729 * we allow modify of opt_write's content in new process
730 * for get rid of warning of assign char * to const char *
732 argv
[argc
] = (char *)opt_write
;
736 strcpy(channel_path
, opt_channel_root
);
737 strcat(channel_path
, "/");
738 strcat(channel_path
, opt_tracename
);
741 argv
[argc
] = channel_path
;
745 sprintf(thread_num
, "%u", opt_dump_threads
);
748 argv
[argc
] = thread_num
;
761 /* overwrite option */
772 execvp(argv
[0], argv
);
774 perror("Error in executing the lttd daemon");
779 if (waitpid(pid
, &status
, 0) == -1) {
780 perror("Error in waitpid\n");
784 if (!WIFEXITED(status
)) {
785 fprintf(stderr
, "lttd process interrupted\n");
789 if (WEXITSTATUS(status
))
790 fprintf(stderr
, "lttd process running failed\n");
792 return WEXITSTATUS(status
);
795 int main(int argc
, char **argv
)
799 ret
= parse_arguments(argc
, argv
);
800 /* If user needs show help, we disregard other options */
806 /* exit program if arguments wrong */
817 printf("lttctl: Creating trace\n");
818 ret
= lttctl_create_trace();
823 printf("lttctl: Forking lttd\n");
824 ret
= lttctl_daemon(0);
831 printf("lttctl: Starting trace\n");
832 ret
= lttctl_start(opt_tracename
);
838 printf("lttctl: Pausing trace\n");
839 ret
= lttctl_pause(opt_tracename
);
846 printf("lttctl: Forking lttd\n");
847 ret
= lttctl_daemon(1);
852 printf("lttctl: Destroying trace\n");
853 ret
= lttctl_destroy_trace(opt_tracename
);
This page took 0.07215 seconds and 4 git commands to generate.