2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include "../command.h"
19 static char *opt_session_name
;
20 static char *opt_viewer
;
21 static char *opt_trace_path
;
22 static const char *babeltrace_bin
= CONFIG_BABELTRACE_BIN
;
23 static const char *babeltrace2_bin
= CONFIG_BABELTRACE2_BIN
;
25 #ifdef LTTNG_EMBED_HELP
26 static const char help_msg
[] =
27 #include <lttng-view.1.h>
36 static struct poptOption long_options
[] = {
37 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
38 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
39 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
40 {"viewer", 'e', POPT_ARG_STRING
, &opt_viewer
, 0, 0, 0},
41 {"trace-path", 't', POPT_ARG_STRING
, &opt_trace_path
, 0, 0, 0},
46 * This is needed for each viewer since we are using execvp().
48 static const char *babeltrace_opts
[] = { "babeltrace" };
49 static const char *babeltrace2_opts
[] = { "babeltrace2" };
52 * Type is also use as the index in the viewers array. So please, make sure
53 * your enum value is in the right order in the array below.
56 VIEWER_BABELTRACE
= 0,
57 VIEWER_BABELTRACE2
= 1,
58 VIEWER_USER_DEFINED
= 2,
62 * NOTE: "lttv" is a shell command and it's not working for exec() family
63 * functions so we might think of removing this wrapper or using bash.
65 static const struct viewers
{
66 const char *exec_name
;
67 enum viewer_type type
;
69 { "babeltrace", VIEWER_BABELTRACE
},
70 { "babeltrace2", VIEWER_BABELTRACE2
},
71 { NULL
, VIEWER_USER_DEFINED
},
74 /* Is the session we are trying to view is in live mode. */
75 static int session_live_mode
;
77 static const struct viewers
*parse_options(void)
79 if (opt_viewer
== NULL
) {
80 /* Default is babeltrace */
81 return &(viewers
[VIEWER_BABELTRACE2
]);
85 * This means that if -e, --viewers is used, we just override everything
86 * with it. For supported viewers like lttv, we could simply detect if "-t"
87 * is passed and if not, add the trace directory to it.
89 return &(viewers
[VIEWER_USER_DEFINED
]);
93 * Alloc an array of string pointer from a simple string having all options
94 * seperated by spaces. Also adds the trace path to the arguments.
96 * The returning pointer is ready to be passed to execvp().
98 static char **alloc_argv_from_user_opts(char *opts
, const char *trace_path
)
100 int i
= 0, ignore_space
= 0;
101 unsigned int num_opts
= 1;
102 char **argv
, *token
= opts
;
104 /* Count number of arguments. */
107 /* Use to ignore consecutive spaces */
116 } while (*token
!= '\0');
118 /* Add two here for the NULL terminating element and trace path */
119 argv
= zmalloc(sizeof(char *) * (num_opts
+ 2));
124 token
= strtok(opts
, " ");
125 while (token
!= NULL
) {
126 argv
[i
] = strdup(token
);
127 if (argv
[i
] == NULL
) {
130 token
= strtok(NULL
, " ");
134 argv
[num_opts
] = (char *) trace_path
;
135 argv
[num_opts
+ 1] = NULL
;
141 for (i
= 0; i
< num_opts
+ 2; i
++) {
151 * Alloc an array of string pointer from an array of strings. It also adds
152 * the trace path to the argv.
154 * The returning pointer is ready to be passed to execvp().
156 static char **alloc_argv_from_local_opts(const char **opts
, size_t opts_len
,
157 const char *trace_path
)
160 size_t size
, mem_len
;
162 /* Add one for the NULL terminating element. */
163 mem_len
= opts_len
+ 1;
164 if (session_live_mode
) {
165 /* Add 3 option for the live mode being "-i lttng-live URL". */
168 /* Add option for the trace path. */
172 size
= sizeof(char *) * mem_len
;
174 /* Add two here for the trace_path and the NULL terminating element. */
175 argv
= zmalloc(size
);
180 memcpy(argv
, opts
, sizeof(char *) * opts_len
);
182 if (session_live_mode
) {
183 argv
[opts_len
] = (char *) "-i";
184 argv
[opts_len
+ 1] = (char *) "lttng-live";
185 argv
[opts_len
+ 2] = (char *) trace_path
;
186 argv
[opts_len
+ 3] = NULL
;
188 argv
[opts_len
] = (char *) trace_path
;
189 argv
[opts_len
+ 1] = NULL
;
197 * Spawn viewer with the trace directory path.
199 static int spawn_viewer(const char *trace_path
)
203 const char *viewer_bin
= NULL
;
204 const struct viewers
*viewer
;
207 /* Check for --viewer options */
208 viewer
= parse_options();
209 if (viewer
== NULL
) {
215 switch (viewer
->type
) {
216 case VIEWER_BABELTRACE2
:
217 if (stat(babeltrace2_bin
, &status
) == 0) {
218 viewer_bin
= babeltrace2_bin
;
220 viewer_bin
= viewer
->exec_name
;
222 argv
= alloc_argv_from_local_opts(babeltrace2_opts
,
223 ARRAY_SIZE(babeltrace2_opts
), trace_path
);
225 case VIEWER_BABELTRACE
:
226 if (stat(babeltrace_bin
, &status
) == 0) {
227 viewer_bin
= babeltrace_bin
;
229 viewer_bin
= viewer
->exec_name
;
231 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
232 ARRAY_SIZE(babeltrace_opts
), trace_path
);
234 case VIEWER_USER_DEFINED
:
235 argv
= alloc_argv_from_user_opts(opt_viewer
, trace_path
);
237 viewer_bin
= argv
[0];
241 viewer_bin
= viewers
[VIEWER_BABELTRACE
].exec_name
;
242 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
243 ARRAY_SIZE(babeltrace_opts
), trace_path
);
247 if (argv
== NULL
|| !viewer_bin
) {
252 DBG("Using %s viewer", viewer_bin
);
254 ret
= execvp(viewer_bin
, argv
);
256 if (errno
== ENOENT
&& viewer
->exec_name
) {
257 if (viewer
->type
== VIEWER_BABELTRACE2
) {
258 /* Fallback to legacy babeltrace. */
259 DBG("babeltrace2 not installed on the system, falling back to babeltrace 1.x");
260 viewer
= &viewers
[VIEWER_BABELTRACE
];
265 ERR("Viewer \"%s\" not found on the system",
269 PERROR("Failed to launch \"%s\" viewer", viewer_bin
);
281 * Build the live path we need for the lttng live view.
283 static char *build_live_path(char *session_name
)
287 char hostname
[HOST_NAME_MAX
];
289 ret
= gethostname(hostname
, sizeof(hostname
));
291 PERROR("gethostname");
295 ret
= asprintf(&path
, "net://localhost/host/%s/%s", hostname
,
298 PERROR("asprintf live path");
307 * Exec viewer if found and use session name path.
309 static int view_trace(void)
312 char *session_name
, *trace_path
= NULL
;
313 struct lttng_session
*sessions
= NULL
;
314 bool free_trace_path
= false;
317 * Safety net. If lttng is suid at some point for *any* useless reasons,
318 * this prevent any bad execution of binaries.
321 if (getuid() != geteuid()) {
322 ERR("UID does not match effective UID.");
325 } else if (getgid() != getegid()) {
326 ERR("GID does not match effective GID.");
332 /* User define trace path override the session name */
333 if (opt_trace_path
) {
335 } else if(opt_session_name
== NULL
) {
336 session_name
= get_session_name();
337 if (session_name
== NULL
) {
342 session_name
= opt_session_name
;
345 DBG("Viewing trace for session %s", session_name
);
348 int i
, count
, found
= 0;
350 /* Getting all sessions */
351 count
= lttng_list_sessions(&sessions
);
353 ERR("Unable to list sessions. Session name %s not found.",
355 MSG("Is there a session daemon running?");
360 /* Find our session listed by the session daemon */
361 for (i
= 0; i
< count
; i
++) {
362 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
369 MSG("Session name %s not found", session_name
);
374 session_live_mode
= sessions
[i
].live_timer_interval
;
376 DBG("Session live mode set to %d", session_live_mode
);
378 if (sessions
[i
].enabled
&& !session_live_mode
) {
379 WARN("Session %s is running. Please stop it before reading it.",
385 /* If the timer interval is set we are in live mode. */
386 if (session_live_mode
) {
387 trace_path
= build_live_path(session_name
);
392 free_trace_path
= true;
394 /* Get file system session path. */
395 trace_path
= sessions
[i
].path
;
398 trace_path
= opt_trace_path
;
401 MSG("Trace directory: %s\n", trace_path
);
403 ret
= spawn_viewer(trace_path
);
405 /* Don't set ret so lttng can interpret the sessiond error. */
410 if (session_live_mode
&& free_trace_path
) {
415 if (opt_session_name
== NULL
) {
423 * The 'view <options>' first level command
425 int cmd_view(int argc
, const char **argv
)
427 int opt
, ret
= CMD_SUCCESS
;
428 static poptContext pc
;
429 const char *leftover
= NULL
;
431 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
432 poptReadDefaultConfig(pc
, 0);
435 WARN("mi does not apply to view command");
438 while ((opt
= poptGetNextOpt(pc
)) != -1) {
443 case OPT_LIST_OPTIONS
:
444 list_cmd_options(stdout
, long_options
);
452 opt_session_name
= (char*) poptGetArg(pc
);
454 leftover
= poptGetArg(pc
);
456 ERR("Unknown argument: %s", leftover
);
This page took 0.0377 seconds and 4 git commands to generate.