10ce2ddfc08539bb121c2a0aa8b3bfe017a93f20
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_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);
63 * Process client request from the command line
64 * options. Every tracing action is done by the
67 static int process_client_opt(void)
71 set_opt_session_info();
74 ret
= process_opt_list_apps();
81 if (opt_list_session
) {
82 ret
= process_opt_list_sessions();
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();
98 ret
= set_session_uuid();
100 ERR("Session %s not found", opt_session_name
);
104 if (opt_destroy_session
) {
105 ret
= lttng_destroy_session(¤t_uuid
);
109 MSG("Session %s destroyed.", opt_session_name
);
112 if (opt_list_traces
) {
113 ret
= process_opt_list_traces();
120 * Action on traces (kernel or/and userspace).
123 if (opt_trace_kernel
) {
124 if (auto_trace
|| opt_create_trace
) {
125 DBG("Creating a kernel trace");
126 ret
= process_kernel_create_trace();
132 if (opt_event_list
!= NULL
) {
133 ret
= process_opt_kernel_event();
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
);
148 MSG("Trace created successfully!");
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
);
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
);
164 MSG("Trace stopped successfully!");
172 ERR("%s", lttng_get_readable_code(ret
));
173 error
: /* fall through */
178 * process_kernel_create_trace
180 * Create a kernel trace.
182 static int process_kernel_create_trace(void)
188 * process_kernel_event
190 * Enable kernel event from the command line list given.
192 static int process_opt_kernel_event(void)
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
);
202 ERR("%s %s", lttng_get_readable_code(ret
), event_name
);
204 MSG("Kernel event %s enabled.", event_name
);
207 event_name
= strtok(NULL
, ",");
214 * set_opt_session_info
216 * Setup session_name, current_uuid, short_str_uuid and
217 * long_str_uuid using the command line options.
219 static void set_opt_session_info(void)
221 if (opt_session_name
!= NULL
) {
222 session_name
= strndup(opt_session_name
, NAME_MAX
);
223 DBG("Session name set to %s", session_name
);
230 * Set current session uuid to the current flow of command(s) using the
233 static int set_session_uuid(void)
235 int ret
, count
, i
, found
= 0;
236 struct lttng_session
*sessions
;
238 if (!uuid_is_null(current_uuid
)) {
239 lttng_set_current_session_uuid(¤t_uuid
);
243 count
= lttng_list_sessions(&sessions
);
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
);
265 DBG("Session UUID set");
273 * process_opt_list_traces
275 * Get list of all traces for a specific session uuid.
277 static int process_opt_list_traces(void)
280 struct lttng_trace
*traces
;
282 ret
= lttng_list_traces(¤t_uuid
, &traces
);
283 DBG("Number of traces to list %d", ret
);
290 MSG("No traces found.");
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
));
305 MSG("Kernel traces:");
306 for (;i
< ret
; i
++) {
307 if (traces
[i
].type
== KERNEL
) {
308 MSG("\t%d) %s", i
, traces
[i
].name
);
319 * process_opt_create_session
321 * Create a new session using the name pass
322 * to the command line.
324 static int process_opt_create_session(void)
331 /* Auto session name creation */
332 if (opt_session_name
== NULL
) {
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
);
340 ret
= lttng_create_session(session_name
);
345 MSG("Session created: %s", session_name
);
352 * process_opt_list_sessions
354 * Get the list of available sessions from
355 * the session daemon and print it to user.
357 static int process_opt_list_sessions(void)
360 struct lttng_session
*sessions
;
362 count
= lttng_list_sessions(&sessions
);
363 DBG("Session count %d", count
);
369 MSG("Available sessions (UUIDs):");
370 for (i
= 0; i
< count
; i
++) {
371 MSG(" %d) %s", i
+1, sessions
[i
].name
);
375 MSG("\nTo select a session, use -s, --session UUID.");
384 * process_opt_list_apps
386 * Get the UST traceable pid list and print
389 static int process_opt_list_apps(void)
395 count
= lttng_ust_list_apps(&pids
);
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
]);
408 MSG("\t%s (%d)", cmdline
, pids
[i
]);
412 /* Allocated by lttng_ust_list_apps() */
424 * Get command line from /proc for a specific pid.
426 * On success, return an allocated string pointer pointing to
428 * On error, return NULL.
430 static char *get_cmdline_by_pid(pid_t pid
)
434 char *cmdline
= NULL
;
435 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
437 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
438 fp
= fopen(path
, "r");
443 /* Caller must free() *cmdline */
444 cmdline
= malloc(PATH_MAX
);
445 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
455 * Make sure that all options passed to the command line are compatible with
458 * On error, return -1
459 * On success, return 0
461 static int validate_options(void)
463 /* If listing options, jump validation */
464 if (opt_list_apps
|| opt_list_session
) {
467 /* Conflicting command */
468 if (opt_start_trace
&& opt_stop_trace
) {
469 ERR("Can't use --start and --stop together.");
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).");
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.");
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.");
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.");
488 } else if (opt_destroy_trace
&& opt_session_name
== NULL
) {
489 ERR("Please specify a session in order to destroy a trace");
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.");
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");
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");
506 /* If start trace, auto start tracing */
507 if (opt_start_trace
) {
508 DBG("Requesting auto tracing");
512 /* If no session, auto create one */
513 if (opt_session_name
== NULL
) {
514 DBG("Requesting an auto session creation");
528 * Spawn a session daemon by forking and execv.
530 static int spawn_sessiond(char *pathname
)
535 MSG("Spawning session daemon");
539 * Spawn session daemon and tell
540 * it to signal us when ready.
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.");
549 kill(getppid(), SIGTERM
); /* unpause parent */
551 } else if (pid
> 0) {
552 /* Wait for ltt-sessiond to start */
568 * Check if the session daemon is available using
569 * the liblttngctl API for the check. If not, try to
572 static int check_ltt_sessiond(void)
575 char *pathname
= NULL
, *alloc_pathname
= NULL
;
577 ret
= lttng_check_session_daemon();
579 /* Try command line option path */
580 if (opt_sessiond_path
!= NULL
) {
581 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
583 ERR("No such file: %s", opt_sessiond_path
);
586 pathname
= opt_sessiond_path
;
588 /* Try LTTNG_SESSIOND_PATH env variable */
589 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
592 /* Let's rock and roll */
593 if (pathname
== NULL
) {
594 ret
= asprintf(&alloc_pathname
, "ltt-sessiond");
598 pathname
= alloc_pathname
;
601 ret
= spawn_sessiond(pathname
);
602 free(alloc_pathname
);
604 ERR("Problem occurs when starting %s", pathname
);
616 * Setup signal handler for SIGCHLD and SIGTERM.
618 static int set_signal_handler(void)
624 if ((ret
= sigemptyset(&sigset
)) < 0) {
625 perror("sigemptyset");
629 sa
.sa_handler
= sighandler
;
632 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
637 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
649 * Signal handler for the daemon
651 static void sighandler(int sig
)
655 DBG("SIGTERM catched");
656 clean_exit(EXIT_FAILURE
);
660 DBG("SIGCHLD catched");
663 DBG("Unknown signal %d catched", sig
);
673 void clean_exit(int code
)
686 int main(int argc
, char *argv
[])
690 progname
= argv
[0] ? argv
[0] : "lttng";
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);
697 ret
= parse_args(argc
, (const char **) argv
);
699 clean_exit(EXIT_FAILURE
);
702 ret
= validate_options();
707 ret
= set_signal_handler();
712 if (opt_tracing_group
!= NULL
) {
713 DBG("Set tracing group to '%s'", opt_tracing_group
);
714 lttng_set_tracing_group(opt_tracing_group
);
717 /* If ask for kernel tracing, need root perms */
718 if (opt_trace_kernel
) {
719 DBG("Kernel tracing activated");
721 ERR("%s must be setuid root", progname
);
726 /* Check if the lttng session daemon is running.
727 * If no, a daemon will be spawned.
729 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
730 clean_exit(EXIT_FAILURE
);
733 ret
= process_client_opt();
This page took 0.04717 seconds and 5 git commands to generate.