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
;
45 * Copy string from src to dst and enforce null terminated byte.
47 static void copy_string(char *dst
, const char *src
, size_t len
)
50 strncpy(dst
, src
, len
);
51 /* Enforce the NULL terminated byte */
59 * Copy domain to lttcomm_session_msg domain.
61 * If domain is unknown, default domain will be the kernel.
63 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
67 case LTTNG_DOMAIN_KERNEL
:
68 case LTTNG_DOMAIN_UST
:
70 case LTTNG_DOMAIN_UST_EXEC_NAME:
71 case LTTNG_DOMAIN_UST_PID:
72 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
74 memcpy(dst
, src
, sizeof(struct lttng_domain
));
77 dst
->type
= LTTNG_DOMAIN_KERNEL
;
84 * Send lttcomm_session_msg to the session daemon.
86 * On success, returns the number of bytes sent (>=0)
87 * On error, returns -1
89 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
98 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
99 sizeof(struct lttcomm_session_msg
));
106 * Receive data from the sessiond socket.
108 * On success, returns the number of bytes received (>=0)
109 * On error, returns -1 (recvmsg() error) or -ENOTCONN
111 static int recv_data_sessiond(void *buf
, size_t len
)
120 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
127 * Check if we are in the specified group.
129 * If yes return 1, else return -1.
131 static int check_tracing_group(const char *grp_name
)
133 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
135 int grp_list_size
, grp_id
, i
;
138 /* Get GID of group 'tracing' */
139 grp_tracing
= getgrnam(grp_name
);
140 if (grp_tracing
== NULL
) {
141 /* NULL means not found also. getgrnam(3) */
148 /* Get number of supplementary group IDs */
149 grp_list_size
= getgroups(0, NULL
);
150 if (grp_list_size
< 0) {
155 /* Alloc group list of the right size */
156 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
161 grp_id
= getgroups(grp_list_size
, grp_list
);
167 for (i
= 0; i
< grp_list_size
; i
++) {
168 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
182 * Try connect to session daemon with sock_path.
184 * Return 0 on success, else -1
186 static int try_connect_sessiond(const char *sock_path
)
190 /* If socket exist, we check if the daemon listens for connect. */
191 ret
= access(sock_path
, F_OK
);
197 ret
= lttcomm_connect_unix_sock(sock_path
);
203 ret
= lttcomm_close_unix_sock(ret
);
205 perror("lttcomm_close_unix_sock");
212 * Set sessiond socket path by putting it in the global
213 * sessiond_sock_path variable.
214 * Returns 0 on success,
215 * -ENOMEM on failure (the sessiond socket path is somehow too long)
217 static int set_session_daemon_path(void)
220 int in_tgroup
= 0; /* In tracing group */
226 /* Are we in the tracing group ? */
227 in_tgroup
= check_tracing_group(tracing_group
);
230 if ((uid
== 0) || in_tgroup
) {
231 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
232 sizeof(sessiond_sock_path
));
238 ret
= try_connect_sessiond(sessiond_sock_path
);
242 /* Global session daemon not available... */
244 /* ...or not in tracing group (and not root), default */
247 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
248 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
250 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
251 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
252 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
261 * Connect to the LTTng session daemon.
263 * On success, return 0. On error, return -1.
265 static int connect_sessiond(void)
269 ret
= set_session_daemon_path();
271 return -1; /* set_session_daemon_path() returns -ENOMEM */
274 /* Connect to the sesssion daemon */
275 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
280 sessiond_socket
= ret
;
287 * Clean disconnect from the session daemon.
288 * On success, return 0. On error, return -1.
290 static int disconnect_sessiond(void)
295 ret
= lttcomm_close_unix_sock(sessiond_socket
);
304 * Ask the session daemon a specific command and put the data into buf.
306 * Return size of data (only payload, not header) or a negative error code.
308 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
313 struct lttcomm_lttng_msg llm
;
315 ret
= connect_sessiond();
320 /* Send command to session daemon */
321 ret
= send_session_msg(lsm
);
326 /* Get header from data transmission */
327 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
332 /* Check error code if OK */
333 if (llm
.ret_code
!= LTTCOMM_OK
) {
338 size
= llm
.data_size
;
340 /* If client free with size 0 */
348 data
= (void*) malloc(size
);
350 /* Get payload data */
351 ret
= recv_data_sessiond(data
, size
);
358 * Extra protection not to dereference a NULL pointer. If buf is NULL at
359 * this point, an error is returned and data is freed.
371 disconnect_sessiond();
376 * Create lttng handle and return pointer.
377 * The returned pointer will be NULL in case of malloc() error.
379 struct lttng_handle
*lttng_create_handle(const char *session_name
,
380 struct lttng_domain
*domain
)
382 struct lttng_handle
*handle
;
384 handle
= malloc(sizeof(struct lttng_handle
));
385 if (handle
== NULL
) {
386 perror("malloc handle");
390 /* Copy session name */
391 copy_string(handle
->session_name
, session_name
,
392 sizeof(handle
->session_name
));
394 /* Copy lttng domain */
395 copy_lttng_domain(&handle
->domain
, domain
);
402 * Destroy handle by free(3) the pointer.
404 void lttng_destroy_handle(struct lttng_handle
*handle
)
412 * Register an outside consumer.
413 * Returns size of returned session payload data or a negative error code.
415 int lttng_register_consumer(struct lttng_handle
*handle
,
416 const char *socket_path
)
418 struct lttcomm_session_msg lsm
;
420 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
421 copy_string(lsm
.session
.name
, handle
->session_name
,
422 sizeof(lsm
.session
.name
));
423 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
425 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
427 return ask_sessiond(&lsm
, NULL
);
431 * Start tracing for all traces of the session.
432 * Returns size of returned session payload data or a negative error code.
434 int lttng_start_tracing(const char *session_name
)
436 struct lttcomm_session_msg lsm
;
438 if (session_name
== NULL
) {
442 lsm
.cmd_type
= LTTNG_START_TRACE
;
444 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
446 return ask_sessiond(&lsm
, NULL
);
450 * Stop tracing for all traces of the session.
451 * Returns size of returned session payload data or a negative error code.
453 int lttng_stop_tracing(const char *session_name
)
455 struct lttcomm_session_msg lsm
;
457 if (session_name
== NULL
) {
461 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
463 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
465 return ask_sessiond(&lsm
, NULL
);
469 * Add context to event and/or channel.
470 * If event_name is NULL, the context is applied to all events of the channel.
471 * If channel_name is NULL, a lookup of the event's channel is done.
472 * If both are NULL, the context is applied to all events of all channels.
474 * Returns the size of the returned payload data or a negative error code.
476 int lttng_add_context(struct lttng_handle
*handle
,
477 struct lttng_event_context
*ctx
, const char *event_name
,
478 const char *channel_name
)
480 struct lttcomm_session_msg lsm
;
482 /* Safety check. Both are mandatory */
483 if (handle
== NULL
|| ctx
== NULL
) {
487 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
489 /* Copy channel name */
490 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
491 sizeof(lsm
.u
.context
.channel_name
));
492 /* Copy event name */
493 copy_string(lsm
.u
.context
.event_name
, event_name
,
494 sizeof(lsm
.u
.context
.event_name
));
496 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
498 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
500 copy_string(lsm
.session
.name
, handle
->session_name
,
501 sizeof(lsm
.session
.name
));
503 return ask_sessiond(&lsm
, NULL
);
507 * Enable event(s) for a channel.
508 * If no event name is specified, all events are enabled.
509 * If no channel name is specified, the default 'channel0' is used.
510 * Returns size of returned session payload data or a negative error code.
512 int lttng_enable_event(struct lttng_handle
*handle
,
513 struct lttng_event
*ev
, const char *channel_name
)
515 struct lttcomm_session_msg lsm
;
517 if (handle
== NULL
|| ev
== NULL
) {
521 /* If no channel name, we put the default name */
522 if (channel_name
== NULL
) {
523 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
524 sizeof(lsm
.u
.enable
.channel_name
));
526 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
527 sizeof(lsm
.u
.enable
.channel_name
));
530 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
532 if (ev
->name
[0] != '\0') {
533 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
535 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
537 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
539 copy_string(lsm
.session
.name
, handle
->session_name
,
540 sizeof(lsm
.session
.name
));
542 return ask_sessiond(&lsm
, NULL
);
546 * Disable event(s) of a channel and domain.
547 * If no event name is specified, all events are disabled.
548 * If no channel name is specified, the default 'channel0' is used.
549 * Returns size of returned session payload data or a negative error code.
551 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
552 const char *channel_name
)
554 struct lttcomm_session_msg lsm
;
556 if (handle
== NULL
) {
561 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
562 sizeof(lsm
.u
.disable
.channel_name
));
564 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
565 sizeof(lsm
.u
.disable
.channel_name
));
568 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
571 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
572 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
574 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
577 copy_string(lsm
.session
.name
, handle
->session_name
,
578 sizeof(lsm
.session
.name
));
580 return ask_sessiond(&lsm
, NULL
);
584 * Enable channel per domain
585 * Returns size of returned session payload data or a negative error code.
587 int lttng_enable_channel(struct lttng_handle
*handle
,
588 struct lttng_channel
*chan
)
590 struct lttcomm_session_msg lsm
;
593 * NULL arguments are forbidden. No default values.
595 if (handle
== NULL
|| chan
== NULL
) {
599 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
601 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
603 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
605 copy_string(lsm
.session
.name
, handle
->session_name
,
606 sizeof(lsm
.session
.name
));
608 return ask_sessiond(&lsm
, NULL
);
612 * All tracing will be stopped for registered events of the channel.
613 * Returns size of returned session payload data or a negative error code.
615 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
617 struct lttcomm_session_msg lsm
;
619 /* Safety check. Both are mandatory */
620 if (handle
== NULL
|| name
== NULL
) {
624 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
626 copy_string(lsm
.u
.disable
.channel_name
, name
,
627 sizeof(lsm
.u
.disable
.channel_name
));
629 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
631 copy_string(lsm
.session
.name
, handle
->session_name
,
632 sizeof(lsm
.session
.name
));
634 return ask_sessiond(&lsm
, NULL
);
638 * Lists all available tracepoints of domain.
639 * Sets the contents of the events array.
640 * Returns the number of lttng_event entries in events;
641 * on error, returns a negative value.
643 int lttng_list_tracepoints(struct lttng_handle
*handle
,
644 struct lttng_event
**events
)
647 struct lttcomm_session_msg lsm
;
649 if (handle
== NULL
) {
653 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
654 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
656 ret
= ask_sessiond(&lsm
, (void **) events
);
661 return ret
/ sizeof(struct lttng_event
);
665 * Returns a human readable string describing
666 * the error code (a negative value).
668 const char *lttng_strerror(int code
)
670 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
671 if (code
> -LTTCOMM_OK
) {
672 return "Ended with errors";
675 return lttcomm_get_readable_code(code
);
679 * Create a brand new session using name and path.
680 * Returns size of returned session payload data or a negative error code.
682 int lttng_create_session(const char *name
, const char *path
)
684 struct lttcomm_session_msg lsm
;
686 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
687 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
688 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
690 return ask_sessiond(&lsm
, NULL
);
694 * Destroy session using name.
695 * Returns size of returned session payload data or a negative error code.
697 int lttng_destroy_session(const char *session_name
)
699 struct lttcomm_session_msg lsm
;
701 if (session_name
== NULL
) {
705 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
707 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
709 return ask_sessiond(&lsm
, NULL
);
713 * Ask the session daemon for all available sessions.
714 * Sets the contents of the sessions array.
715 * Returns the number of lttng_session entries in sessions;
716 * on error, returns a negative value.
718 int lttng_list_sessions(struct lttng_session
**sessions
)
721 struct lttcomm_session_msg lsm
;
723 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
724 ret
= ask_sessiond(&lsm
, (void**) sessions
);
729 return ret
/ sizeof(struct lttng_session
);
733 * Ask the session daemon for all available domains of a session.
734 * Sets the contents of the domains array.
735 * Returns the number of lttng_domain entries in domains;
736 * on error, returns a negative value.
738 int lttng_list_domains(const char *session_name
,
739 struct lttng_domain
**domains
)
742 struct lttcomm_session_msg lsm
;
744 if (session_name
== NULL
) {
748 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
750 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
752 ret
= ask_sessiond(&lsm
, (void**) domains
);
757 return ret
/ sizeof(struct lttng_domain
);
761 * Ask the session daemon for all available channels of a session.
762 * Sets the contents of the channels array.
763 * Returns the number of lttng_channel entries in channels;
764 * on error, returns a negative value.
766 int lttng_list_channels(struct lttng_handle
*handle
,
767 struct lttng_channel
**channels
)
770 struct lttcomm_session_msg lsm
;
772 if (handle
== NULL
) {
776 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
777 copy_string(lsm
.session
.name
, handle
->session_name
,
778 sizeof(lsm
.session
.name
));
780 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
782 ret
= ask_sessiond(&lsm
, (void**) channels
);
787 return ret
/ sizeof(struct lttng_channel
);
791 * Ask the session daemon for all available events of a session channel.
792 * Sets the contents of the events array.
793 * Returns the number of lttng_event entries in events;
794 * on error, returns a negative value.
796 int lttng_list_events(struct lttng_handle
*handle
,
797 const char *channel_name
, struct lttng_event
**events
)
800 struct lttcomm_session_msg lsm
;
802 /* Safety check. An handle and channel name are mandatory */
803 if (handle
== NULL
|| channel_name
== NULL
) {
807 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
808 copy_string(lsm
.session
.name
, handle
->session_name
,
809 sizeof(lsm
.session
.name
));
810 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
811 sizeof(lsm
.u
.list
.channel_name
));
813 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
815 ret
= ask_sessiond(&lsm
, (void**) events
);
820 return ret
/ sizeof(struct lttng_event
);
824 * Sets the tracing_group variable with name.
825 * This function allocates memory pointed to by tracing_group.
826 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
828 int lttng_set_tracing_group(const char *name
)
834 if (asprintf(&tracing_group
, "%s", name
) < 0) {
842 * Returns size of returned session payload data or a negative error code.
844 int lttng_calibrate(struct lttng_handle
*handle
,
845 struct lttng_calibrate
*calibrate
)
847 struct lttcomm_session_msg lsm
;
849 /* Safety check. NULL pointer are forbidden */
850 if (handle
== NULL
|| calibrate
== NULL
) {
854 lsm
.cmd_type
= LTTNG_CALIBRATE
;
855 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
857 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
859 return ask_sessiond(&lsm
, NULL
);
863 * Set default channel attributes.
864 * If either or both of the arguments are null, nothing happens.
866 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
867 struct lttng_channel_attr
*attr
)
870 if (attr
== NULL
|| domain
== NULL
) {
874 switch (domain
->type
) {
875 case LTTNG_DOMAIN_KERNEL
:
876 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
877 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
878 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
880 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
881 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
882 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
884 case LTTNG_DOMAIN_UST
:
886 case LTTNG_DOMAIN_UST_EXEC_NAME
:
887 case LTTNG_DOMAIN_UST_PID
:
888 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
890 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
891 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
892 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
894 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
895 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
896 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
899 /* Default behavior */
900 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
906 * Check if session daemon is alive.
908 * Return 1 if alive or 0 if not.
909 * On error returns a negative value.
911 int lttng_session_daemon_alive(void)
915 ret
= set_session_daemon_path();
921 if (strlen(sessiond_sock_path
) == 0) {
922 /* No socket path set. Weird error */
926 ret
= try_connect_sessiond(sessiond_sock_path
);
939 static void __attribute__((constructor
)) init()
941 /* Set default session group */
942 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);