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>
39 #include "lttng-live-functions.h"
42 * hostname parameter needs to hold NAME_MAX chars.
44 static int parse_url(const char *path
, char *hostname
, int *port
,
47 char remain
[2][NAME_MAX
];
48 int ret
= -1, proto
, proto_offset
= 0;
49 size_t path_len
= strlen(path
);
52 * Since sscanf API does not allow easily checking string length
53 * against a size defined by a macro. Test it beforehand on the
54 * input. We know the output is always <= than the input length.
56 if (path_len
> NAME_MAX
) {
59 ret
= sscanf(path
, "net%d://", &proto
);
63 proto_offset
= strlen("net://");
65 /* net4:// or net6:// */
66 proto_offset
= strlen("netX://");
68 if (proto_offset
> path_len
) {
71 /* TODO : parse for IPv6 as well */
72 /* Parse the hostname or IP */
73 ret
= sscanf(&path
[proto_offset
], "%[a-zA-Z.0-9%-]%s",
76 /* Optional port number */
77 switch (remain
[0][0]) {
79 ret
= sscanf(remain
[0], ":%d%s", port
, remain
[1]);
80 /* Optional session ID with port number */
82 ret
= sscanf(remain
[1], "/%" PRIu64
,
84 /* Accept 0 or 1 (optional) */
91 /* Optional session ID */
92 ret
= sscanf(remain
[0], "/%" PRIu64
, session_id
);
93 /* Accept 0 or 1 (optional) */
99 fprintf(stderr
, "[error] wrong delimitor : %c\n",
107 *port
= LTTNG_DEFAULT_NETWORK_VIEWER_PORT
;
109 if (*session_id
== -1ULL)
110 printf_verbose("Connecting to hostname : %s, port : %d, "
112 hostname
, *port
, proto
);
114 printf_verbose("Connecting to hostname : %s, port : %d, "
115 "session id : %" PRIu64
", proto : IPv%d\n",
116 hostname
, *port
, *session_id
, proto
);
123 static int lttng_live_open_trace_read(const char *path
)
125 char hostname
[NAME_MAX
];
127 uint64_t session_id
= -1ULL;
129 struct lttng_live_ctx ctx
;
131 ctx
.session
= g_new0(struct lttng_live_session
, 1);
133 /* We need a pointer to the context from the packet_seek function. */
134 ctx
.session
->ctx
= &ctx
;
136 /* HT to store the CTF traces. */
137 ctx
.session
->ctf_traces
= g_hash_table_new(g_direct_hash
,
140 ret
= parse_url(path
, hostname
, &port
, &session_id
);
145 ret
= lttng_live_connect_viewer(&ctx
, hostname
, port
);
147 fprintf(stderr
, "[error] Connection failed\n");
150 printf_verbose("LTTng-live connected to relayd\n");
152 ret
= lttng_live_establish_connection(&ctx
);
157 if (session_id
== -1ULL) {
158 printf_verbose("Listing sessions\n");
159 ret
= lttng_live_list_sessions(&ctx
, path
);
161 fprintf(stderr
, "[error] List error\n");
165 lttng_live_read(&ctx
, session_id
);
169 g_hash_table_destroy(ctx
.session
->ctf_traces
);
171 g_free(ctx
.session
->streams
);
176 struct bt_trace_descriptor
*lttng_live_open_trace(const char *path
, int flags
,
177 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
178 int whence
), FILE *metadata_fp
)
180 struct ctf_text_stream_pos
*pos
;
182 switch (flags
& O_ACCMODE
) {
187 fprintf(stderr
, "[error] lttng live plugin cannot be used as output plugin.\n");
190 fprintf(stderr
, "[error] Incorrect open flags.\n");
194 pos
= g_new0(struct ctf_text_stream_pos
, 1);
195 pos
->parent
.rw_table
= NULL
;
196 pos
->parent
.event_cb
= NULL
;
197 pos
->parent
.trace
= &pos
->trace_descriptor
;
198 lttng_live_open_trace_read(path
);
199 return &pos
->trace_descriptor
;
206 int lttng_live_close_trace(struct bt_trace_descriptor
*td
)
208 struct ctf_text_stream_pos
*pos
=
209 container_of(td
, struct ctf_text_stream_pos
,
216 struct bt_format lttng_live_format
= {
217 .open_trace
= lttng_live_open_trace
,
218 .close_trace
= lttng_live_close_trace
,
222 void __attribute__((constructor
)) lttng_live_init(void)
226 lttng_live_format
.name
= g_quark_from_static_string("lttng-live");
227 ret
= bt_register_format(<tng_live_format
);
232 void __attribute__((destructor
)) lttng_live_exit(void)
234 bt_unregister_format(<tng_live_format
);