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