2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #include <common/compat/time.hpp>
19 #include <sys/types.h>
22 #include <unordered_map>
24 #include <common/mi-lttng.hpp>
26 #include "../command.hpp"
27 #include "../utils.hpp"
29 #include <common/defaults.hpp>
30 #include <common/sessiond-comm/sessiond-comm.hpp>
31 #include <common/uri.hpp>
32 #include <common/utils.hpp>
33 #include <common/path.hpp>
34 #include <lttng/lttng.h>
36 static char *opt_output_path
;
37 static char *opt_session_name
;
39 static char *opt_ctrl_url
;
40 static char *opt_data_url
;
41 static char *opt_shm_path
;
42 static char *opt_trace_format
;
43 static int opt_no_consumer
;
44 static int opt_no_output
;
45 static int opt_snapshot
;
46 static uint32_t opt_live_timer
;
48 #ifdef LTTNG_EMBED_HELP
49 static const char help_msg
[] =
50 #include <lttng-create.1.h>
67 static struct mi_writer
*writer
;
68 static struct poptOption long_options
[] = {
69 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
70 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
71 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
72 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
73 {"set-url", 'U', POPT_ARG_STRING
, &opt_url
, 0, 0, 0},
74 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
75 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
76 {"no-output", 0, POPT_ARG_VAL
, &opt_no_output
, 1, 0, 0},
77 {"no-consumer", 0, POPT_ARG_VAL
, &opt_no_consumer
, 1, 0, 0},
78 {"snapshot", 0, POPT_ARG_VAL
, &opt_snapshot
, 1, 0, 0},
79 {"live", 0, POPT_ARG_INT
| POPT_ARGFLAG_OPTIONAL
, 0, OPT_LIVE_TIMER
, 0, 0},
80 {"shm-path", 0, POPT_ARG_STRING
, &opt_shm_path
, 0, 0, 0},
81 {"trace-format", 0, POPT_ARG_STRING
, &opt_trace_format
, 0, 0, 0},
86 * Retrieve the created session and mi output it based on provided argument
87 * This is currently a summary of what was pretty printed and is subject to
90 static int mi_created_session(const char *session_name
)
92 int ret
, i
, count
, found
;
93 struct lttng_session
*sessions
;
95 /* session_name should not be null */
96 LTTNG_ASSERT(session_name
);
99 count
= lttng_list_sessions(&sessions
);
102 ERR("%s", lttng_strerror(ret
));
107 ERR("Error session creation failed: session %s not found", session_name
);
108 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
113 for (i
= 0; i
< count
; i
++) {
114 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
116 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
125 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
137 struct lttng_session_descriptor
*create_session_descriptor(void)
140 enum output_type output_type
;
141 struct lttng_uri
*uris
= NULL
;
142 struct lttng_session_descriptor
*descriptor
= NULL
;
143 const char *uri_str1
= NULL
, *uri_str2
= NULL
;
144 char local_output_path
[LTTNG_PATH_MAX
] = {};
147 output_type
= OUTPUT_NONE
;
148 } else if (opt_output_path
) {
149 char *expanded_output_path
;
152 output_type
= OUTPUT_LOCAL
;
153 expanded_output_path
= utils_expand_path(opt_output_path
);
154 if (!expanded_output_path
) {
155 ERR("Failed to expand output path.");
158 ret
= lttng_strncpy(local_output_path
, expanded_output_path
,
159 sizeof(local_output_path
));
160 free(expanded_output_path
);
162 ERR("Output path exceeds the maximal supported length (%zu bytes)",
163 sizeof(local_output_path
));
166 } else if (opt_url
|| opt_ctrl_url
) {
169 uri_str1
= opt_ctrl_url
? opt_ctrl_url
: opt_url
;
170 uri_str2
= opt_data_url
;
172 uri_count
= uri_parse_str_urls(uri_str1
, uri_str2
, &uris
);
173 if (uri_count
!= 1 && uri_count
!= 2) {
174 ERR("Unrecognized URL format.");
180 output_type
= OUTPUT_LOCAL
;
181 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
182 ERR("Unrecognized URL format.");
185 ret
= lttng_strncpy(local_output_path
, uris
[0].dst
.path
,
186 sizeof(local_output_path
));
188 ERR("Output path exceeds the maximal supported length (%zu bytes)",
189 sizeof(local_output_path
));
193 output_type
= OUTPUT_NETWORK
;
196 /* Already checked. */
200 output_type
= OUTPUT_UNSPECIFIED
;
204 /* Snapshot session. */
205 switch (output_type
) {
206 case OUTPUT_UNSPECIFIED
:
208 descriptor
= lttng_session_descriptor_snapshot_local_create(
210 output_type
== OUTPUT_LOCAL
?
211 local_output_path
: NULL
);
214 descriptor
= lttng_session_descriptor_snapshot_create(
218 descriptor
= lttng_session_descriptor_snapshot_network_create(
219 opt_session_name
, uri_str1
, uri_str2
);
224 } else if (opt_live_timer
) {
226 if (output_type
!= OUTPUT_UNSPECIFIED
&&
227 output_type
!= OUTPUT_NETWORK
) {
228 ERR("Unsupported output type specified for live session.");
231 descriptor
= lttng_session_descriptor_live_network_create(
232 opt_session_name
, uri_str1
, uri_str2
,
235 /* Regular session. */
236 switch (output_type
) {
237 case OUTPUT_UNSPECIFIED
:
239 descriptor
= lttng_session_descriptor_local_create(
241 output_type
== OUTPUT_LOCAL
?
242 local_output_path
: NULL
);
245 descriptor
= lttng_session_descriptor_create(
249 descriptor
= lttng_session_descriptor_network_create(
250 opt_session_name
, uri_str1
, uri_str2
);
257 ERR("Failed to initialize session creation command.");
260 * Auto-launch the relay daemon when a live session
261 * is created using default URLs.
263 if (!opt_url
&& !opt_ctrl_url
&& !opt_data_url
&&
264 opt_live_timer
&& !check_relayd()) {
266 const char *pathname
= opt_relayd_path
? :
267 INSTALL_BIN_PATH
"/lttng-relayd";
269 ret
= spawn_relayd(pathname
, 0);
276 if (opt_trace_format
) {
277 std::unordered_map
<std::string
,
278 std::function
<struct lttng_trace_format_descriptor
*()>>
280 {"ctf1", lttng_trace_format_ctf_1_descriptor_create
},
281 {"ctf2", lttng_trace_format_ctf_2_descriptor_create
},
284 lttng_session_descriptor_status status
;
285 lttng_trace_format_descriptor
*trace_format_descriptor
= nullptr;
286 std::string
s_trace_format(opt_trace_format
);
288 auto it
= factory
.find(s_trace_format
);
290 if (it
== factory
.end()) {
291 ERR("Unknown trace format");
295 trace_format_descriptor
= it
->second();
296 if (trace_format_descriptor
== nullptr) {
297 ERR("Failed to create trace format descriptor for trace format %s",
302 status
= lttng_session_descriptor_set_trace_format_descriptor(
303 descriptor
, trace_format_descriptor
);
304 lttng_trace_format_descriptor_destroy(trace_format_descriptor
);
305 if (status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
306 ERR("Failed to set trace format descriptor");
315 lttng_session_descriptor_destroy(descriptor
);
320 static int trace_format_check(struct lttng_session_descriptor
& descriptor
)
323 enum lttng_error_code ret_code
;
324 const lttng_trace_format_descriptor
*trace_format_descriptor
= nullptr;
325 enum lttng_session_descriptor_status status
;
326 std::string destination
;
328 status
= lttng_session_descriptor_get_trace_format_descriptor(
329 &descriptor
, &trace_format_descriptor
);
330 if (status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
331 ret_code
= LTTNG_ERR_INVALID
;
336 * Only check kernel domain since only the kernel tracer can lag behind
337 * in term of feature.
340 ret_code
= lttng_domain_supports_trace_format(lttng_session_daemon_command_endpoint
,
341 LTTNG_DOMAIN_KERNEL
, trace_format_descriptor
);
342 if (ret_code
== LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER
) {
343 WARN("%s", lttng_strerror(ret_code
));
344 } else if (ret_code
== LTTNG_ERR_KERN_NA
|| ret_code
== LTTNG_ERR_NEED_ROOT_SESSIOND
) {
346 * Not an error, kernel tracers is simply not available at that
347 * time. Next operation will fail as necessary.
349 DBG("Kernel tracer not available for trace format support check");
350 } else if (ret_code
!= LTTNG_OK
) {
351 ERR("Failed to validate trace format support: %s", lttng_strerror(ret_code
));
356 if (opt_output_path
) {
357 char *tmp
= utils_expand_path(opt_output_path
);
364 } else if (opt_url
|| opt_ctrl_url
) {
365 destination
= opt_ctrl_url
? opt_ctrl_url
: opt_url
;
366 } else if (opt_live_timer
) {
367 destination
= "tcp://127.0.0.1";
370 if (!destination
.empty()) {
371 ret_code
= lttng_destination_supports_trace_format(
372 lttng_session_daemon_command_endpoint
, destination
.c_str(),
373 trace_format_descriptor
);
374 if (ret_code
== LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_RELAY_DAEMON
) {
375 WARN("%s", lttng_strerror(ret_code
));
376 } else if (ret_code
== LTTNG_ERR_RELAYD_CONNECT_FAIL
) {
377 DBG("Could not validate trace format support: %s",
378 lttng_strerror(ret_code
));
379 } else if (ret_code
!= LTTNG_OK
) {
380 ERR("Failed to validate trace format support: %s",
381 lttng_strerror(ret_code
));
392 * Create a tracing session.
393 * If no name is specified, a default name is generated.
395 * Returns one of the CMD_* result constants.
397 static int create_session(void)
400 char shm_path
[LTTNG_PATH_MAX
] = {};
401 struct lttng_session_descriptor
*session_descriptor
= NULL
;
402 enum lttng_session_descriptor_status descriptor_status
;
403 enum lttng_error_code ret_code
;
404 struct lttng_session
*sessions
= NULL
;
405 const struct lttng_session
*created_session
= NULL
;
406 const char *created_session_name
;
408 /* Validate options. */
409 if (opt_session_name
) {
410 if (strlen(opt_session_name
) > NAME_MAX
) {
411 ERR("Session name too long. Length must be lower or equal to %d",
417 * Check if the session name begins with "auto-" or is exactly "auto".
418 * Both are reserved for the default session name. See bug #449 to
419 * understand why we need to check both here.
421 if ((strncmp(opt_session_name
, DEFAULT_SESSION_NAME
"-",
422 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) ||
423 (strncmp(opt_session_name
, DEFAULT_SESSION_NAME
,
424 strlen(DEFAULT_SESSION_NAME
)) == 0 &&
425 strlen(opt_session_name
) == strlen(DEFAULT_SESSION_NAME
))) {
426 ERR("%s is a reserved keyword for default session(s)",
427 DEFAULT_SESSION_NAME
);
433 if (opt_snapshot
&& opt_live_timer
) {
434 ERR("Snapshot and live modes are mutually exclusive.");
439 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
440 ERR("Both control and data URLs must be specified.");
445 session_descriptor
= create_session_descriptor();
446 if (!session_descriptor
) {
451 ret_code
= lttng_create_session_ext(session_descriptor
);
452 if (ret_code
!= LTTNG_OK
) {
453 ERR("%s", lttng_strerror(-ret_code
));
458 ret
= trace_format_check(*session_descriptor
);
464 descriptor_status
= lttng_session_descriptor_get_session_name(
465 session_descriptor
, &created_session_name
);
466 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
467 ERR("Failed to obtain created session name");
472 ret
= lttng_list_sessions(&sessions
);
474 ERR("Failed to fetch properties of created session: %s",
475 lttng_strerror(ret
));
479 for (i
= 0; i
< ret
; i
++) {
480 if (!strcmp(created_session_name
, sessions
[i
].name
)) {
481 created_session
= &sessions
[i
];
485 if (!created_session
) {
486 ERR("Failed to fetch properties of created session");
492 char datetime_suffix
[17] = {};
495 * An auto-generated session name already includes the creation
498 if (opt_session_name
) {
499 uint64_t creation_time
;
501 time_t creation_time_t
;
504 ret_code
= lttng_session_get_creation_time(
507 if (ret_code
!= LTTNG_OK
) {
508 ERR("%s", lttng_strerror(-ret_code
));
512 creation_time_t
= (time_t) creation_time
;
513 timeinfo
= localtime(&creation_time_t
);
515 PERROR("Failed to interpret session creation time");
519 strftime_ret
= strftime(datetime_suffix
,
520 sizeof(datetime_suffix
),
521 "-%Y%m%d-%H%M%S", timeinfo
);
522 if (strftime_ret
== 0) {
523 ERR("Failed to format session creation time.");
529 ret
= snprintf(shm_path
, sizeof(shm_path
),
530 "%s/%s%s", opt_shm_path
, created_session_name
,
532 if (ret
< 0 || ret
>= sizeof(shm_path
)) {
533 ERR("Failed to format the shared memory path.");
537 ret
= lttng_set_session_shm_path(created_session_name
,
540 lttng_destroy_session(created_session_name
);
547 MSG("Snapshot session %s created.", created_session_name
);
548 } else if (opt_live_timer
) {
549 MSG("Live session %s created.", created_session_name
);
551 MSG("Session %s created.", created_session_name
);
554 if (*created_session
->path
&& !opt_snapshot
) {
555 MSG("Traces will be output to %s", created_session
->path
);
557 if (opt_live_timer
) {
558 MSG("Live timer interval set to %u %s", opt_live_timer
,
561 } else if (opt_snapshot
) {
562 struct lttng_snapshot_output_list
*list
;
563 struct lttng_snapshot_output
*iter
;
564 char snapshot_url
[LTTNG_PATH_MAX
] = {};
566 ret
= lttng_snapshot_list_output(created_session_name
, &list
);
568 ERR("Failed to list snapshot outputs.");
573 while ((iter
= lttng_snapshot_output_list_get_next(list
))) {
574 const char *url
= NULL
;
576 url
= lttng_snapshot_output_get_ctrl_url(
578 ret
= lttng_strncpy(snapshot_url
, url
,
579 sizeof(snapshot_url
));
581 snapshot_url
[0] = '\0';
582 ERR("Failed to retrieve snapshot output destination");
586 lttng_snapshot_output_list_destroy(list
);
589 MSG("Default snapshot output set to %s",
592 MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode.");
595 MSG("Shared memory path set to %s", shm_path
);
600 ret
= mi_created_session(created_session_name
);
607 /* Init lttng session config */
608 ret
= config_init(created_session_name
);
616 lttng_session_descriptor_destroy(session_descriptor
);
624 * Spawn a session daemon by forking and execv.
626 static int spawn_sessiond(const char *pathname
)
631 MSG("Spawning a session daemon");
635 * Spawn session daemon in daemon mode.
637 execlp(pathname
, "lttng-sessiond",
638 "--daemonize", NULL
);
639 /* execlp only returns if error happened */
640 if (errno
== ENOENT
) {
641 ERR("No session daemon found. Use --sessiond-path.");
645 kill(getppid(), SIGTERM
); /* wake parent */
647 } else if (pid
> 0) {
649 * In daemon mode (--daemonize), sessiond only exits when
650 * it's ready to accept commands.
654 pid_t wait_pid_ret
= waitpid(pid
, &status
, 0);
656 if (wait_pid_ret
< 0) {
657 if (errno
== EINTR
) {
665 if (WIFSIGNALED(status
)) {
666 ERR("Session daemon was killed by signal %d",
670 } else if (WIFEXITED(status
)) {
671 DBG("Session daemon terminated normally (exit status: %d)",
672 WEXITSTATUS(status
));
674 if (WEXITSTATUS(status
) != 0) {
675 ERR("Session daemon terminated with an error (exit status: %d)",
676 WEXITSTATUS(status
));
698 * Check if the session daemon is available using
699 * the liblttngctl API for the check. If not, try to
702 static int launch_sessiond(void)
705 const char *pathname
= NULL
;
707 ret
= lttng_session_daemon_alive();
709 /* Sessiond is alive, not an error */
714 /* Try command line option path */
715 pathname
= opt_sessiond_path
;
717 /* Try LTTNG_SESSIOND_PATH env variable */
718 if (pathname
== NULL
) {
719 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
722 /* Try with configured path */
723 if (pathname
== NULL
) {
724 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
725 pathname
= CONFIG_SESSIOND_BIN
;
729 /* Try the default path */
730 if (pathname
== NULL
) {
731 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
734 DBG("Session daemon binary path: %s", pathname
);
736 /* Check existence and permissions */
737 ret
= access(pathname
, F_OK
| X_OK
);
739 ERR("No such file or access denied: %s", pathname
);
743 ret
= spawn_sessiond(pathname
);
746 ERR("Problem occurred while launching session daemon (%s)",
753 int validate_url_option_combination(void)
758 used_count
+= !!opt_url
;
759 used_count
+= !!opt_output_path
;
760 used_count
+= (opt_data_url
|| opt_ctrl_url
);
761 if (used_count
> 1) {
762 ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once.");
770 * The 'create <options>' first level command
772 * Returns one of the CMD_* result constants.
774 int cmd_create(int argc
, const char **argv
)
776 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
777 char *opt_arg
= NULL
;
778 const char *leftover
= NULL
;
779 static poptContext pc
;
781 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
782 poptReadDefaultConfig(pc
, 0);
784 while ((opt
= poptGetNextOpt(pc
)) != -1) {
789 case OPT_LIST_OPTIONS
:
790 list_cmd_options(stdout
, long_options
);
802 opt_arg
= poptGetOptArg(pc
);
804 /* Set up default values. */
805 opt_live_timer
= (uint32_t) DEFAULT_LTTNG_LIVE_TIMER
;
806 DBG("Session live timer interval set to default value %d",
811 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
812 ERR("Wrong value for --live parameter: %s", opt_arg
);
817 if (v
!= (uint32_t) v
) {
818 ERR("32-bit overflow in --live parameter: %s", opt_arg
);
824 ERR("Live timer interval must be greater than zero");
829 opt_live_timer
= (uint32_t) v
;
830 DBG("Session live timer interval set to %d", opt_live_timer
);
839 if (opt_no_consumer
) {
840 MSG("The option --no-consumer is obsolete. Use --no-output now.");
845 ret
= validate_url_option_combination();
851 /* Spawn a session daemon if needed */
852 if (!opt_no_sessiond
) {
853 ret
= launch_sessiond();
860 /* MI initialization */
862 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
864 ret
= -LTTNG_ERR_NOMEM
;
868 /* Open command element */
869 ret
= mi_lttng_writer_command_open(writer
,
870 mi_lttng_element_command_create
);
876 /* Open output element */
877 ret
= mi_lttng_writer_open_element(writer
,
878 mi_lttng_element_command_output
);
884 opt_session_name
= (char*) poptGetArg(pc
);
886 leftover
= poptGetArg(pc
);
888 ERR("Unknown argument: %s", leftover
);
893 command_ret
= create_session();
899 /* Close output element */
900 ret
= mi_lttng_writer_close_element(writer
);
907 ret
= mi_lttng_writer_write_element_bool(writer
,
908 mi_lttng_element_command_success
, success
);
914 /* Command element close */
915 ret
= mi_lttng_writer_command_close(writer
);
924 if (writer
&& mi_lttng_writer_destroy(writer
)) {
925 /* Preserve original error code */
926 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
929 /* Overwrite ret if an error occurred in create_session() */
930 ret
= command_ret
? command_ret
: ret
;