2 * BabelTrace - LTTng live Output
4 * Copyright 2013 Julien Desfossez <jdesfossez@efficios.com>
5 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include <babeltrace/ctf-text/types.h>
27 #include <babeltrace/format.h>
28 #include <babeltrace/babeltrace-internal.h>
32 #include <sys/types.h>
40 #include "lttng-live.h"
42 static volatile int should_quit
;
44 void bt_lttng_live_hook(void)
47 * Dummy function to prevent the linker from discarding this format as
48 * "unused" in static builds.
52 int lttng_live_should_quit(void)
58 void sighandler(int sig
)
71 * TODO: Eventually, this signal handler setup should be done at the
72 * plugin manager level, rather than within this plugin. Beware, we are
73 * not cleaning up the signal handler after plugin execution.
76 int setup_sighandler(void)
82 if ((ret
= sigemptyset(&sigset
)) < 0) {
83 perror("sigemptyset");
86 sa
.sa_handler
= sighandler
;
89 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
93 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
101 * hostname parameter needs to hold MAXNAMLEN chars.
104 int parse_url(const char *path
, struct lttng_live_ctx
*ctx
)
106 char remain
[3][MAXNAMLEN
];
107 int ret
= -1, proto
, proto_offset
= 0;
108 size_t path_len
= strlen(path
); /* not accounting \0 */
111 * Since sscanf API does not allow easily checking string length
112 * against a size defined by a macro. Test it beforehand on the
113 * input. We know the output is always <= than the input length.
115 if (path_len
>= MAXNAMLEN
) {
118 ret
= sscanf(path
, "net%d://", &proto
);
122 proto_offset
= strlen("net://");
124 /* net4:// or net6:// */
125 proto_offset
= strlen("netX://");
127 if (proto_offset
> path_len
) {
131 fprintf(stderr
, "[error] IPv6 is currently unsupported by lttng-live\n");
134 /* TODO : parse for IPv6 as well */
135 /* Parse the hostname or IP */
136 ret
= sscanf(&path
[proto_offset
], "%[a-zA-Z.0-9%-]%s",
137 ctx
->relay_hostname
, remain
[0]);
139 /* Optional port number */
140 switch (remain
[0][0]) {
142 ret
= sscanf(remain
[0], ":%d%s", &ctx
->port
, remain
[1]);
143 /* Optional session ID with port number */
145 ret
= sscanf(remain
[1], "/%s", remain
[2]);
146 /* Accept 0 or 1 (optional) */
150 } else if (ret
== 0) {
151 fprintf(stderr
, "[error] Missing port number after delimitor ':'\n");
157 /* Optional session ID */
158 ret
= sscanf(remain
[0], "/%s", remain
[2]);
159 /* Accept 0 or 1 (optional) */
165 fprintf(stderr
, "[error] wrong delimitor : %c\n",
173 ctx
->port
= LTTNG_DEFAULT_NETWORK_VIEWER_PORT
;
176 if (strlen(remain
[2]) == 0) {
177 printf_verbose("Connecting to hostname : %s, port : %d, "
179 ctx
->relay_hostname
, ctx
->port
, proto
);
183 ret
= sscanf(remain
[2], "host/%[a-zA-Z.0-9%-]/%s",
184 ctx
->traced_hostname
, ctx
->session_name
);
186 fprintf(stderr
, "[error] Format : "
187 "net://<hostname>/host/<traced_hostname>/<session_name>\n");
191 printf_verbose("Connecting to hostname : %s, port : %d, "
192 "traced hostname : %s, session name : %s, "
194 ctx
->relay_hostname
, ctx
->port
, ctx
->traced_hostname
,
195 ctx
->session_name
, proto
);
203 guint
g_uint64p_hash(gconstpointer key
)
205 uint64_t v
= *(uint64_t *) key
;
207 if (sizeof(gconstpointer
) == sizeof(uint64_t)) {
208 return g_direct_hash((gconstpointer
) (unsigned long) v
);
210 return g_direct_hash((gconstpointer
) (unsigned long) (v
>> 32))
211 ^ g_direct_hash((gconstpointer
) (unsigned long) v
);
216 gboolean
g_uint64p_equal(gconstpointer a
, gconstpointer b
)
218 uint64_t va
= *(uint64_t *) a
;
219 uint64_t vb
= *(uint64_t *) b
;
226 static void free_session_streams(struct lttng_live_session
*lsession
)
228 struct lttng_live_viewer_stream
*lvstream
, *tmp
;
230 bt_list_for_each_entry_safe(lvstream
, tmp
, &lsession
->stream_list
,
232 bt_list_del(&lvstream
->stream_node
);
237 static int lttng_live_open_trace_read(const char *path
)
240 struct lttng_live_ctx
*ctx
;
242 ctx
= g_new0(struct lttng_live_ctx
, 1);
243 ctx
->session
= g_new0(struct lttng_live_session
, 1);
245 BT_INIT_LIST_HEAD(&ctx
->session
->stream_list
);
247 /* We need a pointer to the context from the packet_seek function. */
248 ctx
->session
->ctx
= ctx
;
250 /* HT to store the CTF traces. */
251 ctx
->session
->ctf_traces
= g_hash_table_new(g_uint64p_hash
,
254 ctx
->session_ids
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
256 ret
= parse_url(path
, ctx
);
260 ret
= setup_sighandler();
264 ret
= lttng_live_connect_viewer(ctx
);
268 printf_verbose("LTTng-live connected to relayd\n");
270 ret
= lttng_live_establish_connection(ctx
);
275 printf_verbose("Listing sessions\n");
276 ret
= lttng_live_list_sessions(ctx
, path
);
281 if (ctx
->session_ids
->len
> 0) {
282 ret
= lttng_live_read(ctx
);
286 g_hash_table_destroy(ctx
->session
->ctf_traces
);
287 free_session_streams(ctx
->session
);
288 g_free(ctx
->session
);
291 if (lttng_live_should_quit()) {
298 struct bt_trace_descriptor
*lttng_live_open_trace(const char *path
, int flags
,
299 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
300 int whence
), FILE *metadata_fp
)
302 struct ctf_text_stream_pos
*pos
;
304 switch (flags
& O_ACCMODE
) {
309 fprintf(stderr
, "[error] lttng live plugin cannot be used as output plugin.\n");
312 fprintf(stderr
, "[error] Incorrect open flags.\n");
316 pos
= g_new0(struct ctf_text_stream_pos
, 1);
317 pos
->parent
.rw_table
= NULL
;
318 pos
->parent
.event_cb
= NULL
;
319 pos
->parent
.trace
= &pos
->trace_descriptor
;
321 * Since we do *everything* in this function, we are skipping
322 * the output plugin handling that is part of Babeltrace 1.x.
323 * Therefore, don't expect the --output cmd line option to work.
324 * This limits the output of lttng-live to stderr and stdout.
326 if (lttng_live_open_trace_read(path
) < 0) {
329 return &pos
->trace_descriptor
;
336 int lttng_live_close_trace(struct bt_trace_descriptor
*td
)
338 struct ctf_text_stream_pos
*pos
=
339 container_of(td
, struct ctf_text_stream_pos
,
346 struct bt_format lttng_live_format
= {
347 .open_trace
= lttng_live_open_trace
,
348 .close_trace
= lttng_live_close_trace
,
352 void __attribute__((constructor
)) lttng_live_init(void)
356 lttng_live_format
.name
= g_quark_from_static_string("lttng-live");
357 ret
= bt_register_format(<tng_live_format
);
362 void __attribute__((destructor
)) lttng_live_exit(void)
364 bt_unregister_format(<tng_live_format
);