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
);
96 if (opt_session_uuid
!= NULL
) {
97 lttng_set_current_session_uuid(opt_session_uuid
);
100 if (opt_create_trace
) {
101 DBG("Create trace for pid %d", opt_create_trace
);
102 ret
= lttng_ust_create_trace(opt_create_trace
);
106 MSG("Trace created successfully!\nUse --start PID to start tracing");
112 ERR("%s", lttng_get_readable_code(ret
));
117 * process_opt_create_session
119 * Create a new session using the name pass
120 * to the command line.
122 static int process_opt_create_session(void)
128 ret
= lttng_create_session(opt_create_session
, &session_id
);
133 uuid_unparse(session_id
, str_uuid
);
135 MSG("Session created:");
136 MSG(" %s (%s)", opt_create_session
, str_uuid
);
143 * process_opt_list_sessions
145 * Get the list of available sessions from
146 * the session daemon and print it to user.
148 static int process_opt_list_sessions(void)
151 struct lttng_session
*sess
;
153 count
= lttng_list_sessions(&sess
);
159 MSG("Available sessions [Name (uuid)]:");
160 for (i
= 0; i
< count
; i
++) {
161 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
165 MSG("\nTo select a session, use --session UUID.");
174 * process_opt_list_apps
176 * Get the UST traceable pid list and print
179 static int process_opt_list_apps(void)
184 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
185 char cmdline
[PATH_MAX
];
187 count
= lttng_ust_list_apps(&pids
);
193 MSG("LTTng UST traceable application [name (pid)]:");
194 for (i
=0; i
< count
; i
++) {
195 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pids
[i
]);
196 fp
= fopen(path
, "r");
198 MSG("\t(not running) (%d)", pids
[i
]);
201 ret
= fread(cmdline
, 1, sizeof(cmdline
), fp
);
202 MSG("\t%s (%d)", cmdline
, pids
[i
]);
206 /* Allocated by lttng_ust_list_apps() */
218 * Spawn a session daemon by forking and execv.
220 static int spawn_sessiond(char *pathname
)
225 MSG("Spawning session daemon");
228 /* Spawn session daemon and tell
229 * it to signal us when ready.
231 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
233 if (errno
== ENOENT
) {
234 ERR("No session daemon found. Use --sessiond-path.");
238 kill(getppid(), SIGTERM
);
242 } else if (pid
> 0) {
243 /* Wait for ltt-sessiond to start */
259 * Check if the session daemon is available using
260 * the liblttngctl API for the check. If not, try to
263 static int check_ltt_sessiond(void)
266 char *pathname
= NULL
;
268 ret
= lttng_check_session_daemon();
270 /* Try command line option path */
271 if (opt_sessiond_path
!= NULL
) {
272 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
274 ERR("No such file: %s", opt_sessiond_path
);
277 pathname
= opt_sessiond_path
;
279 /* Try LTTNG_SESSIOND_PATH env variable */
280 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
281 if (pathname
!= NULL
) {
282 /* strdup here in order to make the free()
285 pathname
= strdup(pathname
);
289 /* Let's rock and roll */
290 if (pathname
== NULL
) {
291 ret
= asprintf(&pathname
, "ltt-sessiond");
297 ret
= spawn_sessiond(pathname
);
300 ERR("Problem occurs when starting %s", pathname
);
312 * Setup signal handler for SIGCHLD and SIGTERM.
314 static int set_signal_handler(void)
320 if ((ret
= sigemptyset(&sigset
)) < 0) {
321 perror("sigemptyset");
325 sa
.sa_handler
= sighandler
;
328 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
333 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
345 * Signal handler for the daemon
347 static void sighandler(int sig
)
349 DBG("%d received", sig
);
352 clean_exit(EXIT_FAILURE
);
366 void clean_exit(int code
)
369 if (lttng_disconnect_sessiond() < 0) {
370 ERR("Session daemon disconnect failed.");
378 int main(int argc
, char *argv
[])
382 progname
= argv
[0] ? argv
[0] : "lttng";
384 /* For Mathieu Desnoyers aka Dr Tracing */
385 if (strncmp(progname
, "drtrace", 7) == 0) {
386 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
389 ret
= parse_args(argc
, (const char **) argv
);
391 clean_exit(EXIT_FAILURE
);
394 ret
= set_signal_handler();
399 if (opt_tracing_group
!= NULL
) {
400 DBG("Set tracing group to '%s'", opt_tracing_group
);
401 lttng_set_tracing_group(opt_tracing_group
);
404 /* If ask for kernel tracing, need root perms */
405 if (opt_trace_kernel
) {
406 DBG("Kernel tracing activated");
408 ERR("%s must be setuid root", progname
);
413 /* Check if the lttng session daemon is running.
414 * If no, a daemon will be spawned.
416 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
417 clean_exit(EXIT_FAILURE
);
420 ret
= process_client_opt();
This page took 0.037946 seconds and 4 git commands to generate.