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)
186 /* Setup kernel session */
187 ret
= lttng_kernel_create_session();
192 /* Create an empty channel (with no event) */
193 ret
= lttng_kernel_create_channel();
205 * process_kernel_event
207 * Enable kernel event from the command line list given.
209 static int process_opt_kernel_event(void)
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
);
219 ERR("%s %s", lttng_get_readable_code(ret
), event_name
);
221 MSG("Kernel event %s enabled.", event_name
);
224 event_name
= strtok(NULL
, ",");
231 * set_opt_session_info
233 * Setup session_name, current_uuid, short_str_uuid and
234 * long_str_uuid using the command line options.
236 static void set_opt_session_info(void)
238 if (opt_session_name
!= NULL
) {
239 session_name
= strndup(opt_session_name
, NAME_MAX
);
240 DBG("Session name set to %s", session_name
);
247 * Set current session uuid to the current flow of command(s) using the
250 static int set_session_uuid(void)
252 int ret
, count
, i
, found
= 0;
253 struct lttng_session
*sessions
;
255 if (!uuid_is_null(current_uuid
)) {
256 lttng_set_current_session_uuid(¤t_uuid
);
260 count
= lttng_list_sessions(&sessions
);
266 for (i
= 0; i
< count
; i
++) {
267 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
268 lttng_set_current_session_uuid(&sessions
[i
].uuid
);
269 uuid_copy(current_uuid
, sessions
[i
].uuid
);
282 DBG("Session UUID set");
290 * process_opt_list_traces
292 * Get list of all traces for a specific session uuid.
294 static int process_opt_list_traces(void)
297 struct lttng_trace
*traces
;
299 ret
= lttng_list_traces(¤t_uuid
, &traces
);
300 DBG("Number of traces to list %d", ret
);
307 MSG("No traces found.");
311 MSG("Userspace traces:");
312 for (i
= 0; i
< ret
; i
++) {
313 if (traces
[i
].type
== USERSPACE
) {
314 MSG("\t%d) %s (pid: %d): %s",
315 i
, traces
[i
].name
, traces
[i
].pid
,
316 get_cmdline_by_pid(traces
[i
].pid
));
322 MSG("Kernel traces:");
323 for (;i
< ret
; i
++) {
324 if (traces
[i
].type
== KERNEL
) {
325 MSG("\t%d) %s", i
, traces
[i
].name
);
336 * process_opt_create_session
338 * Create a new session using the name pass
339 * to the command line.
341 static int process_opt_create_session(void)
348 /* Auto session name creation */
349 if (opt_session_name
== NULL
) {
351 timeinfo
= localtime(&rawtime
);
352 strftime(name
, sizeof(name
), "auto-%Y%m%d-%H%M%S", timeinfo
);
353 session_name
= strndup(name
, sizeof(name
));
354 DBG("Auto session name set to %s", session_name
);
357 ret
= lttng_create_session(session_name
);
362 MSG("Session created: %s", session_name
);
369 * process_opt_list_sessions
371 * Get the list of available sessions from
372 * the session daemon and print it to user.
374 static int process_opt_list_sessions(void)
377 struct lttng_session
*sessions
;
379 count
= lttng_list_sessions(&sessions
);
380 DBG("Session count %d", count
);
386 MSG("Available sessions (UUIDs):");
387 for (i
= 0; i
< count
; i
++) {
388 MSG(" %d) %s", i
+1, sessions
[i
].name
);
392 MSG("\nTo select a session, use -s, --session UUID.");
401 * process_opt_list_apps
403 * Get the UST traceable pid list and print
406 static int process_opt_list_apps(void)
412 count
= lttng_ust_list_apps(&pids
);
418 MSG("LTTng UST traceable application [name (pid)]:");
419 for (i
=0; i
< count
; i
++) {
420 cmdline
= get_cmdline_by_pid(pids
[i
]);
421 if (cmdline
== NULL
) {
422 MSG("\t(not running) (%d)", pids
[i
]);
425 MSG("\t%s (%d)", cmdline
, pids
[i
]);
429 /* Allocated by lttng_ust_list_apps() */
441 * Get command line from /proc for a specific pid.
443 * On success, return an allocated string pointer pointing to
445 * On error, return NULL.
447 static char *get_cmdline_by_pid(pid_t pid
)
451 char *cmdline
= NULL
;
452 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
454 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
455 fp
= fopen(path
, "r");
460 /* Caller must free() *cmdline */
461 cmdline
= malloc(PATH_MAX
);
462 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
472 * Make sure that all options passed to the command line are compatible with
475 * On error, return -1
476 * On success, return 0
478 static int validate_options(void)
480 /* If listing options, jump validation */
481 if (opt_list_apps
|| opt_list_session
) {
484 /* Conflicting command */
485 if (opt_start_trace
&& opt_stop_trace
) {
486 ERR("Can't use --start and --stop together.");
488 /* If no PID specified and trace_kernel is off */
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).");
493 /* List traces, we need a session name */
494 } else if (opt_list_traces
&& opt_session_name
== NULL
) {
495 ERR("Can't use -t without -s, --session option.");
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.");
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.");
505 } else if (opt_destroy_trace
&& opt_session_name
== NULL
) {
506 ERR("Please specify a session in order to destroy a trace");
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.");
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");
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");
523 /* If start trace, auto start tracing */
524 if (opt_start_trace
) {
525 DBG("Requesting auto tracing");
529 /* If no session, auto create one */
530 if (opt_session_name
== NULL
) {
531 DBG("Requesting an auto session creation");
545 * Spawn a session daemon by forking and execv.
547 static int spawn_sessiond(char *pathname
)
552 MSG("Spawning session daemon");
556 * Spawn session daemon and tell
557 * it to signal us when ready.
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.");
566 kill(getppid(), SIGTERM
); /* unpause parent */
568 } else if (pid
> 0) {
569 /* Wait for ltt-sessiond to start */
585 * Check if the session daemon is available using
586 * the liblttngctl API for the check. If not, try to
589 static int check_ltt_sessiond(void)
592 char *pathname
= NULL
, *alloc_pathname
= NULL
;
594 ret
= lttng_check_session_daemon();
596 /* Try command line option path */
597 if (opt_sessiond_path
!= NULL
) {
598 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
600 ERR("No such file: %s", opt_sessiond_path
);
603 pathname
= opt_sessiond_path
;
605 /* Try LTTNG_SESSIOND_PATH env variable */
606 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
609 /* Let's rock and roll */
610 if (pathname
== NULL
) {
611 ret
= asprintf(&alloc_pathname
, "ltt-sessiond");
615 pathname
= alloc_pathname
;
618 ret
= spawn_sessiond(pathname
);
619 free(alloc_pathname
);
621 ERR("Problem occurs when starting %s", pathname
);
633 * Setup signal handler for SIGCHLD and SIGTERM.
635 static int set_signal_handler(void)
641 if ((ret
= sigemptyset(&sigset
)) < 0) {
642 perror("sigemptyset");
646 sa
.sa_handler
= sighandler
;
649 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
654 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
666 * Signal handler for the daemon
668 static void sighandler(int sig
)
672 DBG("SIGTERM catched");
673 clean_exit(EXIT_FAILURE
);
677 DBG("SIGCHLD catched");
680 DBG("Unknown signal %d catched", sig
);
690 void clean_exit(int code
)
703 int main(int argc
, char *argv
[])
707 progname
= argv
[0] ? argv
[0] : "lttng";
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);
714 ret
= parse_args(argc
, (const char **) argv
);
716 clean_exit(EXIT_FAILURE
);
719 ret
= validate_options();
724 ret
= set_signal_handler();
729 if (opt_tracing_group
!= NULL
) {
730 DBG("Set tracing group to '%s'", opt_tracing_group
);
731 lttng_set_tracing_group(opt_tracing_group
);
734 /* If ask for kernel tracing, need root perms */
735 if (opt_trace_kernel
) {
736 DBG("Kernel tracing activated");
738 ERR("%s must be setuid root", progname
);
743 /* Check if the lttng session daemon is running.
744 * If no, a daemon will be spawned.
746 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
747 clean_exit(EXIT_FAILURE
);
750 ret
= process_client_opt();
This page took 0.047543 seconds and 5 git commands to generate.