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 | * |
cd80958d DG |
86 | * On success, return 0 |
87 | * On error, return error code | |
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 | * |
cd80958d DG |
108 | * On success, return 0 |
109 | * On error, return recv() error code | |
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 | /* | |
947308c4 | 127 | * Check if the specified group name exist. |
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 MD |
157 | if (!grp_list) { |
158 | ret = -1; | |
159 | goto end; | |
160 | } | |
947308c4 DG |
161 | grp_id = getgroups(grp_list_size, grp_list); |
162 | if (grp_id < -1) { | |
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 | /* | |
212 | * Set sessiond socket path by putting it in the global sessiond_sock_path | |
213 | * variable. | |
947308c4 DG |
214 | */ |
215 | static int set_session_daemon_path(void) | |
216 | { | |
217 | int ret; | |
2269e89e DG |
218 | int in_tgroup = 0; /* In tracing group */ |
219 | uid_t uid; | |
220 | ||
221 | uid = getuid(); | |
947308c4 | 222 | |
2269e89e DG |
223 | if (uid != 0) { |
224 | /* Are we in the tracing group ? */ | |
225 | in_tgroup = check_tracing_group(tracing_group); | |
226 | } | |
227 | ||
228 | if (uid == 0) { | |
229 | /* Root */ | |
230 | copy_string(sessiond_sock_path, | |
231 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
232 | sizeof(sessiond_sock_path)); | |
233 | } else if (in_tgroup) { | |
234 | /* Tracing group */ | |
235 | copy_string(sessiond_sock_path, | |
236 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
237 | sizeof(sessiond_sock_path)); | |
238 | ||
239 | ret = try_connect_sessiond(sessiond_sock_path); | |
240 | if (ret < 0) { | |
241 | /* Global session daemon not available */ | |
242 | if (snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), | |
243 | DEFAULT_HOME_CLIENT_UNIX_SOCK, | |
244 | getenv("HOME")) < 0) { | |
245 | return -ENOMEM; | |
246 | } | |
247 | } | |
248 | } else { | |
249 | /* Not in tracing group and not root, default */ | |
99497cd0 | 250 | if (snprintf(sessiond_sock_path, PATH_MAX, |
cd80958d DG |
251 | DEFAULT_HOME_CLIENT_UNIX_SOCK, |
252 | getenv("HOME")) < 0) { | |
947308c4 DG |
253 | return -ENOMEM; |
254 | } | |
947308c4 DG |
255 | } |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
65beb5ff DG |
260 | /* |
261 | * Connect to the LTTng session daemon. | |
262 | * | |
263 | * On success, return 0. On error, return -1. | |
264 | */ | |
265 | static int connect_sessiond(void) | |
266 | { | |
267 | int ret; | |
268 | ||
269 | ret = set_session_daemon_path(); | |
270 | if (ret < 0) { | |
271 | return ret; | |
272 | } | |
273 | ||
274 | /* Connect to the sesssion daemon */ | |
275 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); | |
276 | if (ret < 0) { | |
277 | return ret; | |
278 | } | |
279 | ||
280 | sessiond_socket = ret; | |
281 | connected = 1; | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
286 | /* | |
287 | * Clean disconnect the session daemon. | |
288 | */ | |
289 | static int disconnect_sessiond(void) | |
290 | { | |
291 | int ret = 0; | |
292 | ||
293 | if (connected) { | |
294 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
295 | sessiond_socket = 0; | |
296 | connected = 0; | |
297 | } | |
298 | ||
299 | return ret; | |
300 | } | |
301 | ||
35a6fdb7 | 302 | /* |
cd80958d | 303 | * Ask the session daemon a specific command and put the data into buf. |
65beb5ff | 304 | * |
af87c45a | 305 | * Return size of data (only payload, not header) or a negative error code. |
65beb5ff | 306 | */ |
cd80958d | 307 | static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf) |
65beb5ff DG |
308 | { |
309 | int ret; | |
310 | size_t size; | |
311 | void *data = NULL; | |
cd80958d | 312 | struct lttcomm_lttng_msg llm; |
65beb5ff DG |
313 | |
314 | ret = connect_sessiond(); | |
315 | if (ret < 0) { | |
316 | goto end; | |
317 | } | |
318 | ||
65beb5ff | 319 | /* Send command to session daemon */ |
cd80958d | 320 | ret = send_session_msg(lsm); |
65beb5ff DG |
321 | if (ret < 0) { |
322 | goto end; | |
323 | } | |
324 | ||
325 | /* Get header from data transmission */ | |
326 | ret = recv_data_sessiond(&llm, sizeof(llm)); | |
327 | if (ret < 0) { | |
328 | goto end; | |
329 | } | |
330 | ||
331 | /* Check error code if OK */ | |
332 | if (llm.ret_code != LTTCOMM_OK) { | |
333 | ret = -llm.ret_code; | |
334 | goto end; | |
335 | } | |
336 | ||
337 | size = llm.data_size; | |
338 | if (size == 0) { | |
874d3f84 | 339 | /* If client free with size 0 */ |
a45d5536 DG |
340 | if (buf != NULL) { |
341 | *buf = NULL; | |
342 | } | |
7d29a247 | 343 | ret = 0; |
65beb5ff DG |
344 | goto end; |
345 | } | |
346 | ||
347 | data = (void*) malloc(size); | |
348 | ||
349 | /* Get payload data */ | |
350 | ret = recv_data_sessiond(data, size); | |
351 | if (ret < 0) { | |
352 | free(data); | |
353 | goto end; | |
354 | } | |
355 | ||
83009e5e DG |
356 | /* |
357 | * Extra protection not to dereference a NULL pointer. If buf is NULL at | |
358 | * this point, an error is returned and data is freed. | |
359 | */ | |
360 | if (buf == NULL) { | |
361 | ret = -1; | |
362 | free(data); | |
363 | goto end; | |
364 | } | |
365 | ||
65beb5ff DG |
366 | *buf = data; |
367 | ret = size; | |
368 | ||
369 | end: | |
370 | disconnect_sessiond(); | |
371 | return ret; | |
372 | } | |
373 | ||
9f19cc17 | 374 | /* |
cd80958d | 375 | * Create lttng handle and return pointer. |
9f19cc17 | 376 | */ |
cd80958d DG |
377 | struct lttng_handle *lttng_create_handle(const char *session_name, |
378 | struct lttng_domain *domain) | |
9f19cc17 | 379 | { |
cd80958d DG |
380 | struct lttng_handle *handle; |
381 | ||
382 | handle = malloc(sizeof(struct lttng_handle)); | |
383 | if (handle == NULL) { | |
384 | perror("malloc handle"); | |
385 | goto end; | |
386 | } | |
387 | ||
388 | /* Copy session name */ | |
389 | copy_string(handle->session_name, session_name, | |
390 | sizeof(handle->session_name)); | |
391 | ||
392 | /* Copy lttng domain */ | |
393 | copy_lttng_domain(&handle->domain, domain); | |
394 | ||
395 | end: | |
396 | return handle; | |
397 | } | |
398 | ||
399 | /* | |
400 | * Destroy handle by free(3) the pointer. | |
401 | */ | |
402 | void lttng_destroy_handle(struct lttng_handle *handle) | |
403 | { | |
404 | if (handle) { | |
405 | free(handle); | |
eb354453 DG |
406 | } |
407 | } | |
408 | ||
d9800920 DG |
409 | /* |
410 | * Register an outside consumer. | |
411 | */ | |
412 | int lttng_register_consumer(struct lttng_handle *handle, | |
413 | const char *socket_path) | |
414 | { | |
415 | struct lttcomm_session_msg lsm; | |
416 | ||
417 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; | |
418 | copy_string(lsm.session.name, handle->session_name, | |
419 | sizeof(lsm.session.name)); | |
420 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
421 | ||
422 | copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path)); | |
423 | ||
424 | return ask_sessiond(&lsm, NULL); | |
425 | } | |
426 | ||
1df4dedd | 427 | /* |
f3ed775e | 428 | * Start tracing for all trace of the session. |
1df4dedd | 429 | */ |
6a4f824d | 430 | int lttng_start_tracing(const char *session_name) |
f3ed775e | 431 | { |
cd80958d DG |
432 | struct lttcomm_session_msg lsm; |
433 | ||
6a4f824d | 434 | if (session_name == NULL) { |
cd80958d DG |
435 | return -1; |
436 | } | |
437 | ||
438 | lsm.cmd_type = LTTNG_START_TRACE; | |
6a4f824d DG |
439 | |
440 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
441 | |
442 | return ask_sessiond(&lsm, NULL); | |
f3ed775e | 443 | } |
1df4dedd DG |
444 | |
445 | /* | |
f3ed775e DG |
446 | * Stop tracing for all trace of the session. |
447 | */ | |
6a4f824d | 448 | int lttng_stop_tracing(const char *session_name) |
f3ed775e | 449 | { |
cd80958d DG |
450 | struct lttcomm_session_msg lsm; |
451 | ||
6a4f824d DG |
452 | if (session_name == NULL) { |
453 | return -1; | |
454 | } | |
455 | ||
cd80958d | 456 | lsm.cmd_type = LTTNG_STOP_TRACE; |
6a4f824d DG |
457 | |
458 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
459 | |
460 | return ask_sessiond(&lsm, NULL); | |
f3ed775e DG |
461 | } |
462 | ||
463 | /* | |
cd80958d | 464 | * Add context to event or/and channel. |
af87c45a DG |
465 | * |
466 | * Returns the size of the returned payload data or a negative error code. | |
1df4dedd | 467 | */ |
cd80958d | 468 | int lttng_add_context(struct lttng_handle *handle, |
38057ed1 DG |
469 | struct lttng_event_context *ctx, const char *event_name, |
470 | const char *channel_name) | |
d65106b1 | 471 | { |
cd80958d DG |
472 | struct lttcomm_session_msg lsm; |
473 | ||
9d697d3d DG |
474 | /* Safety check. Both are mandatory */ |
475 | if (handle == NULL || ctx == NULL) { | |
cd80958d DG |
476 | return -1; |
477 | } | |
478 | ||
479 | lsm.cmd_type = LTTNG_ADD_CONTEXT; | |
480 | ||
481 | /* Copy channel name */ | |
482 | copy_string(lsm.u.context.channel_name, channel_name, | |
483 | sizeof(lsm.u.context.channel_name)); | |
484 | /* Copy event name */ | |
485 | copy_string(lsm.u.context.event_name, event_name, | |
486 | sizeof(lsm.u.context.event_name)); | |
487 | ||
488 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d65106b1 | 489 | |
9d697d3d | 490 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); |
d65106b1 | 491 | |
cd80958d DG |
492 | copy_string(lsm.session.name, handle->session_name, |
493 | sizeof(lsm.session.name)); | |
494 | ||
495 | return ask_sessiond(&lsm, NULL); | |
d65106b1 DG |
496 | } |
497 | ||
f3ed775e | 498 | /* |
cd80958d | 499 | * Enable event |
f3ed775e | 500 | */ |
cd80958d | 501 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 502 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 503 | { |
cd80958d DG |
504 | struct lttcomm_session_msg lsm; |
505 | ||
5117eeec | 506 | if (handle == NULL || ev == NULL) { |
cd80958d DG |
507 | return -1; |
508 | } | |
33a2b854 | 509 | |
5117eeec | 510 | /* If no channel name, we put the default name */ |
94cf3c47 | 511 | if (channel_name == NULL) { |
cd80958d DG |
512 | copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, |
513 | sizeof(lsm.u.enable.channel_name)); | |
33a2b854 | 514 | } else { |
cd80958d DG |
515 | copy_string(lsm.u.enable.channel_name, channel_name, |
516 | sizeof(lsm.u.enable.channel_name)); | |
eb354453 DG |
517 | } |
518 | ||
cd80958d | 519 | copy_lttng_domain(&lsm.domain, &handle->domain); |
0d0c377a | 520 | |
8c9ae521 | 521 | if (ev->name[0] != '\0') { |
cd80958d | 522 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
0d0c377a | 523 | } else { |
cd80958d | 524 | lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT; |
f3ed775e | 525 | } |
8c9ae521 | 526 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
f3ed775e | 527 | |
cd80958d DG |
528 | copy_string(lsm.session.name, handle->session_name, |
529 | sizeof(lsm.session.name)); | |
530 | ||
531 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
532 | } |
533 | ||
534 | /* | |
f5177a38 | 535 | * Disable event of a channel and domain. |
1df4dedd | 536 | */ |
cd80958d | 537 | int lttng_disable_event(struct lttng_handle *handle, const char *name, |
38057ed1 | 538 | const char *channel_name) |
1df4dedd | 539 | { |
cd80958d | 540 | struct lttcomm_session_msg lsm; |
1df4dedd | 541 | |
9d697d3d | 542 | if (handle == NULL) { |
cd80958d DG |
543 | return -1; |
544 | } | |
545 | ||
546 | if (channel_name) { | |
547 | copy_string(lsm.u.disable.channel_name, channel_name, | |
548 | sizeof(lsm.u.disable.channel_name)); | |
f3ed775e | 549 | } else { |
cd80958d DG |
550 | copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, |
551 | sizeof(lsm.u.disable.channel_name)); | |
eb354453 DG |
552 | } |
553 | ||
cd80958d | 554 | copy_lttng_domain(&lsm.domain, &handle->domain); |
f5177a38 | 555 | |
f84efadf | 556 | if (name != NULL) { |
cd80958d DG |
557 | copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name)); |
558 | lsm.cmd_type = LTTNG_DISABLE_EVENT; | |
f5177a38 | 559 | } else { |
cd80958d | 560 | lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT; |
f3ed775e DG |
561 | } |
562 | ||
cd80958d DG |
563 | copy_string(lsm.session.name, handle->session_name, |
564 | sizeof(lsm.session.name)); | |
565 | ||
566 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
567 | } |
568 | ||
569 | /* | |
0d0c377a | 570 | * Enable channel per domain |
a5c5a2bd | 571 | */ |
cd80958d | 572 | int lttng_enable_channel(struct lttng_handle *handle, |
38057ed1 | 573 | struct lttng_channel *chan) |
a5c5a2bd | 574 | { |
cd80958d DG |
575 | struct lttcomm_session_msg lsm; |
576 | ||
5117eeec DG |
577 | /* |
578 | * NULL arguments are forbidden. No default values. | |
579 | */ | |
580 | if (handle == NULL || chan == NULL) { | |
cd80958d DG |
581 | return -1; |
582 | } | |
583 | ||
5117eeec | 584 | memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); |
7d29a247 | 585 | |
cd80958d DG |
586 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
587 | ||
588 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
7d29a247 | 589 | |
cd80958d DG |
590 | copy_string(lsm.session.name, handle->session_name, |
591 | sizeof(lsm.session.name)); | |
592 | ||
593 | return ask_sessiond(&lsm, NULL); | |
8c0faa1d | 594 | } |
1df4dedd | 595 | |
2ef84c95 | 596 | /* |
0b97ec54 | 597 | * All tracing will be stopped for registered events of the channel. |
2ef84c95 | 598 | */ |
cd80958d | 599 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 600 | { |
cd80958d DG |
601 | struct lttcomm_session_msg lsm; |
602 | ||
9d697d3d DG |
603 | /* Safety check. Both are mandatory */ |
604 | if (handle == NULL || name == NULL) { | |
cd80958d DG |
605 | return -1; |
606 | } | |
607 | ||
cd80958d | 608 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
1df4dedd | 609 | |
9d697d3d DG |
610 | copy_string(lsm.u.disable.channel_name, name, |
611 | sizeof(lsm.u.disable.channel_name)); | |
612 | ||
cd80958d DG |
613 | copy_lttng_domain(&lsm.domain, &handle->domain); |
614 | ||
615 | copy_string(lsm.session.name, handle->session_name, | |
616 | sizeof(lsm.session.name)); | |
617 | ||
618 | return ask_sessiond(&lsm, NULL); | |
ca95a216 DG |
619 | } |
620 | ||
fac6795d | 621 | /* |
2a71efd5 | 622 | * List all available tracepoints of domain. |
fac6795d | 623 | * |
2a71efd5 | 624 | * Return the size (bytes) of the list and set the events array. |
7d29a247 | 625 | * On error, return negative value. |
fac6795d | 626 | */ |
cd80958d | 627 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 628 | struct lttng_event **events) |
fac6795d | 629 | { |
052da939 | 630 | int ret; |
cd80958d DG |
631 | struct lttcomm_session_msg lsm; |
632 | ||
9d697d3d | 633 | if (handle == NULL) { |
cd80958d DG |
634 | return -1; |
635 | } | |
fac6795d | 636 | |
cd80958d DG |
637 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
638 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
2a71efd5 | 639 | |
cd80958d | 640 | ret = ask_sessiond(&lsm, (void **) events); |
052da939 DG |
641 | if (ret < 0) { |
642 | return ret; | |
eb354453 | 643 | } |
fac6795d | 644 | |
9f19cc17 | 645 | return ret / sizeof(struct lttng_event); |
fac6795d DG |
646 | } |
647 | ||
1657e9bb | 648 | /* |
7d29a247 | 649 | * Return a human readable string of code |
1657e9bb | 650 | */ |
9a745bc7 | 651 | const char *lttng_strerror(int code) |
1657e9bb | 652 | { |
7d29a247 DG |
653 | if (code > -LTTCOMM_OK) { |
654 | return "Ended with errors"; | |
1657e9bb DG |
655 | } |
656 | ||
7d29a247 | 657 | return lttcomm_get_readable_code(code); |
1657e9bb DG |
658 | } |
659 | ||
aaf97519 | 660 | /* |
894be886 | 661 | * Create a brand new session using name. |
aaf97519 | 662 | */ |
38057ed1 | 663 | int lttng_create_session(const char *name, const char *path) |
aaf97519 | 664 | { |
cd80958d DG |
665 | struct lttcomm_session_msg lsm; |
666 | ||
667 | lsm.cmd_type = LTTNG_CREATE_SESSION; | |
668 | copy_string(lsm.session.name, name, sizeof(lsm.session.name)); | |
669 | copy_string(lsm.session.path, path, sizeof(lsm.session.path)); | |
670 | ||
671 | return ask_sessiond(&lsm, NULL); | |
8028d920 DG |
672 | } |
673 | ||
674 | /* | |
8028d920 DG |
675 | * Destroy session using name. |
676 | */ | |
843f5df9 | 677 | int lttng_destroy_session(const char *session_name) |
8028d920 | 678 | { |
cd80958d DG |
679 | struct lttcomm_session_msg lsm; |
680 | ||
843f5df9 | 681 | if (session_name == NULL) { |
cd80958d DG |
682 | return -1; |
683 | } | |
684 | ||
685 | lsm.cmd_type = LTTNG_DESTROY_SESSION; | |
843f5df9 DG |
686 | |
687 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
688 | |
689 | return ask_sessiond(&lsm, NULL); | |
aaf97519 DG |
690 | } |
691 | ||
57167058 | 692 | /* |
57167058 DG |
693 | * Ask the session daemon for all available sessions. |
694 | * | |
ca95a216 DG |
695 | * Return number of session. |
696 | * On error, return negative value. | |
57167058 | 697 | */ |
ca95a216 | 698 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 699 | { |
ca95a216 | 700 | int ret; |
cd80958d | 701 | struct lttcomm_session_msg lsm; |
57167058 | 702 | |
cd80958d DG |
703 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
704 | ret = ask_sessiond(&lsm, (void**) sessions); | |
57167058 | 705 | if (ret < 0) { |
ca95a216 | 706 | return ret; |
57167058 DG |
707 | } |
708 | ||
ca95a216 | 709 | return ret / sizeof(struct lttng_session); |
57167058 DG |
710 | } |
711 | ||
9f19cc17 DG |
712 | /* |
713 | * List domain of a session. | |
714 | */ | |
330be774 | 715 | int lttng_list_domains(const char *session_name, |
cd80958d | 716 | struct lttng_domain **domains) |
9f19cc17 DG |
717 | { |
718 | int ret; | |
cd80958d DG |
719 | struct lttcomm_session_msg lsm; |
720 | ||
330be774 | 721 | if (session_name == NULL) { |
cd80958d DG |
722 | return -1; |
723 | } | |
9f19cc17 | 724 | |
cd80958d DG |
725 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
726 | ||
330be774 | 727 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); |
cd80958d DG |
728 | |
729 | ret = ask_sessiond(&lsm, (void**) domains); | |
9f19cc17 DG |
730 | if (ret < 0) { |
731 | return ret; | |
732 | } | |
733 | ||
734 | return ret / sizeof(struct lttng_domain); | |
735 | } | |
736 | ||
737 | /* | |
738 | * List channels of a session | |
739 | */ | |
cd80958d DG |
740 | int lttng_list_channels(struct lttng_handle *handle, |
741 | struct lttng_channel **channels) | |
9f19cc17 DG |
742 | { |
743 | int ret; | |
cd80958d DG |
744 | struct lttcomm_session_msg lsm; |
745 | ||
9d697d3d | 746 | if (handle == NULL) { |
cd80958d DG |
747 | return -1; |
748 | } | |
749 | ||
750 | lsm.cmd_type = LTTNG_LIST_CHANNELS; | |
751 | copy_string(lsm.session.name, handle->session_name, | |
752 | sizeof(lsm.session.name)); | |
9f19cc17 | 753 | |
cd80958d | 754 | copy_lttng_domain(&lsm.domain, &handle->domain); |
9f19cc17 | 755 | |
cd80958d | 756 | ret = ask_sessiond(&lsm, (void**) channels); |
9f19cc17 DG |
757 | if (ret < 0) { |
758 | return ret; | |
759 | } | |
760 | ||
761 | return ret / sizeof(struct lttng_channel); | |
762 | } | |
763 | ||
764 | /* | |
765 | * List events of a session channel. | |
766 | */ | |
cd80958d DG |
767 | int lttng_list_events(struct lttng_handle *handle, |
768 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
769 | { |
770 | int ret; | |
cd80958d | 771 | struct lttcomm_session_msg lsm; |
9f19cc17 | 772 | |
9d697d3d DG |
773 | /* Safety check. An handle and channel name are mandatory */ |
774 | if (handle == NULL || channel_name == NULL) { | |
cd80958d DG |
775 | return -1; |
776 | } | |
777 | ||
778 | lsm.cmd_type = LTTNG_LIST_EVENTS; | |
779 | copy_string(lsm.session.name, handle->session_name, | |
780 | sizeof(lsm.session.name)); | |
781 | copy_string(lsm.u.list.channel_name, channel_name, | |
782 | sizeof(lsm.u.list.channel_name)); | |
783 | ||
784 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
9f19cc17 | 785 | |
cd80958d | 786 | ret = ask_sessiond(&lsm, (void**) events); |
9f19cc17 DG |
787 | if (ret < 0) { |
788 | return ret; | |
789 | } | |
790 | ||
791 | return ret / sizeof(struct lttng_event); | |
792 | } | |
793 | ||
fac6795d | 794 | /* |
5edd7e09 DG |
795 | * Set tracing group variable with name. This function allocate memory pointed |
796 | * by tracing_group. | |
fac6795d DG |
797 | */ |
798 | int lttng_set_tracing_group(const char *name) | |
799 | { | |
9d697d3d DG |
800 | if (name == NULL) { |
801 | return -1; | |
802 | } | |
803 | ||
fac6795d DG |
804 | if (asprintf(&tracing_group, "%s", name) < 0) { |
805 | return -ENOMEM; | |
806 | } | |
807 | ||
808 | return 0; | |
809 | } | |
810 | ||
d0254c7c | 811 | /* |
af87c45a | 812 | * Returns size of returned session payload data or a negative error code. |
d0254c7c | 813 | */ |
cd80958d | 814 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
815 | struct lttng_calibrate *calibrate) |
816 | { | |
cd80958d | 817 | struct lttcomm_session_msg lsm; |
d0254c7c | 818 | |
9d697d3d DG |
819 | /* Safety check. NULL pointer are forbidden */ |
820 | if (handle == NULL || calibrate == NULL) { | |
cd80958d DG |
821 | return -1; |
822 | } | |
d0254c7c | 823 | |
cd80958d DG |
824 | lsm.cmd_type = LTTNG_CALIBRATE; |
825 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d0254c7c | 826 | |
cd80958d DG |
827 | memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); |
828 | ||
829 | return ask_sessiond(&lsm, NULL); | |
d0254c7c MD |
830 | } |
831 | ||
5edd7e09 DG |
832 | /* |
833 | * Set default channel attributes. | |
834 | */ | |
835 | void lttng_channel_set_default_attr(struct lttng_domain *domain, | |
836 | struct lttng_channel_attr *attr) | |
837 | { | |
838 | /* Safety check */ | |
839 | if (attr == NULL || domain == NULL) { | |
840 | return; | |
841 | } | |
842 | ||
843 | switch (domain->type) { | |
844 | case LTTNG_DOMAIN_KERNEL: | |
845 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
846 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
847 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
848 | ||
849 | attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE; | |
850 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; | |
851 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
852 | break; | |
853 | case LTTNG_DOMAIN_UST: | |
d78d6610 | 854 | #if 0 |
5edd7e09 DG |
855 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
856 | case LTTNG_DOMAIN_UST_PID: | |
857 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 858 | #endif |
5edd7e09 DG |
859 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; |
860 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
861 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
862 | ||
863 | attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE; | |
864 | attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM; | |
865 | attr->output = DEFAULT_UST_CHANNEL_OUTPUT; | |
866 | break; | |
867 | default: | |
868 | /* Default behavior */ | |
869 | memset(attr, 0, sizeof(struct lttng_channel_attr)); | |
870 | break; | |
871 | } | |
872 | } | |
873 | ||
fac6795d | 874 | /* |
2269e89e | 875 | * Check if session daemon is alive. |
fac6795d | 876 | * |
2269e89e DG |
877 | * Return 1 if alive or 0 if not. |
878 | * On error return -1 | |
fac6795d | 879 | */ |
947308c4 | 880 | int lttng_session_daemon_alive(void) |
fac6795d DG |
881 | { |
882 | int ret; | |
883 | ||
884 | ret = set_session_daemon_path(); | |
885 | if (ret < 0) { | |
947308c4 | 886 | /* Error */ |
fac6795d DG |
887 | return ret; |
888 | } | |
889 | ||
2269e89e DG |
890 | if (strlen(sessiond_sock_path) == 0) { |
891 | /* No socket path set. Weird error */ | |
892 | return -1; | |
fac6795d DG |
893 | } |
894 | ||
2269e89e | 895 | ret = try_connect_sessiond(sessiond_sock_path); |
7d8234d9 MD |
896 | if (ret < 0) { |
897 | /* Not alive */ | |
898 | return 0; | |
899 | } | |
7d8234d9 | 900 | |
947308c4 DG |
901 | /* Is alive */ |
902 | return 1; | |
fac6795d DG |
903 | } |
904 | ||
905 | /* | |
906 | * lib constructor | |
907 | */ | |
908 | static void __attribute__((constructor)) init() | |
909 | { | |
910 | /* Set default session group */ | |
bbccc3d2 | 911 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
fac6795d | 912 | } |