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