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