adedf298966c0c7fad786f13bb34a66f4698a6de
[lttng-tools.git] / src / bin / lttng / commands / view.c
1 /*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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.
12 *
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.
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;
32 static char *opt_trace_path;
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},
46 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
47 {0, 0, 0, 0, 0, 0, 0}
48 };
49
50 /*
51 * This is needed for each viewer since we are using execvp().
52 */
53 static const char *babeltrace_opts[] = { "babeltrace" };
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
79 /* Is the session we are trying to view is in live mode. */
80 static int session_live_mode;
81
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");
94 fprintf(ofp, "Options:\n");
95 fprintf(ofp, " -h, --help Show this help\n");
96 fprintf(ofp, " --list-options Simple listing of options\n");
97 fprintf(ofp, " -t, --trace-path PATH Trace directory path for the viewer\n");
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");
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");
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 */
154 argv = malloc(sizeof(char *) * (num_opts + 2));
155 if (argv == NULL) {
156 goto error;
157 }
158
159 token = strtok(opts, " ");
160 while (token != NULL) {
161 argv[i] = strdup(token);
162 token = strtok(NULL, " ");
163 i++;
164 }
165
166 argv[num_opts] = (char *) trace_path;
167 argv[num_opts + 1] = NULL;
168
169 return argv;
170
171 error:
172 return NULL;
173 }
174
175 /*
176 * Alloc an array of string pointer from an array of strings. It also adds
177 * the trace path to the argv.
178 *
179 * The returning pointer is ready to be passed to execvp().
180 */
181 static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
182 const char *trace_path)
183 {
184 char **argv;
185 size_t size, mem_len;
186
187
188 /* Add one for the NULL terminating element. */
189 mem_len = opts_len + 1;
190 if (session_live_mode) {
191 /* Add 3 option for the live mode being "-i lttng-live URL". */
192 mem_len += 3;
193 } else {
194 /* Add option for the trace path. */
195 mem_len += 1;
196 }
197
198 size = sizeof(char *) * mem_len;
199
200 /* Add two here for the trace_path and the NULL terminating element. */
201 argv = malloc(size);
202 if (argv == NULL) {
203 goto error;
204 }
205
206 memcpy(argv, opts, size);
207
208 if (session_live_mode) {
209 argv[opts_len] = "-i";
210 argv[opts_len + 1] = "lttng-live";
211 argv[opts_len + 2] = (char *) trace_path;
212 argv[opts_len + 3] = NULL;
213 } else {
214 argv[opts_len] = (char *) trace_path;
215 argv[opts_len + 1] = NULL;
216 }
217
218 error:
219 return argv;
220 }
221
222 /*
223 * Spawn viewer with the trace directory path.
224 */
225 static int spawn_viewer(const char *trace_path)
226 {
227 int ret = 0;
228 struct stat status;
229 const char *viewer_bin = NULL;
230 struct viewers *viewer;
231 char **argv = NULL;
232
233 /* Check for --viewer options */
234 viewer = parse_options();
235 if (viewer == NULL) {
236 ret = CMD_ERROR;
237 goto error;
238 }
239
240 switch (viewer->type) {
241 case VIEWER_BABELTRACE:
242 if (stat(babeltrace_bin, &status) == 0) {
243 viewer_bin = babeltrace_bin;
244 } else {
245 viewer_bin = viewer->exec_name;
246 }
247 argv = alloc_argv_from_local_opts(babeltrace_opts,
248 ARRAY_SIZE(babeltrace_opts), trace_path);
249 break;
250 #if 0
251 case VIEWER_LTTV_GUI:
252 if (stat(lttv_gui_bin, &status) == 0) {
253 viewer_bin = lttv_gui_bin;
254 } else {
255 viewer_bin = viewer->exec_name;
256 }
257 argv = alloc_argv_from_local_opts(lttv_gui_opts,
258 ARRAY_SIZE(lttv_gui_opts), trace_path);
259 break;
260 #endif
261 case VIEWER_USER_DEFINED:
262 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
263 if (argv) {
264 viewer_bin = argv[0];
265 }
266 break;
267 default:
268 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
269 argv = alloc_argv_from_local_opts(babeltrace_opts,
270 ARRAY_SIZE(babeltrace_opts), trace_path);
271 break;
272 }
273
274 if (argv == NULL) {
275 ret = CMD_FATAL;
276 goto error;
277 }
278
279 DBG("Using %s viewer", viewer_bin);
280
281 ret = execvp(viewer_bin, argv);
282 if (ret) {
283 if (errno == ENOENT) {
284 ERR("%s not found on the system", viewer_bin);
285 } else {
286 PERROR("exec: %s", viewer_bin);
287 }
288 free(argv);
289 ret = CMD_FATAL;
290 goto error;
291 }
292
293 error:
294 return ret;
295 }
296
297 /*
298 * Build the live path we need for the lttng live view.
299 */
300 static char *build_live_path(char *session_name)
301 {
302 int ret;
303 char *path = NULL;
304 char hostname[HOST_NAME_MAX];
305
306 ret = gethostname(hostname, sizeof(hostname));
307 if (ret < 0) {
308 perror("gethostname");
309 goto error;
310 }
311
312 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
313 session_name);
314 if (ret < 0) {
315 perror("asprintf live path");
316 goto error;
317 }
318
319 error:
320 return path;
321 }
322
323 /*
324 * Exec viewer if found and use session name path.
325 */
326 static int view_trace(void)
327 {
328 int ret;
329 char *session_name, *trace_path = NULL;
330 struct lttng_session *sessions = NULL;
331
332 /*
333 * Safety net. If lttng is suid at some point for *any* useless reasons,
334 * this prevent any bad execution of binaries.
335 */
336 if (getuid() != 0) {
337 if (getuid() != geteuid()) {
338 ERR("UID does not match effective UID.");
339 ret = CMD_ERROR;
340 goto error;
341 } else if (getgid() != getegid()) {
342 ERR("GID does not match effective GID.");
343 ret = CMD_ERROR;
344 goto error;
345 }
346 }
347
348 /* User define trace path override the session name */
349 if (opt_trace_path) {
350 session_name = NULL;
351 } else if(opt_session_name == NULL) {
352 session_name = get_session_name();
353 if (session_name == NULL) {
354 ret = CMD_ERROR;
355 goto error;
356 }
357 } else {
358 session_name = opt_session_name;
359 }
360
361 DBG("Viewing trace for session %s", session_name);
362
363 if (session_name) {
364 int i, count, found = 0;
365
366 /* Getting all sessions */
367 count = lttng_list_sessions(&sessions);
368 if (count < 0) {
369 ERR("Unable to list sessions. Session name %s not found.",
370 session_name);
371 MSG("Is there a session daemon running?");
372 ret = CMD_ERROR;
373 goto free_error;
374 }
375
376 /* Find our session listed by the session daemon */
377 for (i = 0; i < count; i++) {
378 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
379 found = 1;
380 break;
381 }
382 }
383
384 if (!found) {
385 MSG("Session name %s not found", session_name);
386 ret = CMD_ERROR;
387 goto free_sessions;
388 }
389
390 session_live_mode = sessions[i].live_timer_interval;
391
392 DBG("Session live mode set to %d", session_live_mode);
393
394 if (sessions[i].enabled && !session_live_mode) {
395 WARN("Session %s is running. Please stop it before reading it.",
396 session_name);
397 ret = CMD_ERROR;
398 goto free_sessions;
399 }
400
401 /* If the timer interval is set we are in live mode. */
402 if (session_live_mode) {
403 trace_path = build_live_path(session_name);
404 if (!trace_path) {
405 ret = CMD_ERROR;
406 goto free_sessions;
407 }
408 } else {
409 /* Get file system session path. */
410 trace_path = sessions[i].path;
411 }
412 } else {
413 trace_path = opt_trace_path;
414 }
415
416 MSG("Trace directory: %s\n", trace_path);
417
418 ret = spawn_viewer(trace_path);
419 if (ret < 0) {
420 /* Don't set ret so lttng can interpret the sessiond error. */
421 goto free_sessions;
422 }
423
424 free_sessions:
425 if (session_live_mode) {
426 free(trace_path);
427 }
428 free(sessions);
429 free_error:
430 if (opt_session_name == NULL) {
431 free(session_name);
432 }
433 error:
434 return ret;
435 }
436
437 /*
438 * The 'view <options>' first level command
439 */
440 int cmd_view(int argc, const char **argv)
441 {
442 int opt, ret = CMD_SUCCESS;
443 static poptContext pc;
444
445 pc = poptGetContext(NULL, argc, argv, long_options, 0);
446 poptReadDefaultConfig(pc, 0);
447
448 while ((opt = poptGetNextOpt(pc)) != -1) {
449 switch (opt) {
450 case OPT_HELP:
451 usage(stdout);
452 goto end;
453 case OPT_LIST_OPTIONS:
454 list_cmd_options(stdout, long_options);
455 goto end;
456 default:
457 usage(stderr);
458 ret = CMD_UNDEFINED;
459 goto end;
460 }
461 }
462
463 opt_session_name = (char*) poptGetArg(pc);
464
465 ret = view_trace();
466
467 end:
468 poptFreeContext(pc);
469 return ret;
470 }
This page took 0.039146 seconds and 4 git commands to generate.