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 | * |
d14d33bf AM |
8 | * This library is free software; you can redistribute it and/or modify it |
9 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
10 | * as published by the Free Software Foundation. | |
82a3637f DG |
11 | * |
12 | * This library is distributed in the hope that it will be useful, | |
fac6795d | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
82a3637f DG |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Lesser General Public License for more details. | |
16 | * | |
d14d33bf AM |
17 | * You should have received a copy of the GNU Lesser General Public License |
18 | * along with this library; if not, write to the Free Software Foundation, | |
19 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
fac6795d DG |
20 | */ |
21 | ||
22 | #define _GNU_SOURCE | |
44a5e5eb | 23 | #include <assert.h> |
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 | 35 | |
53a80697 | 36 | #include "filter-ast.h" |
95b9bd90 | 37 | #include "filter-parser.h" |
53a80697 MD |
38 | #include "filter-bytecode.h" |
39 | #include "memstream.h" | |
40 | ||
41 | #ifdef DEBUG | |
42 | const int print_xml = 1; | |
43 | #define dbg_printf(fmt, args...) \ | |
44 | printf("[debug liblttng-ctl] " fmt, ## args) | |
45 | #else | |
46 | const int print_xml = 0; | |
47 | #define dbg_printf(fmt, args...) \ | |
48 | do { \ | |
49 | /* do nothing but check printf format */ \ | |
50 | if (0) \ | |
51 | printf("[debug liblttnctl] " fmt, ## args); \ | |
52 | } while (0) | |
53 | #endif | |
54 | ||
55 | ||
fac6795d DG |
56 | /* Socket to session daemon for communication */ |
57 | static int sessiond_socket; | |
58 | static char sessiond_sock_path[PATH_MAX]; | |
44a5e5eb | 59 | static char health_sock_path[PATH_MAX]; |
fac6795d | 60 | |
fac6795d DG |
61 | /* Variables */ |
62 | static char *tracing_group; | |
63 | static int connected; | |
64 | ||
97e19046 DG |
65 | /* Global */ |
66 | ||
67 | /* | |
68 | * Those two variables are used by error.h to silent or control the verbosity of | |
69 | * error message. They are global to the library so application linking with it | |
70 | * are able to compile correctly and also control verbosity of the library. | |
71 | * | |
72 | * Note that it is *not* possible to silent ERR() and PERROR() macros. | |
73 | */ | |
74 | int lttng_opt_quiet; | |
75 | int lttng_opt_verbose; | |
76 | ||
99497cd0 MD |
77 | /* |
78 | * Copy string from src to dst and enforce null terminated byte. | |
79 | */ | |
80 | static void copy_string(char *dst, const char *src, size_t len) | |
81 | { | |
e7d6716d | 82 | if (src && dst) { |
99497cd0 MD |
83 | strncpy(dst, src, len); |
84 | /* Enforce the NULL terminated byte */ | |
85 | dst[len - 1] = '\0'; | |
cd80958d DG |
86 | } else if (dst) { |
87 | dst[0] = '\0'; | |
99497cd0 MD |
88 | } |
89 | } | |
90 | ||
fac6795d | 91 | /* |
cd80958d | 92 | * Copy domain to lttcomm_session_msg domain. |
fac6795d | 93 | * |
cd80958d DG |
94 | * If domain is unknown, default domain will be the kernel. |
95 | */ | |
96 | static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src) | |
97 | { | |
98 | if (src && dst) { | |
99 | switch (src->type) { | |
00e2e675 DG |
100 | case LTTNG_DOMAIN_KERNEL: |
101 | case LTTNG_DOMAIN_UST: | |
102 | /* | |
103 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
104 | case LTTNG_DOMAIN_UST_PID: | |
105 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
106 | */ | |
107 | memcpy(dst, src, sizeof(struct lttng_domain)); | |
108 | break; | |
109 | default: | |
110 | memset(dst, 0, sizeof(struct lttng_domain)); | |
111 | dst->type = LTTNG_DOMAIN_KERNEL; | |
112 | break; | |
cd80958d DG |
113 | } |
114 | } | |
115 | } | |
116 | ||
117 | /* | |
118 | * Send lttcomm_session_msg to the session daemon. | |
fac6795d | 119 | * |
1c8d13c8 TD |
120 | * On success, returns the number of bytes sent (>=0) |
121 | * On error, returns -1 | |
fac6795d | 122 | */ |
cd80958d | 123 | static int send_session_msg(struct lttcomm_session_msg *lsm) |
fac6795d DG |
124 | { |
125 | int ret; | |
126 | ||
127 | if (!connected) { | |
e065084a DG |
128 | ret = -ENOTCONN; |
129 | goto end; | |
fac6795d DG |
130 | } |
131 | ||
be040666 | 132 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, |
cd80958d | 133 | sizeof(struct lttcomm_session_msg)); |
e065084a DG |
134 | |
135 | end: | |
136 | return ret; | |
137 | } | |
138 | ||
53a80697 MD |
139 | /* |
140 | * Send var len data to the session daemon. | |
141 | * | |
142 | * On success, returns the number of bytes sent (>=0) | |
143 | * On error, returns -1 | |
144 | */ | |
145 | static int send_session_varlen(void *data, size_t len) | |
146 | { | |
147 | int ret; | |
148 | ||
149 | if (!connected) { | |
150 | ret = -ENOTCONN; | |
151 | goto end; | |
152 | } | |
153 | if (!data || !len) { | |
154 | ret = 0; | |
155 | goto end; | |
156 | } | |
157 | ||
158 | ret = lttcomm_send_unix_sock(sessiond_socket, data, len); | |
159 | ||
160 | end: | |
161 | return ret; | |
162 | } | |
163 | ||
e065084a | 164 | /* |
cd80958d | 165 | * Receive data from the sessiond socket. |
e065084a | 166 | * |
1c8d13c8 TD |
167 | * On success, returns the number of bytes received (>=0) |
168 | * On error, returns -1 (recvmsg() error) or -ENOTCONN | |
e065084a | 169 | */ |
ca95a216 | 170 | static int recv_data_sessiond(void *buf, size_t len) |
e065084a DG |
171 | { |
172 | int ret; | |
173 | ||
174 | if (!connected) { | |
175 | ret = -ENOTCONN; | |
176 | goto end; | |
fac6795d DG |
177 | } |
178 | ||
ca95a216 | 179 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
fac6795d | 180 | |
e065084a | 181 | end: |
fac6795d DG |
182 | return ret; |
183 | } | |
184 | ||
185 | /* | |
1c8d13c8 | 186 | * Check if we are in the specified group. |
65beb5ff | 187 | * |
2269e89e | 188 | * If yes return 1, else return -1. |
947308c4 DG |
189 | */ |
190 | static int check_tracing_group(const char *grp_name) | |
191 | { | |
192 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ | |
193 | gid_t *grp_list; | |
194 | int grp_list_size, grp_id, i; | |
195 | int ret = -1; | |
196 | ||
197 | /* Get GID of group 'tracing' */ | |
198 | grp_tracing = getgrnam(grp_name); | |
b4d8603b MD |
199 | if (!grp_tracing) { |
200 | /* If grp_tracing is NULL, the group does not exist. */ | |
947308c4 DG |
201 | goto end; |
202 | } | |
203 | ||
204 | /* Get number of supplementary group IDs */ | |
205 | grp_list_size = getgroups(0, NULL); | |
206 | if (grp_list_size < 0) { | |
207 | perror("getgroups"); | |
208 | goto end; | |
209 | } | |
210 | ||
211 | /* Alloc group list of the right size */ | |
212 | grp_list = malloc(grp_list_size * sizeof(gid_t)); | |
00795392 | 213 | if (!grp_list) { |
1c8d13c8 | 214 | perror("malloc"); |
00795392 MD |
215 | goto end; |
216 | } | |
947308c4 | 217 | grp_id = getgroups(grp_list_size, grp_list); |
1c8d13c8 | 218 | if (grp_id < 0) { |
947308c4 DG |
219 | perror("getgroups"); |
220 | goto free_list; | |
221 | } | |
222 | ||
223 | for (i = 0; i < grp_list_size; i++) { | |
224 | if (grp_list[i] == grp_tracing->gr_gid) { | |
2269e89e | 225 | ret = 1; |
947308c4 DG |
226 | break; |
227 | } | |
228 | } | |
229 | ||
230 | free_list: | |
231 | free(grp_list); | |
232 | ||
233 | end: | |
234 | return ret; | |
235 | } | |
236 | ||
237 | /* | |
2269e89e DG |
238 | * Try connect to session daemon with sock_path. |
239 | * | |
240 | * Return 0 on success, else -1 | |
241 | */ | |
242 | static int try_connect_sessiond(const char *sock_path) | |
243 | { | |
244 | int ret; | |
245 | ||
246 | /* If socket exist, we check if the daemon listens for connect. */ | |
247 | ret = access(sock_path, F_OK); | |
248 | if (ret < 0) { | |
249 | /* Not alive */ | |
250 | return -1; | |
251 | } | |
252 | ||
253 | ret = lttcomm_connect_unix_sock(sock_path); | |
254 | if (ret < 0) { | |
255 | /* Not alive */ | |
256 | return -1; | |
257 | } | |
258 | ||
259 | ret = lttcomm_close_unix_sock(ret); | |
260 | if (ret < 0) { | |
261 | perror("lttcomm_close_unix_sock"); | |
262 | } | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | /* | |
1c8d13c8 TD |
268 | * Set sessiond socket path by putting it in the global |
269 | * sessiond_sock_path variable. | |
270 | * Returns 0 on success, | |
271 | * -ENOMEM on failure (the sessiond socket path is somehow too long) | |
947308c4 DG |
272 | */ |
273 | static int set_session_daemon_path(void) | |
274 | { | |
275 | int ret; | |
2269e89e DG |
276 | int in_tgroup = 0; /* In tracing group */ |
277 | uid_t uid; | |
278 | ||
279 | uid = getuid(); | |
947308c4 | 280 | |
2269e89e DG |
281 | if (uid != 0) { |
282 | /* Are we in the tracing group ? */ | |
283 | in_tgroup = check_tracing_group(tracing_group); | |
284 | } | |
285 | ||
08a9c49f TD |
286 | if ((uid == 0) || in_tgroup) { |
287 | copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
2269e89e | 288 | sizeof(sessiond_sock_path)); |
08a9c49f | 289 | } |
2269e89e | 290 | |
08a9c49f TD |
291 | if (uid != 0) { |
292 | if (in_tgroup) { | |
293 | /* Tracing group */ | |
294 | ret = try_connect_sessiond(sessiond_sock_path); | |
295 | if (ret >= 0) { | |
296 | goto end; | |
2269e89e | 297 | } |
08a9c49f | 298 | /* Global session daemon not available... */ |
2269e89e | 299 | } |
08a9c49f TD |
300 | /* ...or not in tracing group (and not root), default */ |
301 | ||
302 | /* | |
303 | * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; | |
304 | * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) | |
305 | */ | |
306 | ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), | |
307 | DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME")); | |
308 | if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { | |
947308c4 DG |
309 | return -ENOMEM; |
310 | } | |
947308c4 | 311 | } |
08a9c49f | 312 | end: |
947308c4 DG |
313 | return 0; |
314 | } | |
315 | ||
65beb5ff DG |
316 | /* |
317 | * Connect to the LTTng session daemon. | |
318 | * | |
319 | * On success, return 0. On error, return -1. | |
320 | */ | |
321 | static int connect_sessiond(void) | |
322 | { | |
323 | int ret; | |
324 | ||
325 | ret = set_session_daemon_path(); | |
326 | if (ret < 0) { | |
1c8d13c8 | 327 | return -1; /* set_session_daemon_path() returns -ENOMEM */ |
65beb5ff DG |
328 | } |
329 | ||
330 | /* Connect to the sesssion daemon */ | |
331 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); | |
332 | if (ret < 0) { | |
333 | return ret; | |
334 | } | |
335 | ||
336 | sessiond_socket = ret; | |
337 | connected = 1; | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
342 | /* | |
1c8d13c8 TD |
343 | * Clean disconnect from the session daemon. |
344 | * On success, return 0. On error, return -1. | |
65beb5ff DG |
345 | */ |
346 | static int disconnect_sessiond(void) | |
347 | { | |
348 | int ret = 0; | |
349 | ||
350 | if (connected) { | |
351 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
352 | sessiond_socket = 0; | |
353 | connected = 0; | |
354 | } | |
355 | ||
356 | return ret; | |
357 | } | |
358 | ||
35a6fdb7 | 359 | /* |
cd80958d | 360 | * Ask the session daemon a specific command and put the data into buf. |
53a80697 | 361 | * Takes extra var. len. data as input to send to the session daemon. |
65beb5ff | 362 | * |
af87c45a | 363 | * Return size of data (only payload, not header) or a negative error code. |
65beb5ff | 364 | */ |
53a80697 MD |
365 | static int ask_sessiond_varlen(struct lttcomm_session_msg *lsm, |
366 | void *vardata, | |
367 | size_t varlen, | |
368 | void **buf) | |
65beb5ff DG |
369 | { |
370 | int ret; | |
371 | size_t size; | |
372 | void *data = NULL; | |
cd80958d | 373 | struct lttcomm_lttng_msg llm; |
65beb5ff DG |
374 | |
375 | ret = connect_sessiond(); | |
376 | if (ret < 0) { | |
377 | goto end; | |
378 | } | |
379 | ||
65beb5ff | 380 | /* Send command to session daemon */ |
cd80958d | 381 | ret = send_session_msg(lsm); |
65beb5ff DG |
382 | if (ret < 0) { |
383 | goto end; | |
384 | } | |
53a80697 MD |
385 | /* Send var len data */ |
386 | ret = send_session_varlen(vardata, varlen); | |
387 | if (ret < 0) { | |
388 | goto end; | |
389 | } | |
65beb5ff DG |
390 | |
391 | /* Get header from data transmission */ | |
392 | ret = recv_data_sessiond(&llm, sizeof(llm)); | |
393 | if (ret < 0) { | |
394 | goto end; | |
395 | } | |
396 | ||
397 | /* Check error code if OK */ | |
398 | if (llm.ret_code != LTTCOMM_OK) { | |
399 | ret = -llm.ret_code; | |
400 | goto end; | |
401 | } | |
402 | ||
403 | size = llm.data_size; | |
404 | if (size == 0) { | |
874d3f84 | 405 | /* If client free with size 0 */ |
a45d5536 DG |
406 | if (buf != NULL) { |
407 | *buf = NULL; | |
408 | } | |
7d29a247 | 409 | ret = 0; |
65beb5ff DG |
410 | goto end; |
411 | } | |
412 | ||
413 | data = (void*) malloc(size); | |
414 | ||
415 | /* Get payload data */ | |
416 | ret = recv_data_sessiond(data, size); | |
417 | if (ret < 0) { | |
418 | free(data); | |
419 | goto end; | |
420 | } | |
421 | ||
83009e5e DG |
422 | /* |
423 | * Extra protection not to dereference a NULL pointer. If buf is NULL at | |
424 | * this point, an error is returned and data is freed. | |
425 | */ | |
426 | if (buf == NULL) { | |
427 | ret = -1; | |
428 | free(data); | |
429 | goto end; | |
430 | } | |
431 | ||
65beb5ff DG |
432 | *buf = data; |
433 | ret = size; | |
434 | ||
435 | end: | |
436 | disconnect_sessiond(); | |
437 | return ret; | |
438 | } | |
439 | ||
53a80697 MD |
440 | /* |
441 | * Ask the session daemon a specific command and put the data into buf. | |
442 | * | |
443 | * Return size of data (only payload, not header) or a negative error code. | |
444 | */ | |
445 | static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf) | |
446 | { | |
447 | return ask_sessiond_varlen(lsm, NULL, 0, buf); | |
448 | } | |
449 | ||
9f19cc17 | 450 | /* |
cd80958d | 451 | * Create lttng handle and return pointer. |
1c8d13c8 | 452 | * The returned pointer will be NULL in case of malloc() error. |
9f19cc17 | 453 | */ |
cd80958d DG |
454 | struct lttng_handle *lttng_create_handle(const char *session_name, |
455 | struct lttng_domain *domain) | |
9f19cc17 | 456 | { |
cd80958d DG |
457 | struct lttng_handle *handle; |
458 | ||
459 | handle = malloc(sizeof(struct lttng_handle)); | |
460 | if (handle == NULL) { | |
461 | perror("malloc handle"); | |
462 | goto end; | |
463 | } | |
464 | ||
465 | /* Copy session name */ | |
466 | copy_string(handle->session_name, session_name, | |
467 | sizeof(handle->session_name)); | |
468 | ||
469 | /* Copy lttng domain */ | |
470 | copy_lttng_domain(&handle->domain, domain); | |
471 | ||
472 | end: | |
473 | return handle; | |
474 | } | |
475 | ||
476 | /* | |
477 | * Destroy handle by free(3) the pointer. | |
478 | */ | |
479 | void lttng_destroy_handle(struct lttng_handle *handle) | |
480 | { | |
481 | if (handle) { | |
482 | free(handle); | |
eb354453 DG |
483 | } |
484 | } | |
485 | ||
d9800920 DG |
486 | /* |
487 | * Register an outside consumer. | |
1c8d13c8 | 488 | * Returns size of returned session payload data or a negative error code. |
d9800920 DG |
489 | */ |
490 | int lttng_register_consumer(struct lttng_handle *handle, | |
491 | const char *socket_path) | |
492 | { | |
493 | struct lttcomm_session_msg lsm; | |
494 | ||
495 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; | |
496 | copy_string(lsm.session.name, handle->session_name, | |
497 | sizeof(lsm.session.name)); | |
498 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
499 | ||
500 | copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path)); | |
501 | ||
502 | return ask_sessiond(&lsm, NULL); | |
503 | } | |
504 | ||
1df4dedd | 505 | /* |
1c8d13c8 TD |
506 | * Start tracing for all traces of the session. |
507 | * Returns size of returned session payload data or a negative error code. | |
1df4dedd | 508 | */ |
6a4f824d | 509 | int lttng_start_tracing(const char *session_name) |
f3ed775e | 510 | { |
cd80958d DG |
511 | struct lttcomm_session_msg lsm; |
512 | ||
6a4f824d | 513 | if (session_name == NULL) { |
cd80958d DG |
514 | return -1; |
515 | } | |
516 | ||
517 | lsm.cmd_type = LTTNG_START_TRACE; | |
6a4f824d DG |
518 | |
519 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
520 | |
521 | return ask_sessiond(&lsm, NULL); | |
f3ed775e | 522 | } |
1df4dedd DG |
523 | |
524 | /* | |
1c8d13c8 TD |
525 | * Stop tracing for all traces of the session. |
526 | * Returns size of returned session payload data or a negative error code. | |
f3ed775e | 527 | */ |
6a4f824d | 528 | int lttng_stop_tracing(const char *session_name) |
f3ed775e | 529 | { |
cd80958d DG |
530 | struct lttcomm_session_msg lsm; |
531 | ||
6a4f824d DG |
532 | if (session_name == NULL) { |
533 | return -1; | |
534 | } | |
535 | ||
cd80958d | 536 | lsm.cmd_type = LTTNG_STOP_TRACE; |
6a4f824d DG |
537 | |
538 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
539 | |
540 | return ask_sessiond(&lsm, NULL); | |
f3ed775e DG |
541 | } |
542 | ||
543 | /* | |
1e46a50f TD |
544 | * Add context to event and/or channel. |
545 | * If event_name is NULL, the context is applied to all events of the channel. | |
546 | * If channel_name is NULL, a lookup of the event's channel is done. | |
547 | * If both are NULL, the context is applied to all events of all channels. | |
af87c45a DG |
548 | * |
549 | * Returns the size of the returned payload data or a negative error code. | |
1df4dedd | 550 | */ |
cd80958d | 551 | int lttng_add_context(struct lttng_handle *handle, |
38057ed1 DG |
552 | struct lttng_event_context *ctx, const char *event_name, |
553 | const char *channel_name) | |
d65106b1 | 554 | { |
cd80958d DG |
555 | struct lttcomm_session_msg lsm; |
556 | ||
9d697d3d DG |
557 | /* Safety check. Both are mandatory */ |
558 | if (handle == NULL || ctx == NULL) { | |
cd80958d DG |
559 | return -1; |
560 | } | |
561 | ||
441c16a7 MD |
562 | memset(&lsm, 0, sizeof(lsm)); |
563 | ||
cd80958d DG |
564 | lsm.cmd_type = LTTNG_ADD_CONTEXT; |
565 | ||
566 | /* Copy channel name */ | |
567 | copy_string(lsm.u.context.channel_name, channel_name, | |
568 | sizeof(lsm.u.context.channel_name)); | |
569 | /* Copy event name */ | |
570 | copy_string(lsm.u.context.event_name, event_name, | |
571 | sizeof(lsm.u.context.event_name)); | |
572 | ||
573 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d65106b1 | 574 | |
9d697d3d | 575 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); |
d65106b1 | 576 | |
cd80958d DG |
577 | copy_string(lsm.session.name, handle->session_name, |
578 | sizeof(lsm.session.name)); | |
579 | ||
580 | return ask_sessiond(&lsm, NULL); | |
d65106b1 DG |
581 | } |
582 | ||
f3ed775e | 583 | /* |
1c8d13c8 TD |
584 | * Enable event(s) for a channel. |
585 | * If no event name is specified, all events are enabled. | |
586 | * If no channel name is specified, the default 'channel0' is used. | |
587 | * Returns size of returned session payload data or a negative error code. | |
f3ed775e | 588 | */ |
cd80958d | 589 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 590 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 591 | { |
cd80958d DG |
592 | struct lttcomm_session_msg lsm; |
593 | ||
5117eeec | 594 | if (handle == NULL || ev == NULL) { |
cd80958d DG |
595 | return -1; |
596 | } | |
33a2b854 | 597 | |
441c16a7 MD |
598 | memset(&lsm, 0, sizeof(lsm)); |
599 | ||
5117eeec | 600 | /* If no channel name, we put the default name */ |
94cf3c47 | 601 | if (channel_name == NULL) { |
cd80958d DG |
602 | copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, |
603 | sizeof(lsm.u.enable.channel_name)); | |
33a2b854 | 604 | } else { |
cd80958d DG |
605 | copy_string(lsm.u.enable.channel_name, channel_name, |
606 | sizeof(lsm.u.enable.channel_name)); | |
eb354453 DG |
607 | } |
608 | ||
cd80958d | 609 | copy_lttng_domain(&lsm.domain, &handle->domain); |
0d0c377a | 610 | |
8c9ae521 | 611 | if (ev->name[0] != '\0') { |
cd80958d | 612 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
0d0c377a | 613 | } else { |
cd80958d | 614 | lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT; |
f3ed775e | 615 | } |
8c9ae521 | 616 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
f3ed775e | 617 | |
cd80958d DG |
618 | copy_string(lsm.session.name, handle->session_name, |
619 | sizeof(lsm.session.name)); | |
620 | ||
621 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
622 | } |
623 | ||
53a80697 MD |
624 | /* |
625 | * set filter for an event | |
626 | * Return negative error value on error. | |
627 | * Return size of returned session payload data if OK. | |
628 | */ | |
629 | ||
630 | int lttng_set_event_filter(struct lttng_handle *handle, | |
631 | const char *event_name, const char *channel_name, | |
632 | const char *filter_expression) | |
633 | { | |
634 | struct lttcomm_session_msg lsm; | |
635 | struct filter_parser_ctx *ctx; | |
636 | FILE *fmem; | |
637 | int ret = 0; | |
638 | ||
639 | /* Safety check. */ | |
640 | if (handle == NULL) { | |
641 | return -1; | |
642 | } | |
643 | ||
644 | if (!filter_expression) { | |
645 | return 0; | |
646 | } | |
647 | ||
648 | /* | |
649 | * casting const to non-const, as the underlying function will | |
650 | * use it in read-only mode. | |
651 | */ | |
652 | fmem = lttng_fmemopen((void *) filter_expression, | |
653 | strlen(filter_expression), "r"); | |
654 | if (!fmem) { | |
655 | fprintf(stderr, "Error opening memory as stream\n"); | |
656 | return -ENOMEM; | |
657 | } | |
658 | ctx = filter_parser_ctx_alloc(fmem); | |
659 | if (!ctx) { | |
660 | fprintf(stderr, "Error allocating parser\n"); | |
661 | ret = -ENOMEM; | |
662 | goto alloc_error; | |
663 | } | |
664 | ret = filter_parser_ctx_append_ast(ctx); | |
665 | if (ret) { | |
666 | fprintf(stderr, "Parse error\n"); | |
667 | ret = -EINVAL; | |
668 | goto parse_error; | |
669 | } | |
670 | ret = filter_visitor_set_parent(ctx); | |
671 | if (ret) { | |
672 | fprintf(stderr, "Set parent error\n"); | |
673 | ret = -EINVAL; | |
674 | goto parse_error; | |
675 | } | |
676 | if (print_xml) { | |
677 | ret = filter_visitor_print_xml(ctx, stdout, 0); | |
678 | if (ret) { | |
679 | fflush(stdout); | |
680 | fprintf(stderr, "XML print error\n"); | |
681 | ret = -EINVAL; | |
682 | goto parse_error; | |
683 | } | |
684 | } | |
685 | ||
686 | dbg_printf("Generating IR... "); | |
687 | fflush(stdout); | |
688 | ret = filter_visitor_ir_generate(ctx); | |
689 | if (ret) { | |
690 | fprintf(stderr, "Generate IR error\n"); | |
691 | ret = -EINVAL; | |
692 | goto parse_error; | |
693 | } | |
694 | dbg_printf("done\n"); | |
695 | ||
696 | dbg_printf("Validating IR... "); | |
697 | fflush(stdout); | |
698 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); | |
699 | if (ret) { | |
700 | ret = -EINVAL; | |
701 | goto parse_error; | |
702 | } | |
703 | dbg_printf("done\n"); | |
704 | ||
705 | dbg_printf("Generating bytecode... "); | |
706 | fflush(stdout); | |
707 | ret = filter_visitor_bytecode_generate(ctx); | |
708 | if (ret) { | |
709 | fprintf(stderr, "Generate bytecode error\n"); | |
710 | ret = -EINVAL; | |
711 | goto parse_error; | |
712 | } | |
713 | dbg_printf("done\n"); | |
714 | dbg_printf("Size of bytecode generated: %u bytes.\n", | |
715 | bytecode_get_len(&ctx->bytecode->b)); | |
716 | ||
717 | memset(&lsm, 0, sizeof(lsm)); | |
718 | ||
719 | lsm.cmd_type = LTTNG_SET_FILTER; | |
720 | ||
721 | /* Copy channel name */ | |
722 | copy_string(lsm.u.filter.channel_name, channel_name, | |
723 | sizeof(lsm.u.filter.channel_name)); | |
724 | /* Copy event name */ | |
725 | copy_string(lsm.u.filter.event_name, event_name, | |
726 | sizeof(lsm.u.filter.event_name)); | |
727 | lsm.u.filter.bytecode_len = sizeof(ctx->bytecode->b) | |
728 | + bytecode_get_len(&ctx->bytecode->b); | |
729 | ||
730 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
731 | ||
732 | copy_string(lsm.session.name, handle->session_name, | |
733 | sizeof(lsm.session.name)); | |
734 | ||
735 | ret = ask_sessiond_varlen(&lsm, &ctx->bytecode->b, | |
736 | lsm.u.filter.bytecode_len, NULL); | |
737 | ||
738 | filter_bytecode_free(ctx); | |
739 | filter_ir_free(ctx); | |
740 | filter_parser_ctx_free(ctx); | |
741 | if (fclose(fmem) != 0) { | |
742 | perror("fclose"); | |
743 | } | |
744 | return ret; | |
745 | ||
746 | parse_error: | |
747 | filter_bytecode_free(ctx); | |
748 | filter_ir_free(ctx); | |
749 | filter_parser_ctx_free(ctx); | |
750 | alloc_error: | |
751 | if (fclose(fmem) != 0) { | |
752 | perror("fclose"); | |
753 | } | |
754 | return ret; | |
755 | } | |
756 | ||
1df4dedd | 757 | /* |
1c8d13c8 TD |
758 | * Disable event(s) of a channel and domain. |
759 | * If no event name is specified, all events are disabled. | |
760 | * If no channel name is specified, the default 'channel0' is used. | |
761 | * Returns size of returned session payload data or a negative error code. | |
1df4dedd | 762 | */ |
cd80958d | 763 | int lttng_disable_event(struct lttng_handle *handle, const char *name, |
38057ed1 | 764 | const char *channel_name) |
1df4dedd | 765 | { |
cd80958d | 766 | struct lttcomm_session_msg lsm; |
1df4dedd | 767 | |
9d697d3d | 768 | if (handle == NULL) { |
cd80958d DG |
769 | return -1; |
770 | } | |
771 | ||
441c16a7 MD |
772 | memset(&lsm, 0, sizeof(lsm)); |
773 | ||
cd80958d DG |
774 | if (channel_name) { |
775 | copy_string(lsm.u.disable.channel_name, channel_name, | |
776 | sizeof(lsm.u.disable.channel_name)); | |
f3ed775e | 777 | } else { |
cd80958d DG |
778 | copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, |
779 | sizeof(lsm.u.disable.channel_name)); | |
eb354453 DG |
780 | } |
781 | ||
cd80958d | 782 | copy_lttng_domain(&lsm.domain, &handle->domain); |
f5177a38 | 783 | |
f84efadf | 784 | if (name != NULL) { |
cd80958d DG |
785 | copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name)); |
786 | lsm.cmd_type = LTTNG_DISABLE_EVENT; | |
f5177a38 | 787 | } else { |
cd80958d | 788 | lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT; |
f3ed775e DG |
789 | } |
790 | ||
cd80958d DG |
791 | copy_string(lsm.session.name, handle->session_name, |
792 | sizeof(lsm.session.name)); | |
793 | ||
794 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
795 | } |
796 | ||
797 | /* | |
1c8d13c8 TD |
798 | * Enable channel per domain |
799 | * Returns size of returned session payload data or a negative error code. | |
a5c5a2bd | 800 | */ |
cd80958d | 801 | int lttng_enable_channel(struct lttng_handle *handle, |
38057ed1 | 802 | struct lttng_channel *chan) |
a5c5a2bd | 803 | { |
cd80958d DG |
804 | struct lttcomm_session_msg lsm; |
805 | ||
5117eeec DG |
806 | /* |
807 | * NULL arguments are forbidden. No default values. | |
808 | */ | |
809 | if (handle == NULL || chan == NULL) { | |
cd80958d DG |
810 | return -1; |
811 | } | |
812 | ||
441c16a7 MD |
813 | memset(&lsm, 0, sizeof(lsm)); |
814 | ||
5117eeec | 815 | memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); |
7d29a247 | 816 | |
cd80958d DG |
817 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
818 | ||
819 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
7d29a247 | 820 | |
cd80958d DG |
821 | copy_string(lsm.session.name, handle->session_name, |
822 | sizeof(lsm.session.name)); | |
823 | ||
824 | return ask_sessiond(&lsm, NULL); | |
8c0faa1d | 825 | } |
1df4dedd | 826 | |
2ef84c95 | 827 | /* |
1c8d13c8 TD |
828 | * All tracing will be stopped for registered events of the channel. |
829 | * Returns size of returned session payload data or a negative error code. | |
2ef84c95 | 830 | */ |
cd80958d | 831 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 832 | { |
cd80958d DG |
833 | struct lttcomm_session_msg lsm; |
834 | ||
9d697d3d DG |
835 | /* Safety check. Both are mandatory */ |
836 | if (handle == NULL || name == NULL) { | |
cd80958d DG |
837 | return -1; |
838 | } | |
839 | ||
441c16a7 MD |
840 | memset(&lsm, 0, sizeof(lsm)); |
841 | ||
cd80958d | 842 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
1df4dedd | 843 | |
9d697d3d DG |
844 | copy_string(lsm.u.disable.channel_name, name, |
845 | sizeof(lsm.u.disable.channel_name)); | |
846 | ||
cd80958d DG |
847 | copy_lttng_domain(&lsm.domain, &handle->domain); |
848 | ||
849 | copy_string(lsm.session.name, handle->session_name, | |
850 | sizeof(lsm.session.name)); | |
851 | ||
852 | return ask_sessiond(&lsm, NULL); | |
ca95a216 DG |
853 | } |
854 | ||
fac6795d | 855 | /* |
1c8d13c8 TD |
856 | * Lists all available tracepoints of domain. |
857 | * Sets the contents of the events array. | |
858 | * Returns the number of lttng_event entries in events; | |
859 | * on error, returns a negative value. | |
fac6795d | 860 | */ |
cd80958d | 861 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 862 | struct lttng_event **events) |
fac6795d | 863 | { |
052da939 | 864 | int ret; |
cd80958d DG |
865 | struct lttcomm_session_msg lsm; |
866 | ||
9d697d3d | 867 | if (handle == NULL) { |
cd80958d DG |
868 | return -1; |
869 | } | |
fac6795d | 870 | |
cd80958d DG |
871 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
872 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
2a71efd5 | 873 | |
cd80958d | 874 | ret = ask_sessiond(&lsm, (void **) events); |
052da939 DG |
875 | if (ret < 0) { |
876 | return ret; | |
eb354453 | 877 | } |
fac6795d | 878 | |
9f19cc17 | 879 | return ret / sizeof(struct lttng_event); |
fac6795d DG |
880 | } |
881 | ||
f37d259d MD |
882 | /* |
883 | * Lists all available tracepoint fields of domain. | |
884 | * Sets the contents of the event field array. | |
885 | * Returns the number of lttng_event_field entries in events; | |
886 | * on error, returns a negative value. | |
887 | */ | |
888 | int lttng_list_tracepoint_fields(struct lttng_handle *handle, | |
889 | struct lttng_event_field **fields) | |
890 | { | |
891 | int ret; | |
892 | struct lttcomm_session_msg lsm; | |
893 | ||
894 | if (handle == NULL) { | |
895 | return -1; | |
896 | } | |
897 | ||
898 | lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; | |
899 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
900 | ||
901 | ret = ask_sessiond(&lsm, (void **) fields); | |
902 | if (ret < 0) { | |
903 | return ret; | |
904 | } | |
905 | ||
906 | return ret / sizeof(struct lttng_event_field); | |
907 | } | |
908 | ||
1657e9bb | 909 | /* |
1c8d13c8 TD |
910 | * Returns a human readable string describing |
911 | * the error code (a negative value). | |
1657e9bb | 912 | */ |
9a745bc7 | 913 | const char *lttng_strerror(int code) |
1657e9bb | 914 | { |
1c8d13c8 | 915 | /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */ |
7d29a247 DG |
916 | if (code > -LTTCOMM_OK) { |
917 | return "Ended with errors"; | |
1657e9bb DG |
918 | } |
919 | ||
7d29a247 | 920 | return lttcomm_get_readable_code(code); |
1657e9bb DG |
921 | } |
922 | ||
aaf97519 | 923 | /* |
1c8d13c8 TD |
924 | * Create a brand new session using name and path. |
925 | * Returns size of returned session payload data or a negative error code. | |
aaf97519 | 926 | */ |
38057ed1 | 927 | int lttng_create_session(const char *name, const char *path) |
aaf97519 | 928 | { |
cd80958d DG |
929 | struct lttcomm_session_msg lsm; |
930 | ||
931 | lsm.cmd_type = LTTNG_CREATE_SESSION; | |
932 | copy_string(lsm.session.name, name, sizeof(lsm.session.name)); | |
933 | copy_string(lsm.session.path, path, sizeof(lsm.session.path)); | |
934 | ||
935 | return ask_sessiond(&lsm, NULL); | |
8028d920 DG |
936 | } |
937 | ||
00e2e675 DG |
938 | /* |
939 | * Create a new tracing session using a name, URIs and a consumer enable flag. | |
940 | */ | |
941 | int lttng_create_session_uri(const char *name, struct lttng_uri *ctrl_uri, | |
942 | struct lttng_uri *data_uri, unsigned int enable_consumer) | |
943 | { | |
944 | struct lttcomm_session_msg lsm; | |
945 | ||
946 | /* Name and ctrl_uri are mandatory */ | |
947 | if (name == NULL || ctrl_uri == NULL) { | |
948 | return -1; | |
949 | } | |
950 | ||
951 | lsm.cmd_type = LTTNG_CREATE_SESSION_URI; | |
952 | ||
953 | copy_string(lsm.session.name, name, sizeof(lsm.session.name)); | |
954 | /* Anything bigger than zero, the consumer(s) will be enabled */ | |
955 | lsm.u.create_uri.enable_consumer = enable_consumer; | |
956 | memcpy(&lsm.u.create_uri.ctrl_uri, ctrl_uri, | |
957 | sizeof(lsm.u.create_uri.ctrl_uri)); | |
958 | if (data_uri) { | |
959 | /* | |
960 | * The only possible scenario where data_uri is NULL is for a local | |
961 | * consumer where the output is at a specified path name on the | |
962 | * filesystem. | |
963 | */ | |
964 | memcpy(&lsm.u.create_uri.data_uri, data_uri, | |
965 | sizeof(lsm.u.create_uri.data_uri)); | |
966 | } | |
967 | ||
968 | return ask_sessiond(&lsm, NULL); | |
969 | } | |
970 | ||
8028d920 | 971 | /* |
8028d920 | 972 | * Destroy session using name. |
1c8d13c8 | 973 | * Returns size of returned session payload data or a negative error code. |
8028d920 | 974 | */ |
843f5df9 | 975 | int lttng_destroy_session(const char *session_name) |
8028d920 | 976 | { |
cd80958d DG |
977 | struct lttcomm_session_msg lsm; |
978 | ||
843f5df9 | 979 | if (session_name == NULL) { |
cd80958d DG |
980 | return -1; |
981 | } | |
982 | ||
983 | lsm.cmd_type = LTTNG_DESTROY_SESSION; | |
843f5df9 DG |
984 | |
985 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); | |
cd80958d DG |
986 | |
987 | return ask_sessiond(&lsm, NULL); | |
aaf97519 DG |
988 | } |
989 | ||
57167058 | 990 | /* |
57167058 | 991 | * Ask the session daemon for all available sessions. |
1c8d13c8 TD |
992 | * Sets the contents of the sessions array. |
993 | * Returns the number of lttng_session entries in sessions; | |
994 | * on error, returns a negative value. | |
57167058 | 995 | */ |
ca95a216 | 996 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 997 | { |
ca95a216 | 998 | int ret; |
cd80958d | 999 | struct lttcomm_session_msg lsm; |
57167058 | 1000 | |
cd80958d DG |
1001 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
1002 | ret = ask_sessiond(&lsm, (void**) sessions); | |
57167058 | 1003 | if (ret < 0) { |
ca95a216 | 1004 | return ret; |
57167058 DG |
1005 | } |
1006 | ||
ca95a216 | 1007 | return ret / sizeof(struct lttng_session); |
57167058 DG |
1008 | } |
1009 | ||
9f19cc17 | 1010 | /* |
1c8d13c8 TD |
1011 | * Ask the session daemon for all available domains of a session. |
1012 | * Sets the contents of the domains array. | |
1013 | * Returns the number of lttng_domain entries in domains; | |
1014 | * on error, returns a negative value. | |
9f19cc17 | 1015 | */ |
330be774 | 1016 | int lttng_list_domains(const char *session_name, |
cd80958d | 1017 | struct lttng_domain **domains) |
9f19cc17 DG |
1018 | { |
1019 | int ret; | |
cd80958d DG |
1020 | struct lttcomm_session_msg lsm; |
1021 | ||
330be774 | 1022 | if (session_name == NULL) { |
cd80958d DG |
1023 | return -1; |
1024 | } | |
9f19cc17 | 1025 | |
cd80958d DG |
1026 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
1027 | ||
330be774 | 1028 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); |
cd80958d DG |
1029 | |
1030 | ret = ask_sessiond(&lsm, (void**) domains); | |
9f19cc17 DG |
1031 | if (ret < 0) { |
1032 | return ret; | |
1033 | } | |
1034 | ||
1035 | return ret / sizeof(struct lttng_domain); | |
1036 | } | |
1037 | ||
1038 | /* | |
1c8d13c8 TD |
1039 | * Ask the session daemon for all available channels of a session. |
1040 | * Sets the contents of the channels array. | |
1041 | * Returns the number of lttng_channel entries in channels; | |
1042 | * on error, returns a negative value. | |
9f19cc17 | 1043 | */ |
cd80958d DG |
1044 | int lttng_list_channels(struct lttng_handle *handle, |
1045 | struct lttng_channel **channels) | |
9f19cc17 DG |
1046 | { |
1047 | int ret; | |
cd80958d DG |
1048 | struct lttcomm_session_msg lsm; |
1049 | ||
9d697d3d | 1050 | if (handle == NULL) { |
cd80958d DG |
1051 | return -1; |
1052 | } | |
1053 | ||
1054 | lsm.cmd_type = LTTNG_LIST_CHANNELS; | |
1055 | copy_string(lsm.session.name, handle->session_name, | |
1056 | sizeof(lsm.session.name)); | |
9f19cc17 | 1057 | |
cd80958d | 1058 | copy_lttng_domain(&lsm.domain, &handle->domain); |
9f19cc17 | 1059 | |
cd80958d | 1060 | ret = ask_sessiond(&lsm, (void**) channels); |
9f19cc17 DG |
1061 | if (ret < 0) { |
1062 | return ret; | |
1063 | } | |
1064 | ||
1065 | return ret / sizeof(struct lttng_channel); | |
1066 | } | |
1067 | ||
1068 | /* | |
1c8d13c8 TD |
1069 | * Ask the session daemon for all available events of a session channel. |
1070 | * Sets the contents of the events array. | |
1071 | * Returns the number of lttng_event entries in events; | |
1072 | * on error, returns a negative value. | |
9f19cc17 | 1073 | */ |
cd80958d DG |
1074 | int lttng_list_events(struct lttng_handle *handle, |
1075 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
1076 | { |
1077 | int ret; | |
cd80958d | 1078 | struct lttcomm_session_msg lsm; |
9f19cc17 | 1079 | |
9d697d3d DG |
1080 | /* Safety check. An handle and channel name are mandatory */ |
1081 | if (handle == NULL || channel_name == NULL) { | |
cd80958d DG |
1082 | return -1; |
1083 | } | |
1084 | ||
1085 | lsm.cmd_type = LTTNG_LIST_EVENTS; | |
1086 | copy_string(lsm.session.name, handle->session_name, | |
1087 | sizeof(lsm.session.name)); | |
1088 | copy_string(lsm.u.list.channel_name, channel_name, | |
1089 | sizeof(lsm.u.list.channel_name)); | |
1090 | ||
1091 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
9f19cc17 | 1092 | |
cd80958d | 1093 | ret = ask_sessiond(&lsm, (void**) events); |
9f19cc17 DG |
1094 | if (ret < 0) { |
1095 | return ret; | |
1096 | } | |
1097 | ||
1098 | return ret / sizeof(struct lttng_event); | |
1099 | } | |
1100 | ||
fac6795d | 1101 | /* |
1c8d13c8 TD |
1102 | * Sets the tracing_group variable with name. |
1103 | * This function allocates memory pointed to by tracing_group. | |
1104 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. | |
fac6795d DG |
1105 | */ |
1106 | int lttng_set_tracing_group(const char *name) | |
1107 | { | |
9d697d3d DG |
1108 | if (name == NULL) { |
1109 | return -1; | |
1110 | } | |
1111 | ||
fac6795d DG |
1112 | if (asprintf(&tracing_group, "%s", name) < 0) { |
1113 | return -ENOMEM; | |
1114 | } | |
1115 | ||
1116 | return 0; | |
1117 | } | |
1118 | ||
d0254c7c | 1119 | /* |
af87c45a | 1120 | * Returns size of returned session payload data or a negative error code. |
d0254c7c | 1121 | */ |
cd80958d | 1122 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
1123 | struct lttng_calibrate *calibrate) |
1124 | { | |
cd80958d | 1125 | struct lttcomm_session_msg lsm; |
d0254c7c | 1126 | |
9d697d3d DG |
1127 | /* Safety check. NULL pointer are forbidden */ |
1128 | if (handle == NULL || calibrate == NULL) { | |
cd80958d DG |
1129 | return -1; |
1130 | } | |
d0254c7c | 1131 | |
cd80958d DG |
1132 | lsm.cmd_type = LTTNG_CALIBRATE; |
1133 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d0254c7c | 1134 | |
cd80958d DG |
1135 | memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); |
1136 | ||
1137 | return ask_sessiond(&lsm, NULL); | |
d0254c7c MD |
1138 | } |
1139 | ||
5edd7e09 DG |
1140 | /* |
1141 | * Set default channel attributes. | |
441c16a7 | 1142 | * If either or both of the arguments are null, attr content is zeroe'd. |
5edd7e09 DG |
1143 | */ |
1144 | void lttng_channel_set_default_attr(struct lttng_domain *domain, | |
1145 | struct lttng_channel_attr *attr) | |
1146 | { | |
1147 | /* Safety check */ | |
1148 | if (attr == NULL || domain == NULL) { | |
1149 | return; | |
1150 | } | |
1151 | ||
eacaa7a6 DS |
1152 | memset(attr, 0, sizeof(struct lttng_channel_attr)); |
1153 | ||
5edd7e09 DG |
1154 | switch (domain->type) { |
1155 | case LTTNG_DOMAIN_KERNEL: | |
1156 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
1157 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
1158 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
1159 | ||
1160 | attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE; | |
1161 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; | |
1162 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
1163 | break; | |
1164 | case LTTNG_DOMAIN_UST: | |
d78d6610 | 1165 | #if 0 |
5edd7e09 DG |
1166 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
1167 | case LTTNG_DOMAIN_UST_PID: | |
1168 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 1169 | #endif |
5edd7e09 DG |
1170 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; |
1171 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
1172 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
1173 | ||
1174 | attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE; | |
1175 | attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM; | |
1176 | attr->output = DEFAULT_UST_CHANNEL_OUTPUT; | |
1177 | break; | |
1178 | default: | |
441c16a7 | 1179 | /* Default behavior: leave set to 0. */ |
5edd7e09 DG |
1180 | break; |
1181 | } | |
1182 | } | |
1183 | ||
fac6795d | 1184 | /* |
2269e89e | 1185 | * Check if session daemon is alive. |
fac6795d | 1186 | * |
2269e89e | 1187 | * Return 1 if alive or 0 if not. |
1c8d13c8 | 1188 | * On error returns a negative value. |
fac6795d | 1189 | */ |
947308c4 | 1190 | int lttng_session_daemon_alive(void) |
fac6795d DG |
1191 | { |
1192 | int ret; | |
1193 | ||
1194 | ret = set_session_daemon_path(); | |
1195 | if (ret < 0) { | |
947308c4 | 1196 | /* Error */ |
fac6795d DG |
1197 | return ret; |
1198 | } | |
1199 | ||
2269e89e DG |
1200 | if (strlen(sessiond_sock_path) == 0) { |
1201 | /* No socket path set. Weird error */ | |
1202 | return -1; | |
fac6795d DG |
1203 | } |
1204 | ||
2269e89e | 1205 | ret = try_connect_sessiond(sessiond_sock_path); |
7d8234d9 MD |
1206 | if (ret < 0) { |
1207 | /* Not alive */ | |
1208 | return 0; | |
1209 | } | |
7d8234d9 | 1210 | |
947308c4 DG |
1211 | /* Is alive */ |
1212 | return 1; | |
fac6795d DG |
1213 | } |
1214 | ||
00e2e675 DG |
1215 | /* |
1216 | * Set URI for a consumer for a session and domain. | |
1217 | * | |
1218 | * Return 0 on success, else a negative value. | |
1219 | */ | |
1220 | int lttng_set_consumer_uri(struct lttng_handle *handle, struct lttng_uri *uri) | |
1221 | { | |
1222 | struct lttcomm_session_msg lsm; | |
1223 | ||
1224 | if (handle == NULL || uri == NULL) { | |
1225 | return -1; | |
1226 | } | |
1227 | ||
1228 | lsm.cmd_type = LTTNG_SET_CONSUMER_URI; | |
1229 | ||
1230 | copy_string(lsm.session.name, handle->session_name, | |
1231 | sizeof(lsm.session.name)); | |
1232 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
1233 | ||
1234 | memcpy(&lsm.u.uri, uri, sizeof(lsm.u.uri)); | |
1235 | ||
1236 | return ask_sessiond(&lsm, NULL); | |
1237 | } | |
1238 | ||
1239 | /* | |
1240 | * Enable consumer for a session and domain. | |
1241 | * | |
1242 | * Return 0 on success, else a negative value. | |
1243 | */ | |
1244 | int lttng_enable_consumer(struct lttng_handle *handle) | |
1245 | { | |
1246 | struct lttcomm_session_msg lsm; | |
1247 | ||
1248 | if (handle == NULL) { | |
1249 | return -1; | |
1250 | } | |
1251 | ||
1252 | lsm.cmd_type = LTTNG_ENABLE_CONSUMER; | |
1253 | ||
1254 | copy_string(lsm.session.name, handle->session_name, | |
1255 | sizeof(lsm.session.name)); | |
1256 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
1257 | ||
1258 | return ask_sessiond(&lsm, NULL); | |
1259 | } | |
1260 | ||
1261 | /* | |
1262 | * Disable consumer for a session and domain. | |
1263 | * | |
1264 | * Return 0 on success, else a negative value. | |
1265 | */ | |
1266 | int lttng_disable_consumer(struct lttng_handle *handle) | |
1267 | { | |
1268 | struct lttcomm_session_msg lsm; | |
1269 | ||
1270 | if (handle == NULL) { | |
1271 | return -1; | |
1272 | } | |
1273 | ||
1274 | lsm.cmd_type = LTTNG_DISABLE_CONSUMER; | |
1275 | ||
1276 | copy_string(lsm.session.name, handle->session_name, | |
1277 | sizeof(lsm.session.name)); | |
1278 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
1279 | ||
1280 | return ask_sessiond(&lsm, NULL); | |
1281 | } | |
1282 | ||
44a5e5eb DG |
1283 | /* |
1284 | * Set health socket path by putting it in the global health_sock_path | |
1285 | * variable. | |
1286 | * | |
1287 | * Returns 0 on success or assert(0) on ENOMEM. | |
1288 | */ | |
1289 | static int set_health_socket_path(void) | |
1290 | { | |
1291 | int ret; | |
1292 | int in_tgroup = 0; /* In tracing group */ | |
1293 | uid_t uid; | |
1294 | const char *home; | |
1295 | ||
1296 | uid = getuid(); | |
1297 | ||
1298 | if (uid != 0) { | |
1299 | /* Are we in the tracing group ? */ | |
1300 | in_tgroup = check_tracing_group(tracing_group); | |
1301 | } | |
1302 | ||
1303 | if ((uid == 0) || in_tgroup) { | |
1304 | copy_string(health_sock_path, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK, | |
1305 | sizeof(health_sock_path)); | |
1306 | } | |
1307 | ||
1308 | if (uid != 0) { | |
1309 | /* | |
1310 | * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; | |
1311 | * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) | |
1312 | */ | |
1313 | home = getenv("HOME"); | |
1314 | if (home == NULL) { | |
1315 | /* Fallback in /tmp .. */ | |
1316 | home = "/tmp"; | |
1317 | } | |
1318 | ||
1319 | ret = snprintf(health_sock_path, sizeof(health_sock_path), | |
1320 | DEFAULT_HOME_HEALTH_UNIX_SOCK, home); | |
1321 | if ((ret < 0) || (ret >= sizeof(health_sock_path))) { | |
1322 | /* ENOMEM at this point... just kill the control lib. */ | |
1323 | assert(0); | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | return 0; | |
1328 | } | |
1329 | ||
1330 | /* | |
1331 | * Check session daemon health for a specific health component. | |
1332 | * | |
1333 | * Return 0 if health is OK or else 1 if BAD. A return value of -1 indicate | |
1334 | * that the control library was not able to connect to the session daemon | |
1335 | * health socket. | |
1336 | * | |
1337 | * Any other positive value is an lttcomm error which can be translate with | |
1338 | * lttng_strerror(). | |
1339 | */ | |
1340 | int lttng_health_check(enum lttng_health_component c) | |
1341 | { | |
1342 | int sock, ret; | |
1343 | struct lttcomm_health_msg msg; | |
1344 | struct lttcomm_health_data reply; | |
1345 | ||
1346 | /* Connect to the sesssion daemon */ | |
1347 | sock = lttcomm_connect_unix_sock(health_sock_path); | |
1348 | if (sock < 0) { | |
1349 | ret = -1; | |
1350 | goto error; | |
1351 | } | |
1352 | ||
1353 | msg.cmd = LTTNG_HEALTH_CHECK; | |
1354 | msg.component = c; | |
1355 | ||
1356 | ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg)); | |
1357 | if (ret < 0) { | |
1358 | goto close_error; | |
1359 | } | |
1360 | ||
1361 | ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply)); | |
1362 | if (ret < 0) { | |
1363 | goto close_error; | |
1364 | } | |
1365 | ||
1366 | ret = reply.ret_code; | |
1367 | ||
1368 | close_error: | |
1369 | close(sock); | |
1370 | ||
1371 | error: | |
1372 | return ret; | |
1373 | } | |
1374 | ||
fac6795d DG |
1375 | /* |
1376 | * lib constructor | |
1377 | */ | |
1378 | static void __attribute__((constructor)) init() | |
1379 | { | |
1380 | /* Set default session group */ | |
bbccc3d2 | 1381 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
44a5e5eb DG |
1382 | /* Set socket for health check */ |
1383 | (void) set_health_socket_path(); | |
fac6795d | 1384 | } |