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