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>
49 #include "filter/filter-ast.h"
50 #include "filter/filter-parser.h"
51 #include "filter/filter-bytecode.h"
52 #include "filter/memstream.h"
53 #include "lttng-ctl-helper.h"
56 static const int print_xml
= 1;
57 #define dbg_printf(fmt, args...) \
58 printf("[debug liblttng-ctl] " fmt, ## args)
60 static const int print_xml
= 0;
61 #define dbg_printf(fmt, args...) \
63 /* do nothing but check printf format */ \
65 printf("[debug liblttnctl] " fmt, ## args); \
70 /* Socket to session daemon for communication */
71 static int sessiond_socket
;
72 static char sessiond_sock_path
[PATH_MAX
];
75 static char *tracing_group
;
81 * Those two variables are used by error.h to silent or control the verbosity of
82 * error message. They are global to the library so application linking with it
83 * are able to compile correctly and also control verbosity of the library.
86 int lttng_opt_verbose
;
90 * Copy string from src to dst and enforce null terminated byte.
93 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
96 strncpy(dst
, src
, len
);
97 /* Enforce the NULL terminated byte */
105 * Copy domain to lttcomm_session_msg domain.
107 * If domain is unknown, default domain will be the kernel.
110 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
111 struct lttng_domain
*src
)
115 case LTTNG_DOMAIN_KERNEL
:
116 case LTTNG_DOMAIN_UST
:
117 case LTTNG_DOMAIN_JUL
:
118 case LTTNG_DOMAIN_LOG4J
:
119 case LTTNG_DOMAIN_PYTHON
:
120 memcpy(dst
, src
, sizeof(struct lttng_domain
));
123 memset(dst
, 0, sizeof(struct lttng_domain
));
130 * Send lttcomm_session_msg to the session daemon.
132 * On success, returns the number of bytes sent (>=0)
133 * On error, returns -1
135 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
140 ret
= -LTTNG_ERR_NO_SESSIOND
;
144 DBG("LSM cmd type : %d", lsm
->cmd_type
);
146 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
147 sizeof(struct lttcomm_session_msg
));
149 ret
= -LTTNG_ERR_FATAL
;
157 * Send var len data to the session daemon.
159 * On success, returns the number of bytes sent (>=0)
160 * On error, returns -1
162 static int send_session_varlen(const void *data
, size_t len
)
167 ret
= -LTTNG_ERR_NO_SESSIOND
;
176 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
178 ret
= -LTTNG_ERR_FATAL
;
186 * Send file descriptors to the session daemon.
188 * On success, returns the number of bytes sent (>=0)
189 * On error, returns -1
191 static int send_session_fds(const int *fds
, size_t nb_fd
)
196 ret
= -LTTNG_ERR_NO_SESSIOND
;
200 if (!fds
|| !nb_fd
) {
205 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
207 ret
= -LTTNG_ERR_FATAL
;
215 * Receive data from the sessiond socket.
217 * On success, returns the number of bytes received (>=0)
218 * On error, returns -1 (recvmsg() error) or -ENOTCONN
220 static int recv_data_sessiond(void *buf
, size_t len
)
225 ret
= -LTTNG_ERR_NO_SESSIOND
;
229 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
231 ret
= -LTTNG_ERR_FATAL
;
239 * Check if we are in the specified group.
241 * If yes return 1, else return -1.
244 int lttng_check_tracing_group(void)
246 gid_t
*grp_list
, tracing_gid
;
247 int grp_list_size
, grp_id
, i
;
249 const char *grp_name
= tracing_group
;
251 /* Get GID of group 'tracing' */
252 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
253 /* If grp_tracing is NULL, the group does not exist. */
257 /* Get number of supplementary group IDs */
258 grp_list_size
= getgroups(0, NULL
);
259 if (grp_list_size
< 0) {
264 /* Alloc group list of the right size */
265 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
270 grp_id
= getgroups(grp_list_size
, grp_list
);
276 for (i
= 0; i
< grp_list_size
; i
++) {
277 if (grp_list
[i
] == tracing_gid
) {
290 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
294 size_t best_mem_info
;
295 size_t num_bytes_requested_total
;
298 * Get the number of CPU currently online to compute the amount of
299 * memory needed to create a buffer for every CPU.
301 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
306 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
309 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
310 * reliable estimate we can get but it is only exposed by the kernel
311 * since 3.14. (See Linux kernel commit:
312 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
314 ret
= utils_get_memory_available(&best_mem_info
);
320 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
321 * is a sanity check for obvious user error.
323 ret
= utils_get_memory_total(&best_mem_info
);
331 return best_mem_info
>= num_bytes_requested_total
;
335 * Try connect to session daemon with sock_path.
337 * Return 0 on success, else -1
339 static int try_connect_sessiond(const char *sock_path
)
343 /* If socket exist, we check if the daemon listens for connect. */
344 ret
= access(sock_path
, F_OK
);
350 ret
= lttcomm_connect_unix_sock(sock_path
);
356 ret
= lttcomm_close_unix_sock(ret
);
358 PERROR("lttcomm_close_unix_sock");
368 * Set sessiond socket path by putting it in the global sessiond_sock_path
371 * Returns 0 on success, negative value on failure (the sessiond socket path
372 * is somehow too long or ENOMEM).
374 static int set_session_daemon_path(void)
376 int in_tgroup
= 0; /* In tracing group. */
382 /* Are we in the tracing group ? */
383 in_tgroup
= lttng_check_tracing_group();
386 if ((uid
== 0) || in_tgroup
) {
387 lttng_ctl_copy_string(sessiond_sock_path
,
388 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
396 ret
= try_connect_sessiond(sessiond_sock_path
);
400 /* Global session daemon not available... */
402 /* ...or not in tracing group (and not root), default */
405 * With GNU C < 2.1, snprintf returns -1 if the target buffer
407 * With GNU C >= 2.1, snprintf returns the required size
408 * (excluding closing null)
410 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
411 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
412 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
424 * Connect to the LTTng session daemon.
426 * On success, return 0. On error, return -1.
428 static int connect_sessiond(void)
432 /* Don't try to connect if already connected. */
437 ret
= set_session_daemon_path();
442 /* Connect to the sesssion daemon. */
443 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
448 sessiond_socket
= ret
;
458 * Clean disconnect from the session daemon.
460 * On success, return 0. On error, return -1.
462 static int disconnect_sessiond(void)
467 ret
= lttcomm_close_unix_sock(sessiond_socket
);
475 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
483 ret
= -LTTNG_ERR_INVALID
;
493 ret
= recv_data_sessiond(buf
, len
);
499 ret
= -LTTNG_ERR_INVALID
;
503 /* Move ownership of command header buffer to user. */
508 /* No command header. */
524 * Ask the session daemon a specific command and put the data into buf.
525 * Takes extra var. len. data and file descriptors as input to send to the
528 * Return size of data (only payload, not header) or a negative error code.
531 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
532 const int *fds
, size_t nb_fd
, const void *vardata
,
533 size_t vardata_len
, void **user_payload_buf
,
534 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
538 struct lttcomm_lttng_msg llm
;
540 ret
= connect_sessiond();
542 ret
= -LTTNG_ERR_NO_SESSIOND
;
546 /* Send command to session daemon */
547 ret
= send_session_msg(lsm
);
549 /* Ret value is a valid lttng error code. */
552 /* Send var len data */
553 ret
= send_session_varlen(vardata
, vardata_len
);
555 /* Ret value is a valid lttng error code. */
560 ret
= send_session_fds(fds
, nb_fd
);
562 /* Ret value is a valid lttng error code. */
566 /* Get header from data transmission */
567 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
569 /* Ret value is a valid lttng error code. */
573 /* Check error code if OK */
574 if (llm
.ret_code
!= LTTNG_OK
) {
579 /* Get command header from data transmission */
580 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
581 user_cmd_header_buf
, user_cmd_header_len
);
586 /* Get payload from data transmission */
587 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
596 disconnect_sessiond();
601 * Create lttng handle and return pointer.
603 * The returned pointer will be NULL in case of malloc() error.
605 struct lttng_handle
*lttng_create_handle(const char *session_name
,
606 struct lttng_domain
*domain
)
608 struct lttng_handle
*handle
= NULL
;
610 handle
= zmalloc(sizeof(struct lttng_handle
));
611 if (handle
== NULL
) {
612 PERROR("malloc handle");
616 /* Copy session name */
617 lttng_ctl_copy_string(handle
->session_name
, session_name
,
618 sizeof(handle
->session_name
));
620 /* Copy lttng domain or leave initialized to 0. */
622 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
630 * Destroy handle by free(3) the pointer.
632 void lttng_destroy_handle(struct lttng_handle
*handle
)
638 * Register an outside consumer.
640 * Returns size of returned session payload data or a negative error code.
642 int lttng_register_consumer(struct lttng_handle
*handle
,
643 const char *socket_path
)
645 struct lttcomm_session_msg lsm
;
647 if (handle
== NULL
|| socket_path
== NULL
) {
648 return -LTTNG_ERR_INVALID
;
651 memset(&lsm
, 0, sizeof(lsm
));
652 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
653 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
654 sizeof(lsm
.session
.name
));
655 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
657 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
658 sizeof(lsm
.u
.reg
.path
));
660 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
664 * Start tracing for all traces of the session.
666 * Returns size of returned session payload data or a negative error code.
668 int lttng_start_tracing(const char *session_name
)
670 struct lttcomm_session_msg lsm
;
672 if (session_name
== NULL
) {
673 return -LTTNG_ERR_INVALID
;
676 memset(&lsm
, 0, sizeof(lsm
));
677 lsm
.cmd_type
= LTTNG_START_TRACE
;
679 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
680 sizeof(lsm
.session
.name
));
682 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
686 * Stop tracing for all traces of the session.
688 static int _lttng_stop_tracing(const char *session_name
, int wait
)
691 struct lttcomm_session_msg lsm
;
693 if (session_name
== NULL
) {
694 return -LTTNG_ERR_INVALID
;
697 memset(&lsm
, 0, sizeof(lsm
));
698 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
700 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
701 sizeof(lsm
.session
.name
));
703 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
704 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
712 /* Check for data availability */
714 data_ret
= lttng_data_pending(session_name
);
716 /* Return the data available call error. */
722 * Data sleep time before retrying (in usec). Don't sleep if the
723 * call returned value indicates availability.
726 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
728 } while (data_ret
!= 0);
736 * Stop tracing and wait for data availability.
738 int lttng_stop_tracing(const char *session_name
)
740 return _lttng_stop_tracing(session_name
, 1);
744 * Stop tracing but _don't_ wait for data availability.
746 int lttng_stop_tracing_no_wait(const char *session_name
)
748 return _lttng_stop_tracing(session_name
, 0);
752 * Add context to a channel.
754 * If the given channel is NULL, add the contexts to all channels.
755 * The event_name param is ignored.
757 * Returns the size of the returned payload data or a negative error code.
759 int lttng_add_context(struct lttng_handle
*handle
,
760 struct lttng_event_context
*ctx
, const char *event_name
,
761 const char *channel_name
)
766 struct lttcomm_session_msg lsm
;
768 /* Safety check. Both are mandatory. */
769 if (handle
== NULL
|| ctx
== NULL
) {
770 ret
= -LTTNG_ERR_INVALID
;
774 memset(&lsm
, 0, sizeof(lsm
));
775 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
777 /* If no channel name, send empty string. */
778 if (channel_name
== NULL
) {
779 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
780 sizeof(lsm
.u
.context
.channel_name
));
782 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
783 sizeof(lsm
.u
.context
.channel_name
));
786 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
787 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
788 sizeof(lsm
.session
.name
));
790 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
791 size_t provider_len
, ctx_len
;
792 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
793 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
795 if (!provider_name
|| !ctx_name
) {
796 ret
= -LTTNG_ERR_INVALID
;
800 provider_len
= strlen(provider_name
);
801 if (provider_len
== 0) {
802 ret
= -LTTNG_ERR_INVALID
;
805 lsm
.u
.context
.provider_name_len
= provider_len
;
807 ctx_len
= strlen(ctx_name
);
809 ret
= -LTTNG_ERR_INVALID
;
812 lsm
.u
.context
.context_name_len
= ctx_len
;
814 len
= provider_len
+ ctx_len
;
817 ret
= -LTTNG_ERR_NOMEM
;
821 memcpy(buf
, provider_name
, provider_len
);
822 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
824 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
826 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
828 * Don't leak application addresses to the sessiond.
829 * This is only necessary when ctx is for an app ctx otherwise
830 * the values inside the union (type & config) are overwritten.
832 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
833 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
836 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
843 * Enable event(s) for a channel.
845 * If no event name is specified, all events are enabled.
846 * If no channel name is specified, the default 'channel0' is used.
848 * Returns size of returned session payload data or a negative error code.
850 int lttng_enable_event(struct lttng_handle
*handle
,
851 struct lttng_event
*ev
, const char *channel_name
)
853 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
858 * Create or enable an event with a filter expression.
860 * Return negative error value on error.
861 * Return size of returned session payload data if OK.
863 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
864 struct lttng_event
*event
, const char *channel_name
,
865 const char *filter_expression
)
867 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
868 filter_expression
, 0, NULL
);
872 * Depending on the event, return a newly allocated agent filter expression or
873 * NULL if not applicable.
875 * An event with NO loglevel and the name is * will return NULL.
877 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
880 char *agent_filter
= NULL
;
884 /* Don't add filter for the '*' event. */
885 if (strcmp(ev
->name
, "*") != 0) {
887 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
890 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
898 /* Add loglevel filtering if any for the JUL domain. */
899 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
902 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
908 if (filter
|| agent_filter
) {
911 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
912 agent_filter
? agent_filter
: filter
, op
,
917 agent_filter
= new_filter
;
919 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
935 * Generate the filter bytecode from a given filter expression string. Put the
936 * newly allocated parser context in ctxp and populate the lsm object with the
939 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
941 static int generate_filter(char *filter_expression
,
942 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
945 struct filter_parser_ctx
*ctx
= NULL
;
948 assert(filter_expression
);
953 * Casting const to non-const, as the underlying function will use it in
956 fmem
= lttng_fmemopen((void *) filter_expression
,
957 strlen(filter_expression
), "r");
959 fprintf(stderr
, "Error opening memory as stream\n");
960 ret
= -LTTNG_ERR_FILTER_NOMEM
;
963 ctx
= filter_parser_ctx_alloc(fmem
);
965 fprintf(stderr
, "Error allocating parser\n");
966 ret
= -LTTNG_ERR_FILTER_NOMEM
;
967 goto filter_alloc_error
;
969 ret
= filter_parser_ctx_append_ast(ctx
);
971 fprintf(stderr
, "Parse error\n");
972 ret
= -LTTNG_ERR_FILTER_INVAL
;
976 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
979 fprintf(stderr
, "XML print error\n");
980 ret
= -LTTNG_ERR_FILTER_INVAL
;
985 dbg_printf("Generating IR... ");
987 ret
= filter_visitor_ir_generate(ctx
);
989 fprintf(stderr
, "Generate IR error\n");
990 ret
= -LTTNG_ERR_FILTER_INVAL
;
993 dbg_printf("done\n");
995 dbg_printf("Validating IR... ");
997 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
999 ret
= -LTTNG_ERR_FILTER_INVAL
;
1003 /* Normalize globbing patterns in the expression. */
1004 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1006 ret
= -LTTNG_ERR_FILTER_INVAL
;
1010 /* Validate strings used as literals in the expression. */
1011 ret
= filter_visitor_ir_validate_string(ctx
);
1013 ret
= -LTTNG_ERR_FILTER_INVAL
;
1017 /* Validate globbing patterns in the expression. */
1018 ret
= filter_visitor_ir_validate_globbing(ctx
);
1020 ret
= -LTTNG_ERR_FILTER_INVAL
;
1024 dbg_printf("done\n");
1026 dbg_printf("Generating bytecode... ");
1028 ret
= filter_visitor_bytecode_generate(ctx
);
1030 fprintf(stderr
, "Generate bytecode error\n");
1031 ret
= -LTTNG_ERR_FILTER_INVAL
;
1034 dbg_printf("done\n");
1035 dbg_printf("Size of bytecode generated: %u bytes.\n",
1036 bytecode_get_len(&ctx
->bytecode
->b
));
1038 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1039 + bytecode_get_len(&ctx
->bytecode
->b
);
1040 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1042 /* No need to keep the memory stream. */
1043 if (fclose(fmem
) != 0) {
1051 filter_ir_free(ctx
);
1052 filter_parser_ctx_free(ctx
);
1054 if (fclose(fmem
) != 0) {
1062 * Enable event(s) for a channel, possibly with exclusions and a filter.
1063 * If no event name is specified, all events are enabled.
1064 * If no channel name is specified, the default name is used.
1065 * If filter expression is not NULL, the filter is set for the event.
1066 * If exclusion count is not zero, the exclusions are set for the event.
1067 * Returns size of returned session payload data or a negative error code.
1069 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1070 struct lttng_event
*ev
, const char *channel_name
,
1071 const char *original_filter_expression
,
1072 int exclusion_count
, char **exclusion_list
)
1074 struct lttcomm_session_msg lsm
;
1075 struct lttng_dynamic_buffer send_buffer
;
1076 int ret
= 0, i
, fd_to_send
= -1;
1077 bool send_fd
= false;
1078 unsigned int free_filter_expression
= 0;
1079 struct filter_parser_ctx
*ctx
= NULL
;
1082 * We have either a filter or some exclusions, so we need to set up
1083 * a variable-length memory block from where to send the data.
1085 lttng_dynamic_buffer_init(&send_buffer
);
1088 * Cast as non-const since we may replace the filter expression
1089 * by a dynamically allocated string. Otherwise, the original
1090 * string is not modified.
1092 char *filter_expression
= (char *) original_filter_expression
;
1094 if (handle
== NULL
|| ev
== NULL
) {
1095 ret
= -LTTNG_ERR_INVALID
;
1100 * Empty filter string will always be rejected by the parser
1101 * anyway, so treat this corner-case early to eliminate
1102 * lttng_fmemopen error for 0-byte allocation.
1104 if (filter_expression
&& filter_expression
[0] == '\0') {
1105 ret
= -LTTNG_ERR_INVALID
;
1109 memset(&lsm
, 0, sizeof(lsm
));
1111 /* If no channel name, send empty string. */
1112 if (channel_name
== NULL
) {
1113 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1114 sizeof(lsm
.u
.enable
.channel_name
));
1116 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1117 sizeof(lsm
.u
.enable
.channel_name
));
1120 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1121 if (ev
->name
[0] == '\0') {
1122 /* Enable all events */
1123 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1126 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1127 /* FIXME: copying non-packed struct to packed struct. */
1128 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1130 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1131 sizeof(lsm
.session
.name
));
1132 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1133 lsm
.u
.enable
.bytecode_len
= 0;
1135 /* Parse filter expression. */
1136 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1137 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1138 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1139 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1140 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1141 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1144 /* Setup JUL filter if needed. */
1145 agent_filter
= set_agent_filter(filter_expression
, ev
);
1146 if (!agent_filter
) {
1147 if (!filter_expression
) {
1149 * No JUL and no filter, just skip
1156 * With an agent filter, the original filter has
1157 * been added to it thus replace the filter
1160 filter_expression
= agent_filter
;
1161 free_filter_expression
= 1;
1165 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1171 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1172 lsm
.u
.enable
.bytecode_len
1173 + lsm
.u
.enable
.expression_len
1174 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1176 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1180 /* Put exclusion names first in the data. */
1181 for (i
= 0; i
< exclusion_count
; i
++) {
1182 size_t exclusion_len
;
1184 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1185 LTTNG_SYMBOL_NAME_LEN
);
1186 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1187 /* Exclusion is not NULL-terminated. */
1188 ret
= -LTTNG_ERR_INVALID
;
1192 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1193 *(exclusion_list
+ i
),
1194 LTTNG_SYMBOL_NAME_LEN
);
1200 /* Add filter expression next. */
1201 if (filter_expression
) {
1202 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1203 filter_expression
, lsm
.u
.enable
.expression_len
);
1208 /* Add filter bytecode next. */
1209 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1210 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1211 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1216 if (ev
->extended
.ptr
) {
1217 struct lttng_event_extended
*ev_ext
=
1218 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1220 if (ev_ext
->probe_location
) {
1222 * lttng_userspace_probe_location_serialize returns the
1223 * number of bytes that was appended to the buffer.
1225 ret
= lttng_userspace_probe_location_serialize(
1226 ev_ext
->probe_location
, &send_buffer
,
1232 send_fd
= fd_to_send
>= 0;
1234 * Set the size of the userspace probe location element
1235 * of the buffer so that the receiving side knows where
1238 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1242 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1243 send_fd
? &fd_to_send
: NULL
,
1245 send_buffer
.size
? send_buffer
.data
: NULL
,
1246 send_buffer
.size
, NULL
, NULL
, 0);
1249 if (filter_expression
&& ctx
) {
1250 filter_bytecode_free(ctx
);
1251 filter_ir_free(ctx
);
1252 filter_parser_ctx_free(ctx
);
1255 if (free_filter_expression
) {
1257 * The filter expression has been replaced and must be freed as
1258 * it is not the original filter expression received as a
1261 free(filter_expression
);
1265 * Return directly to the caller and don't ask the sessiond since
1266 * something went wrong in the parsing of data above.
1268 lttng_dynamic_buffer_reset(&send_buffer
);
1272 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1276 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1277 struct lttng_event
*ev
, const char *channel_name
,
1278 const char *original_filter_expression
)
1280 struct lttcomm_session_msg lsm
;
1283 unsigned int free_filter_expression
= 0;
1284 struct filter_parser_ctx
*ctx
= NULL
;
1286 * Cast as non-const since we may replace the filter expression
1287 * by a dynamically allocated string. Otherwise, the original
1288 * string is not modified.
1290 char *filter_expression
= (char *) original_filter_expression
;
1292 if (handle
== NULL
|| ev
== NULL
) {
1293 ret
= -LTTNG_ERR_INVALID
;
1298 * Empty filter string will always be rejected by the parser
1299 * anyway, so treat this corner-case early to eliminate
1300 * lttng_fmemopen error for 0-byte allocation.
1302 if (filter_expression
&& filter_expression
[0] == '\0') {
1303 ret
= -LTTNG_ERR_INVALID
;
1307 memset(&lsm
, 0, sizeof(lsm
));
1309 /* If no channel name, send empty string. */
1310 if (channel_name
== NULL
) {
1311 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1312 sizeof(lsm
.u
.disable
.channel_name
));
1314 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1315 sizeof(lsm
.u
.disable
.channel_name
));
1318 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1320 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1321 /* FIXME: copying non-packed struct to packed struct. */
1322 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1324 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1325 sizeof(lsm
.session
.name
));
1326 lsm
.u
.disable
.bytecode_len
= 0;
1329 * For the JUL domain, a filter is enforced except for the
1330 * disable all event. This is done to avoid having the event in
1331 * all sessions thus filtering by logger name.
1333 if (filter_expression
== NULL
&&
1334 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1335 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1336 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1341 * We have a filter, so we need to set up a variable-length
1342 * memory block from where to send the data.
1345 /* Parse filter expression */
1346 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1347 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1348 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1349 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1350 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1351 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1354 /* Setup JUL filter if needed. */
1355 agent_filter
= set_agent_filter(filter_expression
, ev
);
1356 if (!agent_filter
) {
1357 if (!filter_expression
) {
1359 * No JUL and no filter, just skip
1366 * With a JUL filter, the original filter has
1367 * been added to it thus replace the filter
1370 filter_expression
= agent_filter
;
1371 free_filter_expression
= 1;
1375 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1381 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1382 + lsm
.u
.disable
.expression_len
);
1384 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1388 /* Add filter expression. */
1389 if (lsm
.u
.disable
.expression_len
!= 0) {
1392 lsm
.u
.disable
.expression_len
);
1394 /* Add filter bytecode next. */
1395 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1397 + lsm
.u
.disable
.expression_len
,
1399 lsm
.u
.disable
.bytecode_len
);
1402 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1403 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1407 if (filter_expression
&& ctx
) {
1408 filter_bytecode_free(ctx
);
1409 filter_ir_free(ctx
);
1410 filter_parser_ctx_free(ctx
);
1413 if (free_filter_expression
) {
1415 * The filter expression has been replaced and must be freed as
1416 * it is not the original filter expression received as a
1419 free(filter_expression
);
1423 * Return directly to the caller and don't ask the sessiond since
1424 * something went wrong in the parsing of data above.
1429 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1434 * Disable event(s) of a channel and domain.
1435 * If no event name is specified, all events are disabled.
1436 * If no channel name is specified, the default 'channel0' is used.
1437 * Returns size of returned session payload data or a negative error code.
1439 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1440 const char *channel_name
)
1442 struct lttng_event ev
;
1444 memset(&ev
, 0, sizeof(ev
));
1446 ev
.type
= LTTNG_EVENT_ALL
;
1447 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1448 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1451 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1453 struct lttng_channel
*channel
= NULL
;
1454 struct lttng_channel_extended
*extended
= NULL
;
1460 /* Validate domain. */
1461 switch (domain
->type
) {
1462 case LTTNG_DOMAIN_UST
:
1463 switch (domain
->buf_type
) {
1464 case LTTNG_BUFFER_PER_UID
:
1465 case LTTNG_BUFFER_PER_PID
:
1471 case LTTNG_DOMAIN_KERNEL
:
1472 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1480 channel
= zmalloc(sizeof(*channel
));
1485 extended
= zmalloc(sizeof(*extended
));
1490 channel
->attr
.extended
.ptr
= extended
;
1492 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1500 void lttng_channel_destroy(struct lttng_channel
*channel
)
1506 if (channel
->attr
.extended
.ptr
) {
1507 free(channel
->attr
.extended
.ptr
);
1513 * Enable channel per domain
1514 * Returns size of returned session payload data or a negative error code.
1516 int lttng_enable_channel(struct lttng_handle
*handle
,
1517 struct lttng_channel
*in_chan
)
1519 struct lttcomm_session_msg lsm
;
1520 size_t total_buffer_size_needed_per_cpu
= 0;
1522 /* NULL arguments are forbidden. No default values. */
1523 if (handle
== NULL
|| in_chan
== NULL
) {
1524 return -LTTNG_ERR_INVALID
;
1527 memset(&lsm
, 0, sizeof(lsm
));
1528 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1529 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1531 if (!in_chan
->attr
.extended
.ptr
) {
1532 struct lttng_channel
*channel
;
1533 struct lttng_channel_extended
*extended
;
1535 channel
= lttng_channel_create(&handle
->domain
);
1537 return -LTTNG_ERR_NOMEM
;
1541 * Create a new channel in order to use default extended
1544 extended
= (struct lttng_channel_extended
*)
1545 channel
->attr
.extended
.ptr
;
1546 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1547 lttng_channel_destroy(channel
);
1549 struct lttng_channel_extended
*extended
;
1551 extended
= (struct lttng_channel_extended
*)
1552 in_chan
->attr
.extended
.ptr
;
1553 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1557 * Verify that the amount of memory required to create the requested
1558 * buffer is available on the system at the moment.
1560 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1561 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1562 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1563 return -LTTNG_ERR_NOMEM
;
1566 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1567 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1569 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1570 sizeof(lsm
.session
.name
));
1572 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1576 * All tracing will be stopped for registered events of the channel.
1577 * Returns size of returned session payload data or a negative error code.
1579 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1581 struct lttcomm_session_msg lsm
;
1583 /* Safety check. Both are mandatory. */
1584 if (handle
== NULL
|| name
== NULL
) {
1585 return -LTTNG_ERR_INVALID
;
1588 memset(&lsm
, 0, sizeof(lsm
));
1590 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1592 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1593 sizeof(lsm
.u
.disable
.channel_name
));
1595 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1597 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1598 sizeof(lsm
.session
.name
));
1600 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1604 * Add PID to session tracker.
1605 * Return 0 on success else a negative LTTng error code.
1607 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1609 struct lttcomm_session_msg lsm
;
1611 /* NULL arguments are forbidden. No default values. */
1612 if (handle
== NULL
) {
1613 return -LTTNG_ERR_INVALID
;
1616 memset(&lsm
, 0, sizeof(lsm
));
1618 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1619 lsm
.u
.pid_tracker
.pid
= pid
;
1621 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1623 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1624 sizeof(lsm
.session
.name
));
1626 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1630 * Remove PID from session tracker.
1631 * Return 0 on success else a negative LTTng error code.
1633 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1635 struct lttcomm_session_msg lsm
;
1637 /* NULL arguments are forbidden. No default values. */
1638 if (handle
== NULL
) {
1639 return -LTTNG_ERR_INVALID
;
1642 memset(&lsm
, 0, sizeof(lsm
));
1644 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1645 lsm
.u
.pid_tracker
.pid
= pid
;
1647 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1649 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1650 sizeof(lsm
.session
.name
));
1652 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1656 * Lists all available tracepoints of domain.
1657 * Sets the contents of the events array.
1658 * Returns the number of lttng_event entries in events;
1659 * on error, returns a negative value.
1661 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1662 struct lttng_event
**events
)
1665 struct lttcomm_session_msg lsm
;
1667 if (handle
== NULL
) {
1668 return -LTTNG_ERR_INVALID
;
1671 memset(&lsm
, 0, sizeof(lsm
));
1672 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1673 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1675 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1680 return ret
/ sizeof(struct lttng_event
);
1684 * Lists all available tracepoint fields of domain.
1685 * Sets the contents of the event field array.
1686 * Returns the number of lttng_event_field entries in events;
1687 * on error, returns a negative value.
1689 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1690 struct lttng_event_field
**fields
)
1693 struct lttcomm_session_msg lsm
;
1695 if (handle
== NULL
) {
1696 return -LTTNG_ERR_INVALID
;
1699 memset(&lsm
, 0, sizeof(lsm
));
1700 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1701 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1703 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1708 return ret
/ sizeof(struct lttng_event_field
);
1712 * Lists all available kernel system calls. Allocates and sets the contents of
1715 * Returns the number of lttng_event entries in events; on error, returns a
1718 int lttng_list_syscalls(struct lttng_event
**events
)
1721 struct lttcomm_session_msg lsm
;
1724 return -LTTNG_ERR_INVALID
;
1727 memset(&lsm
, 0, sizeof(lsm
));
1728 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1729 /* Force kernel domain for system calls. */
1730 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1732 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1737 return ret
/ sizeof(struct lttng_event
);
1741 * Returns a human readable string describing
1742 * the error code (a negative value).
1744 const char *lttng_strerror(int code
)
1746 return error_get_str(code
);
1749 enum lttng_error_code
lttng_create_session_ext(
1750 struct lttng_session_descriptor
*session_descriptor
)
1752 enum lttng_error_code ret_code
;
1753 struct lttcomm_session_msg lsm
= {
1754 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1757 struct lttng_buffer_view reply_view
;
1759 bool sessiond_must_generate_ouput
;
1760 struct lttng_dynamic_buffer payload
;
1762 size_t descriptor_size
;
1763 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1765 lttng_dynamic_buffer_init(&payload
);
1766 if (!session_descriptor
) {
1767 ret_code
= LTTNG_ERR_INVALID
;
1771 sessiond_must_generate_ouput
=
1772 !lttng_session_descriptor_is_output_destination_initialized(
1773 session_descriptor
);
1774 if (sessiond_must_generate_ouput
) {
1775 const char *home_dir
= utils_get_home_dir();
1776 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1778 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1779 ret_code
= LTTNG_ERR_FATAL
;
1783 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1784 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1787 ret_code
= LTTNG_ERR_NOMEM
;
1792 descriptor_size
= payload
.size
;
1793 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1796 ret_code
= LTTNG_ERR_INVALID
;
1799 descriptor_size
= payload
.size
- descriptor_size
;
1800 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1802 /* Command returns a session descriptor on success. */
1803 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1804 payload
.size
, &reply
);
1805 if (reply_ret
< 0) {
1806 ret_code
= -reply_ret
;
1808 } else if (reply_ret
== 0) {
1809 /* Socket unexpectedly closed by the session daemon. */
1810 ret_code
= LTTNG_ERR_FATAL
;
1814 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1815 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1818 ret_code
= LTTNG_ERR_FATAL
;
1821 ret_code
= LTTNG_OK
;
1822 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1825 lttng_dynamic_buffer_reset(&payload
);
1826 lttng_session_descriptor_destroy(descriptor_reply
);
1831 * Create a new session using name and url for destination.
1833 * Returns LTTNG_OK on success or a negative error code.
1835 int lttng_create_session(const char *name
, const char *url
)
1839 struct lttng_uri
*uris
= NULL
;
1840 struct lttng_session_descriptor
*descriptor
= NULL
;
1841 enum lttng_error_code ret_code
;
1844 ret
= -LTTNG_ERR_INVALID
;
1848 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1850 ret
= -LTTNG_ERR_INVALID
;
1855 descriptor
= lttng_session_descriptor_create(name
);
1858 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1859 ret
= -LTTNG_ERR_INVALID
;
1862 descriptor
= lttng_session_descriptor_local_create(name
,
1866 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1870 ret
= -LTTNG_ERR_INVALID
;
1874 ret
= -LTTNG_ERR_INVALID
;
1877 ret_code
= lttng_create_session_ext(descriptor
);
1878 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1880 lttng_session_descriptor_destroy(descriptor
);
1886 * Create a session exclusively used for snapshot.
1888 * Returns LTTNG_OK on success or a negative error code.
1890 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1893 enum lttng_error_code ret_code
;
1895 struct lttng_uri
*uris
= NULL
;
1896 struct lttng_session_descriptor
*descriptor
= NULL
;
1899 ret
= -LTTNG_ERR_INVALID
;
1903 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1905 ret
= -LTTNG_ERR_INVALID
;
1909 * If the user does not specify a custom subdir, use the session name.
1911 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1912 strlen(uris
[0].subdir
) == 0) {
1913 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1916 PERROR("Failed to set session name as network destination sub-directory");
1917 ret
= -LTTNG_ERR_FATAL
;
1919 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1920 /* Truncated output. */
1921 ret
= -LTTNG_ERR_INVALID
;
1928 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1931 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1932 ret
= -LTTNG_ERR_INVALID
;
1935 descriptor
= lttng_session_descriptor_snapshot_local_create(
1940 descriptor
= lttng_session_descriptor_snapshot_network_create(
1946 ret
= -LTTNG_ERR_INVALID
;
1950 ret
= -LTTNG_ERR_INVALID
;
1953 ret_code
= lttng_create_session_ext(descriptor
);
1954 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1956 lttng_session_descriptor_destroy(descriptor
);
1962 * Create a session exclusively used for live.
1964 * Returns LTTNG_OK on success or a negative error code.
1966 int lttng_create_session_live(const char *name
, const char *url
,
1967 unsigned int timer_interval
)
1970 enum lttng_error_code ret_code
;
1971 struct lttng_session_descriptor
*descriptor
= NULL
;
1974 ret
= -LTTNG_ERR_INVALID
;
1979 descriptor
= lttng_session_descriptor_live_network_create(
1980 name
, url
, NULL
, timer_interval
);
1982 descriptor
= lttng_session_descriptor_live_create(
1983 name
, timer_interval
);
1986 ret
= -LTTNG_ERR_INVALID
;
1989 ret_code
= lttng_create_session_ext(descriptor
);
1990 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1992 lttng_session_descriptor_destroy(descriptor
);
1997 * Destroy session using name.
1998 * Returns size of returned session payload data or a negative error code.
2001 int _lttng_destroy_session(const char *session_name
)
2003 struct lttcomm_session_msg lsm
;
2005 if (session_name
== NULL
) {
2006 return -LTTNG_ERR_INVALID
;
2009 memset(&lsm
, 0, sizeof(lsm
));
2010 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
2012 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2013 sizeof(lsm
.session
.name
));
2015 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2019 * Stop the session and wait for the data before destroying it
2021 int lttng_destroy_session(const char *session_name
)
2026 * Stop the tracing and wait for the data.
2028 ret
= _lttng_stop_tracing(session_name
, 1);
2029 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2033 ret
= _lttng_destroy_session(session_name
);
2039 * Destroy the session without waiting for the data.
2041 int lttng_destroy_session_no_wait(const char *session_name
)
2046 * Stop the tracing without waiting for the data.
2047 * The session might already have been stopped, so just
2050 ret
= _lttng_stop_tracing(session_name
, 0);
2051 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2055 ret
= _lttng_destroy_session(session_name
);
2061 * Ask the session daemon for all available sessions.
2062 * Sets the contents of the sessions array.
2063 * Returns the number of lttng_session entries in sessions;
2064 * on error, returns a negative value.
2066 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2069 struct lttcomm_session_msg lsm
;
2070 const size_t session_size
= sizeof(struct lttng_session
) +
2071 sizeof(struct lttng_session_extended
);
2072 size_t session_count
, i
;
2073 struct lttng_session_extended
*sessions_extended_begin
;
2074 struct lttng_session
*sessions
= NULL
;
2076 memset(&lsm
, 0, sizeof(lsm
));
2077 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2078 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2080 ret
= ret
== 0 ? -LTTNG_ERR_FATAL
: ret
;
2084 ret
= -LTTNG_ERR_FATAL
;
2088 if (ret
% session_size
) {
2089 ret
= -LTTNG_ERR_UNK
;
2091 *out_sessions
= NULL
;
2094 session_count
= (size_t) ret
/ session_size
;
2095 sessions_extended_begin
= (struct lttng_session_extended
*)
2096 (&sessions
[session_count
]);
2098 /* Set extended session info pointers. */
2099 for (i
= 0; i
< session_count
; i
++) {
2100 struct lttng_session
*session
= &sessions
[i
];
2101 struct lttng_session_extended
*extended
=
2102 &(sessions_extended_begin
[i
]);
2104 session
->extended
.ptr
= extended
;
2107 ret
= (int) session_count
;
2108 *out_sessions
= sessions
;
2113 enum lttng_error_code
lttng_session_get_creation_time(
2114 const struct lttng_session
*session
, uint64_t *creation_time
)
2116 enum lttng_error_code ret
= LTTNG_OK
;
2117 struct lttng_session_extended
*extended
;
2119 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2120 ret
= LTTNG_ERR_INVALID
;
2124 extended
= session
->extended
.ptr
;
2125 if (!extended
->creation_time
.is_set
) {
2126 /* Not created on the session daemon yet. */
2127 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2130 *creation_time
= extended
->creation_time
.value
;
2135 int lttng_set_session_shm_path(const char *session_name
,
2136 const char *shm_path
)
2138 struct lttcomm_session_msg lsm
;
2140 if (session_name
== NULL
) {
2141 return -LTTNG_ERR_INVALID
;
2144 memset(&lsm
, 0, sizeof(lsm
));
2145 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2147 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2148 sizeof(lsm
.session
.name
));
2149 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
2150 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2152 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2156 * Ask the session daemon for all available domains of a session.
2157 * Sets the contents of the domains array.
2158 * Returns the number of lttng_domain entries in domains;
2159 * on error, returns a negative value.
2161 int lttng_list_domains(const char *session_name
,
2162 struct lttng_domain
**domains
)
2165 struct lttcomm_session_msg lsm
;
2167 if (session_name
== NULL
) {
2168 return -LTTNG_ERR_INVALID
;
2171 memset(&lsm
, 0, sizeof(lsm
));
2172 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2174 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2175 sizeof(lsm
.session
.name
));
2177 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2182 return ret
/ sizeof(struct lttng_domain
);
2186 * Ask the session daemon for all available channels of a session.
2187 * Sets the contents of the channels array.
2188 * Returns the number of lttng_channel entries in channels;
2189 * on error, returns a negative value.
2191 int lttng_list_channels(struct lttng_handle
*handle
,
2192 struct lttng_channel
**channels
)
2195 size_t channel_count
, i
;
2196 const size_t channel_size
= sizeof(struct lttng_channel
) +
2197 sizeof(struct lttng_channel_extended
);
2198 struct lttcomm_session_msg lsm
;
2201 if (handle
== NULL
) {
2202 ret
= -LTTNG_ERR_INVALID
;
2206 memset(&lsm
, 0, sizeof(lsm
));
2207 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2208 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2209 sizeof(lsm
.session
.name
));
2211 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2213 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2218 if (ret
% channel_size
) {
2219 ret
= -LTTNG_ERR_UNK
;
2224 channel_count
= (size_t) ret
/ channel_size
;
2226 /* Set extended info pointers */
2227 extended_at
= ((void *) *channels
) +
2228 channel_count
* sizeof(struct lttng_channel
);
2229 for (i
= 0; i
< channel_count
; i
++) {
2230 struct lttng_channel
*chan
= &(*channels
)[i
];
2232 chan
->attr
.extended
.ptr
= extended_at
;
2233 extended_at
+= sizeof(struct lttng_channel_extended
);
2236 ret
= (int) channel_count
;
2242 * Ask the session daemon for all available events of a session channel.
2243 * Sets the contents of the events array.
2244 * Returns the number of lttng_event entries in events;
2245 * on error, returns a negative value.
2247 int lttng_list_events(struct lttng_handle
*handle
,
2248 const char *channel_name
, struct lttng_event
**events
)
2251 struct lttcomm_session_msg lsm
;
2252 struct lttcomm_event_command_header
*cmd_header
= NULL
;
2253 size_t cmd_header_len
;
2254 uint32_t nb_events
, i
;
2256 char *reception_buffer
= NULL
;
2257 struct lttng_dynamic_buffer listing
;
2260 /* Safety check. An handle and channel name are mandatory */
2261 if (handle
== NULL
|| channel_name
== NULL
) {
2262 return -LTTNG_ERR_INVALID
;
2265 memset(&lsm
, 0, sizeof(lsm
));
2266 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2267 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2268 sizeof(lsm
.session
.name
));
2269 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2270 sizeof(lsm
.u
.list
.channel_name
));
2271 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2273 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2274 (void **) &reception_buffer
, (void **) &cmd_header
,
2281 ret
= -LTTNG_ERR_UNK
;
2285 /* Set number of events and free command header */
2286 nb_events
= cmd_header
->nb_events
;
2287 if (nb_events
> INT_MAX
) {
2295 * The buffer that is returned must contain a "flat" version of
2296 * the events that are returned. In other words, all pointers
2297 * within an lttng_event must point to a location within the returned
2298 * buffer so that the user may free everything by simply calling free()
2299 * on the returned buffer. This is needed in order to maintain API
2302 * A first pass is performed to compute the size of the buffer that
2303 * must be allocated. A second pass is then performed to setup
2304 * the returned events so that their members always point within the
2307 * The layout of the returned buffer is as follows:
2308 * - struct lttng_event[nb_events],
2309 * - nb_events times the following:
2310 * - struct lttng_event_extended,
2311 * - flattened version of userspace_probe_location
2312 * - filter_expression
2314 * - padding to align to 64-bits
2316 comm_ext_at
= reception_buffer
+
2317 (nb_events
* sizeof(struct lttng_event
));
2318 storage_req
= nb_events
* sizeof(struct lttng_event
);
2320 for (i
= 0; i
< nb_events
; i
++) {
2321 struct lttcomm_event_extended_header
*ext_comm
=
2322 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2323 int probe_storage_req
= 0;
2325 comm_ext_at
+= sizeof(*ext_comm
);
2326 comm_ext_at
+= ext_comm
->filter_len
;
2328 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2330 if (ext_comm
->userspace_probe_location_len
) {
2331 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2332 struct lttng_buffer_view probe_location_view
;
2334 probe_location_view
= lttng_buffer_view_init(
2336 ext_comm
->userspace_probe_location_len
);
2339 * Create a temporary userspace probe location to
2340 * determine the size needed by a "flattened" version
2341 * of that same probe location.
2343 ret
= lttng_userspace_probe_location_create_from_buffer(
2344 &probe_location_view
, &probe_location
);
2346 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2350 ret
= lttng_userspace_probe_location_flatten(
2351 probe_location
, NULL
);
2352 lttng_userspace_probe_location_destroy(probe_location
);
2354 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2358 probe_storage_req
= ret
;
2359 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2362 storage_req
+= sizeof(struct lttng_event_extended
);
2363 storage_req
+= ext_comm
->filter_len
;
2364 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2365 /* Padding to ensure the flat probe is aligned. */
2366 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2367 storage_req
+= probe_storage_req
;
2370 lttng_dynamic_buffer_init(&listing
);
2372 * We must ensure that "listing" is never resized so as to preserve
2373 * the validity of the flattened objects.
2375 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2377 ret
= -LTTNG_ERR_NOMEM
;
2381 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2382 nb_events
* sizeof(struct lttng_event
));
2384 ret
= -LTTNG_ERR_NOMEM
;
2385 goto free_dynamic_buffer
;
2388 comm_ext_at
= reception_buffer
+
2389 (nb_events
* sizeof(struct lttng_event
));
2390 for (i
= 0; i
< nb_events
; i
++) {
2391 struct lttng_event
*event
= (struct lttng_event
*)
2392 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2393 struct lttcomm_event_extended_header
*ext_comm
=
2394 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2395 struct lttng_event_extended
*event_extended
=
2396 (struct lttng_event_extended
*)
2397 (listing
.data
+ listing
.size
);
2399 /* Insert struct lttng_event_extended. */
2400 ret
= lttng_dynamic_buffer_set_size(&listing
,
2401 listing
.size
+ sizeof(*event_extended
));
2403 ret
= -LTTNG_ERR_NOMEM
;
2404 goto free_dynamic_buffer
;
2406 event
->extended
.ptr
= event_extended
;
2408 comm_ext_at
+= sizeof(*ext_comm
);
2410 /* Insert filter expression. */
2411 if (ext_comm
->filter_len
) {
2412 event_extended
->filter_expression
= listing
.data
+
2414 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2415 ext_comm
->filter_len
);
2417 ret
= -LTTNG_ERR_NOMEM
;
2418 goto free_dynamic_buffer
;
2420 comm_ext_at
+= ext_comm
->filter_len
;
2423 /* Insert exclusions. */
2424 if (ext_comm
->nb_exclusions
) {
2425 event_extended
->exclusions
.count
=
2426 ext_comm
->nb_exclusions
;
2427 event_extended
->exclusions
.strings
=
2428 listing
.data
+ listing
.size
;
2430 ret
= lttng_dynamic_buffer_append(&listing
,
2432 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2434 ret
= -LTTNG_ERR_NOMEM
;
2435 goto free_dynamic_buffer
;
2437 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2440 /* Insert padding to align to 64-bits. */
2441 ret
= lttng_dynamic_buffer_set_size(&listing
,
2442 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2444 ret
= -LTTNG_ERR_NOMEM
;
2445 goto free_dynamic_buffer
;
2448 /* Insert flattened userspace probe location. */
2449 if (ext_comm
->userspace_probe_location_len
) {
2450 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2451 struct lttng_buffer_view probe_location_view
;
2453 probe_location_view
= lttng_buffer_view_init(
2455 ext_comm
->userspace_probe_location_len
);
2457 ret
= lttng_userspace_probe_location_create_from_buffer(
2458 &probe_location_view
, &probe_location
);
2460 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2461 goto free_dynamic_buffer
;
2464 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2465 (listing
.data
+ listing
.size
);
2466 ret
= lttng_userspace_probe_location_flatten(
2467 probe_location
, &listing
);
2468 lttng_userspace_probe_location_destroy(probe_location
);
2470 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2471 goto free_dynamic_buffer
;
2474 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2478 /* Don't reset listing buffer as we return its content. */
2479 *events
= (struct lttng_event
*) listing
.data
;
2480 lttng_dynamic_buffer_init(&listing
);
2481 ret
= (int) nb_events
;
2482 free_dynamic_buffer
:
2483 lttng_dynamic_buffer_reset(&listing
);
2486 free(reception_buffer
);
2491 * Sets the tracing_group variable with name.
2492 * This function allocates memory pointed to by tracing_group.
2493 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2495 int lttng_set_tracing_group(const char *name
)
2498 return -LTTNG_ERR_INVALID
;
2501 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2502 return -LTTNG_ERR_FATAL
;
2508 int lttng_calibrate(struct lttng_handle
*handle
,
2509 struct lttng_calibrate
*calibrate
)
2512 * This command was removed in LTTng 2.9.
2514 return -LTTNG_ERR_UND
;
2518 * Set default channel attributes.
2519 * If either or both of the arguments are null, attr content is zeroe'd.
2521 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2522 struct lttng_channel_attr
*attr
)
2524 struct lttng_channel_extended
*extended
;
2527 if (attr
== NULL
|| domain
== NULL
) {
2531 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2532 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2534 /* Same for all domains. */
2535 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2536 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2537 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2539 switch (domain
->type
) {
2540 case LTTNG_DOMAIN_KERNEL
:
2541 attr
->switch_timer_interval
=
2542 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2543 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2544 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2545 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2546 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2548 extended
->monitor_timer_interval
=
2549 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2550 extended
->blocking_timeout
=
2551 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2554 case LTTNG_DOMAIN_UST
:
2555 switch (domain
->buf_type
) {
2556 case LTTNG_BUFFER_PER_UID
:
2557 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2558 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2559 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2560 attr
->switch_timer_interval
=
2561 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2562 attr
->read_timer_interval
=
2563 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2565 extended
->monitor_timer_interval
=
2566 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2567 extended
->blocking_timeout
=
2568 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2571 case LTTNG_BUFFER_PER_PID
:
2573 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2574 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2575 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2576 attr
->switch_timer_interval
=
2577 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2578 attr
->read_timer_interval
=
2579 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2581 extended
->monitor_timer_interval
=
2582 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2583 extended
->blocking_timeout
=
2584 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2589 /* Default behavior: leave set to 0. */
2593 attr
->extended
.ptr
= extended
;
2596 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2597 uint64_t *discarded_events
)
2600 struct lttng_channel_extended
*chan_ext
;
2602 if (!channel
|| !discarded_events
) {
2603 ret
= -LTTNG_ERR_INVALID
;
2607 chan_ext
= channel
->attr
.extended
.ptr
;
2610 * This can happen since the lttng_channel structure is
2611 * used for other tasks where this pointer is never set.
2613 *discarded_events
= 0;
2617 *discarded_events
= chan_ext
->discarded_events
;
2622 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2623 uint64_t *lost_packets
)
2626 struct lttng_channel_extended
*chan_ext
;
2628 if (!channel
|| !lost_packets
) {
2629 ret
= -LTTNG_ERR_INVALID
;
2633 chan_ext
= channel
->attr
.extended
.ptr
;
2636 * This can happen since the lttng_channel structure is
2637 * used for other tasks where this pointer is never set.
2643 *lost_packets
= chan_ext
->lost_packets
;
2648 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2649 uint64_t *monitor_timer_interval
)
2653 if (!chan
|| !monitor_timer_interval
) {
2654 ret
= -LTTNG_ERR_INVALID
;
2658 if (!chan
->attr
.extended
.ptr
) {
2659 ret
= -LTTNG_ERR_INVALID
;
2663 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2664 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2669 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2670 uint64_t monitor_timer_interval
)
2674 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2675 ret
= -LTTNG_ERR_INVALID
;
2679 ((struct lttng_channel_extended
*)
2680 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2681 monitor_timer_interval
;
2686 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2687 int64_t *blocking_timeout
)
2691 if (!chan
|| !blocking_timeout
) {
2692 ret
= -LTTNG_ERR_INVALID
;
2696 if (!chan
->attr
.extended
.ptr
) {
2697 ret
= -LTTNG_ERR_INVALID
;
2701 *blocking_timeout
= ((struct lttng_channel_extended
*)
2702 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2707 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2708 int64_t blocking_timeout
)
2711 int64_t msec_timeout
;
2713 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2714 ret
= -LTTNG_ERR_INVALID
;
2718 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2719 ret
= -LTTNG_ERR_INVALID
;
2724 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2725 * us to accept a narrower range of values (msecs expressed as a signed
2728 msec_timeout
= blocking_timeout
/ 1000;
2729 if (msec_timeout
!= (int32_t) msec_timeout
) {
2730 ret
= -LTTNG_ERR_INVALID
;
2734 ((struct lttng_channel_extended
*)
2735 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2742 * Check if session daemon is alive.
2744 * Return 1 if alive or 0 if not.
2745 * On error returns a negative value.
2747 int lttng_session_daemon_alive(void)
2751 ret
= set_session_daemon_path();
2757 if (*sessiond_sock_path
== '\0') {
2759 * No socket path set. Weird error which means the constructor
2765 ret
= try_connect_sessiond(sessiond_sock_path
);
2776 * Set URL for a consumer for a session and domain.
2778 * Return 0 on success, else a negative value.
2780 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2781 const char *control_url
, const char *data_url
)
2785 struct lttcomm_session_msg lsm
;
2786 struct lttng_uri
*uris
= NULL
;
2788 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2789 return -LTTNG_ERR_INVALID
;
2792 memset(&lsm
, 0, sizeof(lsm
));
2794 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2796 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2797 sizeof(lsm
.session
.name
));
2798 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2800 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2802 return -LTTNG_ERR_INVALID
;
2805 lsm
.u
.uri
.size
= size
;
2807 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2808 sizeof(struct lttng_uri
) * size
, NULL
);
2817 int lttng_enable_consumer(struct lttng_handle
*handle
)
2825 int lttng_disable_consumer(struct lttng_handle
*handle
)
2833 int _lttng_create_session_ext(const char *name
, const char *url
,
2834 const char *datetime
)
2840 * For a given session name, this call checks if the data is ready to be read
2841 * or is still being extracted by the consumer(s) hence not ready to be used by
2844 int lttng_data_pending(const char *session_name
)
2847 struct lttcomm_session_msg lsm
;
2848 uint8_t *pending
= NULL
;
2850 if (session_name
== NULL
) {
2851 return -LTTNG_ERR_INVALID
;
2854 memset(&lsm
, 0, sizeof(lsm
));
2855 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2857 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2858 sizeof(lsm
.session
.name
));
2860 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2863 } else if (ret
!= 1) {
2864 /* Unexpected payload size */
2865 ret
= -LTTNG_ERR_INVALID
;
2869 ret
= (int) *pending
;
2876 * List PIDs in the tracker.
2878 * enabled is set to whether the PID tracker is enabled.
2879 * pids is set to an allocated array of PIDs currently tracked. On
2880 * success, pids must be freed by the caller.
2881 * nr_pids is set to the number of entries contained by the pids array.
2883 * Returns 0 on success, else a negative LTTng error code.
2885 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2886 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2890 struct lttcomm_session_msg lsm
;
2892 int32_t *pids
= NULL
;
2894 if (handle
== NULL
) {
2895 return -LTTNG_ERR_INVALID
;
2898 memset(&lsm
, 0, sizeof(lsm
));
2899 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2900 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2901 sizeof(lsm
.session
.name
));
2902 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2904 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2908 nr_pids
= ret
/ sizeof(int32_t);
2909 if (nr_pids
> 0 && !pids
) {
2910 return -LTTNG_ERR_UNK
;
2912 if (nr_pids
== 1 && pids
[0] == -1) {
2918 *_enabled
= enabled
;
2920 *_nr_pids
= nr_pids
;
2925 * Regenerate the metadata for a session.
2926 * Return 0 on success, a negative error code on error.
2928 int lttng_regenerate_metadata(const char *session_name
)
2931 struct lttcomm_session_msg lsm
;
2933 if (!session_name
) {
2934 ret
= -LTTNG_ERR_INVALID
;
2938 memset(&lsm
, 0, sizeof(lsm
));
2939 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2941 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2942 sizeof(lsm
.session
.name
));
2944 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2955 * Deprecated, replaced by lttng_regenerate_metadata.
2957 int lttng_metadata_regenerate(const char *session_name
)
2959 return lttng_regenerate_metadata(session_name
);
2963 * Regenerate the statedump of a session.
2964 * Return 0 on success, a negative error code on error.
2966 int lttng_regenerate_statedump(const char *session_name
)
2969 struct lttcomm_session_msg lsm
;
2971 if (!session_name
) {
2972 ret
= -LTTNG_ERR_INVALID
;
2976 memset(&lsm
, 0, sizeof(lsm
));
2977 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2979 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2980 sizeof(lsm
.session
.name
));
2982 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2992 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2995 struct lttcomm_session_msg lsm
;
2996 struct lttng_dynamic_buffer buffer
;
2998 lttng_dynamic_buffer_init(&buffer
);
3000 ret
= -LTTNG_ERR_INVALID
;
3004 if (!lttng_trigger_validate(trigger
)) {
3005 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3009 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3011 ret
= -LTTNG_ERR_UNK
;
3015 memset(&lsm
, 0, sizeof(lsm
));
3016 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3017 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3018 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3021 lttng_dynamic_buffer_reset(&buffer
);
3025 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3028 struct lttcomm_session_msg lsm
;
3029 struct lttng_dynamic_buffer buffer
;
3031 lttng_dynamic_buffer_init(&buffer
);
3033 ret
= -LTTNG_ERR_INVALID
;
3037 if (!lttng_trigger_validate(trigger
)) {
3038 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3042 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3044 ret
= -LTTNG_ERR_UNK
;
3048 memset(&lsm
, 0, sizeof(lsm
));
3049 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3050 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3051 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3054 lttng_dynamic_buffer_reset(&buffer
);
3061 static void __attribute__((constructor
)) init(void)
3063 /* Set default session group */
3064 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3070 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3072 free(tracing_group
);