Create the live viewer session before attaching
[babeltrace.git] / formats / lttng-live / lttng-live.c
CommitLineData
4a744367
JD
1/*
2 * BabelTrace - LTTng live Output
3 *
4 * Copyright 2013 Julien Desfossez <jdesfossez@efficios.com>
5 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * SOFTWARE.
24 */
25
26#include <babeltrace/ctf-text/types.h>
27#include <babeltrace/format.h>
28#include <babeltrace/babeltrace-internal.h>
29#include <inttypes.h>
30#include <sys/mman.h>
31#include <errno.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <dirent.h>
36#include <glib.h>
37#include <unistd.h>
38#include <stdlib.h>
39#include "lttng-live-functions.h"
40
e84d6f79
MD
41/*
42 * hostname parameter needs to hold NAME_MAX chars.
43 */
4a744367
JD
44static int parse_url(const char *path, char *hostname, int *port,
45 uint64_t *session_id)
46{
e84d6f79
MD
47 char remain[2][NAME_MAX];
48 int ret = -1, proto, proto_offset = 0;
49 size_t path_len = strlen(path);
4a744367 50
e84d6f79
MD
51 /*
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.
55 */
56 if (path_len > NAME_MAX) {
57 goto end;
58 }
59 ret = sscanf(path, "net%d://", &proto);
60 if (ret < 1) {
4a744367
JD
61 proto = 4;
62 /* net:// */
63 proto_offset = strlen("net://");
64 } else {
65 /* net4:// or net6:// */
66 proto_offset = strlen("netX://");
67 }
e84d6f79
MD
68 if (proto_offset > path_len) {
69 goto end;
70 }
4a744367
JD
71 /* TODO : parse for IPv6 as well */
72 /* Parse the hostname or IP */
e84d6f79
MD
73 ret = sscanf(&path[proto_offset], "%[a-zA-Z.0-9%-]%s",
74 hostname, remain[0]);
4a744367
JD
75 if (ret == 2) {
76 /* Optional port number */
e84d6f79
MD
77 switch (remain[0][0]) {
78 case ':':
79 ret = sscanf(remain[0], ":%d%s", port, remain[1]);
4a744367
JD
80 /* Optional session ID with port number */
81 if (ret == 2) {
e84d6f79
MD
82 ret = sscanf(remain[1], "/%" PRIu64,
83 session_id);
84 /* Accept 0 or 1 (optional) */
85 if (ret < 0) {
86 goto end;
87 }
4a744367 88 }
e84d6f79
MD
89 break;
90 case '/':
4a744367 91 /* Optional session ID */
e84d6f79
MD
92 ret = sscanf(remain[0], "/%" PRIu64, session_id);
93 /* Accept 0 or 1 (optional) */
94 if (ret < 0) {
95 goto end;
96 }
97 break;
98 default:
4a744367 99 fprintf(stderr, "[error] wrong delimitor : %c\n",
e84d6f79 100 remain[0][0]);
4a744367
JD
101 ret = -1;
102 goto end;
103 }
104 }
105
106 if (*port < 0)
107 *port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
108
109 if (*session_id == -1ULL)
110 printf_verbose("Connecting to hostname : %s, port : %d, "
111 "proto : IPv%d\n",
112 hostname, *port, proto);
113 else
114 printf_verbose("Connecting to hostname : %s, port : %d, "
115 "session id : %" PRIu64 ", proto : IPv%d\n",
116 hostname, *port, *session_id, proto);
117 ret = 0;
118
119end:
120 return ret;
121}
122
123static int lttng_live_open_trace_read(const char *path)
124{
125 char hostname[NAME_MAX];
126 int port = -1;
127 uint64_t session_id = -1ULL;
128 int ret = 0;
129 struct lttng_live_ctx ctx;
130
4a744367 131 ctx.session = g_new0(struct lttng_live_session, 1);
3af4fc48 132
4a744367
JD
133 /* We need a pointer to the context from the packet_seek function. */
134 ctx.session->ctx = &ctx;
135
136 /* HT to store the CTF traces. */
137 ctx.session->ctf_traces = g_hash_table_new(g_direct_hash,
138 g_direct_equal);
139
140 ret = parse_url(path, hostname, &port, &session_id);
141 if (ret < 0) {
142 goto end_free;
143 }
144
145 ret = lttng_live_connect_viewer(&ctx, hostname, port);
146 if (ret < 0) {
147 fprintf(stderr, "[error] Connection failed\n");
148 goto end_free;
149 }
150 printf_verbose("LTTng-live connected to relayd\n");
151
152 ret = lttng_live_establish_connection(&ctx);
153 if (ret < 0) {
154 goto end_free;
155 }
156
157 if (session_id == -1ULL) {
158 printf_verbose("Listing sessions\n");
159 ret = lttng_live_list_sessions(&ctx, path);
160 if (ret < 0) {
161 fprintf(stderr, "[error] List error\n");
162 goto end_free;
163 }
164 } else {
165 lttng_live_read(&ctx, session_id);
166 }
167
168end_free:
169 g_hash_table_destroy(ctx.session->ctf_traces);
170 g_free(ctx.session);
4a744367
JD
171 g_free(ctx.session->streams);
172 return ret;
173}
174
175static
176struct 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)
179{
180 struct ctf_text_stream_pos *pos;
181
182 switch (flags & O_ACCMODE) {
183 case O_RDONLY:
184 /* OK */
185 break;
186 case O_RDWR:
187 fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n");
188 goto error;
189 default:
190 fprintf(stderr, "[error] Incorrect open flags.\n");
191 goto error;
192 }
193
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;
200
201error:
202 return NULL;
203}
204
205static
206int lttng_live_close_trace(struct bt_trace_descriptor *td)
207{
208 struct ctf_text_stream_pos *pos =
209 container_of(td, struct ctf_text_stream_pos,
210 trace_descriptor);
211 free(pos);
212 return 0;
213}
214
215static
216struct bt_format lttng_live_format = {
217 .open_trace = lttng_live_open_trace,
218 .close_trace = lttng_live_close_trace,
219};
220
221static
222void __attribute__((constructor)) lttng_live_init(void)
223{
224 int ret;
225
226 lttng_live_format.name = g_quark_from_static_string("lttng-live");
227 ret = bt_register_format(&lttng_live_format);
228 assert(!ret);
229}
230
231static
232void __attribute__((destructor)) lttng_live_exit(void)
233{
234 bt_unregister_format(&lttng_live_format);
235}
This page took 0.031231 seconds and 4 git commands to generate.