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