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