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>
37 #include <lttng/health-internal.h>
39 #include "filter/filter-ast.h"
40 #include "filter/filter-parser.h"
41 #include "filter/filter-bytecode.h"
42 #include "filter/memstream.h"
43 #include "lttng-ctl-helper.h"
46 static const int print_xml
= 1;
47 #define dbg_printf(fmt, args...) \
48 printf("[debug liblttng-ctl] " fmt, ## args)
50 static const int print_xml
= 0;
51 #define dbg_printf(fmt, args...) \
53 /* do nothing but check printf format */ \
55 printf("[debug liblttnctl] " fmt, ## args); \
60 /* Socket to session daemon for communication */
61 static int sessiond_socket
;
62 static char sessiond_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.
201 int lttng_check_tracing_group(void)
203 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
205 int grp_list_size
, grp_id
, i
;
207 const char *grp_name
= tracing_group
;
209 /* Get GID of group 'tracing' */
210 grp_tracing
= getgrnam(grp_name
);
212 /* If grp_tracing is NULL, the group does not exist. */
216 /* Get number of supplementary group IDs */
217 grp_list_size
= getgroups(0, NULL
);
218 if (grp_list_size
< 0) {
223 /* Alloc group list of the right size */
224 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
229 grp_id
= getgroups(grp_list_size
, grp_list
);
235 for (i
= 0; i
< grp_list_size
; i
++) {
236 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
250 * Try connect to session daemon with sock_path.
252 * Return 0 on success, else -1
254 static int try_connect_sessiond(const char *sock_path
)
258 /* If socket exist, we check if the daemon listens for connect. */
259 ret
= access(sock_path
, F_OK
);
265 ret
= lttcomm_connect_unix_sock(sock_path
);
271 ret
= lttcomm_close_unix_sock(ret
);
273 perror("lttcomm_close_unix_sock");
283 * Set sessiond socket path by putting it in the global sessiond_sock_path
286 * Returns 0 on success, negative value on failure (the sessiond socket path
287 * is somehow too long or ENOMEM).
289 static int set_session_daemon_path(void)
291 int in_tgroup
= 0; /* In tracing group */
297 /* Are we in the tracing group ? */
298 in_tgroup
= lttng_check_tracing_group();
301 if ((uid
== 0) || in_tgroup
) {
302 lttng_ctl_copy_string(sessiond_sock_path
,
303 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
311 ret
= try_connect_sessiond(sessiond_sock_path
);
315 /* Global session daemon not available... */
317 /* ...or not in tracing group (and not root), default */
320 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
321 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
323 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
324 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
325 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
337 * Connect to the LTTng session daemon.
339 * On success, return 0. On error, return -1.
341 static int connect_sessiond(void)
345 /* Don't try to connect if already connected. */
350 ret
= set_session_daemon_path();
355 /* Connect to the sesssion daemon */
356 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
361 sessiond_socket
= ret
;
371 * Clean disconnect from the session daemon.
372 * On success, return 0. On error, return -1.
374 static int disconnect_sessiond(void)
379 ret
= lttcomm_close_unix_sock(sessiond_socket
);
388 * Ask the session daemon a specific command and put the data into buf.
389 * Takes extra var. len. data as input to send to the session daemon.
391 * Return size of data (only payload, not header) or a negative error code.
394 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
395 void *vardata
, size_t varlen
, void **buf
)
400 struct lttcomm_lttng_msg llm
;
402 ret
= connect_sessiond();
404 ret
= -LTTNG_ERR_NO_SESSIOND
;
408 /* Send command to session daemon */
409 ret
= send_session_msg(lsm
);
411 /* Ret value is a valid lttng error code. */
414 /* Send var len data */
415 ret
= send_session_varlen(vardata
, varlen
);
417 /* Ret value is a valid lttng error code. */
421 /* Get header from data transmission */
422 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
424 /* Ret value is a valid lttng error code. */
428 /* Check error code if OK */
429 if (llm
.ret_code
!= LTTNG_OK
) {
434 size
= llm
.data_size
;
436 /* If client free with size 0 */
444 data
= (void*) malloc(size
);
446 /* Get payload data */
447 ret
= recv_data_sessiond(data
, size
);
454 * Extra protection not to dereference a NULL pointer. If buf is NULL at
455 * this point, an error is returned and data is freed.
458 ret
= -LTTNG_ERR_INVALID
;
467 disconnect_sessiond();
472 * Create lttng handle and return pointer.
473 * The returned pointer will be NULL in case of malloc() error.
475 struct lttng_handle
*lttng_create_handle(const char *session_name
,
476 struct lttng_domain
*domain
)
478 struct lttng_handle
*handle
= NULL
;
480 if (domain
== NULL
) {
484 handle
= malloc(sizeof(struct lttng_handle
));
485 if (handle
== NULL
) {
486 PERROR("malloc handle");
490 /* Copy session name */
491 lttng_ctl_copy_string(handle
->session_name
, session_name
,
492 sizeof(handle
->session_name
));
494 /* Copy lttng domain */
495 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
502 * Destroy handle by free(3) the pointer.
504 void lttng_destroy_handle(struct lttng_handle
*handle
)
510 * Register an outside consumer.
511 * Returns size of returned session payload data or a negative error code.
513 int lttng_register_consumer(struct lttng_handle
*handle
,
514 const char *socket_path
)
516 struct lttcomm_session_msg lsm
;
518 if (handle
== NULL
|| socket_path
== NULL
) {
519 return -LTTNG_ERR_INVALID
;
522 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
523 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
524 sizeof(lsm
.session
.name
));
525 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
527 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
529 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
533 * Start tracing for all traces of the session.
534 * Returns size of returned session payload data or a negative error code.
536 int lttng_start_tracing(const char *session_name
)
538 struct lttcomm_session_msg lsm
;
540 if (session_name
== NULL
) {
541 return -LTTNG_ERR_INVALID
;
544 lsm
.cmd_type
= LTTNG_START_TRACE
;
546 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
547 sizeof(lsm
.session
.name
));
549 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
553 * Stop tracing for all traces of the session.
555 static int _lttng_stop_tracing(const char *session_name
, int wait
)
558 struct lttcomm_session_msg lsm
;
560 if (session_name
== NULL
) {
561 return -LTTNG_ERR_INVALID
;
564 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
566 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
567 sizeof(lsm
.session
.name
));
569 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
570 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
578 _MSG("Waiting for data availability");
581 /* Check for data availability */
583 data_ret
= lttng_data_pending(session_name
);
585 /* Return the data available call error. */
591 * Data sleep time before retrying (in usec). Don't sleep if the call
592 * returned value indicates availability.
595 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
599 } while (data_ret
!= 0);
609 * Stop tracing and wait for data availability.
611 int lttng_stop_tracing(const char *session_name
)
613 return _lttng_stop_tracing(session_name
, 1);
617 * Stop tracing but _don't_ wait for data availability.
619 int lttng_stop_tracing_no_wait(const char *session_name
)
621 return _lttng_stop_tracing(session_name
, 0);
625 * Add context to a channel.
627 * If the given channel is NULL, add the contexts to all channels.
628 * The event_name param is ignored.
630 * Returns the size of the returned payload data or a negative error code.
632 int lttng_add_context(struct lttng_handle
*handle
,
633 struct lttng_event_context
*ctx
, const char *event_name
,
634 const char *channel_name
)
636 struct lttcomm_session_msg lsm
;
638 /* Safety check. Both are mandatory */
639 if (handle
== NULL
|| ctx
== NULL
) {
640 return -LTTNG_ERR_INVALID
;
643 memset(&lsm
, 0, sizeof(lsm
));
645 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
647 /* If no channel name, send empty string. */
648 if (channel_name
== NULL
) {
649 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
650 sizeof(lsm
.u
.context
.channel_name
));
652 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
653 sizeof(lsm
.u
.context
.channel_name
));
656 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
658 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
660 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
661 sizeof(lsm
.session
.name
));
663 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
667 * Enable event(s) for a channel.
668 * If no event name is specified, all events are enabled.
669 * If no channel name is specified, the default 'channel0' is used.
670 * Returns size of returned session payload data or a negative error code.
672 int lttng_enable_event(struct lttng_handle
*handle
,
673 struct lttng_event
*ev
, const char *channel_name
)
675 struct lttcomm_session_msg lsm
;
677 if (handle
== NULL
|| ev
== NULL
) {
678 return -LTTNG_ERR_INVALID
;
681 memset(&lsm
, 0, sizeof(lsm
));
683 /* If no channel name, send empty string. */
684 if (channel_name
== NULL
) {
685 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
686 sizeof(lsm
.u
.enable
.channel_name
));
688 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
689 sizeof(lsm
.u
.enable
.channel_name
));
692 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
694 if (ev
->name
[0] != '\0') {
695 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
697 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
699 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
701 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
702 sizeof(lsm
.session
.name
));
704 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
708 * Create or enable an event with a filter expression.
710 * Return negative error value on error.
711 * Return size of returned session payload data if OK.
713 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
714 struct lttng_event
*event
, const char *channel_name
,
715 const char *filter_expression
)
717 struct lttcomm_session_msg lsm
;
718 struct filter_parser_ctx
*ctx
;
722 if (!filter_expression
) {
724 * Fall back to normal event enabling if no filter
727 return lttng_enable_event(handle
, event
, channel_name
);
731 * Empty filter string will always be rejected by the parser
732 * anyway, so treat this corner-case early to eliminate
733 * lttng_fmemopen error for 0-byte allocation.
735 if (handle
== NULL
|| filter_expression
[0] == '\0') {
736 return -LTTNG_ERR_INVALID
;
740 * casting const to non-const, as the underlying function will
741 * use it in read-only mode.
743 fmem
= lttng_fmemopen((void *) filter_expression
,
744 strlen(filter_expression
), "r");
746 fprintf(stderr
, "Error opening memory as stream\n");
747 return -LTTNG_ERR_FILTER_NOMEM
;
749 ctx
= filter_parser_ctx_alloc(fmem
);
751 fprintf(stderr
, "Error allocating parser\n");
752 ret
= -LTTNG_ERR_FILTER_NOMEM
;
755 ret
= filter_parser_ctx_append_ast(ctx
);
757 fprintf(stderr
, "Parse error\n");
758 ret
= -LTTNG_ERR_FILTER_INVAL
;
761 ret
= filter_visitor_set_parent(ctx
);
763 fprintf(stderr
, "Set parent error\n");
764 ret
= -LTTNG_ERR_FILTER_INVAL
;
768 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
771 fprintf(stderr
, "XML print error\n");
772 ret
= -LTTNG_ERR_FILTER_INVAL
;
777 dbg_printf("Generating IR... ");
779 ret
= filter_visitor_ir_generate(ctx
);
781 fprintf(stderr
, "Generate IR error\n");
782 ret
= -LTTNG_ERR_FILTER_INVAL
;
785 dbg_printf("done\n");
787 dbg_printf("Validating IR... ");
789 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
791 ret
= -LTTNG_ERR_FILTER_INVAL
;
794 dbg_printf("done\n");
796 dbg_printf("Generating bytecode... ");
798 ret
= filter_visitor_bytecode_generate(ctx
);
800 fprintf(stderr
, "Generate bytecode error\n");
801 ret
= -LTTNG_ERR_FILTER_INVAL
;
804 dbg_printf("done\n");
805 dbg_printf("Size of bytecode generated: %u bytes.\n",
806 bytecode_get_len(&ctx
->bytecode
->b
));
808 memset(&lsm
, 0, sizeof(lsm
));
810 lsm
.cmd_type
= LTTNG_ENABLE_EVENT_WITH_FILTER
;
812 /* If no channel name, send empty string. */
813 if (channel_name
== NULL
) {
814 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
815 sizeof(lsm
.u
.enable
.channel_name
));
817 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
818 sizeof(lsm
.u
.enable
.channel_name
));
821 /* Copy event name */
823 memcpy(&lsm
.u
.enable
.event
, event
, sizeof(lsm
.u
.enable
.event
));
826 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
827 + bytecode_get_len(&ctx
->bytecode
->b
);
829 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
831 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
832 sizeof(lsm
.session
.name
));
834 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
835 lsm
.u
.enable
.bytecode_len
, NULL
);
837 filter_bytecode_free(ctx
);
839 filter_parser_ctx_free(ctx
);
840 if (fclose(fmem
) != 0) {
846 filter_bytecode_free(ctx
);
848 filter_parser_ctx_free(ctx
);
850 if (fclose(fmem
) != 0) {
857 * Disable event(s) of a channel and domain.
858 * If no event name is specified, all events are disabled.
859 * If no channel name is specified, the default 'channel0' is used.
860 * Returns size of returned session payload data or a negative error code.
862 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
863 const char *channel_name
)
865 struct lttcomm_session_msg lsm
;
867 if (handle
== NULL
) {
868 return -LTTNG_ERR_INVALID
;
871 memset(&lsm
, 0, sizeof(lsm
));
873 /* If no channel name, send empty string. */
874 if (channel_name
== NULL
) {
875 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
876 sizeof(lsm
.u
.disable
.channel_name
));
878 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
879 sizeof(lsm
.u
.disable
.channel_name
));
882 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
885 lttng_ctl_copy_string(lsm
.u
.disable
.name
, name
,
886 sizeof(lsm
.u
.disable
.name
));
887 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
889 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
892 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
893 sizeof(lsm
.session
.name
));
895 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
899 * Enable channel per domain
900 * Returns size of returned session payload data or a negative error code.
902 int lttng_enable_channel(struct lttng_handle
*handle
,
903 struct lttng_channel
*chan
)
905 struct lttcomm_session_msg lsm
;
908 * NULL arguments are forbidden. No default values.
910 if (handle
== NULL
|| chan
== NULL
) {
911 return -LTTNG_ERR_INVALID
;
914 memset(&lsm
, 0, sizeof(lsm
));
916 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
918 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
920 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
922 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
923 sizeof(lsm
.session
.name
));
925 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
929 * All tracing will be stopped for registered events of the channel.
930 * Returns size of returned session payload data or a negative error code.
932 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
934 struct lttcomm_session_msg lsm
;
936 /* Safety check. Both are mandatory */
937 if (handle
== NULL
|| name
== NULL
) {
938 return -LTTNG_ERR_INVALID
;
941 memset(&lsm
, 0, sizeof(lsm
));
943 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
945 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
946 sizeof(lsm
.u
.disable
.channel_name
));
948 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
950 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
951 sizeof(lsm
.session
.name
));
953 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
957 * Lists all available tracepoints of domain.
958 * Sets the contents of the events array.
959 * Returns the number of lttng_event entries in events;
960 * on error, returns a negative value.
962 int lttng_list_tracepoints(struct lttng_handle
*handle
,
963 struct lttng_event
**events
)
966 struct lttcomm_session_msg lsm
;
968 if (handle
== NULL
) {
969 return -LTTNG_ERR_INVALID
;
972 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
973 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
975 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
980 return ret
/ sizeof(struct lttng_event
);
984 * Lists all available tracepoint fields of domain.
985 * Sets the contents of the event field array.
986 * Returns the number of lttng_event_field entries in events;
987 * on error, returns a negative value.
989 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
990 struct lttng_event_field
**fields
)
993 struct lttcomm_session_msg lsm
;
995 if (handle
== NULL
) {
996 return -LTTNG_ERR_INVALID
;
999 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1000 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1002 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1007 return ret
/ sizeof(struct lttng_event_field
);
1011 * Returns a human readable string describing
1012 * the error code (a negative value).
1014 const char *lttng_strerror(int code
)
1016 return error_get_str(code
);
1020 * Create a brand new session using name and url for destination.
1022 * Returns LTTNG_OK on success or a negative error code.
1024 int lttng_create_session(const char *name
, const char *url
)
1028 struct lttcomm_session_msg lsm
;
1029 struct lttng_uri
*uris
= NULL
;
1032 return -LTTNG_ERR_INVALID
;
1035 memset(&lsm
, 0, sizeof(lsm
));
1037 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1038 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1040 /* There should never be a data URL */
1041 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1043 return -LTTNG_ERR_INVALID
;
1046 lsm
.u
.uri
.size
= size
;
1048 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1049 sizeof(struct lttng_uri
) * size
, NULL
);
1056 * Destroy session using name.
1057 * Returns size of returned session payload data or a negative error code.
1059 int lttng_destroy_session(const char *session_name
)
1061 struct lttcomm_session_msg lsm
;
1063 if (session_name
== NULL
) {
1064 return -LTTNG_ERR_INVALID
;
1067 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1069 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1070 sizeof(lsm
.session
.name
));
1072 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1076 * Ask the session daemon for all available sessions.
1077 * Sets the contents of the sessions array.
1078 * Returns the number of lttng_session entries in sessions;
1079 * on error, returns a negative value.
1081 int lttng_list_sessions(struct lttng_session
**sessions
)
1084 struct lttcomm_session_msg lsm
;
1086 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1087 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1092 return ret
/ sizeof(struct lttng_session
);
1096 * Ask the session daemon for all available domains of a session.
1097 * Sets the contents of the domains array.
1098 * Returns the number of lttng_domain entries in domains;
1099 * on error, returns a negative value.
1101 int lttng_list_domains(const char *session_name
,
1102 struct lttng_domain
**domains
)
1105 struct lttcomm_session_msg lsm
;
1107 if (session_name
== NULL
) {
1108 return -LTTNG_ERR_INVALID
;
1111 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1113 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1114 sizeof(lsm
.session
.name
));
1116 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1121 return ret
/ sizeof(struct lttng_domain
);
1125 * Ask the session daemon for all available channels of a session.
1126 * Sets the contents of the channels array.
1127 * Returns the number of lttng_channel entries in channels;
1128 * on error, returns a negative value.
1130 int lttng_list_channels(struct lttng_handle
*handle
,
1131 struct lttng_channel
**channels
)
1134 struct lttcomm_session_msg lsm
;
1136 if (handle
== NULL
) {
1137 return -LTTNG_ERR_INVALID
;
1140 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1141 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1142 sizeof(lsm
.session
.name
));
1144 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1146 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1151 return ret
/ sizeof(struct lttng_channel
);
1155 * Ask the session daemon for all available events of a session channel.
1156 * Sets the contents of the events array.
1157 * Returns the number of lttng_event entries in events;
1158 * on error, returns a negative value.
1160 int lttng_list_events(struct lttng_handle
*handle
,
1161 const char *channel_name
, struct lttng_event
**events
)
1164 struct lttcomm_session_msg lsm
;
1166 /* Safety check. An handle and channel name are mandatory */
1167 if (handle
== NULL
|| channel_name
== NULL
) {
1168 return -LTTNG_ERR_INVALID
;
1171 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1172 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1173 sizeof(lsm
.session
.name
));
1174 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1175 sizeof(lsm
.u
.list
.channel_name
));
1177 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1179 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) events
);
1184 return ret
/ sizeof(struct lttng_event
);
1188 * Sets the tracing_group variable with name.
1189 * This function allocates memory pointed to by tracing_group.
1190 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1192 int lttng_set_tracing_group(const char *name
)
1195 return -LTTNG_ERR_INVALID
;
1198 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1199 return -LTTNG_ERR_FATAL
;
1206 * Returns size of returned session payload data or a negative error code.
1208 int lttng_calibrate(struct lttng_handle
*handle
,
1209 struct lttng_calibrate
*calibrate
)
1211 struct lttcomm_session_msg lsm
;
1213 /* Safety check. NULL pointer are forbidden */
1214 if (handle
== NULL
|| calibrate
== NULL
) {
1215 return -LTTNG_ERR_INVALID
;
1218 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1219 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1221 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1223 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1227 * Set default channel attributes.
1228 * If either or both of the arguments are null, attr content is zeroe'd.
1230 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1231 struct lttng_channel_attr
*attr
)
1234 if (attr
== NULL
|| domain
== NULL
) {
1238 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1240 /* Same for all domains. */
1241 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1242 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1243 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1245 switch (domain
->type
) {
1246 case LTTNG_DOMAIN_KERNEL
:
1247 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1248 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1249 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1250 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1251 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1253 case LTTNG_DOMAIN_UST
:
1254 switch (domain
->buf_type
) {
1255 case LTTNG_BUFFER_PER_UID
:
1256 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1257 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1258 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1259 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1260 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1262 case LTTNG_BUFFER_PER_PID
:
1264 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1265 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1266 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1267 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1268 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1272 /* Default behavior: leave set to 0. */
1278 * Check if session daemon is alive.
1280 * Return 1 if alive or 0 if not.
1281 * On error returns a negative value.
1283 int lttng_session_daemon_alive(void)
1287 ret
= set_session_daemon_path();
1293 if (*sessiond_sock_path
== '\0') {
1295 * No socket path set. Weird error which means the constructor was not
1301 ret
= try_connect_sessiond(sessiond_sock_path
);
1312 * Set URL for a consumer for a session and domain.
1314 * Return 0 on success, else a negative value.
1316 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1317 const char *control_url
, const char *data_url
)
1321 struct lttcomm_session_msg lsm
;
1322 struct lttng_uri
*uris
= NULL
;
1324 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1325 return -LTTNG_ERR_INVALID
;
1328 memset(&lsm
, 0, sizeof(lsm
));
1330 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1332 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1333 sizeof(lsm
.session
.name
));
1334 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1336 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
1338 return -LTTNG_ERR_INVALID
;
1341 lsm
.u
.uri
.size
= size
;
1343 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1344 sizeof(struct lttng_uri
) * size
, NULL
);
1353 int lttng_enable_consumer(struct lttng_handle
*handle
)
1361 int lttng_disable_consumer(struct lttng_handle
*handle
)
1367 * This is an extension of create session that is ONLY and SHOULD only be used
1368 * by the lttng command line program. It exists to avoid using URI parsing in
1371 * We need the date and time for the trace path subdirectory for the case where
1372 * the user does NOT define one using either -o or -U. Using the normal
1373 * lttng_create_session API call, we have no clue on the session daemon side if
1374 * the URL was generated automatically by the client or define by the user.
1376 * So this function "wrapper" is hidden from the public API, takes the datetime
1377 * string and appends it if necessary to the URI subdirectory before sending it
1378 * to the session daemon.
1380 * With this extra function, the lttng_create_session call behavior is not
1381 * changed and the timestamp is appended to the URI on the session daemon side
1384 int _lttng_create_session_ext(const char *name
, const char *url
,
1385 const char *datetime
)
1389 struct lttcomm_session_msg lsm
;
1390 struct lttng_uri
*uris
= NULL
;
1392 if (name
== NULL
|| datetime
== NULL
) {
1393 return -LTTNG_ERR_INVALID
;
1396 memset(&lsm
, 0, sizeof(lsm
));
1398 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1399 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1401 /* There should never be a data URL */
1402 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1404 ret
= -LTTNG_ERR_INVALID
;
1408 lsm
.u
.uri
.size
= size
;
1410 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1411 /* Don't append datetime if the name was automatically created. */
1412 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1413 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1414 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1417 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1420 PERROR("snprintf uri subdir");
1421 ret
= -LTTNG_ERR_FATAL
;
1426 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1427 sizeof(struct lttng_uri
) * size
, NULL
);
1435 * For a given session name, this call checks if the data is ready to be read
1436 * or is still being extracted by the consumer(s) hence not ready to be used by
1439 int lttng_data_pending(const char *session_name
)
1442 struct lttcomm_session_msg lsm
;
1444 if (session_name
== NULL
) {
1445 return -LTTNG_ERR_INVALID
;
1448 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1450 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1451 sizeof(lsm
.session
.name
));
1453 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1456 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1457 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1458 * that the data is available. Yes it is hackish but for now this is the
1469 * Create a session exclusively used for snapshot.
1471 * Returns LTTNG_OK on success or a negative error code.
1473 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1477 struct lttcomm_session_msg lsm
;
1478 struct lttng_uri
*uris
= NULL
;
1481 return -LTTNG_ERR_INVALID
;
1484 memset(&lsm
, 0, sizeof(lsm
));
1486 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
1487 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1489 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1491 return -LTTNG_ERR_INVALID
;
1494 lsm
.u
.uri
.size
= size
;
1496 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1497 sizeof(struct lttng_uri
) * size
, NULL
);
1504 * Create a session exclusively used for live.
1506 * Returns LTTNG_OK on success or a negative error code.
1508 int lttng_create_session_live(const char *name
, const char *url
,
1509 unsigned int timer_interval
)
1513 struct lttcomm_session_msg lsm
;
1514 struct lttng_uri
*uris
= NULL
;
1517 return -LTTNG_ERR_INVALID
;
1520 memset(&lsm
, 0, sizeof(lsm
));
1522 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
1523 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1525 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1527 ret
= -LTTNG_ERR_INVALID
;
1531 /* file:// is not accepted for live session. */
1532 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1533 ret
= -LTTNG_ERR_INVALID
;
1537 lsm
.u
.session_live
.nb_uri
= size
;
1538 lsm
.u
.session_live
.timer_interval
= timer_interval
;
1540 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1541 sizeof(struct lttng_uri
) * size
, NULL
);
1551 static void __attribute__((constructor
)) init()
1553 /* Set default session group */
1554 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1560 static void __attribute__((destructor
)) lttng_ctl_exit()
1562 free(tracing_group
);