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 | ||
5b97ec60 | 33 | #include <lttng/lttng.h> |
fac6795d | 34 | |
fac6795d | 35 | #include "lttngerr.h" |
64a23ac4 | 36 | #include "options.h" |
fac6795d DG |
37 | |
38 | /* Variables */ | |
39 | static char *progname; | |
96243366 | 40 | static char *session_name; |
96243366 | 41 | static uuid_t current_uuid; |
894be886 DG |
42 | static int auto_session; |
43 | static int auto_trace; | |
fac6795d DG |
44 | |
45 | /* Prototypes */ | |
46 | static int process_client_opt(void); | |
47 | static int process_opt_list_apps(void); | |
57167058 | 48 | static int process_opt_list_sessions(void); |
1657e9bb | 49 | static int process_opt_list_traces(void); |
2ef84c95 | 50 | static int process_opt_kernel_list_events(void); |
aaf97519 | 51 | static int process_opt_create_session(void); |
894be886 DG |
52 | static int process_kernel_create_trace(void); |
53 | static int process_opt_kernel_event(void); | |
8c0faa1d | 54 | static int process_kernel_start_trace(void); |
96243366 | 55 | static int set_session_uuid(void); |
5b8719f5 DG |
56 | static void sighandler(int sig); |
57 | static int set_signal_handler(void); | |
8548ff30 | 58 | static int validate_options(void); |
47b74d63 | 59 | static char *get_cmdline_by_pid(pid_t pid); |
96243366 | 60 | static void set_opt_session_info(void); |
fac6795d DG |
61 | |
62 | /* | |
63 | * start_client | |
64 | * | |
65 | * Process client request from the command line | |
66 | * options. Every tracing action is done by the | |
67 | * liblttngctl API. | |
68 | */ | |
69 | static int process_client_opt(void) | |
70 | { | |
71 | int ret; | |
96243366 DG |
72 | |
73 | set_opt_session_info(); | |
fac6795d | 74 | |
fac6795d DG |
75 | if (opt_list_apps) { |
76 | ret = process_opt_list_apps(); | |
77 | if (ret < 0) { | |
fac6795d DG |
78 | goto end; |
79 | } | |
894be886 | 80 | goto error; |
fac6795d DG |
81 | } |
82 | ||
57167058 DG |
83 | if (opt_list_session) { |
84 | ret = process_opt_list_sessions(); | |
85 | if (ret < 0) { | |
86 | goto end; | |
87 | } | |
894be886 | 88 | goto error; |
57167058 DG |
89 | } |
90 | ||
2ef84c95 DG |
91 | if (opt_list_events) { |
92 | if (opt_trace_kernel) { | |
93 | ret = process_opt_kernel_list_events(); | |
94 | if (ret < 0) { | |
95 | goto end; | |
96 | } | |
97 | } else if (opt_trace_pid != 0) { | |
98 | // TODO | |
99 | } | |
100 | goto error; | |
101 | } | |
102 | ||
894be886 DG |
103 | /* Session creation or auto session set on */ |
104 | if (auto_session || opt_create_session) { | |
105 | DBG("Creating a new session"); | |
106 | ret = process_opt_create_session(); | |
1657e9bb DG |
107 | if (ret < 0) { |
108 | goto end; | |
109 | } | |
110 | } | |
111 | ||
894be886 DG |
112 | ret = set_session_uuid(); |
113 | if (ret < 0) { | |
114 | ERR("Session %s not found", opt_session_name); | |
115 | goto error; | |
116 | } | |
aaf97519 | 117 | |
894be886 DG |
118 | if (opt_destroy_session) { |
119 | ret = lttng_destroy_session(¤t_uuid); | |
8028d920 | 120 | if (ret < 0) { |
894be886 | 121 | goto end; |
8028d920 | 122 | } |
894be886 | 123 | MSG("Session %s destroyed.", opt_session_name); |
8028d920 DG |
124 | } |
125 | ||
96243366 DG |
126 | if (opt_list_traces) { |
127 | ret = process_opt_list_traces(); | |
7442b2ba | 128 | if (ret < 0) { |
96243366 | 129 | goto end; |
7442b2ba | 130 | } |
e8be5f4f DG |
131 | } |
132 | ||
96243366 DG |
133 | /* |
134 | * Action on traces (kernel or/and userspace). | |
135 | */ | |
894be886 | 136 | |
ad874cce | 137 | if (opt_trace_kernel) { |
894be886 DG |
138 | if (auto_trace || opt_create_trace) { |
139 | DBG("Creating a kernel trace"); | |
140 | ret = process_kernel_create_trace(); | |
141 | if (ret < 0) { | |
142 | goto end; | |
143 | } | |
144 | } | |
145 | ||
146 | if (opt_event_list != NULL) { | |
147 | ret = process_opt_kernel_event(); | |
f34daff7 DG |
148 | if (ret < 0) { |
149 | goto end; | |
150 | } | |
894be886 DG |
151 | } else { |
152 | // Enable all events | |
153 | } | |
8c0faa1d DG |
154 | |
155 | if (auto_trace || opt_start_trace) { | |
156 | DBG("Starting kernel tracing"); | |
157 | ret = process_kernel_start_trace(); | |
158 | if (ret < 0) { | |
159 | goto end; | |
160 | } | |
161 | } | |
162 | ||
163 | if (opt_stop_trace) { | |
164 | DBG("Stopping kernel tracing"); | |
165 | ret = lttng_kernel_stop_tracing(); | |
166 | if (ret < 0) { | |
167 | goto end; | |
168 | } | |
169 | } | |
df0da139 DG |
170 | } |
171 | ||
ad874cce | 172 | if (opt_trace_pid != 0) { |
894be886 | 173 | if (auto_trace || opt_create_trace) { |
ad874cce DG |
174 | DBG("Create a userspace trace for pid %d", opt_trace_pid); |
175 | ret = lttng_ust_create_trace(opt_trace_pid); | |
176 | if (ret < 0) { | |
177 | goto end; | |
178 | } | |
894be886 | 179 | MSG("Trace created successfully!"); |
ce3d728c | 180 | } |
ce3d728c | 181 | |
894be886 | 182 | if (auto_trace || opt_start_trace) { |
ad874cce DG |
183 | DBG("Start trace for pid %d", opt_trace_pid); |
184 | ret = lttng_ust_start_trace(opt_trace_pid); | |
185 | if (ret < 0) { | |
186 | goto end; | |
187 | } | |
188 | MSG("Trace started successfully!"); | |
189 | } else if (opt_stop_trace) { | |
190 | DBG("Stop trace for pid %d", opt_trace_pid); | |
191 | ret = lttng_ust_stop_trace(opt_trace_pid); | |
192 | if (ret < 0) { | |
193 | goto end; | |
194 | } | |
195 | MSG("Trace stopped successfully!"); | |
520ff687 | 196 | } |
ad874cce | 197 | |
520ff687 DG |
198 | } |
199 | ||
fac6795d DG |
200 | return 0; |
201 | ||
202 | end: | |
ebafd2a5 | 203 | ERR("%s", lttng_get_readable_code(ret)); |
5e16da05 | 204 | error: /* fall through */ |
7442b2ba DG |
205 | return ret; |
206 | } | |
207 | ||
8c0faa1d DG |
208 | /* |
209 | * process_kernel_start_trace | |
210 | * | |
211 | * Start a kernel trace. | |
212 | */ | |
213 | static int process_kernel_start_trace(void) | |
214 | { | |
215 | int ret; | |
216 | ||
217 | ret = lttng_kernel_create_stream(); | |
218 | if (ret < 0) { | |
219 | goto error; | |
220 | } | |
221 | ||
222 | ret = lttng_kernel_start_tracing(); | |
223 | if (ret < 0) { | |
224 | goto error; | |
225 | } | |
226 | ||
227 | MSG("Kernel tracing started"); | |
228 | ||
229 | return 0; | |
230 | ||
231 | error: | |
232 | return ret; | |
233 | } | |
234 | ||
7442b2ba | 235 | /* |
894be886 | 236 | * process_kernel_create_trace |
96243366 | 237 | * |
894be886 | 238 | * Create a kernel trace. |
96243366 | 239 | */ |
894be886 | 240 | static int process_kernel_create_trace(void) |
96243366 | 241 | { |
20fe2104 DG |
242 | int ret; |
243 | ||
244 | /* Setup kernel session */ | |
245 | ret = lttng_kernel_create_session(); | |
246 | if (ret < 0) { | |
247 | goto error; | |
248 | } | |
249 | ||
250 | /* Create an empty channel (with no event) */ | |
251 | ret = lttng_kernel_create_channel(); | |
252 | if (ret < 0) { | |
253 | goto error; | |
a5c5a2bd DG |
254 | } |
255 | ||
256 | /* Opening metadata for session */ | |
257 | ret = lttng_kernel_open_metadata(); | |
258 | if (ret < 0) { | |
259 | goto error; | |
20fe2104 DG |
260 | } |
261 | ||
894be886 | 262 | return 0; |
20fe2104 DG |
263 | |
264 | error: | |
265 | return ret; | |
894be886 | 266 | } |
96243366 | 267 | |
894be886 | 268 | /* |
2ef84c95 DG |
269 | * process_opt_kernel_list_events |
270 | * | |
271 | * Ask for all trace events in the kernel and pretty print them. | |
272 | */ | |
273 | static int process_opt_kernel_list_events(void) | |
274 | { | |
275 | int ret, pos, size; | |
276 | char *event_list, *event, *ptr; | |
277 | ||
278 | DBG("Getting all tracing events"); | |
279 | ||
280 | ret = lttng_kernel_list_events(&event_list); | |
281 | if (ret < 0) { | |
282 | ERR("Unable to list events."); | |
283 | return ret; | |
284 | } | |
285 | ||
286 | MSG("Kernel tracepoints:\n-------------"); | |
287 | ||
288 | ptr = event_list; | |
289 | while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) { | |
290 | MSG(" - %s", event); | |
291 | /* Move pointer to the next line */ | |
292 | ptr += pos + 1; | |
293 | free(event); | |
294 | } | |
295 | ||
296 | free(event_list); | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
301 | /* | |
302 | * process_opt_kernel_event | |
894be886 DG |
303 | * |
304 | * Enable kernel event from the command line list given. | |
305 | */ | |
306 | static int process_opt_kernel_event(void) | |
307 | { | |
308 | int ret; | |
309 | char *event_name; | |
96243366 | 310 | |
894be886 DG |
311 | event_name = strtok(opt_event_list, ","); |
312 | while (event_name != NULL) { | |
313 | DBG("Enabling kernel event %s", event_name); | |
314 | ret = lttng_kernel_enable_event(event_name); | |
315 | if (ret < 0) { | |
316 | ERR("%s %s", lttng_get_readable_code(ret), event_name); | |
317 | } else { | |
318 | MSG("Kernel event %s enabled.", event_name); | |
96243366 | 319 | } |
894be886 DG |
320 | /* Next event */ |
321 | event_name = strtok(NULL, ","); | |
96243366 DG |
322 | } |
323 | ||
894be886 | 324 | return 0; |
96243366 DG |
325 | } |
326 | ||
327 | /* | |
894be886 | 328 | * set_opt_session_info |
96243366 | 329 | * |
894be886 DG |
330 | * Setup session_name, current_uuid, short_str_uuid and |
331 | * long_str_uuid using the command line options. | |
96243366 | 332 | */ |
894be886 | 333 | static void set_opt_session_info(void) |
96243366 | 334 | { |
894be886 DG |
335 | if (opt_session_name != NULL) { |
336 | session_name = strndup(opt_session_name, NAME_MAX); | |
337 | DBG("Session name set to %s", session_name); | |
338 | } | |
96243366 DG |
339 | } |
340 | ||
341 | /* | |
342 | * set_session_uuid | |
7442b2ba | 343 | * |
894be886 DG |
344 | * Set current session uuid to the current flow of command(s) using the |
345 | * session_name. | |
7442b2ba | 346 | */ |
96243366 | 347 | static int set_session_uuid(void) |
7442b2ba | 348 | { |
894be886 | 349 | int ret, count, i, found = 0; |
7442b2ba DG |
350 | struct lttng_session *sessions; |
351 | ||
96243366 DG |
352 | if (!uuid_is_null(current_uuid)) { |
353 | lttng_set_current_session_uuid(¤t_uuid); | |
354 | goto end; | |
355 | } | |
356 | ||
7442b2ba DG |
357 | count = lttng_list_sessions(&sessions); |
358 | if (count < 0) { | |
359 | ret = count; | |
360 | goto error; | |
361 | } | |
362 | ||
363 | for (i = 0; i < count; i++) { | |
894be886 | 364 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
96243366 | 365 | lttng_set_current_session_uuid(&sessions[i].uuid); |
894be886 DG |
366 | uuid_copy(current_uuid, sessions[i].uuid); |
367 | found = 1; | |
7442b2ba DG |
368 | break; |
369 | } | |
370 | } | |
371 | ||
372 | free(sessions); | |
373 | ||
894be886 DG |
374 | if (!found) { |
375 | return -1; | |
376 | } | |
377 | ||
96243366 | 378 | end: |
894be886 | 379 | DBG("Session UUID set"); |
7442b2ba DG |
380 | return 0; |
381 | ||
382 | error: | |
383 | return ret; | |
fac6795d DG |
384 | } |
385 | ||
1657e9bb DG |
386 | /* |
387 | * process_opt_list_traces | |
388 | * | |
389 | * Get list of all traces for a specific session uuid. | |
390 | */ | |
391 | static int process_opt_list_traces(void) | |
392 | { | |
393 | int ret, i; | |
1657e9bb DG |
394 | struct lttng_trace *traces; |
395 | ||
96243366 | 396 | ret = lttng_list_traces(¤t_uuid, &traces); |
894be886 | 397 | DBG("Number of traces to list %d", ret); |
1657e9bb DG |
398 | if (ret < 0) { |
399 | goto error; | |
400 | } | |
401 | ||
894be886 DG |
402 | /* No traces */ |
403 | if (ret == 0) { | |
404 | MSG("No traces found."); | |
405 | goto error; | |
406 | } | |
407 | ||
1657e9bb DG |
408 | MSG("Userspace traces:"); |
409 | for (i = 0; i < ret; i++) { | |
410 | if (traces[i].type == USERSPACE) { | |
47b74d63 DG |
411 | MSG("\t%d) %s (pid: %d): %s", |
412 | i, traces[i].name, traces[i].pid, | |
413 | get_cmdline_by_pid(traces[i].pid)); | |
1657e9bb DG |
414 | } else { |
415 | break; | |
416 | } | |
417 | } | |
418 | ||
419 | MSG("Kernel traces:"); | |
420 | for (;i < ret; i++) { | |
421 | if (traces[i].type == KERNEL) { | |
422 | MSG("\t%d) %s", i, traces[i].name); | |
423 | } | |
424 | } | |
425 | ||
426 | free(traces); | |
427 | ||
428 | error: | |
429 | return ret; | |
430 | } | |
431 | ||
aaf97519 DG |
432 | /* |
433 | * process_opt_create_session | |
434 | * | |
435 | * Create a new session using the name pass | |
436 | * to the command line. | |
437 | */ | |
438 | static int process_opt_create_session(void) | |
439 | { | |
440 | int ret; | |
96243366 DG |
441 | char name[NAME_MAX]; |
442 | time_t rawtime; | |
443 | struct tm *timeinfo; | |
444 | ||
894be886 DG |
445 | /* Auto session name creation */ |
446 | if (opt_session_name == NULL) { | |
96243366 DG |
447 | time(&rawtime); |
448 | timeinfo = localtime(&rawtime); | |
894be886 | 449 | strftime(name, sizeof(name), "auto-%Y%m%d-%H%M%S", timeinfo); |
96243366 | 450 | session_name = strndup(name, sizeof(name)); |
894be886 | 451 | DBG("Auto session name set to %s", session_name); |
96243366 | 452 | } |
aaf97519 | 453 | |
894be886 | 454 | ret = lttng_create_session(session_name); |
aaf97519 DG |
455 | if (ret < 0) { |
456 | goto error; | |
457 | } | |
458 | ||
894be886 | 459 | MSG("Session created: %s", session_name); |
aaf97519 DG |
460 | |
461 | error: | |
462 | return ret; | |
463 | } | |
464 | ||
57167058 DG |
465 | /* |
466 | * process_opt_list_sessions | |
467 | * | |
468 | * Get the list of available sessions from | |
469 | * the session daemon and print it to user. | |
470 | */ | |
471 | static int process_opt_list_sessions(void) | |
472 | { | |
473 | int ret, count, i; | |
894be886 | 474 | struct lttng_session *sessions; |
57167058 | 475 | |
894be886 | 476 | count = lttng_list_sessions(&sessions); |
7442b2ba | 477 | DBG("Session count %d", count); |
57167058 DG |
478 | if (count < 0) { |
479 | ret = count; | |
480 | goto error; | |
481 | } | |
482 | ||
7442b2ba | 483 | MSG("Available sessions (UUIDs):"); |
57167058 | 484 | for (i = 0; i < count; i++) { |
894be886 | 485 | MSG(" %d) %s", i+1, sessions[i].name); |
57167058 DG |
486 | } |
487 | ||
894be886 | 488 | free(sessions); |
7442b2ba | 489 | MSG("\nTo select a session, use -s, --session UUID."); |
57167058 DG |
490 | |
491 | return 0; | |
492 | ||
493 | error: | |
494 | return ret; | |
495 | } | |
496 | ||
fac6795d DG |
497 | /* |
498 | * process_opt_list_apps | |
499 | * | |
500 | * Get the UST traceable pid list and print | |
501 | * them to the user. | |
502 | */ | |
503 | static int process_opt_list_apps(void) | |
504 | { | |
e8f07c63 | 505 | int i, ret, count; |
fac6795d | 506 | pid_t *pids; |
1c9f7941 | 507 | char *cmdline; |
fac6795d | 508 | |
e8f07c63 DG |
509 | count = lttng_ust_list_apps(&pids); |
510 | if (count < 0) { | |
511 | ret = count; | |
fac6795d DG |
512 | goto error; |
513 | } | |
514 | ||
515 | MSG("LTTng UST traceable application [name (pid)]:"); | |
e8f07c63 | 516 | for (i=0; i < count; i++) { |
47b74d63 DG |
517 | cmdline = get_cmdline_by_pid(pids[i]); |
518 | if (cmdline == NULL) { | |
e8f07c63 | 519 | MSG("\t(not running) (%d)", pids[i]); |
fac6795d DG |
520 | continue; |
521 | } | |
fac6795d | 522 | MSG("\t%s (%d)", cmdline, pids[i]); |
1c9f7941 | 523 | free(cmdline); |
fac6795d DG |
524 | } |
525 | ||
e065084a DG |
526 | /* Allocated by lttng_ust_list_apps() */ |
527 | free(pids); | |
528 | ||
fac6795d DG |
529 | return 0; |
530 | ||
531 | error: | |
532 | return ret; | |
533 | } | |
534 | ||
1c9f7941 DG |
535 | /* |
536 | * get_cmdline_by_pid | |
537 | * | |
47b74d63 | 538 | * Get command line from /proc for a specific pid. |
1c9f7941 | 539 | * |
47b74d63 DG |
540 | * On success, return an allocated string pointer pointing to |
541 | * the proc cmdline. | |
542 | * On error, return NULL. | |
1c9f7941 | 543 | */ |
47b74d63 | 544 | static char *get_cmdline_by_pid(pid_t pid) |
1c9f7941 DG |
545 | { |
546 | int ret; | |
547 | FILE *fp; | |
47b74d63 | 548 | char *cmdline = NULL; |
1c9f7941 DG |
549 | char path[24]; /* Can't go bigger than /proc/65535/cmdline */ |
550 | ||
551 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); | |
552 | fp = fopen(path, "r"); | |
553 | if (fp == NULL) { | |
47b74d63 | 554 | goto end; |
1c9f7941 DG |
555 | } |
556 | ||
557 | /* Caller must free() *cmdline */ | |
47b74d63 DG |
558 | cmdline = malloc(PATH_MAX); |
559 | ret = fread(cmdline, 1, PATH_MAX, fp); | |
1c9f7941 DG |
560 | fclose(fp); |
561 | ||
47b74d63 DG |
562 | end: |
563 | return cmdline; | |
1c9f7941 DG |
564 | } |
565 | ||
8548ff30 DG |
566 | /* |
567 | * validate_options | |
568 | * | |
894be886 DG |
569 | * Make sure that all options passed to the command line are compatible with |
570 | * each others. | |
8548ff30 DG |
571 | * |
572 | * On error, return -1 | |
573 | * On success, return 0 | |
574 | */ | |
575 | static int validate_options(void) | |
576 | { | |
894be886 DG |
577 | /* If listing options, jump validation */ |
578 | if (opt_list_apps || opt_list_session) { | |
579 | goto end; | |
580 | } | |
ad874cce DG |
581 | /* Conflicting command */ |
582 | if (opt_start_trace && opt_stop_trace) { | |
583 | ERR("Can't use --start and --stop together."); | |
584 | goto error; | |
ad874cce | 585 | /* If no PID specified and trace_kernel is off */ |
894be886 DG |
586 | } else if ((opt_trace_pid == 0 && !opt_trace_kernel) && |
587 | (opt_create_trace || opt_start_trace || opt_stop_trace || opt_destroy_trace)) { | |
588 | ERR("Please specify for which tracer (-k or -p PID)."); | |
96243366 | 589 | goto error; |
894be886 DG |
590 | /* List traces, we need a session name */ |
591 | } else if (opt_list_traces && opt_session_name == NULL) { | |
96243366 DG |
592 | ERR("Can't use -t without -s, --session option."); |
593 | goto error; | |
894be886 DG |
594 | /* Can't set event for both kernel and userspace at the same time */ |
595 | } else if (opt_event_list != NULL && (opt_trace_kernel && opt_trace_pid)) { | |
596 | ERR("Please don't use --event for both kernel and userspace.\nOne at a time to enable events."); | |
597 | goto error; | |
598 | /* Don't need a trace name for kernel tracig */ | |
599 | } else if (opt_trace_name != NULL && opt_trace_kernel) { | |
600 | ERR("For action on a kernel trace, please don't specify a trace name."); | |
601 | goto error; | |
602 | } else if (opt_destroy_trace && opt_session_name == NULL) { | |
603 | ERR("Please specify a session in order to destroy a trace"); | |
604 | goto error; | |
605 | } else if (opt_create_trace || opt_destroy_trace) { | |
606 | /* Both kernel and user-space are denied for these options */ | |
607 | if (opt_trace_pid != 0 && opt_trace_kernel) { | |
608 | ERR("Kernel and user-space trace creation and destruction can't be used together."); | |
609 | goto error; | |
610 | /* Need a trace name for user-space tracing */ | |
611 | } else if (opt_trace_name == NULL && opt_trace_pid != 0) { | |
612 | ERR("Please specify a trace name for user-space tracing"); | |
613 | goto error; | |
614 | } | |
615 | } else if (opt_stop_trace && opt_trace_pid != 0 && opt_trace_name == NULL) { | |
616 | ERR("Please specify a trace name for user-space tracing"); | |
617 | goto error; | |
6d8076ad DG |
618 | } else if (opt_stop_trace && opt_session_name == NULL) { |
619 | ERR("Please specify a session to stop tracing"); | |
620 | goto error; | |
7442b2ba DG |
621 | } |
622 | ||
894be886 | 623 | /* If start trace, auto start tracing */ |
f34daff7 | 624 | if (opt_start_trace || opt_event_list != NULL) { |
894be886 DG |
625 | DBG("Requesting auto tracing"); |
626 | auto_trace = 1; | |
627 | } | |
628 | ||
629 | /* If no session, auto create one */ | |
630 | if (opt_session_name == NULL) { | |
631 | DBG("Requesting an auto session creation"); | |
632 | auto_session = 1; | |
633 | } | |
634 | ||
635 | end: | |
8548ff30 DG |
636 | return 0; |
637 | ||
638 | error: | |
639 | return -1; | |
640 | } | |
641 | ||
5b8719f5 DG |
642 | /* |
643 | * spawn_sessiond | |
644 | * | |
645 | * Spawn a session daemon by forking and execv. | |
646 | */ | |
647 | static int spawn_sessiond(char *pathname) | |
648 | { | |
649 | int ret = 0; | |
650 | pid_t pid; | |
651 | ||
652 | MSG("Spawning session daemon"); | |
653 | pid = fork(); | |
654 | if (pid == 0) { | |
5e16da05 MD |
655 | /* |
656 | * Spawn session daemon and tell | |
5b8719f5 DG |
657 | * it to signal us when ready. |
658 | */ | |
5e16da05 MD |
659 | execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL); |
660 | /* execlp only returns if error happened */ | |
661 | if (errno == ENOENT) { | |
662 | ERR("No session daemon found. Use --sessiond-path."); | |
663 | } else { | |
664 | perror("execlp"); | |
5b8719f5 | 665 | } |
5e16da05 MD |
666 | kill(getppid(), SIGTERM); /* unpause parent */ |
667 | exit(EXIT_FAILURE); | |
5b8719f5 DG |
668 | } else if (pid > 0) { |
669 | /* Wait for ltt-sessiond to start */ | |
670 | pause(); | |
671 | goto end; | |
672 | } else { | |
673 | perror("fork"); | |
674 | ret = -1; | |
675 | goto end; | |
676 | } | |
677 | ||
678 | end: | |
679 | return ret; | |
680 | } | |
681 | ||
fac6795d DG |
682 | /* |
683 | * check_ltt_sessiond | |
684 | * | |
685 | * Check if the session daemon is available using | |
5b8719f5 DG |
686 | * the liblttngctl API for the check. If not, try to |
687 | * spawn a daemon. | |
fac6795d DG |
688 | */ |
689 | static int check_ltt_sessiond(void) | |
690 | { | |
691 | int ret; | |
5e16da05 | 692 | char *pathname = NULL, *alloc_pathname = NULL; |
fac6795d | 693 | |
947308c4 DG |
694 | ret = lttng_session_daemon_alive(); |
695 | if (ret == 0) { /* not alive */ | |
5b8719f5 DG |
696 | /* Try command line option path */ |
697 | if (opt_sessiond_path != NULL) { | |
698 | ret = access(opt_sessiond_path, F_OK | X_OK); | |
699 | if (ret < 0) { | |
700 | ERR("No such file: %s", opt_sessiond_path); | |
701 | goto end; | |
702 | } | |
703 | pathname = opt_sessiond_path; | |
704 | } else { | |
705 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
e8f07c63 | 706 | pathname = getenv(LTTNG_SESSIOND_PATH_ENV); |
5b8719f5 DG |
707 | } |
708 | ||
709 | /* Let's rock and roll */ | |
710 | if (pathname == NULL) { | |
5e16da05 | 711 | ret = asprintf(&alloc_pathname, "ltt-sessiond"); |
5b8719f5 DG |
712 | if (ret < 0) { |
713 | goto end; | |
714 | } | |
5e16da05 | 715 | pathname = alloc_pathname; |
5b8719f5 DG |
716 | } |
717 | ||
718 | ret = spawn_sessiond(pathname); | |
5e16da05 | 719 | free(alloc_pathname); |
5b8719f5 DG |
720 | if (ret < 0) { |
721 | ERR("Problem occurs when starting %s", pathname); | |
722 | goto end; | |
723 | } | |
fac6795d DG |
724 | } |
725 | ||
5b8719f5 | 726 | end: |
fac6795d DG |
727 | return ret; |
728 | } | |
729 | ||
5b8719f5 DG |
730 | /* |
731 | * set_signal_handler | |
732 | * | |
733 | * Setup signal handler for SIGCHLD and SIGTERM. | |
734 | */ | |
735 | static int set_signal_handler(void) | |
736 | { | |
737 | int ret = 0; | |
738 | struct sigaction sa; | |
739 | sigset_t sigset; | |
740 | ||
741 | if ((ret = sigemptyset(&sigset)) < 0) { | |
742 | perror("sigemptyset"); | |
743 | goto end; | |
744 | } | |
745 | ||
746 | sa.sa_handler = sighandler; | |
747 | sa.sa_mask = sigset; | |
748 | sa.sa_flags = 0; | |
749 | if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) { | |
750 | perror("sigaction"); | |
751 | goto end; | |
752 | } | |
fac6795d | 753 | |
5b8719f5 DG |
754 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
755 | perror("sigaction"); | |
756 | goto end; | |
757 | } | |
758 | ||
759 | end: | |
760 | return ret; | |
761 | } | |
762 | ||
763 | /* | |
764 | * sighandler | |
765 | * | |
766 | * Signal handler for the daemon | |
767 | */ | |
768 | static void sighandler(int sig) | |
769 | { | |
5b8719f5 DG |
770 | switch (sig) { |
771 | case SIGTERM: | |
ce3d728c | 772 | DBG("SIGTERM catched"); |
5b8719f5 DG |
773 | clean_exit(EXIT_FAILURE); |
774 | break; | |
775 | case SIGCHLD: | |
776 | /* Notify is done */ | |
ce3d728c | 777 | DBG("SIGCHLD catched"); |
5b8719f5 DG |
778 | break; |
779 | default: | |
ce3d728c | 780 | DBG("Unknown signal %d catched", sig); |
5b8719f5 DG |
781 | break; |
782 | } | |
783 | ||
784 | return; | |
785 | } | |
7442b2ba | 786 | |
fac6795d DG |
787 | /* |
788 | * clean_exit | |
789 | */ | |
790 | void clean_exit(int code) | |
791 | { | |
792 | DBG("Clean exit"); | |
96243366 DG |
793 | if (session_name) { |
794 | free(session_name); | |
795 | } | |
796 | ||
fac6795d DG |
797 | exit(code); |
798 | } | |
799 | ||
800 | /* | |
5b8719f5 | 801 | * main |
fac6795d DG |
802 | */ |
803 | int main(int argc, char *argv[]) | |
804 | { | |
805 | int ret; | |
806 | ||
807 | progname = argv[0] ? argv[0] : "lttng"; | |
808 | ||
809 | /* For Mathieu Desnoyers aka Dr Tracing */ | |
810 | if (strncmp(progname, "drtrace", 7) == 0) { | |
811 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0); | |
812 | } | |
813 | ||
814 | ret = parse_args(argc, (const char **) argv); | |
815 | if (ret < 0) { | |
87378cf5 | 816 | clean_exit(EXIT_FAILURE); |
fac6795d DG |
817 | } |
818 | ||
8548ff30 DG |
819 | ret = validate_options(); |
820 | if (ret < 0) { | |
821 | return EXIT_FAILURE; | |
822 | } | |
823 | ||
5b8719f5 DG |
824 | ret = set_signal_handler(); |
825 | if (ret < 0) { | |
87378cf5 | 826 | clean_exit(ret); |
5b8719f5 DG |
827 | } |
828 | ||
fac6795d DG |
829 | if (opt_tracing_group != NULL) { |
830 | DBG("Set tracing group to '%s'", opt_tracing_group); | |
831 | lttng_set_tracing_group(opt_tracing_group); | |
832 | } | |
833 | ||
834 | /* If ask for kernel tracing, need root perms */ | |
835 | if (opt_trace_kernel) { | |
836 | DBG("Kernel tracing activated"); | |
837 | if (getuid() != 0) { | |
838 | ERR("%s must be setuid root", progname); | |
87378cf5 | 839 | clean_exit(-EPERM); |
fac6795d DG |
840 | } |
841 | } | |
842 | ||
843 | /* Check if the lttng session daemon is running. | |
844 | * If no, a daemon will be spawned. | |
845 | */ | |
5b8719f5 | 846 | if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) { |
87378cf5 | 847 | clean_exit(EXIT_FAILURE); |
fac6795d DG |
848 | } |
849 | ||
850 | ret = process_client_opt(); | |
851 | if (ret < 0) { | |
87378cf5 | 852 | clean_exit(ret); |
fac6795d DG |
853 | } |
854 | ||
87378cf5 DG |
855 | clean_exit(0); |
856 | ||
fac6795d DG |
857 | return 0; |
858 | } |