Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
82a3637f DG |
2 | * liblttngctl.c |
3 | * | |
4 | * Linux Trace Toolkit Control Library | |
5 | * | |
826d496d | 6 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
fac6795d | 7 | * |
82a3637f DG |
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. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
fac6795d | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
82a3637f DG |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | * Lesser General Public License for more details. | |
17 | * | |
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 | |
fac6795d DG |
21 | */ |
22 | ||
23 | #define _GNU_SOURCE | |
fac6795d | 24 | #include <grp.h> |
1e307fab | 25 | #include <errno.h> |
fac6795d DG |
26 | #include <stdio.h> |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <unistd.h> | |
30 | ||
990570ed DG |
31 | #include <common/common.h> |
32 | #include <common/defaults.h> | |
db758600 | 33 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 34 | #include <lttng/lttng.h> |
fac6795d DG |
35 | |
36 | /* Socket to session daemon for communication */ | |
37 | static int sessiond_socket; | |
38 | static char sessiond_sock_path[PATH_MAX]; | |
39 | ||
fac6795d DG |
40 | /* Variables */ |
41 | static char *tracing_group; | |
42 | static int connected; | |
43 | ||
99497cd0 MD |
44 | /* |
45 | * Copy string from src to dst and enforce null terminated byte. | |
46 | */ | |
47 | static void copy_string(char *dst, const char *src, size_t len) | |
48 | { | |
e7d6716d | 49 | if (src && dst) { |
99497cd0 MD |
50 | strncpy(dst, src, len); |
51 | /* Enforce the NULL terminated byte */ | |
52 | dst[len - 1] = '\0'; | |
cd80958d DG |
53 | } else if (dst) { |
54 | dst[0] = '\0'; | |
99497cd0 MD |
55 | } |
56 | } | |
57 | ||
fac6795d | 58 | /* |
cd80958d | 59 | * Copy domain to lttcomm_session_msg domain. |
fac6795d | 60 | * |
cd80958d DG |
61 | * If domain is unknown, default domain will be the kernel. |
62 | */ | |
63 | static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src) | |
64 | { | |
65 | if (src && dst) { | |
66 | switch (src->type) { | |
67 | case LTTNG_DOMAIN_KERNEL: | |
68 | case LTTNG_DOMAIN_UST: | |
d78d6610 | 69 | /* |
cd80958d DG |
70 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
71 | case LTTNG_DOMAIN_UST_PID: | |
72 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 73 | */ |
cd80958d DG |
74 | memcpy(dst, src, sizeof(struct lttng_domain)); |
75 | break; | |
76 | default: | |
77 | dst->type = LTTNG_DOMAIN_KERNEL; | |
78 | break; | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | /* | |
84 | * Send lttcomm_session_msg to the session daemon. | |
fac6795d | 85 | * |
1c8d13c8 TD |
86 | * On success, returns the number of bytes sent (>=0) |
87 | * On error, returns -1 | |
fac6795d | 88 | */ |
cd80958d | 89 | static int send_session_msg(struct lttcomm_session_msg *lsm) |
fac6795d DG |
90 | { |
91 | int ret; | |
92 | ||
93 | if (!connected) { | |
e065084a DG |
94 | ret = -ENOTCONN; |
95 | goto end; | |
fac6795d DG |
96 | } |
97 | ||
be040666 | 98 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, |
cd80958d | 99 | sizeof(struct lttcomm_session_msg)); |
e065084a DG |
100 | |
101 | end: | |
102 | return ret; | |
103 | } | |
104 | ||
105 | /* | |
cd80958d | 106 | * Receive data from the sessiond socket. |
e065084a | 107 | * |
1c8d13c8 TD |
108 | * On success, returns the number of bytes received (>=0) |
109 | * On error, returns -1 (recvmsg() error) or -ENOTCONN | |
e065084a | 110 | */ |
ca95a216 | 111 | static int recv_data_sessiond(void *buf, size_t len) |
e065084a DG |
112 | { |
113 | int ret; | |
114 | ||
115 | if (!connected) { | |
116 | ret = -ENOTCONN; | |
117 | goto end; | |
fac6795d DG |
118 | } |
119 | ||
ca95a216 | 120 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
fac6795d | 121 | |
e065084a | 122 | end: |
fac6795d DG |
123 | return ret; |
124 | } | |
125 | ||
126 | /* | |
1c8d13c8 | 127 | * Check if we are in the specified group. |
65beb5ff | 128 | * |
2269e89e | 129 | * If yes return 1, else return -1. |
947308c4 DG |
130 | */ |
131 | static int check_tracing_group(const char *grp_name) | |
132 | { | |
133 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ | |
134 | gid_t *grp_list; | |
135 | int grp_list_size, grp_id, i; | |
136 | int ret = -1; | |
137 | ||
138 | /* Get GID of group 'tracing' */ | |
139 | grp_tracing = getgrnam(grp_name); | |
140 | if (grp_tracing == NULL) { | |
141 | /* NULL means not found also. getgrnam(3) */ | |
142 | if (errno != 0) { | |
143 | perror("getgrnam"); | |
144 | } | |
145 | goto end; | |
146 | } | |
147 | ||
148 | /* Get number of supplementary group IDs */ | |
149 | grp_list_size = getgroups(0, NULL); | |
150 | if (grp_list_size < 0) { | |
151 | perror("getgroups"); | |
152 | goto end; | |
153 | } | |
154 | ||
155 | /* Alloc group list of the right size */ | |
156 | grp_list = malloc(grp_list_size * sizeof(gid_t)); | |
00795392 | 157 | if (!grp_list) { |
1c8d13c8 | 158 | perror("malloc"); |
00795392 MD |
159 | goto end; |
160 | } | |
947308c4 | 161 | grp_id = getgroups(grp_list_size, grp_list); |
1c8d13c8 | 162 | if (grp_id < 0) { |
947308c4 DG |
163 | perror("getgroups"); |
164 | goto free_list; | |
165 | } | |
166 | ||
167 | for (i = 0; i < grp_list_size; i++) { | |
168 | if (grp_list[i] == grp_tracing->gr_gid) { | |
2269e89e | 169 | ret = 1; |
947308c4 DG |
170 | break; |
171 | } | |
172 | } | |
173 | ||
174 | free_list: | |
175 | free(grp_list); | |
176 | ||
177 | end: | |
178 | return ret; | |
179 | } | |
180 | ||
181 | /* | |
2269e89e DG |
182 | * Try connect to session daemon with sock_path. |
183 | * | |
184 | * Return 0 on success, else -1 | |
185 | */ | |
186 | static int try_connect_sessiond(const char *sock_path) | |
187 | { | |
188 | int ret; | |
189 | ||
190 | /* If socket exist, we check if the daemon listens for connect. */ | |
191 | ret = access(sock_path, F_OK); | |
192 | if (ret < 0) { | |
193 | /* Not alive */ | |
194 | return -1; | |
195 | } | |
196 | ||
197 | ret = lttcomm_connect_unix_sock(sock_path); | |
198 | if (ret < 0) { | |
199 | /* Not alive */ | |
200 | return -1; | |
201 | } | |
202 | ||
203 | ret = lttcomm_close_unix_sock(ret); | |
204 | if (ret < 0) { | |
205 | perror("lttcomm_close_unix_sock"); | |
206 | } | |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
211 | /* | |
1c8d13c8 TD |
212 | * Set sessiond socket path by putting it in the global |
213 | * sessiond_sock_path variable. | |
214 | * Returns 0 on success, | |
215 | * -ENOMEM on failure (the sessiond socket path is somehow too long) | |
947308c4 DG |
216 | */ |
217 | static int set_session_daemon_path(void) | |
218 | { | |
219 | int ret; | |
2269e89e DG |
220 | int in_tgroup = 0; /* In tracing group */ |
221 | uid_t uid; | |
222 | ||
223 | uid = getuid(); | |
947308c4 | 224 | |
2269e89e DG |
225 | if (uid != 0) { |
226 | /* Are we in the tracing group ? */ | |
227 | in_tgroup = check_tracing_group(tracing_group); | |
228 | } | |
229 | ||
230 | if (uid == 0) { | |
231 | /* Root */ | |
232 | copy_string(sessiond_sock_path, | |
233 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
234 | sizeof(sessiond_sock_path)); | |
235 | } else if (in_tgroup) { | |
236 | /* Tracing group */ | |
237 | copy_string(sessiond_sock_path, | |
238 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
239 | sizeof(sessiond_sock_path)); | |
240 | ||
241 | ret = try_connect_sessiond(sessiond_sock_path); | |
242 | if (ret < 0) { | |
243 | /* Global session daemon not available */ | |
244 | if (snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), | |
245 | DEFAULT_HOME_CLIENT_UNIX_SOCK, | |
246 | getenv("HOME")) < 0) { | |
247 | return -ENOMEM; | |
248 | } | |
249 | } | |
250 | } else { | |
251 | /* Not in tracing group and not root, default */ | |
99497cd0 | 252 | if (snprintf(sessiond_sock_path, PATH_MAX, |
cd80958d DG |
253 | DEFAULT_HOME_CLIENT_UNIX_SOCK, |
254 | getenv("HOME")) < 0) { | |
947308c4 DG |
255 | return -ENOMEM; |
256 | } | |
947308c4 DG |
257 | } |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
65beb5ff DG |
262 | /* |
263 | * Connect to the LTTng session daemon. | |
264 | * | |
265 | * On success, return 0. On error, return -1. | |
266 | */ | |
267 | static int connect_sessiond(void) | |
268 | { | |
269 | int ret; | |
270 | ||
271 | ret = set_session_daemon_path(); | |
272 | if (ret < 0) { | |
1c8d13c8 | 273 | return -1; /* set_session_daemon_path() returns -ENOMEM */ |
65beb5ff DG |
274 | } |
275 | ||
276 | /* Connect to the sesssion daemon */ | |
277 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); | |
278 | if (ret < 0) { | |
279 | return ret; | |
280 | } | |
281 | ||
282 | sessiond_socket = ret; | |
283 | connected = 1; | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | /* | |
1c8d13c8 TD |
289 | * Clean disconnect from the session daemon. |
290 | * On success, return 0. On error, return -1. | |
65beb5ff DG |
291 | */ |
292 | static int disconnect_sessiond(void) | |
293 | { | |
294 | int ret = 0; | |
295 | ||
296 | if (connected) { | |
297 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
298 | sessiond_socket = 0; | |
299 | connected = 0; | |
300 | } | |
301 | ||
302 | return ret; | |
303 | } | |
304 | ||
35a6fdb7 | 305 | /* |
cd80958d | 306 | * Ask the session daemon a specific command and put the data into buf. |
65beb5ff | 307 | * |
af87c45a | 308 | * Return size of data (only payload, not header) or a negative error code. |
65beb5ff | 309 | */ |
cd80958d | 310 | static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf) |
65beb5ff DG |
311 | { |
312 | int ret; | |
313 | size_t size; | |
314 | void *data = NULL; | |
cd80958d | 315 | struct lttcomm_lttng_msg llm; |
65beb5ff DG |
316 | |
317 | ret = connect_sessiond(); | |
318 | if (ret < 0) { | |
319 | goto end; | |
320 | } | |
321 | ||
65beb5ff | 322 | /* Send command to session daemon */ |
cd80958d | 323 | ret = send_session_msg(lsm); |
65beb5ff DG |
324 | if (ret < 0) { |
325 | goto end; | |
326 | } | |
327 | ||
328 | /* Get header from data transmission */ | |
329 | ret = recv_data_sessiond(&llm, sizeof(llm)); | |
330 | if (ret < 0) { | |
331 | goto end; | |
332 | } | |
333 | ||
334 | /* Check error code if OK */ | |
335 | if (llm.ret_code != LTTCOMM_OK) { | |
336 | ret = -llm.ret_code; | |
337 | goto end; | |
338 | } | |
339 | ||
340 | size = llm.data_size; | |
341 | if (size == 0) { | |
874d3f84 | 342 | /* If client free with size 0 */ |
a45d5536 DG |
343 | if (buf != NULL) { |
344 | *buf = NULL; | |
345 | } | |
7d29a247 | 346 | ret = 0; |
65beb5ff DG |
347 | goto end; |
348 | } | |
349 | ||
350 | data = (void*) malloc(size); | |
351 | ||
352 | /* Get payload data */ | |
353 | ret = recv_data_sessiond(data, size); | |
354 | if (ret < 0) { | |
355 | free(data); | |
356 | goto end; | |
357 | } | |
358 | ||
83009e5e DG |
359 | /* |
360 | * Extra protection not to dereference a NULL pointer. If buf is NULL at | |
361 | * this point, an error is returned and data is freed. | |
362 | */ | |
363 | if (buf == NULL) { | |
364 | ret = -1; | |
365 | free(data); | |
366 | goto end; | |
367 | } | |
368 | ||
65beb5ff DG |
369 | *buf = data; |
370 | ret = size; | |
371 | ||
372 | end: | |
373 | disconnect_sessiond(); | |
374 | return ret; | |
375 | } | |
376 | ||
9f19cc17 | 377 | /* |
cd80958d | 378 | * Create lttng handle and return pointer. |
1c8d13c8 | 379 | * The returned pointer will be NULL in case of malloc() error. |
9f19cc17 | 380 | */ |
cd80958d DG |
381 | struct lttng_handle *lttng_create_handle(const char *session_name, |
382 | struct lttng_domain *domain) | |
9f19cc17 | 383 | { |
cd80958d DG |
384 | struct lttng_handle *handle; |
385 | ||
386 | handle = malloc(sizeof(struct lttng_handle)); | |
387 | if (handle == NULL) { | |
388 | perror("malloc handle"); | |
389 | goto end; | |
390 | } | |
391 | ||
392 | /* Copy session name */ | |
393 | copy_string(handle->session_name, session_name, | |
394 | sizeof(handle->session_name)); | |
395 | ||
396 | /* Copy lttng domain */ | |
397 | copy_lttng_domain(&handle->domain, domain); | |
398 | ||
399 | end: | |
400 | return handle; | |
401 | } | |
402 | ||
403 | /* | |
404 | * Destroy handle by free(3) the pointer. | |
405 | */ | |
406 | void lttng_destroy_handle(struct lttng_handle *handle) | |
407 | { | |
408 | if (handle) { | |
409 | free(handle); | |
eb354453 DG |
410 | } |
411 | } | |
412 | ||
d9800920 DG |
413 | /* |
414 | * Register an outside consumer. | |
1c8d13c8 | 415 | * Returns size of returned session payload data or a negative error code. |
d9800920 DG |
416 | */ |
417 | int lttng_register_consumer(struct lttng_handle *handle, | |
418 | const char *socket_path) | |
419 | { | |
420 | struct lttcomm_session_msg lsm; | |
421 | ||
422 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; | |
423 | copy_string(lsm.session.name, handle->session_name, | |
424 | sizeof(lsm.session.name)); | |
425 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
426 | ||
427 | copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path)); | |
428 | ||
429 | return ask_sessiond(&lsm, NULL); | |
430 | } | |
431 | ||
1df4dedd | 432 | /* |
1c8d13c8 TD |
433 | * Start tracing for all traces of the session. |
434 | * Returns size of returned session payload data or a negative error code. | |
1df4dedd | 435 | */ |
6a4f824d | 436 | int lttng_start_tracing(const char *session_name) |
f3ed775e | 437 | { |
cd80958d DG |
438 | struct lttcomm_session_msg lsm; |
439 | ||
6a4f824d | 440 | if (session_name == NULL) { |
cd80958d DG |
441 | return -1; |
442 | } | |
443 | ||
444 | lsm.cmd_type = LTTNG_START_TRACE; | |
6a4f824d DG |
445 | |
446 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
447 | |
448 | return ask_sessiond(&lsm, NULL); | |
f3ed775e | 449 | } |
1df4dedd DG |
450 | |
451 | /* | |
1c8d13c8 TD |
452 | * Stop tracing for all traces of the session. |
453 | * Returns size of returned session payload data or a negative error code. | |
f3ed775e | 454 | */ |
6a4f824d | 455 | int lttng_stop_tracing(const char *session_name) |
f3ed775e | 456 | { |
cd80958d DG |
457 | struct lttcomm_session_msg lsm; |
458 | ||
6a4f824d DG |
459 | if (session_name == NULL) { |
460 | return -1; | |
461 | } | |
462 | ||
cd80958d | 463 | lsm.cmd_type = LTTNG_STOP_TRACE; |
6a4f824d DG |
464 | |
465 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
466 | |
467 | return ask_sessiond(&lsm, NULL); | |
f3ed775e DG |
468 | } |
469 | ||
470 | /* | |
cd80958d | 471 | * Add context to event or/and channel. |
af87c45a DG |
472 | * |
473 | * Returns the size of the returned payload data or a negative error code. | |
1df4dedd | 474 | */ |
cd80958d | 475 | int lttng_add_context(struct lttng_handle *handle, |
38057ed1 DG |
476 | struct lttng_event_context *ctx, const char *event_name, |
477 | const char *channel_name) | |
d65106b1 | 478 | { |
cd80958d DG |
479 | struct lttcomm_session_msg lsm; |
480 | ||
9d697d3d DG |
481 | /* Safety check. Both are mandatory */ |
482 | if (handle == NULL || ctx == NULL) { | |
cd80958d DG |
483 | return -1; |
484 | } | |
485 | ||
486 | lsm.cmd_type = LTTNG_ADD_CONTEXT; | |
487 | ||
488 | /* Copy channel name */ | |
489 | copy_string(lsm.u.context.channel_name, channel_name, | |
490 | sizeof(lsm.u.context.channel_name)); | |
491 | /* Copy event name */ | |
492 | copy_string(lsm.u.context.event_name, event_name, | |
493 | sizeof(lsm.u.context.event_name)); | |
494 | ||
495 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d65106b1 | 496 | |
9d697d3d | 497 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); |
d65106b1 | 498 | |
cd80958d DG |
499 | copy_string(lsm.session.name, handle->session_name, |
500 | sizeof(lsm.session.name)); | |
501 | ||
502 | return ask_sessiond(&lsm, NULL); | |
d65106b1 DG |
503 | } |
504 | ||
f3ed775e | 505 | /* |
1c8d13c8 TD |
506 | * Enable event(s) for a channel. |
507 | * If no event name is specified, all events are enabled. | |
508 | * If no channel name is specified, the default 'channel0' is used. | |
509 | * Returns size of returned session payload data or a negative error code. | |
f3ed775e | 510 | */ |
cd80958d | 511 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 512 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 513 | { |
cd80958d DG |
514 | struct lttcomm_session_msg lsm; |
515 | ||
5117eeec | 516 | if (handle == NULL || ev == NULL) { |
cd80958d DG |
517 | return -1; |
518 | } | |
33a2b854 | 519 | |
5117eeec | 520 | /* If no channel name, we put the default name */ |
94cf3c47 | 521 | if (channel_name == NULL) { |
cd80958d DG |
522 | copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, |
523 | sizeof(lsm.u.enable.channel_name)); | |
33a2b854 | 524 | } else { |
cd80958d DG |
525 | copy_string(lsm.u.enable.channel_name, channel_name, |
526 | sizeof(lsm.u.enable.channel_name)); | |
eb354453 DG |
527 | } |
528 | ||
cd80958d | 529 | copy_lttng_domain(&lsm.domain, &handle->domain); |
0d0c377a | 530 | |
8c9ae521 | 531 | if (ev->name[0] != '\0') { |
cd80958d | 532 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
0d0c377a | 533 | } else { |
cd80958d | 534 | lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT; |
f3ed775e | 535 | } |
8c9ae521 | 536 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
f3ed775e | 537 | |
cd80958d DG |
538 | copy_string(lsm.session.name, handle->session_name, |
539 | sizeof(lsm.session.name)); | |
540 | ||
541 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
542 | } |
543 | ||
544 | /* | |
1c8d13c8 TD |
545 | * Disable event(s) of a channel and domain. |
546 | * If no event name is specified, all events are disabled. | |
547 | * If no channel name is specified, the default 'channel0' is used. | |
548 | * Returns size of returned session payload data or a negative error code. | |
1df4dedd | 549 | */ |
cd80958d | 550 | int lttng_disable_event(struct lttng_handle *handle, const char *name, |
38057ed1 | 551 | const char *channel_name) |
1df4dedd | 552 | { |
cd80958d | 553 | struct lttcomm_session_msg lsm; |
1df4dedd | 554 | |
9d697d3d | 555 | if (handle == NULL) { |
cd80958d DG |
556 | return -1; |
557 | } | |
558 | ||
559 | if (channel_name) { | |
560 | copy_string(lsm.u.disable.channel_name, channel_name, | |
561 | sizeof(lsm.u.disable.channel_name)); | |
f3ed775e | 562 | } else { |
cd80958d DG |
563 | copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, |
564 | sizeof(lsm.u.disable.channel_name)); | |
eb354453 DG |
565 | } |
566 | ||
cd80958d | 567 | copy_lttng_domain(&lsm.domain, &handle->domain); |
f5177a38 | 568 | |
f84efadf | 569 | if (name != NULL) { |
cd80958d DG |
570 | copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name)); |
571 | lsm.cmd_type = LTTNG_DISABLE_EVENT; | |
f5177a38 | 572 | } else { |
cd80958d | 573 | lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT; |
f3ed775e DG |
574 | } |
575 | ||
cd80958d DG |
576 | copy_string(lsm.session.name, handle->session_name, |
577 | sizeof(lsm.session.name)); | |
578 | ||
579 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
580 | } |
581 | ||
582 | /* | |
1c8d13c8 TD |
583 | * Enable channel per domain |
584 | * Returns size of returned session payload data or a negative error code. | |
a5c5a2bd | 585 | */ |
cd80958d | 586 | int lttng_enable_channel(struct lttng_handle *handle, |
38057ed1 | 587 | struct lttng_channel *chan) |
a5c5a2bd | 588 | { |
cd80958d DG |
589 | struct lttcomm_session_msg lsm; |
590 | ||
5117eeec DG |
591 | /* |
592 | * NULL arguments are forbidden. No default values. | |
593 | */ | |
594 | if (handle == NULL || chan == NULL) { | |
cd80958d DG |
595 | return -1; |
596 | } | |
597 | ||
5117eeec | 598 | memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); |
7d29a247 | 599 | |
cd80958d DG |
600 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
601 | ||
602 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
7d29a247 | 603 | |
cd80958d DG |
604 | copy_string(lsm.session.name, handle->session_name, |
605 | sizeof(lsm.session.name)); | |
606 | ||
607 | return ask_sessiond(&lsm, NULL); | |
8c0faa1d | 608 | } |
1df4dedd | 609 | |
2ef84c95 | 610 | /* |
1c8d13c8 TD |
611 | * All tracing will be stopped for registered events of the channel. |
612 | * Returns size of returned session payload data or a negative error code. | |
2ef84c95 | 613 | */ |
cd80958d | 614 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 615 | { |
cd80958d DG |
616 | struct lttcomm_session_msg lsm; |
617 | ||
9d697d3d DG |
618 | /* Safety check. Both are mandatory */ |
619 | if (handle == NULL || name == NULL) { | |
cd80958d DG |
620 | return -1; |
621 | } | |
622 | ||
cd80958d | 623 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
1df4dedd | 624 | |
9d697d3d DG |
625 | copy_string(lsm.u.disable.channel_name, name, |
626 | sizeof(lsm.u.disable.channel_name)); | |
627 | ||
cd80958d DG |
628 | copy_lttng_domain(&lsm.domain, &handle->domain); |
629 | ||
630 | copy_string(lsm.session.name, handle->session_name, | |
631 | sizeof(lsm.session.name)); | |
632 | ||
633 | return ask_sessiond(&lsm, NULL); | |
ca95a216 DG |
634 | } |
635 | ||
fac6795d | 636 | /* |
1c8d13c8 TD |
637 | * Lists all available tracepoints of domain. |
638 | * Sets the contents of the events array. | |
639 | * Returns the number of lttng_event entries in events; | |
640 | * on error, returns a negative value. | |
fac6795d | 641 | */ |
cd80958d | 642 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 643 | struct lttng_event **events) |
fac6795d | 644 | { |
052da939 | 645 | int ret; |
cd80958d DG |
646 | struct lttcomm_session_msg lsm; |
647 | ||
9d697d3d | 648 | if (handle == NULL) { |
cd80958d DG |
649 | return -1; |
650 | } | |
fac6795d | 651 | |
cd80958d DG |
652 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
653 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
2a71efd5 | 654 | |
cd80958d | 655 | ret = ask_sessiond(&lsm, (void **) events); |
052da939 DG |
656 | if (ret < 0) { |
657 | return ret; | |
eb354453 | 658 | } |
fac6795d | 659 | |
9f19cc17 | 660 | return ret / sizeof(struct lttng_event); |
fac6795d DG |
661 | } |
662 | ||
1657e9bb | 663 | /* |
1c8d13c8 TD |
664 | * Returns a human readable string describing |
665 | * the error code (a negative value). | |
1657e9bb | 666 | */ |
9a745bc7 | 667 | const char *lttng_strerror(int code) |
1657e9bb | 668 | { |
1c8d13c8 | 669 | /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */ |
7d29a247 DG |
670 | if (code > -LTTCOMM_OK) { |
671 | return "Ended with errors"; | |
1657e9bb DG |
672 | } |
673 | ||
7d29a247 | 674 | return lttcomm_get_readable_code(code); |
1657e9bb DG |
675 | } |
676 | ||
aaf97519 | 677 | /* |
1c8d13c8 TD |
678 | * Create a brand new session using name and path. |
679 | * Returns size of returned session payload data or a negative error code. | |
aaf97519 | 680 | */ |
38057ed1 | 681 | int lttng_create_session(const char *name, const char *path) |
aaf97519 | 682 | { |
cd80958d DG |
683 | struct lttcomm_session_msg lsm; |
684 | ||
685 | lsm.cmd_type = LTTNG_CREATE_SESSION; | |
686 | copy_string(lsm.session.name, name, sizeof(lsm.session.name)); | |
687 | copy_string(lsm.session.path, path, sizeof(lsm.session.path)); | |
688 | ||
689 | return ask_sessiond(&lsm, NULL); | |
8028d920 DG |
690 | } |
691 | ||
692 | /* | |
8028d920 | 693 | * Destroy session using name. |
1c8d13c8 | 694 | * Returns size of returned session payload data or a negative error code. |
8028d920 | 695 | */ |
843f5df9 | 696 | int lttng_destroy_session(const char *session_name) |
8028d920 | 697 | { |
cd80958d DG |
698 | struct lttcomm_session_msg lsm; |
699 | ||
843f5df9 | 700 | if (session_name == NULL) { |
cd80958d DG |
701 | return -1; |
702 | } | |
703 | ||
704 | lsm.cmd_type = LTTNG_DESTROY_SESSION; | |
843f5df9 DG |
705 | |
706 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
707 | |
708 | return ask_sessiond(&lsm, NULL); | |
aaf97519 DG |
709 | } |
710 | ||
57167058 | 711 | /* |
57167058 | 712 | * Ask the session daemon for all available sessions. |
1c8d13c8 TD |
713 | * Sets the contents of the sessions array. |
714 | * Returns the number of lttng_session entries in sessions; | |
715 | * on error, returns a negative value. | |
57167058 | 716 | */ |
ca95a216 | 717 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 718 | { |
ca95a216 | 719 | int ret; |
cd80958d | 720 | struct lttcomm_session_msg lsm; |
57167058 | 721 | |
cd80958d DG |
722 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
723 | ret = ask_sessiond(&lsm, (void**) sessions); | |
57167058 | 724 | if (ret < 0) { |
ca95a216 | 725 | return ret; |
57167058 DG |
726 | } |
727 | ||
ca95a216 | 728 | return ret / sizeof(struct lttng_session); |
57167058 DG |
729 | } |
730 | ||
9f19cc17 | 731 | /* |
1c8d13c8 TD |
732 | * Ask the session daemon for all available domains of a session. |
733 | * Sets the contents of the domains array. | |
734 | * Returns the number of lttng_domain entries in domains; | |
735 | * on error, returns a negative value. | |
9f19cc17 | 736 | */ |
330be774 | 737 | int lttng_list_domains(const char *session_name, |
cd80958d | 738 | struct lttng_domain **domains) |
9f19cc17 DG |
739 | { |
740 | int ret; | |
cd80958d DG |
741 | struct lttcomm_session_msg lsm; |
742 | ||
330be774 | 743 | if (session_name == NULL) { |
cd80958d DG |
744 | return -1; |
745 | } | |
9f19cc17 | 746 | |
cd80958d DG |
747 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
748 | ||
330be774 | 749 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); |
cd80958d DG |
750 | |
751 | ret = ask_sessiond(&lsm, (void**) domains); | |
9f19cc17 DG |
752 | if (ret < 0) { |
753 | return ret; | |
754 | } | |
755 | ||
756 | return ret / sizeof(struct lttng_domain); | |
757 | } | |
758 | ||
759 | /* | |
1c8d13c8 TD |
760 | * Ask the session daemon for all available channels of a session. |
761 | * Sets the contents of the channels array. | |
762 | * Returns the number of lttng_channel entries in channels; | |
763 | * on error, returns a negative value. | |
9f19cc17 | 764 | */ |
cd80958d DG |
765 | int lttng_list_channels(struct lttng_handle *handle, |
766 | struct lttng_channel **channels) | |
9f19cc17 DG |
767 | { |
768 | int ret; | |
cd80958d DG |
769 | struct lttcomm_session_msg lsm; |
770 | ||
9d697d3d | 771 | if (handle == NULL) { |
cd80958d DG |
772 | return -1; |
773 | } | |
774 | ||
775 | lsm.cmd_type = LTTNG_LIST_CHANNELS; | |
776 | copy_string(lsm.session.name, handle->session_name, | |
777 | sizeof(lsm.session.name)); | |
9f19cc17 | 778 | |
cd80958d | 779 | copy_lttng_domain(&lsm.domain, &handle->domain); |
9f19cc17 | 780 | |
cd80958d | 781 | ret = ask_sessiond(&lsm, (void**) channels); |
9f19cc17 DG |
782 | if (ret < 0) { |
783 | return ret; | |
784 | } | |
785 | ||
786 | return ret / sizeof(struct lttng_channel); | |
787 | } | |
788 | ||
789 | /* | |
1c8d13c8 TD |
790 | * Ask the session daemon for all available events of a session channel. |
791 | * Sets the contents of the events array. | |
792 | * Returns the number of lttng_event entries in events; | |
793 | * on error, returns a negative value. | |
9f19cc17 | 794 | */ |
cd80958d DG |
795 | int lttng_list_events(struct lttng_handle *handle, |
796 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
797 | { |
798 | int ret; | |
cd80958d | 799 | struct lttcomm_session_msg lsm; |
9f19cc17 | 800 | |
9d697d3d DG |
801 | /* Safety check. An handle and channel name are mandatory */ |
802 | if (handle == NULL || channel_name == NULL) { | |
cd80958d DG |
803 | return -1; |
804 | } | |
805 | ||
806 | lsm.cmd_type = LTTNG_LIST_EVENTS; | |
807 | copy_string(lsm.session.name, handle->session_name, | |
808 | sizeof(lsm.session.name)); | |
809 | copy_string(lsm.u.list.channel_name, channel_name, | |
810 | sizeof(lsm.u.list.channel_name)); | |
811 | ||
812 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
9f19cc17 | 813 | |
cd80958d | 814 | ret = ask_sessiond(&lsm, (void**) events); |
9f19cc17 DG |
815 | if (ret < 0) { |
816 | return ret; | |
817 | } | |
818 | ||
819 | return ret / sizeof(struct lttng_event); | |
820 | } | |
821 | ||
fac6795d | 822 | /* |
1c8d13c8 TD |
823 | * Sets the tracing_group variable with name. |
824 | * This function allocates memory pointed to by tracing_group. | |
825 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. | |
fac6795d DG |
826 | */ |
827 | int lttng_set_tracing_group(const char *name) | |
828 | { | |
9d697d3d DG |
829 | if (name == NULL) { |
830 | return -1; | |
831 | } | |
832 | ||
fac6795d DG |
833 | if (asprintf(&tracing_group, "%s", name) < 0) { |
834 | return -ENOMEM; | |
835 | } | |
836 | ||
837 | return 0; | |
838 | } | |
839 | ||
d0254c7c | 840 | /* |
af87c45a | 841 | * Returns size of returned session payload data or a negative error code. |
d0254c7c | 842 | */ |
cd80958d | 843 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
844 | struct lttng_calibrate *calibrate) |
845 | { | |
cd80958d | 846 | struct lttcomm_session_msg lsm; |
d0254c7c | 847 | |
9d697d3d DG |
848 | /* Safety check. NULL pointer are forbidden */ |
849 | if (handle == NULL || calibrate == NULL) { | |
cd80958d DG |
850 | return -1; |
851 | } | |
d0254c7c | 852 | |
cd80958d DG |
853 | lsm.cmd_type = LTTNG_CALIBRATE; |
854 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d0254c7c | 855 | |
cd80958d DG |
856 | memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); |
857 | ||
858 | return ask_sessiond(&lsm, NULL); | |
d0254c7c MD |
859 | } |
860 | ||
5edd7e09 DG |
861 | /* |
862 | * Set default channel attributes. | |
1c8d13c8 | 863 | * If either or both of the arguments are null, nothing happens. |
5edd7e09 DG |
864 | */ |
865 | void lttng_channel_set_default_attr(struct lttng_domain *domain, | |
866 | struct lttng_channel_attr *attr) | |
867 | { | |
868 | /* Safety check */ | |
869 | if (attr == NULL || domain == NULL) { | |
870 | return; | |
871 | } | |
872 | ||
873 | switch (domain->type) { | |
874 | case LTTNG_DOMAIN_KERNEL: | |
875 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
876 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
877 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
878 | ||
879 | attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE; | |
880 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; | |
881 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
882 | break; | |
883 | case LTTNG_DOMAIN_UST: | |
d78d6610 | 884 | #if 0 |
5edd7e09 DG |
885 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
886 | case LTTNG_DOMAIN_UST_PID: | |
887 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 888 | #endif |
5edd7e09 DG |
889 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; |
890 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
891 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
892 | ||
893 | attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE; | |
894 | attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM; | |
895 | attr->output = DEFAULT_UST_CHANNEL_OUTPUT; | |
896 | break; | |
897 | default: | |
898 | /* Default behavior */ | |
899 | memset(attr, 0, sizeof(struct lttng_channel_attr)); | |
900 | break; | |
901 | } | |
902 | } | |
903 | ||
fac6795d | 904 | /* |
2269e89e | 905 | * Check if session daemon is alive. |
fac6795d | 906 | * |
2269e89e | 907 | * Return 1 if alive or 0 if not. |
1c8d13c8 | 908 | * On error returns a negative value. |
fac6795d | 909 | */ |
947308c4 | 910 | int lttng_session_daemon_alive(void) |
fac6795d DG |
911 | { |
912 | int ret; | |
913 | ||
914 | ret = set_session_daemon_path(); | |
915 | if (ret < 0) { | |
947308c4 | 916 | /* Error */ |
fac6795d DG |
917 | return ret; |
918 | } | |
919 | ||
2269e89e DG |
920 | if (strlen(sessiond_sock_path) == 0) { |
921 | /* No socket path set. Weird error */ | |
922 | return -1; | |
fac6795d DG |
923 | } |
924 | ||
2269e89e | 925 | ret = try_connect_sessiond(sessiond_sock_path); |
7d8234d9 MD |
926 | if (ret < 0) { |
927 | /* Not alive */ | |
928 | return 0; | |
929 | } | |
7d8234d9 | 930 | |
947308c4 DG |
931 | /* Is alive */ |
932 | return 1; | |
fac6795d DG |
933 | } |
934 | ||
935 | /* | |
936 | * lib constructor | |
937 | */ | |
938 | static void __attribute__((constructor)) init() | |
939 | { | |
940 | /* Set default session group */ | |
bbccc3d2 | 941 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
fac6795d | 942 | } |