lttng-view: make babeltrace2 the default viewer
[lttng-tools.git] / src / bin / lttng / commands / view.c
CommitLineData
0c95f5b2
DG
1/*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 *
d14d33bf
AM
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.
0c95f5b2 7 *
d14d33bf
AM
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.
0c95f5b2 12 *
d14d33bf
AM
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.
0c95f5b2
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
0c95f5b2
DG
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"
0c95f5b2
DG
28
29static char *opt_session_name;
30static char *opt_viewer;
f1f887c0 31static char *opt_trace_path;
0c95f5b2 32static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN;
3cd887b3 33static const char *babeltrace2_bin = CONFIG_BABELTRACE2_BIN;
0c95f5b2 34
4fc83d94
PP
35#ifdef LTTNG_EMBED_HELP
36static const char help_msg[] =
37#include <lttng-view.1.h>
38;
39#endif
40
0c95f5b2
DG
41enum {
42 OPT_HELP = 1,
43 OPT_LIST_OPTIONS,
44};
45
46static 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},
f1f887c0 51 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
0c95f5b2
DG
52 {0, 0, 0, 0, 0, 0, 0}
53};
54
55/*
56 * This is needed for each viewer since we are using execvp().
57 */
36ef520f 58static const char *babeltrace_opts[] = { "babeltrace" };
3cd887b3 59static const char *babeltrace2_opts[] = { "babeltrace2" };
0c95f5b2
DG
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 */
65enum viewer_type {
66 VIEWER_BABELTRACE = 0,
3cd887b3
JG
67 VIEWER_BABELTRACE2 = 1,
68 VIEWER_USER_DEFINED = 2,
0c95f5b2
DG
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 */
45167256 75static const struct viewers {
0c95f5b2
DG
76 const char *exec_name;
77 enum viewer_type type;
78} viewers[] = {
79 { "babeltrace", VIEWER_BABELTRACE },
3cd887b3 80 { "babeltrace2", VIEWER_BABELTRACE2 },
0c95f5b2
DG
81 { NULL, VIEWER_USER_DEFINED },
82};
83
8960e9cd
DG
84/* Is the session we are trying to view is in live mode. */
85static int session_live_mode;
86
45167256 87static const struct viewers *parse_options(void)
0c95f5b2
DG
88{
89 if (opt_viewer == NULL) {
90 /* Default is babeltrace */
3cd887b3 91 return &(viewers[VIEWER_BABELTRACE2]);
0c95f5b2
DG
92 }
93
0c95f5b2
DG
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 */
108static 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 */
1025878e 129 argv = zmalloc(sizeof(char *) * (num_opts + 2));
0c95f5b2
DG
130 if (argv == NULL) {
131 goto error;
132 }
133
134 token = strtok(opts, " ");
135 while (token != NULL) {
136 argv[i] = strdup(token);
44721eb2
MD
137 if (argv[i] == NULL) {
138 goto error;
139 }
0c95f5b2
DG
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
149error:
879ba548
JG
150 if (argv) {
151 for (i = 0; i < num_opts + 2; i++) {
152 free(argv[i]);
153 }
154 free(argv);
155 }
156
0c95f5b2
DG
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 */
166static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
167 const char *trace_path)
168{
169 char **argv;
8960e9cd
DG
170 size_t size, mem_len;
171
8960e9cd
DG
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 }
0c95f5b2 181
8960e9cd 182 size = sizeof(char *) * mem_len;
0c95f5b2
DG
183
184 /* Add two here for the trace_path and the NULL terminating element. */
1025878e 185 argv = zmalloc(size);
0c95f5b2
DG
186 if (argv == NULL) {
187 goto error;
188 }
189
e20ca024 190 memcpy(argv, opts, sizeof(char *) * opts_len);
0c95f5b2 191
8960e9cd
DG
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 }
0c95f5b2
DG
201
202error:
203 return argv;
204}
205
206/*
207 * Spawn viewer with the trace directory path.
208 */
209static int spawn_viewer(const char *trace_path)
210{
211 int ret = 0;
0c95f5b2
DG
212 struct stat status;
213 const char *viewer_bin = NULL;
45167256 214 const struct viewers *viewer;
0c95f5b2
DG
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
3cd887b3 224retry_viewer:
85a68078 225 switch (viewer->type) {
3cd887b3
JG
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;
85a68078
DG
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;
85a68078
DG
244 case VIEWER_USER_DEFINED:
245 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
246 if (argv) {
247 viewer_bin = argv[0];
0c95f5b2 248 }
85a68078
DG
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 }
0c95f5b2 256
57138fbd 257 if (argv == NULL || !viewer_bin) {
85a68078
DG
258 ret = CMD_FATAL;
259 goto error;
260 }
0c95f5b2 261
85a68078 262 DBG("Using %s viewer", viewer_bin);
0c95f5b2 263
85a68078
DG
264 ret = execvp(viewer_bin, argv);
265 if (ret) {
3cd887b3
JG
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 }
0c687325 278 } else {
3cd887b3 279 PERROR("Failed to launch \"%s\" viewer", viewer_bin);
0c687325 280 }
0c95f5b2 281 ret = CMD_FATAL;
85a68078 282 goto error;
0c95f5b2
DG
283 }
284
285error:
45da08a2 286 free(argv);
0c95f5b2
DG
287 return ret;
288}
289
8960e9cd
DG
290/*
291 * Build the live path we need for the lttng live view.
292 */
293static 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) {
6f04ed72 301 PERROR("gethostname");
8960e9cd
DG
302 goto error;
303 }
304
305 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
306 session_name);
307 if (ret < 0) {
6f04ed72 308 PERROR("asprintf live path");
8960e9cd
DG
309 goto error;
310 }
311
312error:
313 return path;
314}
315
0c95f5b2
DG
316/*
317 * Exec viewer if found and use session name path.
318 */
319static int view_trace(void)
320{
c617c0c6 321 int ret;
8960e9cd 322 char *session_name, *trace_path = NULL;
0c95f5b2 323 struct lttng_session *sessions = NULL;
f12e3556 324 bool free_trace_path = false;
0c95f5b2
DG
325
326 /*
327 * Safety net. If lttng is suid at some point for *any* useless reasons,
f1f887c0 328 * this prevent any bad execution of binaries.
0c95f5b2
DG
329 */
330 if (getuid() != 0) {
331 if (getuid() != geteuid()) {
332 ERR("UID does not match effective UID.");
6e9261f4 333 ret = CMD_ERROR;
0c95f5b2
DG
334 goto error;
335 } else if (getgid() != getegid()) {
336 ERR("GID does not match effective GID.");
6e9261f4 337 ret = CMD_ERROR;
0c95f5b2
DG
338 goto error;
339 }
340 }
341
f1f887c0
DG
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) {
0c95f5b2
DG
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
f1f887c0 357 if (session_name) {
c617c0c6
MD
358 int i, count, found = 0;
359
f1f887c0
DG
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 }
0c95f5b2 369
f1f887c0
DG
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 }
0c95f5b2 376 }
0c95f5b2 377
f1f887c0
DG
378 if (!found) {
379 MSG("Session name %s not found", session_name);
380 ret = CMD_ERROR;
381 goto free_sessions;
382 }
383
8960e9cd
DG
384 session_live_mode = sessions[i].live_timer_interval;
385
386 DBG("Session live mode set to %d", session_live_mode);
3822545d 387
8960e9cd 388 if (sessions[i].enabled && !session_live_mode) {
3822545d
DG
389 WARN("Session %s is running. Please stop it before reading it.",
390 session_name);
391 ret = CMD_ERROR;
392 goto free_sessions;
393 }
8960e9cd
DG
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 }
f12e3556 402 free_trace_path = true;
8960e9cd
DG
403 } else {
404 /* Get file system session path. */
405 trace_path = sessions[i].path;
406 }
f1f887c0
DG
407 } else {
408 trace_path = opt_trace_path;
0c95f5b2
DG
409 }
410
f1f887c0 411 MSG("Trace directory: %s\n", trace_path);
0c95f5b2 412
f1f887c0 413 ret = spawn_viewer(trace_path);
0c95f5b2
DG
414 if (ret < 0) {
415 /* Don't set ret so lttng can interpret the sessiond error. */
416 goto free_sessions;
417 }
418
0c95f5b2 419free_sessions:
f12e3556 420 if (session_live_mode && free_trace_path) {
8960e9cd
DG
421 free(trace_path);
422 }
0e428499 423 free(sessions);
0c95f5b2
DG
424free_error:
425 if (opt_session_name == NULL) {
426 free(session_name);
427 }
428error:
429 return ret;
430}
431
432/*
433 * The 'view <options>' first level command
434 */
435int cmd_view(int argc, const char **argv)
436{
437 int opt, ret = CMD_SUCCESS;
438 static poptContext pc;
68c7f6e5 439 const char *leftover = NULL;
0c95f5b2
DG
440
441 pc = poptGetContext(NULL, argc, argv, long_options, 0);
442 poptReadDefaultConfig(pc, 0);
443
c7e35b03
JR
444 if (lttng_opt_mi) {
445 WARN("mi does not apply to view command");
446 }
447
0c95f5b2
DG
448 while ((opt = poptGetNextOpt(pc)) != -1) {
449 switch (opt) {
450 case OPT_HELP:
4ba92f18 451 SHOW_HELP();
0c95f5b2
DG
452 goto end;
453 case OPT_LIST_OPTIONS:
454 list_cmd_options(stdout, long_options);
455 goto end;
456 default:
0c95f5b2
DG
457 ret = CMD_UNDEFINED;
458 goto end;
459 }
460 }
461
462 opt_session_name = (char*) poptGetArg(pc);
463
68c7f6e5
JD
464 leftover = poptGetArg(pc);
465 if (leftover) {
466 ERR("Unknown argument: %s", leftover);
467 ret = CMD_ERROR;
468 goto end;
469 }
470
0c95f5b2
DG
471 ret = view_trace();
472
473end:
474 poptFreeContext(pc);
475 return ret;
476}
This page took 0.067971 seconds and 4 git commands to generate.