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