c4724415c31bca500e8054a8dc9411f9d140c7e5
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <sys/types.h>
27 #include "../command.h"
30 static char *opt_session_name
;
31 static char *opt_viewer
;
32 static char *opt_trace_path
;
33 static const char *babeltrace_bin
= CONFIG_BABELTRACE_BIN
;
34 //static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN;
41 static struct poptOption long_options
[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
44 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
45 {"viewer", 'e', POPT_ARG_STRING
, &opt_viewer
, 0, 0, 0},
46 {"trace-path", 't', POPT_ARG_STRING
, &opt_trace_path
, 0, 0, 0},
51 * This is needed for each viewer since we are using execvp().
53 static const char *babeltrace_opts
[] = { "babeltrace" };
54 //static const char *lttv_gui_opts[] = { "lttv-gui", "-t", };
57 * Type is also use as the index in the viewers array. So please, make sure
58 * your enum value is in the right order in the array below.
61 VIEWER_BABELTRACE
= 0,
63 VIEWER_USER_DEFINED
= 2,
67 * NOTE: "lttv" is a shell command and it's not working for exec() family
68 * functions so we might think of removing this wrapper or using bash.
70 static struct viewers
{
71 const char *exec_name
;
72 enum viewer_type type
;
74 { "babeltrace", VIEWER_BABELTRACE
},
75 { "lttv-gui", VIEWER_LTTV_GUI
},
76 { NULL
, VIEWER_USER_DEFINED
},
82 static void usage(FILE *ofp
)
84 fprintf(ofp
, "usage: lttng view [SESSION_NAME] [OPTIONS]\n");
86 fprintf(ofp
, "By default, the babeltrace viewer will be used for text viewing\n");
88 fprintf(ofp
, "Where SESSION_NAME is an optional session name. If not specified, lttng will\n");
89 fprintf(ofp
, "get it from the configuration file (.lttngrc).\n");
91 fprintf(ofp
, " -h, --help Show this help\n");
92 fprintf(ofp
, " --list-options Simple listing of options\n");
93 fprintf(ofp
, " -t, --trace-path PATH Trace directory path for the viewer\n");
94 fprintf(ofp
, " -e, --viewer CMD Specify viewer and/or options to use\n");
95 fprintf(ofp
, " This will completely override the default viewers so\n");
96 fprintf(ofp
, " please make sure to specify the full command. The trace\n");
97 fprintf(ofp
, " directory path of the session will be appended at the end\n");
98 fprintf(ofp
, " to the arguments\n");
102 static struct viewers
*parse_options(void)
104 if (opt_viewer
== NULL
) {
105 /* Default is babeltrace */
106 return &(viewers
[VIEWER_BABELTRACE
]);
110 if (strstr(opt_viewer
, viewers
[VIEWER_LTTV_GUI
].exec_name
) == 0) {
111 return &(viewers
[VIEWER_LTTV_GUI
]);
116 * This means that if -e, --viewers is used, we just override everything
117 * with it. For supported viewers like lttv, we could simply detect if "-t"
118 * is passed and if not, add the trace directory to it.
120 return &(viewers
[VIEWER_USER_DEFINED
]);
124 * Alloc an array of string pointer from a simple string having all options
125 * seperated by spaces. Also adds the trace path to the arguments.
127 * The returning pointer is ready to be passed to execvp().
129 static char **alloc_argv_from_user_opts(char *opts
, const char *trace_path
)
131 int i
= 0, ignore_space
= 0;
132 unsigned int num_opts
= 1;
133 char **argv
, *token
= opts
;
135 /* Count number of arguments. */
138 /* Use to ignore consecutive spaces */
147 } while (*token
!= '\0');
149 /* Add two here for the NULL terminating element and trace path */
150 argv
= malloc(sizeof(char *) * (num_opts
+ 2));
155 token
= strtok(opts
, " ");
156 while (token
!= NULL
) {
157 argv
[i
] = strdup(token
);
158 token
= strtok(NULL
, " ");
162 argv
[num_opts
] = (char *) trace_path
;
163 argv
[num_opts
+ 1] = NULL
;
172 * Alloc an array of string pointer from an array of strings. It also adds
173 * the trace path to the argv.
175 * The returning pointer is ready to be passed to execvp().
177 static char **alloc_argv_from_local_opts(const char **opts
, size_t opts_len
,
178 const char *trace_path
)
183 size
= sizeof(char *) * opts_len
;
185 /* Add two here for the trace_path and the NULL terminating element. */
186 argv
= malloc(size
+ 2);
191 memcpy(argv
, opts
, size
);
193 argv
[opts_len
] = (char *)trace_path
;
194 argv
[opts_len
+ 1] = NULL
;
201 * Spawn viewer with the trace directory path.
203 static int spawn_viewer(const char *trace_path
)
207 const char *viewer_bin
= NULL
;
208 struct viewers
*viewer
;
211 /* Check for --viewer options */
212 viewer
= parse_options();
213 if (viewer
== NULL
) {
218 switch (viewer
->type
) {
219 case VIEWER_BABELTRACE
:
220 if (stat(babeltrace_bin
, &status
) == 0) {
221 viewer_bin
= babeltrace_bin
;
223 viewer_bin
= viewer
->exec_name
;
225 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
226 ARRAY_SIZE(babeltrace_opts
), trace_path
);
229 case VIEWER_LTTV_GUI
:
230 if (stat(lttv_gui_bin
, &status
) == 0) {
231 viewer_bin
= lttv_gui_bin
;
233 viewer_bin
= viewer
->exec_name
;
235 argv
= alloc_argv_from_local_opts(lttv_gui_opts
,
236 ARRAY_SIZE(lttv_gui_opts
), trace_path
);
239 case VIEWER_USER_DEFINED
:
240 argv
= alloc_argv_from_user_opts(opt_viewer
, trace_path
);
242 viewer_bin
= argv
[0];
246 viewer_bin
= viewers
[VIEWER_BABELTRACE
].exec_name
;
247 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
248 ARRAY_SIZE(babeltrace_opts
), trace_path
);
257 DBG("Using %s viewer", viewer_bin
);
259 ret
= execvp(viewer_bin
, argv
);
261 PERROR("exec: %s", viewer_bin
);
272 * Exec viewer if found and use session name path.
274 static int view_trace(void)
276 int ret
, count
, i
, found
= 0;
277 char *session_name
, *trace_path
;
278 struct lttng_session
*sessions
= NULL
;
281 * Safety net. If lttng is suid at some point for *any* useless reasons,
282 * this prevent any bad execution of binaries.
285 if (getuid() != geteuid()) {
286 ERR("UID does not match effective UID.");
289 } else if (getgid() != getegid()) {
290 ERR("GID does not match effective GID.");
296 /* User define trace path override the session name */
297 if (opt_trace_path
) {
299 } else if(opt_session_name
== NULL
) {
300 session_name
= get_session_name();
301 if (session_name
== NULL
) {
306 session_name
= opt_session_name
;
309 DBG("Viewing trace for session %s", session_name
);
312 /* Getting all sessions */
313 count
= lttng_list_sessions(&sessions
);
315 ERR("Unable to list sessions. Session name %s not found.",
317 MSG("Is there a session daemon running?");
322 /* Find our session listed by the session daemon */
323 for (i
= 0; i
< count
; i
++) {
324 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
331 MSG("Session name %s not found", session_name
);
336 trace_path
= sessions
[i
].path
;
338 trace_path
= opt_trace_path
;
341 MSG("Trace directory: %s\n", trace_path
);
343 ret
= spawn_viewer(trace_path
);
345 /* Don't set ret so lttng can interpret the sessiond error. */
356 if (opt_session_name
== NULL
) {
364 * The 'view <options>' first level command
366 int cmd_view(int argc
, const char **argv
)
368 int opt
, ret
= CMD_SUCCESS
;
369 static poptContext pc
;
371 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
372 poptReadDefaultConfig(pc
, 0);
374 while ((opt
= poptGetNextOpt(pc
)) != -1) {
379 case OPT_LIST_OPTIONS
:
380 list_cmd_options(stdout
, long_options
);
389 opt_session_name
= (char*) poptGetArg(pc
);
This page took 0.036539 seconds and 3 git commands to generate.