Add ust create trace feature
[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);
fac6795d
DG
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 */
56static int process_client_opt(void)
57{
58 int ret;
8028d920 59 uuid_t uuid;
fac6795d
DG
60
61 /* Connect to the session daemon */
62 ret = lttng_connect_sessiond();
63 if (ret < 0) {
fac6795d
DG
64 goto end;
65 }
66
67 if (opt_list_apps) {
68 ret = process_opt_list_apps();
69 if (ret < 0) {
fac6795d
DG
70 goto end;
71 }
72 }
73
57167058
DG
74 if (opt_list_session) {
75 ret = process_opt_list_sessions();
76 if (ret < 0) {
77 goto end;
78 }
79 }
80
aaf97519
DG
81 if (opt_create_session != NULL) {
82 ret = process_opt_create_session();
83 if (ret < 0) {
84 goto end;
85 }
86 }
87
8028d920
DG
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
e8be5f4f
DG
96 if (opt_session_uuid != NULL) {
97 lttng_set_current_session_uuid(opt_session_uuid);
98 }
99
df0da139
DG
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
fac6795d
DG
109 return 0;
110
111end:
ebafd2a5 112 ERR("%s", lttng_get_readable_code(ret));
fac6795d
DG
113 return ret;
114}
115
aaf97519
DG
116/*
117 * process_opt_create_session
118 *
119 * Create a new session using the name pass
120 * to the command line.
121 */
122static int process_opt_create_session(void)
123{
124 int ret;
8028d920
DG
125 uuid_t session_id;
126 char str_uuid[37];
aaf97519
DG
127
128 ret = lttng_create_session(opt_create_session, &session_id);
129 if (ret < 0) {
130 goto error;
131 }
132
8028d920
DG
133 uuid_unparse(session_id, str_uuid);
134
aaf97519 135 MSG("Session created:");
8028d920 136 MSG(" %s (%s)", opt_create_session, str_uuid);
aaf97519
DG
137
138error:
139 return ret;
140}
141
57167058
DG
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 */
148static 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
169error:
170 return ret;
171}
172
fac6795d
DG
173/*
174 * process_opt_list_apps
175 *
176 * Get the UST traceable pid list and print
177 * them to the user.
178 */
179static int process_opt_list_apps(void)
180{
e8f07c63 181 int i, ret, count;
fac6795d
DG
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
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++) {
fac6795d
DG
195 snprintf(path, sizeof(path), "/proc/%d/cmdline", pids[i]);
196 fp = fopen(path, "r");
197 if (fp == NULL) {
e8f07c63 198 MSG("\t(not running) (%d)", pids[i]);
fac6795d
DG
199 continue;
200 }
201 ret = fread(cmdline, 1, sizeof(cmdline), fp);
202 MSG("\t%s (%d)", cmdline, pids[i]);
203 fclose(fp);
204 }
205
e065084a
DG
206 /* Allocated by lttng_ust_list_apps() */
207 free(pids);
208
fac6795d
DG
209 return 0;
210
211error:
212 return ret;
213}
214
5b8719f5
DG
215/*
216 * spawn_sessiond
217 *
218 * Spawn a session daemon by forking and execv.
219 */
220static 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 */
75462a81 231 ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
5b8719f5
DG
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
252end:
253 return ret;
254}
255
fac6795d
DG
256/*
257 * check_ltt_sessiond
258 *
259 * Check if the session daemon is available using
5b8719f5
DG
260 * the liblttngctl API for the check. If not, try to
261 * spawn a daemon.
fac6795d
DG
262 */
263static int check_ltt_sessiond(void)
264{
265 int ret;
5b8719f5 266 char *pathname = NULL;
fac6795d
DG
267
268 ret = lttng_check_session_daemon();
269 if (ret < 0) {
5b8719f5
DG
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 */
e8f07c63
DG
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 }
5b8719f5
DG
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 }
fac6795d
DG
303 }
304
5b8719f5 305end:
fac6795d
DG
306 return ret;
307}
308
5b8719f5
DG
309/*
310 * set_signal_handler
311 *
312 * Setup signal handler for SIGCHLD and SIGTERM.
313 */
314static 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 }
fac6795d 332
5b8719f5
DG
333 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
334 perror("sigaction");
335 goto end;
336 }
337
338end:
339 return ret;
340}
341
342/*
343 * sighandler
344 *
345 * Signal handler for the daemon
346 */
347static 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}
fac6795d
DG
363/*
364 * clean_exit
365 */
366void clean_exit(int code)
367{
368 DBG("Clean exit");
87378cf5
DG
369 if (lttng_disconnect_sessiond() < 0) {
370 ERR("Session daemon disconnect failed.");
371 }
fac6795d
DG
372 exit(code);
373}
374
375/*
5b8719f5 376 * main
fac6795d
DG
377 */
378int 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) {
87378cf5 391 clean_exit(EXIT_FAILURE);
fac6795d
DG
392 }
393
5b8719f5
DG
394 ret = set_signal_handler();
395 if (ret < 0) {
87378cf5 396 clean_exit(ret);
5b8719f5
DG
397 }
398
fac6795d
DG
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);
87378cf5 409 clean_exit(-EPERM);
fac6795d
DG
410 }
411 }
412
413 /* Check if the lttng session daemon is running.
414 * If no, a daemon will be spawned.
415 */
5b8719f5 416 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
87378cf5 417 clean_exit(EXIT_FAILURE);
fac6795d
DG
418 }
419
420 ret = process_client_opt();
421 if (ret < 0) {
87378cf5 422 clean_exit(ret);
fac6795d
DG
423 }
424
87378cf5
DG
425 clean_exit(0);
426
fac6795d
DG
427 return 0;
428}
This page took 0.06123 seconds and 4 git commands to generate.