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