lttng-view: make babeltrace2 the default viewer
[lttng-tools.git] / src / bin / lttng / commands / view.c
1 /*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
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, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <popt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include "../command.h"
28
29 static char *opt_session_name;
30 static char *opt_viewer;
31 static char *opt_trace_path;
32 static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN;
33 static const char *babeltrace2_bin = CONFIG_BABELTRACE2_BIN;
34
35 #ifdef LTTNG_EMBED_HELP
36 static const char help_msg[] =
37 #include <lttng-view.1.h>
38 ;
39 #endif
40
41 enum {
42 OPT_HELP = 1,
43 OPT_LIST_OPTIONS,
44 };
45
46 static struct poptOption long_options[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
49 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
50 {"viewer", 'e', POPT_ARG_STRING, &opt_viewer, 0, 0, 0},
51 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
52 {0, 0, 0, 0, 0, 0, 0}
53 };
54
55 /*
56 * This is needed for each viewer since we are using execvp().
57 */
58 static const char *babeltrace_opts[] = { "babeltrace" };
59 static const char *babeltrace2_opts[] = { "babeltrace2" };
60
61 /*
62 * Type is also use as the index in the viewers array. So please, make sure
63 * your enum value is in the right order in the array below.
64 */
65 enum viewer_type {
66 VIEWER_BABELTRACE = 0,
67 VIEWER_BABELTRACE2 = 1,
68 VIEWER_USER_DEFINED = 2,
69 };
70
71 /*
72 * NOTE: "lttv" is a shell command and it's not working for exec() family
73 * functions so we might think of removing this wrapper or using bash.
74 */
75 static const struct viewers {
76 const char *exec_name;
77 enum viewer_type type;
78 } viewers[] = {
79 { "babeltrace", VIEWER_BABELTRACE },
80 { "babeltrace2", VIEWER_BABELTRACE2 },
81 { NULL, VIEWER_USER_DEFINED },
82 };
83
84 /* Is the session we are trying to view is in live mode. */
85 static int session_live_mode;
86
87 static const struct viewers *parse_options(void)
88 {
89 if (opt_viewer == NULL) {
90 /* Default is babeltrace */
91 return &(viewers[VIEWER_BABELTRACE2]);
92 }
93
94 /*
95 * This means that if -e, --viewers is used, we just override everything
96 * with it. For supported viewers like lttv, we could simply detect if "-t"
97 * is passed and if not, add the trace directory to it.
98 */
99 return &(viewers[VIEWER_USER_DEFINED]);
100 }
101
102 /*
103 * Alloc an array of string pointer from a simple string having all options
104 * seperated by spaces. Also adds the trace path to the arguments.
105 *
106 * The returning pointer is ready to be passed to execvp().
107 */
108 static char **alloc_argv_from_user_opts(char *opts, const char *trace_path)
109 {
110 int i = 0, ignore_space = 0;
111 unsigned int num_opts = 1;
112 char **argv, *token = opts;
113
114 /* Count number of arguments. */
115 do {
116 if (*token == ' ') {
117 /* Use to ignore consecutive spaces */
118 if (!ignore_space) {
119 num_opts++;
120 }
121 ignore_space = 1;
122 } else {
123 ignore_space = 0;
124 }
125 token++;
126 } while (*token != '\0');
127
128 /* Add two here for the NULL terminating element and trace path */
129 argv = zmalloc(sizeof(char *) * (num_opts + 2));
130 if (argv == NULL) {
131 goto error;
132 }
133
134 token = strtok(opts, " ");
135 while (token != NULL) {
136 argv[i] = strdup(token);
137 if (argv[i] == NULL) {
138 goto error;
139 }
140 token = strtok(NULL, " ");
141 i++;
142 }
143
144 argv[num_opts] = (char *) trace_path;
145 argv[num_opts + 1] = NULL;
146
147 return argv;
148
149 error:
150 if (argv) {
151 for (i = 0; i < num_opts + 2; i++) {
152 free(argv[i]);
153 }
154 free(argv);
155 }
156
157 return NULL;
158 }
159
160 /*
161 * Alloc an array of string pointer from an array of strings. It also adds
162 * the trace path to the argv.
163 *
164 * The returning pointer is ready to be passed to execvp().
165 */
166 static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
167 const char *trace_path)
168 {
169 char **argv;
170 size_t size, mem_len;
171
172 /* Add one for the NULL terminating element. */
173 mem_len = opts_len + 1;
174 if (session_live_mode) {
175 /* Add 3 option for the live mode being "-i lttng-live URL". */
176 mem_len += 3;
177 } else {
178 /* Add option for the trace path. */
179 mem_len += 1;
180 }
181
182 size = sizeof(char *) * mem_len;
183
184 /* Add two here for the trace_path and the NULL terminating element. */
185 argv = zmalloc(size);
186 if (argv == NULL) {
187 goto error;
188 }
189
190 memcpy(argv, opts, sizeof(char *) * opts_len);
191
192 if (session_live_mode) {
193 argv[opts_len] = "-i";
194 argv[opts_len + 1] = "lttng-live";
195 argv[opts_len + 2] = (char *) trace_path;
196 argv[opts_len + 3] = NULL;
197 } else {
198 argv[opts_len] = (char *) trace_path;
199 argv[opts_len + 1] = NULL;
200 }
201
202 error:
203 return argv;
204 }
205
206 /*
207 * Spawn viewer with the trace directory path.
208 */
209 static int spawn_viewer(const char *trace_path)
210 {
211 int ret = 0;
212 struct stat status;
213 const char *viewer_bin = NULL;
214 const struct viewers *viewer;
215 char **argv = NULL;
216
217 /* Check for --viewer options */
218 viewer = parse_options();
219 if (viewer == NULL) {
220 ret = CMD_ERROR;
221 goto error;
222 }
223
224 retry_viewer:
225 switch (viewer->type) {
226 case VIEWER_BABELTRACE2:
227 if (stat(babeltrace2_bin, &status) == 0) {
228 viewer_bin = babeltrace2_bin;
229 } else {
230 viewer_bin = viewer->exec_name;
231 }
232 argv = alloc_argv_from_local_opts(babeltrace2_opts,
233 ARRAY_SIZE(babeltrace2_opts), trace_path);
234 break;
235 case VIEWER_BABELTRACE:
236 if (stat(babeltrace_bin, &status) == 0) {
237 viewer_bin = babeltrace_bin;
238 } else {
239 viewer_bin = viewer->exec_name;
240 }
241 argv = alloc_argv_from_local_opts(babeltrace_opts,
242 ARRAY_SIZE(babeltrace_opts), trace_path);
243 break;
244 case VIEWER_USER_DEFINED:
245 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
246 if (argv) {
247 viewer_bin = argv[0];
248 }
249 break;
250 default:
251 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
252 argv = alloc_argv_from_local_opts(babeltrace_opts,
253 ARRAY_SIZE(babeltrace_opts), trace_path);
254 break;
255 }
256
257 if (argv == NULL || !viewer_bin) {
258 ret = CMD_FATAL;
259 goto error;
260 }
261
262 DBG("Using %s viewer", viewer_bin);
263
264 ret = execvp(viewer_bin, argv);
265 if (ret) {
266 if (errno == ENOENT && viewer->exec_name) {
267 if (viewer->type == VIEWER_BABELTRACE2) {
268 /* Fallback to legacy babeltrace. */
269 DBG("babeltrace2 not installed on the system, falling back to babeltrace 1.x");
270 viewer = &viewers[VIEWER_BABELTRACE];
271 free(argv);
272 argv = NULL;
273 goto retry_viewer;
274 } else {
275 ERR("Viewer \"%s\" not found on the system",
276 viewer_bin);
277 }
278 } else {
279 PERROR("Failed to launch \"%s\" viewer", viewer_bin);
280 }
281 ret = CMD_FATAL;
282 goto error;
283 }
284
285 error:
286 free(argv);
287 return ret;
288 }
289
290 /*
291 * Build the live path we need for the lttng live view.
292 */
293 static char *build_live_path(char *session_name)
294 {
295 int ret;
296 char *path = NULL;
297 char hostname[HOST_NAME_MAX];
298
299 ret = gethostname(hostname, sizeof(hostname));
300 if (ret < 0) {
301 PERROR("gethostname");
302 goto error;
303 }
304
305 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
306 session_name);
307 if (ret < 0) {
308 PERROR("asprintf live path");
309 goto error;
310 }
311
312 error:
313 return path;
314 }
315
316 /*
317 * Exec viewer if found and use session name path.
318 */
319 static int view_trace(void)
320 {
321 int ret;
322 char *session_name, *trace_path = NULL;
323 struct lttng_session *sessions = NULL;
324 bool free_trace_path = false;
325
326 /*
327 * Safety net. If lttng is suid at some point for *any* useless reasons,
328 * this prevent any bad execution of binaries.
329 */
330 if (getuid() != 0) {
331 if (getuid() != geteuid()) {
332 ERR("UID does not match effective UID.");
333 ret = CMD_ERROR;
334 goto error;
335 } else if (getgid() != getegid()) {
336 ERR("GID does not match effective GID.");
337 ret = CMD_ERROR;
338 goto error;
339 }
340 }
341
342 /* User define trace path override the session name */
343 if (opt_trace_path) {
344 session_name = NULL;
345 } else if(opt_session_name == NULL) {
346 session_name = get_session_name();
347 if (session_name == NULL) {
348 ret = CMD_ERROR;
349 goto error;
350 }
351 } else {
352 session_name = opt_session_name;
353 }
354
355 DBG("Viewing trace for session %s", session_name);
356
357 if (session_name) {
358 int i, count, found = 0;
359
360 /* Getting all sessions */
361 count = lttng_list_sessions(&sessions);
362 if (count < 0) {
363 ERR("Unable to list sessions. Session name %s not found.",
364 session_name);
365 MSG("Is there a session daemon running?");
366 ret = CMD_ERROR;
367 goto free_error;
368 }
369
370 /* Find our session listed by the session daemon */
371 for (i = 0; i < count; i++) {
372 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
373 found = 1;
374 break;
375 }
376 }
377
378 if (!found) {
379 MSG("Session name %s not found", session_name);
380 ret = CMD_ERROR;
381 goto free_sessions;
382 }
383
384 session_live_mode = sessions[i].live_timer_interval;
385
386 DBG("Session live mode set to %d", session_live_mode);
387
388 if (sessions[i].enabled && !session_live_mode) {
389 WARN("Session %s is running. Please stop it before reading it.",
390 session_name);
391 ret = CMD_ERROR;
392 goto free_sessions;
393 }
394
395 /* If the timer interval is set we are in live mode. */
396 if (session_live_mode) {
397 trace_path = build_live_path(session_name);
398 if (!trace_path) {
399 ret = CMD_ERROR;
400 goto free_sessions;
401 }
402 free_trace_path = true;
403 } else {
404 /* Get file system session path. */
405 trace_path = sessions[i].path;
406 }
407 } else {
408 trace_path = opt_trace_path;
409 }
410
411 MSG("Trace directory: %s\n", trace_path);
412
413 ret = spawn_viewer(trace_path);
414 if (ret < 0) {
415 /* Don't set ret so lttng can interpret the sessiond error. */
416 goto free_sessions;
417 }
418
419 free_sessions:
420 if (session_live_mode && free_trace_path) {
421 free(trace_path);
422 }
423 free(sessions);
424 free_error:
425 if (opt_session_name == NULL) {
426 free(session_name);
427 }
428 error:
429 return ret;
430 }
431
432 /*
433 * The 'view <options>' first level command
434 */
435 int cmd_view(int argc, const char **argv)
436 {
437 int opt, ret = CMD_SUCCESS;
438 static poptContext pc;
439 const char *leftover = NULL;
440
441 pc = poptGetContext(NULL, argc, argv, long_options, 0);
442 poptReadDefaultConfig(pc, 0);
443
444 if (lttng_opt_mi) {
445 WARN("mi does not apply to view command");
446 }
447
448 while ((opt = poptGetNextOpt(pc)) != -1) {
449 switch (opt) {
450 case OPT_HELP:
451 SHOW_HELP();
452 goto end;
453 case OPT_LIST_OPTIONS:
454 list_cmd_options(stdout, long_options);
455 goto end;
456 default:
457 ret = CMD_UNDEFINED;
458 goto end;
459 }
460 }
461
462 opt_session_name = (char*) poptGetArg(pc);
463
464 leftover = poptGetArg(pc);
465 if (leftover) {
466 ERR("Unknown argument: %s", leftover);
467 ret = CMD_ERROR;
468 goto end;
469 }
470
471 ret = view_trace();
472
473 end:
474 poptFreeContext(pc);
475 return ret;
476 }
This page took 0.039437 seconds and 5 git commands to generate.