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>
40 #include "filter/filter-ast.h"
41 #include "filter/filter-parser.h"
42 #include "filter/filter-bytecode.h"
43 #include "filter/memstream.h"
44 #include "lttng-ctl-helper.h"
47 static const int print_xml
= 1;
48 #define dbg_printf(fmt, args...) \
49 printf("[debug liblttng-ctl] " fmt, ## args)
51 static const int print_xml
= 0;
52 #define dbg_printf(fmt, args...) \
54 /* do nothing but check printf format */ \
56 printf("[debug liblttnctl] " fmt, ## args); \
61 /* Socket to session daemon for communication */
62 static int sessiond_socket
;
63 static char sessiond_sock_path
[PATH_MAX
];
66 static char *tracing_group
;
72 * Those two variables are used by error.h to silent or control the verbosity of
73 * error message. They are global to the library so application linking with it
74 * are able to compile correctly and also control verbosity of the library.
77 int lttng_opt_verbose
;
81 * Copy string from src to dst and enforce null terminated byte.
84 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
87 strncpy(dst
, src
, len
);
88 /* Enforce the NULL terminated byte */
96 * Copy domain to lttcomm_session_msg domain.
98 * If domain is unknown, default domain will be the kernel.
101 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
102 struct lttng_domain
*src
)
106 case LTTNG_DOMAIN_KERNEL
:
107 case LTTNG_DOMAIN_UST
:
108 case LTTNG_DOMAIN_JUL
:
109 case LTTNG_DOMAIN_LOG4J
:
110 case LTTNG_DOMAIN_PYTHON
:
111 memcpy(dst
, src
, sizeof(struct lttng_domain
));
114 memset(dst
, 0, sizeof(struct lttng_domain
));
121 * Send lttcomm_session_msg to the session daemon.
123 * On success, returns the number of bytes sent (>=0)
124 * On error, returns -1
126 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
131 ret
= -LTTNG_ERR_NO_SESSIOND
;
135 DBG("LSM cmd type : %d", lsm
->cmd_type
);
137 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
138 sizeof(struct lttcomm_session_msg
));
140 ret
= -LTTNG_ERR_FATAL
;
148 * Send var len data to the session daemon.
150 * On success, returns the number of bytes sent (>=0)
151 * On error, returns -1
153 static int send_session_varlen(void *data
, size_t len
)
158 ret
= -LTTNG_ERR_NO_SESSIOND
;
167 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
169 ret
= -LTTNG_ERR_FATAL
;
177 * Receive data from the sessiond socket.
179 * On success, returns the number of bytes received (>=0)
180 * On error, returns -1 (recvmsg() error) or -ENOTCONN
182 static int recv_data_sessiond(void *buf
, size_t len
)
187 ret
= -LTTNG_ERR_NO_SESSIOND
;
191 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
193 ret
= -LTTNG_ERR_FATAL
;
201 * Check if we are in the specified group.
203 * If yes return 1, else return -1.
206 int lttng_check_tracing_group(void)
208 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
210 int grp_list_size
, grp_id
, i
;
212 const char *grp_name
= tracing_group
;
214 /* Get GID of group 'tracing' */
215 grp_tracing
= getgrnam(grp_name
);
217 /* If grp_tracing is NULL, the group does not exist. */
221 /* Get number of supplementary group IDs */
222 grp_list_size
= getgroups(0, NULL
);
223 if (grp_list_size
< 0) {
228 /* Alloc group list of the right size */
229 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
234 grp_id
= getgroups(grp_list_size
, grp_list
);
240 for (i
= 0; i
< grp_list_size
; i
++) {
241 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
255 * Try connect to session daemon with sock_path.
257 * Return 0 on success, else -1
259 static int try_connect_sessiond(const char *sock_path
)
263 /* If socket exist, we check if the daemon listens for connect. */
264 ret
= access(sock_path
, F_OK
);
270 ret
= lttcomm_connect_unix_sock(sock_path
);
276 ret
= lttcomm_close_unix_sock(ret
);
278 PERROR("lttcomm_close_unix_sock");
288 * Set sessiond socket path by putting it in the global sessiond_sock_path
291 * Returns 0 on success, negative value on failure (the sessiond socket path
292 * is somehow too long or ENOMEM).
294 static int set_session_daemon_path(void)
296 int in_tgroup
= 0; /* In tracing group. */
302 /* Are we in the tracing group ? */
303 in_tgroup
= lttng_check_tracing_group();
306 if ((uid
== 0) || in_tgroup
) {
307 lttng_ctl_copy_string(sessiond_sock_path
,
308 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
316 ret
= try_connect_sessiond(sessiond_sock_path
);
320 /* Global session daemon not available... */
322 /* ...or not in tracing group (and not root), default */
325 * With GNU C < 2.1, snprintf returns -1 if the target buffer
327 * With GNU C >= 2.1, snprintf returns the required size
328 * (excluding closing null)
330 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
331 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
332 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
344 * Connect to the LTTng session daemon.
346 * On success, return 0. On error, return -1.
348 static int connect_sessiond(void)
352 /* Don't try to connect if already connected. */
357 ret
= set_session_daemon_path();
362 /* Connect to the sesssion daemon. */
363 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
368 sessiond_socket
= ret
;
378 * Clean disconnect from the session daemon.
380 * On success, return 0. On error, return -1.
382 static int disconnect_sessiond(void)
387 ret
= lttcomm_close_unix_sock(sessiond_socket
);
396 * Ask the session daemon a specific command and put the data into buf.
397 * Takes extra var. len. data as input to send to the session daemon.
399 * Return size of data (only payload, not header) or a negative error code.
402 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
403 void *vardata
, size_t varlen
, void **buf
)
408 struct lttcomm_lttng_msg llm
;
410 ret
= connect_sessiond();
412 ret
= -LTTNG_ERR_NO_SESSIOND
;
416 /* Send command to session daemon */
417 ret
= send_session_msg(lsm
);
419 /* Ret value is a valid lttng error code. */
422 /* Send var len data */
423 ret
= send_session_varlen(vardata
, varlen
);
425 /* Ret value is a valid lttng error code. */
429 /* Get header from data transmission */
430 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
432 /* Ret value is a valid lttng error code. */
436 /* Check error code if OK */
437 if (llm
.ret_code
!= LTTNG_OK
) {
442 size
= llm
.data_size
;
444 /* If client free with size 0 */
452 data
= zmalloc(size
);
458 /* Get payload data */
459 ret
= recv_data_sessiond(data
, size
);
466 * Extra protection not to dereference a NULL pointer. If buf is NULL at
467 * this point, an error is returned and data is freed.
470 ret
= -LTTNG_ERR_INVALID
;
479 disconnect_sessiond();
484 * Create lttng handle and return pointer.
486 * The returned pointer will be NULL in case of malloc() error.
488 struct lttng_handle
*lttng_create_handle(const char *session_name
,
489 struct lttng_domain
*domain
)
491 struct lttng_handle
*handle
= NULL
;
493 if (domain
== NULL
) {
497 handle
= zmalloc(sizeof(struct lttng_handle
));
498 if (handle
== NULL
) {
499 PERROR("malloc handle");
503 /* Copy session name */
504 lttng_ctl_copy_string(handle
->session_name
, session_name
,
505 sizeof(handle
->session_name
));
507 /* Copy lttng domain */
508 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
515 * Destroy handle by free(3) the pointer.
517 void lttng_destroy_handle(struct lttng_handle
*handle
)
523 * Register an outside consumer.
525 * Returns size of returned session payload data or a negative error code.
527 int lttng_register_consumer(struct lttng_handle
*handle
,
528 const char *socket_path
)
530 struct lttcomm_session_msg lsm
;
532 if (handle
== NULL
|| socket_path
== NULL
) {
533 return -LTTNG_ERR_INVALID
;
536 memset(&lsm
, 0, sizeof(lsm
));
537 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
538 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
539 sizeof(lsm
.session
.name
));
540 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
542 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
543 sizeof(lsm
.u
.reg
.path
));
545 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
549 * Start tracing for all traces of the session.
551 * Returns size of returned session payload data or a negative error code.
553 int lttng_start_tracing(const char *session_name
)
555 struct lttcomm_session_msg lsm
;
557 if (session_name
== NULL
) {
558 return -LTTNG_ERR_INVALID
;
561 memset(&lsm
, 0, sizeof(lsm
));
562 lsm
.cmd_type
= LTTNG_START_TRACE
;
564 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
565 sizeof(lsm
.session
.name
));
567 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
571 * Stop tracing for all traces of the session.
573 static int _lttng_stop_tracing(const char *session_name
, int wait
)
576 struct lttcomm_session_msg lsm
;
578 if (session_name
== NULL
) {
579 return -LTTNG_ERR_INVALID
;
582 memset(&lsm
, 0, sizeof(lsm
));
583 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
585 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
586 sizeof(lsm
.session
.name
));
588 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
589 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
597 /* Check for data availability */
599 data_ret
= lttng_data_pending(session_name
);
601 /* Return the data available call error. */
607 * Data sleep time before retrying (in usec). Don't sleep if the
608 * call returned value indicates availability.
611 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
613 } while (data_ret
!= 0);
621 * Stop tracing and wait for data availability.
623 int lttng_stop_tracing(const char *session_name
)
625 return _lttng_stop_tracing(session_name
, 1);
629 * Stop tracing but _don't_ wait for data availability.
631 int lttng_stop_tracing_no_wait(const char *session_name
)
633 return _lttng_stop_tracing(session_name
, 0);
637 * Add context to a channel.
639 * If the given channel is NULL, add the contexts to all channels.
640 * The event_name param is ignored.
642 * Returns the size of the returned payload data or a negative error code.
644 int lttng_add_context(struct lttng_handle
*handle
,
645 struct lttng_event_context
*ctx
, const char *event_name
,
646 const char *channel_name
)
651 struct lttcomm_session_msg lsm
;
653 /* Safety check. Both are mandatory. */
654 if (handle
== NULL
|| ctx
== NULL
) {
655 ret
= -LTTNG_ERR_INVALID
;
659 memset(&lsm
, 0, sizeof(lsm
));
660 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
662 /* If no channel name, send empty string. */
663 if (channel_name
== NULL
) {
664 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
665 sizeof(lsm
.u
.context
.channel_name
));
667 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
668 sizeof(lsm
.u
.context
.channel_name
));
671 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
672 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
673 sizeof(lsm
.session
.name
));
675 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
676 size_t provider_len
, ctx_len
;
677 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
678 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
680 if (!provider_name
|| !ctx_name
) {
681 ret
= -LTTNG_ERR_INVALID
;
685 provider_len
= strlen(provider_name
);
686 if (provider_len
== 0) {
687 ret
= -LTTNG_ERR_INVALID
;
690 lsm
.u
.context
.provider_name_len
= provider_len
;
692 ctx_len
= strlen(ctx_name
);
694 ret
= -LTTNG_ERR_INVALID
;
697 lsm
.u
.context
.context_name_len
= ctx_len
;
699 len
= provider_len
+ ctx_len
;
702 ret
= -LTTNG_ERR_NOMEM
;
706 memcpy(buf
, provider_name
, provider_len
);
707 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
709 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
710 /* Don't leak application addresses to the sessiond. */
711 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
712 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
714 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, buf
, len
, NULL
);
721 * Enable event(s) for a channel.
723 * If no event name is specified, all events are enabled.
724 * If no channel name is specified, the default 'channel0' is used.
726 * Returns size of returned session payload data or a negative error code.
728 int lttng_enable_event(struct lttng_handle
*handle
,
729 struct lttng_event
*ev
, const char *channel_name
)
731 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
736 * Create or enable an event with a filter expression.
738 * Return negative error value on error.
739 * Return size of returned session payload data if OK.
741 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
742 struct lttng_event
*event
, const char *channel_name
,
743 const char *filter_expression
)
745 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
746 filter_expression
, 0, NULL
);
750 * Depending on the event, return a newly allocated agent filter expression or
751 * NULL if not applicable.
753 * An event with NO loglevel and the name is * will return NULL.
755 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
758 char *agent_filter
= NULL
;
762 /* Don't add filter for the '*' event. */
763 if (ev
->name
[0] != '*') {
765 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
768 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
776 /* Add loglevel filtering if any for the JUL domain. */
777 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
780 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
786 if (filter
|| agent_filter
) {
789 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
790 agent_filter
? agent_filter
: filter
, op
,
795 agent_filter
= new_filter
;
797 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
813 * Generate the filter bytecode from a given filter expression string. Put the
814 * newly allocated parser context in ctxp and populate the lsm object with the
817 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
819 static int generate_filter(char *filter_expression
,
820 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
823 struct filter_parser_ctx
*ctx
= NULL
;
826 assert(filter_expression
);
831 * Casting const to non-const, as the underlying function will use it in
834 fmem
= lttng_fmemopen((void *) filter_expression
,
835 strlen(filter_expression
), "r");
837 fprintf(stderr
, "Error opening memory as stream\n");
838 ret
= -LTTNG_ERR_FILTER_NOMEM
;
841 ctx
= filter_parser_ctx_alloc(fmem
);
843 fprintf(stderr
, "Error allocating parser\n");
844 ret
= -LTTNG_ERR_FILTER_NOMEM
;
845 goto filter_alloc_error
;
847 ret
= filter_parser_ctx_append_ast(ctx
);
849 fprintf(stderr
, "Parse error\n");
850 ret
= -LTTNG_ERR_FILTER_INVAL
;
853 ret
= filter_visitor_set_parent(ctx
);
855 fprintf(stderr
, "Set parent error\n");
856 ret
= -LTTNG_ERR_FILTER_INVAL
;
860 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
863 fprintf(stderr
, "XML print error\n");
864 ret
= -LTTNG_ERR_FILTER_INVAL
;
869 dbg_printf("Generating IR... ");
871 ret
= filter_visitor_ir_generate(ctx
);
873 fprintf(stderr
, "Generate IR error\n");
874 ret
= -LTTNG_ERR_FILTER_INVAL
;
877 dbg_printf("done\n");
879 dbg_printf("Validating IR... ");
881 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
883 ret
= -LTTNG_ERR_FILTER_INVAL
;
886 /* Validate strings used as literals in the expression. */
887 ret
= filter_visitor_ir_validate_string(ctx
);
889 ret
= -LTTNG_ERR_FILTER_INVAL
;
892 dbg_printf("done\n");
894 dbg_printf("Generating bytecode... ");
896 ret
= filter_visitor_bytecode_generate(ctx
);
898 fprintf(stderr
, "Generate bytecode error\n");
899 ret
= -LTTNG_ERR_FILTER_INVAL
;
902 dbg_printf("done\n");
903 dbg_printf("Size of bytecode generated: %u bytes.\n",
904 bytecode_get_len(&ctx
->bytecode
->b
));
906 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
907 + bytecode_get_len(&ctx
->bytecode
->b
);
908 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
910 /* No need to keep the memory stream. */
911 if (fclose(fmem
) != 0) {
920 filter_parser_ctx_free(ctx
);
922 if (fclose(fmem
) != 0) {
930 * Enable event(s) for a channel, possibly with exclusions and a filter.
931 * If no event name is specified, all events are enabled.
932 * If no channel name is specified, the default name is used.
933 * If filter expression is not NULL, the filter is set for the event.
934 * If exclusion count is not zero, the exclusions are set for the event.
935 * Returns size of returned session payload data or a negative error code.
937 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
938 struct lttng_event
*ev
, const char *channel_name
,
939 const char *original_filter_expression
,
940 int exclusion_count
, char **exclusion_list
)
942 struct lttcomm_session_msg lsm
;
945 unsigned int free_filter_expression
= 0;
946 struct filter_parser_ctx
*ctx
= NULL
;
948 * Cast as non-const since we may replace the filter expression
949 * by a dynamically allocated string. Otherwise, the original
950 * string is not modified.
952 char *filter_expression
= (char *) original_filter_expression
;
954 if (handle
== NULL
|| ev
== NULL
) {
955 ret
= -LTTNG_ERR_INVALID
;
960 * Empty filter string will always be rejected by the parser
961 * anyway, so treat this corner-case early to eliminate
962 * lttng_fmemopen error for 0-byte allocation.
964 if (filter_expression
&& filter_expression
[0] == '\0') {
965 ret
= -LTTNG_ERR_INVALID
;
969 memset(&lsm
, 0, sizeof(lsm
));
971 /* If no channel name, send empty string. */
972 if (channel_name
== NULL
) {
973 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
974 sizeof(lsm
.u
.enable
.channel_name
));
976 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
977 sizeof(lsm
.u
.enable
.channel_name
));
980 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
981 if (ev
->name
[0] == '\0') {
982 /* Enable all events */
983 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
986 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
987 /* FIXME: copying non-packed struct to packed struct. */
988 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
990 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
991 sizeof(lsm
.session
.name
));
992 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
993 lsm
.u
.enable
.bytecode_len
= 0;
996 * For the JUL domain, a filter is enforced except for the enable all
997 * event. This is done to avoid having the event in all sessions thus
998 * filtering by logger name.
1000 if (exclusion_count
== 0 && filter_expression
== NULL
&&
1001 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1002 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1003 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1008 * We have either a filter or some exclusions, so we need to set up
1009 * a variable-length memory block from where to send the data.
1012 /* Parse filter expression. */
1013 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1014 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1015 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1016 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1017 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1018 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1021 /* Setup JUL filter if needed. */
1022 agent_filter
= set_agent_filter(filter_expression
, ev
);
1023 if (!agent_filter
) {
1024 if (!filter_expression
) {
1026 * No JUL and no filter, just skip
1033 * With an agent filter, the original filter has
1034 * been added to it thus replace the filter
1037 filter_expression
= agent_filter
;
1038 free_filter_expression
= 1;
1042 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1048 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
1049 + lsm
.u
.enable
.expression_len
1050 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1052 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1056 /* Put exclusion names first in the data. */
1057 while (exclusion_count
--) {
1058 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1059 *(exclusion_list
+ exclusion_count
),
1060 LTTNG_SYMBOL_NAME_LEN
- 1);
1062 /* Add filter expression next. */
1063 if (lsm
.u
.enable
.expression_len
!= 0) {
1065 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1067 lsm
.u
.enable
.expression_len
);
1069 /* Add filter bytecode next. */
1070 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1072 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1073 + lsm
.u
.enable
.expression_len
,
1075 lsm
.u
.enable
.bytecode_len
);
1078 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, varlen_data
,
1079 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1080 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
,
1085 if (filter_expression
&& ctx
) {
1086 filter_bytecode_free(ctx
);
1087 filter_ir_free(ctx
);
1088 filter_parser_ctx_free(ctx
);
1091 if (free_filter_expression
) {
1093 * The filter expression has been replaced and must be freed as
1094 * it is not the original filter expression received as a
1097 free(filter_expression
);
1101 * Return directly to the caller and don't ask the sessiond since
1102 * something went wrong in the parsing of data above.
1107 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1111 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1112 struct lttng_event
*ev
, const char *channel_name
,
1113 const char *original_filter_expression
)
1115 struct lttcomm_session_msg lsm
;
1118 unsigned int free_filter_expression
= 0;
1119 struct filter_parser_ctx
*ctx
= NULL
;
1121 * Cast as non-const since we may replace the filter expression
1122 * by a dynamically allocated string. Otherwise, the original
1123 * string is not modified.
1125 char *filter_expression
= (char *) original_filter_expression
;
1127 if (handle
== NULL
|| ev
== NULL
) {
1128 ret
= -LTTNG_ERR_INVALID
;
1133 * Empty filter string will always be rejected by the parser
1134 * anyway, so treat this corner-case early to eliminate
1135 * lttng_fmemopen error for 0-byte allocation.
1137 if (filter_expression
&& filter_expression
[0] == '\0') {
1138 ret
= -LTTNG_ERR_INVALID
;
1142 memset(&lsm
, 0, sizeof(lsm
));
1144 /* If no channel name, send empty string. */
1145 if (channel_name
== NULL
) {
1146 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1147 sizeof(lsm
.u
.disable
.channel_name
));
1149 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1150 sizeof(lsm
.u
.disable
.channel_name
));
1153 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1155 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1156 /* FIXME: copying non-packed struct to packed struct. */
1157 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1159 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1160 sizeof(lsm
.session
.name
));
1161 lsm
.u
.disable
.bytecode_len
= 0;
1164 * For the JUL domain, a filter is enforced except for the
1165 * disable all event. This is done to avoid having the event in
1166 * all sessions thus filtering by logger name.
1168 if (filter_expression
== NULL
&&
1169 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1170 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1171 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1176 * We have a filter, so we need to set up a variable-length
1177 * memory block from where to send the data.
1180 /* Parse filter expression */
1181 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1182 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1183 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1184 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1185 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1186 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1189 /* Setup JUL filter if needed. */
1190 agent_filter
= set_agent_filter(filter_expression
, ev
);
1191 if (!agent_filter
) {
1192 if (!filter_expression
) {
1194 * No JUL and no filter, just skip
1201 * With a JUL filter, the original filter has
1202 * been added to it thus replace the filter
1205 filter_expression
= agent_filter
;
1206 free_filter_expression
= 1;
1210 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1216 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1217 + lsm
.u
.disable
.expression_len
);
1219 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1223 /* Add filter expression. */
1224 if (lsm
.u
.disable
.expression_len
!= 0) {
1227 lsm
.u
.disable
.expression_len
);
1229 /* Add filter bytecode next. */
1230 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1232 + lsm
.u
.disable
.expression_len
,
1234 lsm
.u
.disable
.bytecode_len
);
1237 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, varlen_data
,
1238 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1242 if (filter_expression
&& ctx
) {
1243 filter_bytecode_free(ctx
);
1244 filter_ir_free(ctx
);
1245 filter_parser_ctx_free(ctx
);
1248 if (free_filter_expression
) {
1250 * The filter expression has been replaced and must be freed as
1251 * it is not the original filter expression received as a
1254 free(filter_expression
);
1258 * Return directly to the caller and don't ask the sessiond since
1259 * something went wrong in the parsing of data above.
1264 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1269 * Disable event(s) of a channel and domain.
1270 * If no event name is specified, all events are disabled.
1271 * If no channel name is specified, the default 'channel0' is used.
1272 * Returns size of returned session payload data or a negative error code.
1274 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1275 const char *channel_name
)
1277 struct lttng_event ev
;
1279 memset(&ev
, 0, sizeof(ev
));
1281 ev
.type
= LTTNG_EVENT_ALL
;
1282 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1283 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1287 * Enable channel per domain
1288 * Returns size of returned session payload data or a negative error code.
1290 int lttng_enable_channel(struct lttng_handle
*handle
,
1291 struct lttng_channel
*chan
)
1293 struct lttcomm_session_msg lsm
;
1295 /* NULL arguments are forbidden. No default values. */
1296 if (handle
== NULL
|| chan
== NULL
) {
1297 return -LTTNG_ERR_INVALID
;
1300 memset(&lsm
, 0, sizeof(lsm
));
1302 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1304 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1306 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1308 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1309 sizeof(lsm
.session
.name
));
1311 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1315 * All tracing will be stopped for registered events of the channel.
1316 * Returns size of returned session payload data or a negative error code.
1318 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1320 struct lttcomm_session_msg lsm
;
1322 /* Safety check. Both are mandatory. */
1323 if (handle
== NULL
|| name
== NULL
) {
1324 return -LTTNG_ERR_INVALID
;
1327 memset(&lsm
, 0, sizeof(lsm
));
1329 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1331 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1332 sizeof(lsm
.u
.disable
.channel_name
));
1334 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1336 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1337 sizeof(lsm
.session
.name
));
1339 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1343 * Add PID to session tracker.
1344 * Return 0 on success else a negative LTTng error code.
1346 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1348 struct lttcomm_session_msg lsm
;
1350 /* NULL arguments are forbidden. No default values. */
1351 if (handle
== NULL
) {
1352 return -LTTNG_ERR_INVALID
;
1355 memset(&lsm
, 0, sizeof(lsm
));
1357 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1358 lsm
.u
.pid_tracker
.pid
= pid
;
1360 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1362 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1363 sizeof(lsm
.session
.name
));
1365 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1369 * Remove PID from session tracker.
1370 * Return 0 on success else a negative LTTng error code.
1372 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1374 struct lttcomm_session_msg lsm
;
1376 /* NULL arguments are forbidden. No default values. */
1377 if (handle
== NULL
) {
1378 return -LTTNG_ERR_INVALID
;
1381 memset(&lsm
, 0, sizeof(lsm
));
1383 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1384 lsm
.u
.pid_tracker
.pid
= pid
;
1386 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1388 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1389 sizeof(lsm
.session
.name
));
1391 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1395 * Lists all available tracepoints of domain.
1396 * Sets the contents of the events array.
1397 * Returns the number of lttng_event entries in events;
1398 * on error, returns a negative value.
1400 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1401 struct lttng_event
**events
)
1404 struct lttcomm_session_msg lsm
;
1406 if (handle
== NULL
) {
1407 return -LTTNG_ERR_INVALID
;
1410 memset(&lsm
, 0, sizeof(lsm
));
1411 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1412 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1414 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1419 return ret
/ sizeof(struct lttng_event
);
1423 * Lists all available tracepoint fields of domain.
1424 * Sets the contents of the event field array.
1425 * Returns the number of lttng_event_field entries in events;
1426 * on error, returns a negative value.
1428 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1429 struct lttng_event_field
**fields
)
1432 struct lttcomm_session_msg lsm
;
1434 if (handle
== NULL
) {
1435 return -LTTNG_ERR_INVALID
;
1438 memset(&lsm
, 0, sizeof(lsm
));
1439 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1440 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1442 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1447 return ret
/ sizeof(struct lttng_event_field
);
1451 * Lists all available kernel system calls. Allocates and sets the contents of
1454 * Returns the number of lttng_event entries in events; on error, returns a
1457 int lttng_list_syscalls(struct lttng_event
**events
)
1460 struct lttcomm_session_msg lsm
;
1463 return -LTTNG_ERR_INVALID
;
1466 memset(&lsm
, 0, sizeof(lsm
));
1467 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1468 /* Force kernel domain for system calls. */
1469 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1471 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1476 return ret
/ sizeof(struct lttng_event
);
1480 * Returns a human readable string describing
1481 * the error code (a negative value).
1483 const char *lttng_strerror(int code
)
1485 return error_get_str(code
);
1489 * Create a brand new session using name and url for destination.
1491 * Returns LTTNG_OK on success or a negative error code.
1493 int lttng_create_session(const char *name
, const char *url
)
1497 struct lttcomm_session_msg lsm
;
1498 struct lttng_uri
*uris
= NULL
;
1501 return -LTTNG_ERR_INVALID
;
1504 memset(&lsm
, 0, sizeof(lsm
));
1506 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1507 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1509 /* There should never be a data URL */
1510 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1512 return -LTTNG_ERR_INVALID
;
1515 lsm
.u
.uri
.size
= size
;
1517 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1518 sizeof(struct lttng_uri
) * size
, NULL
);
1525 * Destroy session using name.
1526 * Returns size of returned session payload data or a negative error code.
1528 int lttng_destroy_session(const char *session_name
)
1530 struct lttcomm_session_msg lsm
;
1532 if (session_name
== NULL
) {
1533 return -LTTNG_ERR_INVALID
;
1536 memset(&lsm
, 0, sizeof(lsm
));
1537 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1539 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1540 sizeof(lsm
.session
.name
));
1542 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1546 * Ask the session daemon for all available sessions.
1547 * Sets the contents of the sessions array.
1548 * Returns the number of lttng_session entries in sessions;
1549 * on error, returns a negative value.
1551 int lttng_list_sessions(struct lttng_session
**sessions
)
1554 struct lttcomm_session_msg lsm
;
1556 memset(&lsm
, 0, sizeof(lsm
));
1557 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1558 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1563 return ret
/ sizeof(struct lttng_session
);
1566 int lttng_set_session_shm_path(const char *session_name
,
1567 const char *shm_path
)
1569 struct lttcomm_session_msg lsm
;
1571 if (session_name
== NULL
) {
1572 return -LTTNG_ERR_INVALID
;
1575 memset(&lsm
, 0, sizeof(lsm
));
1576 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1578 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1579 sizeof(lsm
.session
.name
));
1580 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1581 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1583 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1587 * Ask the session daemon for all available domains of a session.
1588 * Sets the contents of the domains array.
1589 * Returns the number of lttng_domain entries in domains;
1590 * on error, returns a negative value.
1592 int lttng_list_domains(const char *session_name
,
1593 struct lttng_domain
**domains
)
1596 struct lttcomm_session_msg lsm
;
1598 if (session_name
== NULL
) {
1599 return -LTTNG_ERR_INVALID
;
1602 memset(&lsm
, 0, sizeof(lsm
));
1603 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1605 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1606 sizeof(lsm
.session
.name
));
1608 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1613 return ret
/ sizeof(struct lttng_domain
);
1617 * Ask the session daemon for all available channels of a session.
1618 * Sets the contents of the channels array.
1619 * Returns the number of lttng_channel entries in channels;
1620 * on error, returns a negative value.
1622 int lttng_list_channels(struct lttng_handle
*handle
,
1623 struct lttng_channel
**channels
)
1626 struct lttcomm_session_msg lsm
;
1628 if (handle
== NULL
) {
1629 return -LTTNG_ERR_INVALID
;
1632 memset(&lsm
, 0, sizeof(lsm
));
1633 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1634 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1635 sizeof(lsm
.session
.name
));
1637 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1639 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1644 return ret
/ sizeof(struct lttng_channel
);
1648 * Ask the session daemon for all available events of a session channel.
1649 * Sets the contents of the events array.
1650 * Returns the number of lttng_event entries in events;
1651 * on error, returns a negative value.
1653 int lttng_list_events(struct lttng_handle
*handle
,
1654 const char *channel_name
, struct lttng_event
**events
)
1657 struct lttcomm_session_msg lsm
;
1659 /* Safety check. An handle and channel name are mandatory */
1660 if (handle
== NULL
|| channel_name
== NULL
) {
1661 return -LTTNG_ERR_INVALID
;
1664 memset(&lsm
, 0, sizeof(lsm
));
1665 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1666 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1667 sizeof(lsm
.session
.name
));
1668 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1669 sizeof(lsm
.u
.list
.channel_name
));
1671 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1673 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) events
);
1678 return ret
/ sizeof(struct lttng_event
);
1682 * Sets the tracing_group variable with name.
1683 * This function allocates memory pointed to by tracing_group.
1684 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1686 int lttng_set_tracing_group(const char *name
)
1689 return -LTTNG_ERR_INVALID
;
1692 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1693 return -LTTNG_ERR_FATAL
;
1700 * Returns size of returned session payload data or a negative error code.
1702 int lttng_calibrate(struct lttng_handle
*handle
,
1703 struct lttng_calibrate
*calibrate
)
1705 struct lttcomm_session_msg lsm
;
1707 /* Safety check. NULL pointer are forbidden */
1708 if (handle
== NULL
|| calibrate
== NULL
) {
1709 return -LTTNG_ERR_INVALID
;
1712 memset(&lsm
, 0, sizeof(lsm
));
1713 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1714 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1716 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1718 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1722 * Set default channel attributes.
1723 * If either or both of the arguments are null, attr content is zeroe'd.
1725 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1726 struct lttng_channel_attr
*attr
)
1729 if (attr
== NULL
|| domain
== NULL
) {
1733 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1735 /* Same for all domains. */
1736 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1737 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1738 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1740 switch (domain
->type
) {
1741 case LTTNG_DOMAIN_KERNEL
:
1742 attr
->switch_timer_interval
=
1743 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1744 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1745 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1746 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1747 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1749 case LTTNG_DOMAIN_UST
:
1750 switch (domain
->buf_type
) {
1751 case LTTNG_BUFFER_PER_UID
:
1752 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1753 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1754 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1755 attr
->switch_timer_interval
=
1756 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1757 attr
->read_timer_interval
=
1758 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1760 case LTTNG_BUFFER_PER_PID
:
1762 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1763 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1764 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1765 attr
->switch_timer_interval
=
1766 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1767 attr
->read_timer_interval
=
1768 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1772 /* Default behavior: leave set to 0. */
1778 * Check if session daemon is alive.
1780 * Return 1 if alive or 0 if not.
1781 * On error returns a negative value.
1783 int lttng_session_daemon_alive(void)
1787 ret
= set_session_daemon_path();
1793 if (*sessiond_sock_path
== '\0') {
1795 * No socket path set. Weird error which means the constructor
1801 ret
= try_connect_sessiond(sessiond_sock_path
);
1812 * Set URL for a consumer for a session and domain.
1814 * Return 0 on success, else a negative value.
1816 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1817 const char *control_url
, const char *data_url
)
1821 struct lttcomm_session_msg lsm
;
1822 struct lttng_uri
*uris
= NULL
;
1824 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1825 return -LTTNG_ERR_INVALID
;
1828 memset(&lsm
, 0, sizeof(lsm
));
1830 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1832 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1833 sizeof(lsm
.session
.name
));
1834 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1836 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
1838 return -LTTNG_ERR_INVALID
;
1841 lsm
.u
.uri
.size
= size
;
1843 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1844 sizeof(struct lttng_uri
) * size
, NULL
);
1853 int lttng_enable_consumer(struct lttng_handle
*handle
)
1861 int lttng_disable_consumer(struct lttng_handle
*handle
)
1867 * This is an extension of create session that is ONLY and SHOULD only be used
1868 * by the lttng command line program. It exists to avoid using URI parsing in
1871 * We need the date and time for the trace path subdirectory for the case where
1872 * the user does NOT define one using either -o or -U. Using the normal
1873 * lttng_create_session API call, we have no clue on the session daemon side if
1874 * the URL was generated automatically by the client or define by the user.
1876 * So this function "wrapper" is hidden from the public API, takes the datetime
1877 * string and appends it if necessary to the URI subdirectory before sending it
1878 * to the session daemon.
1880 * With this extra function, the lttng_create_session call behavior is not
1881 * changed and the timestamp is appended to the URI on the session daemon side
1884 int _lttng_create_session_ext(const char *name
, const char *url
,
1885 const char *datetime
)
1889 struct lttcomm_session_msg lsm
;
1890 struct lttng_uri
*uris
= NULL
;
1892 if (name
== NULL
|| datetime
== NULL
) {
1893 return -LTTNG_ERR_INVALID
;
1896 memset(&lsm
, 0, sizeof(lsm
));
1898 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1899 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1901 /* There should never be a data URL. */
1902 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1904 ret
= -LTTNG_ERR_INVALID
;
1908 lsm
.u
.uri
.size
= size
;
1910 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1911 /* Don't append datetime if the name was automatically created. */
1912 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1913 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1914 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1917 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1920 PERROR("snprintf uri subdir");
1921 ret
= -LTTNG_ERR_FATAL
;
1926 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1927 sizeof(struct lttng_uri
) * size
, NULL
);
1935 * For a given session name, this call checks if the data is ready to be read
1936 * or is still being extracted by the consumer(s) hence not ready to be used by
1939 int lttng_data_pending(const char *session_name
)
1942 struct lttcomm_session_msg lsm
;
1943 uint8_t *pending
= NULL
;
1945 if (session_name
== NULL
) {
1946 return -LTTNG_ERR_INVALID
;
1949 memset(&lsm
, 0, sizeof(lsm
));
1950 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1952 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1953 sizeof(lsm
.session
.name
));
1955 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
1958 } else if (ret
!= 1) {
1959 /* Unexpected payload size */
1960 ret
= -LTTNG_ERR_INVALID
;
1964 ret
= (int) *pending
;
1971 * Create a session exclusively used for snapshot.
1973 * Returns LTTNG_OK on success or a negative error code.
1975 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1979 struct lttcomm_session_msg lsm
;
1980 struct lttng_uri
*uris
= NULL
;
1983 return -LTTNG_ERR_INVALID
;
1986 memset(&lsm
, 0, sizeof(lsm
));
1988 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
1989 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1991 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1993 return -LTTNG_ERR_INVALID
;
1996 lsm
.u
.uri
.size
= size
;
1998 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1999 sizeof(struct lttng_uri
) * size
, NULL
);
2006 * Create a session exclusively used for live.
2008 * Returns LTTNG_OK on success or a negative error code.
2010 int lttng_create_session_live(const char *name
, const char *url
,
2011 unsigned int timer_interval
)
2015 struct lttcomm_session_msg lsm
;
2016 struct lttng_uri
*uris
= NULL
;
2018 if (name
== NULL
|| timer_interval
== 0) {
2019 return -LTTNG_ERR_INVALID
;
2022 memset(&lsm
, 0, sizeof(lsm
));
2024 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
2025 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2028 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2030 ret
= -LTTNG_ERR_INVALID
;
2034 /* file:// is not accepted for live session. */
2035 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2036 ret
= -LTTNG_ERR_INVALID
;
2043 lsm
.u
.session_live
.nb_uri
= size
;
2044 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2046 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
2047 sizeof(struct lttng_uri
) * size
, NULL
);
2055 * List PIDs in the tracker.
2057 * enabled is set to whether the PID tracker is enabled.
2058 * pids is set to an allocated array of PIDs currently tracked. On
2059 * success, pids must be freed by the caller.
2060 * nr_pids is set to the number of entries contained by the pids array.
2062 * Returns 0 on success, else a negative LTTng error code.
2064 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2065 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2069 struct lttcomm_session_msg lsm
;
2073 if (handle
== NULL
) {
2074 return -LTTNG_ERR_INVALID
;
2077 memset(&lsm
, 0, sizeof(lsm
));
2078 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2079 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2080 sizeof(lsm
.session
.name
));
2081 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2083 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2087 nr_pids
= ret
/ sizeof(int32_t);
2088 if (nr_pids
== 1 && pids
[0] == -1) {
2094 *_enabled
= enabled
;
2096 *_nr_pids
= nr_pids
;
2103 static void __attribute__((constructor
)) init()
2105 /* Set default session group */
2106 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2112 static void __attribute__((destructor
)) lttng_ctl_exit()
2114 free(tracing_group
);