f6cad42434e2f718a1a8077a4823866998a7acfd
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_trace_kernel
) {
112 ERR("Not implemented yet");
116 if (opt_trace_pid
!= 0) {
117 if (opt_create_trace
) {
118 DBG("Create a userspace trace for pid %d", opt_trace_pid
);
119 ret
= lttng_ust_create_trace(opt_trace_pid
);
123 MSG("Trace created successfully!\nUse --start to start tracing.");
126 if (opt_start_trace
) {
127 DBG("Start trace for pid %d", opt_trace_pid
);
128 ret
= lttng_ust_start_trace(opt_trace_pid
);
132 MSG("Trace started successfully!");
133 } else if (opt_stop_trace
) {
134 DBG("Stop trace for pid %d", opt_trace_pid
);
135 ret
= lttng_ust_stop_trace(opt_trace_pid
);
139 MSG("Trace stopped successfully!");
147 ERR("%s", lttng_get_readable_code(ret
));
152 * process_opt_list_traces
154 * Get list of all traces for a specific session uuid.
156 static int process_opt_list_traces(void)
160 struct lttng_trace
*traces
;
162 uuid_parse(opt_session_uuid
, uuid
);
163 ret
= lttng_list_traces(&uuid
, &traces
);
168 MSG("Userspace traces:");
169 for (i
= 0; i
< ret
; i
++) {
170 if (traces
[i
].type
== USERSPACE
) {
171 MSG("\t%d) %s (pid: %d): %s",
172 i
, traces
[i
].name
, traces
[i
].pid
,
173 get_cmdline_by_pid(traces
[i
].pid
));
179 MSG("Kernel traces:");
180 for (;i
< ret
; i
++) {
181 if (traces
[i
].type
== KERNEL
) {
182 MSG("\t%d) %s", i
, traces
[i
].name
);
193 * process_opt_create_session
195 * Create a new session using the name pass
196 * to the command line.
198 static int process_opt_create_session(void)
204 ret
= lttng_create_session(opt_create_session
, &session_id
);
209 uuid_unparse(session_id
, str_uuid
);
211 MSG("Session created:");
212 MSG(" %s (%s)", opt_create_session
, str_uuid
);
219 * process_opt_list_sessions
221 * Get the list of available sessions from
222 * the session daemon and print it to user.
224 static int process_opt_list_sessions(void)
227 struct lttng_session
*sess
;
229 count
= lttng_list_sessions(&sess
);
235 MSG("Available sessions [Name (uuid)]:");
236 for (i
= 0; i
< count
; i
++) {
237 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
241 MSG("\nTo select a session, use --session UUID.");
250 * process_opt_list_apps
252 * Get the UST traceable pid list and print
255 static int process_opt_list_apps(void)
261 count
= lttng_ust_list_apps(&pids
);
267 MSG("LTTng UST traceable application [name (pid)]:");
268 for (i
=0; i
< count
; i
++) {
269 cmdline
= get_cmdline_by_pid(pids
[i
]);
270 if (cmdline
== NULL
) {
271 MSG("\t(not running) (%d)", pids
[i
]);
274 MSG("\t%s (%d)", cmdline
, pids
[i
]);
278 /* Allocated by lttng_ust_list_apps() */
290 * Get command line from /proc for a specific pid.
292 * On success, return an allocated string pointer pointing to
294 * On error, return NULL.
296 static char *get_cmdline_by_pid(pid_t pid
)
300 char *cmdline
= NULL
;
301 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
303 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
304 fp
= fopen(path
, "r");
309 /* Caller must free() *cmdline */
310 cmdline
= malloc(PATH_MAX
);
311 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
321 * Make sure that all options passed to the command line
322 * are compatible with each others.
324 * On error, return -1
325 * On success, return 0
327 static int validate_options(void)
329 /* Conflicting command */
330 if (opt_start_trace
&& opt_stop_trace
) {
331 ERR("Can't use --start and --stop together.");
333 /* Must have a session UUID for trace action. */
334 } else if ((opt_session_uuid
== NULL
) &&
335 (opt_create_trace
|| opt_start_trace
|| opt_stop_trace
|| opt_list_traces
)) {
336 ERR("You need to specify a session UUID.\nPlease use --session UUID to do so.");
338 /* If no PID specified and trace_kernel is off */
339 } else if ((opt_trace_pid
== 0 && opt_trace_kernel
== 0) &&
340 (opt_create_trace
|| opt_start_trace
|| opt_stop_trace
)) {
341 ERR("Please specify a PID using -p, --pid PID.");
354 * Spawn a session daemon by forking and execv.
356 static int spawn_sessiond(char *pathname
)
361 MSG("Spawning session daemon");
364 /* Spawn session daemon and tell
365 * it to signal us when ready.
367 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
369 if (errno
== ENOENT
) {
370 ERR("No session daemon found. Use --sessiond-path.");
374 kill(getppid(), SIGTERM
);
378 } else if (pid
> 0) {
379 /* Wait for ltt-sessiond to start */
395 * Check if the session daemon is available using
396 * the liblttngctl API for the check. If not, try to
399 static int check_ltt_sessiond(void)
402 char *pathname
= NULL
;
404 ret
= lttng_check_session_daemon();
406 /* Try command line option path */
407 if (opt_sessiond_path
!= NULL
) {
408 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
410 ERR("No such file: %s", opt_sessiond_path
);
413 pathname
= opt_sessiond_path
;
415 /* Try LTTNG_SESSIOND_PATH env variable */
416 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
417 if (pathname
!= NULL
) {
418 /* strdup here in order to make the free()
421 pathname
= strdup(pathname
);
425 /* Let's rock and roll */
426 if (pathname
== NULL
) {
427 ret
= asprintf(&pathname
, "ltt-sessiond");
433 ret
= spawn_sessiond(pathname
);
436 ERR("Problem occurs when starting %s", pathname
);
448 * Setup signal handler for SIGCHLD and SIGTERM.
450 static int set_signal_handler(void)
456 if ((ret
= sigemptyset(&sigset
)) < 0) {
457 perror("sigemptyset");
461 sa
.sa_handler
= sighandler
;
464 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
469 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
481 * Signal handler for the daemon
483 static void sighandler(int sig
)
487 DBG("SIGTERM catched");
488 clean_exit(EXIT_FAILURE
);
492 DBG("SIGCHLD catched");
495 DBG("Unknown signal %d catched", sig
);
504 void clean_exit(int code
)
507 if (lttng_disconnect_sessiond() < 0) {
508 ERR("Session daemon disconnect failed.");
516 int main(int argc
, char *argv
[])
520 progname
= argv
[0] ? argv
[0] : "lttng";
522 /* For Mathieu Desnoyers aka Dr Tracing */
523 if (strncmp(progname
, "drtrace", 7) == 0) {
524 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
527 ret
= parse_args(argc
, (const char **) argv
);
529 clean_exit(EXIT_FAILURE
);
532 ret
= validate_options();
537 ret
= set_signal_handler();
542 if (opt_tracing_group
!= NULL
) {
543 DBG("Set tracing group to '%s'", opt_tracing_group
);
544 lttng_set_tracing_group(opt_tracing_group
);
547 /* If ask for kernel tracing, need root perms */
548 if (opt_trace_kernel
) {
549 DBG("Kernel tracing activated");
551 ERR("%s must be setuid root", progname
);
556 /* Check if the lttng session daemon is running.
557 * If no, a daemon will be spawned.
559 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
560 clean_exit(EXIT_FAILURE
);
563 ret
= process_client_opt();
This page took 0.04219 seconds and 5 git commands to generate.