| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; only version 2 |
| 7 | * of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <popt.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | #include <inttypes.h> |
| 28 | |
| 29 | #include "../command.h" |
| 30 | |
| 31 | static char *opt_channels; |
| 32 | static int opt_kernel; |
| 33 | static char *opt_session_name; |
| 34 | static int opt_userspace; |
| 35 | static struct lttng_channel chan; |
| 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_DISCARD, |
| 45 | OPT_OVERWRITE, |
| 46 | OPT_SUBBUF_SIZE, |
| 47 | OPT_NUM_SUBBUF, |
| 48 | OPT_SWITCH_TIMER, |
| 49 | OPT_READ_TIMER, |
| 50 | OPT_USERSPACE, |
| 51 | OPT_LIST_OPTIONS, |
| 52 | }; |
| 53 | |
| 54 | static struct lttng_handle *handle; |
| 55 | |
| 56 | static struct poptOption long_options[] = { |
| 57 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 58 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 59 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 60 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 61 | #if 0 |
| 62 | /* Not implemented yet */ |
| 63 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0}, |
| 64 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
| 65 | #else |
| 66 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 67 | #endif |
| 68 | {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0}, |
| 69 | {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0}, |
| 70 | {"subbuf-size", 0, POPT_ARG_DOUBLE, 0, OPT_SUBBUF_SIZE, 0, 0}, |
| 71 | {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0}, |
| 72 | {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0}, |
| 73 | {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0}, |
| 74 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 75 | {0, 0, 0, 0, 0, 0, 0} |
| 76 | }; |
| 77 | |
| 78 | /* |
| 79 | * usage |
| 80 | */ |
| 81 | static void usage(FILE *ofp) |
| 82 | { |
| 83 | fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] [options] [channel_options]\n"); |
| 84 | fprintf(ofp, "\n"); |
| 85 | fprintf(ofp, " -h, --help Show this help\n"); |
| 86 | fprintf(ofp, " --list-options Simple listing of options\n"); |
| 87 | fprintf(ofp, " -s, --session Apply to session name\n"); |
| 88 | fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n"); |
| 89 | #if 0 |
| 90 | fprintf(ofp, " -u, --userspace [CMD] Apply to the user-space tracer\n"); |
| 91 | fprintf(ofp, " If no CMD, the domain used is UST global\n"); |
| 92 | fprintf(ofp, " or else the domain is UST EXEC_NAME\n"); |
| 93 | fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n"); |
| 94 | #else |
| 95 | fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n"); |
| 96 | #endif |
| 97 | fprintf(ofp, "\n"); |
| 98 | fprintf(ofp, "Channel options:\n"); |
| 99 | fprintf(ofp, " --discard Discard event when buffers are full%s\n", |
| 100 | DEFAULT_CHANNEL_OVERWRITE ? "" : " (default)"); |
| 101 | fprintf(ofp, " --overwrite Flight recorder mode%s\n", |
| 102 | DEFAULT_CHANNEL_OVERWRITE ? " (default)" : ""); |
| 103 | fprintf(ofp, " --subbuf-size SIZE Subbuffer size in bytes\n"); |
| 104 | fprintf(ofp, " (default: %u, kernel default: %u)\n", |
| 105 | DEFAULT_CHANNEL_SUBBUF_SIZE, |
| 106 | DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE); |
| 107 | fprintf(ofp, " --num-subbuf NUM Number of subbufers\n"); |
| 108 | fprintf(ofp, " (default: %u, kernel default: %u)\n", |
| 109 | DEFAULT_CHANNEL_SUBBUF_NUM, |
| 110 | DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM); |
| 111 | fprintf(ofp, " --switch-timer USEC Switch timer interval in usec (default: %u)\n", |
| 112 | DEFAULT_CHANNEL_SWITCH_TIMER); |
| 113 | fprintf(ofp, " --read-timer USEC Read timer interval in usec (default: %u)\n", |
| 114 | DEFAULT_CHANNEL_READ_TIMER); |
| 115 | fprintf(ofp, "\n"); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Set default attributes depending on those already defined from the command |
| 120 | * line. |
| 121 | */ |
| 122 | static void set_default_attr(struct lttng_domain *dom) |
| 123 | { |
| 124 | struct lttng_channel_attr default_attr; |
| 125 | |
| 126 | /* Set attributes */ |
| 127 | lttng_channel_set_default_attr(dom, &default_attr); |
| 128 | |
| 129 | if (chan.attr.overwrite == -1) { |
| 130 | chan.attr.overwrite = default_attr.overwrite; |
| 131 | } |
| 132 | if (chan.attr.subbuf_size == -1) { |
| 133 | chan.attr.subbuf_size = default_attr.subbuf_size; |
| 134 | } |
| 135 | if (chan.attr.num_subbuf == -1) { |
| 136 | chan.attr.num_subbuf = default_attr.num_subbuf; |
| 137 | } |
| 138 | if (chan.attr.switch_timer_interval == -1) { |
| 139 | chan.attr.switch_timer_interval = default_attr.switch_timer_interval; |
| 140 | } |
| 141 | if (chan.attr.read_timer_interval == -1) { |
| 142 | chan.attr.read_timer_interval = default_attr.read_timer_interval; |
| 143 | } |
| 144 | if (chan.attr.output == -1) { |
| 145 | chan.attr.output = default_attr.output; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Adding channel using the lttng API. |
| 151 | */ |
| 152 | static int enable_channel(char *session_name) |
| 153 | { |
| 154 | int ret = CMD_SUCCESS, warn = 0; |
| 155 | char *channel_name; |
| 156 | struct lttng_domain dom; |
| 157 | |
| 158 | memset(&dom, 0, sizeof(dom)); |
| 159 | |
| 160 | /* Create lttng domain */ |
| 161 | if (opt_kernel) { |
| 162 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 163 | } else if (opt_userspace) { |
| 164 | dom.type = LTTNG_DOMAIN_UST; |
| 165 | } else { |
| 166 | ERR("Please specify a tracer (-k/--kernel or -u/--userspace)"); |
| 167 | ret = CMD_ERROR; |
| 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | set_default_attr(&dom); |
| 172 | |
| 173 | handle = lttng_create_handle(session_name, &dom); |
| 174 | if (handle == NULL) { |
| 175 | ret = -1; |
| 176 | goto error; |
| 177 | } |
| 178 | |
| 179 | /* Strip channel list (format: chan1,chan2,...) */ |
| 180 | channel_name = strtok(opt_channels, ","); |
| 181 | while (channel_name != NULL) { |
| 182 | /* Copy channel name and normalize it */ |
| 183 | strncpy(chan.name, channel_name, NAME_MAX); |
| 184 | chan.name[NAME_MAX - 1] = '\0'; |
| 185 | |
| 186 | DBG("Enabling channel %s", channel_name); |
| 187 | |
| 188 | ret = lttng_enable_channel(handle, &chan); |
| 189 | if (ret < 0) { |
| 190 | ERR("Channel %s: %s (session %s)", channel_name, |
| 191 | lttng_strerror(ret), session_name); |
| 192 | warn = 1; |
| 193 | } else { |
| 194 | MSG("%s channel %s enabled for session %s", |
| 195 | opt_kernel ? "Kernel" : "UST", channel_name, |
| 196 | session_name); |
| 197 | } |
| 198 | |
| 199 | /* Next event */ |
| 200 | channel_name = strtok(NULL, ","); |
| 201 | } |
| 202 | |
| 203 | ret = CMD_SUCCESS; |
| 204 | |
| 205 | error: |
| 206 | if (warn) { |
| 207 | ret = CMD_WARNING; |
| 208 | } |
| 209 | |
| 210 | lttng_destroy_handle(handle); |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Default value for channel configuration. |
| 217 | */ |
| 218 | static void init_channel_config(void) |
| 219 | { |
| 220 | /* |
| 221 | * Put -1 everywhere so we can identify those set by the command line and |
| 222 | * those needed to be set by the default values. |
| 223 | */ |
| 224 | memset(&chan.attr, -1, sizeof(chan.attr)); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Add channel to trace session |
| 229 | */ |
| 230 | int cmd_enable_channels(int argc, const char **argv) |
| 231 | { |
| 232 | int opt, ret = CMD_SUCCESS; |
| 233 | static poptContext pc; |
| 234 | char *session_name = NULL; |
| 235 | |
| 236 | init_channel_config(); |
| 237 | |
| 238 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 239 | poptReadDefaultConfig(pc, 0); |
| 240 | |
| 241 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 242 | switch (opt) { |
| 243 | case OPT_HELP: |
| 244 | usage(stdout); |
| 245 | goto end; |
| 246 | case OPT_DISCARD: |
| 247 | chan.attr.overwrite = 0; |
| 248 | DBG("Channel set to discard"); |
| 249 | break; |
| 250 | case OPT_OVERWRITE: |
| 251 | chan.attr.overwrite = 1; |
| 252 | DBG("Channel set to overwrite"); |
| 253 | break; |
| 254 | case OPT_SUBBUF_SIZE: |
| 255 | /* TODO Replace atol with strtol and check for errors */ |
| 256 | chan.attr.subbuf_size = atol(poptGetOptArg(pc)); |
| 257 | DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size); |
| 258 | break; |
| 259 | case OPT_NUM_SUBBUF: |
| 260 | /* TODO Replace atoi with strtol and check for errors */ |
| 261 | chan.attr.num_subbuf = atoi(poptGetOptArg(pc)); |
| 262 | DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf); |
| 263 | break; |
| 264 | case OPT_SWITCH_TIMER: |
| 265 | /* TODO Replace atoi with strtol and check for errors */ |
| 266 | chan.attr.switch_timer_interval = atoi(poptGetOptArg(pc)); |
| 267 | DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval); |
| 268 | break; |
| 269 | case OPT_READ_TIMER: |
| 270 | /* TODO Replace atoi with strtol and check for errors */ |
| 271 | chan.attr.read_timer_interval = atoi(poptGetOptArg(pc)); |
| 272 | DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval); |
| 273 | break; |
| 274 | case OPT_USERSPACE: |
| 275 | opt_userspace = 1; |
| 276 | break; |
| 277 | case OPT_LIST_OPTIONS: |
| 278 | list_cmd_options(stdout, long_options); |
| 279 | goto end; |
| 280 | default: |
| 281 | usage(stderr); |
| 282 | ret = CMD_UNDEFINED; |
| 283 | goto end; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | opt_channels = (char*) poptGetArg(pc); |
| 288 | if (opt_channels == NULL) { |
| 289 | ERR("Missing channel name.\n"); |
| 290 | usage(stderr); |
| 291 | ret = CMD_ERROR; |
| 292 | goto end; |
| 293 | } |
| 294 | |
| 295 | if (!opt_session_name) { |
| 296 | session_name = get_session_name(); |
| 297 | if (session_name == NULL) { |
| 298 | ret = CMD_ERROR; |
| 299 | goto end; |
| 300 | } |
| 301 | } else { |
| 302 | session_name = opt_session_name; |
| 303 | } |
| 304 | |
| 305 | ret = enable_channel(session_name); |
| 306 | |
| 307 | end: |
| 308 | if (!opt_session_name && session_name) { |
| 309 | free(session_name); |
| 310 | } |
| 311 | poptFreeContext(pc); |
| 312 | return ret; |
| 313 | } |