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"
32 /* Socket to session daemon for communication */
33 static int sessiond_socket
;
34 static char sessiond_sock_path
[PATH_MAX
];
36 /* Communication structure to ltt-sessiond */
37 static struct lttcomm_session_msg lsm
;
38 static struct lttcomm_lttng_msg llm
;
41 static char *tracing_group
;
47 * Send lttcomm_session_msg to the session daemon.
49 * On success, return 0
50 * On error, return error code
52 static int send_data_sessiond(void)
61 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
70 * Receive data from the sessiond socket.
72 * On success, return 0
73 * On error, return recv() error code
75 static int recv_data_sessiond(void *buf
, size_t len
)
84 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
93 * Ask the session daemon a specific command and put the data into buf.
95 * Return size of data (only payload, not header).
97 static int ask_sessiond(enum lttcomm_sessiond_command lct
, void **buf
)
103 ret
= lttng_connect_sessiond();
110 /* Send command to session daemon */
111 ret
= send_data_sessiond();
116 /* Get header from data transmission */
117 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
122 /* Check error code if OK */
123 if (llm
.ret_code
!= LTTCOMM_OK
) {
128 size
= llm
.trace_name_offset
+ llm
.data_size
;
133 data
= (void*) malloc(size
);
135 /* Get payload data */
136 ret
= recv_data_sessiond(data
, size
);
146 lttng_disconnect_sessiond();
151 * check_tracing_group
153 * Check if the specified group name exist.
156 static int check_tracing_group(const char *grp_name
)
158 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
160 int grp_list_size
, grp_id
, i
;
163 /* Get GID of group 'tracing' */
164 grp_tracing
= getgrnam(grp_name
);
165 if (grp_tracing
== NULL
) {
166 /* NULL means not found also. getgrnam(3) */
173 /* Get number of supplementary group IDs */
174 grp_list_size
= getgroups(0, NULL
);
175 if (grp_list_size
< 0) {
180 /* Alloc group list of the right size */
181 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
182 grp_id
= getgroups(grp_list_size
, grp_list
);
188 for (i
= 0; i
< grp_list_size
; i
++) {
189 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
203 * set_session_daemon_path
205 * Set sessiond socket path by putting it in
206 * the global sessiond_sock_path variable.
208 static int set_session_daemon_path(void)
212 /* Are we in the tracing group ? */
213 ret
= check_tracing_group(tracing_group
);
214 if (ret
< 0 && getuid() != 0) {
215 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
216 getenv("HOME")) < 0) {
220 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
221 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
228 * BEGIN KERNEL CONTROL
232 * lttng_kernel_enable_event
234 * Enable an event in the kernel tracer.
236 int lttng_kernel_enable_event(char *event_name
)
238 strncpy(lsm
.u
.event
.event_name
, event_name
, NAME_MAX
);
239 return ask_sessiond(KERNEL_ENABLE_EVENT
, NULL
);
243 * lttng_kernel_disable_event
245 * Disable an event in the kernel tracer.
247 int lttng_kernel_disable_event(char *event_name
)
249 strncpy(lsm
.u
.event
.event_name
, event_name
, NAME_MAX
);
250 return ask_sessiond(KERNEL_DISABLE_EVENT
, NULL
);
254 * lttng_kernel_create_session
256 * Create a session in the kernel tracer.
258 int lttng_kernel_create_session(void)
260 return ask_sessiond(KERNEL_CREATE_SESSION
, NULL
);
264 * lttng_kernel_create_channel
266 * Create a channel in the kernel tracer.
268 int lttng_kernel_create_channel(void)
270 return ask_sessiond(KERNEL_CREATE_CHANNEL
, NULL
);
274 * lttng_kernel_open_metadata
276 * Open metadata in the kernel tracer.
278 int lttng_kernel_open_metadata(void)
280 return ask_sessiond(KERNEL_OPEN_METADATA
, NULL
);
284 * lttng_kernel_create_stream
286 * Create stream in the kernel tracer.
288 int lttng_kernel_create_stream(void)
290 return ask_sessiond(KERNEL_CREATE_STREAM
, NULL
);
294 * lttng_kernel_list_events
296 * List all available events in the kernel.
298 * Return the size (bytes) of the list and set the event_list array.
299 * On error, return negative value.
301 int lttng_kernel_list_events(char **event_list
)
303 return ask_sessiond(KERNEL_LIST_EVENTS
, (void **) event_list
);
307 * lttng_kernel_start_tracing
309 * Start kernel tracing.
311 int lttng_kernel_start_tracing(void)
313 return ask_sessiond(KERNEL_START_TRACE
, NULL
);
317 * lttng_kernel_stop_tracing
319 * Stop kernel tracing.
321 int lttng_kernel_stop_tracing(void)
323 return ask_sessiond(KERNEL_STOP_TRACE
, NULL
);
331 * lttng_get_readable_code
333 * Return a human readable string of code
335 const char *lttng_get_readable_code(int code
)
337 if (code
> -LTTCOMM_OK
) {
338 return "Ended with errors";
341 return lttcomm_get_readable_code(code
);
345 * lttng_ust_start_trace
347 * Request a trace start for pid.
349 int lttng_ust_start_trace(pid_t pid
)
352 return ask_sessiond(UST_START_TRACE
, NULL
);
356 * lttng_ust_stop_trace
358 * Request a trace stop for pid.
360 int lttng_ust_stop_trace(pid_t pid
)
363 return ask_sessiond(UST_STOP_TRACE
, NULL
);
367 * lttng_ust_create_trace
369 * Request a trace creation for pid.
371 int lttng_ust_create_trace(pid_t pid
)
374 return ask_sessiond(UST_CREATE_TRACE
, NULL
);
378 * lttng_ust_list_apps
380 * Ask the session daemon for all UST traceable applications.
382 * Return the number of pids.
383 * On error, return negative value.
385 int lttng_ust_list_apps(pid_t
**pids
)
389 ret
= ask_sessiond(UST_LIST_APPS
, (void**) pids
);
394 return ret
/ sizeof(pid_t
);
400 * Ask the session daemon for all traces (kernel and ust) for the session
401 * identified by uuid.
403 * Return the number of traces.
404 * On error, return negative value.
406 int lttng_list_traces(uuid_t
*uuid
, struct lttng_trace
**traces
)
410 uuid_copy(lsm
.session_uuid
, *uuid
);
412 ret
= ask_sessiond(LTTNG_LIST_TRACES
, (void **) traces
);
417 return ret
/ sizeof(struct lttng_trace
);
421 * lttng_create_session
423 * Create a brand new session using name.
425 int lttng_create_session(char *name
)
427 strncpy(lsm
.session_name
, name
, NAME_MAX
);
428 return ask_sessiond(LTTNG_CREATE_SESSION
, NULL
);
432 * lttng_destroy_session
434 * Destroy session using name.
436 int lttng_destroy_session(uuid_t
*uuid
)
438 uuid_copy(lsm
.session_uuid
, *uuid
);
439 return ask_sessiond(LTTNG_DESTROY_SESSION
, NULL
);
443 * lttng_list_sessions
445 * Ask the session daemon for all available sessions.
447 * Return number of session.
448 * On error, return negative value.
450 int lttng_list_sessions(struct lttng_session
**sessions
)
454 ret
= ask_sessiond(LTTNG_LIST_SESSIONS
, (void**) sessions
);
459 return ret
/ sizeof(struct lttng_session
);
463 * lttng_connect_sessiond
465 * Connect to the LTTng session daemon.
466 * On success, return 0
467 * On error, return a negative value
469 int lttng_connect_sessiond(void)
473 ret
= set_session_daemon_path();
478 /* Connect to the sesssion daemon */
479 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
484 sessiond_socket
= ret
;
491 * lttng_disconnect_sessiond
493 * Clean disconnect the session daemon.
495 int lttng_disconnect_sessiond(void)
500 ret
= lttcomm_close_unix_sock(sessiond_socket
);
509 * lttng_set_current_session_uuid
511 * Set the session uuid for current lsm.
513 void lttng_set_current_session_uuid(uuid_t
*uuid
)
515 uuid_copy(lsm
.session_uuid
, *uuid
);
519 * lttng_set_tracing_group
521 * Set tracing group variable with name. This function
522 * allocate memory pointed by tracing_group.
524 int lttng_set_tracing_group(const char *name
)
526 if (asprintf(&tracing_group
, "%s", name
) < 0) {
534 * lttng_check_session_daemon
538 * Error, return negative value
540 int lttng_session_daemon_alive(void)
544 ret
= set_session_daemon_path();
550 /* If socket exist, we consider the daemon started */
551 ret
= access(sessiond_sock_path
, F_OK
);
564 static void __attribute__((constructor
)) init()
566 /* Set default session group */
567 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);