Use the consumer stream API in consumer_del_stream()
[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
22#define _GNU_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>
fac6795d 37
d00c599e
DG
38#include "filter/filter-ast.h"
39#include "filter/filter-parser.h"
40#include "filter/filter-bytecode.h"
41#include "filter/memstream.h"
cac3069d 42#include "lttng-ctl-helper.h"
53a80697
MD
43
44#ifdef DEBUG
d00c599e 45static const int print_xml = 1;
53a80697
MD
46#define dbg_printf(fmt, args...) \
47 printf("[debug liblttng-ctl] " fmt, ## args)
48#else
d00c599e 49static const int print_xml = 0;
53a80697
MD
50#define dbg_printf(fmt, args...) \
51do { \
52 /* do nothing but check printf format */ \
53 if (0) \
54 printf("[debug liblttnctl] " fmt, ## args); \
55} while (0)
56#endif
57
58
fac6795d
DG
59/* Socket to session daemon for communication */
60static int sessiond_socket;
61static char sessiond_sock_path[PATH_MAX];
44a5e5eb 62static char health_sock_path[PATH_MAX];
fac6795d 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;
77
99497cd0
MD
78/*
79 * Copy string from src to dst and enforce null terminated byte.
80 */
cac3069d
DG
81LTTNG_HIDDEN
82void lttng_ctl_copy_string(char *dst, const char *src, size_t len)
99497cd0 83{
e7d6716d 84 if (src && dst) {
99497cd0
MD
85 strncpy(dst, src, len);
86 /* Enforce the NULL terminated byte */
87 dst[len - 1] = '\0';
cd80958d
DG
88 } else if (dst) {
89 dst[0] = '\0';
99497cd0
MD
90 }
91}
92
fac6795d 93/*
cd80958d 94 * Copy domain to lttcomm_session_msg domain.
fac6795d 95 *
cd80958d
DG
96 * If domain is unknown, default domain will be the kernel.
97 */
cac3069d
DG
98LTTNG_HIDDEN
99void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
100 struct lttng_domain *src)
cd80958d
DG
101{
102 if (src && dst) {
103 switch (src->type) {
00e2e675
DG
104 case LTTNG_DOMAIN_KERNEL:
105 case LTTNG_DOMAIN_UST:
00e2e675
DG
106 memcpy(dst, src, sizeof(struct lttng_domain));
107 break;
108 default:
109 memset(dst, 0, sizeof(struct lttng_domain));
00e2e675 110 break;
cd80958d
DG
111 }
112 }
113}
114
115/*
116 * Send lttcomm_session_msg to the session daemon.
fac6795d 117 *
1c8d13c8
TD
118 * On success, returns the number of bytes sent (>=0)
119 * On error, returns -1
fac6795d 120 */
cd80958d 121static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
122{
123 int ret;
124
125 if (!connected) {
2f70b271 126 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 127 goto end;
fac6795d
DG
128 }
129
a4b92340
DG
130 DBG("LSM cmd type : %d", lsm->cmd_type);
131
be040666 132 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 133 sizeof(struct lttcomm_session_msg));
2f70b271
DG
134 if (ret < 0) {
135 ret = -LTTNG_ERR_FATAL;
136 }
e065084a
DG
137
138end:
139 return ret;
140}
141
53a80697
MD
142/*
143 * Send var len data to the session daemon.
144 *
145 * On success, returns the number of bytes sent (>=0)
146 * On error, returns -1
147 */
148static int send_session_varlen(void *data, size_t len)
149{
150 int ret;
151
152 if (!connected) {
2f70b271 153 ret = -LTTNG_ERR_NO_SESSIOND;
53a80697
MD
154 goto end;
155 }
a4b92340 156
53a80697
MD
157 if (!data || !len) {
158 ret = 0;
159 goto end;
160 }
161
162 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
2f70b271
DG
163 if (ret < 0) {
164 ret = -LTTNG_ERR_FATAL;
165 }
53a80697
MD
166
167end:
168 return ret;
169}
170
e065084a 171/*
cd80958d 172 * Receive data from the sessiond socket.
e065084a 173 *
1c8d13c8
TD
174 * On success, returns the number of bytes received (>=0)
175 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 176 */
ca95a216 177static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
178{
179 int ret;
180
181 if (!connected) {
2f70b271 182 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 183 goto end;
fac6795d
DG
184 }
185
ca95a216 186 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
2f70b271
DG
187 if (ret < 0) {
188 ret = -LTTNG_ERR_FATAL;
189 }
fac6795d 190
e065084a 191end:
fac6795d
DG
192 return ret;
193}
194
195/*
1c8d13c8 196 * Check if we are in the specified group.
65beb5ff 197 *
2269e89e 198 * If yes return 1, else return -1.
947308c4
DG
199 */
200static int check_tracing_group(const char *grp_name)
201{
202 struct group *grp_tracing; /* no free(). See getgrnam(3) */
203 gid_t *grp_list;
204 int grp_list_size, grp_id, i;
205 int ret = -1;
206
207 /* Get GID of group 'tracing' */
208 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
209 if (!grp_tracing) {
210 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
211 goto end;
212 }
213
214 /* Get number of supplementary group IDs */
215 grp_list_size = getgroups(0, NULL);
216 if (grp_list_size < 0) {
217 perror("getgroups");
218 goto end;
219 }
220
221 /* Alloc group list of the right size */
222 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 223 if (!grp_list) {
1c8d13c8 224 perror("malloc");
00795392
MD
225 goto end;
226 }
947308c4 227 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 228 if (grp_id < 0) {
947308c4
DG
229 perror("getgroups");
230 goto free_list;
231 }
232
233 for (i = 0; i < grp_list_size; i++) {
234 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 235 ret = 1;
947308c4
DG
236 break;
237 }
238 }
239
240free_list:
241 free(grp_list);
242
243end:
244 return ret;
245}
246
247/*
2269e89e
DG
248 * Try connect to session daemon with sock_path.
249 *
250 * Return 0 on success, else -1
251 */
252static int try_connect_sessiond(const char *sock_path)
253{
254 int ret;
255
256 /* If socket exist, we check if the daemon listens for connect. */
257 ret = access(sock_path, F_OK);
258 if (ret < 0) {
259 /* Not alive */
2f70b271 260 goto error;
2269e89e
DG
261 }
262
263 ret = lttcomm_connect_unix_sock(sock_path);
264 if (ret < 0) {
265 /* Not alive */
2f70b271 266 goto error;
2269e89e
DG
267 }
268
269 ret = lttcomm_close_unix_sock(ret);
270 if (ret < 0) {
271 perror("lttcomm_close_unix_sock");
272 }
273
274 return 0;
2f70b271
DG
275
276error:
277 return -1;
2269e89e
DG
278}
279
280/*
2f70b271
DG
281 * Set sessiond socket path by putting it in the global sessiond_sock_path
282 * variable.
283 *
284 * Returns 0 on success, negative value on failure (the sessiond socket path
285 * is somehow too long or ENOMEM).
947308c4
DG
286 */
287static int set_session_daemon_path(void)
288{
2269e89e
DG
289 int in_tgroup = 0; /* In tracing group */
290 uid_t uid;
291
292 uid = getuid();
947308c4 293
2269e89e
DG
294 if (uid != 0) {
295 /* Are we in the tracing group ? */
296 in_tgroup = check_tracing_group(tracing_group);
297 }
298
08a9c49f 299 if ((uid == 0) || in_tgroup) {
cac3069d
DG
300 lttng_ctl_copy_string(sessiond_sock_path,
301 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path));
08a9c49f 302 }
2269e89e 303
08a9c49f 304 if (uid != 0) {
c617c0c6
MD
305 int ret;
306
08a9c49f
TD
307 if (in_tgroup) {
308 /* Tracing group */
309 ret = try_connect_sessiond(sessiond_sock_path);
310 if (ret >= 0) {
311 goto end;
2269e89e 312 }
08a9c49f 313 /* Global session daemon not available... */
2269e89e 314 }
08a9c49f
TD
315 /* ...or not in tracing group (and not root), default */
316
317 /*
318 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
319 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
320 */
321 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
feb0f3e5 322 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
08a9c49f 323 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
2f70b271 324 goto error;
947308c4 325 }
947308c4 326 }
08a9c49f 327end:
947308c4 328 return 0;
2f70b271
DG
329
330error:
331 return -1;
947308c4
DG
332}
333
65beb5ff
DG
334/*
335 * Connect to the LTTng session daemon.
336 *
337 * On success, return 0. On error, return -1.
338 */
339static int connect_sessiond(void)
340{
341 int ret;
342
343 ret = set_session_daemon_path();
344 if (ret < 0) {
2f70b271 345 goto error;
65beb5ff
DG
346 }
347
348 /* Connect to the sesssion daemon */
349 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
350 if (ret < 0) {
2f70b271 351 goto error;
65beb5ff
DG
352 }
353
354 sessiond_socket = ret;
355 connected = 1;
356
357 return 0;
2f70b271
DG
358
359error:
360 return -1;
65beb5ff
DG
361}
362
363/*
1c8d13c8
TD
364 * Clean disconnect from the session daemon.
365 * On success, return 0. On error, return -1.
65beb5ff
DG
366 */
367static int disconnect_sessiond(void)
368{
369 int ret = 0;
370
371 if (connected) {
372 ret = lttcomm_close_unix_sock(sessiond_socket);
373 sessiond_socket = 0;
374 connected = 0;
375 }
376
377 return ret;
378}
379
35a6fdb7 380/*
cd80958d 381 * Ask the session daemon a specific command and put the data into buf.
53a80697 382 * Takes extra var. len. data as input to send to the session daemon.
65beb5ff 383 *
af87c45a 384 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 385 */
cac3069d
DG
386LTTNG_HIDDEN
387int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
a4b92340 388 void *vardata, size_t varlen, void **buf)
65beb5ff
DG
389{
390 int ret;
391 size_t size;
392 void *data = NULL;
cd80958d 393 struct lttcomm_lttng_msg llm;
65beb5ff
DG
394
395 ret = connect_sessiond();
396 if (ret < 0) {
2f70b271 397 ret = -LTTNG_ERR_NO_SESSIOND;
65beb5ff
DG
398 goto end;
399 }
400
65beb5ff 401 /* Send command to session daemon */
cd80958d 402 ret = send_session_msg(lsm);
65beb5ff 403 if (ret < 0) {
2f70b271 404 /* Ret value is a valid lttng error code. */
65beb5ff
DG
405 goto end;
406 }
53a80697
MD
407 /* Send var len data */
408 ret = send_session_varlen(vardata, varlen);
409 if (ret < 0) {
2f70b271 410 /* Ret value is a valid lttng error code. */
53a80697
MD
411 goto end;
412 }
65beb5ff
DG
413
414 /* Get header from data transmission */
415 ret = recv_data_sessiond(&llm, sizeof(llm));
416 if (ret < 0) {
2f70b271 417 /* Ret value is a valid lttng error code. */
65beb5ff
DG
418 goto end;
419 }
420
421 /* Check error code if OK */
f73fabfd 422 if (llm.ret_code != LTTNG_OK) {
65beb5ff
DG
423 ret = -llm.ret_code;
424 goto end;
425 }
426
427 size = llm.data_size;
428 if (size == 0) {
874d3f84 429 /* If client free with size 0 */
a45d5536
DG
430 if (buf != NULL) {
431 *buf = NULL;
432 }
7d29a247 433 ret = 0;
65beb5ff
DG
434 goto end;
435 }
436
437 data = (void*) malloc(size);
438
439 /* Get payload data */
440 ret = recv_data_sessiond(data, size);
441 if (ret < 0) {
442 free(data);
443 goto end;
444 }
445
83009e5e
DG
446 /*
447 * Extra protection not to dereference a NULL pointer. If buf is NULL at
448 * this point, an error is returned and data is freed.
449 */
450 if (buf == NULL) {
2f70b271 451 ret = -LTTNG_ERR_INVALID;
83009e5e
DG
452 free(data);
453 goto end;
454 }
455
65beb5ff
DG
456 *buf = data;
457 ret = size;
458
459end:
460 disconnect_sessiond();
461 return ret;
462}
463
9f19cc17 464/*
cd80958d 465 * Create lttng handle and return pointer.
1c8d13c8 466 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 467 */
cd80958d
DG
468struct lttng_handle *lttng_create_handle(const char *session_name,
469 struct lttng_domain *domain)
9f19cc17 470{
2f70b271
DG
471 struct lttng_handle *handle = NULL;
472
473 if (domain == NULL) {
474 goto end;
475 }
cd80958d
DG
476
477 handle = malloc(sizeof(struct lttng_handle));
478 if (handle == NULL) {
2f70b271 479 PERROR("malloc handle");
cd80958d
DG
480 goto end;
481 }
482
483 /* Copy session name */
cac3069d 484 lttng_ctl_copy_string(handle->session_name, session_name,
cd80958d
DG
485 sizeof(handle->session_name));
486
487 /* Copy lttng domain */
cac3069d 488 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
cd80958d
DG
489
490end:
491 return handle;
492}
493
494/*
495 * Destroy handle by free(3) the pointer.
496 */
497void lttng_destroy_handle(struct lttng_handle *handle)
498{
0e428499 499 free(handle);
eb354453
DG
500}
501
d9800920
DG
502/*
503 * Register an outside consumer.
1c8d13c8 504 * Returns size of returned session payload data or a negative error code.
d9800920
DG
505 */
506int lttng_register_consumer(struct lttng_handle *handle,
507 const char *socket_path)
508{
509 struct lttcomm_session_msg lsm;
510
2f70b271
DG
511 if (handle == NULL || socket_path == NULL) {
512 return -LTTNG_ERR_INVALID;
513 }
514
d9800920 515 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
cac3069d 516 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
d9800920 517 sizeof(lsm.session.name));
cac3069d 518 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d9800920 519
cac3069d 520 lttng_ctl_copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
d9800920 521
cac3069d 522 return lttng_ctl_ask_sessiond(&lsm, NULL);
d9800920
DG
523}
524
1df4dedd 525/*
1c8d13c8
TD
526 * Start tracing for all traces of the session.
527 * Returns size of returned session payload data or a negative error code.
1df4dedd 528 */
6a4f824d 529int lttng_start_tracing(const char *session_name)
f3ed775e 530{
cd80958d
DG
531 struct lttcomm_session_msg lsm;
532
6a4f824d 533 if (session_name == NULL) {
2f70b271 534 return -LTTNG_ERR_INVALID;
cd80958d
DG
535 }
536
537 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d 538
cac3069d
DG
539 lttng_ctl_copy_string(lsm.session.name, session_name,
540 sizeof(lsm.session.name));
cd80958d 541
cac3069d 542 return lttng_ctl_ask_sessiond(&lsm, NULL);
f3ed775e 543}
1df4dedd
DG
544
545/*
38ee087f 546 * Stop tracing for all traces of the session.
f3ed775e 547 */
38ee087f 548static int _lttng_stop_tracing(const char *session_name, int wait)
f3ed775e 549{
38ee087f 550 int ret, data_ret;
cd80958d
DG
551 struct lttcomm_session_msg lsm;
552
6a4f824d 553 if (session_name == NULL) {
2f70b271 554 return -LTTNG_ERR_INVALID;
6a4f824d
DG
555 }
556
cd80958d 557 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d 558
cac3069d
DG
559 lttng_ctl_copy_string(lsm.session.name, session_name,
560 sizeof(lsm.session.name));
cd80958d 561
cac3069d 562 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
38ee087f
DG
563 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
564 goto error;
565 }
566
567 if (!wait) {
568 goto end;
569 }
570
571 _MSG("Waiting for data availability");
572
573 /* Check for data availability */
574 do {
6d805429 575 data_ret = lttng_data_pending(session_name);
38ee087f
DG
576 if (data_ret < 0) {
577 /* Return the data available call error. */
578 ret = data_ret;
579 goto error;
580 }
581
582 /*
583 * Data sleep time before retrying (in usec). Don't sleep if the call
584 * returned value indicates availability.
585 */
6d805429 586 if (data_ret) {
38ee087f
DG
587 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
588 _MSG(".");
589 }
6d805429 590 } while (data_ret != 0);
38ee087f
DG
591
592 MSG("");
593
594end:
595error:
596 return ret;
597}
598
599/*
600 * Stop tracing and wait for data availability.
601 */
602int lttng_stop_tracing(const char *session_name)
603{
604 return _lttng_stop_tracing(session_name, 1);
605}
606
607/*
608 * Stop tracing but _don't_ wait for data availability.
609 */
610int lttng_stop_tracing_no_wait(const char *session_name)
611{
612 return _lttng_stop_tracing(session_name, 0);
f3ed775e
DG
613}
614
615/*
601d5acf
DG
616 * Add context to a channel.
617 *
618 * If the given channel is NULL, add the contexts to all channels.
619 * The event_name param is ignored.
af87c45a
DG
620 *
621 * Returns the size of the returned payload data or a negative error code.
1df4dedd 622 */
cd80958d 623int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
624 struct lttng_event_context *ctx, const char *event_name,
625 const char *channel_name)
d65106b1 626{
cd80958d
DG
627 struct lttcomm_session_msg lsm;
628
9d697d3d
DG
629 /* Safety check. Both are mandatory */
630 if (handle == NULL || ctx == NULL) {
2f70b271 631 return -LTTNG_ERR_INVALID;
cd80958d
DG
632 }
633
441c16a7
MD
634 memset(&lsm, 0, sizeof(lsm));
635
cd80958d
DG
636 lsm.cmd_type = LTTNG_ADD_CONTEXT;
637
638 /* Copy channel name */
cac3069d 639 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
cd80958d 640 sizeof(lsm.u.context.channel_name));
cd80958d 641
cac3069d 642 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 643
9d697d3d 644 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 645
cac3069d 646 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
647 sizeof(lsm.session.name));
648
cac3069d 649 return lttng_ctl_ask_sessiond(&lsm, NULL);
d65106b1
DG
650}
651
f3ed775e 652/*
1c8d13c8
TD
653 * Enable event(s) for a channel.
654 * If no event name is specified, all events are enabled.
655 * If no channel name is specified, the default 'channel0' is used.
656 * Returns size of returned session payload data or a negative error code.
f3ed775e 657 */
cd80958d 658int lttng_enable_event(struct lttng_handle *handle,
38057ed1 659 struct lttng_event *ev, const char *channel_name)
1df4dedd 660{
cd80958d
DG
661 struct lttcomm_session_msg lsm;
662
5117eeec 663 if (handle == NULL || ev == NULL) {
2f70b271 664 return -LTTNG_ERR_INVALID;
cd80958d 665 }
33a2b854 666
441c16a7
MD
667 memset(&lsm, 0, sizeof(lsm));
668
5117eeec 669 /* If no channel name, we put the default name */
94cf3c47 670 if (channel_name == NULL) {
cac3069d 671 lttng_ctl_copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
cd80958d 672 sizeof(lsm.u.enable.channel_name));
33a2b854 673 } else {
cac3069d 674 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
cd80958d 675 sizeof(lsm.u.enable.channel_name));
eb354453
DG
676 }
677
cac3069d 678 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 679
8c9ae521 680 if (ev->name[0] != '\0') {
cd80958d 681 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 682 } else {
cd80958d 683 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 684 }
8c9ae521 685 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 686
cac3069d 687 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
688 sizeof(lsm.session.name));
689
cac3069d 690 return lttng_ctl_ask_sessiond(&lsm, NULL);
1df4dedd
DG
691}
692
53a80697 693/*
025faf73 694 * Create or enable an event with a filter expression.
178191b3 695 *
53a80697
MD
696 * Return negative error value on error.
697 * Return size of returned session payload data if OK.
698 */
025faf73 699int lttng_enable_event_with_filter(struct lttng_handle *handle,
178191b3 700 struct lttng_event *event, const char *channel_name,
53a80697
MD
701 const char *filter_expression)
702{
703 struct lttcomm_session_msg lsm;
704 struct filter_parser_ctx *ctx;
705 FILE *fmem;
706 int ret = 0;
707
52df2401
MD
708 if (!filter_expression) {
709 /*
710 * Fall back to normal event enabling if no filter
711 * specified.
712 */
713 return lttng_enable_event(handle, event, channel_name);
714 }
715
716 /*
717 * Empty filter string will always be rejected by the parser
718 * anyway, so treat this corner-case early to eliminate
719 * lttng_fmemopen error for 0-byte allocation.
720 */
721 if (handle == NULL || filter_expression[0] == '\0') {
2f70b271 722 return -LTTNG_ERR_INVALID;
53a80697
MD
723 }
724
53a80697
MD
725 /*
726 * casting const to non-const, as the underlying function will
727 * use it in read-only mode.
728 */
729 fmem = lttng_fmemopen((void *) filter_expression,
730 strlen(filter_expression), "r");
731 if (!fmem) {
732 fprintf(stderr, "Error opening memory as stream\n");
ff7ef7b8 733 return -LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
734 }
735 ctx = filter_parser_ctx_alloc(fmem);
736 if (!ctx) {
737 fprintf(stderr, "Error allocating parser\n");
ff7ef7b8 738 ret = -LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
739 goto alloc_error;
740 }
741 ret = filter_parser_ctx_append_ast(ctx);
742 if (ret) {
743 fprintf(stderr, "Parse error\n");
ff7ef7b8 744 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
745 goto parse_error;
746 }
747 ret = filter_visitor_set_parent(ctx);
748 if (ret) {
749 fprintf(stderr, "Set parent error\n");
ff7ef7b8 750 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
751 goto parse_error;
752 }
753 if (print_xml) {
754 ret = filter_visitor_print_xml(ctx, stdout, 0);
755 if (ret) {
756 fflush(stdout);
757 fprintf(stderr, "XML print error\n");
ff7ef7b8 758 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
759 goto parse_error;
760 }
761 }
762
763 dbg_printf("Generating IR... ");
764 fflush(stdout);
765 ret = filter_visitor_ir_generate(ctx);
766 if (ret) {
767 fprintf(stderr, "Generate IR error\n");
ff7ef7b8 768 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
769 goto parse_error;
770 }
771 dbg_printf("done\n");
772
773 dbg_printf("Validating IR... ");
774 fflush(stdout);
775 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
776 if (ret) {
ff7ef7b8 777 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
778 goto parse_error;
779 }
780 dbg_printf("done\n");
781
782 dbg_printf("Generating bytecode... ");
783 fflush(stdout);
784 ret = filter_visitor_bytecode_generate(ctx);
785 if (ret) {
786 fprintf(stderr, "Generate bytecode error\n");
ff7ef7b8 787 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
788 goto parse_error;
789 }
790 dbg_printf("done\n");
791 dbg_printf("Size of bytecode generated: %u bytes.\n",
792 bytecode_get_len(&ctx->bytecode->b));
793
794 memset(&lsm, 0, sizeof(lsm));
795
025faf73 796 lsm.cmd_type = LTTNG_ENABLE_EVENT_WITH_FILTER;
53a80697
MD
797
798 /* Copy channel name */
cac3069d 799 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
025faf73 800 sizeof(lsm.u.enable.channel_name));
53a80697 801 /* Copy event name */
178191b3
DG
802 if (event) {
803 memcpy(&lsm.u.enable.event, event, sizeof(lsm.u.enable.event));
804 }
805
025faf73 806 lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b)
53a80697
MD
807 + bytecode_get_len(&ctx->bytecode->b);
808
cac3069d 809 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
53a80697 810
cac3069d 811 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
53a80697
MD
812 sizeof(lsm.session.name));
813
cac3069d 814 ret = lttng_ctl_ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
025faf73 815 lsm.u.enable.bytecode_len, NULL);
53a80697
MD
816
817 filter_bytecode_free(ctx);
818 filter_ir_free(ctx);
819 filter_parser_ctx_free(ctx);
820 if (fclose(fmem) != 0) {
821 perror("fclose");
822 }
823 return ret;
824
825parse_error:
826 filter_bytecode_free(ctx);
827 filter_ir_free(ctx);
828 filter_parser_ctx_free(ctx);
829alloc_error:
830 if (fclose(fmem) != 0) {
831 perror("fclose");
832 }
833 return ret;
834}
835
1df4dedd 836/*
1c8d13c8
TD
837 * Disable event(s) of a channel and domain.
838 * If no event name is specified, all events are disabled.
839 * If no channel name is specified, the default 'channel0' is used.
840 * Returns size of returned session payload data or a negative error code.
1df4dedd 841 */
cd80958d 842int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 843 const char *channel_name)
1df4dedd 844{
cd80958d 845 struct lttcomm_session_msg lsm;
1df4dedd 846
9d697d3d 847 if (handle == NULL) {
2f70b271 848 return -LTTNG_ERR_INVALID;
cd80958d
DG
849 }
850
441c16a7
MD
851 memset(&lsm, 0, sizeof(lsm));
852
cd80958d 853 if (channel_name) {
cac3069d 854 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
cd80958d 855 sizeof(lsm.u.disable.channel_name));
f3ed775e 856 } else {
cac3069d 857 lttng_ctl_copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
cd80958d 858 sizeof(lsm.u.disable.channel_name));
eb354453
DG
859 }
860
cac3069d 861 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 862
f84efadf 863 if (name != NULL) {
cac3069d
DG
864 lttng_ctl_copy_string(lsm.u.disable.name, name,
865 sizeof(lsm.u.disable.name));
cd80958d 866 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 867 } else {
cd80958d 868 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
869 }
870
cac3069d 871 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
872 sizeof(lsm.session.name));
873
cac3069d 874 return lttng_ctl_ask_sessiond(&lsm, NULL);
1df4dedd
DG
875}
876
877/*
1c8d13c8
TD
878 * Enable channel per domain
879 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 880 */
cd80958d 881int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 882 struct lttng_channel *chan)
a5c5a2bd 883{
cd80958d
DG
884 struct lttcomm_session_msg lsm;
885
5117eeec
DG
886 /*
887 * NULL arguments are forbidden. No default values.
888 */
889 if (handle == NULL || chan == NULL) {
2f70b271 890 return -LTTNG_ERR_INVALID;
cd80958d
DG
891 }
892
441c16a7
MD
893 memset(&lsm, 0, sizeof(lsm));
894
5117eeec 895 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 896
cd80958d
DG
897 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
898
cac3069d 899 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 900
cac3069d 901 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
902 sizeof(lsm.session.name));
903
cac3069d 904 return lttng_ctl_ask_sessiond(&lsm, NULL);
8c0faa1d 905}
1df4dedd 906
2ef84c95 907/*
1c8d13c8
TD
908 * All tracing will be stopped for registered events of the channel.
909 * Returns size of returned session payload data or a negative error code.
2ef84c95 910 */
cd80958d 911int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 912{
cd80958d
DG
913 struct lttcomm_session_msg lsm;
914
9d697d3d
DG
915 /* Safety check. Both are mandatory */
916 if (handle == NULL || name == NULL) {
2f70b271 917 return -LTTNG_ERR_INVALID;
cd80958d
DG
918 }
919
441c16a7
MD
920 memset(&lsm, 0, sizeof(lsm));
921
cd80958d 922 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 923
cac3069d 924 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
9d697d3d
DG
925 sizeof(lsm.u.disable.channel_name));
926
cac3069d 927 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cd80958d 928
cac3069d 929 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
930 sizeof(lsm.session.name));
931
cac3069d 932 return lttng_ctl_ask_sessiond(&lsm, NULL);
ca95a216
DG
933}
934
fac6795d 935/*
1c8d13c8
TD
936 * Lists all available tracepoints of domain.
937 * Sets the contents of the events array.
938 * Returns the number of lttng_event entries in events;
939 * on error, returns a negative value.
fac6795d 940 */
cd80958d 941int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 942 struct lttng_event **events)
fac6795d 943{
052da939 944 int ret;
cd80958d
DG
945 struct lttcomm_session_msg lsm;
946
9d697d3d 947 if (handle == NULL) {
2f70b271 948 return -LTTNG_ERR_INVALID;
cd80958d 949 }
fac6795d 950
cd80958d 951 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
cac3069d 952 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 953
cac3069d 954 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
955 if (ret < 0) {
956 return ret;
eb354453 957 }
fac6795d 958
9f19cc17 959 return ret / sizeof(struct lttng_event);
fac6795d
DG
960}
961
f37d259d
MD
962/*
963 * Lists all available tracepoint fields of domain.
964 * Sets the contents of the event field array.
965 * Returns the number of lttng_event_field entries in events;
966 * on error, returns a negative value.
967 */
968int lttng_list_tracepoint_fields(struct lttng_handle *handle,
969 struct lttng_event_field **fields)
970{
971 int ret;
972 struct lttcomm_session_msg lsm;
973
974 if (handle == NULL) {
2f70b271 975 return -LTTNG_ERR_INVALID;
f37d259d
MD
976 }
977
978 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
cac3069d 979 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f37d259d 980
cac3069d 981 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
982 if (ret < 0) {
983 return ret;
984 }
985
986 return ret / sizeof(struct lttng_event_field);
987}
988
1657e9bb 989/*
1c8d13c8
TD
990 * Returns a human readable string describing
991 * the error code (a negative value).
1657e9bb 992 */
9a745bc7 993const char *lttng_strerror(int code)
1657e9bb 994{
f73fabfd 995 return error_get_str(code);
1657e9bb
DG
996}
997
aaf97519 998/*
a4b92340
DG
999 * Create a brand new session using name and url for destination.
1000 *
f73fabfd 1001 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1002 */
a4b92340 1003int lttng_create_session(const char *name, const char *url)
00e2e675 1004{
3dd05a85 1005 int ret;
a4b92340 1006 ssize_t size;
00e2e675 1007 struct lttcomm_session_msg lsm;
a4b92340 1008 struct lttng_uri *uris = NULL;
00e2e675 1009
a4b92340 1010 if (name == NULL) {
2f70b271 1011 return -LTTNG_ERR_INVALID;
00e2e675
DG
1012 }
1013
a4b92340 1014 memset(&lsm, 0, sizeof(lsm));
00e2e675 1015
a4b92340 1016 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1017 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1018
1019 /* There should never be a data URL */
bc894455 1020 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1021 if (size < 0) {
2f70b271 1022 return -LTTNG_ERR_INVALID;
00e2e675
DG
1023 }
1024
a4b92340
DG
1025 lsm.u.uri.size = size;
1026
cac3069d
DG
1027 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1028 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1029
1030 free(uris);
1031 return ret;
00e2e675
DG
1032}
1033
8028d920 1034/*
8028d920 1035 * Destroy session using name.
1c8d13c8 1036 * Returns size of returned session payload data or a negative error code.
8028d920 1037 */
843f5df9 1038int lttng_destroy_session(const char *session_name)
8028d920 1039{
cd80958d
DG
1040 struct lttcomm_session_msg lsm;
1041
843f5df9 1042 if (session_name == NULL) {
2f70b271 1043 return -LTTNG_ERR_INVALID;
cd80958d
DG
1044 }
1045
1046 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9 1047
cac3069d
DG
1048 lttng_ctl_copy_string(lsm.session.name, session_name,
1049 sizeof(lsm.session.name));
cd80958d 1050
cac3069d 1051 return lttng_ctl_ask_sessiond(&lsm, NULL);
aaf97519
DG
1052}
1053
57167058 1054/*
57167058 1055 * Ask the session daemon for all available sessions.
1c8d13c8
TD
1056 * Sets the contents of the sessions array.
1057 * Returns the number of lttng_session entries in sessions;
1058 * on error, returns a negative value.
57167058 1059 */
ca95a216 1060int lttng_list_sessions(struct lttng_session **sessions)
57167058 1061{
ca95a216 1062 int ret;
cd80958d 1063 struct lttcomm_session_msg lsm;
57167058 1064
cd80958d 1065 lsm.cmd_type = LTTNG_LIST_SESSIONS;
cac3069d 1066 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
57167058 1067 if (ret < 0) {
ca95a216 1068 return ret;
57167058
DG
1069 }
1070
ca95a216 1071 return ret / sizeof(struct lttng_session);
57167058
DG
1072}
1073
9f19cc17 1074/*
1c8d13c8
TD
1075 * Ask the session daemon for all available domains of a session.
1076 * Sets the contents of the domains array.
1077 * Returns the number of lttng_domain entries in domains;
1078 * on error, returns a negative value.
9f19cc17 1079 */
330be774 1080int lttng_list_domains(const char *session_name,
cd80958d 1081 struct lttng_domain **domains)
9f19cc17
DG
1082{
1083 int ret;
cd80958d
DG
1084 struct lttcomm_session_msg lsm;
1085
330be774 1086 if (session_name == NULL) {
2f70b271 1087 return -LTTNG_ERR_INVALID;
cd80958d 1088 }
9f19cc17 1089
cd80958d
DG
1090 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1091
cac3069d
DG
1092 lttng_ctl_copy_string(lsm.session.name, session_name,
1093 sizeof(lsm.session.name));
cd80958d 1094
cac3069d 1095 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1096 if (ret < 0) {
1097 return ret;
1098 }
1099
1100 return ret / sizeof(struct lttng_domain);
1101}
1102
1103/*
1c8d13c8
TD
1104 * Ask the session daemon for all available channels of a session.
1105 * Sets the contents of the channels array.
1106 * Returns the number of lttng_channel entries in channels;
1107 * on error, returns a negative value.
9f19cc17 1108 */
cd80958d
DG
1109int lttng_list_channels(struct lttng_handle *handle,
1110 struct lttng_channel **channels)
9f19cc17
DG
1111{
1112 int ret;
cd80958d
DG
1113 struct lttcomm_session_msg lsm;
1114
9d697d3d 1115 if (handle == NULL) {
2f70b271 1116 return -LTTNG_ERR_INVALID;
cd80958d
DG
1117 }
1118
1119 lsm.cmd_type = LTTNG_LIST_CHANNELS;
cac3069d 1120 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1121 sizeof(lsm.session.name));
9f19cc17 1122
cac3069d 1123 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1124
cac3069d 1125 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
1126 if (ret < 0) {
1127 return ret;
1128 }
1129
1130 return ret / sizeof(struct lttng_channel);
1131}
1132
1133/*
1c8d13c8
TD
1134 * Ask the session daemon for all available events of a session channel.
1135 * Sets the contents of the events array.
1136 * Returns the number of lttng_event entries in events;
1137 * on error, returns a negative value.
9f19cc17 1138 */
cd80958d
DG
1139int lttng_list_events(struct lttng_handle *handle,
1140 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1141{
1142 int ret;
cd80958d 1143 struct lttcomm_session_msg lsm;
9f19cc17 1144
9d697d3d
DG
1145 /* Safety check. An handle and channel name are mandatory */
1146 if (handle == NULL || channel_name == NULL) {
2f70b271 1147 return -LTTNG_ERR_INVALID;
cd80958d
DG
1148 }
1149
1150 lsm.cmd_type = LTTNG_LIST_EVENTS;
cac3069d 1151 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1152 sizeof(lsm.session.name));
cac3069d 1153 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
cd80958d
DG
1154 sizeof(lsm.u.list.channel_name));
1155
cac3069d 1156 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1157
cac3069d 1158 ret = lttng_ctl_ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
1159 if (ret < 0) {
1160 return ret;
1161 }
1162
1163 return ret / sizeof(struct lttng_event);
1164}
1165
fac6795d 1166/*
1c8d13c8
TD
1167 * Sets the tracing_group variable with name.
1168 * This function allocates memory pointed to by tracing_group.
1169 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
1170 */
1171int lttng_set_tracing_group(const char *name)
1172{
9d697d3d 1173 if (name == NULL) {
2f70b271 1174 return -LTTNG_ERR_INVALID;
9d697d3d
DG
1175 }
1176
fac6795d 1177 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 1178 return -LTTNG_ERR_FATAL;
fac6795d
DG
1179 }
1180
1181 return 0;
1182}
1183
d0254c7c 1184/*
af87c45a 1185 * Returns size of returned session payload data or a negative error code.
d0254c7c 1186 */
cd80958d 1187int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
1188 struct lttng_calibrate *calibrate)
1189{
cd80958d 1190 struct lttcomm_session_msg lsm;
d0254c7c 1191
9d697d3d
DG
1192 /* Safety check. NULL pointer are forbidden */
1193 if (handle == NULL || calibrate == NULL) {
2f70b271 1194 return -LTTNG_ERR_INVALID;
cd80958d 1195 }
d0254c7c 1196
cd80958d 1197 lsm.cmd_type = LTTNG_CALIBRATE;
cac3069d 1198 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 1199
cd80958d
DG
1200 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1201
cac3069d 1202 return lttng_ctl_ask_sessiond(&lsm, NULL);
d0254c7c
MD
1203}
1204
5edd7e09
DG
1205/*
1206 * Set default channel attributes.
441c16a7 1207 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
1208 */
1209void lttng_channel_set_default_attr(struct lttng_domain *domain,
1210 struct lttng_channel_attr *attr)
1211{
1212 /* Safety check */
1213 if (attr == NULL || domain == NULL) {
1214 return;
1215 }
1216
eacaa7a6
DS
1217 memset(attr, 0, sizeof(struct lttng_channel_attr));
1218
0a9c6494
DG
1219 /* Same for all domains. */
1220 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1221 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
1222 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
1223
5edd7e09
DG
1224 switch (domain->type) {
1225 case LTTNG_DOMAIN_KERNEL:
d92ff3ef
DG
1226 attr->switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
1227 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 1228 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
1229 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1230 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1231 break;
1232 case LTTNG_DOMAIN_UST:
0a9c6494
DG
1233 switch (domain->buf_type) {
1234 case LTTNG_BUFFER_PER_UID:
1235 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
1236 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
1237 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
1238 attr->switch_timer_interval = DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
1239 attr->read_timer_interval = DEFAULT_UST_UID_CHANNEL_READ_TIMER;
1240 break;
1241 case LTTNG_BUFFER_PER_PID:
1242 default:
1243 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
1244 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
1245 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
1246 attr->switch_timer_interval = DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
1247 attr->read_timer_interval = DEFAULT_UST_PID_CHANNEL_READ_TIMER;
1248 break;
1249 }
5edd7e09 1250 default:
441c16a7 1251 /* Default behavior: leave set to 0. */
5edd7e09
DG
1252 break;
1253 }
1254}
1255
fac6795d 1256/*
2269e89e 1257 * Check if session daemon is alive.
fac6795d 1258 *
2269e89e 1259 * Return 1 if alive or 0 if not.
1c8d13c8 1260 * On error returns a negative value.
fac6795d 1261 */
947308c4 1262int lttng_session_daemon_alive(void)
fac6795d
DG
1263{
1264 int ret;
1265
1266 ret = set_session_daemon_path();
1267 if (ret < 0) {
947308c4 1268 /* Error */
fac6795d
DG
1269 return ret;
1270 }
1271
9d035200 1272 if (*sessiond_sock_path == '\0') {
2f70b271
DG
1273 /*
1274 * No socket path set. Weird error which means the constructor was not
1275 * called.
1276 */
1277 assert(0);
fac6795d
DG
1278 }
1279
2269e89e 1280 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
1281 if (ret < 0) {
1282 /* Not alive */
1283 return 0;
1284 }
7d8234d9 1285
947308c4
DG
1286 /* Is alive */
1287 return 1;
fac6795d
DG
1288}
1289
00e2e675 1290/*
a4b92340 1291 * Set URL for a consumer for a session and domain.
00e2e675
DG
1292 *
1293 * Return 0 on success, else a negative value.
1294 */
a4b92340
DG
1295int lttng_set_consumer_url(struct lttng_handle *handle,
1296 const char *control_url, const char *data_url)
00e2e675 1297{
3dd05a85 1298 int ret;
a4b92340 1299 ssize_t size;
00e2e675 1300 struct lttcomm_session_msg lsm;
a4b92340 1301 struct lttng_uri *uris = NULL;
00e2e675 1302
a4b92340 1303 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 1304 return -LTTNG_ERR_INVALID;
00e2e675
DG
1305 }
1306
a4b92340
DG
1307 memset(&lsm, 0, sizeof(lsm));
1308
00e2e675
DG
1309 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1310
cac3069d 1311 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
00e2e675 1312 sizeof(lsm.session.name));
cac3069d 1313 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
00e2e675 1314
bc894455 1315 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 1316 if (size < 0) {
2f70b271 1317 return -LTTNG_ERR_INVALID;
a4b92340
DG
1318 }
1319
1320 lsm.u.uri.size = size;
00e2e675 1321
cac3069d
DG
1322 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1323 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1324
1325 free(uris);
1326 return ret;
00e2e675
DG
1327}
1328
1329/*
9c6bda17 1330 * [OBSOLETE]
00e2e675
DG
1331 */
1332int lttng_enable_consumer(struct lttng_handle *handle)
1333{
785d2d0d 1334 return -ENOSYS;
00e2e675
DG
1335}
1336
1337/*
9c6bda17 1338 * [OBSOLETE]
00e2e675
DG
1339 */
1340int lttng_disable_consumer(struct lttng_handle *handle)
1341{
785d2d0d 1342 return -ENOSYS;
00e2e675
DG
1343}
1344
44a5e5eb
DG
1345/*
1346 * Set health socket path by putting it in the global health_sock_path
1347 * variable.
1348 *
1349 * Returns 0 on success or assert(0) on ENOMEM.
1350 */
1351static int set_health_socket_path(void)
1352{
44a5e5eb
DG
1353 int in_tgroup = 0; /* In tracing group */
1354 uid_t uid;
1355 const char *home;
1356
1357 uid = getuid();
1358
1359 if (uid != 0) {
1360 /* Are we in the tracing group ? */
1361 in_tgroup = check_tracing_group(tracing_group);
1362 }
1363
1364 if ((uid == 0) || in_tgroup) {
cac3069d
DG
1365 lttng_ctl_copy_string(health_sock_path,
1366 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK, sizeof(health_sock_path));
44a5e5eb
DG
1367 }
1368
1369 if (uid != 0) {
c617c0c6
MD
1370 int ret;
1371
44a5e5eb
DG
1372 /*
1373 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1374 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1375 */
feb0f3e5 1376 home = utils_get_home_dir();
44a5e5eb
DG
1377 if (home == NULL) {
1378 /* Fallback in /tmp .. */
1379 home = "/tmp";
1380 }
1381
1382 ret = snprintf(health_sock_path, sizeof(health_sock_path),
1383 DEFAULT_HOME_HEALTH_UNIX_SOCK, home);
1384 if ((ret < 0) || (ret >= sizeof(health_sock_path))) {
1385 /* ENOMEM at this point... just kill the control lib. */
1386 assert(0);
1387 }
1388 }
1389
1390 return 0;
1391}
1392
1393/*
1394 * Check session daemon health for a specific health component.
1395 *
2f70b271 1396 * Return 0 if health is OK or else 1 if BAD.
44a5e5eb 1397 *
2f70b271 1398 * Any other negative value is a lttng error code which can be translated with
44a5e5eb
DG
1399 * lttng_strerror().
1400 */
1401int lttng_health_check(enum lttng_health_component c)
1402{
1403 int sock, ret;
1404 struct lttcomm_health_msg msg;
1405 struct lttcomm_health_data reply;
1406
1407 /* Connect to the sesssion daemon */
1408 sock = lttcomm_connect_unix_sock(health_sock_path);
1409 if (sock < 0) {
2f70b271 1410 ret = -LTTNG_ERR_NO_SESSIOND;
44a5e5eb
DG
1411 goto error;
1412 }
1413
1414 msg.cmd = LTTNG_HEALTH_CHECK;
1415 msg.component = c;
1416
1417 ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg));
1418 if (ret < 0) {
2f70b271 1419 ret = -LTTNG_ERR_FATAL;
44a5e5eb
DG
1420 goto close_error;
1421 }
1422
1423 ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply));
1424 if (ret < 0) {
2f70b271 1425 ret = -LTTNG_ERR_FATAL;
44a5e5eb
DG
1426 goto close_error;
1427 }
1428
1429 ret = reply.ret_code;
1430
1431close_error:
67ef15b7
MD
1432 {
1433 int closeret;
1434
1435 closeret = close(sock);
1436 assert(!closeret);
1437 }
44a5e5eb
DG
1438
1439error:
1440 return ret;
1441}
1442
07424f16
DG
1443/*
1444 * This is an extension of create session that is ONLY and SHOULD only be used
1445 * by the lttng command line program. It exists to avoid using URI parsing in
1446 * the lttng client.
1447 *
1448 * We need the date and time for the trace path subdirectory for the case where
1449 * the user does NOT define one using either -o or -U. Using the normal
1450 * lttng_create_session API call, we have no clue on the session daemon side if
1451 * the URL was generated automatically by the client or define by the user.
1452 *
1453 * So this function "wrapper" is hidden from the public API, takes the datetime
1454 * string and appends it if necessary to the URI subdirectory before sending it
1455 * to the session daemon.
1456 *
1457 * With this extra function, the lttng_create_session call behavior is not
1458 * changed and the timestamp is appended to the URI on the session daemon side
1459 * if necessary.
1460 */
1461int _lttng_create_session_ext(const char *name, const char *url,
1462 const char *datetime)
1463{
3dd05a85 1464 int ret;
07424f16
DG
1465 ssize_t size;
1466 struct lttcomm_session_msg lsm;
1467 struct lttng_uri *uris = NULL;
1468
d0b96690 1469 if (name == NULL || datetime == NULL || url == NULL) {
2f70b271 1470 return -LTTNG_ERR_INVALID;
07424f16
DG
1471 }
1472
1473 memset(&lsm, 0, sizeof(lsm));
1474
1475 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1476 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
07424f16
DG
1477
1478 /* There should never be a data URL */
bc894455 1479 size = uri_parse_str_urls(url, NULL, &uris);
07424f16 1480 if (size < 0) {
3dd05a85
DG
1481 ret = -LTTNG_ERR_INVALID;
1482 goto error;
07424f16
DG
1483 }
1484
1485 lsm.u.uri.size = size;
1486
4b35a6b3 1487 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
da4aa2b8
DG
1488 /* Don't append datetime if the name was automatically created. */
1489 if (strncmp(name, DEFAULT_SESSION_NAME "-",
1490 strlen(DEFAULT_SESSION_NAME) + 1)) {
1491 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
1492 name, datetime);
1493 } else {
1494 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
1495 }
07424f16
DG
1496 if (ret < 0) {
1497 PERROR("snprintf uri subdir");
3dd05a85
DG
1498 ret = -LTTNG_ERR_FATAL;
1499 goto error;
07424f16
DG
1500 }
1501 }
1502
cac3069d
DG
1503 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1504 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1505
1506error:
1507 free(uris);
1508 return ret;
07424f16
DG
1509}
1510
806e2684
DG
1511/*
1512 * For a given session name, this call checks if the data is ready to be read
1513 * or is still being extracted by the consumer(s) hence not ready to be used by
1514 * any readers.
1515 */
6d805429 1516int lttng_data_pending(const char *session_name)
806e2684
DG
1517{
1518 int ret;
1519 struct lttcomm_session_msg lsm;
1520
1521 if (session_name == NULL) {
1522 return -LTTNG_ERR_INVALID;
1523 }
1524
6d805429 1525 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 1526
cac3069d
DG
1527 lttng_ctl_copy_string(lsm.session.name, session_name,
1528 sizeof(lsm.session.name));
806e2684 1529
cac3069d 1530 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
806e2684
DG
1531
1532 /*
cac3069d
DG
1533 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1534 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1535 * that the data is available. Yes it is hackish but for now this is the
1536 * only way.
806e2684
DG
1537 */
1538 if (ret == -1) {
1539 ret = 1;
1540 }
1541
1542 return ret;
1543}
1544
fac6795d
DG
1545/*
1546 * lib constructor
1547 */
1548static void __attribute__((constructor)) init()
1549{
1550 /* Set default session group */
bbccc3d2 1551 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
44a5e5eb
DG
1552 /* Set socket for health check */
1553 (void) set_health_socket_path();
fac6795d 1554}
49cca668
DG
1555
1556/*
1557 * lib destructor
1558 */
1559static void __attribute__((destructor)) lttng_ctl_exit()
1560{
1561 free(tracing_group);
1562}
This page took 0.118497 seconds and 4 git commands to generate.