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