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