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 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License, version 2.1 only,
11 * as published by the Free Software Foundation.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <common/common.h>
33 #include <common/compat/string.h>
34 #include <common/defaults.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/uri.h>
37 #include <common/utils.h>
38 #include <common/dynamic-buffer.h>
39 #include <lttng/lttng.h>
40 #include <lttng/health-internal.h>
41 #include <lttng/trigger/trigger-internal.h>
42 #include <lttng/endpoint.h>
43 #include <lttng/channel-internal.h>
44 #include <lttng/event-internal.h>
45 #include <lttng/userspace-probe-internal.h>
46 #include <lttng/session-internal.h>
47 #include <lttng/session-descriptor-internal.h>
48 #include <lttng/destruction-handle.h>
49 #include <lttng/tracker-internal.h>
51 #include "filter/filter-ast.h"
52 #include "filter/filter-parser.h"
53 #include "filter/filter-bytecode.h"
54 #include "filter/memstream.h"
55 #include "lttng-ctl-helper.h"
58 static const int print_xml
= 1;
59 #define dbg_printf(fmt, args...) \
60 printf("[debug liblttng-ctl] " fmt, ## args)
62 static const int print_xml
= 0;
63 #define dbg_printf(fmt, args...) \
65 /* do nothing but check printf format */ \
67 printf("[debug liblttnctl] " fmt, ## args); \
71 #define COPY_DOMAIN_PACKED(dst, src) \
73 struct lttng_domain _tmp_domain; \
75 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
79 /* Socket to session daemon for communication */
80 static int sessiond_socket
= -1;
81 static char sessiond_sock_path
[PATH_MAX
];
84 static char *tracing_group
;
90 * Those two variables are used by error.h to silent or control the verbosity of
91 * error message. They are global to the library so application linking with it
92 * are able to compile correctly and also control verbosity of the library.
95 int lttng_opt_verbose
;
99 * Copy string from src to dst and enforce null terminated byte.
102 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
105 strncpy(dst
, src
, len
);
106 /* Enforce the NULL terminated byte */
114 * Copy domain to lttcomm_session_msg domain.
116 * If domain is unknown, default domain will be the kernel.
119 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
120 struct lttng_domain
*src
)
124 case LTTNG_DOMAIN_KERNEL
:
125 case LTTNG_DOMAIN_UST
:
126 case LTTNG_DOMAIN_JUL
:
127 case LTTNG_DOMAIN_LOG4J
:
128 case LTTNG_DOMAIN_PYTHON
:
129 memcpy(dst
, src
, sizeof(struct lttng_domain
));
132 memset(dst
, 0, sizeof(struct lttng_domain
));
139 * Send lttcomm_session_msg to the session daemon.
141 * On success, returns the number of bytes sent (>=0)
142 * On error, returns -1
144 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
149 ret
= -LTTNG_ERR_NO_SESSIOND
;
153 DBG("LSM cmd type : %d", lsm
->cmd_type
);
155 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
156 sizeof(struct lttcomm_session_msg
));
158 ret
= -LTTNG_ERR_FATAL
;
166 * Send var len data to the session daemon.
168 * On success, returns the number of bytes sent (>=0)
169 * On error, returns -1
171 static int send_session_varlen(const void *data
, size_t len
)
176 ret
= -LTTNG_ERR_NO_SESSIOND
;
185 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
187 ret
= -LTTNG_ERR_FATAL
;
195 * Send file descriptors to the session daemon.
197 * On success, returns the number of bytes sent (>=0)
198 * On error, returns -1
200 static int send_session_fds(const int *fds
, size_t nb_fd
)
205 ret
= -LTTNG_ERR_NO_SESSIOND
;
209 if (!fds
|| !nb_fd
) {
214 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
216 ret
= -LTTNG_ERR_FATAL
;
224 * Receive data from the sessiond socket.
226 * On success, returns the number of bytes received (>=0)
227 * On error, returns -1 (recvmsg() error) or -ENOTCONN
229 static int recv_data_sessiond(void *buf
, size_t len
)
234 ret
= -LTTNG_ERR_NO_SESSIOND
;
238 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
240 ret
= -LTTNG_ERR_FATAL
;
248 * Check if we are in the specified group.
250 * If yes return 1, else return -1.
253 int lttng_check_tracing_group(void)
255 gid_t
*grp_list
, tracing_gid
;
256 int grp_list_size
, grp_id
, i
;
258 const char *grp_name
= tracing_group
;
260 /* Get GID of group 'tracing' */
261 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
262 /* If grp_tracing is NULL, the group does not exist. */
266 /* Get number of supplementary group IDs */
267 grp_list_size
= getgroups(0, NULL
);
268 if (grp_list_size
< 0) {
273 /* Alloc group list of the right size */
274 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
279 grp_id
= getgroups(grp_list_size
, grp_list
);
285 for (i
= 0; i
< grp_list_size
; i
++) {
286 if (grp_list
[i
] == tracing_gid
) {
299 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
303 size_t best_mem_info
;
304 size_t num_bytes_requested_total
;
307 * Get the number of CPU currently online to compute the amount of
308 * memory needed to create a buffer for every CPU.
310 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
315 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
318 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
319 * reliable estimate we can get but it is only exposed by the kernel
320 * since 3.14. (See Linux kernel commit:
321 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
323 ret
= utils_get_memory_available(&best_mem_info
);
329 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
330 * is a sanity check for obvious user error.
332 ret
= utils_get_memory_total(&best_mem_info
);
340 return best_mem_info
>= num_bytes_requested_total
;
344 * Try connect to session daemon with sock_path.
346 * Return 0 on success, else -1
348 static int try_connect_sessiond(const char *sock_path
)
352 /* If socket exist, we check if the daemon listens for connect. */
353 ret
= access(sock_path
, F_OK
);
359 ret
= lttcomm_connect_unix_sock(sock_path
);
365 ret
= lttcomm_close_unix_sock(ret
);
367 PERROR("lttcomm_close_unix_sock");
377 * Set sessiond socket path by putting it in the global sessiond_sock_path
380 * Returns 0 on success, negative value on failure (the sessiond socket path
381 * is somehow too long or ENOMEM).
383 static int set_session_daemon_path(void)
385 int in_tgroup
= 0; /* In tracing group. */
391 /* Are we in the tracing group ? */
392 in_tgroup
= lttng_check_tracing_group();
395 if ((uid
== 0) || in_tgroup
) {
396 lttng_ctl_copy_string(sessiond_sock_path
,
397 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
405 ret
= try_connect_sessiond(sessiond_sock_path
);
409 /* Global session daemon not available... */
411 /* ...or not in tracing group (and not root), default */
414 * With GNU C < 2.1, snprintf returns -1 if the target buffer
416 * With GNU C >= 2.1, snprintf returns the required size
417 * (excluding closing null)
419 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
420 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
421 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
433 * Connect to the LTTng session daemon.
435 * On success, return the socket's file descriptor. On error, return -1.
437 LTTNG_HIDDEN
int connect_sessiond(void)
441 ret
= set_session_daemon_path();
446 /* Connect to the sesssion daemon. */
447 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
458 static void reset_global_sessiond_connection_state(void)
460 sessiond_socket
= -1;
465 * Clean disconnect from the session daemon.
467 * On success, return 0. On error, return -1.
469 static int disconnect_sessiond(void)
474 ret
= lttcomm_close_unix_sock(sessiond_socket
);
475 reset_global_sessiond_connection_state();
481 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
489 ret
= -LTTNG_ERR_INVALID
;
499 ret
= recv_data_sessiond(buf
, len
);
505 ret
= -LTTNG_ERR_INVALID
;
509 /* Move ownership of command header buffer to user. */
514 /* No command header. */
530 * Ask the session daemon a specific command and put the data into buf.
531 * Takes extra var. len. data and file descriptors as input to send to the
534 * Return size of data (only payload, not header) or a negative error code.
537 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
538 const int *fds
, size_t nb_fd
, const void *vardata
,
539 size_t vardata_len
, void **user_payload_buf
,
540 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
544 struct lttcomm_lttng_msg llm
;
546 ret
= connect_sessiond();
548 ret
= -LTTNG_ERR_NO_SESSIOND
;
551 sessiond_socket
= ret
;
555 /* Send command to session daemon */
556 ret
= send_session_msg(lsm
);
558 /* Ret value is a valid lttng error code. */
561 /* Send var len data */
562 ret
= send_session_varlen(vardata
, vardata_len
);
564 /* Ret value is a valid lttng error code. */
569 ret
= send_session_fds(fds
, nb_fd
);
571 /* Ret value is a valid lttng error code. */
575 /* Get header from data transmission */
576 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
578 /* Ret value is a valid lttng error code. */
582 /* Check error code if OK */
583 if (llm
.ret_code
!= LTTNG_OK
) {
588 /* Get command header from data transmission */
589 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
590 user_cmd_header_buf
, user_cmd_header_len
);
595 /* Get payload from data transmission */
596 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
605 disconnect_sessiond();
610 * Create lttng handle and return pointer.
612 * The returned pointer will be NULL in case of malloc() error.
614 struct lttng_handle
*lttng_create_handle(const char *session_name
,
615 struct lttng_domain
*domain
)
617 struct lttng_handle
*handle
= NULL
;
619 handle
= zmalloc(sizeof(struct lttng_handle
));
620 if (handle
== NULL
) {
621 PERROR("malloc handle");
625 /* Copy session name */
626 lttng_ctl_copy_string(handle
->session_name
, session_name
,
627 sizeof(handle
->session_name
));
629 /* Copy lttng domain or leave initialized to 0. */
631 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
639 * Destroy handle by free(3) the pointer.
641 void lttng_destroy_handle(struct lttng_handle
*handle
)
647 * Register an outside consumer.
649 * Returns size of returned session payload data or a negative error code.
651 int lttng_register_consumer(struct lttng_handle
*handle
,
652 const char *socket_path
)
654 struct lttcomm_session_msg lsm
;
656 if (handle
== NULL
|| socket_path
== NULL
) {
657 return -LTTNG_ERR_INVALID
;
660 memset(&lsm
, 0, sizeof(lsm
));
661 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
662 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
663 sizeof(lsm
.session
.name
));
664 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
666 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
667 sizeof(lsm
.u
.reg
.path
));
669 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
673 * Start tracing for all traces of the session.
675 * Returns size of returned session payload data or a negative error code.
677 int lttng_start_tracing(const char *session_name
)
679 struct lttcomm_session_msg lsm
;
681 if (session_name
== NULL
) {
682 return -LTTNG_ERR_INVALID
;
685 memset(&lsm
, 0, sizeof(lsm
));
686 lsm
.cmd_type
= LTTNG_START_TRACE
;
688 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
689 sizeof(lsm
.session
.name
));
691 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
695 * Stop tracing for all traces of the session.
697 static int _lttng_stop_tracing(const char *session_name
, int wait
)
700 struct lttcomm_session_msg lsm
;
702 if (session_name
== NULL
) {
703 return -LTTNG_ERR_INVALID
;
706 memset(&lsm
, 0, sizeof(lsm
));
707 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
709 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
710 sizeof(lsm
.session
.name
));
712 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
713 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
721 /* Check for data availability */
723 data_ret
= lttng_data_pending(session_name
);
725 /* Return the data available call error. */
731 * Data sleep time before retrying (in usec). Don't sleep if the
732 * call returned value indicates availability.
735 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
737 } while (data_ret
!= 0);
745 * Stop tracing and wait for data availability.
747 int lttng_stop_tracing(const char *session_name
)
749 return _lttng_stop_tracing(session_name
, 1);
753 * Stop tracing but _don't_ wait for data availability.
755 int lttng_stop_tracing_no_wait(const char *session_name
)
757 return _lttng_stop_tracing(session_name
, 0);
761 * Add context to a channel.
763 * If the given channel is NULL, add the contexts to all channels.
764 * The event_name param is ignored.
766 * Returns the size of the returned payload data or a negative error code.
768 int lttng_add_context(struct lttng_handle
*handle
,
769 struct lttng_event_context
*ctx
, const char *event_name
,
770 const char *channel_name
)
775 struct lttcomm_session_msg lsm
;
777 /* Safety check. Both are mandatory. */
778 if (handle
== NULL
|| ctx
== NULL
) {
779 ret
= -LTTNG_ERR_INVALID
;
783 memset(&lsm
, 0, sizeof(lsm
));
784 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
786 /* If no channel name, send empty string. */
787 if (channel_name
== NULL
) {
788 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
789 sizeof(lsm
.u
.context
.channel_name
));
791 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
792 sizeof(lsm
.u
.context
.channel_name
));
795 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
796 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
797 sizeof(lsm
.session
.name
));
799 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
800 size_t provider_len
, ctx_len
;
801 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
802 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
804 if (!provider_name
|| !ctx_name
) {
805 ret
= -LTTNG_ERR_INVALID
;
809 provider_len
= strlen(provider_name
);
810 if (provider_len
== 0) {
811 ret
= -LTTNG_ERR_INVALID
;
814 lsm
.u
.context
.provider_name_len
= provider_len
;
816 ctx_len
= strlen(ctx_name
);
818 ret
= -LTTNG_ERR_INVALID
;
821 lsm
.u
.context
.context_name_len
= ctx_len
;
823 len
= provider_len
+ ctx_len
;
826 ret
= -LTTNG_ERR_NOMEM
;
830 memcpy(buf
, provider_name
, provider_len
);
831 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
833 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
835 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
837 * Don't leak application addresses to the sessiond.
838 * This is only necessary when ctx is for an app ctx otherwise
839 * the values inside the union (type & config) are overwritten.
841 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
842 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
845 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
852 * Enable event(s) for a channel.
854 * If no event name is specified, all events are enabled.
855 * If no channel name is specified, the default 'channel0' is used.
857 * Returns size of returned session payload data or a negative error code.
859 int lttng_enable_event(struct lttng_handle
*handle
,
860 struct lttng_event
*ev
, const char *channel_name
)
862 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
867 * Create or enable an event with a filter expression.
869 * Return negative error value on error.
870 * Return size of returned session payload data if OK.
872 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
873 struct lttng_event
*event
, const char *channel_name
,
874 const char *filter_expression
)
876 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
877 filter_expression
, 0, NULL
);
881 * Depending on the event, return a newly allocated agent filter expression or
882 * NULL if not applicable.
884 * An event with NO loglevel and the name is * will return NULL.
886 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
889 char *agent_filter
= NULL
;
893 /* Don't add filter for the '*' event. */
894 if (strcmp(ev
->name
, "*") != 0) {
896 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
899 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
907 /* Add loglevel filtering if any for the JUL domain. */
908 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
911 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
917 if (filter
|| agent_filter
) {
920 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
921 agent_filter
? agent_filter
: filter
, op
,
926 agent_filter
= new_filter
;
928 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
944 * Generate the filter bytecode from a given filter expression string. Put the
945 * newly allocated parser context in ctxp and populate the lsm object with the
948 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
950 static int generate_filter(char *filter_expression
,
951 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
954 struct filter_parser_ctx
*ctx
= NULL
;
957 assert(filter_expression
);
962 * Casting const to non-const, as the underlying function will use it in
965 fmem
= lttng_fmemopen((void *) filter_expression
,
966 strlen(filter_expression
), "r");
968 fprintf(stderr
, "Error opening memory as stream\n");
969 ret
= -LTTNG_ERR_FILTER_NOMEM
;
972 ctx
= filter_parser_ctx_alloc(fmem
);
974 fprintf(stderr
, "Error allocating parser\n");
975 ret
= -LTTNG_ERR_FILTER_NOMEM
;
976 goto filter_alloc_error
;
978 ret
= filter_parser_ctx_append_ast(ctx
);
980 fprintf(stderr
, "Parse error\n");
981 ret
= -LTTNG_ERR_FILTER_INVAL
;
985 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
988 fprintf(stderr
, "XML print error\n");
989 ret
= -LTTNG_ERR_FILTER_INVAL
;
994 dbg_printf("Generating IR... ");
996 ret
= filter_visitor_ir_generate(ctx
);
998 fprintf(stderr
, "Generate IR error\n");
999 ret
= -LTTNG_ERR_FILTER_INVAL
;
1002 dbg_printf("done\n");
1004 dbg_printf("Validating IR... ");
1006 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
1008 ret
= -LTTNG_ERR_FILTER_INVAL
;
1012 /* Normalize globbing patterns in the expression. */
1013 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1015 ret
= -LTTNG_ERR_FILTER_INVAL
;
1019 /* Validate strings used as literals in the expression. */
1020 ret
= filter_visitor_ir_validate_string(ctx
);
1022 ret
= -LTTNG_ERR_FILTER_INVAL
;
1026 /* Validate globbing patterns in the expression. */
1027 ret
= filter_visitor_ir_validate_globbing(ctx
);
1029 ret
= -LTTNG_ERR_FILTER_INVAL
;
1033 dbg_printf("done\n");
1035 dbg_printf("Generating bytecode... ");
1037 ret
= filter_visitor_bytecode_generate(ctx
);
1039 fprintf(stderr
, "Generate bytecode error\n");
1040 ret
= -LTTNG_ERR_FILTER_INVAL
;
1043 dbg_printf("done\n");
1044 dbg_printf("Size of bytecode generated: %u bytes.\n",
1045 bytecode_get_len(&ctx
->bytecode
->b
));
1047 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1048 + bytecode_get_len(&ctx
->bytecode
->b
);
1049 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1051 /* No need to keep the memory stream. */
1052 if (fclose(fmem
) != 0) {
1060 filter_ir_free(ctx
);
1061 filter_parser_ctx_free(ctx
);
1063 if (fclose(fmem
) != 0) {
1071 * Enable event(s) for a channel, possibly with exclusions and a filter.
1072 * If no event name is specified, all events are enabled.
1073 * If no channel name is specified, the default name is used.
1074 * If filter expression is not NULL, the filter is set for the event.
1075 * If exclusion count is not zero, the exclusions are set for the event.
1076 * Returns size of returned session payload data or a negative error code.
1078 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1079 struct lttng_event
*ev
, const char *channel_name
,
1080 const char *original_filter_expression
,
1081 int exclusion_count
, char **exclusion_list
)
1083 struct lttcomm_session_msg lsm
;
1084 struct lttng_dynamic_buffer send_buffer
;
1085 int ret
= 0, i
, fd_to_send
= -1;
1086 bool send_fd
= false;
1087 unsigned int free_filter_expression
= 0;
1088 struct filter_parser_ctx
*ctx
= NULL
;
1091 * We have either a filter or some exclusions, so we need to set up
1092 * a variable-length memory block from where to send the data.
1094 lttng_dynamic_buffer_init(&send_buffer
);
1097 * Cast as non-const since we may replace the filter expression
1098 * by a dynamically allocated string. Otherwise, the original
1099 * string is not modified.
1101 char *filter_expression
= (char *) original_filter_expression
;
1103 if (handle
== NULL
|| ev
== NULL
) {
1104 ret
= -LTTNG_ERR_INVALID
;
1109 * Empty filter string will always be rejected by the parser
1110 * anyway, so treat this corner-case early to eliminate
1111 * lttng_fmemopen error for 0-byte allocation.
1113 if (filter_expression
&& filter_expression
[0] == '\0') {
1114 ret
= -LTTNG_ERR_INVALID
;
1118 memset(&lsm
, 0, sizeof(lsm
));
1120 /* If no channel name, send empty string. */
1121 if (channel_name
== NULL
) {
1122 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1123 sizeof(lsm
.u
.enable
.channel_name
));
1125 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1126 sizeof(lsm
.u
.enable
.channel_name
));
1129 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1130 if (ev
->name
[0] == '\0') {
1131 /* Enable all events */
1132 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1135 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1136 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1138 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1139 sizeof(lsm
.session
.name
));
1140 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1141 lsm
.u
.enable
.bytecode_len
= 0;
1143 /* Parse filter expression. */
1144 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1145 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1146 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1147 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1148 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1149 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1152 /* Setup JUL filter if needed. */
1153 agent_filter
= set_agent_filter(filter_expression
, ev
);
1154 if (!agent_filter
) {
1155 if (!filter_expression
) {
1157 * No JUL and no filter, just skip
1164 * With an agent filter, the original filter has
1165 * been added to it thus replace the filter
1168 filter_expression
= agent_filter
;
1169 free_filter_expression
= 1;
1173 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1179 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1180 lsm
.u
.enable
.bytecode_len
1181 + lsm
.u
.enable
.expression_len
1182 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1184 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1188 /* Put exclusion names first in the data. */
1189 for (i
= 0; i
< exclusion_count
; i
++) {
1190 size_t exclusion_len
;
1192 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1193 LTTNG_SYMBOL_NAME_LEN
);
1194 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1195 /* Exclusion is not NULL-terminated. */
1196 ret
= -LTTNG_ERR_INVALID
;
1200 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1201 *(exclusion_list
+ i
),
1202 LTTNG_SYMBOL_NAME_LEN
);
1208 /* Add filter expression next. */
1209 if (filter_expression
) {
1210 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1211 filter_expression
, lsm
.u
.enable
.expression_len
);
1216 /* Add filter bytecode next. */
1217 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1218 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1219 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1224 if (ev
->extended
.ptr
) {
1225 struct lttng_event_extended
*ev_ext
=
1226 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1228 if (ev_ext
->probe_location
) {
1230 * lttng_userspace_probe_location_serialize returns the
1231 * number of bytes that was appended to the buffer.
1233 ret
= lttng_userspace_probe_location_serialize(
1234 ev_ext
->probe_location
, &send_buffer
,
1240 send_fd
= fd_to_send
>= 0;
1242 * Set the size of the userspace probe location element
1243 * of the buffer so that the receiving side knows where
1246 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1250 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1251 send_fd
? &fd_to_send
: NULL
,
1253 send_buffer
.size
? send_buffer
.data
: NULL
,
1254 send_buffer
.size
, NULL
, NULL
, 0);
1257 if (filter_expression
&& ctx
) {
1258 filter_bytecode_free(ctx
);
1259 filter_ir_free(ctx
);
1260 filter_parser_ctx_free(ctx
);
1263 if (free_filter_expression
) {
1265 * The filter expression has been replaced and must be freed as
1266 * it is not the original filter expression received as a
1269 free(filter_expression
);
1273 * Return directly to the caller and don't ask the sessiond since
1274 * something went wrong in the parsing of data above.
1276 lttng_dynamic_buffer_reset(&send_buffer
);
1280 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1284 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1285 struct lttng_event
*ev
, const char *channel_name
,
1286 const char *original_filter_expression
)
1288 struct lttcomm_session_msg lsm
;
1291 unsigned int free_filter_expression
= 0;
1292 struct filter_parser_ctx
*ctx
= NULL
;
1294 * Cast as non-const since we may replace the filter expression
1295 * by a dynamically allocated string. Otherwise, the original
1296 * string is not modified.
1298 char *filter_expression
= (char *) original_filter_expression
;
1300 if (handle
== NULL
|| ev
== NULL
) {
1301 ret
= -LTTNG_ERR_INVALID
;
1306 * Empty filter string will always be rejected by the parser
1307 * anyway, so treat this corner-case early to eliminate
1308 * lttng_fmemopen error for 0-byte allocation.
1310 if (filter_expression
&& filter_expression
[0] == '\0') {
1311 ret
= -LTTNG_ERR_INVALID
;
1315 memset(&lsm
, 0, sizeof(lsm
));
1317 /* If no channel name, send empty string. */
1318 if (channel_name
== NULL
) {
1319 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1320 sizeof(lsm
.u
.disable
.channel_name
));
1322 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1323 sizeof(lsm
.u
.disable
.channel_name
));
1326 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1328 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1329 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1331 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1332 sizeof(lsm
.session
.name
));
1333 lsm
.u
.disable
.bytecode_len
= 0;
1336 * For the JUL domain, a filter is enforced except for the
1337 * disable all event. This is done to avoid having the event in
1338 * all sessions thus filtering by logger name.
1340 if (filter_expression
== NULL
&&
1341 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1342 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1343 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1348 * We have a filter, so we need to set up a variable-length
1349 * memory block from where to send the data.
1352 /* Parse filter expression */
1353 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1354 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1355 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1356 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1357 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1358 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1361 /* Setup JUL filter if needed. */
1362 agent_filter
= set_agent_filter(filter_expression
, ev
);
1363 if (!agent_filter
) {
1364 if (!filter_expression
) {
1366 * No JUL and no filter, just skip
1373 * With a JUL filter, the original filter has
1374 * been added to it thus replace the filter
1377 filter_expression
= agent_filter
;
1378 free_filter_expression
= 1;
1382 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1388 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1389 + lsm
.u
.disable
.expression_len
);
1391 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1395 /* Add filter expression. */
1396 if (lsm
.u
.disable
.expression_len
!= 0) {
1399 lsm
.u
.disable
.expression_len
);
1401 /* Add filter bytecode next. */
1402 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1404 + lsm
.u
.disable
.expression_len
,
1406 lsm
.u
.disable
.bytecode_len
);
1409 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1410 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1414 if (filter_expression
&& ctx
) {
1415 filter_bytecode_free(ctx
);
1416 filter_ir_free(ctx
);
1417 filter_parser_ctx_free(ctx
);
1420 if (free_filter_expression
) {
1422 * The filter expression has been replaced and must be freed as
1423 * it is not the original filter expression received as a
1426 free(filter_expression
);
1430 * Return directly to the caller and don't ask the sessiond since
1431 * something went wrong in the parsing of data above.
1436 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1441 * Disable event(s) of a channel and domain.
1442 * If no event name is specified, all events are disabled.
1443 * If no channel name is specified, the default 'channel0' is used.
1444 * Returns size of returned session payload data or a negative error code.
1446 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1447 const char *channel_name
)
1449 struct lttng_event ev
;
1451 memset(&ev
, 0, sizeof(ev
));
1453 ev
.type
= LTTNG_EVENT_ALL
;
1454 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1455 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1458 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1460 struct lttng_channel
*channel
= NULL
;
1461 struct lttng_channel_extended
*extended
= NULL
;
1467 /* Validate domain. */
1468 switch (domain
->type
) {
1469 case LTTNG_DOMAIN_UST
:
1470 switch (domain
->buf_type
) {
1471 case LTTNG_BUFFER_PER_UID
:
1472 case LTTNG_BUFFER_PER_PID
:
1478 case LTTNG_DOMAIN_KERNEL
:
1479 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1487 channel
= zmalloc(sizeof(*channel
));
1492 extended
= zmalloc(sizeof(*extended
));
1497 channel
->attr
.extended
.ptr
= extended
;
1499 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1507 void lttng_channel_destroy(struct lttng_channel
*channel
)
1513 if (channel
->attr
.extended
.ptr
) {
1514 free(channel
->attr
.extended
.ptr
);
1520 * Enable channel per domain
1521 * Returns size of returned session payload data or a negative error code.
1523 int lttng_enable_channel(struct lttng_handle
*handle
,
1524 struct lttng_channel
*in_chan
)
1526 struct lttcomm_session_msg lsm
;
1527 size_t total_buffer_size_needed_per_cpu
= 0;
1529 /* NULL arguments are forbidden. No default values. */
1530 if (handle
== NULL
|| in_chan
== NULL
) {
1531 return -LTTNG_ERR_INVALID
;
1534 memset(&lsm
, 0, sizeof(lsm
));
1535 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1536 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1538 if (!in_chan
->attr
.extended
.ptr
) {
1539 struct lttng_channel
*channel
;
1540 struct lttng_channel_extended
*extended
;
1542 channel
= lttng_channel_create(&handle
->domain
);
1544 return -LTTNG_ERR_NOMEM
;
1548 * Create a new channel in order to use default extended
1551 extended
= (struct lttng_channel_extended
*)
1552 channel
->attr
.extended
.ptr
;
1553 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1554 lttng_channel_destroy(channel
);
1556 struct lttng_channel_extended
*extended
;
1558 extended
= (struct lttng_channel_extended
*)
1559 in_chan
->attr
.extended
.ptr
;
1560 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1564 * Verify that the amount of memory required to create the requested
1565 * buffer is available on the system at the moment.
1567 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1568 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1569 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1570 return -LTTNG_ERR_NOMEM
;
1573 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1574 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1576 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1577 sizeof(lsm
.session
.name
));
1579 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1583 * All tracing will be stopped for registered events of the channel.
1584 * Returns size of returned session payload data or a negative error code.
1586 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1588 struct lttcomm_session_msg lsm
;
1590 /* Safety check. Both are mandatory. */
1591 if (handle
== NULL
|| name
== NULL
) {
1592 return -LTTNG_ERR_INVALID
;
1595 memset(&lsm
, 0, sizeof(lsm
));
1597 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1599 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1600 sizeof(lsm
.u
.disable
.channel_name
));
1602 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1604 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1605 sizeof(lsm
.session
.name
));
1607 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1611 * Lists all available tracepoints of domain.
1612 * Sets the contents of the events array.
1613 * Returns the number of lttng_event entries in events;
1614 * on error, returns a negative value.
1616 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1617 struct lttng_event
**events
)
1620 struct lttcomm_session_msg lsm
;
1622 if (handle
== NULL
) {
1623 return -LTTNG_ERR_INVALID
;
1626 memset(&lsm
, 0, sizeof(lsm
));
1627 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1628 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1630 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1635 return ret
/ sizeof(struct lttng_event
);
1639 * Lists all available tracepoint fields of domain.
1640 * Sets the contents of the event field array.
1641 * Returns the number of lttng_event_field entries in events;
1642 * on error, returns a negative value.
1644 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1645 struct lttng_event_field
**fields
)
1648 struct lttcomm_session_msg lsm
;
1650 if (handle
== NULL
) {
1651 return -LTTNG_ERR_INVALID
;
1654 memset(&lsm
, 0, sizeof(lsm
));
1655 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1656 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1658 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1663 return ret
/ sizeof(struct lttng_event_field
);
1667 * Lists all available kernel system calls. Allocates and sets the contents of
1670 * Returns the number of lttng_event entries in events; on error, returns a
1673 int lttng_list_syscalls(struct lttng_event
**events
)
1676 struct lttcomm_session_msg lsm
;
1679 return -LTTNG_ERR_INVALID
;
1682 memset(&lsm
, 0, sizeof(lsm
));
1683 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1684 /* Force kernel domain for system calls. */
1685 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1687 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1692 return ret
/ sizeof(struct lttng_event
);
1696 * Returns a human readable string describing
1697 * the error code (a negative value).
1699 const char *lttng_strerror(int code
)
1701 return error_get_str(code
);
1704 enum lttng_error_code
lttng_create_session_ext(
1705 struct lttng_session_descriptor
*session_descriptor
)
1707 enum lttng_error_code ret_code
;
1708 struct lttcomm_session_msg lsm
= {
1709 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1712 struct lttng_buffer_view reply_view
;
1714 bool sessiond_must_generate_ouput
;
1715 struct lttng_dynamic_buffer payload
;
1717 size_t descriptor_size
;
1718 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1720 lttng_dynamic_buffer_init(&payload
);
1721 if (!session_descriptor
) {
1722 ret_code
= LTTNG_ERR_INVALID
;
1726 sessiond_must_generate_ouput
=
1727 !lttng_session_descriptor_is_output_destination_initialized(
1728 session_descriptor
);
1729 if (sessiond_must_generate_ouput
) {
1730 const char *home_dir
= utils_get_home_dir();
1731 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1733 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1734 ret_code
= LTTNG_ERR_FATAL
;
1738 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1739 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1742 ret_code
= LTTNG_ERR_NOMEM
;
1747 descriptor_size
= payload
.size
;
1748 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1751 ret_code
= LTTNG_ERR_INVALID
;
1754 descriptor_size
= payload
.size
- descriptor_size
;
1755 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1757 /* Command returns a session descriptor on success. */
1758 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1759 payload
.size
, &reply
);
1760 if (reply_ret
< 0) {
1761 ret_code
= -reply_ret
;
1763 } else if (reply_ret
== 0) {
1764 /* Socket unexpectedly closed by the session daemon. */
1765 ret_code
= LTTNG_ERR_FATAL
;
1769 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1770 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1773 ret_code
= LTTNG_ERR_FATAL
;
1776 ret_code
= LTTNG_OK
;
1777 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1780 lttng_dynamic_buffer_reset(&payload
);
1781 lttng_session_descriptor_destroy(descriptor_reply
);
1786 * Create a new session using name and url for destination.
1788 * Return 0 on success else a negative LTTng error code.
1790 int lttng_create_session(const char *name
, const char *url
)
1794 struct lttng_uri
*uris
= NULL
;
1795 struct lttng_session_descriptor
*descriptor
= NULL
;
1796 enum lttng_error_code ret_code
;
1799 ret
= -LTTNG_ERR_INVALID
;
1803 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1805 ret
= -LTTNG_ERR_INVALID
;
1810 descriptor
= lttng_session_descriptor_create(name
);
1813 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1814 ret
= -LTTNG_ERR_INVALID
;
1817 descriptor
= lttng_session_descriptor_local_create(name
,
1821 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1825 ret
= -LTTNG_ERR_INVALID
;
1829 ret
= -LTTNG_ERR_INVALID
;
1832 ret_code
= lttng_create_session_ext(descriptor
);
1833 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1835 lttng_session_descriptor_destroy(descriptor
);
1841 * Create a session exclusively used for snapshot.
1843 * Return 0 on success else a negative LTTng error code.
1845 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1848 enum lttng_error_code ret_code
;
1850 struct lttng_uri
*uris
= NULL
;
1851 struct lttng_session_descriptor
*descriptor
= NULL
;
1854 ret
= -LTTNG_ERR_INVALID
;
1858 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1860 ret
= -LTTNG_ERR_INVALID
;
1864 * If the user does not specify a custom subdir, use the session name.
1866 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1867 strlen(uris
[0].subdir
) == 0) {
1868 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1871 PERROR("Failed to set session name as network destination sub-directory");
1872 ret
= -LTTNG_ERR_FATAL
;
1874 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1875 /* Truncated output. */
1876 ret
= -LTTNG_ERR_INVALID
;
1883 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1886 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1887 ret
= -LTTNG_ERR_INVALID
;
1890 descriptor
= lttng_session_descriptor_snapshot_local_create(
1895 descriptor
= lttng_session_descriptor_snapshot_network_create(
1901 ret
= -LTTNG_ERR_INVALID
;
1905 ret
= -LTTNG_ERR_INVALID
;
1908 ret_code
= lttng_create_session_ext(descriptor
);
1909 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1911 lttng_session_descriptor_destroy(descriptor
);
1917 * Create a session exclusively used for live.
1919 * Return 0 on success else a negative LTTng error code.
1921 int lttng_create_session_live(const char *name
, const char *url
,
1922 unsigned int timer_interval
)
1925 enum lttng_error_code ret_code
;
1926 struct lttng_session_descriptor
*descriptor
= NULL
;
1929 ret
= -LTTNG_ERR_INVALID
;
1934 descriptor
= lttng_session_descriptor_live_network_create(
1935 name
, url
, NULL
, timer_interval
);
1937 descriptor
= lttng_session_descriptor_live_create(
1938 name
, timer_interval
);
1941 ret
= -LTTNG_ERR_INVALID
;
1944 ret_code
= lttng_create_session_ext(descriptor
);
1945 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1947 lttng_session_descriptor_destroy(descriptor
);
1952 * Stop the session and wait for the data before destroying it
1954 * Return 0 on success else a negative LTTng error code.
1956 int lttng_destroy_session(const char *session_name
)
1959 enum lttng_error_code ret_code
;
1960 enum lttng_destruction_handle_status status
;
1961 struct lttng_destruction_handle
*handle
= NULL
;
1964 * Stop the tracing and wait for the data to be
1967 ret
= _lttng_stop_tracing(session_name
, 1);
1968 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1972 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
1973 if (ret_code
!= LTTNG_OK
) {
1974 ret
= (int) -ret_code
;
1979 /* Block until the completion of the destruction of the session. */
1980 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
1981 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
1982 ret
= -LTTNG_ERR_UNK
;
1986 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
1987 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
1988 ret
= -LTTNG_ERR_UNK
;
1991 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1993 lttng_destruction_handle_destroy(handle
);
1998 * Destroy the session without waiting for the data.
2000 int lttng_destroy_session_no_wait(const char *session_name
)
2002 enum lttng_error_code ret_code
;
2004 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2005 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
2009 * Ask the session daemon for all available sessions.
2010 * Sets the contents of the sessions array.
2011 * Returns the number of lttng_session entries in sessions;
2012 * on error, returns a negative value.
2014 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2017 struct lttcomm_session_msg lsm
;
2018 const size_t session_size
= sizeof(struct lttng_session
) +
2019 sizeof(struct lttng_session_extended
);
2020 size_t session_count
, i
;
2021 struct lttng_session_extended
*sessions_extended_begin
;
2022 struct lttng_session
*sessions
= NULL
;
2024 memset(&lsm
, 0, sizeof(lsm
));
2025 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2026 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2031 ret
= -LTTNG_ERR_FATAL
;
2035 if (ret
% session_size
) {
2036 ret
= -LTTNG_ERR_UNK
;
2038 *out_sessions
= NULL
;
2041 session_count
= (size_t) ret
/ session_size
;
2042 sessions_extended_begin
= (struct lttng_session_extended
*)
2043 (&sessions
[session_count
]);
2045 /* Set extended session info pointers. */
2046 for (i
= 0; i
< session_count
; i
++) {
2047 struct lttng_session
*session
= &sessions
[i
];
2048 struct lttng_session_extended
*extended
=
2049 &(sessions_extended_begin
[i
]);
2051 session
->extended
.ptr
= extended
;
2054 ret
= (int) session_count
;
2055 *out_sessions
= sessions
;
2060 enum lttng_error_code
lttng_session_get_creation_time(
2061 const struct lttng_session
*session
, uint64_t *creation_time
)
2063 enum lttng_error_code ret
= LTTNG_OK
;
2064 struct lttng_session_extended
*extended
;
2066 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2067 ret
= LTTNG_ERR_INVALID
;
2071 extended
= session
->extended
.ptr
;
2072 if (!extended
->creation_time
.is_set
) {
2073 /* Not created on the session daemon yet. */
2074 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2077 *creation_time
= extended
->creation_time
.value
;
2082 int lttng_set_session_shm_path(const char *session_name
,
2083 const char *shm_path
)
2085 struct lttcomm_session_msg lsm
;
2087 if (session_name
== NULL
) {
2088 return -LTTNG_ERR_INVALID
;
2091 memset(&lsm
, 0, sizeof(lsm
));
2092 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2094 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2095 sizeof(lsm
.session
.name
));
2096 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
2097 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2099 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2103 * Ask the session daemon for all available domains of a session.
2104 * Sets the contents of the domains array.
2105 * Returns the number of lttng_domain entries in domains;
2106 * on error, returns a negative value.
2108 int lttng_list_domains(const char *session_name
,
2109 struct lttng_domain
**domains
)
2112 struct lttcomm_session_msg lsm
;
2114 if (session_name
== NULL
) {
2115 return -LTTNG_ERR_INVALID
;
2118 memset(&lsm
, 0, sizeof(lsm
));
2119 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2121 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2122 sizeof(lsm
.session
.name
));
2124 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2129 return ret
/ sizeof(struct lttng_domain
);
2133 * Ask the session daemon for all available channels of a session.
2134 * Sets the contents of the channels array.
2135 * Returns the number of lttng_channel entries in channels;
2136 * on error, returns a negative value.
2138 int lttng_list_channels(struct lttng_handle
*handle
,
2139 struct lttng_channel
**channels
)
2142 size_t channel_count
, i
;
2143 const size_t channel_size
= sizeof(struct lttng_channel
) +
2144 sizeof(struct lttng_channel_extended
);
2145 struct lttcomm_session_msg lsm
;
2148 if (handle
== NULL
) {
2149 ret
= -LTTNG_ERR_INVALID
;
2153 memset(&lsm
, 0, sizeof(lsm
));
2154 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2155 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2156 sizeof(lsm
.session
.name
));
2158 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2160 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2165 if (ret
% channel_size
) {
2166 ret
= -LTTNG_ERR_UNK
;
2171 channel_count
= (size_t) ret
/ channel_size
;
2173 /* Set extended info pointers */
2174 extended_at
= ((void *) *channels
) +
2175 channel_count
* sizeof(struct lttng_channel
);
2176 for (i
= 0; i
< channel_count
; i
++) {
2177 struct lttng_channel
*chan
= &(*channels
)[i
];
2179 chan
->attr
.extended
.ptr
= extended_at
;
2180 extended_at
+= sizeof(struct lttng_channel_extended
);
2183 ret
= (int) channel_count
;
2189 * Ask the session daemon for all available events of a session channel.
2190 * Sets the contents of the events array.
2191 * Returns the number of lttng_event entries in events;
2192 * on error, returns a negative value.
2194 int lttng_list_events(struct lttng_handle
*handle
,
2195 const char *channel_name
, struct lttng_event
**events
)
2198 struct lttcomm_session_msg lsm
;
2199 struct lttcomm_event_command_header
*cmd_header
= NULL
;
2200 size_t cmd_header_len
;
2201 uint32_t nb_events
, i
;
2203 char *reception_buffer
= NULL
;
2204 struct lttng_dynamic_buffer listing
;
2207 /* Safety check. An handle and channel name are mandatory */
2208 if (handle
== NULL
|| channel_name
== NULL
) {
2209 return -LTTNG_ERR_INVALID
;
2212 memset(&lsm
, 0, sizeof(lsm
));
2213 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2214 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2215 sizeof(lsm
.session
.name
));
2216 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2217 sizeof(lsm
.u
.list
.channel_name
));
2218 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2220 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2221 (void **) &reception_buffer
, (void **) &cmd_header
,
2228 ret
= -LTTNG_ERR_UNK
;
2232 /* Set number of events and free command header */
2233 nb_events
= cmd_header
->nb_events
;
2234 if (nb_events
> INT_MAX
) {
2235 ret
= -LTTNG_ERR_OVERFLOW
;
2242 * The buffer that is returned must contain a "flat" version of
2243 * the events that are returned. In other words, all pointers
2244 * within an lttng_event must point to a location within the returned
2245 * buffer so that the user may free everything by simply calling free()
2246 * on the returned buffer. This is needed in order to maintain API
2249 * A first pass is performed to compute the size of the buffer that
2250 * must be allocated. A second pass is then performed to setup
2251 * the returned events so that their members always point within the
2254 * The layout of the returned buffer is as follows:
2255 * - struct lttng_event[nb_events],
2256 * - nb_events times the following:
2257 * - struct lttng_event_extended,
2258 * - flattened version of userspace_probe_location
2259 * - filter_expression
2261 * - padding to align to 64-bits
2263 comm_ext_at
= reception_buffer
+
2264 (nb_events
* sizeof(struct lttng_event
));
2265 storage_req
= nb_events
* sizeof(struct lttng_event
);
2267 for (i
= 0; i
< nb_events
; i
++) {
2268 struct lttcomm_event_extended_header
*ext_comm
=
2269 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2270 int probe_storage_req
= 0;
2272 comm_ext_at
+= sizeof(*ext_comm
);
2273 comm_ext_at
+= ext_comm
->filter_len
;
2275 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2277 if (ext_comm
->userspace_probe_location_len
) {
2278 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2279 struct lttng_buffer_view probe_location_view
;
2281 probe_location_view
= lttng_buffer_view_init(
2283 ext_comm
->userspace_probe_location_len
);
2286 * Create a temporary userspace probe location to
2287 * determine the size needed by a "flattened" version
2288 * of that same probe location.
2290 ret
= lttng_userspace_probe_location_create_from_buffer(
2291 &probe_location_view
, &probe_location
);
2293 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2297 ret
= lttng_userspace_probe_location_flatten(
2298 probe_location
, NULL
);
2299 lttng_userspace_probe_location_destroy(probe_location
);
2301 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2305 probe_storage_req
= ret
;
2306 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2309 storage_req
+= sizeof(struct lttng_event_extended
);
2310 storage_req
+= ext_comm
->filter_len
;
2311 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2312 /* Padding to ensure the flat probe is aligned. */
2313 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2314 storage_req
+= probe_storage_req
;
2317 lttng_dynamic_buffer_init(&listing
);
2319 * We must ensure that "listing" is never resized so as to preserve
2320 * the validity of the flattened objects.
2322 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2324 ret
= -LTTNG_ERR_NOMEM
;
2328 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2329 nb_events
* sizeof(struct lttng_event
));
2331 ret
= -LTTNG_ERR_NOMEM
;
2332 goto free_dynamic_buffer
;
2335 comm_ext_at
= reception_buffer
+
2336 (nb_events
* sizeof(struct lttng_event
));
2337 for (i
= 0; i
< nb_events
; i
++) {
2338 struct lttng_event
*event
= (struct lttng_event
*)
2339 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2340 struct lttcomm_event_extended_header
*ext_comm
=
2341 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2342 struct lttng_event_extended
*event_extended
=
2343 (struct lttng_event_extended
*)
2344 (listing
.data
+ listing
.size
);
2346 /* Insert struct lttng_event_extended. */
2347 ret
= lttng_dynamic_buffer_set_size(&listing
,
2348 listing
.size
+ sizeof(*event_extended
));
2350 ret
= -LTTNG_ERR_NOMEM
;
2351 goto free_dynamic_buffer
;
2353 event
->extended
.ptr
= event_extended
;
2355 comm_ext_at
+= sizeof(*ext_comm
);
2357 /* Insert filter expression. */
2358 if (ext_comm
->filter_len
) {
2359 event_extended
->filter_expression
= listing
.data
+
2361 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2362 ext_comm
->filter_len
);
2364 ret
= -LTTNG_ERR_NOMEM
;
2365 goto free_dynamic_buffer
;
2367 comm_ext_at
+= ext_comm
->filter_len
;
2370 /* Insert exclusions. */
2371 if (ext_comm
->nb_exclusions
) {
2372 event_extended
->exclusions
.count
=
2373 ext_comm
->nb_exclusions
;
2374 event_extended
->exclusions
.strings
=
2375 listing
.data
+ listing
.size
;
2377 ret
= lttng_dynamic_buffer_append(&listing
,
2379 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2381 ret
= -LTTNG_ERR_NOMEM
;
2382 goto free_dynamic_buffer
;
2384 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2387 /* Insert padding to align to 64-bits. */
2388 ret
= lttng_dynamic_buffer_set_size(&listing
,
2389 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2391 ret
= -LTTNG_ERR_NOMEM
;
2392 goto free_dynamic_buffer
;
2395 /* Insert flattened userspace probe location. */
2396 if (ext_comm
->userspace_probe_location_len
) {
2397 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2398 struct lttng_buffer_view probe_location_view
;
2400 probe_location_view
= lttng_buffer_view_init(
2402 ext_comm
->userspace_probe_location_len
);
2404 ret
= lttng_userspace_probe_location_create_from_buffer(
2405 &probe_location_view
, &probe_location
);
2407 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2408 goto free_dynamic_buffer
;
2411 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2412 (listing
.data
+ listing
.size
);
2413 ret
= lttng_userspace_probe_location_flatten(
2414 probe_location
, &listing
);
2415 lttng_userspace_probe_location_destroy(probe_location
);
2417 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2418 goto free_dynamic_buffer
;
2421 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2425 /* Don't reset listing buffer as we return its content. */
2426 *events
= (struct lttng_event
*) listing
.data
;
2427 lttng_dynamic_buffer_init(&listing
);
2428 ret
= (int) nb_events
;
2429 free_dynamic_buffer
:
2430 lttng_dynamic_buffer_reset(&listing
);
2433 free(reception_buffer
);
2438 * Sets the tracing_group variable with name.
2439 * This function allocates memory pointed to by tracing_group.
2440 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2442 int lttng_set_tracing_group(const char *name
)
2445 return -LTTNG_ERR_INVALID
;
2448 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2449 return -LTTNG_ERR_FATAL
;
2455 int lttng_calibrate(struct lttng_handle
*handle
,
2456 struct lttng_calibrate
*calibrate
)
2459 * This command was removed in LTTng 2.9.
2461 return -LTTNG_ERR_UND
;
2465 * Set default channel attributes.
2466 * If either or both of the arguments are null, attr content is zeroe'd.
2468 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2469 struct lttng_channel_attr
*attr
)
2471 struct lttng_channel_extended
*extended
;
2474 if (attr
== NULL
|| domain
== NULL
) {
2478 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2479 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2481 /* Same for all domains. */
2482 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2483 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2484 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2486 switch (domain
->type
) {
2487 case LTTNG_DOMAIN_KERNEL
:
2488 attr
->switch_timer_interval
=
2489 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2490 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2491 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2492 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2493 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2495 extended
->monitor_timer_interval
=
2496 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2497 extended
->blocking_timeout
=
2498 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2501 case LTTNG_DOMAIN_UST
:
2502 switch (domain
->buf_type
) {
2503 case LTTNG_BUFFER_PER_UID
:
2504 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2505 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2506 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2507 attr
->switch_timer_interval
=
2508 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2509 attr
->read_timer_interval
=
2510 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2512 extended
->monitor_timer_interval
=
2513 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2514 extended
->blocking_timeout
=
2515 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2518 case LTTNG_BUFFER_PER_PID
:
2520 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2521 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2522 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2523 attr
->switch_timer_interval
=
2524 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2525 attr
->read_timer_interval
=
2526 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2528 extended
->monitor_timer_interval
=
2529 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2530 extended
->blocking_timeout
=
2531 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2536 /* Default behavior: leave set to 0. */
2540 attr
->extended
.ptr
= extended
;
2543 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2544 uint64_t *discarded_events
)
2547 struct lttng_channel_extended
*chan_ext
;
2549 if (!channel
|| !discarded_events
) {
2550 ret
= -LTTNG_ERR_INVALID
;
2554 chan_ext
= channel
->attr
.extended
.ptr
;
2557 * This can happen since the lttng_channel structure is
2558 * used for other tasks where this pointer is never set.
2560 *discarded_events
= 0;
2564 *discarded_events
= chan_ext
->discarded_events
;
2569 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2570 uint64_t *lost_packets
)
2573 struct lttng_channel_extended
*chan_ext
;
2575 if (!channel
|| !lost_packets
) {
2576 ret
= -LTTNG_ERR_INVALID
;
2580 chan_ext
= channel
->attr
.extended
.ptr
;
2583 * This can happen since the lttng_channel structure is
2584 * used for other tasks where this pointer is never set.
2590 *lost_packets
= chan_ext
->lost_packets
;
2595 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2596 uint64_t *monitor_timer_interval
)
2600 if (!chan
|| !monitor_timer_interval
) {
2601 ret
= -LTTNG_ERR_INVALID
;
2605 if (!chan
->attr
.extended
.ptr
) {
2606 ret
= -LTTNG_ERR_INVALID
;
2610 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2611 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2616 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2617 uint64_t monitor_timer_interval
)
2621 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2622 ret
= -LTTNG_ERR_INVALID
;
2626 ((struct lttng_channel_extended
*)
2627 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2628 monitor_timer_interval
;
2633 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2634 int64_t *blocking_timeout
)
2638 if (!chan
|| !blocking_timeout
) {
2639 ret
= -LTTNG_ERR_INVALID
;
2643 if (!chan
->attr
.extended
.ptr
) {
2644 ret
= -LTTNG_ERR_INVALID
;
2648 *blocking_timeout
= ((struct lttng_channel_extended
*)
2649 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2654 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2655 int64_t blocking_timeout
)
2658 int64_t msec_timeout
;
2660 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2661 ret
= -LTTNG_ERR_INVALID
;
2665 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2666 ret
= -LTTNG_ERR_INVALID
;
2671 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2672 * us to accept a narrower range of values (msecs expressed as a signed
2675 msec_timeout
= blocking_timeout
/ 1000;
2676 if (msec_timeout
!= (int32_t) msec_timeout
) {
2677 ret
= -LTTNG_ERR_INVALID
;
2681 ((struct lttng_channel_extended
*)
2682 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2689 * Check if session daemon is alive.
2691 * Return 1 if alive or 0 if not.
2692 * On error returns a negative value.
2694 int lttng_session_daemon_alive(void)
2698 ret
= set_session_daemon_path();
2704 if (*sessiond_sock_path
== '\0') {
2706 * No socket path set. Weird error which means the constructor
2712 ret
= try_connect_sessiond(sessiond_sock_path
);
2723 * Set URL for a consumer for a session and domain.
2725 * Return 0 on success, else a negative value.
2727 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2728 const char *control_url
, const char *data_url
)
2732 struct lttcomm_session_msg lsm
;
2733 struct lttng_uri
*uris
= NULL
;
2735 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2736 return -LTTNG_ERR_INVALID
;
2739 memset(&lsm
, 0, sizeof(lsm
));
2741 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2743 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2744 sizeof(lsm
.session
.name
));
2745 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2747 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2749 return -LTTNG_ERR_INVALID
;
2752 lsm
.u
.uri
.size
= size
;
2754 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2755 sizeof(struct lttng_uri
) * size
, NULL
);
2764 int lttng_enable_consumer(struct lttng_handle
*handle
);
2765 int lttng_enable_consumer(struct lttng_handle
*handle
)
2773 int lttng_disable_consumer(struct lttng_handle
*handle
);
2774 int lttng_disable_consumer(struct lttng_handle
*handle
)
2782 int _lttng_create_session_ext(const char *name
, const char *url
,
2783 const char *datetime
);
2784 int _lttng_create_session_ext(const char *name
, const char *url
,
2785 const char *datetime
)
2791 * For a given session name, this call checks if the data is ready to be read
2792 * or is still being extracted by the consumer(s) hence not ready to be used by
2795 int lttng_data_pending(const char *session_name
)
2798 struct lttcomm_session_msg lsm
;
2799 uint8_t *pending
= NULL
;
2801 if (session_name
== NULL
) {
2802 return -LTTNG_ERR_INVALID
;
2805 memset(&lsm
, 0, sizeof(lsm
));
2806 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2808 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2809 sizeof(lsm
.session
.name
));
2811 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2814 } else if (ret
!= 1) {
2815 /* Unexpected payload size */
2816 ret
= -LTTNG_ERR_INVALID
;
2818 } else if (!pending
) {
2819 /* Internal error. */
2820 ret
= -LTTNG_ERR_UNK
;
2824 ret
= (int) *pending
;
2831 * List IDs in the tracker.
2833 * tracker_type is the type of tracker.
2834 * ids is set to an allocated array of IDs currently tracked. On
2835 * success, ids and contained ids must be freed/destroy by the caller.
2836 * nr_ids is set to the number of entries contained by the ids array.
2838 * Returns 0 on success, else a negative LTTng error code.
2840 int lttng_list_tracker_ids(struct lttng_handle
*handle
,
2841 enum lttng_tracker_type tracker_type
,
2842 struct lttng_tracker_ids
**_ids
)
2845 struct lttcomm_session_msg lsm
;
2846 struct lttcomm_tracker_command_header
*cmd_header
= NULL
;
2847 char *cmd_payload
= NULL
, *p
;
2848 size_t cmd_header_len
;
2850 struct lttng_tracker_ids
*ids
= NULL
;
2852 if (handle
== NULL
) {
2853 return -LTTNG_ERR_INVALID
;
2856 memset(&lsm
, 0, sizeof(lsm
));
2857 lsm
.cmd_type
= LTTNG_LIST_TRACKER_IDS
;
2858 lsm
.u
.id_tracker_list
.tracker_type
= tracker_type
;
2859 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2860 sizeof(lsm
.session
.name
));
2861 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2863 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2864 (void **) &cmd_payload
, (void **) &cmd_header
,
2870 /* Set number of tracker_id and free command header */
2871 nr_ids
= cmd_header
->nb_tracker_id
;
2872 if (nr_ids
> INT_MAX
) {
2873 ret
= -LTTNG_ERR_OVERFLOW
;
2879 ids
= lttng_tracker_ids_create(nr_ids
);
2881 ret
= -LTTNG_ERR_NOMEM
;
2886 for (i
= 0; i
< nr_ids
; i
++) {
2887 struct lttcomm_tracker_id_header
*tracker_id
;
2888 struct lttng_tracker_id
*id
;
2889 enum lttng_tracker_id_status status
;
2891 tracker_id
= (struct lttcomm_tracker_id_header
*) p
;
2892 p
+= sizeof(struct lttcomm_tracker_id_header
);
2893 id
= lttng_tracker_ids_get_pointer_of_index(ids
, i
);
2895 ret
= -LTTNG_ERR_INVALID
;
2899 switch (tracker_id
->type
) {
2901 status
= lttng_tracker_id_set_all(id
);
2903 case LTTNG_ID_VALUE
:
2904 id
->value
= tracker_id
->u
.value
;
2905 status
= lttng_tracker_id_set_value(
2906 id
, tracker_id
->u
.value
);
2908 case LTTNG_ID_STRING
:
2909 status
= lttng_tracker_id_set_string(id
, p
);
2910 p
+= tracker_id
->u
.var_data_len
;
2916 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
2917 ret
= -LTTNG_ERR_INVALID
;
2926 lttng_tracker_ids_destroy(ids
);
2933 * List PIDs in the tracker.
2935 * enabled is set to whether the PID tracker is enabled.
2936 * pids is set to an allocated array of PIDs currently tracked. On
2937 * success, pids must be freed by the caller.
2938 * nr_pids is set to the number of entries contained by the pids array.
2940 * Returns 0 on success, else a negative LTTng error code.
2942 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2943 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2945 struct lttng_tracker_ids
*ids
= NULL
;
2949 enum lttng_tracker_id_status status
;
2950 const struct lttng_tracker_id
*id
;
2952 ret
= lttng_list_tracker_ids(handle
, LTTNG_TRACKER_PID
, &ids
);
2957 nr_ids
= lttng_tracker_ids_get_count(ids
);
2960 id
= lttng_tracker_ids_get_at_index(ids
, 0);
2961 if (id
&& lttng_tracker_id_get_type(id
) == LTTNG_ID_ALL
) {
2969 pids
= zmalloc(nr_ids
* sizeof(*pids
));
2971 ret
= -LTTNG_ERR_NOMEM
;
2974 for (i
= 0; i
< nr_ids
; i
++) {
2975 id
= lttng_tracker_ids_get_at_index(ids
, i
);
2976 status
= lttng_tracker_id_get_value(id
, &pids
[i
]);
2977 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
2978 ret
= -LTTNG_ERR_UNK
;
2985 lttng_tracker_ids_destroy(ids
);
2993 * Regenerate the metadata for a session.
2994 * Return 0 on success, a negative error code on error.
2996 int lttng_regenerate_metadata(const char *session_name
)
2999 struct lttcomm_session_msg lsm
;
3001 if (!session_name
) {
3002 ret
= -LTTNG_ERR_INVALID
;
3006 memset(&lsm
, 0, sizeof(lsm
));
3007 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3009 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
3010 sizeof(lsm
.session
.name
));
3012 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3023 * Deprecated, replaced by lttng_regenerate_metadata.
3025 int lttng_metadata_regenerate(const char *session_name
)
3027 return lttng_regenerate_metadata(session_name
);
3031 * Regenerate the statedump of a session.
3032 * Return 0 on success, a negative error code on error.
3034 int lttng_regenerate_statedump(const char *session_name
)
3037 struct lttcomm_session_msg lsm
;
3039 if (!session_name
) {
3040 ret
= -LTTNG_ERR_INVALID
;
3044 memset(&lsm
, 0, sizeof(lsm
));
3045 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3047 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
3048 sizeof(lsm
.session
.name
));
3050 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3060 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3063 struct lttcomm_session_msg lsm
;
3064 struct lttng_dynamic_buffer buffer
;
3066 lttng_dynamic_buffer_init(&buffer
);
3068 ret
= -LTTNG_ERR_INVALID
;
3072 if (!lttng_trigger_validate(trigger
)) {
3073 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3077 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3079 ret
= -LTTNG_ERR_UNK
;
3083 memset(&lsm
, 0, sizeof(lsm
));
3084 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3085 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3086 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3089 lttng_dynamic_buffer_reset(&buffer
);
3093 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3096 struct lttcomm_session_msg lsm
;
3097 struct lttng_dynamic_buffer buffer
;
3099 lttng_dynamic_buffer_init(&buffer
);
3101 ret
= -LTTNG_ERR_INVALID
;
3105 if (!lttng_trigger_validate(trigger
)) {
3106 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3110 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3112 ret
= -LTTNG_ERR_UNK
;
3116 memset(&lsm
, 0, sizeof(lsm
));
3117 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3118 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3119 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3122 lttng_dynamic_buffer_reset(&buffer
);
3126 static int lttng_track_untrack_id(struct lttng_handle
*handle
,
3127 enum lttng_tracker_type tracker_type
,
3128 const struct lttng_tracker_id
*id
,
3129 enum lttcomm_sessiond_command cmd
)
3132 struct lttcomm_session_msg lsm
;
3133 const char *var_data
;
3134 size_t var_data_len
= 0;
3136 enum lttng_tracker_id_status status
;
3138 /* NULL arguments are forbidden. No default values. */
3139 if (handle
== NULL
) {
3143 memset(&lsm
, 0, sizeof(lsm
));
3146 lsm
.u
.id_tracker
.tracker_type
= tracker_type
;
3147 lsm
.u
.id_tracker
.id_type
= lttng_tracker_id_get_type(id
);
3148 switch (lsm
.u
.id_tracker
.id_type
) {
3151 case LTTNG_ID_VALUE
:
3152 status
= lttng_tracker_id_get_value(id
, &value
);
3153 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
3156 lsm
.u
.id_tracker
.u
.value
= value
;
3158 case LTTNG_ID_STRING
:
3159 status
= lttng_tracker_id_get_string(id
, &var_data
);
3160 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
3163 var_data_len
= strlen(var_data
) + 1; /* Includes \0. */
3164 lsm
.u
.id_tracker
.u
.var_len
= var_data_len
;
3170 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
3172 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
3173 sizeof(lsm
.session
.name
));
3175 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
3176 &lsm
, (char *) var_data
, var_data_len
, NULL
);
3179 return -LTTNG_ERR_INVALID
;
3183 * Add ID to session tracker.
3184 * Return 0 on success else a negative LTTng error code.
3186 int lttng_track_id(struct lttng_handle
*handle
,
3187 enum lttng_tracker_type tracker_type
,
3188 const struct lttng_tracker_id
*id
)
3190 return lttng_track_untrack_id(handle
, tracker_type
, id
, LTTNG_TRACK_ID
);
3194 * Remove ID from session tracker.
3195 * Return 0 on success else a negative LTTng error code.
3197 int lttng_untrack_id(struct lttng_handle
*handle
,
3198 enum lttng_tracker_type tracker_type
,
3199 const struct lttng_tracker_id
*id
)
3201 return lttng_track_untrack_id(
3202 handle
, tracker_type
, id
, LTTNG_UNTRACK_ID
);
3206 * Add PID to session tracker.
3207 * Return 0 on success else a negative LTTng error code.
3209 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
3212 struct lttng_tracker_id
*id
= NULL
;
3213 enum lttng_tracker_id_status status
;
3215 id
= lttng_tracker_id_create();
3216 status
= lttng_tracker_id_set_value(id
, pid
);
3217 if (status
== LTTNG_TRACKER_ID_STATUS_INVALID
) {
3218 ret
= -LTTNG_ERR_INVALID
;
3222 ret
= lttng_track_id(handle
, LTTNG_TRACKER_PID
, id
);
3224 lttng_tracker_id_destroy(id
);
3229 * Remove PID from session tracker.
3230 * Return 0 on success else a negative LTTng error code.
3232 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
3235 struct lttng_tracker_id
*id
= NULL
;
3236 enum lttng_tracker_id_status status
;
3238 id
= lttng_tracker_id_create();
3239 status
= lttng_tracker_id_set_value(id
, pid
);
3240 if (status
== LTTNG_TRACKER_ID_STATUS_INVALID
) {
3241 ret
= -LTTNG_ERR_INVALID
;
3245 ret
= lttng_untrack_id(handle
, LTTNG_TRACKER_PID
, id
);
3247 lttng_tracker_id_destroy(id
);
3254 static void __attribute__((constructor
)) init(void)
3256 /* Set default session group */
3257 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3263 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3265 free(tracing_group
);