X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fsnapshot.c;h=b6b9faa59be0e3412c18a0b56d7a284819b525b9;hp=e9c60fbbbba42436bba04dfc891168af96cb7627;hb=e7ab49a85e4e5c5749949191bdeefa21a9aef9b1;hpb=8e610b4279bab6b20fd1b92e418f4d28d5c28f5c diff --git a/src/bin/lttng/commands/snapshot.c b/src/bin/lttng/commands/snapshot.c index e9c60fbbb..b6b9faa59 100644 --- a/src/bin/lttng/commands/snapshot.c +++ b/src/bin/lttng/commands/snapshot.c @@ -48,6 +48,12 @@ static int cmd_record(int argc, const char **argv); static const char *indent4 = " "; +#ifdef LTTNG_EMBED_HELP +static const char help_msg[] = +#include +; +#endif + enum { OPT_HELP = 1, OPT_LIST_OPTIONS, @@ -173,11 +179,18 @@ static int list_output(void) } while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) { - MSG("%s[%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", indent4, - lttng_snapshot_output_get_id(s_iter), - lttng_snapshot_output_get_name(s_iter), - lttng_snapshot_output_get_ctrl_url(s_iter), - lttng_snapshot_output_get_maxsize(s_iter)); + if (lttng_snapshot_output_get_maxsize(s_iter)) { + MSG("%s[%" PRIu32 "] %s: %s (max size: %" PRIu64 " bytes)", indent4, + lttng_snapshot_output_get_id(s_iter), + lttng_snapshot_output_get_name(s_iter), + lttng_snapshot_output_get_ctrl_url(s_iter), + lttng_snapshot_output_get_maxsize(s_iter)); + } else { + MSG("%s[%" PRIu32 "] %s: %s", indent4, + lttng_snapshot_output_get_id(s_iter), + lttng_snapshot_output_get_name(s_iter), + lttng_snapshot_output_get_ctrl_url(s_iter)); + } output_seen = 1; if (lttng_opt_mi) { ret = mi_lttng_snapshot_list_output(writer, s_iter); @@ -213,50 +226,6 @@ error: return ret; } -/* - * Delete output by ID (machine interface version). - */ -static int mi_del_output(uint32_t id, const char *name) -{ - int ret; - struct lttng_snapshot_output *output = NULL; - - assert(writer); - - output = lttng_snapshot_output_create(); - if (!output) { - ret = CMD_FATAL; - goto error; - } - - if (name) { - ret = lttng_snapshot_output_set_name(name, output); - } else if (id != UINT32_MAX) { - ret = lttng_snapshot_output_set_id(id, output); - } else { - ret = CMD_ERROR; - goto error; - } - if (ret < 0) { - ret = CMD_FATAL; - goto error; - } - - ret = lttng_snapshot_del_output(current_session_name, output); - if (ret < 0) { - goto error; - } - - ret = mi_lttng_snapshot_del_output(writer, id, name, current_session_name); - if (ret) { - ret = CMD_ERROR; - } - -error: - lttng_snapshot_output_destroy(output); - return ret; -} - /* * Delete output by ID. */ @@ -297,53 +266,12 @@ static int del_output(uint32_t id, const char *name) name, current_session_name); } -error: - lttng_snapshot_output_destroy(output); - return ret; -} - -/* - * Add output from the user URL (machine interface). - */ -static int mi_add_output(const char *url) -{ - int ret; - struct lttng_snapshot_output *output = NULL; - char name[NAME_MAX]; - const char *n_ptr; - - if (!url && (!opt_data_url || !opt_ctrl_url)) { - ret = CMD_ERROR; - goto error; - } - - output = create_output_from_args(url); - if (!output) { - ret = CMD_FATAL; - goto error; - } - - /* This call, if successful, populates the id of the output object. */ - ret = lttng_snapshot_add_output(current_session_name, output); - if (ret < 0) { - goto error; - } - - n_ptr = lttng_snapshot_output_get_name(output); - if (*n_ptr == '\0') { - int pret; - pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32, - lttng_snapshot_output_get_id(output)); - if (pret < 0) { - PERROR("snprintf add output name"); + if (lttng_opt_mi) { + ret = mi_lttng_snapshot_del_output(writer, id, name, + current_session_name); + if (ret) { + ret = CMD_ERROR; } - n_ptr = name; - } - - ret = mi_lttng_snapshot_add_output(writer, current_session_name, n_ptr, - output); - if (ret) { - ret = CMD_ERROR; } error: @@ -391,10 +319,23 @@ static int add_output(const char *url) MSG("Snapshot output successfully added for session %s", current_session_name); - MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", - lttng_snapshot_output_get_id(output), n_ptr, - lttng_snapshot_output_get_ctrl_url(output), - lttng_snapshot_output_get_maxsize(output)); + if (opt_max_size) { + MSG(" [%" PRIu32 "] %s: %s (max size: %" PRIu64 " bytes)", + lttng_snapshot_output_get_id(output), n_ptr, + lttng_snapshot_output_get_ctrl_url(output), + lttng_snapshot_output_get_maxsize(output)); + } else { + MSG(" [%" PRIu32 "] %s: %s", + lttng_snapshot_output_get_id(output), n_ptr, + lttng_snapshot_output_get_ctrl_url(output)); + } + if (lttng_opt_mi) { + ret = mi_lttng_snapshot_add_output(writer, current_session_name, + n_ptr, output); + if (ret) { + ret = CMD_ERROR; + } + } error: lttng_snapshot_output_destroy(output); return ret; @@ -409,11 +350,7 @@ static int cmd_add_output(int argc, const char **argv) goto end; } - if (lttng_opt_mi) { - ret = mi_add_output(argv[1]); - } else { - ret = add_output(argv[1]); - } + ret = add_output(argv[1]); end: return ret; @@ -432,18 +369,10 @@ static int cmd_del_output(int argc, const char **argv) errno = 0; id = strtol(argv[1], &name, 10); - if (id == 0 && errno == 0) { - if (lttng_opt_mi) { - ret = mi_del_output(UINT32_MAX, name); - } else { - ret = del_output(UINT32_MAX, name); - } + if (id == 0 && (errno == 0 || errno == EINVAL)) { + ret = del_output(UINT32_MAX, name); } else if (errno == 0 && *name == '\0') { - if (lttng_opt_mi) { - ret = mi_del_output(id, NULL); - } else { - ret = del_output(id, NULL); - } + ret = del_output(id, NULL); } else { ERR("Argument %s not recognized", argv[1]); ret = -1; @@ -463,37 +392,6 @@ static int cmd_list_output(int argc, const char **argv) return ret; } -/* - * Do a snapshot record with the URL if one is given (machine interface). - */ -static int mi_record(const char *url) -{ - int ret; - struct lttng_snapshot_output *output = NULL; - - output = create_output_from_args(url); - if (!output) { - ret = CMD_FATAL; - goto error; - } - - ret = lttng_snapshot_record(current_session_name, output, 0); - if (ret < 0) { - ret = CMD_ERROR; - goto error; - } - - ret = mi_lttng_snapshot_record(writer, current_session_name, url, - opt_ctrl_url, opt_data_url); - if (ret) { - ret = CMD_ERROR; - } - -error: - lttng_snapshot_output_destroy(output); - return ret; -} - /* * Do a snapshot record with the URL if one is given. */ @@ -525,6 +423,14 @@ static int record(const char *url) opt_data_url); } + if (lttng_opt_mi) { + ret = mi_lttng_snapshot_record(writer, current_session_name, url, + opt_ctrl_url, opt_data_url); + if (ret) { + ret = CMD_ERROR; + } + } + error: lttng_snapshot_output_destroy(output); return ret; @@ -535,73 +441,99 @@ static int cmd_record(int argc, const char **argv) int ret; if (argc == 2) { - /* With a given URL */ - if (lttng_opt_mi) { - ret = mi_record(argv[1]); - } else { - ret = record(argv[1]); - } + ret = record(argv[1]); } else { - if (lttng_opt_mi) { - ret = mi_record(NULL); - } else { - ret = record(NULL); - } + ret = record(NULL); } return ret; } -static int handle_command(const char **argv) +static enum cmd_error_code handle_command(const char **argv) { - int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS; + int mi_ret, i = 0, argc; + enum cmd_error_code cmd_ret; struct cmd_struct *cmd; - if (argv == NULL || (!opt_ctrl_url && opt_data_url) || + if (!argv) { + ERR("No action specified for snapshot command."); + cmd_ret = CMD_ERROR; + goto end; + } + + if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) { - command_ret = CMD_ERROR; + ERR("URLs must be specified for both data and control"); + cmd_ret = CMD_ERROR; goto end; } argc = count_arguments(argv); + /* popt should have passed NULL if no arguments are present. */ + assert(argc > 0); cmd = &actions[i]; while (cmd->func != NULL) { /* Find command */ if (strcmp(argv[0], cmd->name) == 0) { + int result; + if (lttng_opt_mi) { /* Action element */ - ret = mi_lttng_writer_open_element(writer, + mi_ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_action); - if (ret) { - ret = CMD_ERROR; + if (mi_ret) { + cmd_ret = CMD_ERROR; goto end; } /* Name of the action */ - ret = mi_lttng_writer_write_element_string(writer, + mi_ret = mi_lttng_writer_write_element_string(writer, config_element_name, argv[0]); - if (ret) { - ret = CMD_ERROR; + if (mi_ret) { + cmd_ret = CMD_ERROR; goto end; } /* Open output element */ - ret = mi_lttng_writer_open_element(writer, + mi_ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); - if (ret) { - ret = CMD_ERROR; + if (mi_ret) { + cmd_ret = CMD_ERROR; goto end; } } - command_ret = cmd->func(argc, argv); + result = cmd->func(argc, argv); + if (result) { + switch (-result) { + case LTTNG_ERR_SNAPSHOT_NODATA: + WARN("%s", lttng_strerror(result)); + + /* A warning is fine since the user has no control on + * whether or not applications (or the kernel) have + * produced any event between the start of the tracing + * session and the recording of the snapshot. MI wise + * the command is not a success since nothing was + * recorded. + */ + cmd_ret = CMD_SUCCESS; + break; + default: + ERR("%s", lttng_strerror(result)); + cmd_ret = CMD_ERROR; + break; + } + } else { + cmd_ret = CMD_SUCCESS; + } + if (lttng_opt_mi) { /* Close output and action element */ - ret = mi_lttng_close_multi_element(writer, 2); - if (ret) { - ret = CMD_ERROR; + mi_ret = mi_lttng_close_multi_element(writer, 2); + if (mi_ret) { + cmd_ret = CMD_ERROR; goto end; } } @@ -611,19 +543,19 @@ static int handle_command(const char **argv) cmd = &actions[i]; } - ret = CMD_UNDEFINED; + cmd_ret = CMD_UNDEFINED; end: - /* Overwrite ret if an error occurred in cmd->func() */ - ret = command_ret ? command_ret : ret; - return ret; + return cmd_ret; } /* * The 'snapshot ' first level command */ int cmd_snapshot(int argc, const char **argv) { - int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; + int opt; + int mi_ret; + enum cmd_error_code cmd_ret = CMD_SUCCESS; char *session_name = NULL; static poptContext pc; @@ -634,23 +566,23 @@ int cmd_snapshot(int argc, const char **argv) if (lttng_opt_mi) { writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); if (!writer) { - ret = -LTTNG_ERR_NOMEM; + cmd_ret = CMD_ERROR; goto end; } /* Open command element */ - ret = mi_lttng_writer_command_open(writer, + mi_ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_snapshot); - if (ret) { - ret = CMD_ERROR; + if (mi_ret) { + cmd_ret = CMD_ERROR; goto end; } /* Open output element */ - ret = mi_lttng_writer_open_element(writer, + mi_ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); - if (ret) { - ret = CMD_ERROR; + if (mi_ret) { + cmd_ret = CMD_ERROR; goto end; } } @@ -658,8 +590,14 @@ int cmd_snapshot(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: + { + int ret; + + /* SHOW_HELP assigns to ret. */ SHOW_HELP(); + cmd_ret = ret; goto end; + } case OPT_LIST_OPTIONS: list_cmd_options(stdout, snapshot_opts); goto end; @@ -673,7 +611,7 @@ int cmd_snapshot(int argc, const char **argv) if (utils_parse_size_suffix((char *) opt, &val) < 0) { ERR("Unable to handle max-size value %s", opt); - ret = CMD_ERROR; + cmd_ret = CMD_ERROR; goto end; } @@ -682,7 +620,7 @@ int cmd_snapshot(int argc, const char **argv) break; } default: - ret = CMD_UNDEFINED; + cmd_ret = CMD_UNDEFINED; goto end; } } @@ -690,7 +628,7 @@ int cmd_snapshot(int argc, const char **argv) if (!opt_session_name) { session_name = get_session_name(); if (session_name == NULL) { - ret = CMD_ERROR; + cmd_ret = CMD_ERROR; goto end; } current_session_name = session_name; @@ -698,51 +636,29 @@ int cmd_snapshot(int argc, const char **argv) current_session_name = opt_session_name; } - command_ret = handle_command(poptGetArgs(pc)); - if (command_ret) { - switch (-command_ret) { - case LTTNG_ERR_EPERM: - ERR("The session needs to be set in no output mode (--no-output)"); - break; - case LTTNG_ERR_SNAPSHOT_NODATA: - WARN("%s", lttng_strerror(command_ret)); - - /* A warning is fine since the user has no control on - * whether or not applications (or the kernel) have - * produced any event between the start of the tracing - * session and the recording of the snapshot. MI wise - * the command is not a success since nothing was - * recorded. - */ - command_ret = 0; - break; - default: - ERR("%s", lttng_strerror(command_ret)); - break; - } - success = 0; - } + cmd_ret = handle_command(poptGetArgs(pc)); if (lttng_opt_mi) { /* Close output element */ - ret = mi_lttng_writer_close_element(writer); - if (ret) { - ret = CMD_ERROR; + mi_ret = mi_lttng_writer_close_element(writer); + if (mi_ret) { + cmd_ret = CMD_ERROR; goto end; } /* Success ? */ - ret = mi_lttng_writer_write_element_bool(writer, - mi_lttng_element_command_success, success); - if (ret) { - ret = CMD_ERROR; + mi_ret = mi_lttng_writer_write_element_bool(writer, + mi_lttng_element_command_success, + cmd_ret == CMD_SUCCESS); + if (mi_ret) { + cmd_ret = CMD_ERROR; goto end; } /* Command element close */ - ret = mi_lttng_writer_command_close(writer); - if (ret) { - ret = CMD_ERROR; + mi_ret = mi_lttng_writer_command_close(writer); + if (mi_ret) { + cmd_ret = CMD_ERROR; goto end; } } @@ -750,16 +666,13 @@ int cmd_snapshot(int argc, const char **argv) end: /* Mi clean-up */ if (writer && mi_lttng_writer_destroy(writer)) { - /* Preserve original error code */ - ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; + cmd_ret = CMD_ERROR; } if (!opt_session_name) { free(session_name); } - /* Overwrite ret if an error occured during handle_command */ - ret = command_ret ? command_ret : ret; poptFreeContext(pc); - return ret; + return cmd_ret; }