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