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)
62 void sighandler(int sig
)
75 * TODO: Eventually, this signal handler setup should be done at the
76 * plugin manager level, rather than within this plugin. Beware, we are
77 * not cleaning up the signal handler after plugin execution.
80 int setup_sighandler(void)
86 if ((ret
= sigemptyset(&sigset
)) < 0) {
87 perror("sigemptyset");
90 sa
.sa_handler
= sighandler
;
93 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
97 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
104 * hostname parameter needs to hold NAME_MAX chars.
107 int parse_url(const char *path
, struct lttng_live_ctx
*ctx
)
109 char remain
[3][NAME_MAX
];
110 int ret
= -1, proto
, proto_offset
= 0;
111 size_t path_len
= strlen(path
); /* not accounting \0 */
114 * Since sscanf API does not allow easily checking string length
115 * against a size defined by a macro. Test it beforehand on the
116 * input. We know the output is always <= than the input length.
118 if (path_len
>= NAME_MAX
) {
121 ret
= sscanf(path
, "net%d://", &proto
);
125 proto_offset
= strlen("net://");
127 /* net4:// or net6:// */
128 proto_offset
= strlen("netX://");
130 if (proto_offset
> path_len
) {
133 /* TODO : parse for IPv6 as well */
134 /* Parse the hostname or IP */
135 ret
= sscanf(&path
[proto_offset
], "%[a-zA-Z.0-9%-]%s",
136 ctx
->relay_hostname
, remain
[0]);
138 /* Optional port number */
139 switch (remain
[0][0]) {
141 ret
= sscanf(remain
[0], ":%d%s", &ctx
->port
, remain
[1]);
142 /* Optional session ID with port number */
144 ret
= sscanf(remain
[1], "/%s", remain
[2]);
145 /* Accept 0 or 1 (optional) */
149 } else if (ret
== 0) {
150 fprintf(stderr
, "[error] Missing port number after delimitor ':'\n");
156 /* Optional session ID */
157 ret
= sscanf(remain
[0], "/%s", remain
[2]);
158 /* Accept 0 or 1 (optional) */
164 fprintf(stderr
, "[error] wrong delimitor : %c\n",
172 ctx
->port
= LTTNG_DEFAULT_NETWORK_VIEWER_PORT
;
175 if (strlen(remain
[2]) == 0) {
176 printf_verbose("Connecting to hostname : %s, port : %d, "
178 ctx
->relay_hostname
, ctx
->port
, proto
);
182 ret
= sscanf(remain
[2], "host/%[a-zA-Z.0-9%-]/%s",
183 ctx
->traced_hostname
, ctx
->session_name
);
185 fprintf(stderr
, "[error] Format : "
186 "net://<hostname>/host/<traced_hostname>/<session_name>\n");
190 printf_verbose("Connecting to hostname : %s, port : %d, "
191 "traced hostname : %s, session name : %s, "
193 ctx
->relay_hostname
, ctx
->port
, ctx
->traced_hostname
,
194 ctx
->session_name
, proto
);
201 static int lttng_live_open_trace_read(const char *path
)
204 struct lttng_live_ctx
*ctx
;
206 ctx
= g_new0(struct lttng_live_ctx
, 1);
207 ctx
->session
= g_new0(struct lttng_live_session
, 1);
209 /* We need a pointer to the context from the packet_seek function. */
210 ctx
->session
->ctx
= ctx
;
212 /* HT to store the CTF traces. */
213 ctx
->session
->ctf_traces
= g_hash_table_new(g_direct_hash
,
216 ctx
->session_ids
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
218 ret
= parse_url(path
, ctx
);
223 ret
= setup_sighandler();
228 ret
= lttng_live_connect_viewer(ctx
);
232 printf_verbose("LTTng-live connected to relayd\n");
234 ret
= lttng_live_establish_connection(ctx
);
239 printf_verbose("Listing sessions\n");
240 ret
= lttng_live_list_sessions(ctx
, path
);
245 if (ctx
->session_ids
->len
> 0) {
246 ret
= lttng_live_read(ctx
);
250 g_hash_table_destroy(ctx
->session
->ctf_traces
);
251 g_free(ctx
->session
);
252 g_free(ctx
->session
->streams
);
255 if (lttng_live_should_quit()) {
262 struct bt_trace_descriptor
*lttng_live_open_trace(const char *path
, int flags
,
263 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
264 int whence
), FILE *metadata_fp
)
266 struct ctf_text_stream_pos
*pos
;
268 switch (flags
& O_ACCMODE
) {
273 fprintf(stderr
, "[error] lttng live plugin cannot be used as output plugin.\n");
276 fprintf(stderr
, "[error] Incorrect open flags.\n");
280 pos
= g_new0(struct ctf_text_stream_pos
, 1);
281 pos
->parent
.rw_table
= NULL
;
282 pos
->parent
.event_cb
= NULL
;
283 pos
->parent
.trace
= &pos
->trace_descriptor
;
284 if (lttng_live_open_trace_read(path
) < 0) {
287 return &pos
->trace_descriptor
;
294 int lttng_live_close_trace(struct bt_trace_descriptor
*td
)
296 struct ctf_text_stream_pos
*pos
=
297 container_of(td
, struct ctf_text_stream_pos
,
304 struct bt_format lttng_live_format
= {
305 .open_trace
= lttng_live_open_trace
,
306 .close_trace
= lttng_live_close_trace
,
310 void __attribute__((constructor
)) lttng_live_init(void)
314 lttng_live_format
.name
= g_quark_from_static_string("lttng-live");
315 ret
= bt_register_format(<tng_live_format
);
320 void __attribute__((destructor
)) lttng_live_exit(void)
322 bt_unregister_format(<tng_live_format
);