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