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