Refactor: embed mi in "add_output "to remove code duplication
[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 _LGPL_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
29 static char *opt_session_name;
30 static char *opt_viewer;
31 static char *opt_trace_path;
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},
45 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
46 {0, 0, 0, 0, 0, 0, 0}
47 };
48
49 /*
50 * This is needed for each viewer since we are using execvp().
51 */
52 static const char *babeltrace_opts[] = { "babeltrace" };
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
78 /* Is the session we are trying to view is in live mode. */
79 static int session_live_mode;
80
81 static struct viewers *parse_options(void)
82 {
83 if (opt_viewer == NULL) {
84 /* Default is babeltrace */
85 return &(viewers[VIEWER_BABELTRACE]);
86 }
87
88 /*
89 * This means that if -e, --viewers is used, we just override everything
90 * with it. For supported viewers like lttv, we could simply detect if "-t"
91 * is passed and if not, add the trace directory to it.
92 */
93 return &(viewers[VIEWER_USER_DEFINED]);
94 }
95
96 /*
97 * Alloc an array of string pointer from a simple string having all options
98 * seperated by spaces. Also adds the trace path to the arguments.
99 *
100 * The returning pointer is ready to be passed to execvp().
101 */
102 static char **alloc_argv_from_user_opts(char *opts, const char *trace_path)
103 {
104 int i = 0, ignore_space = 0;
105 unsigned int num_opts = 1;
106 char **argv, *token = opts;
107
108 /* Count number of arguments. */
109 do {
110 if (*token == ' ') {
111 /* Use to ignore consecutive spaces */
112 if (!ignore_space) {
113 num_opts++;
114 }
115 ignore_space = 1;
116 } else {
117 ignore_space = 0;
118 }
119 token++;
120 } while (*token != '\0');
121
122 /* Add two here for the NULL terminating element and trace path */
123 argv = zmalloc(sizeof(char *) * (num_opts + 2));
124 if (argv == NULL) {
125 goto error;
126 }
127
128 token = strtok(opts, " ");
129 while (token != NULL) {
130 argv[i] = strdup(token);
131 if (argv[i] == NULL) {
132 goto error;
133 }
134 token = strtok(NULL, " ");
135 i++;
136 }
137
138 argv[num_opts] = (char *) trace_path;
139 argv[num_opts + 1] = NULL;
140
141 return argv;
142
143 error:
144 if (argv) {
145 for (i = 0; i < num_opts + 2; i++) {
146 free(argv[i]);
147 }
148 free(argv);
149 }
150
151 return NULL;
152 }
153
154 /*
155 * Alloc an array of string pointer from an array of strings. It also adds
156 * the trace path to the argv.
157 *
158 * The returning pointer is ready to be passed to execvp().
159 */
160 static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
161 const char *trace_path)
162 {
163 char **argv;
164 size_t size, mem_len;
165
166
167 /* Add one for the NULL terminating element. */
168 mem_len = opts_len + 1;
169 if (session_live_mode) {
170 /* Add 3 option for the live mode being "-i lttng-live URL". */
171 mem_len += 3;
172 } else {
173 /* Add option for the trace path. */
174 mem_len += 1;
175 }
176
177 size = sizeof(char *) * mem_len;
178
179 /* Add two here for the trace_path and the NULL terminating element. */
180 argv = zmalloc(size);
181 if (argv == NULL) {
182 goto error;
183 }
184
185 memcpy(argv, opts, size);
186
187 if (session_live_mode) {
188 argv[opts_len] = "-i";
189 argv[opts_len + 1] = "lttng-live";
190 argv[opts_len + 2] = (char *) trace_path;
191 argv[opts_len + 3] = NULL;
192 } else {
193 argv[opts_len] = (char *) trace_path;
194 argv[opts_len + 1] = NULL;
195 }
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 case VIEWER_USER_DEFINED:
230 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
231 if (argv) {
232 viewer_bin = argv[0];
233 }
234 break;
235 default:
236 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
237 argv = alloc_argv_from_local_opts(babeltrace_opts,
238 ARRAY_SIZE(babeltrace_opts), trace_path);
239 break;
240 }
241
242 if (argv == NULL || !viewer_bin) {
243 ret = CMD_FATAL;
244 goto error;
245 }
246
247 DBG("Using %s viewer", viewer_bin);
248
249 ret = execvp(viewer_bin, argv);
250 if (ret) {
251 if (errno == ENOENT) {
252 ERR("%s not found on the system", viewer_bin);
253 } else {
254 PERROR("exec: %s", viewer_bin);
255 }
256 free(argv);
257 ret = CMD_FATAL;
258 goto error;
259 }
260
261 error:
262 return ret;
263 }
264
265 /*
266 * Build the live path we need for the lttng live view.
267 */
268 static char *build_live_path(char *session_name)
269 {
270 int ret;
271 char *path = NULL;
272 char hostname[HOST_NAME_MAX];
273
274 ret = gethostname(hostname, sizeof(hostname));
275 if (ret < 0) {
276 PERROR("gethostname");
277 goto error;
278 }
279
280 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
281 session_name);
282 if (ret < 0) {
283 PERROR("asprintf live path");
284 goto error;
285 }
286
287 error:
288 return path;
289 }
290
291 /*
292 * Exec viewer if found and use session name path.
293 */
294 static int view_trace(void)
295 {
296 int ret;
297 char *session_name, *trace_path = NULL;
298 struct lttng_session *sessions = NULL;
299
300 /*
301 * Safety net. If lttng is suid at some point for *any* useless reasons,
302 * this prevent any bad execution of binaries.
303 */
304 if (getuid() != 0) {
305 if (getuid() != geteuid()) {
306 ERR("UID does not match effective UID.");
307 ret = CMD_ERROR;
308 goto error;
309 } else if (getgid() != getegid()) {
310 ERR("GID does not match effective GID.");
311 ret = CMD_ERROR;
312 goto error;
313 }
314 }
315
316 /* User define trace path override the session name */
317 if (opt_trace_path) {
318 session_name = NULL;
319 } else if(opt_session_name == NULL) {
320 session_name = get_session_name();
321 if (session_name == NULL) {
322 ret = CMD_ERROR;
323 goto error;
324 }
325 } else {
326 session_name = opt_session_name;
327 }
328
329 DBG("Viewing trace for session %s", session_name);
330
331 if (session_name) {
332 int i, count, found = 0;
333
334 /* Getting all sessions */
335 count = lttng_list_sessions(&sessions);
336 if (count < 0) {
337 ERR("Unable to list sessions. Session name %s not found.",
338 session_name);
339 MSG("Is there a session daemon running?");
340 ret = CMD_ERROR;
341 goto free_error;
342 }
343
344 /* Find our session listed by the session daemon */
345 for (i = 0; i < count; i++) {
346 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
347 found = 1;
348 break;
349 }
350 }
351
352 if (!found) {
353 MSG("Session name %s not found", session_name);
354 ret = CMD_ERROR;
355 goto free_sessions;
356 }
357
358 session_live_mode = sessions[i].live_timer_interval;
359
360 DBG("Session live mode set to %d", session_live_mode);
361
362 if (sessions[i].enabled && !session_live_mode) {
363 WARN("Session %s is running. Please stop it before reading it.",
364 session_name);
365 ret = CMD_ERROR;
366 goto free_sessions;
367 }
368
369 /* If the timer interval is set we are in live mode. */
370 if (session_live_mode) {
371 trace_path = build_live_path(session_name);
372 if (!trace_path) {
373 ret = CMD_ERROR;
374 goto free_sessions;
375 }
376 } else {
377 /* Get file system session path. */
378 trace_path = sessions[i].path;
379 }
380 } else {
381 trace_path = opt_trace_path;
382 }
383
384 MSG("Trace directory: %s\n", trace_path);
385
386 ret = spawn_viewer(trace_path);
387 if (ret < 0) {
388 /* Don't set ret so lttng can interpret the sessiond error. */
389 goto free_sessions;
390 }
391
392 free_sessions:
393 if (session_live_mode) {
394 free(trace_path);
395 }
396 free(sessions);
397 free_error:
398 if (opt_session_name == NULL) {
399 free(session_name);
400 }
401 error:
402 return ret;
403 }
404
405 /*
406 * The 'view <options>' first level command
407 */
408 int cmd_view(int argc, const char **argv)
409 {
410 int opt, ret = CMD_SUCCESS;
411 static poptContext pc;
412
413 pc = poptGetContext(NULL, argc, argv, long_options, 0);
414 poptReadDefaultConfig(pc, 0);
415
416 if (lttng_opt_mi) {
417 WARN("mi does not apply to view command");
418 }
419
420 while ((opt = poptGetNextOpt(pc)) != -1) {
421 switch (opt) {
422 case OPT_HELP:
423 SHOW_HELP();
424 goto end;
425 case OPT_LIST_OPTIONS:
426 list_cmd_options(stdout, long_options);
427 goto end;
428 default:
429 ret = CMD_UNDEFINED;
430 goto end;
431 }
432 }
433
434 opt_session_name = (char*) poptGetArg(pc);
435
436 ret = view_trace();
437
438 end:
439 poptFreeContext(pc);
440 return ret;
441 }
This page took 0.053547 seconds and 4 git commands to generate.