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