8a0d58faeaa8ac7ec7299a7a633d9ada73e8e513
[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_list_traces(void);
46 static int process_opt_create_session(void);
47 static void sighandler(int sig);
48 static int set_signal_handler(void);
49 static int validate_options(void);
50 static char *get_cmdline_by_pid(pid_t pid);
51
52 /*
53 * start_client
54 *
55 * Process client request from the command line
56 * options. Every tracing action is done by the
57 * liblttngctl API.
58 */
59 static int process_client_opt(void)
60 {
61 int ret;
62 uuid_t uuid;
63
64 /* Connect to the session daemon */
65 ret = lttng_connect_sessiond();
66 if (ret < 0) {
67 goto end;
68 }
69
70 if (opt_list_apps) {
71 ret = process_opt_list_apps();
72 if (ret < 0) {
73 goto end;
74 }
75 }
76
77 if (opt_list_session) {
78 ret = process_opt_list_sessions();
79 if (ret < 0) {
80 goto end;
81 }
82 }
83
84 if (opt_list_traces) {
85 ret = process_opt_list_traces();
86 if (ret < 0) {
87 goto end;
88 }
89 }
90
91 if (opt_create_session != NULL) {
92 ret = process_opt_create_session();
93 if (ret < 0) {
94 goto end;
95 }
96 }
97
98 if (opt_destroy_session != NULL) {
99 uuid_parse(opt_destroy_session, uuid);
100 ret = lttng_destroy_session(&uuid);
101 if (ret < 0) {
102 goto end;
103 }
104 }
105
106 if (opt_session_uuid != NULL) {
107 DBG("Set session uuid to %s", opt_session_uuid);
108 lttng_set_current_session_uuid(opt_session_uuid);
109 }
110
111 if (opt_create_trace) {
112 DBG("Create trace for pid %d", opt_create_trace);
113 ret = lttng_ust_create_trace(opt_create_trace);
114 if (ret < 0) {
115 goto end;
116 }
117 MSG("Trace created successfully!\nUse --start PID to start tracing.");
118 }
119
120 if (opt_start_trace) {
121 DBG("Start trace for pid %d", opt_start_trace);
122 ret = lttng_ust_start_trace(opt_start_trace);
123 if (ret < 0) {
124 goto end;
125 }
126 MSG("Trace started successfully!");
127 }
128
129 if (opt_stop_trace) {
130 DBG("Stop trace for pid %d", opt_stop_trace);
131 ret = lttng_ust_stop_trace(opt_stop_trace);
132 if (ret < 0) {
133 goto end;
134 }
135 MSG("Trace stopped successfully!");
136 }
137
138 return 0;
139
140 end:
141 ERR("%s", lttng_get_readable_code(ret));
142 return ret;
143 }
144
145 /*
146 * process_opt_list_traces
147 *
148 * Get list of all traces for a specific session uuid.
149 */
150 static int process_opt_list_traces(void)
151 {
152 int ret, i;
153 uuid_t uuid;
154 struct lttng_trace *traces;
155
156 uuid_parse(opt_session_uuid, uuid);
157 ret = lttng_list_traces(&uuid, &traces);
158 if (ret < 0) {
159 goto error;
160 }
161
162 MSG("Userspace traces:");
163 for (i = 0; i < ret; i++) {
164 if (traces[i].type == USERSPACE) {
165 MSG("\t%d) %s (pid: %d): %s",
166 i, traces[i].name, traces[i].pid,
167 get_cmdline_by_pid(traces[i].pid));
168 } else {
169 break;
170 }
171 }
172
173 MSG("Kernel traces:");
174 for (;i < ret; i++) {
175 if (traces[i].type == KERNEL) {
176 MSG("\t%d) %s", i, traces[i].name);
177 }
178 }
179
180 free(traces);
181
182 error:
183 return ret;
184 }
185
186 /*
187 * process_opt_create_session
188 *
189 * Create a new session using the name pass
190 * to the command line.
191 */
192 static int process_opt_create_session(void)
193 {
194 int ret;
195 uuid_t session_id;
196 char str_uuid[37];
197
198 ret = lttng_create_session(opt_create_session, &session_id);
199 if (ret < 0) {
200 goto error;
201 }
202
203 uuid_unparse(session_id, str_uuid);
204
205 MSG("Session created:");
206 MSG(" %s (%s)", opt_create_session, str_uuid);
207
208 error:
209 return ret;
210 }
211
212 /*
213 * process_opt_list_sessions
214 *
215 * Get the list of available sessions from
216 * the session daemon and print it to user.
217 */
218 static int process_opt_list_sessions(void)
219 {
220 int ret, count, i;
221 struct lttng_session *sess;
222
223 count = lttng_list_sessions(&sess);
224 if (count < 0) {
225 ret = count;
226 goto error;
227 }
228
229 MSG("Available sessions [Name (uuid)]:");
230 for (i = 0; i < count; i++) {
231 MSG("\tName: %s (uuid: %s)", sess[i].name, sess[i].uuid);
232 }
233
234 free(sess);
235 MSG("\nTo select a session, use --session UUID.");
236
237 return 0;
238
239 error:
240 return ret;
241 }
242
243 /*
244 * process_opt_list_apps
245 *
246 * Get the UST traceable pid list and print
247 * them to the user.
248 */
249 static int process_opt_list_apps(void)
250 {
251 int i, ret, count;
252 pid_t *pids;
253 char *cmdline;
254
255 count = lttng_ust_list_apps(&pids);
256 if (count < 0) {
257 ret = count;
258 goto error;
259 }
260
261 MSG("LTTng UST traceable application [name (pid)]:");
262 for (i=0; i < count; i++) {
263 cmdline = get_cmdline_by_pid(pids[i]);
264 if (cmdline == NULL) {
265 MSG("\t(not running) (%d)", pids[i]);
266 continue;
267 }
268 MSG("\t%s (%d)", cmdline, pids[i]);
269 free(cmdline);
270 }
271
272 /* Allocated by lttng_ust_list_apps() */
273 free(pids);
274
275 return 0;
276
277 error:
278 return ret;
279 }
280
281 /*
282 * get_cmdline_by_pid
283 *
284 * Get command line from /proc for a specific pid.
285 *
286 * On success, return an allocated string pointer pointing to
287 * the proc cmdline.
288 * On error, return NULL.
289 */
290 static char *get_cmdline_by_pid(pid_t pid)
291 {
292 int ret;
293 FILE *fp;
294 char *cmdline = NULL;
295 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
296
297 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
298 fp = fopen(path, "r");
299 if (fp == NULL) {
300 goto end;
301 }
302
303 /* Caller must free() *cmdline */
304 cmdline = malloc(PATH_MAX);
305 ret = fread(cmdline, 1, PATH_MAX, fp);
306 fclose(fp);
307
308 end:
309 return cmdline;
310 }
311
312 /*
313 * validate_options
314 *
315 * Make sure that all options passed to the command line
316 * are compatible with each others.
317 *
318 * On error, return -1
319 * On success, return 0
320 */
321 static int validate_options(void)
322 {
323 if ((opt_session_uuid == NULL) &&
324 (opt_create_trace || opt_start_trace || opt_list_traces)) {
325 ERR("You need to specify a session UUID.\nPlease use --session UUID to do so.");
326 goto error;
327 }
328
329 return 0;
330
331 error:
332 return -1;
333 }
334
335 /*
336 * spawn_sessiond
337 *
338 * Spawn a session daemon by forking and execv.
339 */
340 static int spawn_sessiond(char *pathname)
341 {
342 int ret = 0;
343 pid_t pid;
344
345 MSG("Spawning session daemon");
346 pid = fork();
347 if (pid == 0) {
348 /* Spawn session daemon and tell
349 * it to signal us when ready.
350 */
351 ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
352 if (ret < 0) {
353 if (errno == ENOENT) {
354 ERR("No session daemon found. Use --sessiond-path.");
355 } else {
356 perror("execlp");
357 }
358 kill(getppid(), SIGTERM);
359 exit(EXIT_FAILURE);
360 }
361 exit(EXIT_SUCCESS);
362 } else if (pid > 0) {
363 /* Wait for ltt-sessiond to start */
364 pause();
365 goto end;
366 } else {
367 perror("fork");
368 ret = -1;
369 goto end;
370 }
371
372 end:
373 return ret;
374 }
375
376 /*
377 * check_ltt_sessiond
378 *
379 * Check if the session daemon is available using
380 * the liblttngctl API for the check. If not, try to
381 * spawn a daemon.
382 */
383 static int check_ltt_sessiond(void)
384 {
385 int ret;
386 char *pathname = NULL;
387
388 ret = lttng_check_session_daemon();
389 if (ret < 0) {
390 /* Try command line option path */
391 if (opt_sessiond_path != NULL) {
392 ret = access(opt_sessiond_path, F_OK | X_OK);
393 if (ret < 0) {
394 ERR("No such file: %s", opt_sessiond_path);
395 goto end;
396 }
397 pathname = opt_sessiond_path;
398 } else {
399 /* Try LTTNG_SESSIOND_PATH env variable */
400 pathname = getenv(LTTNG_SESSIOND_PATH_ENV);
401 if (pathname != NULL) {
402 /* strdup here in order to make the free()
403 * not fail later on.
404 */
405 pathname = strdup(pathname);
406 }
407 }
408
409 /* Let's rock and roll */
410 if (pathname == NULL) {
411 ret = asprintf(&pathname, "ltt-sessiond");
412 if (ret < 0) {
413 goto end;
414 }
415 }
416
417 ret = spawn_sessiond(pathname);
418 free(pathname);
419 if (ret < 0) {
420 ERR("Problem occurs when starting %s", pathname);
421 goto end;
422 }
423 }
424
425 end:
426 return ret;
427 }
428
429 /*
430 * set_signal_handler
431 *
432 * Setup signal handler for SIGCHLD and SIGTERM.
433 */
434 static int set_signal_handler(void)
435 {
436 int ret = 0;
437 struct sigaction sa;
438 sigset_t sigset;
439
440 if ((ret = sigemptyset(&sigset)) < 0) {
441 perror("sigemptyset");
442 goto end;
443 }
444
445 sa.sa_handler = sighandler;
446 sa.sa_mask = sigset;
447 sa.sa_flags = 0;
448 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
449 perror("sigaction");
450 goto end;
451 }
452
453 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
454 perror("sigaction");
455 goto end;
456 }
457
458 end:
459 return ret;
460 }
461
462 /*
463 * sighandler
464 *
465 * Signal handler for the daemon
466 */
467 static void sighandler(int sig)
468 {
469 switch (sig) {
470 case SIGTERM:
471 DBG("SIGTERM catched");
472 clean_exit(EXIT_FAILURE);
473 break;
474 case SIGCHLD:
475 /* Notify is done */
476 DBG("SIGCHLD catched");
477 break;
478 default:
479 DBG("Unknown signal %d catched", sig);
480 break;
481 }
482
483 return;
484 }
485 /*
486 * clean_exit
487 */
488 void clean_exit(int code)
489 {
490 DBG("Clean exit");
491 if (lttng_disconnect_sessiond() < 0) {
492 ERR("Session daemon disconnect failed.");
493 }
494 exit(code);
495 }
496
497 /*
498 * main
499 */
500 int main(int argc, char *argv[])
501 {
502 int ret;
503
504 progname = argv[0] ? argv[0] : "lttng";
505
506 /* For Mathieu Desnoyers aka Dr Tracing */
507 if (strncmp(progname, "drtrace", 7) == 0) {
508 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
509 }
510
511 ret = parse_args(argc, (const char **) argv);
512 if (ret < 0) {
513 clean_exit(EXIT_FAILURE);
514 }
515
516 ret = validate_options();
517 if (ret < 0) {
518 return EXIT_FAILURE;
519 }
520
521 ret = set_signal_handler();
522 if (ret < 0) {
523 clean_exit(ret);
524 }
525
526 if (opt_tracing_group != NULL) {
527 DBG("Set tracing group to '%s'", opt_tracing_group);
528 lttng_set_tracing_group(opt_tracing_group);
529 }
530
531 /* If ask for kernel tracing, need root perms */
532 if (opt_trace_kernel) {
533 DBG("Kernel tracing activated");
534 if (getuid() != 0) {
535 ERR("%s must be setuid root", progname);
536 clean_exit(-EPERM);
537 }
538 }
539
540 /* Check if the lttng session daemon is running.
541 * If no, a daemon will be spawned.
542 */
543 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
544 clean_exit(EXIT_FAILURE);
545 }
546
547 ret = process_client_opt();
548 if (ret < 0) {
549 clean_exit(ret);
550 }
551
552 clean_exit(0);
553
554 return 0;
555 }
This page took 0.038666 seconds and 3 git commands to generate.