2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <netinet/in.h>
37 #include "lttng-viewer-abi.h"
38 #include "network-live.h"
39 #include "lttng-live-comm.h"
41 #include <babeltrace/babeltrace.h>
42 #include <babeltrace/ctf/events.h>
43 #include <babeltrace/ctf/callbacks.h>
44 #include <babeltrace/ctf/iterator.h>
46 /* for packet_index */
47 #include <babeltrace/ctf/types.h>
49 #include <babeltrace/ctf/metadata.h>
50 #include <babeltrace/ctf-text/types.h>
51 #include <babeltrace/ctf/events-internal.h>
52 #include <lib/babeltrace/ctf/ctf-index.h>
54 static volatile int should_quit
;
56 int lttng_live_should_quit(void)
63 void sighandler(int sig
)
76 * TODO: Eventually, this signal handler setup should be done at the
77 * plugin manager level, rather than within this plugin. Beware, we are
78 * not cleaning up the signal handler after plugin execution.
81 int setup_sighandler(void)
87 if ((ret
= sigemptyset(&sigset
)) < 0) {
88 perror("sigemptyset");
91 sa
.sa_handler
= sighandler
;
94 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
98 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
107 * hostname parameter needs to hold NAME_MAX chars.
110 int parse_url(const char *path
, struct lttng_live_ctx
*ctx
)
112 char remain
[3][NAME_MAX
];
113 int ret
= -1, proto
, proto_offset
= 0;
114 size_t path_len
= strlen(path
); /* not accounting \0 */
117 * Since sscanf API does not allow easily checking string length
118 * against a size defined by a macro. Test it beforehand on the
119 * input. We know the output is always <= than the input length.
121 if (path_len
>= NAME_MAX
) {
124 ret
= sscanf(path
, "net%d://", &proto
);
128 proto_offset
= strlen("net://");
130 /* net4:// or net6:// */
131 proto_offset
= strlen("netX://");
133 if (proto_offset
> path_len
) {
136 /* TODO : parse for IPv6 as well */
137 /* Parse the hostname or IP */
138 ret
= sscanf(&path
[proto_offset
], "%[a-zA-Z.0-9%-]%s",
139 ctx
->relay_hostname
, remain
[0]);
141 /* Optional port number */
142 switch (remain
[0][0]) {
144 ret
= sscanf(remain
[0], ":%d%s", &ctx
->port
, remain
[1]);
145 /* Optional session ID with port number */
147 ret
= sscanf(remain
[1], "/%s", remain
[2]);
148 /* Accept 0 or 1 (optional) */
152 } else if (ret
== 0) {
153 fprintf(stderr
, "[error] Missing port number after delimitor ':'\n");
159 /* Optional session ID */
160 ret
= sscanf(remain
[0], "/%s", remain
[2]);
161 /* Accept 0 or 1 (optional) */
167 fprintf(stderr
, "[error] wrong delimitor : %c\n",
175 ctx
->port
= LTTNG_DEFAULT_NETWORK_VIEWER_PORT
;
178 if (strlen(remain
[2]) == 0) {
179 printf_verbose("Connecting to hostname : %s, port : %d, "
181 ctx
->relay_hostname
, ctx
->port
, proto
);
185 ret
= sscanf(remain
[2], "host/%[a-zA-Z.0-9%-]/%s",
186 ctx
->traced_hostname
, ctx
->session_name
);
188 fprintf(stderr
, "[error] Format : "
189 "net://<hostname>/host/<traced_hostname>/<session_name>\n");
193 printf_verbose("Connecting to hostname : %s, port : %d, "
194 "traced hostname : %s, session name : %s, "
196 ctx
->relay_hostname
, ctx
->port
, ctx
->traced_hostname
,
197 ctx
->session_name
, proto
);
204 static int lttng_live_open_trace_read(const char *path
)
207 struct lttng_live_ctx
*ctx
;
209 ctx
= g_new0(struct lttng_live_ctx
, 1);
210 ctx
->session
= g_new0(struct lttng_live_session
, 1);
212 /* We need a pointer to the context from the packet_seek function. */
213 ctx
->session
->ctx
= ctx
;
215 /* HT to store the CTF traces. */
216 ctx
->session
->ctf_traces
= g_hash_table_new(g_direct_hash
,
219 ctx
->session_ids
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
221 ret
= parse_url(path
, ctx
);
226 ret
= setup_sighandler();
231 ret
= lttng_live_connect_viewer(ctx
);
235 printf_verbose("LTTng-live connected to relayd\n");
237 ret
= lttng_live_establish_connection(ctx
);
242 printf_verbose("Listing sessions\n");
243 ret
= lttng_live_list_sessions(ctx
, path
);
248 if (ctx
->session_ids
->len
> 0) {
249 ret
= lttng_live_read(ctx
);
253 g_hash_table_destroy(ctx
->session
->ctf_traces
);
254 g_free(ctx
->session
);
255 g_free(ctx
->session
->streams
);
258 if (lttng_live_should_quit()) {
265 struct bt_trace_descriptor
*lttng_live_open_trace(const char *path
, int flags
,
266 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
267 int whence
), FILE *metadata_fp
)
269 struct ctf_text_stream_pos
*pos
;
271 switch (flags
& O_ACCMODE
) {
276 fprintf(stderr
, "[error] lttng live plugin cannot be used as output plugin.\n");
279 fprintf(stderr
, "[error] Incorrect open flags.\n");
283 pos
= g_new0(struct ctf_text_stream_pos
, 1);
284 pos
->parent
.rw_table
= NULL
;
285 pos
->parent
.event_cb
= NULL
;
286 pos
->parent
.trace
= &pos
->trace_descriptor
;
287 if (lttng_live_open_trace_read(path
) < 0) {
290 return &pos
->trace_descriptor
;
297 int lttng_live_close_trace(struct bt_trace_descriptor
*td
)
299 struct ctf_text_stream_pos
*pos
=
300 container_of(td
, struct ctf_text_stream_pos
,
307 struct bt_format lttng_live_format
= {
308 .open_trace
= lttng_live_open_trace
,
309 .close_trace
= lttng_live_close_trace
,
313 void __attribute__((constructor
)) lttng_live_init(void)
317 lttng_live_format
.name
= g_quark_from_static_string("lttng-live");
318 ret
= bt_register_format(<tng_live_format
);
323 void __attribute__((destructor
)) lttng_live_exit(void)
325 bt_unregister_format(<tng_live_format
);