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