X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=ltt-control%2Flttctl%2Flttctl.c;h=e08280aab2e066cd19f3b8479df1e6ae8ad9397a;hb=31efe1f8304f09a4f4139c387a98d3215cd423c9;hp=bdf6f98ab1be9fc2822e1e61f3c68b1d1d54234f;hpb=313b377139551fa3e8a1a7cb8783b210f8bed439;p=lttv.git diff --git a/ltt-control/lttctl/lttctl.c b/ltt-control/lttctl/lttctl.c index bdf6f98a..e08280aa 100644 --- a/ltt-control/lttctl/lttctl.c +++ b/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,82 +20,346 @@ #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_DESCRIPTION, - CTL_OP_NONE +#include +#define _GNU_SOURCE +#include + +#define OPT_MAX (1024) +#define OPT_NAMELEN (256) +#define OPT_VALSTRINGLEN (256) + +enum opt_type { + CHANNEL, }; -static char *trace_name = NULL; -static char *trace_type = "relay"; -static char *mode_name = NULL; -static unsigned subbuf_size = 0; -static unsigned n_subbufs = 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; -static char *trace_root = NULL; -static char *num_threads = "1"; - -static int sigchld_received = 0; - -void sigchld_handler(int signo) -{ - printf("signal %d received\n", signo); - sigchld_received = 1; -} +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; +}; + +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 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("-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 or flight recorder mode.\n"); - printf(" Mode values : normal (default) or flight.\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(" (optionnaly, you can set LTT_DAEMON\n"); - printf(" and the LTT_FACILITIES env. vars.)\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/relayfs/ltt)\n"); - printf("-z Size of the subbuffers (will be rounded to next page size)\n"); - printf("-x Number of subbuffers\n"); - printf("-e Get XML facilities description\n"); - printf("-a Append to trace\n"); - printf("-N Number of lttd threads\n"); + printf(" Advanced options:\n"); + printf(" --transport TRANSPORT\n"); + printf(" Set trace's transport. (ex. relay-locked or 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= (in bytes, rounded to " + "next power of 2)\n"); + printf(" can be set to all for all channels\n"); printf("\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"); +} + +/* + * 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 *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); + + 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); + } } +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 (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 * @@ -100,418 +368,466 @@ void show_arguments(void) * 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 { - 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 = (unsigned)atoi(argv[argn+1]); - argn++; - } else { - printf("Specify a number of subbuffers after -z.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'x': - if(argn+1 < argc) { - n_subbufs = (unsigned)atoi(argv[argn+1]); - argn++; - } else { - printf("Specify a subbuffer size after -x.\n"); - printf("\n"); - ret = EINVAL; - } - break; - case 'd': - op = CTL_OP_DAEMON; - break; - case 'e': - op = CTL_OP_DESCRIPTION; - 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(op != CTL_OP_DESCRIPTION && 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) { - 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) { - printf("Please specify -l ltt_root_path with the -d option.\n"); - printf("\n"); - ret = EINVAL; + + if (!opt_channel_root) + if (getdebugfsmntdir(channel_root_default) == 0) { + strcat(channel_root_default, "/ltt"); + opt_channel_root = channel_root_default; + } else { + 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; } } - if(op == CTL_OP_DESCRIPTION) { - if(trace_root == NULL) { - printf("Please specify -t trace_root_path with the -e option.\n"); - printf("\n"); - ret = EINVAL; - } - } + if (opt_dump_threads) { + if (!opt_write) { + fprintf(stderr, + "Dump_threads option must be combine with write" + " option\n"); + return -EINVAL; + } + } - return ret; -} + if (opt_channel_root) { + if (!opt_write) { + fprintf(stderr, + "Channel_root option must be combine with write" + " option\n"); + return -EINVAL; + } + } -void show_info(void) + return 0; +} + +static void show_info(void) { - printf("Linux Trace Toolkit Trace Control\n"); + printf("Linux Trace Toolkit Trace Control " VERSION"\n"); printf("\n"); - if(trace_name != NULL) { - printf("Controlling trace : %s\n", trace_name); - printf("\n"); - } + if (opt_tracename != NULL) { + printf("Controlling trace : %s\n", opt_tracename); + printf("\n"); + } } -int create_eventdefs(void) +static int lttctl_channel_setup(struct channel_option *opt) { - int ret = 0; - char eventdefs_path[PATH_MAX]; - char eventdefs_file[PATH_MAX]; - char facilities_file[PATH_MAX]; - char read_buf[BUF_SIZE]; - struct dirent *entry; - char *facilities_path = getenv("LTT_FACILITIES"); - if(facilities_path == NULL) facilities_path = - PACKAGE_DATA_DIR "/" PACKAGE "/facilities"; - - ret = mkdir(trace_root, S_IRWXU|S_IRWXG|S_IRWXO); - if(ret == -1 && errno != EEXIST) { - ret = errno; - perror("Cannot create trace_root directory"); - printf("trace_root is %s\n", trace_root); - goto error; - } - ret = 0; - - size_t trace_root_len = strlen(trace_root); - strncpy(eventdefs_path, trace_root, PATH_MAX); - strncat(eventdefs_path, "/eventdefs/", PATH_MAX - trace_root_len); - size_t eventdefs_path_len = strlen(eventdefs_path); - ret = mkdir(eventdefs_path, S_IRWXU|S_IRWXG|S_IRWXO); - if(ret == -1 && (!append_trace || errno != EEXIST)) { - ret = errno; - perror("Cannot create eventdefs directory"); - goto error; - } - ret = 0; - - DIR *facilities_dir = opendir(facilities_path); - - if(facilities_dir == NULL) { - perror("Cannot open facilities directory"); - ret = EEXIST; - goto error; - } - - while((entry = readdir(facilities_dir)) != NULL) { - if(entry->d_name[0] == '.') continue; - - printf("Appending facility file %s\n", entry->d_name); - strncpy(eventdefs_file, eventdefs_path, PATH_MAX); - strncat(eventdefs_file, entry->d_name, PATH_MAX - eventdefs_path_len); - /* Append to the file */ - FILE *dest = fopen(eventdefs_file, "a"); - if(!dest) { - perror("Cannot create eventdefs file"); - continue; - } - strncpy(facilities_file, facilities_path, PATH_MAX); - size_t facilities_dir_len = strlen(facilities_path); - strncat(facilities_file, "/", PATH_MAX - facilities_dir_len); - strncat(facilities_file, entry->d_name, PATH_MAX - facilities_dir_len-1); - FILE *src = fopen(facilities_file, "r"); - if(!src) { - ret = errno; - perror("Cannot open eventdefs file for reading"); - goto close_dest; - } - - do { - size_t read_size, write_size; - read_size = fread(read_buf, sizeof(char), BUF_SIZE, src); - if(ferror(src)) { - ret = errno; - perror("Cannot read eventdefs file"); - goto close_src; - } - write_size = fwrite(read_buf, sizeof(char), read_size, dest); - if(ferror(dest)) { - ret = errno; - perror("Cannot write eventdefs file"); - goto close_src; - } - } while(!feof(src)); - - /* Add spacing between facilities */ - fwrite("\n", 1, 1, dest); - -close_src: - fclose(src); -close_dest: - fclose(dest); - } - - closedir(facilities_dir); - -error: - return ret; + int ret; -} + 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; + } + return 0; +} -int lttctl_daemon(struct lttctl_handle *handle, char *trace_name) +static int lttctl_create_trace(void) { - char channel_path[PATH_MAX] = ""; - pid_t pid; int ret; - char *lttd_path = getenv("LTT_DAEMON"); - struct sigaction act; + 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;; + } - 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_set_trans(opt_tracename, opt_transport); + if (ret) + goto set_option_fail; - - ret = lttctl_create_trace(handle, trace_name, mode, trace_type, subbuf_size, n_subbufs); - if(ret != 0) goto create_error; + ret = lttctl_alloc_trace(opt_tracename); + if (ret) + goto alloc_trace_fail; - act.sa_handler = sigchld_handler; - sigemptyset(&(act.sa_mask)); - sigaddset(&(act.sa_mask), SIGCHLD); - sigaction(SIGCHLD, &act, NULL); - - pid = fork(); + return 0; - if(pid > 0) { - int status; - /* parent */ - while(!(sigchld_received)) pause(); +alloc_trace_fail: +set_option_fail: + lttctl_destroy_trace(opt_tracename); +setup_trace_fail: + return ret; +} - waitpid(pid, &status, 0); - ret = 0; - if(WIFEXITED(status)) - ret = WEXITSTATUS(status); - if(ret) goto start_error; +/* + * 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) +{ + pid_t pid; + int status; - printf("Creating supplementary trace files\n"); - ret = create_eventdefs(); - if(ret) goto start_error; + pid = fork(); + if (pid < 0) { + perror("Error in forking for lttd daemon"); + return errno; + } - } else if(pid == 0) { + 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, NULL); - else - ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c", - channel_path, "-d", "-N", num_threads, NULL); - if(ret) { - ret = errno; - perror("Error in executing the lttd daemon"); - exit(ret); + 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++; } - } else { - /* error */ - perror("Error in forking for lttd daemon"); + + /* -d option */ + argv[argc] = "-d"; + argc++; + + /* overwrite option */ + if (overwrite) { + argv[argc] = "-f"; + argc++; + } else { + argv[argc] = "-n"; + argc++; + } + + argv[argc] = NULL; + + execvp(argv[0], argv); + + perror("Error in executing the lttd daemon"); + exit(errno); } - ret = lttctl_start(handle, trace_name); - if(ret != 0) goto start_error; + /* parent */ + if (waitpid(pid, &status, 0) == -1) { + perror("Error in waitpid\n"); + return errno; + } - return 0; + if (!WIFEXITED(status)) { + fprintf(stderr, "lttd process interrupted\n"); + return status; + } - /* error handling */ -start_error: - printf("Trace start error\n"); - ret |= lttctl_destroy_trace(handle, trace_name); -create_error: - return ret; + 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, - n_subbufs); - 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, - n_subbufs); - 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_DESCRIPTION: - ret = create_eventdefs(); - 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; }