Fix: Add missing lttng_live_should_quit checks
[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 (lttng_live_should_quit()) {
548 goto end;
549 }
550 if (ret == -LTTNG_VIEWER_NEW_STREAMS_HUP) {
551 printf_verbose("Session %" PRIu64 " closed\n",
552 id);
553 /*
554 * The streams have already been closed during
555 * the reading, so we only need to get rid of
556 * the trace in our internal table of sessions.
557 */
558 g_array_remove_index(ctx->session_ids, i);
559 /*
560 * We can't continue iterating on the g_array
561 * after a remove, we have to start again.
562 */
563 goto restart;
564 } else {
565 ret = -1;
566 goto end;
567 }
568 }
569 }
570
571 end:
572 return ret;
573 }
574
575 static
576 int append_metadata(struct lttng_live_ctx *ctx,
577 struct lttng_live_viewer_stream *viewer_stream)
578 {
579 int ret;
580 struct lttng_live_viewer_stream *metadata;
581 char *metadata_buf = NULL;
582
583 printf_verbose("get_next_index: new metadata needed\n");
584 ret = get_new_metadata(ctx, viewer_stream, &metadata_buf);
585 if (ret < 0) {
586 free(metadata_buf);
587 goto error;
588 }
589
590 metadata = viewer_stream->ctf_trace->metadata_stream;
591 metadata->ctf_trace->metadata_fp =
592 babeltrace_fmemopen(metadata_buf,
593 metadata->metadata_len, "rb");
594 if (!metadata->ctf_trace->metadata_fp) {
595 perror("Metadata fmemopen");
596 free(metadata_buf);
597 ret = -1;
598 goto error;
599 }
600 ret = ctf_append_trace_metadata(
601 viewer_stream->ctf_trace->handle->td,
602 metadata->ctf_trace->metadata_fp);
603 /* We accept empty metadata packets */
604 if (ret != 0 && ret != -ENOENT) {
605 fprintf(stderr, "[error] Appending metadata\n");
606 goto error;
607 }
608 ret = 0;
609
610 error:
611 return ret;
612 }
613
614 static
615 int get_data_packet(struct lttng_live_ctx *ctx,
616 struct ctf_stream_pos *pos,
617 struct lttng_live_viewer_stream *stream, uint64_t offset,
618 uint64_t len)
619 {
620 struct lttng_viewer_cmd cmd;
621 struct lttng_viewer_get_packet rq;
622 struct lttng_viewer_trace_packet rp;
623 ssize_t ret_len;
624 int ret;
625
626 retry:
627 if (lttng_live_should_quit()) {
628 ret = -1;
629 goto end;
630 }
631 cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
632 cmd.data_size = sizeof(rq);
633 cmd.cmd_version = 0;
634
635 memset(&rq, 0, sizeof(rq));
636 rq.stream_id = htobe64(stream->id);
637 rq.offset = htobe64(offset);
638 rq.len = htobe32(len);
639
640 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
641 if (ret_len < 0) {
642 perror("[error] Error sending cmd");
643 goto error;
644 }
645 assert(ret_len == sizeof(cmd));
646
647 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
648 if (ret_len < 0) {
649 perror("[error] Error sending get_data_packet request");
650 goto error;
651 }
652 assert(ret_len == sizeof(rq));
653
654 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
655 if (ret_len == 0) {
656 fprintf(stderr, "[error] Remote side has closed connection\n");
657 goto error;
658 }
659 if (ret_len < 0) {
660 perror("[error] Error receiving data response");
661 goto error;
662 }
663 if (ret_len != sizeof(rp)) {
664 fprintf(stderr, "[error] get_data_packet: expected %" PRId64
665 ", received %" PRId64 "\n", sizeof(rp),
666 ret_len);
667 goto error;
668 }
669
670 rp.flags = be32toh(rp.flags);
671
672 switch (be32toh(rp.status)) {
673 case LTTNG_VIEWER_GET_PACKET_OK:
674 len = be32toh(rp.len);
675 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
676 "\n", len);
677 break;
678 case LTTNG_VIEWER_GET_PACKET_RETRY:
679 /* Unimplemented by relay daemon */
680 printf_verbose("get_data_packet: retry\n");
681 goto error;
682 case LTTNG_VIEWER_GET_PACKET_ERR:
683 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
684 printf_verbose("get_data_packet: new metadata needed\n");
685 ret = append_metadata(ctx, stream);
686 if (ret)
687 goto error;
688 }
689 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
690 printf_verbose("get_data_packet: new streams needed\n");
691 ret = ask_new_streams(ctx);
692 if (ret < 0)
693 goto error;
694 g_hash_table_foreach(ctx->session->ctf_traces,
695 add_traces, ctx->bt_ctx);
696 }
697 if (rp.flags & (LTTNG_VIEWER_FLAG_NEW_METADATA
698 | LTTNG_VIEWER_FLAG_NEW_STREAM)) {
699 goto retry;
700 }
701 fprintf(stderr, "[error] get_data_packet: error\n");
702 goto error;
703 case LTTNG_VIEWER_GET_PACKET_EOF:
704 ret = -2;
705 goto end;
706 default:
707 printf_verbose("get_data_packet: unknown\n");
708 goto error;
709 }
710
711 if (len == 0) {
712 goto error;
713 }
714
715 if (len > stream->mmap_size) {
716 uint64_t new_size;
717
718 new_size = max_t(uint64_t, len, stream->mmap_size << 1);
719 if (pos->base_mma) {
720 /* unmap old base */
721 ret = munmap_align(pos->base_mma);
722 if (ret) {
723 perror("[error] Unable to unmap old base");
724 goto error;
725 }
726 pos->base_mma = NULL;
727 }
728 pos->base_mma = mmap_align(new_size,
729 PROT_READ | PROT_WRITE,
730 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
731 if (pos->base_mma == MAP_FAILED) {
732 perror("[error] mmap error");
733 pos->base_mma = NULL;
734 goto error;
735 }
736
737 stream->mmap_size = new_size;
738 printf_verbose("Expanding stream mmap size to %" PRIu64 " bytes\n",
739 stream->mmap_size);
740 }
741
742 ret_len = lttng_live_recv(ctx->control_sock,
743 mmap_align_addr(pos->base_mma), len);
744 if (ret_len == 0) {
745 fprintf(stderr, "[error] Remote side has closed connection\n");
746 goto error;
747 }
748 if (ret_len < 0) {
749 perror("[error] Error receiving trace packet");
750 goto error;
751 }
752 assert(ret_len == len);
753 ret = 0;
754 end:
755 return ret;
756
757 error:
758 return -1;
759 }
760
761 static
762 int get_one_metadata_packet(struct lttng_live_ctx *ctx,
763 struct lttng_live_viewer_stream *metadata_stream)
764 {
765 uint64_t len = 0;
766 int ret;
767 struct lttng_viewer_cmd cmd;
768 struct lttng_viewer_get_metadata rq;
769 struct lttng_viewer_metadata_packet rp;
770 char *data = NULL;
771 ssize_t ret_len;
772
773 if (lttng_live_should_quit()) {
774 ret = -1;
775 goto end;
776 }
777
778 rq.stream_id = htobe64(metadata_stream->id);
779 cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
780 cmd.data_size = sizeof(rq);
781 cmd.cmd_version = 0;
782
783 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
784 if (ret_len < 0) {
785 perror("[error] Error sending cmd");
786 goto error;
787 }
788 assert(ret_len == sizeof(cmd));
789
790 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
791 if (ret_len < 0) {
792 perror("[error] Error sending get_metadata request");
793 goto error;
794 }
795 assert(ret_len == sizeof(rq));
796
797 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
798 if (ret_len == 0) {
799 fprintf(stderr, "[error] Remote side has closed connection\n");
800 goto error;
801 }
802 if (ret_len < 0) {
803 perror("[error] Error receiving metadata response");
804 goto error;
805 }
806 assert(ret_len == sizeof(rp));
807
808 switch (be32toh(rp.status)) {
809 case LTTNG_VIEWER_METADATA_OK:
810 printf_verbose("get_metadata : OK\n");
811 break;
812 case LTTNG_VIEWER_NO_NEW_METADATA:
813 printf_verbose("get_metadata : NO NEW\n");
814 ret = 0;
815 goto end;
816 case LTTNG_VIEWER_METADATA_ERR:
817 printf_verbose("get_metadata : ERR\n");
818 goto error;
819 default:
820 printf_verbose("get_metadata : UNKNOWN\n");
821 goto error;
822 }
823
824 len = be64toh(rp.len);
825 printf_verbose("Writing %" PRIu64" bytes to metadata\n", len);
826 if (len <= 0) {
827 goto error;
828 }
829
830 data = zmalloc(len);
831 if (!data) {
832 perror("relay data zmalloc");
833 goto error;
834 }
835 ret_len = lttng_live_recv(ctx->control_sock, data, len);
836 if (ret_len == 0) {
837 fprintf(stderr, "[error] Remote side has closed connection\n");
838 goto error_free_data;
839 }
840 if (ret_len < 0) {
841 perror("[error] Error receiving trace packet");
842 goto error_free_data;
843 }
844 assert(ret_len == len);
845
846 do {
847 ret_len = fwrite(data, 1, len,
848 metadata_stream->metadata_fp_write);
849 } while (ret_len < 0 && errno == EINTR);
850 if (ret_len < 0) {
851 fprintf(stderr, "[error] Writing in the metadata fp\n");
852 goto error_free_data;
853 }
854 assert(ret_len == len);
855 metadata_stream->metadata_len += len;
856 free(data);
857 ret = len;
858 end:
859 return ret;
860
861 error_free_data:
862 free(data);
863 error:
864 return -1;
865 }
866
867 /*
868 * Return 0 on success, a negative value on error.
869 */
870 static
871 int get_new_metadata(struct lttng_live_ctx *ctx,
872 struct lttng_live_viewer_stream *viewer_stream,
873 char **metadata_buf)
874 {
875 int ret = 0;
876 struct lttng_live_viewer_stream *metadata_stream;
877 size_t size, len_read = 0;;
878
879 metadata_stream = viewer_stream->ctf_trace->metadata_stream;
880 if (!metadata_stream) {
881 fprintf(stderr, "[error] No metadata stream\n");
882 ret = -1;
883 goto error;
884 }
885 metadata_stream->metadata_len = 0;
886 ret = open_metadata_fp_write(metadata_stream, metadata_buf, &size);
887 if (ret < 0) {
888 goto error;
889 }
890
891 do {
892 /*
893 * get_one_metadata_packet returns the number of bytes
894 * received, 0 when we have received everything, a
895 * negative value on error.
896 */
897 ret = get_one_metadata_packet(ctx, metadata_stream);
898 if (ret > 0) {
899 len_read += ret;
900 }
901 if (!len_read) {
902 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
903 }
904 } while (ret > 0 || !len_read);
905
906 if (fclose(metadata_stream->metadata_fp_write))
907 perror("fclose");
908 metadata_stream->metadata_fp_write = NULL;
909
910 error:
911 return ret;
912 }
913
914 /*
915 * Get one index for a stream.
916 *
917 * Returns 0 on success or a negative value on error.
918 */
919 static
920 int get_next_index(struct lttng_live_ctx *ctx,
921 struct lttng_live_viewer_stream *viewer_stream,
922 struct packet_index *index)
923 {
924 struct lttng_viewer_cmd cmd;
925 struct lttng_viewer_get_next_index rq;
926 struct lttng_viewer_index rp;
927 int ret;
928 ssize_t ret_len;
929
930 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
931 cmd.data_size = sizeof(rq);
932 cmd.cmd_version = 0;
933
934 memset(&rq, 0, sizeof(rq));
935 rq.stream_id = htobe64(viewer_stream->id);
936
937 retry:
938 if (lttng_live_should_quit()) {
939 ret = -1;
940 goto end;
941 }
942 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
943 if (ret_len < 0) {
944 perror("[error] Error sending cmd");
945 goto error;
946 }
947 assert(ret_len == sizeof(cmd));
948
949 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
950 if (ret_len < 0) {
951 perror("[error] Error sending get_next_index request");
952 goto error;
953 }
954 assert(ret_len == sizeof(rq));
955
956 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
957 if (ret_len == 0) {
958 fprintf(stderr, "[error] Remote side has closed connection\n");
959 goto error;
960 }
961 if (ret_len < 0) {
962 perror("[error] Error receiving index response");
963 goto error;
964 }
965 assert(ret_len == sizeof(rp));
966
967 rp.flags = be32toh(rp.flags);
968
969 switch (be32toh(rp.status)) {
970 case LTTNG_VIEWER_INDEX_INACTIVE:
971 printf_verbose("get_next_index: inactive\n");
972 memset(index, 0, sizeof(struct packet_index));
973 index->ts_cycles.timestamp_end = be64toh(rp.timestamp_end);
974 break;
975 case LTTNG_VIEWER_INDEX_OK:
976 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
977 rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA);
978 index->offset = be64toh(rp.offset);
979 index->packet_size = be64toh(rp.packet_size);
980 index->content_size = be64toh(rp.content_size);
981 index->ts_cycles.timestamp_begin = be64toh(rp.timestamp_begin);
982 index->ts_cycles.timestamp_end = be64toh(rp.timestamp_end);
983 index->events_discarded = be64toh(rp.events_discarded);
984
985 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
986 ret = append_metadata(ctx, viewer_stream);
987 if (ret)
988 goto error;
989 }
990 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
991 ret = ask_new_streams(ctx);
992 if (ret < 0)
993 goto error;
994 g_hash_table_foreach(ctx->session->ctf_traces,
995 add_traces, ctx->bt_ctx);
996 }
997 break;
998 case LTTNG_VIEWER_INDEX_RETRY:
999 printf_verbose("get_next_index: retry\n");
1000 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
1001 goto retry;
1002 case LTTNG_VIEWER_INDEX_HUP:
1003 printf_verbose("get_next_index: stream hung up\n");
1004 viewer_stream->id = -1ULL;
1005 index->offset = EOF;
1006 ctx->session->stream_count--;
1007 break;
1008 case LTTNG_VIEWER_INDEX_ERR:
1009 fprintf(stderr, "[error] get_next_index: error\n");
1010 goto error;
1011 default:
1012 fprintf(stderr, "[error] get_next_index: unkwown value\n");
1013 goto error;
1014 }
1015 ret = 0;
1016 end:
1017 return ret;
1018
1019 error:
1020 return -1;
1021 }
1022
1023 static
1024 void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index,
1025 int whence)
1026 {
1027 struct ctf_stream_pos *pos;
1028 struct ctf_file_stream *file_stream;
1029 struct packet_index *prev_index = NULL, *cur_index;
1030 struct lttng_live_viewer_stream *viewer_stream;
1031 struct lttng_live_session *session;
1032 int ret;
1033
1034 retry:
1035 pos = ctf_pos(stream_pos);
1036 file_stream = container_of(pos, struct ctf_file_stream, pos);
1037 viewer_stream = (struct lttng_live_viewer_stream *) pos->priv;
1038 session = viewer_stream->session;
1039
1040 switch (pos->packet_index->len) {
1041 case 0:
1042 g_array_set_size(pos->packet_index, 1);
1043 cur_index = &g_array_index(pos->packet_index,
1044 struct packet_index, 0);
1045 break;
1046 case 1:
1047 g_array_set_size(pos->packet_index, 2);
1048 prev_index = &g_array_index(pos->packet_index,
1049 struct packet_index, 0);
1050 cur_index = &g_array_index(pos->packet_index,
1051 struct packet_index, 1);
1052 break;
1053 case 2:
1054 g_array_index(pos->packet_index,
1055 struct packet_index, 0) =
1056 g_array_index(pos->packet_index,
1057 struct packet_index, 1);
1058 prev_index = &g_array_index(pos->packet_index,
1059 struct packet_index, 0);
1060 cur_index = &g_array_index(pos->packet_index,
1061 struct packet_index, 1);
1062 break;
1063 default:
1064 abort();
1065 break;
1066 }
1067 printf_verbose("get_next_index for stream %" PRIu64 "\n", viewer_stream->id);
1068 ret = get_next_index(session->ctx, viewer_stream, cur_index);
1069 if (ret < 0) {
1070 pos->offset = EOF;
1071 if (!lttng_live_should_quit()) {
1072 fprintf(stderr, "[error] get_next_index failed\n");
1073 }
1074 return;
1075 }
1076
1077 pos->packet_size = cur_index->packet_size;
1078 pos->content_size = cur_index->content_size;
1079 pos->mmap_base_offset = 0;
1080 if (cur_index->offset == EOF) {
1081 pos->offset = EOF;
1082 } else {
1083 pos->offset = 0;
1084 }
1085
1086 if (cur_index->content_size == 0) {
1087 file_stream->parent.cycles_timestamp =
1088 cur_index->ts_cycles.timestamp_end;
1089 file_stream->parent.real_timestamp = ctf_get_real_timestamp(
1090 &file_stream->parent,
1091 cur_index->ts_cycles.timestamp_end);
1092 } else {
1093 /* Convert the timestamps and append to the real_index. */
1094 cur_index->ts_real.timestamp_begin = ctf_get_real_timestamp(
1095 &file_stream->parent,
1096 cur_index->ts_cycles.timestamp_begin);
1097 cur_index->ts_real.timestamp_end = ctf_get_real_timestamp(
1098 &file_stream->parent,
1099 cur_index->ts_cycles.timestamp_end);
1100
1101 ctf_update_current_packet_index(&file_stream->parent,
1102 prev_index, cur_index);
1103
1104 file_stream->parent.cycles_timestamp =
1105 cur_index->ts_cycles.timestamp_begin;
1106 file_stream->parent.real_timestamp =
1107 cur_index->ts_real.timestamp_begin;
1108 }
1109
1110 if (pos->packet_size == 0 || pos->offset == EOF) {
1111 goto end;
1112 }
1113
1114 printf_verbose("get_data_packet for stream %" PRIu64 "\n",
1115 viewer_stream->id);
1116 ret = get_data_packet(session->ctx, pos, viewer_stream,
1117 cur_index->offset,
1118 cur_index->packet_size / CHAR_BIT);
1119 if (ret == -2) {
1120 goto retry;
1121 } else if (ret < 0) {
1122 pos->offset = EOF;
1123 if (!lttng_live_should_quit()) {
1124 fprintf(stderr, "[error] get_data_packet failed\n");
1125 }
1126 return;
1127 }
1128
1129 printf_verbose("Index received : packet_size : %" PRIu64
1130 ", offset %" PRIu64 ", content_size %" PRIu64
1131 ", timestamp_end : %" PRIu64 "\n",
1132 cur_index->packet_size, cur_index->offset,
1133 cur_index->content_size,
1134 cur_index->ts_cycles.timestamp_end);
1135
1136 /* update trace_packet_header and stream_packet_context */
1137 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
1138 /* Read packet header */
1139 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1140 if (ret) {
1141 pos->offset = EOF;
1142 fprintf(stderr, "[error] trace packet header read failed\n");
1143 goto end;
1144 }
1145 }
1146 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
1147 /* Read packet context */
1148 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1149 if (ret) {
1150 pos->offset = EOF;
1151 fprintf(stderr, "[error] stream packet context read failed\n");
1152 goto end;
1153 }
1154 }
1155 pos->data_offset = pos->offset;
1156
1157 end:
1158 return;
1159 }
1160
1161 int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx)
1162 {
1163 struct lttng_viewer_cmd cmd;
1164 struct lttng_viewer_create_session_response resp;
1165 int ret;
1166 ssize_t ret_len;
1167
1168 if (lttng_live_should_quit()) {
1169 ret = -1;
1170 goto end;
1171 }
1172
1173 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
1174 cmd.data_size = 0;
1175 cmd.cmd_version = 0;
1176
1177 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
1178 if (ret_len < 0) {
1179 perror("[error] Error sending cmd");
1180 goto error;
1181 }
1182 assert(ret_len == sizeof(cmd));
1183
1184 ret_len = lttng_live_recv(ctx->control_sock, &resp, sizeof(resp));
1185 if (ret_len == 0) {
1186 fprintf(stderr, "[error] Remote side has closed connection\n");
1187 goto error;
1188 }
1189 if (ret_len < 0) {
1190 perror("[error] Error receiving create session reply");
1191 goto error;
1192 }
1193 assert(ret_len == sizeof(resp));
1194
1195 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
1196 fprintf(stderr, "[error] Error creating viewer session\n");
1197 goto error;
1198 }
1199 ret = 0;
1200 end:
1201 return ret;
1202
1203 error:
1204 return -1;
1205 }
1206
1207 static
1208 int del_traces(gpointer key, gpointer value, gpointer user_data)
1209 {
1210 struct bt_context *bt_ctx = user_data;
1211 struct lttng_live_ctf_trace *trace = value;
1212 int ret;
1213
1214 ret = bt_context_remove_trace(bt_ctx, trace->trace_id);
1215 if (ret < 0)
1216 fprintf(stderr, "[error] removing trace from context\n");
1217
1218 /* remove the key/value pair from the HT. */
1219 return 1;
1220 }
1221
1222 static
1223 void add_traces(gpointer key, gpointer value, gpointer user_data)
1224 {
1225 int i, ret;
1226 struct bt_context *bt_ctx = user_data;
1227 struct lttng_live_ctf_trace *trace = value;
1228 struct lttng_live_viewer_stream *stream;
1229 struct bt_mmap_stream *new_mmap_stream;
1230 struct bt_mmap_stream_list mmap_list;
1231 struct lttng_live_ctx *ctx = NULL;
1232 struct bt_trace_descriptor *td;
1233 struct bt_trace_handle *handle;
1234
1235 /*
1236 * We don't know how many streams we will receive for a trace, so
1237 * once we are done receiving the traces, we add all the traces
1238 * received to the bt_context.
1239 * We can receive streams during the attach command or the
1240 * get_new_streams, so we have to make sure not to add multiple
1241 * times the same traces.
1242 * If a trace is already in the context, we just skip this function.
1243 */
1244 if (trace->in_use)
1245 return;
1246
1247 BT_INIT_LIST_HEAD(&mmap_list.head);
1248
1249 for (i = 0; i < trace->streams->len; i++) {
1250 stream = g_ptr_array_index(trace->streams, i);
1251 ctx = stream->session->ctx;
1252
1253 if (!stream->metadata_flag) {
1254 new_mmap_stream = zmalloc(sizeof(struct bt_mmap_stream));
1255 new_mmap_stream->priv = (void *) stream;
1256 new_mmap_stream->fd = -1;
1257 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
1258 } else {
1259 char *metadata_buf = NULL;
1260
1261 /* Get all possible metadata before starting */
1262 ret = get_new_metadata(ctx, stream, &metadata_buf);
1263 if (ret) {
1264 free(metadata_buf);
1265 goto end_free;
1266 }
1267 if (!stream->metadata_len) {
1268 fprintf(stderr, "[error] empty metadata\n");
1269 ret = -1;
1270 free(metadata_buf);
1271 goto end_free;
1272 }
1273
1274 trace->metadata_fp = babeltrace_fmemopen(metadata_buf,
1275 stream->metadata_len, "rb");
1276 if (!trace->metadata_fp) {
1277 perror("Metadata fmemopen");
1278 ret = -1;
1279 free(metadata_buf);
1280 goto end_free;
1281 }
1282 }
1283 }
1284
1285 if (!trace->metadata_fp) {
1286 fprintf(stderr, "[error] No metadata stream opened\n");
1287 goto end_free;
1288 }
1289
1290 ret = bt_context_add_trace(bt_ctx, NULL, "ctf",
1291 ctf_live_packet_seek, &mmap_list, trace->metadata_fp);
1292 if (ret < 0) {
1293 fprintf(stderr, "[error] Error adding trace\n");
1294 goto end_free;
1295 }
1296 trace->metadata_stream->metadata_len = 0;
1297
1298 handle = (struct bt_trace_handle *) g_hash_table_lookup(
1299 bt_ctx->trace_handles,
1300 (gpointer) (unsigned long) ret);
1301 td = handle->td;
1302 trace->handle = handle;
1303 if (bt_ctx->current_iterator) {
1304 bt_iter_add_trace(bt_ctx->current_iterator, td);
1305 }
1306
1307 trace->trace_id = ret;
1308 trace->in_use = 1;
1309
1310 goto end;
1311
1312 end_free:
1313 bt_context_put(bt_ctx);
1314 end:
1315 return;
1316 }
1317
1318 int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
1319 {
1320 struct lttng_viewer_cmd cmd;
1321 struct lttng_viewer_new_streams_request rq;
1322 struct lttng_viewer_new_streams_response rp;
1323 struct lttng_viewer_stream stream;
1324 int ret, i;
1325 ssize_t ret_len;
1326 uint32_t stream_count;
1327
1328 if (lttng_live_should_quit()) {
1329 ret = -1;
1330 goto end;
1331 }
1332
1333 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS);
1334 cmd.data_size = sizeof(rq);
1335 cmd.cmd_version = 0;
1336
1337 memset(&rq, 0, sizeof(rq));
1338 rq.session_id = htobe64(id);
1339
1340 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
1341 if (ret_len < 0) {
1342 perror("[error] Error sending cmd");
1343 goto error;
1344 }
1345 assert(ret_len == sizeof(cmd));
1346
1347 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
1348 if (ret_len < 0) {
1349 perror("[error] Error sending get_new_streams request");
1350 goto error;
1351 }
1352 assert(ret_len == sizeof(rq));
1353
1354 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
1355 if (ret_len == 0) {
1356 fprintf(stderr, "[error] Remote side has closed connection\n");
1357 goto error;
1358 }
1359 if (ret_len < 0) {
1360 perror("[error] Error receiving get_new_streams response");
1361 goto error;
1362 }
1363 assert(ret_len == sizeof(rp));
1364
1365 switch(be32toh(rp.status)) {
1366 case LTTNG_VIEWER_NEW_STREAMS_OK:
1367 break;
1368 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW:
1369 ret = 0;
1370 goto end;
1371 case LTTNG_VIEWER_NEW_STREAMS_HUP:
1372 ret = -LTTNG_VIEWER_NEW_STREAMS_HUP;
1373 goto end;
1374 case LTTNG_VIEWER_NEW_STREAMS_ERR:
1375 fprintf(stderr, "[error] get_new_streams error\n");
1376 goto error;
1377 default:
1378 fprintf(stderr, "[error] Unknown return code %u\n",
1379 be32toh(rp.status));
1380 goto error;
1381 }
1382
1383 stream_count = be32toh(rp.streams_count);
1384 ctx->session->stream_count += stream_count;
1385 /*
1386 * When the session is created but not started, we do an active wait
1387 * until it starts. It allows the viewer to start processing the trace
1388 * as soon as the session starts.
1389 */
1390 if (ctx->session->stream_count == 0) {
1391 ret = 0;
1392 goto end;
1393 }
1394 printf_verbose("Waiting for %" PRIu64 " streams:\n",
1395 ctx->session->stream_count);
1396 ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
1397 ctx->session->stream_count);
1398 for (i = 0; i < stream_count; i++) {
1399 ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
1400 if (ret_len == 0) {
1401 fprintf(stderr, "[error] Remote side has closed connection\n");
1402 goto error;
1403 }
1404 if (ret_len < 0) {
1405 perror("[error] Error receiving stream");
1406 goto error;
1407 }
1408 assert(ret_len == sizeof(stream));
1409 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
1410 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
1411
1412 printf_verbose(" stream %" PRIu64 " : %s/%s\n",
1413 be64toh(stream.id), stream.path_name,
1414 stream.channel_name);
1415 ctx->session->streams[i].id = be64toh(stream.id);
1416 ctx->session->streams[i].session = ctx->session;
1417
1418 ctx->session->streams[i].first_read = 1;
1419 ctx->session->streams[i].mmap_size = 0;
1420
1421 if (be32toh(stream.metadata_flag)) {
1422 ctx->session->streams[i].metadata_flag = 1;
1423 }
1424 ret = lttng_live_ctf_trace_assign(&ctx->session->streams[i],
1425 be64toh(stream.ctf_trace_id));
1426 if (ret < 0) {
1427 goto error;
1428 }
1429
1430 }
1431 ret = 0;
1432 end:
1433 return ret;
1434
1435 error:
1436 return -1;
1437 }
1438
1439 int lttng_live_read(struct lttng_live_ctx *ctx)
1440 {
1441 int ret = -1;
1442 int i;
1443 struct bt_ctf_iter *iter;
1444 const struct bt_ctf_event *event;
1445 struct bt_iter_pos begin_pos;
1446 struct bt_trace_descriptor *td_write;
1447 struct bt_format *fmt_write;
1448 struct ctf_text_stream_pos *sout;
1449 uint64_t id;
1450
1451 ctx->bt_ctx = bt_context_create();
1452 if (!ctx->bt_ctx) {
1453 fprintf(stderr, "[error] bt_context_create allocation\n");
1454 goto end;
1455 }
1456
1457 fmt_write = bt_lookup_format(g_quark_from_static_string("text"));
1458 if (!fmt_write) {
1459 fprintf(stderr, "[error] ctf-text error\n");
1460 goto end;
1461 }
1462
1463 td_write = fmt_write->open_trace(NULL, O_RDWR, NULL, NULL);
1464 if (!td_write) {
1465 fprintf(stderr, "[error] Error opening output trace\n");
1466 goto end_free;
1467 }
1468
1469 sout = container_of(td_write, struct ctf_text_stream_pos,
1470 trace_descriptor);
1471 if (!sout->parent.event_cb) {
1472 goto end_free;
1473 }
1474
1475 ret = lttng_live_create_viewer_session(ctx);
1476 if (ret < 0) {
1477 goto end_free;
1478 }
1479
1480 for (i = 0; i < ctx->session_ids->len; i++) {
1481 id = g_array_index(ctx->session_ids, uint64_t, i);
1482 printf_verbose("Attaching to session %lu\n", id);
1483 ret = lttng_live_attach_session(ctx, id);
1484 printf_verbose("Attaching session returns %d\n", ret);
1485 if (ret < 0) {
1486 if (ret == -LTTNG_VIEWER_ATTACH_UNK) {
1487 fprintf(stderr, "[error] Unknown session ID\n");
1488 }
1489 goto end_free;
1490 }
1491 }
1492
1493 /*
1494 * As long as the session is active, we try to get new streams.
1495 */
1496 for (;;) {
1497 int flags;
1498
1499 if (lttng_live_should_quit()) {
1500 ret = 0;
1501 goto end_free;
1502 }
1503
1504 while (!ctx->session->stream_count) {
1505 if (lttng_live_should_quit()
1506 || ctx->session_ids->len == 0) {
1507 ret = 0;
1508 goto end_free;
1509 }
1510 ret = ask_new_streams(ctx);
1511 if (ret < 0) {
1512 goto end_free;
1513 }
1514 if (!ctx->session->stream_count) {
1515 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
1516 }
1517 }
1518
1519 g_hash_table_foreach(ctx->session->ctf_traces, add_traces,
1520 ctx->bt_ctx);
1521
1522 begin_pos.type = BT_SEEK_BEGIN;
1523 iter = bt_ctf_iter_create(ctx->bt_ctx, &begin_pos, NULL);
1524 if (!iter) {
1525 if (lttng_live_should_quit()) {
1526 ret = 0;
1527 goto end;
1528 }
1529 fprintf(stderr, "[error] Iterator creation error\n");
1530 goto end;
1531 }
1532 for (;;) {
1533 if (lttng_live_should_quit()) {
1534 ret = 0;
1535 goto end_free;
1536 }
1537 event = bt_ctf_iter_read_event_flags(iter, &flags);
1538 if (!(flags & BT_ITER_FLAG_RETRY)) {
1539 if (!event) {
1540 /* End of trace */
1541 break;
1542 }
1543 ret = sout->parent.event_cb(&sout->parent,
1544 event->parent->stream);
1545 if (ret) {
1546 fprintf(stderr, "[error] Writing "
1547 "event failed.\n");
1548 goto end_free;
1549 }
1550 }
1551 ret = bt_iter_next(bt_ctf_get_iter(iter));
1552 if (ret < 0) {
1553 goto end_free;
1554 }
1555 }
1556 bt_ctf_iter_destroy(iter);
1557 g_hash_table_foreach_remove(ctx->session->ctf_traces,
1558 del_traces, ctx->bt_ctx);
1559 ctx->session->stream_count = 0;
1560 }
1561
1562 end_free:
1563 bt_context_put(ctx->bt_ctx);
1564 end:
1565 if (lttng_live_should_quit()) {
1566 ret = 0;
1567 }
1568 return ret;
1569 }
This page took 0.063072 seconds and 4 git commands to generate.