lttng-ctl: locate extended event attributes at reception
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
1 /*
2 * lttng-ctl.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 <common/dynamic-buffer.h>
38 #include <lttng/lttng.h>
39 #include <lttng/health-internal.h>
40 #include <lttng/trigger/trigger-internal.h>
41 #include <lttng/endpoint.h>
42 #include <lttng/channel-internal.h>
43 #include <lttng/event-internal.h>
44
45 #include "filter/filter-ast.h"
46 #include "filter/filter-parser.h"
47 #include "filter/filter-bytecode.h"
48 #include "filter/memstream.h"
49 #include "lttng-ctl-helper.h"
50
51 #ifdef DEBUG
52 static const int print_xml = 1;
53 #define dbg_printf(fmt, args...) \
54 printf("[debug liblttng-ctl] " fmt, ## args)
55 #else
56 static const int print_xml = 0;
57 #define dbg_printf(fmt, args...) \
58 do { \
59 /* do nothing but check printf format */ \
60 if (0) \
61 printf("[debug liblttnctl] " fmt, ## args); \
62 } while (0)
63 #endif
64
65
66 /* Socket to session daemon for communication */
67 static int sessiond_socket;
68 static char sessiond_sock_path[PATH_MAX];
69
70 /* Variables */
71 static char *tracing_group;
72 static int connected;
73
74 /* Global */
75
76 /*
77 * Those two variables are used by error.h to silent or control the verbosity of
78 * error message. They are global to the library so application linking with it
79 * are able to compile correctly and also control verbosity of the library.
80 */
81 int lttng_opt_quiet;
82 int lttng_opt_verbose;
83 int lttng_opt_mi;
84
85 /*
86 * Copy string from src to dst and enforce null terminated byte.
87 */
88 LTTNG_HIDDEN
89 void lttng_ctl_copy_string(char *dst, const char *src, size_t len)
90 {
91 if (src && dst) {
92 strncpy(dst, src, len);
93 /* Enforce the NULL terminated byte */
94 dst[len - 1] = '\0';
95 } else if (dst) {
96 dst[0] = '\0';
97 }
98 }
99
100 /*
101 * Copy domain to lttcomm_session_msg domain.
102 *
103 * If domain is unknown, default domain will be the kernel.
104 */
105 LTTNG_HIDDEN
106 void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
107 struct lttng_domain *src)
108 {
109 if (src && dst) {
110 switch (src->type) {
111 case LTTNG_DOMAIN_KERNEL:
112 case LTTNG_DOMAIN_UST:
113 case LTTNG_DOMAIN_JUL:
114 case LTTNG_DOMAIN_LOG4J:
115 case LTTNG_DOMAIN_PYTHON:
116 memcpy(dst, src, sizeof(struct lttng_domain));
117 break;
118 default:
119 memset(dst, 0, sizeof(struct lttng_domain));
120 break;
121 }
122 }
123 }
124
125 /*
126 * Send lttcomm_session_msg to the session daemon.
127 *
128 * On success, returns the number of bytes sent (>=0)
129 * On error, returns -1
130 */
131 static int send_session_msg(struct lttcomm_session_msg *lsm)
132 {
133 int ret;
134
135 if (!connected) {
136 ret = -LTTNG_ERR_NO_SESSIOND;
137 goto end;
138 }
139
140 DBG("LSM cmd type : %d", lsm->cmd_type);
141
142 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
143 sizeof(struct lttcomm_session_msg));
144 if (ret < 0) {
145 ret = -LTTNG_ERR_FATAL;
146 }
147
148 end:
149 return ret;
150 }
151
152 /*
153 * Send var len data to the session daemon.
154 *
155 * On success, returns the number of bytes sent (>=0)
156 * On error, returns -1
157 */
158 static int send_session_varlen(const void *data, size_t len)
159 {
160 int ret;
161
162 if (!connected) {
163 ret = -LTTNG_ERR_NO_SESSIOND;
164 goto end;
165 }
166
167 if (!data || !len) {
168 ret = 0;
169 goto end;
170 }
171
172 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
173 if (ret < 0) {
174 ret = -LTTNG_ERR_FATAL;
175 }
176
177 end:
178 return ret;
179 }
180
181 /*
182 * Send file descriptors to the session daemon.
183 *
184 * On success, returns the number of bytes sent (>=0)
185 * On error, returns -1
186 */
187 static int send_session_fds(const int *fds, size_t nb_fd)
188 {
189 int ret;
190
191 if (!connected) {
192 ret = -LTTNG_ERR_NO_SESSIOND;
193 goto end;
194 }
195
196 if (!fds || !nb_fd) {
197 ret = 0;
198 goto end;
199 }
200
201 ret = lttcomm_send_fds_unix_sock(sessiond_socket, fds, nb_fd);
202 if (ret < 0) {
203 ret = -LTTNG_ERR_FATAL;
204 }
205
206 end:
207 return ret;
208 }
209
210 /*
211 * Receive data from the sessiond socket.
212 *
213 * On success, returns the number of bytes received (>=0)
214 * On error, returns -1 (recvmsg() error) or -ENOTCONN
215 */
216 static int recv_data_sessiond(void *buf, size_t len)
217 {
218 int ret;
219
220 if (!connected) {
221 ret = -LTTNG_ERR_NO_SESSIOND;
222 goto end;
223 }
224
225 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
226 if (ret < 0) {
227 ret = -LTTNG_ERR_FATAL;
228 }
229
230 end:
231 return ret;
232 }
233
234 /*
235 * Check if we are in the specified group.
236 *
237 * If yes return 1, else return -1.
238 */
239 LTTNG_HIDDEN
240 int lttng_check_tracing_group(void)
241 {
242 struct group *grp_tracing; /* no free(). See getgrnam(3) */
243 gid_t *grp_list;
244 int grp_list_size, grp_id, i;
245 int ret = -1;
246 const char *grp_name = tracing_group;
247
248 /* Get GID of group 'tracing' */
249 grp_tracing = getgrnam(grp_name);
250 if (!grp_tracing) {
251 /* If grp_tracing is NULL, the group does not exist. */
252 goto end;
253 }
254
255 /* Get number of supplementary group IDs */
256 grp_list_size = getgroups(0, NULL);
257 if (grp_list_size < 0) {
258 PERROR("getgroups");
259 goto end;
260 }
261
262 /* Alloc group list of the right size */
263 grp_list = zmalloc(grp_list_size * sizeof(gid_t));
264 if (!grp_list) {
265 PERROR("malloc");
266 goto end;
267 }
268 grp_id = getgroups(grp_list_size, grp_list);
269 if (grp_id < 0) {
270 PERROR("getgroups");
271 goto free_list;
272 }
273
274 for (i = 0; i < grp_list_size; i++) {
275 if (grp_list[i] == grp_tracing->gr_gid) {
276 ret = 1;
277 break;
278 }
279 }
280
281 free_list:
282 free(grp_list);
283
284 end:
285 return ret;
286 }
287
288 /*
289 * Try connect to session daemon with sock_path.
290 *
291 * Return 0 on success, else -1
292 */
293 static int try_connect_sessiond(const char *sock_path)
294 {
295 int ret;
296
297 /* If socket exist, we check if the daemon listens for connect. */
298 ret = access(sock_path, F_OK);
299 if (ret < 0) {
300 /* Not alive */
301 goto error;
302 }
303
304 ret = lttcomm_connect_unix_sock(sock_path);
305 if (ret < 0) {
306 /* Not alive. */
307 goto error;
308 }
309
310 ret = lttcomm_close_unix_sock(ret);
311 if (ret < 0) {
312 PERROR("lttcomm_close_unix_sock");
313 }
314
315 return 0;
316
317 error:
318 return -1;
319 }
320
321 /*
322 * Set sessiond socket path by putting it in the global sessiond_sock_path
323 * variable.
324 *
325 * Returns 0 on success, negative value on failure (the sessiond socket path
326 * is somehow too long or ENOMEM).
327 */
328 static int set_session_daemon_path(void)
329 {
330 int in_tgroup = 0; /* In tracing group. */
331 uid_t uid;
332
333 uid = getuid();
334
335 if (uid != 0) {
336 /* Are we in the tracing group ? */
337 in_tgroup = lttng_check_tracing_group();
338 }
339
340 if ((uid == 0) || in_tgroup) {
341 lttng_ctl_copy_string(sessiond_sock_path,
342 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path));
343 }
344
345 if (uid != 0) {
346 int ret;
347
348 if (in_tgroup) {
349 /* Tracing group. */
350 ret = try_connect_sessiond(sessiond_sock_path);
351 if (ret >= 0) {
352 goto end;
353 }
354 /* Global session daemon not available... */
355 }
356 /* ...or not in tracing group (and not root), default */
357
358 /*
359 * With GNU C < 2.1, snprintf returns -1 if the target buffer
360 * is too small;
361 * With GNU C >= 2.1, snprintf returns the required size
362 * (excluding closing null)
363 */
364 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
365 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
366 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
367 goto error;
368 }
369 }
370 end:
371 return 0;
372
373 error:
374 return -1;
375 }
376
377 /*
378 * Connect to the LTTng session daemon.
379 *
380 * On success, return 0. On error, return -1.
381 */
382 static int connect_sessiond(void)
383 {
384 int ret;
385
386 /* Don't try to connect if already connected. */
387 if (connected) {
388 return 0;
389 }
390
391 ret = set_session_daemon_path();
392 if (ret < 0) {
393 goto error;
394 }
395
396 /* Connect to the sesssion daemon. */
397 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
398 if (ret < 0) {
399 goto error;
400 }
401
402 sessiond_socket = ret;
403 connected = 1;
404
405 return 0;
406
407 error:
408 return -1;
409 }
410
411 /*
412 * Clean disconnect from the session daemon.
413 *
414 * On success, return 0. On error, return -1.
415 */
416 static int disconnect_sessiond(void)
417 {
418 int ret = 0;
419
420 if (connected) {
421 ret = lttcomm_close_unix_sock(sessiond_socket);
422 sessiond_socket = 0;
423 connected = 0;
424 }
425
426 return ret;
427 }
428
429 static int recv_sessiond_optional_data(size_t len, void **user_buf,
430 size_t *user_len)
431 {
432 int ret = 0;
433 void *buf = NULL;
434
435 if (len) {
436 if (!user_len) {
437 ret = -LTTNG_ERR_INVALID;
438 goto end;
439 }
440
441 buf = zmalloc(len);
442 if (!buf) {
443 ret = -ENOMEM;
444 goto end;
445 }
446
447 ret = recv_data_sessiond(buf, len);
448 if (ret < 0) {
449 goto end;
450 }
451
452 if (!user_buf) {
453 ret = -LTTNG_ERR_INVALID;
454 goto end;
455 }
456
457 /* Move ownership of command header buffer to user. */
458 *user_buf = buf;
459 buf = NULL;
460 *user_len = len;
461 } else {
462 /* No command header. */
463 if (user_len) {
464 *user_len = 0;
465 }
466
467 if (user_buf) {
468 *user_buf = NULL;
469 }
470 }
471
472 end:
473 free(buf);
474 return ret;
475 }
476
477 /*
478 * Ask the session daemon a specific command and put the data into buf.
479 * Takes extra var. len. data and file descriptors as input to send to the
480 * session daemon.
481 *
482 * Return size of data (only payload, not header) or a negative error code.
483 */
484 LTTNG_HIDDEN
485 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm,
486 const int *fds, size_t nb_fd, const void *vardata,
487 size_t vardata_len, void **user_payload_buf,
488 void **user_cmd_header_buf, size_t *user_cmd_header_len)
489 {
490 int ret;
491 size_t payload_len;
492 struct lttcomm_lttng_msg llm;
493
494 ret = connect_sessiond();
495 if (ret < 0) {
496 ret = -LTTNG_ERR_NO_SESSIOND;
497 goto end;
498 }
499
500 /* Send command to session daemon */
501 ret = send_session_msg(lsm);
502 if (ret < 0) {
503 /* Ret value is a valid lttng error code. */
504 goto end;
505 }
506 /* Send var len data */
507 ret = send_session_varlen(vardata, vardata_len);
508 if (ret < 0) {
509 /* Ret value is a valid lttng error code. */
510 goto end;
511 }
512
513 /* Send fds */
514 ret = send_session_fds(fds, nb_fd);
515 if (ret < 0) {
516 /* Ret value is a valid lttng error code. */
517 goto end;
518 }
519
520 /* Get header from data transmission */
521 ret = recv_data_sessiond(&llm, sizeof(llm));
522 if (ret < 0) {
523 /* Ret value is a valid lttng error code. */
524 goto end;
525 }
526
527 /* Check error code if OK */
528 if (llm.ret_code != LTTNG_OK) {
529 ret = -llm.ret_code;
530 goto end;
531 }
532
533 /* Get command header from data transmission */
534 ret = recv_sessiond_optional_data(llm.cmd_header_size,
535 user_cmd_header_buf, user_cmd_header_len);
536 if (ret < 0) {
537 goto end;
538 }
539
540 /* Get payload from data transmission */
541 ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf,
542 &payload_len);
543 if (ret < 0) {
544 goto end;
545 }
546
547 ret = llm.data_size;
548
549 end:
550 disconnect_sessiond();
551 return ret;
552 }
553
554 /*
555 * Create lttng handle and return pointer.
556 *
557 * The returned pointer will be NULL in case of malloc() error.
558 */
559 struct lttng_handle *lttng_create_handle(const char *session_name,
560 struct lttng_domain *domain)
561 {
562 struct lttng_handle *handle = NULL;
563
564 handle = zmalloc(sizeof(struct lttng_handle));
565 if (handle == NULL) {
566 PERROR("malloc handle");
567 goto end;
568 }
569
570 /* Copy session name */
571 lttng_ctl_copy_string(handle->session_name, session_name,
572 sizeof(handle->session_name));
573
574 /* Copy lttng domain or leave initialized to 0. */
575 if (domain) {
576 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
577 }
578
579 end:
580 return handle;
581 }
582
583 /*
584 * Destroy handle by free(3) the pointer.
585 */
586 void lttng_destroy_handle(struct lttng_handle *handle)
587 {
588 free(handle);
589 }
590
591 /*
592 * Register an outside consumer.
593 *
594 * Returns size of returned session payload data or a negative error code.
595 */
596 int lttng_register_consumer(struct lttng_handle *handle,
597 const char *socket_path)
598 {
599 struct lttcomm_session_msg lsm;
600
601 if (handle == NULL || socket_path == NULL) {
602 return -LTTNG_ERR_INVALID;
603 }
604
605 memset(&lsm, 0, sizeof(lsm));
606 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
607 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
608 sizeof(lsm.session.name));
609 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
610
611 lttng_ctl_copy_string(lsm.u.reg.path, socket_path,
612 sizeof(lsm.u.reg.path));
613
614 return lttng_ctl_ask_sessiond(&lsm, NULL);
615 }
616
617 /*
618 * Start tracing for all traces of the session.
619 *
620 * Returns size of returned session payload data or a negative error code.
621 */
622 int lttng_start_tracing(const char *session_name)
623 {
624 struct lttcomm_session_msg lsm;
625
626 if (session_name == NULL) {
627 return -LTTNG_ERR_INVALID;
628 }
629
630 memset(&lsm, 0, sizeof(lsm));
631 lsm.cmd_type = LTTNG_START_TRACE;
632
633 lttng_ctl_copy_string(lsm.session.name, session_name,
634 sizeof(lsm.session.name));
635
636 return lttng_ctl_ask_sessiond(&lsm, NULL);
637 }
638
639 /*
640 * Stop tracing for all traces of the session.
641 */
642 static int _lttng_stop_tracing(const char *session_name, int wait)
643 {
644 int ret, data_ret;
645 struct lttcomm_session_msg lsm;
646
647 if (session_name == NULL) {
648 return -LTTNG_ERR_INVALID;
649 }
650
651 memset(&lsm, 0, sizeof(lsm));
652 lsm.cmd_type = LTTNG_STOP_TRACE;
653
654 lttng_ctl_copy_string(lsm.session.name, session_name,
655 sizeof(lsm.session.name));
656
657 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
658 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
659 goto error;
660 }
661
662 if (!wait) {
663 goto end;
664 }
665
666 /* Check for data availability */
667 do {
668 data_ret = lttng_data_pending(session_name);
669 if (data_ret < 0) {
670 /* Return the data available call error. */
671 ret = data_ret;
672 goto error;
673 }
674
675 /*
676 * Data sleep time before retrying (in usec). Don't sleep if the
677 * call returned value indicates availability.
678 */
679 if (data_ret) {
680 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
681 }
682 } while (data_ret != 0);
683
684 end:
685 error:
686 return ret;
687 }
688
689 /*
690 * Stop tracing and wait for data availability.
691 */
692 int lttng_stop_tracing(const char *session_name)
693 {
694 return _lttng_stop_tracing(session_name, 1);
695 }
696
697 /*
698 * Stop tracing but _don't_ wait for data availability.
699 */
700 int lttng_stop_tracing_no_wait(const char *session_name)
701 {
702 return _lttng_stop_tracing(session_name, 0);
703 }
704
705 /*
706 * Add context to a channel.
707 *
708 * If the given channel is NULL, add the contexts to all channels.
709 * The event_name param is ignored.
710 *
711 * Returns the size of the returned payload data or a negative error code.
712 */
713 int lttng_add_context(struct lttng_handle *handle,
714 struct lttng_event_context *ctx, const char *event_name,
715 const char *channel_name)
716 {
717 int ret;
718 size_t len = 0;
719 char *buf = NULL;
720 struct lttcomm_session_msg lsm;
721
722 /* Safety check. Both are mandatory. */
723 if (handle == NULL || ctx == NULL) {
724 ret = -LTTNG_ERR_INVALID;
725 goto end;
726 }
727
728 memset(&lsm, 0, sizeof(lsm));
729 lsm.cmd_type = LTTNG_ADD_CONTEXT;
730
731 /* If no channel name, send empty string. */
732 if (channel_name == NULL) {
733 lttng_ctl_copy_string(lsm.u.context.channel_name, "",
734 sizeof(lsm.u.context.channel_name));
735 } else {
736 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
737 sizeof(lsm.u.context.channel_name));
738 }
739
740 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
741 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
742 sizeof(lsm.session.name));
743
744 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
745 size_t provider_len, ctx_len;
746 const char *provider_name = ctx->u.app_ctx.provider_name;
747 const char *ctx_name = ctx->u.app_ctx.ctx_name;
748
749 if (!provider_name || !ctx_name) {
750 ret = -LTTNG_ERR_INVALID;
751 goto end;
752 }
753
754 provider_len = strlen(provider_name);
755 if (provider_len == 0) {
756 ret = -LTTNG_ERR_INVALID;
757 goto end;
758 }
759 lsm.u.context.provider_name_len = provider_len;
760
761 ctx_len = strlen(ctx_name);
762 if (ctx_len == 0) {
763 ret = -LTTNG_ERR_INVALID;
764 goto end;
765 }
766 lsm.u.context.context_name_len = ctx_len;
767
768 len = provider_len + ctx_len;
769 buf = zmalloc(len);
770 if (!buf) {
771 ret = -LTTNG_ERR_NOMEM;
772 goto end;
773 }
774
775 memcpy(buf, provider_name, provider_len);
776 memcpy(buf + provider_len, ctx_name, ctx_len);
777 }
778 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
779
780 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
781 /*
782 * Don't leak application addresses to the sessiond.
783 * This is only necessary when ctx is for an app ctx otherwise
784 * the values inside the union (type & config) are overwritten.
785 */
786 lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
787 lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
788 }
789
790 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL);
791 end:
792 free(buf);
793 return ret;
794 }
795
796 /*
797 * Enable event(s) for a channel.
798 *
799 * If no event name is specified, all events are enabled.
800 * If no channel name is specified, the default 'channel0' is used.
801 *
802 * Returns size of returned session payload data or a negative error code.
803 */
804 int lttng_enable_event(struct lttng_handle *handle,
805 struct lttng_event *ev, const char *channel_name)
806 {
807 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
808 NULL, 0, NULL);
809 }
810
811 /*
812 * Create or enable an event with a filter expression.
813 *
814 * Return negative error value on error.
815 * Return size of returned session payload data if OK.
816 */
817 int lttng_enable_event_with_filter(struct lttng_handle *handle,
818 struct lttng_event *event, const char *channel_name,
819 const char *filter_expression)
820 {
821 return lttng_enable_event_with_exclusions(handle, event, channel_name,
822 filter_expression, 0, NULL);
823 }
824
825 /*
826 * Depending on the event, return a newly allocated agent filter expression or
827 * NULL if not applicable.
828 *
829 * An event with NO loglevel and the name is * will return NULL.
830 */
831 static char *set_agent_filter(const char *filter, struct lttng_event *ev)
832 {
833 int err;
834 char *agent_filter = NULL;
835
836 assert(ev);
837
838 /* Don't add filter for the '*' event. */
839 if (strcmp(ev->name, "*") != 0) {
840 if (filter) {
841 err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
842 ev->name);
843 } else {
844 err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
845 }
846 if (err < 0) {
847 PERROR("asprintf");
848 goto error;
849 }
850 }
851
852 /* Add loglevel filtering if any for the JUL domain. */
853 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
854 char *op;
855
856 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
857 op = ">=";
858 } else {
859 op = "==";
860 }
861
862 if (filter || agent_filter) {
863 char *new_filter;
864
865 err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
866 agent_filter ? agent_filter : filter, op,
867 ev->loglevel);
868 if (agent_filter) {
869 free(agent_filter);
870 }
871 agent_filter = new_filter;
872 } else {
873 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
874 ev->loglevel);
875 }
876 if (err < 0) {
877 PERROR("asprintf");
878 goto error;
879 }
880 }
881
882 return agent_filter;
883 error:
884 free(agent_filter);
885 return NULL;
886 }
887
888 /*
889 * Generate the filter bytecode from a given filter expression string. Put the
890 * newly allocated parser context in ctxp and populate the lsm object with the
891 * expression len.
892 *
893 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
894 */
895 static int generate_filter(char *filter_expression,
896 struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp)
897 {
898 int ret;
899 struct filter_parser_ctx *ctx = NULL;
900 FILE *fmem = NULL;
901
902 assert(filter_expression);
903 assert(lsm);
904 assert(ctxp);
905
906 /*
907 * Casting const to non-const, as the underlying function will use it in
908 * read-only mode.
909 */
910 fmem = lttng_fmemopen((void *) filter_expression,
911 strlen(filter_expression), "r");
912 if (!fmem) {
913 fprintf(stderr, "Error opening memory as stream\n");
914 ret = -LTTNG_ERR_FILTER_NOMEM;
915 goto error;
916 }
917 ctx = filter_parser_ctx_alloc(fmem);
918 if (!ctx) {
919 fprintf(stderr, "Error allocating parser\n");
920 ret = -LTTNG_ERR_FILTER_NOMEM;
921 goto filter_alloc_error;
922 }
923 ret = filter_parser_ctx_append_ast(ctx);
924 if (ret) {
925 fprintf(stderr, "Parse error\n");
926 ret = -LTTNG_ERR_FILTER_INVAL;
927 goto parse_error;
928 }
929 if (print_xml) {
930 ret = filter_visitor_print_xml(ctx, stdout, 0);
931 if (ret) {
932 fflush(stdout);
933 fprintf(stderr, "XML print error\n");
934 ret = -LTTNG_ERR_FILTER_INVAL;
935 goto parse_error;
936 }
937 }
938
939 dbg_printf("Generating IR... ");
940 fflush(stdout);
941 ret = filter_visitor_ir_generate(ctx);
942 if (ret) {
943 fprintf(stderr, "Generate IR error\n");
944 ret = -LTTNG_ERR_FILTER_INVAL;
945 goto parse_error;
946 }
947 dbg_printf("done\n");
948
949 dbg_printf("Validating IR... ");
950 fflush(stdout);
951 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
952 if (ret) {
953 ret = -LTTNG_ERR_FILTER_INVAL;
954 goto parse_error;
955 }
956
957 /* Normalize globbing patterns in the expression. */
958 ret = filter_visitor_ir_normalize_glob_patterns(ctx);
959 if (ret) {
960 ret = -LTTNG_ERR_FILTER_INVAL;
961 goto parse_error;
962 }
963
964 /* Validate strings used as literals in the expression. */
965 ret = filter_visitor_ir_validate_string(ctx);
966 if (ret) {
967 ret = -LTTNG_ERR_FILTER_INVAL;
968 goto parse_error;
969 }
970
971 /* Validate globbing patterns in the expression. */
972 ret = filter_visitor_ir_validate_globbing(ctx);
973 if (ret) {
974 ret = -LTTNG_ERR_FILTER_INVAL;
975 goto parse_error;
976 }
977
978 dbg_printf("done\n");
979
980 dbg_printf("Generating bytecode... ");
981 fflush(stdout);
982 ret = filter_visitor_bytecode_generate(ctx);
983 if (ret) {
984 fprintf(stderr, "Generate bytecode error\n");
985 ret = -LTTNG_ERR_FILTER_INVAL;
986 goto parse_error;
987 }
988 dbg_printf("done\n");
989 dbg_printf("Size of bytecode generated: %u bytes.\n",
990 bytecode_get_len(&ctx->bytecode->b));
991
992 lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b)
993 + bytecode_get_len(&ctx->bytecode->b);
994 lsm->u.enable.expression_len = strlen(filter_expression) + 1;
995
996 /* No need to keep the memory stream. */
997 if (fclose(fmem) != 0) {
998 PERROR("fclose");
999 }
1000
1001 *ctxp = ctx;
1002 return 0;
1003
1004 parse_error:
1005 filter_ir_free(ctx);
1006 filter_parser_ctx_free(ctx);
1007 filter_alloc_error:
1008 if (fclose(fmem) != 0) {
1009 PERROR("fclose");
1010 }
1011 error:
1012 return ret;
1013 }
1014
1015 /*
1016 * Enable event(s) for a channel, possibly with exclusions and a filter.
1017 * If no event name is specified, all events are enabled.
1018 * If no channel name is specified, the default name is used.
1019 * If filter expression is not NULL, the filter is set for the event.
1020 * If exclusion count is not zero, the exclusions are set for the event.
1021 * Returns size of returned session payload data or a negative error code.
1022 */
1023 int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
1024 struct lttng_event *ev, const char *channel_name,
1025 const char *original_filter_expression,
1026 int exclusion_count, char **exclusion_list)
1027 {
1028 struct lttcomm_session_msg lsm;
1029 char *varlen_data;
1030 int ret = 0;
1031 unsigned int free_filter_expression = 0;
1032 struct filter_parser_ctx *ctx = NULL;
1033 /*
1034 * Cast as non-const since we may replace the filter expression
1035 * by a dynamically allocated string. Otherwise, the original
1036 * string is not modified.
1037 */
1038 char *filter_expression = (char *) original_filter_expression;
1039
1040 if (handle == NULL || ev == NULL) {
1041 ret = -LTTNG_ERR_INVALID;
1042 goto error;
1043 }
1044
1045 /*
1046 * Empty filter string will always be rejected by the parser
1047 * anyway, so treat this corner-case early to eliminate
1048 * lttng_fmemopen error for 0-byte allocation.
1049 */
1050 if (filter_expression && filter_expression[0] == '\0') {
1051 ret = -LTTNG_ERR_INVALID;
1052 goto error;
1053 }
1054
1055 memset(&lsm, 0, sizeof(lsm));
1056
1057 /* If no channel name, send empty string. */
1058 if (channel_name == NULL) {
1059 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
1060 sizeof(lsm.u.enable.channel_name));
1061 } else {
1062 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
1063 sizeof(lsm.u.enable.channel_name));
1064 }
1065
1066 lsm.cmd_type = LTTNG_ENABLE_EVENT;
1067 if (ev->name[0] == '\0') {
1068 /* Enable all events */
1069 lttng_ctl_copy_string(ev->name, "*", sizeof(ev->name));
1070 }
1071
1072 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1073 /* FIXME: copying non-packed struct to packed struct. */
1074 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
1075
1076 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1077 sizeof(lsm.session.name));
1078 lsm.u.enable.exclusion_count = exclusion_count;
1079 lsm.u.enable.bytecode_len = 0;
1080
1081 /*
1082 * For the JUL domain, a filter is enforced except for the enable all
1083 * event. This is done to avoid having the event in all sessions thus
1084 * filtering by logger name.
1085 */
1086 if (exclusion_count == 0 && filter_expression == NULL &&
1087 (handle->domain.type != LTTNG_DOMAIN_JUL &&
1088 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1089 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
1090 goto ask_sessiond;
1091 }
1092
1093 /*
1094 * We have either a filter or some exclusions, so we need to set up
1095 * a variable-length memory block from where to send the data.
1096 */
1097
1098 /* Parse filter expression. */
1099 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1100 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1101 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1102 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1103 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1104 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1105 char *agent_filter;
1106
1107 /* Setup JUL filter if needed. */
1108 agent_filter = set_agent_filter(filter_expression, ev);
1109 if (!agent_filter) {
1110 if (!filter_expression) {
1111 /*
1112 * No JUL and no filter, just skip
1113 * everything below.
1114 */
1115 goto ask_sessiond;
1116 }
1117 } else {
1118 /*
1119 * With an agent filter, the original filter has
1120 * been added to it thus replace the filter
1121 * expression.
1122 */
1123 filter_expression = agent_filter;
1124 free_filter_expression = 1;
1125 }
1126 }
1127
1128 ret = generate_filter(filter_expression, &lsm, &ctx);
1129 if (ret) {
1130 goto filter_error;
1131 }
1132 }
1133
1134 varlen_data = zmalloc(lsm.u.enable.bytecode_len
1135 + lsm.u.enable.expression_len
1136 + LTTNG_SYMBOL_NAME_LEN * exclusion_count);
1137 if (!varlen_data) {
1138 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1139 goto mem_error;
1140 }
1141
1142 /* Put exclusion names first in the data. */
1143 while (exclusion_count--) {
1144 strncpy(varlen_data + LTTNG_SYMBOL_NAME_LEN * exclusion_count,
1145 *(exclusion_list + exclusion_count),
1146 LTTNG_SYMBOL_NAME_LEN - 1);
1147 }
1148 /* Add filter expression next. */
1149 if (lsm.u.enable.expression_len != 0) {
1150 memcpy(varlen_data
1151 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count,
1152 filter_expression,
1153 lsm.u.enable.expression_len);
1154 }
1155 /* Add filter bytecode next. */
1156 if (ctx && lsm.u.enable.bytecode_len != 0) {
1157 memcpy(varlen_data
1158 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count
1159 + lsm.u.enable.expression_len,
1160 &ctx->bytecode->b,
1161 lsm.u.enable.bytecode_len);
1162 }
1163
1164 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
1165 (LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count) +
1166 lsm.u.enable.bytecode_len + lsm.u.enable.expression_len,
1167 NULL);
1168 free(varlen_data);
1169
1170 mem_error:
1171 if (filter_expression && ctx) {
1172 filter_bytecode_free(ctx);
1173 filter_ir_free(ctx);
1174 filter_parser_ctx_free(ctx);
1175 }
1176 filter_error:
1177 if (free_filter_expression) {
1178 /*
1179 * The filter expression has been replaced and must be freed as
1180 * it is not the original filter expression received as a
1181 * parameter.
1182 */
1183 free(filter_expression);
1184 }
1185 error:
1186 /*
1187 * Return directly to the caller and don't ask the sessiond since
1188 * something went wrong in the parsing of data above.
1189 */
1190 return ret;
1191
1192 ask_sessiond:
1193 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1194 return ret;
1195 }
1196
1197 int lttng_disable_event_ext(struct lttng_handle *handle,
1198 struct lttng_event *ev, const char *channel_name,
1199 const char *original_filter_expression)
1200 {
1201 struct lttcomm_session_msg lsm;
1202 char *varlen_data;
1203 int ret = 0;
1204 unsigned int free_filter_expression = 0;
1205 struct filter_parser_ctx *ctx = NULL;
1206 /*
1207 * Cast as non-const since we may replace the filter expression
1208 * by a dynamically allocated string. Otherwise, the original
1209 * string is not modified.
1210 */
1211 char *filter_expression = (char *) original_filter_expression;
1212
1213 if (handle == NULL || ev == NULL) {
1214 ret = -LTTNG_ERR_INVALID;
1215 goto error;
1216 }
1217
1218 /*
1219 * Empty filter string will always be rejected by the parser
1220 * anyway, so treat this corner-case early to eliminate
1221 * lttng_fmemopen error for 0-byte allocation.
1222 */
1223 if (filter_expression && filter_expression[0] == '\0') {
1224 ret = -LTTNG_ERR_INVALID;
1225 goto error;
1226 }
1227
1228 memset(&lsm, 0, sizeof(lsm));
1229
1230 /* If no channel name, send empty string. */
1231 if (channel_name == NULL) {
1232 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
1233 sizeof(lsm.u.disable.channel_name));
1234 } else {
1235 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
1236 sizeof(lsm.u.disable.channel_name));
1237 }
1238
1239 lsm.cmd_type = LTTNG_DISABLE_EVENT;
1240
1241 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1242 /* FIXME: copying non-packed struct to packed struct. */
1243 memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event));
1244
1245 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1246 sizeof(lsm.session.name));
1247 lsm.u.disable.bytecode_len = 0;
1248
1249 /*
1250 * For the JUL domain, a filter is enforced except for the
1251 * disable all event. This is done to avoid having the event in
1252 * all sessions thus filtering by logger name.
1253 */
1254 if (filter_expression == NULL &&
1255 (handle->domain.type != LTTNG_DOMAIN_JUL &&
1256 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1257 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
1258 goto ask_sessiond;
1259 }
1260
1261 /*
1262 * We have a filter, so we need to set up a variable-length
1263 * memory block from where to send the data.
1264 */
1265
1266 /* Parse filter expression */
1267 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1268 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1269 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1270 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1271 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1272 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1273 char *agent_filter;
1274
1275 /* Setup JUL filter if needed. */
1276 agent_filter = set_agent_filter(filter_expression, ev);
1277 if (!agent_filter) {
1278 if (!filter_expression) {
1279 /*
1280 * No JUL and no filter, just skip
1281 * everything below.
1282 */
1283 goto ask_sessiond;
1284 }
1285 } else {
1286 /*
1287 * With a JUL filter, the original filter has
1288 * been added to it thus replace the filter
1289 * expression.
1290 */
1291 filter_expression = agent_filter;
1292 free_filter_expression = 1;
1293 }
1294 }
1295
1296 ret = generate_filter(filter_expression, &lsm, &ctx);
1297 if (ret) {
1298 goto filter_error;
1299 }
1300 }
1301
1302 varlen_data = zmalloc(lsm.u.disable.bytecode_len
1303 + lsm.u.disable.expression_len);
1304 if (!varlen_data) {
1305 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1306 goto mem_error;
1307 }
1308
1309 /* Add filter expression. */
1310 if (lsm.u.disable.expression_len != 0) {
1311 memcpy(varlen_data,
1312 filter_expression,
1313 lsm.u.disable.expression_len);
1314 }
1315 /* Add filter bytecode next. */
1316 if (ctx && lsm.u.disable.bytecode_len != 0) {
1317 memcpy(varlen_data
1318 + lsm.u.disable.expression_len,
1319 &ctx->bytecode->b,
1320 lsm.u.disable.bytecode_len);
1321 }
1322
1323 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
1324 lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
1325 free(varlen_data);
1326
1327 mem_error:
1328 if (filter_expression && ctx) {
1329 filter_bytecode_free(ctx);
1330 filter_ir_free(ctx);
1331 filter_parser_ctx_free(ctx);
1332 }
1333 filter_error:
1334 if (free_filter_expression) {
1335 /*
1336 * The filter expression has been replaced and must be freed as
1337 * it is not the original filter expression received as a
1338 * parameter.
1339 */
1340 free(filter_expression);
1341 }
1342 error:
1343 /*
1344 * Return directly to the caller and don't ask the sessiond since
1345 * something went wrong in the parsing of data above.
1346 */
1347 return ret;
1348
1349 ask_sessiond:
1350 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1351 return ret;
1352 }
1353
1354 /*
1355 * Disable event(s) of a channel and domain.
1356 * If no event name is specified, all events are disabled.
1357 * If no channel name is specified, the default 'channel0' is used.
1358 * Returns size of returned session payload data or a negative error code.
1359 */
1360 int lttng_disable_event(struct lttng_handle *handle, const char *name,
1361 const char *channel_name)
1362 {
1363 struct lttng_event ev;
1364
1365 memset(&ev, 0, sizeof(ev));
1366 ev.loglevel = -1;
1367 ev.type = LTTNG_EVENT_ALL;
1368 lttng_ctl_copy_string(ev.name, name, sizeof(ev.name));
1369 return lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1370 }
1371
1372 struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
1373 {
1374 struct lttng_channel *channel = NULL;
1375 struct lttng_channel_extended *extended = NULL;
1376
1377 if (!domain) {
1378 goto error;
1379 }
1380
1381 /* Validate domain. */
1382 switch (domain->type) {
1383 case LTTNG_DOMAIN_UST:
1384 switch (domain->buf_type) {
1385 case LTTNG_BUFFER_PER_UID:
1386 case LTTNG_BUFFER_PER_PID:
1387 break;
1388 default:
1389 goto error;
1390 }
1391 break;
1392 case LTTNG_DOMAIN_KERNEL:
1393 if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
1394 goto error;
1395 }
1396 break;
1397 default:
1398 goto error;
1399 }
1400
1401 channel = zmalloc(sizeof(*channel));
1402 if (!channel) {
1403 goto error;
1404 }
1405
1406 extended = zmalloc(sizeof(*extended));
1407 if (!extended) {
1408 goto error;
1409 }
1410
1411 channel->attr.extended.ptr = extended;
1412
1413 lttng_channel_set_default_attr(domain, &channel->attr);
1414 return channel;
1415 error:
1416 free(channel);
1417 free(extended);
1418 return NULL;
1419 }
1420
1421 void lttng_channel_destroy(struct lttng_channel *channel)
1422 {
1423 if (!channel) {
1424 return;
1425 }
1426
1427 if (channel->attr.extended.ptr) {
1428 free(channel->attr.extended.ptr);
1429 }
1430 free(channel);
1431 }
1432
1433 /*
1434 * Enable channel per domain
1435 * Returns size of returned session payload data or a negative error code.
1436 */
1437 int lttng_enable_channel(struct lttng_handle *handle,
1438 struct lttng_channel *in_chan)
1439 {
1440 struct lttcomm_session_msg lsm;
1441
1442 /* NULL arguments are forbidden. No default values. */
1443 if (handle == NULL || in_chan == NULL) {
1444 return -LTTNG_ERR_INVALID;
1445 }
1446
1447 memset(&lsm, 0, sizeof(lsm));
1448 memcpy(&lsm.u.channel.chan, in_chan, sizeof(lsm.u.channel.chan));
1449 lsm.u.channel.chan.attr.extended.ptr = NULL;
1450
1451 if (!in_chan->attr.extended.ptr) {
1452 struct lttng_channel *channel;
1453 struct lttng_channel_extended *extended;
1454
1455 channel = lttng_channel_create(&handle->domain);
1456 if (!channel) {
1457 return -LTTNG_ERR_NOMEM;
1458 }
1459
1460 /*
1461 * Create a new channel in order to use default extended
1462 * attribute values.
1463 */
1464 extended = (struct lttng_channel_extended *)
1465 channel->attr.extended.ptr;
1466 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1467 lttng_channel_destroy(channel);
1468 } else {
1469 struct lttng_channel_extended *extended;
1470
1471 extended = (struct lttng_channel_extended *)
1472 in_chan->attr.extended.ptr;
1473 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1474 }
1475
1476 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1477 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1478
1479 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1480 sizeof(lsm.session.name));
1481
1482 return lttng_ctl_ask_sessiond(&lsm, NULL);
1483 }
1484
1485 /*
1486 * All tracing will be stopped for registered events of the channel.
1487 * Returns size of returned session payload data or a negative error code.
1488 */
1489 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
1490 {
1491 struct lttcomm_session_msg lsm;
1492
1493 /* Safety check. Both are mandatory. */
1494 if (handle == NULL || name == NULL) {
1495 return -LTTNG_ERR_INVALID;
1496 }
1497
1498 memset(&lsm, 0, sizeof(lsm));
1499
1500 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1501
1502 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
1503 sizeof(lsm.u.disable.channel_name));
1504
1505 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1506
1507 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1508 sizeof(lsm.session.name));
1509
1510 return lttng_ctl_ask_sessiond(&lsm, NULL);
1511 }
1512
1513 /*
1514 * Add PID to session tracker.
1515 * Return 0 on success else a negative LTTng error code.
1516 */
1517 int lttng_track_pid(struct lttng_handle *handle, int pid)
1518 {
1519 struct lttcomm_session_msg lsm;
1520
1521 /* NULL arguments are forbidden. No default values. */
1522 if (handle == NULL) {
1523 return -LTTNG_ERR_INVALID;
1524 }
1525
1526 memset(&lsm, 0, sizeof(lsm));
1527
1528 lsm.cmd_type = LTTNG_TRACK_PID;
1529 lsm.u.pid_tracker.pid = pid;
1530
1531 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1532
1533 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1534 sizeof(lsm.session.name));
1535
1536 return lttng_ctl_ask_sessiond(&lsm, NULL);
1537 }
1538
1539 /*
1540 * Remove PID from session tracker.
1541 * Return 0 on success else a negative LTTng error code.
1542 */
1543 int lttng_untrack_pid(struct lttng_handle *handle, int pid)
1544 {
1545 struct lttcomm_session_msg lsm;
1546
1547 /* NULL arguments are forbidden. No default values. */
1548 if (handle == NULL) {
1549 return -LTTNG_ERR_INVALID;
1550 }
1551
1552 memset(&lsm, 0, sizeof(lsm));
1553
1554 lsm.cmd_type = LTTNG_UNTRACK_PID;
1555 lsm.u.pid_tracker.pid = pid;
1556
1557 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1558
1559 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1560 sizeof(lsm.session.name));
1561
1562 return lttng_ctl_ask_sessiond(&lsm, NULL);
1563 }
1564
1565 /*
1566 * Lists all available tracepoints of domain.
1567 * Sets the contents of the events array.
1568 * Returns the number of lttng_event entries in events;
1569 * on error, returns a negative value.
1570 */
1571 int lttng_list_tracepoints(struct lttng_handle *handle,
1572 struct lttng_event **events)
1573 {
1574 int ret;
1575 struct lttcomm_session_msg lsm;
1576
1577 if (handle == NULL) {
1578 return -LTTNG_ERR_INVALID;
1579 }
1580
1581 memset(&lsm, 0, sizeof(lsm));
1582 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
1583 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1584
1585 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1586 if (ret < 0) {
1587 return ret;
1588 }
1589
1590 return ret / sizeof(struct lttng_event);
1591 }
1592
1593 /*
1594 * Lists all available tracepoint fields of domain.
1595 * Sets the contents of the event field array.
1596 * Returns the number of lttng_event_field entries in events;
1597 * on error, returns a negative value.
1598 */
1599 int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1600 struct lttng_event_field **fields)
1601 {
1602 int ret;
1603 struct lttcomm_session_msg lsm;
1604
1605 if (handle == NULL) {
1606 return -LTTNG_ERR_INVALID;
1607 }
1608
1609 memset(&lsm, 0, sizeof(lsm));
1610 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
1611 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1612
1613 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
1614 if (ret < 0) {
1615 return ret;
1616 }
1617
1618 return ret / sizeof(struct lttng_event_field);
1619 }
1620
1621 /*
1622 * Lists all available kernel system calls. Allocates and sets the contents of
1623 * the events array.
1624 *
1625 * Returns the number of lttng_event entries in events; on error, returns a
1626 * negative value.
1627 */
1628 int lttng_list_syscalls(struct lttng_event **events)
1629 {
1630 int ret;
1631 struct lttcomm_session_msg lsm;
1632
1633 if (!events) {
1634 return -LTTNG_ERR_INVALID;
1635 }
1636
1637 memset(&lsm, 0, sizeof(lsm));
1638 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1639 /* Force kernel domain for system calls. */
1640 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1641
1642 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1643 if (ret < 0) {
1644 return ret;
1645 }
1646
1647 return ret / sizeof(struct lttng_event);
1648 }
1649
1650 /*
1651 * Returns a human readable string describing
1652 * the error code (a negative value).
1653 */
1654 const char *lttng_strerror(int code)
1655 {
1656 return error_get_str(code);
1657 }
1658
1659 /*
1660 * Create a brand new session using name and url for destination.
1661 *
1662 * Returns LTTNG_OK on success or a negative error code.
1663 */
1664 int lttng_create_session(const char *name, const char *url)
1665 {
1666 int ret;
1667 ssize_t size;
1668 struct lttcomm_session_msg lsm;
1669 struct lttng_uri *uris = NULL;
1670
1671 if (name == NULL) {
1672 return -LTTNG_ERR_INVALID;
1673 }
1674
1675 memset(&lsm, 0, sizeof(lsm));
1676
1677 lsm.cmd_type = LTTNG_CREATE_SESSION;
1678 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1679
1680 /* There should never be a data URL */
1681 size = uri_parse_str_urls(url, NULL, &uris);
1682 if (size < 0) {
1683 return -LTTNG_ERR_INVALID;
1684 }
1685
1686 lsm.u.uri.size = size;
1687
1688 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
1689 sizeof(struct lttng_uri) * size, NULL);
1690
1691 free(uris);
1692 return ret;
1693 }
1694
1695 /*
1696 * Destroy session using name.
1697 * Returns size of returned session payload data or a negative error code.
1698 */
1699 static
1700 int _lttng_destroy_session(const char *session_name)
1701 {
1702 struct lttcomm_session_msg lsm;
1703
1704 if (session_name == NULL) {
1705 return -LTTNG_ERR_INVALID;
1706 }
1707
1708 memset(&lsm, 0, sizeof(lsm));
1709 lsm.cmd_type = LTTNG_DESTROY_SESSION;
1710
1711 lttng_ctl_copy_string(lsm.session.name, session_name,
1712 sizeof(lsm.session.name));
1713
1714 return lttng_ctl_ask_sessiond(&lsm, NULL);
1715 }
1716
1717 /*
1718 * Stop the session and wait for the data before destroying it
1719 */
1720 int lttng_destroy_session(const char *session_name)
1721 {
1722 int ret;
1723
1724 /*
1725 * Stop the tracing and wait for the data.
1726 */
1727 ret = _lttng_stop_tracing(session_name, 1);
1728 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1729 goto end;
1730 }
1731
1732 ret = _lttng_destroy_session(session_name);
1733 end:
1734 return ret;
1735 }
1736
1737 /*
1738 * Destroy the session without waiting for the data.
1739 */
1740 int lttng_destroy_session_no_wait(const char *session_name)
1741 {
1742 int ret;
1743
1744 /*
1745 * Stop the tracing without waiting for the data.
1746 * The session might already have been stopped, so just
1747 * skip this error.
1748 */
1749 ret = _lttng_stop_tracing(session_name, 0);
1750 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1751 goto end;
1752 }
1753
1754 ret = _lttng_destroy_session(session_name);
1755 end:
1756 return ret;
1757 }
1758
1759 /*
1760 * Ask the session daemon for all available sessions.
1761 * Sets the contents of the sessions array.
1762 * Returns the number of lttng_session entries in sessions;
1763 * on error, returns a negative value.
1764 */
1765 int lttng_list_sessions(struct lttng_session **sessions)
1766 {
1767 int ret;
1768 struct lttcomm_session_msg lsm;
1769
1770 memset(&lsm, 0, sizeof(lsm));
1771 lsm.cmd_type = LTTNG_LIST_SESSIONS;
1772 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
1773 if (ret < 0) {
1774 return ret;
1775 }
1776
1777 return ret / sizeof(struct lttng_session);
1778 }
1779
1780 int lttng_set_session_shm_path(const char *session_name,
1781 const char *shm_path)
1782 {
1783 struct lttcomm_session_msg lsm;
1784
1785 if (session_name == NULL) {
1786 return -LTTNG_ERR_INVALID;
1787 }
1788
1789 memset(&lsm, 0, sizeof(lsm));
1790 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
1791
1792 lttng_ctl_copy_string(lsm.session.name, session_name,
1793 sizeof(lsm.session.name));
1794 lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path,
1795 sizeof(lsm.u.set_shm_path.shm_path));
1796
1797 return lttng_ctl_ask_sessiond(&lsm, NULL);
1798 }
1799
1800 /*
1801 * Ask the session daemon for all available domains of a session.
1802 * Sets the contents of the domains array.
1803 * Returns the number of lttng_domain entries in domains;
1804 * on error, returns a negative value.
1805 */
1806 int lttng_list_domains(const char *session_name,
1807 struct lttng_domain **domains)
1808 {
1809 int ret;
1810 struct lttcomm_session_msg lsm;
1811
1812 if (session_name == NULL) {
1813 return -LTTNG_ERR_INVALID;
1814 }
1815
1816 memset(&lsm, 0, sizeof(lsm));
1817 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1818
1819 lttng_ctl_copy_string(lsm.session.name, session_name,
1820 sizeof(lsm.session.name));
1821
1822 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
1823 if (ret < 0) {
1824 return ret;
1825 }
1826
1827 return ret / sizeof(struct lttng_domain);
1828 }
1829
1830 /*
1831 * Ask the session daemon for all available channels of a session.
1832 * Sets the contents of the channels array.
1833 * Returns the number of lttng_channel entries in channels;
1834 * on error, returns a negative value.
1835 */
1836 int lttng_list_channels(struct lttng_handle *handle,
1837 struct lttng_channel **channels)
1838 {
1839 int ret;
1840 size_t channel_count, i;
1841 const size_t channel_size = sizeof(struct lttng_channel) +
1842 sizeof(struct lttng_channel_extended);
1843 struct lttcomm_session_msg lsm;
1844 void *extended_at;
1845
1846 if (handle == NULL) {
1847 ret = -LTTNG_ERR_INVALID;
1848 goto end;
1849 }
1850
1851 memset(&lsm, 0, sizeof(lsm));
1852 lsm.cmd_type = LTTNG_LIST_CHANNELS;
1853 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1854 sizeof(lsm.session.name));
1855
1856 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1857
1858 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
1859 if (ret < 0) {
1860 goto end;
1861 }
1862
1863 if (ret % channel_size) {
1864 ret = -LTTNG_ERR_UNK;
1865 free(*channels);
1866 *channels = NULL;
1867 goto end;
1868 }
1869 channel_count = (size_t) ret / channel_size;
1870
1871 /* Set extended info pointers */
1872 extended_at = ((void *) *channels) +
1873 channel_count * sizeof(struct lttng_channel);
1874 for (i = 0; i < channel_count; i++) {
1875 struct lttng_channel *chan = &(*channels)[i];
1876
1877 chan->attr.extended.ptr = extended_at;
1878 extended_at += sizeof(struct lttng_channel_extended);
1879 }
1880
1881 ret = (int) channel_count;
1882 end:
1883 return ret;
1884 }
1885
1886 /*
1887 * Ask the session daemon for all available events of a session channel.
1888 * Sets the contents of the events array.
1889 * Returns the number of lttng_event entries in events;
1890 * on error, returns a negative value.
1891 */
1892 int lttng_list_events(struct lttng_handle *handle,
1893 const char *channel_name, struct lttng_event **events)
1894 {
1895 int ret;
1896 struct lttcomm_session_msg lsm;
1897 struct lttcomm_event_command_header *cmd_header = NULL;
1898 size_t cmd_header_len;
1899 uint32_t nb_events, i;
1900 void *comm_ext_at;
1901 void *listing_at;
1902 char *reception_buffer = NULL;
1903 char *listing = NULL;
1904 size_t storage_req;
1905
1906 /* Safety check. An handle and channel name are mandatory */
1907 if (handle == NULL || channel_name == NULL) {
1908 return -LTTNG_ERR_INVALID;
1909 }
1910
1911 memset(&lsm, 0, sizeof(lsm));
1912 lsm.cmd_type = LTTNG_LIST_EVENTS;
1913 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1914 sizeof(lsm.session.name));
1915 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
1916 sizeof(lsm.u.list.channel_name));
1917 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1918
1919 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
1920 (void **) &reception_buffer, (void **) &cmd_header,
1921 &cmd_header_len);
1922 if (ret < 0) {
1923 goto end;
1924 }
1925
1926 /* Set number of events and free command header */
1927 nb_events = cmd_header->nb_events;
1928 if (nb_events > INT_MAX) {
1929 ret = -EOVERFLOW;
1930 goto end;
1931 }
1932 free(cmd_header);
1933 cmd_header = NULL;
1934
1935 /*
1936 * The buffer that is returned must contain a "flat" version of
1937 * the events that are returned. In other words, all pointers
1938 * within an lttng_event must point to a location within the returned
1939 * buffer so that the user may free by simply calling free() on the
1940 * returned buffer. This is needed in order to maintain API
1941 * compatibility.
1942 *
1943 * A first pass is performed to figure the size of the buffer that
1944 * must be returned. A second pass is then performed to setup
1945 * the returned events so that their members always point within the
1946 * buffer.
1947 *
1948 * The layout of the returned buffer is as follows:
1949 * - struct lttng_event[nb_events],
1950 * - nb_events times the following:
1951 * - struct lttng_event_extended,
1952 * - flattened version of userspace_probe_location
1953 * - filter_expression
1954 * - exclusions
1955 * - padding to align to 64-bits
1956 */
1957 comm_ext_at = reception_buffer +
1958 (nb_events * sizeof(struct lttng_event));
1959 storage_req = nb_events * sizeof(struct lttng_event);
1960
1961 for (i = 0; i < nb_events; i++) {
1962 struct lttcomm_event_extended_header *ext_comm =
1963 (struct lttcomm_event_extended_header *) comm_ext_at;
1964
1965 comm_ext_at += sizeof(*ext_comm);
1966 comm_ext_at += ext_comm->filter_len;
1967 comm_ext_at +=
1968 ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
1969
1970 storage_req += sizeof(struct lttng_event_extended);
1971 /* TODO: missing size of flat userspace probe. */
1972 storage_req += ext_comm->filter_len;
1973 storage_req += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
1974 storage_req += storage_req % 8;
1975 }
1976
1977 listing = zmalloc(storage_req);
1978 if (!listing) {
1979 goto end;
1980 }
1981 memcpy(listing, reception_buffer,
1982 nb_events * sizeof(struct lttng_event));
1983
1984 comm_ext_at = reception_buffer +
1985 (nb_events * sizeof(struct lttng_event));
1986 listing_at = listing +
1987 (nb_events * sizeof(struct lttng_event));
1988 for (i = 0; i < nb_events; i++) {
1989 struct lttng_event *event = (struct lttng_event *)
1990 (listing + (sizeof(struct lttng_event) * i));
1991 struct lttcomm_event_extended_header *ext_comm =
1992 (struct lttcomm_event_extended_header *) comm_ext_at;
1993 struct lttng_event_extended *event_extended =
1994 (struct lttng_event_extended *) listing_at;
1995
1996 event->extended.ptr = event_extended;
1997 listing_at += sizeof(*event_extended);
1998 comm_ext_at += sizeof(*ext_comm);
1999
2000 /* Copy filter expression. */
2001 memcpy(listing_at, comm_ext_at, ext_comm->filter_len);
2002 event_extended->filter_expression = listing_at;
2003 comm_ext_at += ext_comm->filter_len;
2004 listing_at += ext_comm->filter_len;
2005
2006 /* Copy exclusions. */
2007 event_extended->exclusions.count = ext_comm->nb_exclusions;
2008 event_extended->exclusions.strings = listing_at;
2009 memcpy(listing_at, comm_ext_at,
2010 ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN);
2011 listing_at += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
2012 comm_ext_at += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
2013
2014 listing_at += ((uintptr_t) listing_at) % 8;
2015 }
2016
2017 *events = (struct lttng_event *) listing;
2018 listing = NULL;
2019 ret = (int) nb_events;
2020 end:
2021 free(cmd_header);
2022 free(reception_buffer);
2023 free(listing);
2024 return ret;
2025 }
2026
2027 /*
2028 * Sets the tracing_group variable with name.
2029 * This function allocates memory pointed to by tracing_group.
2030 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2031 */
2032 int lttng_set_tracing_group(const char *name)
2033 {
2034 if (name == NULL) {
2035 return -LTTNG_ERR_INVALID;
2036 }
2037
2038 if (asprintf(&tracing_group, "%s", name) < 0) {
2039 return -LTTNG_ERR_FATAL;
2040 }
2041
2042 return 0;
2043 }
2044
2045 int lttng_calibrate(struct lttng_handle *handle,
2046 struct lttng_calibrate *calibrate)
2047 {
2048 /*
2049 * This command was removed in LTTng 2.9.
2050 */
2051 return -LTTNG_ERR_UND;
2052 }
2053
2054 /*
2055 * Set default channel attributes.
2056 * If either or both of the arguments are null, attr content is zeroe'd.
2057 */
2058 void lttng_channel_set_default_attr(struct lttng_domain *domain,
2059 struct lttng_channel_attr *attr)
2060 {
2061 struct lttng_channel_extended *extended;
2062
2063 /* Safety check */
2064 if (attr == NULL || domain == NULL) {
2065 return;
2066 }
2067
2068 extended = (struct lttng_channel_extended *) attr->extended.ptr;
2069 memset(attr, 0, sizeof(struct lttng_channel_attr));
2070
2071 /* Same for all domains. */
2072 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2073 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2074 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2075
2076 switch (domain->type) {
2077 case LTTNG_DOMAIN_KERNEL:
2078 attr->switch_timer_interval =
2079 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
2080 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
2081 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
2082 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2083 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
2084 if (extended) {
2085 extended->monitor_timer_interval =
2086 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
2087 extended->blocking_timeout =
2088 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
2089 }
2090 break;
2091 case LTTNG_DOMAIN_UST:
2092 switch (domain->buf_type) {
2093 case LTTNG_BUFFER_PER_UID:
2094 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2095 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2096 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
2097 attr->switch_timer_interval =
2098 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2099 attr->read_timer_interval =
2100 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
2101 if (extended) {
2102 extended->monitor_timer_interval =
2103 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
2104 extended->blocking_timeout =
2105 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
2106 }
2107 break;
2108 case LTTNG_BUFFER_PER_PID:
2109 default:
2110 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2111 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2112 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
2113 attr->switch_timer_interval =
2114 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2115 attr->read_timer_interval =
2116 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
2117 if (extended) {
2118 extended->monitor_timer_interval =
2119 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
2120 extended->blocking_timeout =
2121 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
2122 }
2123 break;
2124 }
2125 default:
2126 /* Default behavior: leave set to 0. */
2127 break;
2128 }
2129
2130 attr->extended.ptr = extended;
2131 }
2132
2133 int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2134 uint64_t *discarded_events)
2135 {
2136 int ret = 0;
2137 struct lttng_channel_extended *chan_ext;
2138
2139 if (!channel || !discarded_events) {
2140 ret = -LTTNG_ERR_INVALID;
2141 goto end;
2142 }
2143
2144 chan_ext = channel->attr.extended.ptr;
2145 if (!chan_ext) {
2146 /*
2147 * This can happen since the lttng_channel structure is
2148 * used for other tasks where this pointer is never set.
2149 */
2150 *discarded_events = 0;
2151 goto end;
2152 }
2153
2154 *discarded_events = chan_ext->discarded_events;
2155 end:
2156 return ret;
2157 }
2158
2159 int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2160 uint64_t *lost_packets)
2161 {
2162 int ret = 0;
2163 struct lttng_channel_extended *chan_ext;
2164
2165 if (!channel || !lost_packets) {
2166 ret = -LTTNG_ERR_INVALID;
2167 goto end;
2168 }
2169
2170 chan_ext = channel->attr.extended.ptr;
2171 if (!chan_ext) {
2172 /*
2173 * This can happen since the lttng_channel structure is
2174 * used for other tasks where this pointer is never set.
2175 */
2176 *lost_packets = 0;
2177 goto end;
2178 }
2179
2180 *lost_packets = chan_ext->lost_packets;
2181 end:
2182 return ret;
2183 }
2184
2185 int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2186 uint64_t *monitor_timer_interval)
2187 {
2188 int ret = 0;
2189
2190 if (!chan || !monitor_timer_interval) {
2191 ret = -LTTNG_ERR_INVALID;
2192 goto end;
2193 }
2194
2195 if (!chan->attr.extended.ptr) {
2196 ret = -LTTNG_ERR_INVALID;
2197 goto end;
2198 }
2199
2200 *monitor_timer_interval = ((struct lttng_channel_extended *)
2201 chan->attr.extended.ptr)->monitor_timer_interval;
2202 end:
2203 return ret;
2204 }
2205
2206 int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2207 uint64_t monitor_timer_interval)
2208 {
2209 int ret = 0;
2210
2211 if (!chan || !chan->attr.extended.ptr) {
2212 ret = -LTTNG_ERR_INVALID;
2213 goto end;
2214 }
2215
2216 ((struct lttng_channel_extended *)
2217 chan->attr.extended.ptr)->monitor_timer_interval =
2218 monitor_timer_interval;
2219 end:
2220 return ret;
2221 }
2222
2223 int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2224 int64_t *blocking_timeout)
2225 {
2226 int ret = 0;
2227
2228 if (!chan || !blocking_timeout) {
2229 ret = -LTTNG_ERR_INVALID;
2230 goto end;
2231 }
2232
2233 if (!chan->attr.extended.ptr) {
2234 ret = -LTTNG_ERR_INVALID;
2235 goto end;
2236 }
2237
2238 *blocking_timeout = ((struct lttng_channel_extended *)
2239 chan->attr.extended.ptr)->blocking_timeout;
2240 end:
2241 return ret;
2242 }
2243
2244 int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2245 int64_t blocking_timeout)
2246 {
2247 int ret = 0;
2248 int64_t msec_timeout;
2249
2250 if (!chan || !chan->attr.extended.ptr) {
2251 ret = -LTTNG_ERR_INVALID;
2252 goto end;
2253 }
2254
2255 if (blocking_timeout < 0 && blocking_timeout != -1) {
2256 ret = -LTTNG_ERR_INVALID;
2257 goto end;
2258 }
2259
2260 /*
2261 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2262 * us to accept a narrower range of values (msecs expressed as a signed
2263 * 32-bit integer).
2264 */
2265 msec_timeout = blocking_timeout / 1000;
2266 if (msec_timeout != (int32_t) msec_timeout) {
2267 ret = -LTTNG_ERR_INVALID;
2268 goto end;
2269 }
2270
2271 ((struct lttng_channel_extended *)
2272 chan->attr.extended.ptr)->blocking_timeout =
2273 blocking_timeout;
2274 end:
2275 return ret;
2276 }
2277
2278 /*
2279 * Check if session daemon is alive.
2280 *
2281 * Return 1 if alive or 0 if not.
2282 * On error returns a negative value.
2283 */
2284 int lttng_session_daemon_alive(void)
2285 {
2286 int ret;
2287
2288 ret = set_session_daemon_path();
2289 if (ret < 0) {
2290 /* Error. */
2291 return ret;
2292 }
2293
2294 if (*sessiond_sock_path == '\0') {
2295 /*
2296 * No socket path set. Weird error which means the constructor
2297 * was not called.
2298 */
2299 assert(0);
2300 }
2301
2302 ret = try_connect_sessiond(sessiond_sock_path);
2303 if (ret < 0) {
2304 /* Not alive. */
2305 return 0;
2306 }
2307
2308 /* Is alive. */
2309 return 1;
2310 }
2311
2312 /*
2313 * Set URL for a consumer for a session and domain.
2314 *
2315 * Return 0 on success, else a negative value.
2316 */
2317 int lttng_set_consumer_url(struct lttng_handle *handle,
2318 const char *control_url, const char *data_url)
2319 {
2320 int ret;
2321 ssize_t size;
2322 struct lttcomm_session_msg lsm;
2323 struct lttng_uri *uris = NULL;
2324
2325 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2326 return -LTTNG_ERR_INVALID;
2327 }
2328
2329 memset(&lsm, 0, sizeof(lsm));
2330
2331 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2332
2333 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2334 sizeof(lsm.session.name));
2335 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2336
2337 size = uri_parse_str_urls(control_url, data_url, &uris);
2338 if (size < 0) {
2339 return -LTTNG_ERR_INVALID;
2340 }
2341
2342 lsm.u.uri.size = size;
2343
2344 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2345 sizeof(struct lttng_uri) * size, NULL);
2346
2347 free(uris);
2348 return ret;
2349 }
2350
2351 /*
2352 * [OBSOLETE]
2353 */
2354 int lttng_enable_consumer(struct lttng_handle *handle)
2355 {
2356 return -ENOSYS;
2357 }
2358
2359 /*
2360 * [OBSOLETE]
2361 */
2362 int lttng_disable_consumer(struct lttng_handle *handle)
2363 {
2364 return -ENOSYS;
2365 }
2366
2367 /*
2368 * This is an extension of create session that is ONLY and SHOULD only be used
2369 * by the lttng command line program. It exists to avoid using URI parsing in
2370 * the lttng client.
2371 *
2372 * We need the date and time for the trace path subdirectory for the case where
2373 * the user does NOT define one using either -o or -U. Using the normal
2374 * lttng_create_session API call, we have no clue on the session daemon side if
2375 * the URL was generated automatically by the client or define by the user.
2376 *
2377 * So this function "wrapper" is hidden from the public API, takes the datetime
2378 * string and appends it if necessary to the URI subdirectory before sending it
2379 * to the session daemon.
2380 *
2381 * With this extra function, the lttng_create_session call behavior is not
2382 * changed and the timestamp is appended to the URI on the session daemon side
2383 * if necessary.
2384 */
2385 int _lttng_create_session_ext(const char *name, const char *url,
2386 const char *datetime)
2387 {
2388 int ret;
2389 ssize_t size;
2390 struct lttcomm_session_msg lsm;
2391 struct lttng_uri *uris = NULL;
2392
2393 if (name == NULL || datetime == NULL) {
2394 return -LTTNG_ERR_INVALID;
2395 }
2396
2397 memset(&lsm, 0, sizeof(lsm));
2398
2399 lsm.cmd_type = LTTNG_CREATE_SESSION;
2400 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2401
2402 /* There should never be a data URL. */
2403 size = uri_parse_str_urls(url, NULL, &uris);
2404 if (size < 0) {
2405 ret = -LTTNG_ERR_INVALID;
2406 goto error;
2407 }
2408
2409 lsm.u.uri.size = size;
2410
2411 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
2412 /* Don't append datetime if the name was automatically created. */
2413 if (strncmp(name, DEFAULT_SESSION_NAME "-",
2414 strlen(DEFAULT_SESSION_NAME) + 1)) {
2415 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
2416 name, datetime);
2417 } else {
2418 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
2419 }
2420 if (ret < 0) {
2421 PERROR("snprintf uri subdir");
2422 ret = -LTTNG_ERR_FATAL;
2423 goto error;
2424 }
2425 }
2426
2427 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2428 sizeof(struct lttng_uri) * size, NULL);
2429
2430 error:
2431 free(uris);
2432 return ret;
2433 }
2434
2435 /*
2436 * For a given session name, this call checks if the data is ready to be read
2437 * or is still being extracted by the consumer(s) hence not ready to be used by
2438 * any readers.
2439 */
2440 int lttng_data_pending(const char *session_name)
2441 {
2442 int ret;
2443 struct lttcomm_session_msg lsm;
2444 uint8_t *pending = NULL;
2445
2446 if (session_name == NULL) {
2447 return -LTTNG_ERR_INVALID;
2448 }
2449
2450 memset(&lsm, 0, sizeof(lsm));
2451 lsm.cmd_type = LTTNG_DATA_PENDING;
2452
2453 lttng_ctl_copy_string(lsm.session.name, session_name,
2454 sizeof(lsm.session.name));
2455
2456 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2457 if (ret < 0) {
2458 goto end;
2459 } else if (ret != 1) {
2460 /* Unexpected payload size */
2461 ret = -LTTNG_ERR_INVALID;
2462 goto end;
2463 }
2464
2465 ret = (int) *pending;
2466 end:
2467 free(pending);
2468 return ret;
2469 }
2470
2471 /*
2472 * Create a session exclusively used for snapshot.
2473 *
2474 * Returns LTTNG_OK on success or a negative error code.
2475 */
2476 int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
2477 {
2478 int ret;
2479 ssize_t size;
2480 struct lttcomm_session_msg lsm;
2481 struct lttng_uri *uris = NULL;
2482
2483 if (name == NULL) {
2484 return -LTTNG_ERR_INVALID;
2485 }
2486
2487 memset(&lsm, 0, sizeof(lsm));
2488
2489 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
2490 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2491
2492 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
2493 if (size < 0) {
2494 return -LTTNG_ERR_INVALID;
2495 }
2496
2497 lsm.u.uri.size = size;
2498
2499 /*
2500 * If the user does not specify a custom subdir, use the session name.
2501 */
2502 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
2503 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
2504 if (ret < 0) {
2505 PERROR("snprintf uri subdir");
2506 ret = -LTTNG_ERR_FATAL;
2507 goto error;
2508 }
2509 }
2510
2511 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2512 sizeof(struct lttng_uri) * size, NULL);
2513
2514 error:
2515 free(uris);
2516 return ret;
2517 }
2518
2519 /*
2520 * Create a session exclusively used for live.
2521 *
2522 * Returns LTTNG_OK on success or a negative error code.
2523 */
2524 int lttng_create_session_live(const char *name, const char *url,
2525 unsigned int timer_interval)
2526 {
2527 int ret;
2528 ssize_t size;
2529 struct lttcomm_session_msg lsm;
2530 struct lttng_uri *uris = NULL;
2531
2532 if (name == NULL || timer_interval == 0) {
2533 return -LTTNG_ERR_INVALID;
2534 }
2535
2536 memset(&lsm, 0, sizeof(lsm));
2537
2538 lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE;
2539 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2540
2541 if (url) {
2542 size = uri_parse_str_urls(url, NULL, &uris);
2543 if (size <= 0) {
2544 ret = -LTTNG_ERR_INVALID;
2545 goto end;
2546 }
2547
2548 /* file:// is not accepted for live session. */
2549 if (uris[0].dtype == LTTNG_DST_PATH) {
2550 ret = -LTTNG_ERR_INVALID;
2551 goto end;
2552 }
2553 } else {
2554 size = 0;
2555 }
2556
2557 lsm.u.session_live.nb_uri = size;
2558 lsm.u.session_live.timer_interval = timer_interval;
2559
2560 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2561 sizeof(struct lttng_uri) * size, NULL);
2562
2563 end:
2564 free(uris);
2565 return ret;
2566 }
2567
2568 /*
2569 * List PIDs in the tracker.
2570 *
2571 * enabled is set to whether the PID tracker is enabled.
2572 * pids is set to an allocated array of PIDs currently tracked. On
2573 * success, pids must be freed by the caller.
2574 * nr_pids is set to the number of entries contained by the pids array.
2575 *
2576 * Returns 0 on success, else a negative LTTng error code.
2577 */
2578 int lttng_list_tracker_pids(struct lttng_handle *handle,
2579 int *_enabled, int32_t **_pids, size_t *_nr_pids)
2580 {
2581 int ret;
2582 int enabled = 1;
2583 struct lttcomm_session_msg lsm;
2584 size_t nr_pids;
2585 int32_t *pids;
2586
2587 if (handle == NULL) {
2588 return -LTTNG_ERR_INVALID;
2589 }
2590
2591 memset(&lsm, 0, sizeof(lsm));
2592 lsm.cmd_type = LTTNG_LIST_TRACKER_PIDS;
2593 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2594 sizeof(lsm.session.name));
2595 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2596
2597 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pids);
2598 if (ret < 0) {
2599 return ret;
2600 }
2601 nr_pids = ret / sizeof(int32_t);
2602 if (nr_pids == 1 && pids[0] == -1) {
2603 free(pids);
2604 pids = NULL;
2605 enabled = 0;
2606 nr_pids = 0;
2607 }
2608 *_enabled = enabled;
2609 *_pids = pids;
2610 *_nr_pids = nr_pids;
2611 return 0;
2612 }
2613
2614 /*
2615 * Regenerate the metadata for a session.
2616 * Return 0 on success, a negative error code on error.
2617 */
2618 int lttng_regenerate_metadata(const char *session_name)
2619 {
2620 int ret;
2621 struct lttcomm_session_msg lsm;
2622
2623 if (!session_name) {
2624 ret = -LTTNG_ERR_INVALID;
2625 goto end;
2626 }
2627
2628 memset(&lsm, 0, sizeof(lsm));
2629 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
2630
2631 lttng_ctl_copy_string(lsm.session.name, session_name,
2632 sizeof(lsm.session.name));
2633
2634 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2635 if (ret < 0) {
2636 goto end;
2637 }
2638
2639 ret = 0;
2640 end:
2641 return ret;
2642 }
2643
2644 /*
2645 * Deprecated, replaced by lttng_regenerate_metadata.
2646 */
2647 int lttng_metadata_regenerate(const char *session_name)
2648 {
2649 return lttng_regenerate_metadata(session_name);
2650 }
2651
2652 /*
2653 * Regenerate the statedump of a session.
2654 * Return 0 on success, a negative error code on error.
2655 */
2656 int lttng_regenerate_statedump(const char *session_name)
2657 {
2658 int ret;
2659 struct lttcomm_session_msg lsm;
2660
2661 if (!session_name) {
2662 ret = -LTTNG_ERR_INVALID;
2663 goto end;
2664 }
2665
2666 memset(&lsm, 0, sizeof(lsm));
2667 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2668
2669 lttng_ctl_copy_string(lsm.session.name, session_name,
2670 sizeof(lsm.session.name));
2671
2672 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2673 if (ret < 0) {
2674 goto end;
2675 }
2676
2677 ret = 0;
2678 end:
2679 return ret;
2680 }
2681
2682 int lttng_register_trigger(struct lttng_trigger *trigger)
2683 {
2684 int ret;
2685 struct lttcomm_session_msg lsm;
2686 struct lttng_dynamic_buffer buffer;
2687
2688 lttng_dynamic_buffer_init(&buffer);
2689 if (!trigger) {
2690 ret = -LTTNG_ERR_INVALID;
2691 goto end;
2692 }
2693
2694 if (!lttng_trigger_validate(trigger)) {
2695 ret = -LTTNG_ERR_INVALID_TRIGGER;
2696 goto end;
2697 }
2698
2699 ret = lttng_trigger_serialize(trigger, &buffer);
2700 if (ret < 0) {
2701 ret = -LTTNG_ERR_UNK;
2702 goto end;
2703 }
2704
2705 memset(&lsm, 0, sizeof(lsm));
2706 lsm.cmd_type = LTTNG_REGISTER_TRIGGER;
2707 lsm.u.trigger.length = (uint32_t) buffer.size;
2708 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
2709 buffer.size, NULL);
2710 end:
2711 lttng_dynamic_buffer_reset(&buffer);
2712 return ret;
2713 }
2714
2715 int lttng_unregister_trigger(struct lttng_trigger *trigger)
2716 {
2717 int ret;
2718 struct lttcomm_session_msg lsm;
2719 struct lttng_dynamic_buffer buffer;
2720
2721 lttng_dynamic_buffer_init(&buffer);
2722 if (!trigger) {
2723 ret = -LTTNG_ERR_INVALID;
2724 goto end;
2725 }
2726
2727 if (!lttng_trigger_validate(trigger)) {
2728 ret = -LTTNG_ERR_INVALID_TRIGGER;
2729 goto end;
2730 }
2731
2732 ret = lttng_trigger_serialize(trigger, &buffer);
2733 if (ret < 0) {
2734 ret = -LTTNG_ERR_UNK;
2735 goto end;
2736 }
2737
2738 memset(&lsm, 0, sizeof(lsm));
2739 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
2740 lsm.u.trigger.length = (uint32_t) buffer.size;
2741 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
2742 buffer.size, NULL);
2743 end:
2744 lttng_dynamic_buffer_reset(&buffer);
2745 return ret;
2746 }
2747
2748 int lttng_session_get_current_archive_location(const char *session_name,
2749 char **chunk_path)
2750 {
2751 struct lttcomm_session_msg lsm;
2752 struct lttng_session_get_current_output_return *output_return = NULL;
2753 int ret;
2754 size_t path_len;
2755
2756 memset(&lsm, 0, sizeof(lsm));
2757 lsm.cmd_type = LTTNG_SESSION_GET_CURRENT_OUTPUT;
2758 ret = lttng_strncpy(lsm.session.name, session_name,
2759 sizeof(lsm.session.name));
2760 if (ret) {
2761 ret = -LTTNG_ERR_INVALID;
2762 goto end;
2763 }
2764
2765 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &output_return);
2766 if (ret < 0) {
2767 ret = -1;
2768 goto end;
2769 }
2770
2771 path_len = lttng_strnlen(output_return->path,
2772 sizeof(output_return->path));
2773 if (path_len == 0 || path_len == sizeof(output_return->path)) {
2774 ret = -LTTNG_ERR_NO_SESSION_OUTPUT;
2775 goto end;
2776 }
2777
2778 *chunk_path = zmalloc(path_len + 1);
2779 if (!*chunk_path) {
2780 ret = -1;
2781 goto end;
2782 }
2783 memcpy(*chunk_path, output_return->path, path_len);
2784
2785 ret = 0;
2786
2787 end:
2788 free(output_return);
2789 return ret;
2790 }
2791
2792 /*
2793 * lib constructor.
2794 */
2795 static void __attribute__((constructor)) init(void)
2796 {
2797 /* Set default session group */
2798 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
2799 }
2800
2801 /*
2802 * lib destructor.
2803 */
2804 static void __attribute__((destructor)) lttng_ctl_exit(void)
2805 {
2806 free(tracing_group);
2807 }
This page took 0.119055 seconds and 6 git commands to generate.