40cf52bc6a3b02059a1ea8635c412737f8a5f71f
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
);
103 ERR("%s", lttng_get_readable_code(ret
));
108 * process_opt_create_session
110 * Create a new session using the name pass
111 * to the command line.
113 static int process_opt_create_session(void)
119 ret
= lttng_create_session(opt_create_session
, &session_id
);
124 uuid_unparse(session_id
, str_uuid
);
126 MSG("Session created:");
127 MSG(" %s (%s)", opt_create_session
, str_uuid
);
134 * process_opt_list_sessions
136 * Get the list of available sessions from
137 * the session daemon and print it to user.
139 static int process_opt_list_sessions(void)
142 struct lttng_session
*sess
;
144 count
= lttng_list_sessions(&sess
);
150 MSG("Available sessions [Name (uuid)]:");
151 for (i
= 0; i
< count
; i
++) {
152 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
156 MSG("\nTo select a session, use --session UUID.");
165 * process_opt_list_apps
167 * Get the UST traceable pid list and print
170 static int process_opt_list_apps(void)
175 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
176 char cmdline
[PATH_MAX
];
178 count
= lttng_ust_list_apps(&pids
);
184 MSG("LTTng UST traceable application [name (pid)]:");
185 for (i
=0; i
< count
; i
++) {
186 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pids
[i
]);
187 fp
= fopen(path
, "r");
189 MSG("\t(not running) (%d)", pids
[i
]);
192 ret
= fread(cmdline
, 1, sizeof(cmdline
), fp
);
193 MSG("\t%s (%d)", cmdline
, pids
[i
]);
197 /* Allocated by lttng_ust_list_apps() */
209 * Spawn a session daemon by forking and execv.
211 static int spawn_sessiond(char *pathname
)
216 MSG("Spawning session daemon");
219 /* Spawn session daemon and tell
220 * it to signal us when ready.
222 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
224 if (errno
== ENOENT
) {
225 ERR("No session daemon found. Use --sessiond-path.");
229 kill(getppid(), SIGTERM
);
233 } else if (pid
> 0) {
234 /* Wait for ltt-sessiond to start */
250 * Check if the session daemon is available using
251 * the liblttngctl API for the check. If not, try to
254 static int check_ltt_sessiond(void)
257 char *pathname
= NULL
;
259 ret
= lttng_check_session_daemon();
261 /* Try command line option path */
262 if (opt_sessiond_path
!= NULL
) {
263 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
265 ERR("No such file: %s", opt_sessiond_path
);
268 pathname
= opt_sessiond_path
;
270 /* Try LTTNG_SESSIOND_PATH env variable */
271 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
272 if (pathname
!= NULL
) {
273 /* strdup here in order to make the free()
276 pathname
= strdup(pathname
);
280 /* Let's rock and roll */
281 if (pathname
== NULL
) {
282 ret
= asprintf(&pathname
, "ltt-sessiond");
288 ret
= spawn_sessiond(pathname
);
291 ERR("Problem occurs when starting %s", pathname
);
303 * Setup signal handler for SIGCHLD and SIGTERM.
305 static int set_signal_handler(void)
311 if ((ret
= sigemptyset(&sigset
)) < 0) {
312 perror("sigemptyset");
316 sa
.sa_handler
= sighandler
;
319 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
324 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
336 * Signal handler for the daemon
338 static void sighandler(int sig
)
340 DBG("%d received", sig
);
343 clean_exit(EXIT_FAILURE
);
357 void clean_exit(int code
)
360 if (lttng_disconnect_sessiond() < 0) {
361 ERR("Session daemon disconnect failed.");
369 int main(int argc
, char *argv
[])
373 progname
= argv
[0] ? argv
[0] : "lttng";
375 /* For Mathieu Desnoyers aka Dr Tracing */
376 if (strncmp(progname
, "drtrace", 7) == 0) {
377 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
380 ret
= parse_args(argc
, (const char **) argv
);
382 clean_exit(EXIT_FAILURE
);
385 ret
= set_signal_handler();
390 if (opt_tracing_group
!= NULL
) {
391 DBG("Set tracing group to '%s'", opt_tracing_group
);
392 lttng_set_tracing_group(opt_tracing_group
);
395 /* If ask for kernel tracing, need root perms */
396 if (opt_trace_kernel
) {
397 DBG("Kernel tracing activated");
399 ERR("%s must be setuid root", progname
);
404 /* Check if the lttng session daemon is running.
405 * If no, a daemon will be spawned.
407 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
408 clean_exit(EXIT_FAILURE
);
411 ret
= process_client_opt();
This page took 0.039406 seconds and 5 git commands to generate.