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