430f8b62cfd208375691f3dda72ceb64f9ee881a
[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 /*
80 * usage
81 */
82 static void usage(FILE *ofp)
83 {
84 fprintf(ofp, "usage: lttng view [SESSION_NAME] [OPTIONS]\n");
85 fprintf(ofp, "\n");
86 fprintf(ofp, "By default, the babeltrace viewer will be used for text viewing\n");
87 fprintf(ofp, "\n");
88 fprintf(ofp, "Where SESSION_NAME is an optional session name. If not specified, lttng will\n");
89 fprintf(ofp, "get it from the configuration file (.lttngrc).\n");
90 fprintf(ofp, "\n");
91 fprintf(ofp, "Options:\n");
92 fprintf(ofp, " -h, --help Show this help\n");
93 fprintf(ofp, " --list-options Simple listing of options\n");
94 fprintf(ofp, " -t, --trace-path PATH Trace directory path for the viewer\n");
95 fprintf(ofp, " -e, --viewer CMD Specify viewer and/or options to use\n");
96 fprintf(ofp, " This will completely override the default viewers so\n");
97 fprintf(ofp, " please make sure to specify the full command. The trace\n");
98 fprintf(ofp, " directory path of the session will be appended at the end\n");
99 fprintf(ofp, " to the arguments\n");
100 fprintf(ofp, "\n");
101 }
102
103 static struct viewers *parse_options(void)
104 {
105 if (opt_viewer == NULL) {
106 /* Default is babeltrace */
107 return &(viewers[VIEWER_BABELTRACE]);
108 }
109
110 #if 0
111 if (strstr(opt_viewer, viewers[VIEWER_LTTV_GUI].exec_name) == 0) {
112 return &(viewers[VIEWER_LTTV_GUI]);
113 }
114 #endif
115
116 /*
117 * This means that if -e, --viewers is used, we just override everything
118 * with it. For supported viewers like lttv, we could simply detect if "-t"
119 * is passed and if not, add the trace directory to it.
120 */
121 return &(viewers[VIEWER_USER_DEFINED]);
122 }
123
124 /*
125 * Alloc an array of string pointer from a simple string having all options
126 * seperated by spaces. Also adds the trace path to the arguments.
127 *
128 * The returning pointer is ready to be passed to execvp().
129 */
130 static char **alloc_argv_from_user_opts(char *opts, const char *trace_path)
131 {
132 int i = 0, ignore_space = 0;
133 unsigned int num_opts = 1;
134 char **argv, *token = opts;
135
136 /* Count number of arguments. */
137 do {
138 if (*token == ' ') {
139 /* Use to ignore consecutive spaces */
140 if (!ignore_space) {
141 num_opts++;
142 }
143 ignore_space = 1;
144 } else {
145 ignore_space = 0;
146 }
147 token++;
148 } while (*token != '\0');
149
150 /* Add two here for the NULL terminating element and trace path */
151 argv = malloc(sizeof(char *) * (num_opts + 2));
152 if (argv == NULL) {
153 goto error;
154 }
155
156 token = strtok(opts, " ");
157 while (token != NULL) {
158 argv[i] = strdup(token);
159 token = strtok(NULL, " ");
160 i++;
161 }
162
163 argv[num_opts] = (char *) trace_path;
164 argv[num_opts + 1] = NULL;
165
166 return argv;
167
168 error:
169 return NULL;
170 }
171
172 /*
173 * Alloc an array of string pointer from an array of strings. It also adds
174 * the trace path to the argv.
175 *
176 * The returning pointer is ready to be passed to execvp().
177 */
178 static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
179 const char *trace_path)
180 {
181 char **argv;
182 size_t size;
183
184 size = sizeof(char *) * opts_len;
185
186 /* Add two here for the trace_path and the NULL terminating element. */
187 argv = malloc(size + 2);
188 if (argv == NULL) {
189 goto error;
190 }
191
192 memcpy(argv, opts, size);
193
194 argv[opts_len] = (char *)trace_path;
195 argv[opts_len + 1] = NULL;
196
197 error:
198 return argv;
199 }
200
201 /*
202 * Spawn viewer with the trace directory path.
203 */
204 static int spawn_viewer(const char *trace_path)
205 {
206 int ret = 0;
207 struct stat status;
208 const char *viewer_bin = NULL;
209 struct viewers *viewer;
210 char **argv = NULL;
211
212 /* Check for --viewer options */
213 viewer = parse_options();
214 if (viewer == NULL) {
215 ret = CMD_ERROR;
216 goto error;
217 }
218
219 switch (viewer->type) {
220 case VIEWER_BABELTRACE:
221 if (stat(babeltrace_bin, &status) == 0) {
222 viewer_bin = babeltrace_bin;
223 } else {
224 viewer_bin = viewer->exec_name;
225 }
226 argv = alloc_argv_from_local_opts(babeltrace_opts,
227 ARRAY_SIZE(babeltrace_opts), trace_path);
228 break;
229 #if 0
230 case VIEWER_LTTV_GUI:
231 if (stat(lttv_gui_bin, &status) == 0) {
232 viewer_bin = lttv_gui_bin;
233 } else {
234 viewer_bin = viewer->exec_name;
235 }
236 argv = alloc_argv_from_local_opts(lttv_gui_opts,
237 ARRAY_SIZE(lttv_gui_opts), trace_path);
238 break;
239 #endif
240 case VIEWER_USER_DEFINED:
241 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
242 if (argv) {
243 viewer_bin = argv[0];
244 }
245 break;
246 default:
247 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
248 argv = alloc_argv_from_local_opts(babeltrace_opts,
249 ARRAY_SIZE(babeltrace_opts), trace_path);
250 break;
251 }
252
253 if (argv == NULL) {
254 ret = CMD_FATAL;
255 goto error;
256 }
257
258 DBG("Using %s viewer", viewer_bin);
259
260 ret = execvp(viewer_bin, argv);
261 if (ret) {
262 if (errno == ENOENT) {
263 ERR("%s not found on the system", viewer_bin);
264 } else {
265 PERROR("exec: %s", viewer_bin);
266 }
267 free(argv);
268 ret = CMD_FATAL;
269 goto error;
270 }
271
272 error:
273 return ret;
274 }
275
276 /*
277 * Exec viewer if found and use session name path.
278 */
279 static int view_trace(void)
280 {
281 int ret, count, i, found = 0;
282 char *session_name, *trace_path;
283 struct lttng_session *sessions = NULL;
284
285 /*
286 * Safety net. If lttng is suid at some point for *any* useless reasons,
287 * this prevent any bad execution of binaries.
288 */
289 if (getuid() != 0) {
290 if (getuid() != geteuid()) {
291 ERR("UID does not match effective UID.");
292 ret = CMD_ERROR;
293 goto error;
294 } else if (getgid() != getegid()) {
295 ERR("GID does not match effective GID.");
296 ret = CMD_ERROR;
297 goto error;
298 }
299 }
300
301 /* User define trace path override the session name */
302 if (opt_trace_path) {
303 session_name = NULL;
304 } else if(opt_session_name == NULL) {
305 session_name = get_session_name();
306 if (session_name == NULL) {
307 ret = CMD_ERROR;
308 goto error;
309 }
310 } else {
311 session_name = opt_session_name;
312 }
313
314 DBG("Viewing trace for session %s", session_name);
315
316 if (session_name) {
317 /* Getting all sessions */
318 count = lttng_list_sessions(&sessions);
319 if (count < 0) {
320 ERR("Unable to list sessions. Session name %s not found.",
321 session_name);
322 MSG("Is there a session daemon running?");
323 ret = CMD_ERROR;
324 goto free_error;
325 }
326
327 /* Find our session listed by the session daemon */
328 for (i = 0; i < count; i++) {
329 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
330 found = 1;
331 break;
332 }
333 }
334
335 if (!found) {
336 MSG("Session name %s not found", session_name);
337 ret = CMD_ERROR;
338 goto free_sessions;
339 }
340
341 trace_path = sessions[i].path;
342 } else {
343 trace_path = opt_trace_path;
344 }
345
346 MSG("Trace directory: %s\n", trace_path);
347
348 ret = spawn_viewer(trace_path);
349 if (ret < 0) {
350 /* Don't set ret so lttng can interpret the sessiond error. */
351 goto free_sessions;
352 }
353
354 free_sessions:
355 if (sessions) {
356 free(sessions);
357 }
358 free_error:
359 if (opt_session_name == NULL) {
360 free(session_name);
361 }
362 error:
363 return ret;
364 }
365
366 /*
367 * The 'view <options>' first level command
368 */
369 int cmd_view(int argc, const char **argv)
370 {
371 int opt, ret = CMD_SUCCESS;
372 static poptContext pc;
373
374 pc = poptGetContext(NULL, argc, argv, long_options, 0);
375 poptReadDefaultConfig(pc, 0);
376
377 while ((opt = poptGetNextOpt(pc)) != -1) {
378 switch (opt) {
379 case OPT_HELP:
380 usage(stdout);
381 goto end;
382 case OPT_LIST_OPTIONS:
383 list_cmd_options(stdout, long_options);
384 goto end;
385 default:
386 usage(stderr);
387 ret = CMD_UNDEFINED;
388 goto end;
389 }
390 }
391
392 opt_session_name = (char*) poptGetArg(pc);
393
394 ret = view_trace();
395
396 end:
397 poptFreeContext(pc);
398 return ret;
399 }
This page took 0.060837 seconds and 3 git commands to generate.