2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
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>
34 #include <common/utils.h>
36 static char *opt_output_path
;
37 static char *opt_session_name
;
38 static char *opt_uris
;
39 static char *opt_ctrl_uris
;
40 static char *opt_data_uris
;
41 static int opt_no_consumer
= 1;
48 static struct poptOption long_options
[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
51 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
52 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
53 {"set-uri", 'U', POPT_ARG_STRING
, &opt_uris
, 0, 0, 0},
54 {"ctrl-uri", 'C', POPT_ARG_STRING
, &opt_ctrl_uris
, 0, 0, 0},
55 {"data-uri", 'D', POPT_ARG_STRING
, &opt_data_uris
, 0, 0, 0},
56 {"no-consumer", 0, POPT_ARG_NONE
, &opt_no_consumer
, 0, 0, 0},
63 static void usage(FILE *ofp
)
65 fprintf(ofp
, "usage: lttng create [options] [NAME]\n");
67 fprintf(ofp
, " The default NAME is 'auto-yyyymmdd-hhmmss'\n");
68 fprintf(ofp
, " -h, --help Show this help\n");
69 fprintf(ofp
, " --list-options Simple listing of options\n");
70 fprintf(ofp
, " -o, --output PATH Specify output path for traces\n");
71 fprintf(ofp
, " -U, --set-uri=URI Set URI for the enable-consumer destination.\n");
72 fprintf(ofp
, " It is persistent for the session lifetime.\n");
73 fprintf(ofp
, " Redo the command to change it.\n");
74 fprintf(ofp
, " This will set both data and control URI for network.\n");
75 fprintf(ofp
, " -C, --ctrl-uri=URI Set control path URI.\n");
76 fprintf(ofp
, " -D, --data-uri=URI Set data path URI.\n");
77 fprintf(ofp
, " --no-consumer Disable consumer for entire tracing session.\n");
82 * Parse URI from string to lttng_uri object array.
84 static ssize_t
parse_uri_from_str(const char *str_uri
, struct lttng_uri
**uris
)
88 struct lttng_uri
*uri
;
94 size
= uri_parse(str_uri
, uris
);
96 ERR("Bad URI %s. Either the hostname or IP is invalid", str_uri
);
100 for (i
= 0; i
< size
; i
++) {
101 uri
= (struct lttng_uri
*) &uris
[i
];
102 /* Set default port if none was given */
103 if (uri
->port
== 0) {
104 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
105 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
106 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
107 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
118 static void print_uri_msg(struct lttng_uri
*uri
)
122 switch (uri
->dtype
) {
131 MSG("Consumer destination set to %s", dst
);
134 DBG("Unknown URI destination");
138 MSG("Consumer %s stream set to %s with the %s protocol on port %d",
139 uri
->stype
== LTTNG_STREAM_CONTROL
? "control" : "data",
140 dst
, uri
->proto
== LTTNG_TCP
? "TCP" : "UNK", uri
->port
);
147 * Create a tracing session.
148 * If no name is specified, a default name is generated.
150 * Returns one of the CMD_* result constants.
152 static int create_session()
154 int ret
, have_name
= 0, i
;
156 char *session_name
, *traces_path
= NULL
, *alloc_path
= NULL
;
160 struct lttng_uri
*uris
= NULL
, *ctrl_uri
= NULL
, *data_uri
= NULL
;
162 /* Get date and time for automatic session name/path */
164 timeinfo
= localtime(&rawtime
);
165 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
167 /* Auto session name creation */
168 if (opt_session_name
== NULL
) {
169 ret
= asprintf(&session_name
, "auto-%s", datetime
);
171 perror("asprintf session name");
174 DBG("Auto session name set to %s", session_name
);
176 session_name
= opt_session_name
;
180 if (opt_output_path
!= NULL
) {
181 traces_path
= utils_expand_path(opt_output_path
);
182 if (traces_path
== NULL
) {
187 ret
= asprintf(&alloc_path
, "file://%s", traces_path
);
189 PERROR("asprintf expand path");
194 ret
= uri_parse(alloc_path
, &ctrl_uri
);
199 } else if (opt_uris
) { /* Handling URIs (-U opt) */
200 size
= parse_uri_from_str(opt_uris
, &uris
);
204 } else if (size
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
205 ERR("Only net:// and file:// are supported. "
206 "Use -C and -D for more fine grained control");
209 } else if (size
== 2) {
210 uris
[0].stype
= LTTNG_STREAM_CONTROL
;
211 uris
[1].stype
= LTTNG_STREAM_DATA
;
213 for (i
= 0; i
< size
; i
++) {
214 /* Set default port if none was given */
215 if (uris
[i
].port
== 0) {
216 if (uris
[i
].stype
== LTTNG_STREAM_CONTROL
) {
217 uris
[i
].port
= DEFAULT_NETWORK_CONTROL_PORT
;
219 uris
[i
].port
= DEFAULT_NETWORK_DATA_PORT
;
225 print_uri_msg(ctrl_uri
);
227 print_uri_msg(data_uri
);
230 print_uri_msg(ctrl_uri
);
232 } else if (opt_ctrl_uris
|| opt_data_uris
) {
233 /* Setting up control URI (-C opt) */
235 size
= parse_uri_from_str(opt_ctrl_uris
, &uris
);
241 ctrl_uri
->stype
= LTTNG_STREAM_CONTROL
;
242 /* Set default port if none specified */
243 if (ctrl_uri
->port
== 0) {
244 ctrl_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
246 print_uri_msg(ctrl_uri
);
249 /* Setting up data URI (-D opt) */
251 size
= parse_uri_from_str(opt_data_uris
, &uris
);
257 data_uri
->stype
= LTTNG_STREAM_DATA
;
258 /* Set default port if none specified */
259 if (data_uri
->port
== 0) {
260 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
262 print_uri_msg(data_uri
);
265 /* Auto output path */
266 alloc_path
= config_get_default_path();
267 if (alloc_path
== NULL
) {
268 ERR("HOME path not found.\n \
269 Please specify an output path using -o, --output PATH");
273 alloc_path
= strdup(alloc_path
);
276 ret
= asprintf(&traces_path
, "file://%s/" DEFAULT_TRACE_DIR_NAME
277 "/%s-%s", alloc_path
, session_name
, datetime
);
279 ret
= asprintf(&traces_path
, "file://%s/" DEFAULT_TRACE_DIR_NAME
280 "/%s", alloc_path
, session_name
);
283 PERROR("asprintf trace dir name");
288 ret
= uri_parse(traces_path
, &ctrl_uri
);
295 /* If there is no subdir specified and the URI are network */
296 if (strlen(ctrl_uri
->subdir
) == 0) {
298 ret
= snprintf(ctrl_uri
->subdir
, sizeof(ctrl_uri
->subdir
), "%s-%s",
299 session_name
, datetime
);
301 ret
= snprintf(ctrl_uri
->subdir
, sizeof(ctrl_uri
->subdir
), "%s",
305 PERROR("snprintf subdir");
308 DBG("Subdir update to %s", ctrl_uri
->subdir
);
311 ret
= lttng_create_session_uri(session_name
, ctrl_uri
, data_uri
,
314 /* Don't set ret so lttng can interpret the sessiond error. */
316 case LTTCOMM_EXIST_SESS
:
317 WARN("Session %s already exists", session_name
);
323 /* Init lttng session config */
324 ret
= config_init(session_name
);
330 MSG("Session %s created.", session_name
);
331 if (ctrl_uri
->dtype
== LTTNG_DST_PATH
) {
332 MSG("Traces will be written in %s" , ctrl_uri
->dst
.path
);
338 if (opt_session_name
== NULL
) {
353 * The 'create <options>' first level command
355 * Returns one of the CMD_* result constants.
357 int cmd_create(int argc
, const char **argv
)
359 int opt
, ret
= CMD_SUCCESS
;
360 static poptContext pc
;
362 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
363 poptReadDefaultConfig(pc
, 0);
365 while ((opt
= poptGetNextOpt(pc
)) != -1) {
370 case OPT_LIST_OPTIONS
:
371 list_cmd_options(stdout
, long_options
);
380 opt_session_name
= (char*) poptGetArg(pc
);
382 ret
= create_session();