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