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 <lttng-sessiond-comm.h>
32 #include <lttng-share.h>
33 #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
:
69 case LTTNG_DOMAIN_UST_EXEC_NAME
:
70 case LTTNG_DOMAIN_UST_PID
:
71 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
72 memcpy(dst
, src
, sizeof(struct lttng_domain
));
75 dst
->type
= LTTNG_DOMAIN_KERNEL
;
82 * Send lttcomm_session_msg to the session daemon.
84 * On success, return 0
85 * On error, return error code
87 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
96 ret
= lttcomm_send_unix_sock(sessiond_socket
, lsm
,
97 sizeof(struct lttcomm_session_msg
));
104 * Receive data from the sessiond socket.
106 * On success, return 0
107 * On error, return recv() error code
109 static int recv_data_sessiond(void *buf
, size_t len
)
118 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
125 * Check if the specified group name exist.
127 * If yes return 1, else return -1.
129 static int check_tracing_group(const char *grp_name
)
131 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
133 int grp_list_size
, grp_id
, i
;
136 /* Get GID of group 'tracing' */
137 grp_tracing
= getgrnam(grp_name
);
138 if (grp_tracing
== NULL
) {
139 /* NULL means not found also. getgrnam(3) */
146 /* Get number of supplementary group IDs */
147 grp_list_size
= getgroups(0, NULL
);
148 if (grp_list_size
< 0) {
153 /* Alloc group list of the right size */
154 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
159 grp_id
= getgroups(grp_list_size
, grp_list
);
165 for (i
= 0; i
< grp_list_size
; i
++) {
166 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
180 * Try connect to session daemon with sock_path.
182 * Return 0 on success, else -1
184 static int try_connect_sessiond(const char *sock_path
)
188 /* If socket exist, we check if the daemon listens for connect. */
189 ret
= access(sock_path
, F_OK
);
195 ret
= lttcomm_connect_unix_sock(sock_path
);
201 ret
= lttcomm_close_unix_sock(ret
);
203 perror("lttcomm_close_unix_sock");
210 * Set sessiond socket path by putting it in the global sessiond_sock_path
213 static int set_session_daemon_path(void)
216 int in_tgroup
= 0; /* In tracing group */
222 /* Are we in the tracing group ? */
223 in_tgroup
= check_tracing_group(tracing_group
);
228 copy_string(sessiond_sock_path
,
229 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
230 sizeof(sessiond_sock_path
));
231 } else if (in_tgroup
) {
233 copy_string(sessiond_sock_path
,
234 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
235 sizeof(sessiond_sock_path
));
237 ret
= try_connect_sessiond(sessiond_sock_path
);
239 /* Global session daemon not available */
240 if (snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
241 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
242 getenv("HOME")) < 0) {
247 /* Not in tracing group and not root, default */
248 if (snprintf(sessiond_sock_path
, PATH_MAX
,
249 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
250 getenv("HOME")) < 0) {
259 * Connect to the LTTng session daemon.
261 * On success, return 0. On error, return -1.
263 static int connect_sessiond(void)
267 ret
= set_session_daemon_path();
272 /* Connect to the sesssion daemon */
273 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
278 sessiond_socket
= ret
;
285 * Clean disconnect the session daemon.
287 static int disconnect_sessiond(void)
292 ret
= lttcomm_close_unix_sock(sessiond_socket
);
301 * Ask the session daemon a specific command and put the data into buf.
303 * Return size of data (only payload, not header).
305 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
310 struct lttcomm_lttng_msg llm
;
312 ret
= connect_sessiond();
317 /* Send command to session daemon */
318 ret
= send_session_msg(lsm
);
323 /* Get header from data transmission */
324 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
329 /* Check error code if OK */
330 if (llm
.ret_code
!= LTTCOMM_OK
) {
335 size
= llm
.data_size
;
337 /* If client free with size 0 */
345 data
= (void*) malloc(size
);
347 /* Get payload data */
348 ret
= recv_data_sessiond(data
, size
);
355 * Extra protection not to dereference a NULL pointer. If buf is NULL at
356 * this point, an error is returned and data is freed.
368 disconnect_sessiond();
373 * Create lttng handle and return pointer.
375 struct lttng_handle
*lttng_create_handle(const char *session_name
,
376 struct lttng_domain
*domain
)
378 struct lttng_handle
*handle
;
380 handle
= malloc(sizeof(struct lttng_handle
));
381 if (handle
== NULL
) {
382 perror("malloc handle");
386 /* Copy session name */
387 copy_string(handle
->session_name
, session_name
,
388 sizeof(handle
->session_name
));
390 /* Copy lttng domain */
391 copy_lttng_domain(&handle
->domain
, domain
);
398 * Destroy handle by free(3) the pointer.
400 void lttng_destroy_handle(struct lttng_handle
*handle
)
408 * Register an outside consumer.
410 int lttng_register_consumer(struct lttng_handle
*handle
,
411 const char *socket_path
)
413 struct lttcomm_session_msg lsm
;
415 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
416 copy_string(lsm
.session
.name
, handle
->session_name
,
417 sizeof(lsm
.session
.name
));
418 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
420 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
422 return ask_sessiond(&lsm
, NULL
);
426 * Start tracing for all trace of the session.
428 int lttng_start_tracing(struct lttng_handle
*handle
)
430 struct lttcomm_session_msg lsm
;
432 if (handle
== NULL
) {
436 lsm
.cmd_type
= LTTNG_START_TRACE
;
437 copy_string(lsm
.session
.name
, handle
->session_name
,
438 sizeof(lsm
.session
.name
));
440 return ask_sessiond(&lsm
, NULL
);
444 * Stop tracing for all trace of the session.
446 int lttng_stop_tracing(struct lttng_handle
*handle
)
448 struct lttcomm_session_msg lsm
;
450 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
451 copy_string(lsm
.session
.name
, handle
->session_name
,
452 sizeof(lsm
.session
.name
));
454 return ask_sessiond(&lsm
, NULL
);
458 * Add context to event or/and channel.
460 int lttng_add_context(struct lttng_handle
*handle
,
461 struct lttng_event_context
*ctx
, const char *event_name
,
462 const char *channel_name
)
464 struct lttcomm_session_msg lsm
;
466 /* Safety check. Both are mandatory */
467 if (handle
== NULL
|| ctx
== NULL
) {
471 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
473 /* Copy channel name */
474 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
475 sizeof(lsm
.u
.context
.channel_name
));
476 /* Copy event name */
477 copy_string(lsm
.u
.context
.event_name
, event_name
,
478 sizeof(lsm
.u
.context
.event_name
));
480 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
482 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
484 copy_string(lsm
.session
.name
, handle
->session_name
,
485 sizeof(lsm
.session
.name
));
487 return ask_sessiond(&lsm
, NULL
);
493 int lttng_enable_event(struct lttng_handle
*handle
,
494 struct lttng_event
*ev
, const char *channel_name
)
496 struct lttcomm_session_msg lsm
;
498 if (handle
== NULL
|| ev
== NULL
) {
502 /* If no channel name, we put the default name */
503 if (channel_name
== NULL
) {
504 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
505 sizeof(lsm
.u
.enable
.channel_name
));
507 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
508 sizeof(lsm
.u
.enable
.channel_name
));
511 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
513 if (ev
->name
[0] != '\0') {
514 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
516 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
518 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
520 copy_string(lsm
.session
.name
, handle
->session_name
,
521 sizeof(lsm
.session
.name
));
523 return ask_sessiond(&lsm
, NULL
);
527 * Disable event of a channel and domain.
529 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
530 const char *channel_name
)
532 struct lttcomm_session_msg lsm
;
534 if (handle
== NULL
) {
539 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
540 sizeof(lsm
.u
.disable
.channel_name
));
542 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
543 sizeof(lsm
.u
.disable
.channel_name
));
546 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
549 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
550 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
552 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
555 copy_string(lsm
.session
.name
, handle
->session_name
,
556 sizeof(lsm
.session
.name
));
558 return ask_sessiond(&lsm
, NULL
);
562 * Enable channel per domain
564 int lttng_enable_channel(struct lttng_handle
*handle
,
565 struct lttng_channel
*chan
)
567 struct lttcomm_session_msg lsm
;
570 * NULL arguments are forbidden. No default values.
572 if (handle
== NULL
|| chan
== NULL
) {
576 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
578 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
580 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
582 copy_string(lsm
.session
.name
, handle
->session_name
,
583 sizeof(lsm
.session
.name
));
585 return ask_sessiond(&lsm
, NULL
);
589 * All tracing will be stopped for registered events of the channel.
591 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
593 struct lttcomm_session_msg lsm
;
595 /* Safety check. Both are mandatory */
596 if (handle
== NULL
|| name
== NULL
) {
600 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
602 copy_string(lsm
.u
.disable
.channel_name
, name
,
603 sizeof(lsm
.u
.disable
.channel_name
));
605 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
607 copy_string(lsm
.session
.name
, handle
->session_name
,
608 sizeof(lsm
.session
.name
));
610 return ask_sessiond(&lsm
, NULL
);
614 * List all available tracepoints of domain.
616 * Return the size (bytes) of the list and set the events array.
617 * On error, return negative value.
619 int lttng_list_tracepoints(struct lttng_handle
*handle
,
620 struct lttng_event
**events
)
623 struct lttcomm_session_msg lsm
;
625 if (handle
== NULL
) {
629 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
630 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
632 ret
= ask_sessiond(&lsm
, (void **) events
);
637 return ret
/ sizeof(struct lttng_event
);
641 * Return a human readable string of code
643 const char *lttng_strerror(int code
)
645 if (code
> -LTTCOMM_OK
) {
646 return "Ended with errors";
649 return lttcomm_get_readable_code(code
);
653 * Create a brand new session using name.
655 int lttng_create_session(const char *name
, const char *path
)
657 struct lttcomm_session_msg lsm
;
659 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
660 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
661 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
663 return ask_sessiond(&lsm
, NULL
);
667 * Destroy session using name.
669 int lttng_destroy_session(struct lttng_handle
*handle
)
671 struct lttcomm_session_msg lsm
;
673 if (handle
== NULL
) {
677 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
678 copy_string(lsm
.session
.name
, handle
->session_name
,
679 sizeof(lsm
.session
.name
));
681 return ask_sessiond(&lsm
, NULL
);
685 * Ask the session daemon for all available sessions.
687 * Return number of session.
688 * On error, return negative value.
690 int lttng_list_sessions(struct lttng_session
**sessions
)
693 struct lttcomm_session_msg lsm
;
695 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
696 ret
= ask_sessiond(&lsm
, (void**) sessions
);
701 return ret
/ sizeof(struct lttng_session
);
705 * List domain of a session.
707 int lttng_list_domains(struct lttng_handle
*handle
,
708 struct lttng_domain
**domains
)
711 struct lttcomm_session_msg lsm
;
713 if (handle
== NULL
) {
717 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
719 copy_string(lsm
.session
.name
, handle
->session_name
,
720 sizeof(lsm
.session
.name
));
722 ret
= ask_sessiond(&lsm
, (void**) domains
);
727 return ret
/ sizeof(struct lttng_domain
);
731 * List channels of a session
733 int lttng_list_channels(struct lttng_handle
*handle
,
734 struct lttng_channel
**channels
)
737 struct lttcomm_session_msg lsm
;
739 if (handle
== NULL
) {
743 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
744 copy_string(lsm
.session
.name
, handle
->session_name
,
745 sizeof(lsm
.session
.name
));
747 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
749 ret
= ask_sessiond(&lsm
, (void**) channels
);
754 return ret
/ sizeof(struct lttng_channel
);
758 * List events of a session channel.
760 int lttng_list_events(struct lttng_handle
*handle
,
761 const char *channel_name
, struct lttng_event
**events
)
764 struct lttcomm_session_msg lsm
;
766 /* Safety check. An handle and channel name are mandatory */
767 if (handle
== NULL
|| channel_name
== NULL
) {
771 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
772 copy_string(lsm
.session
.name
, handle
->session_name
,
773 sizeof(lsm
.session
.name
));
774 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
775 sizeof(lsm
.u
.list
.channel_name
));
777 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
779 ret
= ask_sessiond(&lsm
, (void**) events
);
784 return ret
/ sizeof(struct lttng_event
);
788 * Set tracing group variable with name. This function allocate memory pointed
791 int lttng_set_tracing_group(const char *name
)
797 if (asprintf(&tracing_group
, "%s", name
) < 0) {
807 int lttng_calibrate(struct lttng_handle
*handle
,
808 struct lttng_calibrate
*calibrate
)
810 struct lttcomm_session_msg lsm
;
812 /* Safety check. NULL pointer are forbidden */
813 if (handle
== NULL
|| calibrate
== NULL
) {
817 lsm
.cmd_type
= LTTNG_CALIBRATE
;
818 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
820 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
822 return ask_sessiond(&lsm
, NULL
);
826 * Set default channel attributes.
828 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
829 struct lttng_channel_attr
*attr
)
832 if (attr
== NULL
|| domain
== NULL
) {
836 switch (domain
->type
) {
837 case LTTNG_DOMAIN_KERNEL
:
838 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
839 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
840 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
842 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
843 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
844 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
846 case LTTNG_DOMAIN_UST
:
847 case LTTNG_DOMAIN_UST_EXEC_NAME
:
848 case LTTNG_DOMAIN_UST_PID
:
849 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
850 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
851 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
852 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
854 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
855 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
856 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
859 /* Default behavior */
860 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
866 * Check if session daemon is alive.
868 * Return 1 if alive or 0 if not.
871 int lttng_session_daemon_alive(void)
875 ret
= set_session_daemon_path();
881 if (strlen(sessiond_sock_path
) == 0) {
882 /* No socket path set. Weird error */
886 ret
= try_connect_sessiond(sessiond_sock_path
);
899 static void __attribute__((constructor
)) init()
901 /* Set default session group */
902 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);