Implement filter bytecode support in lttng-session, and parse filter string
[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
fac6795d 23#include <grp.h>
1e307fab 24#include <errno.h>
fac6795d
DG
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
990570ed
DG
30#include <common/common.h>
31#include <common/defaults.h>
db758600 32#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 33#include <lttng/lttng.h>
fac6795d 34
53a80697
MD
35#include "filter-parser.h"
36#include "filter-ast.h"
37#include "filter-bytecode.h"
38#include "memstream.h"
39
40#ifdef DEBUG
41const int print_xml = 1;
42#define dbg_printf(fmt, args...) \
43 printf("[debug liblttng-ctl] " fmt, ## args)
44#else
45const int print_xml = 0;
46#define dbg_printf(fmt, args...) \
47do { \
48 /* do nothing but check printf format */ \
49 if (0) \
50 printf("[debug liblttnctl] " fmt, ## args); \
51} while (0)
52#endif
53
54
fac6795d
DG
55/* Socket to session daemon for communication */
56static int sessiond_socket;
57static char sessiond_sock_path[PATH_MAX];
58
fac6795d
DG
59/* Variables */
60static char *tracing_group;
61static int connected;
62
97e19046
DG
63/* Global */
64
65/*
66 * Those two variables are used by error.h to silent or control the verbosity of
67 * error message. They are global to the library so application linking with it
68 * are able to compile correctly and also control verbosity of the library.
69 *
70 * Note that it is *not* possible to silent ERR() and PERROR() macros.
71 */
72int lttng_opt_quiet;
73int lttng_opt_verbose;
74
99497cd0
MD
75/*
76 * Copy string from src to dst and enforce null terminated byte.
77 */
78static void copy_string(char *dst, const char *src, size_t len)
79{
e7d6716d 80 if (src && dst) {
99497cd0
MD
81 strncpy(dst, src, len);
82 /* Enforce the NULL terminated byte */
83 dst[len - 1] = '\0';
cd80958d
DG
84 } else if (dst) {
85 dst[0] = '\0';
99497cd0
MD
86 }
87}
88
fac6795d 89/*
cd80958d 90 * Copy domain to lttcomm_session_msg domain.
fac6795d 91 *
cd80958d
DG
92 * If domain is unknown, default domain will be the kernel.
93 */
94static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
95{
96 if (src && dst) {
97 switch (src->type) {
00e2e675
DG
98 case LTTNG_DOMAIN_KERNEL:
99 case LTTNG_DOMAIN_UST:
100 /*
101 case LTTNG_DOMAIN_UST_EXEC_NAME:
102 case LTTNG_DOMAIN_UST_PID:
103 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
104 */
105 memcpy(dst, src, sizeof(struct lttng_domain));
106 break;
107 default:
108 memset(dst, 0, sizeof(struct lttng_domain));
109 dst->type = LTTNG_DOMAIN_KERNEL;
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) {
e065084a
DG
126 ret = -ENOTCONN;
127 goto end;
fac6795d
DG
128 }
129
be040666 130 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 131 sizeof(struct lttcomm_session_msg));
e065084a
DG
132
133end:
134 return ret;
135}
136
53a80697
MD
137/*
138 * Send var len data to the session daemon.
139 *
140 * On success, returns the number of bytes sent (>=0)
141 * On error, returns -1
142 */
143static int send_session_varlen(void *data, size_t len)
144{
145 int ret;
146
147 if (!connected) {
148 ret = -ENOTCONN;
149 goto end;
150 }
151 if (!data || !len) {
152 ret = 0;
153 goto end;
154 }
155
156 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
157
158end:
159 return ret;
160}
161
e065084a 162/*
cd80958d 163 * Receive data from the sessiond socket.
e065084a 164 *
1c8d13c8
TD
165 * On success, returns the number of bytes received (>=0)
166 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 167 */
ca95a216 168static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
169{
170 int ret;
171
172 if (!connected) {
173 ret = -ENOTCONN;
174 goto end;
fac6795d
DG
175 }
176
ca95a216 177 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
fac6795d 178
e065084a 179end:
fac6795d
DG
180 return ret;
181}
182
183/*
1c8d13c8 184 * Check if we are in the specified group.
65beb5ff 185 *
2269e89e 186 * If yes return 1, else return -1.
947308c4
DG
187 */
188static int check_tracing_group(const char *grp_name)
189{
190 struct group *grp_tracing; /* no free(). See getgrnam(3) */
191 gid_t *grp_list;
192 int grp_list_size, grp_id, i;
193 int ret = -1;
194
195 /* Get GID of group 'tracing' */
196 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
197 if (!grp_tracing) {
198 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
199 goto end;
200 }
201
202 /* Get number of supplementary group IDs */
203 grp_list_size = getgroups(0, NULL);
204 if (grp_list_size < 0) {
205 perror("getgroups");
206 goto end;
207 }
208
209 /* Alloc group list of the right size */
210 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 211 if (!grp_list) {
1c8d13c8 212 perror("malloc");
00795392
MD
213 goto end;
214 }
947308c4 215 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 216 if (grp_id < 0) {
947308c4
DG
217 perror("getgroups");
218 goto free_list;
219 }
220
221 for (i = 0; i < grp_list_size; i++) {
222 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 223 ret = 1;
947308c4
DG
224 break;
225 }
226 }
227
228free_list:
229 free(grp_list);
230
231end:
232 return ret;
233}
234
235/*
2269e89e
DG
236 * Try connect to session daemon with sock_path.
237 *
238 * Return 0 on success, else -1
239 */
240static int try_connect_sessiond(const char *sock_path)
241{
242 int ret;
243
244 /* If socket exist, we check if the daemon listens for connect. */
245 ret = access(sock_path, F_OK);
246 if (ret < 0) {
247 /* Not alive */
248 return -1;
249 }
250
251 ret = lttcomm_connect_unix_sock(sock_path);
252 if (ret < 0) {
253 /* Not alive */
254 return -1;
255 }
256
257 ret = lttcomm_close_unix_sock(ret);
258 if (ret < 0) {
259 perror("lttcomm_close_unix_sock");
260 }
261
262 return 0;
263}
264
265/*
1c8d13c8
TD
266 * Set sessiond socket path by putting it in the global
267 * sessiond_sock_path variable.
268 * Returns 0 on success,
269 * -ENOMEM on failure (the sessiond socket path is somehow too long)
947308c4
DG
270 */
271static int set_session_daemon_path(void)
272{
273 int ret;
2269e89e
DG
274 int in_tgroup = 0; /* In tracing group */
275 uid_t uid;
276
277 uid = getuid();
947308c4 278
2269e89e
DG
279 if (uid != 0) {
280 /* Are we in the tracing group ? */
281 in_tgroup = check_tracing_group(tracing_group);
282 }
283
08a9c49f
TD
284 if ((uid == 0) || in_tgroup) {
285 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
2269e89e 286 sizeof(sessiond_sock_path));
08a9c49f 287 }
2269e89e 288
08a9c49f
TD
289 if (uid != 0) {
290 if (in_tgroup) {
291 /* Tracing group */
292 ret = try_connect_sessiond(sessiond_sock_path);
293 if (ret >= 0) {
294 goto end;
2269e89e 295 }
08a9c49f 296 /* Global session daemon not available... */
2269e89e 297 }
08a9c49f
TD
298 /* ...or not in tracing group (and not root), default */
299
300 /*
301 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
302 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
303 */
304 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
305 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
306 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
947308c4
DG
307 return -ENOMEM;
308 }
947308c4 309 }
08a9c49f 310end:
947308c4
DG
311 return 0;
312}
313
65beb5ff
DG
314/*
315 * Connect to the LTTng session daemon.
316 *
317 * On success, return 0. On error, return -1.
318 */
319static int connect_sessiond(void)
320{
321 int ret;
322
323 ret = set_session_daemon_path();
324 if (ret < 0) {
1c8d13c8 325 return -1; /* set_session_daemon_path() returns -ENOMEM */
65beb5ff
DG
326 }
327
328 /* Connect to the sesssion daemon */
329 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
330 if (ret < 0) {
331 return ret;
332 }
333
334 sessiond_socket = ret;
335 connected = 1;
336
337 return 0;
338}
339
340/*
1c8d13c8
TD
341 * Clean disconnect from the session daemon.
342 * On success, return 0. On error, return -1.
65beb5ff
DG
343 */
344static int disconnect_sessiond(void)
345{
346 int ret = 0;
347
348 if (connected) {
349 ret = lttcomm_close_unix_sock(sessiond_socket);
350 sessiond_socket = 0;
351 connected = 0;
352 }
353
354 return ret;
355}
356
35a6fdb7 357/*
cd80958d 358 * Ask the session daemon a specific command and put the data into buf.
53a80697 359 * Takes extra var. len. data as input to send to the session daemon.
65beb5ff 360 *
af87c45a 361 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 362 */
53a80697
MD
363static int ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
364 void *vardata,
365 size_t varlen,
366 void **buf)
65beb5ff
DG
367{
368 int ret;
369 size_t size;
370 void *data = NULL;
cd80958d 371 struct lttcomm_lttng_msg llm;
65beb5ff
DG
372
373 ret = connect_sessiond();
374 if (ret < 0) {
375 goto end;
376 }
377
65beb5ff 378 /* Send command to session daemon */
cd80958d 379 ret = send_session_msg(lsm);
65beb5ff
DG
380 if (ret < 0) {
381 goto end;
382 }
53a80697
MD
383 /* Send var len data */
384 ret = send_session_varlen(vardata, varlen);
385 if (ret < 0) {
386 goto end;
387 }
65beb5ff
DG
388
389 /* Get header from data transmission */
390 ret = recv_data_sessiond(&llm, sizeof(llm));
391 if (ret < 0) {
392 goto end;
393 }
394
395 /* Check error code if OK */
396 if (llm.ret_code != LTTCOMM_OK) {
397 ret = -llm.ret_code;
398 goto end;
399 }
400
401 size = llm.data_size;
402 if (size == 0) {
874d3f84 403 /* If client free with size 0 */
a45d5536
DG
404 if (buf != NULL) {
405 *buf = NULL;
406 }
7d29a247 407 ret = 0;
65beb5ff
DG
408 goto end;
409 }
410
411 data = (void*) malloc(size);
412
413 /* Get payload data */
414 ret = recv_data_sessiond(data, size);
415 if (ret < 0) {
416 free(data);
417 goto end;
418 }
419
83009e5e
DG
420 /*
421 * Extra protection not to dereference a NULL pointer. If buf is NULL at
422 * this point, an error is returned and data is freed.
423 */
424 if (buf == NULL) {
425 ret = -1;
426 free(data);
427 goto end;
428 }
429
65beb5ff
DG
430 *buf = data;
431 ret = size;
432
433end:
434 disconnect_sessiond();
435 return ret;
436}
437
53a80697
MD
438/*
439 * Ask the session daemon a specific command and put the data into buf.
440 *
441 * Return size of data (only payload, not header) or a negative error code.
442 */
443static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
444{
445 return ask_sessiond_varlen(lsm, NULL, 0, buf);
446}
447
9f19cc17 448/*
cd80958d 449 * Create lttng handle and return pointer.
1c8d13c8 450 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 451 */
cd80958d
DG
452struct lttng_handle *lttng_create_handle(const char *session_name,
453 struct lttng_domain *domain)
9f19cc17 454{
cd80958d
DG
455 struct lttng_handle *handle;
456
457 handle = malloc(sizeof(struct lttng_handle));
458 if (handle == NULL) {
459 perror("malloc handle");
460 goto end;
461 }
462
463 /* Copy session name */
464 copy_string(handle->session_name, session_name,
465 sizeof(handle->session_name));
466
467 /* Copy lttng domain */
468 copy_lttng_domain(&handle->domain, domain);
469
470end:
471 return handle;
472}
473
474/*
475 * Destroy handle by free(3) the pointer.
476 */
477void lttng_destroy_handle(struct lttng_handle *handle)
478{
479 if (handle) {
480 free(handle);
eb354453
DG
481 }
482}
483
d9800920
DG
484/*
485 * Register an outside consumer.
1c8d13c8 486 * Returns size of returned session payload data or a negative error code.
d9800920
DG
487 */
488int lttng_register_consumer(struct lttng_handle *handle,
489 const char *socket_path)
490{
491 struct lttcomm_session_msg lsm;
492
493 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
494 copy_string(lsm.session.name, handle->session_name,
495 sizeof(lsm.session.name));
496 copy_lttng_domain(&lsm.domain, &handle->domain);
497
498 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
499
500 return ask_sessiond(&lsm, NULL);
501}
502
1df4dedd 503/*
1c8d13c8
TD
504 * Start tracing for all traces of the session.
505 * Returns size of returned session payload data or a negative error code.
1df4dedd 506 */
6a4f824d 507int lttng_start_tracing(const char *session_name)
f3ed775e 508{
cd80958d
DG
509 struct lttcomm_session_msg lsm;
510
6a4f824d 511 if (session_name == NULL) {
cd80958d
DG
512 return -1;
513 }
514
515 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d
DG
516
517 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
518
519 return ask_sessiond(&lsm, NULL);
f3ed775e 520}
1df4dedd
DG
521
522/*
1c8d13c8
TD
523 * Stop tracing for all traces of the session.
524 * Returns size of returned session payload data or a negative error code.
f3ed775e 525 */
6a4f824d 526int lttng_stop_tracing(const char *session_name)
f3ed775e 527{
cd80958d
DG
528 struct lttcomm_session_msg lsm;
529
6a4f824d
DG
530 if (session_name == NULL) {
531 return -1;
532 }
533
cd80958d 534 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d
DG
535
536 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
537
538 return ask_sessiond(&lsm, NULL);
f3ed775e
DG
539}
540
541/*
1e46a50f
TD
542 * Add context to event and/or channel.
543 * If event_name is NULL, the context is applied to all events of the channel.
544 * If channel_name is NULL, a lookup of the event's channel is done.
545 * If both are NULL, the context is applied to all events of all channels.
af87c45a
DG
546 *
547 * Returns the size of the returned payload data or a negative error code.
1df4dedd 548 */
cd80958d 549int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
550 struct lttng_event_context *ctx, const char *event_name,
551 const char *channel_name)
d65106b1 552{
cd80958d
DG
553 struct lttcomm_session_msg lsm;
554
9d697d3d
DG
555 /* Safety check. Both are mandatory */
556 if (handle == NULL || ctx == NULL) {
cd80958d
DG
557 return -1;
558 }
559
441c16a7
MD
560 memset(&lsm, 0, sizeof(lsm));
561
cd80958d
DG
562 lsm.cmd_type = LTTNG_ADD_CONTEXT;
563
564 /* Copy channel name */
565 copy_string(lsm.u.context.channel_name, channel_name,
566 sizeof(lsm.u.context.channel_name));
567 /* Copy event name */
568 copy_string(lsm.u.context.event_name, event_name,
569 sizeof(lsm.u.context.event_name));
570
571 copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 572
9d697d3d 573 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 574
cd80958d
DG
575 copy_string(lsm.session.name, handle->session_name,
576 sizeof(lsm.session.name));
577
578 return ask_sessiond(&lsm, NULL);
d65106b1
DG
579}
580
f3ed775e 581/*
1c8d13c8
TD
582 * Enable event(s) for a channel.
583 * If no event name is specified, all events are enabled.
584 * If no channel name is specified, the default 'channel0' is used.
585 * Returns size of returned session payload data or a negative error code.
f3ed775e 586 */
cd80958d 587int lttng_enable_event(struct lttng_handle *handle,
38057ed1 588 struct lttng_event *ev, const char *channel_name)
1df4dedd 589{
cd80958d
DG
590 struct lttcomm_session_msg lsm;
591
5117eeec 592 if (handle == NULL || ev == NULL) {
cd80958d
DG
593 return -1;
594 }
33a2b854 595
441c16a7
MD
596 memset(&lsm, 0, sizeof(lsm));
597
5117eeec 598 /* If no channel name, we put the default name */
94cf3c47 599 if (channel_name == NULL) {
cd80958d
DG
600 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
601 sizeof(lsm.u.enable.channel_name));
33a2b854 602 } else {
cd80958d
DG
603 copy_string(lsm.u.enable.channel_name, channel_name,
604 sizeof(lsm.u.enable.channel_name));
eb354453
DG
605 }
606
cd80958d 607 copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 608
8c9ae521 609 if (ev->name[0] != '\0') {
cd80958d 610 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 611 } else {
cd80958d 612 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 613 }
8c9ae521 614 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 615
cd80958d
DG
616 copy_string(lsm.session.name, handle->session_name,
617 sizeof(lsm.session.name));
618
619 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
620}
621
53a80697
MD
622/*
623 * set filter for an event
624 * Return negative error value on error.
625 * Return size of returned session payload data if OK.
626 */
627
628int lttng_set_event_filter(struct lttng_handle *handle,
629 const char *event_name, const char *channel_name,
630 const char *filter_expression)
631{
632 struct lttcomm_session_msg lsm;
633 struct filter_parser_ctx *ctx;
634 FILE *fmem;
635 int ret = 0;
636
637 /* Safety check. */
638 if (handle == NULL) {
639 return -1;
640 }
641
642 if (!filter_expression) {
643 return 0;
644 }
645
646 /*
647 * casting const to non-const, as the underlying function will
648 * use it in read-only mode.
649 */
650 fmem = lttng_fmemopen((void *) filter_expression,
651 strlen(filter_expression), "r");
652 if (!fmem) {
653 fprintf(stderr, "Error opening memory as stream\n");
654 return -ENOMEM;
655 }
656 ctx = filter_parser_ctx_alloc(fmem);
657 if (!ctx) {
658 fprintf(stderr, "Error allocating parser\n");
659 ret = -ENOMEM;
660 goto alloc_error;
661 }
662 ret = filter_parser_ctx_append_ast(ctx);
663 if (ret) {
664 fprintf(stderr, "Parse error\n");
665 ret = -EINVAL;
666 goto parse_error;
667 }
668 ret = filter_visitor_set_parent(ctx);
669 if (ret) {
670 fprintf(stderr, "Set parent error\n");
671 ret = -EINVAL;
672 goto parse_error;
673 }
674 if (print_xml) {
675 ret = filter_visitor_print_xml(ctx, stdout, 0);
676 if (ret) {
677 fflush(stdout);
678 fprintf(stderr, "XML print error\n");
679 ret = -EINVAL;
680 goto parse_error;
681 }
682 }
683
684 dbg_printf("Generating IR... ");
685 fflush(stdout);
686 ret = filter_visitor_ir_generate(ctx);
687 if (ret) {
688 fprintf(stderr, "Generate IR error\n");
689 ret = -EINVAL;
690 goto parse_error;
691 }
692 dbg_printf("done\n");
693
694 dbg_printf("Validating IR... ");
695 fflush(stdout);
696 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
697 if (ret) {
698 ret = -EINVAL;
699 goto parse_error;
700 }
701 dbg_printf("done\n");
702
703 dbg_printf("Generating bytecode... ");
704 fflush(stdout);
705 ret = filter_visitor_bytecode_generate(ctx);
706 if (ret) {
707 fprintf(stderr, "Generate bytecode error\n");
708 ret = -EINVAL;
709 goto parse_error;
710 }
711 dbg_printf("done\n");
712 dbg_printf("Size of bytecode generated: %u bytes.\n",
713 bytecode_get_len(&ctx->bytecode->b));
714
715 memset(&lsm, 0, sizeof(lsm));
716
717 lsm.cmd_type = LTTNG_SET_FILTER;
718
719 /* Copy channel name */
720 copy_string(lsm.u.filter.channel_name, channel_name,
721 sizeof(lsm.u.filter.channel_name));
722 /* Copy event name */
723 copy_string(lsm.u.filter.event_name, event_name,
724 sizeof(lsm.u.filter.event_name));
725 lsm.u.filter.bytecode_len = sizeof(ctx->bytecode->b)
726 + bytecode_get_len(&ctx->bytecode->b);
727
728 copy_lttng_domain(&lsm.domain, &handle->domain);
729
730 copy_string(lsm.session.name, handle->session_name,
731 sizeof(lsm.session.name));
732
733 ret = ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
734 lsm.u.filter.bytecode_len, NULL);
735
736 filter_bytecode_free(ctx);
737 filter_ir_free(ctx);
738 filter_parser_ctx_free(ctx);
739 if (fclose(fmem) != 0) {
740 perror("fclose");
741 }
742 return ret;
743
744parse_error:
745 filter_bytecode_free(ctx);
746 filter_ir_free(ctx);
747 filter_parser_ctx_free(ctx);
748alloc_error:
749 if (fclose(fmem) != 0) {
750 perror("fclose");
751 }
752 return ret;
753}
754
1df4dedd 755/*
1c8d13c8
TD
756 * Disable event(s) of a channel and domain.
757 * If no event name is specified, all events are disabled.
758 * If no channel name is specified, the default 'channel0' is used.
759 * Returns size of returned session payload data or a negative error code.
1df4dedd 760 */
cd80958d 761int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 762 const char *channel_name)
1df4dedd 763{
cd80958d 764 struct lttcomm_session_msg lsm;
1df4dedd 765
9d697d3d 766 if (handle == NULL) {
cd80958d
DG
767 return -1;
768 }
769
441c16a7
MD
770 memset(&lsm, 0, sizeof(lsm));
771
cd80958d
DG
772 if (channel_name) {
773 copy_string(lsm.u.disable.channel_name, channel_name,
774 sizeof(lsm.u.disable.channel_name));
f3ed775e 775 } else {
cd80958d
DG
776 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
777 sizeof(lsm.u.disable.channel_name));
eb354453
DG
778 }
779
cd80958d 780 copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 781
f84efadf 782 if (name != NULL) {
cd80958d
DG
783 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
784 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 785 } else {
cd80958d 786 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
787 }
788
cd80958d
DG
789 copy_string(lsm.session.name, handle->session_name,
790 sizeof(lsm.session.name));
791
792 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
793}
794
795/*
1c8d13c8
TD
796 * Enable channel per domain
797 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 798 */
cd80958d 799int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 800 struct lttng_channel *chan)
a5c5a2bd 801{
cd80958d
DG
802 struct lttcomm_session_msg lsm;
803
5117eeec
DG
804 /*
805 * NULL arguments are forbidden. No default values.
806 */
807 if (handle == NULL || chan == NULL) {
cd80958d
DG
808 return -1;
809 }
810
441c16a7
MD
811 memset(&lsm, 0, sizeof(lsm));
812
5117eeec 813 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 814
cd80958d
DG
815 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
816
817 copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 818
cd80958d
DG
819 copy_string(lsm.session.name, handle->session_name,
820 sizeof(lsm.session.name));
821
822 return ask_sessiond(&lsm, NULL);
8c0faa1d 823}
1df4dedd 824
2ef84c95 825/*
1c8d13c8
TD
826 * All tracing will be stopped for registered events of the channel.
827 * Returns size of returned session payload data or a negative error code.
2ef84c95 828 */
cd80958d 829int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 830{
cd80958d
DG
831 struct lttcomm_session_msg lsm;
832
9d697d3d
DG
833 /* Safety check. Both are mandatory */
834 if (handle == NULL || name == NULL) {
cd80958d
DG
835 return -1;
836 }
837
441c16a7
MD
838 memset(&lsm, 0, sizeof(lsm));
839
cd80958d 840 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 841
9d697d3d
DG
842 copy_string(lsm.u.disable.channel_name, name,
843 sizeof(lsm.u.disable.channel_name));
844
cd80958d
DG
845 copy_lttng_domain(&lsm.domain, &handle->domain);
846
847 copy_string(lsm.session.name, handle->session_name,
848 sizeof(lsm.session.name));
849
850 return ask_sessiond(&lsm, NULL);
ca95a216
DG
851}
852
fac6795d 853/*
1c8d13c8
TD
854 * Lists all available tracepoints of domain.
855 * Sets the contents of the events array.
856 * Returns the number of lttng_event entries in events;
857 * on error, returns a negative value.
fac6795d 858 */
cd80958d 859int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 860 struct lttng_event **events)
fac6795d 861{
052da939 862 int ret;
cd80958d
DG
863 struct lttcomm_session_msg lsm;
864
9d697d3d 865 if (handle == NULL) {
cd80958d
DG
866 return -1;
867 }
fac6795d 868
cd80958d
DG
869 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
870 copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 871
cd80958d 872 ret = ask_sessiond(&lsm, (void **) events);
052da939
DG
873 if (ret < 0) {
874 return ret;
eb354453 875 }
fac6795d 876
9f19cc17 877 return ret / sizeof(struct lttng_event);
fac6795d
DG
878}
879
f37d259d
MD
880/*
881 * Lists all available tracepoint fields of domain.
882 * Sets the contents of the event field array.
883 * Returns the number of lttng_event_field entries in events;
884 * on error, returns a negative value.
885 */
886int lttng_list_tracepoint_fields(struct lttng_handle *handle,
887 struct lttng_event_field **fields)
888{
889 int ret;
890 struct lttcomm_session_msg lsm;
891
892 if (handle == NULL) {
893 return -1;
894 }
895
896 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
897 copy_lttng_domain(&lsm.domain, &handle->domain);
898
899 ret = ask_sessiond(&lsm, (void **) fields);
900 if (ret < 0) {
901 return ret;
902 }
903
904 return ret / sizeof(struct lttng_event_field);
905}
906
1657e9bb 907/*
1c8d13c8
TD
908 * Returns a human readable string describing
909 * the error code (a negative value).
1657e9bb 910 */
9a745bc7 911const char *lttng_strerror(int code)
1657e9bb 912{
1c8d13c8 913 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
7d29a247
DG
914 if (code > -LTTCOMM_OK) {
915 return "Ended with errors";
1657e9bb
DG
916 }
917
7d29a247 918 return lttcomm_get_readable_code(code);
1657e9bb
DG
919}
920
aaf97519 921/*
1c8d13c8
TD
922 * Create a brand new session using name and path.
923 * Returns size of returned session payload data or a negative error code.
aaf97519 924 */
38057ed1 925int lttng_create_session(const char *name, const char *path)
aaf97519 926{
cd80958d
DG
927 struct lttcomm_session_msg lsm;
928
929 lsm.cmd_type = LTTNG_CREATE_SESSION;
930 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
931 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
932
933 return ask_sessiond(&lsm, NULL);
8028d920
DG
934}
935
00e2e675
DG
936/*
937 * Create a new tracing session using a name, URIs and a consumer enable flag.
938 */
939int lttng_create_session_uri(const char *name, struct lttng_uri *ctrl_uri,
940 struct lttng_uri *data_uri, unsigned int enable_consumer)
941{
942 struct lttcomm_session_msg lsm;
943
944 /* Name and ctrl_uri are mandatory */
945 if (name == NULL || ctrl_uri == NULL) {
946 return -1;
947 }
948
949 lsm.cmd_type = LTTNG_CREATE_SESSION_URI;
950
951 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
952 /* Anything bigger than zero, the consumer(s) will be enabled */
953 lsm.u.create_uri.enable_consumer = enable_consumer;
954 memcpy(&lsm.u.create_uri.ctrl_uri, ctrl_uri,
955 sizeof(lsm.u.create_uri.ctrl_uri));
956 if (data_uri) {
957 /*
958 * The only possible scenario where data_uri is NULL is for a local
959 * consumer where the output is at a specified path name on the
960 * filesystem.
961 */
962 memcpy(&lsm.u.create_uri.data_uri, data_uri,
963 sizeof(lsm.u.create_uri.data_uri));
964 }
965
966 return ask_sessiond(&lsm, NULL);
967}
968
8028d920 969/*
8028d920 970 * Destroy session using name.
1c8d13c8 971 * Returns size of returned session payload data or a negative error code.
8028d920 972 */
843f5df9 973int lttng_destroy_session(const char *session_name)
8028d920 974{
cd80958d
DG
975 struct lttcomm_session_msg lsm;
976
843f5df9 977 if (session_name == NULL) {
cd80958d
DG
978 return -1;
979 }
980
981 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9
DG
982
983 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
984
985 return ask_sessiond(&lsm, NULL);
aaf97519
DG
986}
987
57167058 988/*
57167058 989 * Ask the session daemon for all available sessions.
1c8d13c8
TD
990 * Sets the contents of the sessions array.
991 * Returns the number of lttng_session entries in sessions;
992 * on error, returns a negative value.
57167058 993 */
ca95a216 994int lttng_list_sessions(struct lttng_session **sessions)
57167058 995{
ca95a216 996 int ret;
cd80958d 997 struct lttcomm_session_msg lsm;
57167058 998
cd80958d
DG
999 lsm.cmd_type = LTTNG_LIST_SESSIONS;
1000 ret = ask_sessiond(&lsm, (void**) sessions);
57167058 1001 if (ret < 0) {
ca95a216 1002 return ret;
57167058
DG
1003 }
1004
ca95a216 1005 return ret / sizeof(struct lttng_session);
57167058
DG
1006}
1007
9f19cc17 1008/*
1c8d13c8
TD
1009 * Ask the session daemon for all available domains of a session.
1010 * Sets the contents of the domains array.
1011 * Returns the number of lttng_domain entries in domains;
1012 * on error, returns a negative value.
9f19cc17 1013 */
330be774 1014int lttng_list_domains(const char *session_name,
cd80958d 1015 struct lttng_domain **domains)
9f19cc17
DG
1016{
1017 int ret;
cd80958d
DG
1018 struct lttcomm_session_msg lsm;
1019
330be774 1020 if (session_name == NULL) {
cd80958d
DG
1021 return -1;
1022 }
9f19cc17 1023
cd80958d
DG
1024 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1025
330be774 1026 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
1027
1028 ret = ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1029 if (ret < 0) {
1030 return ret;
1031 }
1032
1033 return ret / sizeof(struct lttng_domain);
1034}
1035
1036/*
1c8d13c8
TD
1037 * Ask the session daemon for all available channels of a session.
1038 * Sets the contents of the channels array.
1039 * Returns the number of lttng_channel entries in channels;
1040 * on error, returns a negative value.
9f19cc17 1041 */
cd80958d
DG
1042int lttng_list_channels(struct lttng_handle *handle,
1043 struct lttng_channel **channels)
9f19cc17
DG
1044{
1045 int ret;
cd80958d
DG
1046 struct lttcomm_session_msg lsm;
1047
9d697d3d 1048 if (handle == NULL) {
cd80958d
DG
1049 return -1;
1050 }
1051
1052 lsm.cmd_type = LTTNG_LIST_CHANNELS;
1053 copy_string(lsm.session.name, handle->session_name,
1054 sizeof(lsm.session.name));
9f19cc17 1055
cd80958d 1056 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1057
cd80958d 1058 ret = ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
1059 if (ret < 0) {
1060 return ret;
1061 }
1062
1063 return ret / sizeof(struct lttng_channel);
1064}
1065
1066/*
1c8d13c8
TD
1067 * Ask the session daemon for all available events of a session channel.
1068 * Sets the contents of the events array.
1069 * Returns the number of lttng_event entries in events;
1070 * on error, returns a negative value.
9f19cc17 1071 */
cd80958d
DG
1072int lttng_list_events(struct lttng_handle *handle,
1073 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1074{
1075 int ret;
cd80958d 1076 struct lttcomm_session_msg lsm;
9f19cc17 1077
9d697d3d
DG
1078 /* Safety check. An handle and channel name are mandatory */
1079 if (handle == NULL || channel_name == NULL) {
cd80958d
DG
1080 return -1;
1081 }
1082
1083 lsm.cmd_type = LTTNG_LIST_EVENTS;
1084 copy_string(lsm.session.name, handle->session_name,
1085 sizeof(lsm.session.name));
1086 copy_string(lsm.u.list.channel_name, channel_name,
1087 sizeof(lsm.u.list.channel_name));
1088
1089 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1090
cd80958d 1091 ret = ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
1092 if (ret < 0) {
1093 return ret;
1094 }
1095
1096 return ret / sizeof(struct lttng_event);
1097}
1098
fac6795d 1099/*
1c8d13c8
TD
1100 * Sets the tracing_group variable with name.
1101 * This function allocates memory pointed to by tracing_group.
1102 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
1103 */
1104int lttng_set_tracing_group(const char *name)
1105{
9d697d3d
DG
1106 if (name == NULL) {
1107 return -1;
1108 }
1109
fac6795d
DG
1110 if (asprintf(&tracing_group, "%s", name) < 0) {
1111 return -ENOMEM;
1112 }
1113
1114 return 0;
1115}
1116
d0254c7c 1117/*
af87c45a 1118 * Returns size of returned session payload data or a negative error code.
d0254c7c 1119 */
cd80958d 1120int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
1121 struct lttng_calibrate *calibrate)
1122{
cd80958d 1123 struct lttcomm_session_msg lsm;
d0254c7c 1124
9d697d3d
DG
1125 /* Safety check. NULL pointer are forbidden */
1126 if (handle == NULL || calibrate == NULL) {
cd80958d
DG
1127 return -1;
1128 }
d0254c7c 1129
cd80958d
DG
1130 lsm.cmd_type = LTTNG_CALIBRATE;
1131 copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 1132
cd80958d
DG
1133 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1134
1135 return ask_sessiond(&lsm, NULL);
d0254c7c
MD
1136}
1137
5edd7e09
DG
1138/*
1139 * Set default channel attributes.
441c16a7 1140 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
1141 */
1142void lttng_channel_set_default_attr(struct lttng_domain *domain,
1143 struct lttng_channel_attr *attr)
1144{
1145 /* Safety check */
1146 if (attr == NULL || domain == NULL) {
1147 return;
1148 }
1149
eacaa7a6
DS
1150 memset(attr, 0, sizeof(struct lttng_channel_attr));
1151
5edd7e09
DG
1152 switch (domain->type) {
1153 case LTTNG_DOMAIN_KERNEL:
1154 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1155 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1156 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1157
1158 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1159 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1160 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1161 break;
1162 case LTTNG_DOMAIN_UST:
d78d6610 1163#if 0
5edd7e09
DG
1164 case LTTNG_DOMAIN_UST_EXEC_NAME:
1165 case LTTNG_DOMAIN_UST_PID:
1166 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 1167#endif
5edd7e09
DG
1168 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1169 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1170 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1171
1172 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
1173 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
1174 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
1175 break;
1176 default:
441c16a7 1177 /* Default behavior: leave set to 0. */
5edd7e09
DG
1178 break;
1179 }
1180}
1181
fac6795d 1182/*
2269e89e 1183 * Check if session daemon is alive.
fac6795d 1184 *
2269e89e 1185 * Return 1 if alive or 0 if not.
1c8d13c8 1186 * On error returns a negative value.
fac6795d 1187 */
947308c4 1188int lttng_session_daemon_alive(void)
fac6795d
DG
1189{
1190 int ret;
1191
1192 ret = set_session_daemon_path();
1193 if (ret < 0) {
947308c4 1194 /* Error */
fac6795d
DG
1195 return ret;
1196 }
1197
2269e89e
DG
1198 if (strlen(sessiond_sock_path) == 0) {
1199 /* No socket path set. Weird error */
1200 return -1;
fac6795d
DG
1201 }
1202
2269e89e 1203 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
1204 if (ret < 0) {
1205 /* Not alive */
1206 return 0;
1207 }
7d8234d9 1208
947308c4
DG
1209 /* Is alive */
1210 return 1;
fac6795d
DG
1211}
1212
00e2e675
DG
1213/*
1214 * Set URI for a consumer for a session and domain.
1215 *
1216 * Return 0 on success, else a negative value.
1217 */
1218int lttng_set_consumer_uri(struct lttng_handle *handle, struct lttng_uri *uri)
1219{
1220 struct lttcomm_session_msg lsm;
1221
1222 if (handle == NULL || uri == NULL) {
1223 return -1;
1224 }
1225
1226 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1227
1228 copy_string(lsm.session.name, handle->session_name,
1229 sizeof(lsm.session.name));
1230 copy_lttng_domain(&lsm.domain, &handle->domain);
1231
1232 memcpy(&lsm.u.uri, uri, sizeof(lsm.u.uri));
1233
1234 return ask_sessiond(&lsm, NULL);
1235}
1236
1237/*
1238 * Enable consumer for a session and domain.
1239 *
1240 * Return 0 on success, else a negative value.
1241 */
1242int lttng_enable_consumer(struct lttng_handle *handle)
1243{
1244 struct lttcomm_session_msg lsm;
1245
1246 if (handle == NULL) {
1247 return -1;
1248 }
1249
1250 lsm.cmd_type = LTTNG_ENABLE_CONSUMER;
1251
1252 copy_string(lsm.session.name, handle->session_name,
1253 sizeof(lsm.session.name));
1254 copy_lttng_domain(&lsm.domain, &handle->domain);
1255
1256 return ask_sessiond(&lsm, NULL);
1257}
1258
1259/*
1260 * Disable consumer for a session and domain.
1261 *
1262 * Return 0 on success, else a negative value.
1263 */
1264int lttng_disable_consumer(struct lttng_handle *handle)
1265{
1266 struct lttcomm_session_msg lsm;
1267
1268 if (handle == NULL) {
1269 return -1;
1270 }
1271
1272 lsm.cmd_type = LTTNG_DISABLE_CONSUMER;
1273
1274 copy_string(lsm.session.name, handle->session_name,
1275 sizeof(lsm.session.name));
1276 copy_lttng_domain(&lsm.domain, &handle->domain);
1277
1278 return ask_sessiond(&lsm, NULL);
1279}
1280
fac6795d
DG
1281/*
1282 * lib constructor
1283 */
1284static void __attribute__((constructor)) init()
1285{
1286 /* Set default session group */
bbccc3d2 1287 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 1288}
This page took 0.115423 seconds and 4 git commands to generate.