5319c13d3dd9e33d7f3cddc70f1adf739f89d518
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 process_kernel_start_trace(void);
54 static int set_session_uuid(void);
55 static void sighandler(int sig
);
56 static int set_signal_handler(void);
57 static int validate_options(void);
58 static char *get_cmdline_by_pid(pid_t pid
);
59 static void set_opt_session_info(void);
64 * Process client request from the command line
65 * options. Every tracing action is done by the
68 static int process_client_opt(void)
72 set_opt_session_info();
75 ret
= process_opt_list_apps();
82 if (opt_list_session
) {
83 ret
= process_opt_list_sessions();
90 /* Session creation or auto session set on */
91 if (auto_session
|| opt_create_session
) {
92 DBG("Creating a new session");
93 ret
= process_opt_create_session();
99 ret
= set_session_uuid();
101 ERR("Session %s not found", opt_session_name
);
105 if (opt_destroy_session
) {
106 ret
= lttng_destroy_session(¤t_uuid
);
110 MSG("Session %s destroyed.", opt_session_name
);
113 if (opt_list_traces
) {
114 ret
= process_opt_list_traces();
121 * Action on traces (kernel or/and userspace).
124 if (opt_trace_kernel
) {
125 if (auto_trace
|| opt_create_trace
) {
126 DBG("Creating a kernel trace");
127 ret
= process_kernel_create_trace();
133 if (opt_event_list
!= NULL
) {
134 ret
= process_opt_kernel_event();
142 if (auto_trace
|| opt_start_trace
) {
143 DBG("Starting kernel tracing");
144 ret
= process_kernel_start_trace();
150 if (opt_stop_trace
) {
151 DBG("Stopping kernel tracing");
152 ret
= lttng_kernel_stop_tracing();
159 if (opt_trace_pid
!= 0) {
160 if (auto_trace
|| opt_create_trace
) {
161 DBG("Create a userspace trace for pid %d", opt_trace_pid
);
162 ret
= lttng_ust_create_trace(opt_trace_pid
);
166 MSG("Trace created successfully!");
169 if (auto_trace
|| opt_start_trace
) {
170 DBG("Start trace for pid %d", opt_trace_pid
);
171 ret
= lttng_ust_start_trace(opt_trace_pid
);
175 MSG("Trace started successfully!");
176 } else if (opt_stop_trace
) {
177 DBG("Stop trace for pid %d", opt_trace_pid
);
178 ret
= lttng_ust_stop_trace(opt_trace_pid
);
182 MSG("Trace stopped successfully!");
190 ERR("%s", lttng_get_readable_code(ret
));
191 error
: /* fall through */
196 * process_kernel_start_trace
198 * Start a kernel trace.
200 static int process_kernel_start_trace(void)
204 ret
= lttng_kernel_create_stream();
209 ret
= lttng_kernel_start_tracing();
214 MSG("Kernel tracing started");
223 * process_kernel_create_trace
225 * Create a kernel trace.
227 static int process_kernel_create_trace(void)
231 /* Setup kernel session */
232 ret
= lttng_kernel_create_session();
237 /* Create an empty channel (with no event) */
238 ret
= lttng_kernel_create_channel();
243 /* Opening metadata for session */
244 ret
= lttng_kernel_open_metadata();
256 * process_kernel_event
258 * Enable kernel event from the command line list given.
260 static int process_opt_kernel_event(void)
265 event_name
= strtok(opt_event_list
, ",");
266 while (event_name
!= NULL
) {
267 DBG("Enabling kernel event %s", event_name
);
268 ret
= lttng_kernel_enable_event(event_name
);
270 ERR("%s %s", lttng_get_readable_code(ret
), event_name
);
272 MSG("Kernel event %s enabled.", event_name
);
275 event_name
= strtok(NULL
, ",");
282 * set_opt_session_info
284 * Setup session_name, current_uuid, short_str_uuid and
285 * long_str_uuid using the command line options.
287 static void set_opt_session_info(void)
289 if (opt_session_name
!= NULL
) {
290 session_name
= strndup(opt_session_name
, NAME_MAX
);
291 DBG("Session name set to %s", session_name
);
298 * Set current session uuid to the current flow of command(s) using the
301 static int set_session_uuid(void)
303 int ret
, count
, i
, found
= 0;
304 struct lttng_session
*sessions
;
306 if (!uuid_is_null(current_uuid
)) {
307 lttng_set_current_session_uuid(¤t_uuid
);
311 count
= lttng_list_sessions(&sessions
);
317 for (i
= 0; i
< count
; i
++) {
318 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
319 lttng_set_current_session_uuid(&sessions
[i
].uuid
);
320 uuid_copy(current_uuid
, sessions
[i
].uuid
);
333 DBG("Session UUID set");
341 * process_opt_list_traces
343 * Get list of all traces for a specific session uuid.
345 static int process_opt_list_traces(void)
348 struct lttng_trace
*traces
;
350 ret
= lttng_list_traces(¤t_uuid
, &traces
);
351 DBG("Number of traces to list %d", ret
);
358 MSG("No traces found.");
362 MSG("Userspace traces:");
363 for (i
= 0; i
< ret
; i
++) {
364 if (traces
[i
].type
== USERSPACE
) {
365 MSG("\t%d) %s (pid: %d): %s",
366 i
, traces
[i
].name
, traces
[i
].pid
,
367 get_cmdline_by_pid(traces
[i
].pid
));
373 MSG("Kernel traces:");
374 for (;i
< ret
; i
++) {
375 if (traces
[i
].type
== KERNEL
) {
376 MSG("\t%d) %s", i
, traces
[i
].name
);
387 * process_opt_create_session
389 * Create a new session using the name pass
390 * to the command line.
392 static int process_opt_create_session(void)
399 /* Auto session name creation */
400 if (opt_session_name
== NULL
) {
402 timeinfo
= localtime(&rawtime
);
403 strftime(name
, sizeof(name
), "auto-%Y%m%d-%H%M%S", timeinfo
);
404 session_name
= strndup(name
, sizeof(name
));
405 DBG("Auto session name set to %s", session_name
);
408 ret
= lttng_create_session(session_name
);
413 MSG("Session created: %s", session_name
);
420 * process_opt_list_sessions
422 * Get the list of available sessions from
423 * the session daemon and print it to user.
425 static int process_opt_list_sessions(void)
428 struct lttng_session
*sessions
;
430 count
= lttng_list_sessions(&sessions
);
431 DBG("Session count %d", count
);
437 MSG("Available sessions (UUIDs):");
438 for (i
= 0; i
< count
; i
++) {
439 MSG(" %d) %s", i
+1, sessions
[i
].name
);
443 MSG("\nTo select a session, use -s, --session UUID.");
452 * process_opt_list_apps
454 * Get the UST traceable pid list and print
457 static int process_opt_list_apps(void)
463 count
= lttng_ust_list_apps(&pids
);
469 MSG("LTTng UST traceable application [name (pid)]:");
470 for (i
=0; i
< count
; i
++) {
471 cmdline
= get_cmdline_by_pid(pids
[i
]);
472 if (cmdline
== NULL
) {
473 MSG("\t(not running) (%d)", pids
[i
]);
476 MSG("\t%s (%d)", cmdline
, pids
[i
]);
480 /* Allocated by lttng_ust_list_apps() */
492 * Get command line from /proc for a specific pid.
494 * On success, return an allocated string pointer pointing to
496 * On error, return NULL.
498 static char *get_cmdline_by_pid(pid_t pid
)
502 char *cmdline
= NULL
;
503 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
505 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
506 fp
= fopen(path
, "r");
511 /* Caller must free() *cmdline */
512 cmdline
= malloc(PATH_MAX
);
513 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
523 * Make sure that all options passed to the command line are compatible with
526 * On error, return -1
527 * On success, return 0
529 static int validate_options(void)
531 /* If listing options, jump validation */
532 if (opt_list_apps
|| opt_list_session
) {
535 /* Conflicting command */
536 if (opt_start_trace
&& opt_stop_trace
) {
537 ERR("Can't use --start and --stop together.");
539 /* If no PID specified and trace_kernel is off */
540 } else if ((opt_trace_pid
== 0 && !opt_trace_kernel
) &&
541 (opt_create_trace
|| opt_start_trace
|| opt_stop_trace
|| opt_destroy_trace
)) {
542 ERR("Please specify for which tracer (-k or -p PID).");
544 /* List traces, we need a session name */
545 } else if (opt_list_traces
&& opt_session_name
== NULL
) {
546 ERR("Can't use -t without -s, --session option.");
548 /* Can't set event for both kernel and userspace at the same time */
549 } else if (opt_event_list
!= NULL
&& (opt_trace_kernel
&& opt_trace_pid
)) {
550 ERR("Please don't use --event for both kernel and userspace.\nOne at a time to enable events.");
552 /* Don't need a trace name for kernel tracig */
553 } else if (opt_trace_name
!= NULL
&& opt_trace_kernel
) {
554 ERR("For action on a kernel trace, please don't specify a trace name.");
556 } else if (opt_destroy_trace
&& opt_session_name
== NULL
) {
557 ERR("Please specify a session in order to destroy a trace");
559 } else if (opt_create_trace
|| opt_destroy_trace
) {
560 /* Both kernel and user-space are denied for these options */
561 if (opt_trace_pid
!= 0 && opt_trace_kernel
) {
562 ERR("Kernel and user-space trace creation and destruction can't be used together.");
564 /* Need a trace name for user-space tracing */
565 } else if (opt_trace_name
== NULL
&& opt_trace_pid
!= 0) {
566 ERR("Please specify a trace name for user-space tracing");
569 } else if (opt_stop_trace
&& opt_trace_pid
!= 0 && opt_trace_name
== NULL
) {
570 ERR("Please specify a trace name for user-space tracing");
572 } else if (opt_stop_trace
&& opt_session_name
== NULL
) {
573 ERR("Please specify a session to stop tracing");
577 /* If start trace, auto start tracing */
578 if (opt_start_trace
|| opt_event_list
!= NULL
) {
579 DBG("Requesting auto tracing");
583 /* If no session, auto create one */
584 if (opt_session_name
== NULL
) {
585 DBG("Requesting an auto session creation");
599 * Spawn a session daemon by forking and execv.
601 static int spawn_sessiond(char *pathname
)
606 MSG("Spawning session daemon");
610 * Spawn session daemon and tell
611 * it to signal us when ready.
613 execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
614 /* execlp only returns if error happened */
615 if (errno
== ENOENT
) {
616 ERR("No session daemon found. Use --sessiond-path.");
620 kill(getppid(), SIGTERM
); /* unpause parent */
622 } else if (pid
> 0) {
623 /* Wait for ltt-sessiond to start */
639 * Check if the session daemon is available using
640 * the liblttngctl API for the check. If not, try to
643 static int check_ltt_sessiond(void)
646 char *pathname
= NULL
, *alloc_pathname
= NULL
;
648 ret
= lttng_check_session_daemon();
650 /* Try command line option path */
651 if (opt_sessiond_path
!= NULL
) {
652 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
654 ERR("No such file: %s", opt_sessiond_path
);
657 pathname
= opt_sessiond_path
;
659 /* Try LTTNG_SESSIOND_PATH env variable */
660 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
663 /* Let's rock and roll */
664 if (pathname
== NULL
) {
665 ret
= asprintf(&alloc_pathname
, "ltt-sessiond");
669 pathname
= alloc_pathname
;
672 ret
= spawn_sessiond(pathname
);
673 free(alloc_pathname
);
675 ERR("Problem occurs when starting %s", pathname
);
687 * Setup signal handler for SIGCHLD and SIGTERM.
689 static int set_signal_handler(void)
695 if ((ret
= sigemptyset(&sigset
)) < 0) {
696 perror("sigemptyset");
700 sa
.sa_handler
= sighandler
;
703 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
708 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
720 * Signal handler for the daemon
722 static void sighandler(int sig
)
726 DBG("SIGTERM catched");
727 clean_exit(EXIT_FAILURE
);
731 DBG("SIGCHLD catched");
734 DBG("Unknown signal %d catched", sig
);
744 void clean_exit(int code
)
757 int main(int argc
, char *argv
[])
761 progname
= argv
[0] ? argv
[0] : "lttng";
763 /* For Mathieu Desnoyers aka Dr Tracing */
764 if (strncmp(progname
, "drtrace", 7) == 0) {
765 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
768 ret
= parse_args(argc
, (const char **) argv
);
770 clean_exit(EXIT_FAILURE
);
773 ret
= validate_options();
778 ret
= set_signal_handler();
783 if (opt_tracing_group
!= NULL
) {
784 DBG("Set tracing group to '%s'", opt_tracing_group
);
785 lttng_set_tracing_group(opt_tracing_group
);
788 /* If ask for kernel tracing, need root perms */
789 if (opt_trace_kernel
) {
790 DBG("Kernel tracing activated");
792 ERR("%s must be setuid root", progname
);
797 /* Check if the lttng session daemon is running.
798 * If no, a daemon will be spawned.
800 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
801 clean_exit(EXIT_FAILURE
);
804 ret
= process_client_opt();
This page took 0.044376 seconds and 3 git commands to generate.