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