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();
142 if (opt_trace_pid
!= 0) {
143 if (auto_trace
|| opt_create_trace
) {
144 DBG("Create a userspace trace for pid %d", opt_trace_pid
);
145 ret
= lttng_ust_create_trace(opt_trace_pid
);
149 MSG("Trace created successfully!");
152 if (auto_trace
|| opt_start_trace
) {
153 DBG("Start trace for pid %d", opt_trace_pid
);
154 ret
= lttng_ust_start_trace(opt_trace_pid
);
158 MSG("Trace started successfully!");
159 } else if (opt_stop_trace
) {
160 DBG("Stop trace for pid %d", opt_trace_pid
);
161 ret
= lttng_ust_stop_trace(opt_trace_pid
);
165 MSG("Trace stopped successfully!");
173 ERR("%s", lttng_get_readable_code(ret
));
174 error
: /* fall through */
179 * process_kernel_create_trace
181 * Create a kernel trace.
183 static int process_kernel_create_trace(void)
187 /* Setup kernel session */
188 ret
= lttng_kernel_create_session();
193 /* Create an empty channel (with no event) */
194 ret
= lttng_kernel_create_channel();
199 /* Opening metadata for session */
200 ret
= lttng_kernel_open_metadata();
212 * process_kernel_event
214 * Enable kernel event from the command line list given.
216 static int process_opt_kernel_event(void)
221 event_name
= strtok(opt_event_list
, ",");
222 while (event_name
!= NULL
) {
223 DBG("Enabling kernel event %s", event_name
);
224 ret
= lttng_kernel_enable_event(event_name
);
226 ERR("%s %s", lttng_get_readable_code(ret
), event_name
);
228 MSG("Kernel event %s enabled.", event_name
);
231 event_name
= strtok(NULL
, ",");
238 * set_opt_session_info
240 * Setup session_name, current_uuid, short_str_uuid and
241 * long_str_uuid using the command line options.
243 static void set_opt_session_info(void)
245 if (opt_session_name
!= NULL
) {
246 session_name
= strndup(opt_session_name
, NAME_MAX
);
247 DBG("Session name set to %s", session_name
);
254 * Set current session uuid to the current flow of command(s) using the
257 static int set_session_uuid(void)
259 int ret
, count
, i
, found
= 0;
260 struct lttng_session
*sessions
;
262 if (!uuid_is_null(current_uuid
)) {
263 lttng_set_current_session_uuid(¤t_uuid
);
267 count
= lttng_list_sessions(&sessions
);
273 for (i
= 0; i
< count
; i
++) {
274 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
275 lttng_set_current_session_uuid(&sessions
[i
].uuid
);
276 uuid_copy(current_uuid
, sessions
[i
].uuid
);
289 DBG("Session UUID set");
297 * process_opt_list_traces
299 * Get list of all traces for a specific session uuid.
301 static int process_opt_list_traces(void)
304 struct lttng_trace
*traces
;
306 ret
= lttng_list_traces(¤t_uuid
, &traces
);
307 DBG("Number of traces to list %d", ret
);
314 MSG("No traces found.");
318 MSG("Userspace traces:");
319 for (i
= 0; i
< ret
; i
++) {
320 if (traces
[i
].type
== USERSPACE
) {
321 MSG("\t%d) %s (pid: %d): %s",
322 i
, traces
[i
].name
, traces
[i
].pid
,
323 get_cmdline_by_pid(traces
[i
].pid
));
329 MSG("Kernel traces:");
330 for (;i
< ret
; i
++) {
331 if (traces
[i
].type
== KERNEL
) {
332 MSG("\t%d) %s", i
, traces
[i
].name
);
343 * process_opt_create_session
345 * Create a new session using the name pass
346 * to the command line.
348 static int process_opt_create_session(void)
355 /* Auto session name creation */
356 if (opt_session_name
== NULL
) {
358 timeinfo
= localtime(&rawtime
);
359 strftime(name
, sizeof(name
), "auto-%Y%m%d-%H%M%S", timeinfo
);
360 session_name
= strndup(name
, sizeof(name
));
361 DBG("Auto session name set to %s", session_name
);
364 ret
= lttng_create_session(session_name
);
369 MSG("Session created: %s", session_name
);
376 * process_opt_list_sessions
378 * Get the list of available sessions from
379 * the session daemon and print it to user.
381 static int process_opt_list_sessions(void)
384 struct lttng_session
*sessions
;
386 count
= lttng_list_sessions(&sessions
);
387 DBG("Session count %d", count
);
393 MSG("Available sessions (UUIDs):");
394 for (i
= 0; i
< count
; i
++) {
395 MSG(" %d) %s", i
+1, sessions
[i
].name
);
399 MSG("\nTo select a session, use -s, --session UUID.");
408 * process_opt_list_apps
410 * Get the UST traceable pid list and print
413 static int process_opt_list_apps(void)
419 count
= lttng_ust_list_apps(&pids
);
425 MSG("LTTng UST traceable application [name (pid)]:");
426 for (i
=0; i
< count
; i
++) {
427 cmdline
= get_cmdline_by_pid(pids
[i
]);
428 if (cmdline
== NULL
) {
429 MSG("\t(not running) (%d)", pids
[i
]);
432 MSG("\t%s (%d)", cmdline
, pids
[i
]);
436 /* Allocated by lttng_ust_list_apps() */
448 * Get command line from /proc for a specific pid.
450 * On success, return an allocated string pointer pointing to
452 * On error, return NULL.
454 static char *get_cmdline_by_pid(pid_t pid
)
458 char *cmdline
= NULL
;
459 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
461 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
462 fp
= fopen(path
, "r");
467 /* Caller must free() *cmdline */
468 cmdline
= malloc(PATH_MAX
);
469 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
479 * Make sure that all options passed to the command line are compatible with
482 * On error, return -1
483 * On success, return 0
485 static int validate_options(void)
487 /* If listing options, jump validation */
488 if (opt_list_apps
|| opt_list_session
) {
491 /* Conflicting command */
492 if (opt_start_trace
&& opt_stop_trace
) {
493 ERR("Can't use --start and --stop together.");
495 /* If no PID specified and trace_kernel is off */
496 } else if ((opt_trace_pid
== 0 && !opt_trace_kernel
) &&
497 (opt_create_trace
|| opt_start_trace
|| opt_stop_trace
|| opt_destroy_trace
)) {
498 ERR("Please specify for which tracer (-k or -p PID).");
500 /* List traces, we need a session name */
501 } else if (opt_list_traces
&& opt_session_name
== NULL
) {
502 ERR("Can't use -t without -s, --session option.");
504 /* Can't set event for both kernel and userspace at the same time */
505 } else if (opt_event_list
!= NULL
&& (opt_trace_kernel
&& opt_trace_pid
)) {
506 ERR("Please don't use --event for both kernel and userspace.\nOne at a time to enable events.");
508 /* Don't need a trace name for kernel tracig */
509 } else if (opt_trace_name
!= NULL
&& opt_trace_kernel
) {
510 ERR("For action on a kernel trace, please don't specify a trace name.");
512 } else if (opt_destroy_trace
&& opt_session_name
== NULL
) {
513 ERR("Please specify a session in order to destroy a trace");
515 } else if (opt_create_trace
|| opt_destroy_trace
) {
516 /* Both kernel and user-space are denied for these options */
517 if (opt_trace_pid
!= 0 && opt_trace_kernel
) {
518 ERR("Kernel and user-space trace creation and destruction can't be used together.");
520 /* Need a trace name for user-space tracing */
521 } else if (opt_trace_name
== NULL
&& opt_trace_pid
!= 0) {
522 ERR("Please specify a trace name for user-space tracing");
525 } else if (opt_stop_trace
&& opt_trace_pid
!= 0 && opt_trace_name
== NULL
) {
526 ERR("Please specify a trace name for user-space tracing");
530 /* If start trace, auto start tracing */
531 if (opt_start_trace
|| opt_event_list
!= NULL
) {
532 DBG("Requesting auto tracing");
536 /* If no session, auto create one */
537 if (opt_session_name
== NULL
) {
538 DBG("Requesting an auto session creation");
552 * Spawn a session daemon by forking and execv.
554 static int spawn_sessiond(char *pathname
)
559 MSG("Spawning session daemon");
563 * Spawn session daemon and tell
564 * it to signal us when ready.
566 execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
567 /* execlp only returns if error happened */
568 if (errno
== ENOENT
) {
569 ERR("No session daemon found. Use --sessiond-path.");
573 kill(getppid(), SIGTERM
); /* unpause parent */
575 } else if (pid
> 0) {
576 /* Wait for ltt-sessiond to start */
592 * Check if the session daemon is available using
593 * the liblttngctl API for the check. If not, try to
596 static int check_ltt_sessiond(void)
599 char *pathname
= NULL
, *alloc_pathname
= NULL
;
601 ret
= lttng_check_session_daemon();
603 /* Try command line option path */
604 if (opt_sessiond_path
!= NULL
) {
605 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
607 ERR("No such file: %s", opt_sessiond_path
);
610 pathname
= opt_sessiond_path
;
612 /* Try LTTNG_SESSIOND_PATH env variable */
613 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
616 /* Let's rock and roll */
617 if (pathname
== NULL
) {
618 ret
= asprintf(&alloc_pathname
, "ltt-sessiond");
622 pathname
= alloc_pathname
;
625 ret
= spawn_sessiond(pathname
);
626 free(alloc_pathname
);
628 ERR("Problem occurs when starting %s", pathname
);
640 * Setup signal handler for SIGCHLD and SIGTERM.
642 static int set_signal_handler(void)
648 if ((ret
= sigemptyset(&sigset
)) < 0) {
649 perror("sigemptyset");
653 sa
.sa_handler
= sighandler
;
656 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
661 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
673 * Signal handler for the daemon
675 static void sighandler(int sig
)
679 DBG("SIGTERM catched");
680 clean_exit(EXIT_FAILURE
);
684 DBG("SIGCHLD catched");
687 DBG("Unknown signal %d catched", sig
);
697 void clean_exit(int code
)
710 int main(int argc
, char *argv
[])
714 progname
= argv
[0] ? argv
[0] : "lttng";
716 /* For Mathieu Desnoyers aka Dr Tracing */
717 if (strncmp(progname
, "drtrace", 7) == 0) {
718 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
721 ret
= parse_args(argc
, (const char **) argv
);
723 clean_exit(EXIT_FAILURE
);
726 ret
= validate_options();
731 ret
= set_signal_handler();
736 if (opt_tracing_group
!= NULL
) {
737 DBG("Set tracing group to '%s'", opt_tracing_group
);
738 lttng_set_tracing_group(opt_tracing_group
);
741 /* If ask for kernel tracing, need root perms */
742 if (opt_trace_kernel
) {
743 DBG("Kernel tracing activated");
745 ERR("%s must be setuid root", progname
);
750 /* Check if the lttng session daemon is running.
751 * If no, a daemon will be spawned.
753 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
754 clean_exit(EXIT_FAILURE
);
757 ret
= process_client_opt();
This page took 0.089532 seconds and 6 git commands to generate.