db13ee22718d9fcc0c4eb9636bcd9593ef012032
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
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.
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.
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.
24 #include <sys/types.h>
27 #include "../command.h"
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 //static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN;
40 static 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},
45 {"trace-path", 't', POPT_ARG_STRING
, &opt_trace_path
, 0, 0, 0},
50 * This is needed for each viewer since we are using execvp().
52 static const char *babeltrace_opts
[] = { "babeltrace" };
53 //static const char *lttv_gui_opts[] = { "lttv-gui", "-t", };
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.
60 VIEWER_BABELTRACE
= 0,
62 VIEWER_USER_DEFINED
= 2,
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.
69 static struct viewers
{
70 const char *exec_name
;
71 enum viewer_type type
;
73 { "babeltrace", VIEWER_BABELTRACE
},
74 { "lttv-gui", VIEWER_LTTV_GUI
},
75 { NULL
, VIEWER_USER_DEFINED
},
78 /* Is the session we are trying to view is in live mode. */
79 static int session_live_mode
;
81 static struct viewers
*parse_options(void)
83 if (opt_viewer
== NULL
) {
84 /* Default is babeltrace */
85 return &(viewers
[VIEWER_BABELTRACE
]);
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.
93 return &(viewers
[VIEWER_USER_DEFINED
]);
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.
100 * The returning pointer is ready to be passed to execvp().
102 static char **alloc_argv_from_user_opts(char *opts
, const char *trace_path
)
104 int i
= 0, ignore_space
= 0;
105 unsigned int num_opts
= 1;
106 char **argv
, *token
= opts
;
108 /* Count number of arguments. */
111 /* Use to ignore consecutive spaces */
120 } while (*token
!= '\0');
122 /* Add two here for the NULL terminating element and trace path */
123 argv
= zmalloc(sizeof(char *) * (num_opts
+ 2));
128 token
= strtok(opts
, " ");
129 while (token
!= NULL
) {
130 argv
[i
] = strdup(token
);
131 if (argv
[i
] == NULL
) {
134 token
= strtok(NULL
, " ");
138 argv
[num_opts
] = (char *) trace_path
;
139 argv
[num_opts
+ 1] = NULL
;
145 for (i
= 0; i
< num_opts
+ 2; i
++) {
155 * Alloc an array of string pointer from an array of strings. It also adds
156 * the trace path to the argv.
158 * The returning pointer is ready to be passed to execvp().
160 static char **alloc_argv_from_local_opts(const char **opts
, size_t opts_len
,
161 const char *trace_path
)
164 size_t size
, mem_len
;
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". */
173 /* Add option for the trace path. */
177 size
= sizeof(char *) * mem_len
;
179 /* Add two here for the trace_path and the NULL terminating element. */
180 argv
= zmalloc(size
);
185 memcpy(argv
, opts
, size
);
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
;
193 argv
[opts_len
] = (char *) trace_path
;
194 argv
[opts_len
+ 1] = NULL
;
202 * Spawn viewer with the trace directory path.
204 static int spawn_viewer(const char *trace_path
)
208 const char *viewer_bin
= NULL
;
209 struct viewers
*viewer
;
212 /* Check for --viewer options */
213 viewer
= parse_options();
214 if (viewer
== NULL
) {
219 switch (viewer
->type
) {
220 case VIEWER_BABELTRACE
:
221 if (stat(babeltrace_bin
, &status
) == 0) {
222 viewer_bin
= babeltrace_bin
;
224 viewer_bin
= viewer
->exec_name
;
226 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
227 ARRAY_SIZE(babeltrace_opts
), trace_path
);
229 case VIEWER_USER_DEFINED
:
230 argv
= alloc_argv_from_user_opts(opt_viewer
, trace_path
);
232 viewer_bin
= argv
[0];
236 viewer_bin
= viewers
[VIEWER_BABELTRACE
].exec_name
;
237 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
238 ARRAY_SIZE(babeltrace_opts
), trace_path
);
242 if (argv
== NULL
|| !viewer_bin
) {
247 DBG("Using %s viewer", viewer_bin
);
249 ret
= execvp(viewer_bin
, argv
);
251 if (errno
== ENOENT
) {
252 ERR("%s not found on the system", viewer_bin
);
254 PERROR("exec: %s", viewer_bin
);
266 * Build the live path we need for the lttng live view.
268 static char *build_live_path(char *session_name
)
272 char hostname
[HOST_NAME_MAX
];
274 ret
= gethostname(hostname
, sizeof(hostname
));
276 PERROR("gethostname");
280 ret
= asprintf(&path
, "net://localhost/host/%s/%s", hostname
,
283 PERROR("asprintf live path");
292 * Exec viewer if found and use session name path.
294 static int view_trace(void)
297 char *session_name
, *trace_path
= NULL
;
298 struct lttng_session
*sessions
= NULL
;
301 * Safety net. If lttng is suid at some point for *any* useless reasons,
302 * this prevent any bad execution of binaries.
305 if (getuid() != geteuid()) {
306 ERR("UID does not match effective UID.");
309 } else if (getgid() != getegid()) {
310 ERR("GID does not match effective GID.");
316 /* User define trace path override the session name */
317 if (opt_trace_path
) {
319 } else if(opt_session_name
== NULL
) {
320 session_name
= get_session_name();
321 if (session_name
== NULL
) {
326 session_name
= opt_session_name
;
329 DBG("Viewing trace for session %s", session_name
);
332 int i
, count
, found
= 0;
334 /* Getting all sessions */
335 count
= lttng_list_sessions(&sessions
);
337 ERR("Unable to list sessions. Session name %s not found.",
339 MSG("Is there a session daemon running?");
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) {
353 MSG("Session name %s not found", session_name
);
358 session_live_mode
= sessions
[i
].live_timer_interval
;
360 DBG("Session live mode set to %d", session_live_mode
);
362 if (sessions
[i
].enabled
&& !session_live_mode
) {
363 WARN("Session %s is running. Please stop it before reading it.",
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
);
377 /* Get file system session path. */
378 trace_path
= sessions
[i
].path
;
381 trace_path
= opt_trace_path
;
384 MSG("Trace directory: %s\n", trace_path
);
386 ret
= spawn_viewer(trace_path
);
388 /* Don't set ret so lttng can interpret the sessiond error. */
393 if (session_live_mode
) {
398 if (opt_session_name
== NULL
) {
406 * The 'view <options>' first level command
408 int cmd_view(int argc
, const char **argv
)
410 int opt
, ret
= CMD_SUCCESS
;
411 static poptContext pc
;
413 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
414 poptReadDefaultConfig(pc
, 0);
417 WARN("mi does not apply to view command");
420 while ((opt
= poptGetNextOpt(pc
)) != -1) {
425 case OPT_LIST_OPTIONS
:
426 list_cmd_options(stdout
, long_options
);
434 opt_session_name
= (char*) poptGetArg(pc
);
This page took 0.060515 seconds and 4 git commands to generate.