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