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