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 void sighandler(int sig
);
46 static int set_signal_handler(void);
51 * Process client request from the command line
52 * options. Every tracing action is done by the
55 static int process_client_opt(void)
59 /* Connect to the session daemon */
60 ret
= lttng_connect_sessiond();
66 ret
= process_opt_list_apps();
72 if (opt_list_session
) {
73 ret
= process_opt_list_sessions();
82 ERR("%s", lttng_get_readable_code(ret
));
87 * process_opt_list_sessions
89 * Get the list of available sessions from
90 * the session daemon and print it to user.
92 static int process_opt_list_sessions(void)
95 struct lttng_session
*sess
;
97 count
= lttng_list_sessions(&sess
);
103 MSG("Available sessions [Name (uuid)]:");
104 for (i
= 0; i
< count
; i
++) {
105 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
109 MSG("\nTo select a session, use --session UUID.");
118 * process_opt_list_apps
120 * Get the UST traceable pid list and print
123 static int process_opt_list_apps(void)
128 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
129 char cmdline
[PATH_MAX
];
131 ret
= lttng_ust_list_apps(&pids
);
136 MSG("LTTng UST traceable application [name (pid)]:");
137 for (i
=0; i
< ret
; i
++) {
138 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pids
[i
]);
139 fp
= fopen(path
, "r");
143 ret
= fread(cmdline
, 1, sizeof(cmdline
), fp
);
144 MSG("\t%s (%d)", cmdline
, pids
[i
]);
148 /* Allocated by lttng_ust_list_apps() */
160 * Spawn a session daemon by forking and execv.
162 static int spawn_sessiond(char *pathname
)
167 MSG("Spawning session daemon");
170 /* Spawn session daemon and tell
171 * it to signal us when ready.
173 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
175 if (errno
== ENOENT
) {
176 ERR("No session daemon found. Use --sessiond-path.");
180 kill(getppid(), SIGTERM
);
184 } else if (pid
> 0) {
185 /* Wait for ltt-sessiond to start */
201 * Check if the session daemon is available using
202 * the liblttngctl API for the check. If not, try to
205 static int check_ltt_sessiond(void)
208 char *pathname
= NULL
;
210 ret
= lttng_check_session_daemon();
212 /* Try command line option path */
213 if (opt_sessiond_path
!= NULL
) {
214 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
216 ERR("No such file: %s", opt_sessiond_path
);
219 pathname
= opt_sessiond_path
;
221 /* Try LTTNG_SESSIOND_PATH env variable */
222 pathname
= strdup(getenv(LTTNG_SESSIOND_PATH_ENV
));
225 /* Let's rock and roll */
226 if (pathname
== NULL
) {
227 ret
= asprintf(&pathname
, "ltt-sessiond");
233 ret
= spawn_sessiond(pathname
);
236 ERR("Problem occurs when starting %s", pathname
);
248 * Setup signal handler for SIGCHLD and SIGTERM.
250 static int set_signal_handler(void)
256 if ((ret
= sigemptyset(&sigset
)) < 0) {
257 perror("sigemptyset");
261 sa
.sa_handler
= sighandler
;
264 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
269 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
281 * Signal handler for the daemon
283 static void sighandler(int sig
)
285 DBG("%d received", sig
);
288 clean_exit(EXIT_FAILURE
);
302 void clean_exit(int code
)
311 int main(int argc
, char *argv
[])
315 progname
= argv
[0] ? argv
[0] : "lttng";
317 /* For Mathieu Desnoyers aka Dr Tracing */
318 if (strncmp(progname
, "drtrace", 7) == 0) {
319 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
322 ret
= parse_args(argc
, (const char **) argv
);
327 ret
= set_signal_handler();
332 if (opt_tracing_group
!= NULL
) {
333 DBG("Set tracing group to '%s'", opt_tracing_group
);
334 lttng_set_tracing_group(opt_tracing_group
);
337 /* If ask for kernel tracing, need root perms */
338 if (opt_trace_kernel
) {
339 DBG("Kernel tracing activated");
341 ERR("%s must be setuid root", progname
);
346 /* Check if the lttng session daemon is running.
347 * If no, a daemon will be spawned.
349 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
353 ret
= process_client_opt();
This page took 0.064134 seconds and 4 git commands to generate.