4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <common/common.h>
32 #include <common/defaults.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/uri.h>
35 #include <common/utils.h>
36 #include <lttng/lttng.h>
38 #include "filter/filter-ast.h"
39 #include "filter/filter-parser.h"
40 #include "filter/filter-bytecode.h"
41 #include "filter/memstream.h"
42 #include "lttng-ctl-helper.h"
45 static const int print_xml
= 1;
46 #define dbg_printf(fmt, args...) \
47 printf("[debug liblttng-ctl] " fmt, ## args)
49 static const int print_xml
= 0;
50 #define dbg_printf(fmt, args...) \
52 /* do nothing but check printf format */ \
54 printf("[debug liblttnctl] " fmt, ## args); \
59 /* Socket to session daemon for communication */
60 static int sessiond_socket
;
61 static char sessiond_sock_path
[PATH_MAX
];
62 static char health_sock_path
[PATH_MAX
];
65 static char *tracing_group
;
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
76 int lttng_opt_verbose
;
79 * Copy string from src to dst and enforce null terminated byte.
82 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
85 strncpy(dst
, src
, len
);
86 /* Enforce the NULL terminated byte */
94 * Copy domain to lttcomm_session_msg domain.
96 * If domain is unknown, default domain will be the kernel.
99 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
100 struct lttng_domain
*src
)
104 case LTTNG_DOMAIN_KERNEL
:
105 case LTTNG_DOMAIN_UST
:
106 memcpy(dst
, src
, sizeof(struct lttng_domain
));
109 memset(dst
, 0, sizeof(struct lttng_domain
));
116 * Send lttcomm_session_msg to the session daemon.
118 * On success, returns the number of bytes sent (>=0)
119 * On error, returns -1
121 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
126 ret
= -LTTNG_ERR_NO_SESSIOND
;
130 DBG("LSM cmd type : %d", lsm
->cmd_type
);
132 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
133 sizeof(struct lttcomm_session_msg
));
135 ret
= -LTTNG_ERR_FATAL
;
143 * Send var len data to the session daemon.
145 * On success, returns the number of bytes sent (>=0)
146 * On error, returns -1
148 static int send_session_varlen(void *data
, size_t len
)
153 ret
= -LTTNG_ERR_NO_SESSIOND
;
162 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
164 ret
= -LTTNG_ERR_FATAL
;
172 * Receive data from the sessiond socket.
174 * On success, returns the number of bytes received (>=0)
175 * On error, returns -1 (recvmsg() error) or -ENOTCONN
177 static int recv_data_sessiond(void *buf
, size_t len
)
182 ret
= -LTTNG_ERR_NO_SESSIOND
;
186 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
188 ret
= -LTTNG_ERR_FATAL
;
196 * Check if we are in the specified group.
198 * If yes return 1, else return -1.
200 static int check_tracing_group(const char *grp_name
)
202 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
204 int grp_list_size
, grp_id
, i
;
207 /* Get GID of group 'tracing' */
208 grp_tracing
= getgrnam(grp_name
);
210 /* If grp_tracing is NULL, the group does not exist. */
214 /* Get number of supplementary group IDs */
215 grp_list_size
= getgroups(0, NULL
);
216 if (grp_list_size
< 0) {
221 /* Alloc group list of the right size */
222 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
227 grp_id
= getgroups(grp_list_size
, grp_list
);
233 for (i
= 0; i
< grp_list_size
; i
++) {
234 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
248 * Try connect to session daemon with sock_path.
250 * Return 0 on success, else -1
252 static int try_connect_sessiond(const char *sock_path
)
256 /* If socket exist, we check if the daemon listens for connect. */
257 ret
= access(sock_path
, F_OK
);
263 ret
= lttcomm_connect_unix_sock(sock_path
);
269 ret
= lttcomm_close_unix_sock(ret
);
271 perror("lttcomm_close_unix_sock");
281 * Set sessiond socket path by putting it in the global sessiond_sock_path
284 * Returns 0 on success, negative value on failure (the sessiond socket path
285 * is somehow too long or ENOMEM).
287 static int set_session_daemon_path(void)
289 int in_tgroup
= 0; /* In tracing group */
295 /* Are we in the tracing group ? */
296 in_tgroup
= check_tracing_group(tracing_group
);
299 if ((uid
== 0) || in_tgroup
) {
300 lttng_ctl_copy_string(sessiond_sock_path
,
301 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
309 ret
= try_connect_sessiond(sessiond_sock_path
);
313 /* Global session daemon not available... */
315 /* ...or not in tracing group (and not root), default */
318 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
319 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
321 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
322 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
323 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
335 * Connect to the LTTng session daemon.
337 * On success, return 0. On error, return -1.
339 static int connect_sessiond(void)
343 ret
= set_session_daemon_path();
348 /* Connect to the sesssion daemon */
349 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
354 sessiond_socket
= ret
;
364 * Clean disconnect from the session daemon.
365 * On success, return 0. On error, return -1.
367 static int disconnect_sessiond(void)
372 ret
= lttcomm_close_unix_sock(sessiond_socket
);
381 * Ask the session daemon a specific command and put the data into buf.
382 * Takes extra var. len. data as input to send to the session daemon.
384 * Return size of data (only payload, not header) or a negative error code.
387 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
388 void *vardata
, size_t varlen
, void **buf
)
393 struct lttcomm_lttng_msg llm
;
395 ret
= connect_sessiond();
397 ret
= -LTTNG_ERR_NO_SESSIOND
;
401 /* Send command to session daemon */
402 ret
= send_session_msg(lsm
);
404 /* Ret value is a valid lttng error code. */
407 /* Send var len data */
408 ret
= send_session_varlen(vardata
, varlen
);
410 /* Ret value is a valid lttng error code. */
414 /* Get header from data transmission */
415 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
417 /* Ret value is a valid lttng error code. */
421 /* Check error code if OK */
422 if (llm
.ret_code
!= LTTNG_OK
) {
427 size
= llm
.data_size
;
429 /* If client free with size 0 */
437 data
= (void*) malloc(size
);
439 /* Get payload data */
440 ret
= recv_data_sessiond(data
, size
);
447 * Extra protection not to dereference a NULL pointer. If buf is NULL at
448 * this point, an error is returned and data is freed.
451 ret
= -LTTNG_ERR_INVALID
;
460 disconnect_sessiond();
465 * Create lttng handle and return pointer.
466 * The returned pointer will be NULL in case of malloc() error.
468 struct lttng_handle
*lttng_create_handle(const char *session_name
,
469 struct lttng_domain
*domain
)
471 struct lttng_handle
*handle
= NULL
;
473 if (domain
== NULL
) {
477 handle
= malloc(sizeof(struct lttng_handle
));
478 if (handle
== NULL
) {
479 PERROR("malloc handle");
483 /* Copy session name */
484 lttng_ctl_copy_string(handle
->session_name
, session_name
,
485 sizeof(handle
->session_name
));
487 /* Copy lttng domain */
488 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
495 * Destroy handle by free(3) the pointer.
497 void lttng_destroy_handle(struct lttng_handle
*handle
)
503 * Register an outside consumer.
504 * Returns size of returned session payload data or a negative error code.
506 int lttng_register_consumer(struct lttng_handle
*handle
,
507 const char *socket_path
)
509 struct lttcomm_session_msg lsm
;
511 if (handle
== NULL
|| socket_path
== NULL
) {
512 return -LTTNG_ERR_INVALID
;
515 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
516 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
517 sizeof(lsm
.session
.name
));
518 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
520 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
522 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
526 * Start tracing for all traces of the session.
527 * Returns size of returned session payload data or a negative error code.
529 int lttng_start_tracing(const char *session_name
)
531 struct lttcomm_session_msg lsm
;
533 if (session_name
== NULL
) {
534 return -LTTNG_ERR_INVALID
;
537 lsm
.cmd_type
= LTTNG_START_TRACE
;
539 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
540 sizeof(lsm
.session
.name
));
542 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
546 * Stop tracing for all traces of the session.
548 static int _lttng_stop_tracing(const char *session_name
, int wait
)
551 struct lttcomm_session_msg lsm
;
553 if (session_name
== NULL
) {
554 return -LTTNG_ERR_INVALID
;
557 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
559 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
560 sizeof(lsm
.session
.name
));
562 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
563 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
571 _MSG("Waiting for data availability");
573 /* Check for data availability */
575 data_ret
= lttng_data_pending(session_name
);
577 /* Return the data available call error. */
583 * Data sleep time before retrying (in usec). Don't sleep if the call
584 * returned value indicates availability.
587 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
590 } while (data_ret
!= 0);
600 * Stop tracing and wait for data availability.
602 int lttng_stop_tracing(const char *session_name
)
604 return _lttng_stop_tracing(session_name
, 1);
608 * Stop tracing but _don't_ wait for data availability.
610 int lttng_stop_tracing_no_wait(const char *session_name
)
612 return _lttng_stop_tracing(session_name
, 0);
616 * Add context to a channel.
618 * If the given channel is NULL, add the contexts to all channels.
619 * The event_name param is ignored.
621 * Returns the size of the returned payload data or a negative error code.
623 int lttng_add_context(struct lttng_handle
*handle
,
624 struct lttng_event_context
*ctx
, const char *event_name
,
625 const char *channel_name
)
627 struct lttcomm_session_msg lsm
;
629 /* Safety check. Both are mandatory */
630 if (handle
== NULL
|| ctx
== NULL
) {
631 return -LTTNG_ERR_INVALID
;
634 memset(&lsm
, 0, sizeof(lsm
));
636 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
638 /* Copy channel name */
639 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
640 sizeof(lsm
.u
.context
.channel_name
));
642 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
644 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
646 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
647 sizeof(lsm
.session
.name
));
649 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
653 * Enable event(s) for a channel.
654 * If no event name is specified, all events are enabled.
655 * If no channel name is specified, the default 'channel0' is used.
656 * Returns size of returned session payload data or a negative error code.
658 int lttng_enable_event(struct lttng_handle
*handle
,
659 struct lttng_event
*ev
, const char *channel_name
)
661 struct lttcomm_session_msg lsm
;
663 if (handle
== NULL
|| ev
== NULL
) {
664 return -LTTNG_ERR_INVALID
;
667 memset(&lsm
, 0, sizeof(lsm
));
669 /* If no channel name, we put the default name */
670 if (channel_name
== NULL
) {
671 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
672 sizeof(lsm
.u
.enable
.channel_name
));
674 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
675 sizeof(lsm
.u
.enable
.channel_name
));
678 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
680 if (ev
->name
[0] != '\0') {
681 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
683 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
685 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
687 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
688 sizeof(lsm
.session
.name
));
690 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
694 * Create or enable an event with a filter expression.
696 * Return negative error value on error.
697 * Return size of returned session payload data if OK.
699 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
700 struct lttng_event
*event
, const char *channel_name
,
701 const char *filter_expression
)
703 struct lttcomm_session_msg lsm
;
704 struct filter_parser_ctx
*ctx
;
708 if (!filter_expression
) {
710 * Fall back to normal event enabling if no filter
713 return lttng_enable_event(handle
, event
, channel_name
);
717 * Empty filter string will always be rejected by the parser
718 * anyway, so treat this corner-case early to eliminate
719 * lttng_fmemopen error for 0-byte allocation.
721 if (handle
== NULL
|| filter_expression
[0] == '\0') {
722 return -LTTNG_ERR_INVALID
;
726 * casting const to non-const, as the underlying function will
727 * use it in read-only mode.
729 fmem
= lttng_fmemopen((void *) filter_expression
,
730 strlen(filter_expression
), "r");
732 fprintf(stderr
, "Error opening memory as stream\n");
733 return -LTTNG_ERR_FILTER_NOMEM
;
735 ctx
= filter_parser_ctx_alloc(fmem
);
737 fprintf(stderr
, "Error allocating parser\n");
738 ret
= -LTTNG_ERR_FILTER_NOMEM
;
741 ret
= filter_parser_ctx_append_ast(ctx
);
743 fprintf(stderr
, "Parse error\n");
744 ret
= -LTTNG_ERR_FILTER_INVAL
;
747 ret
= filter_visitor_set_parent(ctx
);
749 fprintf(stderr
, "Set parent error\n");
750 ret
= -LTTNG_ERR_FILTER_INVAL
;
754 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
757 fprintf(stderr
, "XML print error\n");
758 ret
= -LTTNG_ERR_FILTER_INVAL
;
763 dbg_printf("Generating IR... ");
765 ret
= filter_visitor_ir_generate(ctx
);
767 fprintf(stderr
, "Generate IR error\n");
768 ret
= -LTTNG_ERR_FILTER_INVAL
;
771 dbg_printf("done\n");
773 dbg_printf("Validating IR... ");
775 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
777 ret
= -LTTNG_ERR_FILTER_INVAL
;
780 dbg_printf("done\n");
782 dbg_printf("Generating bytecode... ");
784 ret
= filter_visitor_bytecode_generate(ctx
);
786 fprintf(stderr
, "Generate bytecode error\n");
787 ret
= -LTTNG_ERR_FILTER_INVAL
;
790 dbg_printf("done\n");
791 dbg_printf("Size of bytecode generated: %u bytes.\n",
792 bytecode_get_len(&ctx
->bytecode
->b
));
794 memset(&lsm
, 0, sizeof(lsm
));
796 lsm
.cmd_type
= LTTNG_ENABLE_EVENT_WITH_FILTER
;
798 /* Copy channel name */
799 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
800 sizeof(lsm
.u
.enable
.channel_name
));
801 /* Copy event name */
803 memcpy(&lsm
.u
.enable
.event
, event
, sizeof(lsm
.u
.enable
.event
));
806 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
807 + bytecode_get_len(&ctx
->bytecode
->b
);
809 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
811 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
812 sizeof(lsm
.session
.name
));
814 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
815 lsm
.u
.enable
.bytecode_len
, NULL
);
817 filter_bytecode_free(ctx
);
819 filter_parser_ctx_free(ctx
);
820 if (fclose(fmem
) != 0) {
826 filter_bytecode_free(ctx
);
828 filter_parser_ctx_free(ctx
);
830 if (fclose(fmem
) != 0) {
837 * Disable event(s) of a channel and domain.
838 * If no event name is specified, all events are disabled.
839 * If no channel name is specified, the default 'channel0' is used.
840 * Returns size of returned session payload data or a negative error code.
842 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
843 const char *channel_name
)
845 struct lttcomm_session_msg lsm
;
847 if (handle
== NULL
) {
848 return -LTTNG_ERR_INVALID
;
851 memset(&lsm
, 0, sizeof(lsm
));
854 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
855 sizeof(lsm
.u
.disable
.channel_name
));
857 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
858 sizeof(lsm
.u
.disable
.channel_name
));
861 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
864 lttng_ctl_copy_string(lsm
.u
.disable
.name
, name
,
865 sizeof(lsm
.u
.disable
.name
));
866 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
868 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
871 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
872 sizeof(lsm
.session
.name
));
874 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
878 * Enable channel per domain
879 * Returns size of returned session payload data or a negative error code.
881 int lttng_enable_channel(struct lttng_handle
*handle
,
882 struct lttng_channel
*chan
)
884 struct lttcomm_session_msg lsm
;
887 * NULL arguments are forbidden. No default values.
889 if (handle
== NULL
|| chan
== NULL
) {
890 return -LTTNG_ERR_INVALID
;
893 memset(&lsm
, 0, sizeof(lsm
));
895 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
897 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
899 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
901 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
902 sizeof(lsm
.session
.name
));
904 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
908 * All tracing will be stopped for registered events of the channel.
909 * Returns size of returned session payload data or a negative error code.
911 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
913 struct lttcomm_session_msg lsm
;
915 /* Safety check. Both are mandatory */
916 if (handle
== NULL
|| name
== NULL
) {
917 return -LTTNG_ERR_INVALID
;
920 memset(&lsm
, 0, sizeof(lsm
));
922 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
924 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
925 sizeof(lsm
.u
.disable
.channel_name
));
927 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
929 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
930 sizeof(lsm
.session
.name
));
932 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
936 * Lists all available tracepoints of domain.
937 * Sets the contents of the events array.
938 * Returns the number of lttng_event entries in events;
939 * on error, returns a negative value.
941 int lttng_list_tracepoints(struct lttng_handle
*handle
,
942 struct lttng_event
**events
)
945 struct lttcomm_session_msg lsm
;
947 if (handle
== NULL
) {
948 return -LTTNG_ERR_INVALID
;
951 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
952 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
954 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
959 return ret
/ sizeof(struct lttng_event
);
963 * Lists all available tracepoint fields of domain.
964 * Sets the contents of the event field array.
965 * Returns the number of lttng_event_field entries in events;
966 * on error, returns a negative value.
968 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
969 struct lttng_event_field
**fields
)
972 struct lttcomm_session_msg lsm
;
974 if (handle
== NULL
) {
975 return -LTTNG_ERR_INVALID
;
978 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
979 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
981 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
986 return ret
/ sizeof(struct lttng_event_field
);
990 * Returns a human readable string describing
991 * the error code (a negative value).
993 const char *lttng_strerror(int code
)
995 return error_get_str(code
);
999 * Create a brand new session using name and url for destination.
1001 * Returns LTTNG_OK on success or a negative error code.
1003 int lttng_create_session(const char *name
, const char *url
)
1007 struct lttcomm_session_msg lsm
;
1008 struct lttng_uri
*uris
= NULL
;
1011 return -LTTNG_ERR_INVALID
;
1014 memset(&lsm
, 0, sizeof(lsm
));
1016 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1017 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1019 /* There should never be a data URL */
1020 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1022 return -LTTNG_ERR_INVALID
;
1025 lsm
.u
.uri
.size
= size
;
1027 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1028 sizeof(struct lttng_uri
) * size
, NULL
);
1035 * Destroy session using name.
1036 * Returns size of returned session payload data or a negative error code.
1038 int lttng_destroy_session(const char *session_name
)
1040 struct lttcomm_session_msg lsm
;
1042 if (session_name
== NULL
) {
1043 return -LTTNG_ERR_INVALID
;
1046 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1048 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1049 sizeof(lsm
.session
.name
));
1051 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1055 * Ask the session daemon for all available sessions.
1056 * Sets the contents of the sessions array.
1057 * Returns the number of lttng_session entries in sessions;
1058 * on error, returns a negative value.
1060 int lttng_list_sessions(struct lttng_session
**sessions
)
1063 struct lttcomm_session_msg lsm
;
1065 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1066 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1071 return ret
/ sizeof(struct lttng_session
);
1075 * Ask the session daemon for all available domains of a session.
1076 * Sets the contents of the domains array.
1077 * Returns the number of lttng_domain entries in domains;
1078 * on error, returns a negative value.
1080 int lttng_list_domains(const char *session_name
,
1081 struct lttng_domain
**domains
)
1084 struct lttcomm_session_msg lsm
;
1086 if (session_name
== NULL
) {
1087 return -LTTNG_ERR_INVALID
;
1090 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1092 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1093 sizeof(lsm
.session
.name
));
1095 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1100 return ret
/ sizeof(struct lttng_domain
);
1104 * Ask the session daemon for all available channels of a session.
1105 * Sets the contents of the channels array.
1106 * Returns the number of lttng_channel entries in channels;
1107 * on error, returns a negative value.
1109 int lttng_list_channels(struct lttng_handle
*handle
,
1110 struct lttng_channel
**channels
)
1113 struct lttcomm_session_msg lsm
;
1115 if (handle
== NULL
) {
1116 return -LTTNG_ERR_INVALID
;
1119 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1120 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1121 sizeof(lsm
.session
.name
));
1123 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1125 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1130 return ret
/ sizeof(struct lttng_channel
);
1134 * Ask the session daemon for all available events of a session channel.
1135 * Sets the contents of the events array.
1136 * Returns the number of lttng_event entries in events;
1137 * on error, returns a negative value.
1139 int lttng_list_events(struct lttng_handle
*handle
,
1140 const char *channel_name
, struct lttng_event
**events
)
1143 struct lttcomm_session_msg lsm
;
1145 /* Safety check. An handle and channel name are mandatory */
1146 if (handle
== NULL
|| channel_name
== NULL
) {
1147 return -LTTNG_ERR_INVALID
;
1150 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1151 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1152 sizeof(lsm
.session
.name
));
1153 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1154 sizeof(lsm
.u
.list
.channel_name
));
1156 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1158 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) events
);
1163 return ret
/ sizeof(struct lttng_event
);
1167 * Sets the tracing_group variable with name.
1168 * This function allocates memory pointed to by tracing_group.
1169 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1171 int lttng_set_tracing_group(const char *name
)
1174 return -LTTNG_ERR_INVALID
;
1177 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1178 return -LTTNG_ERR_FATAL
;
1185 * Returns size of returned session payload data or a negative error code.
1187 int lttng_calibrate(struct lttng_handle
*handle
,
1188 struct lttng_calibrate
*calibrate
)
1190 struct lttcomm_session_msg lsm
;
1192 /* Safety check. NULL pointer are forbidden */
1193 if (handle
== NULL
|| calibrate
== NULL
) {
1194 return -LTTNG_ERR_INVALID
;
1197 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1198 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1200 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1202 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1206 * Set default channel attributes.
1207 * If either or both of the arguments are null, attr content is zeroe'd.
1209 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1210 struct lttng_channel_attr
*attr
)
1213 if (attr
== NULL
|| domain
== NULL
) {
1217 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1219 /* Same for all domains. */
1220 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1221 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1222 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1224 switch (domain
->type
) {
1225 case LTTNG_DOMAIN_KERNEL
:
1226 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1227 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1228 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1229 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1230 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1232 case LTTNG_DOMAIN_UST
:
1233 switch (domain
->buf_type
) {
1234 case LTTNG_BUFFER_PER_UID
:
1235 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1236 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1237 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1238 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1239 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1241 case LTTNG_BUFFER_PER_PID
:
1243 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1244 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1245 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1246 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1247 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1251 /* Default behavior: leave set to 0. */
1257 * Check if session daemon is alive.
1259 * Return 1 if alive or 0 if not.
1260 * On error returns a negative value.
1262 int lttng_session_daemon_alive(void)
1266 ret
= set_session_daemon_path();
1272 if (*sessiond_sock_path
== '\0') {
1274 * No socket path set. Weird error which means the constructor was not
1280 ret
= try_connect_sessiond(sessiond_sock_path
);
1291 * Set URL for a consumer for a session and domain.
1293 * Return 0 on success, else a negative value.
1295 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1296 const char *control_url
, const char *data_url
)
1300 struct lttcomm_session_msg lsm
;
1301 struct lttng_uri
*uris
= NULL
;
1303 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1304 return -LTTNG_ERR_INVALID
;
1307 memset(&lsm
, 0, sizeof(lsm
));
1309 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1311 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1312 sizeof(lsm
.session
.name
));
1313 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1315 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
1317 return -LTTNG_ERR_INVALID
;
1320 lsm
.u
.uri
.size
= size
;
1322 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1323 sizeof(struct lttng_uri
) * size
, NULL
);
1332 int lttng_enable_consumer(struct lttng_handle
*handle
)
1340 int lttng_disable_consumer(struct lttng_handle
*handle
)
1346 * Set health socket path by putting it in the global health_sock_path
1349 * Returns 0 on success or assert(0) on ENOMEM.
1351 static int set_health_socket_path(void)
1353 int in_tgroup
= 0; /* In tracing group */
1360 /* Are we in the tracing group ? */
1361 in_tgroup
= check_tracing_group(tracing_group
);
1364 if ((uid
== 0) || in_tgroup
) {
1365 lttng_ctl_copy_string(health_sock_path
,
1366 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
, sizeof(health_sock_path
));
1373 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1374 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1376 home
= utils_get_home_dir();
1378 /* Fallback in /tmp .. */
1382 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1383 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1384 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1385 /* ENOMEM at this point... just kill the control lib. */
1394 * Check session daemon health for a specific health component.
1396 * Return 0 if health is OK or else 1 if BAD.
1398 * Any other negative value is a lttng error code which can be translated with
1401 int lttng_health_check(enum lttng_health_component c
)
1404 struct lttcomm_health_msg msg
;
1405 struct lttcomm_health_data reply
;
1407 /* Connect to the sesssion daemon */
1408 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1410 ret
= -LTTNG_ERR_NO_SESSIOND
;
1414 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1417 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1419 ret
= -LTTNG_ERR_FATAL
;
1423 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1425 ret
= -LTTNG_ERR_FATAL
;
1429 ret
= reply
.ret_code
;
1435 closeret
= close(sock
);
1444 * This is an extension of create session that is ONLY and SHOULD only be used
1445 * by the lttng command line program. It exists to avoid using URI parsing in
1448 * We need the date and time for the trace path subdirectory for the case where
1449 * the user does NOT define one using either -o or -U. Using the normal
1450 * lttng_create_session API call, we have no clue on the session daemon side if
1451 * the URL was generated automatically by the client or define by the user.
1453 * So this function "wrapper" is hidden from the public API, takes the datetime
1454 * string and appends it if necessary to the URI subdirectory before sending it
1455 * to the session daemon.
1457 * With this extra function, the lttng_create_session call behavior is not
1458 * changed and the timestamp is appended to the URI on the session daemon side
1461 int _lttng_create_session_ext(const char *name
, const char *url
,
1462 const char *datetime
)
1466 struct lttcomm_session_msg lsm
;
1467 struct lttng_uri
*uris
= NULL
;
1469 if (name
== NULL
|| datetime
== NULL
|| url
== NULL
) {
1470 return -LTTNG_ERR_INVALID
;
1473 memset(&lsm
, 0, sizeof(lsm
));
1475 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1476 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1478 /* There should never be a data URL */
1479 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1481 ret
= -LTTNG_ERR_INVALID
;
1485 lsm
.u
.uri
.size
= size
;
1487 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1488 /* Don't append datetime if the name was automatically created. */
1489 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1490 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1491 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1494 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1497 PERROR("snprintf uri subdir");
1498 ret
= -LTTNG_ERR_FATAL
;
1503 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1504 sizeof(struct lttng_uri
) * size
, NULL
);
1512 * For a given session name, this call checks if the data is ready to be read
1513 * or is still being extracted by the consumer(s) hence not ready to be used by
1516 int lttng_data_pending(const char *session_name
)
1519 struct lttcomm_session_msg lsm
;
1521 if (session_name
== NULL
) {
1522 return -LTTNG_ERR_INVALID
;
1525 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1527 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1528 sizeof(lsm
.session
.name
));
1530 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1533 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1534 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1535 * that the data is available. Yes it is hackish but for now this is the
1548 static void __attribute__((constructor
)) init()
1550 /* Set default session group */
1551 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1552 /* Set socket for health check */
1553 (void) set_health_socket_path();
1559 static void __attribute__((destructor
)) lttng_ctl_exit()
1561 free(tracing_group
);