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