lttng-view: clean-up: static struct viewers array should be const
[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
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 }
0c95f5b2 178
8960e9cd 179 size = sizeof(char *) * mem_len;
0c95f5b2
DG
180
181 /* Add two here for the trace_path and the NULL terminating element. */
1025878e 182 argv = zmalloc(size);
0c95f5b2
DG
183 if (argv == NULL) {
184 goto error;
185 }
186
e20ca024 187 memcpy(argv, opts, sizeof(char *) * opts_len);
0c95f5b2 188
8960e9cd
DG
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 }
0c95f5b2
DG
198
199error:
200 return argv;
201}
202
203/*
204 * Spawn viewer with the trace directory path.
205 */
206static int spawn_viewer(const char *trace_path)
207{
208 int ret = 0;
0c95f5b2
DG
209 struct stat status;
210 const char *viewer_bin = NULL;
45167256 211 const struct viewers *viewer;
0c95f5b2
DG
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
85a68078
DG
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;
85a68078
DG
231 case VIEWER_USER_DEFINED:
232 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
233 if (argv) {
234 viewer_bin = argv[0];
0c95f5b2 235 }
85a68078
DG
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 }
0c95f5b2 243
57138fbd 244 if (argv == NULL || !viewer_bin) {
85a68078
DG
245 ret = CMD_FATAL;
246 goto error;
247 }
0c95f5b2 248
85a68078 249 DBG("Using %s viewer", viewer_bin);
0c95f5b2 250
85a68078
DG
251 ret = execvp(viewer_bin, argv);
252 if (ret) {
0c687325
JD
253 if (errno == ENOENT) {
254 ERR("%s not found on the system", viewer_bin);
255 } else {
256 PERROR("exec: %s", viewer_bin);
257 }
0c95f5b2 258 ret = CMD_FATAL;
85a68078 259 goto error;
0c95f5b2
DG
260 }
261
262error:
45da08a2 263 free(argv);
0c95f5b2
DG
264 return ret;
265}
266
8960e9cd
DG
267/*
268 * Build the live path we need for the lttng live view.
269 */
270static 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) {
6f04ed72 278 PERROR("gethostname");
8960e9cd
DG
279 goto error;
280 }
281
282 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
283 session_name);
284 if (ret < 0) {
6f04ed72 285 PERROR("asprintf live path");
8960e9cd
DG
286 goto error;
287 }
288
289error:
290 return path;
291}
292
0c95f5b2
DG
293/*
294 * Exec viewer if found and use session name path.
295 */
296static int view_trace(void)
297{
c617c0c6 298 int ret;
8960e9cd 299 char *session_name, *trace_path = NULL;
0c95f5b2 300 struct lttng_session *sessions = NULL;
f12e3556 301 bool free_trace_path = false;
0c95f5b2
DG
302
303 /*
304 * Safety net. If lttng is suid at some point for *any* useless reasons,
f1f887c0 305 * this prevent any bad execution of binaries.
0c95f5b2
DG
306 */
307 if (getuid() != 0) {
308 if (getuid() != geteuid()) {
309 ERR("UID does not match effective UID.");
6e9261f4 310 ret = CMD_ERROR;
0c95f5b2
DG
311 goto error;
312 } else if (getgid() != getegid()) {
313 ERR("GID does not match effective GID.");
6e9261f4 314 ret = CMD_ERROR;
0c95f5b2
DG
315 goto error;
316 }
317 }
318
f1f887c0
DG
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) {
0c95f5b2
DG
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
f1f887c0 334 if (session_name) {
c617c0c6
MD
335 int i, count, found = 0;
336
f1f887c0
DG
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 }
0c95f5b2 346
f1f887c0
DG
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 }
0c95f5b2 353 }
0c95f5b2 354
f1f887c0
DG
355 if (!found) {
356 MSG("Session name %s not found", session_name);
357 ret = CMD_ERROR;
358 goto free_sessions;
359 }
360
8960e9cd
DG
361 session_live_mode = sessions[i].live_timer_interval;
362
363 DBG("Session live mode set to %d", session_live_mode);
3822545d 364
8960e9cd 365 if (sessions[i].enabled && !session_live_mode) {
3822545d
DG
366 WARN("Session %s is running. Please stop it before reading it.",
367 session_name);
368 ret = CMD_ERROR;
369 goto free_sessions;
370 }
8960e9cd
DG
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 }
f12e3556 379 free_trace_path = true;
8960e9cd
DG
380 } else {
381 /* Get file system session path. */
382 trace_path = sessions[i].path;
383 }
f1f887c0
DG
384 } else {
385 trace_path = opt_trace_path;
0c95f5b2
DG
386 }
387
f1f887c0 388 MSG("Trace directory: %s\n", trace_path);
0c95f5b2 389
f1f887c0 390 ret = spawn_viewer(trace_path);
0c95f5b2
DG
391 if (ret < 0) {
392 /* Don't set ret so lttng can interpret the sessiond error. */
393 goto free_sessions;
394 }
395
0c95f5b2 396free_sessions:
f12e3556 397 if (session_live_mode && free_trace_path) {
8960e9cd
DG
398 free(trace_path);
399 }
0e428499 400 free(sessions);
0c95f5b2
DG
401free_error:
402 if (opt_session_name == NULL) {
403 free(session_name);
404 }
405error:
406 return ret;
407}
408
409/*
410 * The 'view <options>' first level command
411 */
412int cmd_view(int argc, const char **argv)
413{
414 int opt, ret = CMD_SUCCESS;
415 static poptContext pc;
68c7f6e5 416 const char *leftover = NULL;
0c95f5b2
DG
417
418 pc = poptGetContext(NULL, argc, argv, long_options, 0);
419 poptReadDefaultConfig(pc, 0);
420
c7e35b03
JR
421 if (lttng_opt_mi) {
422 WARN("mi does not apply to view command");
423 }
424
0c95f5b2
DG
425 while ((opt = poptGetNextOpt(pc)) != -1) {
426 switch (opt) {
427 case OPT_HELP:
4ba92f18 428 SHOW_HELP();
0c95f5b2
DG
429 goto end;
430 case OPT_LIST_OPTIONS:
431 list_cmd_options(stdout, long_options);
432 goto end;
433 default:
0c95f5b2
DG
434 ret = CMD_UNDEFINED;
435 goto end;
436 }
437 }
438
439 opt_session_name = (char*) poptGetArg(pc);
440
68c7f6e5
JD
441 leftover = poptGetArg(pc);
442 if (leftover) {
443 ERR("Unknown argument: %s", leftover);
444 ret = CMD_ERROR;
445 goto end;
446 }
447
0c95f5b2
DG
448 ret = view_trace();
449
450end:
451 poptFreeContext(pc);
452 return ret;
453}
This page took 0.072669 seconds and 4 git commands to generate.