lttng-view: clean-up: remove unneeded empty line
[lttng-tools.git] / src / bin / lttng / commands / view.c
CommitLineData
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
29static char *opt_session_name;
30static char *opt_viewer;
f1f887c0 31static char *opt_trace_path;
0c95f5b2 32static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN;
0c95f5b2 33
4fc83d94
PP
34#ifdef LTTNG_EMBED_HELP
35static const char help_msg[] =
36#include <lttng-view.1.h>
37;
38#endif
39
0c95f5b2
DG
40enum {
41 OPT_HELP = 1,
42 OPT_LIST_OPTIONS,
43};
44
45static 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},
f1f887c0 50 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
0c95f5b2
DG
51 {0, 0, 0, 0, 0, 0, 0}
52};
53
54/*
55 * This is needed for each viewer since we are using execvp().
56 */
36ef520f 57static const char *babeltrace_opts[] = { "babeltrace" };
0c95f5b2
DG
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 */
63enum viewer_type {
64 VIEWER_BABELTRACE = 0,
6dd26587 65 VIEWER_USER_DEFINED = 1,
0c95f5b2
DG
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 */
45167256 72static const struct viewers {
0c95f5b2
DG
73 const char *exec_name;
74 enum viewer_type type;
75} viewers[] = {
76 { "babeltrace", VIEWER_BABELTRACE },
0c95f5b2
DG
77 { NULL, VIEWER_USER_DEFINED },
78};
79
8960e9cd
DG
80/* Is the session we are trying to view is in live mode. */
81static int session_live_mode;
82
45167256 83static const struct viewers *parse_options(void)
0c95f5b2
DG
84{
85 if (opt_viewer == NULL) {
86 /* Default is babeltrace */
87 return &(viewers[VIEWER_BABELTRACE]);
88 }
89
0c95f5b2
DG
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 */
104static 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 */
1025878e 125 argv = zmalloc(sizeof(char *) * (num_opts + 2));
0c95f5b2
DG
126 if (argv == NULL) {
127 goto error;
128 }
129
130 token = strtok(opts, " ");
131 while (token != NULL) {
132 argv[i] = strdup(token);
44721eb2
MD
133 if (argv[i] == NULL) {
134 goto error;
135 }
0c95f5b2
DG
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
145error:
879ba548
JG
146 if (argv) {
147 for (i = 0; i < num_opts + 2; i++) {
148 free(argv[i]);
149 }
150 free(argv);
151 }
152
0c95f5b2
DG
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 */
162static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
163 const char *trace_path)
164{
165 char **argv;
8960e9cd
DG
166 size_t size, mem_len;
167
8960e9cd
DG
168 /* Add one for the NULL terminating element. */
169 mem_len = opts_len + 1;
170 if (session_live_mode) {
171 /* Add 3 option for the live mode being "-i lttng-live URL". */
172 mem_len += 3;
173 } else {
174 /* Add option for the trace path. */
175 mem_len += 1;
176 }
0c95f5b2 177
8960e9cd 178 size = sizeof(char *) * mem_len;
0c95f5b2
DG
179
180 /* Add two here for the trace_path and the NULL terminating element. */
1025878e 181 argv = zmalloc(size);
0c95f5b2
DG
182 if (argv == NULL) {
183 goto error;
184 }
185
e20ca024 186 memcpy(argv, opts, sizeof(char *) * opts_len);
0c95f5b2 187
8960e9cd
DG
188 if (session_live_mode) {
189 argv[opts_len] = "-i";
190 argv[opts_len + 1] = "lttng-live";
191 argv[opts_len + 2] = (char *) trace_path;
192 argv[opts_len + 3] = NULL;
193 } else {
194 argv[opts_len] = (char *) trace_path;
195 argv[opts_len + 1] = NULL;
196 }
0c95f5b2
DG
197
198error:
199 return argv;
200}
201
202/*
203 * Spawn viewer with the trace directory path.
204 */
205static int spawn_viewer(const char *trace_path)
206{
207 int ret = 0;
0c95f5b2
DG
208 struct stat status;
209 const char *viewer_bin = NULL;
45167256 210 const struct viewers *viewer;
0c95f5b2
DG
211 char **argv = NULL;
212
213 /* Check for --viewer options */
214 viewer = parse_options();
215 if (viewer == NULL) {
216 ret = CMD_ERROR;
217 goto error;
218 }
219
85a68078
DG
220 switch (viewer->type) {
221 case VIEWER_BABELTRACE:
222 if (stat(babeltrace_bin, &status) == 0) {
223 viewer_bin = babeltrace_bin;
224 } else {
225 viewer_bin = viewer->exec_name;
226 }
227 argv = alloc_argv_from_local_opts(babeltrace_opts,
228 ARRAY_SIZE(babeltrace_opts), trace_path);
229 break;
85a68078
DG
230 case VIEWER_USER_DEFINED:
231 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
232 if (argv) {
233 viewer_bin = argv[0];
0c95f5b2 234 }
85a68078
DG
235 break;
236 default:
237 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
238 argv = alloc_argv_from_local_opts(babeltrace_opts,
239 ARRAY_SIZE(babeltrace_opts), trace_path);
240 break;
241 }
0c95f5b2 242
57138fbd 243 if (argv == NULL || !viewer_bin) {
85a68078
DG
244 ret = CMD_FATAL;
245 goto error;
246 }
0c95f5b2 247
85a68078 248 DBG("Using %s viewer", viewer_bin);
0c95f5b2 249
85a68078
DG
250 ret = execvp(viewer_bin, argv);
251 if (ret) {
0c687325
JD
252 if (errno == ENOENT) {
253 ERR("%s not found on the system", viewer_bin);
254 } else {
255 PERROR("exec: %s", viewer_bin);
256 }
0c95f5b2 257 ret = CMD_FATAL;
85a68078 258 goto error;
0c95f5b2
DG
259 }
260
261error:
45da08a2 262 free(argv);
0c95f5b2
DG
263 return ret;
264}
265
8960e9cd
DG
266/*
267 * Build the live path we need for the lttng live view.
268 */
269static char *build_live_path(char *session_name)
270{
271 int ret;
272 char *path = NULL;
273 char hostname[HOST_NAME_MAX];
274
275 ret = gethostname(hostname, sizeof(hostname));
276 if (ret < 0) {
6f04ed72 277 PERROR("gethostname");
8960e9cd
DG
278 goto error;
279 }
280
281 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
282 session_name);
283 if (ret < 0) {
6f04ed72 284 PERROR("asprintf live path");
8960e9cd
DG
285 goto error;
286 }
287
288error:
289 return path;
290}
291
0c95f5b2
DG
292/*
293 * Exec viewer if found and use session name path.
294 */
295static int view_trace(void)
296{
c617c0c6 297 int ret;
8960e9cd 298 char *session_name, *trace_path = NULL;
0c95f5b2 299 struct lttng_session *sessions = NULL;
f12e3556 300 bool free_trace_path = false;
0c95f5b2
DG
301
302 /*
303 * Safety net. If lttng is suid at some point for *any* useless reasons,
f1f887c0 304 * this prevent any bad execution of binaries.
0c95f5b2
DG
305 */
306 if (getuid() != 0) {
307 if (getuid() != geteuid()) {
308 ERR("UID does not match effective UID.");
6e9261f4 309 ret = CMD_ERROR;
0c95f5b2
DG
310 goto error;
311 } else if (getgid() != getegid()) {
312 ERR("GID does not match effective GID.");
6e9261f4 313 ret = CMD_ERROR;
0c95f5b2
DG
314 goto error;
315 }
316 }
317
f1f887c0
DG
318 /* User define trace path override the session name */
319 if (opt_trace_path) {
320 session_name = NULL;
321 } else if(opt_session_name == NULL) {
0c95f5b2
DG
322 session_name = get_session_name();
323 if (session_name == NULL) {
324 ret = CMD_ERROR;
325 goto error;
326 }
327 } else {
328 session_name = opt_session_name;
329 }
330
331 DBG("Viewing trace for session %s", session_name);
332
f1f887c0 333 if (session_name) {
c617c0c6
MD
334 int i, count, found = 0;
335
f1f887c0
DG
336 /* Getting all sessions */
337 count = lttng_list_sessions(&sessions);
338 if (count < 0) {
339 ERR("Unable to list sessions. Session name %s not found.",
340 session_name);
341 MSG("Is there a session daemon running?");
342 ret = CMD_ERROR;
343 goto free_error;
344 }
0c95f5b2 345
f1f887c0
DG
346 /* Find our session listed by the session daemon */
347 for (i = 0; i < count; i++) {
348 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
349 found = 1;
350 break;
351 }
0c95f5b2 352 }
0c95f5b2 353
f1f887c0
DG
354 if (!found) {
355 MSG("Session name %s not found", session_name);
356 ret = CMD_ERROR;
357 goto free_sessions;
358 }
359
8960e9cd
DG
360 session_live_mode = sessions[i].live_timer_interval;
361
362 DBG("Session live mode set to %d", session_live_mode);
3822545d 363
8960e9cd 364 if (sessions[i].enabled && !session_live_mode) {
3822545d
DG
365 WARN("Session %s is running. Please stop it before reading it.",
366 session_name);
367 ret = CMD_ERROR;
368 goto free_sessions;
369 }
8960e9cd
DG
370
371 /* If the timer interval is set we are in live mode. */
372 if (session_live_mode) {
373 trace_path = build_live_path(session_name);
374 if (!trace_path) {
375 ret = CMD_ERROR;
376 goto free_sessions;
377 }
f12e3556 378 free_trace_path = true;
8960e9cd
DG
379 } else {
380 /* Get file system session path. */
381 trace_path = sessions[i].path;
382 }
f1f887c0
DG
383 } else {
384 trace_path = opt_trace_path;
0c95f5b2
DG
385 }
386
f1f887c0 387 MSG("Trace directory: %s\n", trace_path);
0c95f5b2 388
f1f887c0 389 ret = spawn_viewer(trace_path);
0c95f5b2
DG
390 if (ret < 0) {
391 /* Don't set ret so lttng can interpret the sessiond error. */
392 goto free_sessions;
393 }
394
0c95f5b2 395free_sessions:
f12e3556 396 if (session_live_mode && free_trace_path) {
8960e9cd
DG
397 free(trace_path);
398 }
0e428499 399 free(sessions);
0c95f5b2
DG
400free_error:
401 if (opt_session_name == NULL) {
402 free(session_name);
403 }
404error:
405 return ret;
406}
407
408/*
409 * The 'view <options>' first level command
410 */
411int cmd_view(int argc, const char **argv)
412{
413 int opt, ret = CMD_SUCCESS;
414 static poptContext pc;
68c7f6e5 415 const char *leftover = NULL;
0c95f5b2
DG
416
417 pc = poptGetContext(NULL, argc, argv, long_options, 0);
418 poptReadDefaultConfig(pc, 0);
419
c7e35b03
JR
420 if (lttng_opt_mi) {
421 WARN("mi does not apply to view command");
422 }
423
0c95f5b2
DG
424 while ((opt = poptGetNextOpt(pc)) != -1) {
425 switch (opt) {
426 case OPT_HELP:
4ba92f18 427 SHOW_HELP();
0c95f5b2
DG
428 goto end;
429 case OPT_LIST_OPTIONS:
430 list_cmd_options(stdout, long_options);
431 goto end;
432 default:
0c95f5b2
DG
433 ret = CMD_UNDEFINED;
434 goto end;
435 }
436 }
437
438 opt_session_name = (char*) poptGetArg(pc);
439
68c7f6e5
JD
440 leftover = poptGetArg(pc);
441 if (leftover) {
442 ERR("Unknown argument: %s", leftover);
443 ret = CMD_ERROR;
444 goto end;
445 }
446
0c95f5b2
DG
447 ret = view_trace();
448
449end:
450 poptFreeContext(pc);
451 return ret;
452}
This page took 0.090059 seconds and 5 git commands to generate.