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 | * |
cd80958d | 305 | * Return size of data (only payload, not header). |
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. |
1df4dedd | 465 | */ |
cd80958d | 466 | int lttng_add_context(struct lttng_handle *handle, |
38057ed1 DG |
467 | struct lttng_event_context *ctx, const char *event_name, |
468 | const char *channel_name) | |
d65106b1 | 469 | { |
cd80958d DG |
470 | struct lttcomm_session_msg lsm; |
471 | ||
9d697d3d DG |
472 | /* Safety check. Both are mandatory */ |
473 | if (handle == NULL || ctx == NULL) { | |
cd80958d DG |
474 | return -1; |
475 | } | |
476 | ||
477 | lsm.cmd_type = LTTNG_ADD_CONTEXT; | |
478 | ||
479 | /* Copy channel name */ | |
480 | copy_string(lsm.u.context.channel_name, channel_name, | |
481 | sizeof(lsm.u.context.channel_name)); | |
482 | /* Copy event name */ | |
483 | copy_string(lsm.u.context.event_name, event_name, | |
484 | sizeof(lsm.u.context.event_name)); | |
485 | ||
486 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d65106b1 | 487 | |
9d697d3d | 488 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); |
d65106b1 | 489 | |
cd80958d DG |
490 | copy_string(lsm.session.name, handle->session_name, |
491 | sizeof(lsm.session.name)); | |
492 | ||
493 | return ask_sessiond(&lsm, NULL); | |
d65106b1 DG |
494 | } |
495 | ||
f3ed775e | 496 | /* |
cd80958d | 497 | * Enable event |
f3ed775e | 498 | */ |
cd80958d | 499 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 500 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 501 | { |
cd80958d DG |
502 | struct lttcomm_session_msg lsm; |
503 | ||
5117eeec | 504 | if (handle == NULL || ev == NULL) { |
cd80958d DG |
505 | return -1; |
506 | } | |
33a2b854 | 507 | |
5117eeec | 508 | /* If no channel name, we put the default name */ |
94cf3c47 | 509 | if (channel_name == NULL) { |
cd80958d DG |
510 | copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, |
511 | sizeof(lsm.u.enable.channel_name)); | |
33a2b854 | 512 | } else { |
cd80958d DG |
513 | copy_string(lsm.u.enable.channel_name, channel_name, |
514 | sizeof(lsm.u.enable.channel_name)); | |
eb354453 DG |
515 | } |
516 | ||
cd80958d | 517 | copy_lttng_domain(&lsm.domain, &handle->domain); |
0d0c377a | 518 | |
8c9ae521 | 519 | if (ev->name[0] != '\0') { |
cd80958d | 520 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
0d0c377a | 521 | } else { |
cd80958d | 522 | lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT; |
f3ed775e | 523 | } |
8c9ae521 | 524 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
f3ed775e | 525 | |
cd80958d DG |
526 | copy_string(lsm.session.name, handle->session_name, |
527 | sizeof(lsm.session.name)); | |
528 | ||
529 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
530 | } |
531 | ||
532 | /* | |
f5177a38 | 533 | * Disable event of a channel and domain. |
1df4dedd | 534 | */ |
cd80958d | 535 | int lttng_disable_event(struct lttng_handle *handle, const char *name, |
38057ed1 | 536 | const char *channel_name) |
1df4dedd | 537 | { |
cd80958d | 538 | struct lttcomm_session_msg lsm; |
1df4dedd | 539 | |
9d697d3d | 540 | if (handle == NULL) { |
cd80958d DG |
541 | return -1; |
542 | } | |
543 | ||
544 | if (channel_name) { | |
545 | copy_string(lsm.u.disable.channel_name, channel_name, | |
546 | sizeof(lsm.u.disable.channel_name)); | |
f3ed775e | 547 | } else { |
cd80958d DG |
548 | copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, |
549 | sizeof(lsm.u.disable.channel_name)); | |
eb354453 DG |
550 | } |
551 | ||
cd80958d | 552 | copy_lttng_domain(&lsm.domain, &handle->domain); |
f5177a38 | 553 | |
f84efadf | 554 | if (name != NULL) { |
cd80958d DG |
555 | copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name)); |
556 | lsm.cmd_type = LTTNG_DISABLE_EVENT; | |
f5177a38 | 557 | } else { |
cd80958d | 558 | lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT; |
f3ed775e DG |
559 | } |
560 | ||
cd80958d DG |
561 | copy_string(lsm.session.name, handle->session_name, |
562 | sizeof(lsm.session.name)); | |
563 | ||
564 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
565 | } |
566 | ||
567 | /* | |
0d0c377a | 568 | * Enable channel per domain |
a5c5a2bd | 569 | */ |
cd80958d | 570 | int lttng_enable_channel(struct lttng_handle *handle, |
38057ed1 | 571 | struct lttng_channel *chan) |
a5c5a2bd | 572 | { |
cd80958d DG |
573 | struct lttcomm_session_msg lsm; |
574 | ||
5117eeec DG |
575 | /* |
576 | * NULL arguments are forbidden. No default values. | |
577 | */ | |
578 | if (handle == NULL || chan == NULL) { | |
cd80958d DG |
579 | return -1; |
580 | } | |
581 | ||
5117eeec | 582 | memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); |
7d29a247 | 583 | |
cd80958d DG |
584 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
585 | ||
586 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
7d29a247 | 587 | |
cd80958d DG |
588 | copy_string(lsm.session.name, handle->session_name, |
589 | sizeof(lsm.session.name)); | |
590 | ||
591 | return ask_sessiond(&lsm, NULL); | |
8c0faa1d | 592 | } |
1df4dedd | 593 | |
2ef84c95 | 594 | /* |
0b97ec54 | 595 | * All tracing will be stopped for registered events of the channel. |
2ef84c95 | 596 | */ |
cd80958d | 597 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 598 | { |
cd80958d DG |
599 | struct lttcomm_session_msg lsm; |
600 | ||
9d697d3d DG |
601 | /* Safety check. Both are mandatory */ |
602 | if (handle == NULL || name == NULL) { | |
cd80958d DG |
603 | return -1; |
604 | } | |
605 | ||
cd80958d | 606 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
1df4dedd | 607 | |
9d697d3d DG |
608 | copy_string(lsm.u.disable.channel_name, name, |
609 | sizeof(lsm.u.disable.channel_name)); | |
610 | ||
cd80958d DG |
611 | copy_lttng_domain(&lsm.domain, &handle->domain); |
612 | ||
613 | copy_string(lsm.session.name, handle->session_name, | |
614 | sizeof(lsm.session.name)); | |
615 | ||
616 | return ask_sessiond(&lsm, NULL); | |
ca95a216 DG |
617 | } |
618 | ||
fac6795d | 619 | /* |
2a71efd5 | 620 | * List all available tracepoints of domain. |
fac6795d | 621 | * |
2a71efd5 | 622 | * Return the size (bytes) of the list and set the events array. |
7d29a247 | 623 | * On error, return negative value. |
fac6795d | 624 | */ |
cd80958d | 625 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 626 | struct lttng_event **events) |
fac6795d | 627 | { |
052da939 | 628 | int ret; |
cd80958d DG |
629 | struct lttcomm_session_msg lsm; |
630 | ||
9d697d3d | 631 | if (handle == NULL) { |
cd80958d DG |
632 | return -1; |
633 | } | |
fac6795d | 634 | |
cd80958d DG |
635 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
636 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
2a71efd5 | 637 | |
cd80958d | 638 | ret = ask_sessiond(&lsm, (void **) events); |
052da939 DG |
639 | if (ret < 0) { |
640 | return ret; | |
eb354453 | 641 | } |
fac6795d | 642 | |
9f19cc17 | 643 | return ret / sizeof(struct lttng_event); |
fac6795d DG |
644 | } |
645 | ||
1657e9bb | 646 | /* |
7d29a247 | 647 | * Return a human readable string of code |
1657e9bb | 648 | */ |
9a745bc7 | 649 | const char *lttng_strerror(int code) |
1657e9bb | 650 | { |
7d29a247 DG |
651 | if (code > -LTTCOMM_OK) { |
652 | return "Ended with errors"; | |
1657e9bb DG |
653 | } |
654 | ||
7d29a247 | 655 | return lttcomm_get_readable_code(code); |
1657e9bb DG |
656 | } |
657 | ||
aaf97519 | 658 | /* |
894be886 | 659 | * Create a brand new session using name. |
aaf97519 | 660 | */ |
38057ed1 | 661 | int lttng_create_session(const char *name, const char *path) |
aaf97519 | 662 | { |
cd80958d DG |
663 | struct lttcomm_session_msg lsm; |
664 | ||
665 | lsm.cmd_type = LTTNG_CREATE_SESSION; | |
666 | copy_string(lsm.session.name, name, sizeof(lsm.session.name)); | |
667 | copy_string(lsm.session.path, path, sizeof(lsm.session.path)); | |
668 | ||
669 | return ask_sessiond(&lsm, NULL); | |
8028d920 DG |
670 | } |
671 | ||
672 | /* | |
8028d920 DG |
673 | * Destroy session using name. |
674 | */ | |
843f5df9 | 675 | int lttng_destroy_session(const char *session_name) |
8028d920 | 676 | { |
cd80958d DG |
677 | struct lttcomm_session_msg lsm; |
678 | ||
843f5df9 | 679 | if (session_name == NULL) { |
cd80958d DG |
680 | return -1; |
681 | } | |
682 | ||
683 | lsm.cmd_type = LTTNG_DESTROY_SESSION; | |
843f5df9 DG |
684 | |
685 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
686 | |
687 | return ask_sessiond(&lsm, NULL); | |
aaf97519 DG |
688 | } |
689 | ||
57167058 | 690 | /* |
57167058 DG |
691 | * Ask the session daemon for all available sessions. |
692 | * | |
ca95a216 DG |
693 | * Return number of session. |
694 | * On error, return negative value. | |
57167058 | 695 | */ |
ca95a216 | 696 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 697 | { |
ca95a216 | 698 | int ret; |
cd80958d | 699 | struct lttcomm_session_msg lsm; |
57167058 | 700 | |
cd80958d DG |
701 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
702 | ret = ask_sessiond(&lsm, (void**) sessions); | |
57167058 | 703 | if (ret < 0) { |
ca95a216 | 704 | return ret; |
57167058 DG |
705 | } |
706 | ||
ca95a216 | 707 | return ret / sizeof(struct lttng_session); |
57167058 DG |
708 | } |
709 | ||
9f19cc17 DG |
710 | /* |
711 | * List domain of a session. | |
712 | */ | |
330be774 | 713 | int lttng_list_domains(const char *session_name, |
cd80958d | 714 | struct lttng_domain **domains) |
9f19cc17 DG |
715 | { |
716 | int ret; | |
cd80958d DG |
717 | struct lttcomm_session_msg lsm; |
718 | ||
330be774 | 719 | if (session_name == NULL) { |
cd80958d DG |
720 | return -1; |
721 | } | |
9f19cc17 | 722 | |
cd80958d DG |
723 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
724 | ||
330be774 | 725 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); |
cd80958d DG |
726 | |
727 | ret = ask_sessiond(&lsm, (void**) domains); | |
9f19cc17 DG |
728 | if (ret < 0) { |
729 | return ret; | |
730 | } | |
731 | ||
732 | return ret / sizeof(struct lttng_domain); | |
733 | } | |
734 | ||
735 | /* | |
736 | * List channels of a session | |
737 | */ | |
cd80958d DG |
738 | int lttng_list_channels(struct lttng_handle *handle, |
739 | struct lttng_channel **channels) | |
9f19cc17 DG |
740 | { |
741 | int ret; | |
cd80958d DG |
742 | struct lttcomm_session_msg lsm; |
743 | ||
9d697d3d | 744 | if (handle == NULL) { |
cd80958d DG |
745 | return -1; |
746 | } | |
747 | ||
748 | lsm.cmd_type = LTTNG_LIST_CHANNELS; | |
749 | copy_string(lsm.session.name, handle->session_name, | |
750 | sizeof(lsm.session.name)); | |
9f19cc17 | 751 | |
cd80958d | 752 | copy_lttng_domain(&lsm.domain, &handle->domain); |
9f19cc17 | 753 | |
cd80958d | 754 | ret = ask_sessiond(&lsm, (void**) channels); |
9f19cc17 DG |
755 | if (ret < 0) { |
756 | return ret; | |
757 | } | |
758 | ||
759 | return ret / sizeof(struct lttng_channel); | |
760 | } | |
761 | ||
762 | /* | |
763 | * List events of a session channel. | |
764 | */ | |
cd80958d DG |
765 | int lttng_list_events(struct lttng_handle *handle, |
766 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
767 | { |
768 | int ret; | |
cd80958d | 769 | struct lttcomm_session_msg lsm; |
9f19cc17 | 770 | |
9d697d3d DG |
771 | /* Safety check. An handle and channel name are mandatory */ |
772 | if (handle == NULL || channel_name == NULL) { | |
cd80958d DG |
773 | return -1; |
774 | } | |
775 | ||
776 | lsm.cmd_type = LTTNG_LIST_EVENTS; | |
777 | copy_string(lsm.session.name, handle->session_name, | |
778 | sizeof(lsm.session.name)); | |
779 | copy_string(lsm.u.list.channel_name, channel_name, | |
780 | sizeof(lsm.u.list.channel_name)); | |
781 | ||
782 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
9f19cc17 | 783 | |
cd80958d | 784 | ret = ask_sessiond(&lsm, (void**) events); |
9f19cc17 DG |
785 | if (ret < 0) { |
786 | return ret; | |
787 | } | |
788 | ||
789 | return ret / sizeof(struct lttng_event); | |
790 | } | |
791 | ||
fac6795d | 792 | /* |
5edd7e09 DG |
793 | * Set tracing group variable with name. This function allocate memory pointed |
794 | * by tracing_group. | |
fac6795d DG |
795 | */ |
796 | int lttng_set_tracing_group(const char *name) | |
797 | { | |
9d697d3d DG |
798 | if (name == NULL) { |
799 | return -1; | |
800 | } | |
801 | ||
fac6795d DG |
802 | if (asprintf(&tracing_group, "%s", name) < 0) { |
803 | return -ENOMEM; | |
804 | } | |
805 | ||
806 | return 0; | |
807 | } | |
808 | ||
d0254c7c MD |
809 | /* |
810 | * lttng_calibrate | |
811 | */ | |
cd80958d | 812 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
813 | struct lttng_calibrate *calibrate) |
814 | { | |
cd80958d | 815 | struct lttcomm_session_msg lsm; |
d0254c7c | 816 | |
9d697d3d DG |
817 | /* Safety check. NULL pointer are forbidden */ |
818 | if (handle == NULL || calibrate == NULL) { | |
cd80958d DG |
819 | return -1; |
820 | } | |
d0254c7c | 821 | |
cd80958d DG |
822 | lsm.cmd_type = LTTNG_CALIBRATE; |
823 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d0254c7c | 824 | |
cd80958d DG |
825 | memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); |
826 | ||
827 | return ask_sessiond(&lsm, NULL); | |
d0254c7c MD |
828 | } |
829 | ||
5edd7e09 DG |
830 | /* |
831 | * Set default channel attributes. | |
832 | */ | |
833 | void lttng_channel_set_default_attr(struct lttng_domain *domain, | |
834 | struct lttng_channel_attr *attr) | |
835 | { | |
836 | /* Safety check */ | |
837 | if (attr == NULL || domain == NULL) { | |
838 | return; | |
839 | } | |
840 | ||
841 | switch (domain->type) { | |
842 | case LTTNG_DOMAIN_KERNEL: | |
843 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
844 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
845 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
846 | ||
847 | attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE; | |
848 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; | |
849 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
850 | break; | |
851 | case LTTNG_DOMAIN_UST: | |
d78d6610 | 852 | #if 0 |
5edd7e09 DG |
853 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
854 | case LTTNG_DOMAIN_UST_PID: | |
855 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 856 | #endif |
5edd7e09 DG |
857 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; |
858 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
859 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
860 | ||
861 | attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE; | |
862 | attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM; | |
863 | attr->output = DEFAULT_UST_CHANNEL_OUTPUT; | |
864 | break; | |
865 | default: | |
866 | /* Default behavior */ | |
867 | memset(attr, 0, sizeof(struct lttng_channel_attr)); | |
868 | break; | |
869 | } | |
870 | } | |
871 | ||
fac6795d | 872 | /* |
2269e89e | 873 | * Check if session daemon is alive. |
fac6795d | 874 | * |
2269e89e DG |
875 | * Return 1 if alive or 0 if not. |
876 | * On error return -1 | |
fac6795d | 877 | */ |
947308c4 | 878 | int lttng_session_daemon_alive(void) |
fac6795d DG |
879 | { |
880 | int ret; | |
881 | ||
882 | ret = set_session_daemon_path(); | |
883 | if (ret < 0) { | |
947308c4 | 884 | /* Error */ |
fac6795d DG |
885 | return ret; |
886 | } | |
887 | ||
2269e89e DG |
888 | if (strlen(sessiond_sock_path) == 0) { |
889 | /* No socket path set. Weird error */ | |
890 | return -1; | |
fac6795d DG |
891 | } |
892 | ||
2269e89e | 893 | ret = try_connect_sessiond(sessiond_sock_path); |
7d8234d9 MD |
894 | if (ret < 0) { |
895 | /* Not alive */ | |
896 | return 0; | |
897 | } | |
7d8234d9 | 898 | |
947308c4 DG |
899 | /* Is alive */ |
900 | return 1; | |
fac6795d DG |
901 | } |
902 | ||
903 | /* | |
904 | * lib constructor | |
905 | */ | |
906 | static void __attribute__((constructor)) init() | |
907 | { | |
908 | /* Set default session group */ | |
64a23ac4 | 909 | lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP); |
fac6795d | 910 | } |