X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=trunk%2Fltt-control%2Flttctl%2Flttctl.c;h=eeb810053e1b5634c9800727611b1e9abc5443ea;hb=354b34fd3862bb86f74ecfdcca471d6c66238b61;hp=c04d4f5ca10d9569eb42eea98b7c5029a39fc605;hpb=a9c10be29dd6c32478e6383e43c5b8fd6c4b762c;p=ltt-control.git diff --git a/trunk/ltt-control/lttctl/lttctl.c b/trunk/ltt-control/lttctl/lttctl.c index c04d4f5..eeb8100 100644 --- a/trunk/ltt-control/lttctl/lttctl.c +++ b/trunk/ltt-control/lttctl/lttctl.c @@ -6,6 +6,10 @@ * * Copyright 2005 - * Mathieu Desnoyers + * + * Copyright 2008 FUJITSU + * Zhao Lei + * Gui Jianfeng */ #ifdef HAVE_CONFIG_H @@ -16,103 +20,344 @@ #include #include #include -#include #include #include -#include -#include #include #include -#include - -/* Buffer for file copy : 4k seems optimal. */ -#define BUF_SIZE 4096 - -enum trace_ctl_op { - CTL_OP_CREATE_START, - CTL_OP_CREATE, - CTL_OP_DESTROY, - CTL_OP_STOP_DESTROY, - CTL_OP_START, - CTL_OP_STOP, - CTL_OP_DAEMON, - CTL_OP_DAEMON_HYBRID_FINISH, - CTL_OP_NONE +#define _GNU_SOURCE +#include + +#define OPT_MAX (1024) +#define OPT_NAMELEN (256) +#define OPT_VALSTRINGLEN (256) + +enum opt_type { + CHANNEL, +}; + +struct channel_option { + char chan_name[OPT_NAMELEN]; + int enable; + int overwrite; + int bufnum; + int bufsize; +}; + +struct lttctl_option { + union { + struct channel_option chan_opt; + } opt_mode; + enum opt_type type; + struct lttctl_option *next; }; -static char *trace_name = NULL; -static char *trace_type = "relay"; -static char *mode_name = NULL; -static unsigned subbuf_size_low = 0; -static unsigned n_subbufs_low = 0; -static unsigned subbuf_size_med = 0; -static unsigned n_subbufs_med = 0; -static unsigned subbuf_size_high = 0; -static unsigned n_subbufs_high = 0; -static unsigned append_trace = 0; -static enum trace_mode mode = LTT_TRACE_NORMAL; -static enum trace_ctl_op op = CTL_OP_NONE; -static char *channel_root = NULL; +struct lttctl_option *opt_head, *last_opt; + +static int opt_create; +static int opt_destroy; +static int opt_start; +static int opt_pause; +static int opt_help; +static const char *opt_transport; +static const char *opt_write; +static int opt_append; +static unsigned int opt_dump_threads; static char channel_root_default[PATH_MAX]; -static char *trace_root = NULL; -static char *num_threads = "1"; +static const char *opt_channel_root; +static const char *opt_tracename; /* Args : * */ -void show_arguments(void) +static void show_arguments(void) { - printf("Please use the following arguments :\n"); + printf("Linux Trace Toolkit Trace Control " VERSION"\n"); + printf("\n"); + printf("Usage: lttctl [OPTION]... [TRACENAME]\n"); + printf("\n"); + printf("Examples:\n"); + printf(" lttctl -c trace1 " + "# Create a trace named trace1.\n"); + printf(" lttctl -s trace1 " + "# start a trace named trace1.\n"); + printf(" lttctl -p trace1 " + "# pause a trace named trace1.\n"); + printf(" lttctl -d trace1 " + "# Destroy a trace named trace1.\n"); + printf(" lttctl -C -w /tmp/trace1 trace1 " + "# Create a trace named trace1, start it and\n" + " " + "# write non-overwrite channels' data to\n" + " " + "# /tmp/trace1, debugfs must be mounted for\n" + " " + "# auto-find\n"); + printf(" lttctl -D -w /tmp/trace1 trace1 " + "# Pause and destroy a trace named trace1 and\n" + " " + "# write overwrite channels' data to\n" + " " + "# /tmp/trace1, debugfs must be mounted for\n" + " " + "# auto-find\n"); + printf("\n"); + printf(" Basic options:\n"); + printf(" -c, --create\n"); + printf(" Create a trace.\n"); + printf(" -d, --destroy\n"); + printf(" Destroy a trace.\n"); + printf(" -s, --start\n"); + printf(" Start a trace.\n"); + printf(" -p, --pause\n"); + printf(" Pause a trace.\n"); + printf(" -h, --help\n"); + printf(" Show this help.\n"); + printf("\n"); + printf(" Advanced options:\n"); + printf(" --transport TRANSPORT\n"); + printf(" Set trace's transport. (ex. relay)\n"); + printf(" -o, --option OPTION\n"); + printf(" Set options, following operations are supported:\n"); + printf(" channel..enable=\n"); + printf(" channel..overwrite=\n"); + printf(" channel..bufnum=\n"); + printf(" channel..bufsize=\n"); + printf(" can be set to all for all channels\n"); printf("\n"); - printf("-n name Name of the trace.\n"); - printf("-b Create trace channels and start tracing (no daemon).\n"); - printf("-c Create trace channels.\n"); - printf("-m mode Normal, flight recorder or hybrid mode.\n"); - printf(" Mode values : normal (default), flight or hybrid.\n"); - printf("-r Destroy trace channels.\n"); - printf("-R Stop tracing and destroy trace channels.\n"); - printf("-s Start tracing.\n"); - //printf(" Note : will automatically create a normal trace if " - // "none exists.\n"); - printf("-q Stop tracing.\n"); - printf("-d Create trace, spawn a lttd daemon, start tracing.\n"); - printf(" (optionally, you can set LTT_DAEMON\n"); - printf(" env. var.)\n"); - printf("-f Stop tracing, dump flight recorder trace, destroy channels\n"); - printf(" (for hybrid traces)\n"); - printf("-t Trace root path. (ex. /root/traces/example_trace)\n"); - printf("-T Type of trace (ex. relay)\n"); - printf("-l LTT channels root path. (ex. /mnt/debugfs/ltt)\n"); - printf("-Z Size of the low data rate subbuffers (will be rounded to next page size)\n"); - printf("-X Number of low data rate subbuffers\n"); - printf("-V Size of the medium data rate subbuffers (will be rounded to next page size)\n"); - printf("-B Number of medium data rate subbuffers\n"); - printf("-z Size of the high data rate subbuffers (will be rounded to next page size)\n"); - printf("-x Number of high data rate subbuffers\n"); - printf("-a Append to trace\n"); - printf("-N Number of lttd threads\n"); + printf(" Integration options:\n"); + printf(" -C, --create_start\n"); + printf(" Create and start a trace.\n"); + printf(" -D, --pause_destroy\n"); + printf(" Pause and destroy a trace.\n"); + printf(" -w, --write PATH\n"); + printf(" Path for write trace datas.\n"); + printf(" For -c, -C, -d, -D options\n"); + printf(" -a, --append\n"); + printf(" Append to trace, For -w option\n"); + printf(" -n, --dump_threads NUMBER\n"); + printf(" Number of lttd threads, For -w option\n"); + printf(" --channel_root PATH\n"); + printf(" Set channels root path, For -w option." + " (ex. /mnt/debugfs/ltt)\n"); printf("\n"); } -int getdebugfsmntdir(char *mntdir) +/* + * Separate option name to 3 fields + * Ex: + * Input: name = channel.cpu.bufsize + * Output: name1 = channel + * name2 = cpu + * name3 = bufsize + * Ret: 0 on success + * 1 on fail + * + * Note: + * Make sure that name1~3 longer than OPT_NAMELEN. + * name1~3 can be NULL to discard value + * + */ +static int separate_opt(const char *name, char *name1, char *name2, char *name3) { - char mnt_dir[PATH_MAX]; - char mnt_type[PATH_MAX]; + char *p; + + if (!name) + return 1; + + /* segment1 */ + p = strchr(name, '.'); + if (!p) + return 1; + if (p - name >= OPT_NAMELEN) + return 1; + if (name1) { + memcpy(name1, name, p - name); + name1[p - name] = 0; + } + name = p + 1; + + /* segment2 */ + p = strchr(name, '.'); + if (!p) + return 1; + if (p - name >= OPT_NAMELEN) + return 1; + if (name2) { + memcpy(name2, name, p - name); + name2[p - name] = 0; + } + name = p + 1; + + /* segment3 */ + if (strlen(name) >= OPT_NAMELEN) + return 1; + if (name3) + strcpy(name3, name); - FILE *fp = fopen("/proc/mounts", "r"); - if (!fp) { - return EINVAL; + return 0; +} + +static void init_channel_opt(struct channel_option *opt, char *opt_name) +{ + if (opt && opt_name) { + opt->enable = -1; + opt->overwrite = -1; + opt->bufnum = -1; + opt->bufsize = -1; + strcpy(opt->chan_name, opt_name); } +} - while (1) { - if (fscanf(fp, "%*s %s %s %*s %*s %*s", mnt_dir, mnt_type) <= 0) { - return ENOENT; +static struct lttctl_option *find_insert_channel_opt(char *opt_name) +{ + struct lttctl_option *iter, *new_opt; + + if (!opt_head) { + opt_head = (struct lttctl_option *)malloc(sizeof(struct lttctl_option)); + init_channel_opt(&opt_head->opt_mode.chan_opt, opt_name); + opt_head->type = CHANNEL; + opt_head->next = NULL; + last_opt = opt_head; + return opt_head; + } + + for (iter = opt_head; iter; iter = iter->next) { + if (iter->type != CHANNEL) + continue; + if (!strcmp(iter->opt_mode.chan_opt.chan_name, opt_name)) + return iter; + } + + new_opt = (struct lttctl_option *)malloc(sizeof(struct lttctl_option)); + init_channel_opt(&new_opt->opt_mode.chan_opt, opt_name); + new_opt->type = CHANNEL; + new_opt->next = NULL; + last_opt->next = new_opt; + last_opt = new_opt; + return new_opt; +} + +int set_channel_opt(struct channel_option *opt, char *opt_name, char *opt_valstr) +{ + int opt_val, ret; + + if (!strcmp("enable", opt_name)) { + if (opt_valstr[1] != 0) { + return -EINVAL; + } + if (opt_valstr[0] == 'Y' || opt_valstr[0] == 'y' + || opt_valstr[0] == '1') + opt_val = 1; + else if (opt_valstr[0] == 'N' || opt_valstr[0] == 'n' + || opt_valstr[0] == '0') + opt_val = 0; + else { + return -EINVAL; + } + + opt->enable = opt_val; + return 0; + } else if (!strcmp("overwrite", opt_name)) { + if (opt_valstr[1] != 0) { + return -EINVAL; } - if (!strcmp(mnt_type, "debugfs")) { - strcpy(mntdir, mnt_dir); - return 0; + if (opt_valstr[0] == 'Y' || opt_valstr[0] == 'y' + || opt_valstr[0] == '1') + opt_val = 1; + else if (opt_valstr[0] == 'N' || opt_valstr[0] == 'n' + || opt_valstr[0] == '0') + opt_val = 0; + else { + return -EINVAL; } + + opt->overwrite = opt_val; + return 0; + + } else if (!strcmp("bufnum", opt_name)) { + ret = sscanf(opt_valstr, "%d", &opt_val); + if (ret != 1 || opt_val < 0) { + return -EINVAL; + } + + opt->bufnum = opt_val; + return 0; + } else if (!strcmp("bufsize", opt_name)) { + ret = sscanf(opt_valstr, "%d", &opt_val); + if (ret != 1 || opt_val < 0) { + return -EINVAL; + } + + opt->bufsize = opt_val; + return 0; + } else { + return -EINVAL; } + +} + +static int parst_opt(const char *optarg) +{ + int ret; + char opt_name[OPT_NAMELEN * 3]; + char opt_valstr[OPT_VALSTRINGLEN]; + char *p; + + char name1[OPT_NAMELEN]; + char name2[OPT_NAMELEN]; + char name3[OPT_NAMELEN]; + + int opt_intval; + int opt_val; + unsigned int opt_uintval; + struct lttctl_option *opt; + + if (!optarg) { + fprintf(stderr, "Option empty\n"); + return -EINVAL; + } + + /* Get option name and val_str */ + p = strchr(optarg, '='); + if (!p) { + fprintf(stderr, "Option format error: %s\n", optarg); + return -EINVAL; + } + + if (p - optarg >= sizeof(opt_name)/sizeof(opt_name[0])) { + fprintf(stderr, "Option name too long: %s\n", optarg); + return -EINVAL; + } + + if (strlen(p+1) >= OPT_VALSTRINGLEN) { + fprintf(stderr, "Option value too long: %s\n", optarg); + return -EINVAL; + } + + memcpy(opt_name, optarg, p - optarg); + opt_name[p - optarg] = 0; + strcpy(opt_valstr, p+1); + + /* separate option name into 3 fields */ + ret = separate_opt(opt_name, name1, name2, name3); + if (ret != 0) { + fprintf(stderr, "Option name error1: %s\n", optarg); + return -EINVAL; + } + + if (!strcmp("channel", name1)) { + opt = find_insert_channel_opt(name2); + if ((ret = set_channel_opt(&opt->opt_mode.chan_opt, + name3, opt_valstr) != 0)) { + fprintf(stderr, "Option name error2: %s\n", optarg); + return ret; + } + } else { + fprintf(stderr, "Option name error3: %s\n", optarg); + return -EINVAL; + } + + return 0; } /* parse_arguments @@ -122,445 +367,466 @@ int getdebugfsmntdir(char *mntdir) * Returns -1 if the arguments were correct, but doesn't ask for program * continuation. Returns EINVAL if the arguments are incorrect, or 0 if OK. */ -int parse_arguments(int argc, char **argv) +static int parse_arguments(int argc, char **argv) { int ret = 0; - int argn = 1; - if(argc == 2) { - if(strcmp(argv[1], "-h") == 0) { - return -1; + static struct option longopts[] = { + {"create", no_argument, NULL, 'c'}, + {"destroy", no_argument, NULL, 'd'}, + {"start", no_argument, NULL, 's'}, + {"pause", no_argument, NULL, 'p'}, + {"help", no_argument, NULL, 'h'}, + {"transport", required_argument, NULL, 2}, + {"option", required_argument, NULL, 'o'}, + {"create_start", no_argument, NULL, 'C'}, + {"pause_destroy", no_argument, NULL, 'D'}, + {"write", required_argument, NULL, 'w'}, + {"append", no_argument, NULL, 'a'}, + {"dump_threads", required_argument, NULL, 'n'}, + {"channel_root", required_argument, NULL, 3}, + { NULL, 0, NULL, 0 }, + }; + + /* + * Enable all channels in default + * To make novice users happy + */ + parst_opt("channel.all.enable=1"); + + opterr = 1; /* Print error message on getopt_long */ + while (1) { + int c; + c = getopt_long(argc, argv, "cdspho:CDw:an:", longopts, NULL); + if (-1 == c) { + /* parse end */ + break; } + switch (c) { + case 'c': + opt_create = 1; + break; + case 'd': + opt_destroy = 1; + break; + case 's': + opt_start = 1; + break; + case 'p': + opt_pause = 1; + break; + case 'h': + opt_help = 1; + break; + case 2: + if (!opt_transport) { + opt_transport = optarg; + } else { + fprintf(stderr, + "Please specify only 1 transport\n"); + return -EINVAL; + } + break; + case 'o': + ret = parst_opt(optarg); + if (ret) + return ret; + break; + case 'C': + opt_create = 1; + opt_start = 1; + break; + case 'D': + opt_pause = 1; + opt_destroy = 1; + break; + case 'w': + if (!opt_write) { + opt_write = optarg; + } else { + fprintf(stderr, + "Please specify only 1 write dir\n"); + return -EINVAL; + } + break; + case 'a': + opt_append = 1; + break; + case 'n': + if (opt_dump_threads) { + fprintf(stderr, + "Please specify only 1 dump threads\n"); + return -EINVAL; + } + + ret = sscanf(optarg, "%u", &opt_dump_threads); + if (ret != 1) { + fprintf(stderr, + "Dump threads not positive number\n"); + return -EINVAL; + } + break; + case 3: + if (!opt_channel_root) { + opt_channel_root = optarg; + } else { + fprintf(stderr, + "Please specify only 1 channel root\n"); + return -EINVAL; + } + break; + case '?': + return -EINVAL; + default: + break; + }; + }; + + /* Don't check args when user needs help */ + if (opt_help) + return 0; + + /* Get tracename */ + if (optind < argc - 1) { + fprintf(stderr, "Please specify only 1 trace name\n"); + return -EINVAL; + } + if (optind > argc - 1) { + fprintf(stderr, "Please specify trace name\n"); + return -EINVAL; + } + opt_tracename = argv[optind]; + + /* + * Check arguments + */ + if (!opt_create && !opt_start && !opt_destroy && !opt_pause) { + fprintf(stderr, + "Please specify a option of " + "create, destroy, start, or pause\n"); + return -EINVAL; } - while(argn < argc) { - - switch(argv[argn][0]) { - case '-': - switch(argv[argn][1]) { - case 'n': - if(argn+1 < argc) { - trace_name = argv[argn+1]; - argn++; - } else { - printf("Specify a trace name after -n.\n"); - printf("\n"); - ret = EINVAL; - } - - break; - case 'b': - op = CTL_OP_CREATE_START; - break; - case 'c': - op = CTL_OP_CREATE; - break; - case 'm': - if(argn+1 < argc) { - mode_name = argv[argn+1]; - argn++; - if(strcmp(mode_name, "normal") == 0) - mode = LTT_TRACE_NORMAL; - else if(strcmp(mode_name, "flight") == 0) - mode = LTT_TRACE_FLIGHT; - else if(strcmp(mode_name, "hybrid") == 0) - mode = LTT_TRACE_HYBRID; - else { - printf("Invalid mode '%s'.\n", argv[argn]); - printf("\n"); - ret = EINVAL; - } - } else { - printf("Specify a mode after -m.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'r': - op = CTL_OP_DESTROY; - break; - case 'R': - op = CTL_OP_STOP_DESTROY; - break; - case 's': - op = CTL_OP_START; - break; - case 'q': - op = CTL_OP_STOP; - break; - case 'Z': - if(argn+1 < argc) { - subbuf_size_low = (unsigned)atoi(argv[argn+1]); - argn++; - } else { - printf("Specify a number of low traffic subbuffers after -Z.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'X': - if(argn+1 < argc) { - n_subbufs_low = (unsigned)atoi(argv[argn+1]); - argn++; - } else { - printf("Specify a low traffic subbuffer size after -X.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'V': - if(argn+1 < argc) { - subbuf_size_med = (unsigned)atoi(argv[argn+1]); - argn++; - } else { - printf("Specify a number of medium traffic subbuffers after -V.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'B': - if(argn+1 < argc) { - n_subbufs_med = (unsigned)atoi(argv[argn+1]); - argn++; - } else { - printf("Specify a medium traffic subbuffer size after -B.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'z': - if(argn+1 < argc) { - subbuf_size_high = (unsigned)atoi(argv[argn+1]); - argn++; - } else { - printf("Specify a number of high traffic subbuffers after -z.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'x': - if(argn+1 < argc) { - n_subbufs_high = (unsigned)atoi(argv[argn+1]); - argn++; - } else { - printf("Specify a high traffic subbuffer size after -x.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'd': - op = CTL_OP_DAEMON; - break; - case 'f': - op = CTL_OP_DAEMON_HYBRID_FINISH; - break; - case 't': - if(argn+1 < argc) { - trace_root = argv[argn+1]; - argn++; - } else { - printf("Specify a trace root path after -t.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'l': - if(argn+1 < argc) { - channel_root = argv[argn+1]; - argn++; - } else { - printf("Specify a channel root path after -l.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'a': - append_trace = 1; - break; - case 'N': - if(argn+1 < argc) { - num_threads = argv[argn+1]; - argn++; - } - break; - case 'T': - if(argn+1 < argc) { - trace_type = argv[argn+1]; - argn++; - } else { - printf("Specify a trace type after -T.\n"); - printf("\n"); - ret = EINVAL; - } - break; - default: - printf("Invalid argument '%s'.\n", argv[argn]); - printf("\n"); - ret = EINVAL; - } - break; - default: - printf("Invalid argument '%s'.\n", argv[argn]); - printf("\n"); - ret = EINVAL; - } - argn++; + if ((opt_create || opt_start) && (opt_destroy || opt_pause)) { + fprintf(stderr, + "Create and start conflict with destroy and pause\n"); + return -EINVAL; } - - if(trace_name == NULL) { - printf("Please specify a trace name.\n"); - printf("\n"); - ret = EINVAL; + + if (opt_create) { + if (!opt_transport) + opt_transport = "relay"; } - if(op == CTL_OP_NONE) { - printf("Please specify an operation.\n"); - printf("\n"); - ret = EINVAL; + if (opt_transport) { + if (!opt_create) { + fprintf(stderr, + "Transport option must be combine with create" + " option\n"); + return -EINVAL; + } } - if(op == CTL_OP_DAEMON || op == CTL_OP_DAEMON_HYBRID_FINISH) { - if(trace_root == NULL) { - printf("Please specify -t trace_root_path with the -d option.\n"); - printf("\n"); - ret = EINVAL; + if (opt_write) { + if (!opt_create && !opt_destroy) { + fprintf(stderr, + "Write option must be combine with create or" + " destroy option\n"); + return -EINVAL; } - if(channel_root == NULL) { + + if (!opt_channel_root) if (getdebugfsmntdir(channel_root_default) == 0) { strcat(channel_root_default, "/ltt"); - printf("No -l ltt_root_path with the -d option, using default: %s\n", channel_root_default); - printf("\n"); - channel_root=channel_root_default; + opt_channel_root = channel_root_default; } else { - printf("Please specify -l ltt_root_path with the -d option.\n"); - printf("\n"); - ret = EINVAL; + fprintf(stderr, + "Channel_root is necessary for -w" + " option, but neither --channel_root" + " option\n" + "specified, nor debugfs's mount dir" + " found, mount debugfs also failed\n"); + return -EINVAL; } + + if (opt_dump_threads == 0) + opt_dump_threads = 1; + } + + if (opt_append) { + if (!opt_write) { + fprintf(stderr, + "Append option must be combine with write" + " option\n"); + return -EINVAL; } } - return ret; -} + if (opt_dump_threads) { + if (!opt_write) { + fprintf(stderr, + "Dump_threads option must be combine with write" + " option\n"); + return -EINVAL; + } + } -void show_info(void) + if (opt_channel_root) { + if (!opt_write) { + fprintf(stderr, + "Channel_root option must be combine with write" + " option\n"); + return -EINVAL; + } + } + + return 0; +} + +static void show_info(void) { printf("Linux Trace Toolkit Trace Control " VERSION"\n"); printf("\n"); - if(trace_name != NULL) { - printf("Controlling trace : %s\n", trace_name); + if (opt_tracename != NULL) { + printf("Controlling trace : %s\n", opt_tracename); printf("\n"); } } -int lttctl_daemon(struct lttctl_handle *handle, char *trace_name) +static int lttctl_channel_setup(struct channel_option *opt) { - char channel_path[PATH_MAX] = ""; - pid_t pid; int ret; - char *lttd_path = getenv("LTT_DAEMON"); - - if(lttd_path == NULL) lttd_path = - PACKAGE_BIN_DIR "/lttd"; - - strcat(channel_path, channel_root); - strcat(channel_path, "/"); - strcat(channel_path, trace_name); - - - ret = lttctl_create_trace(handle, trace_name, mode, trace_type, - subbuf_size_low, n_subbufs_low, - subbuf_size_med, n_subbufs_med, - subbuf_size_high, n_subbufs_high); - if(ret != 0) goto create_error; - - pid = fork(); - if(pid > 0) { - int status = 0; - /* parent */ - - ret = waitpid(pid, &status, 0); - if(ret == -1) { - ret = errno; - perror("Error in waitpid"); - goto start_error; - } + if (opt->enable != -1) { + if ((ret = lttctl_set_channel_enable(opt_tracename, + opt->chan_name, + opt->enable)) != 0) + return ret; + } + if (opt->overwrite != -1) { + if ((ret = lttctl_set_channel_overwrite(opt_tracename, + opt->chan_name, + opt->overwrite)) != 0) + return ret; + } + if (opt->bufnum != -1) { + if ((ret = lttctl_set_channel_subbuf_num(opt_tracename, + opt->chan_name, + opt->bufnum)) != 0) + return ret; + } + if (opt->bufsize != -1) { + if ((ret = lttctl_set_channel_subbuf_size(opt_tracename, + opt->chan_name, + opt->bufsize)) != 0) + return ret; + } - ret = 0; - if(WIFEXITED(status)) - ret = WEXITSTATUS(status); - if(ret) goto start_error; + return 0; +} - } else if(pid == 0) { - /* child */ - int ret; - if(mode != LTT_TRACE_HYBRID) { - if(append_trace) - ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c", - channel_path, "-d", "-a", "-N", num_threads, NULL); - else - ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c", - channel_path, "-d", "-N", num_threads, NULL); - } else { - if(append_trace) - ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c", - channel_path, "-d", "-a", "-N", num_threads, "-n", NULL); - else - ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c", - channel_path, "-d", "-N", num_threads, "-n", NULL); - } - if(ret) { - ret = errno; - perror("Error in executing the lttd daemon"); - exit(ret); - } - } else { - /* error */ - perror("Error in forking for lttd daemon"); +static int lttctl_create_trace(void) +{ + int ret; + int i; + struct lttctl_option *opt; + + ret = lttctl_setup_trace(opt_tracename); + if (ret) + goto setup_trace_fail; + + for (opt = opt_head; opt; opt = opt->next) { + if (opt->type != CHANNEL) + continue; + ret = lttctl_channel_setup(&opt->opt_mode.chan_opt); + if (ret) + goto set_option_fail;; } - ret = lttctl_start(handle, trace_name); - if(ret != 0) goto start_error; + ret = lttctl_set_trans(opt_tracename, opt_transport); + if (ret) + goto set_option_fail; + + ret = lttctl_alloc_trace(opt_tracename); + if (ret) + goto alloc_trace_fail; return 0; - /* error handling */ -start_error: - printf("Trace start error\n"); - ret |= lttctl_destroy_trace(handle, trace_name); -create_error: +alloc_trace_fail: +set_option_fail: + lttctl_destroy_trace(opt_tracename); +setup_trace_fail: return ret; } - - - -int lttctl_daemon_hybrid_finish(struct lttctl_handle *handle, char *trace_name) +/* + * Start a lttd daemon to write trace datas + * Dump overwrite channels on overwrite!=0 + * Dump normal(non-overwrite) channels on overwrite=0 + * + * ret: 0 on success + * !0 on fail + */ +static int lttctl_daemon(int overwrite) { - char channel_path[PATH_MAX] = ""; pid_t pid; - int ret; - char *lttd_path = getenv("LTT_DAEMON"); - - if(lttd_path == NULL) lttd_path = - PACKAGE_BIN_DIR "/lttd"; - - strcat(channel_path, channel_root); - strcat(channel_path, "/"); - strcat(channel_path, trace_name); - - - ret = lttctl_stop(handle, trace_name); - if(ret != 0) goto stop_error; + int status; pid = fork(); + if (pid < 0) { + perror("Error in forking for lttd daemon"); + return errno; + } - if(pid > 0) { - int status = 0; - /* parent */ - - ret = waitpid(pid, &status, 0); - if(ret == -1) { - ret = errno; - perror("Error in waitpid"); - goto destroy_error; + if (pid == 0) { + /* child */ + char *argv[16]; + int argc = 0; + char channel_path[PATH_MAX]; + char thread_num[16]; + + /* prog path */ + argv[argc] = getenv("LTT_DAEMON"); + if (argv[argc] == NULL) + argv[argc] = PACKAGE_BIN_DIR "/lttd"; + argc++; + + /* -t option */ + argv[argc] = "-t"; + argc++; + /* + * we allow modify of opt_write's content in new process + * for get rid of warning of assign char * to const char * + */ + argv[argc] = (char *)opt_write; + argc++; + + /* -c option */ + strcpy(channel_path, opt_channel_root); + strcat(channel_path, "/"); + strcat(channel_path, opt_tracename); + argv[argc] = "-c"; + argc++; + argv[argc] = channel_path; + argc++; + + /* -N option */ + sprintf(thread_num, "%u", opt_dump_threads); + argv[argc] = "-N"; + argc++; + argv[argc] = thread_num; + argc++; + + /* -a option */ + if (opt_append) { + argv[argc] = "-a"; + argc++; } - ret = 0; - if(WIFEXITED(status)) - ret = WEXITSTATUS(status); - if(ret) goto destroy_error; + /* -d option */ + argv[argc] = "-d"; + argc++; - } else if(pid == 0) { - /* child */ - int ret; - if(append_trace) - ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c", - channel_path, "-d", "-a", "-N", num_threads, "-f", NULL); - else - ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c", - channel_path, "-d", "-N", num_threads, "-f", NULL); - if(ret) { - ret = errno; - perror("Error in executing the lttd daemon"); - exit(ret); + /* overwrite option */ + if (overwrite) { + argv[argc] = "-f"; + argc++; + } else { + argv[argc] = "-n"; + argc++; } - } else { - /* error */ - perror("Error in forking for lttd daemon"); - } - ret = lttctl_destroy_trace(handle, trace_name); - if(ret != 0) goto destroy_error; + argv[argc] = NULL; - return 0; + execvp(argv[0], argv); - /* error handling */ -destroy_error: - printf("Hybrid trace destroy error\n"); -stop_error: - return ret; -} + perror("Error in executing the lttd daemon"); + exit(errno); + } + + /* parent */ + if (waitpid(pid, &status, 0) == -1) { + perror("Error in waitpid\n"); + return errno; + } + + if (!WIFEXITED(status)) { + fprintf(stderr, "lttd process interrupted\n"); + return status; + } + if (WEXITSTATUS(status)) + fprintf(stderr, "lttd process running failed\n"); + return WEXITSTATUS(status); +} -int main(int argc, char ** argv) +int main(int argc, char **argv) { int ret; - struct lttctl_handle *handle; - + ret = parse_arguments(argc, argv); + /* If user needs show help, we disregard other options */ + if (opt_help) { + show_arguments(); + return 0; + } - if(ret != 0) show_arguments(); - if(ret == EINVAL) return EINVAL; - if(ret == -1) return 0; + /* exit program if arguments wrong */ + if (ret) + return 1; show_info(); - - handle = lttctl_create_handle(); - - if(handle == NULL) return -1; - - switch(op) { - case CTL_OP_CREATE_START: - ret = lttctl_create_trace(handle, trace_name, mode, trace_type, - subbuf_size_low, n_subbufs_low, - subbuf_size_med, n_subbufs_med, - subbuf_size_high, n_subbufs_high); - if(!ret) - ret = lttctl_start(handle, trace_name); - break; - case CTL_OP_CREATE: - ret = lttctl_create_trace(handle, trace_name, mode, trace_type, - subbuf_size_low, n_subbufs_low, - subbuf_size_med, n_subbufs_med, - subbuf_size_high, n_subbufs_high); - break; - case CTL_OP_DESTROY: - ret = lttctl_destroy_trace(handle, trace_name); - break; - case CTL_OP_STOP_DESTROY: - ret = lttctl_stop(handle, trace_name); - if(!ret) - ret = lttctl_destroy_trace(handle, trace_name); - break; - case CTL_OP_START: - ret = lttctl_start(handle, trace_name); - break; - case CTL_OP_STOP: - ret = lttctl_stop(handle, trace_name); - break; - case CTL_OP_DAEMON: - ret = lttctl_daemon(handle, trace_name); - break; - case CTL_OP_DAEMON_HYBRID_FINISH: - ret = lttctl_daemon_hybrid_finish(handle, trace_name); - break; - case CTL_OP_NONE: - break; + + ret = lttctl_init(); + if (ret != 0) + return ret; + + if (opt_create) { + printf("lttctl: Creating trace\n"); + ret = lttctl_create_trace(); + if (ret) + goto op_fail; + + if (opt_write) { + printf("lttctl: Forking lttd\n"); + ret = lttctl_daemon(0); + if (ret) + goto op_fail; + } } - ret |= lttctl_destroy_handle(handle); - + if (opt_start) { + printf("lttctl: Starting trace\n"); + ret = lttctl_start(opt_tracename); + if (ret) + goto op_fail; + } + + if (opt_pause) { + printf("lttctl: Pausing trace\n"); + ret = lttctl_pause(opt_tracename); + if (ret) + goto op_fail; + } + + if (opt_destroy) { + if (opt_write) { + printf("lttctl: Forking lttd\n"); + ret = lttctl_daemon(1); + if (ret) + goto op_fail; + } + + printf("lttctl: Destroying trace\n"); + ret = lttctl_destroy_trace(opt_tracename); + if (ret) + goto op_fail; + } + +op_fail: + lttctl_destroy(); + return ret; }