2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <lttng/lttng.h>
29 #include "liblttsessiondcomm.h"
31 #include "lttng-share.h"
33 /* Socket to session daemon for communication */
34 static int sessiond_socket
;
35 static char sessiond_sock_path
[PATH_MAX
];
37 /* Communication structure to ltt-sessiond */
38 static struct lttcomm_session_msg lsm
;
39 static struct lttcomm_lttng_msg llm
;
42 static char *tracing_group
;
48 * Send lttcomm_session_msg to the session daemon.
50 * On success, return 0
51 * On error, return error code
53 static int send_data_sessiond(void)
62 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
71 * Receive data from the sessiond socket.
73 * On success, return 0
74 * On error, return recv() error code
76 static int recv_data_sessiond(void *buf
, size_t len
)
85 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
94 * Ask the session daemon a specific command and put the data into buf.
96 * Return size of data (only payload, not header).
98 static int ask_sessiond(enum lttcomm_sessiond_command lct
, void **buf
)
104 ret
= lttng_connect_sessiond();
111 /* Send command to session daemon */
112 ret
= send_data_sessiond();
117 /* Get header from data transmission */
118 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
123 /* Check error code if OK */
124 if (llm
.ret_code
!= LTTCOMM_OK
) {
129 size
= llm
.data_size
;
134 data
= (void*) malloc(size
);
136 /* Get payload data */
137 ret
= recv_data_sessiond(data
, size
);
147 lttng_disconnect_sessiond();
152 * check_tracing_group
154 * Check if the specified group name exist.
157 static int check_tracing_group(const char *grp_name
)
159 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
161 int grp_list_size
, grp_id
, i
;
164 /* Get GID of group 'tracing' */
165 grp_tracing
= getgrnam(grp_name
);
166 if (grp_tracing
== NULL
) {
167 /* NULL means not found also. getgrnam(3) */
174 /* Get number of supplementary group IDs */
175 grp_list_size
= getgroups(0, NULL
);
176 if (grp_list_size
< 0) {
181 /* Alloc group list of the right size */
182 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
183 grp_id
= getgroups(grp_list_size
, grp_list
);
189 for (i
= 0; i
< grp_list_size
; i
++) {
190 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
204 * set_session_daemon_path
206 * Set sessiond socket path by putting it in
207 * the global sessiond_sock_path variable.
209 static int set_session_daemon_path(void)
213 /* Are we in the tracing group ? */
214 ret
= check_tracing_group(tracing_group
);
215 if (ret
< 0 && getuid() != 0) {
216 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
217 getenv("HOME")) < 0) {
221 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
222 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
229 * lttng_start_tracing
231 * Start tracing for all trace of the session.
233 int lttng_start_tracing(char *session_name
)
235 strncpy(lsm
.session_name
, session_name
, NAME_MAX
);
236 return ask_sessiond(LTTNG_START_TRACE
, NULL
);
242 * Stop tracing for all trace of the session.
244 int lttng_stop_tracing(char *session_name
)
246 strncpy(lsm
.session_name
, session_name
, NAME_MAX
);
247 return ask_sessiond(LTTNG_STOP_TRACE
, NULL
);
251 * BEGIN Kernel control API
255 * lttng_kernel_add_context
257 int lttng_kernel_add_context(struct lttng_kernel_context
*ctx
,
258 char *event_name
, char *channel_name
)
260 if (strlen(channel_name
) != 0) {
261 strncpy(lsm
.u
.context
.channel_name
, channel_name
, NAME_MAX
);
264 if (strlen(event_name
) != 0) {
265 strncpy(lsm
.u
.context
.event_name
, event_name
, NAME_MAX
);
268 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_kernel_context
));
269 return ask_sessiond(LTTNG_KERNEL_ADD_CONTEXT
, NULL
);
273 * lttng_kernel_enable_event
275 int lttng_kernel_enable_event(struct lttng_event
*ev
, char *channel_name
)
279 if (strlen(channel_name
) == 0) {
280 strncpy(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
, NAME_MAX
);
282 strncpy(lsm
.u
.enable
.channel_name
, channel_name
, NAME_MAX
);
286 ret
= ask_sessiond(LTTNG_KERNEL_ENABLE_ALL_EVENT
, NULL
);
288 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(struct lttng_event
));
289 ret
= ask_sessiond(LTTNG_KERNEL_ENABLE_EVENT
, NULL
);
296 * lttng_kernel_disable_event
298 * Disable an event in the kernel tracer.
300 int lttng_kernel_disable_event(char *name
, char *channel_name
)
304 if (strlen(channel_name
) == 0) {
305 strncpy(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
, NAME_MAX
);
307 strncpy(lsm
.u
.disable
.channel_name
, channel_name
, NAME_MAX
);
311 ret
= ask_sessiond(LTTNG_KERNEL_DISABLE_ALL_EVENT
, NULL
);
313 strncpy(lsm
.u
.disable
.name
, name
, NAME_MAX
);
314 ret
= ask_sessiond(LTTNG_KERNEL_DISABLE_EVENT
, NULL
);
321 * lttng_kernel_enable_channel
323 * Enable recording for a channel for the kernel tracer.
325 int lttng_kernel_enable_channel(char *name
)
327 strncpy(lsm
.u
.enable
.channel_name
, name
, NAME_MAX
);
328 return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL
, NULL
);
332 * lttng_kernel_disable_channel
334 * Disable recording for the channel for the kernel tracer.
336 int lttng_kernel_disable_channel(char *name
)
338 strncpy(lsm
.u
.disable
.channel_name
, name
, NAME_MAX
);
339 return ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL
, NULL
);
343 * lttng_kernel_create_channel
345 * Create a channel in the kernel tracer.
347 int lttng_kernel_create_channel(struct lttng_channel
*chan
)
349 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(struct lttng_channel
));
350 return ask_sessiond(LTTNG_KERNEL_CREATE_CHANNEL
, NULL
);
356 * List all available events in the kernel.
358 * Return the size (bytes) of the list and set the event_list array.
359 * On error, return negative value.
361 int lttng_kernel_list_events(char **event_list
)
363 return ask_sessiond(LTTNG_KERNEL_LIST_EVENTS
, (void **) event_list
);
367 * END Kernel control API
371 * lttng_get_readable_code
373 * Return a human readable string of code
375 const char *lttng_get_readable_code(int code
)
377 if (code
> -LTTCOMM_OK
) {
378 return "Ended with errors";
381 return lttcomm_get_readable_code(code
);
385 * lttng_ust_list_apps
387 * Ask the session daemon for all UST traceable applications.
389 * Return the number of pids.
390 * On error, return negative value.
392 int lttng_ust_list_traceable_apps(pid_t
**pids
)
396 ret
= ask_sessiond(LTTNG_LIST_TRACEABLE_APPS
, (void**) pids
);
401 return ret
/ sizeof(pid_t
);
407 * Ask the session daemon for all traces (kernel and ust) for the session
408 * identified by name.
410 * Return the number of traces.
411 * On error, return negative value.
414 int lttng_list_traces(char *session_name, struct lttng_trace **traces)
418 strncpy(lsm.session_name, session_name, NAME_MAX);
420 ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
425 return ret / sizeof(struct lttng_trace);
430 * lttng_create_session
432 * Create a brand new session using name.
434 int lttng_create_session(char *name
, char *path
)
436 strncpy(lsm
.session_name
, name
, NAME_MAX
);
437 strncpy(lsm
.path
, path
, PATH_MAX
);
438 return ask_sessiond(LTTNG_CREATE_SESSION
, NULL
);
442 * lttng_destroy_session
444 * Destroy session using name.
446 int lttng_destroy_session(char *name
)
448 strncpy(lsm
.session_name
, name
, NAME_MAX
);
449 return ask_sessiond(LTTNG_DESTROY_SESSION
, NULL
);
453 * lttng_list_sessions
455 * Ask the session daemon for all available sessions.
457 * Return number of session.
458 * On error, return negative value.
460 int lttng_list_sessions(struct lttng_session
**sessions
)
464 ret
= ask_sessiond(LTTNG_LIST_SESSIONS
, (void**) sessions
);
469 return ret
/ sizeof(struct lttng_session
);
473 * lttng_connect_sessiond
475 * Connect to the LTTng session daemon.
476 * On success, return 0
477 * On error, return a negative value
479 int lttng_connect_sessiond(void)
483 ret
= set_session_daemon_path();
488 /* Connect to the sesssion daemon */
489 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
494 sessiond_socket
= ret
;
501 * lttng_disconnect_sessiond
503 * Clean disconnect the session daemon.
505 int lttng_disconnect_sessiond(void)
510 ret
= lttcomm_close_unix_sock(sessiond_socket
);
518 void lttng_set_session_name(char *name
)
520 strncpy(lsm
.session_name
, name
, NAME_MAX
);
524 * lttng_set_tracing_group
526 * Set tracing group variable with name. This function
527 * allocate memory pointed by tracing_group.
529 int lttng_set_tracing_group(const char *name
)
531 if (asprintf(&tracing_group
, "%s", name
) < 0) {
539 * lttng_check_session_daemon
543 * Error, return negative value
545 int lttng_session_daemon_alive(void)
549 ret
= set_session_daemon_path();
555 /* If socket exist, we consider the daemon started */
556 ret
= access(sessiond_sock_path
, F_OK
);
569 static void __attribute__((constructor
)) init()
571 /* Set default session group */
572 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);