e286917ed1effff58afaacb91c1f8bcb780d7de4
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!");
132 ERR("%s", lttng_get_readable_code(ret
));
137 * process_opt_list_traces
139 * Get list of all traces for a specific session uuid.
141 static int process_opt_list_traces(void)
145 struct lttng_trace
*traces
;
147 uuid_parse(opt_session_uuid
, uuid
);
148 ret
= lttng_list_traces(&uuid
, &traces
);
153 MSG("Userspace traces:");
154 for (i
= 0; i
< ret
; i
++) {
155 if (traces
[i
].type
== USERSPACE
) {
156 MSG("\t%d) %s (pid: %d): %s",
157 i
, traces
[i
].name
, traces
[i
].pid
,
158 get_cmdline_by_pid(traces
[i
].pid
));
164 MSG("Kernel traces:");
165 for (;i
< ret
; i
++) {
166 if (traces
[i
].type
== KERNEL
) {
167 MSG("\t%d) %s", i
, traces
[i
].name
);
178 * process_opt_create_session
180 * Create a new session using the name pass
181 * to the command line.
183 static int process_opt_create_session(void)
189 ret
= lttng_create_session(opt_create_session
, &session_id
);
194 uuid_unparse(session_id
, str_uuid
);
196 MSG("Session created:");
197 MSG(" %s (%s)", opt_create_session
, str_uuid
);
204 * process_opt_list_sessions
206 * Get the list of available sessions from
207 * the session daemon and print it to user.
209 static int process_opt_list_sessions(void)
212 struct lttng_session
*sess
;
214 count
= lttng_list_sessions(&sess
);
220 MSG("Available sessions [Name (uuid)]:");
221 for (i
= 0; i
< count
; i
++) {
222 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
226 MSG("\nTo select a session, use --session UUID.");
235 * process_opt_list_apps
237 * Get the UST traceable pid list and print
240 static int process_opt_list_apps(void)
246 count
= lttng_ust_list_apps(&pids
);
252 MSG("LTTng UST traceable application [name (pid)]:");
253 for (i
=0; i
< count
; i
++) {
254 cmdline
= get_cmdline_by_pid(pids
[i
]);
255 if (cmdline
== NULL
) {
256 MSG("\t(not running) (%d)", pids
[i
]);
259 MSG("\t%s (%d)", cmdline
, pids
[i
]);
263 /* Allocated by lttng_ust_list_apps() */
275 * Get command line from /proc for a specific pid.
277 * On success, return an allocated string pointer pointing to
279 * On error, return NULL.
281 static char *get_cmdline_by_pid(pid_t pid
)
285 char *cmdline
= NULL
;
286 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
288 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
289 fp
= fopen(path
, "r");
294 /* Caller must free() *cmdline */
295 cmdline
= malloc(PATH_MAX
);
296 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
306 * Make sure that all options passed to the command line
307 * are compatible with each others.
309 * On error, return -1
310 * On success, return 0
312 static int validate_options(void)
314 if ((opt_session_uuid
== NULL
) &&
315 (opt_create_trace
|| opt_start_trace
|| opt_list_traces
)) {
316 ERR("You need to specify a session UUID.\nPlease use --session UUID to do so.");
329 * Spawn a session daemon by forking and execv.
331 static int spawn_sessiond(char *pathname
)
336 MSG("Spawning session daemon");
339 /* Spawn session daemon and tell
340 * it to signal us when ready.
342 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
344 if (errno
== ENOENT
) {
345 ERR("No session daemon found. Use --sessiond-path.");
349 kill(getppid(), SIGTERM
);
353 } else if (pid
> 0) {
354 /* Wait for ltt-sessiond to start */
370 * Check if the session daemon is available using
371 * the liblttngctl API for the check. If not, try to
374 static int check_ltt_sessiond(void)
377 char *pathname
= NULL
;
379 ret
= lttng_check_session_daemon();
381 /* Try command line option path */
382 if (opt_sessiond_path
!= NULL
) {
383 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
385 ERR("No such file: %s", opt_sessiond_path
);
388 pathname
= opt_sessiond_path
;
390 /* Try LTTNG_SESSIOND_PATH env variable */
391 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
392 if (pathname
!= NULL
) {
393 /* strdup here in order to make the free()
396 pathname
= strdup(pathname
);
400 /* Let's rock and roll */
401 if (pathname
== NULL
) {
402 ret
= asprintf(&pathname
, "ltt-sessiond");
408 ret
= spawn_sessiond(pathname
);
411 ERR("Problem occurs when starting %s", pathname
);
423 * Setup signal handler for SIGCHLD and SIGTERM.
425 static int set_signal_handler(void)
431 if ((ret
= sigemptyset(&sigset
)) < 0) {
432 perror("sigemptyset");
436 sa
.sa_handler
= sighandler
;
439 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
444 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
456 * Signal handler for the daemon
458 static void sighandler(int sig
)
462 DBG("SIGTERM catched");
463 clean_exit(EXIT_FAILURE
);
467 DBG("SIGCHLD catched");
470 DBG("Unknown signal %d catched", sig
);
479 void clean_exit(int code
)
482 if (lttng_disconnect_sessiond() < 0) {
483 ERR("Session daemon disconnect failed.");
491 int main(int argc
, char *argv
[])
495 progname
= argv
[0] ? argv
[0] : "lttng";
497 /* For Mathieu Desnoyers aka Dr Tracing */
498 if (strncmp(progname
, "drtrace", 7) == 0) {
499 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
502 ret
= parse_args(argc
, (const char **) argv
);
504 clean_exit(EXIT_FAILURE
);
507 ret
= validate_options();
512 ret
= set_signal_handler();
517 if (opt_tracing_group
!= NULL
) {
518 DBG("Set tracing group to '%s'", opt_tracing_group
);
519 lttng_set_tracing_group(opt_tracing_group
);
522 /* If ask for kernel tracing, need root perms */
523 if (opt_trace_kernel
) {
524 DBG("Kernel tracing activated");
526 ERR("%s must be setuid root", progname
);
531 /* Check if the lttng session daemon is running.
532 * If no, a daemon will be spawned.
534 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
535 clean_exit(EXIT_FAILURE
);
538 ret
= process_client_opt();
This page took 0.051923 seconds and 4 git commands to generate.