Cleanup comments in lttng-ctl.c
[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/*
9ae110e2 200 * Check if we are in the specified group.
65beb5ff 201 *
9ae110e2 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) {
9ae110e2 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{
9ae110e2 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) {
9ae110e2 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 /*
9ae110e2
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/*
9ae110e2 343 * Connect to the LTTng session daemon.
65beb5ff 344 *
9ae110e2 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
9ae110e2 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.
9ae110e2 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.
9ae110e2 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.
9ae110e2 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
9ae110e2
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/*
9ae110e2
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 /*
9ae110e2
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{
cd80958d
DG
647 struct lttcomm_session_msg lsm;
648
9ae110e2 649 /* Safety check. Both are mandatory. */
9d697d3d 650 if (handle == NULL || ctx == NULL) {
2f70b271 651 return -LTTNG_ERR_INVALID;
cd80958d
DG
652 }
653
441c16a7
MD
654 memset(&lsm, 0, sizeof(lsm));
655
cd80958d
DG
656 lsm.cmd_type = LTTNG_ADD_CONTEXT;
657
85076754
MD
658 /* If no channel name, send empty string. */
659 if (channel_name == NULL) {
660 lttng_ctl_copy_string(lsm.u.context.channel_name, "",
661 sizeof(lsm.u.context.channel_name));
662 } else {
663 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
664 sizeof(lsm.u.context.channel_name));
665 }
cd80958d 666
cac3069d 667 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 668
9d697d3d 669 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 670
cac3069d 671 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
672 sizeof(lsm.session.name));
673
cac3069d 674 return lttng_ctl_ask_sessiond(&lsm, NULL);
d65106b1
DG
675}
676
f3ed775e 677/*
9ae110e2
JG
678 * Enable event(s) for a channel.
679 *
680 * If no event name is specified, all events are enabled.
681 * If no channel name is specified, the default 'channel0' is used.
682 *
683 * Returns size of returned session payload data or a negative error code.
f3ed775e 684 */
cd80958d 685int lttng_enable_event(struct lttng_handle *handle,
38057ed1 686 struct lttng_event *ev, const char *channel_name)
1df4dedd 687{
f8a96544
JI
688 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
689 NULL, 0, NULL);
1df4dedd
DG
690}
691
53a80697 692/*
025faf73 693 * Create or enable an event with a filter expression.
178191b3 694 *
53a80697
MD
695 * Return negative error value on error.
696 * Return size of returned session payload data if OK.
697 */
025faf73 698int lttng_enable_event_with_filter(struct lttng_handle *handle,
178191b3 699 struct lttng_event *event, const char *channel_name,
53a80697
MD
700 const char *filter_expression)
701{
f8a96544
JI
702 return lttng_enable_event_with_exclusions(handle, event, channel_name,
703 filter_expression, 0, NULL);
53a80697
MD
704}
705
347c5ab5 706/*
0e115563 707 * Depending on the event, return a newly allocated agent filter expression or
347c5ab5
DG
708 * NULL if not applicable.
709 *
710 * An event with NO loglevel and the name is * will return NULL.
711 */
0e115563 712static char *set_agent_filter(const char *filter, struct lttng_event *ev)
347c5ab5
DG
713{
714 int err;
0e115563 715 char *agent_filter = NULL;
347c5ab5
DG
716
717 assert(ev);
718
719 /* Don't add filter for the '*' event. */
720 if (ev->name[0] != '*') {
721 if (filter) {
0e115563 722 err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
347c5ab5
DG
723 ev->name);
724 } else {
0e115563 725 err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
347c5ab5
DG
726 }
727 if (err < 0) {
728 PERROR("asprintf");
6a556f7b 729 goto error;
347c5ab5
DG
730 }
731 }
732
733 /* Add loglevel filtering if any for the JUL domain. */
734 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
735 char *op;
736
737 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
738 op = ">=";
739 } else {
740 op = "==";
741 }
742
0e115563 743 if (filter || agent_filter) {
6a556f7b
JG
744 char *new_filter;
745
fb0edb23 746 err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
0e115563 747 agent_filter ? agent_filter : filter, op,
347c5ab5 748 ev->loglevel);
0e115563
DG
749 if (agent_filter) {
750 free(agent_filter);
6a556f7b 751 }
0e115563 752 agent_filter = new_filter;
347c5ab5 753 } else {
0e115563 754 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
347c5ab5
DG
755 ev->loglevel);
756 }
757 if (err < 0) {
758 PERROR("asprintf");
6a556f7b 759 goto error;
347c5ab5
DG
760 }
761 }
762
0e115563 763 return agent_filter;
6a556f7b 764error:
0e115563 765 free(agent_filter);
6a556f7b 766 return NULL;
347c5ab5
DG
767}
768
137b9942 769/*
ec166985 770 * Generate the filter bytecode from a given filter expression string. Put the
137b9942
DG
771 * newly allocated parser context in ctxp and populate the lsm object with the
772 * expression len.
773 *
774 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
775 */
776static int generate_filter(char *filter_expression,
777 struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp)
778{
779 int ret;
780 struct filter_parser_ctx *ctx = NULL;
781 FILE *fmem = NULL;
782
783 assert(filter_expression);
784 assert(lsm);
785 assert(ctxp);
786
787 /*
788 * Casting const to non-const, as the underlying function will use it in
789 * read-only mode.
790 */
791 fmem = lttng_fmemopen((void *) filter_expression,
792 strlen(filter_expression), "r");
793 if (!fmem) {
794 fprintf(stderr, "Error opening memory as stream\n");
795 ret = -LTTNG_ERR_FILTER_NOMEM;
796 goto error;
797 }
798 ctx = filter_parser_ctx_alloc(fmem);
799 if (!ctx) {
800 fprintf(stderr, "Error allocating parser\n");
801 ret = -LTTNG_ERR_FILTER_NOMEM;
802 goto filter_alloc_error;
803 }
804 ret = filter_parser_ctx_append_ast(ctx);
805 if (ret) {
806 fprintf(stderr, "Parse error\n");
807 ret = -LTTNG_ERR_FILTER_INVAL;
808 goto parse_error;
809 }
810 ret = filter_visitor_set_parent(ctx);
811 if (ret) {
812 fprintf(stderr, "Set parent error\n");
813 ret = -LTTNG_ERR_FILTER_INVAL;
814 goto parse_error;
815 }
816 if (print_xml) {
817 ret = filter_visitor_print_xml(ctx, stdout, 0);
818 if (ret) {
819 fflush(stdout);
820 fprintf(stderr, "XML print error\n");
821 ret = -LTTNG_ERR_FILTER_INVAL;
822 goto parse_error;
823 }
824 }
825
826 dbg_printf("Generating IR... ");
827 fflush(stdout);
828 ret = filter_visitor_ir_generate(ctx);
829 if (ret) {
830 fprintf(stderr, "Generate IR error\n");
831 ret = -LTTNG_ERR_FILTER_INVAL;
832 goto parse_error;
833 }
834 dbg_printf("done\n");
835
836 dbg_printf("Validating IR... ");
837 fflush(stdout);
838 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
839 if (ret) {
840 ret = -LTTNG_ERR_FILTER_INVAL;
841 goto parse_error;
842 }
9ae110e2 843 /* Validate strings used as literals in the expression. */
dcd5daf2
JG
844 ret = filter_visitor_ir_validate_string(ctx);
845 if (ret) {
846 ret = -LTTNG_ERR_FILTER_INVAL;
847 goto parse_error;
848 }
137b9942
DG
849 dbg_printf("done\n");
850
851 dbg_printf("Generating bytecode... ");
852 fflush(stdout);
853 ret = filter_visitor_bytecode_generate(ctx);
854 if (ret) {
855 fprintf(stderr, "Generate bytecode error\n");
856 ret = -LTTNG_ERR_FILTER_INVAL;
857 goto parse_error;
858 }
859 dbg_printf("done\n");
860 dbg_printf("Size of bytecode generated: %u bytes.\n",
861 bytecode_get_len(&ctx->bytecode->b));
862
863 lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b)
864 + bytecode_get_len(&ctx->bytecode->b);
865 lsm->u.enable.expression_len = strlen(filter_expression) + 1;
866
867 /* No need to keep the memory stream. */
868 if (fclose(fmem) != 0) {
6f04ed72 869 PERROR("fclose");
137b9942
DG
870 }
871
872 *ctxp = ctx;
873 return 0;
874
875parse_error:
137b9942
DG
876 filter_ir_free(ctx);
877 filter_parser_ctx_free(ctx);
878filter_alloc_error:
879 if (fclose(fmem) != 0) {
6f04ed72 880 PERROR("fclose");
137b9942
DG
881 }
882error:
883 return ret;
884}
885
93deb080
JI
886/*
887 * Enable event(s) for a channel, possibly with exclusions and a filter.
888 * If no event name is specified, all events are enabled.
889 * If no channel name is specified, the default name is used.
890 * If filter expression is not NULL, the filter is set for the event.
891 * If exclusion count is not zero, the exclusions are set for the event.
892 * Returns size of returned session payload data or a negative error code.
893 */
894int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
895 struct lttng_event *ev, const char *channel_name,
e9efbcd3 896 const char *original_filter_expression,
93deb080
JI
897 int exclusion_count, char **exclusion_list)
898{
899 struct lttcomm_session_msg lsm;
64226865 900 char *varlen_data;
93deb080 901 int ret = 0;
137b9942 902 unsigned int free_filter_expression = 0;
93deb080 903 struct filter_parser_ctx *ctx = NULL;
e9efbcd3
JG
904 /*
905 * Cast as non-const since we may replace the filter expression
906 * by a dynamically allocated string. Otherwise, the original
907 * string is not modified.
908 */
909 char *filter_expression = (char *) original_filter_expression;
93deb080
JI
910
911 if (handle == NULL || ev == NULL) {
137b9942
DG
912 ret = -LTTNG_ERR_INVALID;
913 goto error;
93deb080
JI
914 }
915
9ae110e2
JG
916 /*
917 * Empty filter string will always be rejected by the parser
93deb080
JI
918 * anyway, so treat this corner-case early to eliminate
919 * lttng_fmemopen error for 0-byte allocation.
920 */
921 if (filter_expression && filter_expression[0] == '\0') {
137b9942
DG
922 ret = -LTTNG_ERR_INVALID;
923 goto error;
93deb080
JI
924 }
925
926 memset(&lsm, 0, sizeof(lsm));
927
928 /* If no channel name, send empty string. */
929 if (channel_name == NULL) {
930 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
931 sizeof(lsm.u.enable.channel_name));
932 } else {
933 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
934 sizeof(lsm.u.enable.channel_name));
935 }
936
18a720cd
MD
937 lsm.cmd_type = LTTNG_ENABLE_EVENT;
938 if (ev->name[0] == '\0') {
939 /* Enable all events */
940 lttng_ctl_copy_string(ev->name, "*", sizeof(ev->name));
6565421f 941 }
93deb080 942
6565421f 943 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
6e911cad 944 /* FIXME: copying non-packed struct to packed struct. */
93deb080
JI
945 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
946
947 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
948 sizeof(lsm.session.name));
949 lsm.u.enable.exclusion_count = exclusion_count;
950 lsm.u.enable.bytecode_len = 0;
951
9b21e6d5
DG
952 /*
953 * For the JUL domain, a filter is enforced except for the enable all
954 * event. This is done to avoid having the event in all sessions thus
955 * filtering by logger name.
956 */
957 if (exclusion_count == 0 && filter_expression == NULL &&
5cdb6027 958 (handle->domain.type != LTTNG_DOMAIN_JUL &&
0e115563
DG
959 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
960 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
3e1c9ff7 961 goto ask_sessiond;
93deb080
JI
962 }
963
964 /*
965 * We have either a filter or some exclusions, so we need to set up
9ae110e2 966 * a variable-length memory block from where to send the data.
93deb080
JI
967 */
968
9ae110e2 969 /* Parse filter expression. */
5cdb6027 970 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
971 || handle->domain.type == LTTNG_DOMAIN_LOG4J
972 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
5cdb6027 973 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
974 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
975 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
976 char *agent_filter;
64226865 977
347c5ab5 978 /* Setup JUL filter if needed. */
0e115563
DG
979 agent_filter = set_agent_filter(filter_expression, ev);
980 if (!agent_filter) {
137b9942 981 if (!filter_expression) {
9ae110e2
JG
982 /*
983 * No JUL and no filter, just skip
984 * everything below.
985 */
137b9942
DG
986 goto ask_sessiond;
987 }
64226865
DG
988 } else {
989 /*
9ae110e2
JG
990 * With an agent filter, the original filter has
991 * been added to it thus replace the filter
992 * expression.
64226865 993 */
0e115563 994 filter_expression = agent_filter;
e9efbcd3 995 free_filter_expression = 1;
9b21e6d5 996 }
9b21e6d5 997 }
93deb080 998
137b9942 999 ret = generate_filter(filter_expression, &lsm, &ctx);
93deb080 1000 if (ret) {
137b9942 1001 goto filter_error;
93deb080 1002 }
6b453b5e
JG
1003 }
1004
1005 varlen_data = zmalloc(lsm.u.enable.bytecode_len
137b9942
DG
1006 + lsm.u.enable.expression_len
1007 + LTTNG_SYMBOL_NAME_LEN * exclusion_count);
6b453b5e
JG
1008 if (!varlen_data) {
1009 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
7ca1dc6f 1010 goto mem_error;
6b453b5e 1011 }
137b9942 1012
9ae110e2 1013 /* Put exclusion names first in the data. */
6b453b5e
JG
1014 while (exclusion_count--) {
1015 strncpy(varlen_data + LTTNG_SYMBOL_NAME_LEN * exclusion_count,
0c82ac62
PP
1016 *(exclusion_list + exclusion_count),
1017 LTTNG_SYMBOL_NAME_LEN - 1);
6b453b5e 1018 }
9ae110e2 1019 /* Add filter expression next. */
6b453b5e
JG
1020 if (lsm.u.enable.expression_len != 0) {
1021 memcpy(varlen_data
1022 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count,
1023 filter_expression,
1024 lsm.u.enable.expression_len);
1025 }
9ae110e2 1026 /* Add filter bytecode next. */
137b9942 1027 if (ctx && lsm.u.enable.bytecode_len != 0) {
6b453b5e
JG
1028 memcpy(varlen_data
1029 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count
1030 + lsm.u.enable.expression_len,
1031 &ctx->bytecode->b,
1032 lsm.u.enable.bytecode_len);
93deb080
JI
1033 }
1034
1035 ret = lttng_ctl_ask_sessiond_varlen(&lsm, varlen_data,
67676bd8 1036 (LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count) +
9ae110e2
JG
1037 lsm.u.enable.bytecode_len + lsm.u.enable.expression_len,
1038 NULL);
6b453b5e 1039 free(varlen_data);
93deb080 1040
7ca1dc6f
DG
1041mem_error:
1042 if (filter_expression && ctx) {
93deb080
JI
1043 filter_bytecode_free(ctx);
1044 filter_ir_free(ctx);
1045 filter_parser_ctx_free(ctx);
7ca1dc6f
DG
1046 }
1047filter_error:
1048 if (free_filter_expression) {
1049 /*
9ae110e2
JG
1050 * The filter expression has been replaced and must be freed as
1051 * it is not the original filter expression received as a
1052 * parameter.
7ca1dc6f
DG
1053 */
1054 free(filter_expression);
93deb080 1055 }
137b9942
DG
1056error:
1057 /*
9ae110e2
JG
1058 * Return directly to the caller and don't ask the sessiond since
1059 * something went wrong in the parsing of data above.
137b9942 1060 */
93deb080 1061 return ret;
3e1c9ff7
DG
1062
1063ask_sessiond:
1064 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1065 return ret;
93deb080
JI
1066}
1067
6e911cad
MD
1068int lttng_disable_event_ext(struct lttng_handle *handle,
1069 struct lttng_event *ev, const char *channel_name,
1070 const char *original_filter_expression)
1df4dedd 1071{
cd80958d 1072 struct lttcomm_session_msg lsm;
6e911cad
MD
1073 char *varlen_data;
1074 int ret = 0;
1075 unsigned int free_filter_expression = 0;
1076 struct filter_parser_ctx *ctx = NULL;
1077 /*
1078 * Cast as non-const since we may replace the filter expression
1079 * by a dynamically allocated string. Otherwise, the original
1080 * string is not modified.
1081 */
1082 char *filter_expression = (char *) original_filter_expression;
1df4dedd 1083
6e911cad
MD
1084 if (handle == NULL || ev == NULL) {
1085 ret = -LTTNG_ERR_INVALID;
1086 goto error;
1087 }
1088
9ae110e2
JG
1089 /*
1090 * Empty filter string will always be rejected by the parser
6e911cad
MD
1091 * anyway, so treat this corner-case early to eliminate
1092 * lttng_fmemopen error for 0-byte allocation.
1093 */
1094 if (filter_expression && filter_expression[0] == '\0') {
1095 ret = -LTTNG_ERR_INVALID;
1096 goto error;
cd80958d
DG
1097 }
1098
441c16a7
MD
1099 memset(&lsm, 0, sizeof(lsm));
1100
85076754
MD
1101 /* If no channel name, send empty string. */
1102 if (channel_name == NULL) {
1103 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
cd80958d 1104 sizeof(lsm.u.disable.channel_name));
f3ed775e 1105 } else {
85076754 1106 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
cd80958d 1107 sizeof(lsm.u.disable.channel_name));
eb354453
DG
1108 }
1109
18a720cd 1110 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f3ed775e 1111
6e911cad
MD
1112 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1113 /* FIXME: copying non-packed struct to packed struct. */
1114 memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event));
1115
cac3069d 1116 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1117 sizeof(lsm.session.name));
6e911cad 1118 lsm.u.disable.bytecode_len = 0;
cd80958d 1119
6e911cad
MD
1120 /*
1121 * For the JUL domain, a filter is enforced except for the
1122 * disable all event. This is done to avoid having the event in
1123 * all sessions thus filtering by logger name.
1124 */
1125 if (filter_expression == NULL &&
1126 (handle->domain.type != LTTNG_DOMAIN_JUL &&
0e115563
DG
1127 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1128 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
6e911cad
MD
1129 goto ask_sessiond;
1130 }
1131
1132 /*
1133 * We have a filter, so we need to set up a variable-length
1134 * memory block from where to send the data.
1135 */
1136
1137 /* Parse filter expression */
1138 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1139 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1140 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
6e911cad 1141 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1142 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1143 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1144 char *agent_filter;
6e911cad
MD
1145
1146 /* Setup JUL filter if needed. */
0e115563
DG
1147 agent_filter = set_agent_filter(filter_expression, ev);
1148 if (!agent_filter) {
6e911cad 1149 if (!filter_expression) {
9ae110e2
JG
1150 /*
1151 * No JUL and no filter, just skip
1152 * everything below.
1153 */
6e911cad
MD
1154 goto ask_sessiond;
1155 }
1156 } else {
1157 /*
9ae110e2
JG
1158 * With a JUL filter, the original filter has
1159 * been added to it thus replace the filter
1160 * expression.
6e911cad 1161 */
0e115563 1162 filter_expression = agent_filter;
6e911cad
MD
1163 free_filter_expression = 1;
1164 }
1165 }
1166
1167 ret = generate_filter(filter_expression, &lsm, &ctx);
1168 if (ret) {
1169 goto filter_error;
1170 }
1171 }
1172
1173 varlen_data = zmalloc(lsm.u.disable.bytecode_len
1174 + lsm.u.disable.expression_len);
1175 if (!varlen_data) {
1176 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1177 goto mem_error;
1178 }
1179
9ae110e2 1180 /* Add filter expression. */
6e911cad
MD
1181 if (lsm.u.disable.expression_len != 0) {
1182 memcpy(varlen_data,
1183 filter_expression,
1184 lsm.u.disable.expression_len);
1185 }
9ae110e2 1186 /* Add filter bytecode next. */
6e911cad
MD
1187 if (ctx && lsm.u.disable.bytecode_len != 0) {
1188 memcpy(varlen_data
1189 + lsm.u.disable.expression_len,
1190 &ctx->bytecode->b,
1191 lsm.u.disable.bytecode_len);
1192 }
1193
1194 ret = lttng_ctl_ask_sessiond_varlen(&lsm, varlen_data,
1195 lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
1196 free(varlen_data);
1197
1198mem_error:
1199 if (filter_expression && ctx) {
1200 filter_bytecode_free(ctx);
1201 filter_ir_free(ctx);
1202 filter_parser_ctx_free(ctx);
1203 }
1204filter_error:
1205 if (free_filter_expression) {
1206 /*
9ae110e2
JG
1207 * The filter expression has been replaced and must be freed as
1208 * it is not the original filter expression received as a
1209 * parameter.
6e911cad
MD
1210 */
1211 free(filter_expression);
1212 }
1213error:
1214 /*
9ae110e2
JG
1215 * Return directly to the caller and don't ask the sessiond since
1216 * something went wrong in the parsing of data above.
6e911cad
MD
1217 */
1218 return ret;
1219
1220ask_sessiond:
1221 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1222 return ret;
1223}
1224
1225/*
9ae110e2
JG
1226 * Disable event(s) of a channel and domain.
1227 * If no event name is specified, all events are disabled.
1228 * If no channel name is specified, the default 'channel0' is used.
1229 * Returns size of returned session payload data or a negative error code.
6e911cad
MD
1230 */
1231int lttng_disable_event(struct lttng_handle *handle, const char *name,
1232 const char *channel_name)
1233{
1234 struct lttng_event ev;
1235
1236 memset(&ev, 0, sizeof(ev));
9b7431cf 1237 ev.loglevel = -1;
6e911cad
MD
1238 ev.type = LTTNG_EVENT_ALL;
1239 lttng_ctl_copy_string(ev.name, name, sizeof(ev.name));
1240 return lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1df4dedd
DG
1241}
1242
1243/*
9ae110e2
JG
1244 * Enable channel per domain
1245 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 1246 */
cd80958d 1247int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 1248 struct lttng_channel *chan)
a5c5a2bd 1249{
cd80958d
DG
1250 struct lttcomm_session_msg lsm;
1251
9ae110e2 1252 /* NULL arguments are forbidden. No default values. */
5117eeec 1253 if (handle == NULL || chan == NULL) {
2f70b271 1254 return -LTTNG_ERR_INVALID;
cd80958d
DG
1255 }
1256
441c16a7
MD
1257 memset(&lsm, 0, sizeof(lsm));
1258
5117eeec 1259 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 1260
cd80958d
DG
1261 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1262
cac3069d 1263 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 1264
cac3069d 1265 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1266 sizeof(lsm.session.name));
1267
cac3069d 1268 return lttng_ctl_ask_sessiond(&lsm, NULL);
8c0faa1d 1269}
1df4dedd 1270
2ef84c95 1271/*
9ae110e2
JG
1272 * All tracing will be stopped for registered events of the channel.
1273 * Returns size of returned session payload data or a negative error code.
2ef84c95 1274 */
cd80958d 1275int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 1276{
cd80958d
DG
1277 struct lttcomm_session_msg lsm;
1278
9ae110e2 1279 /* Safety check. Both are mandatory. */
9d697d3d 1280 if (handle == NULL || name == NULL) {
2f70b271 1281 return -LTTNG_ERR_INVALID;
cd80958d
DG
1282 }
1283
441c16a7
MD
1284 memset(&lsm, 0, sizeof(lsm));
1285
cd80958d 1286 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 1287
cac3069d 1288 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
9d697d3d
DG
1289 sizeof(lsm.u.disable.channel_name));
1290
cac3069d 1291 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cd80958d 1292
cac3069d 1293 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1294 sizeof(lsm.session.name));
1295
cac3069d 1296 return lttng_ctl_ask_sessiond(&lsm, NULL);
ca95a216
DG
1297}
1298
ccf10263 1299/*
9ae110e2
JG
1300 * Add PID to session tracker.
1301 * Return 0 on success else a negative LTTng error code.
ccf10263
MD
1302 */
1303int lttng_track_pid(struct lttng_handle *handle, int pid)
1304{
1305 struct lttcomm_session_msg lsm;
1306
9ae110e2 1307 /* NULL arguments are forbidden. No default values. */
ccf10263
MD
1308 if (handle == NULL) {
1309 return -LTTNG_ERR_INVALID;
1310 }
1311
1312 memset(&lsm, 0, sizeof(lsm));
1313
1314 lsm.cmd_type = LTTNG_TRACK_PID;
1315 lsm.u.pid_tracker.pid = pid;
1316
1317 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1318
1319 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1320 sizeof(lsm.session.name));
1321
1322 return lttng_ctl_ask_sessiond(&lsm, NULL);
1323}
1324
1325/*
9ae110e2
JG
1326 * Remove PID from session tracker.
1327 * Return 0 on success else a negative LTTng error code.
ccf10263
MD
1328 */
1329int lttng_untrack_pid(struct lttng_handle *handle, int pid)
1330{
1331 struct lttcomm_session_msg lsm;
1332
9ae110e2 1333 /* NULL arguments are forbidden. No default values. */
ccf10263
MD
1334 if (handle == NULL) {
1335 return -LTTNG_ERR_INVALID;
1336 }
1337
1338 memset(&lsm, 0, sizeof(lsm));
1339
1340 lsm.cmd_type = LTTNG_UNTRACK_PID;
1341 lsm.u.pid_tracker.pid = pid;
1342
1343 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1344
1345 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1346 sizeof(lsm.session.name));
1347
1348 return lttng_ctl_ask_sessiond(&lsm, NULL);
1349}
1350
fac6795d 1351/*
9ae110e2
JG
1352 * Lists all available tracepoints of domain.
1353 * Sets the contents of the events array.
1354 * Returns the number of lttng_event entries in events;
1355 * on error, returns a negative value.
fac6795d 1356 */
cd80958d 1357int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 1358 struct lttng_event **events)
fac6795d 1359{
052da939 1360 int ret;
cd80958d
DG
1361 struct lttcomm_session_msg lsm;
1362
9d697d3d 1363 if (handle == NULL) {
2f70b271 1364 return -LTTNG_ERR_INVALID;
cd80958d 1365 }
fac6795d 1366
53efb85a 1367 memset(&lsm, 0, sizeof(lsm));
cd80958d 1368 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
cac3069d 1369 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 1370
cac3069d 1371 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
1372 if (ret < 0) {
1373 return ret;
eb354453 1374 }
fac6795d 1375
9f19cc17 1376 return ret / sizeof(struct lttng_event);
fac6795d
DG
1377}
1378
f37d259d 1379/*
9ae110e2
JG
1380 * Lists all available tracepoint fields of domain.
1381 * Sets the contents of the event field array.
1382 * Returns the number of lttng_event_field entries in events;
1383 * on error, returns a negative value.
f37d259d
MD
1384 */
1385int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1386 struct lttng_event_field **fields)
1387{
1388 int ret;
1389 struct lttcomm_session_msg lsm;
1390
1391 if (handle == NULL) {
2f70b271 1392 return -LTTNG_ERR_INVALID;
f37d259d
MD
1393 }
1394
53efb85a 1395 memset(&lsm, 0, sizeof(lsm));
f37d259d 1396 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
cac3069d 1397 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f37d259d 1398
cac3069d 1399 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
1400 if (ret < 0) {
1401 return ret;
1402 }
1403
1404 return ret / sizeof(struct lttng_event_field);
1405}
1406
834978fd 1407/*
9ae110e2
JG
1408 * Lists all available kernel system calls. Allocates and sets the contents of
1409 * the events array.
834978fd 1410 *
9ae110e2
JG
1411 * Returns the number of lttng_event entries in events; on error, returns a
1412 * negative value.
834978fd
DG
1413 */
1414int lttng_list_syscalls(struct lttng_event **events)
1415{
1416 int ret;
1417 struct lttcomm_session_msg lsm;
1418
1419 if (!events) {
1420 return -LTTNG_ERR_INVALID;
1421 }
1422
1423 memset(&lsm, 0, sizeof(lsm));
1424 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1425 /* Force kernel domain for system calls. */
1426 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1427
1428 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1429 if (ret < 0) {
1430 return ret;
1431 }
1432
1433 return ret / sizeof(struct lttng_event);
1434}
1435
1657e9bb 1436/*
9ae110e2
JG
1437 * Returns a human readable string describing
1438 * the error code (a negative value).
1657e9bb 1439 */
9a745bc7 1440const char *lttng_strerror(int code)
1657e9bb 1441{
f73fabfd 1442 return error_get_str(code);
1657e9bb
DG
1443}
1444
aaf97519 1445/*
a4b92340
DG
1446 * Create a brand new session using name and url for destination.
1447 *
f73fabfd 1448 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1449 */
a4b92340 1450int lttng_create_session(const char *name, const char *url)
00e2e675 1451{
3dd05a85 1452 int ret;
a4b92340 1453 ssize_t size;
00e2e675 1454 struct lttcomm_session_msg lsm;
a4b92340 1455 struct lttng_uri *uris = NULL;
00e2e675 1456
a4b92340 1457 if (name == NULL) {
2f70b271 1458 return -LTTNG_ERR_INVALID;
00e2e675
DG
1459 }
1460
a4b92340 1461 memset(&lsm, 0, sizeof(lsm));
00e2e675 1462
a4b92340 1463 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1464 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1465
1466 /* There should never be a data URL */
bc894455 1467 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1468 if (size < 0) {
2f70b271 1469 return -LTTNG_ERR_INVALID;
00e2e675
DG
1470 }
1471
a4b92340
DG
1472 lsm.u.uri.size = size;
1473
cac3069d
DG
1474 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1475 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1476
1477 free(uris);
1478 return ret;
00e2e675
DG
1479}
1480
8028d920 1481/*
9ae110e2
JG
1482 * Destroy session using name.
1483 * Returns size of returned session payload data or a negative error code.
8028d920 1484 */
843f5df9 1485int lttng_destroy_session(const char *session_name)
8028d920 1486{
cd80958d
DG
1487 struct lttcomm_session_msg lsm;
1488
843f5df9 1489 if (session_name == NULL) {
2f70b271 1490 return -LTTNG_ERR_INVALID;
cd80958d
DG
1491 }
1492
53efb85a 1493 memset(&lsm, 0, sizeof(lsm));
cd80958d 1494 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9 1495
cac3069d
DG
1496 lttng_ctl_copy_string(lsm.session.name, session_name,
1497 sizeof(lsm.session.name));
cd80958d 1498
cac3069d 1499 return lttng_ctl_ask_sessiond(&lsm, NULL);
aaf97519
DG
1500}
1501
57167058 1502/*
9ae110e2
JG
1503 * Ask the session daemon for all available sessions.
1504 * Sets the contents of the sessions array.
1505 * Returns the number of lttng_session entries in sessions;
1506 * on error, returns a negative value.
57167058 1507 */
ca95a216 1508int lttng_list_sessions(struct lttng_session **sessions)
57167058 1509{
ca95a216 1510 int ret;
cd80958d 1511 struct lttcomm_session_msg lsm;
57167058 1512
53efb85a 1513 memset(&lsm, 0, sizeof(lsm));
cd80958d 1514 lsm.cmd_type = LTTNG_LIST_SESSIONS;
cac3069d 1515 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
57167058 1516 if (ret < 0) {
ca95a216 1517 return ret;
57167058
DG
1518 }
1519
ca95a216 1520 return ret / sizeof(struct lttng_session);
57167058
DG
1521}
1522
d7ba1388
MD
1523int lttng_set_session_shm_path(const char *session_name,
1524 const char *shm_path)
1525{
1526 struct lttcomm_session_msg lsm;
1527
1528 if (session_name == NULL) {
1529 return -LTTNG_ERR_INVALID;
1530 }
1531
1532 memset(&lsm, 0, sizeof(lsm));
1533 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
1534
1535 lttng_ctl_copy_string(lsm.session.name, session_name,
1536 sizeof(lsm.session.name));
1537 lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path,
1538 sizeof(lsm.u.set_shm_path.shm_path));
1539
1540 return lttng_ctl_ask_sessiond(&lsm, NULL);
1541}
1542
9f19cc17 1543/*
9ae110e2
JG
1544 * Ask the session daemon for all available domains of a session.
1545 * Sets the contents of the domains array.
1546 * Returns the number of lttng_domain entries in domains;
1547 * on error, returns a negative value.
9f19cc17 1548 */
330be774 1549int lttng_list_domains(const char *session_name,
cd80958d 1550 struct lttng_domain **domains)
9f19cc17
DG
1551{
1552 int ret;
cd80958d
DG
1553 struct lttcomm_session_msg lsm;
1554
330be774 1555 if (session_name == NULL) {
2f70b271 1556 return -LTTNG_ERR_INVALID;
cd80958d 1557 }
9f19cc17 1558
53efb85a 1559 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
1560 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1561
cac3069d
DG
1562 lttng_ctl_copy_string(lsm.session.name, session_name,
1563 sizeof(lsm.session.name));
cd80958d 1564
cac3069d 1565 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1566 if (ret < 0) {
1567 return ret;
1568 }
1569
1570 return ret / sizeof(struct lttng_domain);
1571}
1572
1573/*
9ae110e2
JG
1574 * Ask the session daemon for all available channels of a session.
1575 * Sets the contents of the channels array.
1576 * Returns the number of lttng_channel entries in channels;
1577 * on error, returns a negative value.
9f19cc17 1578 */
cd80958d
DG
1579int lttng_list_channels(struct lttng_handle *handle,
1580 struct lttng_channel **channels)
9f19cc17
DG
1581{
1582 int ret;
cd80958d
DG
1583 struct lttcomm_session_msg lsm;
1584
9d697d3d 1585 if (handle == NULL) {
2f70b271 1586 return -LTTNG_ERR_INVALID;
cd80958d
DG
1587 }
1588
53efb85a 1589 memset(&lsm, 0, sizeof(lsm));
cd80958d 1590 lsm.cmd_type = LTTNG_LIST_CHANNELS;
cac3069d 1591 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1592 sizeof(lsm.session.name));
9f19cc17 1593
cac3069d 1594 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1595
cac3069d 1596 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
1597 if (ret < 0) {
1598 return ret;
1599 }
1600
1601 return ret / sizeof(struct lttng_channel);
1602}
1603
1604/*
9ae110e2
JG
1605 * Ask the session daemon for all available events of a session channel.
1606 * Sets the contents of the events array.
1607 * Returns the number of lttng_event entries in events;
1608 * on error, returns a negative value.
9f19cc17 1609 */
cd80958d
DG
1610int lttng_list_events(struct lttng_handle *handle,
1611 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1612{
1613 int ret;
cd80958d 1614 struct lttcomm_session_msg lsm;
9f19cc17 1615
9d697d3d
DG
1616 /* Safety check. An handle and channel name are mandatory */
1617 if (handle == NULL || channel_name == NULL) {
2f70b271 1618 return -LTTNG_ERR_INVALID;
cd80958d
DG
1619 }
1620
53efb85a 1621 memset(&lsm, 0, sizeof(lsm));
cd80958d 1622 lsm.cmd_type = LTTNG_LIST_EVENTS;
cac3069d 1623 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1624 sizeof(lsm.session.name));
cac3069d 1625 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
cd80958d
DG
1626 sizeof(lsm.u.list.channel_name));
1627
cac3069d 1628 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1629
cac3069d 1630 ret = lttng_ctl_ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
1631 if (ret < 0) {
1632 return ret;
1633 }
1634
1635 return ret / sizeof(struct lttng_event);
1636}
1637
fac6795d 1638/*
1c8d13c8
TD
1639 * Sets the tracing_group variable with name.
1640 * This function allocates memory pointed to by tracing_group.
1641 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
1642 */
1643int lttng_set_tracing_group(const char *name)
1644{
9d697d3d 1645 if (name == NULL) {
2f70b271 1646 return -LTTNG_ERR_INVALID;
9d697d3d
DG
1647 }
1648
fac6795d 1649 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 1650 return -LTTNG_ERR_FATAL;
fac6795d
DG
1651 }
1652
1653 return 0;
1654}
1655
d0254c7c 1656/*
af87c45a 1657 * Returns size of returned session payload data or a negative error code.
d0254c7c 1658 */
cd80958d 1659int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
1660 struct lttng_calibrate *calibrate)
1661{
cd80958d 1662 struct lttcomm_session_msg lsm;
d0254c7c 1663
9d697d3d
DG
1664 /* Safety check. NULL pointer are forbidden */
1665 if (handle == NULL || calibrate == NULL) {
2f70b271 1666 return -LTTNG_ERR_INVALID;
cd80958d 1667 }
d0254c7c 1668
53efb85a 1669 memset(&lsm, 0, sizeof(lsm));
cd80958d 1670 lsm.cmd_type = LTTNG_CALIBRATE;
cac3069d 1671 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 1672
cd80958d
DG
1673 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1674
cac3069d 1675 return lttng_ctl_ask_sessiond(&lsm, NULL);
d0254c7c
MD
1676}
1677
5edd7e09
DG
1678/*
1679 * Set default channel attributes.
441c16a7 1680 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
1681 */
1682void lttng_channel_set_default_attr(struct lttng_domain *domain,
1683 struct lttng_channel_attr *attr)
1684{
1685 /* Safety check */
1686 if (attr == NULL || domain == NULL) {
1687 return;
1688 }
1689
eacaa7a6
DS
1690 memset(attr, 0, sizeof(struct lttng_channel_attr));
1691
0a9c6494
DG
1692 /* Same for all domains. */
1693 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1694 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
1695 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
1696
5edd7e09
DG
1697 switch (domain->type) {
1698 case LTTNG_DOMAIN_KERNEL:
9ae110e2
JG
1699 attr->switch_timer_interval =
1700 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
d92ff3ef 1701 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 1702 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
1703 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1704 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1705 break;
1706 case LTTNG_DOMAIN_UST:
0a9c6494
DG
1707 switch (domain->buf_type) {
1708 case LTTNG_BUFFER_PER_UID:
1709 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
1710 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
1711 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
9ae110e2
JG
1712 attr->switch_timer_interval =
1713 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
1714 attr->read_timer_interval =
1715 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
0a9c6494
DG
1716 break;
1717 case LTTNG_BUFFER_PER_PID:
1718 default:
1719 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
1720 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
1721 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
9ae110e2
JG
1722 attr->switch_timer_interval =
1723 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
1724 attr->read_timer_interval =
1725 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
0a9c6494
DG
1726 break;
1727 }
5edd7e09 1728 default:
441c16a7 1729 /* Default behavior: leave set to 0. */
5edd7e09
DG
1730 break;
1731 }
1732}
1733
fac6795d 1734/*
2269e89e 1735 * Check if session daemon is alive.
fac6795d 1736 *
2269e89e 1737 * Return 1 if alive or 0 if not.
1c8d13c8 1738 * On error returns a negative value.
fac6795d 1739 */
947308c4 1740int lttng_session_daemon_alive(void)
fac6795d
DG
1741{
1742 int ret;
1743
1744 ret = set_session_daemon_path();
1745 if (ret < 0) {
9ae110e2 1746 /* Error. */
fac6795d
DG
1747 return ret;
1748 }
1749
9d035200 1750 if (*sessiond_sock_path == '\0') {
2f70b271 1751 /*
9ae110e2
JG
1752 * No socket path set. Weird error which means the constructor
1753 * was not called.
2f70b271
DG
1754 */
1755 assert(0);
fac6795d
DG
1756 }
1757
2269e89e 1758 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9 1759 if (ret < 0) {
9ae110e2 1760 /* Not alive. */
7d8234d9
MD
1761 return 0;
1762 }
7d8234d9 1763
9ae110e2 1764 /* Is alive. */
947308c4 1765 return 1;
fac6795d
DG
1766}
1767
00e2e675 1768/*
a4b92340 1769 * Set URL for a consumer for a session and domain.
00e2e675
DG
1770 *
1771 * Return 0 on success, else a negative value.
1772 */
a4b92340
DG
1773int lttng_set_consumer_url(struct lttng_handle *handle,
1774 const char *control_url, const char *data_url)
00e2e675 1775{
3dd05a85 1776 int ret;
a4b92340 1777 ssize_t size;
00e2e675 1778 struct lttcomm_session_msg lsm;
a4b92340 1779 struct lttng_uri *uris = NULL;
00e2e675 1780
a4b92340 1781 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 1782 return -LTTNG_ERR_INVALID;
00e2e675
DG
1783 }
1784
a4b92340
DG
1785 memset(&lsm, 0, sizeof(lsm));
1786
00e2e675
DG
1787 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1788
cac3069d 1789 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
00e2e675 1790 sizeof(lsm.session.name));
cac3069d 1791 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
00e2e675 1792
bc894455 1793 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 1794 if (size < 0) {
2f70b271 1795 return -LTTNG_ERR_INVALID;
a4b92340
DG
1796 }
1797
1798 lsm.u.uri.size = size;
00e2e675 1799
cac3069d
DG
1800 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1801 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1802
1803 free(uris);
1804 return ret;
00e2e675
DG
1805}
1806
1807/*
9c6bda17 1808 * [OBSOLETE]
00e2e675
DG
1809 */
1810int lttng_enable_consumer(struct lttng_handle *handle)
1811{
785d2d0d 1812 return -ENOSYS;
00e2e675
DG
1813}
1814
1815/*
9c6bda17 1816 * [OBSOLETE]
00e2e675
DG
1817 */
1818int lttng_disable_consumer(struct lttng_handle *handle)
1819{
785d2d0d 1820 return -ENOSYS;
00e2e675
DG
1821}
1822
07424f16
DG
1823/*
1824 * This is an extension of create session that is ONLY and SHOULD only be used
1825 * by the lttng command line program. It exists to avoid using URI parsing in
1826 * the lttng client.
1827 *
1828 * We need the date and time for the trace path subdirectory for the case where
1829 * the user does NOT define one using either -o or -U. Using the normal
1830 * lttng_create_session API call, we have no clue on the session daemon side if
1831 * the URL was generated automatically by the client or define by the user.
1832 *
1833 * So this function "wrapper" is hidden from the public API, takes the datetime
1834 * string and appends it if necessary to the URI subdirectory before sending it
1835 * to the session daemon.
1836 *
1837 * With this extra function, the lttng_create_session call behavior is not
1838 * changed and the timestamp is appended to the URI on the session daemon side
1839 * if necessary.
1840 */
1841int _lttng_create_session_ext(const char *name, const char *url,
1842 const char *datetime)
1843{
3dd05a85 1844 int ret;
07424f16
DG
1845 ssize_t size;
1846 struct lttcomm_session_msg lsm;
1847 struct lttng_uri *uris = NULL;
1848
2bba9e53 1849 if (name == NULL || datetime == NULL) {
2f70b271 1850 return -LTTNG_ERR_INVALID;
07424f16
DG
1851 }
1852
1853 memset(&lsm, 0, sizeof(lsm));
1854
1855 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1856 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
07424f16 1857
9ae110e2 1858 /* There should never be a data URL. */
bc894455 1859 size = uri_parse_str_urls(url, NULL, &uris);
07424f16 1860 if (size < 0) {
3dd05a85
DG
1861 ret = -LTTNG_ERR_INVALID;
1862 goto error;
07424f16
DG
1863 }
1864
1865 lsm.u.uri.size = size;
1866
4b35a6b3 1867 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
da4aa2b8
DG
1868 /* Don't append datetime if the name was automatically created. */
1869 if (strncmp(name, DEFAULT_SESSION_NAME "-",
1870 strlen(DEFAULT_SESSION_NAME) + 1)) {
1871 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
1872 name, datetime);
1873 } else {
1874 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
1875 }
07424f16
DG
1876 if (ret < 0) {
1877 PERROR("snprintf uri subdir");
3dd05a85
DG
1878 ret = -LTTNG_ERR_FATAL;
1879 goto error;
07424f16
DG
1880 }
1881 }
1882
cac3069d
DG
1883 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1884 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1885
1886error:
1887 free(uris);
1888 return ret;
07424f16
DG
1889}
1890
806e2684
DG
1891/*
1892 * For a given session name, this call checks if the data is ready to be read
1893 * or is still being extracted by the consumer(s) hence not ready to be used by
1894 * any readers.
1895 */
6d805429 1896int lttng_data_pending(const char *session_name)
806e2684
DG
1897{
1898 int ret;
1899 struct lttcomm_session_msg lsm;
f6151c55 1900 uint8_t *pending = NULL;
806e2684
DG
1901
1902 if (session_name == NULL) {
1903 return -LTTNG_ERR_INVALID;
1904 }
1905
53efb85a 1906 memset(&lsm, 0, sizeof(lsm));
6d805429 1907 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 1908
cac3069d
DG
1909 lttng_ctl_copy_string(lsm.session.name, session_name,
1910 sizeof(lsm.session.name));
806e2684 1911
f6151c55
JG
1912 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
1913 if (ret < 0) {
1914 goto end;
1915 } else if (ret != 1) {
1916 /* Unexpected payload size */
1917 ret = -LTTNG_ERR_INVALID;
1918 goto end;
806e2684
DG
1919 }
1920
f6151c55
JG
1921 ret = (int) *pending;
1922end:
1923 free(pending);
806e2684
DG
1924 return ret;
1925}
1926
27babd3a
DG
1927/*
1928 * Create a session exclusively used for snapshot.
1929 *
1930 * Returns LTTNG_OK on success or a negative error code.
1931 */
1932int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
1933{
1934 int ret;
1935 ssize_t size;
1936 struct lttcomm_session_msg lsm;
1937 struct lttng_uri *uris = NULL;
1938
1939 if (name == NULL) {
1940 return -LTTNG_ERR_INVALID;
1941 }
1942
1943 memset(&lsm, 0, sizeof(lsm));
1944
1945 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
1946 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1947
1948 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
1949 if (size < 0) {
1950 return -LTTNG_ERR_INVALID;
1951 }
1952
1953 lsm.u.uri.size = size;
1954
1955 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1956 sizeof(struct lttng_uri) * size, NULL);
1957
1958 free(uris);
1959 return ret;
1960}
1961
ecc48a90
JD
1962/*
1963 * Create a session exclusively used for live.
1964 *
1965 * Returns LTTNG_OK on success or a negative error code.
1966 */
1967int lttng_create_session_live(const char *name, const char *url,
1968 unsigned int timer_interval)
1969{
1970 int ret;
1971 ssize_t size;
1972 struct lttcomm_session_msg lsm;
1973 struct lttng_uri *uris = NULL;
1974
92805ee4 1975 if (name == NULL || timer_interval == 0) {
ecc48a90
JD
1976 return -LTTNG_ERR_INVALID;
1977 }
1978
1979 memset(&lsm, 0, sizeof(lsm));
1980
1981 lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE;
1982 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1983
1a241656
DG
1984 if (url) {
1985 size = uri_parse_str_urls(url, NULL, &uris);
1986 if (size <= 0) {
1987 ret = -LTTNG_ERR_INVALID;
1988 goto end;
1989 }
ecc48a90 1990
1a241656
DG
1991 /* file:// is not accepted for live session. */
1992 if (uris[0].dtype == LTTNG_DST_PATH) {
1993 ret = -LTTNG_ERR_INVALID;
1994 goto end;
1995 }
1996 } else {
1997 size = 0;
ecc48a90
JD
1998 }
1999
2000 lsm.u.session_live.nb_uri = size;
2001 lsm.u.session_live.timer_interval = timer_interval;
2002
2003 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
2004 sizeof(struct lttng_uri) * size, NULL);
2005
2006end:
2007 free(uris);
2008 return ret;
2009}
2010
a5dfbb9d
MD
2011/*
2012 * List PIDs in the tracker.
2013 *
1b18ce93
JG
2014 * enabled is set to whether the PID tracker is enabled.
2015 * pids is set to an allocated array of PIDs currently tracked. On
2016 * success, pids must be freed by the caller.
2017 * nr_pids is set to the number of entries contained by the pids array.
a5dfbb9d
MD
2018 *
2019 * Returns 0 on success, else a negative LTTng error code.
2020 */
2021int lttng_list_tracker_pids(struct lttng_handle *handle,
2022 int *_enabled, int32_t **_pids, size_t *_nr_pids)
2023{
ebbf5ab7
JR
2024 int ret;
2025 int enabled = 1;
a5dfbb9d
MD
2026 struct lttcomm_session_msg lsm;
2027 size_t nr_pids;
2028 int32_t *pids;
2029
2030 if (handle == NULL) {
2031 return -LTTNG_ERR_INVALID;
2032 }
2033
2034 memset(&lsm, 0, sizeof(lsm));
2035 lsm.cmd_type = LTTNG_LIST_TRACKER_PIDS;
2036 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2037 sizeof(lsm.session.name));
2038 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2039
2040 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pids);
2041 if (ret < 0) {
2042 return ret;
2043 }
2044 nr_pids = ret / sizeof(int32_t);
2045 if (nr_pids == 1 && pids[0] == -1) {
2046 free(pids);
2047 pids = NULL;
2048 enabled = 0;
2049 nr_pids = 0;
2050 }
2051 *_enabled = enabled;
2052 *_pids = pids;
2053 *_nr_pids = nr_pids;
2054 return 0;
2055}
2056
fac6795d 2057/*
9ae110e2 2058 * lib constructor.
fac6795d
DG
2059 */
2060static void __attribute__((constructor)) init()
2061{
2062 /* Set default session group */
bbccc3d2 2063 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 2064}
49cca668
DG
2065
2066/*
9ae110e2 2067 * lib destructor.
49cca668
DG
2068 */
2069static void __attribute__((destructor)) lttng_ctl_exit()
2070{
2071 free(tracing_group);
2072}
This page took 0.169689 seconds and 5 git commands to generate.