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