| 1 | /* |
| 2 | * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _LGPL_SOURCE |
| 19 | #include <inttypes.h> |
| 20 | #include <popt.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <assert.h> |
| 25 | |
| 26 | #include <common/mi-lttng.h> |
| 27 | |
| 28 | #include "../command.h" |
| 29 | #include <lttng/save.h> |
| 30 | |
| 31 | static char *opt_output_path; |
| 32 | static bool opt_force; |
| 33 | static bool opt_save_all; |
| 34 | static bool opt_omit_name; |
| 35 | static bool opt_omit_output; |
| 36 | static struct mi_writer *writer; |
| 37 | |
| 38 | enum { |
| 39 | OPT_HELP = 1, |
| 40 | OPT_ALL, |
| 41 | OPT_FORCE, |
| 42 | OPT_LIST_OPTIONS, |
| 43 | OPT_OMIT_NAME, |
| 44 | OPT_OMIT_OUTPUT, |
| 45 | }; |
| 46 | |
| 47 | static struct poptOption save_opts[] = { |
| 48 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 49 | {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL}, |
| 50 | {"all", 'a', POPT_ARG_NONE, NULL, OPT_ALL, NULL, NULL}, |
| 51 | {"output-path", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL}, |
| 52 | {"force", 'f', POPT_ARG_NONE, NULL, OPT_FORCE, NULL, NULL}, |
| 53 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 54 | {"omit-name", 0, POPT_ARG_NONE, NULL, OPT_OMIT_NAME, NULL, NULL}, |
| 55 | {"omit-output", 0, POPT_ARG_NONE, NULL, OPT_OMIT_OUTPUT, NULL, NULL}, |
| 56 | {0, 0, 0, 0, 0, 0, 0} |
| 57 | }; |
| 58 | |
| 59 | static int mi_partial_session(const char *session_name) |
| 60 | { |
| 61 | int ret; |
| 62 | assert(writer); |
| 63 | assert(session_name); |
| 64 | |
| 65 | /* Open session element */ |
| 66 | ret = mi_lttng_writer_open_element(writer, config_element_session); |
| 67 | if (ret) { |
| 68 | goto end; |
| 69 | } |
| 70 | |
| 71 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, |
| 72 | session_name); |
| 73 | if (ret) { |
| 74 | goto end; |
| 75 | } |
| 76 | |
| 77 | /* Closing session element */ |
| 78 | ret = mi_lttng_writer_close_element(writer); |
| 79 | end: |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Mi print of save command |
| 85 | */ |
| 86 | static int mi_save_print(const char *session_name) |
| 87 | { |
| 88 | int ret; |
| 89 | assert(writer); |
| 90 | |
| 91 | if (opt_save_all) { |
| 92 | /* We use a wildcard to represent all sessions */ |
| 93 | session_name = "*"; |
| 94 | } |
| 95 | |
| 96 | /* Print save element */ |
| 97 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_save); |
| 98 | if (ret) { |
| 99 | goto end; |
| 100 | } |
| 101 | |
| 102 | /* Print session element */ |
| 103 | ret = mi_partial_session(session_name); |
| 104 | if (ret) { |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | /* Path element */ |
| 109 | if (opt_output_path) { |
| 110 | ret = mi_lttng_writer_write_element_string(writer, config_element_path, |
| 111 | opt_output_path); |
| 112 | if (ret) { |
| 113 | goto end; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* Omit session name. */ |
| 118 | if (opt_omit_name) { |
| 119 | /* Only print if true; assume default value otherwise. */ |
| 120 | ret = mi_lttng_writer_write_element_bool(writer, |
| 121 | config_element_omit_name, opt_omit_name); |
| 122 | if (ret) { |
| 123 | goto end; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* Omit session output. */ |
| 128 | if (opt_omit_output) { |
| 129 | /* Only print if true; assume default value otherwise. */ |
| 130 | ret = mi_lttng_writer_write_element_bool(writer, |
| 131 | config_element_omit_output, opt_omit_output); |
| 132 | if (ret) { |
| 133 | goto end; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* Close save element */ |
| 138 | ret = mi_lttng_writer_close_element(writer); |
| 139 | end: |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * The 'save <options>' first level command |
| 145 | */ |
| 146 | int cmd_save(int argc, const char **argv) |
| 147 | { |
| 148 | int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success; |
| 149 | int opt; |
| 150 | const char *session_name = NULL; |
| 151 | poptContext pc; |
| 152 | struct lttng_save_session_attr *attr; |
| 153 | |
| 154 | pc = poptGetContext(NULL, argc, argv, save_opts, 0); |
| 155 | poptReadDefaultConfig(pc, 0); |
| 156 | |
| 157 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 158 | switch (opt) { |
| 159 | case OPT_HELP: |
| 160 | SHOW_HELP(); |
| 161 | goto end; |
| 162 | case OPT_ALL: |
| 163 | opt_save_all = true; |
| 164 | break; |
| 165 | case OPT_FORCE: |
| 166 | opt_force = true; |
| 167 | break; |
| 168 | case OPT_LIST_OPTIONS: |
| 169 | list_cmd_options(stdout, save_opts); |
| 170 | goto end; |
| 171 | case OPT_OMIT_NAME: |
| 172 | opt_omit_name = true; |
| 173 | break; |
| 174 | case OPT_OMIT_OUTPUT: |
| 175 | opt_omit_output = true; |
| 176 | break; |
| 177 | default: |
| 178 | ret = CMD_UNDEFINED; |
| 179 | goto end; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (!opt_save_all) { |
| 184 | session_name = poptGetArg(pc); |
| 185 | if (session_name) { |
| 186 | DBG2("Session name: %s", session_name); |
| 187 | } else { |
| 188 | /* default to opt_save_all */ |
| 189 | opt_save_all = true; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | attr = lttng_save_session_attr_create(); |
| 194 | if (!attr) { |
| 195 | ret = CMD_FATAL; |
| 196 | goto end_destroy; |
| 197 | } |
| 198 | |
| 199 | if (lttng_save_session_attr_set_session_name(attr, session_name)) { |
| 200 | ret = CMD_ERROR; |
| 201 | goto end_destroy; |
| 202 | } |
| 203 | |
| 204 | if (lttng_save_session_attr_set_overwrite(attr, opt_force)) { |
| 205 | ret = CMD_ERROR; |
| 206 | goto end_destroy; |
| 207 | } |
| 208 | |
| 209 | if (lttng_save_session_attr_set_output_url(attr, opt_output_path)) { |
| 210 | ret = CMD_ERROR; |
| 211 | goto end_destroy; |
| 212 | } |
| 213 | |
| 214 | if (opt_omit_name) { |
| 215 | if (lttng_save_session_attr_set_omit_name(attr, true)) { |
| 216 | ret = CMD_ERROR; |
| 217 | goto end_destroy; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (opt_omit_output) { |
| 222 | if (lttng_save_session_attr_set_omit_output(attr, true)) { |
| 223 | ret = CMD_ERROR; |
| 224 | goto end_destroy; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* Mi check */ |
| 229 | if (lttng_opt_mi) { |
| 230 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 231 | if (!writer) { |
| 232 | ret = -LTTNG_ERR_NOMEM; |
| 233 | goto end_destroy; |
| 234 | } |
| 235 | |
| 236 | /* Open command element */ |
| 237 | ret = mi_lttng_writer_command_open(writer, |
| 238 | mi_lttng_element_command_save); |
| 239 | if (ret) { |
| 240 | ret = CMD_ERROR; |
| 241 | goto end_destroy; |
| 242 | } |
| 243 | |
| 244 | /* Open output element */ |
| 245 | ret = mi_lttng_writer_open_element(writer, |
| 246 | mi_lttng_element_command_output); |
| 247 | if (ret) { |
| 248 | ret = CMD_ERROR; |
| 249 | goto end_destroy; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | command_ret = lttng_save_session(attr); |
| 254 | if (command_ret < 0) { |
| 255 | ERR("%s", lttng_strerror(command_ret)); |
| 256 | success = 0; |
| 257 | } else { |
| 258 | /* Inform the user of what just happened on success. */ |
| 259 | if (session_name && opt_output_path) { |
| 260 | MSG("Session %s saved successfully in %s.", session_name, |
| 261 | opt_output_path); |
| 262 | } else if (session_name && !opt_output_path) { |
| 263 | MSG("Session %s saved successfully.", session_name); |
| 264 | } else if (!session_name && opt_output_path) { |
| 265 | MSG("All sessions have been saved successfully in %s.", |
| 266 | opt_output_path); |
| 267 | } else { |
| 268 | MSG("All sessions have been saved successfully."); |
| 269 | } |
| 270 | success = 1; |
| 271 | } |
| 272 | |
| 273 | /* Mi Printing and closing */ |
| 274 | if (lttng_opt_mi) { |
| 275 | /* Mi print */ |
| 276 | ret = mi_save_print(session_name); |
| 277 | if (ret) { |
| 278 | ret = CMD_ERROR; |
| 279 | goto end_destroy; |
| 280 | } |
| 281 | |
| 282 | /* Close output element */ |
| 283 | ret = mi_lttng_writer_close_element(writer); |
| 284 | if (ret) { |
| 285 | ret = CMD_ERROR; |
| 286 | goto end_destroy; |
| 287 | } |
| 288 | |
| 289 | /* Success ? */ |
| 290 | ret = mi_lttng_writer_write_element_bool(writer, |
| 291 | mi_lttng_element_command_success, success); |
| 292 | if (ret) { |
| 293 | ret = CMD_ERROR; |
| 294 | goto end_destroy; |
| 295 | } |
| 296 | |
| 297 | /* Command element close */ |
| 298 | ret = mi_lttng_writer_command_close(writer); |
| 299 | if (ret) { |
| 300 | ret = CMD_ERROR; |
| 301 | goto end_destroy; |
| 302 | } |
| 303 | } |
| 304 | end_destroy: |
| 305 | lttng_save_session_attr_destroy(attr); |
| 306 | end: |
| 307 | /* Mi clean-up */ |
| 308 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 309 | /* Preserve original error code */ |
| 310 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 311 | } |
| 312 | |
| 313 | /* Overwrite ret if command failed */ |
| 314 | ret = command_ret ? -command_ret : ret; |
| 315 | |
| 316 | poptFreeContext(pc); |
| 317 | return ret; |
| 318 | } |