Fix: refactor function that fixes memory leak also
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
CommitLineData
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>
a4b92340 34#include <common/uri.h>
feb0f3e5 35#include <common/utils.h>
1e307fab 36#include <lttng/lttng.h>
0c89d795 37#include <lttng/health-internal.h>
fac6795d 38
d00c599e
DG
39#include "filter/filter-ast.h"
40#include "filter/filter-parser.h"
41#include "filter/filter-bytecode.h"
42#include "filter/memstream.h"
cac3069d 43#include "lttng-ctl-helper.h"
53a80697
MD
44
45#ifdef DEBUG
d00c599e 46static const int print_xml = 1;
53a80697
MD
47#define dbg_printf(fmt, args...) \
48 printf("[debug liblttng-ctl] " fmt, ## args)
49#else
d00c599e 50static const int print_xml = 0;
53a80697
MD
51#define dbg_printf(fmt, args...) \
52do { \
53 /* do nothing but check printf format */ \
54 if (0) \
55 printf("[debug liblttnctl] " fmt, ## args); \
56} while (0)
57#endif
58
59
fac6795d
DG
60/* Socket to session daemon for communication */
61static int sessiond_socket;
62static char sessiond_sock_path[PATH_MAX];
63
fac6795d
DG
64/* Variables */
65static char *tracing_group;
66static int connected;
67
97e19046
DG
68/* Global */
69
70/*
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
97e19046
DG
74 */
75int lttng_opt_quiet;
76int lttng_opt_verbose;
77
99497cd0
MD
78/*
79 * Copy string from src to dst and enforce null terminated byte.
80 */
cac3069d
DG
81LTTNG_HIDDEN
82void lttng_ctl_copy_string(char *dst, const char *src, size_t len)
99497cd0 83{
e7d6716d 84 if (src && dst) {
99497cd0
MD
85 strncpy(dst, src, len);
86 /* Enforce the NULL terminated byte */
87 dst[len - 1] = '\0';
cd80958d
DG
88 } else if (dst) {
89 dst[0] = '\0';
99497cd0
MD
90 }
91}
92
fac6795d 93/*
cd80958d 94 * Copy domain to lttcomm_session_msg domain.
fac6795d 95 *
cd80958d
DG
96 * If domain is unknown, default domain will be the kernel.
97 */
cac3069d
DG
98LTTNG_HIDDEN
99void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
100 struct lttng_domain *src)
cd80958d
DG
101{
102 if (src && dst) {
103 switch (src->type) {
00e2e675
DG
104 case LTTNG_DOMAIN_KERNEL:
105 case LTTNG_DOMAIN_UST:
b9dfb167 106 case LTTNG_DOMAIN_JUL:
00e2e675
DG
107 memcpy(dst, src, sizeof(struct lttng_domain));
108 break;
109 default:
110 memset(dst, 0, sizeof(struct lttng_domain));
00e2e675 111 break;
cd80958d
DG
112 }
113 }
114}
115
116/*
117 * Send lttcomm_session_msg to the session daemon.
fac6795d 118 *
1c8d13c8
TD
119 * On success, returns the number of bytes sent (>=0)
120 * On error, returns -1
fac6795d 121 */
cd80958d 122static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
123{
124 int ret;
125
126 if (!connected) {
2f70b271 127 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 128 goto end;
fac6795d
DG
129 }
130
a4b92340
DG
131 DBG("LSM cmd type : %d", lsm->cmd_type);
132
be040666 133 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 134 sizeof(struct lttcomm_session_msg));
2f70b271
DG
135 if (ret < 0) {
136 ret = -LTTNG_ERR_FATAL;
137 }
e065084a
DG
138
139end:
140 return ret;
141}
142
53a80697
MD
143/*
144 * Send var len data to the session daemon.
145 *
146 * On success, returns the number of bytes sent (>=0)
147 * On error, returns -1
148 */
149static int send_session_varlen(void *data, size_t len)
150{
151 int ret;
152
153 if (!connected) {
2f70b271 154 ret = -LTTNG_ERR_NO_SESSIOND;
53a80697
MD
155 goto end;
156 }
a4b92340 157
53a80697
MD
158 if (!data || !len) {
159 ret = 0;
160 goto end;
161 }
162
163 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
2f70b271
DG
164 if (ret < 0) {
165 ret = -LTTNG_ERR_FATAL;
166 }
53a80697
MD
167
168end:
169 return ret;
170}
171
e065084a 172/*
cd80958d 173 * Receive data from the sessiond socket.
e065084a 174 *
1c8d13c8
TD
175 * On success, returns the number of bytes received (>=0)
176 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 177 */
ca95a216 178static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
179{
180 int ret;
181
182 if (!connected) {
2f70b271 183 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 184 goto end;
fac6795d
DG
185 }
186
ca95a216 187 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
2f70b271
DG
188 if (ret < 0) {
189 ret = -LTTNG_ERR_FATAL;
190 }
fac6795d 191
e065084a 192end:
fac6795d
DG
193 return ret;
194}
195
196/*
1c8d13c8 197 * Check if we are in the specified group.
65beb5ff 198 *
2269e89e 199 * If yes return 1, else return -1.
947308c4 200 */
6c71277b
MD
201LTTNG_HIDDEN
202int lttng_check_tracing_group(void)
947308c4
DG
203{
204 struct group *grp_tracing; /* no free(). See getgrnam(3) */
205 gid_t *grp_list;
206 int grp_list_size, grp_id, i;
207 int ret = -1;
6c71277b 208 const char *grp_name = tracing_group;
947308c4
DG
209
210 /* Get GID of group 'tracing' */
211 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
212 if (!grp_tracing) {
213 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
214 goto end;
215 }
216
217 /* Get number of supplementary group IDs */
218 grp_list_size = getgroups(0, NULL);
219 if (grp_list_size < 0) {
220 perror("getgroups");
221 goto end;
222 }
223
224 /* Alloc group list of the right size */
225 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 226 if (!grp_list) {
1c8d13c8 227 perror("malloc");
00795392
MD
228 goto end;
229 }
947308c4 230 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 231 if (grp_id < 0) {
947308c4
DG
232 perror("getgroups");
233 goto free_list;
234 }
235
236 for (i = 0; i < grp_list_size; i++) {
237 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 238 ret = 1;
947308c4
DG
239 break;
240 }
241 }
242
243free_list:
244 free(grp_list);
245
246end:
247 return ret;
248}
249
250/*
2269e89e
DG
251 * Try connect to session daemon with sock_path.
252 *
253 * Return 0 on success, else -1
254 */
255static int try_connect_sessiond(const char *sock_path)
256{
257 int ret;
258
259 /* If socket exist, we check if the daemon listens for connect. */
260 ret = access(sock_path, F_OK);
261 if (ret < 0) {
262 /* Not alive */
2f70b271 263 goto error;
2269e89e
DG
264 }
265
266 ret = lttcomm_connect_unix_sock(sock_path);
267 if (ret < 0) {
268 /* Not alive */
2f70b271 269 goto error;
2269e89e
DG
270 }
271
272 ret = lttcomm_close_unix_sock(ret);
273 if (ret < 0) {
274 perror("lttcomm_close_unix_sock");
275 }
276
277 return 0;
2f70b271
DG
278
279error:
280 return -1;
2269e89e
DG
281}
282
283/*
2f70b271
DG
284 * Set sessiond socket path by putting it in the global sessiond_sock_path
285 * variable.
286 *
287 * Returns 0 on success, negative value on failure (the sessiond socket path
288 * is somehow too long or ENOMEM).
947308c4
DG
289 */
290static int set_session_daemon_path(void)
291{
2269e89e
DG
292 int in_tgroup = 0; /* In tracing group */
293 uid_t uid;
294
295 uid = getuid();
947308c4 296
2269e89e
DG
297 if (uid != 0) {
298 /* Are we in the tracing group ? */
6c71277b 299 in_tgroup = lttng_check_tracing_group();
2269e89e
DG
300 }
301
08a9c49f 302 if ((uid == 0) || in_tgroup) {
cac3069d
DG
303 lttng_ctl_copy_string(sessiond_sock_path,
304 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path));
08a9c49f 305 }
2269e89e 306
08a9c49f 307 if (uid != 0) {
c617c0c6
MD
308 int ret;
309
08a9c49f
TD
310 if (in_tgroup) {
311 /* Tracing group */
312 ret = try_connect_sessiond(sessiond_sock_path);
313 if (ret >= 0) {
314 goto end;
2269e89e 315 }
08a9c49f 316 /* Global session daemon not available... */
2269e89e 317 }
08a9c49f
TD
318 /* ...or not in tracing group (and not root), default */
319
320 /*
321 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
322 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
323 */
324 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
feb0f3e5 325 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
08a9c49f 326 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
2f70b271 327 goto error;
947308c4 328 }
947308c4 329 }
08a9c49f 330end:
947308c4 331 return 0;
2f70b271
DG
332
333error:
334 return -1;
947308c4
DG
335}
336
65beb5ff
DG
337/*
338 * Connect to the LTTng session daemon.
339 *
340 * On success, return 0. On error, return -1.
341 */
342static int connect_sessiond(void)
343{
344 int ret;
345
da3c9ec1
DG
346 /* Don't try to connect if already connected. */
347 if (connected) {
348 return 0;
349 }
350
65beb5ff
DG
351 ret = set_session_daemon_path();
352 if (ret < 0) {
2f70b271 353 goto error;
65beb5ff
DG
354 }
355
356 /* Connect to the sesssion daemon */
357 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
358 if (ret < 0) {
2f70b271 359 goto error;
65beb5ff
DG
360 }
361
362 sessiond_socket = ret;
363 connected = 1;
364
365 return 0;
2f70b271
DG
366
367error:
368 return -1;
65beb5ff
DG
369}
370
371/*
1c8d13c8
TD
372 * Clean disconnect from the session daemon.
373 * On success, return 0. On error, return -1.
65beb5ff
DG
374 */
375static int disconnect_sessiond(void)
376{
377 int ret = 0;
378
379 if (connected) {
380 ret = lttcomm_close_unix_sock(sessiond_socket);
381 sessiond_socket = 0;
382 connected = 0;
383 }
384
385 return ret;
386}
387
35a6fdb7 388/*
cd80958d 389 * Ask the session daemon a specific command and put the data into buf.
53a80697 390 * Takes extra var. len. data as input to send to the session daemon.
65beb5ff 391 *
af87c45a 392 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 393 */
cac3069d
DG
394LTTNG_HIDDEN
395int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
a4b92340 396 void *vardata, size_t varlen, void **buf)
65beb5ff
DG
397{
398 int ret;
399 size_t size;
400 void *data = NULL;
cd80958d 401 struct lttcomm_lttng_msg llm;
65beb5ff
DG
402
403 ret = connect_sessiond();
404 if (ret < 0) {
2f70b271 405 ret = -LTTNG_ERR_NO_SESSIOND;
65beb5ff
DG
406 goto end;
407 }
408
65beb5ff 409 /* Send command to session daemon */
cd80958d 410 ret = send_session_msg(lsm);
65beb5ff 411 if (ret < 0) {
2f70b271 412 /* Ret value is a valid lttng error code. */
65beb5ff
DG
413 goto end;
414 }
53a80697
MD
415 /* Send var len data */
416 ret = send_session_varlen(vardata, varlen);
417 if (ret < 0) {
2f70b271 418 /* Ret value is a valid lttng error code. */
53a80697
MD
419 goto end;
420 }
65beb5ff
DG
421
422 /* Get header from data transmission */
423 ret = recv_data_sessiond(&llm, sizeof(llm));
424 if (ret < 0) {
2f70b271 425 /* Ret value is a valid lttng error code. */
65beb5ff
DG
426 goto end;
427 }
428
429 /* Check error code if OK */
f73fabfd 430 if (llm.ret_code != LTTNG_OK) {
65beb5ff
DG
431 ret = -llm.ret_code;
432 goto end;
433 }
434
435 size = llm.data_size;
436 if (size == 0) {
874d3f84 437 /* If client free with size 0 */
a45d5536
DG
438 if (buf != NULL) {
439 *buf = NULL;
440 }
7d29a247 441 ret = 0;
65beb5ff
DG
442 goto end;
443 }
444
445 data = (void*) malloc(size);
446
447 /* Get payload data */
448 ret = recv_data_sessiond(data, size);
449 if (ret < 0) {
450 free(data);
451 goto end;
452 }
453
83009e5e
DG
454 /*
455 * Extra protection not to dereference a NULL pointer. If buf is NULL at
456 * this point, an error is returned and data is freed.
457 */
458 if (buf == NULL) {
2f70b271 459 ret = -LTTNG_ERR_INVALID;
83009e5e
DG
460 free(data);
461 goto end;
462 }
463
65beb5ff
DG
464 *buf = data;
465 ret = size;
466
467end:
468 disconnect_sessiond();
469 return ret;
470}
471
9f19cc17 472/*
cd80958d 473 * Create lttng handle and return pointer.
1c8d13c8 474 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 475 */
cd80958d
DG
476struct lttng_handle *lttng_create_handle(const char *session_name,
477 struct lttng_domain *domain)
9f19cc17 478{
2f70b271
DG
479 struct lttng_handle *handle = NULL;
480
481 if (domain == NULL) {
482 goto end;
483 }
cd80958d
DG
484
485 handle = malloc(sizeof(struct lttng_handle));
486 if (handle == NULL) {
2f70b271 487 PERROR("malloc handle");
cd80958d
DG
488 goto end;
489 }
490
491 /* Copy session name */
cac3069d 492 lttng_ctl_copy_string(handle->session_name, session_name,
cd80958d
DG
493 sizeof(handle->session_name));
494
495 /* Copy lttng domain */
cac3069d 496 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
cd80958d
DG
497
498end:
499 return handle;
500}
501
502/*
503 * Destroy handle by free(3) the pointer.
504 */
505void lttng_destroy_handle(struct lttng_handle *handle)
506{
0e428499 507 free(handle);
eb354453
DG
508}
509
d9800920
DG
510/*
511 * Register an outside consumer.
1c8d13c8 512 * Returns size of returned session payload data or a negative error code.
d9800920
DG
513 */
514int lttng_register_consumer(struct lttng_handle *handle,
515 const char *socket_path)
516{
517 struct lttcomm_session_msg lsm;
518
2f70b271
DG
519 if (handle == NULL || socket_path == NULL) {
520 return -LTTNG_ERR_INVALID;
521 }
522
53efb85a 523 memset(&lsm, 0, sizeof(lsm));
d9800920 524 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
cac3069d 525 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
d9800920 526 sizeof(lsm.session.name));
cac3069d 527 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d9800920 528
cac3069d 529 lttng_ctl_copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
d9800920 530
cac3069d 531 return lttng_ctl_ask_sessiond(&lsm, NULL);
d9800920
DG
532}
533
1df4dedd 534/*
1c8d13c8
TD
535 * Start tracing for all traces of the session.
536 * Returns size of returned session payload data or a negative error code.
1df4dedd 537 */
6a4f824d 538int lttng_start_tracing(const char *session_name)
f3ed775e 539{
cd80958d
DG
540 struct lttcomm_session_msg lsm;
541
6a4f824d 542 if (session_name == NULL) {
2f70b271 543 return -LTTNG_ERR_INVALID;
cd80958d
DG
544 }
545
53efb85a 546 memset(&lsm, 0, sizeof(lsm));
cd80958d 547 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d 548
cac3069d
DG
549 lttng_ctl_copy_string(lsm.session.name, session_name,
550 sizeof(lsm.session.name));
cd80958d 551
cac3069d 552 return lttng_ctl_ask_sessiond(&lsm, NULL);
f3ed775e 553}
1df4dedd
DG
554
555/*
38ee087f 556 * Stop tracing for all traces of the session.
f3ed775e 557 */
38ee087f 558static int _lttng_stop_tracing(const char *session_name, int wait)
f3ed775e 559{
38ee087f 560 int ret, data_ret;
cd80958d
DG
561 struct lttcomm_session_msg lsm;
562
6a4f824d 563 if (session_name == NULL) {
2f70b271 564 return -LTTNG_ERR_INVALID;
6a4f824d
DG
565 }
566
53efb85a 567 memset(&lsm, 0, sizeof(lsm));
cd80958d 568 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d 569
cac3069d
DG
570 lttng_ctl_copy_string(lsm.session.name, session_name,
571 sizeof(lsm.session.name));
cd80958d 572
cac3069d 573 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
38ee087f
DG
574 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
575 goto error;
576 }
577
578 if (!wait) {
579 goto end;
580 }
581
38ee087f
DG
582 /* Check for data availability */
583 do {
6d805429 584 data_ret = lttng_data_pending(session_name);
38ee087f
DG
585 if (data_ret < 0) {
586 /* Return the data available call error. */
587 ret = data_ret;
588 goto error;
589 }
590
591 /*
592 * Data sleep time before retrying (in usec). Don't sleep if the call
593 * returned value indicates availability.
594 */
6d805429 595 if (data_ret) {
38ee087f 596 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
38ee087f 597 }
6d805429 598 } while (data_ret != 0);
38ee087f 599
38ee087f
DG
600end:
601error:
602 return ret;
603}
604
605/*
606 * Stop tracing and wait for data availability.
607 */
608int lttng_stop_tracing(const char *session_name)
609{
610 return _lttng_stop_tracing(session_name, 1);
611}
612
613/*
614 * Stop tracing but _don't_ wait for data availability.
615 */
616int lttng_stop_tracing_no_wait(const char *session_name)
617{
618 return _lttng_stop_tracing(session_name, 0);
f3ed775e
DG
619}
620
621/*
601d5acf
DG
622 * Add context to a channel.
623 *
624 * If the given channel is NULL, add the contexts to all channels.
625 * The event_name param is ignored.
af87c45a
DG
626 *
627 * Returns the size of the returned payload data or a negative error code.
1df4dedd 628 */
cd80958d 629int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
630 struct lttng_event_context *ctx, const char *event_name,
631 const char *channel_name)
d65106b1 632{
cd80958d
DG
633 struct lttcomm_session_msg lsm;
634
9d697d3d
DG
635 /* Safety check. Both are mandatory */
636 if (handle == NULL || ctx == NULL) {
2f70b271 637 return -LTTNG_ERR_INVALID;
cd80958d
DG
638 }
639
441c16a7
MD
640 memset(&lsm, 0, sizeof(lsm));
641
cd80958d
DG
642 lsm.cmd_type = LTTNG_ADD_CONTEXT;
643
85076754
MD
644 /* If no channel name, send empty string. */
645 if (channel_name == NULL) {
646 lttng_ctl_copy_string(lsm.u.context.channel_name, "",
647 sizeof(lsm.u.context.channel_name));
648 } else {
649 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
650 sizeof(lsm.u.context.channel_name));
651 }
cd80958d 652
cac3069d 653 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 654
9d697d3d 655 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 656
cac3069d 657 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
658 sizeof(lsm.session.name));
659
cac3069d 660 return lttng_ctl_ask_sessiond(&lsm, NULL);
d65106b1
DG
661}
662
f3ed775e 663/*
1c8d13c8
TD
664 * Enable event(s) for a channel.
665 * If no event name is specified, all events are enabled.
666 * If no channel name is specified, the default 'channel0' is used.
667 * Returns size of returned session payload data or a negative error code.
f3ed775e 668 */
cd80958d 669int lttng_enable_event(struct lttng_handle *handle,
38057ed1 670 struct lttng_event *ev, const char *channel_name)
1df4dedd 671{
f8a96544
JI
672 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
673 NULL, 0, NULL);
1df4dedd
DG
674}
675
53a80697 676/*
025faf73 677 * Create or enable an event with a filter expression.
178191b3 678 *
53a80697
MD
679 * Return negative error value on error.
680 * Return size of returned session payload data if OK.
681 */
025faf73 682int lttng_enable_event_with_filter(struct lttng_handle *handle,
178191b3 683 struct lttng_event *event, const char *channel_name,
53a80697
MD
684 const char *filter_expression)
685{
f8a96544
JI
686 return lttng_enable_event_with_exclusions(handle, event, channel_name,
687 filter_expression, 0, NULL);
53a80697
MD
688}
689
347c5ab5
DG
690/*
691 * Depending on the event, return a newly allocated JUL filter expression or
692 * NULL if not applicable.
693 *
694 * An event with NO loglevel and the name is * will return NULL.
695 */
696static char *set_jul_filter(const char *filter, struct lttng_event *ev)
697{
698 int err;
699 char *jul_filter = NULL;
700
701 assert(ev);
702
703 /* Don't add filter for the '*' event. */
704 if (ev->name[0] != '*') {
705 if (filter) {
706 err = asprintf(&jul_filter, "%s && logger_name == \"%s\"", filter,
707 ev->name);
708 } else {
709 err = asprintf(&jul_filter, "logger_name == \"%s\"", ev->name);
710 }
711 if (err < 0) {
712 PERROR("asprintf");
713 goto end;
714 }
715 }
716
717 /* Add loglevel filtering if any for the JUL domain. */
718 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
719 char *op;
720
721 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
722 op = ">=";
723 } else {
724 op = "==";
725 }
726
727 if (filter) {
728 err = asprintf(&jul_filter, "%s && int_loglevel %s %d", filter, op,
729 ev->loglevel);
730 } else {
731 err = asprintf(&jul_filter, "int_loglevel %s %d", op,
732 ev->loglevel);
733 }
734 if (err < 0) {
735 PERROR("asprintf");
736 free(jul_filter);
737 jul_filter = NULL;
738 goto end;
739 }
740 }
741
742end:
743 return jul_filter;
744}
745
137b9942
DG
746/*
747 * Generate the filter bytecode from a give filter expression string. Put the
748 * newly allocated parser context in ctxp and populate the lsm object with the
749 * expression len.
750 *
751 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
752 */
753static int generate_filter(char *filter_expression,
754 struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp)
755{
756 int ret;
757 struct filter_parser_ctx *ctx = NULL;
758 FILE *fmem = NULL;
759
760 assert(filter_expression);
761 assert(lsm);
762 assert(ctxp);
763
764 /*
765 * Casting const to non-const, as the underlying function will use it in
766 * read-only mode.
767 */
768 fmem = lttng_fmemopen((void *) filter_expression,
769 strlen(filter_expression), "r");
770 if (!fmem) {
771 fprintf(stderr, "Error opening memory as stream\n");
772 ret = -LTTNG_ERR_FILTER_NOMEM;
773 goto error;
774 }
775 ctx = filter_parser_ctx_alloc(fmem);
776 if (!ctx) {
777 fprintf(stderr, "Error allocating parser\n");
778 ret = -LTTNG_ERR_FILTER_NOMEM;
779 goto filter_alloc_error;
780 }
781 ret = filter_parser_ctx_append_ast(ctx);
782 if (ret) {
783 fprintf(stderr, "Parse error\n");
784 ret = -LTTNG_ERR_FILTER_INVAL;
785 goto parse_error;
786 }
787 ret = filter_visitor_set_parent(ctx);
788 if (ret) {
789 fprintf(stderr, "Set parent error\n");
790 ret = -LTTNG_ERR_FILTER_INVAL;
791 goto parse_error;
792 }
793 if (print_xml) {
794 ret = filter_visitor_print_xml(ctx, stdout, 0);
795 if (ret) {
796 fflush(stdout);
797 fprintf(stderr, "XML print error\n");
798 ret = -LTTNG_ERR_FILTER_INVAL;
799 goto parse_error;
800 }
801 }
802
803 dbg_printf("Generating IR... ");
804 fflush(stdout);
805 ret = filter_visitor_ir_generate(ctx);
806 if (ret) {
807 fprintf(stderr, "Generate IR error\n");
808 ret = -LTTNG_ERR_FILTER_INVAL;
809 goto parse_error;
810 }
811 dbg_printf("done\n");
812
813 dbg_printf("Validating IR... ");
814 fflush(stdout);
815 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
816 if (ret) {
817 ret = -LTTNG_ERR_FILTER_INVAL;
818 goto parse_error;
819 }
820 dbg_printf("done\n");
821
822 dbg_printf("Generating bytecode... ");
823 fflush(stdout);
824 ret = filter_visitor_bytecode_generate(ctx);
825 if (ret) {
826 fprintf(stderr, "Generate bytecode error\n");
827 ret = -LTTNG_ERR_FILTER_INVAL;
828 goto parse_error;
829 }
830 dbg_printf("done\n");
831 dbg_printf("Size of bytecode generated: %u bytes.\n",
832 bytecode_get_len(&ctx->bytecode->b));
833
834 lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b)
835 + bytecode_get_len(&ctx->bytecode->b);
836 lsm->u.enable.expression_len = strlen(filter_expression) + 1;
837
838 /* No need to keep the memory stream. */
839 if (fclose(fmem) != 0) {
840 perror("fclose");
841 }
842
843 *ctxp = ctx;
844 return 0;
845
846parse_error:
847 filter_bytecode_free(ctx);
848 filter_ir_free(ctx);
849 filter_parser_ctx_free(ctx);
850filter_alloc_error:
851 if (fclose(fmem) != 0) {
852 perror("fclose");
853 }
854error:
855 return ret;
856}
857
93deb080
JI
858/*
859 * Enable event(s) for a channel, possibly with exclusions and a filter.
860 * If no event name is specified, all events are enabled.
861 * If no channel name is specified, the default name is used.
862 * If filter expression is not NULL, the filter is set for the event.
863 * If exclusion count is not zero, the exclusions are set for the event.
864 * Returns size of returned session payload data or a negative error code.
865 */
866int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
867 struct lttng_event *ev, const char *channel_name,
e9efbcd3 868 const char *original_filter_expression,
93deb080
JI
869 int exclusion_count, char **exclusion_list)
870{
871 struct lttcomm_session_msg lsm;
64226865 872 char *varlen_data;
93deb080 873 int ret = 0;
137b9942 874 unsigned int free_filter_expression = 0;
93deb080 875 struct filter_parser_ctx *ctx = NULL;
e9efbcd3
JG
876 /*
877 * Cast as non-const since we may replace the filter expression
878 * by a dynamically allocated string. Otherwise, the original
879 * string is not modified.
880 */
881 char *filter_expression = (char *) original_filter_expression;
93deb080
JI
882
883 if (handle == NULL || ev == NULL) {
137b9942
DG
884 ret = -LTTNG_ERR_INVALID;
885 goto error;
93deb080
JI
886 }
887
888 /* Empty filter string will always be rejected by the parser
889 * anyway, so treat this corner-case early to eliminate
890 * lttng_fmemopen error for 0-byte allocation.
891 */
892 if (filter_expression && filter_expression[0] == '\0') {
137b9942
DG
893 ret = -LTTNG_ERR_INVALID;
894 goto error;
93deb080
JI
895 }
896
897 memset(&lsm, 0, sizeof(lsm));
898
899 /* If no channel name, send empty string. */
900 if (channel_name == NULL) {
901 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
902 sizeof(lsm.u.enable.channel_name));
903 } else {
904 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
905 sizeof(lsm.u.enable.channel_name));
906 }
907
6565421f
DG
908 if (ev->name[0] != '\0') {
909 lsm.cmd_type = LTTNG_ENABLE_EVENT;
910 } else {
911 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
912 }
93deb080 913
6565421f 914 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
93deb080
JI
915 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
916
917 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
918 sizeof(lsm.session.name));
919 lsm.u.enable.exclusion_count = exclusion_count;
920 lsm.u.enable.bytecode_len = 0;
921
9b21e6d5
DG
922 /*
923 * For the JUL domain, a filter is enforced except for the enable all
924 * event. This is done to avoid having the event in all sessions thus
925 * filtering by logger name.
926 */
927 if (exclusion_count == 0 && filter_expression == NULL &&
3e1c9ff7
DG
928 handle->domain.type != LTTNG_DOMAIN_JUL) {
929 goto ask_sessiond;
93deb080
JI
930 }
931
932 /*
933 * We have either a filter or some exclusions, so we need to set up
934 * a variable-length memory block from where to send the data
935 */
936
937 /* Parse filter expression */
3e1c9ff7 938 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL) {
9b21e6d5 939 if (handle->domain.type == LTTNG_DOMAIN_JUL) {
64226865
DG
940 char *jul_filter;
941
347c5ab5 942 /* Setup JUL filter if needed. */
64226865 943 jul_filter = set_jul_filter(filter_expression, ev);
137b9942
DG
944 if (!jul_filter) {
945 if (!filter_expression) {
946 /* No JUL and no filter, just skip everything below. */
947 goto ask_sessiond;
948 }
64226865
DG
949 } else {
950 /*
951 * With a JUL filter, the original filter has been added to it
952 * thus replace the filter expression.
953 */
e9efbcd3
JG
954 filter_expression = jul_filter;
955 free_filter_expression = 1;
9b21e6d5 956 }
9b21e6d5 957 }
93deb080 958
137b9942 959 ret = generate_filter(filter_expression, &lsm, &ctx);
93deb080 960 if (ret) {
137b9942 961 goto filter_error;
93deb080 962 }
6b453b5e
JG
963 }
964
965 varlen_data = zmalloc(lsm.u.enable.bytecode_len
137b9942
DG
966 + lsm.u.enable.expression_len
967 + LTTNG_SYMBOL_NAME_LEN * exclusion_count);
6b453b5e
JG
968 if (!varlen_data) {
969 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
137b9942 970 goto filter_error;
6b453b5e 971 }
137b9942 972
6b453b5e
JG
973 /* Put exclusion names first in the data */
974 while (exclusion_count--) {
975 strncpy(varlen_data + LTTNG_SYMBOL_NAME_LEN * exclusion_count,
137b9942 976 *(exclusion_list + exclusion_count), LTTNG_SYMBOL_NAME_LEN);
6b453b5e
JG
977 }
978 /* Add filter expression next */
979 if (lsm.u.enable.expression_len != 0) {
980 memcpy(varlen_data
981 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count,
982 filter_expression,
983 lsm.u.enable.expression_len);
984 }
985 /* Add filter bytecode next */
137b9942 986 if (ctx && lsm.u.enable.bytecode_len != 0) {
6b453b5e
JG
987 memcpy(varlen_data
988 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count
989 + lsm.u.enable.expression_len,
990 &ctx->bytecode->b,
991 lsm.u.enable.bytecode_len);
93deb080
JI
992 }
993
994 ret = lttng_ctl_ask_sessiond_varlen(&lsm, varlen_data,
67676bd8 995 (LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count) +
137b9942 996 lsm.u.enable.bytecode_len + lsm.u.enable.expression_len, NULL);
6b453b5e 997 free(varlen_data);
93deb080 998
137b9942 999filter_error:
93deb080
JI
1000 if (filter_expression) {
1001 filter_bytecode_free(ctx);
1002 filter_ir_free(ctx);
1003 filter_parser_ctx_free(ctx);
e9efbcd3
JG
1004 if (free_filter_expression) {
1005 /*
1006 * The filter expression has been replaced and must be
1007 * freed as it is not the original filter expression
1008 * received as a parameter.
1009 */
1010 free(filter_expression);
1011 }
93deb080 1012 }
137b9942
DG
1013error:
1014 /*
1015 * Return directly to the caller and don't ask the sessiond since something
1016 * went wrong in the parsing of data above.
1017 */
93deb080 1018 return ret;
3e1c9ff7
DG
1019
1020ask_sessiond:
1021 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1022 return ret;
93deb080
JI
1023}
1024
1df4dedd 1025/*
1c8d13c8
TD
1026 * Disable event(s) of a channel and domain.
1027 * If no event name is specified, all events are disabled.
1028 * If no channel name is specified, the default 'channel0' is used.
1029 * Returns size of returned session payload data or a negative error code.
1df4dedd 1030 */
cd80958d 1031int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 1032 const char *channel_name)
1df4dedd 1033{
cd80958d 1034 struct lttcomm_session_msg lsm;
1df4dedd 1035
9d697d3d 1036 if (handle == NULL) {
2f70b271 1037 return -LTTNG_ERR_INVALID;
cd80958d
DG
1038 }
1039
441c16a7
MD
1040 memset(&lsm, 0, sizeof(lsm));
1041
85076754
MD
1042 /* If no channel name, send empty string. */
1043 if (channel_name == NULL) {
1044 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
cd80958d 1045 sizeof(lsm.u.disable.channel_name));
f3ed775e 1046 } else {
85076754 1047 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
cd80958d 1048 sizeof(lsm.u.disable.channel_name));
eb354453
DG
1049 }
1050
cac3069d 1051 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 1052
f84efadf 1053 if (name != NULL) {
cac3069d
DG
1054 lttng_ctl_copy_string(lsm.u.disable.name, name,
1055 sizeof(lsm.u.disable.name));
cd80958d 1056 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 1057 } else {
cd80958d 1058 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
1059 }
1060
cac3069d 1061 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1062 sizeof(lsm.session.name));
1063
cac3069d 1064 return lttng_ctl_ask_sessiond(&lsm, NULL);
1df4dedd
DG
1065}
1066
1067/*
1c8d13c8
TD
1068 * Enable channel per domain
1069 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 1070 */
cd80958d 1071int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 1072 struct lttng_channel *chan)
a5c5a2bd 1073{
cd80958d
DG
1074 struct lttcomm_session_msg lsm;
1075
5117eeec
DG
1076 /*
1077 * NULL arguments are forbidden. No default values.
1078 */
1079 if (handle == NULL || chan == NULL) {
2f70b271 1080 return -LTTNG_ERR_INVALID;
cd80958d
DG
1081 }
1082
441c16a7
MD
1083 memset(&lsm, 0, sizeof(lsm));
1084
5117eeec 1085 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 1086
cd80958d
DG
1087 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1088
cac3069d 1089 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 1090
cac3069d 1091 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1092 sizeof(lsm.session.name));
1093
cac3069d 1094 return lttng_ctl_ask_sessiond(&lsm, NULL);
8c0faa1d 1095}
1df4dedd 1096
2ef84c95 1097/*
1c8d13c8
TD
1098 * All tracing will be stopped for registered events of the channel.
1099 * Returns size of returned session payload data or a negative error code.
2ef84c95 1100 */
cd80958d 1101int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 1102{
cd80958d
DG
1103 struct lttcomm_session_msg lsm;
1104
9d697d3d
DG
1105 /* Safety check. Both are mandatory */
1106 if (handle == NULL || name == NULL) {
2f70b271 1107 return -LTTNG_ERR_INVALID;
cd80958d
DG
1108 }
1109
441c16a7
MD
1110 memset(&lsm, 0, sizeof(lsm));
1111
cd80958d 1112 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 1113
cac3069d 1114 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
9d697d3d
DG
1115 sizeof(lsm.u.disable.channel_name));
1116
cac3069d 1117 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cd80958d 1118
cac3069d 1119 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1120 sizeof(lsm.session.name));
1121
cac3069d 1122 return lttng_ctl_ask_sessiond(&lsm, NULL);
ca95a216
DG
1123}
1124
fac6795d 1125/*
1c8d13c8
TD
1126 * Lists all available tracepoints of domain.
1127 * Sets the contents of the events array.
1128 * Returns the number of lttng_event entries in events;
1129 * on error, returns a negative value.
fac6795d 1130 */
cd80958d 1131int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 1132 struct lttng_event **events)
fac6795d 1133{
052da939 1134 int ret;
cd80958d
DG
1135 struct lttcomm_session_msg lsm;
1136
9d697d3d 1137 if (handle == NULL) {
2f70b271 1138 return -LTTNG_ERR_INVALID;
cd80958d 1139 }
fac6795d 1140
53efb85a 1141 memset(&lsm, 0, sizeof(lsm));
cd80958d 1142 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
cac3069d 1143 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 1144
cac3069d 1145 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
1146 if (ret < 0) {
1147 return ret;
eb354453 1148 }
fac6795d 1149
9f19cc17 1150 return ret / sizeof(struct lttng_event);
fac6795d
DG
1151}
1152
f37d259d
MD
1153/*
1154 * Lists all available tracepoint fields of domain.
1155 * Sets the contents of the event field array.
1156 * Returns the number of lttng_event_field entries in events;
1157 * on error, returns a negative value.
1158 */
1159int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1160 struct lttng_event_field **fields)
1161{
1162 int ret;
1163 struct lttcomm_session_msg lsm;
1164
1165 if (handle == NULL) {
2f70b271 1166 return -LTTNG_ERR_INVALID;
f37d259d
MD
1167 }
1168
53efb85a 1169 memset(&lsm, 0, sizeof(lsm));
f37d259d 1170 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
cac3069d 1171 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f37d259d 1172
cac3069d 1173 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
1174 if (ret < 0) {
1175 return ret;
1176 }
1177
1178 return ret / sizeof(struct lttng_event_field);
1179}
1180
1657e9bb 1181/*
1c8d13c8
TD
1182 * Returns a human readable string describing
1183 * the error code (a negative value).
1657e9bb 1184 */
9a745bc7 1185const char *lttng_strerror(int code)
1657e9bb 1186{
f73fabfd 1187 return error_get_str(code);
1657e9bb
DG
1188}
1189
aaf97519 1190/*
a4b92340
DG
1191 * Create a brand new session using name and url for destination.
1192 *
f73fabfd 1193 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1194 */
a4b92340 1195int lttng_create_session(const char *name, const char *url)
00e2e675 1196{
3dd05a85 1197 int ret;
a4b92340 1198 ssize_t size;
00e2e675 1199 struct lttcomm_session_msg lsm;
a4b92340 1200 struct lttng_uri *uris = NULL;
00e2e675 1201
a4b92340 1202 if (name == NULL) {
2f70b271 1203 return -LTTNG_ERR_INVALID;
00e2e675
DG
1204 }
1205
a4b92340 1206 memset(&lsm, 0, sizeof(lsm));
00e2e675 1207
a4b92340 1208 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1209 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1210
1211 /* There should never be a data URL */
bc894455 1212 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1213 if (size < 0) {
2f70b271 1214 return -LTTNG_ERR_INVALID;
00e2e675
DG
1215 }
1216
a4b92340
DG
1217 lsm.u.uri.size = size;
1218
cac3069d
DG
1219 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1220 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1221
1222 free(uris);
1223 return ret;
00e2e675
DG
1224}
1225
8028d920 1226/*
8028d920 1227 * Destroy session using name.
1c8d13c8 1228 * Returns size of returned session payload data or a negative error code.
8028d920 1229 */
843f5df9 1230int lttng_destroy_session(const char *session_name)
8028d920 1231{
cd80958d
DG
1232 struct lttcomm_session_msg lsm;
1233
843f5df9 1234 if (session_name == NULL) {
2f70b271 1235 return -LTTNG_ERR_INVALID;
cd80958d
DG
1236 }
1237
53efb85a 1238 memset(&lsm, 0, sizeof(lsm));
cd80958d 1239 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9 1240
cac3069d
DG
1241 lttng_ctl_copy_string(lsm.session.name, session_name,
1242 sizeof(lsm.session.name));
cd80958d 1243
cac3069d 1244 return lttng_ctl_ask_sessiond(&lsm, NULL);
aaf97519
DG
1245}
1246
57167058 1247/*
57167058 1248 * Ask the session daemon for all available sessions.
1c8d13c8
TD
1249 * Sets the contents of the sessions array.
1250 * Returns the number of lttng_session entries in sessions;
1251 * on error, returns a negative value.
57167058 1252 */
ca95a216 1253int lttng_list_sessions(struct lttng_session **sessions)
57167058 1254{
ca95a216 1255 int ret;
cd80958d 1256 struct lttcomm_session_msg lsm;
57167058 1257
53efb85a 1258 memset(&lsm, 0, sizeof(lsm));
cd80958d 1259 lsm.cmd_type = LTTNG_LIST_SESSIONS;
cac3069d 1260 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
57167058 1261 if (ret < 0) {
ca95a216 1262 return ret;
57167058
DG
1263 }
1264
ca95a216 1265 return ret / sizeof(struct lttng_session);
57167058
DG
1266}
1267
9f19cc17 1268/*
1c8d13c8
TD
1269 * Ask the session daemon for all available domains of a session.
1270 * Sets the contents of the domains array.
1271 * Returns the number of lttng_domain entries in domains;
1272 * on error, returns a negative value.
9f19cc17 1273 */
330be774 1274int lttng_list_domains(const char *session_name,
cd80958d 1275 struct lttng_domain **domains)
9f19cc17
DG
1276{
1277 int ret;
cd80958d
DG
1278 struct lttcomm_session_msg lsm;
1279
330be774 1280 if (session_name == NULL) {
2f70b271 1281 return -LTTNG_ERR_INVALID;
cd80958d 1282 }
9f19cc17 1283
53efb85a 1284 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
1285 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1286
cac3069d
DG
1287 lttng_ctl_copy_string(lsm.session.name, session_name,
1288 sizeof(lsm.session.name));
cd80958d 1289
cac3069d 1290 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1291 if (ret < 0) {
1292 return ret;
1293 }
1294
1295 return ret / sizeof(struct lttng_domain);
1296}
1297
1298/*
1c8d13c8
TD
1299 * Ask the session daemon for all available channels of a session.
1300 * Sets the contents of the channels array.
1301 * Returns the number of lttng_channel entries in channels;
1302 * on error, returns a negative value.
9f19cc17 1303 */
cd80958d
DG
1304int lttng_list_channels(struct lttng_handle *handle,
1305 struct lttng_channel **channels)
9f19cc17
DG
1306{
1307 int ret;
cd80958d
DG
1308 struct lttcomm_session_msg lsm;
1309
9d697d3d 1310 if (handle == NULL) {
2f70b271 1311 return -LTTNG_ERR_INVALID;
cd80958d
DG
1312 }
1313
53efb85a 1314 memset(&lsm, 0, sizeof(lsm));
cd80958d 1315 lsm.cmd_type = LTTNG_LIST_CHANNELS;
cac3069d 1316 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1317 sizeof(lsm.session.name));
9f19cc17 1318
cac3069d 1319 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1320
cac3069d 1321 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
1322 if (ret < 0) {
1323 return ret;
1324 }
1325
1326 return ret / sizeof(struct lttng_channel);
1327}
1328
1329/*
1c8d13c8
TD
1330 * Ask the session daemon for all available events of a session channel.
1331 * Sets the contents of the events array.
1332 * Returns the number of lttng_event entries in events;
1333 * on error, returns a negative value.
9f19cc17 1334 */
cd80958d
DG
1335int lttng_list_events(struct lttng_handle *handle,
1336 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1337{
1338 int ret;
cd80958d 1339 struct lttcomm_session_msg lsm;
9f19cc17 1340
9d697d3d
DG
1341 /* Safety check. An handle and channel name are mandatory */
1342 if (handle == NULL || channel_name == NULL) {
2f70b271 1343 return -LTTNG_ERR_INVALID;
cd80958d
DG
1344 }
1345
53efb85a 1346 memset(&lsm, 0, sizeof(lsm));
cd80958d 1347 lsm.cmd_type = LTTNG_LIST_EVENTS;
cac3069d 1348 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1349 sizeof(lsm.session.name));
cac3069d 1350 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
cd80958d
DG
1351 sizeof(lsm.u.list.channel_name));
1352
cac3069d 1353 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1354
cac3069d 1355 ret = lttng_ctl_ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
1356 if (ret < 0) {
1357 return ret;
1358 }
1359
1360 return ret / sizeof(struct lttng_event);
1361}
1362
fac6795d 1363/*
1c8d13c8
TD
1364 * Sets the tracing_group variable with name.
1365 * This function allocates memory pointed to by tracing_group.
1366 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
1367 */
1368int lttng_set_tracing_group(const char *name)
1369{
9d697d3d 1370 if (name == NULL) {
2f70b271 1371 return -LTTNG_ERR_INVALID;
9d697d3d
DG
1372 }
1373
fac6795d 1374 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 1375 return -LTTNG_ERR_FATAL;
fac6795d
DG
1376 }
1377
1378 return 0;
1379}
1380
d0254c7c 1381/*
af87c45a 1382 * Returns size of returned session payload data or a negative error code.
d0254c7c 1383 */
cd80958d 1384int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
1385 struct lttng_calibrate *calibrate)
1386{
cd80958d 1387 struct lttcomm_session_msg lsm;
d0254c7c 1388
9d697d3d
DG
1389 /* Safety check. NULL pointer are forbidden */
1390 if (handle == NULL || calibrate == NULL) {
2f70b271 1391 return -LTTNG_ERR_INVALID;
cd80958d 1392 }
d0254c7c 1393
53efb85a 1394 memset(&lsm, 0, sizeof(lsm));
cd80958d 1395 lsm.cmd_type = LTTNG_CALIBRATE;
cac3069d 1396 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 1397
cd80958d
DG
1398 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1399
cac3069d 1400 return lttng_ctl_ask_sessiond(&lsm, NULL);
d0254c7c
MD
1401}
1402
5edd7e09
DG
1403/*
1404 * Set default channel attributes.
441c16a7 1405 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
1406 */
1407void lttng_channel_set_default_attr(struct lttng_domain *domain,
1408 struct lttng_channel_attr *attr)
1409{
1410 /* Safety check */
1411 if (attr == NULL || domain == NULL) {
1412 return;
1413 }
1414
eacaa7a6
DS
1415 memset(attr, 0, sizeof(struct lttng_channel_attr));
1416
0a9c6494
DG
1417 /* Same for all domains. */
1418 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1419 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
1420 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
1421
5edd7e09
DG
1422 switch (domain->type) {
1423 case LTTNG_DOMAIN_KERNEL:
d92ff3ef
DG
1424 attr->switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
1425 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 1426 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
1427 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1428 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1429 break;
1430 case LTTNG_DOMAIN_UST:
0a9c6494
DG
1431 switch (domain->buf_type) {
1432 case LTTNG_BUFFER_PER_UID:
1433 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
1434 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
1435 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
1436 attr->switch_timer_interval = DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
1437 attr->read_timer_interval = DEFAULT_UST_UID_CHANNEL_READ_TIMER;
1438 break;
1439 case LTTNG_BUFFER_PER_PID:
1440 default:
1441 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
1442 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
1443 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
1444 attr->switch_timer_interval = DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
1445 attr->read_timer_interval = DEFAULT_UST_PID_CHANNEL_READ_TIMER;
1446 break;
1447 }
5edd7e09 1448 default:
441c16a7 1449 /* Default behavior: leave set to 0. */
5edd7e09
DG
1450 break;
1451 }
1452}
1453
fac6795d 1454/*
2269e89e 1455 * Check if session daemon is alive.
fac6795d 1456 *
2269e89e 1457 * Return 1 if alive or 0 if not.
1c8d13c8 1458 * On error returns a negative value.
fac6795d 1459 */
947308c4 1460int lttng_session_daemon_alive(void)
fac6795d
DG
1461{
1462 int ret;
1463
1464 ret = set_session_daemon_path();
1465 if (ret < 0) {
947308c4 1466 /* Error */
fac6795d
DG
1467 return ret;
1468 }
1469
9d035200 1470 if (*sessiond_sock_path == '\0') {
2f70b271
DG
1471 /*
1472 * No socket path set. Weird error which means the constructor was not
1473 * called.
1474 */
1475 assert(0);
fac6795d
DG
1476 }
1477
2269e89e 1478 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
1479 if (ret < 0) {
1480 /* Not alive */
1481 return 0;
1482 }
7d8234d9 1483
947308c4
DG
1484 /* Is alive */
1485 return 1;
fac6795d
DG
1486}
1487
00e2e675 1488/*
a4b92340 1489 * Set URL for a consumer for a session and domain.
00e2e675
DG
1490 *
1491 * Return 0 on success, else a negative value.
1492 */
a4b92340
DG
1493int lttng_set_consumer_url(struct lttng_handle *handle,
1494 const char *control_url, const char *data_url)
00e2e675 1495{
3dd05a85 1496 int ret;
a4b92340 1497 ssize_t size;
00e2e675 1498 struct lttcomm_session_msg lsm;
a4b92340 1499 struct lttng_uri *uris = NULL;
00e2e675 1500
a4b92340 1501 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 1502 return -LTTNG_ERR_INVALID;
00e2e675
DG
1503 }
1504
a4b92340
DG
1505 memset(&lsm, 0, sizeof(lsm));
1506
00e2e675
DG
1507 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1508
cac3069d 1509 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
00e2e675 1510 sizeof(lsm.session.name));
cac3069d 1511 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
00e2e675 1512
bc894455 1513 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 1514 if (size < 0) {
2f70b271 1515 return -LTTNG_ERR_INVALID;
a4b92340
DG
1516 }
1517
1518 lsm.u.uri.size = size;
00e2e675 1519
cac3069d
DG
1520 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1521 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1522
1523 free(uris);
1524 return ret;
00e2e675
DG
1525}
1526
1527/*
9c6bda17 1528 * [OBSOLETE]
00e2e675
DG
1529 */
1530int lttng_enable_consumer(struct lttng_handle *handle)
1531{
785d2d0d 1532 return -ENOSYS;
00e2e675
DG
1533}
1534
1535/*
9c6bda17 1536 * [OBSOLETE]
00e2e675
DG
1537 */
1538int lttng_disable_consumer(struct lttng_handle *handle)
1539{
785d2d0d 1540 return -ENOSYS;
00e2e675
DG
1541}
1542
07424f16
DG
1543/*
1544 * This is an extension of create session that is ONLY and SHOULD only be used
1545 * by the lttng command line program. It exists to avoid using URI parsing in
1546 * the lttng client.
1547 *
1548 * We need the date and time for the trace path subdirectory for the case where
1549 * the user does NOT define one using either -o or -U. Using the normal
1550 * lttng_create_session API call, we have no clue on the session daemon side if
1551 * the URL was generated automatically by the client or define by the user.
1552 *
1553 * So this function "wrapper" is hidden from the public API, takes the datetime
1554 * string and appends it if necessary to the URI subdirectory before sending it
1555 * to the session daemon.
1556 *
1557 * With this extra function, the lttng_create_session call behavior is not
1558 * changed and the timestamp is appended to the URI on the session daemon side
1559 * if necessary.
1560 */
1561int _lttng_create_session_ext(const char *name, const char *url,
1562 const char *datetime)
1563{
3dd05a85 1564 int ret;
07424f16
DG
1565 ssize_t size;
1566 struct lttcomm_session_msg lsm;
1567 struct lttng_uri *uris = NULL;
1568
2bba9e53 1569 if (name == NULL || datetime == NULL) {
2f70b271 1570 return -LTTNG_ERR_INVALID;
07424f16
DG
1571 }
1572
1573 memset(&lsm, 0, sizeof(lsm));
1574
1575 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1576 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
07424f16
DG
1577
1578 /* There should never be a data URL */
bc894455 1579 size = uri_parse_str_urls(url, NULL, &uris);
07424f16 1580 if (size < 0) {
3dd05a85
DG
1581 ret = -LTTNG_ERR_INVALID;
1582 goto error;
07424f16
DG
1583 }
1584
1585 lsm.u.uri.size = size;
1586
4b35a6b3 1587 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
da4aa2b8
DG
1588 /* Don't append datetime if the name was automatically created. */
1589 if (strncmp(name, DEFAULT_SESSION_NAME "-",
1590 strlen(DEFAULT_SESSION_NAME) + 1)) {
1591 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
1592 name, datetime);
1593 } else {
1594 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
1595 }
07424f16
DG
1596 if (ret < 0) {
1597 PERROR("snprintf uri subdir");
3dd05a85
DG
1598 ret = -LTTNG_ERR_FATAL;
1599 goto error;
07424f16
DG
1600 }
1601 }
1602
cac3069d
DG
1603 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1604 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1605
1606error:
1607 free(uris);
1608 return ret;
07424f16
DG
1609}
1610
806e2684
DG
1611/*
1612 * For a given session name, this call checks if the data is ready to be read
1613 * or is still being extracted by the consumer(s) hence not ready to be used by
1614 * any readers.
1615 */
6d805429 1616int lttng_data_pending(const char *session_name)
806e2684
DG
1617{
1618 int ret;
1619 struct lttcomm_session_msg lsm;
1620
1621 if (session_name == NULL) {
1622 return -LTTNG_ERR_INVALID;
1623 }
1624
53efb85a 1625 memset(&lsm, 0, sizeof(lsm));
6d805429 1626 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 1627
cac3069d
DG
1628 lttng_ctl_copy_string(lsm.session.name, session_name,
1629 sizeof(lsm.session.name));
806e2684 1630
cac3069d 1631 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
806e2684
DG
1632
1633 /*
cac3069d
DG
1634 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1635 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1636 * that the data is available. Yes it is hackish but for now this is the
1637 * only way.
806e2684
DG
1638 */
1639 if (ret == -1) {
1640 ret = 1;
1641 }
1642
1643 return ret;
1644}
1645
27babd3a
DG
1646/*
1647 * Create a session exclusively used for snapshot.
1648 *
1649 * Returns LTTNG_OK on success or a negative error code.
1650 */
1651int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
1652{
1653 int ret;
1654 ssize_t size;
1655 struct lttcomm_session_msg lsm;
1656 struct lttng_uri *uris = NULL;
1657
1658 if (name == NULL) {
1659 return -LTTNG_ERR_INVALID;
1660 }
1661
1662 memset(&lsm, 0, sizeof(lsm));
1663
1664 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
1665 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1666
1667 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
1668 if (size < 0) {
1669 return -LTTNG_ERR_INVALID;
1670 }
1671
1672 lsm.u.uri.size = size;
1673
1674 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1675 sizeof(struct lttng_uri) * size, NULL);
1676
1677 free(uris);
1678 return ret;
1679}
1680
ecc48a90
JD
1681/*
1682 * Create a session exclusively used for live.
1683 *
1684 * Returns LTTNG_OK on success or a negative error code.
1685 */
1686int lttng_create_session_live(const char *name, const char *url,
1687 unsigned int timer_interval)
1688{
1689 int ret;
1690 ssize_t size;
1691 struct lttcomm_session_msg lsm;
1692 struct lttng_uri *uris = NULL;
1693
1694 if (name == NULL) {
1695 return -LTTNG_ERR_INVALID;
1696 }
1697
1698 memset(&lsm, 0, sizeof(lsm));
1699
1700 lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE;
1701 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1702
1a241656
DG
1703 if (url) {
1704 size = uri_parse_str_urls(url, NULL, &uris);
1705 if (size <= 0) {
1706 ret = -LTTNG_ERR_INVALID;
1707 goto end;
1708 }
ecc48a90 1709
1a241656
DG
1710 /* file:// is not accepted for live session. */
1711 if (uris[0].dtype == LTTNG_DST_PATH) {
1712 ret = -LTTNG_ERR_INVALID;
1713 goto end;
1714 }
1715 } else {
1716 size = 0;
ecc48a90
JD
1717 }
1718
1719 lsm.u.session_live.nb_uri = size;
1720 lsm.u.session_live.timer_interval = timer_interval;
1721
1722 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1723 sizeof(struct lttng_uri) * size, NULL);
1724
1725end:
1726 free(uris);
1727 return ret;
1728}
1729
fac6795d
DG
1730/*
1731 * lib constructor
1732 */
1733static void __attribute__((constructor)) init()
1734{
1735 /* Set default session group */
bbccc3d2 1736 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 1737}
49cca668
DG
1738
1739/*
1740 * lib destructor
1741 */
1742static void __attribute__((destructor)) lttng_ctl_exit()
1743{
1744 free(tracing_group);
1745}
This page took 0.134068 seconds and 4 git commands to generate.