9c34d2c62377ab66c02c9d4b766f01b6cff57eca
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
) {
147 ret
= process_opt_kernel_event();
155 if (auto_trace
|| opt_start_trace
) {
156 DBG("Starting kernel tracing");
157 ret
= process_kernel_start_trace();
163 if (opt_stop_trace
) {
164 DBG("Stopping kernel tracing");
165 ret
= lttng_kernel_stop_tracing();
172 if (opt_trace_pid
!= 0) {
173 if (auto_trace
|| opt_create_trace
) {
174 DBG("Create a userspace trace for pid %d", opt_trace_pid
);
175 ret
= lttng_ust_create_trace(opt_trace_pid
);
179 MSG("Trace created successfully!");
182 if (auto_trace
|| opt_start_trace
) {
183 DBG("Start trace for pid %d", opt_trace_pid
);
184 ret
= lttng_ust_start_trace(opt_trace_pid
);
188 MSG("Trace started successfully!");
189 } else if (opt_stop_trace
) {
190 DBG("Stop trace for pid %d", opt_trace_pid
);
191 ret
= lttng_ust_stop_trace(opt_trace_pid
);
195 MSG("Trace stopped successfully!");
203 ERR("%s", lttng_get_readable_code(ret
));
204 error
: /* fall through */
209 * process_kernel_start_trace
211 * Start a kernel trace.
213 static int process_kernel_start_trace(void)
217 ret
= lttng_kernel_create_stream();
222 ret
= lttng_kernel_start_tracing();
227 MSG("Kernel tracing started");
236 * process_kernel_create_trace
238 * Create a kernel trace.
240 static int process_kernel_create_trace(void)
244 /* Setup kernel session */
245 ret
= lttng_kernel_create_session();
250 /* Create an empty channel (with no event) */
251 ret
= lttng_kernel_create_channel();
256 /* Opening metadata for session */
257 ret
= lttng_kernel_open_metadata();
269 * process_opt_kernel_list_events
271 * Ask for all trace events in the kernel and pretty print them.
273 static int process_opt_kernel_list_events(void)
276 char *event_list
, *event
, *ptr
;
278 DBG("Getting all tracing events");
280 ret
= lttng_kernel_list_events(&event_list
);
282 ERR("Unable to list events.");
286 MSG("Kernel tracepoints:\n-------------");
289 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
291 /* Move pointer to the next line */
302 * process_opt_kernel_event
304 * Enable kernel event from the command line list given.
306 static int process_opt_kernel_event(void)
311 event_name
= strtok(opt_event_list
, ",");
312 while (event_name
!= NULL
) {
313 DBG("Enabling kernel event %s", event_name
);
314 ret
= lttng_kernel_enable_event(event_name
);
316 ERR("%s %s", lttng_get_readable_code(ret
), event_name
);
318 MSG("Kernel event %s enabled.", event_name
);
321 event_name
= strtok(NULL
, ",");
328 * set_opt_session_info
330 * Setup session_name, current_uuid, short_str_uuid and
331 * long_str_uuid using the command line options.
333 static void set_opt_session_info(void)
335 if (opt_session_name
!= NULL
) {
336 session_name
= strndup(opt_session_name
, NAME_MAX
);
337 DBG("Session name set to %s", session_name
);
344 * Set current session uuid to the current flow of command(s) using the
347 static int set_session_uuid(void)
349 int ret
, count
, i
, found
= 0;
350 struct lttng_session
*sessions
;
352 if (!uuid_is_null(current_uuid
)) {
353 lttng_set_current_session_uuid(¤t_uuid
);
357 count
= lttng_list_sessions(&sessions
);
363 for (i
= 0; i
< count
; i
++) {
364 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
365 lttng_set_current_session_uuid(&sessions
[i
].uuid
);
366 uuid_copy(current_uuid
, sessions
[i
].uuid
);
379 DBG("Session UUID set");
387 * process_opt_list_traces
389 * Get list of all traces for a specific session uuid.
391 static int process_opt_list_traces(void)
394 struct lttng_trace
*traces
;
396 ret
= lttng_list_traces(¤t_uuid
, &traces
);
397 DBG("Number of traces to list %d", ret
);
404 MSG("No traces found.");
408 MSG("Userspace traces:");
409 for (i
= 0; i
< ret
; i
++) {
410 if (traces
[i
].type
== USERSPACE
) {
411 MSG("\t%d) %s (pid: %d): %s",
412 i
, traces
[i
].name
, traces
[i
].pid
,
413 get_cmdline_by_pid(traces
[i
].pid
));
419 MSG("Kernel traces:");
420 for (;i
< ret
; i
++) {
421 if (traces
[i
].type
== KERNEL
) {
422 MSG("\t%d) %s", i
, traces
[i
].name
);
433 * process_opt_create_session
435 * Create a new session using the name pass
436 * to the command line.
438 static int process_opt_create_session(void)
445 /* Auto session name creation */
446 if (opt_session_name
== NULL
) {
448 timeinfo
= localtime(&rawtime
);
449 strftime(name
, sizeof(name
), "auto-%Y%m%d-%H%M%S", timeinfo
);
450 session_name
= strndup(name
, sizeof(name
));
451 DBG("Auto session name set to %s", session_name
);
454 ret
= lttng_create_session(session_name
);
459 MSG("Session created: %s", session_name
);
466 * process_opt_list_sessions
468 * Get the list of available sessions from
469 * the session daemon and print it to user.
471 static int process_opt_list_sessions(void)
474 struct lttng_session
*sessions
;
476 count
= lttng_list_sessions(&sessions
);
477 DBG("Session count %d", count
);
483 MSG("Available sessions (UUIDs):");
484 for (i
= 0; i
< count
; i
++) {
485 MSG(" %d) %s", i
+1, sessions
[i
].name
);
489 MSG("\nTo select a session, use -s, --session UUID.");
498 * process_opt_list_apps
500 * Get the UST traceable pid list and print
503 static int process_opt_list_apps(void)
509 count
= lttng_ust_list_apps(&pids
);
515 MSG("LTTng UST traceable application [name (pid)]:");
516 for (i
=0; i
< count
; i
++) {
517 cmdline
= get_cmdline_by_pid(pids
[i
]);
518 if (cmdline
== NULL
) {
519 MSG("\t(not running) (%d)", pids
[i
]);
522 MSG("\t%s (%d)", cmdline
, pids
[i
]);
526 /* Allocated by lttng_ust_list_apps() */
538 * Get command line from /proc for a specific pid.
540 * On success, return an allocated string pointer pointing to
542 * On error, return NULL.
544 static char *get_cmdline_by_pid(pid_t pid
)
548 char *cmdline
= NULL
;
549 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
551 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
552 fp
= fopen(path
, "r");
557 /* Caller must free() *cmdline */
558 cmdline
= malloc(PATH_MAX
);
559 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
569 * Make sure that all options passed to the command line are compatible with
572 * On error, return -1
573 * On success, return 0
575 static int validate_options(void)
577 /* If listing options, jump validation */
578 if (opt_list_apps
|| opt_list_session
) {
581 /* Conflicting command */
582 if (opt_start_trace
&& opt_stop_trace
) {
583 ERR("Can't use --start and --stop together.");
585 /* If no PID specified and trace_kernel is off */
586 } else if ((opt_trace_pid
== 0 && !opt_trace_kernel
) &&
587 (opt_create_trace
|| opt_start_trace
|| opt_stop_trace
|| opt_destroy_trace
)) {
588 ERR("Please specify for which tracer (-k or -p PID).");
590 /* List traces, we need a session name */
591 } else if (opt_list_traces
&& opt_session_name
== NULL
) {
592 ERR("Can't use -t without -s, --session option.");
594 /* Can't set event for both kernel and userspace at the same time */
595 } else if (opt_event_list
!= NULL
&& (opt_trace_kernel
&& opt_trace_pid
)) {
596 ERR("Please don't use --event for both kernel and userspace.\nOne at a time to enable events.");
598 /* Don't need a trace name for kernel tracig */
599 } else if (opt_trace_name
!= NULL
&& opt_trace_kernel
) {
600 ERR("For action on a kernel trace, please don't specify a trace name.");
602 } else if (opt_destroy_trace
&& opt_session_name
== NULL
) {
603 ERR("Please specify a session in order to destroy a trace");
605 } else if (opt_create_trace
|| opt_destroy_trace
) {
606 /* Both kernel and user-space are denied for these options */
607 if (opt_trace_pid
!= 0 && opt_trace_kernel
) {
608 ERR("Kernel and user-space trace creation and destruction can't be used together.");
610 /* Need a trace name for user-space tracing */
611 } else if (opt_trace_name
== NULL
&& opt_trace_pid
!= 0) {
612 ERR("Please specify a trace name for user-space tracing");
615 } else if (opt_stop_trace
&& opt_trace_pid
!= 0 && opt_trace_name
== NULL
) {
616 ERR("Please specify a trace name for user-space tracing");
618 } else if (opt_stop_trace
&& opt_session_name
== NULL
) {
619 ERR("Please specify a session to stop tracing");
623 /* If start trace, auto start tracing */
624 if (opt_start_trace
|| opt_event_list
!= NULL
) {
625 DBG("Requesting auto tracing");
629 /* If no session, auto create one */
630 if (opt_session_name
== NULL
) {
631 DBG("Requesting an auto session creation");
645 * Spawn a session daemon by forking and execv.
647 static int spawn_sessiond(char *pathname
)
652 MSG("Spawning session daemon");
656 * Spawn session daemon and tell
657 * it to signal us when ready.
659 execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
660 /* execlp only returns if error happened */
661 if (errno
== ENOENT
) {
662 ERR("No session daemon found. Use --sessiond-path.");
666 kill(getppid(), SIGTERM
); /* unpause parent */
668 } else if (pid
> 0) {
669 /* Wait for ltt-sessiond to start */
685 * Check if the session daemon is available using
686 * the liblttngctl API for the check. If not, try to
689 static int check_ltt_sessiond(void)
692 char *pathname
= NULL
, *alloc_pathname
= NULL
;
694 ret
= lttng_session_daemon_alive();
695 if (ret
== 0) { /* not alive */
696 /* Try command line option path */
697 if (opt_sessiond_path
!= NULL
) {
698 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
700 ERR("No such file: %s", opt_sessiond_path
);
703 pathname
= opt_sessiond_path
;
705 /* Try LTTNG_SESSIOND_PATH env variable */
706 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
709 /* Let's rock and roll */
710 if (pathname
== NULL
) {
711 ret
= asprintf(&alloc_pathname
, "ltt-sessiond");
715 pathname
= alloc_pathname
;
718 ret
= spawn_sessiond(pathname
);
719 free(alloc_pathname
);
721 ERR("Problem occurs when starting %s", pathname
);
733 * Setup signal handler for SIGCHLD and SIGTERM.
735 static int set_signal_handler(void)
741 if ((ret
= sigemptyset(&sigset
)) < 0) {
742 perror("sigemptyset");
746 sa
.sa_handler
= sighandler
;
749 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
754 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
766 * Signal handler for the daemon
768 static void sighandler(int sig
)
772 DBG("SIGTERM catched");
773 clean_exit(EXIT_FAILURE
);
777 DBG("SIGCHLD catched");
780 DBG("Unknown signal %d catched", sig
);
790 void clean_exit(int code
)
803 int main(int argc
, char *argv
[])
807 progname
= argv
[0] ? argv
[0] : "lttng";
809 /* For Mathieu Desnoyers aka Dr Tracing */
810 if (strncmp(progname
, "drtrace", 7) == 0) {
811 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
814 ret
= parse_args(argc
, (const char **) argv
);
816 clean_exit(EXIT_FAILURE
);
819 ret
= validate_options();
824 ret
= set_signal_handler();
829 if (opt_tracing_group
!= NULL
) {
830 DBG("Set tracing group to '%s'", opt_tracing_group
);
831 lttng_set_tracing_group(opt_tracing_group
);
834 /* If ask for kernel tracing, need root perms */
835 if (opt_trace_kernel
) {
836 DBG("Kernel tracing activated");
838 ERR("%s must be setuid root", progname
);
843 /* Check if the lttng session daemon is running.
844 * If no, a daemon will be spawned.
846 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
847 clean_exit(EXIT_FAILURE
);
850 ret
= process_client_opt();
This page took 0.050138 seconds and 4 git commands to generate.