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