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