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();
206 * process_kernel_event
208 * Enable kernel event from the command line list given.
210 static int process_opt_kernel_event(void)
215 event_name
= strtok(opt_event_list
, ",");
216 while (event_name
!= NULL
) {
217 DBG("Enabling kernel event %s", event_name
);
218 ret
= lttng_kernel_enable_event(event_name
);
220 ERR("%s %s", lttng_get_readable_code(ret
), event_name
);
222 MSG("Kernel event %s enabled.", event_name
);
225 event_name
= strtok(NULL
, ",");
232 * set_opt_session_info
234 * Setup session_name, current_uuid, short_str_uuid and
235 * long_str_uuid using the command line options.
237 static void set_opt_session_info(void)
239 if (opt_session_name
!= NULL
) {
240 session_name
= strndup(opt_session_name
, NAME_MAX
);
241 DBG("Session name set to %s", session_name
);
248 * Set current session uuid to the current flow of command(s) using the
251 static int set_session_uuid(void)
253 int ret
, count
, i
, found
= 0;
254 struct lttng_session
*sessions
;
256 if (!uuid_is_null(current_uuid
)) {
257 lttng_set_current_session_uuid(¤t_uuid
);
261 count
= lttng_list_sessions(&sessions
);
267 for (i
= 0; i
< count
; i
++) {
268 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
269 lttng_set_current_session_uuid(&sessions
[i
].uuid
);
270 uuid_copy(current_uuid
, sessions
[i
].uuid
);
283 DBG("Session UUID set");
291 * process_opt_list_traces
293 * Get list of all traces for a specific session uuid.
295 static int process_opt_list_traces(void)
298 struct lttng_trace
*traces
;
300 ret
= lttng_list_traces(¤t_uuid
, &traces
);
301 DBG("Number of traces to list %d", ret
);
308 MSG("No traces found.");
312 MSG("Userspace traces:");
313 for (i
= 0; i
< ret
; i
++) {
314 if (traces
[i
].type
== USERSPACE
) {
315 MSG("\t%d) %s (pid: %d): %s",
316 i
, traces
[i
].name
, traces
[i
].pid
,
317 get_cmdline_by_pid(traces
[i
].pid
));
323 MSG("Kernel traces:");
324 for (;i
< ret
; i
++) {
325 if (traces
[i
].type
== KERNEL
) {
326 MSG("\t%d) %s", i
, traces
[i
].name
);
337 * process_opt_create_session
339 * Create a new session using the name pass
340 * to the command line.
342 static int process_opt_create_session(void)
349 /* Auto session name creation */
350 if (opt_session_name
== NULL
) {
352 timeinfo
= localtime(&rawtime
);
353 strftime(name
, sizeof(name
), "auto-%Y%m%d-%H%M%S", timeinfo
);
354 session_name
= strndup(name
, sizeof(name
));
355 DBG("Auto session name set to %s", session_name
);
358 ret
= lttng_create_session(session_name
);
363 MSG("Session created: %s", session_name
);
370 * process_opt_list_sessions
372 * Get the list of available sessions from
373 * the session daemon and print it to user.
375 static int process_opt_list_sessions(void)
378 struct lttng_session
*sessions
;
380 count
= lttng_list_sessions(&sessions
);
381 DBG("Session count %d", count
);
387 MSG("Available sessions (UUIDs):");
388 for (i
= 0; i
< count
; i
++) {
389 MSG(" %d) %s", i
+1, sessions
[i
].name
);
393 MSG("\nTo select a session, use -s, --session UUID.");
402 * process_opt_list_apps
404 * Get the UST traceable pid list and print
407 static int process_opt_list_apps(void)
413 count
= lttng_ust_list_apps(&pids
);
419 MSG("LTTng UST traceable application [name (pid)]:");
420 for (i
=0; i
< count
; i
++) {
421 cmdline
= get_cmdline_by_pid(pids
[i
]);
422 if (cmdline
== NULL
) {
423 MSG("\t(not running) (%d)", pids
[i
]);
426 MSG("\t%s (%d)", cmdline
, pids
[i
]);
430 /* Allocated by lttng_ust_list_apps() */
442 * Get command line from /proc for a specific pid.
444 * On success, return an allocated string pointer pointing to
446 * On error, return NULL.
448 static char *get_cmdline_by_pid(pid_t pid
)
452 char *cmdline
= NULL
;
453 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
455 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
456 fp
= fopen(path
, "r");
461 /* Caller must free() *cmdline */
462 cmdline
= malloc(PATH_MAX
);
463 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
473 * Make sure that all options passed to the command line are compatible with
476 * On error, return -1
477 * On success, return 0
479 static int validate_options(void)
481 /* If listing options, jump validation */
482 if (opt_list_apps
|| opt_list_session
) {
485 /* Conflicting command */
486 if (opt_start_trace
&& opt_stop_trace
) {
487 ERR("Can't use --start and --stop together.");
489 /* If no PID specified and trace_kernel is off */
490 } else if ((opt_trace_pid
== 0 && !opt_trace_kernel
) &&
491 (opt_create_trace
|| opt_start_trace
|| opt_stop_trace
|| opt_destroy_trace
)) {
492 ERR("Please specify for which tracer (-k or -p PID).");
494 /* List traces, we need a session name */
495 } else if (opt_list_traces
&& opt_session_name
== NULL
) {
496 ERR("Can't use -t without -s, --session option.");
498 /* Can't set event for both kernel and userspace at the same time */
499 } else if (opt_event_list
!= NULL
&& (opt_trace_kernel
&& opt_trace_pid
)) {
500 ERR("Please don't use --event for both kernel and userspace.\nOne at a time to enable events.");
502 /* Don't need a trace name for kernel tracig */
503 } else if (opt_trace_name
!= NULL
&& opt_trace_kernel
) {
504 ERR("For action on a kernel trace, please don't specify a trace name.");
506 } else if (opt_destroy_trace
&& opt_session_name
== NULL
) {
507 ERR("Please specify a session in order to destroy a trace");
509 } else if (opt_create_trace
|| opt_destroy_trace
) {
510 /* Both kernel and user-space are denied for these options */
511 if (opt_trace_pid
!= 0 && opt_trace_kernel
) {
512 ERR("Kernel and user-space trace creation and destruction can't be used together.");
514 /* Need a trace name for user-space tracing */
515 } else if (opt_trace_name
== NULL
&& opt_trace_pid
!= 0) {
516 ERR("Please specify a trace name for user-space tracing");
519 } else if (opt_stop_trace
&& opt_trace_pid
!= 0 && opt_trace_name
== NULL
) {
520 ERR("Please specify a trace name for user-space tracing");
524 /* If start trace, auto start tracing */
525 if (opt_start_trace
|| opt_event_list
!= NULL
) {
526 DBG("Requesting auto tracing");
530 /* If no session, auto create one */
531 if (opt_session_name
== NULL
) {
532 DBG("Requesting an auto session creation");
546 * Spawn a session daemon by forking and execv.
548 static int spawn_sessiond(char *pathname
)
553 MSG("Spawning session daemon");
557 * Spawn session daemon and tell
558 * it to signal us when ready.
560 execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
561 /* execlp only returns if error happened */
562 if (errno
== ENOENT
) {
563 ERR("No session daemon found. Use --sessiond-path.");
567 kill(getppid(), SIGTERM
); /* unpause parent */
569 } else if (pid
> 0) {
570 /* Wait for ltt-sessiond to start */
586 * Check if the session daemon is available using
587 * the liblttngctl API for the check. If not, try to
590 static int check_ltt_sessiond(void)
593 char *pathname
= NULL
, *alloc_pathname
= NULL
;
595 ret
= lttng_check_session_daemon();
597 /* Try command line option path */
598 if (opt_sessiond_path
!= NULL
) {
599 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
601 ERR("No such file: %s", opt_sessiond_path
);
604 pathname
= opt_sessiond_path
;
606 /* Try LTTNG_SESSIOND_PATH env variable */
607 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
610 /* Let's rock and roll */
611 if (pathname
== NULL
) {
612 ret
= asprintf(&alloc_pathname
, "ltt-sessiond");
616 pathname
= alloc_pathname
;
619 ret
= spawn_sessiond(pathname
);
620 free(alloc_pathname
);
622 ERR("Problem occurs when starting %s", pathname
);
634 * Setup signal handler for SIGCHLD and SIGTERM.
636 static int set_signal_handler(void)
642 if ((ret
= sigemptyset(&sigset
)) < 0) {
643 perror("sigemptyset");
647 sa
.sa_handler
= sighandler
;
650 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
655 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
667 * Signal handler for the daemon
669 static void sighandler(int sig
)
673 DBG("SIGTERM catched");
674 clean_exit(EXIT_FAILURE
);
678 DBG("SIGCHLD catched");
681 DBG("Unknown signal %d catched", sig
);
691 void clean_exit(int code
)
704 int main(int argc
, char *argv
[])
708 progname
= argv
[0] ? argv
[0] : "lttng";
710 /* For Mathieu Desnoyers aka Dr Tracing */
711 if (strncmp(progname
, "drtrace", 7) == 0) {
712 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
715 ret
= parse_args(argc
, (const char **) argv
);
717 clean_exit(EXIT_FAILURE
);
720 ret
= validate_options();
725 ret
= set_signal_handler();
730 if (opt_tracing_group
!= NULL
) {
731 DBG("Set tracing group to '%s'", opt_tracing_group
);
732 lttng_set_tracing_group(opt_tracing_group
);
735 /* If ask for kernel tracing, need root perms */
736 if (opt_trace_kernel
) {
737 DBG("Kernel tracing activated");
739 ERR("%s must be setuid root", progname
);
744 /* Check if the lttng session daemon is running.
745 * If no, a daemon will be spawned.
747 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
748 clean_exit(EXIT_FAILURE
);
751 ret
= process_client_opt();
This page took 0.046315 seconds and 4 git commands to generate.