Fix pointer dereference
[lttng-tools.git] / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
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.
fac6795d
DG
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
5b97ec60 33#include <lttng/lttng.h>
fac6795d
DG
34
35#include "lttng.h"
36#include "lttngerr.h"
37
38/* Variables */
39static char *progname;
96243366 40static char *session_name;
96243366 41static uuid_t current_uuid;
894be886
DG
42static int auto_session;
43static int auto_trace;
fac6795d
DG
44
45/* Prototypes */
46static int process_client_opt(void);
47static int process_opt_list_apps(void);
57167058 48static int process_opt_list_sessions(void);
1657e9bb 49static int process_opt_list_traces(void);
aaf97519 50static int process_opt_create_session(void);
894be886
DG
51static int process_kernel_create_trace(void);
52static int process_opt_kernel_event(void);
96243366 53static int set_session_uuid(void);
5b8719f5
DG
54static void sighandler(int sig);
55static int set_signal_handler(void);
8548ff30 56static int validate_options(void);
47b74d63 57static char *get_cmdline_by_pid(pid_t pid);
96243366 58static void set_opt_session_info(void);
fac6795d
DG
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;
96243366
DG
70
71 set_opt_session_info();
fac6795d 72
fac6795d
DG
73 if (opt_list_apps) {
74 ret = process_opt_list_apps();
75 if (ret < 0) {
fac6795d
DG
76 goto end;
77 }
894be886 78 goto error;
fac6795d
DG
79 }
80
57167058
DG
81 if (opt_list_session) {
82 ret = process_opt_list_sessions();
83 if (ret < 0) {
84 goto end;
85 }
894be886 86 goto error;
57167058
DG
87 }
88
894be886
DG
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();
1657e9bb
DG
93 if (ret < 0) {
94 goto end;
95 }
96 }
97
894be886
DG
98 ret = set_session_uuid();
99 if (ret < 0) {
100 ERR("Session %s not found", opt_session_name);
101 goto error;
102 }
aaf97519 103
894be886
DG
104 if (opt_destroy_session) {
105 ret = lttng_destroy_session(&current_uuid);
8028d920 106 if (ret < 0) {
894be886 107 goto end;
8028d920 108 }
894be886 109 MSG("Session %s destroyed.", opt_session_name);
8028d920
DG
110 }
111
96243366
DG
112 if (opt_list_traces) {
113 ret = process_opt_list_traces();
7442b2ba 114 if (ret < 0) {
96243366 115 goto end;
7442b2ba 116 }
e8be5f4f
DG
117 }
118
96243366
DG
119 /*
120 * Action on traces (kernel or/and userspace).
121 */
894be886 122
ad874cce 123 if (opt_trace_kernel) {
894be886
DG
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;
df0da139
DG
139 }
140
ad874cce 141 if (opt_trace_pid != 0) {
894be886 142 if (auto_trace || opt_create_trace) {
ad874cce
DG
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 }
894be886 148 MSG("Trace created successfully!");
ce3d728c 149 }
ce3d728c 150
894be886 151 if (auto_trace || opt_start_trace) {
ad874cce
DG
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!");
520ff687 165 }
ad874cce 166
520ff687
DG
167 }
168
fac6795d
DG
169 return 0;
170
171end:
ebafd2a5 172 ERR("%s", lttng_get_readable_code(ret));
5e16da05 173error: /* fall through */
7442b2ba
DG
174 return ret;
175}
176
177/*
894be886 178 * process_kernel_create_trace
96243366 179 *
894be886 180 * Create a kernel trace.
96243366 181 */
894be886 182static int process_kernel_create_trace(void)
96243366 183{
20fe2104
DG
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
894be886 198 return 0;
20fe2104
DG
199
200error:
201 return ret;
894be886 202}
96243366 203
894be886
DG
204/*
205 * process_kernel_event
206 *
207 * Enable kernel event from the command line list given.
208 */
209static int process_opt_kernel_event(void)
210{
211 int ret;
212 char *event_name;
96243366 213
894be886
DG
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);
96243366 222 }
894be886
DG
223 /* Next event */
224 event_name = strtok(NULL, ",");
96243366
DG
225 }
226
894be886 227 return 0;
96243366
DG
228}
229
230/*
894be886 231 * set_opt_session_info
96243366 232 *
894be886
DG
233 * Setup session_name, current_uuid, short_str_uuid and
234 * long_str_uuid using the command line options.
96243366 235 */
894be886 236static void set_opt_session_info(void)
96243366 237{
894be886
DG
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 }
96243366
DG
242}
243
244/*
245 * set_session_uuid
7442b2ba 246 *
894be886
DG
247 * Set current session uuid to the current flow of command(s) using the
248 * session_name.
7442b2ba 249 */
96243366 250static int set_session_uuid(void)
7442b2ba 251{
894be886 252 int ret, count, i, found = 0;
7442b2ba
DG
253 struct lttng_session *sessions;
254
96243366
DG
255 if (!uuid_is_null(current_uuid)) {
256 lttng_set_current_session_uuid(&current_uuid);
257 goto end;
258 }
259
7442b2ba
DG
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++) {
894be886 267 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
96243366 268 lttng_set_current_session_uuid(&sessions[i].uuid);
894be886
DG
269 uuid_copy(current_uuid, sessions[i].uuid);
270 found = 1;
7442b2ba
DG
271 break;
272 }
273 }
274
275 free(sessions);
276
894be886
DG
277 if (!found) {
278 return -1;
279 }
280
96243366 281end:
894be886 282 DBG("Session UUID set");
7442b2ba
DG
283 return 0;
284
285error:
286 return ret;
fac6795d
DG
287}
288
1657e9bb
DG
289/*
290 * process_opt_list_traces
291 *
292 * Get list of all traces for a specific session uuid.
293 */
294static int process_opt_list_traces(void)
295{
296 int ret, i;
1657e9bb
DG
297 struct lttng_trace *traces;
298
96243366 299 ret = lttng_list_traces(&current_uuid, &traces);
894be886 300 DBG("Number of traces to list %d", ret);
1657e9bb
DG
301 if (ret < 0) {
302 goto error;
303 }
304
894be886
DG
305 /* No traces */
306 if (ret == 0) {
307 MSG("No traces found.");
308 goto error;
309 }
310
1657e9bb
DG
311 MSG("Userspace traces:");
312 for (i = 0; i < ret; i++) {
313 if (traces[i].type == USERSPACE) {
47b74d63
DG
314 MSG("\t%d) %s (pid: %d): %s",
315 i, traces[i].name, traces[i].pid,
316 get_cmdline_by_pid(traces[i].pid));
1657e9bb
DG
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
331error:
332 return ret;
333}
334
aaf97519
DG
335/*
336 * process_opt_create_session
337 *
338 * Create a new session using the name pass
339 * to the command line.
340 */
341static int process_opt_create_session(void)
342{
343 int ret;
96243366
DG
344 char name[NAME_MAX];
345 time_t rawtime;
346 struct tm *timeinfo;
347
894be886
DG
348 /* Auto session name creation */
349 if (opt_session_name == NULL) {
96243366
DG
350 time(&rawtime);
351 timeinfo = localtime(&rawtime);
894be886 352 strftime(name, sizeof(name), "auto-%Y%m%d-%H%M%S", timeinfo);
96243366 353 session_name = strndup(name, sizeof(name));
894be886 354 DBG("Auto session name set to %s", session_name);
96243366 355 }
aaf97519 356
894be886 357 ret = lttng_create_session(session_name);
aaf97519
DG
358 if (ret < 0) {
359 goto error;
360 }
361
894be886 362 MSG("Session created: %s", session_name);
aaf97519
DG
363
364error:
365 return ret;
366}
367
57167058
DG
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 */
374static int process_opt_list_sessions(void)
375{
376 int ret, count, i;
894be886 377 struct lttng_session *sessions;
57167058 378
894be886 379 count = lttng_list_sessions(&sessions);
7442b2ba 380 DBG("Session count %d", count);
57167058
DG
381 if (count < 0) {
382 ret = count;
383 goto error;
384 }
385
7442b2ba 386 MSG("Available sessions (UUIDs):");
57167058 387 for (i = 0; i < count; i++) {
894be886 388 MSG(" %d) %s", i+1, sessions[i].name);
57167058
DG
389 }
390
894be886 391 free(sessions);
7442b2ba 392 MSG("\nTo select a session, use -s, --session UUID.");
57167058
DG
393
394 return 0;
395
396error:
397 return ret;
398}
399
fac6795d
DG
400/*
401 * process_opt_list_apps
402 *
403 * Get the UST traceable pid list and print
404 * them to the user.
405 */
406static int process_opt_list_apps(void)
407{
e8f07c63 408 int i, ret, count;
fac6795d 409 pid_t *pids;
1c9f7941 410 char *cmdline;
fac6795d 411
e8f07c63
DG
412 count = lttng_ust_list_apps(&pids);
413 if (count < 0) {
414 ret = count;
fac6795d
DG
415 goto error;
416 }
417
418 MSG("LTTng UST traceable application [name (pid)]:");
e8f07c63 419 for (i=0; i < count; i++) {
47b74d63
DG
420 cmdline = get_cmdline_by_pid(pids[i]);
421 if (cmdline == NULL) {
e8f07c63 422 MSG("\t(not running) (%d)", pids[i]);
fac6795d
DG
423 continue;
424 }
fac6795d 425 MSG("\t%s (%d)", cmdline, pids[i]);
1c9f7941 426 free(cmdline);
fac6795d
DG
427 }
428
e065084a
DG
429 /* Allocated by lttng_ust_list_apps() */
430 free(pids);
431
fac6795d
DG
432 return 0;
433
434error:
435 return ret;
436}
437
1c9f7941
DG
438/*
439 * get_cmdline_by_pid
440 *
47b74d63 441 * Get command line from /proc for a specific pid.
1c9f7941 442 *
47b74d63
DG
443 * On success, return an allocated string pointer pointing to
444 * the proc cmdline.
445 * On error, return NULL.
1c9f7941 446 */
47b74d63 447static char *get_cmdline_by_pid(pid_t pid)
1c9f7941
DG
448{
449 int ret;
450 FILE *fp;
47b74d63 451 char *cmdline = NULL;
1c9f7941
DG
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) {
47b74d63 457 goto end;
1c9f7941
DG
458 }
459
460 /* Caller must free() *cmdline */
47b74d63
DG
461 cmdline = malloc(PATH_MAX);
462 ret = fread(cmdline, 1, PATH_MAX, fp);
1c9f7941
DG
463 fclose(fp);
464
47b74d63
DG
465end:
466 return cmdline;
1c9f7941
DG
467}
468
8548ff30
DG
469/*
470 * validate_options
471 *
894be886
DG
472 * Make sure that all options passed to the command line are compatible with
473 * each others.
8548ff30
DG
474 *
475 * On error, return -1
476 * On success, return 0
477 */
478static int validate_options(void)
479{
894be886
DG
480 /* If listing options, jump validation */
481 if (opt_list_apps || opt_list_session) {
482 goto end;
483 }
ad874cce
DG
484 /* Conflicting command */
485 if (opt_start_trace && opt_stop_trace) {
486 ERR("Can't use --start and --stop together.");
487 goto error;
ad874cce 488 /* If no PID specified and trace_kernel is off */
894be886
DG
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).");
96243366 492 goto error;
894be886
DG
493 /* List traces, we need a session name */
494 } else if (opt_list_traces && opt_session_name == NULL) {
96243366
DG
495 ERR("Can't use -t without -s, --session option.");
496 goto error;
894be886
DG
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;
7442b2ba
DG
521 }
522
894be886
DG
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
535end:
8548ff30
DG
536 return 0;
537
538error:
539 return -1;
540}
541
5b8719f5
DG
542/*
543 * spawn_sessiond
544 *
545 * Spawn a session daemon by forking and execv.
546 */
547static 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) {
5e16da05
MD
555 /*
556 * Spawn session daemon and tell
5b8719f5
DG
557 * it to signal us when ready.
558 */
5e16da05
MD
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");
5b8719f5 565 }
5e16da05
MD
566 kill(getppid(), SIGTERM); /* unpause parent */
567 exit(EXIT_FAILURE);
5b8719f5
DG
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
578end:
579 return ret;
580}
581
fac6795d
DG
582/*
583 * check_ltt_sessiond
584 *
585 * Check if the session daemon is available using
5b8719f5
DG
586 * the liblttngctl API for the check. If not, try to
587 * spawn a daemon.
fac6795d
DG
588 */
589static int check_ltt_sessiond(void)
590{
591 int ret;
5e16da05 592 char *pathname = NULL, *alloc_pathname = NULL;
fac6795d
DG
593
594 ret = lttng_check_session_daemon();
595 if (ret < 0) {
5b8719f5
DG
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 */
e8f07c63 606 pathname = getenv(LTTNG_SESSIOND_PATH_ENV);
5b8719f5
DG
607 }
608
609 /* Let's rock and roll */
610 if (pathname == NULL) {
5e16da05 611 ret = asprintf(&alloc_pathname, "ltt-sessiond");
5b8719f5
DG
612 if (ret < 0) {
613 goto end;
614 }
5e16da05 615 pathname = alloc_pathname;
5b8719f5
DG
616 }
617
618 ret = spawn_sessiond(pathname);
5e16da05 619 free(alloc_pathname);
5b8719f5
DG
620 if (ret < 0) {
621 ERR("Problem occurs when starting %s", pathname);
622 goto end;
623 }
fac6795d
DG
624 }
625
5b8719f5 626end:
fac6795d
DG
627 return ret;
628}
629
5b8719f5
DG
630/*
631 * set_signal_handler
632 *
633 * Setup signal handler for SIGCHLD and SIGTERM.
634 */
635static 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 }
fac6795d 653
5b8719f5
DG
654 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
655 perror("sigaction");
656 goto end;
657 }
658
659end:
660 return ret;
661}
662
663/*
664 * sighandler
665 *
666 * Signal handler for the daemon
667 */
668static void sighandler(int sig)
669{
5b8719f5
DG
670 switch (sig) {
671 case SIGTERM:
ce3d728c 672 DBG("SIGTERM catched");
5b8719f5
DG
673 clean_exit(EXIT_FAILURE);
674 break;
675 case SIGCHLD:
676 /* Notify is done */
ce3d728c 677 DBG("SIGCHLD catched");
5b8719f5
DG
678 break;
679 default:
ce3d728c 680 DBG("Unknown signal %d catched", sig);
5b8719f5
DG
681 break;
682 }
683
684 return;
685}
7442b2ba 686
fac6795d
DG
687/*
688 * clean_exit
689 */
690void clean_exit(int code)
691{
692 DBG("Clean exit");
96243366
DG
693 if (session_name) {
694 free(session_name);
695 }
696
fac6795d
DG
697 exit(code);
698}
699
700/*
5b8719f5 701 * main
fac6795d
DG
702 */
703int 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) {
87378cf5 716 clean_exit(EXIT_FAILURE);
fac6795d
DG
717 }
718
8548ff30
DG
719 ret = validate_options();
720 if (ret < 0) {
721 return EXIT_FAILURE;
722 }
723
5b8719f5
DG
724 ret = set_signal_handler();
725 if (ret < 0) {
87378cf5 726 clean_exit(ret);
5b8719f5
DG
727 }
728
fac6795d
DG
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);
87378cf5 739 clean_exit(-EPERM);
fac6795d
DG
740 }
741 }
742
743 /* Check if the lttng session daemon is running.
744 * If no, a daemon will be spawned.
745 */
5b8719f5 746 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
87378cf5 747 clean_exit(EXIT_FAILURE);
fac6795d
DG
748 }
749
750 ret = process_client_opt();
751 if (ret < 0) {
87378cf5 752 clean_exit(ret);
fac6795d
DG
753 }
754
87378cf5
DG
755 clean_exit(0);
756
fac6795d
DG
757 return 0;
758}
This page took 0.05676 seconds and 5 git commands to generate.