| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 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 _GNU_SOURCE |
| 19 | #include <popt.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <common/sessiond-comm/sessiond-comm.h> |
| 28 | #include <common/mi-lttng.h> |
| 29 | |
| 30 | #include "../command.h" |
| 31 | |
| 32 | |
| 33 | static char *opt_session_name; |
| 34 | static struct mi_writer *writer; |
| 35 | |
| 36 | enum { |
| 37 | OPT_HELP = 1, |
| 38 | OPT_LIST_OPTIONS, |
| 39 | }; |
| 40 | |
| 41 | static struct poptOption long_options[] = { |
| 42 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 43 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 44 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 45 | {0, 0, 0, 0, 0, 0, 0} |
| 46 | }; |
| 47 | |
| 48 | /* |
| 49 | * usage |
| 50 | */ |
| 51 | static void usage(FILE *ofp) |
| 52 | { |
| 53 | fprintf(ofp, "usage: lttng start [NAME] [OPTIONS]\n"); |
| 54 | fprintf(ofp, "\n"); |
| 55 | fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n"); |
| 56 | fprintf(ofp, "get it from the configuration directory (.lttng).\n"); |
| 57 | fprintf(ofp, "\n"); |
| 58 | fprintf(ofp, "Options:\n"); |
| 59 | fprintf(ofp, " -h, --help Show this help\n"); |
| 60 | fprintf(ofp, " --list-options Simple listing of options\n"); |
| 61 | fprintf(ofp, "\n"); |
| 62 | } |
| 63 | |
| 64 | static int mi_print_session(char *session_name, int enabled) |
| 65 | { |
| 66 | int ret; |
| 67 | |
| 68 | /* Open session element */ |
| 69 | ret = mi_lttng_writer_open_element(writer, config_element_session); |
| 70 | if (ret) { |
| 71 | goto end; |
| 72 | } |
| 73 | |
| 74 | /* Print session name element */ |
| 75 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, |
| 76 | session_name); |
| 77 | if (ret) { |
| 78 | goto end; |
| 79 | } |
| 80 | |
| 81 | ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, |
| 82 | enabled); |
| 83 | if (ret) { |
| 84 | goto end; |
| 85 | } |
| 86 | |
| 87 | /* Close session element */ |
| 88 | ret = mi_lttng_writer_close_element(writer); |
| 89 | |
| 90 | end: |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * start_tracing |
| 96 | * |
| 97 | * Start tracing for all trace of the session. |
| 98 | */ |
| 99 | static int start_tracing(void) |
| 100 | { |
| 101 | int ret; |
| 102 | char *session_name; |
| 103 | |
| 104 | if (opt_session_name == NULL) { |
| 105 | session_name = get_session_name(); |
| 106 | if (session_name == NULL) { |
| 107 | ret = CMD_ERROR; |
| 108 | goto error; |
| 109 | } |
| 110 | } else { |
| 111 | session_name = opt_session_name; |
| 112 | } |
| 113 | |
| 114 | DBG("Starting tracing for session %s", session_name); |
| 115 | |
| 116 | ret = lttng_start_tracing(session_name); |
| 117 | if (ret < 0) { |
| 118 | switch (-ret) { |
| 119 | case LTTNG_ERR_TRACE_ALREADY_STARTED: |
| 120 | WARN("Tracing already started for session %s", session_name); |
| 121 | break; |
| 122 | default: |
| 123 | ERR("%s", lttng_strerror(ret)); |
| 124 | break; |
| 125 | } |
| 126 | goto free_name; |
| 127 | } |
| 128 | |
| 129 | ret = CMD_SUCCESS; |
| 130 | |
| 131 | MSG("Tracing started for session %s", session_name); |
| 132 | if (lttng_opt_mi) { |
| 133 | ret = mi_print_session(session_name, 1); |
| 134 | if (ret) { |
| 135 | ret = CMD_ERROR; |
| 136 | goto free_name; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | free_name: |
| 141 | if (opt_session_name == NULL) { |
| 142 | free(session_name); |
| 143 | } |
| 144 | error: |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * cmd_start |
| 150 | * |
| 151 | * The 'start <options>' first level command |
| 152 | */ |
| 153 | int cmd_start(int argc, const char **argv) |
| 154 | { |
| 155 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 156 | static poptContext pc; |
| 157 | |
| 158 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 159 | poptReadDefaultConfig(pc, 0); |
| 160 | |
| 161 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 162 | switch (opt) { |
| 163 | case OPT_HELP: |
| 164 | usage(stdout); |
| 165 | goto end; |
| 166 | case OPT_LIST_OPTIONS: |
| 167 | list_cmd_options(stdout, long_options); |
| 168 | goto end; |
| 169 | default: |
| 170 | usage(stderr); |
| 171 | ret = CMD_UNDEFINED; |
| 172 | goto end; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | opt_session_name = (char*) poptGetArg(pc); |
| 177 | |
| 178 | /* Mi check */ |
| 179 | if (lttng_opt_mi) { |
| 180 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 181 | if (!writer) { |
| 182 | ret = -LTTNG_ERR_NOMEM; |
| 183 | goto end; |
| 184 | } |
| 185 | |
| 186 | /* Open command element */ |
| 187 | ret = mi_lttng_writer_command_open(writer, |
| 188 | mi_lttng_element_command_start); |
| 189 | if (ret) { |
| 190 | ret = CMD_ERROR; |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | /* Open output element */ |
| 195 | ret = mi_lttng_writer_open_element(writer, |
| 196 | mi_lttng_element_command_output); |
| 197 | if (ret) { |
| 198 | ret = CMD_ERROR; |
| 199 | goto end; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Open sessions element |
| 204 | * For validation purpose |
| 205 | */ |
| 206 | ret = mi_lttng_writer_open_element(writer, |
| 207 | config_element_sessions); |
| 208 | if (ret) { |
| 209 | ret = CMD_ERROR; |
| 210 | goto end; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | command_ret = start_tracing(); |
| 215 | if (command_ret) { |
| 216 | success = 0; |
| 217 | } |
| 218 | |
| 219 | /* Mi closing */ |
| 220 | if (lttng_opt_mi) { |
| 221 | /* Close sessions and output element */ |
| 222 | ret = mi_lttng_close_multi_element(writer, 2); |
| 223 | if (ret) { |
| 224 | ret = CMD_ERROR; |
| 225 | goto end; |
| 226 | } |
| 227 | |
| 228 | /* Success ? */ |
| 229 | ret = mi_lttng_writer_write_element_bool(writer, |
| 230 | mi_lttng_element_command_success, success); |
| 231 | if (ret) { |
| 232 | ret = CMD_ERROR; |
| 233 | goto end; |
| 234 | } |
| 235 | |
| 236 | /* Command element close */ |
| 237 | ret = mi_lttng_writer_command_close(writer); |
| 238 | if (ret) { |
| 239 | ret = CMD_ERROR; |
| 240 | goto end; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | end: |
| 245 | /* Mi clean-up */ |
| 246 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 247 | /* Preserve original error code */ |
| 248 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 249 | } |
| 250 | |
| 251 | /* Overwrite ret if an error occurred with start_tracing */ |
| 252 | ret = command_ret ? command_ret : ret; |
| 253 | poptFreeContext(pc); |
| 254 | return ret; |
| 255 | } |