Add ust create trace feature
[lttng-tools.git] / lttng / lttng.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <grp.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32
33 #include <lttng/liblttngctl.h>
34
35 #include "lttng.h"
36 #include "lttngerr.h"
37
38 /* Variables */
39 static char *progname;
40
41 /* Prototypes */
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
49 /*
50 * start_client
51 *
52 * Process client request from the command line
53 * options. Every tracing action is done by the
54 * liblttngctl API.
55 */
56 static int process_client_opt(void)
57 {
58 int ret;
59 uuid_t uuid;
60
61 /* Connect to the session daemon */
62 ret = lttng_connect_sessiond();
63 if (ret < 0) {
64 goto end;
65 }
66
67 if (opt_list_apps) {
68 ret = process_opt_list_apps();
69 if (ret < 0) {
70 goto end;
71 }
72 }
73
74 if (opt_list_session) {
75 ret = process_opt_list_sessions();
76 if (ret < 0) {
77 goto end;
78 }
79 }
80
81 if (opt_create_session != NULL) {
82 ret = process_opt_create_session();
83 if (ret < 0) {
84 goto end;
85 }
86 }
87
88 if (opt_destroy_session != NULL) {
89 uuid_parse(opt_destroy_session, uuid);
90 ret = lttng_destroy_session(&uuid);
91 if (ret < 0) {
92 goto end;
93 }
94 }
95
96 if (opt_session_uuid != NULL) {
97 lttng_set_current_session_uuid(opt_session_uuid);
98 }
99
100 if (opt_create_trace) {
101 DBG("Create trace for pid %d", opt_create_trace);
102 ret = lttng_ust_create_trace(opt_create_trace);
103 if (ret < 0) {
104 goto end;
105 }
106 MSG("Trace created successfully!\nUse --start PID to start tracing");
107 }
108
109 return 0;
110
111 end:
112 ERR("%s", lttng_get_readable_code(ret));
113 return ret;
114 }
115
116 /*
117 * process_opt_create_session
118 *
119 * Create a new session using the name pass
120 * to the command line.
121 */
122 static int process_opt_create_session(void)
123 {
124 int ret;
125 uuid_t session_id;
126 char str_uuid[37];
127
128 ret = lttng_create_session(opt_create_session, &session_id);
129 if (ret < 0) {
130 goto error;
131 }
132
133 uuid_unparse(session_id, str_uuid);
134
135 MSG("Session created:");
136 MSG(" %s (%s)", opt_create_session, str_uuid);
137
138 error:
139 return ret;
140 }
141
142 /*
143 * process_opt_list_sessions
144 *
145 * Get the list of available sessions from
146 * the session daemon and print it to user.
147 */
148 static int process_opt_list_sessions(void)
149 {
150 int ret, count, i;
151 struct lttng_session *sess;
152
153 count = lttng_list_sessions(&sess);
154 if (count < 0) {
155 ret = count;
156 goto error;
157 }
158
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);
162 }
163
164 free(sess);
165 MSG("\nTo select a session, use --session UUID.");
166
167 return 0;
168
169 error:
170 return ret;
171 }
172
173 /*
174 * process_opt_list_apps
175 *
176 * Get the UST traceable pid list and print
177 * them to the user.
178 */
179 static int process_opt_list_apps(void)
180 {
181 int i, ret, count;
182 pid_t *pids;
183 FILE *fp;
184 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
185 char cmdline[PATH_MAX];
186
187 count = lttng_ust_list_apps(&pids);
188 if (count < 0) {
189 ret = count;
190 goto error;
191 }
192
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");
197 if (fp == NULL) {
198 MSG("\t(not running) (%d)", pids[i]);
199 continue;
200 }
201 ret = fread(cmdline, 1, sizeof(cmdline), fp);
202 MSG("\t%s (%d)", cmdline, pids[i]);
203 fclose(fp);
204 }
205
206 /* Allocated by lttng_ust_list_apps() */
207 free(pids);
208
209 return 0;
210
211 error:
212 return ret;
213 }
214
215 /*
216 * spawn_sessiond
217 *
218 * Spawn a session daemon by forking and execv.
219 */
220 static int spawn_sessiond(char *pathname)
221 {
222 int ret = 0;
223 pid_t pid;
224
225 MSG("Spawning session daemon");
226 pid = fork();
227 if (pid == 0) {
228 /* Spawn session daemon and tell
229 * it to signal us when ready.
230 */
231 ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
232 if (ret < 0) {
233 if (errno == ENOENT) {
234 ERR("No session daemon found. Use --sessiond-path.");
235 } else {
236 perror("execlp");
237 }
238 kill(getppid(), SIGTERM);
239 exit(EXIT_FAILURE);
240 }
241 exit(EXIT_SUCCESS);
242 } else if (pid > 0) {
243 /* Wait for ltt-sessiond to start */
244 pause();
245 goto end;
246 } else {
247 perror("fork");
248 ret = -1;
249 goto end;
250 }
251
252 end:
253 return ret;
254 }
255
256 /*
257 * check_ltt_sessiond
258 *
259 * Check if the session daemon is available using
260 * the liblttngctl API for the check. If not, try to
261 * spawn a daemon.
262 */
263 static int check_ltt_sessiond(void)
264 {
265 int ret;
266 char *pathname = NULL;
267
268 ret = lttng_check_session_daemon();
269 if (ret < 0) {
270 /* Try command line option path */
271 if (opt_sessiond_path != NULL) {
272 ret = access(opt_sessiond_path, F_OK | X_OK);
273 if (ret < 0) {
274 ERR("No such file: %s", opt_sessiond_path);
275 goto end;
276 }
277 pathname = opt_sessiond_path;
278 } else {
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()
283 * not fail later on.
284 */
285 pathname = strdup(pathname);
286 }
287 }
288
289 /* Let's rock and roll */
290 if (pathname == NULL) {
291 ret = asprintf(&pathname, "ltt-sessiond");
292 if (ret < 0) {
293 goto end;
294 }
295 }
296
297 ret = spawn_sessiond(pathname);
298 free(pathname);
299 if (ret < 0) {
300 ERR("Problem occurs when starting %s", pathname);
301 goto end;
302 }
303 }
304
305 end:
306 return ret;
307 }
308
309 /*
310 * set_signal_handler
311 *
312 * Setup signal handler for SIGCHLD and SIGTERM.
313 */
314 static int set_signal_handler(void)
315 {
316 int ret = 0;
317 struct sigaction sa;
318 sigset_t sigset;
319
320 if ((ret = sigemptyset(&sigset)) < 0) {
321 perror("sigemptyset");
322 goto end;
323 }
324
325 sa.sa_handler = sighandler;
326 sa.sa_mask = sigset;
327 sa.sa_flags = 0;
328 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
329 perror("sigaction");
330 goto end;
331 }
332
333 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
334 perror("sigaction");
335 goto end;
336 }
337
338 end:
339 return ret;
340 }
341
342 /*
343 * sighandler
344 *
345 * Signal handler for the daemon
346 */
347 static void sighandler(int sig)
348 {
349 DBG("%d received", sig);
350 switch (sig) {
351 case SIGTERM:
352 clean_exit(EXIT_FAILURE);
353 break;
354 case SIGCHLD:
355 /* Notify is done */
356 break;
357 default:
358 break;
359 }
360
361 return;
362 }
363 /*
364 * clean_exit
365 */
366 void clean_exit(int code)
367 {
368 DBG("Clean exit");
369 if (lttng_disconnect_sessiond() < 0) {
370 ERR("Session daemon disconnect failed.");
371 }
372 exit(code);
373 }
374
375 /*
376 * main
377 */
378 int main(int argc, char *argv[])
379 {
380 int ret;
381
382 progname = argv[0] ? argv[0] : "lttng";
383
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);
387 }
388
389 ret = parse_args(argc, (const char **) argv);
390 if (ret < 0) {
391 clean_exit(EXIT_FAILURE);
392 }
393
394 ret = set_signal_handler();
395 if (ret < 0) {
396 clean_exit(ret);
397 }
398
399 if (opt_tracing_group != NULL) {
400 DBG("Set tracing group to '%s'", opt_tracing_group);
401 lttng_set_tracing_group(opt_tracing_group);
402 }
403
404 /* If ask for kernel tracing, need root perms */
405 if (opt_trace_kernel) {
406 DBG("Kernel tracing activated");
407 if (getuid() != 0) {
408 ERR("%s must be setuid root", progname);
409 clean_exit(-EPERM);
410 }
411 }
412
413 /* Check if the lttng session daemon is running.
414 * If no, a daemon will be spawned.
415 */
416 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
417 clean_exit(EXIT_FAILURE);
418 }
419
420 ret = process_client_opt();
421 if (ret < 0) {
422 clean_exit(ret);
423 }
424
425 clean_exit(0);
426
427 return 0;
428 }
This page took 0.063691 seconds and 4 git commands to generate.