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