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