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 short_uuid
[9];
43 static int process_client_opt(void);
44 static int process_opt_list_apps(void);
45 static int process_opt_list_sessions(void);
46 static int process_opt_list_traces(void);
47 static int process_opt_create_session(void);
48 static int process_opt_session_uuid(void);
49 static void sighandler(int sig
);
50 static void shorten_uuid(char *in
, char *out
);
51 static int set_signal_handler(void);
52 static int validate_options(void);
53 static char *get_cmdline_by_pid(pid_t pid
);
58 * Process client request from the command line
59 * options. Every tracing action is done by the
62 static int process_client_opt(void)
68 ret
= process_opt_list_apps();
74 if (opt_list_session
) {
75 ret
= process_opt_list_sessions();
81 if (opt_list_traces
) {
82 ret
= process_opt_list_traces();
88 if (opt_create_session
!= NULL
) {
89 ret
= process_opt_create_session();
95 if (opt_destroy_session
!= NULL
) {
96 uuid_parse(opt_destroy_session
, uuid
);
97 ret
= lttng_destroy_session(&uuid
);
103 if (opt_session_uuid
!= NULL
) {
104 DBG("Set session uuid to %s", short_uuid
);
105 ret
= process_opt_session_uuid();
107 ERR("Session UUID %s not found", opt_session_uuid
);
112 if (opt_trace_kernel
) {
113 ERR("Not implemented yet");
117 if (opt_trace_pid
!= 0) {
118 if (opt_create_trace
) {
119 DBG("Create a userspace trace for pid %d", opt_trace_pid
);
120 ret
= lttng_ust_create_trace(opt_trace_pid
);
124 MSG("Trace created successfully!\nUse --start to start tracing.");
127 if (opt_start_trace
) {
128 DBG("Start trace for pid %d", opt_trace_pid
);
129 ret
= lttng_ust_start_trace(opt_trace_pid
);
133 MSG("Trace started successfully!");
134 } else if (opt_stop_trace
) {
135 DBG("Stop trace for pid %d", opt_trace_pid
);
136 ret
= lttng_ust_stop_trace(opt_trace_pid
);
140 MSG("Trace stopped successfully!");
148 ERR("%s", lttng_get_readable_code(ret
));
156 * process_opt_session_uuid
158 * Set current session uuid to the current flow of
159 * command(s) using the already shorten uuid.
161 static int process_opt_session_uuid(void)
164 struct lttng_session
*sessions
;
166 count
= lttng_list_sessions(&sessions
);
172 for (i
= 0; i
< count
; i
++) {
173 if (strncmp(sessions
[i
].uuid
, short_uuid
, 8) == 0) {
174 lttng_set_current_session_uuid(sessions
[i
].uuid
);
188 * process_opt_list_traces
190 * Get list of all traces for a specific session uuid.
192 static int process_opt_list_traces(void)
196 struct lttng_trace
*traces
;
198 uuid_parse(opt_session_uuid
, uuid
);
199 ret
= lttng_list_traces(&uuid
, &traces
);
204 MSG("Userspace traces:");
205 for (i
= 0; i
< ret
; i
++) {
206 if (traces
[i
].type
== USERSPACE
) {
207 MSG("\t%d) %s (pid: %d): %s",
208 i
, traces
[i
].name
, traces
[i
].pid
,
209 get_cmdline_by_pid(traces
[i
].pid
));
215 MSG("Kernel traces:");
216 for (;i
< ret
; i
++) {
217 if (traces
[i
].type
== KERNEL
) {
218 MSG("\t%d) %s", i
, traces
[i
].name
);
229 * process_opt_create_session
231 * Create a new session using the name pass
232 * to the command line.
234 static int process_opt_create_session(void)
240 ret
= lttng_create_session(opt_create_session
, &session_id
);
245 uuid_unparse(session_id
, str_uuid
);
247 MSG("Session created:");
248 MSG(" %s (%s)", opt_create_session
, str_uuid
);
257 * Extract shorten uuid and copy it to out.
258 * Shorten uuid format : '<name>.<short_uuid>'
260 static int extract_short_uuid(char *in
, char *out
)
264 tok
= strchr(in
, '.');
265 if (strlen(tok
+1) == 8) {
266 memcpy(out
, tok
+1, 8);
277 * Small function to shorten the 37 bytes long uuid_t
278 * string representation to 8 characters.
280 static void shorten_uuid(char *in
, char *out
)
287 * process_opt_list_sessions
289 * Get the list of available sessions from
290 * the session daemon and print it to user.
292 static int process_opt_list_sessions(void)
295 char tmp_short_uuid
[9];
296 struct lttng_session
*sess
;
298 count
= lttng_list_sessions(&sess
);
299 DBG("Session count %d", count
);
305 MSG("Available sessions (UUIDs):");
306 for (i
= 0; i
< count
; i
++) {
307 shorten_uuid(sess
[i
].uuid
, tmp_short_uuid
);
308 MSG(" %d) %s.%s", i
+1, sess
[i
].name
, tmp_short_uuid
);
312 MSG("\nTo select a session, use -s, --session UUID.");
321 * process_opt_list_apps
323 * Get the UST traceable pid list and print
326 static int process_opt_list_apps(void)
332 count
= lttng_ust_list_apps(&pids
);
338 MSG("LTTng UST traceable application [name (pid)]:");
339 for (i
=0; i
< count
; i
++) {
340 cmdline
= get_cmdline_by_pid(pids
[i
]);
341 if (cmdline
== NULL
) {
342 MSG("\t(not running) (%d)", pids
[i
]);
345 MSG("\t%s (%d)", cmdline
, pids
[i
]);
349 /* Allocated by lttng_ust_list_apps() */
361 * Get command line from /proc for a specific pid.
363 * On success, return an allocated string pointer pointing to
365 * On error, return NULL.
367 static char *get_cmdline_by_pid(pid_t pid
)
371 char *cmdline
= NULL
;
372 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
374 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
375 fp
= fopen(path
, "r");
380 /* Caller must free() *cmdline */
381 cmdline
= malloc(PATH_MAX
);
382 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
392 * Make sure that all options passed to the command line
393 * are compatible with each others.
395 * On error, return -1
396 * On success, return 0
398 static int validate_options(void)
402 /* Conflicting command */
403 if (opt_start_trace
&& opt_stop_trace
) {
404 ERR("Can't use --start and --stop together.");
406 /* Must have a session UUID for trace action. */
407 } else if ((opt_session_uuid
== NULL
) &&
408 (opt_create_trace
|| opt_start_trace
|| opt_stop_trace
|| opt_list_traces
)) {
409 ERR("You need to specify a session UUID.\nPlease use --session UUID to do so.");
411 /* If no PID specified and trace_kernel is off */
412 } else if ((opt_trace_pid
== 0 && opt_trace_kernel
== 0) &&
413 (opt_create_trace
|| opt_start_trace
|| opt_stop_trace
)) {
414 ERR("Please specify a PID using -p, --pid PID.");
418 if (opt_session_uuid
!= NULL
) {
419 ret
= extract_short_uuid(opt_session_uuid
, short_uuid
);
421 ERR("Session UUID not valid. Must be <name>.<short_uuid>");
435 * Spawn a session daemon by forking and execv.
437 static int spawn_sessiond(char *pathname
)
442 MSG("Spawning session daemon");
445 /* Spawn session daemon and tell
446 * it to signal us when ready.
448 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
450 if (errno
== ENOENT
) {
451 ERR("No session daemon found. Use --sessiond-path.");
455 kill(getppid(), SIGTERM
);
459 } else if (pid
> 0) {
460 /* Wait for ltt-sessiond to start */
476 * Check if the session daemon is available using
477 * the liblttngctl API for the check. If not, try to
480 static int check_ltt_sessiond(void)
483 char *pathname
= NULL
;
485 ret
= lttng_check_session_daemon();
487 /* Try command line option path */
488 if (opt_sessiond_path
!= NULL
) {
489 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
491 ERR("No such file: %s", opt_sessiond_path
);
494 pathname
= opt_sessiond_path
;
496 /* Try LTTNG_SESSIOND_PATH env variable */
497 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
498 if (pathname
!= NULL
) {
499 /* strdup here in order to make the free()
502 pathname
= strdup(pathname
);
506 /* Let's rock and roll */
507 if (pathname
== NULL
) {
508 ret
= asprintf(&pathname
, "ltt-sessiond");
514 ret
= spawn_sessiond(pathname
);
517 ERR("Problem occurs when starting %s", pathname
);
529 * Setup signal handler for SIGCHLD and SIGTERM.
531 static int set_signal_handler(void)
537 if ((ret
= sigemptyset(&sigset
)) < 0) {
538 perror("sigemptyset");
542 sa
.sa_handler
= sighandler
;
545 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
550 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
562 * Signal handler for the daemon
564 static void sighandler(int sig
)
568 DBG("SIGTERM catched");
569 clean_exit(EXIT_FAILURE
);
573 DBG("SIGCHLD catched");
576 DBG("Unknown signal %d catched", sig
);
586 void clean_exit(int code
)
595 int main(int argc
, char *argv
[])
599 progname
= argv
[0] ? argv
[0] : "lttng";
601 /* For Mathieu Desnoyers aka Dr Tracing */
602 if (strncmp(progname
, "drtrace", 7) == 0) {
603 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
606 ret
= parse_args(argc
, (const char **) argv
);
608 clean_exit(EXIT_FAILURE
);
611 ret
= validate_options();
616 ret
= set_signal_handler();
621 if (opt_tracing_group
!= NULL
) {
622 DBG("Set tracing group to '%s'", opt_tracing_group
);
623 lttng_set_tracing_group(opt_tracing_group
);
626 /* If ask for kernel tracing, need root perms */
627 if (opt_trace_kernel
) {
628 DBG("Kernel tracing activated");
630 ERR("%s must be setuid root", progname
);
635 /* Check if the lttng session daemon is running.
636 * If no, a daemon will be spawned.
638 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
639 clean_exit(EXIT_FAILURE
);
642 ret
= process_client_opt();
This page took 0.043136 seconds and 4 git commands to generate.