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