X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=trunk%2Fltt-control%2Flttctl%2Flttctl.c;fp=trunk%2Fltt-control%2Flttctl%2Flttctl.c;h=4620983bc73446da6d0acd4bd76c16b4856ccffa;hb=c928825d1f57008dcf0860ec6b6f24cca463c3d5;hp=c04d4f5ca10d9569eb42eea98b7c5029a39fc605;hpb=43519792a247175cbea349e9c3ab1dad6049876d;p=ltt-control.git diff --git a/trunk/ltt-control/lttctl/lttctl.c b/trunk/ltt-control/lttctl/lttctl.c index c04d4f5..4620983 100644 --- a/trunk/ltt-control/lttctl/lttctl.c +++ b/trunk/ltt-control/lttctl/lttctl.c @@ -16,98 +16,142 @@ #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 lttcrl_option_type { + option_type_unknown, + option_type_string, + option_type_int, + option_type_uint, + option_type_positive, + option_type_bool, }; -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 { + char name1[OPT_NAMELEN]; + char name2[OPT_NAMELEN]; + char name3[OPT_NAMELEN]; + union { + char v_string[OPT_VALSTRINGLEN]; + int v_int; + unsigned int v_uint; + int v_bool:1; + } value; +}; + +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 struct lttctl_option opt_options[OPT_MAX]; +static unsigned int opt_option_n; +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("-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(" 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(" 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) +static int getdebugfsmntdir(char *mntdir) { char mnt_dir[PATH_MAX]; char mnt_type[PATH_MAX]; FILE *fp = fopen("/proc/mounts", "r"); - if (!fp) { - return EINVAL; - } + if (!fp) + return -EINVAL; while (1) { - if (fscanf(fp, "%*s %s %s %*s %*s %*s", mnt_dir, mnt_type) <= 0) { - return ENOENT; - } + if (fscanf(fp, "%*s %s %s %*s %*s %*s", mnt_dir, mnt_type) <= 0) + return -ENOENT; + if (!strcmp(mnt_type, "debugfs")) { strcpy(mntdir, mnt_dir); return 0; @@ -115,6 +159,275 @@ 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 *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; +} + +/* + * get option's type by its name, + * can also be used to check is option exists + * (return option_type_unknown when not exist) + */ +static enum lttcrl_option_type opt_type(const char *name1, const char *name2, + const char *name3) +{ + if (!name1 || !name2 || !name3) + return option_type_unknown; + + if (strlen(name1) >= OPT_NAMELEN + || strlen(name2) >= OPT_NAMELEN + || strlen(name3) >= OPT_NAMELEN) + return option_type_unknown; + + if (strcmp(name1, "channel") == 0) { + /* Option is channel class */ + if (strcmp(name3, "enable") == 0) + return option_type_bool; + if (strcmp(name3, "overwrite") == 0) + return option_type_bool; + if (strcmp(name3, "bufnum") == 0) + return option_type_uint; + if (strcmp(name3, "bufsize") == 0) + return option_type_uint; + return option_type_unknown; + } + + /* + * Now we only support channel options + * other option class' will used in future + */ + + return option_type_unknown; +} + +static struct lttctl_option *find_opt(const char *name1, const char *name2, + const char *name3) +{ + int i; + + if (!name1 || !name2 || !name3) + return NULL; + + for (i = 0; i < opt_option_n; i++) { + if (strcmp(opt_options[i].name1, name1) == 0 + && strcmp(opt_options[i].name2, name2) == 0 + && strcmp(opt_options[i].name3, name3) == 0) + return opt_options + i; + } + + return NULL; +} + +static struct lttctl_option *get_opt(const char *name1, const char *name2, + const char *name3) +{ + struct lttctl_option *opt; + + if (!name1 || !name2 || !name3) + return NULL; + + opt = find_opt(name1, name2, name3); + if (opt) + return opt; + + if (opt_option_n >= OPT_MAX) { + fprintf(stderr, "Option number out of range\n"); + return NULL; + } + + if (strlen(name1) >= OPT_NAMELEN + || strlen(name2) >= OPT_NAMELEN + || strlen(name3) >= OPT_NAMELEN) { + fprintf(stderr, "Option name too long: %s.%s.%s\n", + name1, name2, name3); + return NULL; + } + + opt = &opt_options[opt_option_n]; + strcpy(opt->name1, name1); + strcpy(opt->name2, name2); + strcpy(opt->name3, name3); + opt_option_n++; + + return opt; +} + +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]; + + enum lttcrl_option_type opttype; + int opt_intval; + unsigned int opt_uintval; + struct lttctl_option *newopt; + + 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 error: %s\n", optarg); + return -EINVAL; + } + + /* + * check and add option + */ + opttype = opt_type(name1, name2, name3); + switch (opttype) { + case option_type_unknown: + fprintf(stderr, "Option not supported: %s\n", optarg); + return -EINVAL; + case option_type_string: + newopt = get_opt(name1, name2, name3); + if (!newopt) + return -EINVAL; + strcpy(newopt->value.v_string, opt_valstr); + return 0; + case option_type_int: + ret = sscanf(opt_valstr, "%d", &opt_intval); + if (ret != 1) { + fprintf(stderr, "Option format error: %s\n", optarg); + return -EINVAL; + } + newopt = get_opt(name1, name2, name3); + if (!newopt) + return -EINVAL; + newopt->value.v_int = opt_intval; + return 0; + case option_type_uint: + ret = sscanf(opt_valstr, "%u", &opt_uintval); + if (ret != 1) { + fprintf(stderr, "Option format error: %s\n", optarg); + return -EINVAL; + } + newopt = get_opt(name1, name2, name3); + if (!newopt) + return -EINVAL; + newopt->value.v_uint = opt_uintval; + return 0; + case option_type_positive: + ret = sscanf(opt_valstr, "%u", &opt_uintval); + if (ret != 1 || opt_uintval == 0) { + fprintf(stderr, "Option format error: %s\n", optarg); + return -EINVAL; + } + newopt = get_opt(name1, name2, name3); + if (!newopt) + return -EINVAL; + newopt->value.v_uint = opt_uintval; + return 0; + case option_type_bool: + if (opt_valstr[1] != 0) { + fprintf(stderr, "Option format error: %s\n", optarg); + return -EINVAL; + } + if (opt_valstr[0] == 'Y' || opt_valstr[0] == 'y' + || opt_valstr[0] == '1') + opt_intval = 1; + else if (opt_valstr[0] == 'N' || opt_valstr[0] == 'n' + || opt_valstr[0] == '0') + opt_intval = 0; + else { + fprintf(stderr, "Option format error: %s\n", optarg); + return -EINVAL; + } + + newopt = get_opt(name1, name2, name3); + if (!newopt) + return -EINVAL; + newopt->value.v_bool = opt_intval; + return 0; + default: + fprintf(stderr, "Internal error on opt %s\n", optarg); + return -EINVAL; + } + + return 0; /* should not run to here */ +} + /* parse_arguments * * Parses the command line arguments. @@ -122,445 +435,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; - } else { - printf("Please specify -l ltt_root_path with the -d option.\n"); - printf("\n"); - ret = EINVAL; + opt_channel_root = channel_root_default; } + /* Todo: + * if (!opt_channel_root) + * if (auto_mount_debugfs_dir(channel_root_default) == 0) + * opt_channel_root = debugfs_dir_mnt_point; + */ + if (!opt_channel_root) { + fprintf(stderr, + "Channel_root is necessary for -w option," + " but neither --channel_root option\n" + "specified, nor debugfs's mount dir found.\n"); + return -EINVAL; } + + if (opt_dump_threads == 0) + opt_dump_threads = 1; } - return ret; + if (opt_append) { + if (!opt_write) { + fprintf(stderr, + "Append option must be combine with write" + " option\n"); + return -EINVAL; + } + } + + if (opt_dump_threads) { + if (!opt_write) { + fprintf(stderr, + "Dump_threads option must be combine with write" + " option\n"); + return -EINVAL; + } + } + + if (opt_channel_root) { + if (!opt_write) { + fprintf(stderr, + "Channel_root option must be combine with write" + " option\n"); + return -EINVAL; + } + } + + return 0; } -void show_info(void) +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_create_trace(void) { - 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; + int i; + + ret = lttctl_setup_trace(opt_tracename); + if (ret) + goto setup_trace_fail; + + for (i = 0; i < opt_option_n; i++) { + if (strcmp(opt_options[i].name1, "channel") != 0) + continue; + + if (strcmp(opt_options[i].name3, "enable") == 0) { + ret = lttctl_set_channel_enable(opt_tracename, + opt_options[i].name2, + opt_options[i].value.v_bool); + if (ret) + goto set_option_fail; } - ret = 0; - if(WIFEXITED(status)) - ret = WEXITSTATUS(status); - if(ret) goto start_error; + if (strcmp(opt_options[i].name3, "overwrite") == 0) { + ret = lttctl_set_channel_overwrite(opt_tracename, + opt_options[i].name2, + opt_options[i].value.v_bool); + if (ret) + goto set_option_fail; + } - } 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 (strcmp(opt_options[i].name3, "bufnum") == 0) { + ret = lttctl_set_channel_subbuf_num(opt_tracename, + opt_options[i].name2, + opt_options[i].value.v_uint); + if (ret) + goto set_option_fail; } - if(ret) { - ret = errno; - perror("Error in executing the lttd daemon"); - exit(ret); + + if (strcmp(opt_options[i].name3, "bufsize") == 0) { + ret = lttctl_set_channel_subbuf_size(opt_tracename, + opt_options[i].name2, + opt_options[i].value.v_uint); + if (ret) + goto set_option_fail; } - } else { - /* error */ - perror("Error in forking for lttd daemon"); } - 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"); -int main(int argc, char ** argv) + return WEXITSTATUS(status); +} + +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; + } + } + + 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; } - ret |= lttctl_destroy_handle(handle); - + 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; }