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