Add kernel sesison and channel creation support
[lttng-tools.git] / lttng / lttng.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <grp.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32
33 #include <lttng/lttng.h>
34
35 #include "lttng.h"
36 #include "lttngerr.h"
37
38 /* Variables */
39 static char *progname;
40 static char *session_name;
41 static uuid_t current_uuid;
42 static int auto_session;
43 static int auto_trace;
44
45 /* Prototypes */
46 static int process_client_opt(void);
47 static int process_opt_list_apps(void);
48 static int process_opt_list_sessions(void);
49 static int process_opt_list_traces(void);
50 static int process_opt_create_session(void);
51 static int process_kernel_create_trace(void);
52 static int process_opt_kernel_event(void);
53 static int set_session_uuid(void);
54 static void sighandler(int sig);
55 static int set_signal_handler(void);
56 static int validate_options(void);
57 static char *get_cmdline_by_pid(pid_t pid);
58 static void set_opt_session_info(void);
59
60 /*
61 * start_client
62 *
63 * Process client request from the command line
64 * options. Every tracing action is done by the
65 * liblttngctl API.
66 */
67 static int process_client_opt(void)
68 {
69 int ret;
70
71 set_opt_session_info();
72
73 if (opt_list_apps) {
74 ret = process_opt_list_apps();
75 if (ret < 0) {
76 goto end;
77 }
78 goto error;
79 }
80
81 if (opt_list_session) {
82 ret = process_opt_list_sessions();
83 if (ret < 0) {
84 goto end;
85 }
86 goto error;
87 }
88
89 /* Session creation or auto session set on */
90 if (auto_session || opt_create_session) {
91 DBG("Creating a new session");
92 ret = process_opt_create_session();
93 if (ret < 0) {
94 goto end;
95 }
96 }
97
98 ret = set_session_uuid();
99 if (ret < 0) {
100 ERR("Session %s not found", opt_session_name);
101 goto error;
102 }
103
104 if (opt_destroy_session) {
105 ret = lttng_destroy_session(&current_uuid);
106 if (ret < 0) {
107 goto end;
108 }
109 MSG("Session %s destroyed.", opt_session_name);
110 }
111
112 if (opt_list_traces) {
113 ret = process_opt_list_traces();
114 if (ret < 0) {
115 goto end;
116 }
117 }
118
119 /*
120 * Action on traces (kernel or/and userspace).
121 */
122
123 if (opt_trace_kernel) {
124 if (auto_trace || opt_create_trace) {
125 DBG("Creating a kernel trace");
126 ret = process_kernel_create_trace();
127 if (ret < 0) {
128 goto end;
129 }
130 }
131
132 if (opt_event_list != NULL) {
133 ret = process_opt_kernel_event();
134 } else {
135 // Enable all events
136 }
137
138 goto error;
139 }
140
141 if (opt_trace_pid != 0) {
142 if (auto_trace || opt_create_trace) {
143 DBG("Create a userspace trace for pid %d", opt_trace_pid);
144 ret = lttng_ust_create_trace(opt_trace_pid);
145 if (ret < 0) {
146 goto end;
147 }
148 MSG("Trace created successfully!");
149 }
150
151 if (auto_trace || opt_start_trace) {
152 DBG("Start trace for pid %d", opt_trace_pid);
153 ret = lttng_ust_start_trace(opt_trace_pid);
154 if (ret < 0) {
155 goto end;
156 }
157 MSG("Trace started successfully!");
158 } else if (opt_stop_trace) {
159 DBG("Stop trace for pid %d", opt_trace_pid);
160 ret = lttng_ust_stop_trace(opt_trace_pid);
161 if (ret < 0) {
162 goto end;
163 }
164 MSG("Trace stopped successfully!");
165 }
166
167 }
168
169 return 0;
170
171 end:
172 ERR("%s", lttng_get_readable_code(ret));
173 error: /* fall through */
174 return ret;
175 }
176
177 /*
178 * process_kernel_create_trace
179 *
180 * Create a kernel trace.
181 */
182 static int process_kernel_create_trace(void)
183 {
184 int ret;
185
186 /* Setup kernel session */
187 ret = lttng_kernel_create_session();
188 if (ret < 0) {
189 goto error;
190 }
191
192 /* Create an empty channel (with no event) */
193 ret = lttng_kernel_create_channel();
194 if (ret < 0) {
195 goto error;
196 }
197
198 return 0;
199
200 error:
201 return ret;
202 }
203
204 /*
205 * process_kernel_event
206 *
207 * Enable kernel event from the command line list given.
208 */
209 static int process_opt_kernel_event(void)
210 {
211 int ret;
212 char *event_name;
213
214 event_name = strtok(opt_event_list, ",");
215 while (event_name != NULL) {
216 DBG("Enabling kernel event %s", event_name);
217 ret = lttng_kernel_enable_event(event_name);
218 if (ret < 0) {
219 ERR("%s %s", lttng_get_readable_code(ret), event_name);
220 } else {
221 MSG("Kernel event %s enabled.", event_name);
222 }
223 /* Next event */
224 event_name = strtok(NULL, ",");
225 }
226
227 return 0;
228 }
229
230 /*
231 * set_opt_session_info
232 *
233 * Setup session_name, current_uuid, short_str_uuid and
234 * long_str_uuid using the command line options.
235 */
236 static void set_opt_session_info(void)
237 {
238 if (opt_session_name != NULL) {
239 session_name = strndup(opt_session_name, NAME_MAX);
240 DBG("Session name set to %s", session_name);
241 }
242 }
243
244 /*
245 * set_session_uuid
246 *
247 * Set current session uuid to the current flow of command(s) using the
248 * session_name.
249 */
250 static int set_session_uuid(void)
251 {
252 int ret, count, i, found = 0;
253 struct lttng_session *sessions;
254
255 if (!uuid_is_null(current_uuid)) {
256 lttng_set_current_session_uuid(&current_uuid);
257 goto end;
258 }
259
260 count = lttng_list_sessions(&sessions);
261 if (count < 0) {
262 ret = count;
263 goto error;
264 }
265
266 for (i = 0; i < count; i++) {
267 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
268 lttng_set_current_session_uuid(&sessions[i].uuid);
269 uuid_copy(current_uuid, sessions[i].uuid);
270 found = 1;
271 break;
272 }
273 }
274
275 free(sessions);
276
277 if (!found) {
278 return -1;
279 }
280
281 end:
282 DBG("Session UUID set");
283 return 0;
284
285 error:
286 return ret;
287 }
288
289 /*
290 * process_opt_list_traces
291 *
292 * Get list of all traces for a specific session uuid.
293 */
294 static int process_opt_list_traces(void)
295 {
296 int ret, i;
297 struct lttng_trace *traces;
298
299 ret = lttng_list_traces(&current_uuid, &traces);
300 DBG("Number of traces to list %d", ret);
301 if (ret < 0) {
302 goto error;
303 }
304
305 /* No traces */
306 if (ret == 0) {
307 MSG("No traces found.");
308 goto error;
309 }
310
311 MSG("Userspace traces:");
312 for (i = 0; i < ret; i++) {
313 if (traces[i].type == USERSPACE) {
314 MSG("\t%d) %s (pid: %d): %s",
315 i, traces[i].name, traces[i].pid,
316 get_cmdline_by_pid(traces[i].pid));
317 } else {
318 break;
319 }
320 }
321
322 MSG("Kernel traces:");
323 for (;i < ret; i++) {
324 if (traces[i].type == KERNEL) {
325 MSG("\t%d) %s", i, traces[i].name);
326 }
327 }
328
329 free(traces);
330
331 error:
332 return ret;
333 }
334
335 /*
336 * process_opt_create_session
337 *
338 * Create a new session using the name pass
339 * to the command line.
340 */
341 static int process_opt_create_session(void)
342 {
343 int ret;
344 char name[NAME_MAX];
345 time_t rawtime;
346 struct tm *timeinfo;
347
348 /* Auto session name creation */
349 if (opt_session_name == NULL) {
350 time(&rawtime);
351 timeinfo = localtime(&rawtime);
352 strftime(name, sizeof(name), "auto-%Y%m%d-%H%M%S", timeinfo);
353 session_name = strndup(name, sizeof(name));
354 DBG("Auto session name set to %s", session_name);
355 }
356
357 ret = lttng_create_session(session_name);
358 if (ret < 0) {
359 goto error;
360 }
361
362 MSG("Session created: %s", session_name);
363
364 error:
365 return ret;
366 }
367
368 /*
369 * process_opt_list_sessions
370 *
371 * Get the list of available sessions from
372 * the session daemon and print it to user.
373 */
374 static int process_opt_list_sessions(void)
375 {
376 int ret, count, i;
377 struct lttng_session *sessions;
378
379 count = lttng_list_sessions(&sessions);
380 DBG("Session count %d", count);
381 if (count < 0) {
382 ret = count;
383 goto error;
384 }
385
386 MSG("Available sessions (UUIDs):");
387 for (i = 0; i < count; i++) {
388 MSG(" %d) %s", i+1, sessions[i].name);
389 }
390
391 free(sessions);
392 MSG("\nTo select a session, use -s, --session UUID.");
393
394 return 0;
395
396 error:
397 return ret;
398 }
399
400 /*
401 * process_opt_list_apps
402 *
403 * Get the UST traceable pid list and print
404 * them to the user.
405 */
406 static int process_opt_list_apps(void)
407 {
408 int i, ret, count;
409 pid_t *pids;
410 char *cmdline;
411
412 count = lttng_ust_list_apps(&pids);
413 if (count < 0) {
414 ret = count;
415 goto error;
416 }
417
418 MSG("LTTng UST traceable application [name (pid)]:");
419 for (i=0; i < count; i++) {
420 cmdline = get_cmdline_by_pid(pids[i]);
421 if (cmdline == NULL) {
422 MSG("\t(not running) (%d)", pids[i]);
423 continue;
424 }
425 MSG("\t%s (%d)", cmdline, pids[i]);
426 free(cmdline);
427 }
428
429 /* Allocated by lttng_ust_list_apps() */
430 free(pids);
431
432 return 0;
433
434 error:
435 return ret;
436 }
437
438 /*
439 * get_cmdline_by_pid
440 *
441 * Get command line from /proc for a specific pid.
442 *
443 * On success, return an allocated string pointer pointing to
444 * the proc cmdline.
445 * On error, return NULL.
446 */
447 static char *get_cmdline_by_pid(pid_t pid)
448 {
449 int ret;
450 FILE *fp;
451 char *cmdline = NULL;
452 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
453
454 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
455 fp = fopen(path, "r");
456 if (fp == NULL) {
457 goto end;
458 }
459
460 /* Caller must free() *cmdline */
461 cmdline = malloc(PATH_MAX);
462 ret = fread(cmdline, 1, PATH_MAX, fp);
463 fclose(fp);
464
465 end:
466 return cmdline;
467 }
468
469 /*
470 * validate_options
471 *
472 * Make sure that all options passed to the command line are compatible with
473 * each others.
474 *
475 * On error, return -1
476 * On success, return 0
477 */
478 static int validate_options(void)
479 {
480 /* If listing options, jump validation */
481 if (opt_list_apps || opt_list_session) {
482 goto end;
483 }
484 /* Conflicting command */
485 if (opt_start_trace && opt_stop_trace) {
486 ERR("Can't use --start and --stop together.");
487 goto error;
488 /* If no PID specified and trace_kernel is off */
489 } else if ((opt_trace_pid == 0 && !opt_trace_kernel) &&
490 (opt_create_trace || opt_start_trace || opt_stop_trace || opt_destroy_trace)) {
491 ERR("Please specify for which tracer (-k or -p PID).");
492 goto error;
493 /* List traces, we need a session name */
494 } else if (opt_list_traces && opt_session_name == NULL) {
495 ERR("Can't use -t without -s, --session option.");
496 goto error;
497 /* Can't set event for both kernel and userspace at the same time */
498 } else if (opt_event_list != NULL && (opt_trace_kernel && opt_trace_pid)) {
499 ERR("Please don't use --event for both kernel and userspace.\nOne at a time to enable events.");
500 goto error;
501 /* Don't need a trace name for kernel tracig */
502 } else if (opt_trace_name != NULL && opt_trace_kernel) {
503 ERR("For action on a kernel trace, please don't specify a trace name.");
504 goto error;
505 } else if (opt_destroy_trace && opt_session_name == NULL) {
506 ERR("Please specify a session in order to destroy a trace");
507 goto error;
508 } else if (opt_create_trace || opt_destroy_trace) {
509 /* Both kernel and user-space are denied for these options */
510 if (opt_trace_pid != 0 && opt_trace_kernel) {
511 ERR("Kernel and user-space trace creation and destruction can't be used together.");
512 goto error;
513 /* Need a trace name for user-space tracing */
514 } else if (opt_trace_name == NULL && opt_trace_pid != 0) {
515 ERR("Please specify a trace name for user-space tracing");
516 goto error;
517 }
518 } else if (opt_stop_trace && opt_trace_pid != 0 && opt_trace_name == NULL) {
519 ERR("Please specify a trace name for user-space tracing");
520 goto error;
521 }
522
523 /* If start trace, auto start tracing */
524 if (opt_start_trace) {
525 DBG("Requesting auto tracing");
526 auto_trace = 1;
527 }
528
529 /* If no session, auto create one */
530 if (opt_session_name == NULL) {
531 DBG("Requesting an auto session creation");
532 auto_session = 1;
533 }
534
535 end:
536 return 0;
537
538 error:
539 return -1;
540 }
541
542 /*
543 * spawn_sessiond
544 *
545 * Spawn a session daemon by forking and execv.
546 */
547 static int spawn_sessiond(char *pathname)
548 {
549 int ret = 0;
550 pid_t pid;
551
552 MSG("Spawning session daemon");
553 pid = fork();
554 if (pid == 0) {
555 /*
556 * Spawn session daemon and tell
557 * it to signal us when ready.
558 */
559 execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
560 /* execlp only returns if error happened */
561 if (errno == ENOENT) {
562 ERR("No session daemon found. Use --sessiond-path.");
563 } else {
564 perror("execlp");
565 }
566 kill(getppid(), SIGTERM); /* unpause parent */
567 exit(EXIT_FAILURE);
568 } else if (pid > 0) {
569 /* Wait for ltt-sessiond to start */
570 pause();
571 goto end;
572 } else {
573 perror("fork");
574 ret = -1;
575 goto end;
576 }
577
578 end:
579 return ret;
580 }
581
582 /*
583 * check_ltt_sessiond
584 *
585 * Check if the session daemon is available using
586 * the liblttngctl API for the check. If not, try to
587 * spawn a daemon.
588 */
589 static int check_ltt_sessiond(void)
590 {
591 int ret;
592 char *pathname = NULL, *alloc_pathname = NULL;
593
594 ret = lttng_check_session_daemon();
595 if (ret < 0) {
596 /* Try command line option path */
597 if (opt_sessiond_path != NULL) {
598 ret = access(opt_sessiond_path, F_OK | X_OK);
599 if (ret < 0) {
600 ERR("No such file: %s", opt_sessiond_path);
601 goto end;
602 }
603 pathname = opt_sessiond_path;
604 } else {
605 /* Try LTTNG_SESSIOND_PATH env variable */
606 pathname = getenv(LTTNG_SESSIOND_PATH_ENV);
607 }
608
609 /* Let's rock and roll */
610 if (pathname == NULL) {
611 ret = asprintf(&alloc_pathname, "ltt-sessiond");
612 if (ret < 0) {
613 goto end;
614 }
615 pathname = alloc_pathname;
616 }
617
618 ret = spawn_sessiond(pathname);
619 free(alloc_pathname);
620 if (ret < 0) {
621 ERR("Problem occurs when starting %s", pathname);
622 goto end;
623 }
624 }
625
626 end:
627 return ret;
628 }
629
630 /*
631 * set_signal_handler
632 *
633 * Setup signal handler for SIGCHLD and SIGTERM.
634 */
635 static int set_signal_handler(void)
636 {
637 int ret = 0;
638 struct sigaction sa;
639 sigset_t sigset;
640
641 if ((ret = sigemptyset(&sigset)) < 0) {
642 perror("sigemptyset");
643 goto end;
644 }
645
646 sa.sa_handler = sighandler;
647 sa.sa_mask = sigset;
648 sa.sa_flags = 0;
649 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
650 perror("sigaction");
651 goto end;
652 }
653
654 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
655 perror("sigaction");
656 goto end;
657 }
658
659 end:
660 return ret;
661 }
662
663 /*
664 * sighandler
665 *
666 * Signal handler for the daemon
667 */
668 static void sighandler(int sig)
669 {
670 switch (sig) {
671 case SIGTERM:
672 DBG("SIGTERM catched");
673 clean_exit(EXIT_FAILURE);
674 break;
675 case SIGCHLD:
676 /* Notify is done */
677 DBG("SIGCHLD catched");
678 break;
679 default:
680 DBG("Unknown signal %d catched", sig);
681 break;
682 }
683
684 return;
685 }
686
687 /*
688 * clean_exit
689 */
690 void clean_exit(int code)
691 {
692 DBG("Clean exit");
693 if (session_name) {
694 free(session_name);
695 }
696
697 exit(code);
698 }
699
700 /*
701 * main
702 */
703 int main(int argc, char *argv[])
704 {
705 int ret;
706
707 progname = argv[0] ? argv[0] : "lttng";
708
709 /* For Mathieu Desnoyers aka Dr Tracing */
710 if (strncmp(progname, "drtrace", 7) == 0) {
711 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
712 }
713
714 ret = parse_args(argc, (const char **) argv);
715 if (ret < 0) {
716 clean_exit(EXIT_FAILURE);
717 }
718
719 ret = validate_options();
720 if (ret < 0) {
721 return EXIT_FAILURE;
722 }
723
724 ret = set_signal_handler();
725 if (ret < 0) {
726 clean_exit(ret);
727 }
728
729 if (opt_tracing_group != NULL) {
730 DBG("Set tracing group to '%s'", opt_tracing_group);
731 lttng_set_tracing_group(opt_tracing_group);
732 }
733
734 /* If ask for kernel tracing, need root perms */
735 if (opt_trace_kernel) {
736 DBG("Kernel tracing activated");
737 if (getuid() != 0) {
738 ERR("%s must be setuid root", progname);
739 clean_exit(-EPERM);
740 }
741 }
742
743 /* Check if the lttng session daemon is running.
744 * If no, a daemon will be spawned.
745 */
746 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
747 clean_exit(EXIT_FAILURE);
748 }
749
750 ret = process_client_opt();
751 if (ret < 0) {
752 clean_exit(ret);
753 }
754
755 clean_exit(0);
756
757 return 0;
758 }
This page took 0.044082 seconds and 5 git commands to generate.