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