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