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