| 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 | #include <assert.h> |
| 27 | |
| 28 | #include <common/mi-lttng.h> |
| 29 | |
| 30 | #include "../command.h" |
| 31 | |
| 32 | static char *opt_channels; |
| 33 | static int opt_kernel; |
| 34 | static char *opt_session_name; |
| 35 | static int opt_userspace; |
| 36 | #if 0 |
| 37 | /* Not implemented yet */ |
| 38 | static char *opt_cmd_name; |
| 39 | static pid_t opt_pid; |
| 40 | #endif |
| 41 | |
| 42 | enum { |
| 43 | OPT_HELP = 1, |
| 44 | OPT_USERSPACE, |
| 45 | OPT_LIST_OPTIONS, |
| 46 | }; |
| 47 | |
| 48 | static struct lttng_handle *handle; |
| 49 | static struct mi_writer *writer; |
| 50 | |
| 51 | static struct poptOption long_options[] = { |
| 52 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 53 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 54 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 55 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 56 | #if 0 |
| 57 | /* Not implemented yet */ |
| 58 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0}, |
| 59 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
| 60 | #else |
| 61 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 62 | #endif |
| 63 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 64 | {0, 0, 0, 0, 0, 0, 0} |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * usage |
| 69 | */ |
| 70 | static void usage(FILE *ofp) |
| 71 | { |
| 72 | fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] (-k | -u) [OPTIONS]\n"); |
| 73 | fprintf(ofp, "\n"); |
| 74 | fprintf(ofp, "Options:\n"); |
| 75 | fprintf(ofp, " -h, --help Show this help\n"); |
| 76 | fprintf(ofp, " --list-options Simple listing of options\n"); |
| 77 | fprintf(ofp, " -s, --session NAME Apply to session name\n"); |
| 78 | fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n"); |
| 79 | fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n"); |
| 80 | fprintf(ofp, "\n"); |
| 81 | } |
| 82 | |
| 83 | static int mi_partial_channel_print(char *channel_name, unsigned int enabled, |
| 84 | int success) |
| 85 | { |
| 86 | int ret; |
| 87 | |
| 88 | assert(writer); |
| 89 | assert(channel_name); |
| 90 | |
| 91 | /* Open channel element */ |
| 92 | ret = mi_lttng_writer_open_element(writer, config_element_channel); |
| 93 | if (ret) { |
| 94 | goto end; |
| 95 | } |
| 96 | |
| 97 | /* Name */ |
| 98 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, |
| 99 | channel_name); |
| 100 | if (ret) { |
| 101 | goto end; |
| 102 | } |
| 103 | |
| 104 | /* Enabled ? */ |
| 105 | ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, |
| 106 | enabled); |
| 107 | if (ret) { |
| 108 | goto end; |
| 109 | } |
| 110 | |
| 111 | /* Success ? */ |
| 112 | ret = mi_lttng_writer_write_element_bool(writer, |
| 113 | mi_lttng_element_success, success); |
| 114 | if (ret) { |
| 115 | goto end; |
| 116 | } |
| 117 | |
| 118 | /* Closing channel element */ |
| 119 | ret = mi_lttng_writer_close_element(writer); |
| 120 | |
| 121 | end: |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Disabling channel using the lttng API. |
| 127 | */ |
| 128 | static int disable_channels(char *session_name) |
| 129 | { |
| 130 | int ret = CMD_SUCCESS, warn = 0, success; |
| 131 | |
| 132 | /* Normal case for disable channed is enabled = 0 */ |
| 133 | unsigned int enabled = 0; |
| 134 | char *channel_name; |
| 135 | struct lttng_domain dom; |
| 136 | |
| 137 | memset(&dom, 0, sizeof(dom)); |
| 138 | |
| 139 | /* Create lttng domain */ |
| 140 | if (opt_kernel) { |
| 141 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 142 | } else if (opt_userspace) { |
| 143 | dom.type = LTTNG_DOMAIN_UST; |
| 144 | } else { |
| 145 | print_missing_domain(); |
| 146 | ret = CMD_ERROR; |
| 147 | goto error; |
| 148 | } |
| 149 | |
| 150 | handle = lttng_create_handle(session_name, &dom); |
| 151 | if (handle == NULL) { |
| 152 | ret = -1; |
| 153 | goto error; |
| 154 | } |
| 155 | |
| 156 | /* Prepare MI */ |
| 157 | if (lttng_opt_mi) { |
| 158 | /* open a channels element */ |
| 159 | ret = mi_lttng_writer_open_element(writer, config_element_channels); |
| 160 | if (ret) { |
| 161 | ret = CMD_ERROR; |
| 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | /* Strip channel list */ |
| 168 | channel_name = strtok(opt_channels, ","); |
| 169 | while (channel_name != NULL) { |
| 170 | DBG("Disabling channel %s", channel_name); |
| 171 | |
| 172 | ret = lttng_disable_channel(handle, channel_name); |
| 173 | if (ret < 0) { |
| 174 | ERR("Channel %s: %s (session %s)", channel_name, |
| 175 | lttng_strerror(ret), session_name); |
| 176 | warn = 1; |
| 177 | |
| 178 | /* |
| 179 | * Mi: |
| 180 | * We assume that if an error occurred the channel is still active. |
| 181 | * This might not be the case but is a good assumption. |
| 182 | * The client should look at the stderr stream |
| 183 | * for more informations. |
| 184 | */ |
| 185 | enabled = 1; |
| 186 | success = 0; |
| 187 | |
| 188 | } else { |
| 189 | MSG("%s channel %s disabled for session %s", |
| 190 | get_domain_str(dom.type), channel_name, session_name); |
| 191 | enabled = 0; |
| 192 | success = 1; |
| 193 | } |
| 194 | |
| 195 | /* Print the channel */ |
| 196 | if (lttng_opt_mi) { |
| 197 | ret = mi_partial_channel_print(channel_name, enabled, success); |
| 198 | if (ret) { |
| 199 | ret = CMD_ERROR; |
| 200 | goto error; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* Next channel */ |
| 205 | channel_name = strtok(NULL, ","); |
| 206 | } |
| 207 | |
| 208 | ret = CMD_SUCCESS; |
| 209 | |
| 210 | /* Close Mi */ |
| 211 | if (lttng_opt_mi) { |
| 212 | /* Close channels element */ |
| 213 | ret = mi_lttng_writer_close_element(writer); |
| 214 | if (ret) { |
| 215 | ret = CMD_ERROR; |
| 216 | goto error; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | error: |
| 221 | /* Bypass the warning if a more important error happened */ |
| 222 | if (!ret && warn) { |
| 223 | ret = CMD_WARNING; |
| 224 | } |
| 225 | |
| 226 | lttng_destroy_handle(handle); |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * cmd_disable_channels |
| 233 | * |
| 234 | * Disable channel to trace session |
| 235 | */ |
| 236 | int cmd_disable_channels(int argc, const char **argv) |
| 237 | { |
| 238 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 239 | static poptContext pc; |
| 240 | char *session_name = NULL; |
| 241 | |
| 242 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 243 | poptReadDefaultConfig(pc, 0); |
| 244 | |
| 245 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 246 | switch (opt) { |
| 247 | case OPT_HELP: |
| 248 | usage(stdout); |
| 249 | goto end; |
| 250 | case OPT_USERSPACE: |
| 251 | opt_userspace = 1; |
| 252 | break; |
| 253 | case OPT_LIST_OPTIONS: |
| 254 | list_cmd_options(stdout, long_options); |
| 255 | goto end; |
| 256 | default: |
| 257 | usage(stderr); |
| 258 | ret = CMD_UNDEFINED; |
| 259 | goto end; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | opt_channels = (char*) poptGetArg(pc); |
| 264 | if (opt_channels == NULL) { |
| 265 | ERR("Missing channel name(s).\n"); |
| 266 | usage(stderr); |
| 267 | ret = CMD_ERROR; |
| 268 | goto end; |
| 269 | } |
| 270 | |
| 271 | if (!opt_session_name) { |
| 272 | session_name = get_session_name(); |
| 273 | if (session_name == NULL) { |
| 274 | ret = CMD_ERROR; |
| 275 | goto end; |
| 276 | } |
| 277 | } else { |
| 278 | session_name = opt_session_name; |
| 279 | } |
| 280 | |
| 281 | /* Mi check */ |
| 282 | if (lttng_opt_mi) { |
| 283 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 284 | if (!writer) { |
| 285 | ret = -LTTNG_ERR_NOMEM; |
| 286 | goto end; |
| 287 | } |
| 288 | |
| 289 | /* Open command element */ |
| 290 | ret = mi_lttng_writer_command_open(writer, |
| 291 | mi_lttng_element_command_disable_channel); |
| 292 | if (ret) { |
| 293 | ret = CMD_ERROR; |
| 294 | goto end; |
| 295 | } |
| 296 | |
| 297 | /* Open output element */ |
| 298 | ret = mi_lttng_writer_open_element(writer, |
| 299 | mi_lttng_element_command_output); |
| 300 | if (ret) { |
| 301 | ret = CMD_ERROR; |
| 302 | goto end; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | command_ret = disable_channels(session_name); |
| 307 | if (command_ret) { |
| 308 | success = 0; |
| 309 | } |
| 310 | |
| 311 | /* Mi closing */ |
| 312 | if (lttng_opt_mi) { |
| 313 | /* Close output element */ |
| 314 | ret = mi_lttng_writer_close_element(writer); |
| 315 | if (ret) { |
| 316 | ret = CMD_ERROR; |
| 317 | goto end; |
| 318 | } |
| 319 | |
| 320 | /* Success ? */ |
| 321 | ret = mi_lttng_writer_write_element_bool(writer, |
| 322 | mi_lttng_element_success, success); |
| 323 | if (ret) { |
| 324 | ret = CMD_ERROR; |
| 325 | goto end; |
| 326 | } |
| 327 | |
| 328 | /* Command element close */ |
| 329 | ret = mi_lttng_writer_command_close(writer); |
| 330 | if (ret) { |
| 331 | ret = CMD_ERROR; |
| 332 | goto end; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | end: |
| 337 | /* Mi clean-up */ |
| 338 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 339 | /* Preserve original error code */ |
| 340 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; |
| 341 | } |
| 342 | |
| 343 | if (!opt_session_name && session_name) { |
| 344 | free(session_name); |
| 345 | } |
| 346 | |
| 347 | /* Overwrite ret if an error occurred in disable_channels */ |
| 348 | ret = command_ret ? command_ret : ret; |
| 349 | |
| 350 | poptFreeContext(pc); |
| 351 | return ret; |
| 352 | } |