| 1 | /* |
| 2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * 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 <time.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "../command.h" |
| 29 | #include "../utils.h" |
| 30 | |
| 31 | #include <common/defaults.h> |
| 32 | #include <common/sessiond-comm/sessiond-comm.h> |
| 33 | #include <common/uri.h> |
| 34 | |
| 35 | static int opt_kernel; |
| 36 | static int opt_userspace; |
| 37 | static int opt_enable; |
| 38 | static char *opt_session_name; |
| 39 | static char *opt_url; |
| 40 | static char *opt_ctrl_url; |
| 41 | static char *opt_data_url; |
| 42 | static char *opt_url_arg; |
| 43 | |
| 44 | static struct lttng_handle *handle; |
| 45 | |
| 46 | enum { |
| 47 | OPT_HELP = 1, |
| 48 | OPT_LIST_OPTIONS, |
| 49 | }; |
| 50 | |
| 51 | static struct poptOption long_options[] = { |
| 52 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 53 | {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL}, |
| 54 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 55 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 56 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 57 | {"userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, 0, 0}, |
| 58 | {"set-uri", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0}, |
| 59 | {"ctrl-uri", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0}, |
| 60 | {"data-uri", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0}, |
| 61 | {"enable", 'e', POPT_ARG_VAL, &opt_enable, 1, 0, 0}, |
| 62 | {0, 0, 0, 0, 0, 0, 0} |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * usage |
| 67 | */ |
| 68 | static void usage(FILE *ofp) |
| 69 | { |
| 70 | fprintf(ofp, "usage: lttng enable-consumer [-u|-k] [URL] [OPTIONS]\n"); |
| 71 | fprintf(ofp, "\n"); |
| 72 | fprintf(ofp, "The default behavior is to enable a consumer to the current URL.\n"); |
| 73 | fprintf(ofp, "The default URL is the local filesystem at the path of the session.\n"); |
| 74 | fprintf(ofp, "\n"); |
| 75 | fprintf(ofp, "The enable-consumer feature supports both local and network transport.\n"); |
| 76 | fprintf(ofp, "You must have a running lttng-relayd for network transmission.\n"); |
| 77 | fprintf(ofp, "\n"); |
| 78 | fprintf(ofp, "Options:\n"); |
| 79 | fprintf(ofp, " -h, --help Show this help\n"); |
| 80 | fprintf(ofp, " --list-options Simple listing of options\n"); |
| 81 | fprintf(ofp, " -s, --session NAME Apply to session name\n"); |
| 82 | fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n"); |
| 83 | fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n"); |
| 84 | fprintf(ofp, "\n"); |
| 85 | fprintf(ofp, "Extended Options:\n"); |
| 86 | fprintf(ofp, "\n"); |
| 87 | fprintf(ofp, "Using these options, each API call can be controlled individually.\n"); |
| 88 | fprintf(ofp, "For instance, -C does not enable the consumer automatically.\n"); |
| 89 | fprintf(ofp, "\n"); |
| 90 | fprintf(ofp, " -U, --set-uri=URL Set URL for the enable-consumer destination.\n"); |
| 91 | fprintf(ofp, " It is persistent for the session lifetime.\n"); |
| 92 | fprintf(ofp, " Redo the command to change it.\n"); |
| 93 | fprintf(ofp, " This will set both data and control URL for network.\n"); |
| 94 | fprintf(ofp, " -C, --ctrl-url=URL Set control path URL. (Must use -D also)\n"); |
| 95 | fprintf(ofp, " -D, --data-url=URL Set data path URL. (Must use -C also)\n"); |
| 96 | fprintf(ofp, " -e, --enable Enable consumer\n"); |
| 97 | fprintf(ofp, "\n"); |
| 98 | fprintf(ofp, "\n"); |
| 99 | fprintf(ofp, "Please refer to the man page (lttng(1)) for more information on network\n"); |
| 100 | fprintf(ofp, "streaming mechanisms and explanation of the control and data port\n"); |
| 101 | fprintf(ofp, "\n"); |
| 102 | fprintf(ofp, "URL format is has followed:\n"); |
| 103 | fprintf(ofp, "\n"); |
| 104 | fprintf(ofp, " proto://[HOST|IP][:PORT1[:PORT2]][/TRACE_PATH]\n"); |
| 105 | fprintf(ofp, "\n"); |
| 106 | fprintf(ofp, " Supported protocols are (proto):\n"); |
| 107 | fprintf(ofp, " > file://...\n"); |
| 108 | fprintf(ofp, " Local filesystem full path.\n"); |
| 109 | fprintf(ofp, "\n"); |
| 110 | fprintf(ofp, " > net[6]://...\n"); |
| 111 | fprintf(ofp, " This will use the default network transport layer which is\n"); |
| 112 | fprintf(ofp, " TCP for both control (PORT1) and data port (PORT2).\n"); |
| 113 | fprintf(ofp, " The default ports are respectively 5342 and 5343.\n"); |
| 114 | fprintf(ofp, "\n"); |
| 115 | fprintf(ofp, " > tcp[4|6]://...\n"); |
| 116 | fprintf(ofp, " Can only be used with -C and -D together\n"); |
| 117 | fprintf(ofp, "\n"); |
| 118 | fprintf(ofp, "NOTE: IPv6 address MUST be enclosed in brackets '[]' (rfc2732)\n"); |
| 119 | fprintf(ofp, "\n"); |
| 120 | fprintf(ofp, "Examples:\n"); |
| 121 | fprintf(ofp, " # lttng enable-consumer -u net://192.168.1.42\n"); |
| 122 | fprintf(ofp, " Uses TCP and default ports for user space tracing (-u).\n"); |
| 123 | fprintf(ofp, "\n"); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Enable consumer command. |
| 128 | */ |
| 129 | static int enable_consumer(char *session_name) |
| 130 | { |
| 131 | int ret = CMD_SUCCESS; |
| 132 | int run_enable_cmd = 1; |
| 133 | struct lttng_domain dom; |
| 134 | |
| 135 | memset(&dom, 0, sizeof(dom)); |
| 136 | |
| 137 | /* Create lttng domain */ |
| 138 | if (opt_kernel) { |
| 139 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 140 | } else if (opt_userspace) { |
| 141 | dom.type = LTTNG_DOMAIN_UST; |
| 142 | } else { |
| 143 | /* |
| 144 | * Set handle with domain set to 0. This means to the session daemon |
| 145 | * that the next action applies on the tracing session rather then the |
| 146 | * domain specific session. |
| 147 | * |
| 148 | * XXX: This '0' value should be a domain enum value. |
| 149 | */ |
| 150 | dom.type = 0; |
| 151 | } |
| 152 | |
| 153 | handle = lttng_create_handle(session_name, &dom); |
| 154 | if (handle == NULL) { |
| 155 | ret = -1; |
| 156 | goto error; |
| 157 | } |
| 158 | |
| 159 | /* Handle trailing arguments */ |
| 160 | if (opt_url_arg) { |
| 161 | ret = lttng_set_consumer_url(handle, opt_url_arg, NULL); |
| 162 | if (ret < 0) { |
| 163 | ERR("%s", lttng_strerror(ret)); |
| 164 | goto error; |
| 165 | } |
| 166 | |
| 167 | MSG("URL %s set for session %s.", opt_url_arg, session_name); |
| 168 | } |
| 169 | |
| 170 | /* Handling URLs (-U opt) */ |
| 171 | if (opt_url) { |
| 172 | ret = lttng_set_consumer_url(handle, opt_url, NULL); |
| 173 | if (ret < 0) { |
| 174 | ERR("%s", lttng_strerror(ret)); |
| 175 | goto error; |
| 176 | } |
| 177 | |
| 178 | /* opt_enable will tell us to run or not the enable_consumer cmd. */ |
| 179 | run_enable_cmd = 0; |
| 180 | |
| 181 | MSG("URL %s set for session %s.", opt_url, session_name); |
| 182 | } |
| 183 | |
| 184 | /* Setting up control URL (-C or/and -D opt) */ |
| 185 | if (opt_ctrl_url || opt_data_url) { |
| 186 | ret = lttng_set_consumer_url(handle, opt_ctrl_url, opt_data_url); |
| 187 | if (ret < 0) { |
| 188 | ERR("%s", lttng_strerror(ret)); |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | /* opt_enable will tell us to run or not the enable_consumer cmd. */ |
| 193 | run_enable_cmd = 0; |
| 194 | |
| 195 | if (opt_ctrl_url) { |
| 196 | MSG("Control URL %s set for session %s.", opt_ctrl_url, |
| 197 | session_name); |
| 198 | } |
| 199 | |
| 200 | if (opt_data_url) { |
| 201 | MSG("Data URL %s set for session %s.", opt_data_url, session_name); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* Enable consumer (-e opt) */ |
| 206 | if (opt_enable || run_enable_cmd) { |
| 207 | ret = lttng_enable_consumer(handle); |
| 208 | if (ret < 0) { |
| 209 | ERR("Enabling consumer for session %s: %s", session_name, |
| 210 | lttng_strerror(ret)); |
| 211 | if (ret == -LTTCOMM_ENABLE_CONSUMER_FAIL) { |
| 212 | ERR("Perhaps the session was previously started?"); |
| 213 | } |
| 214 | goto error; |
| 215 | } |
| 216 | |
| 217 | MSG("Consumer enabled successfully"); |
| 218 | } |
| 219 | |
| 220 | error: |
| 221 | lttng_destroy_handle(handle); |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * The 'enable-consumer <options>' first level command |
| 227 | * |
| 228 | * Returns one of the CMD_* result constants. |
| 229 | */ |
| 230 | int cmd_enable_consumer(int argc, const char **argv) |
| 231 | { |
| 232 | int opt, ret = CMD_SUCCESS; |
| 233 | static poptContext pc; |
| 234 | char *session_name = NULL; |
| 235 | |
| 236 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 237 | poptReadDefaultConfig(pc, 0); |
| 238 | |
| 239 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 240 | switch (opt) { |
| 241 | case OPT_HELP: |
| 242 | usage(stdout); |
| 243 | goto end; |
| 244 | case OPT_LIST_OPTIONS: |
| 245 | list_cmd_options(stdout, long_options); |
| 246 | goto end; |
| 247 | default: |
| 248 | usage(stderr); |
| 249 | ret = CMD_UNDEFINED; |
| 250 | goto end; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | opt_url_arg = (char *) poptGetArg(pc); |
| 255 | DBG("URLs: %s", opt_url_arg); |
| 256 | |
| 257 | /* Get session name */ |
| 258 | if (!opt_session_name) { |
| 259 | session_name = get_session_name(); |
| 260 | if (session_name == NULL) { |
| 261 | ret = CMD_ERROR; |
| 262 | goto end; |
| 263 | } |
| 264 | } else { |
| 265 | session_name = opt_session_name; |
| 266 | } |
| 267 | |
| 268 | ret = enable_consumer(session_name); |
| 269 | |
| 270 | end: |
| 271 | if (opt_session_name == NULL) { |
| 272 | free(session_name); |
| 273 | } |
| 274 | |
| 275 | poptFreeContext(pc); |
| 276 | return ret; |
| 277 | } |