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/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 if (lttng_live_should_quit()) {
914 ret = -1;
915 goto error;
916 }
917 /*
918 * get_one_metadata_packet returns the number of bytes
919 * received, 0 when we have received everything, a
920 * negative value on error.
921 */
922 ret = get_one_metadata_packet(ctx, metadata_stream);
923 if (ret > 0) {
924 len_read += ret;
925 }
926 if (!len_read) {
927 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
928 }
929 } while (ret > 0 || !len_read);
930
931 if (fclose(metadata_stream->metadata_fp_write))
932 perror("fclose");
933 metadata_stream->metadata_fp_write = NULL;
934
935 error:
936 return ret;
937 }
938
939 /*
940 * Assign the fields from a lttng_viewer_index to a packet_index.
941 */
942 static
943 void lttng_index_to_packet_index(struct lttng_viewer_index *lindex,
944 struct packet_index *pindex)
945 {
946 assert(lindex);
947 assert(pindex);
948
949 pindex->offset = be64toh(lindex->offset);
950 pindex->packet_size = be64toh(lindex->packet_size);
951 pindex->content_size = be64toh(lindex->content_size);
952 pindex->ts_cycles.timestamp_begin = be64toh(lindex->timestamp_begin);
953 pindex->ts_cycles.timestamp_end = be64toh(lindex->timestamp_end);
954 pindex->events_discarded = be64toh(lindex->events_discarded);
955 }
956
957 /*
958 * Get one index for a stream.
959 *
960 * Returns 0 on success or a negative value on error.
961 */
962 static
963 int get_next_index(struct lttng_live_ctx *ctx,
964 struct lttng_live_viewer_stream *viewer_stream,
965 struct packet_index *index, uint64_t *stream_id)
966 {
967 struct lttng_viewer_cmd cmd;
968 struct lttng_viewer_get_next_index rq;
969 int ret;
970 ssize_t ret_len;
971 struct lttng_viewer_index *rp = &viewer_stream->current_index;
972
973 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
974 cmd.data_size = sizeof(rq);
975 cmd.cmd_version = 0;
976
977 memset(&rq, 0, sizeof(rq));
978 rq.stream_id = htobe64(viewer_stream->id);
979
980 retry:
981 if (lttng_live_should_quit()) {
982 ret = -1;
983 goto end;
984 }
985 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
986 if (ret_len < 0) {
987 perror("[error] Error sending cmd");
988 goto error;
989 }
990 assert(ret_len == sizeof(cmd));
991
992 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
993 if (ret_len < 0) {
994 perror("[error] Error sending get_next_index request");
995 goto error;
996 }
997 assert(ret_len == sizeof(rq));
998
999 ret_len = lttng_live_recv(ctx->control_sock, rp, sizeof(*rp));
1000 if (ret_len == 0) {
1001 fprintf(stderr, "[error] Remote side has closed connection\n");
1002 goto error;
1003 }
1004 if (ret_len < 0) {
1005 perror("[error] Error receiving index response");
1006 goto error;
1007 }
1008 assert(ret_len == sizeof(*rp));
1009
1010 rp->flags = be32toh(rp->flags);
1011
1012 switch (be32toh(rp->status)) {
1013 case LTTNG_VIEWER_INDEX_INACTIVE:
1014 printf_verbose("get_next_index: inactive\n");
1015 memset(index, 0, sizeof(struct packet_index));
1016 index->ts_cycles.timestamp_end = be64toh(rp->timestamp_end);
1017 *stream_id = be64toh(rp->stream_id);
1018 break;
1019 case LTTNG_VIEWER_INDEX_OK:
1020 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
1021 rp->flags & LTTNG_VIEWER_FLAG_NEW_METADATA);
1022 lttng_index_to_packet_index(rp, index);
1023 *stream_id = be64toh(rp->stream_id);
1024 viewer_stream->data_pending = 1;
1025
1026 if (rp->flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
1027 ret = append_metadata(ctx, viewer_stream);
1028 if (ret)
1029 goto error;
1030 }
1031 if (rp->flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
1032 printf_verbose("get_next_index: need new streams\n");
1033 ret = ask_new_streams(ctx);
1034 if (ret < 0)
1035 goto error;
1036 else if (ret > 0)
1037 g_hash_table_foreach(ctx->session->ctf_traces,
1038 add_traces, ctx->bt_ctx);
1039 }
1040 break;
1041 case LTTNG_VIEWER_INDEX_RETRY:
1042 printf_verbose("get_next_index: retry\n");
1043 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
1044 goto retry;
1045 case LTTNG_VIEWER_INDEX_HUP:
1046 printf_verbose("get_next_index: stream hung up\n");
1047 viewer_stream->id = -1ULL;
1048 index->offset = EOF;
1049 ctx->session->stream_count--;
1050 break;
1051 case LTTNG_VIEWER_INDEX_ERR:
1052 fprintf(stderr, "[error] get_next_index: error\n");
1053 goto error;
1054 default:
1055 fprintf(stderr, "[error] get_next_index: unkwown value\n");
1056 goto error;
1057 }
1058 ret = 0;
1059 end:
1060 return ret;
1061
1062 error:
1063 return -1;
1064 }
1065
1066 static
1067 void read_packet_header(struct ctf_stream_pos *pos,
1068 struct ctf_file_stream *file_stream)
1069 {
1070 int ret;
1071
1072 /* update trace_packet_header and stream_packet_context */
1073 if (!(pos->prot & PROT_WRITE) &&
1074 file_stream->parent.trace_packet_header) {
1075 /* Read packet header */
1076 ret = generic_rw(&pos->parent,
1077 &file_stream->parent.trace_packet_header->p);
1078 if (ret) {
1079 pos->offset = EOF;
1080 fprintf(stderr, "[error] trace packet "
1081 "header read failed\n");
1082 goto end;
1083 }
1084 }
1085 if (!(pos->prot & PROT_WRITE) &&
1086 file_stream->parent.stream_packet_context) {
1087 /* Read packet context */
1088 ret = generic_rw(&pos->parent,
1089 &file_stream->parent.stream_packet_context->p);
1090 if (ret) {
1091 pos->offset = EOF;
1092 fprintf(stderr, "[error] stream packet "
1093 "context read failed\n");
1094 goto end;
1095 }
1096 }
1097 pos->data_offset = pos->offset;
1098
1099 end:
1100 return;
1101 }
1102
1103 /*
1104 * Handle the seek parameters.
1105 * Returns 0 if the packet_seek can continue, a positive value to
1106 * cleanly exit the packet_seek, a negative value on error.
1107 */
1108 static
1109 int handle_seek_position(size_t index, int whence,
1110 struct lttng_live_viewer_stream *viewer_stream,
1111 struct ctf_stream_pos *pos,
1112 struct ctf_file_stream *file_stream)
1113 {
1114 int ret;
1115
1116 switch (whence) {
1117 case SEEK_CUR:
1118 ret = 0;
1119 goto end;
1120 case SEEK_SET:
1121 /*
1122 * We only allow to seek to 0.
1123 */
1124 if (index != 0) {
1125 fprintf(stderr, "[error] Arbitrary seek in lttng-live "
1126 "trace not supported\n");
1127 pos->offset = EOF;
1128 ret = -1;
1129 goto end;
1130 }
1131
1132 ret = 0;
1133 goto end;
1134
1135 default:
1136 fprintf(stderr, "[error] Invalid seek parameter\n");
1137 assert(0);
1138 }
1139
1140 end:
1141 return ret;
1142 }
1143
1144 static
1145 void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index,
1146 int whence)
1147 {
1148 struct ctf_stream_pos *pos;
1149 struct ctf_file_stream *file_stream;
1150 struct packet_index *prev_index = NULL, *cur_index;
1151 struct lttng_live_viewer_stream *viewer_stream;
1152 struct lttng_live_session *session;
1153 uint64_t stream_id = -1ULL;
1154 int ret;
1155
1156 pos = ctf_pos(stream_pos);
1157 file_stream = container_of(pos, struct ctf_file_stream, pos);
1158 viewer_stream = (struct lttng_live_viewer_stream *) pos->priv;
1159 session = viewer_stream->session;
1160
1161 ret = handle_seek_position(index, whence, viewer_stream, pos,
1162 file_stream);
1163 if (ret != 0) {
1164 return;
1165 }
1166
1167 retry:
1168 switch (pos->packet_index->len) {
1169 case 0:
1170 g_array_set_size(pos->packet_index, 1);
1171 cur_index = &g_array_index(pos->packet_index,
1172 struct packet_index, 0);
1173 break;
1174 case 1:
1175 g_array_set_size(pos->packet_index, 2);
1176 prev_index = &g_array_index(pos->packet_index,
1177 struct packet_index, 0);
1178 cur_index = &g_array_index(pos->packet_index,
1179 struct packet_index, 1);
1180 break;
1181 case 2:
1182 g_array_index(pos->packet_index,
1183 struct packet_index, 0) =
1184 g_array_index(pos->packet_index,
1185 struct packet_index, 1);
1186 prev_index = &g_array_index(pos->packet_index,
1187 struct packet_index, 0);
1188 cur_index = &g_array_index(pos->packet_index,
1189 struct packet_index, 1);
1190 break;
1191 default:
1192 abort();
1193 break;
1194 }
1195
1196 if (viewer_stream->data_pending) {
1197 lttng_index_to_packet_index(&viewer_stream->current_index, cur_index);
1198 } else {
1199 printf_verbose("get_next_index for stream %" PRIu64 "\n", viewer_stream->id);
1200 ret = get_next_index(session->ctx, viewer_stream, cur_index, &stream_id);
1201 if (ret < 0) {
1202 pos->offset = EOF;
1203 if (!lttng_live_should_quit()) {
1204 fprintf(stderr, "[error] get_next_index failed\n");
1205 }
1206 return;
1207 }
1208 printf_verbose("Index received : packet_size : %" PRIu64
1209 ", offset %" PRIu64 ", content_size %" PRIu64
1210 ", timestamp_end : %" PRIu64 "\n",
1211 cur_index->packet_size, cur_index->offset,
1212 cur_index->content_size,
1213 cur_index->ts_cycles.timestamp_end);
1214
1215 }
1216
1217 /*
1218 * On the first time we receive an index, the stream_id needs to
1219 * be set for the stream in order to use it, we don't want any
1220 * data at this stage.
1221 */
1222 if (file_stream->parent.stream_id == -1ULL) {
1223 /*
1224 * Warning: with lttng-tools < 2.4.2, the beacon does not
1225 * contain the real stream ID, it is memset to 0, so this
1226 * might create a problem when a session has multiple
1227 * channels. We can't detect it at this stage, lttng-tools
1228 * has to be upgraded to fix this problem.
1229 */
1230 printf_verbose("Assigning stream_id %" PRIu64 "\n",
1231 stream_id);
1232 file_stream->parent.stream_id = stream_id;
1233 viewer_stream->ctf_stream_id = stream_id;
1234
1235 return;
1236 }
1237
1238 pos->packet_size = cur_index->packet_size;
1239 pos->content_size = cur_index->content_size;
1240 pos->mmap_base_offset = 0;
1241 if (cur_index->offset == EOF) {
1242 pos->offset = EOF;
1243 } else {
1244 pos->offset = 0;
1245 }
1246
1247 if (cur_index->content_size == 0) {
1248 if (file_stream->parent.stream_class) {
1249 file_stream->parent.cycles_timestamp =
1250 cur_index->ts_cycles.timestamp_end;
1251 file_stream->parent.real_timestamp = ctf_get_real_timestamp(
1252 &file_stream->parent,
1253 cur_index->ts_cycles.timestamp_end);
1254 }
1255 } else {
1256 if (file_stream->parent.stream_class) {
1257 /* Convert the timestamps and append to the real_index. */
1258 cur_index->ts_real.timestamp_begin = ctf_get_real_timestamp(
1259 &file_stream->parent,
1260 cur_index->ts_cycles.timestamp_begin);
1261 cur_index->ts_real.timestamp_end = ctf_get_real_timestamp(
1262 &file_stream->parent,
1263 cur_index->ts_cycles.timestamp_end);
1264 }
1265
1266 ctf_update_current_packet_index(&file_stream->parent,
1267 prev_index, cur_index);
1268
1269 file_stream->parent.cycles_timestamp =
1270 cur_index->ts_cycles.timestamp_begin;
1271 file_stream->parent.real_timestamp =
1272 cur_index->ts_real.timestamp_begin;
1273 }
1274
1275 if (pos->packet_size == 0 || pos->offset == EOF) {
1276 goto end;
1277 }
1278
1279 printf_verbose("get_data_packet for stream %" PRIu64 "\n",
1280 viewer_stream->id);
1281 ret = get_data_packet(session->ctx, pos, viewer_stream,
1282 cur_index->offset,
1283 cur_index->packet_size / CHAR_BIT);
1284 if (ret == -2) {
1285 goto retry;
1286 } else if (ret < 0) {
1287 pos->offset = EOF;
1288 if (!lttng_live_should_quit()) {
1289 fprintf(stderr, "[error] get_data_packet failed\n");
1290 }
1291 return;
1292 }
1293 viewer_stream->data_pending = 0;
1294
1295 read_packet_header(pos, file_stream);
1296
1297 end:
1298 return;
1299 }
1300
1301 int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx)
1302 {
1303 struct lttng_viewer_cmd cmd;
1304 struct lttng_viewer_create_session_response resp;
1305 int ret;
1306 ssize_t ret_len;
1307
1308 if (lttng_live_should_quit()) {
1309 ret = -1;
1310 goto end;
1311 }
1312
1313 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
1314 cmd.data_size = 0;
1315 cmd.cmd_version = 0;
1316
1317 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
1318 if (ret_len < 0) {
1319 perror("[error] Error sending cmd");
1320 goto error;
1321 }
1322 assert(ret_len == sizeof(cmd));
1323
1324 ret_len = lttng_live_recv(ctx->control_sock, &resp, sizeof(resp));
1325 if (ret_len == 0) {
1326 fprintf(stderr, "[error] Remote side has closed connection\n");
1327 goto error;
1328 }
1329 if (ret_len < 0) {
1330 perror("[error] Error receiving create session reply");
1331 goto error;
1332 }
1333 assert(ret_len == sizeof(resp));
1334
1335 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
1336 fprintf(stderr, "[error] Error creating viewer session\n");
1337 goto error;
1338 }
1339 ret = 0;
1340 end:
1341 return ret;
1342
1343 error:
1344 return -1;
1345 }
1346
1347 static
1348 int del_traces(gpointer key, gpointer value, gpointer user_data)
1349 {
1350 struct bt_context *bt_ctx = user_data;
1351 struct lttng_live_ctf_trace *trace = value;
1352 int ret;
1353
1354 ret = bt_context_remove_trace(bt_ctx, trace->trace_id);
1355 if (ret < 0)
1356 fprintf(stderr, "[error] removing trace from context\n");
1357
1358 /* remove the key/value pair from the HT. */
1359 return 1;
1360 }
1361
1362 static
1363 void add_traces(gpointer key, gpointer value, gpointer user_data)
1364 {
1365 int i, ret;
1366 struct bt_context *bt_ctx = user_data;
1367 struct lttng_live_ctf_trace *trace = value;
1368 struct lttng_live_viewer_stream *stream;
1369 struct bt_mmap_stream *new_mmap_stream;
1370 struct bt_mmap_stream_list mmap_list;
1371 struct lttng_live_ctx *ctx = NULL;
1372 struct bt_trace_descriptor *td;
1373 struct bt_trace_handle *handle;
1374
1375 /*
1376 * We don't know how many streams we will receive for a trace, so
1377 * once we are done receiving the traces, we add all the traces
1378 * received to the bt_context.
1379 * We can receive streams during the attach command or the
1380 * get_new_streams, so we have to make sure not to add multiple
1381 * times the same traces.
1382 * If a trace is already in the context, we just skip this function.
1383 */
1384 if (trace->in_use)
1385 return;
1386
1387 BT_INIT_LIST_HEAD(&mmap_list.head);
1388
1389 for (i = 0; i < trace->streams->len; i++) {
1390 stream = g_ptr_array_index(trace->streams, i);
1391 ctx = stream->session->ctx;
1392
1393 if (!stream->metadata_flag) {
1394 new_mmap_stream = zmalloc(sizeof(struct bt_mmap_stream));
1395 new_mmap_stream->priv = (void *) stream;
1396 new_mmap_stream->fd = -1;
1397 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
1398 } else {
1399 char *metadata_buf = NULL;
1400
1401 /* Get all possible metadata before starting */
1402 ret = get_new_metadata(ctx, stream, &metadata_buf);
1403 if (ret) {
1404 free(metadata_buf);
1405 goto end_free;
1406 }
1407 if (!stream->metadata_len) {
1408 fprintf(stderr, "[error] empty metadata\n");
1409 ret = -1;
1410 free(metadata_buf);
1411 goto end_free;
1412 }
1413
1414 trace->metadata_fp = babeltrace_fmemopen(metadata_buf,
1415 stream->metadata_len, "rb");
1416 if (!trace->metadata_fp) {
1417 perror("Metadata fmemopen");
1418 ret = -1;
1419 free(metadata_buf);
1420 goto end_free;
1421 }
1422 }
1423 }
1424
1425 if (!trace->metadata_fp) {
1426 fprintf(stderr, "[error] No metadata stream opened\n");
1427 goto end_free;
1428 }
1429
1430 ret = bt_context_add_trace(bt_ctx, NULL, "ctf",
1431 ctf_live_packet_seek, &mmap_list, trace->metadata_fp);
1432 if (ret < 0) {
1433 fprintf(stderr, "[error] Error adding trace\n");
1434 goto end_free;
1435 }
1436 trace->metadata_stream->metadata_len = 0;
1437
1438 handle = (struct bt_trace_handle *) g_hash_table_lookup(
1439 bt_ctx->trace_handles,
1440 (gpointer) (unsigned long) ret);
1441 td = handle->td;
1442 trace->handle = handle;
1443 if (bt_ctx->current_iterator) {
1444 bt_iter_add_trace(bt_ctx->current_iterator, td);
1445 }
1446
1447 trace->trace_id = ret;
1448 trace->in_use = 1;
1449
1450 goto end;
1451
1452 end_free:
1453 bt_context_put(bt_ctx);
1454 end:
1455 return;
1456 }
1457
1458 /*
1459 * Request new streams for a session.
1460 * Returns the number of streams received or a negative value on error.
1461 */
1462 int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
1463 {
1464 struct lttng_viewer_cmd cmd;
1465 struct lttng_viewer_new_streams_request rq;
1466 struct lttng_viewer_new_streams_response rp;
1467 struct lttng_viewer_stream stream;
1468 int ret, i, nb_streams = 0;
1469 ssize_t ret_len;
1470 uint32_t stream_count;
1471
1472 if (lttng_live_should_quit()) {
1473 ret = -1;
1474 goto end;
1475 }
1476
1477 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS);
1478 cmd.data_size = sizeof(rq);
1479 cmd.cmd_version = 0;
1480
1481 memset(&rq, 0, sizeof(rq));
1482 rq.session_id = htobe64(id);
1483
1484 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
1485 if (ret_len < 0) {
1486 perror("[error] Error sending cmd");
1487 goto error;
1488 }
1489 assert(ret_len == sizeof(cmd));
1490
1491 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
1492 if (ret_len < 0) {
1493 perror("[error] Error sending get_new_streams request");
1494 goto error;
1495 }
1496 assert(ret_len == sizeof(rq));
1497
1498 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
1499 if (ret_len == 0) {
1500 fprintf(stderr, "[error] Remote side has closed connection\n");
1501 goto error;
1502 }
1503 if (ret_len < 0) {
1504 perror("[error] Error receiving get_new_streams response");
1505 goto error;
1506 }
1507 assert(ret_len == sizeof(rp));
1508
1509 switch(be32toh(rp.status)) {
1510 case LTTNG_VIEWER_NEW_STREAMS_OK:
1511 break;
1512 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW:
1513 ret = 0;
1514 goto end;
1515 case LTTNG_VIEWER_NEW_STREAMS_HUP:
1516 ret = -LTTNG_VIEWER_NEW_STREAMS_HUP;
1517 goto end;
1518 case LTTNG_VIEWER_NEW_STREAMS_ERR:
1519 fprintf(stderr, "[error] get_new_streams error\n");
1520 goto error;
1521 default:
1522 fprintf(stderr, "[error] Unknown return code %u\n",
1523 be32toh(rp.status));
1524 goto error;
1525 }
1526
1527 stream_count = be32toh(rp.streams_count);
1528 ctx->session->stream_count += stream_count;
1529 /*
1530 * When the session is created but not started, we do an active wait
1531 * until it starts. It allows the viewer to start processing the trace
1532 * as soon as the session starts.
1533 */
1534 if (ctx->session->stream_count == 0) {
1535 ret = 0;
1536 goto end;
1537 }
1538 printf_verbose("Waiting for %" PRIu64 " streams:\n",
1539 ctx->session->stream_count);
1540 ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
1541 ctx->session->stream_count);
1542 for (i = 0; i < stream_count; i++) {
1543 ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
1544 if (ret_len == 0) {
1545 fprintf(stderr, "[error] Remote side has closed connection\n");
1546 goto error;
1547 }
1548 if (ret_len < 0) {
1549 perror("[error] Error receiving stream");
1550 goto error;
1551 }
1552 assert(ret_len == sizeof(stream));
1553 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
1554 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
1555
1556 printf_verbose(" stream %" PRIu64 " : %s/%s\n",
1557 be64toh(stream.id), stream.path_name,
1558 stream.channel_name);
1559 ctx->session->streams[i].id = be64toh(stream.id);
1560 ctx->session->streams[i].session = ctx->session;
1561
1562 ctx->session->streams[i].mmap_size = 0;
1563 ctx->session->streams[i].ctf_stream_id = -1ULL;
1564
1565 if (be32toh(stream.metadata_flag)) {
1566 ctx->session->streams[i].metadata_flag = 1;
1567 }
1568 ret = lttng_live_ctf_trace_assign(&ctx->session->streams[i],
1569 be64toh(stream.ctf_trace_id));
1570 if (ret < 0) {
1571 goto error;
1572 }
1573 nb_streams++;
1574
1575 }
1576 ret = nb_streams;
1577 end:
1578 return ret;
1579
1580 error:
1581 return -1;
1582 }
1583
1584 int lttng_live_read(struct lttng_live_ctx *ctx)
1585 {
1586 int ret = -1;
1587 int i;
1588 struct bt_ctf_iter *iter;
1589 const struct bt_ctf_event *event;
1590 struct bt_iter_pos begin_pos;
1591 struct bt_trace_descriptor *td_write;
1592 struct bt_format *fmt_write;
1593 struct ctf_text_stream_pos *sout;
1594 uint64_t id;
1595
1596 ctx->bt_ctx = bt_context_create();
1597 if (!ctx->bt_ctx) {
1598 fprintf(stderr, "[error] bt_context_create allocation\n");
1599 goto end;
1600 }
1601
1602 fmt_write = bt_lookup_format(g_quark_from_static_string("text"));
1603 if (!fmt_write) {
1604 fprintf(stderr, "[error] ctf-text error\n");
1605 goto end;
1606 }
1607
1608 td_write = fmt_write->open_trace(NULL, O_RDWR, NULL, NULL);
1609 if (!td_write) {
1610 fprintf(stderr, "[error] Error opening output trace\n");
1611 goto end_free;
1612 }
1613
1614 sout = container_of(td_write, struct ctf_text_stream_pos,
1615 trace_descriptor);
1616 if (!sout->parent.event_cb) {
1617 goto end_free;
1618 }
1619
1620 ret = lttng_live_create_viewer_session(ctx);
1621 if (ret < 0) {
1622 goto end_free;
1623 }
1624
1625 for (i = 0; i < ctx->session_ids->len; i++) {
1626 id = g_array_index(ctx->session_ids, uint64_t, i);
1627 printf_verbose("Attaching to session %lu\n", id);
1628 ret = lttng_live_attach_session(ctx, id);
1629 printf_verbose("Attaching session returns %d\n", ret);
1630 if (ret < 0) {
1631 if (ret == -LTTNG_VIEWER_ATTACH_UNK) {
1632 fprintf(stderr, "[error] Unknown session ID\n");
1633 }
1634 goto end_free;
1635 }
1636 }
1637
1638 /*
1639 * As long as the session is active, we try to get new streams.
1640 */
1641 for (;;) {
1642 int flags;
1643
1644 if (lttng_live_should_quit()) {
1645 ret = 0;
1646 goto end_free;
1647 }
1648
1649 while (!ctx->session->stream_count) {
1650 if (lttng_live_should_quit()
1651 || ctx->session_ids->len == 0) {
1652 ret = 0;
1653 goto end_free;
1654 }
1655 ret = ask_new_streams(ctx);
1656 if (ret < 0) {
1657 goto end_free;
1658 }
1659 if (!ctx->session->stream_count) {
1660 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
1661 }
1662 }
1663
1664 g_hash_table_foreach(ctx->session->ctf_traces, add_traces,
1665 ctx->bt_ctx);
1666
1667 begin_pos.type = BT_SEEK_BEGIN;
1668 iter = bt_ctf_iter_create(ctx->bt_ctx, &begin_pos, NULL);
1669 if (!iter) {
1670 if (lttng_live_should_quit()) {
1671 ret = 0;
1672 goto end;
1673 }
1674 fprintf(stderr, "[error] Iterator creation error\n");
1675 goto end;
1676 }
1677 for (;;) {
1678 if (lttng_live_should_quit()) {
1679 ret = 0;
1680 goto end_free;
1681 }
1682 event = bt_ctf_iter_read_event_flags(iter, &flags);
1683 if (!(flags & BT_ITER_FLAG_RETRY)) {
1684 if (!event) {
1685 /* End of trace */
1686 break;
1687 }
1688 ret = sout->parent.event_cb(&sout->parent,
1689 event->parent->stream);
1690 if (ret) {
1691 fprintf(stderr, "[error] Writing "
1692 "event failed.\n");
1693 goto end_free;
1694 }
1695 }
1696 ret = bt_iter_next(bt_ctf_get_iter(iter));
1697 if (ret < 0) {
1698 goto end_free;
1699 }
1700 }
1701 bt_ctf_iter_destroy(iter);
1702 g_hash_table_foreach_remove(ctx->session->ctf_traces,
1703 del_traces, ctx->bt_ctx);
1704 ctx->session->stream_count = 0;
1705 }
1706
1707 end_free:
1708 bt_context_put(ctx->bt_ctx);
1709 end:
1710 if (lttng_live_should_quit()) {
1711 ret = 0;
1712 }
1713 return ret;
1714 }
This page took 0.09092 seconds and 5 git commands to generate.