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 | |
6c1c0768 | 19 | #define _LGPL_SOURCE |
0c95f5b2 DG |
20 | #include <popt.h> |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <sys/stat.h> | |
25 | #include <sys/types.h> | |
26 | #include <unistd.h> | |
27 | ||
28 | #include "../command.h" | |
29 | #include <config.h> | |
30 | ||
31 | static char *opt_session_name; | |
32 | static char *opt_viewer; | |
f1f887c0 | 33 | static char *opt_trace_path; |
0c95f5b2 DG |
34 | static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN; |
35 | //static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN; | |
36 | ||
37 | enum { | |
38 | OPT_HELP = 1, | |
39 | OPT_LIST_OPTIONS, | |
40 | }; | |
41 | ||
42 | static struct poptOption long_options[] = { | |
43 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
44 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
45 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
46 | {"viewer", 'e', POPT_ARG_STRING, &opt_viewer, 0, 0, 0}, | |
f1f887c0 | 47 | {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0}, |
0c95f5b2 DG |
48 | {0, 0, 0, 0, 0, 0, 0} |
49 | }; | |
50 | ||
51 | /* | |
52 | * This is needed for each viewer since we are using execvp(). | |
53 | */ | |
36ef520f | 54 | static const char *babeltrace_opts[] = { "babeltrace" }; |
0c95f5b2 DG |
55 | //static const char *lttv_gui_opts[] = { "lttv-gui", "-t", }; |
56 | ||
57 | /* | |
58 | * Type is also use as the index in the viewers array. So please, make sure | |
59 | * your enum value is in the right order in the array below. | |
60 | */ | |
61 | enum viewer_type { | |
62 | VIEWER_BABELTRACE = 0, | |
63 | VIEWER_LTTV_GUI = 1, | |
64 | VIEWER_USER_DEFINED = 2, | |
65 | }; | |
66 | ||
67 | /* | |
68 | * NOTE: "lttv" is a shell command and it's not working for exec() family | |
69 | * functions so we might think of removing this wrapper or using bash. | |
70 | */ | |
71 | static struct viewers { | |
72 | const char *exec_name; | |
73 | enum viewer_type type; | |
74 | } viewers[] = { | |
75 | { "babeltrace", VIEWER_BABELTRACE }, | |
76 | { "lttv-gui", VIEWER_LTTV_GUI }, | |
77 | { NULL, VIEWER_USER_DEFINED }, | |
78 | }; | |
79 | ||
8960e9cd DG |
80 | /* Is the session we are trying to view is in live mode. */ |
81 | static int session_live_mode; | |
82 | ||
0c95f5b2 DG |
83 | /* |
84 | * usage | |
85 | */ | |
86 | static void usage(FILE *ofp) | |
87 | { | |
88 | fprintf(ofp, "usage: lttng view [SESSION_NAME] [OPTIONS]\n"); | |
89 | fprintf(ofp, "\n"); | |
90 | fprintf(ofp, "By default, the babeltrace viewer will be used for text viewing\n"); | |
91 | fprintf(ofp, "\n"); | |
92 | fprintf(ofp, "Where SESSION_NAME is an optional session name. If not specified, lttng will\n"); | |
93 | fprintf(ofp, "get it from the configuration file (.lttngrc).\n"); | |
94 | fprintf(ofp, "\n"); | |
32a6298d | 95 | fprintf(ofp, "Options:\n"); |
0c95f5b2 DG |
96 | fprintf(ofp, " -h, --help Show this help\n"); |
97 | fprintf(ofp, " --list-options Simple listing of options\n"); | |
f1f887c0 | 98 | fprintf(ofp, " -t, --trace-path PATH Trace directory path for the viewer\n"); |
0c95f5b2 DG |
99 | fprintf(ofp, " -e, --viewer CMD Specify viewer and/or options to use\n"); |
100 | fprintf(ofp, " This will completely override the default viewers so\n"); | |
f1f887c0 DG |
101 | fprintf(ofp, " please make sure to specify the full command. The trace\n"); |
102 | fprintf(ofp, " directory path of the session will be appended at the end\n"); | |
103 | fprintf(ofp, " to the arguments\n"); | |
0c95f5b2 DG |
104 | fprintf(ofp, "\n"); |
105 | } | |
106 | ||
107 | static struct viewers *parse_options(void) | |
108 | { | |
109 | if (opt_viewer == NULL) { | |
110 | /* Default is babeltrace */ | |
111 | return &(viewers[VIEWER_BABELTRACE]); | |
112 | } | |
113 | ||
0c95f5b2 DG |
114 | /* |
115 | * This means that if -e, --viewers is used, we just override everything | |
116 | * with it. For supported viewers like lttv, we could simply detect if "-t" | |
117 | * is passed and if not, add the trace directory to it. | |
118 | */ | |
119 | return &(viewers[VIEWER_USER_DEFINED]); | |
120 | } | |
121 | ||
122 | /* | |
123 | * Alloc an array of string pointer from a simple string having all options | |
124 | * seperated by spaces. Also adds the trace path to the arguments. | |
125 | * | |
126 | * The returning pointer is ready to be passed to execvp(). | |
127 | */ | |
128 | static char **alloc_argv_from_user_opts(char *opts, const char *trace_path) | |
129 | { | |
130 | int i = 0, ignore_space = 0; | |
131 | unsigned int num_opts = 1; | |
132 | char **argv, *token = opts; | |
133 | ||
134 | /* Count number of arguments. */ | |
135 | do { | |
136 | if (*token == ' ') { | |
137 | /* Use to ignore consecutive spaces */ | |
138 | if (!ignore_space) { | |
139 | num_opts++; | |
140 | } | |
141 | ignore_space = 1; | |
142 | } else { | |
143 | ignore_space = 0; | |
144 | } | |
145 | token++; | |
146 | } while (*token != '\0'); | |
147 | ||
148 | /* Add two here for the NULL terminating element and trace path */ | |
1025878e | 149 | argv = zmalloc(sizeof(char *) * (num_opts + 2)); |
0c95f5b2 DG |
150 | if (argv == NULL) { |
151 | goto error; | |
152 | } | |
153 | ||
154 | token = strtok(opts, " "); | |
155 | while (token != NULL) { | |
156 | argv[i] = strdup(token); | |
44721eb2 MD |
157 | if (argv[i] == NULL) { |
158 | goto error; | |
159 | } | |
0c95f5b2 DG |
160 | token = strtok(NULL, " "); |
161 | i++; | |
162 | } | |
163 | ||
164 | argv[num_opts] = (char *) trace_path; | |
165 | argv[num_opts + 1] = NULL; | |
166 | ||
167 | return argv; | |
168 | ||
169 | error: | |
879ba548 JG |
170 | if (argv) { |
171 | for (i = 0; i < num_opts + 2; i++) { | |
172 | free(argv[i]); | |
173 | } | |
174 | free(argv); | |
175 | } | |
176 | ||
0c95f5b2 DG |
177 | return NULL; |
178 | } | |
179 | ||
180 | /* | |
181 | * Alloc an array of string pointer from an array of strings. It also adds | |
182 | * the trace path to the argv. | |
183 | * | |
184 | * The returning pointer is ready to be passed to execvp(). | |
185 | */ | |
186 | static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len, | |
187 | const char *trace_path) | |
188 | { | |
189 | char **argv; | |
8960e9cd DG |
190 | size_t size, mem_len; |
191 | ||
192 | ||
193 | /* Add one for the NULL terminating element. */ | |
194 | mem_len = opts_len + 1; | |
195 | if (session_live_mode) { | |
196 | /* Add 3 option for the live mode being "-i lttng-live URL". */ | |
197 | mem_len += 3; | |
198 | } else { | |
199 | /* Add option for the trace path. */ | |
200 | mem_len += 1; | |
201 | } | |
0c95f5b2 | 202 | |
8960e9cd | 203 | size = sizeof(char *) * mem_len; |
0c95f5b2 DG |
204 | |
205 | /* Add two here for the trace_path and the NULL terminating element. */ | |
1025878e | 206 | argv = zmalloc(size); |
0c95f5b2 DG |
207 | if (argv == NULL) { |
208 | goto error; | |
209 | } | |
210 | ||
211 | memcpy(argv, opts, size); | |
212 | ||
8960e9cd DG |
213 | if (session_live_mode) { |
214 | argv[opts_len] = "-i"; | |
215 | argv[opts_len + 1] = "lttng-live"; | |
216 | argv[opts_len + 2] = (char *) trace_path; | |
217 | argv[opts_len + 3] = NULL; | |
218 | } else { | |
219 | argv[opts_len] = (char *) trace_path; | |
220 | argv[opts_len + 1] = NULL; | |
221 | } | |
0c95f5b2 DG |
222 | |
223 | error: | |
224 | return argv; | |
225 | } | |
226 | ||
227 | /* | |
228 | * Spawn viewer with the trace directory path. | |
229 | */ | |
230 | static int spawn_viewer(const char *trace_path) | |
231 | { | |
232 | int ret = 0; | |
0c95f5b2 DG |
233 | struct stat status; |
234 | const char *viewer_bin = NULL; | |
235 | struct viewers *viewer; | |
236 | char **argv = NULL; | |
237 | ||
238 | /* Check for --viewer options */ | |
239 | viewer = parse_options(); | |
240 | if (viewer == NULL) { | |
241 | ret = CMD_ERROR; | |
242 | goto error; | |
243 | } | |
244 | ||
85a68078 DG |
245 | switch (viewer->type) { |
246 | case VIEWER_BABELTRACE: | |
247 | if (stat(babeltrace_bin, &status) == 0) { | |
248 | viewer_bin = babeltrace_bin; | |
249 | } else { | |
250 | viewer_bin = viewer->exec_name; | |
251 | } | |
252 | argv = alloc_argv_from_local_opts(babeltrace_opts, | |
253 | ARRAY_SIZE(babeltrace_opts), trace_path); | |
254 | break; | |
85a68078 DG |
255 | case VIEWER_USER_DEFINED: |
256 | argv = alloc_argv_from_user_opts(opt_viewer, trace_path); | |
257 | if (argv) { | |
258 | viewer_bin = argv[0]; | |
0c95f5b2 | 259 | } |
85a68078 DG |
260 | break; |
261 | default: | |
262 | viewer_bin = viewers[VIEWER_BABELTRACE].exec_name; | |
263 | argv = alloc_argv_from_local_opts(babeltrace_opts, | |
264 | ARRAY_SIZE(babeltrace_opts), trace_path); | |
265 | break; | |
266 | } | |
0c95f5b2 | 267 | |
57138fbd | 268 | if (argv == NULL || !viewer_bin) { |
85a68078 DG |
269 | ret = CMD_FATAL; |
270 | goto error; | |
271 | } | |
0c95f5b2 | 272 | |
85a68078 | 273 | DBG("Using %s viewer", viewer_bin); |
0c95f5b2 | 274 | |
85a68078 DG |
275 | ret = execvp(viewer_bin, argv); |
276 | if (ret) { | |
0c687325 JD |
277 | if (errno == ENOENT) { |
278 | ERR("%s not found on the system", viewer_bin); | |
279 | } else { | |
280 | PERROR("exec: %s", viewer_bin); | |
281 | } | |
85a68078 | 282 | free(argv); |
0c95f5b2 | 283 | ret = CMD_FATAL; |
85a68078 | 284 | goto error; |
0c95f5b2 DG |
285 | } |
286 | ||
287 | error: | |
288 | return ret; | |
289 | } | |
290 | ||
8960e9cd DG |
291 | /* |
292 | * Build the live path we need for the lttng live view. | |
293 | */ | |
294 | static char *build_live_path(char *session_name) | |
295 | { | |
296 | int ret; | |
297 | char *path = NULL; | |
298 | char hostname[HOST_NAME_MAX]; | |
299 | ||
300 | ret = gethostname(hostname, sizeof(hostname)); | |
301 | if (ret < 0) { | |
6f04ed72 | 302 | PERROR("gethostname"); |
8960e9cd DG |
303 | goto error; |
304 | } | |
305 | ||
306 | ret = asprintf(&path, "net://localhost/host/%s/%s", hostname, | |
307 | session_name); | |
308 | if (ret < 0) { | |
6f04ed72 | 309 | PERROR("asprintf live path"); |
8960e9cd DG |
310 | goto error; |
311 | } | |
312 | ||
313 | error: | |
314 | return path; | |
315 | } | |
316 | ||
0c95f5b2 DG |
317 | /* |
318 | * Exec viewer if found and use session name path. | |
319 | */ | |
320 | static int view_trace(void) | |
321 | { | |
c617c0c6 | 322 | int ret; |
8960e9cd | 323 | char *session_name, *trace_path = NULL; |
0c95f5b2 DG |
324 | struct lttng_session *sessions = NULL; |
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 | } | |
402 | } else { | |
403 | /* Get file system session path. */ | |
404 | trace_path = sessions[i].path; | |
405 | } | |
f1f887c0 DG |
406 | } else { |
407 | trace_path = opt_trace_path; | |
0c95f5b2 DG |
408 | } |
409 | ||
f1f887c0 | 410 | MSG("Trace directory: %s\n", trace_path); |
0c95f5b2 | 411 | |
f1f887c0 | 412 | ret = spawn_viewer(trace_path); |
0c95f5b2 DG |
413 | if (ret < 0) { |
414 | /* Don't set ret so lttng can interpret the sessiond error. */ | |
415 | goto free_sessions; | |
416 | } | |
417 | ||
0c95f5b2 | 418 | free_sessions: |
8960e9cd DG |
419 | if (session_live_mode) { |
420 | free(trace_path); | |
421 | } | |
0e428499 | 422 | free(sessions); |
0c95f5b2 DG |
423 | free_error: |
424 | if (opt_session_name == NULL) { | |
425 | free(session_name); | |
426 | } | |
427 | error: | |
428 | return ret; | |
429 | } | |
430 | ||
431 | /* | |
432 | * The 'view <options>' first level command | |
433 | */ | |
434 | int cmd_view(int argc, const char **argv) | |
435 | { | |
436 | int opt, ret = CMD_SUCCESS; | |
437 | static poptContext pc; | |
438 | ||
439 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
440 | poptReadDefaultConfig(pc, 0); | |
441 | ||
c7e35b03 JR |
442 | if (lttng_opt_mi) { |
443 | WARN("mi does not apply to view command"); | |
444 | } | |
445 | ||
0c95f5b2 DG |
446 | while ((opt = poptGetNextOpt(pc)) != -1) { |
447 | switch (opt) { | |
448 | case OPT_HELP: | |
449 | usage(stdout); | |
450 | goto end; | |
451 | case OPT_LIST_OPTIONS: | |
452 | list_cmd_options(stdout, long_options); | |
453 | goto end; | |
454 | default: | |
455 | usage(stderr); | |
456 | ret = CMD_UNDEFINED; | |
457 | goto end; | |
458 | } | |
459 | } | |
460 | ||
461 | opt_session_name = (char*) poptGetArg(pc); | |
462 | ||
463 | ret = view_trace(); | |
464 | ||
465 | end: | |
466 | poptFreeContext(pc); | |
467 | return ret; | |
468 | } |