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