8a0d58faeaa8ac7ec7299a7a633d9ada73e8e513
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_list_traces(void);
46 static int process_opt_create_session(void);
47 static void sighandler(int sig
);
48 static int set_signal_handler(void);
49 static int validate_options(void);
50 static char *get_cmdline_by_pid(pid_t pid
);
55 * Process client request from the command line
56 * options. Every tracing action is done by the
59 static int process_client_opt(void)
64 /* Connect to the session daemon */
65 ret
= lttng_connect_sessiond();
71 ret
= process_opt_list_apps();
77 if (opt_list_session
) {
78 ret
= process_opt_list_sessions();
84 if (opt_list_traces
) {
85 ret
= process_opt_list_traces();
91 if (opt_create_session
!= NULL
) {
92 ret
= process_opt_create_session();
98 if (opt_destroy_session
!= NULL
) {
99 uuid_parse(opt_destroy_session
, uuid
);
100 ret
= lttng_destroy_session(&uuid
);
106 if (opt_session_uuid
!= NULL
) {
107 DBG("Set session uuid to %s", opt_session_uuid
);
108 lttng_set_current_session_uuid(opt_session_uuid
);
111 if (opt_create_trace
) {
112 DBG("Create trace for pid %d", opt_create_trace
);
113 ret
= lttng_ust_create_trace(opt_create_trace
);
117 MSG("Trace created successfully!\nUse --start PID to start tracing.");
120 if (opt_start_trace
) {
121 DBG("Start trace for pid %d", opt_start_trace
);
122 ret
= lttng_ust_start_trace(opt_start_trace
);
126 MSG("Trace started successfully!");
129 if (opt_stop_trace
) {
130 DBG("Stop trace for pid %d", opt_stop_trace
);
131 ret
= lttng_ust_stop_trace(opt_stop_trace
);
135 MSG("Trace stopped successfully!");
141 ERR("%s", lttng_get_readable_code(ret
));
146 * process_opt_list_traces
148 * Get list of all traces for a specific session uuid.
150 static int process_opt_list_traces(void)
154 struct lttng_trace
*traces
;
156 uuid_parse(opt_session_uuid
, uuid
);
157 ret
= lttng_list_traces(&uuid
, &traces
);
162 MSG("Userspace traces:");
163 for (i
= 0; i
< ret
; i
++) {
164 if (traces
[i
].type
== USERSPACE
) {
165 MSG("\t%d) %s (pid: %d): %s",
166 i
, traces
[i
].name
, traces
[i
].pid
,
167 get_cmdline_by_pid(traces
[i
].pid
));
173 MSG("Kernel traces:");
174 for (;i
< ret
; i
++) {
175 if (traces
[i
].type
== KERNEL
) {
176 MSG("\t%d) %s", i
, traces
[i
].name
);
187 * process_opt_create_session
189 * Create a new session using the name pass
190 * to the command line.
192 static int process_opt_create_session(void)
198 ret
= lttng_create_session(opt_create_session
, &session_id
);
203 uuid_unparse(session_id
, str_uuid
);
205 MSG("Session created:");
206 MSG(" %s (%s)", opt_create_session
, str_uuid
);
213 * process_opt_list_sessions
215 * Get the list of available sessions from
216 * the session daemon and print it to user.
218 static int process_opt_list_sessions(void)
221 struct lttng_session
*sess
;
223 count
= lttng_list_sessions(&sess
);
229 MSG("Available sessions [Name (uuid)]:");
230 for (i
= 0; i
< count
; i
++) {
231 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
235 MSG("\nTo select a session, use --session UUID.");
244 * process_opt_list_apps
246 * Get the UST traceable pid list and print
249 static int process_opt_list_apps(void)
255 count
= lttng_ust_list_apps(&pids
);
261 MSG("LTTng UST traceable application [name (pid)]:");
262 for (i
=0; i
< count
; i
++) {
263 cmdline
= get_cmdline_by_pid(pids
[i
]);
264 if (cmdline
== NULL
) {
265 MSG("\t(not running) (%d)", pids
[i
]);
268 MSG("\t%s (%d)", cmdline
, pids
[i
]);
272 /* Allocated by lttng_ust_list_apps() */
284 * Get command line from /proc for a specific pid.
286 * On success, return an allocated string pointer pointing to
288 * On error, return NULL.
290 static char *get_cmdline_by_pid(pid_t pid
)
294 char *cmdline
= NULL
;
295 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
297 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
298 fp
= fopen(path
, "r");
303 /* Caller must free() *cmdline */
304 cmdline
= malloc(PATH_MAX
);
305 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
315 * Make sure that all options passed to the command line
316 * are compatible with each others.
318 * On error, return -1
319 * On success, return 0
321 static int validate_options(void)
323 if ((opt_session_uuid
== NULL
) &&
324 (opt_create_trace
|| opt_start_trace
|| opt_list_traces
)) {
325 ERR("You need to specify a session UUID.\nPlease use --session UUID to do so.");
338 * Spawn a session daemon by forking and execv.
340 static int spawn_sessiond(char *pathname
)
345 MSG("Spawning session daemon");
348 /* Spawn session daemon and tell
349 * it to signal us when ready.
351 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
353 if (errno
== ENOENT
) {
354 ERR("No session daemon found. Use --sessiond-path.");
358 kill(getppid(), SIGTERM
);
362 } else if (pid
> 0) {
363 /* Wait for ltt-sessiond to start */
379 * Check if the session daemon is available using
380 * the liblttngctl API for the check. If not, try to
383 static int check_ltt_sessiond(void)
386 char *pathname
= NULL
;
388 ret
= lttng_check_session_daemon();
390 /* Try command line option path */
391 if (opt_sessiond_path
!= NULL
) {
392 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
394 ERR("No such file: %s", opt_sessiond_path
);
397 pathname
= opt_sessiond_path
;
399 /* Try LTTNG_SESSIOND_PATH env variable */
400 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
401 if (pathname
!= NULL
) {
402 /* strdup here in order to make the free()
405 pathname
= strdup(pathname
);
409 /* Let's rock and roll */
410 if (pathname
== NULL
) {
411 ret
= asprintf(&pathname
, "ltt-sessiond");
417 ret
= spawn_sessiond(pathname
);
420 ERR("Problem occurs when starting %s", pathname
);
432 * Setup signal handler for SIGCHLD and SIGTERM.
434 static int set_signal_handler(void)
440 if ((ret
= sigemptyset(&sigset
)) < 0) {
441 perror("sigemptyset");
445 sa
.sa_handler
= sighandler
;
448 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
453 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
465 * Signal handler for the daemon
467 static void sighandler(int sig
)
471 DBG("SIGTERM catched");
472 clean_exit(EXIT_FAILURE
);
476 DBG("SIGCHLD catched");
479 DBG("Unknown signal %d catched", sig
);
488 void clean_exit(int code
)
491 if (lttng_disconnect_sessiond() < 0) {
492 ERR("Session daemon disconnect failed.");
500 int main(int argc
, char *argv
[])
504 progname
= argv
[0] ? argv
[0] : "lttng";
506 /* For Mathieu Desnoyers aka Dr Tracing */
507 if (strncmp(progname
, "drtrace", 7) == 0) {
508 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
511 ret
= parse_args(argc
, (const char **) argv
);
513 clean_exit(EXIT_FAILURE
);
516 ret
= validate_options();
521 ret
= set_signal_handler();
526 if (opt_tracing_group
!= NULL
) {
527 DBG("Set tracing group to '%s'", opt_tracing_group
);
528 lttng_set_tracing_group(opt_tracing_group
);
531 /* If ask for kernel tracing, need root perms */
532 if (opt_trace_kernel
) {
533 DBG("Kernel tracing activated");
535 ERR("%s must be setuid root", progname
);
540 /* Check if the lttng session daemon is running.
541 * If no, a daemon will be spawned.
543 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
544 clean_exit(EXIT_FAILURE
);
547 ret
= process_client_opt();
This page took 0.069591 seconds and 4 git commands to generate.