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