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