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/liblttngctl.h>
39 static char *progname
;
42 static int process_client_opt(void);
43 static int process_opt_list_apps(void);
44 static int process_opt_list_sessions(void);
45 static int process_opt_create_session(void);
46 static void sighandler(int sig
);
47 static int set_signal_handler(void);
48 static int get_cmdline_by_pid(pid_t pid
, char **cmdline
);
53 * Process client request from the command line
54 * options. Every tracing action is done by the
57 static int process_client_opt(void)
62 /* Connect to the session daemon */
63 ret
= lttng_connect_sessiond();
69 ret
= process_opt_list_apps();
75 if (opt_list_session
) {
76 ret
= process_opt_list_sessions();
82 if (opt_create_session
!= NULL
) {
83 ret
= process_opt_create_session();
89 if (opt_destroy_session
!= NULL
) {
90 uuid_parse(opt_destroy_session
, uuid
);
91 ret
= lttng_destroy_session(&uuid
);
97 if (opt_session_uuid
!= NULL
) {
98 lttng_set_current_session_uuid(opt_session_uuid
);
101 if (opt_create_trace
) {
102 DBG("Create trace for pid %d", opt_create_trace
);
103 ret
= lttng_ust_create_trace(opt_create_trace
);
107 MSG("Trace created successfully!\nUse --start PID to start tracing");
113 ERR("%s", lttng_get_readable_code(ret
));
118 * process_opt_create_session
120 * Create a new session using the name pass
121 * to the command line.
123 static int process_opt_create_session(void)
129 ret
= lttng_create_session(opt_create_session
, &session_id
);
134 uuid_unparse(session_id
, str_uuid
);
136 MSG("Session created:");
137 MSG(" %s (%s)", opt_create_session
, str_uuid
);
144 * process_opt_list_sessions
146 * Get the list of available sessions from
147 * the session daemon and print it to user.
149 static int process_opt_list_sessions(void)
152 struct lttng_session
*sess
;
154 count
= lttng_list_sessions(&sess
);
160 MSG("Available sessions [Name (uuid)]:");
161 for (i
= 0; i
< count
; i
++) {
162 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
166 MSG("\nTo select a session, use --session UUID.");
175 * process_opt_list_apps
177 * Get the UST traceable pid list and print
180 static int process_opt_list_apps(void)
186 count
= lttng_ust_list_apps(&pids
);
192 MSG("LTTng UST traceable application [name (pid)]:");
193 for (i
=0; i
< count
; i
++) {
194 ret
= get_cmdline_by_pid(pids
[i
], &cmdline
);
196 MSG("\t(not running) (%d)", pids
[i
]);
199 MSG("\t%s (%d)", cmdline
, pids
[i
]);
203 /* Allocated by lttng_ust_list_apps() */
215 * Get command line from /proc for a
216 * specific pid. Allocate cmdline so the
217 * user must free() that pointer.
219 * On success, return 1
220 * On error (not found), return 0
222 static int get_cmdline_by_pid(pid_t pid
, char **cmdline
)
226 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
228 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
229 fp
= fopen(path
, "r");
234 /* Caller must free() *cmdline */
235 *cmdline
= malloc(PATH_MAX
);
236 ret
= fread(*cmdline
, 1, PATH_MAX
, fp
);
248 * Spawn a session daemon by forking and execv.
250 static int spawn_sessiond(char *pathname
)
255 MSG("Spawning session daemon");
258 /* Spawn session daemon and tell
259 * it to signal us when ready.
261 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
263 if (errno
== ENOENT
) {
264 ERR("No session daemon found. Use --sessiond-path.");
268 kill(getppid(), SIGTERM
);
272 } else if (pid
> 0) {
273 /* Wait for ltt-sessiond to start */
289 * Check if the session daemon is available using
290 * the liblttngctl API for the check. If not, try to
293 static int check_ltt_sessiond(void)
296 char *pathname
= NULL
;
298 ret
= lttng_check_session_daemon();
300 /* Try command line option path */
301 if (opt_sessiond_path
!= NULL
) {
302 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
304 ERR("No such file: %s", opt_sessiond_path
);
307 pathname
= opt_sessiond_path
;
309 /* Try LTTNG_SESSIOND_PATH env variable */
310 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
311 if (pathname
!= NULL
) {
312 /* strdup here in order to make the free()
315 pathname
= strdup(pathname
);
319 /* Let's rock and roll */
320 if (pathname
== NULL
) {
321 ret
= asprintf(&pathname
, "ltt-sessiond");
327 ret
= spawn_sessiond(pathname
);
330 ERR("Problem occurs when starting %s", pathname
);
342 * Setup signal handler for SIGCHLD and SIGTERM.
344 static int set_signal_handler(void)
350 if ((ret
= sigemptyset(&sigset
)) < 0) {
351 perror("sigemptyset");
355 sa
.sa_handler
= sighandler
;
358 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
363 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
375 * Signal handler for the daemon
377 static void sighandler(int sig
)
379 DBG("%d received", sig
);
382 clean_exit(EXIT_FAILURE
);
396 void clean_exit(int code
)
399 if (lttng_disconnect_sessiond() < 0) {
400 ERR("Session daemon disconnect failed.");
408 int main(int argc
, char *argv
[])
412 progname
= argv
[0] ? argv
[0] : "lttng";
414 /* For Mathieu Desnoyers aka Dr Tracing */
415 if (strncmp(progname
, "drtrace", 7) == 0) {
416 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
419 ret
= parse_args(argc
, (const char **) argv
);
421 clean_exit(EXIT_FAILURE
);
424 ret
= set_signal_handler();
429 if (opt_tracing_group
!= NULL
) {
430 DBG("Set tracing group to '%s'", opt_tracing_group
);
431 lttng_set_tracing_group(opt_tracing_group
);
434 /* If ask for kernel tracing, need root perms */
435 if (opt_trace_kernel
) {
436 DBG("Kernel tracing activated");
438 ERR("%s must be setuid root", progname
);
443 /* Check if the lttng session daemon is running.
444 * If no, a daemon will be spawned.
446 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
447 clean_exit(EXIT_FAILURE
);
450 ret
= process_client_opt();
This page took 0.038749 seconds and 4 git commands to generate.