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/lttng.h>
33 #include "liblttsessiondcomm.h"
35 #include "lttng-share.h"
37 /* Socket to session daemon for communication */
38 static int sessiond_socket
;
39 static char sessiond_sock_path
[PATH_MAX
];
41 /* Communication structure to ltt-sessiond */
42 static struct lttcomm_session_msg lsm
;
43 static struct lttcomm_lttng_msg llm
;
46 static char *tracing_group
;
52 * Send lttcomm_session_msg to the session daemon.
54 * On success, return 0
55 * On error, return error code
57 static int send_data_sessiond(void)
66 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
75 * Receive data from the sessiond socket.
77 * On success, return 0
78 * On error, return recv() error code
80 static int recv_data_sessiond(void *buf
, size_t len
)
89 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
96 * Check if the specified group name exist.
98 * If yes return 0, else return -1.
100 static int check_tracing_group(const char *grp_name
)
102 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
104 int grp_list_size
, grp_id
, i
;
107 /* Get GID of group 'tracing' */
108 grp_tracing
= getgrnam(grp_name
);
109 if (grp_tracing
== NULL
) {
110 /* NULL means not found also. getgrnam(3) */
117 /* Get number of supplementary group IDs */
118 grp_list_size
= getgroups(0, NULL
);
119 if (grp_list_size
< 0) {
124 /* Alloc group list of the right size */
125 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
126 grp_id
= getgroups(grp_list_size
, grp_list
);
132 for (i
= 0; i
< grp_list_size
; i
++) {
133 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
147 * Set sessiond socket path by putting it in the global sessiond_sock_path
150 static int set_session_daemon_path(void)
154 /* Are we in the tracing group ? */
155 ret
= check_tracing_group(tracing_group
);
156 if (ret
< 0 && getuid() != 0) {
157 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
158 getenv("HOME")) < 0) {
162 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
163 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
170 * Connect to the LTTng session daemon.
172 * On success, return 0. On error, return -1.
174 static int connect_sessiond(void)
178 ret
= set_session_daemon_path();
183 /* Connect to the sesssion daemon */
184 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
189 sessiond_socket
= ret
;
196 * Clean disconnect the session daemon.
198 static int disconnect_sessiond(void)
203 ret
= lttcomm_close_unix_sock(sessiond_socket
);
212 * Reset the session message structure.
214 static void reset_session_msg(void)
216 memset(&lsm
, 0, sizeof(struct lttcomm_session_msg
));
222 * Ask the session daemon a specific command and put the data into buf.
224 * Return size of data (only payload, not header).
226 static int ask_sessiond(enum lttcomm_sessiond_command lct
, void **buf
)
232 ret
= connect_sessiond();
239 /* Send command to session daemon */
240 ret
= send_data_sessiond();
245 /* Get header from data transmission */
246 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
251 /* Check error code if OK */
252 if (llm
.ret_code
!= LTTCOMM_OK
) {
257 size
= llm
.data_size
;
263 data
= (void*) malloc(size
);
265 /* Get payload data */
266 ret
= recv_data_sessiond(data
, size
);
276 disconnect_sessiond();
282 * Copy domain to lttcomm_session_msg domain. If unknown domain, default domain
283 * will be the kernel.
285 static void copy_lttng_domain(struct lttng_domain
*dom
)
289 case LTTNG_DOMAIN_KERNEL
:
290 case LTTNG_DOMAIN_UST
:
291 case LTTNG_DOMAIN_UST_EXEC_NAME
:
292 case LTTNG_DOMAIN_UST_PID
:
293 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
294 memcpy(&lsm
.domain
, dom
, sizeof(struct lttng_domain
));
297 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
304 * Copy string from src to dst and enforce null terminated byte.
306 static void copy_string(char *dst
, const char *src
, size_t len
)
309 strncpy(dst
, src
, len
);
310 /* Enforce the NULL terminated byte */
316 * Start tracing for all trace of the session.
318 int lttng_start_tracing(const char *session_name
)
320 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
321 return ask_sessiond(LTTNG_START_TRACE
, NULL
);
325 * Stop tracing for all trace of the session.
327 int lttng_stop_tracing(const char *session_name
)
329 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
330 return ask_sessiond(LTTNG_STOP_TRACE
, NULL
);
336 int lttng_add_context(struct lttng_domain
*domain
,
337 struct lttng_event_context
*ctx
, const char *event_name
,
338 const char *channel_name
)
340 copy_string(lsm
.u
.context
.channel_name
, channel_name
, NAME_MAX
);
341 copy_string(lsm
.u
.context
.event_name
, event_name
, NAME_MAX
);
342 copy_lttng_domain(domain
);
345 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
348 return ask_sessiond(LTTNG_ADD_CONTEXT
, NULL
);
354 int lttng_enable_event(struct lttng_domain
*domain
,
355 struct lttng_event
*ev
, const char *channel_name
)
359 if (channel_name
== NULL
) {
360 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
, NAME_MAX
);
362 copy_string(lsm
.u
.enable
.channel_name
, channel_name
, NAME_MAX
);
365 copy_lttng_domain(domain
);
368 ret
= ask_sessiond(LTTNG_ENABLE_ALL_EVENT
, NULL
);
370 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(struct lttng_event
));
371 ret
= ask_sessiond(LTTNG_ENABLE_EVENT
, NULL
);
378 * Disable event of a channel and domain.
380 int lttng_disable_event(struct lttng_domain
*domain
, const char *name
,
381 const char *channel_name
)
385 if (channel_name
== NULL
) {
386 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
, NAME_MAX
);
388 copy_string(lsm
.u
.disable
.channel_name
, channel_name
, NAME_MAX
);
391 copy_lttng_domain(domain
);
394 ret
= ask_sessiond(LTTNG_DISABLE_ALL_EVENT
, NULL
);
396 copy_string(lsm
.u
.disable
.name
, name
, NAME_MAX
);
397 ret
= ask_sessiond(LTTNG_DISABLE_EVENT
, NULL
);
404 * Enable channel per domain
406 int lttng_enable_channel(struct lttng_domain
*domain
,
407 struct lttng_channel
*chan
)
410 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(struct lttng_channel
));
413 copy_lttng_domain(domain
);
415 return ask_sessiond(LTTNG_ENABLE_CHANNEL
, NULL
);
419 * All tracing will be stopped for registered events of the channel.
421 int lttng_disable_channel(struct lttng_domain
*domain
, const char *name
)
423 copy_string(lsm
.u
.disable
.channel_name
, name
, NAME_MAX
);
424 copy_lttng_domain(domain
);
426 return ask_sessiond(LTTNG_DISABLE_CHANNEL
, NULL
);
430 * List all available tracepoints of domain.
432 * Return the size (bytes) of the list and set the events array.
433 * On error, return negative value.
435 int lttng_list_tracepoints(struct lttng_domain
*domain
,
436 struct lttng_event
**events
)
440 copy_lttng_domain(domain
);
442 ret
= ask_sessiond(LTTNG_LIST_TRACEPOINTS
, (void **) events
);
447 return ret
/ sizeof(struct lttng_event
);
451 * Return a human readable string of code
453 const char *lttng_get_readable_code(int code
)
455 if (code
> -LTTCOMM_OK
) {
456 return "Ended with errors";
459 return lttcomm_get_readable_code(code
);
463 * Create a brand new session using name.
465 int lttng_create_session(const char *name
, const char *path
)
467 copy_string(lsm
.session
.name
, name
, NAME_MAX
);
468 copy_string(lsm
.session
.path
, path
, PATH_MAX
);
469 return ask_sessiond(LTTNG_CREATE_SESSION
, NULL
);
473 * Destroy session using name.
475 int lttng_destroy_session(const char *name
)
477 copy_string(lsm
.session
.name
, name
, NAME_MAX
);
478 return ask_sessiond(LTTNG_DESTROY_SESSION
, NULL
);
482 * Ask the session daemon for all available sessions.
484 * Return number of session.
485 * On error, return negative value.
487 int lttng_list_sessions(struct lttng_session
**sessions
)
491 ret
= ask_sessiond(LTTNG_LIST_SESSIONS
, (void**) sessions
);
496 return ret
/ sizeof(struct lttng_session
);
500 * List domain of a session.
502 int lttng_list_domains(const char *session_name
, struct lttng_domain
**domains
)
506 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
507 ret
= ask_sessiond(LTTNG_LIST_DOMAINS
, (void**) domains
);
512 return ret
/ sizeof(struct lttng_domain
);
516 * List channels of a session
518 int lttng_list_channels(struct lttng_domain
*domain
,
519 const char *session_name
, struct lttng_channel
**channels
)
523 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
524 copy_lttng_domain(domain
);
526 ret
= ask_sessiond(LTTNG_LIST_CHANNELS
, (void**) channels
);
531 return ret
/ sizeof(struct lttng_channel
);
535 * List events of a session channel.
537 int lttng_list_events(struct lttng_domain
*domain
,
538 const char *session_name
, const char *channel_name
,
539 struct lttng_event
**events
)
543 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
544 copy_string(lsm
.u
.list
.channel_name
, channel_name
, NAME_MAX
);
545 copy_lttng_domain(domain
);
547 ret
= ask_sessiond(LTTNG_LIST_EVENTS
, (void**) events
);
552 return ret
/ sizeof(struct lttng_event
);
556 * Set session name for the current lsm.
558 void lttng_set_session_name(const char *name
)
560 copy_string(lsm
.session
.name
, name
, NAME_MAX
);
564 * lttng_set_tracing_group
566 * Set tracing group variable with name. This function
567 * allocate memory pointed by tracing_group.
569 int lttng_set_tracing_group(const char *name
)
571 if (asprintf(&tracing_group
, "%s", name
) < 0) {
581 int lttng_calibrate(struct lttng_domain
*domain
,
582 struct lttng_calibrate
*calibrate
)
586 copy_lttng_domain(domain
);
588 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(struct lttng_calibrate
));
589 ret
= ask_sessiond(LTTNG_CALIBRATE
, NULL
);
595 * lttng_check_session_daemon
599 * Error, return negative value
601 int lttng_session_daemon_alive(void)
605 ret
= set_session_daemon_path();
611 /* If socket exist, we check if the daemon listens to connect. */
612 ret
= access(sessiond_sock_path
, F_OK
);
618 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
623 ret
= lttcomm_close_unix_sock(ret
);
625 perror("lttcomm_close_unix_sock");
634 static void __attribute__((constructor
)) init()
636 /* Set default session group */
637 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);