Fix: missing initializer braces warning
[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 printf_verbose("Create trace ctf_trace_id %" PRIu64 "\n", ctf_trace_id);
394 BT_INIT_LIST_HEAD(&trace->stream_list);
395 g_hash_table_insert(stream->session->ctf_traces,
396 &trace->ctf_trace_id,
397 trace);
398 }
399 if (stream->metadata_flag)
400 trace->metadata_stream = stream;
401
402 assert(!stream->in_trace);
403 stream->in_trace = 1;
404 stream->ctf_trace = trace;
405 bt_list_add(&stream->trace_stream_node, &trace->stream_list);
406
407 return ret;
408 }
409
410 static
411 int open_metadata_fp_write(struct lttng_live_viewer_stream *stream,
412 char **metadata_buf, size_t *size)
413 {
414 int ret = 0;
415
416 stream->metadata_fp_write =
417 babeltrace_open_memstream(metadata_buf, size);
418 if (!stream->metadata_fp_write) {
419 perror("Metadata open_memstream");
420 ret = -1;
421 }
422
423 return ret;
424 }
425
426 int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
427 {
428 struct lttng_viewer_cmd cmd;
429 struct lttng_viewer_attach_session_request rq;
430 struct lttng_viewer_attach_session_response rp;
431 struct lttng_viewer_stream stream;
432 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
433 char cmd_buf[cmd_buf_len];
434 int ret, i;
435 ssize_t ret_len;
436
437 if (lttng_live_should_quit()) {
438 ret = -1;
439 goto end;
440 }
441
442 cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION);
443 cmd.data_size = htobe64((uint64_t) sizeof(rq));
444 cmd.cmd_version = htobe32(0);
445
446 memset(&rq, 0, sizeof(rq));
447 rq.session_id = htobe64(id);
448 // TODO: add cmd line parameter to select seek beginning
449 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
450 rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST);
451
452 /*
453 * Merge the cmd and connection request to prevent a write-write
454 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
455 * second write to be performed quickly in presence of Nagle's algorithm.
456 */
457 memcpy(cmd_buf, &cmd, sizeof(cmd));
458 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
459
460 ret_len = lttng_live_send(ctx->control_sock, cmd_buf, cmd_buf_len);
461 if (ret_len < 0) {
462 perror("[error] Error sending attach command and request");
463 goto error;
464 }
465 assert(ret_len == cmd_buf_len);
466
467 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
468 if (ret_len == 0) {
469 fprintf(stderr, "[error] Remote side has closed connection\n");
470 goto error;
471 }
472 if (ret_len < 0) {
473 perror("[error] Error receiving attach response");
474 goto error;
475 }
476 assert(ret_len == sizeof(rp));
477
478 switch(be32toh(rp.status)) {
479 case LTTNG_VIEWER_ATTACH_OK:
480 break;
481 case LTTNG_VIEWER_ATTACH_UNK:
482 ret = -LTTNG_VIEWER_ATTACH_UNK;
483 goto end;
484 case LTTNG_VIEWER_ATTACH_ALREADY:
485 fprintf(stderr, "[error] There is already a viewer attached to this session\n");
486 goto error;
487 case LTTNG_VIEWER_ATTACH_NOT_LIVE:
488 fprintf(stderr, "[error] Not a live session\n");
489 goto error;
490 case LTTNG_VIEWER_ATTACH_SEEK_ERR:
491 fprintf(stderr, "[error] Wrong seek parameter\n");
492 goto error;
493 default:
494 fprintf(stderr, "[error] Unknown attach return code %u\n",
495 be32toh(rp.status));
496 goto error;
497 }
498 if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) {
499 goto error;
500 }
501
502 ctx->session->stream_count += be32toh(rp.streams_count);
503 /*
504 * When the session is created but not started, we do an active wait
505 * until it starts. It allows the viewer to start processing the trace
506 * as soon as the session starts.
507 */
508 if (ctx->session->stream_count == 0) {
509 ret = 0;
510 goto end;
511 }
512 printf_verbose("Waiting for %d streams:\n",
513 be32toh(rp.streams_count));
514 for (i = 0; i < be32toh(rp.streams_count); i++) {
515 struct lttng_live_viewer_stream *lvstream;
516
517 lvstream = g_new0(struct lttng_live_viewer_stream, 1);
518 ret_len = lttng_live_recv(ctx->control_sock, &stream,
519 sizeof(stream));
520 if (ret_len == 0) {
521 fprintf(stderr, "[error] Remote side has closed connection\n");
522 g_free(lvstream);
523 goto error;
524 }
525 if (ret_len < 0) {
526 perror("[error] Error receiving stream");
527 g_free(lvstream);
528 goto error;
529 }
530 assert(ret_len == sizeof(stream));
531 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
532 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
533
534 printf_verbose(" stream %" PRIu64 " : %s/%s\n",
535 be64toh(stream.id), stream.path_name,
536 stream.channel_name);
537 lvstream->id = be64toh(stream.id);
538 lvstream->session = ctx->session;
539
540 lvstream->mmap_size = 0;
541 lvstream->ctf_stream_id = -1ULL;
542
543 if (be32toh(stream.metadata_flag)) {
544 lvstream->metadata_flag = 1;
545 }
546 ret = lttng_live_ctf_trace_assign(lvstream,
547 be64toh(stream.ctf_trace_id));
548 if (ret < 0) {
549 g_free(lvstream);
550 goto error;
551 }
552 bt_list_add(&lvstream->session_stream_node,
553 &ctx->session->stream_list);
554 }
555 ret = 0;
556 end:
557 return ret;
558
559 error:
560 return -1;
561 }
562
563 /*
564 * Ask the relay for new streams.
565 *
566 * Returns the number of new streams received or a negative value on error.
567 */
568 static
569 int ask_new_streams(struct lttng_live_ctx *ctx)
570 {
571 int i, ret = 0, nb_streams = 0;
572 uint64_t id;
573
574 restart:
575 for (i = 0; i < ctx->session_ids->len; i++) {
576 id = g_array_index(ctx->session_ids, uint64_t, i);
577 ret = lttng_live_get_new_streams(ctx, id);
578 printf_verbose("Asking for new streams returns %d\n", ret);
579 if (lttng_live_should_quit()) {
580 ret = -1;
581 goto end;
582 }
583 if (ret < 0) {
584 if (ret == -LTTNG_VIEWER_NEW_STREAMS_HUP) {
585 printf_verbose("Session %" PRIu64 " closed\n",
586 id);
587 /*
588 * The streams have already been closed during
589 * the reading, so we only need to get rid of
590 * the trace in our internal table of sessions.
591 */
592 g_array_remove_index(ctx->session_ids, i);
593 /*
594 * We can't continue iterating on the g_array
595 * after a remove, we have to start again.
596 */
597 goto restart;
598 } else {
599 ret = -1;
600 goto end;
601 }
602 } else {
603 nb_streams += ret;
604 }
605 }
606 if (ctx->session_ids->len == 0) {
607 /* All sessions are closed. */
608 ret = -1;
609 } else {
610 ret = nb_streams;
611 }
612
613 end:
614 return ret;
615 }
616
617 static
618 int append_metadata(struct lttng_live_ctx *ctx,
619 struct lttng_live_viewer_stream *viewer_stream)
620 {
621 int ret;
622 struct lttng_live_viewer_stream *metadata;
623 char *metadata_buf = NULL;
624
625 if (!viewer_stream->ctf_trace->handle) {
626 printf_verbose("append_metadata: trace handle not ready yet.\n");
627 return 0;
628 }
629
630 printf_verbose("get_next_index: new metadata needed\n");
631 ret = get_new_metadata(ctx, viewer_stream, &metadata_buf);
632 if (ret < 0) {
633 free(metadata_buf);
634 goto error;
635 }
636
637 metadata = viewer_stream->ctf_trace->metadata_stream;
638 metadata->ctf_trace->metadata_fp =
639 babeltrace_fmemopen(metadata_buf,
640 metadata->metadata_len, "rb");
641 if (!metadata->ctf_trace->metadata_fp) {
642 perror("Metadata fmemopen");
643 free(metadata_buf);
644 ret = -1;
645 goto error;
646 }
647 ret = ctf_append_trace_metadata(
648 viewer_stream->ctf_trace->handle->td,
649 metadata->ctf_trace->metadata_fp);
650 /* We accept empty metadata packets */
651 if (ret != 0 && ret != -ENOENT) {
652 fprintf(stderr, "[error] Appending metadata\n");
653 goto error;
654 }
655 ret = 0;
656
657 error:
658 return ret;
659 }
660
661 static
662 int get_data_packet(struct lttng_live_ctx *ctx,
663 struct ctf_stream_pos *pos,
664 struct lttng_live_viewer_stream *stream, uint64_t offset,
665 uint64_t len)
666 {
667 struct lttng_viewer_cmd cmd;
668 struct lttng_viewer_get_packet rq;
669 struct lttng_viewer_trace_packet rp;
670 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
671 char cmd_buf[cmd_buf_len];
672 ssize_t ret_len;
673 int ret;
674
675 retry:
676 if (lttng_live_should_quit()) {
677 ret = -1;
678 goto end;
679 }
680
681 cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
682 cmd.data_size = htobe64((uint64_t) sizeof(rq));
683 cmd.cmd_version = htobe32(0);
684
685 memset(&rq, 0, sizeof(rq));
686 rq.stream_id = htobe64(stream->id);
687 rq.offset = htobe64(offset);
688 rq.len = htobe32(len);
689
690 /*
691 * Merge the cmd and connection request to prevent a write-write
692 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
693 * second write to be performed quickly in presence of Nagle's algorithm.
694 */
695 memcpy(cmd_buf, &cmd, sizeof(cmd));
696 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
697
698 ret_len = lttng_live_send(ctx->control_sock, cmd_buf, cmd_buf_len);
699 if (ret_len < 0) {
700 perror("[error] Error sending get_data_packet cmd and request");
701 goto error;
702 }
703 assert(ret_len == cmd_buf_len);
704
705 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
706 if (ret_len == 0) {
707 fprintf(stderr, "[error] Remote side has closed connection\n");
708 goto error;
709 }
710 if (ret_len < 0) {
711 perror("[error] Error receiving data response");
712 goto error;
713 }
714 if (ret_len != sizeof(rp)) {
715 fprintf(stderr, "[error] get_data_packet: expected %zu"
716 ", received %zd\n", sizeof(rp),
717 ret_len);
718 goto error;
719 }
720
721 rp.flags = be32toh(rp.flags);
722
723 switch (be32toh(rp.status)) {
724 case LTTNG_VIEWER_GET_PACKET_OK:
725 len = be32toh(rp.len);
726 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
727 "\n", len);
728 break;
729 case LTTNG_VIEWER_GET_PACKET_RETRY:
730 /* Unimplemented by relay daemon */
731 printf_verbose("get_data_packet: retry\n");
732 goto error;
733 case LTTNG_VIEWER_GET_PACKET_ERR:
734 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
735 printf_verbose("get_data_packet: new metadata needed\n");
736 ret = append_metadata(ctx, stream);
737 if (ret)
738 goto error;
739 }
740 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
741 printf_verbose("get_data_packet: new streams needed\n");
742 ret = ask_new_streams(ctx);
743 if (ret < 0) {
744 goto error;
745 } else if (ret > 0) {
746 ret = add_traces(ctx);
747 if (ret < 0) {
748 goto error;
749 }
750 }
751 }
752 if (rp.flags & (LTTNG_VIEWER_FLAG_NEW_METADATA
753 | LTTNG_VIEWER_FLAG_NEW_STREAM)) {
754 goto retry;
755 }
756 fprintf(stderr, "[error] get_data_packet: error\n");
757 goto error;
758 case LTTNG_VIEWER_GET_PACKET_EOF:
759 ret = -2;
760 goto end;
761 default:
762 printf_verbose("get_data_packet: unknown\n");
763 goto error;
764 }
765
766 if (len == 0) {
767 goto error;
768 }
769
770 if (len > stream->mmap_size) {
771 uint64_t new_size;
772
773 new_size = max_t(uint64_t, len, stream->mmap_size << 1);
774 if (pos->base_mma) {
775 /* unmap old base */
776 ret = munmap_align(pos->base_mma);
777 if (ret) {
778 perror("[error] Unable to unmap old base");
779 goto error;
780 }
781 pos->base_mma = NULL;
782 }
783 pos->base_mma = mmap_align(new_size,
784 PROT_READ | PROT_WRITE,
785 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
786 if (pos->base_mma == MAP_FAILED) {
787 perror("[error] mmap error");
788 pos->base_mma = NULL;
789 goto error;
790 }
791
792 stream->mmap_size = new_size;
793 printf_verbose("Expanding stream mmap size to %" PRIu64 " bytes\n",
794 stream->mmap_size);
795 }
796
797 ret_len = lttng_live_recv(ctx->control_sock,
798 mmap_align_addr(pos->base_mma), len);
799 if (ret_len == 0) {
800 fprintf(stderr, "[error] Remote side has closed connection\n");
801 goto error;
802 }
803 if (ret_len < 0) {
804 perror("[error] Error receiving trace packet");
805 goto error;
806 }
807 assert(ret_len == len);
808 ret = 0;
809 end:
810 return ret;
811
812 error:
813 return -1;
814 }
815
816 static
817 int get_one_metadata_packet(struct lttng_live_ctx *ctx,
818 struct lttng_live_viewer_stream *metadata_stream)
819 {
820 uint64_t len = 0;
821 int ret;
822 struct lttng_viewer_cmd cmd;
823 struct lttng_viewer_get_metadata rq;
824 struct lttng_viewer_metadata_packet rp;
825 char *data = NULL;
826 ssize_t ret_len;
827 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
828 char cmd_buf[cmd_buf_len];
829
830 if (lttng_live_should_quit()) {
831 ret = -1;
832 goto end;
833 }
834
835 rq.stream_id = htobe64(metadata_stream->id);
836 cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
837 cmd.data_size = htobe64((uint64_t) sizeof(rq));
838 cmd.cmd_version = htobe32(0);
839
840 /*
841 * Merge the cmd and connection request to prevent a write-write
842 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
843 * second write to be performed quickly in presence of Nagle's algorithm.
844 */
845 memcpy(cmd_buf, &cmd, sizeof(cmd));
846 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
847
848 printf_verbose("get_metadata for trace_id: %d, ctf_trace_id: %" PRIu64 "\n",
849 metadata_stream->ctf_trace->trace_id,
850 metadata_stream->ctf_trace->ctf_trace_id);
851 ret_len = lttng_live_send(ctx->control_sock, cmd_buf, cmd_buf_len);
852 if (ret_len < 0) {
853 perror("[error] Error sending get_metadata cmd and request");
854 goto error;
855 }
856 assert(ret_len == cmd_buf_len);
857
858 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
859 if (ret_len == 0) {
860 fprintf(stderr, "[error] Remote side has closed connection\n");
861 goto error;
862 }
863 if (ret_len < 0) {
864 perror("[error] Error receiving metadata response");
865 goto error;
866 }
867 assert(ret_len == sizeof(rp));
868
869 switch (be32toh(rp.status)) {
870 case LTTNG_VIEWER_METADATA_OK:
871 printf_verbose("get_metadata : OK\n");
872 break;
873 case LTTNG_VIEWER_NO_NEW_METADATA:
874 printf_verbose("get_metadata : NO NEW\n");
875 ret = 0;
876 goto end;
877 case LTTNG_VIEWER_METADATA_ERR:
878 printf_verbose("get_metadata : ERR\n");
879 goto error;
880 default:
881 printf_verbose("get_metadata : UNKNOWN\n");
882 goto error;
883 }
884
885 len = be64toh(rp.len);
886 printf_verbose("Writing %" PRIu64" bytes to metadata\n", len);
887 if (len <= 0) {
888 goto error;
889 }
890
891 data = zmalloc(len);
892 if (!data) {
893 perror("relay data zmalloc");
894 goto error;
895 }
896 ret_len = lttng_live_recv(ctx->control_sock, data, len);
897 if (ret_len == 0) {
898 fprintf(stderr, "[error] Remote side has closed connection\n");
899 goto error_free_data;
900 }
901 if (ret_len < 0) {
902 perror("[error] Error receiving trace packet");
903 goto error_free_data;
904 }
905 assert(ret_len == len);
906
907 do {
908 ret_len = fwrite(data, 1, len,
909 metadata_stream->metadata_fp_write);
910 } while (ret_len < 0 && errno == EINTR);
911 if (ret_len < 0) {
912 fprintf(stderr, "[error] Writing in the metadata fp\n");
913 goto error_free_data;
914 }
915 assert(ret_len == len);
916 metadata_stream->metadata_len += len;
917 free(data);
918 ret = len;
919 end:
920 return ret;
921
922 error_free_data:
923 free(data);
924 error:
925 return -1;
926 }
927
928 /*
929 * Return 0 on success, a negative value on error.
930 */
931 static
932 int get_new_metadata(struct lttng_live_ctx *ctx,
933 struct lttng_live_viewer_stream *viewer_stream,
934 char **metadata_buf)
935 {
936 int ret = 0;
937 struct lttng_live_viewer_stream *metadata_stream;
938 size_t size, len_read = 0;
939
940 metadata_stream = viewer_stream->ctf_trace->metadata_stream;
941 if (!metadata_stream) {
942 fprintf(stderr, "[error] No metadata stream\n");
943 ret = -1;
944 goto error;
945 }
946 metadata_stream->metadata_len = 0;
947 ret = open_metadata_fp_write(metadata_stream, metadata_buf, &size);
948 if (ret < 0) {
949 goto error;
950 }
951
952 do {
953 if (lttng_live_should_quit()) {
954 ret = -1;
955 goto error;
956 }
957 /*
958 * get_one_metadata_packet returns the number of bytes
959 * received, 0 when we have received everything, a
960 * negative value on error.
961 */
962 ret = get_one_metadata_packet(ctx, metadata_stream);
963 if (ret > 0) {
964 len_read += ret;
965 }
966 if (!len_read) {
967 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
968 }
969 if (ret < 0) {
970 break; /* Stop on error. */
971 }
972 } while (ret > 0 || !len_read);
973
974 if (babeltrace_close_memstream(metadata_buf, &size,
975 metadata_stream->metadata_fp_write)) {
976 perror("babeltrace_close_memstream");
977 }
978 metadata_stream->metadata_fp_write = NULL;
979
980 error:
981 return ret;
982 }
983
984 /*
985 * Assign the fields from a lttng_viewer_index to a packet_index.
986 */
987 static
988 void lttng_index_to_packet_index(struct lttng_viewer_index *lindex,
989 struct packet_index *pindex)
990 {
991 assert(lindex);
992 assert(pindex);
993
994 pindex->offset = be64toh(lindex->offset);
995 pindex->packet_size = be64toh(lindex->packet_size);
996 pindex->content_size = be64toh(lindex->content_size);
997 pindex->ts_cycles.timestamp_begin = be64toh(lindex->timestamp_begin);
998 pindex->ts_cycles.timestamp_end = be64toh(lindex->timestamp_end);
999 pindex->events_discarded = be64toh(lindex->events_discarded);
1000 }
1001
1002 /*
1003 * Get one index for a stream.
1004 *
1005 * Returns 0 on success or a negative value on error.
1006 */
1007 static
1008 int get_next_index(struct lttng_live_ctx *ctx,
1009 struct lttng_live_viewer_stream *viewer_stream,
1010 struct packet_index *index, uint64_t *stream_id)
1011 {
1012 struct lttng_viewer_cmd cmd;
1013 struct lttng_viewer_get_next_index rq;
1014 int ret;
1015 ssize_t ret_len;
1016 struct lttng_viewer_index *rp = &viewer_stream->current_index;
1017 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
1018 char cmd_buf[cmd_buf_len];
1019
1020 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
1021 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1022 cmd.cmd_version = htobe32(0);
1023
1024 memset(&rq, 0, sizeof(rq));
1025 rq.stream_id = htobe64(viewer_stream->id);
1026
1027 /*
1028 * Merge the cmd and connection request to prevent a write-write
1029 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1030 * second write to be performed quickly in presence of Nagle's algorithm.
1031 */
1032 memcpy(cmd_buf, &cmd, sizeof(cmd));
1033 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
1034 retry:
1035 if (lttng_live_should_quit()) {
1036 ret = -1;
1037 goto end;
1038 }
1039 ret_len = lttng_live_send(ctx->control_sock, &cmd_buf, cmd_buf_len);
1040 if (ret_len < 0) {
1041 perror("[error] Error sending get_next_index cmd and request");
1042 goto error;
1043 }
1044 assert(ret_len == cmd_buf_len);
1045
1046 ret_len = lttng_live_recv(ctx->control_sock, rp, sizeof(*rp));
1047 if (ret_len == 0) {
1048 fprintf(stderr, "[error] Remote side has closed connection\n");
1049 goto error;
1050 }
1051 if (ret_len < 0) {
1052 perror("[error] Error receiving index response");
1053 goto error;
1054 }
1055 assert(ret_len == sizeof(*rp));
1056
1057 rp->flags = be32toh(rp->flags);
1058
1059 switch (be32toh(rp->status)) {
1060 case LTTNG_VIEWER_INDEX_INACTIVE:
1061 printf_verbose("get_next_index: inactive\n");
1062
1063 if (index->ts_cycles.timestamp_end ==
1064 be64toh(rp->timestamp_end)) {
1065 /* Already seen this timestamp. */
1066 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
1067 }
1068
1069 memset(index, 0, sizeof(struct packet_index));
1070 index->ts_cycles.timestamp_end = be64toh(rp->timestamp_end);
1071 *stream_id = be64toh(rp->stream_id);
1072 break;
1073 case LTTNG_VIEWER_INDEX_OK:
1074 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
1075 rp->flags & LTTNG_VIEWER_FLAG_NEW_METADATA);
1076 lttng_index_to_packet_index(rp, index);
1077 *stream_id = be64toh(rp->stream_id);
1078 viewer_stream->data_pending = 1;
1079
1080 if (rp->flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
1081 ret = append_metadata(ctx, viewer_stream);
1082 if (ret)
1083 goto error;
1084 }
1085 if (rp->flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
1086 printf_verbose("get_next_index: need new streams\n");
1087 ret = ask_new_streams(ctx);
1088 if (ret < 0) {
1089 goto error;
1090 } else if (ret > 0) {
1091 ret = add_traces(ctx);
1092 if (ret < 0) {
1093 goto error;
1094 }
1095 }
1096 }
1097 break;
1098 case LTTNG_VIEWER_INDEX_RETRY:
1099 printf_verbose("get_next_index: retry\n");
1100 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
1101 goto retry;
1102 case LTTNG_VIEWER_INDEX_HUP:
1103 printf_verbose("get_next_index: stream hung up\n");
1104 viewer_stream->id = -1ULL;
1105 index->offset = EOF;
1106 ctx->session->stream_count--;
1107 viewer_stream->in_trace = 0;
1108 bt_list_del(&viewer_stream->trace_stream_node);
1109 bt_list_del(&viewer_stream->session_stream_node);
1110 g_free(viewer_stream);
1111 *stream_id = be64toh(rp->stream_id);
1112 break;
1113 case LTTNG_VIEWER_INDEX_ERR:
1114 fprintf(stderr, "[error] get_next_index: error\n");
1115 goto error;
1116 default:
1117 fprintf(stderr, "[error] get_next_index: unkwown value\n");
1118 goto error;
1119 }
1120 ret = 0;
1121 end:
1122 return ret;
1123
1124 error:
1125 return -1;
1126 }
1127
1128 static
1129 void read_packet_header(struct ctf_stream_pos *pos,
1130 struct ctf_file_stream *file_stream)
1131 {
1132 int ret;
1133
1134 /* update trace_packet_header and stream_packet_context */
1135 if (!(pos->prot & PROT_WRITE) &&
1136 file_stream->parent.trace_packet_header) {
1137 /* Read packet header */
1138 ret = generic_rw(&pos->parent,
1139 &file_stream->parent.trace_packet_header->p);
1140 if (ret) {
1141 pos->offset = EOF;
1142 fprintf(stderr, "[error] trace packet "
1143 "header read failed\n");
1144 goto end;
1145 }
1146 }
1147 if (!(pos->prot & PROT_WRITE) &&
1148 file_stream->parent.stream_packet_context) {
1149 /* Read packet context */
1150 ret = generic_rw(&pos->parent,
1151 &file_stream->parent.stream_packet_context->p);
1152 if (ret) {
1153 pos->offset = EOF;
1154 fprintf(stderr, "[error] stream packet "
1155 "context read failed\n");
1156 goto end;
1157 }
1158 }
1159 pos->data_offset = pos->offset;
1160
1161 end:
1162 return;
1163 }
1164
1165 /*
1166 * Handle the seek parameters.
1167 * Returns 0 if the packet_seek can continue, a positive value to
1168 * cleanly exit the packet_seek, a negative value on error.
1169 */
1170 static
1171 int handle_seek_position(size_t index, int whence,
1172 struct lttng_live_viewer_stream *viewer_stream,
1173 struct ctf_stream_pos *pos,
1174 struct ctf_file_stream *file_stream)
1175 {
1176 int ret = 0;
1177
1178 switch (whence) {
1179 case SEEK_CUR:
1180 ret = 0;
1181 goto end;
1182 case SEEK_SET:
1183 /*
1184 * We only allow to seek to 0.
1185 */
1186 if (index != 0) {
1187 fprintf(stderr, "[error] Arbitrary seek in lttng-live "
1188 "trace not supported\n");
1189 pos->offset = EOF;
1190 ret = -1;
1191 goto end;
1192 }
1193
1194 ret = 0;
1195 goto end;
1196
1197 default:
1198 fprintf(stderr, "[error] Invalid seek parameter\n");
1199 assert(0);
1200 }
1201
1202 end:
1203 return ret;
1204 }
1205
1206 static
1207 void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index,
1208 int whence)
1209 {
1210 struct ctf_stream_pos *pos;
1211 struct ctf_file_stream *file_stream;
1212 struct packet_index *prev_index = NULL, *cur_index;
1213 struct lttng_live_viewer_stream *viewer_stream;
1214 struct lttng_live_session *session;
1215 uint64_t stream_id = -1ULL;
1216 int ret;
1217
1218 pos = ctf_pos(stream_pos);
1219 file_stream = container_of(pos, struct ctf_file_stream, pos);
1220 viewer_stream = (struct lttng_live_viewer_stream *) pos->priv;
1221 session = viewer_stream->session;
1222
1223 ret = handle_seek_position(index, whence, viewer_stream, pos,
1224 file_stream);
1225 if (ret != 0) {
1226 ret = -BT_PACKET_SEEK_ERROR;
1227 goto end;
1228 }
1229
1230 retry:
1231 switch (pos->packet_index->len) {
1232 case 0:
1233 g_array_set_size(pos->packet_index, 1);
1234 cur_index = &g_array_index(pos->packet_index,
1235 struct packet_index, 0);
1236 break;
1237 case 1:
1238 g_array_set_size(pos->packet_index, 2);
1239 prev_index = &g_array_index(pos->packet_index,
1240 struct packet_index, 0);
1241 cur_index = &g_array_index(pos->packet_index,
1242 struct packet_index, 1);
1243 break;
1244 case 2:
1245 g_array_index(pos->packet_index,
1246 struct packet_index, 0) =
1247 g_array_index(pos->packet_index,
1248 struct packet_index, 1);
1249 prev_index = &g_array_index(pos->packet_index,
1250 struct packet_index, 0);
1251 cur_index = &g_array_index(pos->packet_index,
1252 struct packet_index, 1);
1253 break;
1254 default:
1255 abort();
1256 break;
1257 }
1258
1259 if (viewer_stream->data_pending) {
1260 lttng_index_to_packet_index(&viewer_stream->current_index, cur_index);
1261 } else {
1262 printf_verbose("get_next_index for stream %" PRIu64 "\n", viewer_stream->id);
1263 ret = get_next_index(session->ctx, viewer_stream, cur_index, &stream_id);
1264 if (ret < 0) {
1265 pos->offset = EOF;
1266 if (!lttng_live_should_quit()) {
1267 fprintf(stderr, "[error] get_next_index failed\n");
1268 }
1269 ret = -BT_PACKET_SEEK_ERROR;
1270 goto end;
1271 }
1272 printf_verbose("Index received : packet_size : %" PRIu64
1273 ", offset %" PRIu64 ", content_size %" PRIu64
1274 ", timestamp_end : %" PRIu64 "\n",
1275 cur_index->packet_size, cur_index->offset,
1276 cur_index->content_size,
1277 cur_index->ts_cycles.timestamp_end);
1278
1279 }
1280
1281 /*
1282 * On the first time we receive an index, the stream_id needs to
1283 * be set for the stream in order to use it, we don't want any
1284 * data at this stage.
1285 */
1286 if (file_stream->parent.stream_id == -1ULL) {
1287 /*
1288 * Warning: with lttng-tools < 2.4.2, the beacon does not
1289 * contain the real stream ID, it is memset to 0, so this
1290 * might create a problem when a session has multiple
1291 * channels. We can't detect it at this stage, lttng-tools
1292 * has to be upgraded to fix this problem.
1293 */
1294 printf_verbose("Assigning stream_id %" PRIu64 "\n",
1295 stream_id);
1296 file_stream->parent.stream_id = stream_id;
1297 viewer_stream->ctf_stream_id = stream_id;
1298
1299 ret = 0;
1300 goto end;
1301 }
1302
1303 pos->packet_size = cur_index->packet_size;
1304 pos->content_size = cur_index->content_size;
1305 pos->mmap_base_offset = 0;
1306 if (cur_index->offset == EOF) {
1307 pos->offset = EOF;
1308 } else {
1309 pos->offset = 0;
1310 }
1311
1312 if (cur_index->content_size == 0) {
1313 /* Beacon packet index */
1314 if (file_stream->parent.stream_class) {
1315 file_stream->parent.cycles_timestamp =
1316 cur_index->ts_cycles.timestamp_end;
1317 file_stream->parent.real_timestamp = ctf_get_real_timestamp(
1318 &file_stream->parent,
1319 cur_index->ts_cycles.timestamp_end);
1320
1321 /*
1322 * Duplicate the data from the previous index, because
1323 * the one we just received is only a beacon with no
1324 * relevant information except the timestamp_end. We
1325 * don't need to keep this timestamp_end because we already
1326 * updated the file_stream timestamps, so we only need
1327 * to keep the last real index data as prev_index. That
1328 * way, we keep the original prev timestamps and
1329 * discarded events counter. This is the same behaviour
1330 * as if we were reading a local trace, we would not
1331 * have fake indexes between real indexes.
1332 */
1333 memcpy(cur_index, prev_index, sizeof(struct packet_index));
1334 }
1335 } else {
1336 /* Real packet index */
1337 if (file_stream->parent.stream_class) {
1338 /* Convert the timestamps and append to the real_index. */
1339 cur_index->ts_real.timestamp_begin = ctf_get_real_timestamp(
1340 &file_stream->parent,
1341 cur_index->ts_cycles.timestamp_begin);
1342 cur_index->ts_real.timestamp_end = ctf_get_real_timestamp(
1343 &file_stream->parent,
1344 cur_index->ts_cycles.timestamp_end);
1345 }
1346
1347 ctf_update_current_packet_index(&file_stream->parent,
1348 prev_index, cur_index);
1349
1350 /*
1351 * We need to check if we are in trace read or called
1352 * from packet indexing. In this last case, the
1353 * collection is not there, so we cannot print the
1354 * timestamps.
1355 */
1356 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
1357 ctf_print_discarded_lost(stderr, &file_stream->parent);
1358 }
1359
1360 file_stream->parent.cycles_timestamp =
1361 cur_index->ts_cycles.timestamp_begin;
1362 file_stream->parent.real_timestamp =
1363 cur_index->ts_real.timestamp_begin;
1364 }
1365
1366 /*
1367 * Flush the output between attempts to grab a packet, thus
1368 * ensuring we flush at least at the periodical timer period.
1369 * This ensures the output remains reactive for interactive users and
1370 * that the output is flushed when redirected to a file by the shell.
1371 */
1372 if (fflush(LTTNG_LIVE_OUTPUT_FP) < 0) {
1373 perror("fflush");
1374 goto end;
1375 }
1376
1377 if (pos->packet_size == 0 || pos->offset == EOF) {
1378 goto end;
1379 }
1380
1381 printf_verbose("get_data_packet for stream %" PRIu64 "\n",
1382 viewer_stream->id);
1383 ret = get_data_packet(session->ctx, pos, viewer_stream,
1384 cur_index->offset,
1385 cur_index->packet_size / CHAR_BIT);
1386 if (ret == -2) {
1387 goto retry;
1388 } else if (ret < 0) {
1389 pos->offset = EOF;
1390 if (!lttng_live_should_quit()) {
1391 fprintf(stderr, "[error] get_data_packet failed\n");
1392 ret = -BT_PACKET_SEEK_ERROR;
1393 } else {
1394 ret = 0;
1395 }
1396 goto end;
1397 }
1398 viewer_stream->data_pending = 0;
1399
1400 read_packet_header(pos, file_stream);
1401 ret = 0;
1402 end:
1403 bt_packet_seek_set_error(ret);
1404 }
1405
1406 int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx)
1407 {
1408 struct lttng_viewer_cmd cmd;
1409 struct lttng_viewer_create_session_response resp;
1410 int ret;
1411 ssize_t ret_len;
1412
1413 if (lttng_live_should_quit()) {
1414 ret = -1;
1415 goto end;
1416 }
1417
1418 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
1419 cmd.data_size = htobe64((uint64_t) 0);
1420 cmd.cmd_version = htobe32(0);
1421
1422 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
1423 if (ret_len < 0) {
1424 perror("[error] Error sending cmd");
1425 goto error;
1426 }
1427 assert(ret_len == sizeof(cmd));
1428
1429 ret_len = lttng_live_recv(ctx->control_sock, &resp, sizeof(resp));
1430 if (ret_len == 0) {
1431 fprintf(stderr, "[error] Remote side has closed connection\n");
1432 goto error;
1433 }
1434 if (ret_len < 0) {
1435 perror("[error] Error receiving create session reply");
1436 goto error;
1437 }
1438 assert(ret_len == sizeof(resp));
1439
1440 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
1441 fprintf(stderr, "[error] Error creating viewer session\n");
1442 goto error;
1443 }
1444 ret = 0;
1445 end:
1446 return ret;
1447
1448 error:
1449 return -1;
1450 }
1451
1452 static
1453 int del_traces(gpointer key, gpointer value, gpointer user_data)
1454 {
1455 struct bt_context *bt_ctx = user_data;
1456 struct lttng_live_ctf_trace *trace = value;
1457 int ret;
1458 struct lttng_live_viewer_stream *lvstream, *tmp;
1459
1460 /*
1461 * We don't have ownership of the live viewer stream, just
1462 * remove them from our list.
1463 */
1464 bt_list_for_each_entry_safe(lvstream, tmp, &trace->stream_list,
1465 trace_stream_node) {
1466 lvstream->in_trace = 0;
1467 bt_list_del(&lvstream->trace_stream_node);
1468 }
1469 if (trace->in_use) {
1470 ret = bt_context_remove_trace(bt_ctx, trace->trace_id);
1471 if (ret < 0)
1472 fprintf(stderr, "[error] removing trace from context\n");
1473 }
1474
1475 /* remove the key/value pair from the HT. */
1476 return 1;
1477 }
1478
1479 static
1480 int add_one_trace(struct lttng_live_ctx *ctx,
1481 struct lttng_live_ctf_trace *trace)
1482 {
1483 int ret;
1484 struct bt_context *bt_ctx = ctx->bt_ctx;
1485 struct lttng_live_viewer_stream *stream;
1486 struct bt_mmap_stream *new_mmap_stream;
1487 struct bt_mmap_stream_list mmap_list;
1488 struct bt_trace_descriptor *td;
1489 struct bt_trace_handle *handle;
1490
1491 printf_verbose("Add one trace ctf_trace_id: %" PRIu64
1492 " (metadata_stream: %p)\n",
1493 trace->ctf_trace_id, trace->metadata_stream);
1494 /*
1495 * We don't know how many streams we will receive for a trace, so
1496 * once we are done receiving the traces, we add all the traces
1497 * received to the bt_context.
1498 * We can receive streams during the attach command or the
1499 * get_new_streams, so we have to make sure not to add multiple
1500 * times the same traces.
1501 * If a trace is already in the context, we just skip this function.
1502 */
1503 if (trace->in_use) {
1504 printf_verbose("Trace already in use\n");
1505 ret = 0;
1506 goto end;
1507 }
1508 /*
1509 * add_one_trace can be called recursively if during the
1510 * bt_context_add_trace call we need to fetch new streams, so we need to
1511 * prevent a recursive call to process our current trace.
1512 */
1513 trace->in_use = 1;
1514
1515 BT_INIT_LIST_HEAD(&mmap_list.head);
1516
1517 bt_list_for_each_entry(stream, &trace->stream_list, trace_stream_node) {
1518 if (!stream->metadata_flag) {
1519 new_mmap_stream = zmalloc(sizeof(struct bt_mmap_stream));
1520 new_mmap_stream->priv = (void *) stream;
1521 new_mmap_stream->fd = -1;
1522 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
1523 } else {
1524 char *metadata_buf = NULL;
1525
1526 /* Get all possible metadata before starting */
1527 ret = get_new_metadata(ctx, stream, &metadata_buf);
1528 if (ret) {
1529 free(metadata_buf);
1530 goto end_free;
1531 }
1532 if (!stream->metadata_len) {
1533 fprintf(stderr, "[error] empty metadata\n");
1534 ret = -1;
1535 free(metadata_buf);
1536 goto end_free;
1537 }
1538
1539 printf_verbose("Metadata stream found\n");
1540 trace->metadata_fp = babeltrace_fmemopen(metadata_buf,
1541 stream->metadata_len, "rb");
1542 if (!trace->metadata_fp) {
1543 perror("Metadata fmemopen");
1544 ret = -1;
1545 free(metadata_buf);
1546 goto end_free;
1547 }
1548 }
1549 }
1550
1551 if (!trace->metadata_fp) {
1552 fprintf(stderr, "[error] No metadata stream opened\n");
1553 ret = -1;
1554 goto end_free;
1555 }
1556
1557 ret = bt_context_add_trace(bt_ctx, NULL, "ctf",
1558 ctf_live_packet_seek, &mmap_list, trace->metadata_fp);
1559 if (ret < 0) {
1560 fprintf(stderr, "[error] Error adding trace\n");
1561 ret = -1;
1562 goto end_free;
1563 }
1564 trace->metadata_stream->metadata_len = 0;
1565
1566 handle = (struct bt_trace_handle *) g_hash_table_lookup(
1567 bt_ctx->trace_handles,
1568 (gpointer) (unsigned long) ret);
1569 td = handle->td;
1570 trace->handle = handle;
1571 if (bt_ctx->current_iterator) {
1572 bt_iter_add_trace(bt_ctx->current_iterator, td);
1573 }
1574
1575 trace->trace_id = ret;
1576 printf_verbose("Trace now in use, id = %d\n", trace->trace_id);
1577
1578 goto end;
1579
1580 end_free:
1581 bt_context_put(bt_ctx);
1582 end:
1583 return ret;
1584 }
1585
1586 /*
1587 * Make sure all the traces we know have a metadata stream or loop on
1588 * ask_new_streams until it is done. This must be called before we call
1589 * add_one_trace.
1590 *
1591 * Return 0 when all known traces have a metadata stream, a negative value
1592 * on error.
1593 */
1594 static
1595 int check_traces_metadata(struct lttng_live_ctx *ctx)
1596 {
1597 int ret;
1598 struct lttng_live_ctf_trace *trace;
1599 GHashTableIter it;
1600 gpointer key;
1601 gpointer value;
1602
1603 retry:
1604 g_hash_table_iter_init(&it, ctx->session->ctf_traces);
1605 while (g_hash_table_iter_next(&it, &key, &value)) {
1606 trace = (struct lttng_live_ctf_trace *) value;
1607 printf_verbose("Check trace %" PRIu64 " metadata\n", trace->ctf_trace_id);
1608 while (!trace->metadata_stream) {
1609 printf_verbose("Waiting for metadata stream\n");
1610 if (lttng_live_should_quit()) {
1611 ret = 0;
1612 goto end;
1613 }
1614 ret = ask_new_streams(ctx);
1615 if (ret < 0) {
1616 goto end;
1617 } else if (ret == 0) {
1618 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
1619 } else {
1620 /*
1621 * If ask_new_stream got streams from a trace we did not know
1622 * about until now, we have to reinitialize the iterator.
1623 */
1624 goto retry;
1625 }
1626 }
1627 }
1628
1629 ret = 0;
1630
1631 end:
1632 printf_verbose("End check traces metadata\n");
1633 return ret;
1634 }
1635
1636 static
1637 int add_traces(struct lttng_live_ctx *ctx)
1638 {
1639 int ret;
1640 struct lttng_live_ctf_trace *trace;
1641 GHashTableIter it;
1642 gpointer key;
1643 gpointer value;
1644 unsigned int nr_traces;
1645
1646 printf_verbose("Begin add traces\n");
1647
1648 retry:
1649 nr_traces = g_hash_table_size(ctx->session->ctf_traces);
1650
1651 ret = check_traces_metadata(ctx);
1652 if (ret < 0) {
1653 goto end;
1654 }
1655
1656 g_hash_table_iter_init(&it, ctx->session->ctf_traces);
1657 while (g_hash_table_iter_next(&it, &key, &value)) {
1658 trace = (struct lttng_live_ctf_trace *) value;
1659 ret = add_one_trace(ctx, trace);
1660 if (ret < 0) {
1661 goto end;
1662 }
1663 /*
1664 * If a new trace got added while we were adding the trace, the
1665 * iterator is invalid and we have to restart.
1666 */
1667 if (g_hash_table_size(ctx->session->ctf_traces) != nr_traces) {
1668 printf_verbose("New trace(s) added during add_one_trace()\n");
1669 printf_verbose("JORAJ: GREP HERE\n");
1670 goto retry;
1671 }
1672 }
1673
1674 ret = 0;
1675
1676 end:
1677 printf_verbose("End add traces\n");
1678 return ret;
1679 }
1680
1681 /*
1682 * Request new streams for a session.
1683 * Returns the number of streams received or a negative value on error.
1684 */
1685 int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
1686 {
1687 struct lttng_viewer_cmd cmd;
1688 struct lttng_viewer_new_streams_request rq;
1689 struct lttng_viewer_new_streams_response rp;
1690 struct lttng_viewer_stream stream;
1691 int ret, i, nb_streams = 0;
1692 ssize_t ret_len;
1693 uint32_t stream_count;
1694 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
1695 char cmd_buf[cmd_buf_len];
1696
1697 if (lttng_live_should_quit()) {
1698 ret = -1;
1699 goto end;
1700 }
1701
1702 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS);
1703 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1704 cmd.cmd_version = htobe32(0);
1705
1706 memset(&rq, 0, sizeof(rq));
1707 rq.session_id = htobe64(id);
1708
1709 /*
1710 * Merge the cmd and connection request to prevent a write-write
1711 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1712 * second write to be performed quickly in presence of Nagle's algorithm.
1713 */
1714 memcpy(cmd_buf, &cmd, sizeof(cmd));
1715 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
1716
1717 ret_len = lttng_live_send(ctx->control_sock, cmd_buf, cmd_buf_len);
1718 if (ret_len < 0) {
1719 perror("[error] Error sending get_new_streams cmd and request");
1720 goto error;
1721 }
1722 assert(ret_len == cmd_buf_len);
1723
1724 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
1725 if (ret_len == 0) {
1726 fprintf(stderr, "[error] Remote side has closed connection\n");
1727 goto error;
1728 }
1729 if (ret_len < 0) {
1730 perror("[error] Error receiving get_new_streams response");
1731 goto error;
1732 }
1733 assert(ret_len == sizeof(rp));
1734
1735 switch(be32toh(rp.status)) {
1736 case LTTNG_VIEWER_NEW_STREAMS_OK:
1737 break;
1738 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW:
1739 ret = 0;
1740 goto end;
1741 case LTTNG_VIEWER_NEW_STREAMS_HUP:
1742 ret = -LTTNG_VIEWER_NEW_STREAMS_HUP;
1743 goto end;
1744 case LTTNG_VIEWER_NEW_STREAMS_ERR:
1745 fprintf(stderr, "[error] get_new_streams error\n");
1746 goto error;
1747 default:
1748 fprintf(stderr, "[error] Unknown return code %u\n",
1749 be32toh(rp.status));
1750 goto error;
1751 }
1752
1753 stream_count = be32toh(rp.streams_count);
1754 ctx->session->stream_count += stream_count;
1755 /*
1756 * When the session is created but not started, we do an active wait
1757 * until it starts. It allows the viewer to start processing the trace
1758 * as soon as the session starts.
1759 */
1760 if (ctx->session->stream_count == 0) {
1761 ret = 0;
1762 goto end;
1763 }
1764 printf_verbose("Waiting for %d streams:\n", stream_count);
1765
1766 for (i = 0; i < stream_count; i++) {
1767 struct lttng_live_viewer_stream *lvstream;
1768
1769 lvstream = g_new0(struct lttng_live_viewer_stream, 1);
1770 ret_len = lttng_live_recv(ctx->control_sock, &stream,
1771 sizeof(stream));
1772 if (ret_len == 0) {
1773 fprintf(stderr, "[error] Remote side has closed connection\n");
1774 g_free(lvstream);
1775 goto error;
1776 }
1777 if (ret_len < 0) {
1778 perror("[error] Error receiving stream");
1779 g_free(lvstream);
1780 goto error;
1781 }
1782 assert(ret_len == sizeof(stream));
1783 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
1784 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
1785
1786 printf_verbose(" stream %" PRIu64 " : %s/%s\n",
1787 be64toh(stream.id), stream.path_name,
1788 stream.channel_name);
1789 lvstream->id = be64toh(stream.id);
1790 lvstream->session = ctx->session;
1791
1792 lvstream->mmap_size = 0;
1793 lvstream->ctf_stream_id = -1ULL;
1794
1795 if (be32toh(stream.metadata_flag)) {
1796 lvstream->metadata_flag = 1;
1797 }
1798 ret = lttng_live_ctf_trace_assign(lvstream,
1799 be64toh(stream.ctf_trace_id));
1800 if (ret < 0) {
1801 g_free(lvstream);
1802 goto error;
1803 }
1804 nb_streams++;
1805 bt_list_add(&lvstream->session_stream_node,
1806 &ctx->session->stream_list);
1807 }
1808 ret = nb_streams;
1809 end:
1810 return ret;
1811
1812 error:
1813 return -1;
1814 }
1815
1816 int lttng_live_read(struct lttng_live_ctx *ctx)
1817 {
1818 int ret = -1;
1819 int i;
1820 struct bt_ctf_iter *iter;
1821 const struct bt_ctf_event *event;
1822 struct bt_iter_pos begin_pos;
1823 struct bt_trace_descriptor *td_write;
1824 struct bt_format *fmt_write;
1825 struct ctf_text_stream_pos *sout;
1826 uint64_t id;
1827
1828 ctx->bt_ctx = bt_context_create();
1829 if (!ctx->bt_ctx) {
1830 fprintf(stderr, "[error] bt_context_create allocation\n");
1831 goto end;
1832 }
1833
1834 fmt_write = bt_lookup_format(g_quark_from_static_string("text"));
1835 if (!fmt_write) {
1836 fprintf(stderr, "[error] ctf-text error\n");
1837 goto end_free;
1838 }
1839
1840 td_write = fmt_write->open_trace(NULL, O_RDWR, NULL, NULL);
1841 if (!td_write) {
1842 fprintf(stderr, "[error] Error opening output trace\n");
1843 goto end_free;
1844 }
1845
1846 sout = container_of(td_write, struct ctf_text_stream_pos,
1847 trace_descriptor);
1848 if (!sout->parent.event_cb) {
1849 goto end_free;
1850 }
1851
1852 ret = lttng_live_create_viewer_session(ctx);
1853 if (ret < 0) {
1854 goto end_free;
1855 }
1856
1857 for (i = 0; i < ctx->session_ids->len; i++) {
1858 id = g_array_index(ctx->session_ids, uint64_t, i);
1859 printf_verbose("Attaching to session %" PRIu64 "\n", id);
1860 ret = lttng_live_attach_session(ctx, id);
1861 printf_verbose("Attaching session returns %d\n", ret);
1862 if (ret < 0) {
1863 if (ret == -LTTNG_VIEWER_ATTACH_UNK) {
1864 fprintf(stderr, "[error] Unknown session ID\n");
1865 }
1866 goto end_free;
1867 }
1868 }
1869
1870 /*
1871 * As long as the session is active, we try to get new streams.
1872 */
1873 for (;;) {
1874 int flags;
1875
1876 if (lttng_live_should_quit()) {
1877 ret = 0;
1878 goto end_free;
1879 }
1880
1881 while (!ctx->session->stream_count) {
1882 if (lttng_live_should_quit()
1883 || ctx->session_ids->len == 0) {
1884 ret = 0;
1885 goto end_free;
1886 }
1887 ret = ask_new_streams(ctx);
1888 if (ret < 0) {
1889 ret = 0;
1890 goto end_free;
1891 }
1892 if (!ctx->session->stream_count) {
1893 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
1894 }
1895 }
1896
1897 ret = add_traces(ctx);
1898 if (ret < 0) {
1899 goto end_free;
1900 }
1901
1902 begin_pos.type = BT_SEEK_BEGIN;
1903 iter = bt_ctf_iter_create(ctx->bt_ctx, &begin_pos, NULL);
1904 if (!iter) {
1905 if (lttng_live_should_quit()) {
1906 ret = 0;
1907 goto end_free;
1908 }
1909 fprintf(stderr, "[error] Iterator creation error\n");
1910 goto end_free;
1911 }
1912 for (;;) {
1913 if (lttng_live_should_quit()) {
1914 ret = 0;
1915 goto end_free;
1916 }
1917 event = bt_ctf_iter_read_event_flags(iter, &flags);
1918 if (!(flags & BT_ITER_FLAG_RETRY)) {
1919 if (!event) {
1920 /* End of trace */
1921 break;
1922 }
1923 ret = sout->parent.event_cb(&sout->parent,
1924 event->parent->stream);
1925 if (ret) {
1926 fprintf(stderr, "[error] Writing "
1927 "event failed.\n");
1928 goto end_free;
1929 }
1930 }
1931 ret = bt_iter_next(bt_ctf_get_iter(iter));
1932 if (ret < 0) {
1933 goto end_free;
1934 }
1935 }
1936 bt_ctf_iter_destroy(iter);
1937 g_hash_table_foreach_remove(ctx->session->ctf_traces,
1938 del_traces, ctx->bt_ctx);
1939 ctx->session->stream_count = 0;
1940 }
1941
1942 end_free:
1943 g_hash_table_foreach_remove(ctx->session->ctf_traces,
1944 del_traces, ctx->bt_ctx);
1945 bt_context_put(ctx->bt_ctx);
1946 end:
1947 if (lttng_live_should_quit()) {
1948 ret = 0;
1949 }
1950 return ret;
1951 }
This page took 0.071203 seconds and 5 git commands to generate.