7380136fc6eefcbe1e0c4bb3da3e81e80815a438
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
];
47 struct lttctl_option
{
49 struct channel_option chan_opt
;
52 struct lttctl_option
*next
;
55 struct lttctl_option
*opt_head
, *last_opt
;
57 static int opt_create
;
58 static int opt_destroy
;
62 static const char *opt_transport
;
63 static const char *opt_write
;
64 static int opt_append
;
65 static unsigned int opt_dump_threads
;
66 static char channel_root_default
[PATH_MAX
];
67 static const char *opt_channel_root
;
68 static const char *opt_tracename
;
73 static void show_arguments(void)
75 printf("Linux Trace Toolkit Trace Control " VERSION
"\n");
77 printf("Usage: lttctl [OPTION]... [TRACENAME]\n");
79 printf("Examples:\n");
80 printf(" lttctl -c trace1 "
81 "# Create a trace named trace1.\n");
82 printf(" lttctl -s trace1 "
83 "# start a trace named trace1.\n");
84 printf(" lttctl -p trace1 "
85 "# pause a trace named trace1.\n");
86 printf(" lttctl -d trace1 "
87 "# Destroy a trace named trace1.\n");
88 printf(" lttctl -C -w /tmp/trace1 trace1 "
89 "# Create a trace named trace1, start it and\n"
91 "# write non-overwrite channels' data to\n"
93 "# /tmp/trace1, debugfs must be mounted for\n"
96 printf(" lttctl -D -w /tmp/trace1 trace1 "
97 "# Pause and destroy a trace named trace1 and\n"
99 "# write overwrite channels' data to\n"
101 "# /tmp/trace1, debugfs must be mounted for\n"
105 printf(" Basic options:\n");
106 printf(" -c, --create\n");
107 printf(" Create a trace.\n");
108 printf(" -d, --destroy\n");
109 printf(" Destroy a trace.\n");
110 printf(" -s, --start\n");
111 printf(" Start a trace.\n");
112 printf(" -p, --pause\n");
113 printf(" Pause a trace.\n");
114 printf(" -h, --help\n");
115 printf(" Show this help.\n");
117 printf(" Advanced options:\n");
118 printf(" --transport TRANSPORT\n");
119 printf(" Set trace's transport. (ex. relay-locked or relay)\n");
120 printf(" -o, --option OPTION\n");
121 printf(" Set options, following operations are supported:\n");
122 printf(" channel.<channelname>.enable=\n");
123 printf(" channel.<channelname>.overwrite=\n");
124 printf(" channel.<channelname>.bufnum=\n");
125 printf(" channel.<channelname>.bufsize= (in bytes, rounded to "
126 "next power of 2)\n");
127 printf(" <channelname> can be set to all for all channels\n");
128 printf(" channel.<channelname>.switch_timer= (timer interval in "
131 printf(" Integration options:\n");
132 printf(" -C, --create_start\n");
133 printf(" Create and start a trace.\n");
134 printf(" -D, --pause_destroy\n");
135 printf(" Pause and destroy a trace.\n");
136 printf(" -w, --write PATH\n");
137 printf(" Path for write trace datas.\n");
138 printf(" For -c, -C, -d, -D options\n");
139 printf(" -a, --append\n");
140 printf(" Append to trace, For -w option\n");
141 printf(" -n, --dump_threads NUMBER\n");
142 printf(" Number of lttd threads, For -w option\n");
143 printf(" --channel_root PATH\n");
144 printf(" Set channels root path, For -w option."
145 " (ex. /mnt/debugfs/ltt)\n");
150 * Separate option name to 3 fields
152 * Input: name = channel.cpu.bufsize
153 * Output: name1 = channel
160 * Make sure that name1~3 longer than OPT_NAMELEN.
161 * name1~3 can be NULL to discard value
164 static int separate_opt(const char *name
, char *name1
, char *name2
, char *name3
)
172 p
= strchr(name
, '.');
175 if (p
- name
>= OPT_NAMELEN
)
178 memcpy(name1
, name
, p
- name
);
184 p
= strchr(name
, '.');
187 if (p
- name
>= OPT_NAMELEN
)
190 memcpy(name2
, name
, p
- name
);
196 if (strlen(name
) >= OPT_NAMELEN
)
204 static void init_channel_opt(struct channel_option
*opt
, char *opt_name
)
206 if (opt
&& opt_name
) {
211 opt
->switch_timer
= -1;
212 strcpy(opt
->chan_name
, opt_name
);
216 static struct lttctl_option
*find_insert_channel_opt(char *opt_name
)
218 struct lttctl_option
*iter
, *new_opt
;
221 opt_head
= (struct lttctl_option
*)malloc(sizeof(struct lttctl_option
));
222 init_channel_opt(&opt_head
->opt_mode
.chan_opt
, opt_name
);
223 opt_head
->type
= CHANNEL
;
224 opt_head
->next
= NULL
;
229 for (iter
= opt_head
; iter
; iter
= iter
->next
) {
230 if (iter
->type
!= CHANNEL
)
232 if (!strcmp(iter
->opt_mode
.chan_opt
.chan_name
, opt_name
))
236 new_opt
= (struct lttctl_option
*)malloc(sizeof(struct lttctl_option
));
237 init_channel_opt(&new_opt
->opt_mode
.chan_opt
, opt_name
);
238 new_opt
->type
= CHANNEL
;
239 new_opt
->next
= NULL
;
240 last_opt
->next
= new_opt
;
245 int set_channel_opt(struct channel_option
*opt
, char *opt_name
, char *opt_valstr
)
249 if (!strcmp("enable", opt_name
)) {
250 if (opt_valstr
[1] != 0) {
253 if (opt_valstr
[0] == 'Y' || opt_valstr
[0] == 'y'
254 || opt_valstr
[0] == '1')
256 else if (opt_valstr
[0] == 'N' || opt_valstr
[0] == 'n'
257 || opt_valstr
[0] == '0')
263 opt
->enable
= opt_val
;
265 } else if (!strcmp("overwrite", opt_name
)) {
266 if (opt_valstr
[1] != 0) {
269 if (opt_valstr
[0] == 'Y' || opt_valstr
[0] == 'y'
270 || opt_valstr
[0] == '1')
272 else if (opt_valstr
[0] == 'N' || opt_valstr
[0] == 'n'
273 || opt_valstr
[0] == '0')
279 opt
->overwrite
= opt_val
;
282 } else if (!strcmp("bufnum", opt_name
)) {
283 ret
= sscanf(opt_valstr
, "%d", &opt_val
);
284 if (ret
!= 1 || opt_val
< 0) {
288 opt
->bufnum
= opt_val
;
290 } else if (!strcmp("bufsize", opt_name
)) {
291 ret
= sscanf(opt_valstr
, "%d", &opt_val
);
292 if (ret
!= 1 || opt_val
< 0) {
296 opt
->bufsize
= opt_val
;
298 } else if (!strcmp("switch_timer", opt_name
)) {
299 ret
= sscanf(opt_valstr
, "%d", &opt_val
);
300 if (ret
!= 1 || opt_val
< 0) {
304 opt
->switch_timer
= opt_val
;
312 static int parst_opt(const char *optarg
)
315 char opt_name
[OPT_NAMELEN
* 3];
316 char opt_valstr
[OPT_VALSTRINGLEN
];
319 char name1
[OPT_NAMELEN
];
320 char name2
[OPT_NAMELEN
];
321 char name3
[OPT_NAMELEN
];
325 unsigned int opt_uintval
;
326 struct lttctl_option
*opt
;
329 fprintf(stderr
, "Option empty\n");
333 /* Get option name and val_str */
334 p
= strchr(optarg
, '=');
336 fprintf(stderr
, "Option format error: %s\n", optarg
);
340 if (p
- optarg
>= sizeof(opt_name
)/sizeof(opt_name
[0])) {
341 fprintf(stderr
, "Option name too long: %s\n", optarg
);
345 if (strlen(p
+1) >= OPT_VALSTRINGLEN
) {
346 fprintf(stderr
, "Option value too long: %s\n", optarg
);
350 memcpy(opt_name
, optarg
, p
- optarg
);
351 opt_name
[p
- optarg
] = 0;
352 strcpy(opt_valstr
, p
+1);
354 /* separate option name into 3 fields */
355 ret
= separate_opt(opt_name
, name1
, name2
, name3
);
357 fprintf(stderr
, "Option name error1: %s\n", optarg
);
361 if (!strcmp("channel", name1
)) {
362 opt
= find_insert_channel_opt(name2
);
363 if ((ret
= set_channel_opt(&opt
->opt_mode
.chan_opt
,
364 name3
, opt_valstr
) != 0)) {
365 fprintf(stderr
, "Option name error2: %s\n", optarg
);
369 fprintf(stderr
, "Option name error3: %s\n", optarg
);
378 * Parses the command line arguments.
380 * Returns -1 if the arguments were correct, but doesn't ask for program
381 * continuation. Returns EINVAL if the arguments are incorrect, or 0 if OK.
383 static int parse_arguments(int argc
, char **argv
)
387 static struct option longopts
[] = {
388 {"create", no_argument
, NULL
, 'c'},
389 {"destroy", no_argument
, NULL
, 'd'},
390 {"start", no_argument
, NULL
, 's'},
391 {"pause", no_argument
, NULL
, 'p'},
392 {"help", no_argument
, NULL
, 'h'},
393 {"transport", required_argument
, NULL
, 2},
394 {"option", required_argument
, NULL
, 'o'},
395 {"create_start", no_argument
, NULL
, 'C'},
396 {"pause_destroy", no_argument
, NULL
, 'D'},
397 {"write", required_argument
, NULL
, 'w'},
398 {"append", no_argument
, NULL
, 'a'},
399 {"dump_threads", required_argument
, NULL
, 'n'},
400 {"channel_root", required_argument
, NULL
, 3},
401 { NULL
, 0, NULL
, 0 },
405 * Enable all channels in default
406 * To make novice users happy
408 parst_opt("channel.all.enable=1");
410 opterr
= 1; /* Print error message on getopt_long */
413 c
= getopt_long(argc
, argv
, "cdspho:CDw:an:", longopts
, NULL
);
435 if (!opt_transport
) {
436 opt_transport
= optarg
;
439 "Please specify only 1 transport\n");
444 ret
= parst_opt(optarg
);
461 "Please specify only 1 write dir\n");
469 if (opt_dump_threads
) {
471 "Please specify only 1 dump threads\n");
475 ret
= sscanf(optarg
, "%u", &opt_dump_threads
);
478 "Dump threads not positive number\n");
483 if (!opt_channel_root
) {
484 opt_channel_root
= optarg
;
487 "Please specify only 1 channel root\n");
498 /* Don't check args when user needs help */
503 if (optind
< argc
- 1) {
504 fprintf(stderr
, "Please specify only 1 trace name\n");
507 if (optind
> argc
- 1) {
508 fprintf(stderr
, "Please specify trace name\n");
511 opt_tracename
= argv
[optind
];
516 if (!opt_create
&& !opt_start
&& !opt_destroy
&& !opt_pause
) {
518 "Please specify a option of "
519 "create, destroy, start, or pause\n");
523 if ((opt_create
|| opt_start
) && (opt_destroy
|| opt_pause
)) {
525 "Create and start conflict with destroy and pause\n");
531 opt_transport
= "relay";
537 "Transport option must be combine with create"
544 if (!opt_create
&& !opt_destroy
) {
546 "Write option must be combine with create or"
547 " destroy option\n");
551 if (!opt_channel_root
)
552 if (getdebugfsmntdir(channel_root_default
) == 0) {
553 strcat(channel_root_default
, "/ltt");
554 opt_channel_root
= channel_root_default
;
557 "Channel_root is necessary for -w"
558 " option, but neither --channel_root"
560 "specified, nor debugfs's mount dir"
561 " found, mount debugfs also failed\n");
565 if (opt_dump_threads
== 0)
566 opt_dump_threads
= 1;
572 "Append option must be combine with write"
578 if (opt_dump_threads
) {
581 "Dump_threads option must be combine with write"
587 if (opt_channel_root
) {
590 "Channel_root option must be combine with write"
599 static void show_info(void)
601 printf("Linux Trace Toolkit Trace Control " VERSION
"\n");
603 if (opt_tracename
!= NULL
) {
604 printf("Controlling trace : %s\n", opt_tracename
);
609 static int lttctl_channel_setup(struct channel_option
*opt
)
613 if (opt
->enable
!= -1) {
614 if ((ret
= lttctl_set_channel_enable(opt_tracename
,
619 if (opt
->overwrite
!= -1) {
620 if ((ret
= lttctl_set_channel_overwrite(opt_tracename
,
622 opt
->overwrite
)) != 0)
625 if (opt
->bufnum
!= -1) {
626 if ((ret
= lttctl_set_channel_subbuf_num(opt_tracename
,
631 if (opt
->bufsize
!= -1) {
632 if ((ret
= lttctl_set_channel_subbuf_size(opt_tracename
,
637 if (opt
->switch_timer
!= -1) {
638 if ((ret
= lttctl_set_channel_switch_timer(opt_tracename
,
639 opt
->chan_name
, opt
->switch_timer
)) != 0)
646 static int lttctl_create_trace(void)
650 struct lttctl_option
*opt
;
652 ret
= lttctl_setup_trace(opt_tracename
);
654 goto setup_trace_fail
;
656 for (opt
= opt_head
; opt
; opt
= opt
->next
) {
657 if (opt
->type
!= CHANNEL
)
659 ret
= lttctl_channel_setup(&opt
->opt_mode
.chan_opt
);
661 goto set_option_fail
;;
664 ret
= lttctl_set_trans(opt_tracename
, opt_transport
);
666 goto set_option_fail
;
668 ret
= lttctl_alloc_trace(opt_tracename
);
670 goto alloc_trace_fail
;
676 lttctl_destroy_trace(opt_tracename
);
682 * Start a lttd daemon to write trace datas
683 * Dump overwrite channels on overwrite!=0
684 * Dump normal(non-overwrite) channels on overwrite=0
689 static int lttctl_daemon(int overwrite
)
696 perror("Error in forking for lttd daemon");
704 char channel_path
[PATH_MAX
];
708 argv
[argc
] = getenv("LTT_DAEMON");
709 if (argv
[argc
] == NULL
)
710 argv
[argc
] = PACKAGE_BIN_DIR
"/lttd";
717 * we allow modify of opt_write's content in new process
718 * for get rid of warning of assign char * to const char *
720 argv
[argc
] = (char *)opt_write
;
724 strcpy(channel_path
, opt_channel_root
);
725 strcat(channel_path
, "/");
726 strcat(channel_path
, opt_tracename
);
729 argv
[argc
] = channel_path
;
733 sprintf(thread_num
, "%u", opt_dump_threads
);
736 argv
[argc
] = thread_num
;
749 /* overwrite option */
760 execvp(argv
[0], argv
);
762 perror("Error in executing the lttd daemon");
767 if (waitpid(pid
, &status
, 0) == -1) {
768 perror("Error in waitpid\n");
772 if (!WIFEXITED(status
)) {
773 fprintf(stderr
, "lttd process interrupted\n");
777 if (WEXITSTATUS(status
))
778 fprintf(stderr
, "lttd process running failed\n");
780 return WEXITSTATUS(status
);
783 int main(int argc
, char **argv
)
787 ret
= parse_arguments(argc
, argv
);
788 /* If user needs show help, we disregard other options */
794 /* exit program if arguments wrong */
805 printf("lttctl: Creating trace\n");
806 ret
= lttctl_create_trace();
811 printf("lttctl: Forking lttd\n");
812 ret
= lttctl_daemon(0);
819 printf("lttctl: Starting trace\n");
820 ret
= lttctl_start(opt_tracename
);
826 printf("lttctl: Pausing trace\n");
827 ret
= lttctl_pause(opt_tracename
);
834 printf("lttctl: Forking lttd\n");
835 ret
= lttctl_daemon(1);
840 printf("lttctl: Destroying trace\n");
841 ret
= lttctl_destroy_trace(opt_tracename
);
This page took 0.068331 seconds and 4 git commands to generate.