3 * Linux Trace Toolkit Control
5 * Small program that controls LTT through libltt.
8 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
10 * Copyright 2008 FUJITSU
11 * Zhao Lei <zhaolei@cn.fujitsu.com>
12 * Gui Jianfeng <guijianfeng@cn.fujitsu.com>
19 #include <liblttctl/lttctl.h>
30 #define OPT_MAX (1024)
31 #define OPT_NAMELEN (256)
32 #define OPT_VALSTRINGLEN (256)
38 struct channel_option
{
39 char chan_name
[OPT_NAMELEN
];
46 struct lttctl_option
{
48 struct channel_option chan_opt
;
51 struct lttctl_option
*next
;
54 struct lttctl_option
*opt_head
, *last_opt
;
56 static int opt_create
;
57 static int opt_destroy
;
61 static const char *opt_transport
;
62 static const char *opt_write
;
63 static int opt_append
;
64 static unsigned int opt_dump_threads
;
65 static char channel_root_default
[PATH_MAX
];
66 static const char *opt_channel_root
;
67 static const char *opt_tracename
;
72 static void show_arguments(void)
74 printf("Linux Trace Toolkit Trace Control " VERSION
"\n");
76 printf("Usage: lttctl [OPTION]... [TRACENAME]\n");
78 printf("Examples:\n");
79 printf(" lttctl -c trace1 "
80 "# Create a trace named trace1.\n");
81 printf(" lttctl -s trace1 "
82 "# start a trace named trace1.\n");
83 printf(" lttctl -p trace1 "
84 "# pause a trace named trace1.\n");
85 printf(" lttctl -d trace1 "
86 "# Destroy a trace named trace1.\n");
87 printf(" lttctl -C -w /tmp/trace1 trace1 "
88 "# Create a trace named trace1, start it and\n"
90 "# write non-overwrite channels' data to\n"
92 "# /tmp/trace1, debugfs must be mounted for\n"
95 printf(" lttctl -D -w /tmp/trace1 trace1 "
96 "# Pause and destroy a trace named trace1 and\n"
98 "# write overwrite channels' data to\n"
100 "# /tmp/trace1, debugfs must be mounted for\n"
104 printf(" Basic options:\n");
105 printf(" -c, --create\n");
106 printf(" Create a trace.\n");
107 printf(" -d, --destroy\n");
108 printf(" Destroy a trace.\n");
109 printf(" -s, --start\n");
110 printf(" Start a trace.\n");
111 printf(" -p, --pause\n");
112 printf(" Pause a trace.\n");
113 printf(" -h, --help\n");
114 printf(" Show this help.\n");
116 printf(" Advanced options:\n");
117 printf(" --transport TRANSPORT\n");
118 printf(" Set trace's transport. (ex. relay)\n");
119 printf(" -o, --option OPTION\n");
120 printf(" Set options, following operations are supported:\n");
121 printf(" channel.<channelname>.enable=\n");
122 printf(" channel.<channelname>.overwrite=\n");
123 printf(" channel.<channelname>.bufnum=\n");
124 printf(" channel.<channelname>.bufsize=\n");
125 printf(" <channelname> can be set to all for all channels\n");
127 printf(" Integration options:\n");
128 printf(" -C, --create_start\n");
129 printf(" Create and start a trace.\n");
130 printf(" -D, --pause_destroy\n");
131 printf(" Pause and destroy a trace.\n");
132 printf(" -w, --write PATH\n");
133 printf(" Path for write trace datas.\n");
134 printf(" For -c, -C, -d, -D options\n");
135 printf(" -a, --append\n");
136 printf(" Append to trace, For -w option\n");
137 printf(" -n, --dump_threads NUMBER\n");
138 printf(" Number of lttd threads, For -w option\n");
139 printf(" --channel_root PATH\n");
140 printf(" Set channels root path, For -w option."
141 " (ex. /mnt/debugfs/ltt)\n");
146 * Separate option name to 3 fields
148 * Input: name = channel.cpu.bufsize
149 * Output: name1 = channel
156 * Make sure that name1~3 longer than OPT_NAMELEN.
157 * name1~3 can be NULL to discard value
160 static int separate_opt(const char *name
, char *name1
, char *name2
, char *name3
)
168 p
= strchr(name
, '.');
171 if (p
- name
>= OPT_NAMELEN
)
174 memcpy(name1
, name
, p
- name
);
180 p
= strchr(name
, '.');
183 if (p
- name
>= OPT_NAMELEN
)
186 memcpy(name2
, name
, p
- name
);
192 if (strlen(name
) >= OPT_NAMELEN
)
200 static void init_channel_opt(struct channel_option
*opt
, char *opt_name
)
202 if (opt
&& opt_name
) {
207 strcpy(opt
->chan_name
, opt_name
);
211 static struct lttctl_option
*find_insert_channel_opt(char *opt_name
)
213 struct lttctl_option
*iter
, *new_opt
;
216 opt_head
= (struct lttctl_option
*)malloc(sizeof(struct lttctl_option
));
217 init_channel_opt(&opt_head
->opt_mode
.chan_opt
, opt_name
);
218 opt_head
->type
= CHANNEL
;
219 opt_head
->next
= NULL
;
224 for (iter
= opt_head
; iter
; iter
= iter
->next
) {
225 if (iter
->type
!= CHANNEL
)
227 if (!strcmp(iter
->opt_mode
.chan_opt
.chan_name
, opt_name
))
231 new_opt
= (struct lttctl_option
*)malloc(sizeof(struct lttctl_option
));
232 init_channel_opt(&new_opt
->opt_mode
.chan_opt
, opt_name
);
233 new_opt
->type
= CHANNEL
;
234 new_opt
->next
= NULL
;
235 last_opt
->next
= new_opt
;
240 int set_channel_opt(struct channel_option
*opt
, char *opt_name
, char *opt_valstr
)
244 if (!strcmp("enable", opt_name
)) {
245 if (opt_valstr
[1] != 0) {
248 if (opt_valstr
[0] == 'Y' || opt_valstr
[0] == 'y'
249 || opt_valstr
[0] == '1')
251 else if (opt_valstr
[0] == 'N' || opt_valstr
[0] == 'n'
252 || opt_valstr
[0] == '0')
258 opt
->enable
= opt_val
;
260 } else if (!strcmp("overwrite", opt_name
)) {
261 if (opt_valstr
[1] != 0) {
264 if (opt_valstr
[0] == 'Y' || opt_valstr
[0] == 'y'
265 || opt_valstr
[0] == '1')
267 else if (opt_valstr
[0] == 'N' || opt_valstr
[0] == 'n'
268 || opt_valstr
[0] == '0')
274 opt
->overwrite
= opt_val
;
277 } else if (!strcmp("bufnum", opt_name
)) {
278 ret
= sscanf(opt_valstr
, "%d", &opt_val
);
279 if (ret
!= 1 || opt_val
< 0) {
283 opt
->bufnum
= opt_val
;
285 } else if (!strcmp("bufsize", opt_name
)) {
286 ret
= sscanf(opt_valstr
, "%d", &opt_val
);
287 if (ret
!= 1 || opt_val
< 0) {
291 opt
->bufsize
= opt_val
;
299 static int parst_opt(const char *optarg
)
302 char opt_name
[OPT_NAMELEN
* 3];
303 char opt_valstr
[OPT_VALSTRINGLEN
];
306 char name1
[OPT_NAMELEN
];
307 char name2
[OPT_NAMELEN
];
308 char name3
[OPT_NAMELEN
];
312 unsigned int opt_uintval
;
313 struct lttctl_option
*opt
;
316 fprintf(stderr
, "Option empty\n");
320 /* Get option name and val_str */
321 p
= strchr(optarg
, '=');
323 fprintf(stderr
, "Option format error: %s\n", optarg
);
327 if (p
- optarg
>= sizeof(opt_name
)/sizeof(opt_name
[0])) {
328 fprintf(stderr
, "Option name too long: %s\n", optarg
);
332 if (strlen(p
+1) >= OPT_VALSTRINGLEN
) {
333 fprintf(stderr
, "Option value too long: %s\n", optarg
);
337 memcpy(opt_name
, optarg
, p
- optarg
);
338 opt_name
[p
- optarg
] = 0;
339 strcpy(opt_valstr
, p
+1);
341 /* separate option name into 3 fields */
342 ret
= separate_opt(opt_name
, name1
, name2
, name3
);
344 fprintf(stderr
, "Option name error1: %s\n", optarg
);
348 if (!strcmp("channel", name1
)) {
349 opt
= find_insert_channel_opt(name2
);
350 if ((ret
= set_channel_opt(&opt
->opt_mode
.chan_opt
,
351 name3
, opt_valstr
) != 0)) {
352 fprintf(stderr
, "Option name error2: %s\n", optarg
);
356 fprintf(stderr
, "Option name error3: %s\n", optarg
);
365 * Parses the command line arguments.
367 * Returns -1 if the arguments were correct, but doesn't ask for program
368 * continuation. Returns EINVAL if the arguments are incorrect, or 0 if OK.
370 static int parse_arguments(int argc
, char **argv
)
374 static struct option longopts
[] = {
375 {"create", no_argument
, NULL
, 'c'},
376 {"destroy", no_argument
, NULL
, 'd'},
377 {"start", no_argument
, NULL
, 's'},
378 {"pause", no_argument
, NULL
, 'p'},
379 {"help", no_argument
, NULL
, 'h'},
380 {"transport", required_argument
, NULL
, 2},
381 {"option", required_argument
, NULL
, 'o'},
382 {"create_start", no_argument
, NULL
, 'C'},
383 {"pause_destroy", no_argument
, NULL
, 'D'},
384 {"write", required_argument
, NULL
, 'w'},
385 {"append", no_argument
, NULL
, 'a'},
386 {"dump_threads", required_argument
, NULL
, 'n'},
387 {"channel_root", required_argument
, NULL
, 3},
388 { NULL
, 0, NULL
, 0 },
392 * Enable all channels in default
393 * To make novice users happy
395 parst_opt("channel.all.enable=1");
397 opterr
= 1; /* Print error message on getopt_long */
400 c
= getopt_long(argc
, argv
, "cdspho:CDw:an:", longopts
, NULL
);
422 if (!opt_transport
) {
423 opt_transport
= optarg
;
426 "Please specify only 1 transport\n");
431 ret
= parst_opt(optarg
);
448 "Please specify only 1 write dir\n");
456 if (opt_dump_threads
) {
458 "Please specify only 1 dump threads\n");
462 ret
= sscanf(optarg
, "%u", &opt_dump_threads
);
465 "Dump threads not positive number\n");
470 if (!opt_channel_root
) {
471 opt_channel_root
= optarg
;
474 "Please specify only 1 channel root\n");
485 /* Don't check args when user needs help */
490 if (optind
< argc
- 1) {
491 fprintf(stderr
, "Please specify only 1 trace name\n");
494 if (optind
> argc
- 1) {
495 fprintf(stderr
, "Please specify trace name\n");
498 opt_tracename
= argv
[optind
];
503 if (!opt_create
&& !opt_start
&& !opt_destroy
&& !opt_pause
) {
505 "Please specify a option of "
506 "create, destroy, start, or pause\n");
510 if ((opt_create
|| opt_start
) && (opt_destroy
|| opt_pause
)) {
512 "Create and start conflict with destroy and pause\n");
518 opt_transport
= "relay";
524 "Transport option must be combine with create"
531 if (!opt_create
&& !opt_destroy
) {
533 "Write option must be combine with create or"
534 " destroy option\n");
538 if (!opt_channel_root
)
539 if (getdebugfsmntdir(channel_root_default
) == 0) {
540 strcat(channel_root_default
, "/ltt");
541 opt_channel_root
= channel_root_default
;
544 "Channel_root is necessary for -w"
545 " option, but neither --channel_root"
547 "specified, nor debugfs's mount dir"
548 " found, mount debugfs also failed\n");
552 if (opt_dump_threads
== 0)
553 opt_dump_threads
= 1;
559 "Append option must be combine with write"
565 if (opt_dump_threads
) {
568 "Dump_threads option must be combine with write"
574 if (opt_channel_root
) {
577 "Channel_root option must be combine with write"
586 static void show_info(void)
588 printf("Linux Trace Toolkit Trace Control " VERSION
"\n");
590 if (opt_tracename
!= NULL
) {
591 printf("Controlling trace : %s\n", opt_tracename
);
596 static int lttctl_channel_setup(struct channel_option
*opt
)
600 if (opt
->enable
!= -1) {
601 if ((ret
= lttctl_set_channel_enable(opt_tracename
,
606 if (opt
->overwrite
!= -1) {
607 if ((ret
= lttctl_set_channel_overwrite(opt_tracename
,
609 opt
->overwrite
)) != 0)
612 if (opt
->bufnum
!= -1) {
613 if ((ret
= lttctl_set_channel_subbuf_num(opt_tracename
,
618 if (opt
->bufsize
!= -1) {
619 if ((ret
= lttctl_set_channel_subbuf_size(opt_tracename
,
628 static int lttctl_create_trace(void)
632 struct lttctl_option
*opt
;
634 ret
= lttctl_setup_trace(opt_tracename
);
636 goto setup_trace_fail
;
638 for (opt
= opt_head
; opt
; opt
= opt
->next
) {
639 if (opt
->type
!= CHANNEL
)
641 ret
= lttctl_channel_setup(&opt
->opt_mode
.chan_opt
);
643 goto set_option_fail
;;
646 ret
= lttctl_set_trans(opt_tracename
, opt_transport
);
648 goto set_option_fail
;
650 ret
= lttctl_alloc_trace(opt_tracename
);
652 goto alloc_trace_fail
;
658 lttctl_destroy_trace(opt_tracename
);
664 * Start a lttd daemon to write trace datas
665 * Dump overwrite channels on overwrite!=0
666 * Dump normal(non-overwrite) channels on overwrite=0
671 static int lttctl_daemon(int overwrite
)
678 perror("Error in forking for lttd daemon");
686 char channel_path
[PATH_MAX
];
690 argv
[argc
] = getenv("LTT_DAEMON");
691 if (argv
[argc
] == NULL
)
692 argv
[argc
] = PACKAGE_BIN_DIR
"/lttd";
699 * we allow modify of opt_write's content in new process
700 * for get rid of warning of assign char * to const char *
702 argv
[argc
] = (char *)opt_write
;
706 strcpy(channel_path
, opt_channel_root
);
707 strcat(channel_path
, "/");
708 strcat(channel_path
, opt_tracename
);
711 argv
[argc
] = channel_path
;
715 sprintf(thread_num
, "%u", opt_dump_threads
);
718 argv
[argc
] = thread_num
;
731 /* overwrite option */
742 execvp(argv
[0], argv
);
744 perror("Error in executing the lttd daemon");
749 if (waitpid(pid
, &status
, 0) == -1) {
750 perror("Error in waitpid\n");
754 if (!WIFEXITED(status
)) {
755 fprintf(stderr
, "lttd process interrupted\n");
759 if (WEXITSTATUS(status
))
760 fprintf(stderr
, "lttd process running failed\n");
762 return WEXITSTATUS(status
);
765 int main(int argc
, char **argv
)
769 ret
= parse_arguments(argc
, argv
);
770 /* If user needs show help, we disregard other options */
776 /* exit program if arguments wrong */
787 printf("lttctl: Creating trace\n");
788 ret
= lttctl_create_trace();
793 printf("lttctl: Forking lttd\n");
794 ret
= lttctl_daemon(0);
801 printf("lttctl: Starting trace\n");
802 ret
= lttctl_start(opt_tracename
);
808 printf("lttctl: Pausing trace\n");
809 ret
= lttctl_pause(opt_tracename
);
816 printf("lttctl: Forking lttd\n");
817 ret
= lttctl_daemon(1);
822 printf("lttctl: Destroying trace\n");
823 ret
= lttctl_destroy_trace(opt_tracename
);
This page took 0.045943 seconds and 4 git commands to generate.