From 0c89df6cb75f1d967003137e7fc334af1128a42f Mon Sep 17 00:00:00 2001 From: Nils Carlson Date: Fri, 7 Jan 2011 15:41:27 +0100 Subject: [PATCH] ustctl cli rewrite Completely re-write the ustctl cli making it behave in a similar fashion to git. This makes more sense than using long-options as commands. Signed-off-by: Nils Carlson --- ustctl/Makefile.am | 2 +- ustctl/channel_cmds.c | 153 ++++++++++ ustctl/cli.c | 207 ++++++++++++++ ustctl/cli.h | 50 ++++ ustctl/marker_cmds.c | 144 ++++++++++ ustctl/scanning_functions.c | 84 ++++++ ustctl/scanning_functions.h | 26 ++ ustctl/trace_cmds.c | 162 +++++++++++ ustctl/ustctl.c | 557 ++++++++---------------------------- 9 files changed, 951 insertions(+), 434 deletions(-) create mode 100644 ustctl/channel_cmds.c create mode 100644 ustctl/cli.c create mode 100644 ustctl/cli.h create mode 100644 ustctl/marker_cmds.c create mode 100644 ustctl/scanning_functions.c create mode 100644 ustctl/scanning_functions.h create mode 100644 ustctl/trace_cmds.c diff --git a/ustctl/Makefile.am b/ustctl/Makefile.am index 49e46f0..3b09745 100644 --- a/ustctl/Makefile.am +++ b/ustctl/Makefile.am @@ -5,7 +5,7 @@ AM_CFLAGS = -fno-strict-aliasing bin_PROGRAMS = ustctl ustctl_SOURCES = \ - ustctl.c + ustctl.c marker_cmds.c trace_cmds.c channel_cmds.c cli.c cli.h scanning_functions.c scanning_functions.h ustctl_CFLAGS = -DUST_COMPONENT=ustctl -fno-strict-aliasing diff --git a/ustctl/channel_cmds.c b/ustctl/channel_cmds.c new file mode 100644 index 0000000..51f7d50 --- /dev/null +++ b/ustctl/channel_cmds.c @@ -0,0 +1,153 @@ +/* Copyright (C) 2011 Ericsson AB, Nils Carlson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include +#include +#include +#include "scanning_functions.h" +#include "usterr.h" +#include "cli.h" + +static int set_subbuf_size(int argc, char *argv[]) +{ + int result = 0; + pid_t pid; + char *channel = NULL; + unsigned int size; + + pid = parse_pid(argv[1]); + + if (scan_ch_and_num(argv[3], &channel, &size)) { + fprintf(stderr, "Failed to scan channel and size from" + " %s\n", argv[3]); + if (channel) + free(channel); + return -1; + } + if (ustcmd_set_subbuf_size(argv[2], channel, size, pid)) { + ERR("error while trying to set the size of subbuffers " + "for PID %u\n", + pid); + result = -1; + } + + free(channel); + + return result; +} + +static int set_subbuf_num(int argc, char *argv[]) +{ + int result = 0; + pid_t pid; + char *channel = NULL; + unsigned int num; + + pid = parse_pid(argv[1]); + + if (scan_ch_and_num(argv[3], &channel, &num)) { + fprintf(stderr, "Failed to scan channel and number from" + " %s\n", argv[3]); + if (channel) + free(channel); + return -1; + } + if (ustcmd_set_subbuf_num(argv[2], channel, num, pid)) { + ERR("error while trying to set the number of subbuffers for PID %u\n", + pid); + result = -1; + } + + free(channel); + + return result; +} + +static int get_subbuf_size(int argc, char *argv[]) +{ + pid_t pid; + unsigned int size; + + pid = parse_pid(argv[1]); + + if ((size = ustcmd_get_subbuf_size(argv[2], argv[3], pid)) < 0) { + ERR("error while trying to get the subbuffer size from PID %u\n", + pid); + return -1; + } + + printf("The subbufer size is %d bytes\n", size); + + return 0; +} + +static int get_subbuf_num(int argc, char *argv[]) +{ + pid_t pid; + unsigned int num; + + pid = parse_pid(argv[1]); + + if ((num = ustcmd_get_subbuf_num(argv[2], argv[3], pid)) < 0) { + ERR("error while trying to get the subbuffer size from PID %u\n", + pid); + return -1; + } + + printf("There are %u subbufers in each buffer\n", num); + + return 0; +} + +struct cli_cmd __cli_cmds channel_cmds[] = { + { + .name = "set-subbuf-size", + .description = "Set the subbuffer size for a channel", + .help_text = "set-subbuf-size / \n" + "Set the subbuffer size for a channel\n", + .function = set_subbuf_size, + .desired_args = 3, + .desired_args_op = CLI_EQ, + }, + { + .name = "set-subbuf-num", + .description = "Set the number of subbuffers for a channel", + .help_text = "set-subbuf-num / \n" + "Set the number of subbuffers for a channel\n", + .function = set_subbuf_num, + .desired_args = 3, + .desired_args_op = CLI_EQ, + }, + { + .name = "get-subbuf-size", + .description = "Get the subbuffer size for a channel", + .help_text = "get-subbuf-size \n" + "Get the subbuffer size for a channel\n", + .function = get_subbuf_size, + .desired_args = 3, + .desired_args_op = CLI_EQ, + }, + { + .name = "get-subbuf-num", + .description = "Get the number of subbuffers for a channel", + .help_text = "get-subbuf-num \n" + "Get the number of subbuffers for a channel\n", + .function = get_subbuf_num, + .desired_args = 3, + .desired_args_op = CLI_EQ, + }, +}; diff --git a/ustctl/cli.c b/ustctl/cli.c new file mode 100644 index 0000000..0ca9db5 --- /dev/null +++ b/ustctl/cli.c @@ -0,0 +1,207 @@ +/* Copyright (C) 2011 Ericsson AB, Nils Carlson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define _GNU_SOURCE +#include +#include +#include +#include "cli.h" + +/* This dummy command is needed to create the sections in cli.o before + * other .o files have these sections, usefull for development. + */ +static int _dummy(int argc, char *argv[]) { + return 0; +} + +/* Define a dummy cmd to guarantee existence of the builtin variables */ +struct cli_cmd __cli_cmds __dummy_cli_cmd[] = { + { + .name = "_dummy", + .description = NULL, + .help_text = NULL, + .function = _dummy, + .desired_args = 0, + .desired_args_op = 0, + }, +}; + +extern struct cli_cmd __start___cli_cmds[] __attribute__((visibility("hidden"))); +extern struct cli_cmd __stop___cli_cmds[] __attribute__((visibility("hidden"))); + +static struct cli_cmd **cli_cmd_list; +static int cli_cmd_list_size; + +static char *process_name; + +static int compute_cli_cmds_size(void) +{ + long cli_cmds_start, cli_cmds_end; + + cli_cmds_start = (long)__start___cli_cmds; + cli_cmds_end = (long)__stop___cli_cmds; + + return (cli_cmds_end - cli_cmds_start) / sizeof(struct cli_cmd); +} + +static void __attribute__((constructor)) generate_cli_cmd_list(int argc, char *argv[]) +{ + struct cli_cmd *cli_cmd; + int section_size, i; + + process_name = basename(argv[0]); + + section_size = compute_cli_cmds_size(); + + cli_cmd_list = malloc(section_size * sizeof(void *)); + if (!cli_cmd_list) { + fprintf(stderr, "Failed to allocate command list!"); + exit(EXIT_FAILURE); + } + + cli_cmd_list_size = 0; + + cli_cmd = __start___cli_cmds; + for (i = 0; i < section_size; i++) { + if (&cli_cmd[i] == &__dummy_cli_cmd[0]) { + continue; + } + + if (cli_cmd[i].name) { + cli_cmd_list[cli_cmd_list_size++] = &cli_cmd[i]; + } + } +} + +struct cli_cmd *find_cli_cmd(const char *command) +{ + int i; + + for (i = 0; i < cli_cmd_list_size; i++) { + if (!strcmp(cli_cmd_list[i]->name, command)) { + return cli_cmd_list[i]; + } + } + + return NULL; +} + +static int cmpcli_cmds(const void *p1, const void *p2) +{ + return strcmp(* (char * const *) ((struct cli_cmd *)p1)->name, + * (char * const *) ((struct cli_cmd *)p2)->name); +} + +#define HELP_BUFFER_SIZE 4096 + +static void print_cmd_help(const char *prefix, const char *infix, + struct cli_cmd *cli_cmd) +{ + if (cli_cmd->help_text) { + fprintf(stderr, "%s%s%s", + prefix, + infix, + cli_cmd->help_text); + } else if (cli_cmd->description) { + fprintf(stderr, "%s%s%s\n%s\n", + prefix, + infix, + cli_cmd->name, + cli_cmd->description); + } else { + fprintf(stderr, "No help available for %s\n", + cli_cmd->name); + } +} + +void list_cli_cmds(int option) +{ + int i; + + qsort(cli_cmd_list, cli_cmd_list_size, sizeof(void *), cmpcli_cmds); + + for (i = 0; i < cli_cmd_list_size; i++) { + switch (option) { + case CLI_SIMPLE_LIST: + fprintf(stderr, "%s ", cli_cmd_list[i]->name); + break; + case CLI_DESCRIPTIVE_LIST: + fprintf(stderr, " %-25s%s\n", cli_cmd_list[i]->name, + cli_cmd_list[i]->description); + break; + case CLI_EXTENDED_LIST: + print_cmd_help("", "", cli_cmd_list[i]); + fprintf(stderr, "\n"); + break; + } + } + + if (option == CLI_SIMPLE_LIST) { + fprintf(stderr, "\n"); + } +} + +int cli_print_help(const char *command) +{ + struct cli_cmd *cli_cmd; + + cli_cmd = find_cli_cmd(command); + if (!cli_cmd) { + return -1; + } + + print_cmd_help(process_name, " ", cli_cmd); + + return 0; +} + +static void cli_check_argc(const char *command, int args, + int operator, int desired_args) +{ + switch(operator) { + case CLI_EQ: + if (args != desired_args) + goto print_error; + break; + case CLI_GE: + if (args < desired_args) + goto print_error; + break; + } + + return; + +print_error: + fprintf(stderr, "%s %s requires %s%d argument%s, see usage.\n", + process_name, command, operator == CLI_EQ ? "" : "at least ", + desired_args, desired_args > 1 ? "s" : ""); + cli_print_help(command); + exit(EXIT_FAILURE); +} + + +void cli_dispatch_cmd(struct cli_cmd *cmd, int argc, char *argv[]) +{ + cli_check_argc(cmd->name, argc - 1, cmd->desired_args_op, + cmd->desired_args); + + if (cmd->function(argc, argv)) { + exit(EXIT_FAILURE); + } + + exit(EXIT_SUCCESS); +} diff --git a/ustctl/cli.h b/ustctl/cli.h new file mode 100644 index 0000000..a1e4eed --- /dev/null +++ b/ustctl/cli.h @@ -0,0 +1,50 @@ +/* Copyright (C) 2011 Ericsson AB, Nils Carlson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef _CLI_H +#define _CLI_H + +struct cli_cmd { + const char *name; + const char *description; + const char *help_text; + int (*function)(int, char **); + int desired_args; + int desired_args_op; +} __attribute__((aligned(8))); + +#define __cli_cmds __attribute__((section("__cli_cmds"), aligned(8), used)) + +struct cli_cmd *find_cli_cmd(const char *command); + +enum cli_list_opts { + CLI_SIMPLE_LIST, + CLI_DESCRIPTIVE_LIST, + CLI_EXTENDED_LIST, +}; + +void list_cli_cmds(int option); + +int cli_print_help(const char *command); + +enum cli_arg_ops { + CLI_EQ, + CLI_GE, +}; + +void cli_dispatch_cmd(struct cli_cmd *cmd, int argc, char *argv[]); + +#endif /* _CLI_H */ diff --git a/ustctl/marker_cmds.c b/ustctl/marker_cmds.c new file mode 100644 index 0000000..20222dd --- /dev/null +++ b/ustctl/marker_cmds.c @@ -0,0 +1,144 @@ +/* Copyright (C) 2011 Ericsson AB, Nils Carlson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include +#include +#include +#include "scanning_functions.h" +#include "usterr.h" +#include "cli.h" + +static int list_markers(int argc, char *argv[]) +{ + struct marker_status *cmsf = NULL; + int i; + pid_t pid; + + pid = parse_pid(argv[1]); + + if (ustcmd_get_cmsf(&cmsf, pid)) { + ERR("error while trying to list markers for PID %u\n", pid); + return -1; + } + for (i = 0; cmsf[i].channel; i++) { + printf("{PID: %u, channel/marker: %s/%s, " + "state: %u, fmt: %s}\n", + (unsigned int) pid, + cmsf[i].channel, + cmsf[i].marker, + cmsf[i].state, + cmsf[i].fs); + } + ustcmd_free_cmsf(cmsf); + return 0; +} + +static int enable_marker(int argc, char *argv[]) +{ + int i, result = 0; + pid_t pid; + char *channel, *marker; + + pid = parse_pid(argv[1]); + + for (i = 3; i < argc; i++) { + channel = NULL; + marker = NULL; + if (scan_ch_marker(argv[i], + &channel, &marker)) { + result = -1; + fprintf(stderr, "Failed to scan channel and marker from" + " %s\n", argv[i]); + if (channel) + free(channel); + if (marker) + free(marker); + } + if (ustcmd_set_marker_state(argv[2], channel, marker, 1, pid)) { + PERROR("error while trying to enable marker %s with PID %u", + argv[i], pid); + result = -1; + } + free(channel); + free(marker); + } + + return result; +} + +static int disable_marker(int argc, char *argv[]) +{ + int i, result = 0; + pid_t pid; + char *channel, *marker; + + pid = parse_pid(argv[1]); + + for (i = 3; i < argc; i++) { + channel = NULL; + marker = NULL; + if (scan_ch_marker(argv[i], + &channel, &marker)) { + fprintf(stderr, "Failed to scan channel and marker from" + " %s\n", argv[i]); + if (channel) + free(channel); + if (marker) + free(marker); + return -1; + } + if (ustcmd_set_marker_state(argv[2], channel, marker, 0, pid)) { + PERROR("error while trying to disable marker %s with PID %u", + argv[i], pid); + result = -1; + } + free(channel); + free(marker); + } + + return result; +} + +struct cli_cmd __cli_cmds marker_cmds[] = { + { + .name = "list-markers", + .description = "List markers for a given pid", + .help_text = "list-markers \n" + "List the markers in a process\n", + .function = list_markers, + .desired_args = 1, + .desired_args_op = CLI_EQ, + }, + { + .name = "enable-marker", + .description = "Enable markers for a given pid", + .help_text = "enable-marker /... \n" + "Enable the listed markers for the trace in process pid\n", + .function = enable_marker, + .desired_args = 3, + .desired_args_op = CLI_GE, + }, + { + .name = "disable-marker", + .description = "Disable markers for a given pid", + .help_text = "disable-marker /... \n" + "Disable the listed markers for the trace in process pid\n", + .function = disable_marker, + .desired_args = 3, + .desired_args_op = CLI_GE, + } +}; diff --git a/ustctl/scanning_functions.c b/ustctl/scanning_functions.c new file mode 100644 index 0000000..0cf809e --- /dev/null +++ b/ustctl/scanning_functions.c @@ -0,0 +1,84 @@ +/* Copyright (C) 2011 Ericsson AB, Nils Carlson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define _GNU_SOURCE +#include +#include +#include "usterr.h" + + +pid_t parse_pid(const char *pid_string) +{ + pid_t pid; + + errno = 0; + pid = strtoull(pid_string, NULL, 10); + if (errno) { + perror("Failed to parse pid"); + exit(EXIT_FAILURE); + } + + return pid; +} + +int scan_ch_marker(const char *channel_marker, char **channel, char **marker) +{ + int result; + + *channel = NULL; + *marker = NULL; + + result = sscanf(channel_marker, "%a[^/]/%as", channel, marker); + if (result != 2) { + if (errno) { + PERROR("Failed to read channel and marker names"); + } else { + ERR("Failed to parse marker and channel names"); + } + if (*channel) { + free(*channel); + } + if (*marker) { + free(*marker); + } + return -1; + } + + return 0; +} + +int scan_ch_and_num(const char *ch_num, char **channel, unsigned int *num) +{ + int result; + + *channel = NULL; + + result = sscanf(ch_num, "%a[^/]/%u", channel, num); + if (result != 2) { + if (errno) { + PERROR("Failed to parse channel and number"); + } else { + ERR("Failed to parse channel and number"); + } + if (*channel) { + free(*channel); + } + return -1; + } + + return 0; +} diff --git a/ustctl/scanning_functions.h b/ustctl/scanning_functions.h new file mode 100644 index 0000000..c37eba1 --- /dev/null +++ b/ustctl/scanning_functions.h @@ -0,0 +1,26 @@ +/* Copyright (C) 2011 Ericsson AB, Nils Carlson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __SCANNING_FUNCTIONS_H +#define __SCANNING_FUNCTIONS_H + +pid_t parse_pid(const char *pid_string); + +int scan_ch_marker(const char *channel_marker, char **channel, char **marker); + +int scan_ch_and_num(const char *ch_num, char **channel, unsigned int *num); + +#endif /* __SCANNING_FUNCTIONS_H */ diff --git a/ustctl/trace_cmds.c b/ustctl/trace_cmds.c new file mode 100644 index 0000000..f6e11a7 --- /dev/null +++ b/ustctl/trace_cmds.c @@ -0,0 +1,162 @@ +/* Copyright (C) 2011 Ericsson AB, Nils Carlson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include +#include +#include +#include "scanning_functions.h" +#include "usterr.h" +#include "cli.h" + + +static int create_trace(int argc, char *argv[]) +{ + pid_t pid; + + pid = parse_pid(argv[1]); + + if (ustcmd_create_trace(argv[2], pid)) { + ERR("Failed to create trace %s for PID %u\n", argv[2], pid); + return -1; + } + + return 0; +} + +static int alloc_trace(int argc, char *argv[]) +{ + pid_t pid; + + pid = parse_pid(argv[1]); + + if (ustcmd_alloc_trace(argv[2], pid)) { + ERR("Failed to allocate trace %s for PID %u\n", argv[2], pid); + return -1; + } + return 0; +} + +static int start_trace(int argc, char *argv[]) +{ + pid_t pid; + + pid = parse_pid(argv[1]); + + if (ustcmd_start_trace(argv[2], pid)) { + ERR("Failed to start trace %s for PID %u\n", argv[2], pid); + return -1; + } + return 0; +} + +static int stop_trace(int argc, char *argv[]) +{ + pid_t pid; + + pid = parse_pid(argv[1]); + + if (ustcmd_stop_trace(argv[2], pid)) { + ERR("Failed to stop trace %s for PID %u\n", argv[2], pid); + return -1; + } + return 0; +} + +static int destroy_trace(int argc, char *argv[]) +{ + pid_t pid; + + pid = parse_pid(argv[1]); + + if (ustcmd_destroy_trace(argv[2], pid)) { + ERR("Failed to destroy trace %s for PID %u\n", argv[2], pid); + return -1; + } + return 0; +} + +static int force_subbuf_switch(int argc, char *argv[]) +{ + pid_t pid; + + pid = parse_pid(argv[1]); + + if (ustcmd_force_switch(pid)) { + ERR("error while trying to force switch for PID %u\n", pid); + return -1; + } + + return 0; +} + +struct cli_cmd __cli_cmds trace_cmds[] = { + { + .name = "create-trace", + .description = "Create a trace for a process", + .help_text = "create-trace \n" + "Create a trace for a process\n", + .function = create_trace, + .desired_args = 2, + .desired_args_op = CLI_EQ, + }, + { + .name = "alloc-trace", + .description = "Allocate a trace for a process", + .help_text = "alloc-trace \n" + "Allocate a trace for a process\n", + .function = alloc_trace, + .desired_args = 2, + .desired_args_op = CLI_EQ, + }, + { + .name = "start-trace", + .description = "Start a trace for a process", + .help_text = "start-trace \n" + "Start a trace for a process\n", + .function = start_trace, + .desired_args = 2, + .desired_args_op = CLI_EQ, + }, + { + .name = "stop-trace", + .description = "Stop a trace for a process", + .help_text = "stop-trace \n" + "Stop a trace for a process\n", + .function = stop_trace, + .desired_args = 2, + .desired_args_op = CLI_EQ, + }, + { + .name = "destroy-trace", + .description = "Destroy a trace for a process", + .help_text = "destroy-trace \n" + "Destroy a trace for a process\n", + .function = destroy_trace, + .desired_args = 2, + .desired_args_op = CLI_EQ, + }, + { + .name = "force-subbuf-switch", + .description = "Force a subbuffer switch", + .help_text = "force-subbuf-switch \n" + "Force a subbuffer switch for a trace, currently this forces\n" + "a subbuffer switch for all traces in a process\n", + .function = force_subbuf_switch, + .desired_args = 2, + .desired_args_op = CLI_EQ, + }, +}; diff --git a/ustctl/ustctl.c b/ustctl/ustctl.c index aad3834..2b75a58 100644 --- a/ustctl/ustctl.c +++ b/ustctl/ustctl.c @@ -1,4 +1,5 @@ /* Copyright (C) 2009 Pierre-Marc Fournier + * Copyright (C) 2011 Ericsson AB, Nils Carlson * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -24,476 +25,166 @@ #include "ust/ustcmd.h" #include "usterr.h" +#include "cli.h" +#include "scanning_functions.h" -enum command { - CREATE_TRACE=1000, - ALLOC_TRACE, - START_TRACE, - STOP_TRACE, - DESTROY_TRACE, - LIST_MARKERS, - LIST_TRACE_EVENTS, - ENABLE_MARKER, - DISABLE_MARKER, - GET_ONLINE_PIDS, - SET_SUBBUF_SIZE, - SET_SUBBUF_NUM, - GET_SUBBUF_SIZE, - GET_SUBBUF_NUM, - GET_SOCK_PATH, - SET_SOCK_PATH, - FORCE_SWITCH, - UNKNOWN -}; - -struct ust_opts { - enum command cmd; - pid_t *pids; - char *regex; -}; - -char *progname = NULL; - -void usage(void) +void usage(const char *process_name) { - fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname); - fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\ -\n\ -Commands:\n\ - --create-trace\t\t\tCreate trace\n\ - --alloc-trace\t\t\tAlloc trace\n\ - --start-trace\t\t\tStart tracing\n\ - --stop-trace\t\t\tStop tracing\n\ - --destroy-trace\t\t\tDestroy the trace\n\ - --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\ - --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\ - --set-sock-path\t\t\tSet the path of the daemon socket\n\ - --get-subbuf-size \"CHANNEL\"\t\tGet the size of subbuffers per channel\n\ - --get-subbuf-num \"CHANNEL\"\t\tGet the number of subbuffers per channel\n\ - --get-sock-path\t\t\tGet the path of the daemon socket\n\ - --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\ - --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\ - --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\ - --list-trace-events\t\t\tList the trace-events of the process\n\ - --force-switch\t\t\tForce a subbuffer switch\n\ -\ -"); + fprintf(stderr, "Usage: %s COMMAND [ARGS]...\n", process_name); + fprintf(stderr, + "Control tracing within a process that supports UST,\n" + " the Userspace Tracing libary\n" + "Options:\n" + " -h[], --help[=] " + "help, for a command if provided\n" + " -l, --list " + "short list of commands\n" + " -e, --extended-list " + "extented list of commands with help\n" + "Commands:\n"); + list_cli_cmds(CLI_DESCRIPTIVE_LIST); } -int parse_opts_long(int argc, char **argv, struct ust_opts *opts) +struct option options[] = { - int c; + {"help", 2, NULL, 'h'}, + {"list", 0, NULL, 'l'}, + {"extended-list", 0, NULL, 'e'}, + {NULL, 0, NULL, 0}, +}; - opts->pids = NULL; - opts->regex = NULL; +int main(int argc, char *argv[]) +{ + struct cli_cmd *cli_cmd; + int opt; - while (1) { - int option_index = 0; - static struct option long_options[] = { - { "create-trace", 0, 0, CREATE_TRACE }, - { "alloc-trace", 0, 0, ALLOC_TRACE }, - { "start-trace", 0, 0, START_TRACE }, - { "stop-trace", 0, 0, STOP_TRACE }, - { "destroy-trace", 0, 0, DESTROY_TRACE }, - { "list-markers", 0, 0, LIST_MARKERS }, - { "list-trace-events", 0, 0, LIST_TRACE_EVENTS}, - { "enable-marker", 1, 0, ENABLE_MARKER }, - { "disable-marker", 1, 0, DISABLE_MARKER }, - { "help", 0, 0, 'h' }, - { "online-pids", 0, 0, GET_ONLINE_PIDS }, - { "set-subbuf-size", 1, 0, SET_SUBBUF_SIZE }, - { "set-subbuf-num", 1, 0, SET_SUBBUF_NUM }, - { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE }, - { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM }, - { "get-sock-path", 0, 0, GET_SOCK_PATH }, - { "set-sock-path", 1, 0, SET_SOCK_PATH }, - { "force-switch", 0, 0, FORCE_SWITCH }, - { 0, 0, 0, 0 } - }; + if(argc <= 1) { + fprintf(stderr, "No operation specified.\n"); + usage(argv[0]); + exit(EXIT_FAILURE); + } - c = getopt_long(argc, argv, "h", long_options, &option_index); - if (c == -1) + while ((opt = getopt_long(argc, argv, "+h::le", + options, NULL)) != -1) { + switch (opt) { + case 'h': + if (!optarg) { + usage(argv[0]); + } else { + if (cli_print_help(optarg)) { + fprintf(stderr, "No such command %s\n", + optarg); + } + } + exit(EXIT_FAILURE); break; - - if(c >= 1000) - opts->cmd = c; - - switch (c) { - case 0: - printf("option %s", long_options[option_index].name); - if (optarg) - printf(" with arg %s", optarg); - printf("\n"); + case 'l': + list_cli_cmds(CLI_SIMPLE_LIST); + exit(EXIT_FAILURE); break; - - case ENABLE_MARKER: - case DISABLE_MARKER: - case SET_SUBBUF_SIZE: - case SET_SUBBUF_NUM: - case GET_SUBBUF_SIZE: - case GET_SUBBUF_NUM: - case SET_SOCK_PATH: - opts->regex = strdup(optarg); + case 'e': + list_cli_cmds(CLI_EXTENDED_LIST); + exit(EXIT_FAILURE); + default: + fprintf(stderr, "Unknown option\n"); break; - - case 'h': - usage(); - exit(0); - - case '?': - fprintf(stderr, "Invalid argument\n\n"); - usage(); - exit(1); } } - if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) { - int i; - int pididx=0; - opts->pids = zmalloc((argc-optind+1) * sizeof(pid_t)); - - for(i=optind; ipids[pididx++] = (pid_t) tmp; - } - opts->pids[pididx] = -1; + cli_cmd = find_cli_cmd(argv[optind]); + if (!cli_cmd) { + fprintf(stderr, "No such command %s\n", + argv[optind]); + exit(EXIT_FAILURE); } + cli_dispatch_cmd(cli_cmd, argc - optind, &argv[optind]); + return 0; } -static int scan_ch_marker(const char *channel_marker, char **channel, - char **marker) +static int list_trace_events(int argc, char *argv[]) { - int result; + struct trace_event_status *tes = NULL; + int i; + pid_t pid; - *channel = NULL; - *marker = NULL; + pid = parse_pid(argv[1]); - result = sscanf(channel_marker, "%a[^/]/%as", channel, marker); - if (result != 2) { - if (errno) { - PERROR("Failed to read channel and marker names"); - } else { - ERR("Failed to parse marker and channel names"); - } - if (*channel) { - free(*channel); - } - if (*marker) { - free(*marker); - } + if (ustcmd_get_tes(&tes, pid)) { + ERR("error while trying to list " + "trace_events for PID %u\n", + pid); return -1; - } else { - return 0; } + i = 0; + for (i = 0; tes[i].name; i++) { + printf("{PID: %u, trace_event: %s}\n", + pid, + tes[i].name); + } + ustcmd_free_tes(tes); + + return 0; } -static int scan_ch_and_num(const char *ch_num, char **channel, unsigned int *num) +static int set_sock_path(int argc, char *argv[]) { - int result; + pid_t pid; - *channel = NULL; + pid = parse_pid(argv[1]); - result = sscanf(ch_num, "%a[^/]/%u", channel, num); - if (result != 2) { - if (errno) { - PERROR("Failed to parse channel and number"); - } else { - ERR("Failed to parse channel and number"); - } - if (*channel) { - free(*channel); - } + if (ustcmd_set_sock_path(argv[2], pid)) { + ERR("error while trying to set sock path for PID %u\n", pid); return -1; } -} -char *trace = "auto"; + return 0; +} -int main(int argc, char *argv[]) +static int get_sock_path(int argc, char *argv[]) { - pid_t *pidit; - int result; - int retval = EXIT_SUCCESS; - char *tmp; - struct ust_opts opts; + pid_t pid; + char *sock_path; - progname = argv[0]; + pid = parse_pid(argv[1]); - if(argc <= 1) { - fprintf(stderr, "No operation specified.\n"); - usage(); - exit(EXIT_FAILURE); - } - - result = parse_opts_long(argc, argv, &opts); - if(result) { - fprintf(stderr, "\n"); - usage(); - exit(EXIT_FAILURE); - } - - if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) { - fprintf(stderr, "No pid specified.\n"); - usage(); - exit(EXIT_FAILURE); - } - if(opts.cmd == UNKNOWN) { - fprintf(stderr, "No command specified.\n"); - usage(); - exit(EXIT_FAILURE); - } - if (opts.cmd == GET_ONLINE_PIDS) { - pid_t *pp = ustcmd_get_online_pids(); - unsigned int i = 0; - - if (pp) { - while (pp[i] != 0) { - printf("%u\n", (unsigned int) pp[i]); - ++i; - } - free(pp); - } - - exit(EXIT_SUCCESS); - } - - pidit = opts.pids; - struct marker_status *cmsf = NULL; - struct trace_event_status *tes = NULL; - unsigned int i = 0; - - while(*pidit != -1) { - switch (opts.cmd) { - case CREATE_TRACE: - result = ustcmd_create_trace(trace, *pidit); - if (result) { - ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - break; - } - break; - - case START_TRACE: - result = ustcmd_start_trace(trace, *pidit); - if (result) { - ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - break; - } - break; - - case STOP_TRACE: - result = ustcmd_stop_trace(trace, *pidit); - if (result) { - ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - break; - } - break; - - case DESTROY_TRACE: - result = ustcmd_destroy_trace(trace, *pidit); - if (result) { - ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - break; - } - break; - - case LIST_MARKERS: - cmsf = NULL; - if (ustcmd_get_cmsf(&cmsf, *pidit)) { - ERR("error while trying to list markers for PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - break; - } - i = 0; - while (cmsf[i].channel != NULL) { - printf("{PID: %u, channel/marker: %s/%s, " - "state: %u, fmt: %s}\n", - (unsigned int) *pidit, - cmsf[i].channel, - cmsf[i].marker, - cmsf[i].state, - cmsf[i].fs); - ++i; - } - ustcmd_free_cmsf(cmsf); - break; - - case LIST_TRACE_EVENTS: - tes = NULL; - if (ustcmd_get_tes(&tes, *pidit)) { - ERR("error while trying to list " - "trace_events for PID %u\n", - (unsigned int) *pidit); - break; - } - i = 0; - while (tes[i].name != NULL) { - printf("{PID: %u, trace_event: %s}\n", - (unsigned int) *pidit, - tes[i].name); - ++i; - } - ustcmd_free_tes(tes); - - break; - case ENABLE_MARKER: - if (opts.regex) { - char *channel, *marker; - - if (scan_ch_marker(opts.regex, - &channel, &marker)) { - retval = EXIT_FAILURE; - break; - } - if (ustcmd_set_marker_state(trace, channel, marker, 1, *pidit)) { - PERROR("error while trying to enable marker %s with PID %u", - opts.regex, (unsigned int) *pidit); - retval = EXIT_FAILURE; - } - } - - break; - case DISABLE_MARKER: - if (opts.regex) { - char *channel, *marker; - - if (scan_ch_marker(opts.regex, - &channel, &marker)) { - retval = EXIT_FAILURE; - break; - } - if (ustcmd_set_marker_state(trace, channel, marker, 0, *pidit)) { - ERR("error while trying to disable marker %s with PID %u\n", - opts.regex, (unsigned int) *pidit); - retval = EXIT_FAILURE; - } - } - break; - - case SET_SUBBUF_SIZE: - if (opts.regex) { - char *channel; - unsigned int size; - if (scan_ch_and_num(opts.regex, &channel, &size)) { - retval = EXIT_FAILURE; - break; - } - - if (ustcmd_set_subbuf_size(trace, channel, size, *pidit)) { - ERR("error while trying to set the size of subbuffers with PID %u\n", - (unsigned int) *pidit); - retval = EXIT_FAILURE; - } - } - break; - - case SET_SUBBUF_NUM: - if (opts.regex) { - char *channel; - unsigned int num; - if (scan_ch_and_num(opts.regex, &channel, &num)) { - retval = EXIT_FAILURE; - break; - } - - if (num < 2) { - ERR("Subbuffer count should be greater or equal to 2"); - retval = EXIT_FAILURE; - break; - } - if (ustcmd_set_subbuf_num(trace, channel, num, *pidit)) { - ERR("error while trying to set the number of subbuffers with PID %u\n", - (unsigned int) *pidit); - retval = EXIT_FAILURE; - } - } - break; - - case GET_SUBBUF_SIZE: - result = ustcmd_get_subbuf_size(trace, opts.regex, *pidit); - if (result == -1) { - ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - break; - } - - printf("the size of subbufers is %d\n", result); - break; - - case GET_SUBBUF_NUM: - result = ustcmd_get_subbuf_num(trace, opts.regex, *pidit); - if (result == -1) { - ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - break; - } - - printf("the number of subbufers is %d\n", result); - break; - - case ALLOC_TRACE: - result = ustcmd_alloc_trace(trace, *pidit); - if (result) { - ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - } - break; - - case GET_SOCK_PATH: - result = ustcmd_get_sock_path(&tmp, *pidit); - if (result) { - ERR("error while trying to get sock path for PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - break; - } - printf("the socket path is %s\n", tmp); - free(tmp); - break; - - case SET_SOCK_PATH: - result = ustcmd_set_sock_path(opts.regex, *pidit); - if (result) { - ERR("error while trying to set sock path for PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - } - break; - - case FORCE_SWITCH: - result = ustcmd_force_switch(*pidit); - if (result) { - ERR("error while trying to force switch for PID %u\n", (unsigned int) *pidit); - retval = EXIT_FAILURE; - } - break; - - default: - ERR("unknown command\n"); - retval = EXIT_FAILURE; - break; - } - - pidit++; - } - - if (opts.pids != NULL) { - free(opts.pids); - } - if (opts.regex != NULL) { - free(opts.regex); + if (ustcmd_get_sock_path(&sock_path, pid)) { + ERR("error while trying to get sock path for PID %u\n", pid); + return -1; } + printf("The socket path is %s\n", sock_path); + free(sock_path); - return retval; + return 0; } +struct cli_cmd __cli_cmds general_cmds[] = { + { + .name = "list-trace-events", + .description = "List trace-events for a given pid", + .help_text = "list-trace-events \n" + "List the trace-events in a process\n", + .function = list_trace_events, + .desired_args = 1, + .desired_args_op = CLI_EQ, + }, + { + .name = "set-sock-path", + .description = "Set the path to the consumer daemon socket", + .help_text = "set-sock-path \n" + "Set the path to the consumer daemon socket\n", + .function = set_sock_path, + .desired_args = 2, + .desired_args_op = CLI_EQ, + }, + { + .name = "get-sock-path", + .description = "Get the path to the consumer daemon socket", + .help_text = "get-sock-path \n" + "Get the path to the consumer daemon socket\n", + .function = get_sock_path, + .desired_args = 1, + .desired_args_op = CLI_EQ, + }, +}; -- 2.34.1