2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
6 * SPDX-License-Identifier: LGPL-2.1-only
12 #include <sys/types.h>
15 #include <lttng/constant.h>
17 #include <common/compat/errno.hpp>
20 #include "spawn-viewer.hpp"
23 * Type is also use as the index in the viewers array. So please, make sure
24 * your enum value is in the right order in the array below.
27 VIEWER_BABELTRACE
= 0,
28 VIEWER_BABELTRACE2
= 1,
29 VIEWER_USER_DEFINED
= 2,
33 const char *babeltrace_bin
= CONFIG_BABELTRACE_BIN
;
34 const char *babeltrace2_bin
= CONFIG_BABELTRACE2_BIN
;
37 * This is needed for each viewer since we are using execvp().
39 const char *babeltrace_opts
[] = { "babeltrace" };
40 const char *babeltrace2_opts
[] = { "babeltrace2" };
43 const char *exec_name
;
44 enum viewer_type type
;
46 { "babeltrace", VIEWER_BABELTRACE
},
47 { "babeltrace2", VIEWER_BABELTRACE2
},
48 { NULL
, VIEWER_USER_DEFINED
},
52 static const struct viewer
*parse_viewer_option(const char *opt_viewer
)
54 if (opt_viewer
== NULL
) {
55 /* Default is babeltrace2 */
56 return &(viewers
[VIEWER_BABELTRACE2
]);
59 return &(viewers
[VIEWER_USER_DEFINED
]);
63 * Alloc an array of string pointer from a simple string having all options
64 * seperated by spaces. Also adds the trace path to the arguments.
66 * The returning pointer is ready to be passed to execvp().
68 static char **alloc_argv_from_user_opts(char *opts
, const char *trace_path
)
70 int i
= 0, ignore_space
= 0;
71 unsigned int num_opts
= 1;
72 char **argv
, *token
= opts
, *saveptr
= NULL
;
74 /* Count number of arguments. */
77 /* Use to ignore consecutive spaces */
86 } while (*token
!= '\0');
88 /* Add two here for the NULL terminating element and trace path */
89 argv
= calloc
<char *>(num_opts
+ 2);
94 token
= strtok_r(opts
, " ", &saveptr
);
95 while (token
!= NULL
) {
96 argv
[i
] = strdup(token
);
97 if (argv
[i
] == NULL
) {
100 token
= strtok_r(NULL
, " ", &saveptr
);
104 argv
[num_opts
] = (char *) trace_path
;
105 argv
[num_opts
+ 1] = NULL
;
111 for (i
= 0; i
< num_opts
+ 2; i
++) {
121 * Alloc an array of string pointer from an array of strings. It also adds
122 * the trace path to the argv.
124 * The returning pointer is ready to be passed to execvp().
126 static char **alloc_argv_from_local_opts(const char **opts
, size_t opts_len
,
127 const char *trace_path
, bool opt_live_mode
)
132 /* Add one for the NULL terminating element. */
133 mem_len
= opts_len
+ 1;
135 /* Add 3 option for the live mode being "-i lttng-live URL". */
138 /* Add option for the trace path. */
142 argv
= calloc
<char *>(mem_len
);
147 memcpy(argv
, opts
, sizeof(char *) * opts_len
);
150 argv
[opts_len
] = (char *) "-i";
151 argv
[opts_len
+ 1] = (char *) "lttng-live";
152 argv
[opts_len
+ 2] = (char *) trace_path
;
153 argv
[opts_len
+ 3] = NULL
;
155 argv
[opts_len
] = (char *) trace_path
;
156 argv
[opts_len
+ 1] = NULL
;
165 * Spawn viewer with the trace directory path.
167 int spawn_viewer(const char *trace_path
, char *opt_viewer
, bool opt_live_mode
)
171 const char *viewer_bin
= NULL
;
172 const struct viewer
*viewer
;
175 /* Check for --viewer option. */
176 viewer
= parse_viewer_option(opt_viewer
);
177 if (viewer
== NULL
) {
183 switch (viewer
->type
) {
184 case VIEWER_BABELTRACE2
:
185 if (stat(babeltrace2_bin
, &status
) == 0) {
186 viewer_bin
= babeltrace2_bin
;
188 viewer_bin
= viewer
->exec_name
;
190 argv
= alloc_argv_from_local_opts(babeltrace2_opts
,
191 ARRAY_SIZE(babeltrace2_opts
), trace_path
,
194 case VIEWER_BABELTRACE
:
195 if (stat(babeltrace_bin
, &status
) == 0) {
196 viewer_bin
= babeltrace_bin
;
198 viewer_bin
= viewer
->exec_name
;
200 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
201 ARRAY_SIZE(babeltrace_opts
), trace_path
,
204 case VIEWER_USER_DEFINED
:
205 argv
= alloc_argv_from_user_opts(opt_viewer
, trace_path
);
207 viewer_bin
= argv
[0];
214 if (argv
== NULL
|| !viewer_bin
) {
219 DBG("Using %s viewer", viewer_bin
);
221 ret
= execvp(viewer_bin
, argv
);
223 if (errno
== ENOENT
&& viewer
->exec_name
) {
224 if (viewer
->type
== VIEWER_BABELTRACE2
) {
225 /* Fallback to legacy babeltrace. */
226 DBG("Default viewer \"%s\" not installed on the system, falling back to \"%s\"",
227 viewers
[VIEWER_BABELTRACE2
].exec_name
,
228 viewers
[VIEWER_BABELTRACE
].exec_name
);
229 viewer
= &viewers
[VIEWER_BABELTRACE
];
234 ERR("Default viewer \"%s\" (and fallback \"%s\") not found on the system",
235 viewers
[VIEWER_BABELTRACE2
].exec_name
,
236 viewers
[VIEWER_BABELTRACE
].exec_name
);
239 PERROR("Failed to launch \"%s\" viewer", viewer_bin
);
246 * This function should never return if successfull because `execvp(3)`
247 * onle returns if an error has occurred.
249 LTTNG_ASSERT(ret
!= 0);
This page took 0.040292 seconds and 4 git commands to generate.