Fix: memstream compat layer requires use of babeltrace_close_memstream
[babeltrace.git] / formats / lttng-live / lttng-live-comm.c
CommitLineData
4a744367
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * SOFTWARE.
22 */
23
24#include <sys/socket.h>
25#include <sys/types.h>
26#include <netinet/in.h>
27#include <netdb.h>
28#include <stdio.h>
4a744367
JD
29#include <stdlib.h>
30#include <unistd.h>
31#include <errno.h>
32#include <inttypes.h>
33#include <fcntl.h>
34#include <sys/mman.h>
85e2085e 35#include <poll.h>
4a744367
JD
36
37#include <babeltrace/ctf/ctf-index.h>
38
39#include <babeltrace/babeltrace.h>
f6afb94e 40#include <babeltrace/endian.h>
4a744367
JD
41#include <babeltrace/ctf/events.h>
42#include <babeltrace/ctf/callbacks.h>
43#include <babeltrace/ctf/iterator.h>
44
45/* for packet_index */
46#include <babeltrace/ctf/types.h>
47
48#include <babeltrace/ctf/metadata.h>
49#include <babeltrace/ctf-text/types.h>
50#include <babeltrace/ctf/events-internal.h>
51#include <formats/ctf/events-private.h>
52
af701dc4 53#include <babeltrace/compat/memstream.h>
b319430e 54#include <babeltrace/compat/send.h>
5ea98697 55#include <babeltrace/compat/string.h>
af701dc4 56
c98627ca 57#include "lttng-live.h"
158eea6e 58#include "lttng-viewer-abi.h"
4a744367 59
85e2085e
MD
60#define ACTIVE_POLL_DELAY 100 /* ms */
61
4a744367
JD
62/*
63 * Memory allocation zeroed
64 */
65#define zmalloc(x) calloc(1, x)
66
67#ifndef max_t
68#define max_t(type, a, b) \
69 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
70#endif
71
b5a1fa45
JD
72static void ctf_live_packet_seek(struct bt_stream_pos *stream_pos,
73 size_t index, int whence);
7840bb08 74static int add_traces(struct lttng_live_ctx *ctx);
2acdc547 75static int del_traces(gpointer key, gpointer value, gpointer user_data);
9abca178 76static int get_new_metadata(struct lttng_live_ctx *ctx,
af701dc4
JD
77 struct lttng_live_viewer_stream *viewer_stream,
78 char **metadata_buf);
2acdc547 79
b7e8a8e1
MD
80static
81ssize_t lttng_live_recv(int fd, void *buf, size_t len)
82{
83 ssize_t ret;
84 size_t copied = 0, to_copy = len;
85
86 do {
87 ret = recv(fd, buf + copied, to_copy, 0);
88 if (ret > 0) {
89 assert(ret <= to_copy);
90 copied += ret;
91 to_copy -= ret;
92 }
93 } while ((ret > 0 && to_copy > 0)
94 || (ret < 0 && errno == EINTR));
95 if (ret > 0)
96 ret = copied;
97 /* ret = 0 means orderly shutdown, ret < 0 is error. */
98 return ret;
99}
100
101static
102ssize_t lttng_live_send(int fd, const void *buf, size_t len)
103{
104 ssize_t ret;
105
106 do {
b319430e 107 ret = bt_send_nosigpipe(fd, buf, len);
b7e8a8e1
MD
108 } while (ret < 0 && errno == EINTR);
109 return ret;
110}
111
b5a1fa45 112int lttng_live_connect_viewer(struct lttng_live_ctx *ctx)
4a744367
JD
113{
114 struct hostent *host;
115 struct sockaddr_in server_addr;
116 int ret;
117
8ace20bf 118 if (lttng_live_should_quit()) {
4a744367
JD
119 ret = -1;
120 goto end;
121 }
122
8ace20bf
MD
123 host = gethostbyname(ctx->relay_hostname);
124 if (!host) {
125 fprintf(stderr, "[error] Cannot lookup hostname %s\n",
126 ctx->relay_hostname);
127 goto error;
128 }
129
4a744367
JD
130 if ((ctx->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
131 perror("Socket");
8ace20bf 132 goto error;
4a744367
JD
133 }
134
135 server_addr.sin_family = AF_INET;
b5a1fa45 136 server_addr.sin_port = htons(ctx->port);
4a744367 137 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
ed959a6d 138 memset(&(server_addr.sin_zero), 0, 8);
4a744367
JD
139
140 if (connect(ctx->control_sock, (struct sockaddr *) &server_addr,
141 sizeof(struct sockaddr)) == -1) {
142 perror("Connect");
8ace20bf 143 goto error;
4a744367
JD
144 }
145
146 ret = 0;
147
148end:
149 return ret;
8ace20bf
MD
150
151error:
152 fprintf(stderr, "[error] Connection failed\n");
153 return -1;
4a744367
JD
154}
155
156int lttng_live_establish_connection(struct lttng_live_ctx *ctx)
157{
158 struct lttng_viewer_cmd cmd;
159 struct lttng_viewer_connect connect;
160 int ret;
161 ssize_t ret_len;
162
8ace20bf
MD
163 if (lttng_live_should_quit()) {
164 ret = -1;
165 goto end;
166 }
167
4a744367
JD
168 cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT);
169 cmd.data_size = sizeof(connect);
170 cmd.cmd_version = 0;
171
0c3cd7e1 172 connect.viewer_session_id = -1ULL; /* will be set on recv */
4a744367
JD
173 connect.major = htobe32(LTTNG_LIVE_MAJOR);
174 connect.minor = htobe32(LTTNG_LIVE_MINOR);
175 connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);
176
b7e8a8e1 177 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 178 if (ret_len < 0) {
79620497 179 perror("[error] Error sending cmd");
4a744367
JD
180 goto error;
181 }
182 assert(ret_len == sizeof(cmd));
183
b7e8a8e1 184 ret_len = lttng_live_send(ctx->control_sock, &connect, sizeof(connect));
4a744367 185 if (ret_len < 0) {
79620497 186 perror("[error] Error sending version");
4a744367
JD
187 goto error;
188 }
189 assert(ret_len == sizeof(connect));
190
b7e8a8e1 191 ret_len = lttng_live_recv(ctx->control_sock, &connect, sizeof(connect));
f8612f31
MD
192 if (ret_len == 0) {
193 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
194 goto error;
195 }
4a744367 196 if (ret_len < 0) {
79620497 197 perror("[error] Error receiving version");
4a744367
JD
198 goto error;
199 }
200 assert(ret_len == sizeof(connect));
201
202 printf_verbose("Received viewer session ID : %" PRIu64 "\n",
203 be64toh(connect.viewer_session_id));
204 printf_verbose("Relayd version : %u.%u\n", be32toh(connect.major),
205 be32toh(connect.minor));
9aa155c5
JD
206
207 if (LTTNG_LIVE_MAJOR != be32toh(connect.major)) {
208 fprintf(stderr, "[error] Incompatible lttng-relayd protocol\n");
209 goto error;
210 }
211 /* Use the smallest protocol version implemented. */
212 if (LTTNG_LIVE_MINOR > be32toh(connect.minor)) {
213 ctx->minor = be32toh(connect.minor);
214 } else {
215 ctx->minor = LTTNG_LIVE_MINOR;
216 }
217 ctx->major = LTTNG_LIVE_MAJOR;
4a744367 218 ret = 0;
8ace20bf
MD
219end:
220 return ret;
4a744367
JD
221
222error:
8ace20bf
MD
223 fprintf(stderr, "[error] Unable to establish connection\n");
224 return -1;
4a744367
JD
225}
226
b5a1fa45
JD
227static
228void free_session_list(GPtrArray *session_list)
229{
230 int i;
231 struct lttng_live_relay_session *relay_session;
232
233 for (i = 0; i < session_list->len; i++) {
234 relay_session = g_ptr_array_index(session_list, i);
235 free(relay_session->name);
236 free(relay_session->hostname);
237 }
238 g_ptr_array_free(session_list, TRUE);
239}
240
241static
242void print_session_list(GPtrArray *session_list, const char *path)
243{
244 int i;
245 struct lttng_live_relay_session *relay_session;
246
247 for (i = 0; i < session_list->len; i++) {
248 relay_session = g_ptr_array_index(session_list, i);
249 fprintf(stdout, "%s/host/%s/%s (timer = %u, "
250 "%u stream(s), %u client(s) connected)\n",
251 path, relay_session->hostname,
252 relay_session->name, relay_session->timer,
253 relay_session->streams, relay_session->clients);
254 }
255}
256
257static
258void update_session_list(GPtrArray *session_list, char *hostname,
259 char *session_name, uint32_t streams, uint32_t clients,
260 uint32_t timer)
261{
262 int i, found = 0;
263 struct lttng_live_relay_session *relay_session;
264
265 for (i = 0; i < session_list->len; i++) {
266 relay_session = g_ptr_array_index(session_list, i);
0206bb4a 267 if ((strncmp(relay_session->hostname, hostname, MAXNAMLEN) == 0) &&
b5a1fa45 268 strncmp(relay_session->name,
0206bb4a 269 session_name, MAXNAMLEN) == 0) {
b5a1fa45
JD
270 relay_session->streams += streams;
271 if (relay_session->clients < clients)
272 relay_session->clients = clients;
273 found = 1;
274 break;
275 }
276 }
277 if (found)
278 return;
279
280 relay_session = g_new0(struct lttng_live_relay_session, 1);
5ea98697
MJ
281 relay_session->hostname = bt_strndup(hostname, MAXNAMLEN);
282 relay_session->name = bt_strndup(session_name, MAXNAMLEN);
b5a1fa45
JD
283 relay_session->clients = clients;
284 relay_session->streams = streams;
285 relay_session->timer = timer;
286 g_ptr_array_add(session_list, relay_session);
287}
288
4a744367
JD
289int lttng_live_list_sessions(struct lttng_live_ctx *ctx, const char *path)
290{
291 struct lttng_viewer_cmd cmd;
292 struct lttng_viewer_list_sessions list;
293 struct lttng_viewer_session lsession;
b5a1fa45 294 int i, ret, sessions_count, print_list = 0;
4a744367 295 ssize_t ret_len;
b5a1fa45
JD
296 uint64_t session_id;
297 GPtrArray *session_list = NULL;
298
8ace20bf
MD
299 if (lttng_live_should_quit()) {
300 ret = -1;
301 goto end;
302 }
303
b5a1fa45
JD
304 if (strlen(ctx->session_name) == 0) {
305 print_list = 1;
306 session_list = g_ptr_array_new();
307 }
4a744367
JD
308
309 cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);
310 cmd.data_size = 0;
311 cmd.cmd_version = 0;
312
b7e8a8e1 313 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 314 if (ret_len < 0) {
79620497 315 perror("[error] Error sending cmd");
4a744367
JD
316 goto error;
317 }
318 assert(ret_len == sizeof(cmd));
319
b7e8a8e1 320 ret_len = lttng_live_recv(ctx->control_sock, &list, sizeof(list));
f8612f31
MD
321 if (ret_len == 0) {
322 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
323 goto error;
324 }
4a744367 325 if (ret_len < 0) {
79620497 326 perror("[error] Error receiving session list");
4a744367
JD
327 goto error;
328 }
329 assert(ret_len == sizeof(list));
330
331 sessions_count = be32toh(list.sessions_count);
4a744367 332 for (i = 0; i < sessions_count; i++) {
b7e8a8e1 333 ret_len = lttng_live_recv(ctx->control_sock, &lsession, sizeof(lsession));
f8612f31
MD
334 if (ret_len == 0) {
335 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
336 goto error;
337 }
4a744367 338 if (ret_len < 0) {
79620497 339 perror("[error] Error receiving session");
4a744367
JD
340 goto error;
341 }
342 assert(ret_len == sizeof(lsession));
9ed31e53
MD
343 lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0';
344 lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
b5a1fa45
JD
345 session_id = be64toh(lsession.id);
346
347 if (print_list) {
348 update_session_list(session_list,
349 lsession.hostname,
350 lsession.session_name,
351 be32toh(lsession.streams),
352 be32toh(lsession.clients),
353 be32toh(lsession.live_timer));
354 } else {
355 if ((strncmp(lsession.session_name, ctx->session_name,
0206bb4a
MJ
356 MAXNAMLEN) == 0) && (strncmp(lsession.hostname,
357 ctx->traced_hostname, MAXNAMLEN) == 0)) {
b5a1fa45
JD
358 printf_verbose("Reading from session %" PRIu64 "\n",
359 session_id);
360 g_array_append_val(ctx->session_ids,
361 session_id);
362 }
363 }
364 }
4a744367 365
b5a1fa45
JD
366 if (print_list) {
367 print_session_list(session_list, path);
368 free_session_list(session_list);
4a744367 369 }
4a744367 370 ret = 0;
8ace20bf
MD
371end:
372 return ret;
4a744367
JD
373
374error:
8ace20bf
MD
375 fprintf(stderr, "[error] Unable to list sessions\n");
376 return -1;
4a744367
JD
377}
378
379int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream *stream,
380 uint64_t ctf_trace_id)
381{
382 struct lttng_live_ctf_trace *trace;
383 int ret = 0;
384
385 trace = g_hash_table_lookup(stream->session->ctf_traces,
37307240 386 &ctf_trace_id);
4a744367
JD
387 if (!trace) {
388 trace = g_new0(struct lttng_live_ctf_trace, 1);
4a744367
JD
389 trace->ctf_trace_id = ctf_trace_id;
390 trace->streams = g_ptr_array_new();
391 g_hash_table_insert(stream->session->ctf_traces,
37307240 392 &trace->ctf_trace_id,
4a744367
JD
393 trace);
394 }
395 if (stream->metadata_flag)
396 trace->metadata_stream = stream;
397
398 stream->ctf_trace = trace;
399 g_ptr_array_add(trace->streams, stream);
400
4a744367
JD
401 return ret;
402}
403
af701dc4
JD
404static
405int open_metadata_fp_write(struct lttng_live_viewer_stream *stream,
406 char **metadata_buf, size_t *size)
407{
408 int ret = 0;
409
410 stream->metadata_fp_write =
411 babeltrace_open_memstream(metadata_buf, size);
412 if (!stream->metadata_fp_write) {
413 perror("Metadata open_memstream");
414 ret = -1;
415 }
416
417 return ret;
418}
419
4a744367
JD
420int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
421{
422 struct lttng_viewer_cmd cmd;
423 struct lttng_viewer_attach_session_request rq;
424 struct lttng_viewer_attach_session_response rp;
425 struct lttng_viewer_stream stream;
426 int ret, i;
427 ssize_t ret_len;
428
8ace20bf
MD
429 if (lttng_live_should_quit()) {
430 ret = -1;
431 goto end;
432 }
433
4a744367
JD
434 cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION);
435 cmd.data_size = sizeof(rq);
436 cmd.cmd_version = 0;
437
438 memset(&rq, 0, sizeof(rq));
439 rq.session_id = htobe64(id);
440 // TODO: add cmd line parameter to select seek beginning
441 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
442 rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST);
443
b7e8a8e1 444 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 445 if (ret_len < 0) {
79620497 446 perror("[error] Error sending cmd");
4a744367
JD
447 goto error;
448 }
449 assert(ret_len == sizeof(cmd));
450
b7e8a8e1 451 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
4a744367 452 if (ret_len < 0) {
79620497 453 perror("[error] Error sending attach request");
4a744367
JD
454 goto error;
455 }
456 assert(ret_len == sizeof(rq));
457
b7e8a8e1 458 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
f8612f31
MD
459 if (ret_len == 0) {
460 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
461 goto error;
462 }
4a744367 463 if (ret_len < 0) {
79620497 464 perror("[error] Error receiving attach response");
4a744367
JD
465 goto error;
466 }
467 assert(ret_len == sizeof(rp));
468
469 switch(be32toh(rp.status)) {
470 case LTTNG_VIEWER_ATTACH_OK:
471 break;
472 case LTTNG_VIEWER_ATTACH_UNK:
473 ret = -LTTNG_VIEWER_ATTACH_UNK;
474 goto end;
475 case LTTNG_VIEWER_ATTACH_ALREADY:
79620497 476 fprintf(stderr, "[error] There is already a viewer attached to this session\n");
8ace20bf 477 goto error;
4a744367
JD
478 case LTTNG_VIEWER_ATTACH_NOT_LIVE:
479 fprintf(stderr, "[error] Not a live session\n");
8ace20bf 480 goto error;
4a744367
JD
481 case LTTNG_VIEWER_ATTACH_SEEK_ERR:
482 fprintf(stderr, "[error] Wrong seek parameter\n");
8ace20bf 483 goto error;
4a744367
JD
484 default:
485 fprintf(stderr, "[error] Unknown attach return code %u\n",
486 be32toh(rp.status));
8ace20bf 487 goto error;
4a744367
JD
488 }
489 if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) {
8ace20bf 490 goto error;
4a744367
JD
491 }
492
2acdc547 493 ctx->session->stream_count += be32toh(rp.streams_count);
4a744367
JD
494 /*
495 * When the session is created but not started, we do an active wait
496 * until it starts. It allows the viewer to start processing the trace
497 * as soon as the session starts.
498 */
499 if (ctx->session->stream_count == 0) {
500 ret = 0;
501 goto end;
502 }
503 printf_verbose("Waiting for %" PRIu64 " streams:\n",
504 ctx->session->stream_count);
505 ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
506 ctx->session->stream_count);
4a744367 507 for (i = 0; i < be32toh(rp.streams_count); i++) {
b7e8a8e1 508 ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
f8612f31
MD
509 if (ret_len == 0) {
510 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
511 goto error;
512 }
4a744367 513 if (ret_len < 0) {
79620497 514 perror("[error] Error receiving stream");
4a744367
JD
515 goto error;
516 }
517 assert(ret_len == sizeof(stream));
9ed31e53
MD
518 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
519 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
4a744367
JD
520
521 printf_verbose(" stream %" PRIu64 " : %s/%s\n",
522 be64toh(stream.id), stream.path_name,
523 stream.channel_name);
524 ctx->session->streams[i].id = be64toh(stream.id);
525 ctx->session->streams[i].session = ctx->session;
526
4a744367 527 ctx->session->streams[i].mmap_size = 0;
fb6b45aa 528 ctx->session->streams[i].ctf_stream_id = -1ULL;
4a744367
JD
529
530 if (be32toh(stream.metadata_flag)) {
4a744367 531 ctx->session->streams[i].metadata_flag = 1;
4a744367
JD
532 }
533 ret = lttng_live_ctf_trace_assign(&ctx->session->streams[i],
534 be64toh(stream.ctf_trace_id));
535 if (ret < 0) {
536 goto error;
537 }
538
539 }
540 ret = 0;
4a744367 541end:
4a744367 542 return ret;
8ace20bf
MD
543
544error:
545 return -1;
4a744367
JD
546}
547
73b2b0d1
JD
548/*
549 * Ask the relay for new streams.
550 *
551 * Returns the number of new streams received or a negative value on error.
552 */
2acdc547
JD
553static
554int ask_new_streams(struct lttng_live_ctx *ctx)
555{
73b2b0d1 556 int i, ret = 0, nb_streams = 0;
2acdc547
JD
557 uint64_t id;
558
559restart:
560 for (i = 0; i < ctx->session_ids->len; i++) {
561 id = g_array_index(ctx->session_ids, uint64_t, i);
562 ret = lttng_live_get_new_streams(ctx, id);
73b2b0d1 563 printf_verbose("Asking for new streams returns %d\n", ret);
2acdc547 564 if (ret < 0) {
e29395a8
JG
565 if (lttng_live_should_quit()) {
566 goto end;
567 }
2acdc547
JD
568 if (ret == -LTTNG_VIEWER_NEW_STREAMS_HUP) {
569 printf_verbose("Session %" PRIu64 " closed\n",
570 id);
b5a1fa45
JD
571 /*
572 * The streams have already been closed during
573 * the reading, so we only need to get rid of
574 * the trace in our internal table of sessions.
575 */
2acdc547
JD
576 g_array_remove_index(ctx->session_ids, i);
577 /*
578 * We can't continue iterating on the g_array
579 * after a remove, we have to start again.
580 */
581 goto restart;
582 } else {
583 ret = -1;
584 goto end;
585 }
73b2b0d1
JD
586 } else {
587 nb_streams += ret;
2acdc547
JD
588 }
589 }
73b2b0d1 590 ret = nb_streams;
2acdc547
JD
591
592end:
593 return ret;
594}
595
50d207af
JD
596static
597int append_metadata(struct lttng_live_ctx *ctx,
598 struct lttng_live_viewer_stream *viewer_stream)
599{
600 int ret;
601 struct lttng_live_viewer_stream *metadata;
602 char *metadata_buf = NULL;
603
604 printf_verbose("get_next_index: new metadata needed\n");
605 ret = get_new_metadata(ctx, viewer_stream, &metadata_buf);
606 if (ret < 0) {
607 free(metadata_buf);
608 goto error;
609 }
610
611 metadata = viewer_stream->ctf_trace->metadata_stream;
612 metadata->ctf_trace->metadata_fp =
613 babeltrace_fmemopen(metadata_buf,
614 metadata->metadata_len, "rb");
615 if (!metadata->ctf_trace->metadata_fp) {
616 perror("Metadata fmemopen");
617 free(metadata_buf);
618 ret = -1;
619 goto error;
620 }
621 ret = ctf_append_trace_metadata(
622 viewer_stream->ctf_trace->handle->td,
623 metadata->ctf_trace->metadata_fp);
251fc08c
MD
624 /* We accept empty metadata packets */
625 if (ret != 0 && ret != -ENOENT) {
50d207af
JD
626 fprintf(stderr, "[error] Appending metadata\n");
627 goto error;
628 }
251fc08c 629 ret = 0;
50d207af
JD
630
631error:
632 return ret;
633}
634
4a744367
JD
635static
636int get_data_packet(struct lttng_live_ctx *ctx,
637 struct ctf_stream_pos *pos,
638 struct lttng_live_viewer_stream *stream, uint64_t offset,
639 uint64_t len)
640{
641 struct lttng_viewer_cmd cmd;
642 struct lttng_viewer_get_packet rq;
643 struct lttng_viewer_trace_packet rp;
644 ssize_t ret_len;
645 int ret;
646
50d207af 647retry:
8ace20bf
MD
648 if (lttng_live_should_quit()) {
649 ret = -1;
650 goto end;
651 }
4a744367
JD
652 cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
653 cmd.data_size = sizeof(rq);
654 cmd.cmd_version = 0;
655
656 memset(&rq, 0, sizeof(rq));
657 rq.stream_id = htobe64(stream->id);
c1e8ac81 658 rq.offset = htobe64(offset);
4a744367
JD
659 rq.len = htobe32(len);
660
b7e8a8e1 661 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 662 if (ret_len < 0) {
79620497 663 perror("[error] Error sending cmd");
4a744367
JD
664 goto error;
665 }
666 assert(ret_len == sizeof(cmd));
667
b7e8a8e1 668 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
4a744367 669 if (ret_len < 0) {
79620497 670 perror("[error] Error sending get_data_packet request");
4a744367
JD
671 goto error;
672 }
673 assert(ret_len == sizeof(rq));
674
b7e8a8e1 675 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
f8612f31
MD
676 if (ret_len == 0) {
677 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
678 goto error;
679 }
4a744367 680 if (ret_len < 0) {
79620497 681 perror("[error] Error receiving data response");
4a744367
JD
682 goto error;
683 }
57b9d843 684 if (ret_len != sizeof(rp)) {
7e5e4176
MD
685 fprintf(stderr, "[error] get_data_packet: expected %zu"
686 ", received %zd\n", sizeof(rp),
a5e4b11e 687 ret_len);
57b9d843
JD
688 goto error;
689 }
4a744367
JD
690
691 rp.flags = be32toh(rp.flags);
692
693 switch (be32toh(rp.status)) {
694 case LTTNG_VIEWER_GET_PACKET_OK:
695 len = be32toh(rp.len);
696 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
697 "\n", len);
698 break;
699 case LTTNG_VIEWER_GET_PACKET_RETRY:
8ace20bf 700 /* Unimplemented by relay daemon */
4a744367 701 printf_verbose("get_data_packet: retry\n");
8ace20bf 702 goto error;
4a744367
JD
703 case LTTNG_VIEWER_GET_PACKET_ERR:
704 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
705 printf_verbose("get_data_packet: new metadata needed\n");
50d207af
JD
706 ret = append_metadata(ctx, stream);
707 if (ret)
708 goto error;
4a744367 709 }
2acdc547 710 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
f312b3aa 711 printf_verbose("get_data_packet: new streams needed\n");
2acdc547 712 ret = ask_new_streams(ctx);
7840bb08 713 if (ret < 0) {
2acdc547 714 goto error;
7840bb08
JD
715 } else if (ret > 0) {
716 ret = add_traces(ctx);
717 if (ret < 0) {
718 goto error;
719 }
720 }
2acdc547 721 }
f312b3aa
MD
722 if (rp.flags & (LTTNG_VIEWER_FLAG_NEW_METADATA
723 | LTTNG_VIEWER_FLAG_NEW_STREAM)) {
724 goto retry;
725 }
4a744367 726 fprintf(stderr, "[error] get_data_packet: error\n");
8ace20bf 727 goto error;
4a744367
JD
728 case LTTNG_VIEWER_GET_PACKET_EOF:
729 ret = -2;
8ace20bf 730 goto end;
4a744367
JD
731 default:
732 printf_verbose("get_data_packet: unknown\n");
8ace20bf 733 goto error;
4a744367
JD
734 }
735
65ae9a82 736 if (len == 0) {
96943040 737 goto error;
4a744367
JD
738 }
739
740 if (len > stream->mmap_size) {
741 uint64_t new_size;
742
743 new_size = max_t(uint64_t, len, stream->mmap_size << 1);
744 if (pos->base_mma) {
745 /* unmap old base */
746 ret = munmap_align(pos->base_mma);
747 if (ret) {
79620497 748 perror("[error] Unable to unmap old base");
4a744367
JD
749 goto error;
750 }
751 pos->base_mma = NULL;
752 }
753 pos->base_mma = mmap_align(new_size,
754 PROT_READ | PROT_WRITE,
755 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
756 if (pos->base_mma == MAP_FAILED) {
79620497 757 perror("[error] mmap error");
4a744367 758 pos->base_mma = NULL;
4a744367
JD
759 goto error;
760 }
761
762 stream->mmap_size = new_size;
763 printf_verbose("Expanding stream mmap size to %" PRIu64 " bytes\n",
764 stream->mmap_size);
765 }
766
b7e8a8e1
MD
767 ret_len = lttng_live_recv(ctx->control_sock,
768 mmap_align_addr(pos->base_mma), len);
f8612f31
MD
769 if (ret_len == 0) {
770 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
771 goto error;
772 }
4a744367 773 if (ret_len < 0) {
79620497 774 perror("[error] Error receiving trace packet");
4a744367
JD
775 goto error;
776 }
777 assert(ret_len == len);
f312b3aa 778 ret = 0;
4a744367 779end:
4a744367 780 return ret;
8ace20bf
MD
781
782error:
783 return -1;
4a744367
JD
784}
785
4a744367 786static
9abca178
JD
787int get_one_metadata_packet(struct lttng_live_ctx *ctx,
788 struct lttng_live_viewer_stream *metadata_stream)
4a744367
JD
789{
790 uint64_t len = 0;
791 int ret;
792 struct lttng_viewer_cmd cmd;
793 struct lttng_viewer_get_metadata rq;
794 struct lttng_viewer_metadata_packet rp;
4a744367
JD
795 char *data = NULL;
796 ssize_t ret_len;
797
8ace20bf
MD
798 if (lttng_live_should_quit()) {
799 ret = -1;
800 goto end;
801 }
802
9abca178 803 rq.stream_id = htobe64(metadata_stream->id);
4a744367
JD
804 cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
805 cmd.data_size = sizeof(rq);
806 cmd.cmd_version = 0;
807
b7e8a8e1 808 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 809 if (ret_len < 0) {
79620497 810 perror("[error] Error sending cmd");
4a744367
JD
811 goto error;
812 }
813 assert(ret_len == sizeof(cmd));
814
b7e8a8e1 815 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
4a744367 816 if (ret_len < 0) {
79620497 817 perror("[error] Error sending get_metadata request");
4a744367
JD
818 goto error;
819 }
820 assert(ret_len == sizeof(rq));
821
b7e8a8e1 822 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
f8612f31
MD
823 if (ret_len == 0) {
824 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
825 goto error;
826 }
4a744367 827 if (ret_len < 0) {
79620497 828 perror("[error] Error receiving metadata response");
4a744367
JD
829 goto error;
830 }
831 assert(ret_len == sizeof(rp));
832
833 switch (be32toh(rp.status)) {
834 case LTTNG_VIEWER_METADATA_OK:
835 printf_verbose("get_metadata : OK\n");
836 break;
837 case LTTNG_VIEWER_NO_NEW_METADATA:
838 printf_verbose("get_metadata : NO NEW\n");
9abca178 839 ret = 0;
4a744367
JD
840 goto end;
841 case LTTNG_VIEWER_METADATA_ERR:
842 printf_verbose("get_metadata : ERR\n");
8ace20bf 843 goto error;
4a744367
JD
844 default:
845 printf_verbose("get_metadata : UNKNOWN\n");
8ace20bf 846 goto error;
4a744367
JD
847 }
848
849 len = be64toh(rp.len);
850 printf_verbose("Writing %" PRIu64" bytes to metadata\n", len);
851 if (len <= 0) {
8ace20bf 852 goto error;
4a744367
JD
853 }
854
855 data = zmalloc(len);
856 if (!data) {
857 perror("relay data zmalloc");
4a744367
JD
858 goto error;
859 }
b7e8a8e1 860 ret_len = lttng_live_recv(ctx->control_sock, data, len);
f8612f31
MD
861 if (ret_len == 0) {
862 fprintf(stderr, "[error] Remote side has closed connection\n");
8ace20bf 863 goto error_free_data;
f8612f31 864 }
4a744367 865 if (ret_len < 0) {
79620497 866 perror("[error] Error receiving trace packet");
8ace20bf 867 goto error_free_data;
4a744367
JD
868 }
869 assert(ret_len == len);
870
871 do {
af701dc4
JD
872 ret_len = fwrite(data, 1, len,
873 metadata_stream->metadata_fp_write);
4a744367
JD
874 } while (ret_len < 0 && errno == EINTR);
875 if (ret_len < 0) {
af701dc4 876 fprintf(stderr, "[error] Writing in the metadata fp\n");
8ace20bf 877 goto error_free_data;
4a744367
JD
878 }
879 assert(ret_len == len);
af701dc4 880 metadata_stream->metadata_len += len;
8ace20bf 881 free(data);
9abca178 882 ret = len;
8ace20bf
MD
883end:
884 return ret;
4a744367 885
8ace20bf 886error_free_data:
4a744367 887 free(data);
4a744367 888error:
8ace20bf 889 return -1;
4a744367
JD
890}
891
9abca178
JD
892/*
893 * Return 0 on success, a negative value on error.
894 */
895static
896int get_new_metadata(struct lttng_live_ctx *ctx,
af701dc4
JD
897 struct lttng_live_viewer_stream *viewer_stream,
898 char **metadata_buf)
9abca178
JD
899{
900 int ret = 0;
901 struct lttng_live_viewer_stream *metadata_stream;
73b2b0d1 902 size_t size, len_read = 0;
9abca178
JD
903
904 metadata_stream = viewer_stream->ctf_trace->metadata_stream;
50d207af
JD
905 if (!metadata_stream) {
906 fprintf(stderr, "[error] No metadata stream\n");
907 ret = -1;
908 goto error;
909 }
af701dc4
JD
910 metadata_stream->metadata_len = 0;
911 ret = open_metadata_fp_write(metadata_stream, metadata_buf, &size);
912 if (ret < 0) {
913 goto error;
914 }
9abca178
JD
915
916 do {
d1694e41
JD
917 if (lttng_live_should_quit()) {
918 ret = -1;
919 goto error;
920 }
9abca178
JD
921 /*
922 * get_one_metadata_packet returns the number of bytes
923 * received, 0 when we have received everything, a
924 * negative value on error.
925 */
926 ret = get_one_metadata_packet(ctx, metadata_stream);
c111d5d7
MD
927 if (ret > 0) {
928 len_read += ret;
929 }
85e2085e
MD
930 if (!len_read) {
931 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
932 }
c111d5d7 933 } while (ret > 0 || !len_read);
9abca178 934
91bb0024
MD
935 if (babeltrace_close_memstream(metadata_buf, &size,
936 metadata_stream->metadata_fp_write)) {
937 perror("babeltrace_close_memstream");
938 }
af701dc4
JD
939 metadata_stream->metadata_fp_write = NULL;
940
941error:
9abca178
JD
942 return ret;
943}
944
fb6b45aa
JD
945/*
946 * Assign the fields from a lttng_viewer_index to a packet_index.
947 */
948static
949void lttng_index_to_packet_index(struct lttng_viewer_index *lindex,
950 struct packet_index *pindex)
951{
952 assert(lindex);
953 assert(pindex);
954
955 pindex->offset = be64toh(lindex->offset);
956 pindex->packet_size = be64toh(lindex->packet_size);
957 pindex->content_size = be64toh(lindex->content_size);
958 pindex->ts_cycles.timestamp_begin = be64toh(lindex->timestamp_begin);
959 pindex->ts_cycles.timestamp_end = be64toh(lindex->timestamp_end);
960 pindex->events_discarded = be64toh(lindex->events_discarded);
961}
962
4a744367
JD
963/*
964 * Get one index for a stream.
965 *
966 * Returns 0 on success or a negative value on error.
967 */
968static
969int get_next_index(struct lttng_live_ctx *ctx,
970 struct lttng_live_viewer_stream *viewer_stream,
fb6b45aa 971 struct packet_index *index, uint64_t *stream_id)
4a744367
JD
972{
973 struct lttng_viewer_cmd cmd;
974 struct lttng_viewer_get_next_index rq;
4a744367 975 int ret;
4a744367 976 ssize_t ret_len;
fb6b45aa 977 struct lttng_viewer_index *rp = &viewer_stream->current_index;
4a744367
JD
978
979 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
980 cmd.data_size = sizeof(rq);
981 cmd.cmd_version = 0;
982
983 memset(&rq, 0, sizeof(rq));
984 rq.stream_id = htobe64(viewer_stream->id);
985
986retry:
8ace20bf
MD
987 if (lttng_live_should_quit()) {
988 ret = -1;
989 goto end;
990 }
b7e8a8e1 991 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 992 if (ret_len < 0) {
79620497 993 perror("[error] Error sending cmd");
4a744367
JD
994 goto error;
995 }
996 assert(ret_len == sizeof(cmd));
997
b7e8a8e1 998 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
4a744367 999 if (ret_len < 0) {
79620497 1000 perror("[error] Error sending get_next_index request");
4a744367
JD
1001 goto error;
1002 }
1003 assert(ret_len == sizeof(rq));
1004
fb6b45aa 1005 ret_len = lttng_live_recv(ctx->control_sock, rp, sizeof(*rp));
f8612f31
MD
1006 if (ret_len == 0) {
1007 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
1008 goto error;
1009 }
4a744367 1010 if (ret_len < 0) {
79620497 1011 perror("[error] Error receiving index response");
4a744367
JD
1012 goto error;
1013 }
fb6b45aa 1014 assert(ret_len == sizeof(*rp));
4a744367 1015
fb6b45aa 1016 rp->flags = be32toh(rp->flags);
4a744367 1017
fb6b45aa 1018 switch (be32toh(rp->status)) {
4a744367
JD
1019 case LTTNG_VIEWER_INDEX_INACTIVE:
1020 printf_verbose("get_next_index: inactive\n");
1021 memset(index, 0, sizeof(struct packet_index));
fb6b45aa
JD
1022 index->ts_cycles.timestamp_end = be64toh(rp->timestamp_end);
1023 *stream_id = be64toh(rp->stream_id);
4a744367
JD
1024 break;
1025 case LTTNG_VIEWER_INDEX_OK:
1026 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
fb6b45aa
JD
1027 rp->flags & LTTNG_VIEWER_FLAG_NEW_METADATA);
1028 lttng_index_to_packet_index(rp, index);
1029 *stream_id = be64toh(rp->stream_id);
1030 viewer_stream->data_pending = 1;
4a744367 1031
fb6b45aa 1032 if (rp->flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
50d207af
JD
1033 ret = append_metadata(ctx, viewer_stream);
1034 if (ret)
4a744367 1035 goto error;
4a744367 1036 }
fb6b45aa 1037 if (rp->flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
73b2b0d1 1038 printf_verbose("get_next_index: need new streams\n");
2acdc547 1039 ret = ask_new_streams(ctx);
7840bb08 1040 if (ret < 0) {
2acdc547 1041 goto error;
7840bb08
JD
1042 } else if (ret > 0) {
1043 ret = add_traces(ctx);
1044 if (ret < 0) {
1045 goto error;
1046 }
1047 }
2acdc547 1048 }
4a744367
JD
1049 break;
1050 case LTTNG_VIEWER_INDEX_RETRY:
1051 printf_verbose("get_next_index: retry\n");
85e2085e 1052 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
4a744367
JD
1053 goto retry;
1054 case LTTNG_VIEWER_INDEX_HUP:
1055 printf_verbose("get_next_index: stream hung up\n");
1056 viewer_stream->id = -1ULL;
4a744367 1057 index->offset = EOF;
2acdc547 1058 ctx->session->stream_count--;
4a744367
JD
1059 break;
1060 case LTTNG_VIEWER_INDEX_ERR:
1061 fprintf(stderr, "[error] get_next_index: error\n");
4a744367
JD
1062 goto error;
1063 default:
1064 fprintf(stderr, "[error] get_next_index: unkwown value\n");
4a744367
JD
1065 goto error;
1066 }
4a744367 1067 ret = 0;
8ace20bf
MD
1068end:
1069 return ret;
4a744367
JD
1070
1071error:
8ace20bf 1072 return -1;
4a744367
JD
1073}
1074
fb6b45aa
JD
1075static
1076void read_packet_header(struct ctf_stream_pos *pos,
1077 struct ctf_file_stream *file_stream)
1078{
1079 int ret;
1080
1081 /* update trace_packet_header and stream_packet_context */
fcf10417
JG
1082 if (!(pos->prot & PROT_WRITE) &&
1083 file_stream->parent.trace_packet_header) {
fb6b45aa
JD
1084 /* Read packet header */
1085 ret = generic_rw(&pos->parent,
1086 &file_stream->parent.trace_packet_header->p);
1087 if (ret) {
1088 pos->offset = EOF;
1089 fprintf(stderr, "[error] trace packet "
1090 "header read failed\n");
1091 goto end;
1092 }
1093 }
fcf10417
JG
1094 if (!(pos->prot & PROT_WRITE) &&
1095 file_stream->parent.stream_packet_context) {
fb6b45aa
JD
1096 /* Read packet context */
1097 ret = generic_rw(&pos->parent,
1098 &file_stream->parent.stream_packet_context->p);
1099 if (ret) {
1100 pos->offset = EOF;
1101 fprintf(stderr, "[error] stream packet "
1102 "context read failed\n");
1103 goto end;
1104 }
1105 }
1106 pos->data_offset = pos->offset;
1107
1108end:
1109 return;
1110}
1111
1112/*
1113 * Handle the seek parameters.
1114 * Returns 0 if the packet_seek can continue, a positive value to
1115 * cleanly exit the packet_seek, a negative value on error.
1116 */
1117static
1118int handle_seek_position(size_t index, int whence,
1119 struct lttng_live_viewer_stream *viewer_stream,
1120 struct ctf_stream_pos *pos,
1121 struct ctf_file_stream *file_stream)
1122{
6a724a1e 1123 int ret = 0;
fb6b45aa
JD
1124
1125 switch (whence) {
1126 case SEEK_CUR:
1127 ret = 0;
1128 goto end;
1129 case SEEK_SET:
1130 /*
1131 * We only allow to seek to 0.
1132 */
1133 if (index != 0) {
1134 fprintf(stderr, "[error] Arbitrary seek in lttng-live "
1135 "trace not supported\n");
1136 pos->offset = EOF;
1137 ret = -1;
1138 goto end;
1139 }
1140
1141 ret = 0;
1142 goto end;
1143
1144 default:
1145 fprintf(stderr, "[error] Invalid seek parameter\n");
1146 assert(0);
1147 }
1148
1149end:
1150 return ret;
1151}
1152
b5a1fa45 1153static
4a744367
JD
1154void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index,
1155 int whence)
1156{
1157 struct ctf_stream_pos *pos;
1158 struct ctf_file_stream *file_stream;
f1f52630 1159 struct packet_index *prev_index = NULL, *cur_index;
4a744367
JD
1160 struct lttng_live_viewer_stream *viewer_stream;
1161 struct lttng_live_session *session;
fb6b45aa 1162 uint64_t stream_id = -1ULL;
4a744367
JD
1163 int ret;
1164
4a744367
JD
1165 pos = ctf_pos(stream_pos);
1166 file_stream = container_of(pos, struct ctf_file_stream, pos);
1167 viewer_stream = (struct lttng_live_viewer_stream *) pos->priv;
1168 session = viewer_stream->session;
1169
fb6b45aa
JD
1170 ret = handle_seek_position(index, whence, viewer_stream, pos,
1171 file_stream);
1172 if (ret != 0) {
1173 return;
1174 }
1175
1176retry:
f1f52630
MD
1177 switch (pos->packet_index->len) {
1178 case 0:
1179 g_array_set_size(pos->packet_index, 1);
1180 cur_index = &g_array_index(pos->packet_index,
1181 struct packet_index, 0);
1182 break;
1183 case 1:
1184 g_array_set_size(pos->packet_index, 2);
1185 prev_index = &g_array_index(pos->packet_index,
1186 struct packet_index, 0);
1187 cur_index = &g_array_index(pos->packet_index,
1188 struct packet_index, 1);
1189 break;
1190 case 2:
1191 g_array_index(pos->packet_index,
1192 struct packet_index, 0) =
1193 g_array_index(pos->packet_index,
1194 struct packet_index, 1);
1195 prev_index = &g_array_index(pos->packet_index,
1196 struct packet_index, 0);
1197 cur_index = &g_array_index(pos->packet_index,
1198 struct packet_index, 1);
1199 break;
1200 default:
1201 abort();
1202 break;
1203 }
fb6b45aa
JD
1204
1205 if (viewer_stream->data_pending) {
1206 lttng_index_to_packet_index(&viewer_stream->current_index, cur_index);
1207 } else {
1208 printf_verbose("get_next_index for stream %" PRIu64 "\n", viewer_stream->id);
1209 ret = get_next_index(session->ctx, viewer_stream, cur_index, &stream_id);
1210 if (ret < 0) {
1211 pos->offset = EOF;
1212 if (!lttng_live_should_quit()) {
1213 fprintf(stderr, "[error] get_next_index failed\n");
1214 }
1215 return;
e29395a8 1216 }
fb6b45aa
JD
1217 printf_verbose("Index received : packet_size : %" PRIu64
1218 ", offset %" PRIu64 ", content_size %" PRIu64
1219 ", timestamp_end : %" PRIu64 "\n",
1220 cur_index->packet_size, cur_index->offset,
1221 cur_index->content_size,
1222 cur_index->ts_cycles.timestamp_end);
1223
1224 }
1225
1226 /*
1227 * On the first time we receive an index, the stream_id needs to
1228 * be set for the stream in order to use it, we don't want any
1229 * data at this stage.
1230 */
1231 if (file_stream->parent.stream_id == -1ULL) {
1232 /*
1233 * Warning: with lttng-tools < 2.4.2, the beacon does not
1234 * contain the real stream ID, it is memset to 0, so this
1235 * might create a problem when a session has multiple
1236 * channels. We can't detect it at this stage, lttng-tools
1237 * has to be upgraded to fix this problem.
1238 */
1239 printf_verbose("Assigning stream_id %" PRIu64 "\n",
1240 stream_id);
1241 file_stream->parent.stream_id = stream_id;
1242 viewer_stream->ctf_stream_id = stream_id;
1243
4a744367
JD
1244 return;
1245 }
1246
f1f52630
MD
1247 pos->packet_size = cur_index->packet_size;
1248 pos->content_size = cur_index->content_size;
4a744367 1249 pos->mmap_base_offset = 0;
f1f52630 1250 if (cur_index->offset == EOF) {
4a744367
JD
1251 pos->offset = EOF;
1252 } else {
1253 pos->offset = 0;
1254 }
1255
f1f52630 1256 if (cur_index->content_size == 0) {
fb6b45aa
JD
1257 if (file_stream->parent.stream_class) {
1258 file_stream->parent.cycles_timestamp =
f1f52630 1259 cur_index->ts_cycles.timestamp_end;
fb6b45aa
JD
1260 file_stream->parent.real_timestamp = ctf_get_real_timestamp(
1261 &file_stream->parent,
1262 cur_index->ts_cycles.timestamp_end);
1263 }
4a744367 1264 } else {
fb6b45aa
JD
1265 if (file_stream->parent.stream_class) {
1266 /* Convert the timestamps and append to the real_index. */
1267 cur_index->ts_real.timestamp_begin = ctf_get_real_timestamp(
1268 &file_stream->parent,
1269 cur_index->ts_cycles.timestamp_begin);
1270 cur_index->ts_real.timestamp_end = ctf_get_real_timestamp(
1271 &file_stream->parent,
1272 cur_index->ts_cycles.timestamp_end);
1273 }
f1f52630
MD
1274
1275 ctf_update_current_packet_index(&file_stream->parent,
1276 prev_index, cur_index);
1277
1278 file_stream->parent.cycles_timestamp =
1279 cur_index->ts_cycles.timestamp_begin;
1280 file_stream->parent.real_timestamp =
1281 cur_index->ts_real.timestamp_begin;
4a744367
JD
1282 }
1283
1284 if (pos->packet_size == 0 || pos->offset == EOF) {
1285 goto end;
1286 }
1287
1288 printf_verbose("get_data_packet for stream %" PRIu64 "\n",
1289 viewer_stream->id);
1290 ret = get_data_packet(session->ctx, pos, viewer_stream,
c1e8ac81 1291 cur_index->offset,
f1f52630 1292 cur_index->packet_size / CHAR_BIT);
4a744367
JD
1293 if (ret == -2) {
1294 goto retry;
1295 } else if (ret < 0) {
1296 pos->offset = EOF;
e29395a8
JG
1297 if (!lttng_live_should_quit()) {
1298 fprintf(stderr, "[error] get_data_packet failed\n");
1299 }
4a744367
JD
1300 return;
1301 }
fb6b45aa 1302 viewer_stream->data_pending = 0;
4a744367 1303
fb6b45aa 1304 read_packet_header(pos, file_stream);
4a744367
JD
1305
1306end:
1307 return;
1308}
1309
2acdc547
JD
1310int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx)
1311{
1312 struct lttng_viewer_cmd cmd;
1313 struct lttng_viewer_create_session_response resp;
1314 int ret;
1315 ssize_t ret_len;
1316
8ace20bf
MD
1317 if (lttng_live_should_quit()) {
1318 ret = -1;
1319 goto end;
1320 }
1321
2acdc547
JD
1322 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
1323 cmd.data_size = 0;
1324 cmd.cmd_version = 0;
1325
b7e8a8e1 1326 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
2acdc547 1327 if (ret_len < 0) {
79620497 1328 perror("[error] Error sending cmd");
2acdc547
JD
1329 goto error;
1330 }
1331 assert(ret_len == sizeof(cmd));
1332
b7e8a8e1 1333 ret_len = lttng_live_recv(ctx->control_sock, &resp, sizeof(resp));
f8612f31
MD
1334 if (ret_len == 0) {
1335 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
1336 goto error;
1337 }
2acdc547 1338 if (ret_len < 0) {
79620497 1339 perror("[error] Error receiving create session reply");
2acdc547
JD
1340 goto error;
1341 }
1342 assert(ret_len == sizeof(resp));
1343
1344 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
1345 fprintf(stderr, "[error] Error creating viewer session\n");
2acdc547
JD
1346 goto error;
1347 }
1348 ret = 0;
8ace20bf
MD
1349end:
1350 return ret;
2acdc547
JD
1351
1352error:
8ace20bf 1353 return -1;
2acdc547
JD
1354}
1355
1356static
1357int del_traces(gpointer key, gpointer value, gpointer user_data)
4a744367
JD
1358{
1359 struct bt_context *bt_ctx = user_data;
1360 struct lttng_live_ctf_trace *trace = value;
1361 int ret;
1362
1363 ret = bt_context_remove_trace(bt_ctx, trace->trace_id);
1364 if (ret < 0)
1365 fprintf(stderr, "[error] removing trace from context\n");
1366
1367 /* remove the key/value pair from the HT. */
1368 return 1;
1369}
1370
2acdc547 1371static
7840bb08
JD
1372int add_one_trace(struct lttng_live_ctx *ctx,
1373 struct lttng_live_ctf_trace *trace)
4a744367 1374{
9abca178 1375 int i, ret;
7840bb08 1376 struct bt_context *bt_ctx = ctx->bt_ctx;
4a744367
JD
1377 struct lttng_live_viewer_stream *stream;
1378 struct bt_mmap_stream *new_mmap_stream;
1379 struct bt_mmap_stream_list mmap_list;
150595c4
JD
1380 struct bt_trace_descriptor *td;
1381 struct bt_trace_handle *handle;
4a744367 1382
b5a1fa45
JD
1383 /*
1384 * We don't know how many streams we will receive for a trace, so
1385 * once we are done receiving the traces, we add all the traces
1386 * received to the bt_context.
1387 * We can receive streams during the attach command or the
1388 * get_new_streams, so we have to make sure not to add multiple
1389 * times the same traces.
1390 * If a trace is already in the context, we just skip this function.
1391 */
7840bb08
JD
1392 if (trace->in_use) {
1393 ret = 0;
1394 goto end;
1395 }
2acdc547 1396
4a744367
JD
1397 BT_INIT_LIST_HEAD(&mmap_list.head);
1398
1399 for (i = 0; i < trace->streams->len; i++) {
1400 stream = g_ptr_array_index(trace->streams, i);
4a744367
JD
1401
1402 if (!stream->metadata_flag) {
1403 new_mmap_stream = zmalloc(sizeof(struct bt_mmap_stream));
1404 new_mmap_stream->priv = (void *) stream;
1405 new_mmap_stream->fd = -1;
1406 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
1407 } else {
af701dc4
JD
1408 char *metadata_buf = NULL;
1409
4a744367 1410 /* Get all possible metadata before starting */
af701dc4
JD
1411 ret = get_new_metadata(ctx, stream, &metadata_buf);
1412 if (ret) {
1413 free(metadata_buf);
1414 goto end_free;
1415 }
1416 if (!stream->metadata_len) {
1417 fprintf(stderr, "[error] empty metadata\n");
1418 ret = -1;
1419 free(metadata_buf);
1420 goto end_free;
1421 }
1422
1423 trace->metadata_fp = babeltrace_fmemopen(metadata_buf,
1424 stream->metadata_len, "rb");
1425 if (!trace->metadata_fp) {
1426 perror("Metadata fmemopen");
1427 ret = -1;
1428 free(metadata_buf);
9abca178 1429 goto end_free;
af701dc4 1430 }
4a744367
JD
1431 }
1432 }
1433
1434 if (!trace->metadata_fp) {
1435 fprintf(stderr, "[error] No metadata stream opened\n");
7840bb08 1436 ret = -1;
4a744367
JD
1437 goto end_free;
1438 }
1439
1440 ret = bt_context_add_trace(bt_ctx, NULL, "ctf",
1441 ctf_live_packet_seek, &mmap_list, trace->metadata_fp);
1442 if (ret < 0) {
1443 fprintf(stderr, "[error] Error adding trace\n");
7840bb08 1444 ret = -1;
4a744367
JD
1445 goto end_free;
1446 }
af701dc4
JD
1447 trace->metadata_stream->metadata_len = 0;
1448
150595c4
JD
1449 handle = (struct bt_trace_handle *) g_hash_table_lookup(
1450 bt_ctx->trace_handles,
1451 (gpointer) (unsigned long) ret);
1452 td = handle->td;
1453 trace->handle = handle;
2acdc547 1454 if (bt_ctx->current_iterator) {
2acdc547
JD
1455 bt_iter_add_trace(bt_ctx->current_iterator, td);
1456 }
1457
4a744367 1458 trace->trace_id = ret;
2acdc547 1459 trace->in_use = 1;
4a744367
JD
1460
1461 goto end;
1462
1463end_free:
1464 bt_context_put(bt_ctx);
1465end:
7840bb08
JD
1466 return ret;
1467}
1468
1469static
1470int add_traces(struct lttng_live_ctx *ctx)
1471{
1472 int ret;
1473 struct lttng_live_ctf_trace *trace;
1474 GHashTableIter it;
1475 gpointer key;
1476 gpointer value;
1477
1478 g_hash_table_iter_init(&it, ctx->session->ctf_traces);
1479 while (g_hash_table_iter_next(&it, &key, &value)) {
1480 trace = (struct lttng_live_ctf_trace *) value;
1481 ret = add_one_trace(ctx, trace);
1482 if (ret < 0) {
1483 goto end;
1484 }
1485 }
1486
1487 ret = 0;
1488
1489end:
1490 return ret;
4a744367
JD
1491}
1492
73b2b0d1
JD
1493/*
1494 * Request new streams for a session.
1495 * Returns the number of streams received or a negative value on error.
1496 */
2acdc547 1497int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
6192b988
JD
1498{
1499 struct lttng_viewer_cmd cmd;
2acdc547
JD
1500 struct lttng_viewer_new_streams_request rq;
1501 struct lttng_viewer_new_streams_response rp;
1502 struct lttng_viewer_stream stream;
73b2b0d1 1503 int ret, i, nb_streams = 0;
6192b988 1504 ssize_t ret_len;
b5a1fa45 1505 uint32_t stream_count;
6192b988 1506
8ace20bf
MD
1507 if (lttng_live_should_quit()) {
1508 ret = -1;
1509 goto end;
1510 }
1511
2acdc547
JD
1512 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS);
1513 cmd.data_size = sizeof(rq);
6192b988
JD
1514 cmd.cmd_version = 0;
1515
2acdc547
JD
1516 memset(&rq, 0, sizeof(rq));
1517 rq.session_id = htobe64(id);
1518
b7e8a8e1 1519 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
6192b988 1520 if (ret_len < 0) {
79620497 1521 perror("[error] Error sending cmd");
6192b988
JD
1522 goto error;
1523 }
1524 assert(ret_len == sizeof(cmd));
1525
b7e8a8e1 1526 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
6192b988 1527 if (ret_len < 0) {
79620497 1528 perror("[error] Error sending get_new_streams request");
6192b988
JD
1529 goto error;
1530 }
2acdc547 1531 assert(ret_len == sizeof(rq));
6192b988 1532
b7e8a8e1 1533 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
f8612f31
MD
1534 if (ret_len == 0) {
1535 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
1536 goto error;
1537 }
2acdc547 1538 if (ret_len < 0) {
79620497 1539 perror("[error] Error receiving get_new_streams response");
6192b988
JD
1540 goto error;
1541 }
2acdc547
JD
1542 assert(ret_len == sizeof(rp));
1543
1544 switch(be32toh(rp.status)) {
1545 case LTTNG_VIEWER_NEW_STREAMS_OK:
1546 break;
1547 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW:
1548 ret = 0;
1549 goto end;
1550 case LTTNG_VIEWER_NEW_STREAMS_HUP:
1551 ret = -LTTNG_VIEWER_NEW_STREAMS_HUP;
1552 goto end;
1553 case LTTNG_VIEWER_NEW_STREAMS_ERR:
1554 fprintf(stderr, "[error] get_new_streams error\n");
8ace20bf 1555 goto error;
2acdc547
JD
1556 default:
1557 fprintf(stderr, "[error] Unknown return code %u\n",
1558 be32toh(rp.status));
8ace20bf 1559 goto error;
2acdc547
JD
1560 }
1561
b5a1fa45
JD
1562 stream_count = be32toh(rp.streams_count);
1563 ctx->session->stream_count += stream_count;
2acdc547
JD
1564 /*
1565 * When the session is created but not started, we do an active wait
1566 * until it starts. It allows the viewer to start processing the trace
1567 * as soon as the session starts.
1568 */
1569 if (ctx->session->stream_count == 0) {
1570 ret = 0;
1571 goto end;
1572 }
1573 printf_verbose("Waiting for %" PRIu64 " streams:\n",
1574 ctx->session->stream_count);
1575 ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
1576 ctx->session->stream_count);
b5a1fa45 1577 for (i = 0; i < stream_count; i++) {
b7e8a8e1 1578 ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
f8612f31
MD
1579 if (ret_len == 0) {
1580 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
1581 goto error;
1582 }
2acdc547 1583 if (ret_len < 0) {
79620497 1584 perror("[error] Error receiving stream");
2acdc547
JD
1585 goto error;
1586 }
1587 assert(ret_len == sizeof(stream));
1588 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
1589 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
1590
1591 printf_verbose(" stream %" PRIu64 " : %s/%s\n",
1592 be64toh(stream.id), stream.path_name,
1593 stream.channel_name);
1594 ctx->session->streams[i].id = be64toh(stream.id);
1595 ctx->session->streams[i].session = ctx->session;
1596
2acdc547 1597 ctx->session->streams[i].mmap_size = 0;
fb6b45aa 1598 ctx->session->streams[i].ctf_stream_id = -1ULL;
2acdc547
JD
1599
1600 if (be32toh(stream.metadata_flag)) {
2acdc547 1601 ctx->session->streams[i].metadata_flag = 1;
2acdc547
JD
1602 }
1603 ret = lttng_live_ctf_trace_assign(&ctx->session->streams[i],
1604 be64toh(stream.ctf_trace_id));
1605 if (ret < 0) {
1606 goto error;
1607 }
73b2b0d1 1608 nb_streams++;
2acdc547
JD
1609
1610 }
73b2b0d1 1611 ret = nb_streams;
2acdc547 1612end:
6192b988 1613 return ret;
8ace20bf
MD
1614
1615error:
1616 return -1;
6192b988
JD
1617}
1618
28756729 1619int lttng_live_read(struct lttng_live_ctx *ctx)
4a744367 1620{
28756729
JG
1621 int ret = -1;
1622 int i;
4a744367
JD
1623 struct bt_ctf_iter *iter;
1624 const struct bt_ctf_event *event;
1625 struct bt_iter_pos begin_pos;
1626 struct bt_trace_descriptor *td_write;
1627 struct bt_format *fmt_write;
1628 struct ctf_text_stream_pos *sout;
2acdc547 1629 uint64_t id;
4a744367 1630
2acdc547
JD
1631 ctx->bt_ctx = bt_context_create();
1632 if (!ctx->bt_ctx) {
4a744367
JD
1633 fprintf(stderr, "[error] bt_context_create allocation\n");
1634 goto end;
1635 }
1636
1637 fmt_write = bt_lookup_format(g_quark_from_static_string("text"));
1638 if (!fmt_write) {
1639 fprintf(stderr, "[error] ctf-text error\n");
1640 goto end;
1641 }
1642
1643 td_write = fmt_write->open_trace(NULL, O_RDWR, NULL, NULL);
1644 if (!td_write) {
1645 fprintf(stderr, "[error] Error opening output trace\n");
1646 goto end_free;
1647 }
1648
1649 sout = container_of(td_write, struct ctf_text_stream_pos,
1650 trace_descriptor);
85bb4663 1651 if (!sout->parent.event_cb) {
4a744367 1652 goto end_free;
85bb4663 1653 }
4a744367 1654
6192b988
JD
1655 ret = lttng_live_create_viewer_session(ctx);
1656 if (ret < 0) {
1657 goto end_free;
1658 }
1659
2acdc547
JD
1660 for (i = 0; i < ctx->session_ids->len; i++) {
1661 id = g_array_index(ctx->session_ids, uint64_t, i);
7e5e4176 1662 printf_verbose("Attaching to session %" PRIu64 "\n", id);
2acdc547
JD
1663 ret = lttng_live_attach_session(ctx, id);
1664 printf_verbose("Attaching session returns %d\n", ret);
1665 if (ret < 0) {
1666 if (ret == -LTTNG_VIEWER_ATTACH_UNK) {
1667 fprintf(stderr, "[error] Unknown session ID\n");
1668 }
1669 goto end_free;
1670 }
1671 }
1672
4a744367 1673 /*
2acdc547 1674 * As long as the session is active, we try to get new streams.
4a744367 1675 */
2acdc547 1676 for (;;) {
4a744367
JD
1677 int flags;
1678
8ace20bf 1679 if (lttng_live_should_quit()) {
28756729 1680 ret = 0;
8ace20bf
MD
1681 goto end_free;
1682 }
1683
2acdc547 1684 while (!ctx->session->stream_count) {
8ace20bf
MD
1685 if (lttng_live_should_quit()
1686 || ctx->session_ids->len == 0) {
28756729 1687 ret = 0;
4a744367 1688 goto end_free;
85bb4663 1689 }
2acdc547 1690 ret = ask_new_streams(ctx);
85bb4663 1691 if (ret < 0) {
2acdc547 1692 goto end_free;
85bb4663
MD
1693 }
1694 if (!ctx->session->stream_count) {
85e2085e 1695 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
85bb4663 1696 }
2acdc547 1697 }
4a744367 1698
7840bb08
JD
1699 ret = add_traces(ctx);
1700 if (ret < 0) {
1701 goto end_free;
1702 }
4a744367
JD
1703
1704 begin_pos.type = BT_SEEK_BEGIN;
2acdc547 1705 iter = bt_ctf_iter_create(ctx->bt_ctx, &begin_pos, NULL);
4a744367 1706 if (!iter) {
e29395a8
JG
1707 if (lttng_live_should_quit()) {
1708 ret = 0;
1709 goto end;
1710 }
4a744367
JD
1711 fprintf(stderr, "[error] Iterator creation error\n");
1712 goto end;
1713 }
1714 for (;;) {
8ace20bf 1715 if (lttng_live_should_quit()) {
28756729 1716 ret = 0;
8ace20bf
MD
1717 goto end_free;
1718 }
4a744367
JD
1719 event = bt_ctf_iter_read_event_flags(iter, &flags);
1720 if (!(flags & BT_ITER_FLAG_RETRY)) {
1721 if (!event) {
1722 /* End of trace */
1723 break;
1724 }
1725 ret = sout->parent.event_cb(&sout->parent,
1726 event->parent->stream);
1727 if (ret) {
1728 fprintf(stderr, "[error] Writing "
1729 "event failed.\n");
1730 goto end_free;
1731 }
1732 }
1733 ret = bt_iter_next(bt_ctf_get_iter(iter));
1734 if (ret < 0) {
1735 goto end_free;
1736 }
1737 }
1738 bt_ctf_iter_destroy(iter);
2acdc547
JD
1739 g_hash_table_foreach_remove(ctx->session->ctf_traces,
1740 del_traces, ctx->bt_ctx);
1741 ctx->session->stream_count = 0;
1742 }
4a744367
JD
1743
1744end_free:
2acdc547 1745 bt_context_put(ctx->bt_ctx);
4a744367 1746end:
e29395a8
JG
1747 if (lttng_live_should_quit()) {
1748 ret = 0;
1749 }
28756729 1750 return ret;
4a744367 1751}
This page took 0.108218 seconds and 4 git commands to generate.