Add lttng-modules detection for tests
[lttng-tools.git] / liblttngctl / lttngctl.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 <errno.h>
25 #include <grp.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <lttng/lttng.h>
32
33 #include <lttng-sessiond-comm.h>
34 #include "lttngerr.h"
35 #include "lttng-share.h"
36
37 /* Socket to session daemon for communication */
38 static int sessiond_socket;
39 static char sessiond_sock_path[PATH_MAX];
40
41 /* Variables */
42 static char *tracing_group;
43 static int connected;
44
45 /*
46 * Copy string from src to dst and enforce null terminated byte.
47 */
48 static void copy_string(char *dst, const char *src, size_t len)
49 {
50 if (src && dst) {
51 strncpy(dst, src, len);
52 /* Enforce the NULL terminated byte */
53 dst[len - 1] = '\0';
54 } else if (dst) {
55 dst[0] = '\0';
56 }
57 }
58
59 /*
60 * Copy domain to lttcomm_session_msg domain.
61 *
62 * If domain is unknown, default domain will be the kernel.
63 */
64 static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
65 {
66 if (src && dst) {
67 switch (src->type) {
68 case LTTNG_DOMAIN_KERNEL:
69 case LTTNG_DOMAIN_UST:
70 case LTTNG_DOMAIN_UST_EXEC_NAME:
71 case LTTNG_DOMAIN_UST_PID:
72 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
73 memcpy(dst, src, sizeof(struct lttng_domain));
74 break;
75 default:
76 dst->type = LTTNG_DOMAIN_KERNEL;
77 break;
78 }
79 }
80 }
81
82 /*
83 * Send lttcomm_session_msg to the session daemon.
84 *
85 * On success, return 0
86 * On error, return error code
87 */
88 static int send_session_msg(struct lttcomm_session_msg *lsm)
89 {
90 int ret;
91
92 if (!connected) {
93 ret = -ENOTCONN;
94 goto end;
95 }
96
97 ret = lttcomm_send_unix_sock(sessiond_socket, lsm,
98 sizeof(struct lttcomm_session_msg));
99
100 end:
101 return ret;
102 }
103
104 /*
105 * Receive data from the sessiond socket.
106 *
107 * On success, return 0
108 * On error, return recv() error code
109 */
110 static int recv_data_sessiond(void *buf, size_t len)
111 {
112 int ret;
113
114 if (!connected) {
115 ret = -ENOTCONN;
116 goto end;
117 }
118
119 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
120
121 end:
122 return ret;
123 }
124
125 /*
126 * Check if the specified group name exist.
127 *
128 * If yes return 0, else return -1.
129 */
130 static int check_tracing_group(const char *grp_name)
131 {
132 struct group *grp_tracing; /* no free(). See getgrnam(3) */
133 gid_t *grp_list;
134 int grp_list_size, grp_id, i;
135 int ret = -1;
136
137 /* Get GID of group 'tracing' */
138 grp_tracing = getgrnam(grp_name);
139 if (grp_tracing == NULL) {
140 /* NULL means not found also. getgrnam(3) */
141 if (errno != 0) {
142 perror("getgrnam");
143 }
144 goto end;
145 }
146
147 /* Get number of supplementary group IDs */
148 grp_list_size = getgroups(0, NULL);
149 if (grp_list_size < 0) {
150 perror("getgroups");
151 goto end;
152 }
153
154 /* Alloc group list of the right size */
155 grp_list = malloc(grp_list_size * sizeof(gid_t));
156 grp_id = getgroups(grp_list_size, grp_list);
157 if (grp_id < -1) {
158 perror("getgroups");
159 goto free_list;
160 }
161
162 for (i = 0; i < grp_list_size; i++) {
163 if (grp_list[i] == grp_tracing->gr_gid) {
164 ret = 0;
165 break;
166 }
167 }
168
169 free_list:
170 free(grp_list);
171
172 end:
173 return ret;
174 }
175
176 /*
177 * Set sessiond socket path by putting it in the global sessiond_sock_path
178 * variable.
179 */
180 static int set_session_daemon_path(void)
181 {
182 int ret;
183
184 /* Are we in the tracing group ? */
185 ret = check_tracing_group(tracing_group);
186 if (ret < 0 && getuid() != 0) {
187 if (snprintf(sessiond_sock_path, PATH_MAX,
188 DEFAULT_HOME_CLIENT_UNIX_SOCK,
189 getenv("HOME")) < 0) {
190 return -ENOMEM;
191 }
192 } else {
193 copy_string(sessiond_sock_path,
194 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
195 PATH_MAX);
196 }
197
198 return 0;
199 }
200
201 /*
202 * Connect to the LTTng session daemon.
203 *
204 * On success, return 0. On error, return -1.
205 */
206 static int connect_sessiond(void)
207 {
208 int ret;
209
210 ret = set_session_daemon_path();
211 if (ret < 0) {
212 return ret;
213 }
214
215 /* Connect to the sesssion daemon */
216 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
217 if (ret < 0) {
218 return ret;
219 }
220
221 sessiond_socket = ret;
222 connected = 1;
223
224 return 0;
225 }
226
227 /*
228 * Clean disconnect the session daemon.
229 */
230 static int disconnect_sessiond(void)
231 {
232 int ret = 0;
233
234 if (connected) {
235 ret = lttcomm_close_unix_sock(sessiond_socket);
236 sessiond_socket = 0;
237 connected = 0;
238 }
239
240 return ret;
241 }
242
243 /*
244 * Ask the session daemon a specific command and put the data into buf.
245 *
246 * Return size of data (only payload, not header).
247 */
248 static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
249 {
250 int ret;
251 size_t size;
252 void *data = NULL;
253 struct lttcomm_lttng_msg llm;
254
255 ret = connect_sessiond();
256 if (ret < 0) {
257 goto end;
258 }
259
260 /* Send command to session daemon */
261 ret = send_session_msg(lsm);
262 if (ret < 0) {
263 goto end;
264 }
265
266 /* Get header from data transmission */
267 ret = recv_data_sessiond(&llm, sizeof(llm));
268 if (ret < 0) {
269 goto end;
270 }
271
272 /* Check error code if OK */
273 if (llm.ret_code != LTTCOMM_OK) {
274 ret = -llm.ret_code;
275 goto end;
276 }
277
278 size = llm.data_size;
279 if (size == 0) {
280 ret = 0;
281 goto end;
282 }
283
284 data = (void*) malloc(size);
285
286 /* Get payload data */
287 ret = recv_data_sessiond(data, size);
288 if (ret < 0) {
289 free(data);
290 goto end;
291 }
292
293 *buf = data;
294 ret = size;
295
296 end:
297 disconnect_sessiond();
298 return ret;
299 }
300
301 /*
302 * Create lttng handle and return pointer.
303 */
304 struct lttng_handle *lttng_create_handle(const char *session_name,
305 struct lttng_domain *domain)
306 {
307 struct lttng_handle *handle;
308
309 handle = malloc(sizeof(struct lttng_handle));
310 if (handle == NULL) {
311 perror("malloc handle");
312 goto end;
313 }
314
315 /* Copy session name */
316 copy_string(handle->session_name, session_name,
317 sizeof(handle->session_name));
318
319 /* Copy lttng domain */
320 copy_lttng_domain(&handle->domain, domain);
321
322 end:
323 return handle;
324 }
325
326 /*
327 * Destroy handle by free(3) the pointer.
328 */
329 void lttng_destroy_handle(struct lttng_handle *handle)
330 {
331 if (handle) {
332 free(handle);
333 }
334 }
335
336 /*
337 * Register an outside consumer.
338 */
339 int lttng_register_consumer(struct lttng_handle *handle,
340 const char *socket_path)
341 {
342 struct lttcomm_session_msg lsm;
343
344 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
345 copy_string(lsm.session.name, handle->session_name,
346 sizeof(lsm.session.name));
347 copy_lttng_domain(&lsm.domain, &handle->domain);
348
349 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
350
351 return ask_sessiond(&lsm, NULL);
352 }
353
354 /*
355 * Start tracing for all trace of the session.
356 */
357 int lttng_start_tracing(struct lttng_handle *handle)
358 {
359 struct lttcomm_session_msg lsm;
360
361 if (!handle) {
362 return -1;
363 }
364
365 lsm.cmd_type = LTTNG_START_TRACE;
366 copy_string(lsm.session.name, handle->session_name,
367 sizeof(lsm.session.name));
368
369 return ask_sessiond(&lsm, NULL);
370 }
371
372 /*
373 * Stop tracing for all trace of the session.
374 */
375 int lttng_stop_tracing(struct lttng_handle *handle)
376 {
377 struct lttcomm_session_msg lsm;
378
379 lsm.cmd_type = LTTNG_STOP_TRACE;
380 copy_string(lsm.session.name, handle->session_name,
381 sizeof(lsm.session.name));
382
383 return ask_sessiond(&lsm, NULL);
384 }
385
386 /*
387 * Add context to event or/and channel.
388 */
389 int lttng_add_context(struct lttng_handle *handle,
390 struct lttng_event_context *ctx, const char *event_name,
391 const char *channel_name)
392 {
393 struct lttcomm_session_msg lsm;
394
395 if (!handle) {
396 return -1;
397 }
398
399 lsm.cmd_type = LTTNG_ADD_CONTEXT;
400
401 /* Copy channel name */
402 copy_string(lsm.u.context.channel_name, channel_name,
403 sizeof(lsm.u.context.channel_name));
404 /* Copy event name */
405 copy_string(lsm.u.context.event_name, event_name,
406 sizeof(lsm.u.context.event_name));
407
408 copy_lttng_domain(&lsm.domain, &handle->domain);
409
410 if (ctx) {
411 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
412 }
413
414 copy_string(lsm.session.name, handle->session_name,
415 sizeof(lsm.session.name));
416
417 return ask_sessiond(&lsm, NULL);
418 }
419
420 /*
421 * Enable event
422 */
423 int lttng_enable_event(struct lttng_handle *handle,
424 struct lttng_event *ev, const char *channel_name)
425 {
426 struct lttcomm_session_msg lsm;
427
428 if (!handle) {
429 return -1;
430 }
431
432 if (channel_name == NULL) {
433 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
434 sizeof(lsm.u.enable.channel_name));
435 } else {
436 copy_string(lsm.u.enable.channel_name, channel_name,
437 sizeof(lsm.u.enable.channel_name));
438 }
439
440 copy_lttng_domain(&lsm.domain, &handle->domain);
441
442 if (ev) {
443 lsm.cmd_type = LTTNG_ENABLE_EVENT;
444 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
445 } else {
446 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
447 }
448
449 copy_string(lsm.session.name, handle->session_name,
450 sizeof(lsm.session.name));
451
452 return ask_sessiond(&lsm, NULL);
453 }
454
455 /*
456 * Disable event of a channel and domain.
457 */
458 int lttng_disable_event(struct lttng_handle *handle, const char *name,
459 const char *channel_name)
460 {
461 struct lttcomm_session_msg lsm;
462
463 if (!handle) {
464 return -1;
465 }
466
467 if (channel_name) {
468 copy_string(lsm.u.disable.channel_name, channel_name,
469 sizeof(lsm.u.disable.channel_name));
470 } else {
471 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
472 sizeof(lsm.u.disable.channel_name));
473 }
474
475 copy_lttng_domain(&lsm.domain, &handle->domain);
476
477 if (name == NULL) {
478 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
479 lsm.cmd_type = LTTNG_DISABLE_EVENT;
480 } else {
481 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
482 }
483
484 copy_string(lsm.session.name, handle->session_name,
485 sizeof(lsm.session.name));
486
487 return ask_sessiond(&lsm, NULL);
488 }
489
490 /*
491 * Enable channel per domain
492 */
493 int lttng_enable_channel(struct lttng_handle *handle,
494 struct lttng_channel *chan)
495 {
496 struct lttcomm_session_msg lsm;
497
498 if (!handle) {
499 return -1;
500 }
501
502 if (chan) {
503 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
504 }
505
506 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
507
508 copy_lttng_domain(&lsm.domain, &handle->domain);
509
510 copy_string(lsm.session.name, handle->session_name,
511 sizeof(lsm.session.name));
512
513 return ask_sessiond(&lsm, NULL);
514 }
515
516 /*
517 * All tracing will be stopped for registered events of the channel.
518 */
519 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
520 {
521 struct lttcomm_session_msg lsm;
522
523 if (!handle) {
524 return -1;
525 }
526
527 if (name) {
528 copy_string(lsm.u.disable.channel_name, name,
529 sizeof(lsm.u.disable.channel_name));
530 }
531
532 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
533
534 copy_lttng_domain(&lsm.domain, &handle->domain);
535
536 copy_string(lsm.session.name, handle->session_name,
537 sizeof(lsm.session.name));
538
539 return ask_sessiond(&lsm, NULL);
540 }
541
542 /*
543 * List all available tracepoints of domain.
544 *
545 * Return the size (bytes) of the list and set the events array.
546 * On error, return negative value.
547 */
548 int lttng_list_tracepoints(struct lttng_handle *handle,
549 struct lttng_event **events)
550 {
551 int ret;
552 struct lttcomm_session_msg lsm;
553
554 if (!handle) {
555 return -1;
556 }
557
558 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
559 copy_lttng_domain(&lsm.domain, &handle->domain);
560
561 ret = ask_sessiond(&lsm, (void **) events);
562 if (ret < 0) {
563 return ret;
564 }
565
566 return ret / sizeof(struct lttng_event);
567 }
568
569 /*
570 * Return a human readable string of code
571 */
572 const char *lttng_get_readable_code(int code)
573 {
574 if (code > -LTTCOMM_OK) {
575 return "Ended with errors";
576 }
577
578 return lttcomm_get_readable_code(code);
579 }
580
581 /*
582 * Create a brand new session using name.
583 */
584 int lttng_create_session(const char *name, const char *path)
585 {
586 struct lttcomm_session_msg lsm;
587
588 lsm.cmd_type = LTTNG_CREATE_SESSION;
589 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
590 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
591
592 return ask_sessiond(&lsm, NULL);
593 }
594
595 /*
596 * Destroy session using name.
597 */
598 int lttng_destroy_session(struct lttng_handle *handle)
599 {
600 struct lttcomm_session_msg lsm;
601
602 if (!handle) {
603 return -1;
604 }
605
606 lsm.cmd_type = LTTNG_DESTROY_SESSION;
607 copy_string(lsm.session.name, handle->session_name,
608 sizeof(lsm.session.name));
609
610 return ask_sessiond(&lsm, NULL);
611 }
612
613 /*
614 * Ask the session daemon for all available sessions.
615 *
616 * Return number of session.
617 * On error, return negative value.
618 */
619 int lttng_list_sessions(struct lttng_session **sessions)
620 {
621 int ret;
622 struct lttcomm_session_msg lsm;
623
624 lsm.cmd_type = LTTNG_LIST_SESSIONS;
625 ret = ask_sessiond(&lsm, (void**) sessions);
626 if (ret < 0) {
627 return ret;
628 }
629
630 return ret / sizeof(struct lttng_session);
631 }
632
633 /*
634 * List domain of a session.
635 */
636 int lttng_list_domains(struct lttng_handle *handle,
637 struct lttng_domain **domains)
638 {
639 int ret;
640 struct lttcomm_session_msg lsm;
641
642 if (!handle) {
643 return -1;
644 }
645
646 lsm.cmd_type = LTTNG_LIST_DOMAINS;
647
648 copy_string(lsm.session.name, handle->session_name,
649 sizeof(lsm.session.name));
650
651 ret = ask_sessiond(&lsm, (void**) domains);
652 if (ret < 0) {
653 return ret;
654 }
655
656 return ret / sizeof(struct lttng_domain);
657 }
658
659 /*
660 * List channels of a session
661 */
662 int lttng_list_channels(struct lttng_handle *handle,
663 struct lttng_channel **channels)
664 {
665 int ret;
666 struct lttcomm_session_msg lsm;
667
668 if (!handle) {
669 return -1;
670 }
671
672 lsm.cmd_type = LTTNG_LIST_CHANNELS;
673 copy_string(lsm.session.name, handle->session_name,
674 sizeof(lsm.session.name));
675
676 copy_lttng_domain(&lsm.domain, &handle->domain);
677
678 ret = ask_sessiond(&lsm, (void**) channels);
679 if (ret < 0) {
680 return ret;
681 }
682
683 return ret / sizeof(struct lttng_channel);
684 }
685
686 /*
687 * List events of a session channel.
688 */
689 int lttng_list_events(struct lttng_handle *handle,
690 const char *channel_name, struct lttng_event **events)
691 {
692 int ret;
693 struct lttcomm_session_msg lsm;
694
695 if (!handle) {
696 return -1;
697 }
698
699 lsm.cmd_type = LTTNG_LIST_EVENTS;
700 copy_string(lsm.session.name, handle->session_name,
701 sizeof(lsm.session.name));
702 copy_string(lsm.u.list.channel_name, channel_name,
703 sizeof(lsm.u.list.channel_name));
704
705 copy_lttng_domain(&lsm.domain, &handle->domain);
706
707 ret = ask_sessiond(&lsm, (void**) events);
708 if (ret < 0) {
709 return ret;
710 }
711
712 return ret / sizeof(struct lttng_event);
713 }
714
715 /*
716 * lttng_set_tracing_group
717 *
718 * Set tracing group variable with name. This function
719 * allocate memory pointed by tracing_group.
720 */
721 int lttng_set_tracing_group(const char *name)
722 {
723 if (asprintf(&tracing_group, "%s", name) < 0) {
724 return -ENOMEM;
725 }
726
727 return 0;
728 }
729
730 /*
731 * lttng_calibrate
732 */
733 int lttng_calibrate(struct lttng_handle *handle,
734 struct lttng_calibrate *calibrate)
735 {
736 struct lttcomm_session_msg lsm;
737
738 if (!handle) {
739 return -1;
740 }
741
742 lsm.cmd_type = LTTNG_CALIBRATE;
743 copy_lttng_domain(&lsm.domain, &handle->domain);
744
745 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
746
747 return ask_sessiond(&lsm, NULL);
748 }
749
750 /*
751 * lttng_check_session_daemon
752 *
753 * Yes, return 1
754 * No, return 0
755 * Error, return negative value
756 */
757 int lttng_session_daemon_alive(void)
758 {
759 int ret;
760
761 ret = set_session_daemon_path();
762 if (ret < 0) {
763 /* Error */
764 return ret;
765 }
766
767 /* If socket exist, we check if the daemon listens to connect. */
768 ret = access(sessiond_sock_path, F_OK);
769 if (ret < 0) {
770 /* Not alive */
771 return 0;
772 }
773
774 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
775 if (ret < 0) {
776 /* Not alive */
777 return 0;
778 }
779 ret = lttcomm_close_unix_sock(ret);
780 if (ret < 0)
781 perror("lttcomm_close_unix_sock");
782
783 /* Is alive */
784 return 1;
785 }
786
787 /*
788 * lib constructor
789 */
790 static void __attribute__((constructor)) init()
791 {
792 /* Set default session group */
793 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
794 }
This page took 0.0467689999999999 seconds and 4 git commands to generate.