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