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