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