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);
52 * Process client request from the command line
53 * options. Every tracing action is done by the
56 static int process_client_opt(void)
61 /* Connect to the session daemon */
62 ret
= lttng_connect_sessiond();
68 ret
= process_opt_list_apps();
74 if (opt_list_session
) {
75 ret
= process_opt_list_sessions();
81 if (opt_create_session
!= NULL
) {
82 ret
= process_opt_create_session();
88 if (opt_destroy_session
!= NULL
) {
89 uuid_parse(opt_destroy_session
, uuid
);
90 ret
= lttng_destroy_session(&uuid
);
99 ERR("%s", lttng_get_readable_code(ret
));
104 * process_opt_create_session
106 * Create a new session using the name pass
107 * to the command line.
109 static int process_opt_create_session(void)
115 ret
= lttng_create_session(opt_create_session
, &session_id
);
120 uuid_unparse(session_id
, str_uuid
);
122 MSG("Session created:");
123 MSG(" %s (%s)", opt_create_session
, str_uuid
);
130 * process_opt_list_sessions
132 * Get the list of available sessions from
133 * the session daemon and print it to user.
135 static int process_opt_list_sessions(void)
138 struct lttng_session
*sess
;
140 count
= lttng_list_sessions(&sess
);
146 MSG("Available sessions [Name (uuid)]:");
147 for (i
= 0; i
< count
; i
++) {
148 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
152 MSG("\nTo select a session, use --session UUID.");
161 * process_opt_list_apps
163 * Get the UST traceable pid list and print
166 static int process_opt_list_apps(void)
171 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
172 char cmdline
[PATH_MAX
];
174 count
= lttng_ust_list_apps(&pids
);
180 MSG("LTTng UST traceable application [name (pid)]:");
181 for (i
=0; i
< count
; i
++) {
182 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pids
[i
]);
183 fp
= fopen(path
, "r");
185 MSG("\t(not running) (%d)", pids
[i
]);
188 ret
= fread(cmdline
, 1, sizeof(cmdline
), fp
);
189 MSG("\t%s (%d)", cmdline
, pids
[i
]);
193 /* Allocated by lttng_ust_list_apps() */
205 * Spawn a session daemon by forking and execv.
207 static int spawn_sessiond(char *pathname
)
212 MSG("Spawning session daemon");
215 /* Spawn session daemon and tell
216 * it to signal us when ready.
218 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
220 if (errno
== ENOENT
) {
221 ERR("No session daemon found. Use --sessiond-path.");
225 kill(getppid(), SIGTERM
);
229 } else if (pid
> 0) {
230 /* Wait for ltt-sessiond to start */
246 * Check if the session daemon is available using
247 * the liblttngctl API for the check. If not, try to
250 static int check_ltt_sessiond(void)
253 char *pathname
= NULL
;
255 ret
= lttng_check_session_daemon();
257 /* Try command line option path */
258 if (opt_sessiond_path
!= NULL
) {
259 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
261 ERR("No such file: %s", opt_sessiond_path
);
264 pathname
= opt_sessiond_path
;
266 /* Try LTTNG_SESSIOND_PATH env variable */
267 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
268 if (pathname
!= NULL
) {
269 /* strdup here in order to make the free()
272 pathname
= strdup(pathname
);
276 /* Let's rock and roll */
277 if (pathname
== NULL
) {
278 ret
= asprintf(&pathname
, "ltt-sessiond");
284 ret
= spawn_sessiond(pathname
);
287 ERR("Problem occurs when starting %s", pathname
);
299 * Setup signal handler for SIGCHLD and SIGTERM.
301 static int set_signal_handler(void)
307 if ((ret
= sigemptyset(&sigset
)) < 0) {
308 perror("sigemptyset");
312 sa
.sa_handler
= sighandler
;
315 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
320 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
332 * Signal handler for the daemon
334 static void sighandler(int sig
)
336 DBG("%d received", sig
);
339 clean_exit(EXIT_FAILURE
);
353 void clean_exit(int code
)
362 int main(int argc
, char *argv
[])
366 progname
= argv
[0] ? argv
[0] : "lttng";
368 /* For Mathieu Desnoyers aka Dr Tracing */
369 if (strncmp(progname
, "drtrace", 7) == 0) {
370 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
373 ret
= parse_args(argc
, (const char **) argv
);
378 ret
= set_signal_handler();
383 if (opt_tracing_group
!= NULL
) {
384 DBG("Set tracing group to '%s'", opt_tracing_group
);
385 lttng_set_tracing_group(opt_tracing_group
);
388 /* If ask for kernel tracing, need root perms */
389 if (opt_trace_kernel
) {
390 DBG("Kernel tracing activated");
392 ERR("%s must be setuid root", progname
);
397 /* Check if the lttng session daemon is running.
398 * If no, a daemon will be spawned.
400 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
404 ret
= process_client_opt();
This page took 0.039743 seconds and 5 git commands to generate.