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