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 void sighandler(int sig
);
45 static int set_signal_handler(void);
50 * Process client request from the command line
51 * options. Every tracing action is done by the
54 static int process_client_opt(void)
58 /* Connect to the session daemon */
59 ret
= lttng_connect_sessiond();
65 ret
= process_opt_list_apps();
74 ERR("%s", lttng_get_readable_code(ret
));
79 * process_opt_list_apps
81 * Get the UST traceable pid list and print
84 static int process_opt_list_apps(void)
89 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
90 char cmdline
[PATH_MAX
];
92 ret
= lttng_ust_list_apps(&pids
);
97 MSG("LTTng UST traceable application [name (pid)]:");
98 for (i
=0; i
< ret
; i
++) {
99 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pids
[i
]);
100 fp
= fopen(path
, "r");
104 ret
= fread(cmdline
, 1, sizeof(cmdline
), fp
);
105 MSG("\t%s (%d)", cmdline
, pids
[i
]);
109 /* Allocated by lttng_ust_list_apps() */
121 * Spawn a session daemon by forking and execv.
123 static int spawn_sessiond(char *pathname
)
128 MSG("Spawning session daemon");
131 /* Spawn session daemon and tell
132 * it to signal us when ready.
134 ret
= execlp(pathname
, "ltt-sessiond", "--sig-parent", NULL
);
136 if (errno
== ENOENT
) {
137 ERR("No session daemon found. Use --sessiond-path.");
141 kill(getppid(), SIGTERM
);
145 } else if (pid
> 0) {
146 /* Wait for ltt-sessiond to start */
162 * Check if the session daemon is available using
163 * the liblttngctl API for the check. If not, try to
166 static int check_ltt_sessiond(void)
169 char *pathname
= NULL
;
171 ret
= lttng_check_session_daemon();
173 /* Try command line option path */
174 if (opt_sessiond_path
!= NULL
) {
175 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
177 ERR("No such file: %s", opt_sessiond_path
);
180 pathname
= opt_sessiond_path
;
182 /* Try LTTNG_SESSIOND_PATH env variable */
183 pathname
= strdup(getenv(LTTNG_SESSIOND_PATH_ENV
));
186 /* Let's rock and roll */
187 if (pathname
== NULL
) {
188 ret
= asprintf(&pathname
, "ltt-sessiond");
194 ret
= spawn_sessiond(pathname
);
197 ERR("Problem occurs when starting %s", pathname
);
209 * Setup signal handler for SIGCHLD and SIGTERM.
211 static int set_signal_handler(void)
217 if ((ret
= sigemptyset(&sigset
)) < 0) {
218 perror("sigemptyset");
222 sa
.sa_handler
= sighandler
;
225 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
230 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
242 * Signal handler for the daemon
244 static void sighandler(int sig
)
246 DBG("%d received", sig
);
249 clean_exit(EXIT_FAILURE
);
263 void clean_exit(int code
)
272 int main(int argc
, char *argv
[])
276 progname
= argv
[0] ? argv
[0] : "lttng";
278 /* For Mathieu Desnoyers aka Dr Tracing */
279 if (strncmp(progname
, "drtrace", 7) == 0) {
280 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
283 ret
= parse_args(argc
, (const char **) argv
);
288 ret
= set_signal_handler();
293 if (opt_tracing_group
!= NULL
) {
294 DBG("Set tracing group to '%s'", opt_tracing_group
);
295 lttng_set_tracing_group(opt_tracing_group
);
298 /* If ask for kernel tracing, need root perms */
299 if (opt_trace_kernel
) {
300 DBG("Kernel tracing activated");
302 ERR("%s must be setuid root", progname
);
307 /* Check if the lttng session daemon is running.
308 * If no, a daemon will be spawned.
310 if (opt_no_sessiond
== 0 && (check_ltt_sessiond() < 0)) {
314 ret
= process_client_opt();
This page took 0.038469 seconds and 5 git commands to generate.