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"
41 static int parse_url(const char *path
, char *hostname
, int *port
,
44 char remain
[NAME_MAX
];
45 int ret
, proto
, proto_offset
= 0;
47 ret
= sscanf(path
, "net%d%s", &proto
, remain
);
51 proto_offset
= strlen("net://");
53 /* net4:// or net6:// */
54 proto_offset
= strlen("netX://");
56 /* TODO : parse for IPv6 as well */
57 /* Parse the hostname or IP */
58 ret
= sscanf(path
+ proto_offset
, "%[a-zA-Z.1-9%-]%s",
61 /* Optional port number */
62 if (remain
[0] == ':') {
63 ret
= sscanf(remain
, ":%d%s", port
, remain
);
64 /* Optional session ID with port number */
66 ret
= sscanf(remain
, "/%" PRIu64
, session_id
);
68 /* Optional session ID */
69 } else if (remain
[0] == '/') {
70 ret
= sscanf(remain
, "/%" PRIu64
, session_id
);
72 fprintf(stderr
, "[error] wrong delimitor : %c\n",
80 *port
= LTTNG_DEFAULT_NETWORK_VIEWER_PORT
;
82 if (*session_id
== -1ULL)
83 printf_verbose("Connecting to hostname : %s, port : %d, "
85 hostname
, *port
, proto
);
87 printf_verbose("Connecting to hostname : %s, port : %d, "
88 "session id : %" PRIu64
", proto : IPv%d\n",
89 hostname
, *port
, *session_id
, proto
);
96 static int lttng_live_open_trace_read(const char *path
)
98 char hostname
[NAME_MAX
];
100 uint64_t session_id
= -1ULL;
102 struct lttng_live_ctx ctx
;
104 ctx
.session
= g_new0(struct lttng_live_session
, 1);
106 /* We need a pointer to the context from the packet_seek function. */
107 ctx
.session
->ctx
= &ctx
;
109 /* HT to store the CTF traces. */
110 ctx
.session
->ctf_traces
= g_hash_table_new(g_direct_hash
,
113 ret
= parse_url(path
, hostname
, &port
, &session_id
);
118 ret
= lttng_live_connect_viewer(&ctx
, hostname
, port
);
120 fprintf(stderr
, "[error] Connection failed\n");
123 printf_verbose("LTTng-live connected to relayd\n");
125 ret
= lttng_live_establish_connection(&ctx
);
130 if (session_id
== -1ULL) {
131 printf_verbose("Listing sessions\n");
132 ret
= lttng_live_list_sessions(&ctx
, path
);
134 fprintf(stderr
, "[error] List error\n");
138 lttng_live_read(&ctx
, session_id
);
142 g_hash_table_destroy(ctx
.session
->ctf_traces
);
144 g_free(ctx
.session
->streams
);
149 struct bt_trace_descriptor
*lttng_live_open_trace(const char *path
, int flags
,
150 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
151 int whence
), FILE *metadata_fp
)
153 struct ctf_text_stream_pos
*pos
;
155 switch (flags
& O_ACCMODE
) {
160 fprintf(stderr
, "[error] lttng live plugin cannot be used as output plugin.\n");
163 fprintf(stderr
, "[error] Incorrect open flags.\n");
167 pos
= g_new0(struct ctf_text_stream_pos
, 1);
168 pos
->parent
.rw_table
= NULL
;
169 pos
->parent
.event_cb
= NULL
;
170 pos
->parent
.trace
= &pos
->trace_descriptor
;
171 lttng_live_open_trace_read(path
);
172 return &pos
->trace_descriptor
;
179 int lttng_live_close_trace(struct bt_trace_descriptor
*td
)
181 struct ctf_text_stream_pos
*pos
=
182 container_of(td
, struct ctf_text_stream_pos
,
189 struct bt_format lttng_live_format
= {
190 .open_trace
= lttng_live_open_trace
,
191 .close_trace
= lttng_live_close_trace
,
195 void __attribute__((constructor
)) lttng_live_init(void)
199 lttng_live_format
.name
= g_quark_from_static_string("lttng-live");
200 ret
= bt_register_format(<tng_live_format
);
205 void __attribute__((destructor
)) lttng_live_exit(void)
207 bt_unregister_format(<tng_live_format
);