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