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
;
40 static char *opt_ctrl_url
;
41 static char *opt_data_url
;
42 static char *opt_url_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_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},
68 static void usage(FILE *ofp
)
70 fprintf(ofp
, "usage: lttng enable-consumer [-u|-k] [URL] [OPTIONS]\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");
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
, "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");
85 fprintf(ofp
, "Extended Options:\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");
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");
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");
102 fprintf(ofp
, "URL format is has followed:\n");
104 fprintf(ofp
, " proto://[HOST|IP][:PORT1[:PORT2]][/TRACE_PATH]\n");
106 fprintf(ofp
, " Supported protocols are (proto):\n");
107 fprintf(ofp
, " > file://...\n");
108 fprintf(ofp
, " Local filesystem full path.\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");
115 fprintf(ofp
, " > tcp[4|6]://...\n");
116 fprintf(ofp
, " Can only be used with -C and -D together\n");
118 fprintf(ofp
, "NOTE: IPv6 address MUST be enclosed in brackets '[]' (rfc2732)\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");
127 * Enable consumer command.
129 static int enable_consumer(char *session_name
)
131 int ret
= CMD_SUCCESS
;
132 int run_enable_cmd
= 1;
133 struct lttng_domain dom
;
135 memset(&dom
, 0, sizeof(dom
));
137 /* Create lttng domain */
139 dom
.type
= LTTNG_DOMAIN_KERNEL
;
140 } else if (opt_userspace
) {
141 dom
.type
= LTTNG_DOMAIN_UST
;
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.
148 * XXX: This '0' value should be a domain enum value.
153 handle
= lttng_create_handle(session_name
, &dom
);
154 if (handle
== NULL
) {
159 /* Handle trailing arguments */
161 ret
= lttng_set_consumer_url(handle
, opt_url_arg
, NULL
);
163 ERR("%s", lttng_strerror(ret
));
167 MSG("URL %s set for session %s.", opt_url_arg
, session_name
);
170 /* Handling URLs (-U opt) */
172 ret
= lttng_set_consumer_url(handle
, opt_url
, NULL
);
174 ERR("%s", lttng_strerror(ret
));
178 /* opt_enable will tell us to run or not the enable_consumer cmd. */
181 MSG("URL %s set for session %s.", opt_url
, session_name
);
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
);
188 ERR("%s", lttng_strerror(ret
));
192 /* opt_enable will tell us to run or not the enable_consumer cmd. */
196 MSG("Control URL %s set for session %s.", opt_ctrl_url
,
201 MSG("Data URL %s set for session %s.", opt_data_url
, session_name
);
205 /* Enable consumer (-e opt) */
206 if (opt_enable
|| run_enable_cmd
) {
207 ret
= lttng_enable_consumer(handle
);
209 ERR("Enabling consumer for session %s: %s", session_name
,
210 lttng_strerror(ret
));
211 if (ret
== -LTTNG_ERR_ENABLE_CONSUMER_FAIL
) {
212 ERR("Perhaps the session was previously started?");
217 MSG("Consumer enabled successfully");
221 lttng_destroy_handle(handle
);
226 * The 'enable-consumer <options>' first level command
228 * Returns one of the CMD_* result constants.
230 int cmd_enable_consumer(int argc
, const char **argv
)
232 int opt
, ret
= CMD_SUCCESS
;
233 static poptContext pc
;
234 char *session_name
= NULL
;
236 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
237 poptReadDefaultConfig(pc
, 0);
239 while ((opt
= poptGetNextOpt(pc
)) != -1) {
244 case OPT_LIST_OPTIONS
:
245 list_cmd_options(stdout
, long_options
);
254 opt_url_arg
= (char *) poptGetArg(pc
);
255 DBG("URLs: %s", opt_url_arg
);
257 /* Get session name */
258 if (!opt_session_name
) {
259 session_name
= get_session_name();
260 if (session_name
== NULL
) {
265 session_name
= opt_session_name
;
268 ret
= enable_consumer(session_name
);
271 if (opt_session_name
== NULL
) {