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/defaults.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/uri.h>
36 #include <common/utils.h>
37 #include <lttng/lttng.h>
38 #include <lttng/health-internal.h>
39 #include <lttng/trigger/trigger-internal.h>
40 #include <lttng/endpoint.h>
41 #include <lttng/channel-internal.h>
43 #include "filter/filter-ast.h"
44 #include "filter/filter-parser.h"
45 #include "filter/filter-bytecode.h"
46 #include "filter/memstream.h"
47 #include "lttng-ctl-helper.h"
50 static const int print_xml
= 1;
51 #define dbg_printf(fmt, args...) \
52 printf("[debug liblttng-ctl] " fmt, ## args)
54 static const int print_xml
= 0;
55 #define dbg_printf(fmt, args...) \
57 /* do nothing but check printf format */ \
59 printf("[debug liblttnctl] " fmt, ## args); \
64 /* Socket to session daemon for communication */
65 static int sessiond_socket
;
66 static char sessiond_sock_path
[PATH_MAX
];
69 static char *tracing_group
;
75 * Those two variables are used by error.h to silent or control the verbosity of
76 * error message. They are global to the library so application linking with it
77 * are able to compile correctly and also control verbosity of the library.
80 int lttng_opt_verbose
;
84 * Copy string from src to dst and enforce null terminated byte.
87 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
90 strncpy(dst
, src
, len
);
91 /* Enforce the NULL terminated byte */
99 * Copy domain to lttcomm_session_msg domain.
101 * If domain is unknown, default domain will be the kernel.
104 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
105 struct lttng_domain
*src
)
109 case LTTNG_DOMAIN_KERNEL
:
110 case LTTNG_DOMAIN_UST
:
111 case LTTNG_DOMAIN_JUL
:
112 case LTTNG_DOMAIN_LOG4J
:
113 case LTTNG_DOMAIN_PYTHON
:
114 memcpy(dst
, src
, sizeof(struct lttng_domain
));
117 memset(dst
, 0, sizeof(struct lttng_domain
));
124 * Send lttcomm_session_msg to the session daemon.
126 * On success, returns the number of bytes sent (>=0)
127 * On error, returns -1
129 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
134 ret
= -LTTNG_ERR_NO_SESSIOND
;
138 DBG("LSM cmd type : %d", lsm
->cmd_type
);
140 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
141 sizeof(struct lttcomm_session_msg
));
143 ret
= -LTTNG_ERR_FATAL
;
151 * Send var len data to the session daemon.
153 * On success, returns the number of bytes sent (>=0)
154 * On error, returns -1
156 static int send_session_varlen(const void *data
, size_t len
)
161 ret
= -LTTNG_ERR_NO_SESSIOND
;
170 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
172 ret
= -LTTNG_ERR_FATAL
;
180 * Receive data from the sessiond socket.
182 * On success, returns the number of bytes received (>=0)
183 * On error, returns -1 (recvmsg() error) or -ENOTCONN
185 static int recv_data_sessiond(void *buf
, size_t len
)
190 ret
= -LTTNG_ERR_NO_SESSIOND
;
194 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
196 ret
= -LTTNG_ERR_FATAL
;
204 * Check if we are in the specified group.
206 * If yes return 1, else return -1.
209 int lttng_check_tracing_group(void)
211 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
213 int grp_list_size
, grp_id
, i
;
215 const char *grp_name
= tracing_group
;
217 /* Get GID of group 'tracing' */
218 grp_tracing
= getgrnam(grp_name
);
220 /* If grp_tracing is NULL, the group does not exist. */
224 /* Get number of supplementary group IDs */
225 grp_list_size
= getgroups(0, NULL
);
226 if (grp_list_size
< 0) {
231 /* Alloc group list of the right size */
232 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
237 grp_id
= getgroups(grp_list_size
, grp_list
);
243 for (i
= 0; i
< grp_list_size
; i
++) {
244 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
258 * Try connect to session daemon with sock_path.
260 * Return 0 on success, else -1
262 static int try_connect_sessiond(const char *sock_path
)
266 /* If socket exist, we check if the daemon listens for connect. */
267 ret
= access(sock_path
, F_OK
);
273 ret
= lttcomm_connect_unix_sock(sock_path
);
279 ret
= lttcomm_close_unix_sock(ret
);
281 PERROR("lttcomm_close_unix_sock");
291 * Set sessiond socket path by putting it in the global sessiond_sock_path
294 * Returns 0 on success, negative value on failure (the sessiond socket path
295 * is somehow too long or ENOMEM).
297 static int set_session_daemon_path(void)
299 int in_tgroup
= 0; /* In tracing group. */
305 /* Are we in the tracing group ? */
306 in_tgroup
= lttng_check_tracing_group();
309 if ((uid
== 0) || in_tgroup
) {
310 lttng_ctl_copy_string(sessiond_sock_path
,
311 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
319 ret
= try_connect_sessiond(sessiond_sock_path
);
323 /* Global session daemon not available... */
325 /* ...or not in tracing group (and not root), default */
328 * With GNU C < 2.1, snprintf returns -1 if the target buffer
330 * With GNU C >= 2.1, snprintf returns the required size
331 * (excluding closing null)
333 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
334 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
335 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
347 * Connect to the LTTng session daemon.
349 * On success, return 0. On error, return -1.
351 static int connect_sessiond(void)
355 /* Don't try to connect if already connected. */
360 ret
= set_session_daemon_path();
365 /* Connect to the sesssion daemon. */
366 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
371 sessiond_socket
= ret
;
381 * Clean disconnect from the session daemon.
383 * On success, return 0. On error, return -1.
385 static int disconnect_sessiond(void)
390 ret
= lttcomm_close_unix_sock(sessiond_socket
);
398 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
406 ret
= -LTTNG_ERR_INVALID
;
416 ret
= recv_data_sessiond(buf
, len
);
422 ret
= -LTTNG_ERR_INVALID
;
426 /* Move ownership of command header buffer to user. */
431 /* No command header. */
447 * Ask the session daemon a specific command and put the data into buf.
448 * Takes extra var. len. data as input to send to the session daemon.
450 * Return size of data (only payload, not header) or a negative error code.
453 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
454 const void *vardata
, size_t vardata_len
,
455 void **user_payload_buf
, void **user_cmd_header_buf
,
456 size_t *user_cmd_header_len
)
460 struct lttcomm_lttng_msg llm
;
462 ret
= connect_sessiond();
464 ret
= -LTTNG_ERR_NO_SESSIOND
;
468 /* Send command to session daemon */
469 ret
= send_session_msg(lsm
);
471 /* Ret value is a valid lttng error code. */
474 /* Send var len data */
475 ret
= send_session_varlen(vardata
, vardata_len
);
477 /* Ret value is a valid lttng error code. */
481 /* Get header from data transmission */
482 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
484 /* Ret value is a valid lttng error code. */
488 /* Check error code if OK */
489 if (llm
.ret_code
!= LTTNG_OK
) {
494 /* Get command header from data transmission */
495 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
496 user_cmd_header_buf
, user_cmd_header_len
);
501 /* Get payload from data transmission */
502 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
511 disconnect_sessiond();
516 * Create lttng handle and return pointer.
518 * The returned pointer will be NULL in case of malloc() error.
520 struct lttng_handle
*lttng_create_handle(const char *session_name
,
521 struct lttng_domain
*domain
)
523 struct lttng_handle
*handle
= NULL
;
525 handle
= zmalloc(sizeof(struct lttng_handle
));
526 if (handle
== NULL
) {
527 PERROR("malloc handle");
531 /* Copy session name */
532 lttng_ctl_copy_string(handle
->session_name
, session_name
,
533 sizeof(handle
->session_name
));
535 /* Copy lttng domain or leave initialized to 0. */
537 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
545 * Destroy handle by free(3) the pointer.
547 void lttng_destroy_handle(struct lttng_handle
*handle
)
553 * Register an outside consumer.
555 * Returns size of returned session payload data or a negative error code.
557 int lttng_register_consumer(struct lttng_handle
*handle
,
558 const char *socket_path
)
560 struct lttcomm_session_msg lsm
;
562 if (handle
== NULL
|| socket_path
== NULL
) {
563 return -LTTNG_ERR_INVALID
;
566 memset(&lsm
, 0, sizeof(lsm
));
567 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
568 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
569 sizeof(lsm
.session
.name
));
570 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
572 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
573 sizeof(lsm
.u
.reg
.path
));
575 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
579 * Start tracing for all traces of the session.
581 * Returns size of returned session payload data or a negative error code.
583 int lttng_start_tracing(const char *session_name
)
585 struct lttcomm_session_msg lsm
;
587 if (session_name
== NULL
) {
588 return -LTTNG_ERR_INVALID
;
591 memset(&lsm
, 0, sizeof(lsm
));
592 lsm
.cmd_type
= LTTNG_START_TRACE
;
594 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
595 sizeof(lsm
.session
.name
));
597 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
601 * Stop tracing for all traces of the session.
603 static int _lttng_stop_tracing(const char *session_name
, int wait
)
606 struct lttcomm_session_msg lsm
;
608 if (session_name
== NULL
) {
609 return -LTTNG_ERR_INVALID
;
612 memset(&lsm
, 0, sizeof(lsm
));
613 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
615 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
616 sizeof(lsm
.session
.name
));
618 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
619 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
627 /* Check for data availability */
629 data_ret
= lttng_data_pending(session_name
);
631 /* Return the data available call error. */
637 * Data sleep time before retrying (in usec). Don't sleep if the
638 * call returned value indicates availability.
641 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
643 } while (data_ret
!= 0);
651 * Stop tracing and wait for data availability.
653 int lttng_stop_tracing(const char *session_name
)
655 return _lttng_stop_tracing(session_name
, 1);
659 * Stop tracing but _don't_ wait for data availability.
661 int lttng_stop_tracing_no_wait(const char *session_name
)
663 return _lttng_stop_tracing(session_name
, 0);
667 * Add context to a channel.
669 * If the given channel is NULL, add the contexts to all channels.
670 * The event_name param is ignored.
672 * Returns the size of the returned payload data or a negative error code.
674 int lttng_add_context(struct lttng_handle
*handle
,
675 struct lttng_event_context
*ctx
, const char *event_name
,
676 const char *channel_name
)
681 struct lttcomm_session_msg lsm
;
683 /* Safety check. Both are mandatory. */
684 if (handle
== NULL
|| ctx
== NULL
) {
685 ret
= -LTTNG_ERR_INVALID
;
689 memset(&lsm
, 0, sizeof(lsm
));
690 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
692 /* If no channel name, send empty string. */
693 if (channel_name
== NULL
) {
694 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
695 sizeof(lsm
.u
.context
.channel_name
));
697 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
698 sizeof(lsm
.u
.context
.channel_name
));
701 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
702 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
703 sizeof(lsm
.session
.name
));
705 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
706 size_t provider_len
, ctx_len
;
707 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
708 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
710 if (!provider_name
|| !ctx_name
) {
711 ret
= -LTTNG_ERR_INVALID
;
715 provider_len
= strlen(provider_name
);
716 if (provider_len
== 0) {
717 ret
= -LTTNG_ERR_INVALID
;
720 lsm
.u
.context
.provider_name_len
= provider_len
;
722 ctx_len
= strlen(ctx_name
);
724 ret
= -LTTNG_ERR_INVALID
;
727 lsm
.u
.context
.context_name_len
= ctx_len
;
729 len
= provider_len
+ ctx_len
;
732 ret
= -LTTNG_ERR_NOMEM
;
736 memcpy(buf
, provider_name
, provider_len
);
737 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
739 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
741 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
743 * Don't leak application addresses to the sessiond.
744 * This is only necessary when ctx is for an app ctx otherwise
745 * the values inside the union (type & config) are overwritten.
747 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
748 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
751 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
758 * Enable event(s) for a channel.
760 * If no event name is specified, all events are enabled.
761 * If no channel name is specified, the default 'channel0' is used.
763 * Returns size of returned session payload data or a negative error code.
765 int lttng_enable_event(struct lttng_handle
*handle
,
766 struct lttng_event
*ev
, const char *channel_name
)
768 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
773 * Create or enable an event with a filter expression.
775 * Return negative error value on error.
776 * Return size of returned session payload data if OK.
778 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
779 struct lttng_event
*event
, const char *channel_name
,
780 const char *filter_expression
)
782 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
783 filter_expression
, 0, NULL
);
787 * Depending on the event, return a newly allocated agent filter expression or
788 * NULL if not applicable.
790 * An event with NO loglevel and the name is * will return NULL.
792 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
795 char *agent_filter
= NULL
;
799 /* Don't add filter for the '*' event. */
800 if (strcmp(ev
->name
, "*") != 0) {
802 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
805 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
813 /* Add loglevel filtering if any for the JUL domain. */
814 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
817 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
823 if (filter
|| agent_filter
) {
826 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
827 agent_filter
? agent_filter
: filter
, op
,
832 agent_filter
= new_filter
;
834 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
850 * Generate the filter bytecode from a given filter expression string. Put the
851 * newly allocated parser context in ctxp and populate the lsm object with the
854 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
856 static int generate_filter(char *filter_expression
,
857 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
860 struct filter_parser_ctx
*ctx
= NULL
;
863 assert(filter_expression
);
868 * Casting const to non-const, as the underlying function will use it in
871 fmem
= lttng_fmemopen((void *) filter_expression
,
872 strlen(filter_expression
), "r");
874 fprintf(stderr
, "Error opening memory as stream\n");
875 ret
= -LTTNG_ERR_FILTER_NOMEM
;
878 ctx
= filter_parser_ctx_alloc(fmem
);
880 fprintf(stderr
, "Error allocating parser\n");
881 ret
= -LTTNG_ERR_FILTER_NOMEM
;
882 goto filter_alloc_error
;
884 ret
= filter_parser_ctx_append_ast(ctx
);
886 fprintf(stderr
, "Parse error\n");
887 ret
= -LTTNG_ERR_FILTER_INVAL
;
890 ret
= filter_visitor_set_parent(ctx
);
892 fprintf(stderr
, "Set parent error\n");
893 ret
= -LTTNG_ERR_FILTER_INVAL
;
897 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
900 fprintf(stderr
, "XML print error\n");
901 ret
= -LTTNG_ERR_FILTER_INVAL
;
906 dbg_printf("Generating IR... ");
908 ret
= filter_visitor_ir_generate(ctx
);
910 fprintf(stderr
, "Generate IR error\n");
911 ret
= -LTTNG_ERR_FILTER_INVAL
;
914 dbg_printf("done\n");
916 dbg_printf("Validating IR... ");
918 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
920 ret
= -LTTNG_ERR_FILTER_INVAL
;
924 /* Normalize globbing patterns in the expression. */
925 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
927 ret
= -LTTNG_ERR_FILTER_INVAL
;
931 /* Validate strings used as literals in the expression. */
932 ret
= filter_visitor_ir_validate_string(ctx
);
934 ret
= -LTTNG_ERR_FILTER_INVAL
;
938 /* Validate globbing patterns in the expression. */
939 ret
= filter_visitor_ir_validate_globbing(ctx
);
941 ret
= -LTTNG_ERR_FILTER_INVAL
;
945 dbg_printf("done\n");
947 dbg_printf("Generating bytecode... ");
949 ret
= filter_visitor_bytecode_generate(ctx
);
951 fprintf(stderr
, "Generate bytecode error\n");
952 ret
= -LTTNG_ERR_FILTER_INVAL
;
955 dbg_printf("done\n");
956 dbg_printf("Size of bytecode generated: %u bytes.\n",
957 bytecode_get_len(&ctx
->bytecode
->b
));
959 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
960 + bytecode_get_len(&ctx
->bytecode
->b
);
961 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
963 /* No need to keep the memory stream. */
964 if (fclose(fmem
) != 0) {
973 filter_parser_ctx_free(ctx
);
975 if (fclose(fmem
) != 0) {
983 * Enable event(s) for a channel, possibly with exclusions and a filter.
984 * If no event name is specified, all events are enabled.
985 * If no channel name is specified, the default name is used.
986 * If filter expression is not NULL, the filter is set for the event.
987 * If exclusion count is not zero, the exclusions are set for the event.
988 * Returns size of returned session payload data or a negative error code.
990 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
991 struct lttng_event
*ev
, const char *channel_name
,
992 const char *original_filter_expression
,
993 int exclusion_count
, char **exclusion_list
)
995 struct lttcomm_session_msg lsm
;
998 unsigned int free_filter_expression
= 0;
999 struct filter_parser_ctx
*ctx
= NULL
;
1001 * Cast as non-const since we may replace the filter expression
1002 * by a dynamically allocated string. Otherwise, the original
1003 * string is not modified.
1005 char *filter_expression
= (char *) original_filter_expression
;
1007 if (handle
== NULL
|| ev
== NULL
) {
1008 ret
= -LTTNG_ERR_INVALID
;
1013 * Empty filter string will always be rejected by the parser
1014 * anyway, so treat this corner-case early to eliminate
1015 * lttng_fmemopen error for 0-byte allocation.
1017 if (filter_expression
&& filter_expression
[0] == '\0') {
1018 ret
= -LTTNG_ERR_INVALID
;
1022 memset(&lsm
, 0, sizeof(lsm
));
1024 /* If no channel name, send empty string. */
1025 if (channel_name
== NULL
) {
1026 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1027 sizeof(lsm
.u
.enable
.channel_name
));
1029 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1030 sizeof(lsm
.u
.enable
.channel_name
));
1033 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1034 if (ev
->name
[0] == '\0') {
1035 /* Enable all events */
1036 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1039 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1040 /* FIXME: copying non-packed struct to packed struct. */
1041 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1043 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1044 sizeof(lsm
.session
.name
));
1045 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1046 lsm
.u
.enable
.bytecode_len
= 0;
1049 * For the JUL domain, a filter is enforced except for the enable all
1050 * event. This is done to avoid having the event in all sessions thus
1051 * filtering by logger name.
1053 if (exclusion_count
== 0 && filter_expression
== NULL
&&
1054 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1055 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1056 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1061 * We have either a filter or some exclusions, so we need to set up
1062 * a variable-length memory block from where to send the data.
1065 /* Parse filter expression. */
1066 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1067 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1068 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1069 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1070 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1071 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1074 /* Setup JUL filter if needed. */
1075 agent_filter
= set_agent_filter(filter_expression
, ev
);
1076 if (!agent_filter
) {
1077 if (!filter_expression
) {
1079 * No JUL and no filter, just skip
1086 * With an agent filter, the original filter has
1087 * been added to it thus replace the filter
1090 filter_expression
= agent_filter
;
1091 free_filter_expression
= 1;
1095 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1101 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
1102 + lsm
.u
.enable
.expression_len
1103 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1105 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1109 /* Put exclusion names first in the data. */
1110 while (exclusion_count
--) {
1111 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1112 *(exclusion_list
+ exclusion_count
),
1113 LTTNG_SYMBOL_NAME_LEN
- 1);
1115 /* Add filter expression next. */
1116 if (lsm
.u
.enable
.expression_len
!= 0) {
1118 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1120 lsm
.u
.enable
.expression_len
);
1122 /* Add filter bytecode next. */
1123 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1125 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1126 + lsm
.u
.enable
.expression_len
,
1128 lsm
.u
.enable
.bytecode_len
);
1131 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1132 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1133 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
,
1138 if (filter_expression
&& ctx
) {
1139 filter_bytecode_free(ctx
);
1140 filter_ir_free(ctx
);
1141 filter_parser_ctx_free(ctx
);
1144 if (free_filter_expression
) {
1146 * The filter expression has been replaced and must be freed as
1147 * it is not the original filter expression received as a
1150 free(filter_expression
);
1154 * Return directly to the caller and don't ask the sessiond since
1155 * something went wrong in the parsing of data above.
1160 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1164 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1165 struct lttng_event
*ev
, const char *channel_name
,
1166 const char *original_filter_expression
)
1168 struct lttcomm_session_msg lsm
;
1171 unsigned int free_filter_expression
= 0;
1172 struct filter_parser_ctx
*ctx
= NULL
;
1174 * Cast as non-const since we may replace the filter expression
1175 * by a dynamically allocated string. Otherwise, the original
1176 * string is not modified.
1178 char *filter_expression
= (char *) original_filter_expression
;
1180 if (handle
== NULL
|| ev
== NULL
) {
1181 ret
= -LTTNG_ERR_INVALID
;
1186 * Empty filter string will always be rejected by the parser
1187 * anyway, so treat this corner-case early to eliminate
1188 * lttng_fmemopen error for 0-byte allocation.
1190 if (filter_expression
&& filter_expression
[0] == '\0') {
1191 ret
= -LTTNG_ERR_INVALID
;
1195 memset(&lsm
, 0, sizeof(lsm
));
1197 /* If no channel name, send empty string. */
1198 if (channel_name
== NULL
) {
1199 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1200 sizeof(lsm
.u
.disable
.channel_name
));
1202 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1203 sizeof(lsm
.u
.disable
.channel_name
));
1206 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1208 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1209 /* FIXME: copying non-packed struct to packed struct. */
1210 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1212 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1213 sizeof(lsm
.session
.name
));
1214 lsm
.u
.disable
.bytecode_len
= 0;
1217 * For the JUL domain, a filter is enforced except for the
1218 * disable all event. This is done to avoid having the event in
1219 * all sessions thus filtering by logger name.
1221 if (filter_expression
== NULL
&&
1222 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1223 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1224 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1229 * We have a filter, so we need to set up a variable-length
1230 * memory block from where to send the data.
1233 /* Parse filter expression */
1234 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1235 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1236 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1237 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1238 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1239 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1242 /* Setup JUL filter if needed. */
1243 agent_filter
= set_agent_filter(filter_expression
, ev
);
1244 if (!agent_filter
) {
1245 if (!filter_expression
) {
1247 * No JUL and no filter, just skip
1254 * With a JUL filter, the original filter has
1255 * been added to it thus replace the filter
1258 filter_expression
= agent_filter
;
1259 free_filter_expression
= 1;
1263 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1269 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1270 + lsm
.u
.disable
.expression_len
);
1272 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1276 /* Add filter expression. */
1277 if (lsm
.u
.disable
.expression_len
!= 0) {
1280 lsm
.u
.disable
.expression_len
);
1282 /* Add filter bytecode next. */
1283 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1285 + lsm
.u
.disable
.expression_len
,
1287 lsm
.u
.disable
.bytecode_len
);
1290 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1291 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1295 if (filter_expression
&& ctx
) {
1296 filter_bytecode_free(ctx
);
1297 filter_ir_free(ctx
);
1298 filter_parser_ctx_free(ctx
);
1301 if (free_filter_expression
) {
1303 * The filter expression has been replaced and must be freed as
1304 * it is not the original filter expression received as a
1307 free(filter_expression
);
1311 * Return directly to the caller and don't ask the sessiond since
1312 * something went wrong in the parsing of data above.
1317 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1322 * Disable event(s) of a channel and domain.
1323 * If no event name is specified, all events are disabled.
1324 * If no channel name is specified, the default 'channel0' is used.
1325 * Returns size of returned session payload data or a negative error code.
1327 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1328 const char *channel_name
)
1330 struct lttng_event ev
;
1332 memset(&ev
, 0, sizeof(ev
));
1334 ev
.type
= LTTNG_EVENT_ALL
;
1335 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1336 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1339 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1341 struct lttng_channel
*channel
= NULL
;
1342 struct lttng_channel_extended
*extended
= NULL
;
1348 /* Validate domain. */
1349 switch (domain
->type
) {
1350 case LTTNG_DOMAIN_UST
:
1351 switch (domain
->buf_type
) {
1352 case LTTNG_BUFFER_PER_UID
:
1353 case LTTNG_BUFFER_PER_PID
:
1359 case LTTNG_DOMAIN_KERNEL
:
1360 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1368 channel
= zmalloc(sizeof(*channel
));
1373 extended
= zmalloc(sizeof(*extended
));
1378 channel
->attr
.extended
.ptr
= extended
;
1380 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1388 void lttng_channel_destroy(struct lttng_channel
*channel
)
1394 if (channel
->attr
.extended
.ptr
) {
1395 free(channel
->attr
.extended
.ptr
);
1401 * Enable channel per domain
1402 * Returns size of returned session payload data or a negative error code.
1404 int lttng_enable_channel(struct lttng_handle
*handle
,
1405 struct lttng_channel
*in_chan
)
1407 struct lttcomm_session_msg lsm
;
1409 /* NULL arguments are forbidden. No default values. */
1410 if (handle
== NULL
|| in_chan
== NULL
) {
1411 return -LTTNG_ERR_INVALID
;
1414 memset(&lsm
, 0, sizeof(lsm
));
1415 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1416 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1418 if (!in_chan
->attr
.extended
.ptr
) {
1419 struct lttng_channel
*channel
;
1420 struct lttng_channel_extended
*extended
;
1422 channel
= lttng_channel_create(&handle
->domain
);
1424 return -LTTNG_ERR_NOMEM
;
1428 * Create a new channel in order to use default extended
1431 extended
= (struct lttng_channel_extended
*)
1432 channel
->attr
.extended
.ptr
;
1433 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1434 lttng_channel_destroy(channel
);
1436 struct lttng_channel_extended
*extended
;
1438 extended
= (struct lttng_channel_extended
*)
1439 in_chan
->attr
.extended
.ptr
;
1440 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1443 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1444 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1446 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1447 sizeof(lsm
.session
.name
));
1449 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1453 * All tracing will be stopped for registered events of the channel.
1454 * Returns size of returned session payload data or a negative error code.
1456 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1458 struct lttcomm_session_msg lsm
;
1460 /* Safety check. Both are mandatory. */
1461 if (handle
== NULL
|| name
== NULL
) {
1462 return -LTTNG_ERR_INVALID
;
1465 memset(&lsm
, 0, sizeof(lsm
));
1467 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1469 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1470 sizeof(lsm
.u
.disable
.channel_name
));
1472 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1474 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1475 sizeof(lsm
.session
.name
));
1477 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1481 * Add PID to session tracker.
1482 * Return 0 on success else a negative LTTng error code.
1484 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1486 struct lttcomm_session_msg lsm
;
1488 /* NULL arguments are forbidden. No default values. */
1489 if (handle
== NULL
) {
1490 return -LTTNG_ERR_INVALID
;
1493 memset(&lsm
, 0, sizeof(lsm
));
1495 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1496 lsm
.u
.pid_tracker
.pid
= pid
;
1498 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1500 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1501 sizeof(lsm
.session
.name
));
1503 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1507 * Remove PID from session tracker.
1508 * Return 0 on success else a negative LTTng error code.
1510 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1512 struct lttcomm_session_msg lsm
;
1514 /* NULL arguments are forbidden. No default values. */
1515 if (handle
== NULL
) {
1516 return -LTTNG_ERR_INVALID
;
1519 memset(&lsm
, 0, sizeof(lsm
));
1521 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1522 lsm
.u
.pid_tracker
.pid
= pid
;
1524 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1526 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1527 sizeof(lsm
.session
.name
));
1529 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1533 * Lists all available tracepoints of domain.
1534 * Sets the contents of the events array.
1535 * Returns the number of lttng_event entries in events;
1536 * on error, returns a negative value.
1538 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1539 struct lttng_event
**events
)
1542 struct lttcomm_session_msg lsm
;
1544 if (handle
== NULL
) {
1545 return -LTTNG_ERR_INVALID
;
1548 memset(&lsm
, 0, sizeof(lsm
));
1549 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1550 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1552 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1557 return ret
/ sizeof(struct lttng_event
);
1561 * Lists all available tracepoint fields of domain.
1562 * Sets the contents of the event field array.
1563 * Returns the number of lttng_event_field entries in events;
1564 * on error, returns a negative value.
1566 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1567 struct lttng_event_field
**fields
)
1570 struct lttcomm_session_msg lsm
;
1572 if (handle
== NULL
) {
1573 return -LTTNG_ERR_INVALID
;
1576 memset(&lsm
, 0, sizeof(lsm
));
1577 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1578 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1580 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1585 return ret
/ sizeof(struct lttng_event_field
);
1589 * Lists all available kernel system calls. Allocates and sets the contents of
1592 * Returns the number of lttng_event entries in events; on error, returns a
1595 int lttng_list_syscalls(struct lttng_event
**events
)
1598 struct lttcomm_session_msg lsm
;
1601 return -LTTNG_ERR_INVALID
;
1604 memset(&lsm
, 0, sizeof(lsm
));
1605 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1606 /* Force kernel domain for system calls. */
1607 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1609 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1614 return ret
/ sizeof(struct lttng_event
);
1618 * Returns a human readable string describing
1619 * the error code (a negative value).
1621 const char *lttng_strerror(int code
)
1623 return error_get_str(code
);
1627 * Create a brand new session using name and url for destination.
1629 * Returns LTTNG_OK on success or a negative error code.
1631 int lttng_create_session(const char *name
, const char *url
)
1635 struct lttcomm_session_msg lsm
;
1636 struct lttng_uri
*uris
= NULL
;
1639 return -LTTNG_ERR_INVALID
;
1642 memset(&lsm
, 0, sizeof(lsm
));
1644 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1645 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1647 /* There should never be a data URL */
1648 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1650 return -LTTNG_ERR_INVALID
;
1653 lsm
.u
.uri
.size
= size
;
1655 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
1656 sizeof(struct lttng_uri
) * size
, NULL
);
1663 * Destroy session using name.
1664 * Returns size of returned session payload data or a negative error code.
1667 int _lttng_destroy_session(const char *session_name
)
1669 struct lttcomm_session_msg lsm
;
1671 if (session_name
== NULL
) {
1672 return -LTTNG_ERR_INVALID
;
1675 memset(&lsm
, 0, sizeof(lsm
));
1676 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1678 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1679 sizeof(lsm
.session
.name
));
1681 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1685 * Stop the session and wait for the data before destroying it
1687 int lttng_destroy_session(const char *session_name
)
1692 * Stop the tracing and wait for the data.
1694 ret
= _lttng_stop_tracing(session_name
, 1);
1695 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1699 ret
= _lttng_destroy_session(session_name
);
1705 * Destroy the session without waiting for the data.
1707 int lttng_destroy_session_no_wait(const char *session_name
)
1712 * Stop the tracing without waiting for the data.
1713 * The session might already have been stopped, so just
1716 ret
= _lttng_stop_tracing(session_name
, 0);
1717 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1721 ret
= _lttng_destroy_session(session_name
);
1727 * Ask the session daemon for all available sessions.
1728 * Sets the contents of the sessions array.
1729 * Returns the number of lttng_session entries in sessions;
1730 * on error, returns a negative value.
1732 int lttng_list_sessions(struct lttng_session
**sessions
)
1735 struct lttcomm_session_msg lsm
;
1737 memset(&lsm
, 0, sizeof(lsm
));
1738 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1739 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1744 return ret
/ sizeof(struct lttng_session
);
1747 int lttng_set_session_shm_path(const char *session_name
,
1748 const char *shm_path
)
1750 struct lttcomm_session_msg lsm
;
1752 if (session_name
== NULL
) {
1753 return -LTTNG_ERR_INVALID
;
1756 memset(&lsm
, 0, sizeof(lsm
));
1757 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1759 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1760 sizeof(lsm
.session
.name
));
1761 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1762 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1764 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1768 * Ask the session daemon for all available domains of a session.
1769 * Sets the contents of the domains array.
1770 * Returns the number of lttng_domain entries in domains;
1771 * on error, returns a negative value.
1773 int lttng_list_domains(const char *session_name
,
1774 struct lttng_domain
**domains
)
1777 struct lttcomm_session_msg lsm
;
1779 if (session_name
== NULL
) {
1780 return -LTTNG_ERR_INVALID
;
1783 memset(&lsm
, 0, sizeof(lsm
));
1784 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1786 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1787 sizeof(lsm
.session
.name
));
1789 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1794 return ret
/ sizeof(struct lttng_domain
);
1798 * Ask the session daemon for all available channels of a session.
1799 * Sets the contents of the channels array.
1800 * Returns the number of lttng_channel entries in channels;
1801 * on error, returns a negative value.
1803 int lttng_list_channels(struct lttng_handle
*handle
,
1804 struct lttng_channel
**channels
)
1807 size_t channel_count
, i
;
1808 const size_t channel_size
= sizeof(struct lttng_channel
) +
1809 sizeof(struct lttng_channel_extended
);
1810 struct lttcomm_session_msg lsm
;
1813 if (handle
== NULL
) {
1814 ret
= -LTTNG_ERR_INVALID
;
1818 memset(&lsm
, 0, sizeof(lsm
));
1819 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1820 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1821 sizeof(lsm
.session
.name
));
1823 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1825 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1830 if (ret
% channel_size
) {
1831 ret
= -LTTNG_ERR_UNK
;
1836 channel_count
= (size_t) ret
/ channel_size
;
1838 /* Set extended info pointers */
1839 extended_at
= ((void *) *channels
) +
1840 channel_count
* sizeof(struct lttng_channel
);
1841 for (i
= 0; i
< channel_count
; i
++) {
1842 struct lttng_channel
*chan
= &(*channels
)[i
];
1844 chan
->attr
.extended
.ptr
= extended_at
;
1845 extended_at
+= sizeof(struct lttng_channel_extended
);
1848 ret
= (int) channel_count
;
1854 * Ask the session daemon for all available events of a session channel.
1855 * Sets the contents of the events array.
1856 * Returns the number of lttng_event entries in events;
1857 * on error, returns a negative value.
1859 int lttng_list_events(struct lttng_handle
*handle
,
1860 const char *channel_name
, struct lttng_event
**events
)
1863 struct lttcomm_session_msg lsm
;
1864 struct lttcomm_event_command_header
*cmd_header
= NULL
;
1865 size_t cmd_header_len
;
1866 uint32_t nb_events
, i
;
1869 /* Safety check. An handle and channel name are mandatory */
1870 if (handle
== NULL
|| channel_name
== NULL
) {
1871 return -LTTNG_ERR_INVALID
;
1874 memset(&lsm
, 0, sizeof(lsm
));
1875 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1876 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1877 sizeof(lsm
.session
.name
));
1878 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1879 sizeof(lsm
.u
.list
.channel_name
));
1880 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1882 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, NULL
, 0, (void **) events
,
1883 (void **) &cmd_header
, &cmd_header_len
);
1888 /* Set number of events and free command header */
1889 nb_events
= cmd_header
->nb_events
;
1890 if (nb_events
> INT_MAX
) {
1894 ret
= (int) nb_events
;
1898 /* Set extended info pointers */
1899 extended_at
= ((void*) (*events
)) +
1900 nb_events
* sizeof(struct lttng_event
);
1902 for (i
= 0; i
< nb_events
; i
++) {
1903 struct lttcomm_event_extended_header
*ext_header
;
1904 struct lttng_event
*event
= &(*events
)[i
];
1906 event
->extended
.ptr
= extended_at
;
1908 (struct lttcomm_event_extended_header
*) extended_at
;
1909 extended_at
+= sizeof(*ext_header
);
1910 extended_at
+= ext_header
->filter_len
;
1912 ext_header
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
1922 int lttng_event_get_filter_expression(struct lttng_event
*event
,
1923 const char **filter_expression
)
1926 struct lttcomm_event_extended_header
*ext_header
;
1928 if (!event
|| !filter_expression
) {
1929 ret
= -LTTNG_ERR_INVALID
;
1933 ext_header
= event
->extended
.ptr
;
1937 * This can happen since the lttng_event structure is
1938 * used for other tasks where this pointer is never set.
1940 *filter_expression
= NULL
;
1944 if (ext_header
->filter_len
) {
1945 *filter_expression
= ((const char *) (ext_header
)) +
1946 sizeof(*ext_header
);
1948 *filter_expression
= NULL
;
1955 int lttng_event_get_exclusion_name_count(struct lttng_event
*event
)
1958 struct lttcomm_event_extended_header
*ext_header
;
1961 ret
= -LTTNG_ERR_INVALID
;
1965 ext_header
= event
->extended
.ptr
;
1968 * This can happen since the lttng_event structure is
1969 * used for other tasks where this pointer is never set.
1975 if (ext_header
->nb_exclusions
> INT_MAX
) {
1976 ret
= -LTTNG_ERR_OVERFLOW
;
1979 ret
= (int) ext_header
->nb_exclusions
;
1984 int lttng_event_get_exclusion_name(struct lttng_event
*event
,
1985 size_t index
, const char **exclusion_name
)
1988 struct lttcomm_event_extended_header
*ext_header
;
1991 if (!event
|| !exclusion_name
) {
1992 ret
= -LTTNG_ERR_INVALID
;
1996 ext_header
= event
->extended
.ptr
;
1998 ret
= -LTTNG_ERR_INVALID
;
2002 if (index
>= ext_header
->nb_exclusions
) {
2003 ret
= -LTTNG_ERR_INVALID
;
2007 at
= (void *) ext_header
+ sizeof(*ext_header
);
2008 at
+= ext_header
->filter_len
;
2009 at
+= index
* LTTNG_SYMBOL_NAME_LEN
;
2010 *exclusion_name
= at
;
2017 * Sets the tracing_group variable with name.
2018 * This function allocates memory pointed to by tracing_group.
2019 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2021 int lttng_set_tracing_group(const char *name
)
2024 return -LTTNG_ERR_INVALID
;
2027 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2028 return -LTTNG_ERR_FATAL
;
2034 int lttng_calibrate(struct lttng_handle
*handle
,
2035 struct lttng_calibrate
*calibrate
)
2038 * This command was removed in LTTng 2.9.
2040 return -LTTNG_ERR_UND
;
2044 * Set default channel attributes.
2045 * If either or both of the arguments are null, attr content is zeroe'd.
2047 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2048 struct lttng_channel_attr
*attr
)
2050 struct lttng_channel_extended
*extended
;
2053 if (attr
== NULL
|| domain
== NULL
) {
2057 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2058 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2060 /* Same for all domains. */
2061 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2062 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2063 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2065 switch (domain
->type
) {
2066 case LTTNG_DOMAIN_KERNEL
:
2067 attr
->switch_timer_interval
=
2068 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2069 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2070 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2071 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2072 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2074 extended
->monitor_timer_interval
=
2075 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2076 extended
->blocking_timeout
=
2077 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2080 case LTTNG_DOMAIN_UST
:
2081 switch (domain
->buf_type
) {
2082 case LTTNG_BUFFER_PER_UID
:
2083 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2084 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2085 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2086 attr
->switch_timer_interval
=
2087 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2088 attr
->read_timer_interval
=
2089 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2091 extended
->monitor_timer_interval
=
2092 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2093 extended
->blocking_timeout
=
2094 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2097 case LTTNG_BUFFER_PER_PID
:
2099 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2100 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2101 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2102 attr
->switch_timer_interval
=
2103 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2104 attr
->read_timer_interval
=
2105 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2107 extended
->monitor_timer_interval
=
2108 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2109 extended
->blocking_timeout
=
2110 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2115 /* Default behavior: leave set to 0. */
2119 attr
->extended
.ptr
= extended
;
2122 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2123 uint64_t *discarded_events
)
2126 struct lttng_channel_extended
*chan_ext
;
2128 if (!channel
|| !discarded_events
) {
2129 ret
= -LTTNG_ERR_INVALID
;
2133 chan_ext
= channel
->attr
.extended
.ptr
;
2136 * This can happen since the lttng_channel structure is
2137 * used for other tasks where this pointer is never set.
2139 *discarded_events
= 0;
2143 *discarded_events
= chan_ext
->discarded_events
;
2148 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2149 uint64_t *lost_packets
)
2152 struct lttng_channel_extended
*chan_ext
;
2154 if (!channel
|| !lost_packets
) {
2155 ret
= -LTTNG_ERR_INVALID
;
2159 chan_ext
= channel
->attr
.extended
.ptr
;
2162 * This can happen since the lttng_channel structure is
2163 * used for other tasks where this pointer is never set.
2169 *lost_packets
= chan_ext
->lost_packets
;
2174 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2175 uint64_t *monitor_timer_interval
)
2179 if (!chan
|| !monitor_timer_interval
) {
2180 ret
= -LTTNG_ERR_INVALID
;
2184 if (!chan
->attr
.extended
.ptr
) {
2185 ret
= -LTTNG_ERR_INVALID
;
2189 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2190 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2195 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2196 uint64_t monitor_timer_interval
)
2200 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2201 ret
= -LTTNG_ERR_INVALID
;
2205 ((struct lttng_channel_extended
*)
2206 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2207 monitor_timer_interval
;
2212 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2213 int64_t *blocking_timeout
)
2217 if (!chan
|| !blocking_timeout
) {
2218 ret
= -LTTNG_ERR_INVALID
;
2222 if (!chan
->attr
.extended
.ptr
) {
2223 ret
= -LTTNG_ERR_INVALID
;
2227 *blocking_timeout
= ((struct lttng_channel_extended
*)
2228 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2233 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2234 int64_t blocking_timeout
)
2237 int64_t msec_timeout
;
2239 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2240 ret
= -LTTNG_ERR_INVALID
;
2244 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2245 ret
= -LTTNG_ERR_INVALID
;
2250 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2251 * us to accept a narrower range of values (msecs expressed as a signed
2254 msec_timeout
= blocking_timeout
/ 1000;
2255 if (msec_timeout
!= (int32_t) msec_timeout
) {
2256 ret
= -LTTNG_ERR_INVALID
;
2260 ((struct lttng_channel_extended
*)
2261 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2268 * Check if session daemon is alive.
2270 * Return 1 if alive or 0 if not.
2271 * On error returns a negative value.
2273 int lttng_session_daemon_alive(void)
2277 ret
= set_session_daemon_path();
2283 if (*sessiond_sock_path
== '\0') {
2285 * No socket path set. Weird error which means the constructor
2291 ret
= try_connect_sessiond(sessiond_sock_path
);
2302 * Set URL for a consumer for a session and domain.
2304 * Return 0 on success, else a negative value.
2306 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2307 const char *control_url
, const char *data_url
)
2311 struct lttcomm_session_msg lsm
;
2312 struct lttng_uri
*uris
= NULL
;
2314 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2315 return -LTTNG_ERR_INVALID
;
2318 memset(&lsm
, 0, sizeof(lsm
));
2320 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2322 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2323 sizeof(lsm
.session
.name
));
2324 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2326 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2328 return -LTTNG_ERR_INVALID
;
2331 lsm
.u
.uri
.size
= size
;
2333 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2334 sizeof(struct lttng_uri
) * size
, NULL
);
2343 int lttng_enable_consumer(struct lttng_handle
*handle
)
2351 int lttng_disable_consumer(struct lttng_handle
*handle
)
2357 * This is an extension of create session that is ONLY and SHOULD only be used
2358 * by the lttng command line program. It exists to avoid using URI parsing in
2361 * We need the date and time for the trace path subdirectory for the case where
2362 * the user does NOT define one using either -o or -U. Using the normal
2363 * lttng_create_session API call, we have no clue on the session daemon side if
2364 * the URL was generated automatically by the client or define by the user.
2366 * So this function "wrapper" is hidden from the public API, takes the datetime
2367 * string and appends it if necessary to the URI subdirectory before sending it
2368 * to the session daemon.
2370 * With this extra function, the lttng_create_session call behavior is not
2371 * changed and the timestamp is appended to the URI on the session daemon side
2374 int _lttng_create_session_ext(const char *name
, const char *url
,
2375 const char *datetime
)
2379 struct lttcomm_session_msg lsm
;
2380 struct lttng_uri
*uris
= NULL
;
2382 if (name
== NULL
|| datetime
== NULL
) {
2383 return -LTTNG_ERR_INVALID
;
2386 memset(&lsm
, 0, sizeof(lsm
));
2388 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
2389 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2391 /* There should never be a data URL. */
2392 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2394 ret
= -LTTNG_ERR_INVALID
;
2398 lsm
.u
.uri
.size
= size
;
2400 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2401 /* Don't append datetime if the name was automatically created. */
2402 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
2403 strlen(DEFAULT_SESSION_NAME
) + 1)) {
2404 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
2407 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2410 PERROR("snprintf uri subdir");
2411 ret
= -LTTNG_ERR_FATAL
;
2416 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2417 sizeof(struct lttng_uri
) * size
, NULL
);
2425 * For a given session name, this call checks if the data is ready to be read
2426 * or is still being extracted by the consumer(s) hence not ready to be used by
2429 int lttng_data_pending(const char *session_name
)
2432 struct lttcomm_session_msg lsm
;
2433 uint8_t *pending
= NULL
;
2435 if (session_name
== NULL
) {
2436 return -LTTNG_ERR_INVALID
;
2439 memset(&lsm
, 0, sizeof(lsm
));
2440 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2442 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2443 sizeof(lsm
.session
.name
));
2445 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2448 } else if (ret
!= 1) {
2449 /* Unexpected payload size */
2450 ret
= -LTTNG_ERR_INVALID
;
2454 ret
= (int) *pending
;
2461 * Create a session exclusively used for snapshot.
2463 * Returns LTTNG_OK on success or a negative error code.
2465 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2469 struct lttcomm_session_msg lsm
;
2470 struct lttng_uri
*uris
= NULL
;
2473 return -LTTNG_ERR_INVALID
;
2476 memset(&lsm
, 0, sizeof(lsm
));
2478 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
2479 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2481 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
2483 return -LTTNG_ERR_INVALID
;
2486 lsm
.u
.uri
.size
= size
;
2489 * If the user does not specify a custom subdir, use the session name.
2491 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2492 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2494 PERROR("snprintf uri subdir");
2495 ret
= -LTTNG_ERR_FATAL
;
2500 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2501 sizeof(struct lttng_uri
) * size
, NULL
);
2509 * Create a session exclusively used for live.
2511 * Returns LTTNG_OK on success or a negative error code.
2513 int lttng_create_session_live(const char *name
, const char *url
,
2514 unsigned int timer_interval
)
2518 struct lttcomm_session_msg lsm
;
2519 struct lttng_uri
*uris
= NULL
;
2521 if (name
== NULL
|| timer_interval
== 0) {
2522 return -LTTNG_ERR_INVALID
;
2525 memset(&lsm
, 0, sizeof(lsm
));
2527 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
2528 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2531 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2533 ret
= -LTTNG_ERR_INVALID
;
2537 /* file:// is not accepted for live session. */
2538 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2539 ret
= -LTTNG_ERR_INVALID
;
2546 lsm
.u
.session_live
.nb_uri
= size
;
2547 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2549 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2550 sizeof(struct lttng_uri
) * size
, NULL
);
2558 * List PIDs in the tracker.
2560 * enabled is set to whether the PID tracker is enabled.
2561 * pids is set to an allocated array of PIDs currently tracked. On
2562 * success, pids must be freed by the caller.
2563 * nr_pids is set to the number of entries contained by the pids array.
2565 * Returns 0 on success, else a negative LTTng error code.
2567 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2568 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2572 struct lttcomm_session_msg lsm
;
2576 if (handle
== NULL
) {
2577 return -LTTNG_ERR_INVALID
;
2580 memset(&lsm
, 0, sizeof(lsm
));
2581 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2582 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2583 sizeof(lsm
.session
.name
));
2584 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2586 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2590 nr_pids
= ret
/ sizeof(int32_t);
2591 if (nr_pids
== 1 && pids
[0] == -1) {
2597 *_enabled
= enabled
;
2599 *_nr_pids
= nr_pids
;
2604 * Regenerate the metadata for a session.
2605 * Return 0 on success, a negative error code on error.
2607 int lttng_regenerate_metadata(const char *session_name
)
2610 struct lttcomm_session_msg lsm
;
2612 if (!session_name
) {
2613 ret
= -LTTNG_ERR_INVALID
;
2617 memset(&lsm
, 0, sizeof(lsm
));
2618 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2620 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2621 sizeof(lsm
.session
.name
));
2623 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2634 * Deprecated, replaced by lttng_regenerate_metadata.
2636 int lttng_metadata_regenerate(const char *session_name
)
2638 return lttng_regenerate_metadata(session_name
);
2642 * Regenerate the statedump of a session.
2643 * Return 0 on success, a negative error code on error.
2645 int lttng_regenerate_statedump(const char *session_name
)
2648 struct lttcomm_session_msg lsm
;
2650 if (!session_name
) {
2651 ret
= -LTTNG_ERR_INVALID
;
2655 memset(&lsm
, 0, sizeof(lsm
));
2656 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2658 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2659 sizeof(lsm
.session
.name
));
2661 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2671 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2674 struct lttcomm_session_msg lsm
;
2675 char *trigger_buf
= NULL
;
2676 ssize_t trigger_size
;
2679 ret
= -LTTNG_ERR_INVALID
;
2683 if (!lttng_trigger_validate(trigger
)) {
2684 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2688 trigger_size
= lttng_trigger_serialize(trigger
, NULL
);
2689 if (trigger_size
< 0) {
2690 ret
= -LTTNG_ERR_UNK
;
2694 trigger_buf
= zmalloc(trigger_size
);
2696 ret
= -LTTNG_ERR_NOMEM
;
2700 memset(&lsm
, 0, sizeof(lsm
));
2701 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
2702 if (lttng_trigger_serialize(trigger
, trigger_buf
) < 0) {
2703 ret
= -LTTNG_ERR_UNK
;
2707 lsm
.u
.trigger
.length
= (uint32_t) trigger_size
;
2708 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, trigger_buf
,
2709 trigger_size
, NULL
);
2715 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
2718 struct lttcomm_session_msg lsm
;
2719 char *trigger_buf
= NULL
;
2720 ssize_t trigger_size
;
2723 ret
= -LTTNG_ERR_INVALID
;
2727 if (!lttng_trigger_validate(trigger
)) {
2728 ret
= -LTTNG_ERR_INVALID
;
2732 trigger_size
= lttng_trigger_serialize(trigger
, NULL
);
2733 if (trigger_size
< 0) {
2734 ret
= -LTTNG_ERR_UNK
;
2738 trigger_buf
= zmalloc(trigger_size
);
2740 ret
= -LTTNG_ERR_NOMEM
;
2744 memset(&lsm
, 0, sizeof(lsm
));
2745 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
2746 if (lttng_trigger_serialize(trigger
, trigger_buf
) < 0) {
2747 ret
= -LTTNG_ERR_UNK
;
2751 lsm
.u
.trigger
.length
= (uint32_t) trigger_size
;
2752 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, trigger_buf
,
2753 trigger_size
, NULL
);
2762 static void __attribute__((constructor
)) init(void)
2764 /* Set default session group */
2765 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2771 static void __attribute__((destructor
)) lttng_ctl_exit(void)
2773 free(tracing_group
);