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
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, 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 <lttng/lttng.h>
36 /* Socket to session daemon for communication */
37 static int sessiond_socket
;
38 static char sessiond_sock_path
[PATH_MAX
];
41 static char *tracing_group
;
47 * Those two variables are used by error.h to silent or control the verbosity of
48 * error message. They are global to the library so application linking with it
49 * are able to compile correctly and also control verbosity of the library.
51 * Note that it is *not* possible to silent ERR() and PERROR() macros.
54 int lttng_opt_verbose
;
57 * Copy string from src to dst and enforce null terminated byte.
59 static void copy_string(char *dst
, const char *src
, size_t len
)
62 strncpy(dst
, src
, len
);
63 /* Enforce the NULL terminated byte */
71 * Copy domain to lttcomm_session_msg domain.
73 * If domain is unknown, default domain will be the kernel.
75 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
79 case LTTNG_DOMAIN_KERNEL
:
80 case LTTNG_DOMAIN_UST
:
82 case LTTNG_DOMAIN_UST_EXEC_NAME:
83 case LTTNG_DOMAIN_UST_PID:
84 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
86 memcpy(dst
, src
, sizeof(struct lttng_domain
));
89 memset(dst
, 0, sizeof(struct lttng_domain
));
90 dst
->type
= LTTNG_DOMAIN_KERNEL
;
97 * Send lttcomm_session_msg to the session daemon.
99 * On success, returns the number of bytes sent (>=0)
100 * On error, returns -1
102 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
111 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
112 sizeof(struct lttcomm_session_msg
));
119 * Receive data from the sessiond socket.
121 * On success, returns the number of bytes received (>=0)
122 * On error, returns -1 (recvmsg() error) or -ENOTCONN
124 static int recv_data_sessiond(void *buf
, size_t len
)
133 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
140 * Check if we are in the specified group.
142 * If yes return 1, else return -1.
144 static int check_tracing_group(const char *grp_name
)
146 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
148 int grp_list_size
, grp_id
, i
;
151 /* Get GID of group 'tracing' */
152 grp_tracing
= getgrnam(grp_name
);
154 /* If grp_tracing is NULL, the group does not exist. */
158 /* Get number of supplementary group IDs */
159 grp_list_size
= getgroups(0, NULL
);
160 if (grp_list_size
< 0) {
165 /* Alloc group list of the right size */
166 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
171 grp_id
= getgroups(grp_list_size
, grp_list
);
177 for (i
= 0; i
< grp_list_size
; i
++) {
178 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
192 * Try connect to session daemon with sock_path.
194 * Return 0 on success, else -1
196 static int try_connect_sessiond(const char *sock_path
)
200 /* If socket exist, we check if the daemon listens for connect. */
201 ret
= access(sock_path
, F_OK
);
207 ret
= lttcomm_connect_unix_sock(sock_path
);
213 ret
= lttcomm_close_unix_sock(ret
);
215 perror("lttcomm_close_unix_sock");
222 * Set sessiond socket path by putting it in the global
223 * sessiond_sock_path variable.
224 * Returns 0 on success,
225 * -ENOMEM on failure (the sessiond socket path is somehow too long)
227 static int set_session_daemon_path(void)
230 int in_tgroup
= 0; /* In tracing group */
236 /* Are we in the tracing group ? */
237 in_tgroup
= check_tracing_group(tracing_group
);
240 if ((uid
== 0) || in_tgroup
) {
241 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
242 sizeof(sessiond_sock_path
));
248 ret
= try_connect_sessiond(sessiond_sock_path
);
252 /* Global session daemon not available... */
254 /* ...or not in tracing group (and not root), default */
257 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
258 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
260 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
261 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
262 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
271 * Connect to the LTTng session daemon.
273 * On success, return 0. On error, return -1.
275 static int connect_sessiond(void)
279 ret
= set_session_daemon_path();
281 return -1; /* set_session_daemon_path() returns -ENOMEM */
284 /* Connect to the sesssion daemon */
285 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
290 sessiond_socket
= ret
;
297 * Clean disconnect from the session daemon.
298 * On success, return 0. On error, return -1.
300 static int disconnect_sessiond(void)
305 ret
= lttcomm_close_unix_sock(sessiond_socket
);
314 * Ask the session daemon a specific command and put the data into buf.
316 * Return size of data (only payload, not header) or a negative error code.
318 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
323 struct lttcomm_lttng_msg llm
;
325 ret
= connect_sessiond();
330 /* Send command to session daemon */
331 ret
= send_session_msg(lsm
);
336 /* Get header from data transmission */
337 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
342 /* Check error code if OK */
343 if (llm
.ret_code
!= LTTCOMM_OK
) {
348 size
= llm
.data_size
;
350 /* If client free with size 0 */
358 data
= (void*) malloc(size
);
360 /* Get payload data */
361 ret
= recv_data_sessiond(data
, size
);
368 * Extra protection not to dereference a NULL pointer. If buf is NULL at
369 * this point, an error is returned and data is freed.
381 disconnect_sessiond();
386 * Create lttng handle and return pointer.
387 * The returned pointer will be NULL in case of malloc() error.
389 struct lttng_handle
*lttng_create_handle(const char *session_name
,
390 struct lttng_domain
*domain
)
392 struct lttng_handle
*handle
;
394 handle
= malloc(sizeof(struct lttng_handle
));
395 if (handle
== NULL
) {
396 perror("malloc handle");
400 /* Copy session name */
401 copy_string(handle
->session_name
, session_name
,
402 sizeof(handle
->session_name
));
404 /* Copy lttng domain */
405 copy_lttng_domain(&handle
->domain
, domain
);
412 * Destroy handle by free(3) the pointer.
414 void lttng_destroy_handle(struct lttng_handle
*handle
)
422 * Register an outside consumer.
423 * Returns size of returned session payload data or a negative error code.
425 int lttng_register_consumer(struct lttng_handle
*handle
,
426 const char *socket_path
)
428 struct lttcomm_session_msg lsm
;
430 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
431 copy_string(lsm
.session
.name
, handle
->session_name
,
432 sizeof(lsm
.session
.name
));
433 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
435 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
437 return ask_sessiond(&lsm
, NULL
);
441 * Start tracing for all traces of the session.
442 * Returns size of returned session payload data or a negative error code.
444 int lttng_start_tracing(const char *session_name
)
446 struct lttcomm_session_msg lsm
;
448 if (session_name
== NULL
) {
452 lsm
.cmd_type
= LTTNG_START_TRACE
;
454 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
456 return ask_sessiond(&lsm
, NULL
);
460 * Stop tracing for all traces of the session.
461 * Returns size of returned session payload data or a negative error code.
463 int lttng_stop_tracing(const char *session_name
)
465 struct lttcomm_session_msg lsm
;
467 if (session_name
== NULL
) {
471 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
473 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
475 return ask_sessiond(&lsm
, NULL
);
479 * Add context to event and/or channel.
480 * If event_name is NULL, the context is applied to all events of the channel.
481 * If channel_name is NULL, a lookup of the event's channel is done.
482 * If both are NULL, the context is applied to all events of all channels.
484 * Returns the size of the returned payload data or a negative error code.
486 int lttng_add_context(struct lttng_handle
*handle
,
487 struct lttng_event_context
*ctx
, const char *event_name
,
488 const char *channel_name
)
490 struct lttcomm_session_msg lsm
;
492 /* Safety check. Both are mandatory */
493 if (handle
== NULL
|| ctx
== NULL
) {
497 memset(&lsm
, 0, sizeof(lsm
));
499 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
501 /* Copy channel name */
502 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
503 sizeof(lsm
.u
.context
.channel_name
));
504 /* Copy event name */
505 copy_string(lsm
.u
.context
.event_name
, event_name
,
506 sizeof(lsm
.u
.context
.event_name
));
508 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
510 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
512 copy_string(lsm
.session
.name
, handle
->session_name
,
513 sizeof(lsm
.session
.name
));
515 return ask_sessiond(&lsm
, NULL
);
519 * Enable event(s) for a channel.
520 * If no event name is specified, all events are enabled.
521 * If no channel name is specified, the default 'channel0' is used.
522 * Returns size of returned session payload data or a negative error code.
524 int lttng_enable_event(struct lttng_handle
*handle
,
525 struct lttng_event
*ev
, const char *channel_name
)
527 struct lttcomm_session_msg lsm
;
529 if (handle
== NULL
|| ev
== NULL
) {
533 memset(&lsm
, 0, sizeof(lsm
));
535 /* If no channel name, we put the default name */
536 if (channel_name
== NULL
) {
537 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
538 sizeof(lsm
.u
.enable
.channel_name
));
540 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
541 sizeof(lsm
.u
.enable
.channel_name
));
544 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
546 if (ev
->name
[0] != '\0') {
547 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
549 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
551 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
553 copy_string(lsm
.session
.name
, handle
->session_name
,
554 sizeof(lsm
.session
.name
));
556 return ask_sessiond(&lsm
, NULL
);
560 * Disable event(s) of a channel and domain.
561 * If no event name is specified, all events are disabled.
562 * If no channel name is specified, the default 'channel0' is used.
563 * Returns size of returned session payload data or a negative error code.
565 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
566 const char *channel_name
)
568 struct lttcomm_session_msg lsm
;
570 if (handle
== NULL
) {
574 memset(&lsm
, 0, sizeof(lsm
));
577 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
578 sizeof(lsm
.u
.disable
.channel_name
));
580 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
581 sizeof(lsm
.u
.disable
.channel_name
));
584 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
587 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
588 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
590 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
593 copy_string(lsm
.session
.name
, handle
->session_name
,
594 sizeof(lsm
.session
.name
));
596 return ask_sessiond(&lsm
, NULL
);
600 * Enable channel per domain
601 * Returns size of returned session payload data or a negative error code.
603 int lttng_enable_channel(struct lttng_handle
*handle
,
604 struct lttng_channel
*chan
)
606 struct lttcomm_session_msg lsm
;
609 * NULL arguments are forbidden. No default values.
611 if (handle
== NULL
|| chan
== NULL
) {
615 memset(&lsm
, 0, sizeof(lsm
));
617 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
619 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
621 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
623 copy_string(lsm
.session
.name
, handle
->session_name
,
624 sizeof(lsm
.session
.name
));
626 return ask_sessiond(&lsm
, NULL
);
630 * All tracing will be stopped for registered events of the channel.
631 * Returns size of returned session payload data or a negative error code.
633 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
635 struct lttcomm_session_msg lsm
;
637 /* Safety check. Both are mandatory */
638 if (handle
== NULL
|| name
== NULL
) {
642 memset(&lsm
, 0, sizeof(lsm
));
644 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
646 copy_string(lsm
.u
.disable
.channel_name
, name
,
647 sizeof(lsm
.u
.disable
.channel_name
));
649 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
651 copy_string(lsm
.session
.name
, handle
->session_name
,
652 sizeof(lsm
.session
.name
));
654 return ask_sessiond(&lsm
, NULL
);
658 * Lists all available tracepoints of domain.
659 * Sets the contents of the events array.
660 * Returns the number of lttng_event entries in events;
661 * on error, returns a negative value.
663 int lttng_list_tracepoints(struct lttng_handle
*handle
,
664 struct lttng_event
**events
)
667 struct lttcomm_session_msg lsm
;
669 if (handle
== NULL
) {
673 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
674 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
676 ret
= ask_sessiond(&lsm
, (void **) events
);
681 return ret
/ sizeof(struct lttng_event
);
685 * Returns a human readable string describing
686 * the error code (a negative value).
688 const char *lttng_strerror(int code
)
690 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
691 if (code
> -LTTCOMM_OK
) {
692 return "Ended with errors";
695 return lttcomm_get_readable_code(code
);
699 * Create a brand new session using name and path.
700 * Returns size of returned session payload data or a negative error code.
702 int lttng_create_session(const char *name
, const char *path
)
704 struct lttcomm_session_msg lsm
;
706 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
707 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
708 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
710 return ask_sessiond(&lsm
, NULL
);
714 * Destroy session using name.
715 * Returns size of returned session payload data or a negative error code.
717 int lttng_destroy_session(const char *session_name
)
719 struct lttcomm_session_msg lsm
;
721 if (session_name
== NULL
) {
725 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
727 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
729 return ask_sessiond(&lsm
, NULL
);
733 * Ask the session daemon for all available sessions.
734 * Sets the contents of the sessions array.
735 * Returns the number of lttng_session entries in sessions;
736 * on error, returns a negative value.
738 int lttng_list_sessions(struct lttng_session
**sessions
)
741 struct lttcomm_session_msg lsm
;
743 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
744 ret
= ask_sessiond(&lsm
, (void**) sessions
);
749 return ret
/ sizeof(struct lttng_session
);
753 * Ask the session daemon for all available domains of a session.
754 * Sets the contents of the domains array.
755 * Returns the number of lttng_domain entries in domains;
756 * on error, returns a negative value.
758 int lttng_list_domains(const char *session_name
,
759 struct lttng_domain
**domains
)
762 struct lttcomm_session_msg lsm
;
764 if (session_name
== NULL
) {
768 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
770 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
772 ret
= ask_sessiond(&lsm
, (void**) domains
);
777 return ret
/ sizeof(struct lttng_domain
);
781 * Ask the session daemon for all available channels of a session.
782 * Sets the contents of the channels array.
783 * Returns the number of lttng_channel entries in channels;
784 * on error, returns a negative value.
786 int lttng_list_channels(struct lttng_handle
*handle
,
787 struct lttng_channel
**channels
)
790 struct lttcomm_session_msg lsm
;
792 if (handle
== NULL
) {
796 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
797 copy_string(lsm
.session
.name
, handle
->session_name
,
798 sizeof(lsm
.session
.name
));
800 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
802 ret
= ask_sessiond(&lsm
, (void**) channels
);
807 return ret
/ sizeof(struct lttng_channel
);
811 * Ask the session daemon for all available events of a session channel.
812 * Sets the contents of the events array.
813 * Returns the number of lttng_event entries in events;
814 * on error, returns a negative value.
816 int lttng_list_events(struct lttng_handle
*handle
,
817 const char *channel_name
, struct lttng_event
**events
)
820 struct lttcomm_session_msg lsm
;
822 /* Safety check. An handle and channel name are mandatory */
823 if (handle
== NULL
|| channel_name
== NULL
) {
827 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
828 copy_string(lsm
.session
.name
, handle
->session_name
,
829 sizeof(lsm
.session
.name
));
830 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
831 sizeof(lsm
.u
.list
.channel_name
));
833 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
835 ret
= ask_sessiond(&lsm
, (void**) events
);
840 return ret
/ sizeof(struct lttng_event
);
844 * Sets the tracing_group variable with name.
845 * This function allocates memory pointed to by tracing_group.
846 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
848 int lttng_set_tracing_group(const char *name
)
854 if (asprintf(&tracing_group
, "%s", name
) < 0) {
862 * Returns size of returned session payload data or a negative error code.
864 int lttng_calibrate(struct lttng_handle
*handle
,
865 struct lttng_calibrate
*calibrate
)
867 struct lttcomm_session_msg lsm
;
869 /* Safety check. NULL pointer are forbidden */
870 if (handle
== NULL
|| calibrate
== NULL
) {
874 lsm
.cmd_type
= LTTNG_CALIBRATE
;
875 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
877 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
879 return ask_sessiond(&lsm
, NULL
);
883 * Set default channel attributes.
884 * If either or both of the arguments are null, attr content is zeroe'd.
886 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
887 struct lttng_channel_attr
*attr
)
889 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
892 if (attr
== NULL
|| domain
== NULL
) {
896 switch (domain
->type
) {
897 case LTTNG_DOMAIN_KERNEL
:
898 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
899 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
900 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
902 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
903 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
904 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
906 case LTTNG_DOMAIN_UST
:
908 case LTTNG_DOMAIN_UST_EXEC_NAME
:
909 case LTTNG_DOMAIN_UST_PID
:
910 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
912 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
913 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
914 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
916 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
917 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
918 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
921 /* Default behavior: leave set to 0. */
927 * Check if session daemon is alive.
929 * Return 1 if alive or 0 if not.
930 * On error returns a negative value.
932 int lttng_session_daemon_alive(void)
936 ret
= set_session_daemon_path();
942 if (strlen(sessiond_sock_path
) == 0) {
943 /* No socket path set. Weird error */
947 ret
= try_connect_sessiond(sessiond_sock_path
);
960 static void __attribute__((constructor
)) init()
962 /* Set default session group */
963 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);