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