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>
35 static char *opt_output_path
;
36 static char *opt_session_name
;
37 static char *opt_uris
;
38 static char *opt_ctrl_uris
;
39 static char *opt_data_uris
;
40 static int opt_no_consumer
= 1;
47 static struct poptOption long_options
[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
50 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
51 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
52 {"set-uri", 'U', POPT_ARG_STRING
, &opt_uris
, 0, 0, 0},
53 {"ctrl-uri", 'C', POPT_ARG_STRING
, &opt_ctrl_uris
, 0, 0, 0},
54 {"data-uri", 'D', POPT_ARG_STRING
, &opt_data_uris
, 0, 0, 0},
55 {"no-consumer", 0, POPT_ARG_NONE
, &opt_no_consumer
, 0, 0, 0},
62 static void usage(FILE *ofp
)
64 fprintf(ofp
, "usage: lttng create [options] [NAME]\n");
66 fprintf(ofp
, " The default NAME is 'auto-yyyymmdd-hhmmss'\n");
67 fprintf(ofp
, " -h, --help Show this help\n");
68 fprintf(ofp
, " --list-options Simple listing of options\n");
69 fprintf(ofp
, " -o, --output PATH Specify output path for traces\n");
70 fprintf(ofp
, " -U, --set-uri=URI Set URI for the enable-consumer destination.\n");
71 fprintf(ofp
, " It is persistent for the session lifetime.\n");
72 fprintf(ofp
, " Redo the command to change it.\n");
73 fprintf(ofp
, " This will set both data and control URI for network.\n");
74 fprintf(ofp
, " -C, --ctrl-uri=URI Set control path URI.\n");
75 fprintf(ofp
, " -D, --data-uri=URI Set data path URI.\n");
76 fprintf(ofp
, " --no-consumer Disable consumer for entire tracing session.\n");
81 * Parse URI from string to lttng_uri object array.
83 static ssize_t
parse_uri_from_str(const char *str_uri
, struct lttng_uri
**uris
)
87 struct lttng_uri
*uri
;
93 size
= uri_parse(str_uri
, uris
);
95 ERR("Bad URI %s. Either the hostname or IP is invalid", str_uri
);
99 for (i
= 0; i
< size
; i
++) {
100 uri
= (struct lttng_uri
*) &uris
[i
];
101 /* Set default port if none was given */
102 if (uri
->port
== 0) {
103 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
104 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
105 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
106 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
117 static void print_uri_msg(struct lttng_uri
*uri
)
121 switch (uri
->dtype
) {
130 MSG("Consumer destination set to %s", dst
);
133 DBG("Unknown URI destination");
137 MSG("Consumer %s stream set to %s with the %s protocol on port %d",
138 uri
->stype
== LTTNG_STREAM_CONTROL
? "control" : "data",
139 dst
, uri
->proto
== LTTNG_TCP
? "TCP" : "UNK", uri
->port
);
146 * Create a tracing session.
147 * If no name is specified, a default name is generated.
149 * Returns one of the CMD_* result constants.
151 static int create_session()
153 int ret
, have_name
= 0, i
;
155 char *session_name
, *traces_path
= NULL
, *alloc_path
= NULL
;
159 struct lttng_uri
*uris
= NULL
, *ctrl_uri
= NULL
, *data_uri
= NULL
;
161 /* Get date and time for automatic session name/path */
163 timeinfo
= localtime(&rawtime
);
164 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
166 /* Auto session name creation */
167 if (opt_session_name
== NULL
) {
168 ret
= asprintf(&session_name
, "auto-%s", datetime
);
170 perror("asprintf session name");
173 DBG("Auto session name set to %s", session_name
);
175 session_name
= opt_session_name
;
179 if (opt_output_path
!= NULL
) {
180 traces_path
= expand_full_path(opt_output_path
);
181 if (traces_path
== NULL
) {
186 ret
= asprintf(&alloc_path
, "file://%s", traces_path
);
188 PERROR("asprintf expand path");
193 ret
= uri_parse(alloc_path
, &ctrl_uri
);
198 } else if (opt_uris
) { /* Handling URIs (-U opt) */
199 size
= parse_uri_from_str(opt_uris
, &uris
);
203 } else if (size
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
204 ERR("Only net:// and file:// are supported. "
205 "Use -C and -D for more fine grained control");
208 } else if (size
== 2) {
209 uris
[0].stype
= LTTNG_STREAM_CONTROL
;
210 uris
[1].stype
= LTTNG_STREAM_DATA
;
212 for (i
= 0; i
< size
; i
++) {
213 /* Set default port if none was given */
214 if (uris
[i
].port
== 0) {
215 if (uris
[i
].stype
== LTTNG_STREAM_CONTROL
) {
216 uris
[i
].port
= DEFAULT_NETWORK_CONTROL_PORT
;
218 uris
[i
].port
= DEFAULT_NETWORK_DATA_PORT
;
224 print_uri_msg(ctrl_uri
);
226 print_uri_msg(data_uri
);
229 print_uri_msg(ctrl_uri
);
231 } else if (opt_ctrl_uris
|| opt_data_uris
) {
232 /* Setting up control URI (-C opt) */
234 size
= parse_uri_from_str(opt_ctrl_uris
, &uris
);
240 ctrl_uri
->stype
= LTTNG_STREAM_CONTROL
;
241 /* Set default port if none specified */
242 if (ctrl_uri
->port
== 0) {
243 ctrl_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
245 print_uri_msg(ctrl_uri
);
248 /* Setting up data URI (-D opt) */
250 size
= parse_uri_from_str(opt_data_uris
, &uris
);
256 data_uri
->stype
= LTTNG_STREAM_DATA
;
257 /* Set default port if none specified */
258 if (data_uri
->port
== 0) {
259 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
261 print_uri_msg(data_uri
);
264 /* Auto output path */
265 alloc_path
= config_get_default_path();
266 if (alloc_path
== NULL
) {
267 ERR("HOME path not found.\n \
268 Please specify an output path using -o, --output PATH");
272 alloc_path
= strdup(alloc_path
);
275 ret
= asprintf(&traces_path
, "file://%s/" DEFAULT_TRACE_DIR_NAME
276 "/%s-%s", alloc_path
, session_name
, datetime
);
278 ret
= asprintf(&traces_path
, "file://%s/" DEFAULT_TRACE_DIR_NAME
279 "/%s", alloc_path
, session_name
);
282 PERROR("asprintf trace dir name");
287 ret
= uri_parse(traces_path
, &ctrl_uri
);
294 /* If there is no subdir specified and the URI are network */
295 if (strlen(ctrl_uri
->subdir
) == 0) {
297 ret
= snprintf(ctrl_uri
->subdir
, sizeof(ctrl_uri
->subdir
), "%s-%s",
298 session_name
, datetime
);
300 ret
= snprintf(ctrl_uri
->subdir
, sizeof(ctrl_uri
->subdir
), "%s",
304 PERROR("snprintf subdir");
307 DBG("Subdir update to %s", ctrl_uri
->subdir
);
310 ret
= lttng_create_session_uri(session_name
, ctrl_uri
, data_uri
,
313 /* Don't set ret so lttng can interpret the sessiond error. */
315 case LTTCOMM_EXIST_SESS
:
316 WARN("Session %s already exists", session_name
);
322 /* Init lttng session config */
323 ret
= config_init(session_name
);
329 MSG("Session %s created.", session_name
);
330 if (ctrl_uri
->dtype
== LTTNG_DST_PATH
) {
331 MSG("Traces will be written in %s" , ctrl_uri
->dst
.path
);
337 if (opt_session_name
== NULL
) {
352 * The 'create <options>' first level command
354 * Returns one of the CMD_* result constants.
356 int cmd_create(int argc
, const char **argv
)
358 int opt
, ret
= CMD_SUCCESS
;
359 static poptContext pc
;
361 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
362 poptReadDefaultConfig(pc
, 0);
364 while ((opt
= poptGetNextOpt(pc
)) != -1) {
369 case OPT_LIST_OPTIONS
:
370 list_cmd_options(stdout
, long_options
);
379 opt_session_name
= (char*) poptGetArg(pc
);
381 ret
= create_session();