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 gid_t
*grp_list
, tracing_gid
;
212 int grp_list_size
, grp_id
, i
;
214 const char *grp_name
= tracing_group
;
216 /* Get GID of group 'tracing' */
217 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
218 /* If grp_tracing is NULL, the group does not exist. */
222 /* Get number of supplementary group IDs */
223 grp_list_size
= getgroups(0, NULL
);
224 if (grp_list_size
< 0) {
229 /* Alloc group list of the right size */
230 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
235 grp_id
= getgroups(grp_list_size
, grp_list
);
241 for (i
= 0; i
< grp_list_size
; i
++) {
242 if (grp_list
[i
] == tracing_gid
) {
256 * Try connect to session daemon with sock_path.
258 * Return 0 on success, else -1
260 static int try_connect_sessiond(const char *sock_path
)
264 /* If socket exist, we check if the daemon listens for connect. */
265 ret
= access(sock_path
, F_OK
);
271 ret
= lttcomm_connect_unix_sock(sock_path
);
277 ret
= lttcomm_close_unix_sock(ret
);
279 PERROR("lttcomm_close_unix_sock");
289 * Set sessiond socket path by putting it in the global sessiond_sock_path
292 * Returns 0 on success, negative value on failure (the sessiond socket path
293 * is somehow too long or ENOMEM).
295 static int set_session_daemon_path(void)
297 int in_tgroup
= 0; /* In tracing group. */
303 /* Are we in the tracing group ? */
304 in_tgroup
= lttng_check_tracing_group();
307 if ((uid
== 0) || in_tgroup
) {
308 lttng_ctl_copy_string(sessiond_sock_path
,
309 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
317 ret
= try_connect_sessiond(sessiond_sock_path
);
321 /* Global session daemon not available... */
323 /* ...or not in tracing group (and not root), default */
326 * With GNU C < 2.1, snprintf returns -1 if the target buffer
328 * With GNU C >= 2.1, snprintf returns the required size
329 * (excluding closing null)
331 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
332 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
333 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
345 * Connect to the LTTng session daemon.
347 * On success, return 0. On error, return -1.
349 static int connect_sessiond(void)
353 /* Don't try to connect if already connected. */
358 ret
= set_session_daemon_path();
363 /* Connect to the sesssion daemon. */
364 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
369 sessiond_socket
= ret
;
379 * Clean disconnect from the session daemon.
381 * On success, return 0. On error, return -1.
383 static int disconnect_sessiond(void)
388 ret
= lttcomm_close_unix_sock(sessiond_socket
);
396 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
404 ret
= -LTTNG_ERR_INVALID
;
414 ret
= recv_data_sessiond(buf
, len
);
420 ret
= -LTTNG_ERR_INVALID
;
424 /* Move ownership of command header buffer to user. */
429 /* No command header. */
445 * Ask the session daemon a specific command and put the data into buf.
446 * Takes extra var. len. data as input to send to the session daemon.
448 * Return size of data (only payload, not header) or a negative error code.
451 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
452 const void *vardata
, size_t vardata_len
,
453 void **user_payload_buf
, void **user_cmd_header_buf
,
454 size_t *user_cmd_header_len
)
458 struct lttcomm_lttng_msg llm
;
460 ret
= connect_sessiond();
462 ret
= -LTTNG_ERR_NO_SESSIOND
;
466 /* Send command to session daemon */
467 ret
= send_session_msg(lsm
);
469 /* Ret value is a valid lttng error code. */
472 /* Send var len data */
473 ret
= send_session_varlen(vardata
, vardata_len
);
475 /* Ret value is a valid lttng error code. */
479 /* Get header from data transmission */
480 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
482 /* Ret value is a valid lttng error code. */
486 /* Check error code if OK */
487 if (llm
.ret_code
!= LTTNG_OK
) {
492 /* Get command header from data transmission */
493 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
494 user_cmd_header_buf
, user_cmd_header_len
);
499 /* Get payload from data transmission */
500 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
509 disconnect_sessiond();
514 * Create lttng handle and return pointer.
516 * The returned pointer will be NULL in case of malloc() error.
518 struct lttng_handle
*lttng_create_handle(const char *session_name
,
519 struct lttng_domain
*domain
)
521 struct lttng_handle
*handle
= NULL
;
523 handle
= zmalloc(sizeof(struct lttng_handle
));
524 if (handle
== NULL
) {
525 PERROR("malloc handle");
529 /* Copy session name */
530 lttng_ctl_copy_string(handle
->session_name
, session_name
,
531 sizeof(handle
->session_name
));
533 /* Copy lttng domain or leave initialized to 0. */
535 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
543 * Destroy handle by free(3) the pointer.
545 void lttng_destroy_handle(struct lttng_handle
*handle
)
551 * Register an outside consumer.
553 * Returns size of returned session payload data or a negative error code.
555 int lttng_register_consumer(struct lttng_handle
*handle
,
556 const char *socket_path
)
558 struct lttcomm_session_msg lsm
;
560 if (handle
== NULL
|| socket_path
== NULL
) {
561 return -LTTNG_ERR_INVALID
;
564 memset(&lsm
, 0, sizeof(lsm
));
565 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
566 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
567 sizeof(lsm
.session
.name
));
568 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
570 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
571 sizeof(lsm
.u
.reg
.path
));
573 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
577 * Start tracing for all traces of the session.
579 * Returns size of returned session payload data or a negative error code.
581 int lttng_start_tracing(const char *session_name
)
583 struct lttcomm_session_msg lsm
;
585 if (session_name
== NULL
) {
586 return -LTTNG_ERR_INVALID
;
589 memset(&lsm
, 0, sizeof(lsm
));
590 lsm
.cmd_type
= LTTNG_START_TRACE
;
592 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
593 sizeof(lsm
.session
.name
));
595 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
599 * Stop tracing for all traces of the session.
601 static int _lttng_stop_tracing(const char *session_name
, int wait
)
604 struct lttcomm_session_msg lsm
;
606 if (session_name
== NULL
) {
607 return -LTTNG_ERR_INVALID
;
610 memset(&lsm
, 0, sizeof(lsm
));
611 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
613 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
614 sizeof(lsm
.session
.name
));
616 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
617 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
625 /* Check for data availability */
627 data_ret
= lttng_data_pending(session_name
);
629 /* Return the data available call error. */
635 * Data sleep time before retrying (in usec). Don't sleep if the
636 * call returned value indicates availability.
639 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
641 } while (data_ret
!= 0);
649 * Stop tracing and wait for data availability.
651 int lttng_stop_tracing(const char *session_name
)
653 return _lttng_stop_tracing(session_name
, 1);
657 * Stop tracing but _don't_ wait for data availability.
659 int lttng_stop_tracing_no_wait(const char *session_name
)
661 return _lttng_stop_tracing(session_name
, 0);
665 * Add context to a channel.
667 * If the given channel is NULL, add the contexts to all channels.
668 * The event_name param is ignored.
670 * Returns the size of the returned payload data or a negative error code.
672 int lttng_add_context(struct lttng_handle
*handle
,
673 struct lttng_event_context
*ctx
, const char *event_name
,
674 const char *channel_name
)
679 struct lttcomm_session_msg lsm
;
681 /* Safety check. Both are mandatory. */
682 if (handle
== NULL
|| ctx
== NULL
) {
683 ret
= -LTTNG_ERR_INVALID
;
687 memset(&lsm
, 0, sizeof(lsm
));
688 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
690 /* If no channel name, send empty string. */
691 if (channel_name
== NULL
) {
692 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
693 sizeof(lsm
.u
.context
.channel_name
));
695 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
696 sizeof(lsm
.u
.context
.channel_name
));
699 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
700 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
701 sizeof(lsm
.session
.name
));
703 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
704 size_t provider_len
, ctx_len
;
705 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
706 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
708 if (!provider_name
|| !ctx_name
) {
709 ret
= -LTTNG_ERR_INVALID
;
713 provider_len
= strlen(provider_name
);
714 if (provider_len
== 0) {
715 ret
= -LTTNG_ERR_INVALID
;
718 lsm
.u
.context
.provider_name_len
= provider_len
;
720 ctx_len
= strlen(ctx_name
);
722 ret
= -LTTNG_ERR_INVALID
;
725 lsm
.u
.context
.context_name_len
= ctx_len
;
727 len
= provider_len
+ ctx_len
;
730 ret
= -LTTNG_ERR_NOMEM
;
734 memcpy(buf
, provider_name
, provider_len
);
735 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
737 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
739 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
741 * Don't leak application addresses to the sessiond.
742 * This is only necessary when ctx is for an app ctx otherwise
743 * the values inside the union (type & config) are overwritten.
745 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
746 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
749 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
756 * Enable event(s) for a channel.
758 * If no event name is specified, all events are enabled.
759 * If no channel name is specified, the default 'channel0' is used.
761 * Returns size of returned session payload data or a negative error code.
763 int lttng_enable_event(struct lttng_handle
*handle
,
764 struct lttng_event
*ev
, const char *channel_name
)
766 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
771 * Create or enable an event with a filter expression.
773 * Return negative error value on error.
774 * Return size of returned session payload data if OK.
776 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
777 struct lttng_event
*event
, const char *channel_name
,
778 const char *filter_expression
)
780 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
781 filter_expression
, 0, NULL
);
785 * Depending on the event, return a newly allocated agent filter expression or
786 * NULL if not applicable.
788 * An event with NO loglevel and the name is * will return NULL.
790 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
793 char *agent_filter
= NULL
;
797 /* Don't add filter for the '*' event. */
798 if (strcmp(ev
->name
, "*") != 0) {
800 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
803 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
811 /* Add loglevel filtering if any for the JUL domain. */
812 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
815 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
821 if (filter
|| agent_filter
) {
824 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
825 agent_filter
? agent_filter
: filter
, op
,
830 agent_filter
= new_filter
;
832 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
848 * Generate the filter bytecode from a given filter expression string. Put the
849 * newly allocated parser context in ctxp and populate the lsm object with the
852 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
854 static int generate_filter(char *filter_expression
,
855 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
858 struct filter_parser_ctx
*ctx
= NULL
;
861 assert(filter_expression
);
866 * Casting const to non-const, as the underlying function will use it in
869 fmem
= lttng_fmemopen((void *) filter_expression
,
870 strlen(filter_expression
), "r");
872 fprintf(stderr
, "Error opening memory as stream\n");
873 ret
= -LTTNG_ERR_FILTER_NOMEM
;
876 ctx
= filter_parser_ctx_alloc(fmem
);
878 fprintf(stderr
, "Error allocating parser\n");
879 ret
= -LTTNG_ERR_FILTER_NOMEM
;
880 goto filter_alloc_error
;
882 ret
= filter_parser_ctx_append_ast(ctx
);
884 fprintf(stderr
, "Parse error\n");
885 ret
= -LTTNG_ERR_FILTER_INVAL
;
888 ret
= filter_visitor_set_parent(ctx
);
890 fprintf(stderr
, "Set parent error\n");
891 ret
= -LTTNG_ERR_FILTER_INVAL
;
895 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
898 fprintf(stderr
, "XML print error\n");
899 ret
= -LTTNG_ERR_FILTER_INVAL
;
904 dbg_printf("Generating IR... ");
906 ret
= filter_visitor_ir_generate(ctx
);
908 fprintf(stderr
, "Generate IR error\n");
909 ret
= -LTTNG_ERR_FILTER_INVAL
;
912 dbg_printf("done\n");
914 dbg_printf("Validating IR... ");
916 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
918 ret
= -LTTNG_ERR_FILTER_INVAL
;
922 /* Normalize globbing patterns in the expression. */
923 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
925 ret
= -LTTNG_ERR_FILTER_INVAL
;
929 /* Validate strings used as literals in the expression. */
930 ret
= filter_visitor_ir_validate_string(ctx
);
932 ret
= -LTTNG_ERR_FILTER_INVAL
;
936 /* Validate globbing patterns in the expression. */
937 ret
= filter_visitor_ir_validate_globbing(ctx
);
939 ret
= -LTTNG_ERR_FILTER_INVAL
;
943 dbg_printf("done\n");
945 dbg_printf("Generating bytecode... ");
947 ret
= filter_visitor_bytecode_generate(ctx
);
949 fprintf(stderr
, "Generate bytecode error\n");
950 ret
= -LTTNG_ERR_FILTER_INVAL
;
953 dbg_printf("done\n");
954 dbg_printf("Size of bytecode generated: %u bytes.\n",
955 bytecode_get_len(&ctx
->bytecode
->b
));
957 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
958 + bytecode_get_len(&ctx
->bytecode
->b
);
959 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
961 /* No need to keep the memory stream. */
962 if (fclose(fmem
) != 0) {
971 filter_parser_ctx_free(ctx
);
973 if (fclose(fmem
) != 0) {
981 * Enable event(s) for a channel, possibly with exclusions and a filter.
982 * If no event name is specified, all events are enabled.
983 * If no channel name is specified, the default name is used.
984 * If filter expression is not NULL, the filter is set for the event.
985 * If exclusion count is not zero, the exclusions are set for the event.
986 * Returns size of returned session payload data or a negative error code.
988 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
989 struct lttng_event
*ev
, const char *channel_name
,
990 const char *original_filter_expression
,
991 int exclusion_count
, char **exclusion_list
)
993 struct lttcomm_session_msg lsm
;
996 unsigned int free_filter_expression
= 0;
997 struct filter_parser_ctx
*ctx
= NULL
;
999 * Cast as non-const since we may replace the filter expression
1000 * by a dynamically allocated string. Otherwise, the original
1001 * string is not modified.
1003 char *filter_expression
= (char *) original_filter_expression
;
1005 if (handle
== NULL
|| ev
== NULL
) {
1006 ret
= -LTTNG_ERR_INVALID
;
1011 * Empty filter string will always be rejected by the parser
1012 * anyway, so treat this corner-case early to eliminate
1013 * lttng_fmemopen error for 0-byte allocation.
1015 if (filter_expression
&& filter_expression
[0] == '\0') {
1016 ret
= -LTTNG_ERR_INVALID
;
1020 memset(&lsm
, 0, sizeof(lsm
));
1022 /* If no channel name, send empty string. */
1023 if (channel_name
== NULL
) {
1024 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1025 sizeof(lsm
.u
.enable
.channel_name
));
1027 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1028 sizeof(lsm
.u
.enable
.channel_name
));
1031 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1032 if (ev
->name
[0] == '\0') {
1033 /* Enable all events */
1034 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1037 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1038 /* FIXME: copying non-packed struct to packed struct. */
1039 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1041 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1042 sizeof(lsm
.session
.name
));
1043 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1044 lsm
.u
.enable
.bytecode_len
= 0;
1047 * For the JUL domain, a filter is enforced except for the enable all
1048 * event. This is done to avoid having the event in all sessions thus
1049 * filtering by logger name.
1051 if (exclusion_count
== 0 && filter_expression
== NULL
&&
1052 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1053 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1054 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1059 * We have either a filter or some exclusions, so we need to set up
1060 * a variable-length memory block from where to send the data.
1063 /* Parse filter expression. */
1064 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1065 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1066 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1067 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1068 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1069 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1072 /* Setup JUL filter if needed. */
1073 agent_filter
= set_agent_filter(filter_expression
, ev
);
1074 if (!agent_filter
) {
1075 if (!filter_expression
) {
1077 * No JUL and no filter, just skip
1084 * With an agent filter, the original filter has
1085 * been added to it thus replace the filter
1088 filter_expression
= agent_filter
;
1089 free_filter_expression
= 1;
1093 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1099 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
1100 + lsm
.u
.enable
.expression_len
1101 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1103 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1107 /* Put exclusion names first in the data. */
1108 while (exclusion_count
--) {
1109 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1110 *(exclusion_list
+ exclusion_count
),
1111 LTTNG_SYMBOL_NAME_LEN
- 1);
1113 /* Add filter expression next. */
1114 if (lsm
.u
.enable
.expression_len
!= 0) {
1116 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1118 lsm
.u
.enable
.expression_len
);
1120 /* Add filter bytecode next. */
1121 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1123 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1124 + lsm
.u
.enable
.expression_len
,
1126 lsm
.u
.enable
.bytecode_len
);
1129 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1130 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1131 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
,
1136 if (filter_expression
&& ctx
) {
1137 filter_bytecode_free(ctx
);
1138 filter_ir_free(ctx
);
1139 filter_parser_ctx_free(ctx
);
1142 if (free_filter_expression
) {
1144 * The filter expression has been replaced and must be freed as
1145 * it is not the original filter expression received as a
1148 free(filter_expression
);
1152 * Return directly to the caller and don't ask the sessiond since
1153 * something went wrong in the parsing of data above.
1158 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1162 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1163 struct lttng_event
*ev
, const char *channel_name
,
1164 const char *original_filter_expression
)
1166 struct lttcomm_session_msg lsm
;
1169 unsigned int free_filter_expression
= 0;
1170 struct filter_parser_ctx
*ctx
= NULL
;
1172 * Cast as non-const since we may replace the filter expression
1173 * by a dynamically allocated string. Otherwise, the original
1174 * string is not modified.
1176 char *filter_expression
= (char *) original_filter_expression
;
1178 if (handle
== NULL
|| ev
== NULL
) {
1179 ret
= -LTTNG_ERR_INVALID
;
1184 * Empty filter string will always be rejected by the parser
1185 * anyway, so treat this corner-case early to eliminate
1186 * lttng_fmemopen error for 0-byte allocation.
1188 if (filter_expression
&& filter_expression
[0] == '\0') {
1189 ret
= -LTTNG_ERR_INVALID
;
1193 memset(&lsm
, 0, sizeof(lsm
));
1195 /* If no channel name, send empty string. */
1196 if (channel_name
== NULL
) {
1197 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1198 sizeof(lsm
.u
.disable
.channel_name
));
1200 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1201 sizeof(lsm
.u
.disable
.channel_name
));
1204 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1206 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1207 /* FIXME: copying non-packed struct to packed struct. */
1208 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1210 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1211 sizeof(lsm
.session
.name
));
1212 lsm
.u
.disable
.bytecode_len
= 0;
1215 * For the JUL domain, a filter is enforced except for the
1216 * disable all event. This is done to avoid having the event in
1217 * all sessions thus filtering by logger name.
1219 if (filter_expression
== NULL
&&
1220 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1221 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1222 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1227 * We have a filter, so we need to set up a variable-length
1228 * memory block from where to send the data.
1231 /* Parse filter expression */
1232 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1233 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1234 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1235 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1236 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1237 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1240 /* Setup JUL filter if needed. */
1241 agent_filter
= set_agent_filter(filter_expression
, ev
);
1242 if (!agent_filter
) {
1243 if (!filter_expression
) {
1245 * No JUL and no filter, just skip
1252 * With a JUL filter, the original filter has
1253 * been added to it thus replace the filter
1256 filter_expression
= agent_filter
;
1257 free_filter_expression
= 1;
1261 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1267 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1268 + lsm
.u
.disable
.expression_len
);
1270 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1274 /* Add filter expression. */
1275 if (lsm
.u
.disable
.expression_len
!= 0) {
1278 lsm
.u
.disable
.expression_len
);
1280 /* Add filter bytecode next. */
1281 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1283 + lsm
.u
.disable
.expression_len
,
1285 lsm
.u
.disable
.bytecode_len
);
1288 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1289 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1293 if (filter_expression
&& ctx
) {
1294 filter_bytecode_free(ctx
);
1295 filter_ir_free(ctx
);
1296 filter_parser_ctx_free(ctx
);
1299 if (free_filter_expression
) {
1301 * The filter expression has been replaced and must be freed as
1302 * it is not the original filter expression received as a
1305 free(filter_expression
);
1309 * Return directly to the caller and don't ask the sessiond since
1310 * something went wrong in the parsing of data above.
1315 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1320 * Disable event(s) of a channel and domain.
1321 * If no event name is specified, all events are disabled.
1322 * If no channel name is specified, the default 'channel0' is used.
1323 * Returns size of returned session payload data or a negative error code.
1325 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1326 const char *channel_name
)
1328 struct lttng_event ev
;
1330 memset(&ev
, 0, sizeof(ev
));
1332 ev
.type
= LTTNG_EVENT_ALL
;
1333 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1334 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1337 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1339 struct lttng_channel
*channel
= NULL
;
1340 struct lttng_channel_extended
*extended
= NULL
;
1346 /* Validate domain. */
1347 switch (domain
->type
) {
1348 case LTTNG_DOMAIN_UST
:
1349 switch (domain
->buf_type
) {
1350 case LTTNG_BUFFER_PER_UID
:
1351 case LTTNG_BUFFER_PER_PID
:
1357 case LTTNG_DOMAIN_KERNEL
:
1358 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1366 channel
= zmalloc(sizeof(*channel
));
1371 extended
= zmalloc(sizeof(*extended
));
1376 channel
->attr
.extended
.ptr
= extended
;
1378 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1386 void lttng_channel_destroy(struct lttng_channel
*channel
)
1392 if (channel
->attr
.extended
.ptr
) {
1393 free(channel
->attr
.extended
.ptr
);
1399 * Enable channel per domain
1400 * Returns size of returned session payload data or a negative error code.
1402 int lttng_enable_channel(struct lttng_handle
*handle
,
1403 struct lttng_channel
*in_chan
)
1405 struct lttcomm_session_msg lsm
;
1407 /* NULL arguments are forbidden. No default values. */
1408 if (handle
== NULL
|| in_chan
== NULL
) {
1409 return -LTTNG_ERR_INVALID
;
1412 memset(&lsm
, 0, sizeof(lsm
));
1413 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1414 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1416 if (!in_chan
->attr
.extended
.ptr
) {
1417 struct lttng_channel
*channel
;
1418 struct lttng_channel_extended
*extended
;
1420 channel
= lttng_channel_create(&handle
->domain
);
1422 return -LTTNG_ERR_NOMEM
;
1426 * Create a new channel in order to use default extended
1429 extended
= (struct lttng_channel_extended
*)
1430 channel
->attr
.extended
.ptr
;
1431 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1432 lttng_channel_destroy(channel
);
1434 struct lttng_channel_extended
*extended
;
1436 extended
= (struct lttng_channel_extended
*)
1437 in_chan
->attr
.extended
.ptr
;
1438 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1441 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1442 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1444 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1445 sizeof(lsm
.session
.name
));
1447 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1451 * All tracing will be stopped for registered events of the channel.
1452 * Returns size of returned session payload data or a negative error code.
1454 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1456 struct lttcomm_session_msg lsm
;
1458 /* Safety check. Both are mandatory. */
1459 if (handle
== NULL
|| name
== NULL
) {
1460 return -LTTNG_ERR_INVALID
;
1463 memset(&lsm
, 0, sizeof(lsm
));
1465 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1467 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1468 sizeof(lsm
.u
.disable
.channel_name
));
1470 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1472 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1473 sizeof(lsm
.session
.name
));
1475 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1479 * Add PID to session tracker.
1480 * Return 0 on success else a negative LTTng error code.
1482 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1484 struct lttcomm_session_msg lsm
;
1486 /* NULL arguments are forbidden. No default values. */
1487 if (handle
== NULL
) {
1488 return -LTTNG_ERR_INVALID
;
1491 memset(&lsm
, 0, sizeof(lsm
));
1493 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1494 lsm
.u
.pid_tracker
.pid
= pid
;
1496 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1498 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1499 sizeof(lsm
.session
.name
));
1501 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1505 * Remove PID from session tracker.
1506 * Return 0 on success else a negative LTTng error code.
1508 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1510 struct lttcomm_session_msg lsm
;
1512 /* NULL arguments are forbidden. No default values. */
1513 if (handle
== NULL
) {
1514 return -LTTNG_ERR_INVALID
;
1517 memset(&lsm
, 0, sizeof(lsm
));
1519 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1520 lsm
.u
.pid_tracker
.pid
= pid
;
1522 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1524 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1525 sizeof(lsm
.session
.name
));
1527 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1531 * Lists all available tracepoints of domain.
1532 * Sets the contents of the events array.
1533 * Returns the number of lttng_event entries in events;
1534 * on error, returns a negative value.
1536 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1537 struct lttng_event
**events
)
1540 struct lttcomm_session_msg lsm
;
1542 if (handle
== NULL
) {
1543 return -LTTNG_ERR_INVALID
;
1546 memset(&lsm
, 0, sizeof(lsm
));
1547 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1548 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1550 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1555 return ret
/ sizeof(struct lttng_event
);
1559 * Lists all available tracepoint fields of domain.
1560 * Sets the contents of the event field array.
1561 * Returns the number of lttng_event_field entries in events;
1562 * on error, returns a negative value.
1564 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1565 struct lttng_event_field
**fields
)
1568 struct lttcomm_session_msg lsm
;
1570 if (handle
== NULL
) {
1571 return -LTTNG_ERR_INVALID
;
1574 memset(&lsm
, 0, sizeof(lsm
));
1575 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1576 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1578 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1583 return ret
/ sizeof(struct lttng_event_field
);
1587 * Lists all available kernel system calls. Allocates and sets the contents of
1590 * Returns the number of lttng_event entries in events; on error, returns a
1593 int lttng_list_syscalls(struct lttng_event
**events
)
1596 struct lttcomm_session_msg lsm
;
1599 return -LTTNG_ERR_INVALID
;
1602 memset(&lsm
, 0, sizeof(lsm
));
1603 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1604 /* Force kernel domain for system calls. */
1605 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1607 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1612 return ret
/ sizeof(struct lttng_event
);
1616 * Returns a human readable string describing
1617 * the error code (a negative value).
1619 const char *lttng_strerror(int code
)
1621 return error_get_str(code
);
1625 * Create a brand new session using name and url for destination.
1627 * Returns LTTNG_OK on success or a negative error code.
1629 int lttng_create_session(const char *name
, const char *url
)
1633 struct lttcomm_session_msg lsm
;
1634 struct lttng_uri
*uris
= NULL
;
1637 return -LTTNG_ERR_INVALID
;
1640 memset(&lsm
, 0, sizeof(lsm
));
1642 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1643 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1645 /* There should never be a data URL */
1646 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1648 return -LTTNG_ERR_INVALID
;
1651 lsm
.u
.uri
.size
= size
;
1653 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
1654 sizeof(struct lttng_uri
) * size
, NULL
);
1661 * Destroy session using name.
1662 * Returns size of returned session payload data or a negative error code.
1665 int _lttng_destroy_session(const char *session_name
)
1667 struct lttcomm_session_msg lsm
;
1669 if (session_name
== NULL
) {
1670 return -LTTNG_ERR_INVALID
;
1673 memset(&lsm
, 0, sizeof(lsm
));
1674 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1676 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1677 sizeof(lsm
.session
.name
));
1679 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1683 * Stop the session and wait for the data before destroying it
1685 int lttng_destroy_session(const char *session_name
)
1690 * Stop the tracing and wait for the data.
1692 ret
= _lttng_stop_tracing(session_name
, 1);
1693 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1697 ret
= _lttng_destroy_session(session_name
);
1703 * Destroy the session without waiting for the data.
1705 int lttng_destroy_session_no_wait(const char *session_name
)
1710 * Stop the tracing without waiting for the data.
1711 * The session might already have been stopped, so just
1714 ret
= _lttng_stop_tracing(session_name
, 0);
1715 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1719 ret
= _lttng_destroy_session(session_name
);
1725 * Ask the session daemon for all available sessions.
1726 * Sets the contents of the sessions array.
1727 * Returns the number of lttng_session entries in sessions;
1728 * on error, returns a negative value.
1730 int lttng_list_sessions(struct lttng_session
**sessions
)
1733 struct lttcomm_session_msg lsm
;
1735 memset(&lsm
, 0, sizeof(lsm
));
1736 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1737 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1742 return ret
/ sizeof(struct lttng_session
);
1745 int lttng_set_session_shm_path(const char *session_name
,
1746 const char *shm_path
)
1748 struct lttcomm_session_msg lsm
;
1750 if (session_name
== NULL
) {
1751 return -LTTNG_ERR_INVALID
;
1754 memset(&lsm
, 0, sizeof(lsm
));
1755 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1757 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1758 sizeof(lsm
.session
.name
));
1759 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1760 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1762 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1766 * Ask the session daemon for all available domains of a session.
1767 * Sets the contents of the domains array.
1768 * Returns the number of lttng_domain entries in domains;
1769 * on error, returns a negative value.
1771 int lttng_list_domains(const char *session_name
,
1772 struct lttng_domain
**domains
)
1775 struct lttcomm_session_msg lsm
;
1777 if (session_name
== NULL
) {
1778 return -LTTNG_ERR_INVALID
;
1781 memset(&lsm
, 0, sizeof(lsm
));
1782 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1784 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1785 sizeof(lsm
.session
.name
));
1787 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1792 return ret
/ sizeof(struct lttng_domain
);
1796 * Ask the session daemon for all available channels of a session.
1797 * Sets the contents of the channels array.
1798 * Returns the number of lttng_channel entries in channels;
1799 * on error, returns a negative value.
1801 int lttng_list_channels(struct lttng_handle
*handle
,
1802 struct lttng_channel
**channels
)
1805 size_t channel_count
, i
;
1806 const size_t channel_size
= sizeof(struct lttng_channel
) +
1807 sizeof(struct lttng_channel_extended
);
1808 struct lttcomm_session_msg lsm
;
1811 if (handle
== NULL
) {
1812 ret
= -LTTNG_ERR_INVALID
;
1816 memset(&lsm
, 0, sizeof(lsm
));
1817 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1818 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1819 sizeof(lsm
.session
.name
));
1821 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1823 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1828 if (ret
% channel_size
) {
1829 ret
= -LTTNG_ERR_UNK
;
1834 channel_count
= (size_t) ret
/ channel_size
;
1836 /* Set extended info pointers */
1837 extended_at
= ((void *) *channels
) +
1838 channel_count
* sizeof(struct lttng_channel
);
1839 for (i
= 0; i
< channel_count
; i
++) {
1840 struct lttng_channel
*chan
= &(*channels
)[i
];
1842 chan
->attr
.extended
.ptr
= extended_at
;
1843 extended_at
+= sizeof(struct lttng_channel_extended
);
1846 ret
= (int) channel_count
;
1852 * Ask the session daemon for all available events of a session channel.
1853 * Sets the contents of the events array.
1854 * Returns the number of lttng_event entries in events;
1855 * on error, returns a negative value.
1857 int lttng_list_events(struct lttng_handle
*handle
,
1858 const char *channel_name
, struct lttng_event
**events
)
1861 struct lttcomm_session_msg lsm
;
1862 struct lttcomm_event_command_header
*cmd_header
= NULL
;
1863 size_t cmd_header_len
;
1864 uint32_t nb_events
, i
;
1867 /* Safety check. An handle and channel name are mandatory */
1868 if (handle
== NULL
|| channel_name
== NULL
) {
1869 return -LTTNG_ERR_INVALID
;
1872 memset(&lsm
, 0, sizeof(lsm
));
1873 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1874 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1875 sizeof(lsm
.session
.name
));
1876 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1877 sizeof(lsm
.u
.list
.channel_name
));
1878 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1880 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, NULL
, 0, (void **) events
,
1881 (void **) &cmd_header
, &cmd_header_len
);
1886 /* Set number of events and free command header */
1887 nb_events
= cmd_header
->nb_events
;
1888 if (nb_events
> INT_MAX
) {
1892 ret
= (int) nb_events
;
1896 /* Set extended info pointers */
1897 extended_at
= ((void*) (*events
)) +
1898 nb_events
* sizeof(struct lttng_event
);
1900 for (i
= 0; i
< nb_events
; i
++) {
1901 struct lttcomm_event_extended_header
*ext_header
;
1902 struct lttng_event
*event
= &(*events
)[i
];
1904 event
->extended
.ptr
= extended_at
;
1906 (struct lttcomm_event_extended_header
*) extended_at
;
1907 extended_at
+= sizeof(*ext_header
);
1908 extended_at
+= ext_header
->filter_len
;
1910 ext_header
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
1920 int lttng_event_get_filter_expression(struct lttng_event
*event
,
1921 const char **filter_expression
)
1924 struct lttcomm_event_extended_header
*ext_header
;
1926 if (!event
|| !filter_expression
) {
1927 ret
= -LTTNG_ERR_INVALID
;
1931 ext_header
= event
->extended
.ptr
;
1935 * This can happen since the lttng_event structure is
1936 * used for other tasks where this pointer is never set.
1938 *filter_expression
= NULL
;
1942 if (ext_header
->filter_len
) {
1943 *filter_expression
= ((const char *) (ext_header
)) +
1944 sizeof(*ext_header
);
1946 *filter_expression
= NULL
;
1953 int lttng_event_get_exclusion_name_count(struct lttng_event
*event
)
1956 struct lttcomm_event_extended_header
*ext_header
;
1959 ret
= -LTTNG_ERR_INVALID
;
1963 ext_header
= event
->extended
.ptr
;
1966 * This can happen since the lttng_event structure is
1967 * used for other tasks where this pointer is never set.
1973 if (ext_header
->nb_exclusions
> INT_MAX
) {
1974 ret
= -LTTNG_ERR_OVERFLOW
;
1977 ret
= (int) ext_header
->nb_exclusions
;
1982 int lttng_event_get_exclusion_name(struct lttng_event
*event
,
1983 size_t index
, const char **exclusion_name
)
1986 struct lttcomm_event_extended_header
*ext_header
;
1989 if (!event
|| !exclusion_name
) {
1990 ret
= -LTTNG_ERR_INVALID
;
1994 ext_header
= event
->extended
.ptr
;
1996 ret
= -LTTNG_ERR_INVALID
;
2000 if (index
>= ext_header
->nb_exclusions
) {
2001 ret
= -LTTNG_ERR_INVALID
;
2005 at
= (void *) ext_header
+ sizeof(*ext_header
);
2006 at
+= ext_header
->filter_len
;
2007 at
+= index
* LTTNG_SYMBOL_NAME_LEN
;
2008 *exclusion_name
= at
;
2015 * Sets the tracing_group variable with name.
2016 * This function allocates memory pointed to by tracing_group.
2017 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2019 int lttng_set_tracing_group(const char *name
)
2022 return -LTTNG_ERR_INVALID
;
2025 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2026 return -LTTNG_ERR_FATAL
;
2032 int lttng_calibrate(struct lttng_handle
*handle
,
2033 struct lttng_calibrate
*calibrate
)
2036 * This command was removed in LTTng 2.9.
2038 return -LTTNG_ERR_UND
;
2042 * Set default channel attributes.
2043 * If either or both of the arguments are null, attr content is zeroe'd.
2045 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2046 struct lttng_channel_attr
*attr
)
2048 struct lttng_channel_extended
*extended
;
2051 if (attr
== NULL
|| domain
== NULL
) {
2055 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2056 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2058 /* Same for all domains. */
2059 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2060 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2061 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2063 switch (domain
->type
) {
2064 case LTTNG_DOMAIN_KERNEL
:
2065 attr
->switch_timer_interval
=
2066 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2067 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2068 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2069 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2070 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2072 extended
->monitor_timer_interval
=
2073 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2074 extended
->blocking_timeout
=
2075 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2078 case LTTNG_DOMAIN_UST
:
2079 switch (domain
->buf_type
) {
2080 case LTTNG_BUFFER_PER_UID
:
2081 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2082 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2083 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2084 attr
->switch_timer_interval
=
2085 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2086 attr
->read_timer_interval
=
2087 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2089 extended
->monitor_timer_interval
=
2090 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2091 extended
->blocking_timeout
=
2092 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2095 case LTTNG_BUFFER_PER_PID
:
2097 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2098 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2099 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2100 attr
->switch_timer_interval
=
2101 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2102 attr
->read_timer_interval
=
2103 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2105 extended
->monitor_timer_interval
=
2106 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2107 extended
->blocking_timeout
=
2108 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2113 /* Default behavior: leave set to 0. */
2117 attr
->extended
.ptr
= extended
;
2120 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2121 uint64_t *discarded_events
)
2124 struct lttng_channel_extended
*chan_ext
;
2126 if (!channel
|| !discarded_events
) {
2127 ret
= -LTTNG_ERR_INVALID
;
2131 chan_ext
= channel
->attr
.extended
.ptr
;
2134 * This can happen since the lttng_channel structure is
2135 * used for other tasks where this pointer is never set.
2137 *discarded_events
= 0;
2141 *discarded_events
= chan_ext
->discarded_events
;
2146 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2147 uint64_t *lost_packets
)
2150 struct lttng_channel_extended
*chan_ext
;
2152 if (!channel
|| !lost_packets
) {
2153 ret
= -LTTNG_ERR_INVALID
;
2157 chan_ext
= channel
->attr
.extended
.ptr
;
2160 * This can happen since the lttng_channel structure is
2161 * used for other tasks where this pointer is never set.
2167 *lost_packets
= chan_ext
->lost_packets
;
2172 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2173 uint64_t *monitor_timer_interval
)
2177 if (!chan
|| !monitor_timer_interval
) {
2178 ret
= -LTTNG_ERR_INVALID
;
2182 if (!chan
->attr
.extended
.ptr
) {
2183 ret
= -LTTNG_ERR_INVALID
;
2187 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2188 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2193 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2194 uint64_t monitor_timer_interval
)
2198 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2199 ret
= -LTTNG_ERR_INVALID
;
2203 ((struct lttng_channel_extended
*)
2204 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2205 monitor_timer_interval
;
2210 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2211 int64_t *blocking_timeout
)
2215 if (!chan
|| !blocking_timeout
) {
2216 ret
= -LTTNG_ERR_INVALID
;
2220 if (!chan
->attr
.extended
.ptr
) {
2221 ret
= -LTTNG_ERR_INVALID
;
2225 *blocking_timeout
= ((struct lttng_channel_extended
*)
2226 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2231 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2232 int64_t blocking_timeout
)
2235 int64_t msec_timeout
;
2237 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2238 ret
= -LTTNG_ERR_INVALID
;
2242 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2243 ret
= -LTTNG_ERR_INVALID
;
2248 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2249 * us to accept a narrower range of values (msecs expressed as a signed
2252 msec_timeout
= blocking_timeout
/ 1000;
2253 if (msec_timeout
!= (int32_t) msec_timeout
) {
2254 ret
= -LTTNG_ERR_INVALID
;
2258 ((struct lttng_channel_extended
*)
2259 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2266 * Check if session daemon is alive.
2268 * Return 1 if alive or 0 if not.
2269 * On error returns a negative value.
2271 int lttng_session_daemon_alive(void)
2275 ret
= set_session_daemon_path();
2281 if (*sessiond_sock_path
== '\0') {
2283 * No socket path set. Weird error which means the constructor
2289 ret
= try_connect_sessiond(sessiond_sock_path
);
2300 * Set URL for a consumer for a session and domain.
2302 * Return 0 on success, else a negative value.
2304 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2305 const char *control_url
, const char *data_url
)
2309 struct lttcomm_session_msg lsm
;
2310 struct lttng_uri
*uris
= NULL
;
2312 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2313 return -LTTNG_ERR_INVALID
;
2316 memset(&lsm
, 0, sizeof(lsm
));
2318 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2320 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2321 sizeof(lsm
.session
.name
));
2322 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2324 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2326 return -LTTNG_ERR_INVALID
;
2329 lsm
.u
.uri
.size
= size
;
2331 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2332 sizeof(struct lttng_uri
) * size
, NULL
);
2341 int lttng_enable_consumer(struct lttng_handle
*handle
)
2349 int lttng_disable_consumer(struct lttng_handle
*handle
)
2355 * This is an extension of create session that is ONLY and SHOULD only be used
2356 * by the lttng command line program. It exists to avoid using URI parsing in
2359 * We need the date and time for the trace path subdirectory for the case where
2360 * the user does NOT define one using either -o or -U. Using the normal
2361 * lttng_create_session API call, we have no clue on the session daemon side if
2362 * the URL was generated automatically by the client or define by the user.
2364 * So this function "wrapper" is hidden from the public API, takes the datetime
2365 * string and appends it if necessary to the URI subdirectory before sending it
2366 * to the session daemon.
2368 * With this extra function, the lttng_create_session call behavior is not
2369 * changed and the timestamp is appended to the URI on the session daemon side
2372 int _lttng_create_session_ext(const char *name
, const char *url
,
2373 const char *datetime
)
2377 struct lttcomm_session_msg lsm
;
2378 struct lttng_uri
*uris
= NULL
;
2380 if (name
== NULL
|| datetime
== NULL
) {
2381 return -LTTNG_ERR_INVALID
;
2384 memset(&lsm
, 0, sizeof(lsm
));
2386 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
2387 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2389 /* There should never be a data URL. */
2390 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2392 ret
= -LTTNG_ERR_INVALID
;
2396 lsm
.u
.uri
.size
= size
;
2398 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2399 /* Don't append datetime if the name was automatically created. */
2400 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
2401 strlen(DEFAULT_SESSION_NAME
) + 1)) {
2402 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
2405 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2408 PERROR("snprintf uri subdir");
2409 ret
= -LTTNG_ERR_FATAL
;
2414 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2415 sizeof(struct lttng_uri
) * size
, NULL
);
2423 * For a given session name, this call checks if the data is ready to be read
2424 * or is still being extracted by the consumer(s) hence not ready to be used by
2427 int lttng_data_pending(const char *session_name
)
2430 struct lttcomm_session_msg lsm
;
2431 uint8_t *pending
= NULL
;
2433 if (session_name
== NULL
) {
2434 return -LTTNG_ERR_INVALID
;
2437 memset(&lsm
, 0, sizeof(lsm
));
2438 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2440 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2441 sizeof(lsm
.session
.name
));
2443 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2446 } else if (ret
!= 1) {
2447 /* Unexpected payload size */
2448 ret
= -LTTNG_ERR_INVALID
;
2450 } else if (!pending
) {
2451 /* Internal error. */
2452 ret
= -LTTNG_ERR_UNK
;
2456 ret
= (int) *pending
;
2463 * Create a session exclusively used for snapshot.
2465 * Returns LTTNG_OK on success or a negative error code.
2467 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2471 struct lttcomm_session_msg lsm
;
2472 struct lttng_uri
*uris
= NULL
;
2475 return -LTTNG_ERR_INVALID
;
2478 memset(&lsm
, 0, sizeof(lsm
));
2480 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
2481 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2483 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
2485 return -LTTNG_ERR_INVALID
;
2488 lsm
.u
.uri
.size
= size
;
2490 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2491 sizeof(struct lttng_uri
) * size
, NULL
);
2498 * Create a session exclusively used for live.
2500 * Returns LTTNG_OK on success or a negative error code.
2502 int lttng_create_session_live(const char *name
, const char *url
,
2503 unsigned int timer_interval
)
2507 struct lttcomm_session_msg lsm
;
2508 struct lttng_uri
*uris
= NULL
;
2510 if (name
== NULL
|| timer_interval
== 0) {
2511 return -LTTNG_ERR_INVALID
;
2514 memset(&lsm
, 0, sizeof(lsm
));
2516 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
2517 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2520 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2522 ret
= -LTTNG_ERR_INVALID
;
2526 /* file:// is not accepted for live session. */
2527 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2528 ret
= -LTTNG_ERR_INVALID
;
2535 lsm
.u
.session_live
.nb_uri
= size
;
2536 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2538 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2539 sizeof(struct lttng_uri
) * size
, NULL
);
2547 * List PIDs in the tracker.
2549 * enabled is set to whether the PID tracker is enabled.
2550 * pids is set to an allocated array of PIDs currently tracked. On
2551 * success, pids must be freed by the caller.
2552 * nr_pids is set to the number of entries contained by the pids array.
2554 * Returns 0 on success, else a negative LTTng error code.
2556 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2557 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2561 struct lttcomm_session_msg lsm
;
2563 int32_t *pids
= NULL
;
2565 if (handle
== NULL
) {
2566 return -LTTNG_ERR_INVALID
;
2569 memset(&lsm
, 0, sizeof(lsm
));
2570 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2571 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2572 sizeof(lsm
.session
.name
));
2573 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2575 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2579 nr_pids
= ret
/ sizeof(int32_t);
2580 if (nr_pids
> 0 && !pids
) {
2581 return -LTTNG_ERR_UNK
;
2583 if (nr_pids
== 1 && pids
[0] == -1) {
2589 *_enabled
= enabled
;
2591 *_nr_pids
= nr_pids
;
2596 * Regenerate the metadata for a session.
2597 * Return 0 on success, a negative error code on error.
2599 int lttng_regenerate_metadata(const char *session_name
)
2602 struct lttcomm_session_msg lsm
;
2604 if (!session_name
) {
2605 ret
= -LTTNG_ERR_INVALID
;
2609 memset(&lsm
, 0, sizeof(lsm
));
2610 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2612 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2613 sizeof(lsm
.session
.name
));
2615 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2626 * Deprecated, replaced by lttng_regenerate_metadata.
2628 int lttng_metadata_regenerate(const char *session_name
)
2630 return lttng_regenerate_metadata(session_name
);
2634 * Regenerate the statedump of a session.
2635 * Return 0 on success, a negative error code on error.
2637 int lttng_regenerate_statedump(const char *session_name
)
2640 struct lttcomm_session_msg lsm
;
2642 if (!session_name
) {
2643 ret
= -LTTNG_ERR_INVALID
;
2647 memset(&lsm
, 0, sizeof(lsm
));
2648 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2650 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2651 sizeof(lsm
.session
.name
));
2653 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2663 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2666 struct lttcomm_session_msg lsm
;
2667 char *trigger_buf
= NULL
;
2668 ssize_t trigger_size
;
2671 ret
= -LTTNG_ERR_INVALID
;
2675 if (!lttng_trigger_validate(trigger
)) {
2676 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2680 trigger_size
= lttng_trigger_serialize(trigger
, NULL
);
2681 if (trigger_size
< 0) {
2682 ret
= -LTTNG_ERR_UNK
;
2686 trigger_buf
= zmalloc(trigger_size
);
2688 ret
= -LTTNG_ERR_NOMEM
;
2692 memset(&lsm
, 0, sizeof(lsm
));
2693 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
2694 if (lttng_trigger_serialize(trigger
, trigger_buf
) < 0) {
2695 ret
= -LTTNG_ERR_UNK
;
2699 lsm
.u
.trigger
.length
= (uint32_t) trigger_size
;
2700 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, trigger_buf
,
2701 trigger_size
, NULL
);
2707 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
2710 struct lttcomm_session_msg lsm
;
2711 char *trigger_buf
= NULL
;
2712 ssize_t trigger_size
;
2715 ret
= -LTTNG_ERR_INVALID
;
2719 if (!lttng_trigger_validate(trigger
)) {
2720 ret
= -LTTNG_ERR_INVALID
;
2724 trigger_size
= lttng_trigger_serialize(trigger
, NULL
);
2725 if (trigger_size
< 0) {
2726 ret
= -LTTNG_ERR_UNK
;
2730 trigger_buf
= zmalloc(trigger_size
);
2732 ret
= -LTTNG_ERR_NOMEM
;
2736 memset(&lsm
, 0, sizeof(lsm
));
2737 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
2738 if (lttng_trigger_serialize(trigger
, trigger_buf
) < 0) {
2739 ret
= -LTTNG_ERR_UNK
;
2743 lsm
.u
.trigger
.length
= (uint32_t) trigger_size
;
2744 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, trigger_buf
,
2745 trigger_size
, NULL
);
2754 static void __attribute__((constructor
)) init(void)
2756 /* Set default session group */
2757 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2763 static void __attribute__((destructor
)) lttng_ctl_exit(void)
2765 free(tracing_group
);