2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
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.
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
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.
24 #include <sys/types.h>
28 #include "../command.h"
31 #include <common/defaults.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
33 #include <common/uri.h>
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_uris
;
40 static char *opt_ctrl_uris
;
41 static char *opt_data_uris
;
42 static char *opt_uris_arg
;
44 static struct lttng_handle
*handle
;
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_uris
, 0, 0, 0},
59 {"ctrl-uri", 'C', POPT_ARG_STRING
, &opt_ctrl_uris
, 0, 0, 0},
60 {"data-uri", 'D', POPT_ARG_STRING
, &opt_data_uris
, 0, 0, 0},
61 {"enable", 'e', POPT_ARG_VAL
, &opt_enable
, 1, 0, 0},
68 static void usage(FILE *ofp
)
70 fprintf(ofp
, "usage: lttng enable-consumer [-u|-k] [URI] [OPTIONS]\n");
72 fprintf(ofp
, "The default behavior is to enable a consumer to the current URI.\n");
73 fprintf(ofp
, "The default URI is the local filesystem at the path of the session.\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");
78 fprintf(ofp
, "You can optionally specify two URIs for respectively the\n");
79 fprintf(ofp
, "control and data channel. URI supported:\n");
80 fprintf(ofp
, " > file://PATH\n");
81 fprintf(ofp
, " Local file full system path.\n");
83 fprintf(ofp
, " > net://DST[:CTRL_PORT[:DATA_PORT]] and net6://...\n");
84 fprintf(ofp
, " This will use the default network transport layer which is\n");
85 fprintf(ofp
, " TCP for both control and data port. The default ports are\n");
86 fprintf(ofp
, " respectively 5342 and 5343.\n");
87 fprintf(ofp
, " Example:\n");
88 fprintf(ofp
, " # lttng enable-consumer net://192.168.1.42 -k\n");
89 fprintf(ofp
, " Uses TCP and default ports for the given destination.\n");
91 fprintf(ofp
, " > tcp://DST:PORT and tcp6://DST:PORT\n");
93 fprintf(ofp
, "Options:\n");
94 fprintf(ofp
, " -h, --help Show this help\n");
95 fprintf(ofp
, " --list-options Simple listing of options\n");
96 fprintf(ofp
, " -s, --session=NAME Apply to session name\n");
97 fprintf(ofp
, " -k, --kernel Apply to the kernel tracer\n");
98 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
99 //fprintf(ofp, " -U, --set-uri=URI1[,URI2,...]\n");
101 fprintf(ofp
, "Extended Options:\n");
103 fprintf(ofp
, "Using these options, each API call is controlled individually.\n");
104 fprintf(ofp
, "For instance, -C does not enable the consumer automatically.\n");
106 fprintf(ofp
, " -U, --set-uri=URI Set URI for the enable-consumer destination.\n");
107 fprintf(ofp
, " It is persistent for the session lifetime.\n");
108 fprintf(ofp
, " Redo the command to change it.\n");
109 fprintf(ofp
, " This will set both data and control URI for network.\n");
110 //fprintf(ofp, " -C, --ctrl-uri=URI1[,URI2,...]\n");
111 fprintf(ofp
, " -C, --ctrl-uri=URI Set control path URI.\n");
112 //fprintf(ofp, " -D, --data-uri=URI1[,URI2,...]\n");
113 fprintf(ofp
, " -D, --data-uri=URI Set data path URI.\n");
114 fprintf(ofp
, " -e, --enable Enable consumer\n");
121 static void print_uri_msg(struct lttng_uri
*uri
)
125 switch (uri
->dtype
) {
134 MSG("Consumer destination set to %s", dst
);
137 DBG("Unknown URI destination");
141 MSG("Consumer %s stream set to %s with the %s protocol on port %d",
142 uri
->stype
== LTTNG_STREAM_CONTROL
? "control" : "data",
143 dst
, uri
->proto
== LTTNG_TCP
? "TCP" : "UNK", uri
->port
);
150 * Setting URIs taking from the command line arguments. There is some
151 * manipulations and special cases using the default args.
153 static int set_consumer_arg_uris(struct lttng_uri
*uri
, size_t size
)
158 /* URIs are the control and data stream respectively for net:// */
159 uri
[0].stype
= LTTNG_STREAM_CONTROL
;
160 uri
[1].stype
= LTTNG_STREAM_DATA
;
162 for (i
= 0; i
< size
; i
++) {
163 ret
= lttng_set_consumer_uri(handle
, &uri
[i
]);
165 ERR("Setting %s stream URI: %s",
166 uri
[i
].stype
== LTTNG_STREAM_DATA
? "data" : "control",
167 lttng_strerror(ret
));
170 /* Set default port if none was given */
171 if (uri
[i
].port
== 0) {
172 if (uri
[i
].stype
== LTTNG_STREAM_CONTROL
) {
173 uri
[i
].port
= DEFAULT_NETWORK_CONTROL_PORT
;
175 uri
[i
].port
= DEFAULT_NETWORK_DATA_PORT
;
178 print_uri_msg(&uri
[i
]);
180 } else if (size
== 1 && uri
[0].dtype
== LTTNG_DST_PATH
) {
181 /* Set URI if it's file:// */
182 ret
= lttng_set_consumer_uri(handle
, &uri
[0]);
184 ERR("Failed to set URI %s: %s", opt_uris_arg
,
185 lttng_strerror(ret
));
188 print_uri_msg(&uri
[0]);
190 ERR("Only net:// and file:// are supported. "
191 "Use -D or -U for more fine grained control");
201 * Parse URI from string to lttng_uri object array.
203 static ssize_t
parse_uri_from_str(const char *str_uri
, struct lttng_uri
**uris
)
211 size
= uri_parse(str_uri
, uris
);
213 ERR("Bad URI %s. Either the hostname or IP is invalid", str_uri
);
221 * Enable consumer command.
223 static int enable_consumer(char *session_name
)
225 int ret
= CMD_SUCCESS
;
226 int run_enable_cmd
= 1;
228 struct lttng_domain dom
;
229 struct lttng_uri
*uri
= NULL
;
231 memset(&dom
, 0, sizeof(dom
));
233 /* Create lttng domain */
235 dom
.type
= LTTNG_DOMAIN_KERNEL
;
236 } else if (opt_userspace
) {
237 dom
.type
= LTTNG_DOMAIN_UST
;
239 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
244 handle
= lttng_create_handle(session_name
, &dom
);
245 if (handle
== NULL
) {
250 /* Handle trailing arguments */
252 size
= parse_uri_from_str(opt_uris_arg
, &uri
);
258 ret
= set_consumer_arg_uris(uri
, size
);
264 /* Handling URIs (-U opt) */
266 size
= parse_uri_from_str(opt_uris
, &uri
);
272 ret
= set_consumer_arg_uris(uri
, size
);
277 /* opt_enable will tell us to run or not the enable_consumer cmd. */
281 /* Setting up control URI (-C opt) */
283 size
= parse_uri_from_str(opt_ctrl_uris
, &uri
);
289 /* Set default port if none specified */
290 if (uri
[0].port
== 0) {
291 uri
[0].port
= DEFAULT_NETWORK_CONTROL_PORT
;
294 uri
[0].stype
= LTTNG_STREAM_CONTROL
;
296 ret
= lttng_set_consumer_uri(handle
, &uri
[0]);
298 ERR("Failed to set control URI %s: %s", opt_ctrl_uris
,
299 lttng_strerror(ret
));
302 print_uri_msg(&uri
[0]);
304 /* opt_enable will tell us to run or not the enable_consumer cmd. */
308 /* Setting up data URI (-D opt) */
310 size
= parse_uri_from_str(opt_data_uris
, &uri
);
316 /* Set default port if none specified */
317 if (uri
[0].port
== 0) {
318 uri
[0].port
= DEFAULT_NETWORK_DATA_PORT
;
321 uri
[0].stype
= LTTNG_STREAM_DATA
;
323 ret
= lttng_set_consumer_uri(handle
, &uri
[0]);
325 ERR("Failed to set data URI %s: %s", opt_data_uris
,
326 lttng_strerror(ret
));
329 print_uri_msg(&uri
[0]);
331 /* opt_enable will tell us to run or not the enable_consumer cmd. */
335 /* Enable consumer (-e opt) */
336 if (opt_enable
|| run_enable_cmd
) {
337 ret
= lttng_enable_consumer(handle
);
339 ERR("Enabling consumer for session %s: %s", session_name
,
340 lttng_strerror(ret
));
341 if (ret
== -LTTCOMM_ENABLE_CONSUMER_FAIL
) {
342 ERR("Perhaps the session was previously started?");
347 MSG("Consumer enabled successfully");
354 lttng_destroy_handle(handle
);
359 * The 'enable-consumer <options>' first level command
361 * Returns one of the CMD_* result constants.
363 int cmd_enable_consumer(int argc
, const char **argv
)
365 int opt
, ret
= CMD_SUCCESS
;
366 static poptContext pc
;
367 char *session_name
= NULL
;
369 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
370 poptReadDefaultConfig(pc
, 0);
372 while ((opt
= poptGetNextOpt(pc
)) != -1) {
377 case OPT_LIST_OPTIONS
:
378 list_cmd_options(stdout
, long_options
);
387 opt_uris_arg
= (char *) poptGetArg(pc
);
388 DBG("URIs: %s", opt_uris_arg
);
390 /* Get session name */
391 if (!opt_session_name
) {
392 session_name
= get_session_name();
393 if (session_name
== NULL
) {
398 session_name
= opt_session_name
;
401 ret
= enable_consumer(session_name
);
404 if (opt_session_name
== NULL
) {