4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
22 #include <common/bytecode/bytecode.h>
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 <common/filter/filter-ast.h>
43 #include <common/filter/filter-parser.h>
44 #include <common/filter/memstream.h>
45 #include "lttng-ctl-helper.h"
47 #define COPY_DOMAIN_PACKED(dst, src) \
49 struct lttng_domain _tmp_domain; \
51 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
55 /* Socket to session daemon for communication */
56 static int sessiond_socket
= -1;
57 static char sessiond_sock_path
[PATH_MAX
];
60 static char *tracing_group
;
66 * Those two variables are used by error.h to silent or control the verbosity of
67 * error message. They are global to the library so application linking with it
68 * are able to compile correctly and also control verbosity of the library.
71 int lttng_opt_verbose
;
75 * Copy string from src to dst and enforce null terminated byte.
78 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
81 strncpy(dst
, src
, len
);
82 /* Enforce the NULL terminated byte */
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 int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
279 size_t best_mem_info
;
280 size_t num_bytes_requested_total
;
283 * Get the number of CPU currently online to compute the amount of
284 * memory needed to create a buffer for every CPU.
286 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
291 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
294 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
295 * reliable estimate we can get but it is only exposed by the kernel
296 * since 3.14. (See Linux kernel commit:
297 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
299 ret
= utils_get_memory_available(&best_mem_info
);
305 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
306 * is a sanity check for obvious user error.
308 ret
= utils_get_memory_total(&best_mem_info
);
316 return best_mem_info
>= num_bytes_requested_total
;
320 * Try connect to session daemon with sock_path.
322 * Return 0 on success, else -1
324 static int try_connect_sessiond(const char *sock_path
)
328 /* If socket exist, we check if the daemon listens for connect. */
329 ret
= access(sock_path
, F_OK
);
335 ret
= lttcomm_connect_unix_sock(sock_path
);
341 ret
= lttcomm_close_unix_sock(ret
);
343 PERROR("lttcomm_close_unix_sock");
353 * Set sessiond socket path by putting it in the global sessiond_sock_path
356 * Returns 0 on success, negative value on failure (the sessiond socket path
357 * is somehow too long or ENOMEM).
359 static int set_session_daemon_path(void)
361 int in_tgroup
= 0; /* In tracing group. */
367 /* Are we in the tracing group ? */
368 in_tgroup
= lttng_check_tracing_group();
371 if ((uid
== 0) || in_tgroup
) {
372 lttng_ctl_copy_string(sessiond_sock_path
,
373 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
381 ret
= try_connect_sessiond(sessiond_sock_path
);
385 /* Global session daemon not available... */
387 /* ...or not in tracing group (and not root), default */
390 * With GNU C < 2.1, snprintf returns -1 if the target buffer
392 * With GNU C >= 2.1, snprintf returns the required size
393 * (excluding closing null)
395 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
396 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
397 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
409 * Connect to the LTTng session daemon.
411 * On success, return the socket's file descriptor. On error, return -1.
413 LTTNG_HIDDEN
int connect_sessiond(void)
417 ret
= set_session_daemon_path();
422 /* Connect to the sesssion daemon. */
423 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
434 static void reset_global_sessiond_connection_state(void)
436 sessiond_socket
= -1;
441 * Clean disconnect from the session daemon.
443 * On success, return 0. On error, return -1.
445 static int disconnect_sessiond(void)
450 ret
= lttcomm_close_unix_sock(sessiond_socket
);
451 reset_global_sessiond_connection_state();
457 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
465 ret
= -LTTNG_ERR_INVALID
;
475 ret
= recv_data_sessiond(buf
, len
);
481 ret
= -LTTNG_ERR_INVALID
;
485 /* Move ownership of command header buffer to user. */
490 /* No command header. */
506 * Ask the session daemon a specific command and put the data into buf.
507 * Takes extra var. len. data and file descriptors as input to send to the
510 * Return size of data (only payload, not header) or a negative error code.
513 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
514 const int *fds
, size_t nb_fd
, const void *vardata
,
515 size_t vardata_len
, void **user_payload_buf
,
516 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
520 struct lttcomm_lttng_msg llm
;
522 ret
= connect_sessiond();
524 ret
= -LTTNG_ERR_NO_SESSIOND
;
527 sessiond_socket
= ret
;
531 /* Send command to session daemon */
532 ret
= send_session_msg(lsm
);
534 /* Ret value is a valid lttng error code. */
537 /* Send var len data */
538 ret
= send_session_varlen(vardata
, vardata_len
);
540 /* Ret value is a valid lttng error code. */
545 ret
= send_session_fds(fds
, nb_fd
);
547 /* Ret value is a valid lttng error code. */
551 /* Get header from data transmission */
552 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
554 /* Ret value is a valid lttng error code. */
558 /* Check error code if OK */
559 if (llm
.ret_code
!= LTTNG_OK
) {
564 /* Get command header from data transmission */
565 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
566 user_cmd_header_buf
, user_cmd_header_len
);
571 /* Get payload from data transmission */
572 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
581 disconnect_sessiond();
586 * Create lttng handle and return pointer.
588 * The returned pointer will be NULL in case of malloc() error.
590 struct lttng_handle
*lttng_create_handle(const char *session_name
,
591 struct lttng_domain
*domain
)
593 struct lttng_handle
*handle
= NULL
;
595 handle
= zmalloc(sizeof(struct lttng_handle
));
596 if (handle
== NULL
) {
597 PERROR("malloc handle");
601 /* Copy session name */
602 lttng_ctl_copy_string(handle
->session_name
, session_name
,
603 sizeof(handle
->session_name
));
605 /* Copy lttng domain or leave initialized to 0. */
607 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
615 * Destroy handle by free(3) the pointer.
617 void lttng_destroy_handle(struct lttng_handle
*handle
)
623 * Register an outside consumer.
625 * Returns size of returned session payload data or a negative error code.
627 int lttng_register_consumer(struct lttng_handle
*handle
,
628 const char *socket_path
)
630 struct lttcomm_session_msg lsm
;
632 if (handle
== NULL
|| socket_path
== NULL
) {
633 return -LTTNG_ERR_INVALID
;
636 memset(&lsm
, 0, sizeof(lsm
));
637 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
638 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
639 sizeof(lsm
.session
.name
));
640 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
642 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
643 sizeof(lsm
.u
.reg
.path
));
645 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
649 * Start tracing for all traces of the session.
651 * Returns size of returned session payload data or a negative error code.
653 int lttng_start_tracing(const char *session_name
)
655 struct lttcomm_session_msg lsm
;
657 if (session_name
== NULL
) {
658 return -LTTNG_ERR_INVALID
;
661 memset(&lsm
, 0, sizeof(lsm
));
662 lsm
.cmd_type
= LTTNG_START_TRACE
;
664 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
665 sizeof(lsm
.session
.name
));
667 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
671 * Stop tracing for all traces of the session.
673 static int _lttng_stop_tracing(const char *session_name
, int wait
)
676 struct lttcomm_session_msg lsm
;
678 if (session_name
== NULL
) {
679 return -LTTNG_ERR_INVALID
;
682 memset(&lsm
, 0, sizeof(lsm
));
683 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
685 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
686 sizeof(lsm
.session
.name
));
688 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
689 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
697 /* Check for data availability */
699 data_ret
= lttng_data_pending(session_name
);
701 /* Return the data available call error. */
707 * Data sleep time before retrying (in usec). Don't sleep if the
708 * call returned value indicates availability.
711 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
713 } while (data_ret
!= 0);
721 * Stop tracing and wait for data availability.
723 int lttng_stop_tracing(const char *session_name
)
725 return _lttng_stop_tracing(session_name
, 1);
729 * Stop tracing but _don't_ wait for data availability.
731 int lttng_stop_tracing_no_wait(const char *session_name
)
733 return _lttng_stop_tracing(session_name
, 0);
737 * Add context to a channel.
739 * If the given channel is NULL, add the contexts to all channels.
740 * The event_name param is ignored.
742 * Returns the size of the returned payload data or a negative error code.
744 int lttng_add_context(struct lttng_handle
*handle
,
745 struct lttng_event_context
*ctx
, const char *event_name
,
746 const char *channel_name
)
751 struct lttcomm_session_msg lsm
;
753 /* Safety check. Both are mandatory. */
754 if (handle
== NULL
|| ctx
== NULL
) {
755 ret
= -LTTNG_ERR_INVALID
;
759 memset(&lsm
, 0, sizeof(lsm
));
760 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
762 /* If no channel name, send empty string. */
763 if (channel_name
== NULL
) {
764 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
765 sizeof(lsm
.u
.context
.channel_name
));
767 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
768 sizeof(lsm
.u
.context
.channel_name
));
771 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
772 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
773 sizeof(lsm
.session
.name
));
775 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
776 size_t provider_len
, ctx_len
;
777 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
778 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
780 if (!provider_name
|| !ctx_name
) {
781 ret
= -LTTNG_ERR_INVALID
;
785 provider_len
= strlen(provider_name
);
786 if (provider_len
== 0) {
787 ret
= -LTTNG_ERR_INVALID
;
790 lsm
.u
.context
.provider_name_len
= provider_len
;
792 ctx_len
= strlen(ctx_name
);
794 ret
= -LTTNG_ERR_INVALID
;
797 lsm
.u
.context
.context_name_len
= ctx_len
;
799 len
= provider_len
+ ctx_len
;
802 ret
= -LTTNG_ERR_NOMEM
;
806 memcpy(buf
, provider_name
, provider_len
);
807 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
809 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
811 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
813 * Don't leak application addresses to the sessiond.
814 * This is only necessary when ctx is for an app ctx otherwise
815 * the values inside the union (type & config) are overwritten.
817 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
818 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
821 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
828 * Enable event(s) for a channel.
830 * If no event name is specified, all events are enabled.
831 * If no channel name is specified, the default 'channel0' is used.
833 * Returns size of returned session payload data or a negative error code.
835 int lttng_enable_event(struct lttng_handle
*handle
,
836 struct lttng_event
*ev
, const char *channel_name
)
838 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
843 * Create or enable an event with a filter expression.
845 * Return negative error value on error.
846 * Return size of returned session payload data if OK.
848 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
849 struct lttng_event
*event
, const char *channel_name
,
850 const char *filter_expression
)
852 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
853 filter_expression
, 0, NULL
);
857 * Depending on the event, return a newly allocated agent filter expression or
858 * NULL if not applicable.
860 * An event with NO loglevel and the name is * will return NULL.
862 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
865 char *agent_filter
= NULL
;
869 /* Don't add filter for the '*' event. */
870 if (strcmp(ev
->name
, "*") != 0) {
872 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
875 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
883 /* Add loglevel filtering if any for the JUL domain. */
884 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
887 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
893 if (filter
|| agent_filter
) {
896 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
897 agent_filter
? agent_filter
: filter
, op
,
902 agent_filter
= new_filter
;
904 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
920 * Enable event(s) for a channel, possibly with exclusions and a filter.
921 * If no event name is specified, all events are enabled.
922 * If no channel name is specified, the default name is used.
923 * If filter expression is not NULL, the filter is set for the event.
924 * If exclusion count is not zero, the exclusions are set for the event.
925 * Returns size of returned session payload data or a negative error code.
927 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
928 struct lttng_event
*ev
, const char *channel_name
,
929 const char *original_filter_expression
,
930 int exclusion_count
, char **exclusion_list
)
932 struct lttcomm_session_msg lsm
;
933 struct lttng_dynamic_buffer send_buffer
;
934 int ret
= 0, i
, fd_to_send
= -1;
935 bool send_fd
= false;
936 unsigned int free_filter_expression
= 0;
937 struct filter_parser_ctx
*ctx
= NULL
;
940 * We have either a filter or some exclusions, so we need to set up
941 * a variable-length memory block from where to send the data.
943 lttng_dynamic_buffer_init(&send_buffer
);
946 * Cast as non-const since we may replace the filter expression
947 * by a dynamically allocated string. Otherwise, the original
948 * string is not modified.
950 char *filter_expression
= (char *) original_filter_expression
;
952 if (handle
== NULL
|| ev
== NULL
) {
953 ret
= -LTTNG_ERR_INVALID
;
958 * Empty filter string will always be rejected by the parser
959 * anyway, so treat this corner-case early to eliminate
960 * lttng_fmemopen error for 0-byte allocation.
962 if (filter_expression
&& filter_expression
[0] == '\0') {
963 ret
= -LTTNG_ERR_INVALID
;
967 memset(&lsm
, 0, sizeof(lsm
));
969 /* If no channel name, send empty string. */
970 if (channel_name
== NULL
) {
971 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
972 sizeof(lsm
.u
.enable
.channel_name
));
974 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
975 sizeof(lsm
.u
.enable
.channel_name
));
978 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
979 if (ev
->name
[0] == '\0') {
980 /* Enable all events */
981 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
984 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
985 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
987 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
988 sizeof(lsm
.session
.name
));
989 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
990 lsm
.u
.enable
.bytecode_len
= 0;
992 /* Parse filter expression. */
993 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
994 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
995 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
996 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
997 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
998 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1001 /* Setup JUL filter if needed. */
1002 agent_filter
= set_agent_filter(filter_expression
, ev
);
1003 if (!agent_filter
) {
1004 if (!filter_expression
) {
1006 * No JUL and no filter, just skip
1013 * With an agent filter, the original filter has
1014 * been added to it thus replace the filter
1017 filter_expression
= agent_filter
;
1018 free_filter_expression
= 1;
1022 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1027 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1028 + bytecode_get_len(&ctx
->bytecode
->b
);
1029 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1032 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1033 lsm
.u
.enable
.bytecode_len
1034 + lsm
.u
.enable
.expression_len
1035 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1037 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1041 /* Put exclusion names first in the data. */
1042 for (i
= 0; i
< exclusion_count
; i
++) {
1043 size_t exclusion_len
;
1045 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1046 LTTNG_SYMBOL_NAME_LEN
);
1047 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1048 /* Exclusion is not NULL-terminated. */
1049 ret
= -LTTNG_ERR_INVALID
;
1053 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1054 *(exclusion_list
+ i
),
1055 LTTNG_SYMBOL_NAME_LEN
);
1061 /* Add filter expression next. */
1062 if (filter_expression
) {
1063 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1064 filter_expression
, lsm
.u
.enable
.expression_len
);
1069 /* Add filter bytecode next. */
1070 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1071 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1072 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1077 if (ev
->extended
.ptr
) {
1078 struct lttng_event_extended
*ev_ext
=
1079 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1081 if (ev_ext
->probe_location
) {
1083 * lttng_userspace_probe_location_serialize returns the
1084 * number of bytes that was appended to the buffer.
1086 ret
= lttng_userspace_probe_location_serialize(
1087 ev_ext
->probe_location
, &send_buffer
,
1093 send_fd
= fd_to_send
>= 0;
1095 * Set the size of the userspace probe location element
1096 * of the buffer so that the receiving side knows where
1099 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1103 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1104 send_fd
? &fd_to_send
: NULL
,
1106 send_buffer
.size
? send_buffer
.data
: NULL
,
1107 send_buffer
.size
, NULL
, NULL
, 0);
1110 if (filter_expression
&& ctx
) {
1111 filter_bytecode_free(ctx
);
1112 filter_ir_free(ctx
);
1113 filter_parser_ctx_free(ctx
);
1116 if (free_filter_expression
) {
1118 * The filter expression has been replaced and must be freed as
1119 * it is not the original filter expression received as a
1122 free(filter_expression
);
1126 * Return directly to the caller and don't ask the sessiond since
1127 * something went wrong in the parsing of data above.
1129 lttng_dynamic_buffer_reset(&send_buffer
);
1133 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1137 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1138 struct lttng_event
*ev
, const char *channel_name
,
1139 const char *original_filter_expression
)
1141 struct lttcomm_session_msg lsm
;
1144 unsigned int free_filter_expression
= 0;
1145 struct filter_parser_ctx
*ctx
= NULL
;
1147 * Cast as non-const since we may replace the filter expression
1148 * by a dynamically allocated string. Otherwise, the original
1149 * string is not modified.
1151 char *filter_expression
= (char *) original_filter_expression
;
1153 if (handle
== NULL
|| ev
== NULL
) {
1154 ret
= -LTTNG_ERR_INVALID
;
1159 * Empty filter string will always be rejected by the parser
1160 * anyway, so treat this corner-case early to eliminate
1161 * lttng_fmemopen error for 0-byte allocation.
1163 if (filter_expression
&& filter_expression
[0] == '\0') {
1164 ret
= -LTTNG_ERR_INVALID
;
1168 memset(&lsm
, 0, sizeof(lsm
));
1170 /* If no channel name, send empty string. */
1171 if (channel_name
== NULL
) {
1172 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1173 sizeof(lsm
.u
.disable
.channel_name
));
1175 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1176 sizeof(lsm
.u
.disable
.channel_name
));
1179 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1181 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1182 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1184 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1185 sizeof(lsm
.session
.name
));
1186 lsm
.u
.disable
.bytecode_len
= 0;
1189 * For the JUL domain, a filter is enforced except for the
1190 * disable all event. This is done to avoid having the event in
1191 * all sessions thus filtering by logger name.
1193 if (filter_expression
== NULL
&&
1194 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1195 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1196 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1201 * We have a filter, so we need to set up a variable-length
1202 * memory block from where to send the data.
1205 /* Parse filter expression */
1206 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1207 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1208 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1209 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1210 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1211 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1214 /* Setup JUL filter if needed. */
1215 agent_filter
= set_agent_filter(filter_expression
, ev
);
1216 if (!agent_filter
) {
1217 if (!filter_expression
) {
1219 * No JUL and no filter, just skip
1226 * With a JUL filter, the original filter has
1227 * been added to it thus replace the filter
1230 filter_expression
= agent_filter
;
1231 free_filter_expression
= 1;
1235 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1240 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1241 + bytecode_get_len(&ctx
->bytecode
->b
);
1242 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1245 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1246 + lsm
.u
.disable
.expression_len
);
1248 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1252 /* Add filter expression. */
1253 if (lsm
.u
.disable
.expression_len
!= 0) {
1256 lsm
.u
.disable
.expression_len
);
1258 /* Add filter bytecode next. */
1259 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1261 + lsm
.u
.disable
.expression_len
,
1263 lsm
.u
.disable
.bytecode_len
);
1266 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1267 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1271 if (filter_expression
&& ctx
) {
1272 filter_bytecode_free(ctx
);
1273 filter_ir_free(ctx
);
1274 filter_parser_ctx_free(ctx
);
1277 if (free_filter_expression
) {
1279 * The filter expression has been replaced and must be freed as
1280 * it is not the original filter expression received as a
1283 free(filter_expression
);
1287 * Return directly to the caller and don't ask the sessiond since
1288 * something went wrong in the parsing of data above.
1293 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1298 * Disable event(s) of a channel and domain.
1299 * If no event name is specified, all events are disabled.
1300 * If no channel name is specified, the default 'channel0' is used.
1301 * Returns size of returned session payload data or a negative error code.
1303 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1304 const char *channel_name
)
1306 struct lttng_event ev
;
1308 memset(&ev
, 0, sizeof(ev
));
1310 ev
.type
= LTTNG_EVENT_ALL
;
1311 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1312 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1315 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1317 struct lttng_channel
*channel
= NULL
;
1318 struct lttng_channel_extended
*extended
= NULL
;
1324 /* Validate domain. */
1325 switch (domain
->type
) {
1326 case LTTNG_DOMAIN_UST
:
1327 switch (domain
->buf_type
) {
1328 case LTTNG_BUFFER_PER_UID
:
1329 case LTTNG_BUFFER_PER_PID
:
1335 case LTTNG_DOMAIN_KERNEL
:
1336 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1344 channel
= zmalloc(sizeof(*channel
));
1349 extended
= zmalloc(sizeof(*extended
));
1354 channel
->attr
.extended
.ptr
= extended
;
1356 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1364 void lttng_channel_destroy(struct lttng_channel
*channel
)
1370 if (channel
->attr
.extended
.ptr
) {
1371 free(channel
->attr
.extended
.ptr
);
1377 * Enable channel per domain
1378 * Returns size of returned session payload data or a negative error code.
1380 int lttng_enable_channel(struct lttng_handle
*handle
,
1381 struct lttng_channel
*in_chan
)
1383 struct lttcomm_session_msg lsm
;
1384 size_t total_buffer_size_needed_per_cpu
= 0;
1386 /* NULL arguments are forbidden. No default values. */
1387 if (handle
== NULL
|| in_chan
== NULL
) {
1388 return -LTTNG_ERR_INVALID
;
1391 memset(&lsm
, 0, sizeof(lsm
));
1392 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1393 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1395 if (!in_chan
->attr
.extended
.ptr
) {
1396 struct lttng_channel
*channel
;
1397 struct lttng_channel_extended
*extended
;
1399 channel
= lttng_channel_create(&handle
->domain
);
1401 return -LTTNG_ERR_NOMEM
;
1405 * Create a new channel in order to use default extended
1408 extended
= (struct lttng_channel_extended
*)
1409 channel
->attr
.extended
.ptr
;
1410 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1411 lttng_channel_destroy(channel
);
1413 struct lttng_channel_extended
*extended
;
1415 extended
= (struct lttng_channel_extended
*)
1416 in_chan
->attr
.extended
.ptr
;
1417 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1421 * Verify that the amount of memory required to create the requested
1422 * buffer is available on the system at the moment.
1424 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1425 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1426 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1427 return -LTTNG_ERR_NOMEM
;
1430 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1431 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1433 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1434 sizeof(lsm
.session
.name
));
1436 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1440 * All tracing will be stopped for registered events of the channel.
1441 * Returns size of returned session payload data or a negative error code.
1443 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1445 struct lttcomm_session_msg lsm
;
1447 /* Safety check. Both are mandatory. */
1448 if (handle
== NULL
|| name
== NULL
) {
1449 return -LTTNG_ERR_INVALID
;
1452 memset(&lsm
, 0, sizeof(lsm
));
1454 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1456 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1457 sizeof(lsm
.u
.disable
.channel_name
));
1459 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1461 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1462 sizeof(lsm
.session
.name
));
1464 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1468 * Lists all available tracepoints of domain.
1469 * Sets the contents of the events array.
1470 * Returns the number of lttng_event entries in events;
1471 * on error, returns a negative value.
1473 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1474 struct lttng_event
**events
)
1477 struct lttcomm_session_msg lsm
;
1479 if (handle
== NULL
) {
1480 return -LTTNG_ERR_INVALID
;
1483 memset(&lsm
, 0, sizeof(lsm
));
1484 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1485 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1487 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1492 return ret
/ sizeof(struct lttng_event
);
1496 * Lists all available tracepoint fields of domain.
1497 * Sets the contents of the event field array.
1498 * Returns the number of lttng_event_field entries in events;
1499 * on error, returns a negative value.
1501 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1502 struct lttng_event_field
**fields
)
1505 struct lttcomm_session_msg lsm
;
1507 if (handle
== NULL
) {
1508 return -LTTNG_ERR_INVALID
;
1511 memset(&lsm
, 0, sizeof(lsm
));
1512 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1513 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1515 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1520 return ret
/ sizeof(struct lttng_event_field
);
1524 * Lists all available kernel system calls. Allocates and sets the contents of
1527 * Returns the number of lttng_event entries in events; on error, returns a
1530 int lttng_list_syscalls(struct lttng_event
**events
)
1533 struct lttcomm_session_msg lsm
;
1536 return -LTTNG_ERR_INVALID
;
1539 memset(&lsm
, 0, sizeof(lsm
));
1540 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1541 /* Force kernel domain for system calls. */
1542 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1544 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1549 return ret
/ sizeof(struct lttng_event
);
1553 * Returns a human readable string describing
1554 * the error code (a negative value).
1556 const char *lttng_strerror(int code
)
1558 return error_get_str(code
);
1561 enum lttng_error_code
lttng_create_session_ext(
1562 struct lttng_session_descriptor
*session_descriptor
)
1564 enum lttng_error_code ret_code
;
1565 struct lttcomm_session_msg lsm
= {
1566 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1569 struct lttng_buffer_view reply_view
;
1571 bool sessiond_must_generate_ouput
;
1572 struct lttng_dynamic_buffer payload
;
1574 size_t descriptor_size
;
1575 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1577 lttng_dynamic_buffer_init(&payload
);
1578 if (!session_descriptor
) {
1579 ret_code
= LTTNG_ERR_INVALID
;
1583 sessiond_must_generate_ouput
=
1584 !lttng_session_descriptor_is_output_destination_initialized(
1585 session_descriptor
);
1586 if (sessiond_must_generate_ouput
) {
1587 const char *home_dir
= utils_get_home_dir();
1588 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1590 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1591 ret_code
= LTTNG_ERR_FATAL
;
1595 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1596 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1599 ret_code
= LTTNG_ERR_NOMEM
;
1604 descriptor_size
= payload
.size
;
1605 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1608 ret_code
= LTTNG_ERR_INVALID
;
1611 descriptor_size
= payload
.size
- descriptor_size
;
1612 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1614 /* Command returns a session descriptor on success. */
1615 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1616 payload
.size
, &reply
);
1617 if (reply_ret
< 0) {
1618 ret_code
= -reply_ret
;
1620 } else if (reply_ret
== 0) {
1621 /* Socket unexpectedly closed by the session daemon. */
1622 ret_code
= LTTNG_ERR_FATAL
;
1626 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1627 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1630 ret_code
= LTTNG_ERR_FATAL
;
1633 ret_code
= LTTNG_OK
;
1634 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1637 lttng_dynamic_buffer_reset(&payload
);
1638 lttng_session_descriptor_destroy(descriptor_reply
);
1643 * Create a new session using name and url for destination.
1645 * Return 0 on success else a negative LTTng error code.
1647 int lttng_create_session(const char *name
, const char *url
)
1651 struct lttng_uri
*uris
= NULL
;
1652 struct lttng_session_descriptor
*descriptor
= NULL
;
1653 enum lttng_error_code ret_code
;
1656 ret
= -LTTNG_ERR_INVALID
;
1660 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1662 ret
= -LTTNG_ERR_INVALID
;
1667 descriptor
= lttng_session_descriptor_create(name
);
1670 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1671 ret
= -LTTNG_ERR_INVALID
;
1674 descriptor
= lttng_session_descriptor_local_create(name
,
1678 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1682 ret
= -LTTNG_ERR_INVALID
;
1686 ret
= -LTTNG_ERR_INVALID
;
1689 ret_code
= lttng_create_session_ext(descriptor
);
1690 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1692 lttng_session_descriptor_destroy(descriptor
);
1698 * Create a session exclusively used for snapshot.
1700 * Return 0 on success else a negative LTTng error code.
1702 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1705 enum lttng_error_code ret_code
;
1707 struct lttng_uri
*uris
= NULL
;
1708 struct lttng_session_descriptor
*descriptor
= NULL
;
1711 ret
= -LTTNG_ERR_INVALID
;
1715 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1717 ret
= -LTTNG_ERR_INVALID
;
1721 * If the user does not specify a custom subdir, use the session name.
1723 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1724 strlen(uris
[0].subdir
) == 0) {
1725 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1728 PERROR("Failed to set session name as network destination sub-directory");
1729 ret
= -LTTNG_ERR_FATAL
;
1731 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1732 /* Truncated output. */
1733 ret
= -LTTNG_ERR_INVALID
;
1740 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1743 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1744 ret
= -LTTNG_ERR_INVALID
;
1747 descriptor
= lttng_session_descriptor_snapshot_local_create(
1752 descriptor
= lttng_session_descriptor_snapshot_network_create(
1758 ret
= -LTTNG_ERR_INVALID
;
1762 ret
= -LTTNG_ERR_INVALID
;
1765 ret_code
= lttng_create_session_ext(descriptor
);
1766 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1768 lttng_session_descriptor_destroy(descriptor
);
1774 * Create a session exclusively used for live.
1776 * Return 0 on success else a negative LTTng error code.
1778 int lttng_create_session_live(const char *name
, const char *url
,
1779 unsigned int timer_interval
)
1782 enum lttng_error_code ret_code
;
1783 struct lttng_session_descriptor
*descriptor
= NULL
;
1786 ret
= -LTTNG_ERR_INVALID
;
1791 descriptor
= lttng_session_descriptor_live_network_create(
1792 name
, url
, NULL
, timer_interval
);
1794 descriptor
= lttng_session_descriptor_live_create(
1795 name
, timer_interval
);
1798 ret
= -LTTNG_ERR_INVALID
;
1801 ret_code
= lttng_create_session_ext(descriptor
);
1802 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1804 lttng_session_descriptor_destroy(descriptor
);
1809 * Stop the session and wait for the data before destroying it
1811 * Return 0 on success else a negative LTTng error code.
1813 int lttng_destroy_session(const char *session_name
)
1816 enum lttng_error_code ret_code
;
1817 enum lttng_destruction_handle_status status
;
1818 struct lttng_destruction_handle
*handle
= NULL
;
1821 * Stop the tracing and wait for the data to be
1824 ret
= _lttng_stop_tracing(session_name
, 1);
1825 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1829 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
1830 if (ret_code
!= LTTNG_OK
) {
1831 ret
= (int) -ret_code
;
1836 /* Block until the completion of the destruction of the session. */
1837 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
1838 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
1839 ret
= -LTTNG_ERR_UNK
;
1843 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
1844 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
1845 ret
= -LTTNG_ERR_UNK
;
1848 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1850 lttng_destruction_handle_destroy(handle
);
1855 * Destroy the session without waiting for the data.
1857 int lttng_destroy_session_no_wait(const char *session_name
)
1859 enum lttng_error_code ret_code
;
1861 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
1862 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
1866 * Ask the session daemon for all available sessions.
1867 * Sets the contents of the sessions array.
1868 * Returns the number of lttng_session entries in sessions;
1869 * on error, returns a negative value.
1871 int lttng_list_sessions(struct lttng_session
**out_sessions
)
1874 struct lttcomm_session_msg lsm
;
1875 const size_t session_size
= sizeof(struct lttng_session
) +
1876 sizeof(struct lttng_session_extended
);
1877 size_t session_count
, i
;
1878 struct lttng_session_extended
*sessions_extended_begin
;
1879 struct lttng_session
*sessions
= NULL
;
1881 memset(&lsm
, 0, sizeof(lsm
));
1882 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1883 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
1888 ret
= -LTTNG_ERR_FATAL
;
1892 if (ret
% session_size
) {
1893 ret
= -LTTNG_ERR_UNK
;
1895 *out_sessions
= NULL
;
1898 session_count
= (size_t) ret
/ session_size
;
1899 sessions_extended_begin
= (struct lttng_session_extended
*)
1900 (&sessions
[session_count
]);
1902 /* Set extended session info pointers. */
1903 for (i
= 0; i
< session_count
; i
++) {
1904 struct lttng_session
*session
= &sessions
[i
];
1905 struct lttng_session_extended
*extended
=
1906 &(sessions_extended_begin
[i
]);
1908 session
->extended
.ptr
= extended
;
1911 ret
= (int) session_count
;
1912 *out_sessions
= sessions
;
1917 enum lttng_error_code
lttng_session_get_creation_time(
1918 const struct lttng_session
*session
, uint64_t *creation_time
)
1920 enum lttng_error_code ret
= LTTNG_OK
;
1921 struct lttng_session_extended
*extended
;
1923 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
1924 ret
= LTTNG_ERR_INVALID
;
1928 extended
= session
->extended
.ptr
;
1929 if (!extended
->creation_time
.is_set
) {
1930 /* Not created on the session daemon yet. */
1931 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
1934 *creation_time
= extended
->creation_time
.value
;
1939 int lttng_set_session_shm_path(const char *session_name
,
1940 const char *shm_path
)
1942 struct lttcomm_session_msg lsm
;
1944 if (session_name
== NULL
) {
1945 return -LTTNG_ERR_INVALID
;
1948 memset(&lsm
, 0, sizeof(lsm
));
1949 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1951 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1952 sizeof(lsm
.session
.name
));
1953 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1954 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1956 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1960 * Ask the session daemon for all available domains of a session.
1961 * Sets the contents of the domains array.
1962 * Returns the number of lttng_domain entries in domains;
1963 * on error, returns a negative value.
1965 int lttng_list_domains(const char *session_name
,
1966 struct lttng_domain
**domains
)
1969 struct lttcomm_session_msg lsm
;
1971 if (session_name
== NULL
) {
1972 return -LTTNG_ERR_INVALID
;
1975 memset(&lsm
, 0, sizeof(lsm
));
1976 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1978 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1979 sizeof(lsm
.session
.name
));
1981 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1986 return ret
/ sizeof(struct lttng_domain
);
1990 * Ask the session daemon for all available channels of a session.
1991 * Sets the contents of the channels array.
1992 * Returns the number of lttng_channel entries in channels;
1993 * on error, returns a negative value.
1995 int lttng_list_channels(struct lttng_handle
*handle
,
1996 struct lttng_channel
**channels
)
1999 size_t channel_count
, i
;
2000 const size_t channel_size
= sizeof(struct lttng_channel
) +
2001 sizeof(struct lttng_channel_extended
);
2002 struct lttcomm_session_msg lsm
;
2005 if (handle
== NULL
) {
2006 ret
= -LTTNG_ERR_INVALID
;
2010 memset(&lsm
, 0, sizeof(lsm
));
2011 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2012 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2013 sizeof(lsm
.session
.name
));
2015 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2017 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2022 if (ret
% channel_size
) {
2023 ret
= -LTTNG_ERR_UNK
;
2028 channel_count
= (size_t) ret
/ channel_size
;
2030 /* Set extended info pointers */
2031 extended_at
= ((void *) *channels
) +
2032 channel_count
* sizeof(struct lttng_channel
);
2033 for (i
= 0; i
< channel_count
; i
++) {
2034 struct lttng_channel
*chan
= &(*channels
)[i
];
2036 chan
->attr
.extended
.ptr
= extended_at
;
2037 extended_at
+= sizeof(struct lttng_channel_extended
);
2040 ret
= (int) channel_count
;
2046 * Ask the session daemon for all available events of a session channel.
2047 * Sets the contents of the events array.
2048 * Returns the number of lttng_event entries in events;
2049 * on error, returns a negative value.
2051 int lttng_list_events(struct lttng_handle
*handle
,
2052 const char *channel_name
, struct lttng_event
**events
)
2055 struct lttcomm_session_msg lsm
;
2056 struct lttcomm_event_command_header
*cmd_header
= NULL
;
2057 size_t cmd_header_len
;
2058 uint32_t nb_events
, i
;
2060 char *reception_buffer
= NULL
;
2061 struct lttng_dynamic_buffer listing
;
2064 /* Safety check. An handle and channel name are mandatory */
2065 if (handle
== NULL
|| channel_name
== NULL
) {
2066 return -LTTNG_ERR_INVALID
;
2069 memset(&lsm
, 0, sizeof(lsm
));
2070 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2071 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2072 sizeof(lsm
.session
.name
));
2073 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2074 sizeof(lsm
.u
.list
.channel_name
));
2075 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2077 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2078 (void **) &reception_buffer
, (void **) &cmd_header
,
2085 ret
= -LTTNG_ERR_UNK
;
2089 /* Set number of events and free command header */
2090 nb_events
= cmd_header
->nb_events
;
2091 if (nb_events
> INT_MAX
) {
2092 ret
= -LTTNG_ERR_OVERFLOW
;
2099 * The buffer that is returned must contain a "flat" version of
2100 * the events that are returned. In other words, all pointers
2101 * within an lttng_event must point to a location within the returned
2102 * buffer so that the user may free everything by simply calling free()
2103 * on the returned buffer. This is needed in order to maintain API
2106 * A first pass is performed to compute the size of the buffer that
2107 * must be allocated. A second pass is then performed to setup
2108 * the returned events so that their members always point within the
2111 * The layout of the returned buffer is as follows:
2112 * - struct lttng_event[nb_events],
2113 * - nb_events times the following:
2114 * - struct lttng_event_extended,
2115 * - flattened version of userspace_probe_location
2116 * - filter_expression
2118 * - padding to align to 64-bits
2120 comm_ext_at
= reception_buffer
+
2121 (nb_events
* sizeof(struct lttng_event
));
2122 storage_req
= nb_events
* sizeof(struct lttng_event
);
2124 for (i
= 0; i
< nb_events
; i
++) {
2125 struct lttcomm_event_extended_header
*ext_comm
=
2126 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2127 int probe_storage_req
= 0;
2129 comm_ext_at
+= sizeof(*ext_comm
);
2130 comm_ext_at
+= ext_comm
->filter_len
;
2132 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2134 if (ext_comm
->userspace_probe_location_len
) {
2135 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2136 struct lttng_buffer_view probe_location_view
;
2138 probe_location_view
= lttng_buffer_view_init(
2140 ext_comm
->userspace_probe_location_len
);
2143 * Create a temporary userspace probe location to
2144 * determine the size needed by a "flattened" version
2145 * of that same probe location.
2147 ret
= lttng_userspace_probe_location_create_from_buffer(
2148 &probe_location_view
, &probe_location
);
2150 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2154 ret
= lttng_userspace_probe_location_flatten(
2155 probe_location
, NULL
);
2156 lttng_userspace_probe_location_destroy(probe_location
);
2158 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2162 probe_storage_req
= ret
;
2163 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2166 storage_req
+= sizeof(struct lttng_event_extended
);
2167 storage_req
+= ext_comm
->filter_len
;
2168 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2169 /* Padding to ensure the flat probe is aligned. */
2170 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2171 storage_req
+= probe_storage_req
;
2174 lttng_dynamic_buffer_init(&listing
);
2176 * We must ensure that "listing" is never resized so as to preserve
2177 * the validity of the flattened objects.
2179 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2181 ret
= -LTTNG_ERR_NOMEM
;
2185 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2186 nb_events
* sizeof(struct lttng_event
));
2188 ret
= -LTTNG_ERR_NOMEM
;
2189 goto free_dynamic_buffer
;
2192 comm_ext_at
= reception_buffer
+
2193 (nb_events
* sizeof(struct lttng_event
));
2194 for (i
= 0; i
< nb_events
; i
++) {
2195 struct lttng_event
*event
= (struct lttng_event
*)
2196 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2197 struct lttcomm_event_extended_header
*ext_comm
=
2198 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2199 struct lttng_event_extended
*event_extended
=
2200 (struct lttng_event_extended
*)
2201 (listing
.data
+ listing
.size
);
2203 /* Insert struct lttng_event_extended. */
2204 ret
= lttng_dynamic_buffer_set_size(&listing
,
2205 listing
.size
+ sizeof(*event_extended
));
2207 ret
= -LTTNG_ERR_NOMEM
;
2208 goto free_dynamic_buffer
;
2210 event
->extended
.ptr
= event_extended
;
2212 comm_ext_at
+= sizeof(*ext_comm
);
2214 /* Insert filter expression. */
2215 if (ext_comm
->filter_len
) {
2216 event_extended
->filter_expression
= listing
.data
+
2218 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2219 ext_comm
->filter_len
);
2221 ret
= -LTTNG_ERR_NOMEM
;
2222 goto free_dynamic_buffer
;
2224 comm_ext_at
+= ext_comm
->filter_len
;
2227 /* Insert exclusions. */
2228 if (ext_comm
->nb_exclusions
) {
2229 event_extended
->exclusions
.count
=
2230 ext_comm
->nb_exclusions
;
2231 event_extended
->exclusions
.strings
=
2232 listing
.data
+ listing
.size
;
2234 ret
= lttng_dynamic_buffer_append(&listing
,
2236 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2238 ret
= -LTTNG_ERR_NOMEM
;
2239 goto free_dynamic_buffer
;
2241 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2244 /* Insert padding to align to 64-bits. */
2245 ret
= lttng_dynamic_buffer_set_size(&listing
,
2246 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2248 ret
= -LTTNG_ERR_NOMEM
;
2249 goto free_dynamic_buffer
;
2252 /* Insert flattened userspace probe location. */
2253 if (ext_comm
->userspace_probe_location_len
) {
2254 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2255 struct lttng_buffer_view probe_location_view
;
2257 probe_location_view
= lttng_buffer_view_init(
2259 ext_comm
->userspace_probe_location_len
);
2261 ret
= lttng_userspace_probe_location_create_from_buffer(
2262 &probe_location_view
, &probe_location
);
2264 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2265 goto free_dynamic_buffer
;
2268 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2269 (listing
.data
+ listing
.size
);
2270 ret
= lttng_userspace_probe_location_flatten(
2271 probe_location
, &listing
);
2272 lttng_userspace_probe_location_destroy(probe_location
);
2274 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2275 goto free_dynamic_buffer
;
2278 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2282 /* Don't reset listing buffer as we return its content. */
2283 *events
= (struct lttng_event
*) listing
.data
;
2284 lttng_dynamic_buffer_init(&listing
);
2285 ret
= (int) nb_events
;
2286 free_dynamic_buffer
:
2287 lttng_dynamic_buffer_reset(&listing
);
2290 free(reception_buffer
);
2295 * Sets the tracing_group variable with name.
2296 * This function allocates memory pointed to by tracing_group.
2297 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2299 int lttng_set_tracing_group(const char *name
)
2302 return -LTTNG_ERR_INVALID
;
2305 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2306 return -LTTNG_ERR_FATAL
;
2312 int lttng_calibrate(struct lttng_handle
*handle
,
2313 struct lttng_calibrate
*calibrate
)
2316 * This command was removed in LTTng 2.9.
2318 return -LTTNG_ERR_UND
;
2322 * Set default channel attributes.
2323 * If either or both of the arguments are null, attr content is zeroe'd.
2325 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2326 struct lttng_channel_attr
*attr
)
2328 struct lttng_channel_extended
*extended
;
2331 if (attr
== NULL
|| domain
== NULL
) {
2335 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2336 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2338 /* Same for all domains. */
2339 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2340 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2341 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2343 switch (domain
->type
) {
2344 case LTTNG_DOMAIN_KERNEL
:
2345 attr
->switch_timer_interval
=
2346 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2347 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2348 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2349 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2350 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2352 extended
->monitor_timer_interval
=
2353 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2354 extended
->blocking_timeout
=
2355 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2358 case LTTNG_DOMAIN_UST
:
2359 switch (domain
->buf_type
) {
2360 case LTTNG_BUFFER_PER_UID
:
2361 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2362 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2363 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2364 attr
->switch_timer_interval
=
2365 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2366 attr
->read_timer_interval
=
2367 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2369 extended
->monitor_timer_interval
=
2370 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2371 extended
->blocking_timeout
=
2372 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2375 case LTTNG_BUFFER_PER_PID
:
2377 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2378 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2379 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2380 attr
->switch_timer_interval
=
2381 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2382 attr
->read_timer_interval
=
2383 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2385 extended
->monitor_timer_interval
=
2386 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2387 extended
->blocking_timeout
=
2388 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2393 /* Default behavior: leave set to 0. */
2397 attr
->extended
.ptr
= extended
;
2400 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2401 uint64_t *discarded_events
)
2404 struct lttng_channel_extended
*chan_ext
;
2406 if (!channel
|| !discarded_events
) {
2407 ret
= -LTTNG_ERR_INVALID
;
2411 chan_ext
= channel
->attr
.extended
.ptr
;
2414 * This can happen since the lttng_channel structure is
2415 * used for other tasks where this pointer is never set.
2417 *discarded_events
= 0;
2421 *discarded_events
= chan_ext
->discarded_events
;
2426 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2427 uint64_t *lost_packets
)
2430 struct lttng_channel_extended
*chan_ext
;
2432 if (!channel
|| !lost_packets
) {
2433 ret
= -LTTNG_ERR_INVALID
;
2437 chan_ext
= channel
->attr
.extended
.ptr
;
2440 * This can happen since the lttng_channel structure is
2441 * used for other tasks where this pointer is never set.
2447 *lost_packets
= chan_ext
->lost_packets
;
2452 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2453 uint64_t *monitor_timer_interval
)
2457 if (!chan
|| !monitor_timer_interval
) {
2458 ret
= -LTTNG_ERR_INVALID
;
2462 if (!chan
->attr
.extended
.ptr
) {
2463 ret
= -LTTNG_ERR_INVALID
;
2467 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2468 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2473 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2474 uint64_t monitor_timer_interval
)
2478 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2479 ret
= -LTTNG_ERR_INVALID
;
2483 ((struct lttng_channel_extended
*)
2484 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2485 monitor_timer_interval
;
2490 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2491 int64_t *blocking_timeout
)
2495 if (!chan
|| !blocking_timeout
) {
2496 ret
= -LTTNG_ERR_INVALID
;
2500 if (!chan
->attr
.extended
.ptr
) {
2501 ret
= -LTTNG_ERR_INVALID
;
2505 *blocking_timeout
= ((struct lttng_channel_extended
*)
2506 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2511 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2512 int64_t blocking_timeout
)
2515 int64_t msec_timeout
;
2517 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2518 ret
= -LTTNG_ERR_INVALID
;
2522 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2523 ret
= -LTTNG_ERR_INVALID
;
2528 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2529 * us to accept a narrower range of values (msecs expressed as a signed
2532 msec_timeout
= blocking_timeout
/ 1000;
2533 if (msec_timeout
!= (int32_t) msec_timeout
) {
2534 ret
= -LTTNG_ERR_INVALID
;
2538 ((struct lttng_channel_extended
*)
2539 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2546 * Check if session daemon is alive.
2548 * Return 1 if alive or 0 if not.
2549 * On error returns a negative value.
2551 int lttng_session_daemon_alive(void)
2555 ret
= set_session_daemon_path();
2561 if (*sessiond_sock_path
== '\0') {
2563 * No socket path set. Weird error which means the constructor
2569 ret
= try_connect_sessiond(sessiond_sock_path
);
2580 * Set URL for a consumer for a session and domain.
2582 * Return 0 on success, else a negative value.
2584 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2585 const char *control_url
, const char *data_url
)
2589 struct lttcomm_session_msg lsm
;
2590 struct lttng_uri
*uris
= NULL
;
2592 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2593 return -LTTNG_ERR_INVALID
;
2596 memset(&lsm
, 0, sizeof(lsm
));
2598 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2600 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2601 sizeof(lsm
.session
.name
));
2602 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2604 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2606 return -LTTNG_ERR_INVALID
;
2609 lsm
.u
.uri
.size
= size
;
2611 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2612 sizeof(struct lttng_uri
) * size
, NULL
);
2621 int lttng_enable_consumer(struct lttng_handle
*handle
);
2622 int lttng_enable_consumer(struct lttng_handle
*handle
)
2630 int lttng_disable_consumer(struct lttng_handle
*handle
);
2631 int lttng_disable_consumer(struct lttng_handle
*handle
)
2639 int _lttng_create_session_ext(const char *name
, const char *url
,
2640 const char *datetime
);
2641 int _lttng_create_session_ext(const char *name
, const char *url
,
2642 const char *datetime
)
2648 * For a given session name, this call checks if the data is ready to be read
2649 * or is still being extracted by the consumer(s) hence not ready to be used by
2652 int lttng_data_pending(const char *session_name
)
2655 struct lttcomm_session_msg lsm
;
2656 uint8_t *pending
= NULL
;
2658 if (session_name
== NULL
) {
2659 return -LTTNG_ERR_INVALID
;
2662 memset(&lsm
, 0, sizeof(lsm
));
2663 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2665 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2666 sizeof(lsm
.session
.name
));
2668 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2671 } else if (ret
!= 1) {
2672 /* Unexpected payload size */
2673 ret
= -LTTNG_ERR_INVALID
;
2675 } else if (!pending
) {
2676 /* Internal error. */
2677 ret
= -LTTNG_ERR_UNK
;
2681 ret
= (int) *pending
;
2688 * Regenerate the metadata for a session.
2689 * Return 0 on success, a negative error code on error.
2691 int lttng_regenerate_metadata(const char *session_name
)
2694 struct lttcomm_session_msg lsm
;
2696 if (!session_name
) {
2697 ret
= -LTTNG_ERR_INVALID
;
2701 memset(&lsm
, 0, sizeof(lsm
));
2702 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2704 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2705 sizeof(lsm
.session
.name
));
2707 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2718 * Deprecated, replaced by lttng_regenerate_metadata.
2720 int lttng_metadata_regenerate(const char *session_name
)
2722 return lttng_regenerate_metadata(session_name
);
2726 * Regenerate the statedump of a session.
2727 * Return 0 on success, a negative error code on error.
2729 int lttng_regenerate_statedump(const char *session_name
)
2732 struct lttcomm_session_msg lsm
;
2734 if (!session_name
) {
2735 ret
= -LTTNG_ERR_INVALID
;
2739 memset(&lsm
, 0, sizeof(lsm
));
2740 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2742 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2743 sizeof(lsm
.session
.name
));
2745 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2755 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2759 struct lttcomm_session_msg lsm
;
2760 struct lttng_dynamic_buffer buffer
;
2762 struct lttng_buffer_view reply_view
;
2763 struct lttng_trigger
*reply_trigger
= NULL
;
2764 bool send_fd
= false;
2766 enum lttng_domain_type domain_type
;
2768 lttng_dynamic_buffer_init(&buffer
);
2770 ret
= -LTTNG_ERR_INVALID
;
2774 if (!lttng_trigger_validate(trigger
)) {
2775 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2779 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
2782 ret
= lttng_trigger_serialize(trigger
, &buffer
, &fd_to_send
);
2784 ret
= -LTTNG_ERR_UNK
;
2788 if (getenv("LTTNG_REGISTER_TRIGGER_DRY_RUN")) {
2790 * Don't really send the request, just deserialize, validate
2791 * that it is equal to the original trigger (to test
2792 * serialization and deserialization), and return.
2794 struct lttng_buffer_view bv
;
2797 bv
= lttng_buffer_view_from_dynamic_buffer(&buffer
, 0, -1);
2798 sz
= lttng_trigger_create_from_buffer(&bv
, &reply_trigger
);
2799 if (sz
!= bv
.size
) {
2800 ret
= -LTTNG_ERR_UNK
;
2804 if (!reply_trigger
) {
2805 ret
= -LTTNG_ERR_UNK
;
2809 if (!lttng_trigger_is_equal(trigger
, reply_trigger
)) {
2810 ret
= -LTTNG_ERR_UNK
;
2814 /* Give it a dummy name. */
2815 lttng_trigger_set_name(trigger
, "yop");
2821 send_fd
= fd_to_send
>= 0;
2823 memset(&lsm
, 0, sizeof(lsm
));
2824 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
2825 lsm
.domain
.type
= domain_type
;
2826 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
2827 reply_ret
= lttng_ctl_ask_sessiond_fds_varlen_no_cmd_header(&lsm
,
2828 send_fd
? &fd_to_send
: NULL
,
2833 if (reply_ret
< 0) {
2836 } else if (reply_ret
== 0) {
2837 /* Socket unexpectedly closed by the session daemon. */
2838 ret
= -LTTNG_ERR_FATAL
;
2842 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
2843 ret
= lttng_trigger_create_from_buffer(&reply_view
, &reply_trigger
);
2845 ret
= -LTTNG_ERR_FATAL
;
2849 ret
= lttng_trigger_assign(trigger
, reply_trigger
);
2851 ret
= -LTTNG_ERR_FATAL
;
2858 lttng_dynamic_buffer_reset(&buffer
);
2859 lttng_trigger_destroy(reply_trigger
);
2863 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
2866 struct lttcomm_session_msg lsm
;
2867 struct lttng_dynamic_buffer buffer
;
2869 lttng_dynamic_buffer_init(&buffer
);
2871 ret
= -LTTNG_ERR_INVALID
;
2875 if (!lttng_trigger_validate(trigger
)) {
2876 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2880 ret
= lttng_trigger_serialize(trigger
, &buffer
, NULL
);
2882 ret
= -LTTNG_ERR_UNK
;
2886 memset(&lsm
, 0, sizeof(lsm
));
2887 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
2888 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
2889 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
2892 lttng_dynamic_buffer_reset(&buffer
);
2897 * Ask the session daemon for all registered triggers.
2898 * Allocate a lttng_triggers collection.
2899 * On error, returns a negative value.
2901 int lttng_list_triggers(struct lttng_triggers
**triggers
)
2905 struct lttcomm_session_msg lsm
;
2906 struct lttng_buffer_view reply_view
;
2907 struct lttng_triggers
*local_triggers
= NULL
;
2910 memset(&lsm
, 0, sizeof(lsm
));
2911 lsm
.cmd_type
= LTTNG_LIST_TRIGGERS
;
2913 reply_ret
= lttng_ctl_ask_sessiond(&lsm
, &reply
);
2914 if (reply_ret
< 0) {
2917 } else if (reply_ret
== 0) {
2918 /* Socket unexpectedly closed by the session daemon. */
2919 ret
= -LTTNG_ERR_FATAL
;
2923 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
2924 ret
= lttng_triggers_create_from_buffer(&reply_view
, &local_triggers
);
2926 ret
= -LTTNG_ERR_FATAL
;
2930 *triggers
= local_triggers
;
2931 local_triggers
= NULL
;
2935 free(local_triggers
);
2942 static void __attribute__((constructor
)) init(void)
2944 /* Set default session group */
2945 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2951 static void __attribute__((destructor
)) lttng_ctl_exit(void)
2953 free(tracing_group
);