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