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
));
155 grp_id
= getgroups(grp_list_size
, grp_list
);
161 for (i
= 0; i
< grp_list_size
; i
++) {
162 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
176 * Try connect to session daemon with sock_path.
178 * Return 0 on success, else -1
180 static int try_connect_sessiond(const char *sock_path
)
184 /* If socket exist, we check if the daemon listens for connect. */
185 ret
= access(sock_path
, F_OK
);
191 ret
= lttcomm_connect_unix_sock(sock_path
);
197 ret
= lttcomm_close_unix_sock(ret
);
199 perror("lttcomm_close_unix_sock");
206 * Set sessiond socket path by putting it in the global sessiond_sock_path
209 static int set_session_daemon_path(void)
212 int in_tgroup
= 0; /* In tracing group */
218 /* Are we in the tracing group ? */
219 in_tgroup
= check_tracing_group(tracing_group
);
224 copy_string(sessiond_sock_path
,
225 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
226 sizeof(sessiond_sock_path
));
227 } else if (in_tgroup
) {
229 copy_string(sessiond_sock_path
,
230 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
231 sizeof(sessiond_sock_path
));
233 ret
= try_connect_sessiond(sessiond_sock_path
);
235 /* Global session daemon not available */
236 if (snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
237 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
238 getenv("HOME")) < 0) {
243 /* Not in tracing group and not root, default */
244 if (snprintf(sessiond_sock_path
, PATH_MAX
,
245 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
246 getenv("HOME")) < 0) {
255 * Connect to the LTTng session daemon.
257 * On success, return 0. On error, return -1.
259 static int connect_sessiond(void)
263 ret
= set_session_daemon_path();
268 /* Connect to the sesssion daemon */
269 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
274 sessiond_socket
= ret
;
281 * Clean disconnect the session daemon.
283 static int disconnect_sessiond(void)
288 ret
= lttcomm_close_unix_sock(sessiond_socket
);
297 * Ask the session daemon a specific command and put the data into buf.
299 * Return size of data (only payload, not header).
301 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
306 struct lttcomm_lttng_msg llm
;
308 ret
= connect_sessiond();
313 /* Send command to session daemon */
314 ret
= send_session_msg(lsm
);
319 /* Get header from data transmission */
320 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
325 /* Check error code if OK */
326 if (llm
.ret_code
!= LTTCOMM_OK
) {
331 size
= llm
.data_size
;
337 data
= (void*) malloc(size
);
339 /* Get payload data */
340 ret
= recv_data_sessiond(data
, size
);
347 * Extra protection not to dereference a NULL pointer. If buf is NULL at
348 * this point, an error is returned and data is freed.
360 disconnect_sessiond();
365 * Create lttng handle and return pointer.
367 struct lttng_handle
*lttng_create_handle(const char *session_name
,
368 struct lttng_domain
*domain
)
370 struct lttng_handle
*handle
;
372 handle
= malloc(sizeof(struct lttng_handle
));
373 if (handle
== NULL
) {
374 perror("malloc handle");
378 /* Copy session name */
379 copy_string(handle
->session_name
, session_name
,
380 sizeof(handle
->session_name
));
382 /* Copy lttng domain */
383 copy_lttng_domain(&handle
->domain
, domain
);
390 * Destroy handle by free(3) the pointer.
392 void lttng_destroy_handle(struct lttng_handle
*handle
)
400 * Register an outside consumer.
402 int lttng_register_consumer(struct lttng_handle
*handle
,
403 const char *socket_path
)
405 struct lttcomm_session_msg lsm
;
407 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
408 copy_string(lsm
.session
.name
, handle
->session_name
,
409 sizeof(lsm
.session
.name
));
410 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
412 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
414 return ask_sessiond(&lsm
, NULL
);
418 * Start tracing for all trace of the session.
420 int lttng_start_tracing(struct lttng_handle
*handle
)
422 struct lttcomm_session_msg lsm
;
428 lsm
.cmd_type
= LTTNG_START_TRACE
;
429 copy_string(lsm
.session
.name
, handle
->session_name
,
430 sizeof(lsm
.session
.name
));
432 return ask_sessiond(&lsm
, NULL
);
436 * Stop tracing for all trace of the session.
438 int lttng_stop_tracing(struct lttng_handle
*handle
)
440 struct lttcomm_session_msg lsm
;
442 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
443 copy_string(lsm
.session
.name
, handle
->session_name
,
444 sizeof(lsm
.session
.name
));
446 return ask_sessiond(&lsm
, NULL
);
450 * Add context to event or/and channel.
452 int lttng_add_context(struct lttng_handle
*handle
,
453 struct lttng_event_context
*ctx
, const char *event_name
,
454 const char *channel_name
)
456 struct lttcomm_session_msg lsm
;
462 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
464 /* Copy channel name */
465 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
466 sizeof(lsm
.u
.context
.channel_name
));
467 /* Copy event name */
468 copy_string(lsm
.u
.context
.event_name
, event_name
,
469 sizeof(lsm
.u
.context
.event_name
));
471 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
474 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
477 copy_string(lsm
.session
.name
, handle
->session_name
,
478 sizeof(lsm
.session
.name
));
480 return ask_sessiond(&lsm
, NULL
);
486 int lttng_enable_event(struct lttng_handle
*handle
,
487 struct lttng_event
*ev
, const char *channel_name
)
489 struct lttcomm_session_msg lsm
;
495 if (channel_name
== NULL
) {
496 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
497 sizeof(lsm
.u
.enable
.channel_name
));
499 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
500 sizeof(lsm
.u
.enable
.channel_name
));
503 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
506 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
507 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
509 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
512 copy_string(lsm
.session
.name
, handle
->session_name
,
513 sizeof(lsm
.session
.name
));
515 return ask_sessiond(&lsm
, NULL
);
519 * Disable event of a channel and domain.
521 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
522 const char *channel_name
)
524 struct lttcomm_session_msg lsm
;
531 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
532 sizeof(lsm
.u
.disable
.channel_name
));
534 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
535 sizeof(lsm
.u
.disable
.channel_name
));
538 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
541 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
542 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
544 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
547 copy_string(lsm
.session
.name
, handle
->session_name
,
548 sizeof(lsm
.session
.name
));
550 return ask_sessiond(&lsm
, NULL
);
554 * Enable channel per domain
556 int lttng_enable_channel(struct lttng_handle
*handle
,
557 struct lttng_channel
*chan
)
559 struct lttcomm_session_msg lsm
;
566 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
569 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
571 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
573 copy_string(lsm
.session
.name
, handle
->session_name
,
574 sizeof(lsm
.session
.name
));
576 return ask_sessiond(&lsm
, NULL
);
580 * All tracing will be stopped for registered events of the channel.
582 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
584 struct lttcomm_session_msg lsm
;
591 copy_string(lsm
.u
.disable
.channel_name
, name
,
592 sizeof(lsm
.u
.disable
.channel_name
));
595 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
597 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
599 copy_string(lsm
.session
.name
, handle
->session_name
,
600 sizeof(lsm
.session
.name
));
602 return ask_sessiond(&lsm
, NULL
);
606 * List all available tracepoints of domain.
608 * Return the size (bytes) of the list and set the events array.
609 * On error, return negative value.
611 int lttng_list_tracepoints(struct lttng_handle
*handle
,
612 struct lttng_event
**events
)
615 struct lttcomm_session_msg lsm
;
621 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
622 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
624 ret
= ask_sessiond(&lsm
, (void **) events
);
629 return ret
/ sizeof(struct lttng_event
);
633 * Return a human readable string of code
635 const char *lttng_get_readable_code(int code
)
637 if (code
> -LTTCOMM_OK
) {
638 return "Ended with errors";
641 return lttcomm_get_readable_code(code
);
645 * Create a brand new session using name.
647 int lttng_create_session(const char *name
, const char *path
)
649 struct lttcomm_session_msg lsm
;
651 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
652 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
653 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
655 return ask_sessiond(&lsm
, NULL
);
659 * Destroy session using name.
661 int lttng_destroy_session(struct lttng_handle
*handle
)
663 struct lttcomm_session_msg lsm
;
669 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
670 copy_string(lsm
.session
.name
, handle
->session_name
,
671 sizeof(lsm
.session
.name
));
673 return ask_sessiond(&lsm
, NULL
);
677 * Ask the session daemon for all available sessions.
679 * Return number of session.
680 * On error, return negative value.
682 int lttng_list_sessions(struct lttng_session
**sessions
)
685 struct lttcomm_session_msg lsm
;
687 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
688 ret
= ask_sessiond(&lsm
, (void**) sessions
);
693 return ret
/ sizeof(struct lttng_session
);
697 * List domain of a session.
699 int lttng_list_domains(struct lttng_handle
*handle
,
700 struct lttng_domain
**domains
)
703 struct lttcomm_session_msg lsm
;
709 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
711 copy_string(lsm
.session
.name
, handle
->session_name
,
712 sizeof(lsm
.session
.name
));
714 ret
= ask_sessiond(&lsm
, (void**) domains
);
719 return ret
/ sizeof(struct lttng_domain
);
723 * List channels of a session
725 int lttng_list_channels(struct lttng_handle
*handle
,
726 struct lttng_channel
**channels
)
729 struct lttcomm_session_msg lsm
;
735 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
736 copy_string(lsm
.session
.name
, handle
->session_name
,
737 sizeof(lsm
.session
.name
));
739 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
741 ret
= ask_sessiond(&lsm
, (void**) channels
);
746 return ret
/ sizeof(struct lttng_channel
);
750 * List events of a session channel.
752 int lttng_list_events(struct lttng_handle
*handle
,
753 const char *channel_name
, struct lttng_event
**events
)
756 struct lttcomm_session_msg lsm
;
762 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
763 copy_string(lsm
.session
.name
, handle
->session_name
,
764 sizeof(lsm
.session
.name
));
765 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
766 sizeof(lsm
.u
.list
.channel_name
));
768 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
770 ret
= ask_sessiond(&lsm
, (void**) events
);
775 return ret
/ sizeof(struct lttng_event
);
779 * lttng_set_tracing_group
781 * Set tracing group variable with name. This function
782 * allocate memory pointed by tracing_group.
784 int lttng_set_tracing_group(const char *name
)
786 if (asprintf(&tracing_group
, "%s", name
) < 0) {
796 int lttng_calibrate(struct lttng_handle
*handle
,
797 struct lttng_calibrate
*calibrate
)
799 struct lttcomm_session_msg lsm
;
805 lsm
.cmd_type
= LTTNG_CALIBRATE
;
806 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
808 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
810 return ask_sessiond(&lsm
, NULL
);
814 * Check if session daemon is alive.
816 * Return 1 if alive or 0 if not.
819 int lttng_session_daemon_alive(void)
823 ret
= set_session_daemon_path();
829 if (strlen(sessiond_sock_path
) == 0) {
830 /* No socket path set. Weird error */
834 ret
= try_connect_sessiond(sessiond_sock_path
);
847 static void __attribute__((constructor
)) init()
849 /* Set default session group */
850 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);