da93da1cc35078ec232bd0997d023509dec56807
[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
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #define _GNU_SOURCE
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 <lttng/lttng.h>
35
36 /* Socket to session daemon for communication */
37 static int sessiond_socket;
38 static char sessiond_sock_path[PATH_MAX];
39
40 /* Variables */
41 static char *tracing_group;
42 static int connected;
43
44 /* Global */
45
46 /*
47 * Those two variables are used by error.h to silent or control the verbosity of
48 * error message. They are global to the library so application linking with it
49 * are able to compile correctly and also control verbosity of the library.
50 *
51 * Note that it is *not* possible to silent ERR() and PERROR() macros.
52 */
53 int lttng_opt_quiet;
54 int lttng_opt_verbose;
55
56 /*
57 * Copy string from src to dst and enforce null terminated byte.
58 */
59 static void copy_string(char *dst, const char *src, size_t len)
60 {
61 if (src && dst) {
62 strncpy(dst, src, len);
63 /* Enforce the NULL terminated byte */
64 dst[len - 1] = '\0';
65 } else if (dst) {
66 dst[0] = '\0';
67 }
68 }
69
70 /*
71 * Copy domain to lttcomm_session_msg domain.
72 *
73 * If domain is unknown, default domain will be the kernel.
74 */
75 static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
76 {
77 if (src && dst) {
78 switch (src->type) {
79 case LTTNG_DOMAIN_KERNEL:
80 case LTTNG_DOMAIN_UST:
81 /*
82 case LTTNG_DOMAIN_UST_EXEC_NAME:
83 case LTTNG_DOMAIN_UST_PID:
84 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
85 */
86 memcpy(dst, src, sizeof(struct lttng_domain));
87 break;
88 default:
89 memset(dst, 0, sizeof(struct lttng_domain));
90 dst->type = LTTNG_DOMAIN_KERNEL;
91 break;
92 }
93 }
94 }
95
96 /*
97 * Send lttcomm_session_msg to the session daemon.
98 *
99 * On success, returns the number of bytes sent (>=0)
100 * On error, returns -1
101 */
102 static int send_session_msg(struct lttcomm_session_msg *lsm)
103 {
104 int ret;
105
106 if (!connected) {
107 ret = -ENOTCONN;
108 goto end;
109 }
110
111 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
112 sizeof(struct lttcomm_session_msg));
113
114 end:
115 return ret;
116 }
117
118 /*
119 * Receive data from the sessiond socket.
120 *
121 * On success, returns the number of bytes received (>=0)
122 * On error, returns -1 (recvmsg() error) or -ENOTCONN
123 */
124 static int recv_data_sessiond(void *buf, size_t len)
125 {
126 int ret;
127
128 if (!connected) {
129 ret = -ENOTCONN;
130 goto end;
131 }
132
133 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
134
135 end:
136 return ret;
137 }
138
139 /*
140 * Check if we are in the specified group.
141 *
142 * If yes return 1, else return -1.
143 */
144 static int check_tracing_group(const char *grp_name)
145 {
146 struct group *grp_tracing; /* no free(). See getgrnam(3) */
147 gid_t *grp_list;
148 int grp_list_size, grp_id, i;
149 int ret = -1;
150
151 /* Get GID of group 'tracing' */
152 grp_tracing = getgrnam(grp_name);
153 if (!grp_tracing) {
154 /* If grp_tracing is NULL, the group does not exist. */
155 goto end;
156 }
157
158 /* Get number of supplementary group IDs */
159 grp_list_size = getgroups(0, NULL);
160 if (grp_list_size < 0) {
161 perror("getgroups");
162 goto end;
163 }
164
165 /* Alloc group list of the right size */
166 grp_list = malloc(grp_list_size * sizeof(gid_t));
167 if (!grp_list) {
168 perror("malloc");
169 goto end;
170 }
171 grp_id = getgroups(grp_list_size, grp_list);
172 if (grp_id < 0) {
173 perror("getgroups");
174 goto free_list;
175 }
176
177 for (i = 0; i < grp_list_size; i++) {
178 if (grp_list[i] == grp_tracing->gr_gid) {
179 ret = 1;
180 break;
181 }
182 }
183
184 free_list:
185 free(grp_list);
186
187 end:
188 return ret;
189 }
190
191 /*
192 * Try connect to session daemon with sock_path.
193 *
194 * Return 0 on success, else -1
195 */
196 static int try_connect_sessiond(const char *sock_path)
197 {
198 int ret;
199
200 /* If socket exist, we check if the daemon listens for connect. */
201 ret = access(sock_path, F_OK);
202 if (ret < 0) {
203 /* Not alive */
204 return -1;
205 }
206
207 ret = lttcomm_connect_unix_sock(sock_path);
208 if (ret < 0) {
209 /* Not alive */
210 return -1;
211 }
212
213 ret = lttcomm_close_unix_sock(ret);
214 if (ret < 0) {
215 perror("lttcomm_close_unix_sock");
216 }
217
218 return 0;
219 }
220
221 /*
222 * Set sessiond socket path by putting it in the global
223 * sessiond_sock_path variable.
224 * Returns 0 on success,
225 * -ENOMEM on failure (the sessiond socket path is somehow too long)
226 */
227 static int set_session_daemon_path(void)
228 {
229 int ret;
230 int in_tgroup = 0; /* In tracing group */
231 uid_t uid;
232
233 uid = getuid();
234
235 if (uid != 0) {
236 /* Are we in the tracing group ? */
237 in_tgroup = check_tracing_group(tracing_group);
238 }
239
240 if ((uid == 0) || in_tgroup) {
241 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
242 sizeof(sessiond_sock_path));
243 }
244
245 if (uid != 0) {
246 if (in_tgroup) {
247 /* Tracing group */
248 ret = try_connect_sessiond(sessiond_sock_path);
249 if (ret >= 0) {
250 goto end;
251 }
252 /* Global session daemon not available... */
253 }
254 /* ...or not in tracing group (and not root), default */
255
256 /*
257 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
258 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
259 */
260 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
261 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
262 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
263 return -ENOMEM;
264 }
265 }
266 end:
267 return 0;
268 }
269
270 /*
271 * Connect to the LTTng session daemon.
272 *
273 * On success, return 0. On error, return -1.
274 */
275 static int connect_sessiond(void)
276 {
277 int ret;
278
279 ret = set_session_daemon_path();
280 if (ret < 0) {
281 return -1; /* set_session_daemon_path() returns -ENOMEM */
282 }
283
284 /* Connect to the sesssion daemon */
285 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
286 if (ret < 0) {
287 return ret;
288 }
289
290 sessiond_socket = ret;
291 connected = 1;
292
293 return 0;
294 }
295
296 /*
297 * Clean disconnect from the session daemon.
298 * On success, return 0. On error, return -1.
299 */
300 static int disconnect_sessiond(void)
301 {
302 int ret = 0;
303
304 if (connected) {
305 ret = lttcomm_close_unix_sock(sessiond_socket);
306 sessiond_socket = 0;
307 connected = 0;
308 }
309
310 return ret;
311 }
312
313 /*
314 * Ask the session daemon a specific command and put the data into buf.
315 *
316 * Return size of data (only payload, not header) or a negative error code.
317 */
318 static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
319 {
320 int ret;
321 size_t size;
322 void *data = NULL;
323 struct lttcomm_lttng_msg llm;
324
325 ret = connect_sessiond();
326 if (ret < 0) {
327 goto end;
328 }
329
330 /* Send command to session daemon */
331 ret = send_session_msg(lsm);
332 if (ret < 0) {
333 goto end;
334 }
335
336 /* Get header from data transmission */
337 ret = recv_data_sessiond(&llm, sizeof(llm));
338 if (ret < 0) {
339 goto end;
340 }
341
342 /* Check error code if OK */
343 if (llm.ret_code != LTTCOMM_OK) {
344 ret = -llm.ret_code;
345 goto end;
346 }
347
348 size = llm.data_size;
349 if (size == 0) {
350 /* If client free with size 0 */
351 if (buf != NULL) {
352 *buf = NULL;
353 }
354 ret = 0;
355 goto end;
356 }
357
358 data = (void*) malloc(size);
359
360 /* Get payload data */
361 ret = recv_data_sessiond(data, size);
362 if (ret < 0) {
363 free(data);
364 goto end;
365 }
366
367 /*
368 * Extra protection not to dereference a NULL pointer. If buf is NULL at
369 * this point, an error is returned and data is freed.
370 */
371 if (buf == NULL) {
372 ret = -1;
373 free(data);
374 goto end;
375 }
376
377 *buf = data;
378 ret = size;
379
380 end:
381 disconnect_sessiond();
382 return ret;
383 }
384
385 /*
386 * Create lttng handle and return pointer.
387 * The returned pointer will be NULL in case of malloc() error.
388 */
389 struct lttng_handle *lttng_create_handle(const char *session_name,
390 struct lttng_domain *domain)
391 {
392 struct lttng_handle *handle;
393
394 handle = malloc(sizeof(struct lttng_handle));
395 if (handle == NULL) {
396 perror("malloc handle");
397 goto end;
398 }
399
400 /* Copy session name */
401 copy_string(handle->session_name, session_name,
402 sizeof(handle->session_name));
403
404 /* Copy lttng domain */
405 copy_lttng_domain(&handle->domain, domain);
406
407 end:
408 return handle;
409 }
410
411 /*
412 * Destroy handle by free(3) the pointer.
413 */
414 void lttng_destroy_handle(struct lttng_handle *handle)
415 {
416 if (handle) {
417 free(handle);
418 }
419 }
420
421 /*
422 * Register an outside consumer.
423 * Returns size of returned session payload data or a negative error code.
424 */
425 int lttng_register_consumer(struct lttng_handle *handle,
426 const char *socket_path)
427 {
428 struct lttcomm_session_msg lsm;
429
430 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
431 copy_string(lsm.session.name, handle->session_name,
432 sizeof(lsm.session.name));
433 copy_lttng_domain(&lsm.domain, &handle->domain);
434
435 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
436
437 return ask_sessiond(&lsm, NULL);
438 }
439
440 /*
441 * Start tracing for all traces of the session.
442 * Returns size of returned session payload data or a negative error code.
443 */
444 int lttng_start_tracing(const char *session_name)
445 {
446 struct lttcomm_session_msg lsm;
447
448 if (session_name == NULL) {
449 return -1;
450 }
451
452 lsm.cmd_type = LTTNG_START_TRACE;
453
454 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
455
456 return ask_sessiond(&lsm, NULL);
457 }
458
459 /*
460 * Stop tracing for all traces of the session.
461 * Returns size of returned session payload data or a negative error code.
462 */
463 int lttng_stop_tracing(const char *session_name)
464 {
465 struct lttcomm_session_msg lsm;
466
467 if (session_name == NULL) {
468 return -1;
469 }
470
471 lsm.cmd_type = LTTNG_STOP_TRACE;
472
473 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
474
475 return ask_sessiond(&lsm, NULL);
476 }
477
478 /*
479 * Add context to event and/or channel.
480 * If event_name is NULL, the context is applied to all events of the channel.
481 * If channel_name is NULL, a lookup of the event's channel is done.
482 * If both are NULL, the context is applied to all events of all channels.
483 *
484 * Returns the size of the returned payload data or a negative error code.
485 */
486 int lttng_add_context(struct lttng_handle *handle,
487 struct lttng_event_context *ctx, const char *event_name,
488 const char *channel_name)
489 {
490 struct lttcomm_session_msg lsm;
491
492 /* Safety check. Both are mandatory */
493 if (handle == NULL || ctx == NULL) {
494 return -1;
495 }
496
497 memset(&lsm, 0, sizeof(lsm));
498
499 lsm.cmd_type = LTTNG_ADD_CONTEXT;
500
501 /* Copy channel name */
502 copy_string(lsm.u.context.channel_name, channel_name,
503 sizeof(lsm.u.context.channel_name));
504 /* Copy event name */
505 copy_string(lsm.u.context.event_name, event_name,
506 sizeof(lsm.u.context.event_name));
507
508 copy_lttng_domain(&lsm.domain, &handle->domain);
509
510 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
511
512 copy_string(lsm.session.name, handle->session_name,
513 sizeof(lsm.session.name));
514
515 return ask_sessiond(&lsm, NULL);
516 }
517
518 /*
519 * Enable event(s) for a channel.
520 * If no event name is specified, all events are enabled.
521 * If no channel name is specified, the default 'channel0' is used.
522 * Returns size of returned session payload data or a negative error code.
523 */
524 int lttng_enable_event(struct lttng_handle *handle,
525 struct lttng_event *ev, const char *channel_name)
526 {
527 struct lttcomm_session_msg lsm;
528
529 if (handle == NULL || ev == NULL) {
530 return -1;
531 }
532
533 memset(&lsm, 0, sizeof(lsm));
534
535 /* If no channel name, we put the default name */
536 if (channel_name == NULL) {
537 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
538 sizeof(lsm.u.enable.channel_name));
539 } else {
540 copy_string(lsm.u.enable.channel_name, channel_name,
541 sizeof(lsm.u.enable.channel_name));
542 }
543
544 copy_lttng_domain(&lsm.domain, &handle->domain);
545
546 if (ev->name[0] != '\0') {
547 lsm.cmd_type = LTTNG_ENABLE_EVENT;
548 } else {
549 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
550 }
551 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
552
553 copy_string(lsm.session.name, handle->session_name,
554 sizeof(lsm.session.name));
555
556 return ask_sessiond(&lsm, NULL);
557 }
558
559 /*
560 * Disable event(s) of a channel and domain.
561 * If no event name is specified, all events are disabled.
562 * If no channel name is specified, the default 'channel0' is used.
563 * Returns size of returned session payload data or a negative error code.
564 */
565 int lttng_disable_event(struct lttng_handle *handle, const char *name,
566 const char *channel_name)
567 {
568 struct lttcomm_session_msg lsm;
569
570 if (handle == NULL) {
571 return -1;
572 }
573
574 memset(&lsm, 0, sizeof(lsm));
575
576 if (channel_name) {
577 copy_string(lsm.u.disable.channel_name, channel_name,
578 sizeof(lsm.u.disable.channel_name));
579 } else {
580 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
581 sizeof(lsm.u.disable.channel_name));
582 }
583
584 copy_lttng_domain(&lsm.domain, &handle->domain);
585
586 if (name != NULL) {
587 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
588 lsm.cmd_type = LTTNG_DISABLE_EVENT;
589 } else {
590 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
591 }
592
593 copy_string(lsm.session.name, handle->session_name,
594 sizeof(lsm.session.name));
595
596 return ask_sessiond(&lsm, NULL);
597 }
598
599 /*
600 * Enable channel per domain
601 * Returns size of returned session payload data or a negative error code.
602 */
603 int lttng_enable_channel(struct lttng_handle *handle,
604 struct lttng_channel *chan)
605 {
606 struct lttcomm_session_msg lsm;
607
608 /*
609 * NULL arguments are forbidden. No default values.
610 */
611 if (handle == NULL || chan == NULL) {
612 return -1;
613 }
614
615 memset(&lsm, 0, sizeof(lsm));
616
617 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
618
619 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
620
621 copy_lttng_domain(&lsm.domain, &handle->domain);
622
623 copy_string(lsm.session.name, handle->session_name,
624 sizeof(lsm.session.name));
625
626 return ask_sessiond(&lsm, NULL);
627 }
628
629 /*
630 * All tracing will be stopped for registered events of the channel.
631 * Returns size of returned session payload data or a negative error code.
632 */
633 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
634 {
635 struct lttcomm_session_msg lsm;
636
637 /* Safety check. Both are mandatory */
638 if (handle == NULL || name == NULL) {
639 return -1;
640 }
641
642 memset(&lsm, 0, sizeof(lsm));
643
644 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
645
646 copy_string(lsm.u.disable.channel_name, name,
647 sizeof(lsm.u.disable.channel_name));
648
649 copy_lttng_domain(&lsm.domain, &handle->domain);
650
651 copy_string(lsm.session.name, handle->session_name,
652 sizeof(lsm.session.name));
653
654 return ask_sessiond(&lsm, NULL);
655 }
656
657 /*
658 * Lists all available tracepoints of domain.
659 * Sets the contents of the events array.
660 * Returns the number of lttng_event entries in events;
661 * on error, returns a negative value.
662 */
663 int lttng_list_tracepoints(struct lttng_handle *handle,
664 struct lttng_event **events)
665 {
666 int ret;
667 struct lttcomm_session_msg lsm;
668
669 if (handle == NULL) {
670 return -1;
671 }
672
673 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
674 copy_lttng_domain(&lsm.domain, &handle->domain);
675
676 ret = ask_sessiond(&lsm, (void **) events);
677 if (ret < 0) {
678 return ret;
679 }
680
681 return ret / sizeof(struct lttng_event);
682 }
683
684 /*
685 * Returns a human readable string describing
686 * the error code (a negative value).
687 */
688 const char *lttng_strerror(int code)
689 {
690 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
691 if (code > -LTTCOMM_OK) {
692 return "Ended with errors";
693 }
694
695 return lttcomm_get_readable_code(code);
696 }
697
698 /*
699 * Create a brand new session using name and path.
700 * Returns size of returned session payload data or a negative error code.
701 */
702 int lttng_create_session(const char *name, const char *path)
703 {
704 struct lttcomm_session_msg lsm;
705
706 lsm.cmd_type = LTTNG_CREATE_SESSION;
707 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
708 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
709
710 return ask_sessiond(&lsm, NULL);
711 }
712
713 /*
714 * Destroy session using name.
715 * Returns size of returned session payload data or a negative error code.
716 */
717 int lttng_destroy_session(const char *session_name)
718 {
719 struct lttcomm_session_msg lsm;
720
721 if (session_name == NULL) {
722 return -1;
723 }
724
725 lsm.cmd_type = LTTNG_DESTROY_SESSION;
726
727 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
728
729 return ask_sessiond(&lsm, NULL);
730 }
731
732 /*
733 * Ask the session daemon for all available sessions.
734 * Sets the contents of the sessions array.
735 * Returns the number of lttng_session entries in sessions;
736 * on error, returns a negative value.
737 */
738 int lttng_list_sessions(struct lttng_session **sessions)
739 {
740 int ret;
741 struct lttcomm_session_msg lsm;
742
743 lsm.cmd_type = LTTNG_LIST_SESSIONS;
744 ret = ask_sessiond(&lsm, (void**) sessions);
745 if (ret < 0) {
746 return ret;
747 }
748
749 return ret / sizeof(struct lttng_session);
750 }
751
752 /*
753 * Ask the session daemon for all available domains of a session.
754 * Sets the contents of the domains array.
755 * Returns the number of lttng_domain entries in domains;
756 * on error, returns a negative value.
757 */
758 int lttng_list_domains(const char *session_name,
759 struct lttng_domain **domains)
760 {
761 int ret;
762 struct lttcomm_session_msg lsm;
763
764 if (session_name == NULL) {
765 return -1;
766 }
767
768 lsm.cmd_type = LTTNG_LIST_DOMAINS;
769
770 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
771
772 ret = ask_sessiond(&lsm, (void**) domains);
773 if (ret < 0) {
774 return ret;
775 }
776
777 return ret / sizeof(struct lttng_domain);
778 }
779
780 /*
781 * Ask the session daemon for all available channels of a session.
782 * Sets the contents of the channels array.
783 * Returns the number of lttng_channel entries in channels;
784 * on error, returns a negative value.
785 */
786 int lttng_list_channels(struct lttng_handle *handle,
787 struct lttng_channel **channels)
788 {
789 int ret;
790 struct lttcomm_session_msg lsm;
791
792 if (handle == NULL) {
793 return -1;
794 }
795
796 lsm.cmd_type = LTTNG_LIST_CHANNELS;
797 copy_string(lsm.session.name, handle->session_name,
798 sizeof(lsm.session.name));
799
800 copy_lttng_domain(&lsm.domain, &handle->domain);
801
802 ret = ask_sessiond(&lsm, (void**) channels);
803 if (ret < 0) {
804 return ret;
805 }
806
807 return ret / sizeof(struct lttng_channel);
808 }
809
810 /*
811 * Ask the session daemon for all available events of a session channel.
812 * Sets the contents of the events array.
813 * Returns the number of lttng_event entries in events;
814 * on error, returns a negative value.
815 */
816 int lttng_list_events(struct lttng_handle *handle,
817 const char *channel_name, struct lttng_event **events)
818 {
819 int ret;
820 struct lttcomm_session_msg lsm;
821
822 /* Safety check. An handle and channel name are mandatory */
823 if (handle == NULL || channel_name == NULL) {
824 return -1;
825 }
826
827 lsm.cmd_type = LTTNG_LIST_EVENTS;
828 copy_string(lsm.session.name, handle->session_name,
829 sizeof(lsm.session.name));
830 copy_string(lsm.u.list.channel_name, channel_name,
831 sizeof(lsm.u.list.channel_name));
832
833 copy_lttng_domain(&lsm.domain, &handle->domain);
834
835 ret = ask_sessiond(&lsm, (void**) events);
836 if (ret < 0) {
837 return ret;
838 }
839
840 return ret / sizeof(struct lttng_event);
841 }
842
843 /*
844 * Sets the tracing_group variable with name.
845 * This function allocates memory pointed to by tracing_group.
846 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
847 */
848 int lttng_set_tracing_group(const char *name)
849 {
850 if (name == NULL) {
851 return -1;
852 }
853
854 if (asprintf(&tracing_group, "%s", name) < 0) {
855 return -ENOMEM;
856 }
857
858 return 0;
859 }
860
861 /*
862 * Returns size of returned session payload data or a negative error code.
863 */
864 int lttng_calibrate(struct lttng_handle *handle,
865 struct lttng_calibrate *calibrate)
866 {
867 struct lttcomm_session_msg lsm;
868
869 /* Safety check. NULL pointer are forbidden */
870 if (handle == NULL || calibrate == NULL) {
871 return -1;
872 }
873
874 lsm.cmd_type = LTTNG_CALIBRATE;
875 copy_lttng_domain(&lsm.domain, &handle->domain);
876
877 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
878
879 return ask_sessiond(&lsm, NULL);
880 }
881
882 /*
883 * Set default channel attributes.
884 * If either or both of the arguments are null, attr content is zeroe'd.
885 */
886 void lttng_channel_set_default_attr(struct lttng_domain *domain,
887 struct lttng_channel_attr *attr)
888 {
889 memset(attr, 0, sizeof(struct lttng_channel_attr));
890
891 /* Safety check */
892 if (attr == NULL || domain == NULL) {
893 return;
894 }
895
896 switch (domain->type) {
897 case LTTNG_DOMAIN_KERNEL:
898 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
899 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
900 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
901
902 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
903 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
904 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
905 break;
906 case LTTNG_DOMAIN_UST:
907 #if 0
908 case LTTNG_DOMAIN_UST_EXEC_NAME:
909 case LTTNG_DOMAIN_UST_PID:
910 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
911 #endif
912 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
913 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
914 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
915
916 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
917 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
918 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
919 break;
920 default:
921 /* Default behavior: leave set to 0. */
922 break;
923 }
924 }
925
926 /*
927 * Check if session daemon is alive.
928 *
929 * Return 1 if alive or 0 if not.
930 * On error returns a negative value.
931 */
932 int lttng_session_daemon_alive(void)
933 {
934 int ret;
935
936 ret = set_session_daemon_path();
937 if (ret < 0) {
938 /* Error */
939 return ret;
940 }
941
942 if (strlen(sessiond_sock_path) == 0) {
943 /* No socket path set. Weird error */
944 return -1;
945 }
946
947 ret = try_connect_sessiond(sessiond_sock_path);
948 if (ret < 0) {
949 /* Not alive */
950 return 0;
951 }
952
953 /* Is alive */
954 return 1;
955 }
956
957 /*
958 * lib constructor
959 */
960 static void __attribute__((constructor)) init()
961 {
962 /* Set default session group */
963 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
964 }
This page took 0.048995 seconds and 4 git commands to generate.