b523960c33063be98e5bdf35fc9359fcdf76835f
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
29 #include <sys/types.h>
33 #include <lttng/lttng.h>
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
;
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_kernel_list_events(void);
51 static int process_opt_create_session(void);
52 static int process_kernel_create_trace(void);
53 static int process_opt_kernel_event(void);
54 static int process_kernel_start_trace(void);
55 static int set_session_uuid(void);
56 static void sighandler(int sig
);
57 static int set_signal_handler(void);
58 static int validate_options(void);
59 static char *get_cmdline_by_pid(pid_t pid
);
60 static void set_opt_session_info(void);
65 * Process client request from the command line
66 * options. Every tracing action is done by the
69 static int process_client_opt(void)
73 set_opt_session_info();
76 ret
= process_opt_list_apps();
83 if (opt_list_session
) {
84 ret
= process_opt_list_sessions();
91 if (opt_list_events
) {
92 if (opt_trace_kernel
) {
93 ret
= process_opt_kernel_list_events();
97 } else if (opt_trace_pid
!= 0) {
103 /* Session creation or auto session set on */
104 if (auto_session
|| opt_create_session
) {
105 DBG("Creating a new session");
106 ret
= process_opt_create_session();
112 ret
= set_session_uuid();
114 ERR("Session %s not found", opt_session_name
);
118 if (opt_destroy_session
) {
119 ret
= lttng_destroy_session(¤t_uuid
);
123 MSG("Session %s destroyed.", opt_session_name
);
126 if (opt_list_traces
) {
127 ret
= process_opt_list_traces();
134 * Action on traces (kernel or/and userspace).
137 if (opt_trace_kernel
) {
138 if (auto_trace
|| opt_create_trace
) {
139 DBG("Creating a kernel trace");
140 ret
= process_kernel_create_trace();
146 if (opt_event_list
!= NULL
|| opt_enable_all_event
) {
147 ret
= process_opt_kernel_event();
153 if (auto_trace
|| opt_start_trace
) {
154 DBG("Starting kernel tracing");
155 ret
= process_kernel_start_trace();
161 if (opt_stop_trace
) {
162 DBG("Stopping kernel tracing");
163 ret
= lttng_kernel_stop_tracing();
170 if (opt_trace_pid
!= 0) {
171 if (auto_trace
|| opt_create_trace
) {
172 DBG("Create a userspace trace for pid %d", opt_trace_pid
);
173 ret
= lttng_ust_create_trace(opt_trace_pid
);
177 MSG("Trace created successfully!");
180 if (auto_trace
|| opt_start_trace
) {
181 DBG("Start trace for pid %d", opt_trace_pid
);
182 ret
= lttng_ust_start_trace(opt_trace_pid
);
186 MSG("Trace started successfully!");
187 } else if (opt_stop_trace
) {
188 DBG("Stop trace for pid %d", opt_trace_pid
);
189 ret
= lttng_ust_stop_trace(opt_trace_pid
);
193 MSG("Trace stopped successfully!");
201 ERR("%s", lttng_get_readable_code(ret
));
202 error
: /* fall through */
207 * process_kernel_start_trace
209 * Start a kernel trace.
211 static int process_kernel_start_trace(void)
215 ret
= lttng_kernel_create_stream();
220 ret
= lttng_kernel_start_tracing();
225 MSG("Kernel tracing started");
234 * process_kernel_create_trace
236 * Create a kernel trace.
238 static int process_kernel_create_trace(void)
242 /* Setup kernel session */
243 ret
= lttng_kernel_create_session();
248 /* Create an empty channel (with no event) */
249 ret
= lttng_kernel_create_channel();
254 /* Opening metadata for session */
255 ret
= lttng_kernel_open_metadata();
267 * process_opt_kernel_list_events
269 * Ask for all trace events in the kernel and pretty print them.
271 static int process_opt_kernel_list_events(void)
274 char *event_list
, *event
, *ptr
;
276 DBG("Getting all tracing events");
278 ret
= lttng_kernel_list_events(&event_list
);
280 ERR("Unable to list events.");
284 MSG("Kernel tracepoints:\n-------------");
287 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
289 /* Move pointer to the next line */
300 * process_opt_kernel_event
302 * Enable kernel event from the command line list given.
304 static int process_opt_kernel_event(void)
309 if (opt_enable_all_event
) {
310 ret
= lttng_kernel_enable_event(NULL
);
312 ERR("%s", lttng_get_readable_code(ret
));
314 MSG("All kernel event enabled");
320 event_name
= strtok(opt_event_list
, ",");
321 while (event_name
!= NULL
) {
322 DBG("Enabling kernel event %s", event_name
);
323 ret
= lttng_kernel_enable_event(event_name
);
325 ERR("%s %s", lttng_get_readable_code(ret
), event_name
);
327 MSG("Kernel event %s enabled.", event_name
);
330 event_name
= strtok(NULL
, ",");
338 * set_opt_session_info
340 * Setup session_name, current_uuid, short_str_uuid and
341 * long_str_uuid using the command line options.
343 static void set_opt_session_info(void)
345 if (opt_session_name
!= NULL
) {
346 session_name
= strndup(opt_session_name
, NAME_MAX
);
347 DBG("Session name set to %s", session_name
);
354 * Set current session uuid to the current flow of command(s) using the
357 static int set_session_uuid(void)
359 int ret
, count
, i
, found
= 0;
360 struct lttng_session
*sessions
;
362 if (!uuid_is_null(current_uuid
)) {
363 lttng_set_current_session_uuid(¤t_uuid
);
367 count
= lttng_list_sessions(&sessions
);
373 for (i
= 0; i
< count
; i
++) {
374 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
375 lttng_set_current_session_uuid(&sessions
[i
].uuid
);
376 uuid_copy(current_uuid
, sessions
[i
].uuid
);
389 DBG("Session UUID set");
397 * process_opt_list_traces
399 * Get list of all traces for a specific session uuid.
401 static int process_opt_list_traces(void)
404 struct lttng_trace
*traces
;
406 ret
= lttng_list_traces(¤t_uuid
, &traces
);
407 DBG("Number of traces to list %d", ret
);
414 MSG("No traces found.");
418 MSG("Userspace traces:");
419 for (i
= 0; i
< ret
; i
++) {
420 if (traces
[i
].type
== USERSPACE
) {
421 MSG("\t%d) %s (pid: %d): %s",
422 i
, traces
[i
].name
, traces
[i
].pid
,
423 get_cmdline_by_pid(traces
[i
].pid
));
429 MSG("Kernel traces:");
430 for (;i
< ret
; i
++) {
431 if (traces
[i
].type
== KERNEL
) {
432 MSG("\t%d) %s", i
, traces
[i
].name
);
443 * process_opt_create_session
445 * Create a new session using the name pass
446 * to the command line.
448 static int process_opt_create_session(void)
455 /* Auto session name creation */
456 if (opt_session_name
== NULL
) {
458 timeinfo
= localtime(&rawtime
);
459 strftime(name
, sizeof(name
), "auto-%Y%m%d-%H%M%S", timeinfo
);
460 session_name
= strndup(name
, sizeof(name
));
461 DBG("Auto session name set to %s", session_name
);
464 ret
= lttng_create_session(session_name
);
469 MSG("Session created: %s", session_name
);
476 * process_opt_list_sessions
478 * Get the list of available sessions from
479 * the session daemon and print it to user.
481 static int process_opt_list_sessions(void)
484 struct lttng_session
*sessions
;
486 count
= lttng_list_sessions(&sessions
);
487 DBG("Session count %d", count
);
493 MSG("Available sessions (UUIDs):");
494 for (i
= 0; i
< count
; i
++) {
495 MSG(" %d) %s", i
+1, sessions
[i
].name
);
499 MSG("\nTo select a session, use -s, --session UUID.");
508 * process_opt_list_apps
510 * Get the UST traceable pid list and print
513 static int process_opt_list_apps(void)
519 count
= lttng_ust_list_apps(&pids
);
525 MSG("LTTng UST traceable application [name (pid)]:");
526 for (i
=0; i
< count
; i
++) {
527 cmdline
= get_cmdline_by_pid(pids
[i
]);
528 if (cmdline
== NULL
) {
529 MSG("\t(not running) (%d)", pids
[i
]);
532 MSG("\t%s (%d)", cmdline
, pids
[i
]);
536 /* Allocated by lttng_ust_list_apps() */
548 * Get command line from /proc for a specific pid.
550 * On success, return an allocated string pointer pointing to
552 * On error, return NULL.
554 static char *get_cmdline_by_pid(pid_t pid
)
558 char *cmdline
= NULL
;
559 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
561 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
562 fp
= fopen(path
, "r");
567 /* Caller must free() *cmdline */
568 cmdline
= malloc(PATH_MAX
);
569 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
579 * Make sure that all options passed to the command line are compatible with
582 * On error, return -1
583 * On success, return 0
585 static int validate_options(void)
587 /* If listing options, jump validation */
588 if (opt_list_apps
|| opt_list_session
) {
591 /* Conflicting command */
592 if (opt_start_trace
&& opt_stop_trace
) {
593 ERR("Can't use --start and --stop together.");
595 /* If no PID specified and trace_kernel is off */
596 } else if ((opt_trace_pid
== 0 && !opt_trace_kernel
) &&
597 (opt_create_trace
|| opt_start_trace
|| opt_stop_trace
|| opt_destroy_trace
)) {
598 ERR("Please specify for which tracer (-k or -p PID).");
600 /* List traces, we need a session name */
601 } else if (opt_list_traces
&& opt_session_name
== NULL
) {
602 ERR("Can't use -t without -s, --session option.");
604 /* Can't set event for both kernel and userspace at the same time */
605 } else if (opt_event_list
!= NULL
&& (opt_trace_kernel
&& opt_trace_pid
)) {
606 ERR("Please don't use --event for both kernel and userspace.\nOne at a time to enable events.");
608 /* Don't need a trace name for kernel tracig */
609 } else if (opt_trace_name
!= NULL
&& opt_trace_kernel
) {
610 ERR("For action on a kernel trace, please don't specify a trace name.");
612 } else if (opt_destroy_trace
&& opt_session_name
== NULL
) {
613 ERR("Please specify a session in order to destroy a trace");
615 } else if (opt_create_trace
|| opt_destroy_trace
) {
616 /* Both kernel and user-space are denied for these options */
617 if (opt_trace_pid
!= 0 && opt_trace_kernel
) {
618 ERR("Kernel and user-space trace creation and destruction can't be used together.");
620 /* Need a trace name for user-space tracing */
621 } else if (opt_trace_name
== NULL
&& opt_trace_pid
!= 0) {
622 ERR("Please specify a trace name for user-space tracing");
625 } else if (opt_stop_trace
&& opt_trace_pid
!= 0 && opt_trace_name
== NULL
) {
626 ERR("Please specify a trace name for user-space tracing");
628 } else if (opt_stop_trace
&& opt_session_name
== NULL
) {
629 ERR("Please specify a session to stop tracing");
633 /* If start trace, auto start tracing */
634 if (opt_start_trace
|| opt_event_list
!= NULL
|| opt_enable_all_event
) {
635 DBG("Requesting auto tracing");
639 /* If no session, auto create one */
640 if (opt_session_name
== NULL
) {
641 DBG("Requesting an auto session creation");
655 * Spawn a session daemon by forking and execv.
657 static int spawn_sessiond(char *pathname
)
662 MSG("Spawning session daemon");
666 * Spawn session daemon and tell
667 * it to signal us when ready.
669 execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
670 /* execlp only returns if error happened */
671 if (errno
== ENOENT
) {
672 ERR("No session daemon found. Use --sessiond-path.");
676 kill(getppid(), SIGTERM
); /* unpause parent */
678 } else if (pid
> 0) {
679 /* Wait for ltt-sessiond to start */
695 * Check if the session daemon is available using
696 * the liblttngctl API for the check. If not, try to
699 static int check_ltt_sessiond(void)
702 char *pathname
= NULL
, *alloc_pathname
= NULL
;
704 ret
= lttng_session_daemon_alive();
705 if (ret
== 0) { /* not alive */
706 /* Try command line option path */
707 if (opt_sessiond_path
!= NULL
) {
708 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
710 ERR("No such file: %s", opt_sessiond_path
);
713 pathname
= opt_sessiond_path
;
715 /* Try LTTNG_SESSIOND_PATH env variable */
716 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
719 /* Let's rock and roll */
720 if (pathname
== NULL
) {
721 ret
= asprintf(&alloc_pathname
, "ltt-sessiond");
725 pathname
= alloc_pathname
;
728 ret
= spawn_sessiond(pathname
);
729 free(alloc_pathname
);
731 ERR("Problem occurs when starting %s", pathname
);
743 * Setup signal handler for SIGCHLD and SIGTERM.
745 static int set_signal_handler(void)
751 if ((ret
= sigemptyset(&sigset
)) < 0) {
752 perror("sigemptyset");
756 sa
.sa_handler
= sighandler
;
759 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
764 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
776 * Signal handler for the daemon
778 static void sighandler(int sig
)
782 DBG("SIGTERM catched");
783 clean_exit(EXIT_FAILURE
);
787 DBG("SIGCHLD catched");
790 DBG("Unknown signal %d catched", sig
);
800 void clean_exit(int code
)
813 int main(int argc
, char *argv
[])
817 progname
= argv
[0] ? argv
[0] : "lttng";
819 /* For Mathieu Desnoyers aka Dr Tracing */
820 if (strncmp(progname
, "drtrace", 7) == 0) {
821 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
824 ret
= parse_args(argc
, (const char **) argv
);
826 clean_exit(EXIT_FAILURE
);
829 ret
= validate_options();
834 ret
= set_signal_handler();
839 if (opt_tracing_group
!= NULL
) {
840 DBG("Set tracing group to '%s'", opt_tracing_group
);
841 lttng_set_tracing_group(opt_tracing_group
);
844 /* If ask for kernel tracing, need root perms */
845 if (opt_trace_kernel
) {
846 DBG("Kernel tracing activated");
848 ERR("%s must be setuid root", progname
);
853 /* Check if the lttng session daemon is running.
854 * If no, a daemon will be spawned.
856 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
857 clean_exit(EXIT_FAILURE
);
860 ret
= process_client_opt();
This page took 0.04976 seconds and 5 git commands to generate.