9751621ff84f2c4fceb2267c5dd1b1ec6f83e1ae
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);
48 static int get_cmdline_by_pid(pid_t pid
, char **cmdline
);
49 static int validate_options(void);
54 * Process client request from the command line
55 * options. Every tracing action is done by the
58 static int process_client_opt(void)
63 /* Connect to the session daemon */
64 ret
= lttng_connect_sessiond();
70 ret
= process_opt_list_apps();
76 if (opt_list_session
) {
77 ret
= process_opt_list_sessions();
83 if (opt_create_session
!= NULL
) {
84 ret
= process_opt_create_session();
90 if (opt_destroy_session
!= NULL
) {
91 uuid_parse(opt_destroy_session
, uuid
);
92 ret
= lttng_destroy_session(&uuid
);
98 if (opt_session_uuid
!= NULL
) {
99 lttng_set_current_session_uuid(opt_session_uuid
);
102 if (opt_create_trace
) {
103 DBG("Create trace for pid %d", opt_create_trace
);
104 ret
= lttng_ust_create_trace(opt_create_trace
);
108 MSG("Trace created successfully!\nUse --start PID to start tracing.");
114 ERR("%s", lttng_get_readable_code(ret
));
119 * process_opt_create_session
121 * Create a new session using the name pass
122 * to the command line.
124 static int process_opt_create_session(void)
130 ret
= lttng_create_session(opt_create_session
, &session_id
);
135 uuid_unparse(session_id
, str_uuid
);
137 MSG("Session created:");
138 MSG(" %s (%s)", opt_create_session
, str_uuid
);
145 * process_opt_list_sessions
147 * Get the list of available sessions from
148 * the session daemon and print it to user.
150 static int process_opt_list_sessions(void)
153 struct lttng_session
*sess
;
155 count
= lttng_list_sessions(&sess
);
161 MSG("Available sessions [Name (uuid)]:");
162 for (i
= 0; i
< count
; i
++) {
163 MSG("\tName: %s (uuid: %s)", sess
[i
].name
, sess
[i
].uuid
);
167 MSG("\nTo select a session, use --session UUID.");
176 * process_opt_list_apps
178 * Get the UST traceable pid list and print
181 static int process_opt_list_apps(void)
187 count
= lttng_ust_list_apps(&pids
);
193 MSG("LTTng UST traceable application [name (pid)]:");
194 for (i
=0; i
< count
; i
++) {
195 ret
= get_cmdline_by_pid(pids
[i
], &cmdline
);
197 MSG("\t(not running) (%d)", pids
[i
]);
200 MSG("\t%s (%d)", cmdline
, pids
[i
]);
204 /* Allocated by lttng_ust_list_apps() */
216 * Get command line from /proc for a
217 * specific pid. Allocate cmdline so the
218 * user must free() that pointer.
220 * On success, return 1
221 * On error (not found), return 0
223 static int get_cmdline_by_pid(pid_t pid
, char **cmdline
)
227 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
229 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
230 fp
= fopen(path
, "r");
235 /* Caller must free() *cmdline */
236 *cmdline
= malloc(PATH_MAX
);
237 ret
= fread(*cmdline
, 1, PATH_MAX
, fp
);
249 * Make sure that all options passed to the command line
250 * are compatible with each others.
252 * On error, return -1
253 * On success, return 0
255 static int validate_options(void)
257 if ((opt_session_uuid
== NULL
) &&
258 (opt_create_trace
)) {
259 ERR("Can't create trace without a session ID.\nPlease specify using --session UUID");
272 * Spawn a session daemon by forking and execv.
274 static int spawn_sessiond(char *pathname
)
279 MSG("Spawning session daemon");
282 /* Spawn session daemon and tell
283 * it to signal us when ready.
285 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
287 if (errno
== ENOENT
) {
288 ERR("No session daemon found. Use --sessiond-path.");
292 kill(getppid(), SIGTERM
);
296 } else if (pid
> 0) {
297 /* Wait for ltt-sessiond to start */
313 * Check if the session daemon is available using
314 * the liblttngctl API for the check. If not, try to
317 static int check_ltt_sessiond(void)
320 char *pathname
= NULL
;
322 ret
= lttng_check_session_daemon();
324 /* Try command line option path */
325 if (opt_sessiond_path
!= NULL
) {
326 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
328 ERR("No such file: %s", opt_sessiond_path
);
331 pathname
= opt_sessiond_path
;
333 /* Try LTTNG_SESSIOND_PATH env variable */
334 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
335 if (pathname
!= NULL
) {
336 /* strdup here in order to make the free()
339 pathname
= strdup(pathname
);
343 /* Let's rock and roll */
344 if (pathname
== NULL
) {
345 ret
= asprintf(&pathname
, "ltt-sessiond");
351 ret
= spawn_sessiond(pathname
);
354 ERR("Problem occurs when starting %s", pathname
);
366 * Setup signal handler for SIGCHLD and SIGTERM.
368 static int set_signal_handler(void)
374 if ((ret
= sigemptyset(&sigset
)) < 0) {
375 perror("sigemptyset");
379 sa
.sa_handler
= sighandler
;
382 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
387 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
399 * Signal handler for the daemon
401 static void sighandler(int sig
)
403 DBG("%d received", sig
);
406 clean_exit(EXIT_FAILURE
);
420 void clean_exit(int code
)
423 if (lttng_disconnect_sessiond() < 0) {
424 ERR("Session daemon disconnect failed.");
432 int main(int argc
, char *argv
[])
436 progname
= argv
[0] ? argv
[0] : "lttng";
438 /* For Mathieu Desnoyers aka Dr Tracing */
439 if (strncmp(progname
, "drtrace", 7) == 0) {
440 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
443 ret
= parse_args(argc
, (const char **) argv
);
445 clean_exit(EXIT_FAILURE
);
448 ret
= validate_options();
453 ret
= set_signal_handler();
458 if (opt_tracing_group
!= NULL
) {
459 DBG("Set tracing group to '%s'", opt_tracing_group
);
460 lttng_set_tracing_group(opt_tracing_group
);
463 /* If ask for kernel tracing, need root perms */
464 if (opt_trace_kernel
) {
465 DBG("Kernel tracing activated");
467 ERR("%s must be setuid root", progname
);
472 /* Check if the lttng session daemon is running.
473 * If no, a daemon will be spawned.
475 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
476 clean_exit(EXIT_FAILURE
);
479 ret
= process_client_opt();
This page took 0.053222 seconds and 3 git commands to generate.