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, return 0
87 * On error, return error code
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, return 0
109 * On error, return recv() error code
111 static int recv_data_sessiond(void *buf
, size_t len
)
120 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
127 * Check if the specified group name exist.
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 sessiond_sock_path
215 static int set_session_daemon_path(void)
218 int in_tgroup
= 0; /* In tracing group */
224 /* Are we in the tracing group ? */
225 in_tgroup
= check_tracing_group(tracing_group
);
230 copy_string(sessiond_sock_path
,
231 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
232 sizeof(sessiond_sock_path
));
233 } else if (in_tgroup
) {
235 copy_string(sessiond_sock_path
,
236 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
237 sizeof(sessiond_sock_path
));
239 ret
= try_connect_sessiond(sessiond_sock_path
);
241 /* Global session daemon not available */
242 if (snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
243 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
244 getenv("HOME")) < 0) {
249 /* Not in tracing group and not root, default */
250 if (snprintf(sessiond_sock_path
, PATH_MAX
,
251 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
252 getenv("HOME")) < 0) {
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();
274 /* Connect to the sesssion daemon */
275 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
280 sessiond_socket
= ret
;
287 * Clean disconnect the session daemon.
289 static int disconnect_sessiond(void)
294 ret
= lttcomm_close_unix_sock(sessiond_socket
);
303 * Ask the session daemon a specific command and put the data into buf.
305 * Return size of data (only payload, not header) or a negative error code.
307 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
312 struct lttcomm_lttng_msg llm
;
314 ret
= connect_sessiond();
319 /* Send command to session daemon */
320 ret
= send_session_msg(lsm
);
325 /* Get header from data transmission */
326 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
331 /* Check error code if OK */
332 if (llm
.ret_code
!= LTTCOMM_OK
) {
337 size
= llm
.data_size
;
339 /* If client free with size 0 */
347 data
= (void*) malloc(size
);
349 /* Get payload data */
350 ret
= recv_data_sessiond(data
, size
);
357 * Extra protection not to dereference a NULL pointer. If buf is NULL at
358 * this point, an error is returned and data is freed.
370 disconnect_sessiond();
375 * Create lttng handle and return pointer.
377 struct lttng_handle
*lttng_create_handle(const char *session_name
,
378 struct lttng_domain
*domain
)
380 struct lttng_handle
*handle
;
382 handle
= malloc(sizeof(struct lttng_handle
));
383 if (handle
== NULL
) {
384 perror("malloc handle");
388 /* Copy session name */
389 copy_string(handle
->session_name
, session_name
,
390 sizeof(handle
->session_name
));
392 /* Copy lttng domain */
393 copy_lttng_domain(&handle
->domain
, domain
);
400 * Destroy handle by free(3) the pointer.
402 void lttng_destroy_handle(struct lttng_handle
*handle
)
410 * Register an outside consumer.
412 int lttng_register_consumer(struct lttng_handle
*handle
,
413 const char *socket_path
)
415 struct lttcomm_session_msg lsm
;
417 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
418 copy_string(lsm
.session
.name
, handle
->session_name
,
419 sizeof(lsm
.session
.name
));
420 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
422 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
424 return ask_sessiond(&lsm
, NULL
);
428 * Start tracing for all trace of the session.
430 int lttng_start_tracing(const char *session_name
)
432 struct lttcomm_session_msg lsm
;
434 if (session_name
== NULL
) {
438 lsm
.cmd_type
= LTTNG_START_TRACE
;
440 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
442 return ask_sessiond(&lsm
, NULL
);
446 * Stop tracing for all trace of the session.
448 int lttng_stop_tracing(const char *session_name
)
450 struct lttcomm_session_msg lsm
;
452 if (session_name
== NULL
) {
456 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
458 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
460 return ask_sessiond(&lsm
, NULL
);
464 * Add context to event or/and channel.
466 * Returns the size of the returned payload data or a negative error code.
468 int lttng_add_context(struct lttng_handle
*handle
,
469 struct lttng_event_context
*ctx
, const char *event_name
,
470 const char *channel_name
)
472 struct lttcomm_session_msg lsm
;
474 /* Safety check. Both are mandatory */
475 if (handle
== NULL
|| ctx
== NULL
) {
479 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
481 /* Copy channel name */
482 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
483 sizeof(lsm
.u
.context
.channel_name
));
484 /* Copy event name */
485 copy_string(lsm
.u
.context
.event_name
, event_name
,
486 sizeof(lsm
.u
.context
.event_name
));
488 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
490 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
492 copy_string(lsm
.session
.name
, handle
->session_name
,
493 sizeof(lsm
.session
.name
));
495 return ask_sessiond(&lsm
, NULL
);
501 int lttng_enable_event(struct lttng_handle
*handle
,
502 struct lttng_event
*ev
, const char *channel_name
)
504 struct lttcomm_session_msg lsm
;
506 if (handle
== NULL
|| ev
== NULL
) {
510 /* If no channel name, we put the default name */
511 if (channel_name
== NULL
) {
512 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
513 sizeof(lsm
.u
.enable
.channel_name
));
515 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
516 sizeof(lsm
.u
.enable
.channel_name
));
519 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
521 if (ev
->name
[0] != '\0') {
522 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
524 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
526 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
528 copy_string(lsm
.session
.name
, handle
->session_name
,
529 sizeof(lsm
.session
.name
));
531 return ask_sessiond(&lsm
, NULL
);
535 * Disable event of a channel and domain.
537 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
538 const char *channel_name
)
540 struct lttcomm_session_msg lsm
;
542 if (handle
== NULL
) {
547 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
548 sizeof(lsm
.u
.disable
.channel_name
));
550 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
551 sizeof(lsm
.u
.disable
.channel_name
));
554 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
557 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
558 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
560 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
563 copy_string(lsm
.session
.name
, handle
->session_name
,
564 sizeof(lsm
.session
.name
));
566 return ask_sessiond(&lsm
, NULL
);
570 * Enable channel per domain
572 int lttng_enable_channel(struct lttng_handle
*handle
,
573 struct lttng_channel
*chan
)
575 struct lttcomm_session_msg lsm
;
578 * NULL arguments are forbidden. No default values.
580 if (handle
== NULL
|| chan
== NULL
) {
584 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
586 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
588 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
590 copy_string(lsm
.session
.name
, handle
->session_name
,
591 sizeof(lsm
.session
.name
));
593 return ask_sessiond(&lsm
, NULL
);
597 * All tracing will be stopped for registered events of the channel.
599 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
601 struct lttcomm_session_msg lsm
;
603 /* Safety check. Both are mandatory */
604 if (handle
== NULL
|| name
== NULL
) {
608 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
610 copy_string(lsm
.u
.disable
.channel_name
, name
,
611 sizeof(lsm
.u
.disable
.channel_name
));
613 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
615 copy_string(lsm
.session
.name
, handle
->session_name
,
616 sizeof(lsm
.session
.name
));
618 return ask_sessiond(&lsm
, NULL
);
622 * List all available tracepoints of domain.
624 * Return the size (bytes) of the list and set the events array.
625 * On error, return negative value.
627 int lttng_list_tracepoints(struct lttng_handle
*handle
,
628 struct lttng_event
**events
)
631 struct lttcomm_session_msg lsm
;
633 if (handle
== NULL
) {
637 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
638 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
640 ret
= ask_sessiond(&lsm
, (void **) events
);
645 return ret
/ sizeof(struct lttng_event
);
649 * Return a human readable string of code
651 const char *lttng_strerror(int code
)
653 if (code
> -LTTCOMM_OK
) {
654 return "Ended with errors";
657 return lttcomm_get_readable_code(code
);
661 * Create a brand new session using name.
663 int lttng_create_session(const char *name
, const char *path
)
665 struct lttcomm_session_msg lsm
;
667 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
668 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
669 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
671 return ask_sessiond(&lsm
, NULL
);
675 * Destroy session using name.
677 int lttng_destroy_session(const char *session_name
)
679 struct lttcomm_session_msg lsm
;
681 if (session_name
== NULL
) {
685 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
687 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
689 return ask_sessiond(&lsm
, NULL
);
693 * Ask the session daemon for all available sessions.
695 * Return number of session.
696 * On error, return negative value.
698 int lttng_list_sessions(struct lttng_session
**sessions
)
701 struct lttcomm_session_msg lsm
;
703 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
704 ret
= ask_sessiond(&lsm
, (void**) sessions
);
709 return ret
/ sizeof(struct lttng_session
);
713 * List domain of a session.
715 int lttng_list_domains(const char *session_name
,
716 struct lttng_domain
**domains
)
719 struct lttcomm_session_msg lsm
;
721 if (session_name
== NULL
) {
725 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
727 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
729 ret
= ask_sessiond(&lsm
, (void**) domains
);
734 return ret
/ sizeof(struct lttng_domain
);
738 * List channels of a session
740 int lttng_list_channels(struct lttng_handle
*handle
,
741 struct lttng_channel
**channels
)
744 struct lttcomm_session_msg lsm
;
746 if (handle
== NULL
) {
750 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
751 copy_string(lsm
.session
.name
, handle
->session_name
,
752 sizeof(lsm
.session
.name
));
754 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
756 ret
= ask_sessiond(&lsm
, (void**) channels
);
761 return ret
/ sizeof(struct lttng_channel
);
765 * List events of a session channel.
767 int lttng_list_events(struct lttng_handle
*handle
,
768 const char *channel_name
, struct lttng_event
**events
)
771 struct lttcomm_session_msg lsm
;
773 /* Safety check. An handle and channel name are mandatory */
774 if (handle
== NULL
|| channel_name
== NULL
) {
778 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
779 copy_string(lsm
.session
.name
, handle
->session_name
,
780 sizeof(lsm
.session
.name
));
781 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
782 sizeof(lsm
.u
.list
.channel_name
));
784 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
786 ret
= ask_sessiond(&lsm
, (void**) events
);
791 return ret
/ sizeof(struct lttng_event
);
795 * Set tracing group variable with name. This function allocate memory pointed
798 int lttng_set_tracing_group(const char *name
)
804 if (asprintf(&tracing_group
, "%s", name
) < 0) {
812 * Returns size of returned session payload data or a negative error code.
814 int lttng_calibrate(struct lttng_handle
*handle
,
815 struct lttng_calibrate
*calibrate
)
817 struct lttcomm_session_msg lsm
;
819 /* Safety check. NULL pointer are forbidden */
820 if (handle
== NULL
|| calibrate
== NULL
) {
824 lsm
.cmd_type
= LTTNG_CALIBRATE
;
825 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
827 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
829 return ask_sessiond(&lsm
, NULL
);
833 * Set default channel attributes.
835 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
836 struct lttng_channel_attr
*attr
)
839 if (attr
== NULL
|| domain
== NULL
) {
843 switch (domain
->type
) {
844 case LTTNG_DOMAIN_KERNEL
:
845 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
846 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
847 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
849 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
850 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
851 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
853 case LTTNG_DOMAIN_UST
:
855 case LTTNG_DOMAIN_UST_EXEC_NAME
:
856 case LTTNG_DOMAIN_UST_PID
:
857 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
859 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
860 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
861 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
863 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
864 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
865 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
868 /* Default behavior */
869 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
875 * Check if session daemon is alive.
877 * Return 1 if alive or 0 if not.
880 int lttng_session_daemon_alive(void)
884 ret
= set_session_daemon_path();
890 if (strlen(sessiond_sock_path
) == 0) {
891 /* No socket path set. Weird error */
895 ret
= try_connect_sessiond(sessiond_sock_path
);
908 static void __attribute__((constructor
)) init()
910 /* Set default session group */
911 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);