e538df2fd403f0eefaf2be2c58b3c8a04973067d
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
);
49 static int validate_options(void);
54 * Process client request from the command line
55 * options. Every tracing action is done by the
58 static int process_client_opt(void)
63 /* Connect to the session daemon */
64 ret
= lttng_connect_sessiond();
70 ret
= process_opt_list_apps();
76 if (opt_list_session
) {
77 ret
= process_opt_list_sessions();
83 if (opt_create_session
!= NULL
) {
84 ret
= process_opt_create_session();
90 if (opt_destroy_session
!= NULL
) {
91 uuid_parse(opt_destroy_session
, uuid
);
92 ret
= lttng_destroy_session(&uuid
);
98 if (opt_session_uuid
!= NULL
) {
99 DBG("Set session uuid to %s", opt_session_uuid
);
100 lttng_set_current_session_uuid(opt_session_uuid
);
103 if (opt_create_trace
) {
104 DBG("Create trace for pid %d", opt_create_trace
);
105 ret
= lttng_ust_create_trace(opt_create_trace
);
109 MSG("Trace created successfully!\nUse --start PID to start tracing.");
112 if (opt_start_trace
) {
113 DBG("Start trace for pid %d", opt_start_trace
);
114 ret
= lttng_ust_start_trace(opt_start_trace
);
118 MSG("Trace started successfully!");
124 ERR("%s", lttng_get_readable_code(ret
));
129 * process_opt_create_session
131 * Create a new session using the name pass
132 * to the command line.
134 static int process_opt_create_session(void)
140 ret
= lttng_create_session(opt_create_session
, &session_id
);
145 uuid_unparse(session_id
, str_uuid
);
147 MSG("Session created:");
148 MSG(" %s (%s)", opt_create_session
, str_uuid
);
155 * process_opt_list_sessions
157 * Get the list of available sessions from
158 * the session daemon and print it to user.
160 static int process_opt_list_sessions(void)
163 struct lttng_session
*sess
;
165 count
= lttng_list_sessions(&sess
);
171 MSG("Available sessions [Name (uuid)]:");
172 for (i
= 0; i
< count
; i
++) {
173 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
177 MSG("\nTo select a session, use --session UUID.");
186 * process_opt_list_apps
188 * Get the UST traceable pid list and print
191 static int process_opt_list_apps(void)
197 count
= lttng_ust_list_apps(&pids
);
203 MSG("LTTng UST traceable application [name (pid)]:");
204 for (i
=0; i
< count
; i
++) {
205 ret
= get_cmdline_by_pid(pids
[i
], &cmdline
);
207 MSG("\t(not running) (%d)", pids
[i
]);
210 MSG("\t%s (%d)", cmdline
, pids
[i
]);
214 /* Allocated by lttng_ust_list_apps() */
226 * Get command line from /proc for a
227 * specific pid. Allocate cmdline so the
228 * user must free() that pointer.
230 * On success, return 1
231 * On error (not found), return 0
233 static int get_cmdline_by_pid(pid_t pid
, char **cmdline
)
237 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
239 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
240 fp
= fopen(path
, "r");
245 /* Caller must free() *cmdline */
246 *cmdline
= malloc(PATH_MAX
);
247 ret
= fread(*cmdline
, 1, PATH_MAX
, fp
);
259 * Make sure that all options passed to the command line
260 * are compatible with each others.
262 * On error, return -1
263 * On success, return 0
265 static int validate_options(void)
267 if ((opt_session_uuid
== NULL
) &&
268 (opt_create_trace
|| opt_start_trace
)) {
269 ERR("Can't act on trace without a session ID.\nPlease specify using --session UUID");
282 * Spawn a session daemon by forking and execv.
284 static int spawn_sessiond(char *pathname
)
289 MSG("Spawning session daemon");
292 /* Spawn session daemon and tell
293 * it to signal us when ready.
295 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
297 if (errno
== ENOENT
) {
298 ERR("No session daemon found. Use --sessiond-path.");
302 kill(getppid(), SIGTERM
);
306 } else if (pid
> 0) {
307 /* Wait for ltt-sessiond to start */
323 * Check if the session daemon is available using
324 * the liblttngctl API for the check. If not, try to
327 static int check_ltt_sessiond(void)
330 char *pathname
= NULL
;
332 ret
= lttng_check_session_daemon();
334 /* Try command line option path */
335 if (opt_sessiond_path
!= NULL
) {
336 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
338 ERR("No such file: %s", opt_sessiond_path
);
341 pathname
= opt_sessiond_path
;
343 /* Try LTTNG_SESSIOND_PATH env variable */
344 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
345 if (pathname
!= NULL
) {
346 /* strdup here in order to make the free()
349 pathname
= strdup(pathname
);
353 /* Let's rock and roll */
354 if (pathname
== NULL
) {
355 ret
= asprintf(&pathname
, "ltt-sessiond");
361 ret
= spawn_sessiond(pathname
);
364 ERR("Problem occurs when starting %s", pathname
);
376 * Setup signal handler for SIGCHLD and SIGTERM.
378 static int set_signal_handler(void)
384 if ((ret
= sigemptyset(&sigset
)) < 0) {
385 perror("sigemptyset");
389 sa
.sa_handler
= sighandler
;
392 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
397 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
409 * Signal handler for the daemon
411 static void sighandler(int sig
)
415 DBG("SIGTERM catched");
416 clean_exit(EXIT_FAILURE
);
420 DBG("SIGCHLD catched");
423 DBG("Unknown signal %d catched", sig
);
432 void clean_exit(int code
)
435 if (lttng_disconnect_sessiond() < 0) {
436 ERR("Session daemon disconnect failed.");
444 int main(int argc
, char *argv
[])
448 progname
= argv
[0] ? argv
[0] : "lttng";
450 /* For Mathieu Desnoyers aka Dr Tracing */
451 if (strncmp(progname
, "drtrace", 7) == 0) {
452 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
455 ret
= parse_args(argc
, (const char **) argv
);
457 clean_exit(EXIT_FAILURE
);
460 ret
= validate_options();
465 ret
= set_signal_handler();
470 if (opt_tracing_group
!= NULL
) {
471 DBG("Set tracing group to '%s'", opt_tracing_group
);
472 lttng_set_tracing_group(opt_tracing_group
);
475 /* If ask for kernel tracing, need root perms */
476 if (opt_trace_kernel
) {
477 DBG("Kernel tracing activated");
479 ERR("%s must be setuid root", progname
);
484 /* Check if the lttng session daemon is running.
485 * If no, a daemon will be spawned.
487 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
488 clean_exit(EXIT_FAILURE
);
491 ret
= process_client_opt();
This page took 0.039623 seconds and 5 git commands to generate.