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.
26 #include <sys/types.h>
32 #include <common/mi-lttng.h>
33 #include <common/config/session-config.h>
35 #include "../command.h"
38 #include <common/defaults.h>
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/uri.h>
41 #include <common/utils.h>
42 #include <lttng/snapshot.h>
44 static char *opt_output_path
;
45 static char *opt_session_name
;
47 static char *opt_ctrl_url
;
48 static char *opt_data_url
;
49 static char *opt_shm_path
;
50 static char *opt_template_path
;
51 static int opt_no_consumer
;
52 static int opt_no_output
;
53 static int opt_snapshot
;
54 static unsigned int opt_live_timer
;
55 static bool opt_default_name
;
56 static bool opt_default_output
;
80 static struct mi_writer
*writer
;
82 static struct poptOption long_options
[] = {
83 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
84 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
85 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
86 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
87 {"set-url", 'U', POPT_ARG_STRING
, &opt_url
, 0, 0, 0},
88 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
89 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
90 {"no-output", 0, POPT_ARG_VAL
, &opt_no_output
, 1, 0, 0},
91 {"no-consumer", 0, POPT_ARG_VAL
, &opt_no_consumer
, 1, 0, 0},
92 {"snapshot", 0, POPT_ARG_VAL
, &opt_snapshot
, 1, 0, 0},
93 {"live", 0, POPT_ARG_INT
| POPT_ARGFLAG_OPTIONAL
, 0, OPT_LIVE_TIMER
, 0, 0},
94 {"shm-path", 0, POPT_ARG_STRING
, &opt_shm_path
, 0, 0, 0},
95 {"template-path", 0, POPT_ARG_STRING
, &opt_template_path
, 0, 0, 0},
96 {"default-name", 0, POPT_ARG_NONE
, NULL
, OPT_DEFAULT_NAME
, 0, 0},
97 {"default-output", 0, POPT_ARG_NONE
, NULL
, OPT_DEFAULT_OUTPUT
, 0, 0},
102 * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on
103 * why this declaration exists and used ONLY in for this command.
105 extern int _lttng_create_session_ext(const char *name
, const char *url
,
106 const char *datetime
);
109 * Retrieve the created session and mi output it based on provided argument
110 * This is currently a summary of what was pretty printed and is subject to
113 static int mi_created_session(const char *session_name
)
115 int ret
, i
, count
, found
;
116 struct lttng_session
*sessions
;
118 /* session_name should not be null */
119 assert(session_name
);
122 count
= lttng_list_sessions(&sessions
);
125 ERR("%s", lttng_strerror(ret
));
130 ERR("Error session creation failed: session %s not found", session_name
);
131 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
136 for (i
= 0; i
< count
; i
++) {
137 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
139 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
148 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
160 * For a session name, set the consumer URLs.
162 static int set_consumer_url(const char *session_name
, const char *ctrl_url
,
163 const char *data_url
)
166 struct lttng_handle
*handle
;
168 assert(session_name
);
171 * Set handle with the session_name, but no domain. This implies that
172 * the actions taken with this handle apply on the tracing session
173 * rather then the domain-specific session.
175 handle
= lttng_create_handle(session_name
, NULL
);
176 if (handle
== NULL
) {
181 ret
= lttng_set_consumer_url(handle
, ctrl_url
, data_url
);
187 lttng_destroy_handle(handle
);
191 static int add_snapshot_output(const char *session_name
, const char *ctrl_url
,
192 const char *data_url
)
195 struct lttng_snapshot_output
*output
= NULL
;
197 assert(session_name
);
199 output
= lttng_snapshot_output_create();
206 ret
= lttng_snapshot_output_set_ctrl_url(ctrl_url
, output
);
213 ret
= lttng_snapshot_output_set_data_url(data_url
, output
);
219 /* This call, if successful, populates the id of the output object. */
220 ret
= lttng_snapshot_add_output(session_name
, output
);
226 lttng_snapshot_output_destroy(output
);
232 * Validate the combinations of passed options
235 * CMD_SUCCESS on success
237 static int validate_command_options(enum session_type type
)
239 int ret
= CMD_SUCCESS
;
241 if (opt_snapshot
&& opt_live_timer
) {
242 ERR("Snapshot and live modes are mutually exclusive.");
247 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
248 ERR("Control and data URLs must both be set.");
253 if (opt_template_path
) {
254 /* Restriction on flags exist when using template */
255 /* Session type flags are not permitted */
256 /* --live & --snapshot */
257 if ((opt_live_timer
&& type
!= SESSION_LIVE
) ||
258 (opt_snapshot
&& type
!= SESSION_SNAPSHOT
)) {
259 ERR("It is not possible to change the session type of a template");
265 if (opt_session_name
&& opt_default_name
) {
266 ERR("A session name must not be provided if the --default-name option is used");
271 if (opt_default_output
&& (opt_data_url
|| opt_ctrl_url
|| opt_url
||
273 ERR("The --default-output option may not be used with the --output/-o, --set-url/-U, --ctrl-url/-C, or --data-url/-D options");
282 * Create a session using direct calls to liblttng-ctl.
284 * Return CMD_SUCCESS on success, negative value on internal lttng errors and positive
285 * value on command errors.
287 static int create_session_basic (const char *session_name
,
288 enum session_type session_type
,
290 enum output_type output_type
,
292 const char* ctrl_url
,
293 const char* data_url
,
294 const char* shm_path
,
295 const char* datetime
)
297 /* Create session based on session creation */
298 int ret
= CMD_SUCCESS
;
299 const char *pathname
;
303 if (opt_relayd_path
) {
304 pathname
= opt_relayd_path
;
306 pathname
= INSTALL_BIN_PATH
"/lttng-relayd";
309 switch (session_type
) {
311 ret
= _lttng_create_session_ext(session_name
, url
, datetime
);
313 case SESSION_SNAPSHOT
:
314 if (output_type
== OUTPUT_NONE
) {
315 ERR("--no-output on a snapshot session is invalid");
316 ret
= CMD_UNSUPPORTED
;
319 ret
= lttng_create_session_snapshot(session_name
, url
);
322 if (output_type
== OUTPUT_NONE
) {
323 ERR("--no-output on a live session is invalid");
324 ret
= CMD_UNSUPPORTED
;
328 if (output_type
== OUTPUT_LOCAL
) {
329 ERR("Local file output on a live session is invalid");
330 ret
= CMD_UNSUPPORTED
;
333 if (output_type
!= OUTPUT_NET
&& !check_relayd() &&
334 spawn_relayd(pathname
, 0) < 0) {
338 ret
= lttng_create_session_live(session_name
, url
, live_timer
);
341 ERR("Unknown session type");
347 /* Don't set ret so the sessiond error is propagated. */
349 case LTTNG_ERR_EXIST_SESS
:
350 WARN("Session %s already exists", session_name
);
358 /* Configure the session based on the output type */
359 switch (output_type
) {
363 if (!ctrl_url
|| !data_url
) {
367 if (session_type
== SESSION_SNAPSHOT
) {
368 ret
= add_snapshot_output(session_name
, ctrl_url
,
372 * Normal sessions and live sessions behave the same way
373 * regarding consumer url.
375 ret
= set_consumer_url(session_name
, ctrl_url
, data_url
);
378 /* Destroy created session on errors */
379 lttng_destroy_session(session_name
);
386 ERR("Unknown output type");
392 * Set the session shared memory path
395 ret
= lttng_set_session_shm_path(session_name
, shm_path
);
397 lttng_destroy_session(session_name
);
405 static int generate_output(const char *session_name
,
406 enum session_type session_type
,
408 enum output_type output_type
,
410 const char* ctrl_url
,
411 const char* data_url
,
412 const char* shm_path
)
414 int ret
= CMD_SUCCESS
;
417 * TODO move this to after session name
418 * for now we only emulate previous behaviour.
420 if (session_type
!= SESSION_SNAPSHOT
) {
422 MSG("Control URL %s set for session %s", ctrl_url
, session_name
);
426 MSG("Data URL %s set for session %s", data_url
, session_name
);
430 if (url
&& output_type
== OUTPUT_LOCAL
) {
431 /* Remove the file:// */
432 if (strlen(url
) > strlen("file://")){
433 url
= url
+ strlen("file://");
437 MSG("Session %s created.", session_name
);
438 if (url
&& session_type
!= SESSION_SNAPSHOT
) {
439 MSG("Traces will be written in %s", url
);
442 MSG("Live timer set to %u usec", live_timer
);
444 } else if (session_type
== SESSION_SNAPSHOT
) {
446 MSG("Default snapshot output set to: %s", url
);
448 MSG("Snapshot mode set. Every channel enabled for that session will "
449 "be set to mmap output, and default to overwrite mode.");
453 MSG("Session %s set to shm_path: %s.", session_name
,
459 ret
= mi_created_session(session_name
);
469 static int parse_template (struct config_document
*template,
471 enum session_type
*session_type
,
473 enum output_type
*output_type
,
481 char *raw_value
= NULL
;
486 *session_name
= config_document_get_element_value(template,"/sessions/session/name");
488 /* Check the type of session we have in the template */
489 if(config_document_element_exist(template, "/sessions/session/attributes/snapshot_mode")) {
490 *session_type
= SESSION_SNAPSHOT
;
491 } else if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
492 *session_type
= SESSION_LIVE
;
493 raw_value
= config_document_get_element_value(template,"/sessions/session/attributes/live_timer_interval");
494 *live_timer
= config_parse_value(raw_value
);
498 *session_type
= SESSION_NORMAL
;
502 switch (*session_type
) {
505 if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")){
508 if (config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/path")){
509 raw_value
= config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/path");
515 if (strlen(raw_value
) > 0) {
516 *output_type
= OUTPUT_LOCAL
;
517 printed_bytes
= asprintf(url
, "file://%s", raw_value
);
518 if (printed_bytes
< 0) {
523 *output_type
= OUTPUT_NONE
;
529 } else if(config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/net_output")) {
530 *ctrl_url
= config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/control_uri");
531 *data_url
= config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/data_uri");
532 if (!*ctrl_url
|| ! *data_url
) {
536 *output_type
= OUTPUT_NET
;
538 /* There is no output definition */
541 case SESSION_SNAPSHOT
:
542 if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")){
545 if (config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path")){
546 raw_value
= config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path");
552 if (strlen(raw_value
) > 0) {
553 *output_type
= OUTPUT_LOCAL
;
554 printed_bytes
= asprintf(url
, "file://%s", raw_value
);
555 if (printed_bytes
< 0) {
560 *output_type
= OUTPUT_NONE
;
566 } else if(config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output")) {
567 *ctrl_url
= config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/control_uri");
568 *data_url
= config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/data_uri");
569 if (!*ctrl_url
|| ! *data_url
) {
573 *output_type
= OUTPUT_NET
;
575 /* There is no output definition */
578 case SESSION_UNKNOWN
:
584 /* shared memory path */
585 *shm_path
= config_document_get_element_value(template,"/sessions/session/shared_memory_path");
592 static int create_session_from_template(struct config_document
*template,
593 const char *session_name
,
594 enum session_type session_type
,
596 enum output_type output_type
,
598 const char *ctrl_url
,
599 const char *data_url
,
600 const char *shm_path
,
601 const char *datetime
)
603 int ret
= CMD_SUCCESS
;
605 struct config_element
*temp_element
= NULL
;
606 struct config_element
*temp_element_child
= NULL
;
607 char tmp_ctrl_uri
[PATH_MAX
];
608 char tmp_data_uri
[PATH_MAX
];
609 struct lttng_uri
*uris
= NULL
;
610 ssize_t uri_array_size
= 0;
611 char *tmp_string
= NULL
;
614 assert(session_name
);
616 memset(tmp_ctrl_uri
, 0, sizeof(tmp_ctrl_uri
));
617 memset(tmp_data_uri
, 0, sizeof(tmp_data_uri
));
620 if (config_document_element_exist(template, "/sessions/session/name")) {
621 /* Replace the node value */
622 config_document_replace_element_value(template, "/sessions/session/name", session_name
);
624 /* insert the node */
625 temp_element
= config_element_create("name", session_name
);
627 ERR("Could not create session name node configuration");
631 ret
= config_document_insert_element(template, "/sessions/session", temp_element
);
633 ERR("Could not insert session name node configuration");
637 config_element_free(temp_element
);
643 if (session_type
== SESSION_LIVE
) {
644 if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
645 printed_bytes
= asprintf(&tmp_string
, "%d", live_timer
);
646 if (printed_bytes
< 0) {
647 ERR("Asprintf failed for live timer");
651 ret
= config_document_replace_element_value(template, "/sessions/session/attributes/live_timer_interval", tmp_string
);
654 printf("error: %d\n", ret
);
655 ERR("Replacement of live_timer_interval failed");
663 ERR("Invalid live timer template. Missing live timer node");
671 * Generate the output node
674 /* Get output from urls */
676 /* Get lttng uris from single url */
677 uri_array_size
= uri_parse_str_urls(url
, NULL
, &uris
);
678 if (uri_array_size
< 0) {
682 } else if (ctrl_url
&& data_url
) {
683 uri_array_size
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
684 if (uri_array_size
< 0) {
693 /* Validate if the session output type still match the passed data */
694 if ( (uri_array_size
== 0 && output_type
!= OUTPUT_NONE
) ||
695 (uri_array_size
== 1 && output_type
!= OUTPUT_LOCAL
) ||
696 (uri_array_size
== 2 && output_type
!= OUTPUT_NET
)) {
697 ERR("Overwriting value for output do not match the base output type");
702 switch (output_type
) {
704 temp_element_child
= config_element_create("path", "");
705 if (!temp_element_child
) {
706 ERR("Could not create empty path node configuration");
712 temp_element_child
= config_element_create("path", uris
[0].dst
.path
);
713 if (!temp_element_child
) {
714 ERR("Could not create local path node configuration");
720 uri_to_str_url(&uris
[0], tmp_ctrl_uri
, sizeof(tmp_ctrl_uri
));
721 uri_to_str_url(&uris
[1], tmp_data_uri
, sizeof(tmp_data_uri
));
723 temp_element_child
= config_element_create("net_output", NULL
);
724 if (!temp_element_child
) {
725 ERR("Could not create net_output node configuration");
730 temp_element
= config_element_create("control_uri", tmp_ctrl_uri
);
731 if (!temp_element_child
) {
732 ERR("Could not create ctrl uri node configuration");
737 ret
= config_element_add_child(temp_element_child
, temp_element
);
739 ERR("Could not append control uri to the net_output node configuration");
744 config_element_free(temp_element
);
747 temp_element
= config_element_create("data_uri", tmp_data_uri
);
749 if (!temp_element_child
) {
750 ERR("Could not create data_uri configuration");
755 ret
= config_element_add_child(temp_element_child
, temp_element
);
757 ERR("Could not append data uri to the net_output node configuration");
761 config_element_free(temp_element
);
769 temp_element
= config_element_create("destination", NULL
);
771 ERR("Could not create destination node configuration");
776 ret
= config_element_add_child(temp_element
, temp_element_child
);
778 ERR("Could not append output data to the destination node configuration");
783 config_element_free(temp_element_child
);
784 temp_element_child
= NULL
;
786 /* Construct the output/consumer_output/ node*/
787 temp_element_child
= temp_element
;
788 temp_element
= config_element_create("consumer_output", NULL
);
790 ERR("Could not create consumer_output node configuration");
795 ret
= config_element_add_child(temp_element
, temp_element_child
);
797 ERR("Could not append output data to the consumer_output node configuration");
802 config_element_free(temp_element_child
);
803 temp_element_child
= config_element_create("enabled", "true");
804 if (!temp_element_child
) {
805 ERR("Could not create enbaled node configuration");
810 ret
= config_element_add_child(temp_element
, temp_element_child
);
812 ERR("Could not append node to the consumer_output node configuration");
817 config_element_free(temp_element_child
);
818 temp_element_child
= NULL
;
821 temp_element_child
= temp_element
;
822 temp_element
= config_element_create("output", NULL
);
824 ERR("Could not create output node configuration");
829 ret
= config_element_add_child(temp_element
, temp_element_child
);
831 ERR("Could not append output data to the output node configuration");
836 config_element_free(temp_element_child
);
837 temp_element_child
= NULL
;
841 * validate and replace the destination node for each session type
842 * TODO: export string as const and simply assign a base path for the
843 * destination node based on the session type
845 switch (session_type
) {
849 case SESSION_SNAPSHOT
:
850 /* construct the output/snapshots_outputs/ */
851 temp_element_child
= temp_element
;
852 temp_element
= config_element_create("snapshot_outputs", NULL
);
854 ERR("Could not create snapshot_outputs node configuration");
859 ret
= config_element_add_child(temp_element
, temp_element_child
);
861 ERR("Could not append output data to the snapshot_outputs node configuration");
866 config_element_free(temp_element_child
);
867 temp_element_child
= NULL
;
869 temp_element_child
= temp_element
;
870 temp_element
= config_element_create("output", NULL
);
872 ERR("Could not create output node configuration");
877 ret
= config_element_add_child(temp_element
, temp_element_child
);
879 ERR("Could not append output data to the output node configuration");
884 config_element_free(temp_element_child
);
885 temp_element_child
= NULL
;
888 ERR("Invalid session type");
893 if (!config_document_element_exist(template, "/sessions/session/output")) {
894 ret
= config_document_insert_element(template, "/sessions/session", temp_element
);
896 ret
= config_document_replace_element(template, "/sessions/session/output", temp_element
);
899 config_element_free(temp_element
);
903 ERR("%s", lttng_strerror(ret
));
910 if (shm_path
&& config_document_element_exist(template, "/sessions/session/shared_memory_path")) {
911 /* Replace the node value */
912 config_document_replace_element_value(template, "/sessions/session/shared_memory_path", shm_path
);
913 } else if (shm_path
) {
914 /* insert the node */
915 temp_element
= config_element_create("shared_memory_path", shm_path
);
917 ERR("Could not create shared_memory_path node configuration");
921 ret
= config_document_insert_element(template, "/sessions/session", temp_element
);
923 ERR("Could not insert shared_memory_path node configuration");
928 config_element_free(temp_element
);
932 ret
= config_load_configuration_sessions(template, session_name
, 0);
936 config_element_free(temp_element
);
937 config_element_free(temp_element_child
);
945 * Create a tracing session.
946 * If no name is specified, a default name is generated.
948 * Returns one of the CMD_* result constants.
950 static int create_session(void)
957 struct config_document
*template = NULL
;
960 enum session_type base_session_type
= SESSION_UNKNOWN
;
961 enum output_type base_output_type
= OUTPUT_UNKNOWN
;
962 char *base_session_name
= NULL
;
963 char *base_url
= NULL
;
964 char *base_ctrl_url
= NULL
;
965 char *base_data_url
= NULL
;
966 char *base_shm_path
= NULL
;
967 int base_live_timer
= 0;
972 struct tm
*timeinfo
= NULL
;
974 /* Temporary variables */
975 char *traces_path
= NULL
;
976 char *temp_url
= NULL
;
977 char *session_name_date
= NULL
;
978 char *tmp_url
= NULL
;
979 char *tmp_home_path
= NULL
;
980 struct lttng_uri
*uris
= NULL
;
981 ssize_t uri_array_size
= 0;
984 /* Get date and time for automatic session name/path */
986 timeinfo
= localtime(&rawtime
);
987 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
989 if (opt_template_path
) {
990 /* Restriction on flags exist when using template */
991 /* Session type flags are not permitted */
992 /* --live & --snapshot */
993 template = config_document_get(opt_template_path
);
995 ERR("Template could not be parsed");
999 /* Load info from template if any */
1000 /* TODO: might want to use a struct in the end for the session... */
1001 ret
= parse_template(template, &base_session_name
,
1014 /* Option validation */
1015 if (validate_command_options(base_session_type
) != CMD_SUCCESS
) {
1020 /* Find the session type based on options */
1021 if(base_session_type
== SESSION_UNKNOWN
) {
1023 base_session_type
= SESSION_SNAPSHOT
;
1024 } else if (opt_live_timer
) {
1025 base_session_type
= SESSION_LIVE
;
1027 base_session_type
= SESSION_NORMAL
;
1032 * Session name handling
1034 if (opt_session_name
) {
1035 /* Override the session name */
1036 if (strlen(opt_session_name
) > NAME_MAX
) {
1037 ERR("Session name too long. Length must be lower or equal to %d",
1039 ret
= LTTNG_ERR_SESSION_FAIL
;
1040 free(session_name_date
);
1044 * Check if the session name begins with "auto-" or is exactly "auto".
1045 * Both are reserved for the default session name. See bug #449 to
1046 * understand why we need to check both here.
1048 if ((strncmp(opt_session_name
, DEFAULT_SESSION_NAME
"-",
1049 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) ||
1050 (strncmp(opt_session_name
, DEFAULT_SESSION_NAME
,
1051 strlen(DEFAULT_SESSION_NAME
)) == 0 &&
1052 strlen(opt_session_name
) == strlen(DEFAULT_SESSION_NAME
))) {
1053 ERR("%s is a reserved keyword for default session(s)",
1054 DEFAULT_SESSION_NAME
);
1060 base_session_name
= strndup(opt_session_name
, NAME_MAX
);
1061 if (!base_session_name
) {
1062 PERROR("Strdup session name");
1067 printed_bytes
= asprintf(&session_name_date
, "%s-%s", base_session_name
, datetime
);
1068 if (printed_bytes
< 0) {
1069 PERROR("Asprintf session name");
1073 DBG("Session name from command option set to %s", base_session_name
);
1074 } else if (base_session_name
&& !opt_default_name
) {
1075 printed_bytes
= asprintf(&session_name_date
, "%s-%s", base_session_name
, datetime
);
1076 if (printed_bytes
< 0) {
1077 PERROR("Asprintf session name");
1082 /* Generate a name */
1083 printed_bytes
= asprintf(&base_session_name
, DEFAULT_SESSION_NAME
"-%s", datetime
);
1084 if (printed_bytes
< 0) {
1085 PERROR("Asprintf session name");
1089 session_name_date
= strdup(base_session_name
);
1090 DBG("Auto session name set to %s", base_session_name
);
1099 * If any of those options are present clear all output related data.
1101 if (opt_output_path
|| opt_url
|| (opt_ctrl_url
&& opt_data_url
) || opt_no_output
) {
1102 /* Overwrite output */
1104 free(base_ctrl_url
);
1105 free(base_data_url
);
1107 base_ctrl_url
= NULL
;
1108 base_data_url
= NULL
;
1111 if (opt_output_path
) {
1113 traces_path
= utils_expand_path(opt_output_path
);
1119 /* Create URL string from the local file system path */
1120 printed_bytes
= asprintf(&temp_url
, "file://%s", traces_path
);
1121 if (printed_bytes
< 0) {
1122 PERROR("asprintf url path");
1127 base_url
= temp_url
;
1128 } else if (opt_url
) { /* Handling URL (-U opt) */
1129 base_url
= strdup(opt_url
);
1130 } else if (opt_data_url
&& opt_ctrl_url
) {
1132 * With both control and data, we'll be setting the consumer URL
1133 * after session creation thus use no URL.
1135 base_ctrl_url
= strdup(opt_ctrl_url
);
1136 base_data_url
= strdup(opt_data_url
);
1137 } else if (opt_default_output
|| !(opt_no_output
||
1138 base_output_type
== OUTPUT_NONE
|| base_url
||
1139 base_ctrl_url
|| base_data_url
)) {
1140 /* Generate default output depending on the session type */
1141 switch (base_session_type
) {
1142 case SESSION_NORMAL
:
1144 case SESSION_SNAPSHOT
:
1145 /* Default to a local path */
1146 tmp_home_path
= utils_get_home_dir();
1147 if (tmp_home_path
== NULL
) {
1148 ERR("HOME path not found.\n \
1149 Please specify an output path using -o, --output PATH");
1154 printed_bytes
= asprintf(&tmp_url
,
1155 "file://%s/" DEFAULT_TRACE_DIR_NAME
"/%s",
1156 tmp_home_path
, session_name_date
);
1158 if (printed_bytes
< 0) {
1159 PERROR("asprintf trace dir name");
1164 base_url
= tmp_url
;
1167 /* Default to a net output */
1168 printed_bytes
= asprintf(&tmp_url
, "net://127.0.0.1");
1169 if (printed_bytes
< 0) {
1170 PERROR("asprintf default live URL");
1174 base_url
= tmp_url
;
1177 ERR("Unknown session type");
1184 * Shared memory path handling
1187 /* Overwrite shm_path so clear any previously defined one */
1188 free(base_shm_path
);
1189 printed_bytes
= asprintf(&base_shm_path
, "%s/%s", opt_shm_path
, session_name_date
);
1190 if (printed_bytes
< 0) {
1191 PERROR("asprintf shm_path");
1198 * Live timer handling
1200 if (opt_live_timer
) {
1201 base_live_timer
= opt_live_timer
;
1204 /* Get output type from urls */
1205 /* TODO: Find a better way of inferring output type */
1207 /* Get lttng uris from single url */
1208 uri_array_size
= uri_parse_str_urls(base_url
, NULL
, &uris
);
1209 if (uri_array_size
< 0) {
1213 } else if (base_ctrl_url
&& base_data_url
) {
1214 uri_array_size
= uri_parse_str_urls(base_ctrl_url
, base_data_url
, &uris
);
1215 if (uri_array_size
< 0) {
1224 switch (uri_array_size
) {
1226 base_output_type
= OUTPUT_NONE
;
1229 base_output_type
= OUTPUT_LOCAL
;
1232 base_output_type
= OUTPUT_NET
;
1240 ret
= create_session_from_template(template,
1251 ret
= create_session_basic(base_session_name
,
1265 ret
= generate_output(base_session_name
,
1277 /* Init lttng session config */
1278 ret
= config_init(base_session_name
);
1287 /* Session temp stuff */
1288 config_document_free(template);
1289 free(session_name_date
);
1294 ERR("%s", lttng_strerror(ret
));
1297 free(base_session_name
);
1299 free(base_ctrl_url
);
1300 free(base_data_url
);
1301 free(base_shm_path
);
1308 * Spawn a session daemon by forking and execv.
1310 static int spawn_sessiond(char *pathname
)
1315 MSG("Spawning a session daemon");
1319 * Spawn session daemon in daemon mode.
1321 execlp(pathname
, "lttng-sessiond",
1322 "--daemonize", NULL
);
1323 /* execlp only returns if error happened */
1324 if (errno
== ENOENT
) {
1325 ERR("No session daemon found. Use --sessiond-path.");
1329 kill(getppid(), SIGTERM
); /* wake parent */
1331 } else if (pid
> 0) {
1333 * In daemon mode (--daemonize), sessiond only exits when
1334 * it's ready to accept commands.
1338 pid_t wait_pid_ret
= waitpid(pid
, &status
, 0);
1340 if (wait_pid_ret
< 0) {
1341 if (errno
== EINTR
) {
1349 if (WIFSIGNALED(status
)) {
1350 ERR("Session daemon was killed by signal %d",
1354 } else if (WIFEXITED(status
)) {
1355 DBG("Session daemon terminated normally (exit status: %d)",
1356 WEXITSTATUS(status
));
1358 if (WEXITSTATUS(status
) != 0) {
1359 ERR("Session daemon terminated with an error (exit status: %d)",
1360 WEXITSTATUS(status
));
1382 * Check if the session daemon is available using
1383 * the liblttngctl API for the check. If not, try to
1386 static int launch_sessiond(void)
1389 char *pathname
= NULL
;
1391 ret
= lttng_session_daemon_alive();
1393 /* Sessiond is alive, not an error */
1398 /* Try command line option path */
1399 pathname
= opt_sessiond_path
;
1401 /* Try LTTNG_SESSIOND_PATH env variable */
1402 if (pathname
== NULL
) {
1403 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
1406 /* Try with configured path */
1407 if (pathname
== NULL
) {
1408 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
1409 pathname
= CONFIG_SESSIOND_BIN
;
1413 /* Try the default path */
1414 if (pathname
== NULL
) {
1415 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
1418 DBG("Session daemon binary path: %s", pathname
);
1420 /* Check existence and permissions */
1421 ret
= access(pathname
, F_OK
| X_OK
);
1423 ERR("No such file or access denied: %s", pathname
);
1427 ret
= spawn_sessiond(pathname
);
1430 ERR("Problem occurred while launching session daemon (%s)",
1437 * The 'create <options>' first level command
1439 * Returns one of the CMD_* result constants.
1441 int cmd_create(int argc
, const char **argv
)
1443 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1444 char *opt_arg
= NULL
;
1445 static poptContext pc
;
1447 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1448 poptReadDefaultConfig(pc
, 0);
1450 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1455 case OPT_LIST_OPTIONS
:
1456 list_cmd_options(stdout
, long_options
);
1458 case OPT_LIVE_TIMER
:
1463 opt_arg
= poptGetOptArg(pc
);
1465 /* Set up default values. */
1466 opt_live_timer
= (uint32_t) DEFAULT_LTTNG_LIVE_TIMER
;
1467 DBG("Session live timer interval set to default value %d",
1472 v
= strtoul(opt_arg
, NULL
, 0);
1473 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
1474 ERR("Wrong value in --live parameter: %s", opt_arg
);
1478 if (v
!= (uint32_t) v
) {
1479 ERR("32-bit overflow in --live parameter: %s", opt_arg
);
1484 ERR("Live timer interval must be greater than zero");
1488 opt_live_timer
= (uint32_t) v
;
1489 DBG("Session live timer interval set to %d", opt_live_timer
);
1492 case OPT_DEFAULT_NAME
:
1493 opt_default_name
= true;
1495 case OPT_DEFAULT_OUTPUT
:
1496 opt_default_output
= true;
1499 ret
= CMD_UNDEFINED
;
1504 if (opt_no_consumer
) {
1505 MSG("The option --no-consumer is obsolete. Use --no-output now.");
1510 /* Spawn a session daemon if needed */
1511 if (!opt_no_sessiond
) {
1512 ret
= launch_sessiond();
1519 /* MI initialization */
1521 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1523 ret
= -LTTNG_ERR_NOMEM
;
1527 /* Open command element */
1528 ret
= mi_lttng_writer_command_open(writer
,
1529 mi_lttng_element_command_create
);
1535 /* Open output element */
1536 ret
= mi_lttng_writer_open_element(writer
,
1537 mi_lttng_element_command_output
);
1543 opt_session_name
= (char*) poptGetArg(pc
);
1545 command_ret
= create_session();
1552 /* Close output element */
1553 ret
= mi_lttng_writer_close_element(writer
);
1560 ret
= mi_lttng_writer_write_element_bool(writer
,
1561 mi_lttng_element_command_success
, success
);
1567 /* Command element close */
1568 ret
= mi_lttng_writer_command_close(writer
);
1577 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1578 /* Preserve original error code */
1579 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1582 /* Overwrite ret if an error occurred in create_session() */
1583 ret
= command_ret
? command_ret
: ret
;
1585 poptFreeContext(pc
);