Fix: logging: log_add_time() save/restore errno
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
CommitLineData
826d496d 1/*
eb5c4f4e 2 * lttng-ctl.c
82a3637f
DG
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 32#include <common/common.h>
15e37663 33#include <common/compat/string.h>
990570ed 34#include <common/defaults.h>
db758600 35#include <common/sessiond-comm/sessiond-comm.h>
a4b92340 36#include <common/uri.h>
feb0f3e5 37#include <common/utils.h>
de453daa 38#include <common/dynamic-buffer.h>
1e307fab 39#include <lttng/lttng.h>
0c89d795 40#include <lttng/health-internal.h>
a58c490f
JG
41#include <lttng/trigger/trigger-internal.h>
42#include <lttng/endpoint.h>
43#include <lttng/channel-internal.h>
de453daa 44#include <lttng/event-internal.h>
15e37663 45#include <lttng/userspace-probe-internal.h>
b178f53e
JG
46#include <lttng/session-internal.h>
47#include <lttng/session-descriptor-internal.h>
fac6795d 48
d00c599e
DG
49#include "filter/filter-ast.h"
50#include "filter/filter-parser.h"
51#include "filter/filter-bytecode.h"
52#include "filter/memstream.h"
cac3069d 53#include "lttng-ctl-helper.h"
53a80697
MD
54
55#ifdef DEBUG
d00c599e 56static const int print_xml = 1;
53a80697
MD
57#define dbg_printf(fmt, args...) \
58 printf("[debug liblttng-ctl] " fmt, ## args)
59#else
d00c599e 60static const int print_xml = 0;
53a80697
MD
61#define dbg_printf(fmt, args...) \
62do { \
63 /* do nothing but check printf format */ \
64 if (0) \
65 printf("[debug liblttnctl] " fmt, ## args); \
66} while (0)
67#endif
68
69
fac6795d
DG
70/* Socket to session daemon for communication */
71static int sessiond_socket;
72static char sessiond_sock_path[PATH_MAX];
73
fac6795d
DG
74/* Variables */
75static char *tracing_group;
76static int connected;
77
97e19046
DG
78/* Global */
79
80/*
81 * Those two variables are used by error.h to silent or control the verbosity of
82 * error message. They are global to the library so application linking with it
83 * are able to compile correctly and also control verbosity of the library.
97e19046
DG
84 */
85int lttng_opt_quiet;
86int lttng_opt_verbose;
c7e35b03 87int lttng_opt_mi;
97e19046 88
99497cd0
MD
89/*
90 * Copy string from src to dst and enforce null terminated byte.
91 */
cac3069d
DG
92LTTNG_HIDDEN
93void lttng_ctl_copy_string(char *dst, const char *src, size_t len)
99497cd0 94{
e7d6716d 95 if (src && dst) {
99497cd0
MD
96 strncpy(dst, src, len);
97 /* Enforce the NULL terminated byte */
98 dst[len - 1] = '\0';
cd80958d
DG
99 } else if (dst) {
100 dst[0] = '\0';
99497cd0
MD
101 }
102}
103
fac6795d 104/*
cd80958d 105 * Copy domain to lttcomm_session_msg domain.
fac6795d 106 *
cd80958d
DG
107 * If domain is unknown, default domain will be the kernel.
108 */
cac3069d
DG
109LTTNG_HIDDEN
110void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
111 struct lttng_domain *src)
cd80958d
DG
112{
113 if (src && dst) {
114 switch (src->type) {
00e2e675
DG
115 case LTTNG_DOMAIN_KERNEL:
116 case LTTNG_DOMAIN_UST:
b9dfb167 117 case LTTNG_DOMAIN_JUL:
5cdb6027 118 case LTTNG_DOMAIN_LOG4J:
0e115563 119 case LTTNG_DOMAIN_PYTHON:
00e2e675
DG
120 memcpy(dst, src, sizeof(struct lttng_domain));
121 break;
122 default:
123 memset(dst, 0, sizeof(struct lttng_domain));
00e2e675 124 break;
cd80958d
DG
125 }
126 }
127}
128
129/*
130 * Send lttcomm_session_msg to the session daemon.
fac6795d 131 *
1c8d13c8
TD
132 * On success, returns the number of bytes sent (>=0)
133 * On error, returns -1
fac6795d 134 */
cd80958d 135static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
136{
137 int ret;
138
139 if (!connected) {
2f70b271 140 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 141 goto end;
fac6795d
DG
142 }
143
a4b92340
DG
144 DBG("LSM cmd type : %d", lsm->cmd_type);
145
be040666 146 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 147 sizeof(struct lttcomm_session_msg));
2f70b271
DG
148 if (ret < 0) {
149 ret = -LTTNG_ERR_FATAL;
150 }
e065084a
DG
151
152end:
153 return ret;
154}
155
53a80697
MD
156/*
157 * Send var len data to the session daemon.
158 *
159 * On success, returns the number of bytes sent (>=0)
160 * On error, returns -1
161 */
c2d69327 162static int send_session_varlen(const void *data, size_t len)
53a80697
MD
163{
164 int ret;
165
166 if (!connected) {
2f70b271 167 ret = -LTTNG_ERR_NO_SESSIOND;
53a80697
MD
168 goto end;
169 }
a4b92340 170
53a80697
MD
171 if (!data || !len) {
172 ret = 0;
173 goto end;
174 }
175
176 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
2f70b271
DG
177 if (ret < 0) {
178 ret = -LTTNG_ERR_FATAL;
179 }
53a80697
MD
180
181end:
182 return ret;
183}
184
a04d53fc
FD
185/*
186 * Send file descriptors to the session daemon.
187 *
188 * On success, returns the number of bytes sent (>=0)
189 * On error, returns -1
190 */
191static int send_session_fds(const int *fds, size_t nb_fd)
192{
193 int ret;
194
195 if (!connected) {
196 ret = -LTTNG_ERR_NO_SESSIOND;
197 goto end;
198 }
199
200 if (!fds || !nb_fd) {
201 ret = 0;
202 goto end;
203 }
204
205 ret = lttcomm_send_fds_unix_sock(sessiond_socket, fds, nb_fd);
206 if (ret < 0) {
207 ret = -LTTNG_ERR_FATAL;
208 }
209
210end:
211 return ret;
212}
213
e065084a 214/*
cd80958d 215 * Receive data from the sessiond socket.
e065084a 216 *
1c8d13c8
TD
217 * On success, returns the number of bytes received (>=0)
218 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 219 */
ca95a216 220static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
221{
222 int ret;
223
224 if (!connected) {
2f70b271 225 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 226 goto end;
fac6795d
DG
227 }
228
ca95a216 229 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
2f70b271
DG
230 if (ret < 0) {
231 ret = -LTTNG_ERR_FATAL;
232 }
fac6795d 233
e065084a 234end:
fac6795d
DG
235 return ret;
236}
237
238/*
9ae110e2 239 * Check if we are in the specified group.
65beb5ff 240 *
9ae110e2 241 * If yes return 1, else return -1.
947308c4 242 */
6c71277b
MD
243LTTNG_HIDDEN
244int lttng_check_tracing_group(void)
947308c4
DG
245{
246 struct group *grp_tracing; /* no free(). See getgrnam(3) */
247 gid_t *grp_list;
248 int grp_list_size, grp_id, i;
249 int ret = -1;
6c71277b 250 const char *grp_name = tracing_group;
947308c4
DG
251
252 /* Get GID of group 'tracing' */
253 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
254 if (!grp_tracing) {
255 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
256 goto end;
257 }
258
259 /* Get number of supplementary group IDs */
260 grp_list_size = getgroups(0, NULL);
261 if (grp_list_size < 0) {
6f04ed72 262 PERROR("getgroups");
947308c4
DG
263 goto end;
264 }
265
266 /* Alloc group list of the right size */
3f451dc0 267 grp_list = zmalloc(grp_list_size * sizeof(gid_t));
00795392 268 if (!grp_list) {
6f04ed72 269 PERROR("malloc");
00795392
MD
270 goto end;
271 }
947308c4 272 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 273 if (grp_id < 0) {
6f04ed72 274 PERROR("getgroups");
947308c4
DG
275 goto free_list;
276 }
277
278 for (i = 0; i < grp_list_size; i++) {
279 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 280 ret = 1;
947308c4
DG
281 break;
282 }
283 }
284
285free_list:
286 free(grp_list);
287
288end:
289 return ret;
290}
291
09b72f7a
FD
292static int check_enough_available_memory(size_t num_bytes_requested_per_cpu)
293{
294 int ret;
295 long num_cpu;
296 size_t best_mem_info;
297 size_t num_bytes_requested_total;
298
299 /*
300 * Get the number of CPU currently online to compute the amount of
301 * memory needed to create a buffer for every CPU.
302 */
303 num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
304 if (num_cpu == -1) {
305 goto error;
306 }
307
308 num_bytes_requested_total = num_bytes_requested_per_cpu * num_cpu;
309
310 /*
311 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
312 * reliable estimate we can get but it is only exposed by the kernel
313 * since 3.14. (See Linux kernel commit:
314 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
315 */
316 ret = utils_get_memory_available(&best_mem_info);
317 if (ret >= 0) {
318 goto success;
319 }
320
321 /*
322 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
323 * is a sanity check for obvious user error.
324 */
325 ret = utils_get_memory_total(&best_mem_info);
326 if (ret >= 0) {
327 goto success;
328 }
329
330error:
331 return -1;
332success:
333 return best_mem_info >= num_bytes_requested_total;
334}
335
947308c4 336/*
2269e89e
DG
337 * Try connect to session daemon with sock_path.
338 *
339 * Return 0 on success, else -1
340 */
341static int try_connect_sessiond(const char *sock_path)
342{
343 int ret;
344
345 /* If socket exist, we check if the daemon listens for connect. */
346 ret = access(sock_path, F_OK);
347 if (ret < 0) {
348 /* Not alive */
2f70b271 349 goto error;
2269e89e
DG
350 }
351
352 ret = lttcomm_connect_unix_sock(sock_path);
353 if (ret < 0) {
9ae110e2 354 /* Not alive. */
2f70b271 355 goto error;
2269e89e
DG
356 }
357
358 ret = lttcomm_close_unix_sock(ret);
359 if (ret < 0) {
6f04ed72 360 PERROR("lttcomm_close_unix_sock");
2269e89e
DG
361 }
362
363 return 0;
2f70b271
DG
364
365error:
366 return -1;
2269e89e
DG
367}
368
369/*
2f70b271
DG
370 * Set sessiond socket path by putting it in the global sessiond_sock_path
371 * variable.
372 *
373 * Returns 0 on success, negative value on failure (the sessiond socket path
374 * is somehow too long or ENOMEM).
947308c4
DG
375 */
376static int set_session_daemon_path(void)
377{
9ae110e2 378 int in_tgroup = 0; /* In tracing group. */
2269e89e
DG
379 uid_t uid;
380
381 uid = getuid();
947308c4 382
2269e89e
DG
383 if (uid != 0) {
384 /* Are we in the tracing group ? */
6c71277b 385 in_tgroup = lttng_check_tracing_group();
2269e89e
DG
386 }
387
08a9c49f 388 if ((uid == 0) || in_tgroup) {
cac3069d
DG
389 lttng_ctl_copy_string(sessiond_sock_path,
390 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path));
08a9c49f 391 }
2269e89e 392
08a9c49f 393 if (uid != 0) {
c617c0c6
MD
394 int ret;
395
08a9c49f 396 if (in_tgroup) {
9ae110e2 397 /* Tracing group. */
08a9c49f
TD
398 ret = try_connect_sessiond(sessiond_sock_path);
399 if (ret >= 0) {
400 goto end;
2269e89e 401 }
08a9c49f 402 /* Global session daemon not available... */
2269e89e 403 }
08a9c49f
TD
404 /* ...or not in tracing group (and not root), default */
405
406 /*
9ae110e2
JG
407 * With GNU C < 2.1, snprintf returns -1 if the target buffer
408 * is too small;
409 * With GNU C >= 2.1, snprintf returns the required size
410 * (excluding closing null)
08a9c49f
TD
411 */
412 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
feb0f3e5 413 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
08a9c49f 414 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
2f70b271 415 goto error;
947308c4 416 }
947308c4 417 }
08a9c49f 418end:
947308c4 419 return 0;
2f70b271
DG
420
421error:
422 return -1;
947308c4
DG
423}
424
65beb5ff 425/*
9ae110e2 426 * Connect to the LTTng session daemon.
65beb5ff 427 *
9ae110e2 428 * On success, return 0. On error, return -1.
65beb5ff
DG
429 */
430static int connect_sessiond(void)
431{
432 int ret;
433
da3c9ec1
DG
434 /* Don't try to connect if already connected. */
435 if (connected) {
436 return 0;
437 }
438
65beb5ff
DG
439 ret = set_session_daemon_path();
440 if (ret < 0) {
2f70b271 441 goto error;
65beb5ff
DG
442 }
443
9ae110e2 444 /* Connect to the sesssion daemon. */
65beb5ff
DG
445 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
446 if (ret < 0) {
2f70b271 447 goto error;
65beb5ff
DG
448 }
449
450 sessiond_socket = ret;
451 connected = 1;
452
453 return 0;
2f70b271
DG
454
455error:
456 return -1;
65beb5ff
DG
457}
458
459/*
1c8d13c8 460 * Clean disconnect from the session daemon.
9ae110e2 461 *
1c8d13c8 462 * On success, return 0. On error, return -1.
65beb5ff
DG
463 */
464static int disconnect_sessiond(void)
465{
466 int ret = 0;
467
468 if (connected) {
469 ret = lttcomm_close_unix_sock(sessiond_socket);
470 sessiond_socket = 0;
471 connected = 0;
472 }
473
474 return ret;
475}
476
795a978d
PP
477static int recv_sessiond_optional_data(size_t len, void **user_buf,
478 size_t *user_len)
479{
480 int ret = 0;
481 void *buf = NULL;
482
483 if (len) {
484 if (!user_len) {
485 ret = -LTTNG_ERR_INVALID;
486 goto end;
487 }
488
489 buf = zmalloc(len);
490 if (!buf) {
491 ret = -ENOMEM;
492 goto end;
493 }
494
495 ret = recv_data_sessiond(buf, len);
496 if (ret < 0) {
497 goto end;
498 }
499
500 if (!user_buf) {
501 ret = -LTTNG_ERR_INVALID;
502 goto end;
503 }
504
505 /* Move ownership of command header buffer to user. */
506 *user_buf = buf;
507 buf = NULL;
508 *user_len = len;
509 } else {
510 /* No command header. */
511 if (user_len) {
512 *user_len = 0;
513 }
514
515 if (user_buf) {
516 *user_buf = NULL;
517 }
518 }
519
520end:
521 free(buf);
522 return ret;
523}
524
35a6fdb7 525/*
cd80958d 526 * Ask the session daemon a specific command and put the data into buf.
a04d53fc
FD
527 * Takes extra var. len. data and file descriptors as input to send to the
528 * session daemon.
65beb5ff 529 *
af87c45a 530 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 531 */
cac3069d 532LTTNG_HIDDEN
a04d53fc
FD
533int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm,
534 const int *fds, size_t nb_fd, const void *vardata,
535 size_t vardata_len, void **user_payload_buf,
536 void **user_cmd_header_buf, size_t *user_cmd_header_len)
65beb5ff
DG
537{
538 int ret;
795a978d 539 size_t payload_len;
cd80958d 540 struct lttcomm_lttng_msg llm;
65beb5ff
DG
541
542 ret = connect_sessiond();
543 if (ret < 0) {
2f70b271 544 ret = -LTTNG_ERR_NO_SESSIOND;
65beb5ff
DG
545 goto end;
546 }
547
65beb5ff 548 /* Send command to session daemon */
cd80958d 549 ret = send_session_msg(lsm);
65beb5ff 550 if (ret < 0) {
2f70b271 551 /* Ret value is a valid lttng error code. */
65beb5ff
DG
552 goto end;
553 }
53a80697 554 /* Send var len data */
795a978d 555 ret = send_session_varlen(vardata, vardata_len);
53a80697 556 if (ret < 0) {
2f70b271 557 /* Ret value is a valid lttng error code. */
53a80697
MD
558 goto end;
559 }
65beb5ff 560
a04d53fc
FD
561 /* Send fds */
562 ret = send_session_fds(fds, nb_fd);
563 if (ret < 0) {
564 /* Ret value is a valid lttng error code. */
565 goto end;
566 }
567
65beb5ff
DG
568 /* Get header from data transmission */
569 ret = recv_data_sessiond(&llm, sizeof(llm));
570 if (ret < 0) {
2f70b271 571 /* Ret value is a valid lttng error code. */
65beb5ff
DG
572 goto end;
573 }
574
575 /* Check error code if OK */
f73fabfd 576 if (llm.ret_code != LTTNG_OK) {
65beb5ff
DG
577 ret = -llm.ret_code;
578 goto end;
579 }
580
795a978d
PP
581 /* Get command header from data transmission */
582 ret = recv_sessiond_optional_data(llm.cmd_header_size,
583 user_cmd_header_buf, user_cmd_header_len);
65beb5ff 584 if (ret < 0) {
65beb5ff
DG
585 goto end;
586 }
587
795a978d
PP
588 /* Get payload from data transmission */
589 ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf,
590 &payload_len);
591 if (ret < 0) {
83009e5e
DG
592 goto end;
593 }
594
795a978d 595 ret = llm.data_size;
65beb5ff
DG
596
597end:
598 disconnect_sessiond();
599 return ret;
600}
601
9f19cc17 602/*
cd80958d 603 * Create lttng handle and return pointer.
9ae110e2 604 *
1c8d13c8 605 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 606 */
cd80958d
DG
607struct lttng_handle *lttng_create_handle(const char *session_name,
608 struct lttng_domain *domain)
9f19cc17 609{
2f70b271
DG
610 struct lttng_handle *handle = NULL;
611
3f451dc0 612 handle = zmalloc(sizeof(struct lttng_handle));
cd80958d 613 if (handle == NULL) {
2f70b271 614 PERROR("malloc handle");
cd80958d
DG
615 goto end;
616 }
617
618 /* Copy session name */
cac3069d 619 lttng_ctl_copy_string(handle->session_name, session_name,
cd80958d
DG
620 sizeof(handle->session_name));
621
95681498
JG
622 /* Copy lttng domain or leave initialized to 0. */
623 if (domain) {
624 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
625 }
cd80958d
DG
626
627end:
628 return handle;
629}
630
631/*
632 * Destroy handle by free(3) the pointer.
633 */
634void lttng_destroy_handle(struct lttng_handle *handle)
635{
0e428499 636 free(handle);
eb354453
DG
637}
638
d9800920
DG
639/*
640 * Register an outside consumer.
9ae110e2 641 *
1c8d13c8 642 * Returns size of returned session payload data or a negative error code.
d9800920
DG
643 */
644int lttng_register_consumer(struct lttng_handle *handle,
645 const char *socket_path)
646{
647 struct lttcomm_session_msg lsm;
648
2f70b271
DG
649 if (handle == NULL || socket_path == NULL) {
650 return -LTTNG_ERR_INVALID;
651 }
652
53efb85a 653 memset(&lsm, 0, sizeof(lsm));
d9800920 654 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
cac3069d 655 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
d9800920 656 sizeof(lsm.session.name));
cac3069d 657 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d9800920 658
9ae110e2
JG
659 lttng_ctl_copy_string(lsm.u.reg.path, socket_path,
660 sizeof(lsm.u.reg.path));
d9800920 661
cac3069d 662 return lttng_ctl_ask_sessiond(&lsm, NULL);
d9800920
DG
663}
664
1df4dedd 665/*
9ae110e2
JG
666 * Start tracing for all traces of the session.
667 *
668 * Returns size of returned session payload data or a negative error code.
1df4dedd 669 */
6a4f824d 670int lttng_start_tracing(const char *session_name)
f3ed775e 671{
cd80958d
DG
672 struct lttcomm_session_msg lsm;
673
6a4f824d 674 if (session_name == NULL) {
2f70b271 675 return -LTTNG_ERR_INVALID;
cd80958d
DG
676 }
677
53efb85a 678 memset(&lsm, 0, sizeof(lsm));
cd80958d 679 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d 680
cac3069d
DG
681 lttng_ctl_copy_string(lsm.session.name, session_name,
682 sizeof(lsm.session.name));
cd80958d 683
cac3069d 684 return lttng_ctl_ask_sessiond(&lsm, NULL);
f3ed775e 685}
1df4dedd
DG
686
687/*
38ee087f 688 * Stop tracing for all traces of the session.
f3ed775e 689 */
38ee087f 690static int _lttng_stop_tracing(const char *session_name, int wait)
f3ed775e 691{
38ee087f 692 int ret, data_ret;
cd80958d
DG
693 struct lttcomm_session_msg lsm;
694
6a4f824d 695 if (session_name == NULL) {
2f70b271 696 return -LTTNG_ERR_INVALID;
6a4f824d
DG
697 }
698
53efb85a 699 memset(&lsm, 0, sizeof(lsm));
cd80958d 700 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d 701
cac3069d
DG
702 lttng_ctl_copy_string(lsm.session.name, session_name,
703 sizeof(lsm.session.name));
cd80958d 704
cac3069d 705 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
38ee087f
DG
706 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
707 goto error;
708 }
709
710 if (!wait) {
711 goto end;
712 }
713
38ee087f
DG
714 /* Check for data availability */
715 do {
6d805429 716 data_ret = lttng_data_pending(session_name);
38ee087f
DG
717 if (data_ret < 0) {
718 /* Return the data available call error. */
719 ret = data_ret;
720 goto error;
721 }
722
723 /*
9ae110e2
JG
724 * Data sleep time before retrying (in usec). Don't sleep if the
725 * call returned value indicates availability.
38ee087f 726 */
6d805429 727 if (data_ret) {
38ee087f 728 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
38ee087f 729 }
6d805429 730 } while (data_ret != 0);
38ee087f 731
38ee087f
DG
732end:
733error:
734 return ret;
735}
736
737/*
738 * Stop tracing and wait for data availability.
739 */
740int lttng_stop_tracing(const char *session_name)
741{
742 return _lttng_stop_tracing(session_name, 1);
743}
744
745/*
746 * Stop tracing but _don't_ wait for data availability.
747 */
748int lttng_stop_tracing_no_wait(const char *session_name)
749{
750 return _lttng_stop_tracing(session_name, 0);
f3ed775e
DG
751}
752
753/*
601d5acf
DG
754 * Add context to a channel.
755 *
756 * If the given channel is NULL, add the contexts to all channels.
757 * The event_name param is ignored.
af87c45a
DG
758 *
759 * Returns the size of the returned payload data or a negative error code.
1df4dedd 760 */
cd80958d 761int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
762 struct lttng_event_context *ctx, const char *event_name,
763 const char *channel_name)
d65106b1 764{
2001793c
JG
765 int ret;
766 size_t len = 0;
767 char *buf = NULL;
cd80958d
DG
768 struct lttcomm_session_msg lsm;
769
9ae110e2 770 /* Safety check. Both are mandatory. */
9d697d3d 771 if (handle == NULL || ctx == NULL) {
2001793c
JG
772 ret = -LTTNG_ERR_INVALID;
773 goto end;
cd80958d
DG
774 }
775
441c16a7 776 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
777 lsm.cmd_type = LTTNG_ADD_CONTEXT;
778
85076754
MD
779 /* If no channel name, send empty string. */
780 if (channel_name == NULL) {
781 lttng_ctl_copy_string(lsm.u.context.channel_name, "",
782 sizeof(lsm.u.context.channel_name));
783 } else {
784 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
785 sizeof(lsm.u.context.channel_name));
786 }
cd80958d 787
cac3069d 788 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cac3069d 789 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
790 sizeof(lsm.session.name));
791
2001793c
JG
792 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
793 size_t provider_len, ctx_len;
794 const char *provider_name = ctx->u.app_ctx.provider_name;
795 const char *ctx_name = ctx->u.app_ctx.ctx_name;
796
797 if (!provider_name || !ctx_name) {
798 ret = -LTTNG_ERR_INVALID;
799 goto end;
800 }
801
802 provider_len = strlen(provider_name);
803 if (provider_len == 0) {
804 ret = -LTTNG_ERR_INVALID;
805 goto end;
806 }
807 lsm.u.context.provider_name_len = provider_len;
808
809 ctx_len = strlen(ctx_name);
810 if (ctx_len == 0) {
811 ret = -LTTNG_ERR_INVALID;
812 goto end;
813 }
814 lsm.u.context.context_name_len = ctx_len;
815
816 len = provider_len + ctx_len;
817 buf = zmalloc(len);
818 if (!buf) {
819 ret = -LTTNG_ERR_NOMEM;
820 goto end;
821 }
822
823 memcpy(buf, provider_name, provider_len);
824 memcpy(buf + provider_len, ctx_name, ctx_len);
825 }
826 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
46f44e2a
JR
827
828 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
829 /*
830 * Don't leak application addresses to the sessiond.
831 * This is only necessary when ctx is for an app ctx otherwise
832 * the values inside the union (type & config) are overwritten.
833 */
834 lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
835 lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
836 }
2001793c 837
795a978d 838 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL);
2001793c
JG
839end:
840 free(buf);
841 return ret;
d65106b1
DG
842}
843
f3ed775e 844/*
9ae110e2
JG
845 * Enable event(s) for a channel.
846 *
847 * If no event name is specified, all events are enabled.
848 * If no channel name is specified, the default 'channel0' is used.
849 *
850 * Returns size of returned session payload data or a negative error code.
f3ed775e 851 */
cd80958d 852int lttng_enable_event(struct lttng_handle *handle,
38057ed1 853 struct lttng_event *ev, const char *channel_name)
1df4dedd 854{
f8a96544
JI
855 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
856 NULL, 0, NULL);
1df4dedd
DG
857}
858
53a80697 859/*
025faf73 860 * Create or enable an event with a filter expression.
178191b3 861 *
53a80697
MD
862 * Return negative error value on error.
863 * Return size of returned session payload data if OK.
864 */
025faf73 865int lttng_enable_event_with_filter(struct lttng_handle *handle,
178191b3 866 struct lttng_event *event, const char *channel_name,
53a80697
MD
867 const char *filter_expression)
868{
f8a96544
JI
869 return lttng_enable_event_with_exclusions(handle, event, channel_name,
870 filter_expression, 0, NULL);
53a80697
MD
871}
872
347c5ab5 873/*
0e115563 874 * Depending on the event, return a newly allocated agent filter expression or
347c5ab5
DG
875 * NULL if not applicable.
876 *
877 * An event with NO loglevel and the name is * will return NULL.
878 */
0e115563 879static char *set_agent_filter(const char *filter, struct lttng_event *ev)
347c5ab5
DG
880{
881 int err;
0e115563 882 char *agent_filter = NULL;
347c5ab5
DG
883
884 assert(ev);
885
886 /* Don't add filter for the '*' event. */
9f449915 887 if (strcmp(ev->name, "*") != 0) {
347c5ab5 888 if (filter) {
0e115563 889 err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
347c5ab5
DG
890 ev->name);
891 } else {
0e115563 892 err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
347c5ab5
DG
893 }
894 if (err < 0) {
895 PERROR("asprintf");
6a556f7b 896 goto error;
347c5ab5
DG
897 }
898 }
899
900 /* Add loglevel filtering if any for the JUL domain. */
901 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
902 char *op;
903
904 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
905 op = ">=";
906 } else {
907 op = "==";
908 }
909
0e115563 910 if (filter || agent_filter) {
6a556f7b
JG
911 char *new_filter;
912
fb0edb23 913 err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
0e115563 914 agent_filter ? agent_filter : filter, op,
347c5ab5 915 ev->loglevel);
0e115563
DG
916 if (agent_filter) {
917 free(agent_filter);
6a556f7b 918 }
0e115563 919 agent_filter = new_filter;
347c5ab5 920 } else {
0e115563 921 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
347c5ab5
DG
922 ev->loglevel);
923 }
924 if (err < 0) {
925 PERROR("asprintf");
6a556f7b 926 goto error;
347c5ab5
DG
927 }
928 }
929
0e115563 930 return agent_filter;
6a556f7b 931error:
0e115563 932 free(agent_filter);
6a556f7b 933 return NULL;
347c5ab5
DG
934}
935
137b9942 936/*
ec166985 937 * Generate the filter bytecode from a given filter expression string. Put the
137b9942
DG
938 * newly allocated parser context in ctxp and populate the lsm object with the
939 * expression len.
940 *
941 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
942 */
943static int generate_filter(char *filter_expression,
944 struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp)
945{
946 int ret;
947 struct filter_parser_ctx *ctx = NULL;
948 FILE *fmem = NULL;
949
950 assert(filter_expression);
951 assert(lsm);
952 assert(ctxp);
953
954 /*
955 * Casting const to non-const, as the underlying function will use it in
956 * read-only mode.
957 */
958 fmem = lttng_fmemopen((void *) filter_expression,
959 strlen(filter_expression), "r");
960 if (!fmem) {
961 fprintf(stderr, "Error opening memory as stream\n");
962 ret = -LTTNG_ERR_FILTER_NOMEM;
963 goto error;
964 }
965 ctx = filter_parser_ctx_alloc(fmem);
966 if (!ctx) {
967 fprintf(stderr, "Error allocating parser\n");
968 ret = -LTTNG_ERR_FILTER_NOMEM;
969 goto filter_alloc_error;
970 }
971 ret = filter_parser_ctx_append_ast(ctx);
972 if (ret) {
973 fprintf(stderr, "Parse error\n");
974 ret = -LTTNG_ERR_FILTER_INVAL;
975 goto parse_error;
976 }
137b9942
DG
977 if (print_xml) {
978 ret = filter_visitor_print_xml(ctx, stdout, 0);
979 if (ret) {
980 fflush(stdout);
981 fprintf(stderr, "XML print error\n");
982 ret = -LTTNG_ERR_FILTER_INVAL;
983 goto parse_error;
984 }
985 }
986
987 dbg_printf("Generating IR... ");
988 fflush(stdout);
989 ret = filter_visitor_ir_generate(ctx);
990 if (ret) {
991 fprintf(stderr, "Generate IR error\n");
992 ret = -LTTNG_ERR_FILTER_INVAL;
993 goto parse_error;
994 }
995 dbg_printf("done\n");
996
997 dbg_printf("Validating IR... ");
998 fflush(stdout);
999 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
1000 if (ret) {
1001 ret = -LTTNG_ERR_FILTER_INVAL;
1002 goto parse_error;
1003 }
9f449915
PP
1004
1005 /* Normalize globbing patterns in the expression. */
1006 ret = filter_visitor_ir_normalize_glob_patterns(ctx);
1007 if (ret) {
1008 ret = -LTTNG_ERR_FILTER_INVAL;
1009 goto parse_error;
1010 }
1011
9ae110e2 1012 /* Validate strings used as literals in the expression. */
dcd5daf2
JG
1013 ret = filter_visitor_ir_validate_string(ctx);
1014 if (ret) {
1015 ret = -LTTNG_ERR_FILTER_INVAL;
1016 goto parse_error;
1017 }
9f449915
PP
1018
1019 /* Validate globbing patterns in the expression. */
1020 ret = filter_visitor_ir_validate_globbing(ctx);
1021 if (ret) {
1022 ret = -LTTNG_ERR_FILTER_INVAL;
1023 goto parse_error;
1024 }
1025
137b9942
DG
1026 dbg_printf("done\n");
1027
1028 dbg_printf("Generating bytecode... ");
1029 fflush(stdout);
1030 ret = filter_visitor_bytecode_generate(ctx);
1031 if (ret) {
1032 fprintf(stderr, "Generate bytecode error\n");
1033 ret = -LTTNG_ERR_FILTER_INVAL;
1034 goto parse_error;
1035 }
1036 dbg_printf("done\n");
1037 dbg_printf("Size of bytecode generated: %u bytes.\n",
1038 bytecode_get_len(&ctx->bytecode->b));
1039
1040 lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b)
1041 + bytecode_get_len(&ctx->bytecode->b);
1042 lsm->u.enable.expression_len = strlen(filter_expression) + 1;
1043
1044 /* No need to keep the memory stream. */
1045 if (fclose(fmem) != 0) {
6f04ed72 1046 PERROR("fclose");
137b9942
DG
1047 }
1048
1049 *ctxp = ctx;
1050 return 0;
1051
1052parse_error:
137b9942
DG
1053 filter_ir_free(ctx);
1054 filter_parser_ctx_free(ctx);
1055filter_alloc_error:
1056 if (fclose(fmem) != 0) {
6f04ed72 1057 PERROR("fclose");
137b9942
DG
1058 }
1059error:
1060 return ret;
1061}
1062
93deb080
JI
1063/*
1064 * Enable event(s) for a channel, possibly with exclusions and a filter.
1065 * If no event name is specified, all events are enabled.
1066 * If no channel name is specified, the default name is used.
1067 * If filter expression is not NULL, the filter is set for the event.
1068 * If exclusion count is not zero, the exclusions are set for the event.
1069 * Returns size of returned session payload data or a negative error code.
1070 */
1071int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
1072 struct lttng_event *ev, const char *channel_name,
e9efbcd3 1073 const char *original_filter_expression,
93deb080
JI
1074 int exclusion_count, char **exclusion_list)
1075{
1076 struct lttcomm_session_msg lsm;
15e37663
JG
1077 struct lttng_dynamic_buffer send_buffer;
1078 int ret = 0, i, fd_to_send = -1;
1079 bool send_fd = false;
137b9942 1080 unsigned int free_filter_expression = 0;
93deb080 1081 struct filter_parser_ctx *ctx = NULL;
ec005ec6
FD
1082
1083 memset(&send_buffer, 0, sizeof(send_buffer));
1084
e9efbcd3
JG
1085 /*
1086 * Cast as non-const since we may replace the filter expression
1087 * by a dynamically allocated string. Otherwise, the original
1088 * string is not modified.
1089 */
1090 char *filter_expression = (char *) original_filter_expression;
93deb080
JI
1091
1092 if (handle == NULL || ev == NULL) {
137b9942
DG
1093 ret = -LTTNG_ERR_INVALID;
1094 goto error;
93deb080
JI
1095 }
1096
9ae110e2
JG
1097 /*
1098 * Empty filter string will always be rejected by the parser
93deb080
JI
1099 * anyway, so treat this corner-case early to eliminate
1100 * lttng_fmemopen error for 0-byte allocation.
1101 */
1102 if (filter_expression && filter_expression[0] == '\0') {
137b9942
DG
1103 ret = -LTTNG_ERR_INVALID;
1104 goto error;
93deb080
JI
1105 }
1106
1107 memset(&lsm, 0, sizeof(lsm));
1108
1109 /* If no channel name, send empty string. */
1110 if (channel_name == NULL) {
1111 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
1112 sizeof(lsm.u.enable.channel_name));
1113 } else {
1114 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
1115 sizeof(lsm.u.enable.channel_name));
1116 }
1117
18a720cd
MD
1118 lsm.cmd_type = LTTNG_ENABLE_EVENT;
1119 if (ev->name[0] == '\0') {
1120 /* Enable all events */
1121 lttng_ctl_copy_string(ev->name, "*", sizeof(ev->name));
6565421f 1122 }
93deb080 1123
6565421f 1124 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
6e911cad 1125 /* FIXME: copying non-packed struct to packed struct. */
93deb080
JI
1126 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
1127
1128 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1129 sizeof(lsm.session.name));
1130 lsm.u.enable.exclusion_count = exclusion_count;
1131 lsm.u.enable.bytecode_len = 0;
1132
93deb080
JI
1133 /*
1134 * We have either a filter or some exclusions, so we need to set up
9ae110e2 1135 * a variable-length memory block from where to send the data.
93deb080 1136 */
15e37663 1137 lttng_dynamic_buffer_init(&send_buffer);
93deb080 1138
9ae110e2 1139 /* Parse filter expression. */
5cdb6027 1140 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1141 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1142 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
5cdb6027 1143 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1144 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1145 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1146 char *agent_filter;
64226865 1147
347c5ab5 1148 /* Setup JUL filter if needed. */
0e115563
DG
1149 agent_filter = set_agent_filter(filter_expression, ev);
1150 if (!agent_filter) {
137b9942 1151 if (!filter_expression) {
9ae110e2
JG
1152 /*
1153 * No JUL and no filter, just skip
1154 * everything below.
1155 */
137b9942
DG
1156 goto ask_sessiond;
1157 }
64226865
DG
1158 } else {
1159 /*
9ae110e2
JG
1160 * With an agent filter, the original filter has
1161 * been added to it thus replace the filter
1162 * expression.
64226865 1163 */
0e115563 1164 filter_expression = agent_filter;
e9efbcd3 1165 free_filter_expression = 1;
9b21e6d5 1166 }
9b21e6d5 1167 }
93deb080 1168
137b9942 1169 ret = generate_filter(filter_expression, &lsm, &ctx);
93deb080 1170 if (ret) {
137b9942 1171 goto filter_error;
93deb080 1172 }
6b453b5e
JG
1173 }
1174
15e37663
JG
1175 ret = lttng_dynamic_buffer_set_capacity(&send_buffer,
1176 lsm.u.enable.bytecode_len
137b9942
DG
1177 + lsm.u.enable.expression_len
1178 + LTTNG_SYMBOL_NAME_LEN * exclusion_count);
15e37663 1179 if (ret) {
6b453b5e 1180 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
7ca1dc6f 1181 goto mem_error;
6b453b5e 1182 }
137b9942 1183
9ae110e2 1184 /* Put exclusion names first in the data. */
15e37663
JG
1185 for (i = 0; i < exclusion_count; i++) {
1186 size_t exclusion_len;
1187
1188 exclusion_len = lttng_strnlen(*(exclusion_list + i),
1189 LTTNG_SYMBOL_NAME_LEN);
1190 if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) {
1191 /* Exclusion is not NULL-terminated. */
1192 ret = -LTTNG_ERR_INVALID;
1193 goto mem_error;
1194 }
1195
1196 ret = lttng_dynamic_buffer_append(&send_buffer,
1197 *(exclusion_list + i),
1198 LTTNG_SYMBOL_NAME_LEN);
1199 if (ret) {
1200 goto mem_error;
1201 }
6b453b5e 1202 }
15e37663 1203
9ae110e2 1204 /* Add filter expression next. */
15e37663
JG
1205 if (filter_expression) {
1206 ret = lttng_dynamic_buffer_append(&send_buffer,
1207 filter_expression, lsm.u.enable.expression_len);
1208 if (ret) {
1209 goto mem_error;
1210 }
6b453b5e 1211 }
9ae110e2 1212 /* Add filter bytecode next. */
137b9942 1213 if (ctx && lsm.u.enable.bytecode_len != 0) {
15e37663
JG
1214 ret = lttng_dynamic_buffer_append(&send_buffer,
1215 &ctx->bytecode->b, lsm.u.enable.bytecode_len);
1216 if (ret) {
1217 goto mem_error;
1218 }
1219 }
1220 if (ev->extended.ptr) {
1221 struct lttng_event_extended *ev_ext =
1222 (struct lttng_event_extended *) ev->extended.ptr;
1223
1224 if (ev_ext->probe_location) {
1225 /*
1226 * lttng_userspace_probe_location_serialize returns the
1227 * number of bytes that was appended to the buffer.
1228 */
1229 ret = lttng_userspace_probe_location_serialize(
1230 ev_ext->probe_location, &send_buffer,
1231 &fd_to_send);
56f0bc67 1232 if (ret < 0) {
15e37663
JG
1233 goto mem_error;
1234 }
1235
1236 send_fd = fd_to_send >= 0;
1237 /*
1238 * Set the size of the userspace probe location element
1239 * of the buffer so that the receiving side knows where
1240 * to split it.
1241 */
1242 lsm.u.enable.userspace_probe_location_len = ret;
1243 }
93deb080
JI
1244 }
1245
15e37663
JG
1246 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1247 send_fd ? &fd_to_send : NULL,
1248 send_fd ? 1 : 0,
1249 send_buffer.size ? send_buffer.data : NULL,
1250 send_buffer.size, NULL, NULL, 0);
93deb080 1251
7ca1dc6f
DG
1252mem_error:
1253 if (filter_expression && ctx) {
93deb080
JI
1254 filter_bytecode_free(ctx);
1255 filter_ir_free(ctx);
1256 filter_parser_ctx_free(ctx);
7ca1dc6f
DG
1257 }
1258filter_error:
1259 if (free_filter_expression) {
1260 /*
9ae110e2
JG
1261 * The filter expression has been replaced and must be freed as
1262 * it is not the original filter expression received as a
1263 * parameter.
7ca1dc6f
DG
1264 */
1265 free(filter_expression);
93deb080 1266 }
137b9942
DG
1267error:
1268 /*
9ae110e2
JG
1269 * Return directly to the caller and don't ask the sessiond since
1270 * something went wrong in the parsing of data above.
137b9942 1271 */
15e37663 1272 lttng_dynamic_buffer_reset(&send_buffer);
93deb080 1273 return ret;
3e1c9ff7
DG
1274
1275ask_sessiond:
1276 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1277 return ret;
93deb080
JI
1278}
1279
6e911cad
MD
1280int lttng_disable_event_ext(struct lttng_handle *handle,
1281 struct lttng_event *ev, const char *channel_name,
1282 const char *original_filter_expression)
1df4dedd 1283{
cd80958d 1284 struct lttcomm_session_msg lsm;
6e911cad
MD
1285 char *varlen_data;
1286 int ret = 0;
1287 unsigned int free_filter_expression = 0;
1288 struct filter_parser_ctx *ctx = NULL;
1289 /*
1290 * Cast as non-const since we may replace the filter expression
1291 * by a dynamically allocated string. Otherwise, the original
1292 * string is not modified.
1293 */
1294 char *filter_expression = (char *) original_filter_expression;
1df4dedd 1295
6e911cad
MD
1296 if (handle == NULL || ev == NULL) {
1297 ret = -LTTNG_ERR_INVALID;
1298 goto error;
1299 }
1300
9ae110e2
JG
1301 /*
1302 * Empty filter string will always be rejected by the parser
6e911cad
MD
1303 * anyway, so treat this corner-case early to eliminate
1304 * lttng_fmemopen error for 0-byte allocation.
1305 */
1306 if (filter_expression && filter_expression[0] == '\0') {
1307 ret = -LTTNG_ERR_INVALID;
1308 goto error;
cd80958d
DG
1309 }
1310
441c16a7
MD
1311 memset(&lsm, 0, sizeof(lsm));
1312
85076754
MD
1313 /* If no channel name, send empty string. */
1314 if (channel_name == NULL) {
1315 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
cd80958d 1316 sizeof(lsm.u.disable.channel_name));
f3ed775e 1317 } else {
85076754 1318 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
cd80958d 1319 sizeof(lsm.u.disable.channel_name));
eb354453
DG
1320 }
1321
18a720cd 1322 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f3ed775e 1323
6e911cad
MD
1324 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1325 /* FIXME: copying non-packed struct to packed struct. */
1326 memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event));
1327
cac3069d 1328 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1329 sizeof(lsm.session.name));
6e911cad 1330 lsm.u.disable.bytecode_len = 0;
cd80958d 1331
6e911cad
MD
1332 /*
1333 * For the JUL domain, a filter is enforced except for the
1334 * disable all event. This is done to avoid having the event in
1335 * all sessions thus filtering by logger name.
1336 */
1337 if (filter_expression == NULL &&
1338 (handle->domain.type != LTTNG_DOMAIN_JUL &&
0e115563
DG
1339 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1340 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
6e911cad
MD
1341 goto ask_sessiond;
1342 }
1343
1344 /*
1345 * We have a filter, so we need to set up a variable-length
1346 * memory block from where to send the data.
1347 */
1348
1349 /* Parse filter expression */
1350 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1351 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1352 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
6e911cad 1353 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1354 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1355 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1356 char *agent_filter;
6e911cad
MD
1357
1358 /* Setup JUL filter if needed. */
0e115563
DG
1359 agent_filter = set_agent_filter(filter_expression, ev);
1360 if (!agent_filter) {
6e911cad 1361 if (!filter_expression) {
9ae110e2
JG
1362 /*
1363 * No JUL and no filter, just skip
1364 * everything below.
1365 */
6e911cad
MD
1366 goto ask_sessiond;
1367 }
1368 } else {
1369 /*
9ae110e2
JG
1370 * With a JUL filter, the original filter has
1371 * been added to it thus replace the filter
1372 * expression.
6e911cad 1373 */
0e115563 1374 filter_expression = agent_filter;
6e911cad
MD
1375 free_filter_expression = 1;
1376 }
1377 }
1378
1379 ret = generate_filter(filter_expression, &lsm, &ctx);
1380 if (ret) {
1381 goto filter_error;
1382 }
1383 }
1384
1385 varlen_data = zmalloc(lsm.u.disable.bytecode_len
1386 + lsm.u.disable.expression_len);
1387 if (!varlen_data) {
1388 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1389 goto mem_error;
1390 }
1391
9ae110e2 1392 /* Add filter expression. */
6e911cad
MD
1393 if (lsm.u.disable.expression_len != 0) {
1394 memcpy(varlen_data,
1395 filter_expression,
1396 lsm.u.disable.expression_len);
1397 }
9ae110e2 1398 /* Add filter bytecode next. */
6e911cad
MD
1399 if (ctx && lsm.u.disable.bytecode_len != 0) {
1400 memcpy(varlen_data
1401 + lsm.u.disable.expression_len,
1402 &ctx->bytecode->b,
1403 lsm.u.disable.bytecode_len);
1404 }
1405
795a978d 1406 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
6e911cad
MD
1407 lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
1408 free(varlen_data);
1409
1410mem_error:
1411 if (filter_expression && ctx) {
1412 filter_bytecode_free(ctx);
1413 filter_ir_free(ctx);
1414 filter_parser_ctx_free(ctx);
1415 }
1416filter_error:
1417 if (free_filter_expression) {
1418 /*
9ae110e2
JG
1419 * The filter expression has been replaced and must be freed as
1420 * it is not the original filter expression received as a
1421 * parameter.
6e911cad
MD
1422 */
1423 free(filter_expression);
1424 }
1425error:
1426 /*
9ae110e2
JG
1427 * Return directly to the caller and don't ask the sessiond since
1428 * something went wrong in the parsing of data above.
6e911cad
MD
1429 */
1430 return ret;
1431
1432ask_sessiond:
1433 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1434 return ret;
1435}
1436
1437/*
9ae110e2
JG
1438 * Disable event(s) of a channel and domain.
1439 * If no event name is specified, all events are disabled.
1440 * If no channel name is specified, the default 'channel0' is used.
1441 * Returns size of returned session payload data or a negative error code.
6e911cad
MD
1442 */
1443int lttng_disable_event(struct lttng_handle *handle, const char *name,
1444 const char *channel_name)
1445{
1446 struct lttng_event ev;
1447
1448 memset(&ev, 0, sizeof(ev));
9b7431cf 1449 ev.loglevel = -1;
6e911cad
MD
1450 ev.type = LTTNG_EVENT_ALL;
1451 lttng_ctl_copy_string(ev.name, name, sizeof(ev.name));
1452 return lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1df4dedd
DG
1453}
1454
cf0bcb51
JG
1455struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
1456{
1457 struct lttng_channel *channel = NULL;
1458 struct lttng_channel_extended *extended = NULL;
1459
1460 if (!domain) {
1461 goto error;
1462 }
1463
1464 /* Validate domain. */
1465 switch (domain->type) {
1466 case LTTNG_DOMAIN_UST:
1467 switch (domain->buf_type) {
1468 case LTTNG_BUFFER_PER_UID:
1469 case LTTNG_BUFFER_PER_PID:
1470 break;
1471 default:
1472 goto error;
1473 }
1474 break;
1475 case LTTNG_DOMAIN_KERNEL:
1476 if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
1477 goto error;
1478 }
1479 break;
1480 default:
1481 goto error;
1482 }
1483
1484 channel = zmalloc(sizeof(*channel));
1485 if (!channel) {
1486 goto error;
1487 }
1488
1489 extended = zmalloc(sizeof(*extended));
1490 if (!extended) {
1491 goto error;
1492 }
1493
1494 channel->attr.extended.ptr = extended;
1495
1496 lttng_channel_set_default_attr(domain, &channel->attr);
1497 return channel;
1498error:
1499 free(channel);
1500 free(extended);
1501 return NULL;
1502}
1503
1504void lttng_channel_destroy(struct lttng_channel *channel)
1505{
1506 if (!channel) {
1507 return;
1508 }
1509
1510 if (channel->attr.extended.ptr) {
1511 free(channel->attr.extended.ptr);
1512 }
1513 free(channel);
1514}
1515
1df4dedd 1516/*
9ae110e2
JG
1517 * Enable channel per domain
1518 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 1519 */
cd80958d 1520int lttng_enable_channel(struct lttng_handle *handle,
cf0bcb51 1521 struct lttng_channel *in_chan)
a5c5a2bd 1522{
cd80958d 1523 struct lttcomm_session_msg lsm;
09b72f7a 1524 size_t total_buffer_size_needed_per_cpu = 0;
cd80958d 1525
9ae110e2 1526 /* NULL arguments are forbidden. No default values. */
cf0bcb51 1527 if (handle == NULL || in_chan == NULL) {
2f70b271 1528 return -LTTNG_ERR_INVALID;
cd80958d
DG
1529 }
1530
441c16a7 1531 memset(&lsm, 0, sizeof(lsm));
cf0bcb51
JG
1532 memcpy(&lsm.u.channel.chan, in_chan, sizeof(lsm.u.channel.chan));
1533 lsm.u.channel.chan.attr.extended.ptr = NULL;
441c16a7 1534
cf0bcb51
JG
1535 if (!in_chan->attr.extended.ptr) {
1536 struct lttng_channel *channel;
1537 struct lttng_channel_extended *extended;
7d29a247 1538
cf0bcb51
JG
1539 channel = lttng_channel_create(&handle->domain);
1540 if (!channel) {
1541 return -LTTNG_ERR_NOMEM;
1542 }
1543
1544 /*
1545 * Create a new channel in order to use default extended
1546 * attribute values.
1547 */
1548 extended = (struct lttng_channel_extended *)
1549 channel->attr.extended.ptr;
1550 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1551 lttng_channel_destroy(channel);
1552 } else {
1553 struct lttng_channel_extended *extended;
1554
1555 extended = (struct lttng_channel_extended *)
1556 in_chan->attr.extended.ptr;
1557 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1558 }
cd80958d 1559
09b72f7a
FD
1560 /*
1561 * Verify that the amount of memory required to create the requested
1562 * buffer is available on the system at the moment.
1563 */
1564 total_buffer_size_needed_per_cpu = lsm.u.channel.chan.attr.num_subbuf *
1565 lsm.u.channel.chan.attr.subbuf_size;
1566 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu)) {
1567 return -LTTNG_ERR_NOMEM;
1568 }
1569
cf0bcb51 1570 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
cac3069d 1571 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 1572
cac3069d 1573 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1574 sizeof(lsm.session.name));
1575
cac3069d 1576 return lttng_ctl_ask_sessiond(&lsm, NULL);
8c0faa1d 1577}
1df4dedd 1578
2ef84c95 1579/*
9ae110e2
JG
1580 * All tracing will be stopped for registered events of the channel.
1581 * Returns size of returned session payload data or a negative error code.
2ef84c95 1582 */
cd80958d 1583int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 1584{
cd80958d
DG
1585 struct lttcomm_session_msg lsm;
1586
9ae110e2 1587 /* Safety check. Both are mandatory. */
9d697d3d 1588 if (handle == NULL || name == NULL) {
2f70b271 1589 return -LTTNG_ERR_INVALID;
cd80958d
DG
1590 }
1591
441c16a7
MD
1592 memset(&lsm, 0, sizeof(lsm));
1593
cd80958d 1594 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 1595
cac3069d 1596 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
9d697d3d
DG
1597 sizeof(lsm.u.disable.channel_name));
1598
cac3069d 1599 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cd80958d 1600
cac3069d 1601 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1602 sizeof(lsm.session.name));
1603
cac3069d 1604 return lttng_ctl_ask_sessiond(&lsm, NULL);
ca95a216
DG
1605}
1606
ccf10263 1607/*
9ae110e2
JG
1608 * Add PID to session tracker.
1609 * Return 0 on success else a negative LTTng error code.
ccf10263
MD
1610 */
1611int lttng_track_pid(struct lttng_handle *handle, int pid)
1612{
1613 struct lttcomm_session_msg lsm;
1614
9ae110e2 1615 /* NULL arguments are forbidden. No default values. */
ccf10263
MD
1616 if (handle == NULL) {
1617 return -LTTNG_ERR_INVALID;
1618 }
1619
1620 memset(&lsm, 0, sizeof(lsm));
1621
1622 lsm.cmd_type = LTTNG_TRACK_PID;
1623 lsm.u.pid_tracker.pid = pid;
1624
1625 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1626
1627 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1628 sizeof(lsm.session.name));
1629
1630 return lttng_ctl_ask_sessiond(&lsm, NULL);
1631}
1632
1633/*
9ae110e2
JG
1634 * Remove PID from session tracker.
1635 * Return 0 on success else a negative LTTng error code.
ccf10263
MD
1636 */
1637int lttng_untrack_pid(struct lttng_handle *handle, int pid)
1638{
1639 struct lttcomm_session_msg lsm;
1640
9ae110e2 1641 /* NULL arguments are forbidden. No default values. */
ccf10263
MD
1642 if (handle == NULL) {
1643 return -LTTNG_ERR_INVALID;
1644 }
1645
1646 memset(&lsm, 0, sizeof(lsm));
1647
1648 lsm.cmd_type = LTTNG_UNTRACK_PID;
1649 lsm.u.pid_tracker.pid = pid;
1650
1651 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1652
1653 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1654 sizeof(lsm.session.name));
1655
1656 return lttng_ctl_ask_sessiond(&lsm, NULL);
1657}
1658
fac6795d 1659/*
9ae110e2
JG
1660 * Lists all available tracepoints of domain.
1661 * Sets the contents of the events array.
1662 * Returns the number of lttng_event entries in events;
1663 * on error, returns a negative value.
fac6795d 1664 */
cd80958d 1665int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 1666 struct lttng_event **events)
fac6795d 1667{
052da939 1668 int ret;
cd80958d
DG
1669 struct lttcomm_session_msg lsm;
1670
9d697d3d 1671 if (handle == NULL) {
2f70b271 1672 return -LTTNG_ERR_INVALID;
cd80958d 1673 }
fac6795d 1674
53efb85a 1675 memset(&lsm, 0, sizeof(lsm));
cd80958d 1676 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
cac3069d 1677 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 1678
cac3069d 1679 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
1680 if (ret < 0) {
1681 return ret;
eb354453 1682 }
fac6795d 1683
9f19cc17 1684 return ret / sizeof(struct lttng_event);
fac6795d
DG
1685}
1686
f37d259d 1687/*
9ae110e2
JG
1688 * Lists all available tracepoint fields of domain.
1689 * Sets the contents of the event field array.
1690 * Returns the number of lttng_event_field entries in events;
1691 * on error, returns a negative value.
f37d259d
MD
1692 */
1693int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1694 struct lttng_event_field **fields)
1695{
1696 int ret;
1697 struct lttcomm_session_msg lsm;
1698
1699 if (handle == NULL) {
2f70b271 1700 return -LTTNG_ERR_INVALID;
f37d259d
MD
1701 }
1702
53efb85a 1703 memset(&lsm, 0, sizeof(lsm));
f37d259d 1704 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
cac3069d 1705 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f37d259d 1706
cac3069d 1707 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
1708 if (ret < 0) {
1709 return ret;
1710 }
1711
1712 return ret / sizeof(struct lttng_event_field);
1713}
1714
834978fd 1715/*
9ae110e2
JG
1716 * Lists all available kernel system calls. Allocates and sets the contents of
1717 * the events array.
834978fd 1718 *
9ae110e2
JG
1719 * Returns the number of lttng_event entries in events; on error, returns a
1720 * negative value.
834978fd
DG
1721 */
1722int lttng_list_syscalls(struct lttng_event **events)
1723{
1724 int ret;
1725 struct lttcomm_session_msg lsm;
1726
1727 if (!events) {
1728 return -LTTNG_ERR_INVALID;
1729 }
1730
1731 memset(&lsm, 0, sizeof(lsm));
1732 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1733 /* Force kernel domain for system calls. */
1734 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1735
1736 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1737 if (ret < 0) {
1738 return ret;
1739 }
1740
1741 return ret / sizeof(struct lttng_event);
1742}
1743
1657e9bb 1744/*
9ae110e2
JG
1745 * Returns a human readable string describing
1746 * the error code (a negative value).
1657e9bb 1747 */
9a745bc7 1748const char *lttng_strerror(int code)
1657e9bb 1749{
f73fabfd 1750 return error_get_str(code);
1657e9bb
DG
1751}
1752
b178f53e
JG
1753enum lttng_error_code lttng_create_session_ext(
1754 struct lttng_session_descriptor *session_descriptor)
1755{
1756 enum lttng_error_code ret_code;
1757 struct lttcomm_session_msg lsm = {
1758 .cmd_type = LTTNG_CREATE_SESSION_EXT,
1759 };
1760 void *reply = NULL;
1761 struct lttng_buffer_view reply_view;
1762 int reply_ret;
1763 bool sessiond_must_generate_ouput;
1764 struct lttng_dynamic_buffer payload;
1765 int ret;
1766 size_t descriptor_size;
1767 struct lttng_session_descriptor *descriptor_reply = NULL;
1768
1769 lttng_dynamic_buffer_init(&payload);
1770 if (!session_descriptor) {
1771 ret_code = LTTNG_ERR_INVALID;
1772 goto end;
1773 }
1774
1775 sessiond_must_generate_ouput =
1776 !lttng_session_descriptor_is_output_destination_initialized(
1777 session_descriptor);
1778 if (sessiond_must_generate_ouput) {
1779 const char *home_dir = utils_get_home_dir();
1780 size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0;
1781
1782 if (!home_dir || home_dir_len > LTTNG_PATH_MAX) {
1783 ret_code = LTTNG_ERR_FATAL;
1784 goto end;
1785 }
1786
1787 lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len;
1788 ret = lttng_dynamic_buffer_append(&payload, home_dir,
1789 home_dir_len);
1790 if (ret) {
1791 ret_code = LTTNG_ERR_NOMEM;
1792 goto end;
1793 }
1794 }
1795
1796 descriptor_size = payload.size;
1797 ret = lttng_session_descriptor_serialize(session_descriptor,
1798 &payload);
1799 if (ret) {
1800 ret_code = LTTNG_ERR_INVALID;
1801 goto end;
1802 }
1803 descriptor_size = payload.size - descriptor_size;
1804 lsm.u.create_session.session_descriptor_size = descriptor_size;
1805
1806 /* Command returns a session descriptor on success. */
1807 reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data,
1808 payload.size, &reply);
1809 if (reply_ret < 0) {
1810 ret_code = -reply_ret;
1811 goto end;
1812 } else if (reply_ret == 0) {
1813 /* Socket unexpectedly closed by the session daemon. */
1814 ret_code = LTTNG_ERR_FATAL;
1815 goto end;
1816 }
1817
1818 reply_view = lttng_buffer_view_init(reply, 0, reply_ret);
1819 ret = lttng_session_descriptor_create_from_buffer(&reply_view,
1820 &descriptor_reply);
1821 if (ret < 0) {
1822 ret_code = LTTNG_ERR_FATAL;
1823 goto end;
1824 }
1825 ret_code = LTTNG_OK;
1826 lttng_session_descriptor_assign(session_descriptor, descriptor_reply);
1827end:
1828 free(reply);
1829 lttng_dynamic_buffer_reset(&payload);
1830 lttng_session_descriptor_destroy(descriptor_reply);
1831 return ret_code;
1832}
1833
aaf97519 1834/*
b178f53e 1835 * Create a new session using name and url for destination.
a4b92340 1836 *
f73fabfd 1837 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1838 */
a4b92340 1839int lttng_create_session(const char *name, const char *url)
00e2e675 1840{
3dd05a85 1841 int ret;
a4b92340 1842 ssize_t size;
a4b92340 1843 struct lttng_uri *uris = NULL;
b178f53e
JG
1844 struct lttng_session_descriptor *descriptor = NULL;
1845 enum lttng_error_code ret_code;
00e2e675 1846
b178f53e
JG
1847 if (!name) {
1848 ret = -LTTNG_ERR_INVALID;
1849 goto end;
00e2e675
DG
1850 }
1851
bc894455 1852 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1853 if (size < 0) {
b178f53e
JG
1854 ret = -LTTNG_ERR_INVALID;
1855 goto end;
1856 }
1857 switch (size) {
1858 case 0:
1859 descriptor = lttng_session_descriptor_create(name);
1860 break;
1861 case 1:
1862 if (uris[0].dtype != LTTNG_DST_PATH) {
1863 ret = -LTTNG_ERR_INVALID;
1864 goto end;
1865 }
1866 descriptor = lttng_session_descriptor_local_create(name,
1867 uris[0].dst.path);
1868 break;
1869 case 2:
1870 descriptor = lttng_session_descriptor_network_create(name, url,
1871 NULL);
1872 break;
1873 default:
1874 ret = -LTTNG_ERR_INVALID;
1875 goto end;
1876 }
1877 if (!descriptor) {
1878 ret = -LTTNG_ERR_INVALID;
1879 goto end;
00e2e675 1880 }
b178f53e
JG
1881 ret_code = lttng_create_session_ext(descriptor);
1882 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
1883end:
1884 lttng_session_descriptor_destroy(descriptor);
1885 free(uris);
1886 return ret;
1887}
00e2e675 1888
b178f53e
JG
1889/*
1890 * Create a session exclusively used for snapshot.
1891 *
1892 * Returns LTTNG_OK on success or a negative error code.
1893 */
1894int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
1895{
1896 int ret;
1897 enum lttng_error_code ret_code;
1898 ssize_t size;
1899 struct lttng_uri *uris = NULL;
1900 struct lttng_session_descriptor *descriptor = NULL;
a4b92340 1901
b178f53e
JG
1902 if (!name) {
1903 ret = -LTTNG_ERR_INVALID;
1904 goto end;
1905 }
1906
1907 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
1908 if (size < 0) {
1909 ret = -LTTNG_ERR_INVALID;
1910 goto end;
1911 }
1912 /*
1913 * If the user does not specify a custom subdir, use the session name.
1914 */
1915 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH &&
1916 strlen(uris[0].subdir) == 0) {
1917 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s",
1918 name);
1919 if (ret < 0) {
1920 PERROR("Failed to set session name as network destination sub-directory");
1921 ret = -LTTNG_ERR_FATAL;
1922 goto end;
1923 } else if (ret >= sizeof(uris[0].subdir)) {
1924 /* Truncated output. */
1925 ret = -LTTNG_ERR_INVALID;
1926 goto end;
1927 }
1928 }
3dd05a85 1929
b178f53e
JG
1930 switch (size) {
1931 case 0:
1932 descriptor = lttng_session_descriptor_snapshot_create(name);
1933 break;
1934 case 1:
1935 if (uris[0].dtype != LTTNG_DST_PATH) {
1936 ret = -LTTNG_ERR_INVALID;
1937 goto end;
1938 }
1939 descriptor = lttng_session_descriptor_snapshot_local_create(
1940 name,
1941 uris[0].dst.path);
1942 break;
1943 case 2:
1944 descriptor = lttng_session_descriptor_snapshot_network_create(
1945 name,
1946 snapshot_url,
1947 NULL);
1948 break;
1949 default:
1950 ret = -LTTNG_ERR_INVALID;
1951 goto end;
1952 }
1953 if (!descriptor) {
1954 ret = -LTTNG_ERR_INVALID;
1955 goto end;
1956 }
1957 ret_code = lttng_create_session_ext(descriptor);
1958 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
1959end:
1960 lttng_session_descriptor_destroy(descriptor);
3dd05a85
DG
1961 free(uris);
1962 return ret;
00e2e675
DG
1963}
1964
b178f53e
JG
1965/*
1966 * Create a session exclusively used for live.
1967 *
1968 * Returns LTTNG_OK on success or a negative error code.
1969 */
1970int lttng_create_session_live(const char *name, const char *url,
1971 unsigned int timer_interval)
1972{
1973 int ret;
1974 enum lttng_error_code ret_code;
1975 struct lttng_session_descriptor *descriptor = NULL;
1976
1977 if (!name) {
1978 ret = -LTTNG_ERR_INVALID;
1979 goto end;
1980 }
1981
1982 if (url) {
1983 descriptor = lttng_session_descriptor_live_network_create(
1984 name, url, NULL, timer_interval);
1985 } else {
1986 descriptor = lttng_session_descriptor_live_create(
1987 name, timer_interval);
1988 }
1989 if (!descriptor) {
1990 ret = -LTTNG_ERR_INVALID;
1991 goto end;
1992 }
1993 ret_code = lttng_create_session_ext(descriptor);
1994 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
1995end:
1996 lttng_session_descriptor_destroy(descriptor);
1997 return ret;
1998}
1999
8028d920 2000/*
9ae110e2
JG
2001 * Destroy session using name.
2002 * Returns size of returned session payload data or a negative error code.
8028d920 2003 */
8fd2c92c 2004static
e20ee7c2 2005int _lttng_destroy_session(const char *session_name)
8028d920 2006{
cd80958d
DG
2007 struct lttcomm_session_msg lsm;
2008
843f5df9 2009 if (session_name == NULL) {
2f70b271 2010 return -LTTNG_ERR_INVALID;
cd80958d
DG
2011 }
2012
53efb85a 2013 memset(&lsm, 0, sizeof(lsm));
cd80958d 2014 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9 2015
cac3069d
DG
2016 lttng_ctl_copy_string(lsm.session.name, session_name,
2017 sizeof(lsm.session.name));
cd80958d 2018
cac3069d 2019 return lttng_ctl_ask_sessiond(&lsm, NULL);
aaf97519
DG
2020}
2021
e20ee7c2
JD
2022/*
2023 * Stop the session and wait for the data before destroying it
2024 */
2025int lttng_destroy_session(const char *session_name)
2026{
2027 int ret;
2028
2029 /*
2030 * Stop the tracing and wait for the data.
2031 */
2032 ret = _lttng_stop_tracing(session_name, 1);
2033 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
2034 goto end;
2035 }
2036
2037 ret = _lttng_destroy_session(session_name);
2038end:
2039 return ret;
2040}
2041
2042/*
2043 * Destroy the session without waiting for the data.
2044 */
2045int lttng_destroy_session_no_wait(const char *session_name)
2046{
2047 int ret;
2048
2049 /*
2050 * Stop the tracing without waiting for the data.
2051 * The session might already have been stopped, so just
2052 * skip this error.
2053 */
2054 ret = _lttng_stop_tracing(session_name, 0);
2055 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
2056 goto end;
2057 }
2058
2059 ret = _lttng_destroy_session(session_name);
2060end:
2061 return ret;
2062}
2063
57167058 2064/*
9ae110e2
JG
2065 * Ask the session daemon for all available sessions.
2066 * Sets the contents of the sessions array.
2067 * Returns the number of lttng_session entries in sessions;
2068 * on error, returns a negative value.
57167058 2069 */
b178f53e 2070int lttng_list_sessions(struct lttng_session **out_sessions)
57167058 2071{
ca95a216 2072 int ret;
cd80958d 2073 struct lttcomm_session_msg lsm;
b178f53e
JG
2074 const size_t session_size = sizeof(struct lttng_session) +
2075 sizeof(struct lttng_session_extended);
2076 size_t session_count, i;
2077 struct lttng_session_extended *sessions_extended_begin;
2078 struct lttng_session *sessions = NULL;
57167058 2079
53efb85a 2080 memset(&lsm, 0, sizeof(lsm));
cd80958d 2081 lsm.cmd_type = LTTNG_LIST_SESSIONS;
b178f53e
JG
2082 ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions);
2083 if (ret <= 0) {
2084 ret = ret == 0 ? -LTTNG_ERR_FATAL : ret;
2085 goto end;
2086 }
2087 if (!sessions) {
2088 ret = -LTTNG_ERR_FATAL;
2089 goto end;
2090 }
2091
2092 if (ret % session_size) {
2093 ret = -LTTNG_ERR_UNK;
2094 free(sessions);
2095 *out_sessions = NULL;
2096 goto end;
57167058 2097 }
b178f53e
JG
2098 session_count = (size_t) ret / session_size;
2099 sessions_extended_begin = (struct lttng_session_extended *)
2100 (&sessions[session_count]);
57167058 2101
b178f53e
JG
2102 /* Set extended session info pointers. */
2103 for (i = 0; i < session_count; i++) {
2104 struct lttng_session *session = &sessions[i];
2105 struct lttng_session_extended *extended =
2106 &(sessions_extended_begin[i]);
2107
2108 session->extended.ptr = extended;
2109 }
2110
2111 ret = (int) session_count;
2112 *out_sessions = sessions;
2113end:
2114 return ret;
2115}
2116
2117enum lttng_error_code lttng_session_get_creation_time(
2118 const struct lttng_session *session, uint64_t *creation_time)
2119{
2120 enum lttng_error_code ret = LTTNG_OK;
2121 struct lttng_session_extended *extended;
2122
2123 if (!session || !creation_time || !session->extended.ptr) {
2124 ret = LTTNG_ERR_INVALID;
2125 goto end;
2126 }
2127
2128 extended = session->extended.ptr;
2129 if (!extended->creation_time.is_set) {
2130 /* Not created on the session daemon yet. */
2131 ret = LTTNG_ERR_SESSION_NOT_EXIST;
2132 goto end;
2133 }
2134 *creation_time = extended->creation_time.value;
2135end:
2136 return ret;
57167058
DG
2137}
2138
d7ba1388
MD
2139int lttng_set_session_shm_path(const char *session_name,
2140 const char *shm_path)
2141{
2142 struct lttcomm_session_msg lsm;
2143
2144 if (session_name == NULL) {
2145 return -LTTNG_ERR_INVALID;
2146 }
2147
2148 memset(&lsm, 0, sizeof(lsm));
2149 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
2150
2151 lttng_ctl_copy_string(lsm.session.name, session_name,
2152 sizeof(lsm.session.name));
2153 lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path,
2154 sizeof(lsm.u.set_shm_path.shm_path));
2155
2156 return lttng_ctl_ask_sessiond(&lsm, NULL);
2157}
2158
9f19cc17 2159/*
9ae110e2
JG
2160 * Ask the session daemon for all available domains of a session.
2161 * Sets the contents of the domains array.
2162 * Returns the number of lttng_domain entries in domains;
2163 * on error, returns a negative value.
9f19cc17 2164 */
330be774 2165int lttng_list_domains(const char *session_name,
cd80958d 2166 struct lttng_domain **domains)
9f19cc17
DG
2167{
2168 int ret;
cd80958d
DG
2169 struct lttcomm_session_msg lsm;
2170
330be774 2171 if (session_name == NULL) {
2f70b271 2172 return -LTTNG_ERR_INVALID;
cd80958d 2173 }
9f19cc17 2174
53efb85a 2175 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
2176 lsm.cmd_type = LTTNG_LIST_DOMAINS;
2177
cac3069d
DG
2178 lttng_ctl_copy_string(lsm.session.name, session_name,
2179 sizeof(lsm.session.name));
cd80958d 2180
cac3069d 2181 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
2182 if (ret < 0) {
2183 return ret;
2184 }
2185
2186 return ret / sizeof(struct lttng_domain);
2187}
2188
2189/*
9ae110e2
JG
2190 * Ask the session daemon for all available channels of a session.
2191 * Sets the contents of the channels array.
2192 * Returns the number of lttng_channel entries in channels;
2193 * on error, returns a negative value.
9f19cc17 2194 */
cd80958d
DG
2195int lttng_list_channels(struct lttng_handle *handle,
2196 struct lttng_channel **channels)
9f19cc17
DG
2197{
2198 int ret;
53e367f9
JG
2199 size_t channel_count, i;
2200 const size_t channel_size = sizeof(struct lttng_channel) +
cf0bcb51 2201 sizeof(struct lttng_channel_extended);
cd80958d 2202 struct lttcomm_session_msg lsm;
53e367f9 2203 void *extended_at;
cd80958d 2204
9d697d3d 2205 if (handle == NULL) {
53e367f9
JG
2206 ret = -LTTNG_ERR_INVALID;
2207 goto end;
cd80958d
DG
2208 }
2209
53efb85a 2210 memset(&lsm, 0, sizeof(lsm));
cd80958d 2211 lsm.cmd_type = LTTNG_LIST_CHANNELS;
cac3069d 2212 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 2213 sizeof(lsm.session.name));
9f19cc17 2214
cac3069d 2215 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 2216
cac3069d 2217 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17 2218 if (ret < 0) {
53e367f9
JG
2219 goto end;
2220 }
2221
2222 if (ret % channel_size) {
2223 ret = -LTTNG_ERR_UNK;
2224 free(*channels);
2225 *channels = NULL;
2226 goto end;
9f19cc17 2227 }
53e367f9 2228 channel_count = (size_t) ret / channel_size;
9f19cc17 2229
53e367f9
JG
2230 /* Set extended info pointers */
2231 extended_at = ((void *) *channels) +
2232 channel_count * sizeof(struct lttng_channel);
2233 for (i = 0; i < channel_count; i++) {
2234 struct lttng_channel *chan = &(*channels)[i];
2235
2236 chan->attr.extended.ptr = extended_at;
cf0bcb51 2237 extended_at += sizeof(struct lttng_channel_extended);
53e367f9
JG
2238 }
2239
2240 ret = (int) channel_count;
2241end:
2242 return ret;
9f19cc17
DG
2243}
2244
2245/*
9ae110e2
JG
2246 * Ask the session daemon for all available events of a session channel.
2247 * Sets the contents of the events array.
2248 * Returns the number of lttng_event entries in events;
2249 * on error, returns a negative value.
9f19cc17 2250 */
cd80958d
DG
2251int lttng_list_events(struct lttng_handle *handle,
2252 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
2253{
2254 int ret;
cd80958d 2255 struct lttcomm_session_msg lsm;
b4e3ceb9
PP
2256 struct lttcomm_event_command_header *cmd_header = NULL;
2257 size_t cmd_header_len;
2258 uint32_t nb_events, i;
de453daa 2259 void *comm_ext_at;
de453daa 2260 char *reception_buffer = NULL;
56f0bc67 2261 struct lttng_dynamic_buffer listing;
de453daa 2262 size_t storage_req;
9f19cc17 2263
9d697d3d
DG
2264 /* Safety check. An handle and channel name are mandatory */
2265 if (handle == NULL || channel_name == NULL) {
2f70b271 2266 return -LTTNG_ERR_INVALID;
cd80958d
DG
2267 }
2268
53efb85a 2269 memset(&lsm, 0, sizeof(lsm));
cd80958d 2270 lsm.cmd_type = LTTNG_LIST_EVENTS;
cac3069d 2271 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 2272 sizeof(lsm.session.name));
cac3069d 2273 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
cd80958d 2274 sizeof(lsm.u.list.channel_name));
cac3069d 2275 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 2276
a04d53fc 2277 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
de453daa
JG
2278 (void **) &reception_buffer, (void **) &cmd_header,
2279 &cmd_header_len);
9f19cc17 2280 if (ret < 0) {
de453daa 2281 goto end;
9f19cc17
DG
2282 }
2283
d9f484bc
JG
2284 if (!cmd_header) {
2285 ret = -LTTNG_ERR_UNK;
2286 goto end;
2287 }
2288
b4e3ceb9
PP
2289 /* Set number of events and free command header */
2290 nb_events = cmd_header->nb_events;
2291 if (nb_events > INT_MAX) {
2292 ret = -EOVERFLOW;
de453daa 2293 goto end;
b4e3ceb9 2294 }
b4e3ceb9
PP
2295 free(cmd_header);
2296 cmd_header = NULL;
2297
de453daa
JG
2298 /*
2299 * The buffer that is returned must contain a "flat" version of
2300 * the events that are returned. In other words, all pointers
2301 * within an lttng_event must point to a location within the returned
56f0bc67
JG
2302 * buffer so that the user may free everything by simply calling free()
2303 * on the returned buffer. This is needed in order to maintain API
de453daa
JG
2304 * compatibility.
2305 *
56f0bc67
JG
2306 * A first pass is performed to compute the size of the buffer that
2307 * must be allocated. A second pass is then performed to setup
de453daa
JG
2308 * the returned events so that their members always point within the
2309 * buffer.
2310 *
2311 * The layout of the returned buffer is as follows:
2312 * - struct lttng_event[nb_events],
2313 * - nb_events times the following:
2314 * - struct lttng_event_extended,
2315 * - flattened version of userspace_probe_location
2316 * - filter_expression
2317 * - exclusions
2318 * - padding to align to 64-bits
2319 */
2320 comm_ext_at = reception_buffer +
2321 (nb_events * sizeof(struct lttng_event));
2322 storage_req = nb_events * sizeof(struct lttng_event);
b4e3ceb9
PP
2323
2324 for (i = 0; i < nb_events; i++) {
de453daa
JG
2325 struct lttcomm_event_extended_header *ext_comm =
2326 (struct lttcomm_event_extended_header *) comm_ext_at;
56f0bc67 2327 int probe_storage_req = 0;
b4e3ceb9 2328
de453daa
JG
2329 comm_ext_at += sizeof(*ext_comm);
2330 comm_ext_at += ext_comm->filter_len;
2331 comm_ext_at +=
2332 ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
2333
56f0bc67
JG
2334 if (ext_comm->userspace_probe_location_len) {
2335 struct lttng_userspace_probe_location *probe_location = NULL;
2336 struct lttng_buffer_view probe_location_view;
2337
2338 probe_location_view = lttng_buffer_view_init(
2339 comm_ext_at, 0,
2340 ext_comm->userspace_probe_location_len);
2341
2342 /*
2343 * Create a temporary userspace probe location to
2344 * determine the size needed by a "flattened" version
2345 * of that same probe location.
2346 */
2347 ret = lttng_userspace_probe_location_create_from_buffer(
2348 &probe_location_view, &probe_location);
2349 if (ret < 0) {
2350 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2351 goto end;
2352 }
2353
2354 ret = lttng_userspace_probe_location_flatten(
2355 probe_location, NULL);
2356 lttng_userspace_probe_location_destroy(probe_location);
2357 if (ret < 0) {
2358 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2359 goto end;
2360 }
2361
2362 probe_storage_req = ret;
2363 comm_ext_at += ext_comm->userspace_probe_location_len;
56f0bc67
JG
2364 }
2365
de453daa 2366 storage_req += sizeof(struct lttng_event_extended);
de453daa
JG
2367 storage_req += ext_comm->filter_len;
2368 storage_req += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
56f0bc67
JG
2369 /* Padding to ensure the flat probe is aligned. */
2370 storage_req = ALIGN_TO(storage_req, sizeof(uint64_t));
2371 storage_req += probe_storage_req;
b4e3ceb9
PP
2372 }
2373
56f0bc67
JG
2374 lttng_dynamic_buffer_init(&listing);
2375 /*
2376 * We must ensure that "listing" is never resized so as to preserve
2377 * the validity of the flattened objects.
2378 */
2379 ret = lttng_dynamic_buffer_set_capacity(&listing, storage_req);
2380 if (ret) {
2381 ret = -LTTNG_ERR_NOMEM;
de453daa
JG
2382 goto end;
2383 }
56f0bc67
JG
2384
2385 ret = lttng_dynamic_buffer_append(&listing, reception_buffer,
2386 nb_events * sizeof(struct lttng_event));
2387 if (ret) {
2388 ret = -LTTNG_ERR_NOMEM;
2389 goto free_dynamic_buffer;
2390 }
de453daa
JG
2391
2392 comm_ext_at = reception_buffer +
56f0bc67 2393 (nb_events * sizeof(struct lttng_event));
de453daa
JG
2394 for (i = 0; i < nb_events; i++) {
2395 struct lttng_event *event = (struct lttng_event *)
56f0bc67 2396 (listing.data + (sizeof(struct lttng_event) * i));
de453daa
JG
2397 struct lttcomm_event_extended_header *ext_comm =
2398 (struct lttcomm_event_extended_header *) comm_ext_at;
2399 struct lttng_event_extended *event_extended =
56f0bc67
JG
2400 (struct lttng_event_extended *)
2401 (listing.data + listing.size);
de453daa 2402
56f0bc67
JG
2403 /* Insert struct lttng_event_extended. */
2404 ret = lttng_dynamic_buffer_set_size(&listing,
2405 listing.size + sizeof(*event_extended));
2406 if (ret) {
2407 ret = -LTTNG_ERR_NOMEM;
2408 goto free_dynamic_buffer;
2409 }
de453daa 2410 event->extended.ptr = event_extended;
56f0bc67 2411
de453daa
JG
2412 comm_ext_at += sizeof(*ext_comm);
2413
56f0bc67
JG
2414 /* Insert filter expression. */
2415 if (ext_comm->filter_len) {
2416 event_extended->filter_expression = listing.data +
2417 listing.size;
2418 ret = lttng_dynamic_buffer_append(&listing, comm_ext_at,
2419 ext_comm->filter_len);
2420 if (ret) {
2421 ret = -LTTNG_ERR_NOMEM;
2422 goto free_dynamic_buffer;
2423 }
2424 comm_ext_at += ext_comm->filter_len;
2425 }
2426
2427 /* Insert exclusions. */
2428 if (ext_comm->nb_exclusions) {
2429 event_extended->exclusions.count =
2430 ext_comm->nb_exclusions;
2431 event_extended->exclusions.strings =
2432 listing.data + listing.size;
2433
2434 ret = lttng_dynamic_buffer_append(&listing,
2435 comm_ext_at,
2436 ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN);
2437 if (ret) {
2438 ret = -LTTNG_ERR_NOMEM;
5e4f4898 2439 goto free_dynamic_buffer;
56f0bc67
JG
2440 }
2441 comm_ext_at += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
2442 }
2443
2444 /* Insert padding to align to 64-bits. */
2445 ret = lttng_dynamic_buffer_set_size(&listing,
2446 ALIGN_TO(listing.size, sizeof(uint64_t)));
2447 if (ret) {
2448 ret = -LTTNG_ERR_NOMEM;
2449 goto free_dynamic_buffer;
2450 }
2451
2452 /* Insert flattened userspace probe location. */
2453 if (ext_comm->userspace_probe_location_len) {
2454 struct lttng_userspace_probe_location *probe_location = NULL;
2455 struct lttng_buffer_view probe_location_view;
de453daa 2456
56f0bc67
JG
2457 probe_location_view = lttng_buffer_view_init(
2458 comm_ext_at, 0,
2459 ext_comm->userspace_probe_location_len);
de453daa 2460
56f0bc67
JG
2461 ret = lttng_userspace_probe_location_create_from_buffer(
2462 &probe_location_view, &probe_location);
2463 if (ret < 0) {
2464 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2465 goto free_dynamic_buffer;
2466 }
2467
2468 event_extended->probe_location = (struct lttng_userspace_probe_location *)
2469 (listing.data + listing.size);
2470 ret = lttng_userspace_probe_location_flatten(
2471 probe_location, &listing);
2472 lttng_userspace_probe_location_destroy(probe_location);
2473 if (ret < 0) {
2474 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2475 goto free_dynamic_buffer;
2476 }
2477
2478 comm_ext_at += ext_comm->userspace_probe_location_len;
2479 }
de453daa
JG
2480 }
2481
56f0bc67
JG
2482 /* Don't reset listing buffer as we return its content. */
2483 *events = (struct lttng_event *) listing.data;
2484 lttng_dynamic_buffer_init(&listing);
de453daa 2485 ret = (int) nb_events;
56f0bc67
JG
2486free_dynamic_buffer:
2487 lttng_dynamic_buffer_reset(&listing);
de453daa 2488end:
b4e3ceb9 2489 free(cmd_header);
de453daa 2490 free(reception_buffer);
b4e3ceb9 2491 return ret;
9f19cc17
DG
2492}
2493
fac6795d 2494/*
1c8d13c8
TD
2495 * Sets the tracing_group variable with name.
2496 * This function allocates memory pointed to by tracing_group.
2497 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
2498 */
2499int lttng_set_tracing_group(const char *name)
2500{
9d697d3d 2501 if (name == NULL) {
2f70b271 2502 return -LTTNG_ERR_INVALID;
9d697d3d
DG
2503 }
2504
fac6795d 2505 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 2506 return -LTTNG_ERR_FATAL;
fac6795d
DG
2507 }
2508
2509 return 0;
2510}
2511
cd80958d 2512int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
2513 struct lttng_calibrate *calibrate)
2514{
b812e5ca
PP
2515 /*
2516 * This command was removed in LTTng 2.9.
2517 */
2518 return -LTTNG_ERR_UND;
d0254c7c
MD
2519}
2520
5edd7e09
DG
2521/*
2522 * Set default channel attributes.
441c16a7 2523 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
2524 */
2525void lttng_channel_set_default_attr(struct lttng_domain *domain,
2526 struct lttng_channel_attr *attr)
2527{
294851b0 2528 struct lttng_channel_extended *extended;
cf0bcb51 2529
5edd7e09
DG
2530 /* Safety check */
2531 if (attr == NULL || domain == NULL) {
2532 return;
2533 }
2534
294851b0 2535 extended = (struct lttng_channel_extended *) attr->extended.ptr;
eacaa7a6
DS
2536 memset(attr, 0, sizeof(struct lttng_channel_attr));
2537
0a9c6494
DG
2538 /* Same for all domains. */
2539 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2540 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2541 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2542
5edd7e09
DG
2543 switch (domain->type) {
2544 case LTTNG_DOMAIN_KERNEL:
9ae110e2
JG
2545 attr->switch_timer_interval =
2546 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
d92ff3ef 2547 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 2548 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
2549 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2550 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
cf0bcb51
JG
2551 if (extended) {
2552 extended->monitor_timer_interval =
2553 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
491d1539
MD
2554 extended->blocking_timeout =
2555 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2556 }
5edd7e09
DG
2557 break;
2558 case LTTNG_DOMAIN_UST:
0a9c6494
DG
2559 switch (domain->buf_type) {
2560 case LTTNG_BUFFER_PER_UID:
2561 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2562 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2563 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
9ae110e2
JG
2564 attr->switch_timer_interval =
2565 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2566 attr->read_timer_interval =
2567 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2568 if (extended) {
2569 extended->monitor_timer_interval =
2570 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2571 extended->blocking_timeout =
2572 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2573 }
0a9c6494
DG
2574 break;
2575 case LTTNG_BUFFER_PER_PID:
2576 default:
2577 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2578 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2579 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
9ae110e2
JG
2580 attr->switch_timer_interval =
2581 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2582 attr->read_timer_interval =
2583 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2584 if (extended) {
2585 extended->monitor_timer_interval =
2586 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2587 extended->blocking_timeout =
2588 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2589 }
0a9c6494
DG
2590 break;
2591 }
5edd7e09 2592 default:
441c16a7 2593 /* Default behavior: leave set to 0. */
5edd7e09
DG
2594 break;
2595 }
cf0bcb51
JG
2596
2597 attr->extended.ptr = extended;
5edd7e09
DG
2598}
2599
5ba3702f
JG
2600int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2601 uint64_t *discarded_events)
2602{
2603 int ret = 0;
cf0bcb51 2604 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2605
2606 if (!channel || !discarded_events) {
2607 ret = -LTTNG_ERR_INVALID;
2608 goto end;
2609 }
2610
2611 chan_ext = channel->attr.extended.ptr;
2612 if (!chan_ext) {
2613 /*
2614 * This can happen since the lttng_channel structure is
2615 * used for other tasks where this pointer is never set.
2616 */
2617 *discarded_events = 0;
2618 goto end;
2619 }
2620
2621 *discarded_events = chan_ext->discarded_events;
2622end:
2623 return ret;
2624}
2625
2626int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2627 uint64_t *lost_packets)
2628{
2629 int ret = 0;
cf0bcb51 2630 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2631
2632 if (!channel || !lost_packets) {
2633 ret = -LTTNG_ERR_INVALID;
2634 goto end;
2635 }
2636
2637 chan_ext = channel->attr.extended.ptr;
2638 if (!chan_ext) {
2639 /*
2640 * This can happen since the lttng_channel structure is
2641 * used for other tasks where this pointer is never set.
2642 */
2643 *lost_packets = 0;
2644 goto end;
2645 }
2646
2647 *lost_packets = chan_ext->lost_packets;
2648end:
2649 return ret;
2650}
2651
cf0bcb51
JG
2652int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2653 uint64_t *monitor_timer_interval)
2654{
2655 int ret = 0;
2656
2657 if (!chan || !monitor_timer_interval) {
2658 ret = -LTTNG_ERR_INVALID;
2659 goto end;
2660 }
2661
2662 if (!chan->attr.extended.ptr) {
2663 ret = -LTTNG_ERR_INVALID;
2664 goto end;
2665 }
2666
2667 *monitor_timer_interval = ((struct lttng_channel_extended *)
2668 chan->attr.extended.ptr)->monitor_timer_interval;
2669end:
2670 return ret;
2671}
2672
2673int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2674 uint64_t monitor_timer_interval)
2675{
2676 int ret = 0;
2677
2678 if (!chan || !chan->attr.extended.ptr) {
2679 ret = -LTTNG_ERR_INVALID;
2680 goto end;
2681 }
2682
2683 ((struct lttng_channel_extended *)
2684 chan->attr.extended.ptr)->monitor_timer_interval =
2685 monitor_timer_interval;
2686end:
2687 return ret;
2688}
2689
491d1539
MD
2690int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2691 int64_t *blocking_timeout)
2692{
2693 int ret = 0;
2694
2695 if (!chan || !blocking_timeout) {
2696 ret = -LTTNG_ERR_INVALID;
2697 goto end;
2698 }
2699
2700 if (!chan->attr.extended.ptr) {
2701 ret = -LTTNG_ERR_INVALID;
2702 goto end;
2703 }
2704
2705 *blocking_timeout = ((struct lttng_channel_extended *)
2706 chan->attr.extended.ptr)->blocking_timeout;
2707end:
2708 return ret;
2709}
2710
2711int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2712 int64_t blocking_timeout)
2713{
2714 int ret = 0;
2715 int64_t msec_timeout;
2716
2717 if (!chan || !chan->attr.extended.ptr) {
2718 ret = -LTTNG_ERR_INVALID;
2719 goto end;
2720 }
2721
2722 if (blocking_timeout < 0 && blocking_timeout != -1) {
2723 ret = -LTTNG_ERR_INVALID;
2724 goto end;
2725 }
2726
2727 /*
2728 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2729 * us to accept a narrower range of values (msecs expressed as a signed
2730 * 32-bit integer).
2731 */
2732 msec_timeout = blocking_timeout / 1000;
2733 if (msec_timeout != (int32_t) msec_timeout) {
2734 ret = -LTTNG_ERR_INVALID;
2735 goto end;
2736 }
2737
2738 ((struct lttng_channel_extended *)
2739 chan->attr.extended.ptr)->blocking_timeout =
2740 blocking_timeout;
2741end:
2742 return ret;
2743}
2744
fac6795d 2745/*
2269e89e 2746 * Check if session daemon is alive.
fac6795d 2747 *
2269e89e 2748 * Return 1 if alive or 0 if not.
1c8d13c8 2749 * On error returns a negative value.
fac6795d 2750 */
947308c4 2751int lttng_session_daemon_alive(void)
fac6795d
DG
2752{
2753 int ret;
2754
2755 ret = set_session_daemon_path();
2756 if (ret < 0) {
9ae110e2 2757 /* Error. */
fac6795d
DG
2758 return ret;
2759 }
2760
9d035200 2761 if (*sessiond_sock_path == '\0') {
2f70b271 2762 /*
9ae110e2
JG
2763 * No socket path set. Weird error which means the constructor
2764 * was not called.
2f70b271
DG
2765 */
2766 assert(0);
fac6795d
DG
2767 }
2768
2269e89e 2769 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9 2770 if (ret < 0) {
9ae110e2 2771 /* Not alive. */
7d8234d9
MD
2772 return 0;
2773 }
7d8234d9 2774
9ae110e2 2775 /* Is alive. */
947308c4 2776 return 1;
fac6795d
DG
2777}
2778
00e2e675 2779/*
a4b92340 2780 * Set URL for a consumer for a session and domain.
00e2e675
DG
2781 *
2782 * Return 0 on success, else a negative value.
2783 */
a4b92340
DG
2784int lttng_set_consumer_url(struct lttng_handle *handle,
2785 const char *control_url, const char *data_url)
00e2e675 2786{
3dd05a85 2787 int ret;
a4b92340 2788 ssize_t size;
00e2e675 2789 struct lttcomm_session_msg lsm;
a4b92340 2790 struct lttng_uri *uris = NULL;
00e2e675 2791
a4b92340 2792 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 2793 return -LTTNG_ERR_INVALID;
00e2e675
DG
2794 }
2795
a4b92340
DG
2796 memset(&lsm, 0, sizeof(lsm));
2797
00e2e675
DG
2798 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2799
cac3069d 2800 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
00e2e675 2801 sizeof(lsm.session.name));
cac3069d 2802 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
00e2e675 2803
bc894455 2804 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 2805 if (size < 0) {
2f70b271 2806 return -LTTNG_ERR_INVALID;
a4b92340
DG
2807 }
2808
2809 lsm.u.uri.size = size;
00e2e675 2810
795a978d 2811 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 2812 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
2813
2814 free(uris);
2815 return ret;
00e2e675
DG
2816}
2817
2818/*
9c6bda17 2819 * [OBSOLETE]
00e2e675
DG
2820 */
2821int lttng_enable_consumer(struct lttng_handle *handle)
2822{
785d2d0d 2823 return -ENOSYS;
00e2e675
DG
2824}
2825
2826/*
9c6bda17 2827 * [OBSOLETE]
00e2e675
DG
2828 */
2829int lttng_disable_consumer(struct lttng_handle *handle)
2830{
785d2d0d 2831 return -ENOSYS;
00e2e675
DG
2832}
2833
07424f16 2834/*
b178f53e 2835 * [OBSOLETE]
07424f16
DG
2836 */
2837int _lttng_create_session_ext(const char *name, const char *url,
2838 const char *datetime)
2839{
b178f53e 2840 return -ENOSYS;
07424f16
DG
2841}
2842
806e2684
DG
2843/*
2844 * For a given session name, this call checks if the data is ready to be read
2845 * or is still being extracted by the consumer(s) hence not ready to be used by
2846 * any readers.
2847 */
6d805429 2848int lttng_data_pending(const char *session_name)
806e2684
DG
2849{
2850 int ret;
2851 struct lttcomm_session_msg lsm;
f6151c55 2852 uint8_t *pending = NULL;
806e2684
DG
2853
2854 if (session_name == NULL) {
2855 return -LTTNG_ERR_INVALID;
2856 }
2857
53efb85a 2858 memset(&lsm, 0, sizeof(lsm));
6d805429 2859 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 2860
cac3069d
DG
2861 lttng_ctl_copy_string(lsm.session.name, session_name,
2862 sizeof(lsm.session.name));
806e2684 2863
f6151c55
JG
2864 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2865 if (ret < 0) {
2866 goto end;
2867 } else if (ret != 1) {
2868 /* Unexpected payload size */
2869 ret = -LTTNG_ERR_INVALID;
2870 goto end;
806e2684
DG
2871 }
2872
f6151c55
JG
2873 ret = (int) *pending;
2874end:
2875 free(pending);
806e2684
DG
2876 return ret;
2877}
2878
a5dfbb9d
MD
2879/*
2880 * List PIDs in the tracker.
2881 *
1b18ce93
JG
2882 * enabled is set to whether the PID tracker is enabled.
2883 * pids is set to an allocated array of PIDs currently tracked. On
2884 * success, pids must be freed by the caller.
2885 * nr_pids is set to the number of entries contained by the pids array.
a5dfbb9d
MD
2886 *
2887 * Returns 0 on success, else a negative LTTng error code.
2888 */
2889int lttng_list_tracker_pids(struct lttng_handle *handle,
2890 int *_enabled, int32_t **_pids, size_t *_nr_pids)
2891{
ebbf5ab7
JR
2892 int ret;
2893 int enabled = 1;
a5dfbb9d
MD
2894 struct lttcomm_session_msg lsm;
2895 size_t nr_pids;
ff75c11e 2896 int32_t *pids = NULL;
a5dfbb9d
MD
2897
2898 if (handle == NULL) {
2899 return -LTTNG_ERR_INVALID;
2900 }
2901
2902 memset(&lsm, 0, sizeof(lsm));
2903 lsm.cmd_type = LTTNG_LIST_TRACKER_PIDS;
2904 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2905 sizeof(lsm.session.name));
2906 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2907
2908 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pids);
2909 if (ret < 0) {
2910 return ret;
2911 }
2912 nr_pids = ret / sizeof(int32_t);
de8218d4
JG
2913 if (nr_pids > 0 && !pids) {
2914 return -LTTNG_ERR_UNK;
2915 }
a5dfbb9d
MD
2916 if (nr_pids == 1 && pids[0] == -1) {
2917 free(pids);
2918 pids = NULL;
2919 enabled = 0;
2920 nr_pids = 0;
2921 }
2922 *_enabled = enabled;
2923 *_pids = pids;
2924 *_nr_pids = nr_pids;
2925 return 0;
2926}
2927
93ec662e
JD
2928/*
2929 * Regenerate the metadata for a session.
2930 * Return 0 on success, a negative error code on error.
2931 */
eded6438 2932int lttng_regenerate_metadata(const char *session_name)
93ec662e
JD
2933{
2934 int ret;
2935 struct lttcomm_session_msg lsm;
2936
2937 if (!session_name) {
2938 ret = -LTTNG_ERR_INVALID;
2939 goto end;
2940 }
2941
2942 memset(&lsm, 0, sizeof(lsm));
eded6438 2943 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
93ec662e
JD
2944
2945 lttng_ctl_copy_string(lsm.session.name, session_name,
2946 sizeof(lsm.session.name));
2947
2948 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2949 if (ret < 0) {
2950 goto end;
2951 }
2952
2953 ret = 0;
2954end:
2955 return ret;
2956}
2957
eded6438
JD
2958/*
2959 * Deprecated, replaced by lttng_regenerate_metadata.
2960 */
2961int lttng_metadata_regenerate(const char *session_name)
2962{
2963 return lttng_regenerate_metadata(session_name);
2964}
2965
c2561365
JD
2966/*
2967 * Regenerate the statedump of a session.
2968 * Return 0 on success, a negative error code on error.
2969 */
2970int lttng_regenerate_statedump(const char *session_name)
2971{
2972 int ret;
2973 struct lttcomm_session_msg lsm;
2974
2975 if (!session_name) {
2976 ret = -LTTNG_ERR_INVALID;
2977 goto end;
2978 }
2979
2980 memset(&lsm, 0, sizeof(lsm));
2981 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2982
2983 lttng_ctl_copy_string(lsm.session.name, session_name,
2984 sizeof(lsm.session.name));
2985
2986 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2987 if (ret < 0) {
2988 goto end;
2989 }
2990
2991 ret = 0;
2992end:
2993 return ret;
2994}
2995
a58c490f
JG
2996int lttng_register_trigger(struct lttng_trigger *trigger)
2997{
2998 int ret;
2999 struct lttcomm_session_msg lsm;
3647288f 3000 struct lttng_dynamic_buffer buffer;
a58c490f 3001
3647288f 3002 lttng_dynamic_buffer_init(&buffer);
a58c490f
JG
3003 if (!trigger) {
3004 ret = -LTTNG_ERR_INVALID;
3005 goto end;
3006 }
3007
3008 if (!lttng_trigger_validate(trigger)) {
eac4828d 3009 ret = -LTTNG_ERR_INVALID_TRIGGER;
a58c490f
JG
3010 goto end;
3011 }
3012
3647288f
JG
3013 ret = lttng_trigger_serialize(trigger, &buffer);
3014 if (ret < 0) {
a58c490f
JG
3015 ret = -LTTNG_ERR_UNK;
3016 goto end;
3017 }
3018
a58c490f
JG
3019 memset(&lsm, 0, sizeof(lsm));
3020 lsm.cmd_type = LTTNG_REGISTER_TRIGGER;
3647288f
JG
3021 lsm.u.trigger.length = (uint32_t) buffer.size;
3022 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
3023 buffer.size, NULL);
a58c490f 3024end:
3647288f 3025 lttng_dynamic_buffer_reset(&buffer);
a58c490f
JG
3026 return ret;
3027}
3028
3029int lttng_unregister_trigger(struct lttng_trigger *trigger)
3030{
3031 int ret;
3032 struct lttcomm_session_msg lsm;
3647288f 3033 struct lttng_dynamic_buffer buffer;
a58c490f 3034
3647288f 3035 lttng_dynamic_buffer_init(&buffer);
a58c490f
JG
3036 if (!trigger) {
3037 ret = -LTTNG_ERR_INVALID;
3038 goto end;
3039 }
3040
3041 if (!lttng_trigger_validate(trigger)) {
3647288f 3042 ret = -LTTNG_ERR_INVALID_TRIGGER;
a58c490f
JG
3043 goto end;
3044 }
3045
3647288f
JG
3046 ret = lttng_trigger_serialize(trigger, &buffer);
3047 if (ret < 0) {
a58c490f
JG
3048 ret = -LTTNG_ERR_UNK;
3049 goto end;
3050 }
3051
a58c490f
JG
3052 memset(&lsm, 0, sizeof(lsm));
3053 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
3647288f
JG
3054 lsm.u.trigger.length = (uint32_t) buffer.size;
3055 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
3056 buffer.size, NULL);
a58c490f 3057end:
3647288f 3058 lttng_dynamic_buffer_reset(&buffer);
a58c490f
JG
3059 return ret;
3060}
3061
fac6795d 3062/*
9ae110e2 3063 * lib constructor.
fac6795d 3064 */
b2b89e8a 3065static void __attribute__((constructor)) init(void)
fac6795d
DG
3066{
3067 /* Set default session group */
bbccc3d2 3068 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 3069}
49cca668
DG
3070
3071/*
9ae110e2 3072 * lib destructor.
49cca668 3073 */
b2b89e8a 3074static void __attribute__((destructor)) lttng_ctl_exit(void)
49cca668
DG
3075{
3076 free(tracing_group);
3077}
This page took 0.236049 seconds and 5 git commands to generate.