2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/spawn-viewer.hpp>
18 #include <sys/types.h>
21 static char *opt_viewer
;
22 static char *opt_trace_path
;
24 #ifdef LTTNG_EMBED_HELP
25 static const char help_msg
[] =
26 #include <lttng-view.1.h>
35 static struct poptOption long_options
[] = {
36 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
37 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
38 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
39 { "viewer", 'e', POPT_ARG_STRING
, &opt_viewer
, 0, nullptr, nullptr },
40 { "trace-path", 't', POPT_ARG_STRING
, &opt_trace_path
, 0, nullptr, nullptr },
41 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
44 /* Is the session we are trying to view is in live mode. */
45 static int session_live_mode
;
48 * Build the live path we need for the lttng live view.
50 static char *build_live_path(char *session_name
)
54 char hostname
[LTTNG_HOST_NAME_MAX
];
56 ret
= gethostname(hostname
, sizeof(hostname
));
58 PERROR("gethostname");
62 ret
= asprintf(&path
, "net://localhost/host/%s/%s", hostname
, session_name
);
64 PERROR("asprintf live path");
73 * Exec viewer if found and use session name path.
75 static int view_trace(const char *arg_session_name
)
78 char *session_name
, *trace_path
= nullptr;
79 struct lttng_session
*sessions
= nullptr;
80 bool free_trace_path
= false;
83 * Safety net. If lttng is suid at some point for *any* useless reasons,
84 * this prevent any bad execution of binaries.
87 if (getuid() != geteuid()) {
88 ERR("UID does not match effective UID.");
91 } else if (getgid() != getegid()) {
92 ERR("GID does not match effective GID.");
98 /* User define trace path override the session name */
100 session_name
= nullptr;
102 if (arg_session_name
== nullptr) {
103 session_name
= get_session_name();
105 session_name
= strdup(arg_session_name
);
106 if (session_name
== nullptr) {
107 PERROR("Failed to copy session name");
111 if (session_name
== nullptr) {
117 DBG("Viewing trace for session %s", session_name
);
120 int i
, count
, found
= 0;
122 /* Getting all sessions */
123 count
= lttng_list_sessions(&sessions
);
125 ERR("Unable to list sessions. Session name %s not found.", session_name
);
126 MSG("Is there a session daemon running?");
131 /* Find our session listed by the session daemon */
132 for (i
= 0; i
< count
; i
++) {
133 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
140 MSG("Session name %s not found", session_name
);
145 session_live_mode
= sessions
[i
].live_timer_interval
;
147 DBG("Session live mode set to %d", session_live_mode
);
149 if (sessions
[i
].enabled
&& !session_live_mode
) {
150 WARN("Session %s is running. Please stop it before reading it.",
156 /* If the timer interval is set we are in live mode. */
157 if (session_live_mode
) {
158 trace_path
= build_live_path(session_name
);
163 free_trace_path
= true;
165 /* Get file system session path. */
166 trace_path
= sessions
[i
].path
;
169 trace_path
= opt_trace_path
;
172 MSG("Trace directory: %s\n", trace_path
);
174 ret
= spawn_viewer(trace_path
, opt_viewer
, session_live_mode
);
176 /* Don't set ret so lttng can interpret the sessiond error. */
181 if (session_live_mode
&& free_trace_path
) {
192 * The 'view <options>' first level command
194 int cmd_view(int argc
, const char **argv
)
196 int opt
, ret
= CMD_SUCCESS
;
197 static poptContext pc
;
198 const char *arg_session_name
= nullptr;
199 const char *leftover
= nullptr;
201 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
202 poptReadDefaultConfig(pc
, 0);
205 WARN("mi does not apply to view command");
208 while ((opt
= poptGetNextOpt(pc
)) != -1) {
213 case OPT_LIST_OPTIONS
:
214 list_cmd_options(stdout
, long_options
);
222 arg_session_name
= poptGetArg(pc
);
224 leftover
= poptGetArg(pc
);
226 ERR("Unknown argument: %s", leftover
);
231 ret
= view_trace(arg_session_name
);
This page took 0.033464 seconds and 4 git commands to generate.