Kernel ioctl to regenerate the statedump
[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
35enum {
36 OPT_HELP = 1,
37 OPT_LIST_OPTIONS,
38};
39
40static 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},
f1f887c0 45 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
0c95f5b2
DG
46 {0, 0, 0, 0, 0, 0, 0}
47};
48
49/*
50 * This is needed for each viewer since we are using execvp().
51 */
36ef520f 52static const char *babeltrace_opts[] = { "babeltrace" };
0c95f5b2
DG
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 */
59enum 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 */
69static 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
8960e9cd
DG
78/* Is the session we are trying to view is in live mode. */
79static int session_live_mode;
80
0c95f5b2
DG
81static struct viewers *parse_options(void)
82{
83 if (opt_viewer == NULL) {
84 /* Default is babeltrace */
85 return &(viewers[VIEWER_BABELTRACE]);
86 }
87
0c95f5b2
DG
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 */
102static 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 */
1025878e 123 argv = zmalloc(sizeof(char *) * (num_opts + 2));
0c95f5b2
DG
124 if (argv == NULL) {
125 goto error;
126 }
127
128 token = strtok(opts, " ");
129 while (token != NULL) {
130 argv[i] = strdup(token);
44721eb2
MD
131 if (argv[i] == NULL) {
132 goto error;
133 }
0c95f5b2
DG
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
143error:
879ba548
JG
144 if (argv) {
145 for (i = 0; i < num_opts + 2; i++) {
146 free(argv[i]);
147 }
148 free(argv);
149 }
150
0c95f5b2
DG
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 */
160static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
161 const char *trace_path)
162{
163 char **argv;
8960e9cd
DG
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 }
0c95f5b2 176
8960e9cd 177 size = sizeof(char *) * mem_len;
0c95f5b2
DG
178
179 /* Add two here for the trace_path and the NULL terminating element. */
1025878e 180 argv = zmalloc(size);
0c95f5b2
DG
181 if (argv == NULL) {
182 goto error;
183 }
184
185 memcpy(argv, opts, size);
186
8960e9cd
DG
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 }
0c95f5b2
DG
196
197error:
198 return argv;
199}
200
201/*
202 * Spawn viewer with the trace directory path.
203 */
204static int spawn_viewer(const char *trace_path)
205{
206 int ret = 0;
0c95f5b2
DG
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
85a68078
DG
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;
85a68078
DG
229 case VIEWER_USER_DEFINED:
230 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
231 if (argv) {
232 viewer_bin = argv[0];
0c95f5b2 233 }
85a68078
DG
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 }
0c95f5b2 241
57138fbd 242 if (argv == NULL || !viewer_bin) {
85a68078
DG
243 ret = CMD_FATAL;
244 goto error;
245 }
0c95f5b2 246
85a68078 247 DBG("Using %s viewer", viewer_bin);
0c95f5b2 248
85a68078
DG
249 ret = execvp(viewer_bin, argv);
250 if (ret) {
0c687325
JD
251 if (errno == ENOENT) {
252 ERR("%s not found on the system", viewer_bin);
253 } else {
254 PERROR("exec: %s", viewer_bin);
255 }
0c95f5b2 256 ret = CMD_FATAL;
85a68078 257 goto error;
0c95f5b2
DG
258 }
259
260error:
45da08a2 261 free(argv);
0c95f5b2
DG
262 return ret;
263}
264
8960e9cd
DG
265/*
266 * Build the live path we need for the lttng live view.
267 */
268static 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) {
6f04ed72 276 PERROR("gethostname");
8960e9cd
DG
277 goto error;
278 }
279
280 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
281 session_name);
282 if (ret < 0) {
6f04ed72 283 PERROR("asprintf live path");
8960e9cd
DG
284 goto error;
285 }
286
287error:
288 return path;
289}
290
0c95f5b2
DG
291/*
292 * Exec viewer if found and use session name path.
293 */
294static int view_trace(void)
295{
c617c0c6 296 int ret;
8960e9cd 297 char *session_name, *trace_path = NULL;
0c95f5b2
DG
298 struct lttng_session *sessions = NULL;
299
300 /*
301 * Safety net. If lttng is suid at some point for *any* useless reasons,
f1f887c0 302 * this prevent any bad execution of binaries.
0c95f5b2
DG
303 */
304 if (getuid() != 0) {
305 if (getuid() != geteuid()) {
306 ERR("UID does not match effective UID.");
6e9261f4 307 ret = CMD_ERROR;
0c95f5b2
DG
308 goto error;
309 } else if (getgid() != getegid()) {
310 ERR("GID does not match effective GID.");
6e9261f4 311 ret = CMD_ERROR;
0c95f5b2
DG
312 goto error;
313 }
314 }
315
f1f887c0
DG
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) {
0c95f5b2
DG
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
f1f887c0 331 if (session_name) {
c617c0c6
MD
332 int i, count, found = 0;
333
f1f887c0
DG
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 }
0c95f5b2 343
f1f887c0
DG
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 }
0c95f5b2 350 }
0c95f5b2 351
f1f887c0
DG
352 if (!found) {
353 MSG("Session name %s not found", session_name);
354 ret = CMD_ERROR;
355 goto free_sessions;
356 }
357
8960e9cd
DG
358 session_live_mode = sessions[i].live_timer_interval;
359
360 DBG("Session live mode set to %d", session_live_mode);
3822545d 361
8960e9cd 362 if (sessions[i].enabled && !session_live_mode) {
3822545d
DG
363 WARN("Session %s is running. Please stop it before reading it.",
364 session_name);
365 ret = CMD_ERROR;
366 goto free_sessions;
367 }
8960e9cd
DG
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 }
f1f887c0
DG
380 } else {
381 trace_path = opt_trace_path;
0c95f5b2
DG
382 }
383
f1f887c0 384 MSG("Trace directory: %s\n", trace_path);
0c95f5b2 385
f1f887c0 386 ret = spawn_viewer(trace_path);
0c95f5b2
DG
387 if (ret < 0) {
388 /* Don't set ret so lttng can interpret the sessiond error. */
389 goto free_sessions;
390 }
391
0c95f5b2 392free_sessions:
8960e9cd
DG
393 if (session_live_mode) {
394 free(trace_path);
395 }
0e428499 396 free(sessions);
0c95f5b2
DG
397free_error:
398 if (opt_session_name == NULL) {
399 free(session_name);
400 }
401error:
402 return ret;
403}
404
405/*
406 * The 'view <options>' first level command
407 */
408int 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
c7e35b03
JR
416 if (lttng_opt_mi) {
417 WARN("mi does not apply to view command");
418 }
419
0c95f5b2
DG
420 while ((opt = poptGetNextOpt(pc)) != -1) {
421 switch (opt) {
422 case OPT_HELP:
4ba92f18 423 SHOW_HELP();
0c95f5b2
DG
424 goto end;
425 case OPT_LIST_OPTIONS:
426 list_cmd_options(stdout, long_options);
427 goto end;
428 default:
0c95f5b2
DG
429 ret = CMD_UNDEFINED;
430 goto end;
431 }
432 }
433
434 opt_session_name = (char*) poptGetArg(pc);
435
436 ret = view_trace();
437
438end:
439 poptFreeContext(pc);
440 return ret;
441}
This page took 0.060412 seconds and 4 git commands to generate.