Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / bin / lttng / commands / view.c
1 /*
2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
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"
18
19 static char *opt_session_name;
20 static char *opt_viewer;
21 static char *opt_trace_path;
22 static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN;
23 static const char *babeltrace2_bin = CONFIG_BABELTRACE2_BIN;
24
25 #ifdef LTTNG_EMBED_HELP
26 static const char help_msg[] =
27 #include <lttng-view.1.h>
28 ;
29 #endif
30
31 enum {
32 OPT_HELP = 1,
33 OPT_LIST_OPTIONS,
34 };
35
36 static 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},
41 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
42 {0, 0, 0, 0, 0, 0, 0}
43 };
44
45 /*
46 * This is needed for each viewer since we are using execvp().
47 */
48 static const char *babeltrace_opts[] = { "babeltrace" };
49 static const char *babeltrace2_opts[] = { "babeltrace2" };
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 */
55 enum viewer_type {
56 VIEWER_BABELTRACE = 0,
57 VIEWER_BABELTRACE2 = 1,
58 VIEWER_USER_DEFINED = 2,
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 */
65 static const struct viewers {
66 const char *exec_name;
67 enum viewer_type type;
68 } viewers[] = {
69 { "babeltrace", VIEWER_BABELTRACE },
70 { "babeltrace2", VIEWER_BABELTRACE2 },
71 { NULL, VIEWER_USER_DEFINED },
72 };
73
74 /* Is the session we are trying to view is in live mode. */
75 static int session_live_mode;
76
77 static const struct viewers *parse_options(void)
78 {
79 if (opt_viewer == NULL) {
80 /* Default is babeltrace */
81 return &(viewers[VIEWER_BABELTRACE2]);
82 }
83
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 */
98 static 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 */
119 argv = zmalloc(sizeof(char *) * (num_opts + 2));
120 if (argv == NULL) {
121 goto error;
122 }
123
124 token = strtok(opts, " ");
125 while (token != NULL) {
126 argv[i] = strdup(token);
127 if (argv[i] == NULL) {
128 goto error;
129 }
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
139 error:
140 if (argv) {
141 for (i = 0; i < num_opts + 2; i++) {
142 free(argv[i]);
143 }
144 free(argv);
145 }
146
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 */
156 static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
157 const char *trace_path)
158 {
159 char **argv;
160 size_t size, mem_len;
161
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 }
171
172 size = sizeof(char *) * mem_len;
173
174 /* Add two here for the trace_path and the NULL terminating element. */
175 argv = zmalloc(size);
176 if (argv == NULL) {
177 goto error;
178 }
179
180 memcpy(argv, opts, sizeof(char *) * opts_len);
181
182 if (session_live_mode) {
183 argv[opts_len] = "-i";
184 argv[opts_len + 1] = "lttng-live";
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 }
191
192 error:
193 return argv;
194 }
195
196 /*
197 * Spawn viewer with the trace directory path.
198 */
199 static int spawn_viewer(const char *trace_path)
200 {
201 int ret = 0;
202 struct stat status;
203 const char *viewer_bin = NULL;
204 const struct viewers *viewer;
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
214 retry_viewer:
215 switch (viewer->type) {
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;
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;
234 case VIEWER_USER_DEFINED:
235 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
236 if (argv) {
237 viewer_bin = argv[0];
238 }
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 }
246
247 if (argv == NULL || !viewer_bin) {
248 ret = CMD_FATAL;
249 goto error;
250 }
251
252 DBG("Using %s viewer", viewer_bin);
253
254 ret = execvp(viewer_bin, argv);
255 if (ret) {
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 }
268 } else {
269 PERROR("Failed to launch \"%s\" viewer", viewer_bin);
270 }
271 ret = CMD_FATAL;
272 goto error;
273 }
274
275 error:
276 free(argv);
277 return ret;
278 }
279
280 /*
281 * Build the live path we need for the lttng live view.
282 */
283 static 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) {
291 PERROR("gethostname");
292 goto error;
293 }
294
295 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
296 session_name);
297 if (ret < 0) {
298 PERROR("asprintf live path");
299 goto error;
300 }
301
302 error:
303 return path;
304 }
305
306 /*
307 * Exec viewer if found and use session name path.
308 */
309 static int view_trace(void)
310 {
311 int ret;
312 char *session_name, *trace_path = NULL;
313 struct lttng_session *sessions = NULL;
314 bool free_trace_path = false;
315
316 /*
317 * Safety net. If lttng is suid at some point for *any* useless reasons,
318 * this prevent any bad execution of binaries.
319 */
320 if (getuid() != 0) {
321 if (getuid() != geteuid()) {
322 ERR("UID does not match effective UID.");
323 ret = CMD_ERROR;
324 goto error;
325 } else if (getgid() != getegid()) {
326 ERR("GID does not match effective GID.");
327 ret = CMD_ERROR;
328 goto error;
329 }
330 }
331
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) {
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
347 if (session_name) {
348 int i, count, found = 0;
349
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 }
359
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 }
366 }
367
368 if (!found) {
369 MSG("Session name %s not found", session_name);
370 ret = CMD_ERROR;
371 goto free_sessions;
372 }
373
374 session_live_mode = sessions[i].live_timer_interval;
375
376 DBG("Session live mode set to %d", session_live_mode);
377
378 if (sessions[i].enabled && !session_live_mode) {
379 WARN("Session %s is running. Please stop it before reading it.",
380 session_name);
381 ret = CMD_ERROR;
382 goto free_sessions;
383 }
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 }
392 free_trace_path = true;
393 } else {
394 /* Get file system session path. */
395 trace_path = sessions[i].path;
396 }
397 } else {
398 trace_path = opt_trace_path;
399 }
400
401 MSG("Trace directory: %s\n", trace_path);
402
403 ret = spawn_viewer(trace_path);
404 if (ret < 0) {
405 /* Don't set ret so lttng can interpret the sessiond error. */
406 goto free_sessions;
407 }
408
409 free_sessions:
410 if (session_live_mode && free_trace_path) {
411 free(trace_path);
412 }
413 free(sessions);
414 free_error:
415 if (opt_session_name == NULL) {
416 free(session_name);
417 }
418 error:
419 return ret;
420 }
421
422 /*
423 * The 'view <options>' first level command
424 */
425 int cmd_view(int argc, const char **argv)
426 {
427 int opt, ret = CMD_SUCCESS;
428 static poptContext pc;
429 const char *leftover = NULL;
430
431 pc = poptGetContext(NULL, argc, argv, long_options, 0);
432 poptReadDefaultConfig(pc, 0);
433
434 if (lttng_opt_mi) {
435 WARN("mi does not apply to view command");
436 }
437
438 while ((opt = poptGetNextOpt(pc)) != -1) {
439 switch (opt) {
440 case OPT_HELP:
441 SHOW_HELP();
442 goto end;
443 case OPT_LIST_OPTIONS:
444 list_cmd_options(stdout, long_options);
445 goto end;
446 default:
447 ret = CMD_UNDEFINED;
448 goto end;
449 }
450 }
451
452 opt_session_name = (char*) poptGetArg(pc);
453
454 leftover = poptGetArg(pc);
455 if (leftover) {
456 ERR("Unknown argument: %s", leftover);
457 ret = CMD_ERROR;
458 goto end;
459 }
460
461 ret = view_trace();
462
463 end:
464 poptFreeContext(pc);
465 return ret;
466 }
This page took 0.039008 seconds and 5 git commands to generate.