4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 EfficiOS Inc.
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
23 #include <common/common.h>
24 #include <common/compat/string.h>
25 #include <common/defaults.h>
26 #include <common/dynamic-buffer.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/tracker.h>
29 #include <common/uri.h>
30 #include <common/utils.h>
31 #include <lttng/channel-internal.h>
32 #include <lttng/destruction-handle.h>
33 #include <lttng/endpoint.h>
34 #include <lttng/event-internal.h>
35 #include <lttng/health-internal.h>
36 #include <lttng/lttng.h>
37 #include <lttng/session-descriptor-internal.h>
38 #include <lttng/session-internal.h>
39 #include <lttng/trigger/trigger-internal.h>
40 #include <lttng/userspace-probe-internal.h>
42 #include "filter/filter-ast.h"
43 #include "filter/filter-parser.h"
44 #include "filter/filter-bytecode.h"
45 #include "filter/memstream.h"
46 #include "lttng-ctl-helper.h"
49 static const int print_xml
= 1;
50 #define dbg_printf(fmt, args...) \
51 printf("[debug liblttng-ctl] " fmt, ## args)
53 static const int print_xml
= 0;
54 #define dbg_printf(fmt, args...) \
56 /* do nothing but check printf format */ \
58 printf("[debug liblttnctl] " fmt, ## args); \
62 #define COPY_DOMAIN_PACKED(dst, src) \
64 struct lttng_domain _tmp_domain; \
66 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
70 /* Socket to session daemon for communication */
71 static int sessiond_socket
= -1;
72 static char sessiond_sock_path
[PATH_MAX
];
75 static char *tracing_group
;
81 * Those two variables are used by error.h to silent or control the verbosity of
82 * error message. They are global to the library so application linking with it
83 * are able to compile correctly and also control verbosity of the library.
86 int lttng_opt_verbose
;
90 * Copy domain to lttcomm_session_msg domain.
92 * If domain is unknown, default domain will be the kernel.
95 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
96 struct lttng_domain
*src
)
100 case LTTNG_DOMAIN_KERNEL
:
101 case LTTNG_DOMAIN_UST
:
102 case LTTNG_DOMAIN_JUL
:
103 case LTTNG_DOMAIN_LOG4J
:
104 case LTTNG_DOMAIN_PYTHON
:
105 memcpy(dst
, src
, sizeof(struct lttng_domain
));
108 memset(dst
, 0, sizeof(struct lttng_domain
));
115 * Send lttcomm_session_msg to the session daemon.
117 * On success, returns the number of bytes sent (>=0)
118 * On error, returns -1
120 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
125 ret
= -LTTNG_ERR_NO_SESSIOND
;
129 DBG("LSM cmd type : %d", lsm
->cmd_type
);
131 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
132 sizeof(struct lttcomm_session_msg
));
134 ret
= -LTTNG_ERR_FATAL
;
142 * Send var len data to the session daemon.
144 * On success, returns the number of bytes sent (>=0)
145 * On error, returns -1
147 static int send_session_varlen(const void *data
, size_t len
)
152 ret
= -LTTNG_ERR_NO_SESSIOND
;
161 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
163 ret
= -LTTNG_ERR_FATAL
;
171 * Send file descriptors to the session daemon.
173 * On success, returns the number of bytes sent (>=0)
174 * On error, returns -1
176 static int send_session_fds(const int *fds
, size_t nb_fd
)
181 ret
= -LTTNG_ERR_NO_SESSIOND
;
185 if (!fds
|| !nb_fd
) {
190 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
192 ret
= -LTTNG_ERR_FATAL
;
200 * Receive data from the sessiond socket.
202 * On success, returns the number of bytes received (>=0)
203 * On error, returns -1 (recvmsg() error) or -ENOTCONN
205 static int recv_data_sessiond(void *buf
, size_t len
)
210 ret
= -LTTNG_ERR_NO_SESSIOND
;
214 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
216 ret
= -LTTNG_ERR_FATAL
;
224 * Check if we are in the specified group.
226 * If yes return 1, else return -1.
229 int lttng_check_tracing_group(void)
231 gid_t
*grp_list
, tracing_gid
;
232 int grp_list_size
, grp_id
, i
;
234 const char *grp_name
= tracing_group
;
236 /* Get GID of group 'tracing' */
237 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
238 /* If grp_tracing is NULL, the group does not exist. */
242 /* Get number of supplementary group IDs */
243 grp_list_size
= getgroups(0, NULL
);
244 if (grp_list_size
< 0) {
249 /* Alloc group list of the right size */
250 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
255 grp_id
= getgroups(grp_list_size
, grp_list
);
261 for (i
= 0; i
< grp_list_size
; i
++) {
262 if (grp_list
[i
] == tracing_gid
) {
275 static enum lttng_error_code
check_enough_available_memory(
276 uint64_t num_bytes_requested_per_cpu
)
280 uint64_t best_mem_info
;
281 uint64_t num_bytes_requested_total
;
284 * Get the number of CPU currently online to compute the amount of
285 * memory needed to create a buffer for every CPU.
287 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
289 ret
= LTTNG_ERR_FATAL
;
293 if (num_bytes_requested_per_cpu
> UINT64_MAX
/ (uint64_t) num_cpu
) {
295 ret
= LTTNG_ERR_OVERFLOW
;
299 num_bytes_requested_total
=
300 num_bytes_requested_per_cpu
* (uint64_t) num_cpu
;
303 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
304 * reliable estimate we can get but it is only exposed by the kernel
305 * since 3.14. (See Linux kernel commit:
306 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
308 ret
= utils_get_memory_available(&best_mem_info
);
314 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
315 * is a sanity check for obvious user error.
317 ret
= utils_get_memory_total(&best_mem_info
);
322 /* No valid source of information. */
323 ret
= LTTNG_ERR_NOMEM
;
327 if (best_mem_info
>= num_bytes_requested_total
) {
330 ret
= LTTNG_ERR_NOMEM
;
337 * Try connect to session daemon with sock_path.
339 * Return 0 on success, else -1
341 static int try_connect_sessiond(const char *sock_path
)
345 /* If socket exist, we check if the daemon listens for connect. */
346 ret
= access(sock_path
, F_OK
);
352 ret
= lttcomm_connect_unix_sock(sock_path
);
358 ret
= lttcomm_close_unix_sock(ret
);
360 PERROR("lttcomm_close_unix_sock");
370 * Set sessiond socket path by putting it in the global sessiond_sock_path
373 * Returns 0 on success, negative value on failure (the sessiond socket path
374 * is somehow too long or ENOMEM).
376 static int set_session_daemon_path(void)
378 int in_tgroup
= 0; /* In tracing group. */
384 /* Are we in the tracing group ? */
385 in_tgroup
= lttng_check_tracing_group();
388 if ((uid
== 0) || in_tgroup
== 1) {
389 const int ret
= lttng_strncpy(sessiond_sock_path
,
390 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
391 sizeof(sessiond_sock_path
));
403 ret
= try_connect_sessiond(sessiond_sock_path
);
407 /* Global session daemon not available... */
409 /* ...or not in tracing group (and not root), default */
412 * With GNU C < 2.1, snprintf returns -1 if the target buffer
414 * With GNU C >= 2.1, snprintf returns the required size
415 * (excluding closing null)
417 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
418 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
419 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
431 * Connect to the LTTng session daemon.
433 * On success, return the socket's file descriptor. On error, return -1.
435 LTTNG_HIDDEN
int connect_sessiond(void)
439 ret
= set_session_daemon_path();
444 /* Connect to the sesssion daemon. */
445 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
456 static void reset_global_sessiond_connection_state(void)
458 sessiond_socket
= -1;
463 * Clean disconnect from the session daemon.
465 * On success, return 0. On error, return -1.
467 static int disconnect_sessiond(void)
472 ret
= lttcomm_close_unix_sock(sessiond_socket
);
473 reset_global_sessiond_connection_state();
479 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
487 ret
= -LTTNG_ERR_INVALID
;
497 ret
= recv_data_sessiond(buf
, len
);
503 ret
= -LTTNG_ERR_INVALID
;
507 /* Move ownership of command header buffer to user. */
512 /* No command header. */
528 * Ask the session daemon a specific command and put the data into buf.
529 * Takes extra var. len. data and file descriptors as input to send to the
532 * Return size of data (only payload, not header) or a negative error code.
535 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
536 const int *fds
, size_t nb_fd
, const void *vardata
,
537 size_t vardata_len
, void **user_payload_buf
,
538 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
542 struct lttcomm_lttng_msg llm
;
544 ret
= connect_sessiond();
546 ret
= -LTTNG_ERR_NO_SESSIOND
;
549 sessiond_socket
= ret
;
553 /* Send command to session daemon */
554 ret
= send_session_msg(lsm
);
556 /* Ret value is a valid lttng error code. */
559 /* Send var len data */
560 ret
= send_session_varlen(vardata
, vardata_len
);
562 /* Ret value is a valid lttng error code. */
567 ret
= send_session_fds(fds
, nb_fd
);
569 /* Ret value is a valid lttng error code. */
573 /* Get header from data transmission */
574 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
576 /* Ret value is a valid lttng error code. */
580 /* Check error code if OK */
581 if (llm
.ret_code
!= LTTNG_OK
) {
586 /* Get command header from data transmission */
587 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
588 user_cmd_header_buf
, user_cmd_header_len
);
593 /* Get payload from data transmission */
594 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
603 disconnect_sessiond();
608 * Create lttng handle and return pointer.
610 * The returned pointer will be NULL in case of malloc() error.
612 struct lttng_handle
*lttng_create_handle(const char *session_name
,
613 struct lttng_domain
*domain
)
616 struct lttng_handle
*handle
= NULL
;
618 handle
= zmalloc(sizeof(struct lttng_handle
));
619 if (handle
== NULL
) {
620 PERROR("malloc handle");
624 /* Copy session name */
625 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
626 sizeof(handle
->session_name
));
631 /* Copy lttng domain or leave initialized to 0. */
633 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
644 * Destroy handle by free(3) the pointer.
646 void lttng_destroy_handle(struct lttng_handle
*handle
)
652 * Register an outside consumer.
654 * Returns size of returned session payload data or a negative error code.
656 int lttng_register_consumer(struct lttng_handle
*handle
,
657 const char *socket_path
)
660 struct lttcomm_session_msg lsm
;
662 if (handle
== NULL
|| socket_path
== NULL
) {
663 ret
= -LTTNG_ERR_INVALID
;
667 memset(&lsm
, 0, sizeof(lsm
));
668 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
669 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
670 sizeof(lsm
.session
.name
));
672 ret
= -LTTNG_ERR_INVALID
;
676 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
678 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
679 sizeof(lsm
.u
.reg
.path
));
681 ret
= -LTTNG_ERR_INVALID
;
685 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
691 * Start tracing for all traces of the session.
693 * Returns size of returned session payload data or a negative error code.
695 int lttng_start_tracing(const char *session_name
)
698 struct lttcomm_session_msg lsm
;
700 if (session_name
== NULL
) {
701 ret
= -LTTNG_ERR_INVALID
;
705 memset(&lsm
, 0, sizeof(lsm
));
706 lsm
.cmd_type
= LTTNG_START_TRACE
;
708 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
709 sizeof(lsm
.session
.name
));
711 ret
= -LTTNG_ERR_INVALID
;
715 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
721 * Stop tracing for all traces of the session.
723 static int _lttng_stop_tracing(const char *session_name
, int wait
)
726 struct lttcomm_session_msg lsm
;
728 if (session_name
== NULL
) {
729 ret
= -LTTNG_ERR_INVALID
;
733 memset(&lsm
, 0, sizeof(lsm
));
734 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
736 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
737 sizeof(lsm
.session
.name
));
739 ret
= -LTTNG_ERR_INVALID
;
743 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
744 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
752 /* Check for data availability */
754 data_ret
= lttng_data_pending(session_name
);
756 /* Return the data available call error. */
762 * Data sleep time before retrying (in usec). Don't sleep if the
763 * call returned value indicates availability.
766 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
768 } while (data_ret
!= 0);
776 * Stop tracing and wait for data availability.
778 int lttng_stop_tracing(const char *session_name
)
780 return _lttng_stop_tracing(session_name
, 1);
784 * Stop tracing but _don't_ wait for data availability.
786 int lttng_stop_tracing_no_wait(const char *session_name
)
788 return _lttng_stop_tracing(session_name
, 0);
792 * Add context to a channel.
794 * If the given channel is NULL, add the contexts to all channels.
795 * The event_name param is ignored.
797 * Returns the size of the returned payload data or a negative error code.
799 int lttng_add_context(struct lttng_handle
*handle
,
800 struct lttng_event_context
*ctx
, const char *event_name
,
801 const char *channel_name
)
806 struct lttcomm_session_msg lsm
;
808 /* Safety check. Both are mandatory. */
809 if (handle
== NULL
|| ctx
== NULL
) {
810 ret
= -LTTNG_ERR_INVALID
;
814 memset(&lsm
, 0, sizeof(lsm
));
815 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
817 /* If no channel name, send empty string. */
818 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
819 sizeof(lsm
.u
.context
.channel_name
));
821 ret
= -LTTNG_ERR_INVALID
;
825 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
826 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
827 sizeof(lsm
.session
.name
));
829 ret
= -LTTNG_ERR_INVALID
;
833 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
834 size_t provider_len
, ctx_len
;
835 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
836 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
838 if (!provider_name
|| !ctx_name
) {
839 ret
= -LTTNG_ERR_INVALID
;
843 provider_len
= strlen(provider_name
);
844 if (provider_len
== 0) {
845 ret
= -LTTNG_ERR_INVALID
;
848 lsm
.u
.context
.provider_name_len
= provider_len
;
850 ctx_len
= strlen(ctx_name
);
852 ret
= -LTTNG_ERR_INVALID
;
855 lsm
.u
.context
.context_name_len
= ctx_len
;
857 len
= provider_len
+ ctx_len
;
860 ret
= -LTTNG_ERR_NOMEM
;
864 memcpy(buf
, provider_name
, provider_len
);
865 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
867 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
869 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
871 * Don't leak application addresses to the sessiond.
872 * This is only necessary when ctx is for an app ctx otherwise
873 * the values inside the union (type & config) are overwritten.
875 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
876 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
879 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
886 * Enable event(s) for a channel.
888 * If no event name is specified, all events are enabled.
889 * If no channel name is specified, the default 'channel0' is used.
891 * Returns size of returned session payload data or a negative error code.
893 int lttng_enable_event(struct lttng_handle
*handle
,
894 struct lttng_event
*ev
, const char *channel_name
)
896 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
901 * Create or enable an event with a filter expression.
903 * Return negative error value on error.
904 * Return size of returned session payload data if OK.
906 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
907 struct lttng_event
*event
, const char *channel_name
,
908 const char *filter_expression
)
910 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
911 filter_expression
, 0, NULL
);
915 * Depending on the event, return a newly allocated agent filter expression or
916 * NULL if not applicable.
918 * An event with NO loglevel and the name is * will return NULL.
920 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
923 char *agent_filter
= NULL
;
927 /* Don't add filter for the '*' event. */
928 if (strcmp(ev
->name
, "*") != 0) {
930 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
933 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
941 /* Add loglevel filtering if any for the JUL domain. */
942 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
945 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
951 if (filter
|| agent_filter
) {
954 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
955 agent_filter
? agent_filter
: filter
, op
,
960 agent_filter
= new_filter
;
962 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
978 * Generate the filter bytecode from a given filter expression string. Put the
979 * newly allocated parser context in ctxp and populate the lsm object with the
982 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
984 static int generate_filter(char *filter_expression
,
985 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
988 struct filter_parser_ctx
*ctx
= NULL
;
991 assert(filter_expression
);
996 * Casting const to non-const, as the underlying function will use it in
999 fmem
= lttng_fmemopen((void *) filter_expression
,
1000 strlen(filter_expression
), "r");
1002 fprintf(stderr
, "Error opening memory as stream\n");
1003 ret
= -LTTNG_ERR_FILTER_NOMEM
;
1006 ctx
= filter_parser_ctx_alloc(fmem
);
1008 fprintf(stderr
, "Error allocating parser\n");
1009 ret
= -LTTNG_ERR_FILTER_NOMEM
;
1010 goto filter_alloc_error
;
1012 ret
= filter_parser_ctx_append_ast(ctx
);
1014 fprintf(stderr
, "Parse error\n");
1015 ret
= -LTTNG_ERR_FILTER_INVAL
;
1019 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
1022 fprintf(stderr
, "XML print error\n");
1023 ret
= -LTTNG_ERR_FILTER_INVAL
;
1028 dbg_printf("Generating IR... ");
1030 ret
= filter_visitor_ir_generate(ctx
);
1032 fprintf(stderr
, "Generate IR error\n");
1033 ret
= -LTTNG_ERR_FILTER_INVAL
;
1036 dbg_printf("done\n");
1038 dbg_printf("Validating IR... ");
1040 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
1042 ret
= -LTTNG_ERR_FILTER_INVAL
;
1046 /* Normalize globbing patterns in the expression. */
1047 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1049 ret
= -LTTNG_ERR_FILTER_INVAL
;
1053 /* Validate strings used as literals in the expression. */
1054 ret
= filter_visitor_ir_validate_string(ctx
);
1056 ret
= -LTTNG_ERR_FILTER_INVAL
;
1060 /* Validate globbing patterns in the expression. */
1061 ret
= filter_visitor_ir_validate_globbing(ctx
);
1063 ret
= -LTTNG_ERR_FILTER_INVAL
;
1067 dbg_printf("done\n");
1069 dbg_printf("Generating bytecode... ");
1071 ret
= filter_visitor_bytecode_generate(ctx
);
1073 fprintf(stderr
, "Generate bytecode error\n");
1074 ret
= -LTTNG_ERR_FILTER_INVAL
;
1077 dbg_printf("done\n");
1078 dbg_printf("Size of bytecode generated: %u bytes.\n",
1079 bytecode_get_len(&ctx
->bytecode
->b
));
1081 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1082 + bytecode_get_len(&ctx
->bytecode
->b
);
1083 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1085 /* No need to keep the memory stream. */
1086 if (fclose(fmem
) != 0) {
1094 filter_ir_free(ctx
);
1095 filter_parser_ctx_free(ctx
);
1097 if (fclose(fmem
) != 0) {
1105 * Enable event(s) for a channel, possibly with exclusions and a filter.
1106 * If no event name is specified, all events are enabled.
1107 * If no channel name is specified, the default name is used.
1108 * If filter expression is not NULL, the filter is set for the event.
1109 * If exclusion count is not zero, the exclusions are set for the event.
1110 * Returns size of returned session payload data or a negative error code.
1112 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1113 struct lttng_event
*ev
, const char *channel_name
,
1114 const char *original_filter_expression
,
1115 int exclusion_count
, char **exclusion_list
)
1117 struct lttcomm_session_msg lsm
;
1118 struct lttng_dynamic_buffer send_buffer
;
1119 int ret
= 0, i
, fd_to_send
= -1;
1120 bool send_fd
= false;
1121 unsigned int free_filter_expression
= 0;
1122 struct filter_parser_ctx
*ctx
= NULL
;
1125 * We have either a filter or some exclusions, so we need to set up
1126 * a variable-length memory block from where to send the data.
1128 lttng_dynamic_buffer_init(&send_buffer
);
1131 * Cast as non-const since we may replace the filter expression
1132 * by a dynamically allocated string. Otherwise, the original
1133 * string is not modified.
1135 char *filter_expression
= (char *) original_filter_expression
;
1137 if (handle
== NULL
|| ev
== NULL
) {
1138 ret
= -LTTNG_ERR_INVALID
;
1143 * Empty filter string will always be rejected by the parser
1144 * anyway, so treat this corner-case early to eliminate
1145 * lttng_fmemopen error for 0-byte allocation.
1147 if (filter_expression
&& filter_expression
[0] == '\0') {
1148 ret
= -LTTNG_ERR_INVALID
;
1152 memset(&lsm
, 0, sizeof(lsm
));
1154 /* If no channel name, send empty string. */
1155 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1156 sizeof(lsm
.u
.enable
.channel_name
));
1158 ret
= -LTTNG_ERR_INVALID
;
1162 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1163 if (ev
->name
[0] == '\0') {
1164 /* Enable all events. */
1165 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1169 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1170 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1172 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1173 sizeof(lsm
.session
.name
));
1175 ret
= -LTTNG_ERR_INVALID
;
1179 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1180 lsm
.u
.enable
.bytecode_len
= 0;
1182 /* Parse filter expression. */
1183 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1184 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1185 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1186 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1187 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1188 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1191 /* Setup JUL filter if needed. */
1192 agent_filter
= set_agent_filter(filter_expression
, ev
);
1193 if (!agent_filter
) {
1194 if (!filter_expression
) {
1196 * No JUL and no filter, just skip
1203 * With an agent filter, the original filter has
1204 * been added to it thus replace the filter
1207 filter_expression
= agent_filter
;
1208 free_filter_expression
= 1;
1212 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1218 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1219 lsm
.u
.enable
.bytecode_len
1220 + lsm
.u
.enable
.expression_len
1221 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1223 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1227 /* Put exclusion names first in the data. */
1228 for (i
= 0; i
< exclusion_count
; i
++) {
1229 size_t exclusion_len
;
1231 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1232 LTTNG_SYMBOL_NAME_LEN
);
1233 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1234 /* Exclusion is not NULL-terminated. */
1235 ret
= -LTTNG_ERR_INVALID
;
1239 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1240 *(exclusion_list
+ i
),
1241 LTTNG_SYMBOL_NAME_LEN
);
1247 /* Add filter expression next. */
1248 if (filter_expression
) {
1249 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1250 filter_expression
, lsm
.u
.enable
.expression_len
);
1255 /* Add filter bytecode next. */
1256 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1257 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1258 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1263 if (ev
->extended
.ptr
) {
1264 struct lttng_event_extended
*ev_ext
=
1265 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1267 if (ev_ext
->probe_location
) {
1269 * lttng_userspace_probe_location_serialize returns the
1270 * number of bytes that was appended to the buffer.
1272 ret
= lttng_userspace_probe_location_serialize(
1273 ev_ext
->probe_location
, &send_buffer
,
1279 send_fd
= fd_to_send
>= 0;
1281 * Set the size of the userspace probe location element
1282 * of the buffer so that the receiving side knows where
1285 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1289 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1290 send_fd
? &fd_to_send
: NULL
,
1292 send_buffer
.size
? send_buffer
.data
: NULL
,
1293 send_buffer
.size
, NULL
, NULL
, 0);
1296 if (filter_expression
&& ctx
) {
1297 filter_bytecode_free(ctx
);
1298 filter_ir_free(ctx
);
1299 filter_parser_ctx_free(ctx
);
1302 if (free_filter_expression
) {
1304 * The filter expression has been replaced and must be freed as
1305 * it is not the original filter expression received as a
1308 free(filter_expression
);
1312 * Return directly to the caller and don't ask the sessiond since
1313 * something went wrong in the parsing of data above.
1315 lttng_dynamic_buffer_reset(&send_buffer
);
1319 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1323 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1324 struct lttng_event
*ev
, const char *channel_name
,
1325 const char *original_filter_expression
)
1327 struct lttcomm_session_msg lsm
;
1330 unsigned int free_filter_expression
= 0;
1331 struct filter_parser_ctx
*ctx
= NULL
;
1333 * Cast as non-const since we may replace the filter expression
1334 * by a dynamically allocated string. Otherwise, the original
1335 * string is not modified.
1337 char *filter_expression
= (char *) original_filter_expression
;
1339 if (handle
== NULL
|| ev
== NULL
) {
1340 ret
= -LTTNG_ERR_INVALID
;
1345 * Empty filter string will always be rejected by the parser
1346 * anyway, so treat this corner-case early to eliminate
1347 * lttng_fmemopen error for 0-byte allocation.
1349 if (filter_expression
&& filter_expression
[0] == '\0') {
1350 ret
= -LTTNG_ERR_INVALID
;
1354 memset(&lsm
, 0, sizeof(lsm
));
1356 /* If no channel name, send empty string. */
1357 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1358 sizeof(lsm
.u
.disable
.channel_name
));
1360 ret
= -LTTNG_ERR_INVALID
;
1364 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1366 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1367 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1369 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1370 sizeof(lsm
.session
.name
));
1372 ret
= -LTTNG_ERR_INVALID
;
1376 lsm
.u
.disable
.bytecode_len
= 0;
1379 * For the JUL domain, a filter is enforced except for the
1380 * disable all event. This is done to avoid having the event in
1381 * all sessions thus filtering by logger name.
1383 if (filter_expression
== NULL
&&
1384 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1385 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1386 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1391 * We have a filter, so we need to set up a variable-length
1392 * memory block from where to send the data.
1395 /* Parse filter expression */
1396 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1397 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1398 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1399 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1400 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1401 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1404 /* Setup JUL filter if needed. */
1405 agent_filter
= set_agent_filter(filter_expression
, ev
);
1406 if (!agent_filter
) {
1407 if (!filter_expression
) {
1409 * No JUL and no filter, just skip
1416 * With a JUL filter, the original filter has
1417 * been added to it thus replace the filter
1420 filter_expression
= agent_filter
;
1421 free_filter_expression
= 1;
1425 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1431 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1432 + lsm
.u
.disable
.expression_len
);
1434 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1438 /* Add filter expression. */
1439 if (lsm
.u
.disable
.expression_len
!= 0) {
1442 lsm
.u
.disable
.expression_len
);
1444 /* Add filter bytecode next. */
1445 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1447 + lsm
.u
.disable
.expression_len
,
1449 lsm
.u
.disable
.bytecode_len
);
1452 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1453 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1457 if (filter_expression
&& ctx
) {
1458 filter_bytecode_free(ctx
);
1459 filter_ir_free(ctx
);
1460 filter_parser_ctx_free(ctx
);
1463 if (free_filter_expression
) {
1465 * The filter expression has been replaced and must be freed as
1466 * it is not the original filter expression received as a
1469 free(filter_expression
);
1473 * Return directly to the caller and don't ask the sessiond since
1474 * something went wrong in the parsing of data above.
1479 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1484 * Disable event(s) of a channel and domain.
1485 * If no event name is specified, all events are disabled.
1486 * If no channel name is specified, the default 'channel0' is used.
1487 * Returns size of returned session payload data or a negative error code.
1489 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1490 const char *channel_name
)
1493 struct lttng_event ev
;
1495 memset(&ev
, 0, sizeof(ev
));
1497 ev
.type
= LTTNG_EVENT_ALL
;
1498 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1500 ret
= -LTTNG_ERR_INVALID
;
1504 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1509 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1511 struct lttng_channel
*channel
= NULL
;
1512 struct lttng_channel_extended
*extended
= NULL
;
1518 /* Validate domain. */
1519 switch (domain
->type
) {
1520 case LTTNG_DOMAIN_UST
:
1521 switch (domain
->buf_type
) {
1522 case LTTNG_BUFFER_PER_UID
:
1523 case LTTNG_BUFFER_PER_PID
:
1529 case LTTNG_DOMAIN_KERNEL
:
1530 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1538 channel
= zmalloc(sizeof(*channel
));
1543 extended
= zmalloc(sizeof(*extended
));
1548 channel
->attr
.extended
.ptr
= extended
;
1550 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1558 void lttng_channel_destroy(struct lttng_channel
*channel
)
1564 if (channel
->attr
.extended
.ptr
) {
1565 free(channel
->attr
.extended
.ptr
);
1571 * Enable channel per domain
1572 * Returns size of returned session payload data or a negative error code.
1574 int lttng_enable_channel(struct lttng_handle
*handle
,
1575 struct lttng_channel
*in_chan
)
1577 enum lttng_error_code ret_code
;
1579 struct lttcomm_session_msg lsm
;
1580 uint64_t total_buffer_size_needed_per_cpu
= 0;
1582 /* NULL arguments are forbidden. No default values. */
1583 if (handle
== NULL
|| in_chan
== NULL
) {
1584 return -LTTNG_ERR_INVALID
;
1587 memset(&lsm
, 0, sizeof(lsm
));
1588 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1589 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1591 if (!in_chan
->attr
.extended
.ptr
) {
1592 struct lttng_channel
*channel
;
1593 struct lttng_channel_extended
*extended
;
1595 channel
= lttng_channel_create(&handle
->domain
);
1597 return -LTTNG_ERR_NOMEM
;
1601 * Create a new channel in order to use default extended
1604 extended
= (struct lttng_channel_extended
*)
1605 channel
->attr
.extended
.ptr
;
1606 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1607 lttng_channel_destroy(channel
);
1609 struct lttng_channel_extended
*extended
;
1611 extended
= (struct lttng_channel_extended
*)
1612 in_chan
->attr
.extended
.ptr
;
1613 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1617 * Verify that the amount of memory required to create the requested
1618 * buffer is available on the system at the moment.
1620 if (lsm
.u
.channel
.chan
.attr
.num_subbuf
>
1621 UINT64_MAX
/ lsm
.u
.channel
.chan
.attr
.subbuf_size
) {
1623 ret
= -LTTNG_ERR_OVERFLOW
;
1627 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1628 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1629 ret_code
= check_enough_available_memory(
1630 total_buffer_size_needed_per_cpu
);
1631 if (ret_code
!= LTTNG_OK
) {
1636 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1637 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1639 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1640 sizeof(lsm
.session
.name
));
1642 ret
= -LTTNG_ERR_INVALID
;
1646 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1652 * All tracing will be stopped for registered events of the channel.
1653 * Returns size of returned session payload data or a negative error code.
1655 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1658 struct lttcomm_session_msg lsm
;
1660 /* Safety check. Both are mandatory. */
1661 if (handle
== NULL
|| name
== NULL
) {
1662 return -LTTNG_ERR_INVALID
;
1665 memset(&lsm
, 0, sizeof(lsm
));
1667 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1669 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1670 sizeof(lsm
.u
.disable
.channel_name
));
1672 ret
= -LTTNG_ERR_INVALID
;
1676 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1678 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1679 sizeof(lsm
.session
.name
));
1681 ret
= -LTTNG_ERR_INVALID
;
1685 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1691 * Lists all available tracepoints of domain.
1692 * Sets the contents of the events array.
1693 * Returns the number of lttng_event entries in events;
1694 * on error, returns a negative value.
1696 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1697 struct lttng_event
**events
)
1700 struct lttcomm_session_msg lsm
;
1702 if (handle
== NULL
) {
1703 return -LTTNG_ERR_INVALID
;
1706 memset(&lsm
, 0, sizeof(lsm
));
1707 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1708 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1710 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1715 return ret
/ sizeof(struct lttng_event
);
1719 * Lists all available tracepoint fields of domain.
1720 * Sets the contents of the event field array.
1721 * Returns the number of lttng_event_field entries in events;
1722 * on error, returns a negative value.
1724 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1725 struct lttng_event_field
**fields
)
1728 struct lttcomm_session_msg lsm
;
1730 if (handle
== NULL
) {
1731 return -LTTNG_ERR_INVALID
;
1734 memset(&lsm
, 0, sizeof(lsm
));
1735 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1736 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1738 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1743 return ret
/ sizeof(struct lttng_event_field
);
1747 * Lists all available kernel system calls. Allocates and sets the contents of
1750 * Returns the number of lttng_event entries in events; on error, returns a
1753 int lttng_list_syscalls(struct lttng_event
**events
)
1756 struct lttcomm_session_msg lsm
;
1759 return -LTTNG_ERR_INVALID
;
1762 memset(&lsm
, 0, sizeof(lsm
));
1763 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1764 /* Force kernel domain for system calls. */
1765 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1767 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1772 return ret
/ sizeof(struct lttng_event
);
1776 * Returns a human readable string describing
1777 * the error code (a negative value).
1779 const char *lttng_strerror(int code
)
1781 return error_get_str(code
);
1784 enum lttng_error_code
lttng_create_session_ext(
1785 struct lttng_session_descriptor
*session_descriptor
)
1787 enum lttng_error_code ret_code
;
1788 struct lttcomm_session_msg lsm
= {
1789 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1792 struct lttng_buffer_view reply_view
;
1794 bool sessiond_must_generate_ouput
;
1795 struct lttng_dynamic_buffer payload
;
1797 size_t descriptor_size
;
1798 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1800 lttng_dynamic_buffer_init(&payload
);
1801 if (!session_descriptor
) {
1802 ret_code
= LTTNG_ERR_INVALID
;
1806 sessiond_must_generate_ouput
=
1807 !lttng_session_descriptor_is_output_destination_initialized(
1808 session_descriptor
);
1809 if (sessiond_must_generate_ouput
) {
1810 const char *home_dir
= utils_get_home_dir();
1811 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1813 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1814 ret_code
= LTTNG_ERR_FATAL
;
1818 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1819 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1822 ret_code
= LTTNG_ERR_NOMEM
;
1827 descriptor_size
= payload
.size
;
1828 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1831 ret_code
= LTTNG_ERR_INVALID
;
1834 descriptor_size
= payload
.size
- descriptor_size
;
1835 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1837 /* Command returns a session descriptor on success. */
1838 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1839 payload
.size
, &reply
);
1840 if (reply_ret
< 0) {
1841 ret_code
= -reply_ret
;
1843 } else if (reply_ret
== 0) {
1844 /* Socket unexpectedly closed by the session daemon. */
1845 ret_code
= LTTNG_ERR_FATAL
;
1849 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1850 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1853 ret_code
= LTTNG_ERR_FATAL
;
1856 ret_code
= LTTNG_OK
;
1857 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1860 lttng_dynamic_buffer_reset(&payload
);
1861 lttng_session_descriptor_destroy(descriptor_reply
);
1866 * Create a new session using name and url for destination.
1868 * Return 0 on success else a negative LTTng error code.
1870 int lttng_create_session(const char *name
, const char *url
)
1874 struct lttng_uri
*uris
= NULL
;
1875 struct lttng_session_descriptor
*descriptor
= NULL
;
1876 enum lttng_error_code ret_code
;
1879 ret
= -LTTNG_ERR_INVALID
;
1883 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1885 ret
= -LTTNG_ERR_INVALID
;
1890 descriptor
= lttng_session_descriptor_create(name
);
1893 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1894 ret
= -LTTNG_ERR_INVALID
;
1897 descriptor
= lttng_session_descriptor_local_create(name
,
1901 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1905 ret
= -LTTNG_ERR_INVALID
;
1909 ret
= -LTTNG_ERR_INVALID
;
1912 ret_code
= lttng_create_session_ext(descriptor
);
1913 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1915 lttng_session_descriptor_destroy(descriptor
);
1921 * Create a session exclusively used for snapshot.
1923 * Return 0 on success else a negative LTTng error code.
1925 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1928 enum lttng_error_code ret_code
;
1930 struct lttng_uri
*uris
= NULL
;
1931 struct lttng_session_descriptor
*descriptor
= NULL
;
1934 ret
= -LTTNG_ERR_INVALID
;
1938 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1940 ret
= -LTTNG_ERR_INVALID
;
1944 * If the user does not specify a custom subdir, use the session name.
1946 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1947 strlen(uris
[0].subdir
) == 0) {
1948 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1951 PERROR("Failed to set session name as network destination sub-directory");
1952 ret
= -LTTNG_ERR_FATAL
;
1954 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1955 /* Truncated output. */
1956 ret
= -LTTNG_ERR_INVALID
;
1963 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1966 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1967 ret
= -LTTNG_ERR_INVALID
;
1970 descriptor
= lttng_session_descriptor_snapshot_local_create(
1975 descriptor
= lttng_session_descriptor_snapshot_network_create(
1981 ret
= -LTTNG_ERR_INVALID
;
1985 ret
= -LTTNG_ERR_INVALID
;
1988 ret_code
= lttng_create_session_ext(descriptor
);
1989 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1991 lttng_session_descriptor_destroy(descriptor
);
1997 * Create a session exclusively used for live.
1999 * Return 0 on success else a negative LTTng error code.
2001 int lttng_create_session_live(const char *name
, const char *url
,
2002 unsigned int timer_interval
)
2005 enum lttng_error_code ret_code
;
2006 struct lttng_session_descriptor
*descriptor
= NULL
;
2009 ret
= -LTTNG_ERR_INVALID
;
2014 descriptor
= lttng_session_descriptor_live_network_create(
2015 name
, url
, NULL
, timer_interval
);
2017 descriptor
= lttng_session_descriptor_live_create(
2018 name
, timer_interval
);
2021 ret
= -LTTNG_ERR_INVALID
;
2024 ret_code
= lttng_create_session_ext(descriptor
);
2025 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2027 lttng_session_descriptor_destroy(descriptor
);
2032 * Stop the session and wait for the data before destroying it
2034 * Return 0 on success else a negative LTTng error code.
2036 int lttng_destroy_session(const char *session_name
)
2039 enum lttng_error_code ret_code
;
2040 enum lttng_destruction_handle_status status
;
2041 struct lttng_destruction_handle
*handle
= NULL
;
2044 * Stop the tracing and wait for the data to be
2047 ret
= _lttng_stop_tracing(session_name
, 1);
2048 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2052 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2053 if (ret_code
!= LTTNG_OK
) {
2054 ret
= (int) -ret_code
;
2059 /* Block until the completion of the destruction of the session. */
2060 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2061 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2062 ret
= -LTTNG_ERR_UNK
;
2066 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2067 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2068 ret
= -LTTNG_ERR_UNK
;
2071 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2073 lttng_destruction_handle_destroy(handle
);
2078 * Destroy the session without waiting for the data.
2080 int lttng_destroy_session_no_wait(const char *session_name
)
2082 enum lttng_error_code ret_code
;
2084 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2085 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2089 * Ask the session daemon for all available sessions.
2090 * Sets the contents of the sessions array.
2091 * Returns the number of lttng_session entries in sessions;
2092 * on error, returns a negative value.
2094 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2097 struct lttcomm_session_msg lsm
;
2098 const size_t session_size
= sizeof(struct lttng_session
) +
2099 sizeof(struct lttng_session_extended
);
2100 size_t session_count
, i
;
2101 struct lttng_session_extended
*sessions_extended_begin
;
2102 struct lttng_session
*sessions
= NULL
;
2104 memset(&lsm
, 0, sizeof(lsm
));
2105 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2107 * Initialize out_sessions to NULL so it is initialized when
2108 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2109 * be subsequently freed.
2111 *out_sessions
= NULL
;
2112 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2117 ret
= -LTTNG_ERR_FATAL
;
2121 if (ret
% session_size
) {
2122 ret
= -LTTNG_ERR_UNK
;
2126 session_count
= (size_t) ret
/ session_size
;
2127 sessions_extended_begin
= (struct lttng_session_extended
*)
2128 (&sessions
[session_count
]);
2130 /* Set extended session info pointers. */
2131 for (i
= 0; i
< session_count
; i
++) {
2132 struct lttng_session
*session
= &sessions
[i
];
2133 struct lttng_session_extended
*extended
=
2134 &(sessions_extended_begin
[i
]);
2136 session
->extended
.ptr
= extended
;
2139 ret
= (int) session_count
;
2140 *out_sessions
= sessions
;
2145 enum lttng_error_code
lttng_session_get_creation_time(
2146 const struct lttng_session
*session
, uint64_t *creation_time
)
2148 enum lttng_error_code ret
= LTTNG_OK
;
2149 struct lttng_session_extended
*extended
;
2151 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2152 ret
= LTTNG_ERR_INVALID
;
2156 extended
= session
->extended
.ptr
;
2157 if (!extended
->creation_time
.is_set
) {
2158 /* Not created on the session daemon yet. */
2159 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2162 *creation_time
= extended
->creation_time
.value
;
2167 int lttng_set_session_shm_path(const char *session_name
,
2168 const char *shm_path
)
2171 struct lttcomm_session_msg lsm
;
2173 if (session_name
== NULL
) {
2174 return -LTTNG_ERR_INVALID
;
2177 memset(&lsm
, 0, sizeof(lsm
));
2178 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2180 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2181 sizeof(lsm
.session
.name
));
2183 ret
= -LTTNG_ERR_INVALID
;
2187 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2188 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2190 ret
= -LTTNG_ERR_INVALID
;
2194 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2200 * Ask the session daemon for all available domains of a session.
2201 * Sets the contents of the domains array.
2202 * Returns the number of lttng_domain entries in domains;
2203 * on error, returns a negative value.
2205 int lttng_list_domains(const char *session_name
,
2206 struct lttng_domain
**domains
)
2209 struct lttcomm_session_msg lsm
;
2211 if (session_name
== NULL
) {
2212 ret
= -LTTNG_ERR_INVALID
;
2216 memset(&lsm
, 0, sizeof(lsm
));
2217 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2219 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2220 sizeof(lsm
.session
.name
));
2222 ret
= -LTTNG_ERR_INVALID
;
2226 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2231 return ret
/ sizeof(struct lttng_domain
);
2237 * Ask the session daemon for all available channels of a session.
2238 * Sets the contents of the channels array.
2239 * Returns the number of lttng_channel entries in channels;
2240 * on error, returns a negative value.
2242 int lttng_list_channels(struct lttng_handle
*handle
,
2243 struct lttng_channel
**channels
)
2246 size_t channel_count
, i
;
2247 const size_t channel_size
= sizeof(struct lttng_channel
) +
2248 sizeof(struct lttng_channel_extended
);
2249 struct lttcomm_session_msg lsm
;
2252 if (handle
== NULL
) {
2253 ret
= -LTTNG_ERR_INVALID
;
2257 memset(&lsm
, 0, sizeof(lsm
));
2258 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2259 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2260 sizeof(lsm
.session
.name
));
2262 ret
= -LTTNG_ERR_INVALID
;
2266 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2268 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2273 if (ret
% channel_size
) {
2274 ret
= -LTTNG_ERR_UNK
;
2279 channel_count
= (size_t) ret
/ channel_size
;
2281 /* Set extended info pointers */
2282 extended_at
= ((void *) *channels
) +
2283 channel_count
* sizeof(struct lttng_channel
);
2284 for (i
= 0; i
< channel_count
; i
++) {
2285 struct lttng_channel
*chan
= &(*channels
)[i
];
2287 chan
->attr
.extended
.ptr
= extended_at
;
2288 extended_at
+= sizeof(struct lttng_channel_extended
);
2291 ret
= (int) channel_count
;
2297 * Ask the session daemon for all available events of a session channel.
2298 * Sets the contents of the events array.
2299 * Returns the number of lttng_event entries in events;
2300 * on error, returns a negative value.
2302 int lttng_list_events(struct lttng_handle
*handle
,
2303 const char *channel_name
, struct lttng_event
**events
)
2306 struct lttcomm_session_msg lsm
;
2307 struct lttcomm_event_command_header
*cmd_header
= NULL
;
2308 size_t cmd_header_len
;
2309 uint32_t nb_events
, i
;
2311 char *reception_buffer
= NULL
;
2312 struct lttng_dynamic_buffer listing
;
2315 /* Safety check. An handle and channel name are mandatory */
2316 if (handle
== NULL
|| channel_name
== NULL
) {
2317 return -LTTNG_ERR_INVALID
;
2320 memset(&lsm
, 0, sizeof(lsm
));
2321 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2322 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2323 sizeof(lsm
.session
.name
));
2325 ret
= -LTTNG_ERR_INVALID
;
2329 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2330 sizeof(lsm
.u
.list
.channel_name
));
2332 ret
= -LTTNG_ERR_INVALID
;
2336 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2338 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2339 (void **) &reception_buffer
, (void **) &cmd_header
,
2346 ret
= -LTTNG_ERR_UNK
;
2350 /* Set number of events and free command header */
2351 nb_events
= cmd_header
->nb_events
;
2352 if (nb_events
> INT_MAX
) {
2353 ret
= -LTTNG_ERR_OVERFLOW
;
2360 * The buffer that is returned must contain a "flat" version of
2361 * the events that are returned. In other words, all pointers
2362 * within an lttng_event must point to a location within the returned
2363 * buffer so that the user may free everything by simply calling free()
2364 * on the returned buffer. This is needed in order to maintain API
2367 * A first pass is performed to compute the size of the buffer that
2368 * must be allocated. A second pass is then performed to setup
2369 * the returned events so that their members always point within the
2372 * The layout of the returned buffer is as follows:
2373 * - struct lttng_event[nb_events],
2374 * - nb_events times the following:
2375 * - struct lttng_event_extended,
2376 * - flattened version of userspace_probe_location
2377 * - filter_expression
2379 * - padding to align to 64-bits
2381 comm_ext_at
= reception_buffer
+
2382 (nb_events
* sizeof(struct lttng_event
));
2383 storage_req
= nb_events
* sizeof(struct lttng_event
);
2385 for (i
= 0; i
< nb_events
; i
++) {
2386 struct lttcomm_event_extended_header
*ext_comm
=
2387 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2388 int probe_storage_req
= 0;
2390 comm_ext_at
+= sizeof(*ext_comm
);
2391 comm_ext_at
+= ext_comm
->filter_len
;
2393 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2395 if (ext_comm
->userspace_probe_location_len
) {
2396 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2397 struct lttng_buffer_view probe_location_view
;
2399 probe_location_view
= lttng_buffer_view_init(
2401 ext_comm
->userspace_probe_location_len
);
2404 * Create a temporary userspace probe location to
2405 * determine the size needed by a "flattened" version
2406 * of that same probe location.
2408 ret
= lttng_userspace_probe_location_create_from_buffer(
2409 &probe_location_view
, &probe_location
);
2411 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2415 ret
= lttng_userspace_probe_location_flatten(
2416 probe_location
, NULL
);
2417 lttng_userspace_probe_location_destroy(probe_location
);
2419 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2423 probe_storage_req
= ret
;
2424 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2427 storage_req
+= sizeof(struct lttng_event_extended
);
2428 storage_req
+= ext_comm
->filter_len
;
2429 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2430 /* Padding to ensure the flat probe is aligned. */
2431 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2432 storage_req
+= probe_storage_req
;
2435 lttng_dynamic_buffer_init(&listing
);
2437 * We must ensure that "listing" is never resized so as to preserve
2438 * the validity of the flattened objects.
2440 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2442 ret
= -LTTNG_ERR_NOMEM
;
2446 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2447 nb_events
* sizeof(struct lttng_event
));
2449 ret
= -LTTNG_ERR_NOMEM
;
2450 goto free_dynamic_buffer
;
2453 comm_ext_at
= reception_buffer
+
2454 (nb_events
* sizeof(struct lttng_event
));
2455 for (i
= 0; i
< nb_events
; i
++) {
2456 struct lttng_event
*event
= (struct lttng_event
*)
2457 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2458 struct lttcomm_event_extended_header
*ext_comm
=
2459 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2460 struct lttng_event_extended
*event_extended
=
2461 (struct lttng_event_extended
*)
2462 (listing
.data
+ listing
.size
);
2464 /* Insert struct lttng_event_extended. */
2465 ret
= lttng_dynamic_buffer_set_size(&listing
,
2466 listing
.size
+ sizeof(*event_extended
));
2468 ret
= -LTTNG_ERR_NOMEM
;
2469 goto free_dynamic_buffer
;
2471 event
->extended
.ptr
= event_extended
;
2473 comm_ext_at
+= sizeof(*ext_comm
);
2475 /* Insert filter expression. */
2476 if (ext_comm
->filter_len
) {
2477 event_extended
->filter_expression
= listing
.data
+
2479 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2480 ext_comm
->filter_len
);
2482 ret
= -LTTNG_ERR_NOMEM
;
2483 goto free_dynamic_buffer
;
2485 comm_ext_at
+= ext_comm
->filter_len
;
2488 /* Insert exclusions. */
2489 if (ext_comm
->nb_exclusions
) {
2490 event_extended
->exclusions
.count
=
2491 ext_comm
->nb_exclusions
;
2492 event_extended
->exclusions
.strings
=
2493 listing
.data
+ listing
.size
;
2495 ret
= lttng_dynamic_buffer_append(&listing
,
2497 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2499 ret
= -LTTNG_ERR_NOMEM
;
2500 goto free_dynamic_buffer
;
2502 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2505 /* Insert padding to align to 64-bits. */
2506 ret
= lttng_dynamic_buffer_set_size(&listing
,
2507 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2509 ret
= -LTTNG_ERR_NOMEM
;
2510 goto free_dynamic_buffer
;
2513 /* Insert flattened userspace probe location. */
2514 if (ext_comm
->userspace_probe_location_len
) {
2515 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2516 struct lttng_buffer_view probe_location_view
;
2518 probe_location_view
= lttng_buffer_view_init(
2520 ext_comm
->userspace_probe_location_len
);
2522 ret
= lttng_userspace_probe_location_create_from_buffer(
2523 &probe_location_view
, &probe_location
);
2525 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2526 goto free_dynamic_buffer
;
2529 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2530 (listing
.data
+ listing
.size
);
2531 ret
= lttng_userspace_probe_location_flatten(
2532 probe_location
, &listing
);
2533 lttng_userspace_probe_location_destroy(probe_location
);
2535 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2536 goto free_dynamic_buffer
;
2539 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2543 /* Don't reset listing buffer as we return its content. */
2544 *events
= (struct lttng_event
*) listing
.data
;
2545 lttng_dynamic_buffer_init(&listing
);
2546 ret
= (int) nb_events
;
2547 free_dynamic_buffer
:
2548 lttng_dynamic_buffer_reset(&listing
);
2551 free(reception_buffer
);
2556 * Sets the tracing_group variable with name.
2557 * This function allocates memory pointed to by tracing_group.
2558 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2560 int lttng_set_tracing_group(const char *name
)
2564 return -LTTNG_ERR_INVALID
;
2567 if (asprintf(&new_group
, "%s", name
) < 0) {
2568 return -LTTNG_ERR_FATAL
;
2571 free(tracing_group
);
2572 tracing_group
= new_group
;
2578 int lttng_calibrate(struct lttng_handle
*handle
,
2579 struct lttng_calibrate
*calibrate
)
2582 * This command was removed in LTTng 2.9.
2584 return -LTTNG_ERR_UND
;
2588 * Set default channel attributes.
2589 * If either or both of the arguments are null, attr content is zeroe'd.
2591 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2592 struct lttng_channel_attr
*attr
)
2594 struct lttng_channel_extended
*extended
;
2597 if (attr
== NULL
|| domain
== NULL
) {
2601 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2602 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2604 /* Same for all domains. */
2605 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2606 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2607 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2609 switch (domain
->type
) {
2610 case LTTNG_DOMAIN_KERNEL
:
2611 attr
->switch_timer_interval
=
2612 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2613 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2614 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2615 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2616 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2618 extended
->monitor_timer_interval
=
2619 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2620 extended
->blocking_timeout
=
2621 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2624 case LTTNG_DOMAIN_UST
:
2625 switch (domain
->buf_type
) {
2626 case LTTNG_BUFFER_PER_UID
:
2627 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2628 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2629 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2630 attr
->switch_timer_interval
=
2631 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2632 attr
->read_timer_interval
=
2633 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2635 extended
->monitor_timer_interval
=
2636 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2637 extended
->blocking_timeout
=
2638 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2641 case LTTNG_BUFFER_PER_PID
:
2643 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2644 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2645 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2646 attr
->switch_timer_interval
=
2647 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2648 attr
->read_timer_interval
=
2649 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2651 extended
->monitor_timer_interval
=
2652 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2653 extended
->blocking_timeout
=
2654 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2659 /* Default behavior: leave set to 0. */
2663 attr
->extended
.ptr
= extended
;
2666 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2667 uint64_t *discarded_events
)
2670 struct lttng_channel_extended
*chan_ext
;
2672 if (!channel
|| !discarded_events
) {
2673 ret
= -LTTNG_ERR_INVALID
;
2677 chan_ext
= channel
->attr
.extended
.ptr
;
2680 * This can happen since the lttng_channel structure is
2681 * used for other tasks where this pointer is never set.
2683 *discarded_events
= 0;
2687 *discarded_events
= chan_ext
->discarded_events
;
2692 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2693 uint64_t *lost_packets
)
2696 struct lttng_channel_extended
*chan_ext
;
2698 if (!channel
|| !lost_packets
) {
2699 ret
= -LTTNG_ERR_INVALID
;
2703 chan_ext
= channel
->attr
.extended
.ptr
;
2706 * This can happen since the lttng_channel structure is
2707 * used for other tasks where this pointer is never set.
2713 *lost_packets
= chan_ext
->lost_packets
;
2718 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2719 uint64_t *monitor_timer_interval
)
2723 if (!chan
|| !monitor_timer_interval
) {
2724 ret
= -LTTNG_ERR_INVALID
;
2728 if (!chan
->attr
.extended
.ptr
) {
2729 ret
= -LTTNG_ERR_INVALID
;
2733 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2734 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2739 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2740 uint64_t monitor_timer_interval
)
2744 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2745 ret
= -LTTNG_ERR_INVALID
;
2749 ((struct lttng_channel_extended
*)
2750 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2751 monitor_timer_interval
;
2756 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2757 int64_t *blocking_timeout
)
2761 if (!chan
|| !blocking_timeout
) {
2762 ret
= -LTTNG_ERR_INVALID
;
2766 if (!chan
->attr
.extended
.ptr
) {
2767 ret
= -LTTNG_ERR_INVALID
;
2771 *blocking_timeout
= ((struct lttng_channel_extended
*)
2772 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2777 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2778 int64_t blocking_timeout
)
2781 int64_t msec_timeout
;
2783 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2784 ret
= -LTTNG_ERR_INVALID
;
2788 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2789 ret
= -LTTNG_ERR_INVALID
;
2794 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2795 * us to accept a narrower range of values (msecs expressed as a signed
2798 msec_timeout
= blocking_timeout
/ 1000;
2799 if (msec_timeout
!= (int32_t) msec_timeout
) {
2800 ret
= -LTTNG_ERR_INVALID
;
2804 ((struct lttng_channel_extended
*)
2805 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2812 * Check if session daemon is alive.
2814 * Return 1 if alive or 0 if not.
2815 * On error returns a negative value.
2817 int lttng_session_daemon_alive(void)
2821 ret
= set_session_daemon_path();
2827 if (*sessiond_sock_path
== '\0') {
2829 * No socket path set. Weird error which means the constructor
2835 ret
= try_connect_sessiond(sessiond_sock_path
);
2846 * Set URL for a consumer for a session and domain.
2848 * Return 0 on success, else a negative value.
2850 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2851 const char *control_url
, const char *data_url
)
2855 struct lttcomm_session_msg lsm
;
2856 struct lttng_uri
*uris
= NULL
;
2858 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2859 ret
= -LTTNG_ERR_INVALID
;
2863 memset(&lsm
, 0, sizeof(lsm
));
2865 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2867 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2868 sizeof(lsm
.session
.name
));
2870 ret
= -LTTNG_ERR_INVALID
;
2874 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2876 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2878 ret
= -LTTNG_ERR_INVALID
;
2882 lsm
.u
.uri
.size
= size
;
2884 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2885 sizeof(struct lttng_uri
) * size
, NULL
);
2895 int lttng_enable_consumer(struct lttng_handle
*handle
);
2896 int lttng_enable_consumer(struct lttng_handle
*handle
)
2904 int lttng_disable_consumer(struct lttng_handle
*handle
);
2905 int lttng_disable_consumer(struct lttng_handle
*handle
)
2913 int _lttng_create_session_ext(const char *name
, const char *url
,
2914 const char *datetime
);
2915 int _lttng_create_session_ext(const char *name
, const char *url
,
2916 const char *datetime
)
2922 * For a given session name, this call checks if the data is ready to be read
2923 * or is still being extracted by the consumer(s) hence not ready to be used by
2926 int lttng_data_pending(const char *session_name
)
2929 struct lttcomm_session_msg lsm
;
2930 uint8_t *pending
= NULL
;
2932 if (session_name
== NULL
) {
2933 return -LTTNG_ERR_INVALID
;
2936 memset(&lsm
, 0, sizeof(lsm
));
2937 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2939 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2940 sizeof(lsm
.session
.name
));
2942 ret
= -LTTNG_ERR_INVALID
;
2946 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2949 } else if (ret
!= 1) {
2950 /* Unexpected payload size */
2951 ret
= -LTTNG_ERR_INVALID
;
2953 } else if (!pending
) {
2954 /* Internal error. */
2955 ret
= -LTTNG_ERR_UNK
;
2959 ret
= (int) *pending
;
2966 * Regenerate the metadata for a session.
2967 * Return 0 on success, a negative error code on error.
2969 int lttng_regenerate_metadata(const char *session_name
)
2972 struct lttcomm_session_msg lsm
;
2974 if (!session_name
) {
2975 ret
= -LTTNG_ERR_INVALID
;
2979 memset(&lsm
, 0, sizeof(lsm
));
2980 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2982 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2983 sizeof(lsm
.session
.name
));
2985 ret
= -LTTNG_ERR_INVALID
;
2989 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3000 * Deprecated, replaced by lttng_regenerate_metadata.
3002 int lttng_metadata_regenerate(const char *session_name
)
3004 return lttng_regenerate_metadata(session_name
);
3008 * Regenerate the statedump of a session.
3009 * Return 0 on success, a negative error code on error.
3011 int lttng_regenerate_statedump(const char *session_name
)
3014 struct lttcomm_session_msg lsm
;
3016 if (!session_name
) {
3017 ret
= -LTTNG_ERR_INVALID
;
3021 memset(&lsm
, 0, sizeof(lsm
));
3022 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3024 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3025 sizeof(lsm
.session
.name
));
3027 ret
= -LTTNG_ERR_INVALID
;
3031 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3041 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3044 struct lttcomm_session_msg lsm
;
3045 struct lttng_dynamic_buffer buffer
;
3047 lttng_dynamic_buffer_init(&buffer
);
3049 ret
= -LTTNG_ERR_INVALID
;
3053 if (!lttng_trigger_validate(trigger
)) {
3054 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3058 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3060 ret
= -LTTNG_ERR_UNK
;
3064 memset(&lsm
, 0, sizeof(lsm
));
3065 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3066 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3067 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3070 lttng_dynamic_buffer_reset(&buffer
);
3074 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3077 struct lttcomm_session_msg lsm
;
3078 struct lttng_dynamic_buffer buffer
;
3080 lttng_dynamic_buffer_init(&buffer
);
3082 ret
= -LTTNG_ERR_INVALID
;
3086 if (!lttng_trigger_validate(trigger
)) {
3087 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3091 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3093 ret
= -LTTNG_ERR_UNK
;
3097 memset(&lsm
, 0, sizeof(lsm
));
3098 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3099 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3100 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3103 lttng_dynamic_buffer_reset(&buffer
);
3110 static void __attribute__((constructor
)) init(void)
3112 /* Set default session group */
3113 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3119 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3121 free(tracing_group
);