2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <common/spawn-viewer.hpp>
18 #include "../command.hpp"
20 static char *opt_session_name
;
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
, 0, OPT_HELP
, 0, 0},
38 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
39 {"viewer", 'e', POPT_ARG_STRING
, &opt_viewer
, 0, 0, 0},
40 {"trace-path", 't', POPT_ARG_STRING
, &opt_trace_path
, 0, 0, 0},
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
,
65 PERROR("asprintf live path");
74 * Exec viewer if found and use session name path.
76 static int view_trace(void)
79 char *session_name
, *trace_path
= NULL
;
80 struct lttng_session
*sessions
= NULL
;
81 bool free_trace_path
= false;
84 * Safety net. If lttng is suid at some point for *any* useless reasons,
85 * this prevent any bad execution of binaries.
88 if (getuid() != geteuid()) {
89 ERR("UID does not match effective UID.");
92 } else if (getgid() != getegid()) {
93 ERR("GID does not match effective GID.");
99 /* User define trace path override the session name */
100 if (opt_trace_path
) {
102 } else if(opt_session_name
== NULL
) {
103 session_name
= get_session_name();
104 if (session_name
== NULL
) {
109 session_name
= opt_session_name
;
112 DBG("Viewing trace for session %s", session_name
);
115 int i
, count
, found
= 0;
117 /* Getting all sessions */
118 count
= lttng_list_sessions(&sessions
);
120 ERR("Unable to list sessions. Session name %s not found.",
122 MSG("Is there a session daemon running?");
127 /* Find our session listed by the session daemon */
128 for (i
= 0; i
< count
; i
++) {
129 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
136 MSG("Session name %s not found", session_name
);
141 session_live_mode
= sessions
[i
].live_timer_interval
;
143 DBG("Session live mode set to %d", session_live_mode
);
145 if (sessions
[i
].enabled
&& !session_live_mode
) {
146 WARN("Session %s is running. Please stop it before reading it.",
152 /* If the timer interval is set we are in live mode. */
153 if (session_live_mode
) {
154 trace_path
= build_live_path(session_name
);
159 free_trace_path
= true;
161 /* Get file system session path. */
162 trace_path
= sessions
[i
].path
;
165 trace_path
= opt_trace_path
;
168 MSG("Trace directory: %s\n", trace_path
);
170 ret
= spawn_viewer(trace_path
, opt_viewer
, session_live_mode
);
172 /* Don't set ret so lttng can interpret the sessiond error. */
177 if (session_live_mode
&& free_trace_path
) {
182 if (opt_session_name
== NULL
) {
190 * The 'view <options>' first level command
192 int cmd_view(int argc
, const char **argv
)
194 int opt
, ret
= CMD_SUCCESS
;
195 static poptContext pc
;
196 const char *leftover
= NULL
;
198 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
199 poptReadDefaultConfig(pc
, 0);
202 WARN("mi does not apply to view command");
205 while ((opt
= poptGetNextOpt(pc
)) != -1) {
210 case OPT_LIST_OPTIONS
:
211 list_cmd_options(stdout
, long_options
);
219 opt_session_name
= (char*) poptGetArg(pc
);
221 leftover
= poptGetArg(pc
);
223 ERR("Unknown argument: %s", leftover
);
This page took 0.055687 seconds and 4 git commands to generate.